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_CFG_STREAM		0x1000
26 #define MXC_JPEG_H_ALIGN		3
27 #define MXC_JPEG_W_ALIGN		3
28 #define MXC_JPEG_MAX_SIZEIMAGE		0xFFFFFC00
29 #define MXC_JPEG_MAX_PLANES		2
30 
31 enum mxc_jpeg_enc_state {
32 	MXC_JPEG_ENCODING	= 0, /* jpeg encode phase */
33 	MXC_JPEG_ENC_CONF	= 1, /* jpeg encoder config phase */
34 };
35 
36 enum mxc_jpeg_mode {
37 	MXC_JPEG_DECODE	= 0, /* jpeg decode mode */
38 	MXC_JPEG_ENCODE	= 1, /* jpeg encode mode */
39 };
40 
41 /**
42  * struct mxc_jpeg_fmt - driver's internal color format data
43  * @name:	format description
44  * @fourcc:	fourcc code, 0 if not applicable
45  * @subsampling: subsampling of jpeg components
46  * @nc:		number of color components
47  * @depth:	number of bits per pixel
48  * @colplanes:	number of color planes (1 for packed formats)
49  * @h_align:	horizontal alignment order (align to 2^h_align)
50  * @v_align:	vertical alignment order (align to 2^v_align)
51  * @flags:	flags describing format applicability
52  * @precision:  jpeg sample precision
53  */
54 struct mxc_jpeg_fmt {
55 	const char				*name;
56 	u32					fourcc;
57 	enum v4l2_jpeg_chroma_subsampling	subsampling;
58 	int					nc;
59 	int					depth;
60 	int					colplanes;
61 	int					h_align;
62 	int					v_align;
63 	u32					flags;
64 	u8					precision;
65 };
66 
67 struct mxc_jpeg_desc {
68 	u32 next_descpt_ptr;
69 	u32 buf_base0;
70 	u32 buf_base1;
71 	u32 line_pitch;
72 	u32 stm_bufbase;
73 	u32 stm_bufsize;
74 	u32 imgsize;
75 	u32 stm_ctrl;
76 } __packed;
77 
78 struct mxc_jpeg_q_data {
79 	const struct mxc_jpeg_fmt	*fmt;
80 	u32				sizeimage[MXC_JPEG_MAX_PLANES];
81 	u32				bytesperline[MXC_JPEG_MAX_PLANES];
82 	int				w;
83 	int				w_adjusted;
84 	int				h;
85 	int				h_adjusted;
86 	unsigned int			sequence;
87 };
88 
89 struct mxc_jpeg_ctx {
90 	struct mxc_jpeg_dev		*mxc_jpeg;
91 	struct mxc_jpeg_q_data		out_q;
92 	struct mxc_jpeg_q_data		cap_q;
93 	struct v4l2_fh			fh;
94 	enum mxc_jpeg_enc_state		enc_state;
95 	unsigned int			slot;
96 	unsigned int			source_change;
97 	bool				header_parsed;
98 	struct v4l2_ctrl_handler	ctrl_handler;
99 	u8				jpeg_quality;
100 };
101 
102 struct mxc_jpeg_slot_data {
103 	bool used;
104 	struct mxc_jpeg_desc *desc; // enc/dec descriptor
105 	struct mxc_jpeg_desc *cfg_desc; // configuration descriptor
106 	void *cfg_stream_vaddr; // configuration bitstream virtual address
107 	unsigned int cfg_stream_size;
108 	dma_addr_t desc_handle;
109 	dma_addr_t cfg_desc_handle; // configuration descriptor dma address
110 	dma_addr_t cfg_stream_handle; // configuration bitstream dma address
111 };
112 
113 struct mxc_jpeg_dev {
114 	spinlock_t			hw_lock; /* hardware access lock */
115 	unsigned int			mode;
116 	struct mutex			lock; /* v4l2 ioctls serialization */
117 	struct clk			*clk_ipg;
118 	struct clk			*clk_per;
119 	struct platform_device		*pdev;
120 	struct device			*dev;
121 	void __iomem			*base_reg;
122 	struct v4l2_device		v4l2_dev;
123 	struct v4l2_m2m_dev		*m2m_dev;
124 	struct video_device		*dec_vdev;
125 	struct mxc_jpeg_slot_data	slot_data[MXC_MAX_SLOTS];
126 	int				num_domains;
127 	struct device			**pd_dev;
128 	struct device_link		**pd_link;
129 };
130 
131 /**
132  * struct mxc_jpeg_sof_comp - JPEG Start Of Frame component fields
133  * @id:				component id
134  * @v:				vertical sampling
135  * @h:				horizontal sampling
136  * @quantization_table_no:	id of quantization table
137  */
138 struct mxc_jpeg_sof_comp {
139 	u8 id;
140 	u8 v :4;
141 	u8 h :4;
142 	u8 quantization_table_no;
143 } __packed;
144 
145 #define MXC_JPEG_MAX_COMPONENTS 4
146 /**
147  * struct mxc_jpeg_sof - JPEG Start Of Frame marker fields
148  * @length:		Start of Frame length
149  * @precision:		precision (bits per pixel per color component)
150  * @height:		image height
151  * @width:		image width
152  * @components_no:	number of color components
153  * @comp:		component fields for each color component
154  */
155 struct mxc_jpeg_sof {
156 	u16 length;
157 	u8 precision;
158 	u16 height, width;
159 	u8 components_no;
160 	struct mxc_jpeg_sof_comp comp[MXC_JPEG_MAX_COMPONENTS];
161 } __packed;
162 
163 /**
164  * struct mxc_jpeg_sos_comp - JPEG Start Of Scan component fields
165  * @id:			component id
166  * @huffman_table_no:	id of the Huffman table
167  */
168 struct mxc_jpeg_sos_comp {
169 	u8 id; /*component id*/
170 	u8 huffman_table_no;
171 } __packed;
172 
173 /**
174  * struct mxc_jpeg_sos - JPEG Start Of Scan marker fields
175  * @length:		Start of Frame length
176  * @components_no:	number of color components
177  * @comp:		SOS component fields for each color component
178  * @ignorable_bytes:	ignorable bytes
179  */
180 struct mxc_jpeg_sos {
181 	u16 length;
182 	u8 components_no;
183 	struct mxc_jpeg_sos_comp comp[MXC_JPEG_MAX_COMPONENTS];
184 	u8 ignorable_bytes[3];
185 } __packed;
186 
187 #endif
188