197 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*3 * i.MX8QXP/i.MX8QM JPEG encoder/decoder v4l2 driver4 *5 * Copyright 2018-2019 NXP6 */7 8#include <media/v4l2-ctrls.h>9#include <media/v4l2-device.h>10#include <media/v4l2-fh.h>11 12#ifndef _MXC_JPEG_CORE_H13#define _MXC_JPEG_CORE_H14 15#define MXC_JPEG_NAME "mxc-jpeg"16#define MXC_JPEG_FMT_TYPE_ENC 017#define MXC_JPEG_FMT_TYPE_RAW 118#define MXC_JPEG_DEFAULT_WIDTH 128019#define MXC_JPEG_DEFAULT_HEIGHT 72020#define MXC_JPEG_DEFAULT_PFMT V4L2_PIX_FMT_BGR2421#define MXC_JPEG_MIN_WIDTH 6422#define MXC_JPEG_MIN_HEIGHT 6423#define MXC_JPEG_MAX_WIDTH 0x200024#define MXC_JPEG_MAX_HEIGHT 0x200025#define MXC_JPEG_MAX_LINE 0x800026#define MXC_JPEG_MAX_CFG_STREAM 0x100027#define MXC_JPEG_H_ALIGN 328#define MXC_JPEG_W_ALIGN 329#define MXC_JPEG_MAX_SIZEIMAGE 0xFFFFFC0030#define MXC_JPEG_MAX_PLANES 231 32enum mxc_jpeg_enc_state {33 MXC_JPEG_ENCODING = 0, /* jpeg encode phase */34 MXC_JPEG_ENC_CONF = 1, /* jpeg encoder config phase */35};36 37enum mxc_jpeg_mode {38 MXC_JPEG_DECODE = 0, /* jpeg decode mode */39 MXC_JPEG_ENCODE = 1, /* jpeg encode mode */40};41 42/**43 * struct mxc_jpeg_fmt - driver's internal color format data44 * @name: format description45 * @fourcc: fourcc code, 0 if not applicable46 * @subsampling: subsampling of jpeg components47 * @nc: number of color components48 * @depth: number of bits per pixel49 * @mem_planes: number of memory planes (1 for packed formats)50 * @comp_planes:number of component planes, which includes the alpha plane (1 to 4).51 * @h_align: horizontal alignment order (align to 2^h_align)52 * @v_align: vertical alignment order (align to 2^v_align)53 * @flags: flags describing format applicability54 * @precision: jpeg sample precision55 * @is_rgb: is an RGB pixel format56 */57struct mxc_jpeg_fmt {58 const char *name;59 u32 fourcc;60 enum v4l2_jpeg_chroma_subsampling subsampling;61 int nc;62 int depth;63 int mem_planes;64 int comp_planes;65 int h_align;66 int v_align;67 u32 flags;68 u8 precision;69 u8 is_rgb;70};71 72struct mxc_jpeg_desc {73 u32 next_descpt_ptr;74 u32 buf_base0;75 u32 buf_base1;76 u32 line_pitch;77 u32 stm_bufbase;78 u32 stm_bufsize;79 u32 imgsize;80 u32 stm_ctrl;81} __packed;82 83struct mxc_jpeg_q_data {84 const struct mxc_jpeg_fmt *fmt;85 u32 sizeimage[MXC_JPEG_MAX_PLANES];86 u32 bytesperline[MXC_JPEG_MAX_PLANES];87 int w;88 int w_adjusted;89 int h;90 int h_adjusted;91 unsigned int sequence;92 struct v4l2_rect crop;93};94 95struct mxc_jpeg_ctx {96 struct mxc_jpeg_dev *mxc_jpeg;97 struct mxc_jpeg_q_data out_q;98 struct mxc_jpeg_q_data cap_q;99 struct v4l2_fh fh;100 enum mxc_jpeg_enc_state enc_state;101 int slot;102 unsigned int source_change;103 bool need_initial_source_change_evt;104 bool header_parsed;105 struct v4l2_ctrl_handler ctrl_handler;106 u8 jpeg_quality;107 struct delayed_work task_timer;108};109 110struct mxc_jpeg_slot_data {111 int slot;112 bool used;113 struct mxc_jpeg_desc *desc; // enc/dec descriptor114 struct mxc_jpeg_desc *cfg_desc; // configuration descriptor115 void *cfg_stream_vaddr; // configuration bitstream virtual address116 unsigned int cfg_stream_size;117 dma_addr_t desc_handle;118 dma_addr_t cfg_desc_handle; // configuration descriptor dma address119 dma_addr_t cfg_stream_handle; // configuration bitstream dma address120};121 122struct mxc_jpeg_dev {123 spinlock_t hw_lock; /* hardware access lock */124 unsigned int mode;125 struct mutex lock; /* v4l2 ioctls serialization */126 struct clk_bulk_data *clks;127 int num_clks;128 struct platform_device *pdev;129 struct device *dev;130 void __iomem *base_reg;131 struct v4l2_device v4l2_dev;132 struct v4l2_m2m_dev *m2m_dev;133 struct video_device *dec_vdev;134 struct mxc_jpeg_slot_data slot_data;135 int num_domains;136 struct device **pd_dev;137 struct device_link **pd_link;138};139 140/**141 * struct mxc_jpeg_sof_comp - JPEG Start Of Frame component fields142 * @id: component id143 * @v: vertical sampling144 * @h: horizontal sampling145 * @quantization_table_no: id of quantization table146 */147struct mxc_jpeg_sof_comp {148 u8 id;149 u8 v :4;150 u8 h :4;151 u8 quantization_table_no;152} __packed;153 154#define MXC_JPEG_MAX_COMPONENTS 4155/**156 * struct mxc_jpeg_sof - JPEG Start Of Frame marker fields157 * @length: Start of Frame length158 * @precision: precision (bits per pixel per color component)159 * @height: image height160 * @width: image width161 * @components_no: number of color components162 * @comp: component fields for each color component163 */164struct mxc_jpeg_sof {165 u16 length;166 u8 precision;167 u16 height, width;168 u8 components_no;169 struct mxc_jpeg_sof_comp comp[MXC_JPEG_MAX_COMPONENTS];170} __packed;171 172/**173 * struct mxc_jpeg_sos_comp - JPEG Start Of Scan component fields174 * @id: component id175 * @huffman_table_no: id of the Huffman table176 */177struct mxc_jpeg_sos_comp {178 u8 id; /*component id*/179 u8 huffman_table_no;180} __packed;181 182/**183 * struct mxc_jpeg_sos - JPEG Start Of Scan marker fields184 * @length: Start of Frame length185 * @components_no: number of color components186 * @comp: SOS component fields for each color component187 * @ignorable_bytes: ignorable bytes188 */189struct mxc_jpeg_sos {190 u16 length;191 u8 components_no;192 struct mxc_jpeg_sos_comp comp[MXC_JPEG_MAX_COMPONENTS];193 u8 ignorable_bytes[3];194} __packed;195 196#endif197