1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * i.MX8QXP/i.MX8QM JPEG encoder/decoder v4l2 driver 4 * 5 * Copyright 2018-2019 NXP 6 */ 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_H 13 #define _MXC_JPEG_CORE_H 14 15 #define MXC_JPEG_NAME "mxc-jpeg" 16 #define MXC_JPEG_FMT_TYPE_ENC 0 17 #define MXC_JPEG_FMT_TYPE_RAW 1 18 #define MXC_JPEG_DEFAULT_WIDTH 1280 19 #define MXC_JPEG_DEFAULT_HEIGHT 720 20 #define MXC_JPEG_DEFAULT_PFMT V4L2_PIX_FMT_BGR24 21 #define MXC_JPEG_MIN_WIDTH 64 22 #define MXC_JPEG_MIN_HEIGHT 64 23 #define MXC_JPEG_MAX_WIDTH 0x2000 24 #define MXC_JPEG_MAX_HEIGHT 0x2000 25 #define MXC_JPEG_MAX_LINE 0x8000 26 #define MXC_JPEG_MAX_CFG_STREAM 0x1000 27 #define MXC_JPEG_H_ALIGN 3 28 #define MXC_JPEG_W_ALIGN 3 29 #define MXC_JPEG_MAX_SIZEIMAGE 0xFFFFFC00 30 #define MXC_JPEG_MAX_PLANES 2 31 #define MXC_JPEG_PATTERN_WIDTH 128 32 #define MXC_JPEG_PATTERN_HEIGHT 64 33 34 enum mxc_jpeg_enc_state { 35 MXC_JPEG_ENCODING = 0, /* jpeg encode phase */ 36 MXC_JPEG_ENC_CONF = 1, /* jpeg encoder config phase */ 37 }; 38 39 enum mxc_jpeg_mode { 40 MXC_JPEG_DECODE = 0, /* jpeg decode mode */ 41 MXC_JPEG_ENCODE = 1, /* jpeg encode mode */ 42 }; 43 44 /** 45 * struct mxc_jpeg_fmt - driver's internal color format data 46 * @name: format description 47 * @fourcc: fourcc code, 0 if not applicable 48 * @subsampling: subsampling of jpeg components 49 * @nc: number of color components 50 * @depth: number of bits per pixel 51 * @mem_planes: number of memory planes (1 for packed formats) 52 * @comp_planes:number of component planes, which includes the alpha plane (1 to 4). 53 * @h_align: horizontal alignment order (align to 2^h_align) 54 * @v_align: vertical alignment order (align to 2^v_align) 55 * @flags: flags describing format applicability 56 * @precision: jpeg sample precision 57 * @is_rgb: is an RGB pixel format 58 */ 59 struct mxc_jpeg_fmt { 60 const char *name; 61 u32 fourcc; 62 enum v4l2_jpeg_chroma_subsampling subsampling; 63 int nc; 64 int depth; 65 int mem_planes; 66 int comp_planes; 67 int h_align; 68 int v_align; 69 u32 flags; 70 u8 precision; 71 u8 is_rgb; 72 }; 73 74 struct mxc_jpeg_desc { 75 u32 next_descpt_ptr; 76 u32 buf_base0; 77 u32 buf_base1; 78 u32 line_pitch; 79 u32 stm_bufbase; 80 u32 stm_bufsize; 81 u32 imgsize; 82 u32 stm_ctrl; 83 } __packed; 84 85 struct mxc_jpeg_q_data { 86 const struct mxc_jpeg_fmt *fmt; 87 u32 sizeimage[MXC_JPEG_MAX_PLANES]; 88 u32 bytesperline[MXC_JPEG_MAX_PLANES]; 89 int w; 90 int w_adjusted; 91 int h; 92 int h_adjusted; 93 unsigned int sequence; 94 struct v4l2_rect crop; 95 }; 96 97 struct mxc_jpeg_ctx { 98 struct mxc_jpeg_dev *mxc_jpeg; 99 struct mxc_jpeg_q_data out_q; 100 struct mxc_jpeg_q_data cap_q; 101 struct v4l2_fh fh; 102 enum mxc_jpeg_enc_state enc_state; 103 int slot; 104 unsigned int source_change; 105 bool need_initial_source_change_evt; 106 bool header_parsed; 107 struct v4l2_ctrl_handler ctrl_handler; 108 u8 jpeg_quality; 109 struct delayed_work task_timer; 110 }; 111 112 struct mxc_jpeg_slot_data { 113 int slot; 114 bool used; 115 struct mxc_jpeg_desc *desc; // enc/dec descriptor 116 struct mxc_jpeg_desc *cfg_desc; // configuration descriptor 117 void *cfg_stream_vaddr; // configuration bitstream virtual address 118 unsigned int cfg_stream_size; 119 dma_addr_t desc_handle; 120 dma_addr_t cfg_desc_handle; // configuration descriptor dma address 121 dma_addr_t cfg_stream_handle; // configuration bitstream dma address 122 dma_addr_t cfg_dec_size; 123 void *cfg_dec_vaddr; 124 dma_addr_t cfg_dec_daddr; 125 }; 126 127 struct mxc_jpeg_dev { 128 spinlock_t hw_lock; /* hardware access lock */ 129 unsigned int mode; 130 struct mutex lock; /* v4l2 ioctls serialization */ 131 struct clk_bulk_data *clks; 132 int num_clks; 133 struct platform_device *pdev; 134 struct device *dev; 135 void __iomem *base_reg; 136 struct v4l2_device v4l2_dev; 137 struct v4l2_m2m_dev *m2m_dev; 138 struct video_device *dec_vdev; 139 struct mxc_jpeg_slot_data slot_data; 140 int num_domains; 141 struct device **pd_dev; 142 struct device_link **pd_link; 143 }; 144 145 /** 146 * struct mxc_jpeg_sof_comp - JPEG Start Of Frame component fields 147 * @id: component id 148 * @v: vertical sampling 149 * @h: horizontal sampling 150 * @quantization_table_no: id of quantization table 151 */ 152 struct mxc_jpeg_sof_comp { 153 u8 id; 154 u8 v :4; 155 u8 h :4; 156 u8 quantization_table_no; 157 } __packed; 158 159 #define MXC_JPEG_MAX_COMPONENTS 4 160 /** 161 * struct mxc_jpeg_sof - JPEG Start Of Frame marker fields 162 * @length: Start of Frame length 163 * @precision: precision (bits per pixel per color component) 164 * @height: image height 165 * @width: image width 166 * @components_no: number of color components 167 * @comp: component fields for each color component 168 */ 169 struct mxc_jpeg_sof { 170 u16 length; 171 u8 precision; 172 u16 height, width; 173 u8 components_no; 174 struct mxc_jpeg_sof_comp comp[MXC_JPEG_MAX_COMPONENTS]; 175 } __packed; 176 177 /** 178 * struct mxc_jpeg_sos_comp - JPEG Start Of Scan component fields 179 * @id: component id 180 * @huffman_table_no: id of the Huffman table 181 */ 182 struct mxc_jpeg_sos_comp { 183 u8 id; /*component id*/ 184 u8 huffman_table_no; 185 } __packed; 186 187 /** 188 * struct mxc_jpeg_sos - JPEG Start Of Scan marker fields 189 * @length: Start of Frame length 190 * @components_no: number of color components 191 * @comp: SOS component fields for each color component 192 * @ignorable_bytes: ignorable bytes 193 */ 194 struct mxc_jpeg_sos { 195 u16 length; 196 u8 components_no; 197 struct mxc_jpeg_sos_comp comp[MXC_JPEG_MAX_COMPONENTS]; 198 u8 ignorable_bytes[3]; 199 } __packed; 200 201 #endif 202