xref: /linux/drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c (revision a713222906e4f77b5fb1b5346d4f5de1adc639b4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * V4L2 driver for the JPEG encoder/decoder from i.MX8QXP/i.MX8QM application
4  * processors.
5  *
6  * The multi-planar buffers API is used.
7  *
8  * Baseline and extended sequential jpeg decoding is supported.
9  * Progressive jpeg decoding is not supported by the IP.
10  * Supports encode and decode of various formats:
11  *     YUV444, YUV422, YUV420, BGR, ABGR, Gray
12  * YUV420 is the only multi-planar format supported.
13  * Minimum resolution is 64 x 64, maximum 8192 x 8192.
14  * To achieve 8192 x 8192, modify in defconfig: CONFIG_CMA_SIZE_MBYTES=320
15  * The alignment requirements for the resolution depend on the format,
16  * multiple of 16 resolutions should work for all formats.
17  * Special workarounds are made in the driver to support NV12 1080p.
18  * When decoding, the driver detects image resolution and pixel format
19  * from the jpeg stream, by parsing the jpeg markers.
20  *
21  * The IP has 4 slots available for context switching, but only slot 0
22  * was fully tested to work. Context switching is not used by the driver.
23  * Each driver instance (context) allocates a slot for itself, but this
24  * is postponed until device_run, to allow unlimited opens.
25  *
26  * The driver submits jobs to the IP by setting up a descriptor for the
27  * used slot, and then validating it. The encoder has an additional descriptor
28  * for the configuration phase. The driver expects FRM_DONE interrupt from
29  * IP to mark the job as finished.
30  *
31  * The decoder IP has some limitations regarding the component ID's,
32  * but the driver works around this by replacing them in the jpeg stream.
33  *
34  * A module parameter is available for debug purpose (jpeg_tracing), to enable
35  * it, enable dynamic debug for this module and:
36  * echo 1 > /sys/module/mxc_jpeg_encdec/parameters/jpeg_tracing
37  *
38  * This is inspired by the drivers/media/platform/samsung/s5p-jpeg driver
39  *
40  * Copyright 2018-2019 NXP
41  */
42 
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/io.h>
46 #include <linux/clk.h>
47 #include <linux/of_platform.h>
48 #include <linux/platform_device.h>
49 #include <linux/slab.h>
50 #include <linux/irqreturn.h>
51 #include <linux/interrupt.h>
52 #include <linux/pm_runtime.h>
53 #include <linux/pm_domain.h>
54 #include <linux/string.h>
55 
56 #include <media/v4l2-jpeg.h>
57 #include <media/v4l2-mem2mem.h>
58 #include <media/v4l2-ioctl.h>
59 #include <media/v4l2-common.h>
60 #include <media/v4l2-event.h>
61 #include <media/videobuf2-dma-contig.h>
62 
63 #include "mxc-jpeg-hw.h"
64 #include "mxc-jpeg.h"
65 
66 static const struct mxc_jpeg_fmt mxc_formats[] = {
67 	{
68 		.name		= "JPEG",
69 		.fourcc		= V4L2_PIX_FMT_JPEG,
70 		.subsampling	= -1,
71 		.nc		= -1,
72 		.mem_planes	= 1,
73 		.comp_planes	= 1,
74 		.flags		= MXC_JPEG_FMT_TYPE_ENC,
75 	},
76 	{
77 		.name		= "BGR", /*BGR packed format*/
78 		.fourcc		= V4L2_PIX_FMT_BGR24,
79 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
80 		.nc		= 3,
81 		.depth		= 24,
82 		.mem_planes	= 1,
83 		.comp_planes	= 1,
84 		.h_align	= 3,
85 		.v_align	= 3,
86 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
87 		.precision	= 8,
88 		.is_rgb		= 1,
89 	},
90 	{
91 		.name		= "BGR 12bit", /*12-bit BGR packed format*/
92 		.fourcc		= V4L2_PIX_FMT_BGR48_12,
93 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
94 		.nc		= 3,
95 		.depth		= 36,
96 		.mem_planes	= 1,
97 		.comp_planes	= 1,
98 		.h_align	= 3,
99 		.v_align	= 3,
100 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
101 		.precision	= 12,
102 		.is_rgb		= 1,
103 	},
104 	{
105 		.name		= "ABGR", /* ABGR packed format */
106 		.fourcc		= V4L2_PIX_FMT_ABGR32,
107 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
108 		.nc		= 4,
109 		.depth		= 32,
110 		.mem_planes	= 1,
111 		.comp_planes	= 1,
112 		.h_align	= 3,
113 		.v_align	= 3,
114 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
115 		.precision	= 8,
116 		.is_rgb		= 1,
117 	},
118 	{
119 		.name		= "ABGR 12bit", /* 12-bit ABGR packed format */
120 		.fourcc		= V4L2_PIX_FMT_ABGR64_12,
121 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
122 		.nc		= 4,
123 		.depth		= 48,
124 		.mem_planes	= 1,
125 		.comp_planes	= 1,
126 		.h_align	= 3,
127 		.v_align	= 3,
128 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
129 		.precision	= 12,
130 		.is_rgb		= 1,
131 	},
132 	{
133 		.name		= "YUV420", /* 1st plane = Y, 2nd plane = UV */
134 		.fourcc		= V4L2_PIX_FMT_NV12M,
135 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_420,
136 		.nc		= 3,
137 		.depth		= 12, /* 6 bytes (4Y + UV) for 4 pixels */
138 		.mem_planes	= 2,
139 		.comp_planes	= 2, /* 1 plane Y, 1 plane UV interleaved */
140 		.h_align	= 4,
141 		.v_align	= 4,
142 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
143 		.precision	= 8,
144 	},
145 	{
146 		.name		= "YUV420", /* 1st plane = Y, 2nd plane = UV */
147 		.fourcc		= V4L2_PIX_FMT_NV12,
148 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_420,
149 		.nc		= 3,
150 		.depth		= 12, /* 6 bytes (4Y + UV) for 4 pixels */
151 		.mem_planes	= 1,
152 		.comp_planes	= 2, /* 1 plane Y, 1 plane UV interleaved */
153 		.h_align	= 4,
154 		.v_align	= 4,
155 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
156 		.precision	= 8,
157 	},
158 	{
159 		.name		= "YUV420 12bit", /* 1st plane = Y, 2nd plane = UV */
160 		.fourcc		= V4L2_PIX_FMT_P012M,
161 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_420,
162 		.nc		= 3,
163 		.depth		= 18, /* 6 x 12 bits (4Y + UV) for 4 pixels */
164 		.mem_planes	= 2,
165 		.comp_planes	= 2, /* 1 plane Y, 1 plane UV interleaved */
166 		.h_align	= 4,
167 		.v_align	= 4,
168 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
169 		.precision	= 12,
170 	},
171 	{
172 		.name		= "YUV420 12bit", /* 1st plane = Y, 2nd plane = UV */
173 		.fourcc		= V4L2_PIX_FMT_P012,
174 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_420,
175 		.nc		= 3,
176 		.depth		= 18, /* 6 x 12 bits (4Y + UV) for 4 pixels */
177 		.mem_planes	= 1,
178 		.comp_planes	= 2, /* 1 plane Y, 1 plane UV interleaved */
179 		.h_align	= 4,
180 		.v_align	= 4,
181 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
182 		.precision	= 12,
183 	},
184 	{
185 		.name		= "YUV422", /* YUYV */
186 		.fourcc		= V4L2_PIX_FMT_YUYV,
187 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_422,
188 		.nc		= 3,
189 		.depth		= 16,
190 		.mem_planes	= 1,
191 		.comp_planes	= 1,
192 		.h_align	= 4,
193 		.v_align	= 3,
194 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
195 		.precision	= 8,
196 	},
197 	{
198 		.name		= "YUV422 12bit", /* YUYV */
199 		.fourcc		= V4L2_PIX_FMT_Y212,
200 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_422,
201 		.nc		= 3,
202 		.depth		= 24,
203 		.mem_planes	= 1,
204 		.comp_planes	= 1,
205 		.h_align	= 4,
206 		.v_align	= 3,
207 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
208 		.precision	= 12,
209 	},
210 	{
211 		.name		= "YUV444", /* YUVYUV */
212 		.fourcc		= V4L2_PIX_FMT_YUV24,
213 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
214 		.nc		= 3,
215 		.depth		= 24,
216 		.mem_planes	= 1,
217 		.comp_planes	= 1,
218 		.h_align	= 3,
219 		.v_align	= 3,
220 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
221 		.precision	= 8,
222 	},
223 	{
224 		.name		= "YUV444 12bit", /* YUVYUV */
225 		.fourcc		= V4L2_PIX_FMT_YUV48_12,
226 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
227 		.nc		= 3,
228 		.depth		= 36,
229 		.mem_planes	= 1,
230 		.comp_planes	= 1,
231 		.h_align	= 3,
232 		.v_align	= 3,
233 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
234 		.precision	= 12,
235 	},
236 	{
237 		.name		= "Gray", /* Gray (Y8/Y12) or Single Comp */
238 		.fourcc		= V4L2_PIX_FMT_GREY,
239 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
240 		.nc		= 1,
241 		.depth		= 8,
242 		.mem_planes	= 1,
243 		.comp_planes	= 1,
244 		.h_align	= 3,
245 		.v_align	= 3,
246 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
247 		.precision	= 8,
248 	},
249 	{
250 		.name		= "Gray 12bit", /* Gray (Y8/Y12) or Single Comp */
251 		.fourcc		= V4L2_PIX_FMT_Y012,
252 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
253 		.nc		= 1,
254 		.depth		= 12,
255 		.mem_planes	= 1,
256 		.comp_planes	= 1,
257 		.h_align	= 3,
258 		.v_align	= 3,
259 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
260 		.precision	= 12,
261 	},
262 };
263 
264 #define MXC_JPEG_NUM_FORMATS ARRAY_SIZE(mxc_formats)
265 
266 static const int mxc_decode_mode = MXC_JPEG_DECODE;
267 static const int mxc_encode_mode = MXC_JPEG_ENCODE;
268 
269 static const struct of_device_id mxc_jpeg_match[] = {
270 	{
271 		.compatible = "nxp,imx8qxp-jpgdec",
272 		.data       = &mxc_decode_mode,
273 	},
274 	{
275 		.compatible = "nxp,imx8qxp-jpgenc",
276 		.data       = &mxc_encode_mode,
277 	},
278 	{ },
279 };
280 
281 /*
282  * default configuration stream, 64x64 yuv422
283  * split by JPEG marker, so it's easier to modify & use
284  */
285 static const unsigned char jpeg_soi[] = {
286 	0xFF, 0xD8
287 };
288 
289 static const unsigned char jpeg_app0[] = {
290 	0xFF, 0xE0,
291 	0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00,
292 	0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01,
293 	0x00, 0x00
294 };
295 
296 static const unsigned char jpeg_app14[] = {
297 	0xFF, 0xEE,
298 	0x00, 0x0E, 0x41, 0x64, 0x6F, 0x62, 0x65,
299 	0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00
300 };
301 
302 static const unsigned char jpeg_dqt[] = {
303 	0xFF, 0xDB,
304 	0x00, 0x84, 0x00, 0x10, 0x0B, 0x0C, 0x0E,
305 	0x0C, 0x0A, 0x10, 0x0E, 0x0D, 0x0E, 0x12,
306 	0x11, 0x10, 0x13, 0x18, 0x28, 0x1A, 0x18,
307 	0x16, 0x16, 0x18, 0x31, 0x23, 0x25, 0x1D,
308 	0x28, 0x3A, 0x33, 0x3D, 0x3C, 0x39, 0x33,
309 	0x38, 0x37, 0x40, 0x48, 0x5C, 0x4E, 0x40,
310 	0x44, 0x57, 0x45, 0x37, 0x38, 0x50, 0x6D,
311 	0x51, 0x57, 0x5F, 0x62, 0x67, 0x68, 0x67,
312 	0x3E, 0x4D, 0x71, 0x79, 0x70, 0x64, 0x78,
313 	0x5C, 0x65, 0x67, 0x63, 0x01, 0x11, 0x12,
314 	0x12, 0x18, 0x15, 0x18, 0x2F, 0x1A, 0x1A,
315 	0x2F, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
316 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
317 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
318 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
319 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
320 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
321 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
322 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63
323 };
324 
325 static const unsigned char jpeg_dqt_extseq[] = {
326 	0xFF, 0xDB,
327 	0x01, 0x04,
328 	0x10,
329 	0x00, 0x80, 0x00, 0x58, 0x00, 0x60, 0x00, 0x70,
330 	0x00, 0x60, 0x00, 0x50, 0x00, 0x80, 0x00, 0x70,
331 	0x00, 0x68, 0x00, 0x70, 0x00, 0x90, 0x00, 0x88,
332 	0x00, 0x80, 0x00, 0x98, 0x00, 0xC0, 0x01, 0x40,
333 	0x00, 0xD0, 0x00, 0xC0, 0x00, 0xB0, 0x00, 0xB0,
334 	0x00, 0xC0, 0x01, 0x88, 0x01, 0x18, 0x01, 0x28,
335 	0x00, 0xE8, 0x01, 0x40, 0x01, 0xD0, 0x01, 0x98,
336 	0x01, 0xE8, 0x01, 0xE0, 0x01, 0xC8, 0x01, 0x98,
337 	0x01, 0xC0, 0x01, 0xB8, 0x02, 0x00, 0x02, 0x40,
338 	0x02, 0xE0, 0x02, 0x70, 0x02, 0x00, 0x02, 0x20,
339 	0x02, 0xB8, 0x02, 0x28, 0x01, 0xB8, 0x01, 0xC0,
340 	0x02, 0x80, 0x03, 0x68, 0x02, 0x88, 0x02, 0xB8,
341 	0x02, 0xF8, 0x03, 0x10, 0x03, 0x38, 0x03, 0x40,
342 	0x03, 0x38, 0x01, 0xF0, 0x02, 0x68, 0x03, 0x88,
343 	0x03, 0xC8, 0x03, 0x80, 0x03, 0x20, 0x03, 0xC0,
344 	0x02, 0xE0, 0x03, 0x28, 0x03, 0x38, 0x03, 0x18,
345 	0x11,
346 	0x00, 0x88, 0x00, 0x90, 0x00, 0x90, 0x00, 0xC0,
347 	0x00, 0xA8, 0x00, 0xC0, 0x01, 0x78, 0x00, 0xD0,
348 	0x00, 0xD0, 0x01, 0x78, 0x03, 0x18, 0x02, 0x10,
349 	0x01, 0xC0, 0x02, 0x10, 0x03, 0x18, 0x03, 0x18,
350 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
351 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
352 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
353 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
354 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
355 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
356 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
357 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
358 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
359 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
360 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
361 	0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18,
362 };
363 
364 static const unsigned char jpeg_sof_maximal[] = {
365 	0xFF, 0xC0,
366 	0x00, 0x14, 0x08, 0x00, 0x40, 0x00, 0x40,
367 	0x04, 0x01, 0x11, 0x00, 0x02, 0x11, 0x01,
368 	0x03, 0x11, 0x01, 0x04, 0x11, 0x01
369 };
370 
371 static const unsigned char jpeg_sof_extseq[] = {
372 	0xFF, 0xC1,
373 	0x00, 0x14, 0x08, 0x00, 0x40, 0x00, 0x40,
374 	0x04, 0x01, 0x11, 0x00, 0x02, 0x11, 0x01,
375 	0x03, 0x11, 0x01, 0x04, 0x11, 0x01
376 };
377 
378 static const unsigned char jpeg_dht[] = {
379 	0xFF, 0xC4,
380 	0x01, 0xA2, 0x00, 0x00, 0x01, 0x05, 0x01,
381 	0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
382 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
383 	0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
384 	0x09, 0x0A, 0x0B, 0x10, 0x00, 0x02, 0x01,
385 	0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05,
386 	0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
387 	0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
388 	0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
389 	0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91,
390 	0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15,
391 	0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72,
392 	0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19,
393 	0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
394 	0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
395 	0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
396 	0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
397 	0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
398 	0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76,
399 	0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85,
400 	0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93,
401 	0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A,
402 	0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
403 	0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
404 	0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4,
405 	0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
406 	0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
407 	0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
408 	0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
409 	0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
410 	0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01,
411 	0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
412 	0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
413 	0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
414 	0x0B, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
415 	0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
416 	0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02,
417 	0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06,
418 	0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13,
419 	0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
420 	0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52,
421 	0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
422 	0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18,
423 	0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A,
424 	0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43,
425 	0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A,
426 	0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
427 	0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
428 	0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77,
429 	0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85,
430 	0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93,
431 	0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A,
432 	0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
433 	0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
434 	0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4,
435 	0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
436 	0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
437 	0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
438 	0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5,
439 	0xF6, 0xF7, 0xF8, 0xF9, 0xFA
440 };
441 
442 static const unsigned char jpeg_dht_extseq[] = {
443 	0xFF, 0xC4,
444 	0x02, 0x2a, 0x00, 0x00, 0x01, 0x05, 0x01,
445 	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
446 	0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
447 	0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
448 	0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
449 	0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
450 	0x04, 0x03, 0x05, 0x05, 0x02, 0x03, 0x02,
451 	0x00, 0x00, 0xbf, 0x01, 0x02, 0x03, 0x00,
452 	0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41,
453 	0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71,
454 	0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
455 	0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
456 	0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a,
457 	0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26,
458 	0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36,
459 	0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
460 	0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54,
461 	0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63,
462 	0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
463 	0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
464 	0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
465 	0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
466 	0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4,
467 	0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2,
468 	0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
469 	0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
470 	0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5,
471 	0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
472 	0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
473 	0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6,
474 	0xf7, 0xf8, 0xf9, 0xfa, 0x0b, 0x0c, 0x0d,
475 	0x0e, 0x1b, 0x1c, 0x1d, 0x1e, 0x2b, 0x2c,
476 	0x2d, 0x2e, 0x3b, 0x3c, 0x3d, 0x3e, 0x4b,
477 	0x4c, 0x4d, 0x4e, 0x5b, 0x5c, 0x5d, 0x5e,
478 	0x6b, 0x6c, 0x6d, 0x6e, 0x7b, 0x7c, 0x7d,
479 	0x7e, 0x8b, 0x8c, 0x8d, 0x8e, 0x9b, 0x9c,
480 	0x9d, 0x9e, 0xab, 0xac, 0xad, 0xae, 0xbb,
481 	0xbc, 0xbd, 0xbe, 0xcb, 0xcc, 0xcd, 0xce,
482 	0xdb, 0xdc, 0xdd, 0xde, 0xeb, 0xec, 0xed,
483 	0xee, 0xfb, 0xfc, 0xfd, 0xfe, 0x01, 0x00,
484 	0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
485 	0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
486 	0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
487 	0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
488 	0x0d, 0x0e, 0x0f, 0x11, 0x00, 0x02, 0x01,
489 	0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05,
490 	0x02, 0x03, 0x02, 0x00, 0x00, 0xbf, 0x01,
491 	0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
492 	0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
493 	0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91,
494 	0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15,
495 	0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72,
496 	0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19,
497 	0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
498 	0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a,
499 	0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
500 	0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
501 	0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67,
502 	0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,
503 	0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85,
504 	0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93,
505 	0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a,
506 	0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
507 	0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
508 	0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4,
509 	0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
510 	0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
511 	0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
512 	0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
513 	0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa,
514 	0x0b, 0x0c, 0x0d, 0x0e, 0x1b, 0x1c, 0x1d,
515 	0x1e, 0x2b, 0x2c, 0x2d, 0x2e, 0x3b, 0x3c,
516 	0x3d, 0x3e, 0x4b, 0x4c, 0x4d, 0x4e, 0x5b,
517 	0x5c, 0x5d, 0x5e, 0x6b, 0x6c, 0x6d, 0x6e,
518 	0x7b, 0x7c, 0x7d, 0x7e, 0x8b, 0x8c, 0x8d,
519 	0x8e, 0x9b, 0x9c, 0x9d, 0x9e, 0xab, 0xac,
520 	0xad, 0xae, 0xbb, 0xbc, 0xbd, 0xbe, 0xcb,
521 	0xcc, 0xcd, 0xce, 0xdb, 0xdc, 0xdd, 0xde,
522 	0xeb, 0xec, 0xed, 0xee, 0xfb, 0xfc, 0xfd,
523 	0xfe,
524 };
525 
526 static const unsigned char jpeg_dri[] = {
527 	0xFF, 0xDD,
528 	0x00, 0x04, 0x00, 0x20
529 };
530 
531 static const unsigned char jpeg_sos_maximal[] = {
532 	0xFF, 0xDA,
533 	0x00, 0x0C, 0x04, 0x01, 0x00, 0x02, 0x11, 0x03,
534 	0x11, 0x04, 0x11, 0x00, 0x3F, 0x00
535 };
536 
537 static const unsigned char jpeg_image_red[] = {
538 	0xF9, 0xFE, 0x8A, 0xFC, 0x34, 0xFD, 0xC4, 0x28,
539 	0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A,
540 	0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0,
541 	0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00,
542 	0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02,
543 	0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28,
544 	0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A,
545 	0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0,
546 	0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00,
547 	0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02,
548 	0x8A, 0x00, 0x28, 0xA0, 0x0F, 0xFF, 0xD0, 0xF9,
549 	0xFE, 0x8A, 0xFC, 0x34, 0xFD, 0xC4, 0x28, 0xA0,
550 	0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00,
551 	0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02,
552 	0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28,
553 	0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A,
554 	0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0,
555 	0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00,
556 	0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02,
557 	0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28,
558 	0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A,
559 	0x00, 0x28, 0xA0, 0x0F
560 };
561 
562 static const unsigned char jpeg_eoi[] = {
563 	0xFF, 0xD9
564 };
565 
566 struct mxc_jpeg_src_buf {
567 	/* common v4l buffer stuff -- must be first */
568 	struct vb2_v4l2_buffer	b;
569 	struct list_head	list;
570 
571 	/* mxc-jpeg specific */
572 	bool			dht_needed;
573 	bool			jpeg_parse_error;
574 	const struct mxc_jpeg_fmt	*fmt;
575 	int			w;
576 	int			h;
577 };
578 
579 static inline struct mxc_jpeg_src_buf *vb2_to_mxc_buf(struct vb2_buffer *vb)
580 {
581 	return container_of(to_vb2_v4l2_buffer(vb),
582 			    struct mxc_jpeg_src_buf, b);
583 }
584 
585 static unsigned int debug;
586 module_param(debug, int, 0644);
587 MODULE_PARM_DESC(debug, "Debug level (0-3)");
588 
589 static unsigned int hw_timeout = 2000;
590 module_param(hw_timeout, int, 0644);
591 MODULE_PARM_DESC(hw_timeout, "MXC JPEG hw timeout, the number of milliseconds");
592 
593 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision);
594 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q);
595 
596 static void _bswap16(u16 *a)
597 {
598 	*a = ((*a & 0x00FF) << 8) | ((*a & 0xFF00) >> 8);
599 }
600 
601 static void print_mxc_buf(struct mxc_jpeg_dev *jpeg, struct vb2_buffer *buf,
602 			  unsigned long len)
603 {
604 	unsigned int plane_no;
605 	u32 dma_addr;
606 	void *vaddr;
607 	unsigned long payload;
608 
609 	if (debug < 3)
610 		return;
611 
612 	for (plane_no = 0; plane_no < buf->num_planes; plane_no++) {
613 		payload = vb2_get_plane_payload(buf, plane_no);
614 		if (len == 0)
615 			len = payload;
616 		dma_addr = vb2_dma_contig_plane_dma_addr(buf, plane_no);
617 		vaddr = vb2_plane_vaddr(buf, plane_no);
618 		v4l2_dbg(3, debug, &jpeg->v4l2_dev,
619 			 "plane %d (vaddr=%p dma_addr=%x payload=%ld):",
620 			  plane_no, vaddr, dma_addr, payload);
621 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
622 			       vaddr, len, false);
623 	}
624 }
625 
626 static inline struct mxc_jpeg_ctx *mxc_jpeg_fh_to_ctx(struct v4l2_fh *fh)
627 {
628 	return container_of(fh, struct mxc_jpeg_ctx, fh);
629 }
630 
631 static int enum_fmt(const struct mxc_jpeg_fmt *mxc_formats, int n,
632 		    struct v4l2_fmtdesc *f, u32 type)
633 {
634 	int i, num = 0;
635 
636 	for (i = 0; i < n; ++i) {
637 		if (mxc_formats[i].flags == type) {
638 			/* index-th format of searched type found ? */
639 			if (num == f->index)
640 				break;
641 			/* Correct type but haven't reached our index yet,
642 			 * just increment per-type index
643 			 */
644 			++num;
645 		}
646 	}
647 
648 	/* Format not found */
649 	if (i >= n)
650 		return -EINVAL;
651 
652 	f->pixelformat = mxc_formats[i].fourcc;
653 
654 	return 0;
655 }
656 
657 static const struct mxc_jpeg_fmt *mxc_jpeg_find_format(u32 pixelformat)
658 {
659 	unsigned int k;
660 
661 	for (k = 0; k < MXC_JPEG_NUM_FORMATS; k++) {
662 		const struct mxc_jpeg_fmt *fmt = &mxc_formats[k];
663 
664 		if (fmt->fourcc == pixelformat)
665 			return fmt;
666 	}
667 	return NULL;
668 }
669 
670 static enum mxc_jpeg_image_format mxc_jpeg_fourcc_to_imgfmt(u32 fourcc)
671 {
672 	switch (fourcc) {
673 	case V4L2_PIX_FMT_GREY:
674 	case V4L2_PIX_FMT_Y012:
675 		return MXC_JPEG_GRAY;
676 	case V4L2_PIX_FMT_YUYV:
677 	case V4L2_PIX_FMT_Y212:
678 		return MXC_JPEG_YUV422;
679 	case V4L2_PIX_FMT_NV12:
680 	case V4L2_PIX_FMT_NV12M:
681 	case V4L2_PIX_FMT_P012:
682 	case V4L2_PIX_FMT_P012M:
683 		return MXC_JPEG_YUV420;
684 	case V4L2_PIX_FMT_YUV24:
685 	case V4L2_PIX_FMT_YUV48_12:
686 		return MXC_JPEG_YUV444;
687 	case V4L2_PIX_FMT_BGR24:
688 	case V4L2_PIX_FMT_BGR48_12:
689 		return MXC_JPEG_BGR;
690 	case V4L2_PIX_FMT_ABGR32:
691 	case V4L2_PIX_FMT_ABGR64_12:
692 		return MXC_JPEG_ABGR;
693 	default:
694 		return MXC_JPEG_INVALID;
695 	}
696 }
697 
698 static struct mxc_jpeg_q_data *mxc_jpeg_get_q_data(struct mxc_jpeg_ctx *ctx,
699 						   enum v4l2_buf_type type)
700 {
701 	if (V4L2_TYPE_IS_OUTPUT(type))
702 		return &ctx->out_q;
703 	return &ctx->cap_q;
704 }
705 
706 static void mxc_jpeg_addrs(struct mxc_jpeg_desc *desc,
707 			   struct vb2_buffer *raw_buf,
708 			   struct vb2_buffer *jpeg_buf, int offset)
709 {
710 	int img_fmt = desc->stm_ctrl & STM_CTRL_IMAGE_FORMAT_MASK;
711 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(raw_buf->vb2_queue);
712 	struct mxc_jpeg_q_data *q_data;
713 
714 	q_data = mxc_jpeg_get_q_data(ctx, raw_buf->type);
715 	desc->buf_base0 = vb2_dma_contig_plane_dma_addr(raw_buf, 0);
716 	desc->buf_base1 = 0;
717 	if (img_fmt == STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV420)) {
718 		if (raw_buf->num_planes == 2)
719 			desc->buf_base1 = vb2_dma_contig_plane_dma_addr(raw_buf, 1);
720 		else
721 			desc->buf_base1 = desc->buf_base0 + q_data->sizeimage[0];
722 	}
723 	desc->stm_bufbase = vb2_dma_contig_plane_dma_addr(jpeg_buf, 0) +
724 		offset;
725 }
726 
727 static bool mxc_jpeg_is_extended_sequential(const struct mxc_jpeg_fmt *fmt)
728 {
729 	if (!fmt || !(fmt->flags & MXC_JPEG_FMT_TYPE_RAW))
730 		return false;
731 
732 	if (fmt->precision > 8)
733 		return true;
734 
735 	return false;
736 }
737 
738 static void notify_eos(struct mxc_jpeg_ctx *ctx)
739 {
740 	const struct v4l2_event ev = {
741 		.type = V4L2_EVENT_EOS
742 	};
743 
744 	dev_dbg(ctx->mxc_jpeg->dev, "Notify app event EOS reached");
745 	v4l2_event_queue_fh(&ctx->fh, &ev);
746 }
747 
748 static void notify_src_chg(struct mxc_jpeg_ctx *ctx)
749 {
750 	const struct v4l2_event ev = {
751 		.type = V4L2_EVENT_SOURCE_CHANGE,
752 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
753 	};
754 
755 	dev_dbg(ctx->mxc_jpeg->dev, "Notify app event SRC_CH_RESOLUTION");
756 	v4l2_event_queue_fh(&ctx->fh, &ev);
757 }
758 
759 static int mxc_get_free_slot(struct mxc_jpeg_slot_data *slot_data)
760 {
761 	if (!slot_data->used)
762 		return slot_data->slot;
763 	return -1;
764 }
765 
766 static void mxc_jpeg_free_slot_data(struct mxc_jpeg_dev *jpeg)
767 {
768 	/* free descriptor for decoding/encoding phase */
769 	dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc),
770 			  jpeg->slot_data.desc,
771 			  jpeg->slot_data.desc_handle);
772 	jpeg->slot_data.desc = NULL;
773 	jpeg->slot_data.desc_handle = 0;
774 
775 	/* free descriptor for encoder configuration phase / decoder DHT */
776 	dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc),
777 			  jpeg->slot_data.cfg_desc,
778 			  jpeg->slot_data.cfg_desc_handle);
779 	jpeg->slot_data.cfg_desc_handle = 0;
780 	jpeg->slot_data.cfg_desc = NULL;
781 
782 	/* free configuration stream */
783 	dma_free_coherent(jpeg->dev, MXC_JPEG_MAX_CFG_STREAM,
784 			  jpeg->slot_data.cfg_stream_vaddr,
785 			  jpeg->slot_data.cfg_stream_handle);
786 	jpeg->slot_data.cfg_stream_vaddr = NULL;
787 	jpeg->slot_data.cfg_stream_handle = 0;
788 
789 	dma_free_coherent(jpeg->dev, jpeg->slot_data.cfg_dec_size,
790 			  jpeg->slot_data.cfg_dec_vaddr,
791 			  jpeg->slot_data.cfg_dec_daddr);
792 	jpeg->slot_data.cfg_dec_size = 0;
793 	jpeg->slot_data.cfg_dec_vaddr = NULL;
794 	jpeg->slot_data.cfg_dec_daddr = 0;
795 
796 	jpeg->slot_data.used = false;
797 }
798 
799 static bool mxc_jpeg_alloc_slot_data(struct mxc_jpeg_dev *jpeg)
800 {
801 	struct mxc_jpeg_desc *desc;
802 	struct mxc_jpeg_desc *cfg_desc;
803 	void *cfg_stm;
804 
805 	if (jpeg->slot_data.desc)
806 		goto skip_alloc; /* already allocated, reuse it */
807 
808 	/* allocate descriptor for decoding/encoding phase */
809 	desc = dma_alloc_coherent(jpeg->dev,
810 				  sizeof(struct mxc_jpeg_desc),
811 				  &jpeg->slot_data.desc_handle,
812 				  GFP_ATOMIC);
813 	if (!desc)
814 		goto err;
815 	jpeg->slot_data.desc = desc;
816 
817 	/* allocate descriptor for configuration phase (encoder only) */
818 	cfg_desc = dma_alloc_coherent(jpeg->dev,
819 				      sizeof(struct mxc_jpeg_desc),
820 				      &jpeg->slot_data.cfg_desc_handle,
821 				      GFP_ATOMIC);
822 	if (!cfg_desc)
823 		goto err;
824 	jpeg->slot_data.cfg_desc = cfg_desc;
825 
826 	/* allocate configuration stream */
827 	cfg_stm = dma_alloc_coherent(jpeg->dev,
828 				     MXC_JPEG_MAX_CFG_STREAM,
829 				     &jpeg->slot_data.cfg_stream_handle,
830 				     GFP_ATOMIC);
831 	if (!cfg_stm)
832 		goto err;
833 	jpeg->slot_data.cfg_stream_vaddr = cfg_stm;
834 
835 	jpeg->slot_data.cfg_dec_size = MXC_JPEG_PATTERN_WIDTH * MXC_JPEG_PATTERN_HEIGHT * 2;
836 	jpeg->slot_data.cfg_dec_vaddr = dma_alloc_coherent(jpeg->dev,
837 							   jpeg->slot_data.cfg_dec_size,
838 							   &jpeg->slot_data.cfg_dec_daddr,
839 							   GFP_ATOMIC);
840 	if (!jpeg->slot_data.cfg_dec_vaddr)
841 		goto err;
842 
843 skip_alloc:
844 	jpeg->slot_data.used = true;
845 
846 	return true;
847 err:
848 	dev_err(jpeg->dev, "Could not allocate descriptors for slot %d", jpeg->slot_data.slot);
849 	mxc_jpeg_free_slot_data(jpeg);
850 
851 	return false;
852 }
853 
854 static void mxc_jpeg_check_and_set_last_buffer(struct mxc_jpeg_ctx *ctx,
855 					       struct vb2_v4l2_buffer *src_buf,
856 					       struct vb2_v4l2_buffer *dst_buf)
857 {
858 	if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) {
859 		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
860 		v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx);
861 		notify_eos(ctx);
862 		ctx->header_parsed = false;
863 	}
864 }
865 
866 static void mxc_jpeg_job_finish(struct mxc_jpeg_ctx *ctx, enum vb2_buffer_state state, bool reset)
867 {
868 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
869 	void __iomem *reg = jpeg->base_reg;
870 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
871 
872 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
873 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
874 	mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf);
875 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
876 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
877 	v4l2_m2m_buf_done(src_buf, state);
878 	v4l2_m2m_buf_done(dst_buf, state);
879 
880 	mxc_jpeg_disable_irq(reg, ctx->slot);
881 	jpeg->slot_data.used = false;
882 	if (reset)
883 		mxc_jpeg_sw_reset(reg);
884 }
885 
886 static u32 mxc_jpeg_get_plane_size(struct mxc_jpeg_q_data *q_data, u32 plane_no)
887 {
888 	const struct mxc_jpeg_fmt *fmt = q_data->fmt;
889 	u32 size;
890 	int i;
891 
892 	if (plane_no >= fmt->mem_planes)
893 		return 0;
894 
895 	if (fmt->mem_planes == fmt->comp_planes)
896 		return q_data->sizeimage[plane_no];
897 
898 	if (plane_no < fmt->mem_planes - 1)
899 		return q_data->sizeimage[plane_no];
900 
901 	size = q_data->sizeimage[fmt->mem_planes - 1];
902 
903 	/* Should be impossible given mxc_formats. */
904 	if (WARN_ON_ONCE(fmt->comp_planes > ARRAY_SIZE(q_data->sizeimage)))
905 		return size;
906 
907 	for (i = fmt->mem_planes; i < fmt->comp_planes; i++)
908 		size += q_data->sizeimage[i];
909 
910 	return size;
911 }
912 
913 static bool mxc_dec_is_ongoing(struct mxc_jpeg_ctx *ctx)
914 {
915 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
916 	u32 curr_desc;
917 	u32 slot_status;
918 
919 	curr_desc = readl(jpeg->base_reg + MXC_SLOT_OFFSET(ctx->slot, SLOT_CUR_DESCPT_PTR));
920 	if (curr_desc == jpeg->slot_data.cfg_desc_handle)
921 		return true;
922 
923 	slot_status = readl(jpeg->base_reg + MXC_SLOT_OFFSET(ctx->slot, SLOT_STATUS));
924 	if (slot_status & SLOT_STATUS_ONGOING)
925 		return true;
926 
927 	/*
928 	 * The curr_desc register is updated when next_descpt_ptr is loaded,
929 	 * the ongoing bit of slot_status is set when the 32 bytes descriptor is loaded.
930 	 * So there will be a short time interval in between, which may cause fake false.
931 	 * Consider read register is quite slow compared with IP read 32byte from memory,
932 	 * read twice slot_status can avoid this situation.
933 	 */
934 	slot_status = readl(jpeg->base_reg + MXC_SLOT_OFFSET(ctx->slot, SLOT_STATUS));
935 	if (slot_status & SLOT_STATUS_ONGOING)
936 		return true;
937 
938 	return false;
939 }
940 
941 static irqreturn_t mxc_jpeg_dec_irq(int irq, void *priv)
942 {
943 	struct mxc_jpeg_dev *jpeg = priv;
944 	struct mxc_jpeg_ctx *ctx;
945 	void __iomem *reg = jpeg->base_reg;
946 	struct device *dev = jpeg->dev;
947 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
948 	struct mxc_jpeg_src_buf *jpeg_src_buf;
949 	enum vb2_buffer_state buf_state;
950 	u32 dec_ret, com_status;
951 	unsigned long payload;
952 	struct mxc_jpeg_q_data *q_data;
953 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
954 	unsigned int slot;
955 
956 	spin_lock(&jpeg->hw_lock);
957 
958 	com_status = readl(reg + COM_STATUS);
959 	slot = COM_STATUS_CUR_SLOT(com_status);
960 	dev_dbg(dev, "Irq %d on slot %d.\n", irq, slot);
961 
962 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
963 	if (WARN_ON(!ctx))
964 		goto job_unlock;
965 
966 	if (slot != ctx->slot) {
967 		/* TODO investigate when adding multi-instance support */
968 		dev_warn(dev, "IRQ slot %d != context slot %d.\n",
969 			 slot, ctx->slot);
970 		goto job_unlock;
971 	}
972 
973 	if (!jpeg->slot_data.used)
974 		goto job_unlock;
975 
976 	dec_ret = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS));
977 	writel(dec_ret, reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS)); /* w1c */
978 
979 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
980 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
981 	if (!dst_buf || !src_buf) {
982 		dev_err(dev, "No source or destination buffer.\n");
983 		goto job_unlock;
984 	}
985 	jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf);
986 
987 	if (dec_ret & SLOT_STATUS_ENC_CONFIG_ERR) {
988 		u32 ret = readl(reg + CAST_STATUS12);
989 
990 		dev_err(dev, "Encoder/decoder error, dec_ret = 0x%08x, status=0x%08x",
991 			dec_ret, ret);
992 		mxc_jpeg_clr_desc(reg, slot);
993 		mxc_jpeg_sw_reset(reg);
994 		buf_state = VB2_BUF_STATE_ERROR;
995 		goto buffers_done;
996 	}
997 
998 	if (!(dec_ret & SLOT_STATUS_FRMDONE))
999 		goto job_unlock;
1000 
1001 	if (jpeg->mode == MXC_JPEG_ENCODE &&
1002 	    ctx->enc_state == MXC_JPEG_ENC_CONF) {
1003 		q_data = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1004 		ctx->enc_state = MXC_JPEG_ENCODING;
1005 		dev_dbg(dev, "Encoder config finished. Start encoding...\n");
1006 		mxc_jpeg_enc_set_quality(dev, reg, ctx->jpeg_quality);
1007 		mxc_jpeg_enc_mode_go(dev, reg, mxc_jpeg_is_extended_sequential(q_data->fmt));
1008 		goto job_unlock;
1009 	}
1010 	if (jpeg->mode == MXC_JPEG_DECODE && jpeg_src_buf->dht_needed &&
1011 	    mxc_dec_is_ongoing(ctx)) {
1012 		jpeg_src_buf->dht_needed = false;
1013 		dev_dbg(dev, "Decoder DHT cfg finished. Start decoding...\n");
1014 		goto job_unlock;
1015 	}
1016 
1017 	if (jpeg->mode == MXC_JPEG_ENCODE) {
1018 		payload = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_BUF_PTR));
1019 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
1020 		dev_dbg(dev, "Encoding finished, payload size: %ld\n",
1021 			payload);
1022 	} else {
1023 		q_data = mxc_jpeg_get_q_data(ctx, cap_type);
1024 		payload = mxc_jpeg_get_plane_size(q_data, 0);
1025 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
1026 		vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
1027 		if (q_data->fmt->mem_planes == 2) {
1028 			payload = mxc_jpeg_get_plane_size(q_data, 1);
1029 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, payload);
1030 		}
1031 		dev_dbg(dev, "Decoding finished, payload size: %ld + %ld\n",
1032 			vb2_get_plane_payload(&dst_buf->vb2_buf, 0),
1033 			vb2_get_plane_payload(&dst_buf->vb2_buf, 1));
1034 	}
1035 
1036 	/* short preview of the results */
1037 	dev_dbg(dev, "src_buf preview: ");
1038 	print_mxc_buf(jpeg, &src_buf->vb2_buf, 32);
1039 	dev_dbg(dev, "dst_buf preview: ");
1040 	print_mxc_buf(jpeg, &dst_buf->vb2_buf, 32);
1041 	buf_state = VB2_BUF_STATE_DONE;
1042 
1043 buffers_done:
1044 	mxc_jpeg_job_finish(ctx, buf_state, false);
1045 	spin_unlock(&jpeg->hw_lock);
1046 	cancel_delayed_work(&ctx->task_timer);
1047 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1048 	return IRQ_HANDLED;
1049 job_unlock:
1050 	spin_unlock(&jpeg->hw_lock);
1051 	return IRQ_HANDLED;
1052 }
1053 
1054 static int mxc_jpeg_fixup_sof(struct mxc_jpeg_sof *sof,
1055 			      u32 fourcc,
1056 			      u16 w, u16 h)
1057 {
1058 	int sof_length;
1059 	const struct mxc_jpeg_fmt *fmt = mxc_jpeg_find_format(fourcc);
1060 
1061 	if (fmt)
1062 		sof->precision = fmt->precision;
1063 	else
1064 		sof->precision = 8; /* TODO allow 8/12 bit precision*/
1065 	sof->height = h;
1066 	_bswap16(&sof->height);
1067 	sof->width = w;
1068 	_bswap16(&sof->width);
1069 
1070 	switch (fourcc) {
1071 	case V4L2_PIX_FMT_NV12:
1072 	case V4L2_PIX_FMT_NV12M:
1073 	case V4L2_PIX_FMT_P012:
1074 	case V4L2_PIX_FMT_P012M:
1075 		sof->components_no = 3;
1076 		sof->comp[0].v = 0x2;
1077 		sof->comp[0].h = 0x2;
1078 		break;
1079 	case V4L2_PIX_FMT_YUYV:
1080 	case V4L2_PIX_FMT_Y212:
1081 		sof->components_no = 3;
1082 		sof->comp[0].v = 0x1;
1083 		sof->comp[0].h = 0x2;
1084 		break;
1085 	case V4L2_PIX_FMT_YUV24:
1086 	case V4L2_PIX_FMT_YUV48_12:
1087 	case V4L2_PIX_FMT_BGR24:
1088 	case V4L2_PIX_FMT_BGR48_12:
1089 	default:
1090 		sof->components_no = 3;
1091 		break;
1092 	case V4L2_PIX_FMT_ABGR32:
1093 	case V4L2_PIX_FMT_ABGR64_12:
1094 		sof->components_no = 4;
1095 		break;
1096 	case V4L2_PIX_FMT_GREY:
1097 	case V4L2_PIX_FMT_Y012:
1098 		sof->components_no = 1;
1099 		break;
1100 	}
1101 	sof_length = 8 + 3 * sof->components_no;
1102 	sof->length = sof_length;
1103 	_bswap16(&sof->length);
1104 
1105 	return sof_length; /* not swaped */
1106 }
1107 
1108 static int mxc_jpeg_fixup_sos(struct mxc_jpeg_sos *sos,
1109 			      u32 fourcc)
1110 {
1111 	int sos_length;
1112 	u8 *sof_u8 = (u8 *)sos;
1113 
1114 	switch (fourcc) {
1115 	case V4L2_PIX_FMT_NV12:
1116 	case V4L2_PIX_FMT_NV12M:
1117 	case V4L2_PIX_FMT_P012:
1118 	case V4L2_PIX_FMT_P012M:
1119 		sos->components_no = 3;
1120 		break;
1121 	case V4L2_PIX_FMT_YUYV:
1122 	case V4L2_PIX_FMT_Y212:
1123 		sos->components_no = 3;
1124 		break;
1125 	case V4L2_PIX_FMT_YUV24:
1126 	case V4L2_PIX_FMT_YUV48_12:
1127 	case V4L2_PIX_FMT_BGR24:
1128 	case V4L2_PIX_FMT_BGR48_12:
1129 	default:
1130 		sos->components_no = 3;
1131 		break;
1132 	case V4L2_PIX_FMT_ABGR32:
1133 	case V4L2_PIX_FMT_ABGR64_12:
1134 		sos->components_no = 4;
1135 		break;
1136 	case V4L2_PIX_FMT_GREY:
1137 	case V4L2_PIX_FMT_Y012:
1138 		sos->components_no = 1;
1139 		break;
1140 	}
1141 	sos_length = 6 + 2 * sos->components_no;
1142 	sos->length = sos_length;
1143 	_bswap16(&sos->length);
1144 
1145 	/* SOS ignorable bytes, not so ignorable after all */
1146 	sof_u8[sos_length - 1] = 0x0;
1147 	sof_u8[sos_length - 2] = 0x3f;
1148 	sof_u8[sos_length - 3] = 0x0;
1149 
1150 	return sos_length; /* not swaped */
1151 }
1152 
1153 static unsigned int mxc_jpeg_setup_cfg_stream(void *cfg_stream_vaddr,
1154 					      u32 fourcc,
1155 					      u16 w, u16 h)
1156 {
1157 	/*
1158 	 * There is a hardware issue that first 128 bytes of configuration data
1159 	 * can't be loaded correctly.
1160 	 * To avoid this issue, we need to write the configuration from
1161 	 * an offset which should be no less than 0x80 (128 bytes).
1162 	 */
1163 	unsigned int offset = 0x80;
1164 	u8 *cfg = (u8 *)cfg_stream_vaddr;
1165 	struct mxc_jpeg_sof *sof;
1166 	struct mxc_jpeg_sos *sos;
1167 	const struct mxc_jpeg_fmt *fmt = mxc_jpeg_find_format(fourcc);
1168 
1169 	if (!fmt)
1170 		return 0;
1171 
1172 	memcpy(cfg + offset, jpeg_soi, ARRAY_SIZE(jpeg_soi));
1173 	offset += ARRAY_SIZE(jpeg_soi);
1174 
1175 	if (fmt->is_rgb) {
1176 		memcpy(cfg + offset, jpeg_app14, sizeof(jpeg_app14));
1177 		offset += sizeof(jpeg_app14);
1178 	} else {
1179 		memcpy(cfg + offset, jpeg_app0, sizeof(jpeg_app0));
1180 		offset += sizeof(jpeg_app0);
1181 	}
1182 
1183 	if (mxc_jpeg_is_extended_sequential(fmt)) {
1184 		memcpy(cfg + offset, jpeg_dqt_extseq, sizeof(jpeg_dqt_extseq));
1185 		offset += sizeof(jpeg_dqt_extseq);
1186 
1187 		memcpy(cfg + offset, jpeg_sof_extseq, sizeof(jpeg_sof_extseq));
1188 	} else {
1189 		memcpy(cfg + offset, jpeg_dqt, sizeof(jpeg_dqt));
1190 		offset += sizeof(jpeg_dqt);
1191 
1192 		memcpy(cfg + offset, jpeg_sof_maximal, sizeof(jpeg_sof_maximal));
1193 	}
1194 	offset += 2; /* skip marker ID */
1195 	sof = (struct mxc_jpeg_sof *)(cfg + offset);
1196 	offset += mxc_jpeg_fixup_sof(sof, fourcc, w, h);
1197 
1198 	if (mxc_jpeg_is_extended_sequential(fmt)) {
1199 		memcpy(cfg + offset, jpeg_dht_extseq, sizeof(jpeg_dht_extseq));
1200 		offset += sizeof(jpeg_dht_extseq);
1201 	} else {
1202 		memcpy(cfg + offset, jpeg_dht, sizeof(jpeg_dht));
1203 		offset += sizeof(jpeg_dht);
1204 	}
1205 
1206 	memcpy(cfg + offset, jpeg_dri, sizeof(jpeg_dri));
1207 	offset += sizeof(jpeg_dri);
1208 
1209 	memcpy(cfg + offset, jpeg_sos_maximal, sizeof(jpeg_sos_maximal));
1210 	offset += 2; /* skip marker ID */
1211 	sos = (struct mxc_jpeg_sos *)(cfg + offset);
1212 	offset += mxc_jpeg_fixup_sos(sos, fourcc);
1213 
1214 	memcpy(cfg + offset, jpeg_image_red, sizeof(jpeg_image_red));
1215 	offset += sizeof(jpeg_image_red);
1216 
1217 	memcpy(cfg + offset, jpeg_eoi, sizeof(jpeg_eoi));
1218 	offset += sizeof(jpeg_eoi);
1219 
1220 	return offset;
1221 }
1222 
1223 static void mxc_jpeg_config_dec_desc(struct vb2_buffer *out_buf,
1224 				     struct mxc_jpeg_ctx *ctx,
1225 				     struct vb2_buffer *src_buf,
1226 				     struct vb2_buffer *dst_buf)
1227 {
1228 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1229 	struct mxc_jpeg_q_data *q_data_cap;
1230 	enum mxc_jpeg_image_format img_fmt;
1231 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1232 	void __iomem *reg = jpeg->base_reg;
1233 	unsigned int slot = ctx->slot;
1234 	struct mxc_jpeg_desc *desc = jpeg->slot_data.desc;
1235 	struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data.cfg_desc;
1236 	dma_addr_t desc_handle = jpeg->slot_data.desc_handle;
1237 	dma_addr_t cfg_desc_handle = jpeg->slot_data.cfg_desc_handle;
1238 	dma_addr_t cfg_stream_handle = jpeg->slot_data.cfg_stream_handle;
1239 	unsigned int *cfg_size = &jpeg->slot_data.cfg_stream_size;
1240 	void *cfg_stream_vaddr = jpeg->slot_data.cfg_stream_vaddr;
1241 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1242 
1243 	jpeg_src_buf = vb2_to_mxc_buf(src_buf);
1244 
1245 	/* setup the decoding descriptor */
1246 	desc->next_descpt_ptr = 0; /* end of chain */
1247 	q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type);
1248 	desc->imgsize = q_data_cap->w_adjusted << 16 | q_data_cap->h_adjusted;
1249 	img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data_cap->fmt->fourcc);
1250 	desc->stm_ctrl &= ~STM_CTRL_IMAGE_FORMAT(0xF); /* clear image format */
1251 	desc->stm_ctrl |= STM_CTRL_IMAGE_FORMAT(img_fmt);
1252 	desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
1253 	if (mxc_jpeg_is_extended_sequential(jpeg_src_buf->fmt))
1254 		desc->stm_ctrl |= STM_CTRL_PIXEL_PRECISION;
1255 	else
1256 		desc->stm_ctrl &= ~STM_CTRL_PIXEL_PRECISION;
1257 	desc->line_pitch = q_data_cap->bytesperline[0];
1258 	mxc_jpeg_addrs(desc, dst_buf, src_buf, 0);
1259 	mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(src_buf, 0), 1024));
1260 	print_descriptor_info(jpeg->dev, desc);
1261 
1262 	if (!jpeg_src_buf->dht_needed) {
1263 		/* validate the decoding descriptor */
1264 		mxc_jpeg_set_desc(desc_handle, reg, slot);
1265 		return;
1266 	}
1267 
1268 	/*
1269 	 * if a default huffman table is needed, use the config descriptor to
1270 	 * inject a DHT, by chaining it before the decoding descriptor
1271 	 */
1272 	*cfg_size = mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr,
1273 					      V4L2_PIX_FMT_YUYV,
1274 					      MXC_JPEG_PATTERN_WIDTH,
1275 					      MXC_JPEG_PATTERN_HEIGHT);
1276 	cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN;
1277 	cfg_desc->buf_base0 = jpeg->slot_data.cfg_dec_daddr;
1278 	cfg_desc->buf_base1 = 0;
1279 	cfg_desc->imgsize = MXC_JPEG_PATTERN_WIDTH << 16;
1280 	cfg_desc->imgsize |= MXC_JPEG_PATTERN_HEIGHT;
1281 	cfg_desc->line_pitch = MXC_JPEG_PATTERN_WIDTH * 2;
1282 	cfg_desc->stm_ctrl = STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV422);
1283 	cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
1284 	cfg_desc->stm_bufbase = cfg_stream_handle;
1285 	cfg_desc->stm_bufsize = ALIGN(*cfg_size, 1024);
1286 	print_descriptor_info(jpeg->dev, cfg_desc);
1287 
1288 	/* validate the configuration descriptor */
1289 	mxc_jpeg_set_desc(cfg_desc_handle, reg, slot);
1290 }
1291 
1292 static void mxc_jpeg_config_enc_desc(struct vb2_buffer *out_buf,
1293 				     struct mxc_jpeg_ctx *ctx,
1294 				     struct vb2_buffer *src_buf,
1295 				     struct vb2_buffer *dst_buf)
1296 {
1297 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1298 	void __iomem *reg = jpeg->base_reg;
1299 	unsigned int slot = ctx->slot;
1300 	struct mxc_jpeg_desc *desc = jpeg->slot_data.desc;
1301 	struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data.cfg_desc;
1302 	dma_addr_t desc_handle = jpeg->slot_data.desc_handle;
1303 	dma_addr_t cfg_desc_handle = jpeg->slot_data.cfg_desc_handle;
1304 	void *cfg_stream_vaddr = jpeg->slot_data.cfg_stream_vaddr;
1305 	struct mxc_jpeg_q_data *q_data;
1306 	enum mxc_jpeg_image_format img_fmt;
1307 	int w, h;
1308 
1309 	q_data = mxc_jpeg_get_q_data(ctx, src_buf->vb2_queue->type);
1310 
1311 	jpeg->slot_data.cfg_stream_size =
1312 			mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr,
1313 						  q_data->fmt->fourcc,
1314 						  q_data->crop.width,
1315 						  q_data->crop.height);
1316 
1317 	/* chain the config descriptor with the encoding descriptor */
1318 	cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN;
1319 
1320 	cfg_desc->buf_base0 = jpeg->slot_data.cfg_stream_handle;
1321 	cfg_desc->buf_base1 = 0;
1322 	cfg_desc->line_pitch = 0;
1323 	cfg_desc->stm_bufbase = 0; /* no output expected */
1324 	cfg_desc->stm_bufsize = 0x0;
1325 	cfg_desc->imgsize = 0;
1326 	cfg_desc->stm_ctrl = STM_CTRL_CONFIG_MOD(1);
1327 	cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
1328 
1329 	desc->next_descpt_ptr = 0; /* end of chain */
1330 
1331 	/* use adjusted resolution for CAST IP job */
1332 	w = q_data->crop.width;
1333 	h = q_data->crop.height;
1334 	v4l_bound_align_image(&w, w, MXC_JPEG_MAX_WIDTH, q_data->fmt->h_align,
1335 			      &h, h, MXC_JPEG_MAX_HEIGHT, q_data->fmt->v_align, 0);
1336 	mxc_jpeg_set_res(desc, w, h);
1337 	mxc_jpeg_set_line_pitch(desc, q_data->bytesperline[0]);
1338 	mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(dst_buf, 0), 1024));
1339 	img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data->fmt->fourcc);
1340 	if (img_fmt == MXC_JPEG_INVALID)
1341 		dev_err(jpeg->dev, "No valid image format detected\n");
1342 	desc->stm_ctrl = STM_CTRL_CONFIG_MOD(0) |
1343 			 STM_CTRL_IMAGE_FORMAT(img_fmt);
1344 	desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
1345 	if (mxc_jpeg_is_extended_sequential(q_data->fmt))
1346 		desc->stm_ctrl |= STM_CTRL_PIXEL_PRECISION;
1347 	else
1348 		desc->stm_ctrl &= ~STM_CTRL_PIXEL_PRECISION;
1349 	mxc_jpeg_addrs(desc, src_buf, dst_buf, 0);
1350 	dev_dbg(jpeg->dev, "cfg_desc:\n");
1351 	print_descriptor_info(jpeg->dev, cfg_desc);
1352 	dev_dbg(jpeg->dev, "enc desc:\n");
1353 	print_descriptor_info(jpeg->dev, desc);
1354 	print_wrapper_info(jpeg->dev, reg);
1355 	print_cast_status(jpeg->dev, reg, MXC_JPEG_ENCODE);
1356 
1357 	/* validate the configuration descriptor */
1358 	mxc_jpeg_set_desc(cfg_desc_handle, reg, slot);
1359 }
1360 
1361 static const struct mxc_jpeg_fmt *mxc_jpeg_get_sibling_format(const struct mxc_jpeg_fmt *fmt)
1362 {
1363 	int i;
1364 
1365 	for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++) {
1366 		if (mxc_formats[i].subsampling == fmt->subsampling &&
1367 		    mxc_formats[i].nc == fmt->nc &&
1368 		    mxc_formats[i].precision == fmt->precision &&
1369 		    mxc_formats[i].is_rgb == fmt->is_rgb &&
1370 		    mxc_formats[i].fourcc != fmt->fourcc)
1371 			return &mxc_formats[i];
1372 	}
1373 
1374 	return NULL;
1375 }
1376 
1377 static bool mxc_jpeg_compare_format(const struct mxc_jpeg_fmt *fmt1,
1378 				    const struct mxc_jpeg_fmt *fmt2)
1379 {
1380 	if (fmt1 == fmt2)
1381 		return true;
1382 	if (mxc_jpeg_get_sibling_format(fmt1) == fmt2)
1383 		return true;
1384 	return false;
1385 }
1386 
1387 static void mxc_jpeg_set_last_buffer(struct mxc_jpeg_ctx *ctx)
1388 {
1389 	struct vb2_v4l2_buffer *next_dst_buf;
1390 
1391 	next_dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1392 	if (!next_dst_buf) {
1393 		ctx->fh.m2m_ctx->is_draining = true;
1394 		ctx->fh.m2m_ctx->next_buf_last = true;
1395 		return;
1396 	}
1397 
1398 	v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, next_dst_buf);
1399 }
1400 
1401 static bool mxc_jpeg_source_change(struct mxc_jpeg_ctx *ctx,
1402 				   struct mxc_jpeg_src_buf *jpeg_src_buf)
1403 {
1404 	struct device *dev = ctx->mxc_jpeg->dev;
1405 	struct mxc_jpeg_q_data *q_data_cap;
1406 
1407 	if (!jpeg_src_buf->fmt)
1408 		return false;
1409 
1410 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1411 	if (mxc_jpeg_compare_format(q_data_cap->fmt, jpeg_src_buf->fmt))
1412 		jpeg_src_buf->fmt = q_data_cap->fmt;
1413 	if (ctx->need_initial_source_change_evt ||
1414 	    q_data_cap->fmt != jpeg_src_buf->fmt ||
1415 	    q_data_cap->w != jpeg_src_buf->w ||
1416 	    q_data_cap->h != jpeg_src_buf->h) {
1417 		dev_dbg(dev, "Detected jpeg res=(%dx%d)->(%dx%d), pixfmt=%c%c%c%c\n",
1418 			q_data_cap->w, q_data_cap->h,
1419 			jpeg_src_buf->w, jpeg_src_buf->h,
1420 			(jpeg_src_buf->fmt->fourcc & 0xff),
1421 			(jpeg_src_buf->fmt->fourcc >>  8) & 0xff,
1422 			(jpeg_src_buf->fmt->fourcc >> 16) & 0xff,
1423 			(jpeg_src_buf->fmt->fourcc >> 24) & 0xff);
1424 
1425 		/*
1426 		 * set-up the capture queue with the pixelformat and resolution
1427 		 * detected from the jpeg output stream
1428 		 */
1429 		q_data_cap->w = jpeg_src_buf->w;
1430 		q_data_cap->h = jpeg_src_buf->h;
1431 		q_data_cap->fmt = jpeg_src_buf->fmt;
1432 		q_data_cap->w_adjusted = q_data_cap->w;
1433 		q_data_cap->h_adjusted = q_data_cap->h;
1434 		q_data_cap->crop.left = 0;
1435 		q_data_cap->crop.top = 0;
1436 		q_data_cap->crop.width = jpeg_src_buf->w;
1437 		q_data_cap->crop.height = jpeg_src_buf->h;
1438 		q_data_cap->bytesperline[0] = 0;
1439 		q_data_cap->bytesperline[1] = 0;
1440 
1441 		/*
1442 		 * align up the resolution for CAST IP,
1443 		 * but leave the buffer resolution unchanged
1444 		 */
1445 		v4l_bound_align_image(&q_data_cap->w_adjusted,
1446 				      q_data_cap->w_adjusted,  /* adjust up */
1447 				      MXC_JPEG_MAX_WIDTH,
1448 				      q_data_cap->fmt->h_align,
1449 				      &q_data_cap->h_adjusted,
1450 				      q_data_cap->h_adjusted, /* adjust up */
1451 				      MXC_JPEG_MAX_HEIGHT,
1452 				      q_data_cap->fmt->v_align,
1453 				      0);
1454 
1455 		/* setup bytesperline/sizeimage for capture queue */
1456 		mxc_jpeg_bytesperline(q_data_cap, jpeg_src_buf->fmt->precision);
1457 		mxc_jpeg_sizeimage(q_data_cap);
1458 		notify_src_chg(ctx);
1459 		ctx->source_change = 1;
1460 		ctx->need_initial_source_change_evt = false;
1461 		if (vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)))
1462 			mxc_jpeg_set_last_buffer(ctx);
1463 	}
1464 
1465 	return ctx->source_change ? true : false;
1466 }
1467 
1468 static int mxc_jpeg_job_ready(void *priv)
1469 {
1470 	struct mxc_jpeg_ctx *ctx = priv;
1471 
1472 	return ctx->source_change ? 0 : 1;
1473 }
1474 
1475 static void mxc_jpeg_device_run_timeout(struct work_struct *work)
1476 {
1477 	struct delayed_work *dwork = to_delayed_work(work);
1478 	struct mxc_jpeg_ctx *ctx = container_of(dwork, struct mxc_jpeg_ctx, task_timer);
1479 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1480 	unsigned long flags;
1481 
1482 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1483 	if (ctx->mxc_jpeg->slot_data.used) {
1484 		dev_warn(jpeg->dev, "%s timeout, cancel it\n",
1485 			 ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ? "decode" : "encode");
1486 		mxc_jpeg_job_finish(ctx, VB2_BUF_STATE_ERROR, true);
1487 		v4l2_m2m_job_finish(ctx->mxc_jpeg->m2m_dev, ctx->fh.m2m_ctx);
1488 	}
1489 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1490 }
1491 
1492 static void mxc_jpeg_device_run(void *priv)
1493 {
1494 	struct mxc_jpeg_ctx *ctx = priv;
1495 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1496 	void __iomem *reg = jpeg->base_reg;
1497 	struct device *dev = jpeg->dev;
1498 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1499 	unsigned long flags;
1500 	struct mxc_jpeg_q_data *q_data_cap, *q_data_out;
1501 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1502 
1503 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1504 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1505 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1506 	if (!src_buf || !dst_buf) {
1507 		dev_err(dev, "Null src or dst buf\n");
1508 		goto end;
1509 	}
1510 
1511 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1512 	if (!q_data_cap)
1513 		goto end;
1514 	q_data_out = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1515 	if (!q_data_out)
1516 		goto end;
1517 	src_buf->sequence = q_data_out->sequence++;
1518 	dst_buf->sequence = q_data_cap->sequence++;
1519 
1520 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
1521 
1522 	jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf);
1523 	if (q_data_cap->fmt->mem_planes != dst_buf->vb2_buf.num_planes) {
1524 		dev_err(dev, "Capture format %s has %d planes, but capture buffer has %d planes\n",
1525 			q_data_cap->fmt->name, q_data_cap->fmt->mem_planes,
1526 			dst_buf->vb2_buf.num_planes);
1527 		jpeg_src_buf->jpeg_parse_error = true;
1528 	}
1529 	if (jpeg_src_buf->jpeg_parse_error) {
1530 		mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf);
1531 		v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1532 		v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1533 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1534 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1535 		spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1536 		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1537 
1538 		return;
1539 	}
1540 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) {
1541 		if (ctx->source_change || mxc_jpeg_source_change(ctx, jpeg_src_buf)) {
1542 			spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1543 			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1544 			return;
1545 		}
1546 	}
1547 
1548 	mxc_jpeg_enable(reg);
1549 	mxc_jpeg_set_l_endian(reg, 1);
1550 
1551 	ctx->slot = mxc_get_free_slot(&jpeg->slot_data);
1552 	if (ctx->slot < 0) {
1553 		dev_err(dev, "No more free slots\n");
1554 		goto end;
1555 	}
1556 	if (!mxc_jpeg_alloc_slot_data(jpeg)) {
1557 		dev_err(dev, "Cannot allocate slot data\n");
1558 		goto end;
1559 	}
1560 
1561 	mxc_jpeg_enable_slot(reg, ctx->slot);
1562 	mxc_jpeg_enable_irq(reg, ctx->slot);
1563 
1564 	if (jpeg->mode == MXC_JPEG_ENCODE) {
1565 		dev_dbg(dev, "Encoding on slot %d\n", ctx->slot);
1566 		ctx->enc_state = MXC_JPEG_ENC_CONF;
1567 		mxc_jpeg_config_enc_desc(&dst_buf->vb2_buf, ctx,
1568 					 &src_buf->vb2_buf, &dst_buf->vb2_buf);
1569 		/* start config phase */
1570 		mxc_jpeg_enc_mode_conf(dev, reg,
1571 				       mxc_jpeg_is_extended_sequential(q_data_out->fmt));
1572 	} else {
1573 		dev_dbg(dev, "Decoding on slot %d\n", ctx->slot);
1574 		print_mxc_buf(jpeg, &src_buf->vb2_buf, 0);
1575 		mxc_jpeg_config_dec_desc(&dst_buf->vb2_buf, ctx,
1576 					 &src_buf->vb2_buf, &dst_buf->vb2_buf);
1577 		mxc_jpeg_dec_mode_go(dev, reg);
1578 	}
1579 	schedule_delayed_work(&ctx->task_timer, msecs_to_jiffies(hw_timeout));
1580 end:
1581 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1582 }
1583 
1584 static int mxc_jpeg_decoder_cmd(struct file *file, void *priv,
1585 				struct v4l2_decoder_cmd *cmd)
1586 {
1587 	struct v4l2_fh *fh = file->private_data;
1588 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
1589 	unsigned long flags;
1590 	int ret;
1591 
1592 	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
1593 	if (ret < 0)
1594 		return ret;
1595 
1596 	if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx)))
1597 		return 0;
1598 
1599 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1600 	ret = v4l2_m2m_ioctl_decoder_cmd(file, priv, cmd);
1601 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1602 	if (ret < 0)
1603 		return ret;
1604 
1605 	if (cmd->cmd == V4L2_DEC_CMD_STOP &&
1606 	    v4l2_m2m_has_stopped(fh->m2m_ctx)) {
1607 		notify_eos(ctx);
1608 		ctx->header_parsed = false;
1609 	}
1610 
1611 	if (cmd->cmd == V4L2_DEC_CMD_START &&
1612 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1613 		vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q);
1614 	return 0;
1615 }
1616 
1617 static int mxc_jpeg_encoder_cmd(struct file *file, void *priv,
1618 				struct v4l2_encoder_cmd *cmd)
1619 {
1620 	struct v4l2_fh *fh = file->private_data;
1621 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
1622 	unsigned long flags;
1623 	int ret;
1624 
1625 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
1626 	if (ret < 0)
1627 		return ret;
1628 
1629 	if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx)) ||
1630 	    !vb2_is_streaming(v4l2_m2m_get_dst_vq(fh->m2m_ctx)))
1631 		return 0;
1632 
1633 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1634 	ret = v4l2_m2m_ioctl_encoder_cmd(file, fh, cmd);
1635 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1636 	if (ret < 0)
1637 		return 0;
1638 
1639 	if (cmd->cmd == V4L2_ENC_CMD_STOP &&
1640 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1641 		notify_eos(ctx);
1642 
1643 	if (cmd->cmd == V4L2_ENC_CMD_START &&
1644 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1645 		vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q);
1646 
1647 	return 0;
1648 }
1649 
1650 static int mxc_jpeg_queue_setup(struct vb2_queue *q,
1651 				unsigned int *nbuffers,
1652 				unsigned int *nplanes,
1653 				unsigned int sizes[],
1654 				struct device *alloc_ctxs[])
1655 {
1656 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1657 	struct mxc_jpeg_q_data *q_data = NULL;
1658 	int i;
1659 
1660 	q_data = mxc_jpeg_get_q_data(ctx, q->type);
1661 	if (!q_data)
1662 		return -EINVAL;
1663 
1664 	/* Handle CREATE_BUFS situation - *nplanes != 0 */
1665 	if (*nplanes) {
1666 		if (*nplanes != q_data->fmt->mem_planes)
1667 			return -EINVAL;
1668 		for (i = 0; i < *nplanes; i++) {
1669 			if (sizes[i] < mxc_jpeg_get_plane_size(q_data, i))
1670 				return -EINVAL;
1671 		}
1672 		return 0;
1673 	}
1674 
1675 	/* Handle REQBUFS situation */
1676 	*nplanes = q_data->fmt->mem_planes;
1677 	for (i = 0; i < *nplanes; i++)
1678 		sizes[i] = mxc_jpeg_get_plane_size(q_data, i);
1679 
1680 	if (V4L2_TYPE_IS_OUTPUT(q->type))
1681 		ctx->need_initial_source_change_evt = true;
1682 
1683 	return 0;
1684 }
1685 
1686 static int mxc_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
1687 {
1688 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1689 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, q->type);
1690 	int ret;
1691 
1692 	v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
1693 
1694 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(q->type))
1695 		ctx->source_change = 0;
1696 	dev_dbg(ctx->mxc_jpeg->dev, "Start streaming ctx=%p", ctx);
1697 	q_data->sequence = 0;
1698 
1699 	if (V4L2_TYPE_IS_CAPTURE(q->type))
1700 		ctx->need_initial_source_change_evt = false;
1701 
1702 	ret = pm_runtime_resume_and_get(ctx->mxc_jpeg->dev);
1703 	if (ret < 0) {
1704 		dev_err(ctx->mxc_jpeg->dev, "Failed to power up jpeg\n");
1705 		return ret;
1706 	}
1707 
1708 	return 0;
1709 }
1710 
1711 static void mxc_jpeg_stop_streaming(struct vb2_queue *q)
1712 {
1713 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1714 	struct vb2_v4l2_buffer *vbuf;
1715 
1716 	dev_dbg(ctx->mxc_jpeg->dev, "Stop streaming ctx=%p", ctx);
1717 
1718 	/* Release all active buffers */
1719 	for (;;) {
1720 		if (V4L2_TYPE_IS_OUTPUT(q->type))
1721 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1722 		else
1723 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1724 		if (!vbuf)
1725 			break;
1726 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1727 	}
1728 
1729 	v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
1730 	/* if V4L2_DEC_CMD_STOP is sent before the source change triggered,
1731 	 * restore the is_draining flag
1732 	 */
1733 	if (V4L2_TYPE_IS_CAPTURE(q->type) && ctx->source_change && ctx->fh.m2m_ctx->last_src_buf)
1734 		ctx->fh.m2m_ctx->is_draining = true;
1735 
1736 	if (V4L2_TYPE_IS_OUTPUT(q->type) &&
1737 	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) {
1738 		notify_eos(ctx);
1739 		ctx->header_parsed = false;
1740 	}
1741 
1742 	pm_runtime_put_sync(&ctx->mxc_jpeg->pdev->dev);
1743 }
1744 
1745 static int mxc_jpeg_valid_comp_id(struct device *dev,
1746 				  struct mxc_jpeg_sof *sof,
1747 				  struct mxc_jpeg_sos *sos)
1748 {
1749 	int valid = 1;
1750 	int i;
1751 
1752 	/*
1753 	 * there's a limitation in the IP that the component IDs must be
1754 	 * between 0..4, if they are not, let's patch them
1755 	 */
1756 	for (i = 0; i < sof->components_no; i++)
1757 		if (sof->comp[i].id > MXC_JPEG_MAX_COMPONENTS) {
1758 			valid = 0;
1759 			dev_err(dev, "Component %d has invalid ID: %d",
1760 				i, sof->comp[i].id);
1761 		}
1762 	if (!valid)
1763 		/* patch all comp IDs if at least one is invalid */
1764 		for (i = 0; i < sof->components_no; i++) {
1765 			dev_warn(dev, "Component %d ID patched to: %d",
1766 				 i, i + 1);
1767 			sof->comp[i].id = i + 1;
1768 			sos->comp[i].id = i + 1;
1769 		}
1770 
1771 	return valid;
1772 }
1773 
1774 static bool mxc_jpeg_match_image_format(const struct mxc_jpeg_fmt *fmt,
1775 					const struct v4l2_jpeg_header *header)
1776 {
1777 	if (fmt->subsampling != header->frame.subsampling ||
1778 	    fmt->nc != header->frame.num_components ||
1779 	    fmt->precision != header->frame.precision)
1780 		return false;
1781 
1782 	/*
1783 	 * If the transform flag from APP14 marker is 0, images that are
1784 	 * encoded with 3 components have RGB colorspace, see Recommendation
1785 	 * ITU-T T.872 chapter 6.5.3 APP14 marker segment for colour encoding
1786 	 */
1787 	if (header->frame.subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) {
1788 		u8 is_rgb = header->app14_tf == V4L2_JPEG_APP14_TF_CMYK_RGB ? 1 : 0;
1789 
1790 		if (is_rgb != fmt->is_rgb)
1791 			return false;
1792 	}
1793 	return true;
1794 }
1795 
1796 static u32 mxc_jpeg_get_image_format(struct device *dev,
1797 				     const struct v4l2_jpeg_header *header)
1798 {
1799 	int i;
1800 	u32 fourcc = 0;
1801 
1802 	for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++) {
1803 		if (mxc_jpeg_match_image_format(&mxc_formats[i], header)) {
1804 			fourcc = mxc_formats[i].fourcc;
1805 			break;
1806 		}
1807 	}
1808 	if (fourcc == 0) {
1809 		dev_err(dev,
1810 			"Could not identify image format nc=%d, subsampling=%d, precision=%d\n",
1811 			header->frame.num_components,
1812 			header->frame.subsampling,
1813 			header->frame.precision);
1814 		return fourcc;
1815 	}
1816 
1817 	return fourcc;
1818 }
1819 
1820 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision)
1821 {
1822 	u32 bytesperline[2];
1823 
1824 	bytesperline[0] = q->bytesperline[0];
1825 	bytesperline[1] = q->bytesperline[0];	/*imx-jpeg only support the same line pitch*/
1826 	v4l_bound_align_image(&bytesperline[0], 0, MXC_JPEG_MAX_LINE, 2,
1827 			      &bytesperline[1], 0, MXC_JPEG_MAX_LINE, 2,
1828 			      0);
1829 
1830 	/* Bytes distance between the leftmost pixels in two adjacent lines */
1831 	if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1832 		/* bytesperline unused for compressed formats */
1833 		q->bytesperline[0] = 0;
1834 		q->bytesperline[1] = 0;
1835 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
1836 		/* When the image format is planar the bytesperline value
1837 		 * applies to the first plane and is divided by the same factor
1838 		 * as the width field for the other planes
1839 		 */
1840 		q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8);
1841 		q->bytesperline[1] = q->bytesperline[0];
1842 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
1843 		q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8) * 2;
1844 		q->bytesperline[1] = 0;
1845 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) {
1846 		q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8) * q->fmt->nc;
1847 		q->bytesperline[1] = 0;
1848 	} else {
1849 		/* grayscale */
1850 		q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8);
1851 		q->bytesperline[1] = 0;
1852 	}
1853 
1854 	if (q->fmt->fourcc != V4L2_PIX_FMT_JPEG) {
1855 		q->bytesperline[0] = max(q->bytesperline[0], bytesperline[0]);
1856 		if (q->fmt->mem_planes > 1)
1857 			q->bytesperline[1] = max(q->bytesperline[1], bytesperline[1]);
1858 	}
1859 }
1860 
1861 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q)
1862 {
1863 	if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1864 		/* if no sizeimage from user, assume worst jpeg compression */
1865 		if (!q->sizeimage[0])
1866 			q->sizeimage[0] = 6 * q->w * q->h;
1867 		q->sizeimage[1] = 0;
1868 
1869 		if (q->sizeimage[0] > MXC_JPEG_MAX_SIZEIMAGE)
1870 			q->sizeimage[0] = MXC_JPEG_MAX_SIZEIMAGE;
1871 
1872 		/* jpeg stream size must be multiple of 1K */
1873 		q->sizeimage[0] = ALIGN(q->sizeimage[0], 1024);
1874 	} else {
1875 		q->sizeimage[0] = q->bytesperline[0] * q->h_adjusted;
1876 		q->sizeimage[1] = 0;
1877 		if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420)
1878 			q->sizeimage[1] = q->sizeimage[0] / 2;
1879 	}
1880 }
1881 
1882 static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx, struct vb2_buffer *vb)
1883 {
1884 	struct device *dev = ctx->mxc_jpeg->dev;
1885 	struct mxc_jpeg_q_data *q_data_out;
1886 	struct mxc_jpeg_q_data *q_data_cap;
1887 	u32 fourcc;
1888 	struct v4l2_jpeg_header header;
1889 	struct mxc_jpeg_sof *psof = NULL;
1890 	struct mxc_jpeg_sos *psos = NULL;
1891 	struct mxc_jpeg_src_buf *jpeg_src_buf = vb2_to_mxc_buf(vb);
1892 	u8 *src_addr = (u8 *)vb2_plane_vaddr(vb, 0);
1893 	u32 size = vb2_get_plane_payload(vb, 0);
1894 	int ret;
1895 
1896 	memset(&header, 0, sizeof(header));
1897 	ret = v4l2_jpeg_parse_header((void *)src_addr, size, &header);
1898 	if (ret < 0) {
1899 		dev_err(dev, "Error parsing JPEG stream markers\n");
1900 		return ret;
1901 	}
1902 
1903 	/* if DHT marker present, no need to inject default one */
1904 	jpeg_src_buf->dht_needed = (header.num_dht == 0);
1905 
1906 	q_data_out = mxc_jpeg_get_q_data(ctx,
1907 					 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1908 	q_data_out->w = header.frame.width;
1909 	q_data_out->h = header.frame.height;
1910 	if (header.frame.width > MXC_JPEG_MAX_WIDTH ||
1911 	    header.frame.height > MXC_JPEG_MAX_HEIGHT) {
1912 		dev_err(dev, "JPEG width or height should be <= 8192: %dx%d\n",
1913 			header.frame.width, header.frame.height);
1914 		return -EINVAL;
1915 	}
1916 	if (header.frame.width < MXC_JPEG_MIN_WIDTH ||
1917 	    header.frame.height < MXC_JPEG_MIN_HEIGHT) {
1918 		dev_err(dev, "JPEG width or height should be > 64: %dx%d\n",
1919 			header.frame.width, header.frame.height);
1920 		return -EINVAL;
1921 	}
1922 	if (header.frame.num_components > V4L2_JPEG_MAX_COMPONENTS) {
1923 		dev_err(dev, "JPEG number of components should be <=%d",
1924 			V4L2_JPEG_MAX_COMPONENTS);
1925 		return -EINVAL;
1926 	}
1927 	/* check and, if necessary, patch component IDs*/
1928 	psof = (struct mxc_jpeg_sof *)header.sof.start;
1929 	psos = (struct mxc_jpeg_sos *)header.sos.start;
1930 	if (!mxc_jpeg_valid_comp_id(dev, psof, psos))
1931 		dev_warn(dev, "JPEG component ids should be 0-3 or 1-4");
1932 
1933 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1934 	if (q_data_cap->fmt && mxc_jpeg_match_image_format(q_data_cap->fmt, &header))
1935 		fourcc = q_data_cap->fmt->fourcc;
1936 	else
1937 		fourcc = mxc_jpeg_get_image_format(dev, &header);
1938 	if (fourcc == 0)
1939 		return -EINVAL;
1940 
1941 	jpeg_src_buf->fmt = mxc_jpeg_find_format(fourcc);
1942 	jpeg_src_buf->w = header.frame.width;
1943 	jpeg_src_buf->h = header.frame.height;
1944 	ctx->header_parsed = true;
1945 
1946 	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx))
1947 		mxc_jpeg_source_change(ctx, jpeg_src_buf);
1948 
1949 	return 0;
1950 }
1951 
1952 static void mxc_jpeg_buf_queue(struct vb2_buffer *vb)
1953 {
1954 	int ret;
1955 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1956 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1957 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1958 
1959 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1960 	    vb2_is_streaming(vb->vb2_queue) &&
1961 	    v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
1962 		struct mxc_jpeg_q_data *q_data;
1963 
1964 		q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type);
1965 		vbuf->field = V4L2_FIELD_NONE;
1966 		vbuf->sequence = q_data->sequence++;
1967 		v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
1968 		notify_eos(ctx);
1969 		ctx->header_parsed = false;
1970 		return;
1971 	}
1972 
1973 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1974 		goto end;
1975 
1976 	/* for V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE */
1977 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
1978 		goto end;
1979 
1980 	jpeg_src_buf = vb2_to_mxc_buf(vb);
1981 	jpeg_src_buf->jpeg_parse_error = false;
1982 	ret = mxc_jpeg_parse(ctx, vb);
1983 	if (ret) {
1984 		jpeg_src_buf->jpeg_parse_error = true;
1985 
1986 		/*
1987 		 * if the capture queue is not setup, the device_run() won't be scheduled,
1988 		 * need to drop the error buffer, so that the decoding can continue
1989 		 */
1990 		if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx))) {
1991 			v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1992 			return;
1993 		}
1994 	}
1995 
1996 end:
1997 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1998 }
1999 
2000 static int mxc_jpeg_buf_out_validate(struct vb2_buffer *vb)
2001 {
2002 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2003 
2004 	vbuf->field = V4L2_FIELD_NONE;
2005 
2006 	return 0;
2007 }
2008 
2009 static int mxc_jpeg_buf_prepare(struct vb2_buffer *vb)
2010 {
2011 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2012 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2013 	struct mxc_jpeg_q_data *q_data = NULL;
2014 	struct device *dev = ctx->mxc_jpeg->dev;
2015 	unsigned long sizeimage;
2016 	int i;
2017 
2018 	vbuf->field = V4L2_FIELD_NONE;
2019 
2020 	q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type);
2021 	if (!q_data)
2022 		return -EINVAL;
2023 	for (i = 0; i < q_data->fmt->mem_planes; i++) {
2024 		sizeimage = mxc_jpeg_get_plane_size(q_data, i);
2025 		if (!ctx->source_change && vb2_plane_size(vb, i) < sizeimage) {
2026 			dev_err(dev, "plane %d too small (%lu < %lu)",
2027 				i, vb2_plane_size(vb, i), sizeimage);
2028 			return -EINVAL;
2029 		}
2030 	}
2031 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) {
2032 		vb2_set_plane_payload(vb, 0, 0);
2033 		vb2_set_plane_payload(vb, 1, 0);
2034 	}
2035 	return 0;
2036 }
2037 
2038 static const struct vb2_ops mxc_jpeg_qops = {
2039 	.queue_setup		= mxc_jpeg_queue_setup,
2040 	.buf_out_validate	= mxc_jpeg_buf_out_validate,
2041 	.buf_prepare		= mxc_jpeg_buf_prepare,
2042 	.start_streaming	= mxc_jpeg_start_streaming,
2043 	.stop_streaming		= mxc_jpeg_stop_streaming,
2044 	.buf_queue		= mxc_jpeg_buf_queue,
2045 };
2046 
2047 static int mxc_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
2048 			       struct vb2_queue *dst_vq)
2049 {
2050 	struct mxc_jpeg_ctx *ctx = priv;
2051 	int ret;
2052 
2053 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
2054 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
2055 	src_vq->drv_priv = ctx;
2056 	src_vq->buf_struct_size = sizeof(struct mxc_jpeg_src_buf);
2057 	src_vq->ops = &mxc_jpeg_qops;
2058 	src_vq->mem_ops = &vb2_dma_contig_memops;
2059 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2060 	src_vq->lock = &ctx->mxc_jpeg->lock;
2061 	src_vq->dev = ctx->mxc_jpeg->dev;
2062 
2063 	ret = vb2_queue_init(src_vq);
2064 	if (ret)
2065 		return ret;
2066 
2067 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
2068 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
2069 	dst_vq->drv_priv = ctx;
2070 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2071 	dst_vq->ops = &mxc_jpeg_qops;
2072 	dst_vq->mem_ops = &vb2_dma_contig_memops;
2073 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2074 	dst_vq->lock = &ctx->mxc_jpeg->lock;
2075 	dst_vq->dev = ctx->mxc_jpeg->dev;
2076 
2077 	ret = vb2_queue_init(dst_vq);
2078 	return ret;
2079 }
2080 
2081 static void mxc_jpeg_set_default_params(struct mxc_jpeg_ctx *ctx)
2082 {
2083 	struct mxc_jpeg_q_data *out_q = &ctx->out_q;
2084 	struct mxc_jpeg_q_data *cap_q = &ctx->cap_q;
2085 	struct mxc_jpeg_q_data *q[2] = {out_q, cap_q};
2086 	int i;
2087 
2088 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) {
2089 		out_q->fmt = mxc_jpeg_find_format(MXC_JPEG_DEFAULT_PFMT);
2090 		cap_q->fmt = mxc_jpeg_find_format(V4L2_PIX_FMT_JPEG);
2091 	} else {
2092 		out_q->fmt = mxc_jpeg_find_format(V4L2_PIX_FMT_JPEG);
2093 		cap_q->fmt = mxc_jpeg_find_format(MXC_JPEG_DEFAULT_PFMT);
2094 	}
2095 
2096 	for (i = 0; i < 2; i++) {
2097 		q[i]->w = MXC_JPEG_DEFAULT_WIDTH;
2098 		q[i]->h = MXC_JPEG_DEFAULT_HEIGHT;
2099 		q[i]->w_adjusted = MXC_JPEG_DEFAULT_WIDTH;
2100 		q[i]->h_adjusted = MXC_JPEG_DEFAULT_HEIGHT;
2101 		q[i]->crop.left = 0;
2102 		q[i]->crop.top = 0;
2103 		q[i]->crop.width = MXC_JPEG_DEFAULT_WIDTH;
2104 		q[i]->crop.height = MXC_JPEG_DEFAULT_HEIGHT;
2105 		mxc_jpeg_bytesperline(q[i], q[i]->fmt->precision);
2106 		mxc_jpeg_sizeimage(q[i]);
2107 	}
2108 }
2109 
2110 static int mxc_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
2111 {
2112 	struct mxc_jpeg_ctx *ctx =
2113 		container_of(ctrl->handler, struct mxc_jpeg_ctx, ctrl_handler);
2114 
2115 	switch (ctrl->id) {
2116 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2117 		ctx->jpeg_quality = ctrl->val;
2118 		break;
2119 	default:
2120 		dev_err(ctx->mxc_jpeg->dev, "Invalid control, id = %d, val = %d\n",
2121 			ctrl->id, ctrl->val);
2122 		return -EINVAL;
2123 	}
2124 
2125 	return 0;
2126 }
2127 
2128 static const struct v4l2_ctrl_ops mxc_jpeg_ctrl_ops = {
2129 	.s_ctrl = mxc_jpeg_s_ctrl,
2130 };
2131 
2132 static void mxc_jpeg_encode_ctrls(struct mxc_jpeg_ctx *ctx)
2133 {
2134 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &mxc_jpeg_ctrl_ops,
2135 			  V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75);
2136 }
2137 
2138 static int mxc_jpeg_ctrls_setup(struct mxc_jpeg_ctx *ctx)
2139 {
2140 	int err;
2141 
2142 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 2);
2143 
2144 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE)
2145 		mxc_jpeg_encode_ctrls(ctx);
2146 
2147 	if (ctx->ctrl_handler.error) {
2148 		err = ctx->ctrl_handler.error;
2149 
2150 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2151 		return err;
2152 	}
2153 
2154 	err = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
2155 	if (err)
2156 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2157 	return err;
2158 }
2159 
2160 static int mxc_jpeg_open(struct file *file)
2161 {
2162 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
2163 	struct video_device *mxc_vfd = video_devdata(file);
2164 	struct device *dev = mxc_jpeg->dev;
2165 	struct mxc_jpeg_ctx *ctx;
2166 	int ret = 0;
2167 
2168 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2169 	if (!ctx)
2170 		return -ENOMEM;
2171 
2172 	if (mutex_lock_interruptible(&mxc_jpeg->lock)) {
2173 		ret = -ERESTARTSYS;
2174 		goto free;
2175 	}
2176 
2177 	v4l2_fh_init(&ctx->fh, mxc_vfd);
2178 	file->private_data = &ctx->fh;
2179 	v4l2_fh_add(&ctx->fh);
2180 
2181 	ctx->mxc_jpeg = mxc_jpeg;
2182 
2183 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(mxc_jpeg->m2m_dev, ctx,
2184 					    mxc_jpeg_queue_init);
2185 
2186 	if (IS_ERR(ctx->fh.m2m_ctx)) {
2187 		ret = PTR_ERR(ctx->fh.m2m_ctx);
2188 		goto error;
2189 	}
2190 
2191 	ret = mxc_jpeg_ctrls_setup(ctx);
2192 	if (ret) {
2193 		dev_err(ctx->mxc_jpeg->dev, "failed to setup mxc jpeg controls\n");
2194 		goto err_ctrls_setup;
2195 	}
2196 	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
2197 	mxc_jpeg_set_default_params(ctx);
2198 	ctx->slot = -1; /* slot not allocated yet */
2199 	INIT_DELAYED_WORK(&ctx->task_timer, mxc_jpeg_device_run_timeout);
2200 
2201 	if (mxc_jpeg->mode == MXC_JPEG_DECODE)
2202 		dev_dbg(dev, "Opened JPEG decoder instance %p\n", ctx);
2203 	else
2204 		dev_dbg(dev, "Opened JPEG encoder instance %p\n", ctx);
2205 	mutex_unlock(&mxc_jpeg->lock);
2206 
2207 	return 0;
2208 
2209 err_ctrls_setup:
2210 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2211 error:
2212 	v4l2_fh_del(&ctx->fh);
2213 	v4l2_fh_exit(&ctx->fh);
2214 	mutex_unlock(&mxc_jpeg->lock);
2215 free:
2216 	kfree(ctx);
2217 	return ret;
2218 }
2219 
2220 static int mxc_jpeg_querycap(struct file *file, void *priv,
2221 			     struct v4l2_capability *cap)
2222 {
2223 	strscpy(cap->driver, MXC_JPEG_NAME " codec", sizeof(cap->driver));
2224 	strscpy(cap->card, MXC_JPEG_NAME " codec", sizeof(cap->card));
2225 	cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
2226 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
2227 
2228 	return 0;
2229 }
2230 
2231 static int mxc_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
2232 				     struct v4l2_fmtdesc *f)
2233 {
2234 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2235 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type);
2236 
2237 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) {
2238 		return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f,
2239 			MXC_JPEG_FMT_TYPE_ENC);
2240 	} else if (!ctx->header_parsed) {
2241 		return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f,
2242 			MXC_JPEG_FMT_TYPE_RAW);
2243 	} else {
2244 		/* For the decoder CAPTURE queue, only enumerate the raw formats
2245 		 * supported for the format currently active on OUTPUT
2246 		 * (more precisely what was propagated on capture queue
2247 		 * after jpeg parse on the output buffer)
2248 		 */
2249 		int ret = -EINVAL;
2250 		const struct mxc_jpeg_fmt *sibling;
2251 
2252 		switch (f->index) {
2253 		case 0:
2254 			f->pixelformat = q_data->fmt->fourcc;
2255 			ret = 0;
2256 			break;
2257 		case 1:
2258 			sibling = mxc_jpeg_get_sibling_format(q_data->fmt);
2259 			if (sibling) {
2260 				f->pixelformat = sibling->fourcc;
2261 				ret = 0;
2262 			}
2263 			break;
2264 		default:
2265 			break;
2266 		}
2267 		return ret;
2268 	}
2269 }
2270 
2271 static int mxc_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
2272 				     struct v4l2_fmtdesc *f)
2273 {
2274 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2275 	u32 type = ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ?  MXC_JPEG_FMT_TYPE_ENC :
2276 							     MXC_JPEG_FMT_TYPE_RAW;
2277 	int ret;
2278 
2279 	ret = enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, type);
2280 	if (ret)
2281 		return ret;
2282 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
2283 		f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION;
2284 	return 0;
2285 }
2286 
2287 static u32 mxc_jpeg_get_fmt_type(struct mxc_jpeg_ctx *ctx, u32 type)
2288 {
2289 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
2290 		return V4L2_TYPE_IS_OUTPUT(type) ? MXC_JPEG_FMT_TYPE_ENC : MXC_JPEG_FMT_TYPE_RAW;
2291 	else
2292 		return V4L2_TYPE_IS_CAPTURE(type) ? MXC_JPEG_FMT_TYPE_ENC : MXC_JPEG_FMT_TYPE_RAW;
2293 }
2294 
2295 static u32 mxc_jpeg_get_default_fourcc(struct mxc_jpeg_ctx *ctx, u32 type)
2296 {
2297 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
2298 		return V4L2_TYPE_IS_OUTPUT(type) ? V4L2_PIX_FMT_JPEG : MXC_JPEG_DEFAULT_PFMT;
2299 	else
2300 		return V4L2_TYPE_IS_CAPTURE(type) ? V4L2_PIX_FMT_JPEG : MXC_JPEG_DEFAULT_PFMT;
2301 }
2302 
2303 static u32 mxc_jpeg_try_fourcc(struct mxc_jpeg_ctx *ctx, u32 fourcc)
2304 {
2305 	const struct mxc_jpeg_fmt *sibling;
2306 	struct mxc_jpeg_q_data *q_data_cap;
2307 
2308 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
2309 		return fourcc;
2310 	if (!ctx->header_parsed)
2311 		return fourcc;
2312 
2313 	q_data_cap = &ctx->cap_q;
2314 	if (q_data_cap->fmt->fourcc == fourcc)
2315 		return fourcc;
2316 
2317 	sibling = mxc_jpeg_get_sibling_format(q_data_cap->fmt);
2318 	if (sibling && sibling->fourcc == fourcc)
2319 		return sibling->fourcc;
2320 
2321 	return q_data_cap->fmt->fourcc;
2322 }
2323 
2324 static int mxc_jpeg_try_fmt(struct v4l2_format *f,
2325 			    struct mxc_jpeg_ctx *ctx, struct mxc_jpeg_q_data *q_data)
2326 {
2327 	const struct mxc_jpeg_fmt *fmt;
2328 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
2329 	struct v4l2_plane_pix_format *pfmt;
2330 	u32 fourcc = f->fmt.pix_mp.pixelformat;
2331 	u32 w = (pix_mp->width < MXC_JPEG_MAX_WIDTH) ?
2332 		 pix_mp->width : MXC_JPEG_MAX_WIDTH;
2333 	u32 h = (pix_mp->height < MXC_JPEG_MAX_HEIGHT) ?
2334 		 pix_mp->height : MXC_JPEG_MAX_HEIGHT;
2335 	int i;
2336 
2337 	fmt = mxc_jpeg_find_format(fourcc);
2338 	if (!fmt || fmt->flags != mxc_jpeg_get_fmt_type(ctx, f->type)) {
2339 		dev_warn(ctx->mxc_jpeg->dev, "Format not supported: %c%c%c%c, use the default.\n",
2340 			 (fourcc & 0xff),
2341 			 (fourcc >>  8) & 0xff,
2342 			 (fourcc >> 16) & 0xff,
2343 			 (fourcc >> 24) & 0xff);
2344 		fourcc = mxc_jpeg_get_default_fourcc(ctx, f->type);
2345 		fmt = mxc_jpeg_find_format(fourcc);
2346 		if (!fmt)
2347 			return -EINVAL;
2348 		f->fmt.pix_mp.pixelformat = fourcc;
2349 	}
2350 	q_data->fmt = fmt;
2351 
2352 	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
2353 	pix_mp->field = V4L2_FIELD_NONE;
2354 	pix_mp->num_planes = fmt->mem_planes;
2355 	pix_mp->pixelformat = fmt->fourcc;
2356 
2357 	q_data->w = w;
2358 	q_data->h = h;
2359 	q_data->w_adjusted = w;
2360 	q_data->h_adjusted = h;
2361 	v4l_bound_align_image(&q_data->w_adjusted,
2362 			      w, /* adjust upwards*/
2363 			      MXC_JPEG_MAX_WIDTH,
2364 			      fmt->h_align,
2365 			      &q_data->h_adjusted,
2366 			      h, /* adjust upwards*/
2367 			      MXC_JPEG_MAX_HEIGHT,
2368 			      fmt->v_align,
2369 			      0);
2370 	for (i = 0; i < pix_mp->num_planes; i++) {
2371 		pfmt = &pix_mp->plane_fmt[i];
2372 		q_data->bytesperline[i] = pfmt->bytesperline;
2373 		q_data->sizeimage[i] = pfmt->sizeimage;
2374 	}
2375 
2376 	/* calculate bytesperline & sizeimage */
2377 	mxc_jpeg_bytesperline(q_data, fmt->precision);
2378 	mxc_jpeg_sizeimage(q_data);
2379 
2380 	/* adjust user format according to our calculations */
2381 	for (i = 0; i < pix_mp->num_planes; i++) {
2382 		pfmt = &pix_mp->plane_fmt[i];
2383 		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
2384 		pfmt->bytesperline = q_data->bytesperline[i];
2385 		pfmt->sizeimage = mxc_jpeg_get_plane_size(q_data, i);
2386 	}
2387 
2388 	/* fix colorspace information to sRGB for both output & capture */
2389 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
2390 	pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601;
2391 	pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB;
2392 	/*
2393 	 * this hardware does not change the range of the samples
2394 	 * but since inside JPEG the YUV quantization is full-range,
2395 	 * this driver will always use full-range for the raw frames, too
2396 	 */
2397 	pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2398 
2399 	if (fmt->flags == MXC_JPEG_FMT_TYPE_RAW) {
2400 		q_data->crop.left = 0;
2401 		q_data->crop.top = 0;
2402 		q_data->crop.width = q_data->w;
2403 		q_data->crop.height = q_data->h;
2404 	}
2405 
2406 	pix_mp->width = q_data->w_adjusted;
2407 	pix_mp->height = q_data->h_adjusted;
2408 
2409 	return 0;
2410 }
2411 
2412 static int mxc_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
2413 				    struct v4l2_format *f)
2414 {
2415 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2416 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
2417 	struct device *dev = jpeg->dev;
2418 	struct mxc_jpeg_q_data tmp_q;
2419 
2420 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
2421 		dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type);
2422 		return -EINVAL;
2423 	}
2424 
2425 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(f->type))
2426 		f->fmt.pix_mp.pixelformat = mxc_jpeg_try_fourcc(ctx, f->fmt.pix_mp.pixelformat);
2427 
2428 	return mxc_jpeg_try_fmt(f, ctx, &tmp_q);
2429 }
2430 
2431 static int mxc_jpeg_try_fmt_vid_out(struct file *file, void *priv,
2432 				    struct v4l2_format *f)
2433 {
2434 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2435 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
2436 	struct device *dev = jpeg->dev;
2437 	struct mxc_jpeg_q_data tmp_q;
2438 
2439 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
2440 		dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type);
2441 		return -EINVAL;
2442 	}
2443 
2444 	return mxc_jpeg_try_fmt(f, ctx, &tmp_q);
2445 }
2446 
2447 static void mxc_jpeg_s_parsed_fmt(struct mxc_jpeg_ctx *ctx, struct v4l2_format *f)
2448 {
2449 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
2450 	struct mxc_jpeg_q_data *q_data_cap;
2451 
2452 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE || !V4L2_TYPE_IS_CAPTURE(f->type))
2453 		return;
2454 	if (!ctx->header_parsed)
2455 		return;
2456 
2457 	q_data_cap = mxc_jpeg_get_q_data(ctx, f->type);
2458 	pix_mp->pixelformat = mxc_jpeg_try_fourcc(ctx, pix_mp->pixelformat);
2459 	pix_mp->width = q_data_cap->w;
2460 	pix_mp->height = q_data_cap->h;
2461 }
2462 
2463 static int mxc_jpeg_s_fmt(struct mxc_jpeg_ctx *ctx,
2464 			  struct v4l2_format *f)
2465 {
2466 	struct vb2_queue *vq;
2467 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
2468 
2469 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
2470 	if (!vq)
2471 		return -EINVAL;
2472 
2473 	if (vb2_is_busy(vq)) {
2474 		v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
2475 		return -EBUSY;
2476 	}
2477 
2478 	mxc_jpeg_s_parsed_fmt(ctx, f);
2479 
2480 	return mxc_jpeg_try_fmt(f, ctx, mxc_jpeg_get_q_data(ctx, f->type));
2481 }
2482 
2483 static int mxc_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
2484 				  struct v4l2_format *f)
2485 {
2486 	return mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f);
2487 }
2488 
2489 static int mxc_jpeg_s_fmt_vid_out(struct file *file, void *priv,
2490 				  struct v4l2_format *f)
2491 {
2492 	int ret;
2493 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2494 	struct vb2_queue *dst_vq;
2495 	struct mxc_jpeg_q_data *q_data_cap;
2496 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
2497 	struct v4l2_format fc;
2498 
2499 	ret = mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f);
2500 	if (ret)
2501 		return ret;
2502 
2503 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
2504 		return 0;
2505 
2506 	dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, cap_type);
2507 	if (!dst_vq)
2508 		return -EINVAL;
2509 
2510 	if (vb2_is_busy(dst_vq))
2511 		return 0;
2512 
2513 	q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type);
2514 	if (q_data_cap->w == f->fmt.pix_mp.width && q_data_cap->h == f->fmt.pix_mp.height)
2515 		return 0;
2516 	memset(&fc, 0, sizeof(fc));
2517 	fc.type = cap_type;
2518 	fc.fmt.pix_mp.pixelformat = q_data_cap->fmt->fourcc;
2519 	fc.fmt.pix_mp.width = f->fmt.pix_mp.width;
2520 	fc.fmt.pix_mp.height = f->fmt.pix_mp.height;
2521 
2522 	return mxc_jpeg_s_fmt_vid_cap(file, priv, &fc);
2523 }
2524 
2525 static int mxc_jpeg_g_fmt_vid(struct file *file, void *priv,
2526 			      struct v4l2_format *f)
2527 {
2528 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2529 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
2530 	struct device *dev = jpeg->dev;
2531 	struct v4l2_pix_format_mplane   *pix_mp = &f->fmt.pix_mp;
2532 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type);
2533 	int i;
2534 
2535 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
2536 		dev_err(dev, "G_FMT with Invalid type: %d\n", f->type);
2537 		return -EINVAL;
2538 	}
2539 
2540 	pix_mp->pixelformat = q_data->fmt->fourcc;
2541 	pix_mp->width = q_data->w;
2542 	pix_mp->height = q_data->h;
2543 	pix_mp->field = V4L2_FIELD_NONE;
2544 	if (q_data->fmt->flags == MXC_JPEG_FMT_TYPE_RAW) {
2545 		pix_mp->width = q_data->w_adjusted;
2546 		pix_mp->height = q_data->h_adjusted;
2547 	}
2548 
2549 	/* fix colorspace information to sRGB for both output & capture */
2550 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
2551 	pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601;
2552 	pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB;
2553 	pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2554 
2555 	pix_mp->num_planes = q_data->fmt->mem_planes;
2556 	for (i = 0; i < pix_mp->num_planes; i++) {
2557 		pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i];
2558 		pix_mp->plane_fmt[i].sizeimage = mxc_jpeg_get_plane_size(q_data, i);
2559 	}
2560 
2561 	return 0;
2562 }
2563 
2564 static int mxc_jpeg_dec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2565 {
2566 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
2567 	struct mxc_jpeg_q_data *q_data_cap;
2568 
2569 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2570 		return -EINVAL;
2571 
2572 	q_data_cap = mxc_jpeg_get_q_data(ctx, s->type);
2573 
2574 	switch (s->target) {
2575 	case V4L2_SEL_TGT_COMPOSE:
2576 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
2577 		s->r = q_data_cap->crop;
2578 		break;
2579 	case V4L2_SEL_TGT_COMPOSE_PADDED:
2580 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2581 		s->r.left = 0;
2582 		s->r.top = 0;
2583 		s->r.width = q_data_cap->w_adjusted;
2584 		s->r.height = q_data_cap->h_adjusted;
2585 		break;
2586 	default:
2587 		return -EINVAL;
2588 	}
2589 
2590 	return 0;
2591 }
2592 
2593 static int mxc_jpeg_enc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2594 {
2595 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
2596 	struct mxc_jpeg_q_data *q_data_out;
2597 
2598 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2599 		return -EINVAL;
2600 
2601 	q_data_out = mxc_jpeg_get_q_data(ctx, s->type);
2602 
2603 	switch (s->target) {
2604 	case V4L2_SEL_TGT_CROP_DEFAULT:
2605 	case V4L2_SEL_TGT_CROP_BOUNDS:
2606 		s->r.left = 0;
2607 		s->r.top = 0;
2608 		s->r.width = q_data_out->w;
2609 		s->r.height = q_data_out->h;
2610 		break;
2611 	case V4L2_SEL_TGT_CROP:
2612 		s->r = q_data_out->crop;
2613 		break;
2614 	default:
2615 		return -EINVAL;
2616 	}
2617 
2618 	return 0;
2619 }
2620 
2621 static int mxc_jpeg_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2622 {
2623 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
2624 
2625 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
2626 		return mxc_jpeg_dec_g_selection(file, fh, s);
2627 	else
2628 		return mxc_jpeg_enc_g_selection(file, fh, s);
2629 }
2630 
2631 static int mxc_jpeg_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
2632 {
2633 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
2634 	struct mxc_jpeg_q_data *q_data_out;
2635 
2636 	if (ctx->mxc_jpeg->mode != MXC_JPEG_ENCODE)
2637 		return -ENOTTY;
2638 
2639 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2640 		return -EINVAL;
2641 	if (s->target != V4L2_SEL_TGT_CROP)
2642 		return -EINVAL;
2643 
2644 	q_data_out = mxc_jpeg_get_q_data(ctx, s->type);
2645 	if (s->r.left || s->r.top)
2646 		return -EINVAL;
2647 	if (s->r.width > q_data_out->w || s->r.height > q_data_out->h)
2648 		return -EINVAL;
2649 
2650 	q_data_out->crop.left = 0;
2651 	q_data_out->crop.top = 0;
2652 	q_data_out->crop.width = s->r.width;
2653 	q_data_out->crop.height = s->r.height;
2654 
2655 	return 0;
2656 }
2657 
2658 static int mxc_jpeg_subscribe_event(struct v4l2_fh *fh,
2659 				    const struct v4l2_event_subscription *sub)
2660 {
2661 	switch (sub->type) {
2662 	case V4L2_EVENT_EOS:
2663 		return v4l2_event_subscribe(fh, sub, 0, NULL);
2664 	case V4L2_EVENT_SOURCE_CHANGE:
2665 		return v4l2_src_change_event_subscribe(fh, sub);
2666 	case V4L2_EVENT_CTRL:
2667 		return v4l2_ctrl_subscribe_event(fh, sub);
2668 	default:
2669 		return -EINVAL;
2670 	}
2671 }
2672 
2673 static const struct v4l2_ioctl_ops mxc_jpeg_ioctl_ops = {
2674 	.vidioc_querycap		= mxc_jpeg_querycap,
2675 	.vidioc_enum_fmt_vid_cap	= mxc_jpeg_enum_fmt_vid_cap,
2676 	.vidioc_enum_fmt_vid_out	= mxc_jpeg_enum_fmt_vid_out,
2677 
2678 	.vidioc_try_fmt_vid_cap_mplane	= mxc_jpeg_try_fmt_vid_cap,
2679 	.vidioc_try_fmt_vid_out_mplane	= mxc_jpeg_try_fmt_vid_out,
2680 
2681 	.vidioc_s_fmt_vid_cap_mplane	= mxc_jpeg_s_fmt_vid_cap,
2682 	.vidioc_s_fmt_vid_out_mplane	= mxc_jpeg_s_fmt_vid_out,
2683 
2684 	.vidioc_g_fmt_vid_cap_mplane	= mxc_jpeg_g_fmt_vid,
2685 	.vidioc_g_fmt_vid_out_mplane	= mxc_jpeg_g_fmt_vid,
2686 
2687 	.vidioc_g_selection		= mxc_jpeg_g_selection,
2688 	.vidioc_s_selection		= mxc_jpeg_s_selection,
2689 
2690 	.vidioc_subscribe_event		= mxc_jpeg_subscribe_event,
2691 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
2692 
2693 	.vidioc_try_decoder_cmd		= v4l2_m2m_ioctl_try_decoder_cmd,
2694 	.vidioc_decoder_cmd		= mxc_jpeg_decoder_cmd,
2695 	.vidioc_try_encoder_cmd		= v4l2_m2m_ioctl_try_encoder_cmd,
2696 	.vidioc_encoder_cmd		= mxc_jpeg_encoder_cmd,
2697 
2698 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
2699 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
2700 
2701 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
2702 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
2703 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
2704 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
2705 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
2706 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
2707 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
2708 };
2709 
2710 static int mxc_jpeg_release(struct file *file)
2711 {
2712 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
2713 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(file->private_data);
2714 	struct device *dev = mxc_jpeg->dev;
2715 
2716 	mutex_lock(&mxc_jpeg->lock);
2717 	if (mxc_jpeg->mode == MXC_JPEG_DECODE)
2718 		dev_dbg(dev, "Release JPEG decoder instance on slot %d.",
2719 			ctx->slot);
2720 	else
2721 		dev_dbg(dev, "Release JPEG encoder instance on slot %d.",
2722 			ctx->slot);
2723 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2724 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2725 	v4l2_fh_del(&ctx->fh);
2726 	v4l2_fh_exit(&ctx->fh);
2727 	kfree(ctx);
2728 	mutex_unlock(&mxc_jpeg->lock);
2729 
2730 	return 0;
2731 }
2732 
2733 static const struct v4l2_file_operations mxc_jpeg_fops = {
2734 	.owner		= THIS_MODULE,
2735 	.open		= mxc_jpeg_open,
2736 	.release	= mxc_jpeg_release,
2737 	.poll		= v4l2_m2m_fop_poll,
2738 	.unlocked_ioctl	= video_ioctl2,
2739 	.mmap		= v4l2_m2m_fop_mmap,
2740 };
2741 
2742 static const struct v4l2_m2m_ops mxc_jpeg_m2m_ops = {
2743 	.job_ready      = mxc_jpeg_job_ready,
2744 	.device_run	= mxc_jpeg_device_run,
2745 };
2746 
2747 static void mxc_jpeg_detach_pm_domains(struct mxc_jpeg_dev *jpeg)
2748 {
2749 	int i;
2750 
2751 	for (i = 0; i < jpeg->num_domains; i++) {
2752 		if (!IS_ERR_OR_NULL(jpeg->pd_dev[i]) &&
2753 		    !pm_runtime_suspended(jpeg->pd_dev[i]))
2754 			pm_runtime_force_suspend(jpeg->pd_dev[i]);
2755 		if (!IS_ERR_OR_NULL(jpeg->pd_link[i]))
2756 			device_link_del(jpeg->pd_link[i]);
2757 		if (!IS_ERR_OR_NULL(jpeg->pd_dev[i]))
2758 			dev_pm_domain_detach(jpeg->pd_dev[i], true);
2759 		jpeg->pd_dev[i] = NULL;
2760 		jpeg->pd_link[i] = NULL;
2761 	}
2762 }
2763 
2764 static int mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev *jpeg)
2765 {
2766 	struct device *dev = jpeg->dev;
2767 	struct device_node *np = jpeg->pdev->dev.of_node;
2768 	int i;
2769 	int ret;
2770 
2771 	jpeg->num_domains = of_count_phandle_with_args(np, "power-domains",
2772 						       "#power-domain-cells");
2773 	if (jpeg->num_domains < 0) {
2774 		dev_err(dev, "No power domains defined for jpeg node\n");
2775 		return jpeg->num_domains;
2776 	}
2777 	if (jpeg->num_domains == 1) {
2778 		/* genpd_dev_pm_attach() attach automatically if power domains count is 1 */
2779 		jpeg->num_domains = 0;
2780 		return 0;
2781 	}
2782 
2783 	jpeg->pd_dev = devm_kmalloc_array(dev, jpeg->num_domains,
2784 					  sizeof(*jpeg->pd_dev), GFP_KERNEL);
2785 	if (!jpeg->pd_dev)
2786 		return -ENOMEM;
2787 
2788 	jpeg->pd_link = devm_kmalloc_array(dev, jpeg->num_domains,
2789 					   sizeof(*jpeg->pd_link), GFP_KERNEL);
2790 	if (!jpeg->pd_link)
2791 		return -ENOMEM;
2792 
2793 	for (i = 0; i < jpeg->num_domains; i++) {
2794 		jpeg->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i);
2795 		if (IS_ERR(jpeg->pd_dev[i])) {
2796 			ret = PTR_ERR(jpeg->pd_dev[i]);
2797 			goto fail;
2798 		}
2799 
2800 		jpeg->pd_link[i] = device_link_add(dev, jpeg->pd_dev[i],
2801 						   DL_FLAG_STATELESS |
2802 						   DL_FLAG_PM_RUNTIME);
2803 		if (!jpeg->pd_link[i]) {
2804 			ret = -EINVAL;
2805 			goto fail;
2806 		}
2807 	}
2808 
2809 	return 0;
2810 fail:
2811 	mxc_jpeg_detach_pm_domains(jpeg);
2812 	return ret;
2813 }
2814 
2815 static int mxc_jpeg_probe(struct platform_device *pdev)
2816 {
2817 	struct mxc_jpeg_dev *jpeg;
2818 	struct device *dev = &pdev->dev;
2819 	int dec_irq;
2820 	int ret;
2821 	int mode;
2822 	const struct of_device_id *of_id;
2823 
2824 	of_id = of_match_node(mxc_jpeg_match, dev->of_node);
2825 	if (!of_id)
2826 		return -ENODEV;
2827 	mode = *(const int *)of_id->data;
2828 
2829 	jpeg = devm_kzalloc(dev, sizeof(struct mxc_jpeg_dev), GFP_KERNEL);
2830 	if (!jpeg)
2831 		return -ENOMEM;
2832 
2833 	mutex_init(&jpeg->lock);
2834 	spin_lock_init(&jpeg->hw_lock);
2835 
2836 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2837 	if (ret) {
2838 		dev_err(&pdev->dev, "No suitable DMA available.\n");
2839 		goto err_irq;
2840 	}
2841 
2842 	jpeg->base_reg = devm_platform_ioremap_resource(pdev, 0);
2843 	if (IS_ERR(jpeg->base_reg))
2844 		return PTR_ERR(jpeg->base_reg);
2845 
2846 	ret = of_property_read_u32_index(pdev->dev.of_node, "slot", 0, &jpeg->slot_data.slot);
2847 	if (ret)
2848 		jpeg->slot_data.slot = 0;
2849 	dev_info(&pdev->dev, "choose slot %d\n", jpeg->slot_data.slot);
2850 	dec_irq = platform_get_irq(pdev, 0);
2851 	if (dec_irq < 0) {
2852 		ret = dec_irq;
2853 		goto err_irq;
2854 	}
2855 	ret = devm_request_irq(&pdev->dev, dec_irq, mxc_jpeg_dec_irq,
2856 			       0, pdev->name, jpeg);
2857 	if (ret) {
2858 		dev_err(&pdev->dev, "Failed to request irq %d (%d)\n",
2859 			dec_irq, ret);
2860 		goto err_irq;
2861 	}
2862 
2863 	jpeg->pdev = pdev;
2864 	jpeg->dev = dev;
2865 	jpeg->mode = mode;
2866 
2867 	/* Get clocks */
2868 	ret = devm_clk_bulk_get_all(&pdev->dev, &jpeg->clks);
2869 	if (ret < 0) {
2870 		dev_err(dev, "failed to get clock\n");
2871 		goto err_clk;
2872 	}
2873 	jpeg->num_clks = ret;
2874 
2875 	ret = mxc_jpeg_attach_pm_domains(jpeg);
2876 	if (ret < 0) {
2877 		dev_err(dev, "failed to attach power domains %d\n", ret);
2878 		goto err_clk;
2879 	}
2880 
2881 	/* v4l2 */
2882 	ret = v4l2_device_register(dev, &jpeg->v4l2_dev);
2883 	if (ret) {
2884 		dev_err(dev, "failed to register v4l2 device\n");
2885 		goto err_register;
2886 	}
2887 	jpeg->m2m_dev = v4l2_m2m_init(&mxc_jpeg_m2m_ops);
2888 	if (IS_ERR(jpeg->m2m_dev)) {
2889 		dev_err(dev, "failed to register v4l2 device\n");
2890 		ret = PTR_ERR(jpeg->m2m_dev);
2891 		goto err_m2m;
2892 	}
2893 
2894 	jpeg->dec_vdev = video_device_alloc();
2895 	if (!jpeg->dec_vdev) {
2896 		dev_err(dev, "failed to register v4l2 device\n");
2897 		ret = -ENOMEM;
2898 		goto err_vdev_alloc;
2899 	}
2900 	if (mode == MXC_JPEG_ENCODE)
2901 		snprintf(jpeg->dec_vdev->name,
2902 			 sizeof(jpeg->dec_vdev->name),
2903 			 "%s-enc", MXC_JPEG_NAME);
2904 	else
2905 		snprintf(jpeg->dec_vdev->name,
2906 			 sizeof(jpeg->dec_vdev->name),
2907 			 "%s-dec", MXC_JPEG_NAME);
2908 
2909 	jpeg->dec_vdev->fops = &mxc_jpeg_fops;
2910 	jpeg->dec_vdev->ioctl_ops = &mxc_jpeg_ioctl_ops;
2911 	jpeg->dec_vdev->minor = -1;
2912 	jpeg->dec_vdev->release = video_device_release;
2913 	jpeg->dec_vdev->lock = &jpeg->lock; /* lock for ioctl serialization */
2914 	jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
2915 	jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
2916 	jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
2917 					V4L2_CAP_VIDEO_M2M_MPLANE;
2918 	video_set_drvdata(jpeg->dec_vdev, jpeg);
2919 	if (mode == MXC_JPEG_ENCODE) {
2920 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_DECODER_CMD);
2921 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_DECODER_CMD);
2922 	} else {
2923 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_ENCODER_CMD);
2924 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_ENCODER_CMD);
2925 	}
2926 	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_VIDEO, -1);
2927 	if (ret) {
2928 		dev_err(dev, "failed to register video device\n");
2929 		goto err_vdev_register;
2930 	}
2931 	if (mode == MXC_JPEG_ENCODE)
2932 		v4l2_info(&jpeg->v4l2_dev,
2933 			  "encoder device registered as /dev/video%d (%d,%d)\n",
2934 			  jpeg->dec_vdev->num, VIDEO_MAJOR,
2935 			  jpeg->dec_vdev->minor);
2936 	else
2937 		v4l2_info(&jpeg->v4l2_dev,
2938 			  "decoder device registered as /dev/video%d (%d,%d)\n",
2939 			  jpeg->dec_vdev->num, VIDEO_MAJOR,
2940 			  jpeg->dec_vdev->minor);
2941 
2942 	platform_set_drvdata(pdev, jpeg);
2943 	pm_runtime_enable(dev);
2944 
2945 	return 0;
2946 
2947 err_vdev_register:
2948 	video_device_release(jpeg->dec_vdev);
2949 
2950 err_vdev_alloc:
2951 	v4l2_m2m_release(jpeg->m2m_dev);
2952 
2953 err_m2m:
2954 	v4l2_device_unregister(&jpeg->v4l2_dev);
2955 
2956 err_register:
2957 	mxc_jpeg_detach_pm_domains(jpeg);
2958 
2959 err_irq:
2960 err_clk:
2961 	return ret;
2962 }
2963 
2964 static int mxc_jpeg_runtime_resume(struct device *dev)
2965 {
2966 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2967 	int ret;
2968 
2969 	ret = clk_bulk_prepare_enable(jpeg->num_clks, jpeg->clks);
2970 	if (ret < 0) {
2971 		dev_err(dev, "failed to enable clock\n");
2972 		return ret;
2973 	}
2974 
2975 	return 0;
2976 }
2977 
2978 static int mxc_jpeg_runtime_suspend(struct device *dev)
2979 {
2980 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2981 
2982 	clk_bulk_disable_unprepare(jpeg->num_clks, jpeg->clks);
2983 
2984 	return 0;
2985 }
2986 
2987 static int mxc_jpeg_suspend(struct device *dev)
2988 {
2989 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2990 
2991 	v4l2_m2m_suspend(jpeg->m2m_dev);
2992 	return pm_runtime_force_suspend(dev);
2993 }
2994 
2995 static int mxc_jpeg_resume(struct device *dev)
2996 {
2997 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2998 	int ret;
2999 
3000 	ret = pm_runtime_force_resume(dev);
3001 	if (ret < 0)
3002 		return ret;
3003 
3004 	v4l2_m2m_resume(jpeg->m2m_dev);
3005 	return ret;
3006 }
3007 
3008 static const struct dev_pm_ops	mxc_jpeg_pm_ops = {
3009 	RUNTIME_PM_OPS(mxc_jpeg_runtime_suspend, mxc_jpeg_runtime_resume, NULL)
3010 	SYSTEM_SLEEP_PM_OPS(mxc_jpeg_suspend, mxc_jpeg_resume)
3011 };
3012 
3013 static void mxc_jpeg_remove(struct platform_device *pdev)
3014 {
3015 	struct mxc_jpeg_dev *jpeg = platform_get_drvdata(pdev);
3016 
3017 	mxc_jpeg_free_slot_data(jpeg);
3018 
3019 	pm_runtime_disable(&pdev->dev);
3020 	video_unregister_device(jpeg->dec_vdev);
3021 	v4l2_m2m_release(jpeg->m2m_dev);
3022 	v4l2_device_unregister(&jpeg->v4l2_dev);
3023 	mxc_jpeg_detach_pm_domains(jpeg);
3024 }
3025 
3026 MODULE_DEVICE_TABLE(of, mxc_jpeg_match);
3027 
3028 static struct platform_driver mxc_jpeg_driver = {
3029 	.probe = mxc_jpeg_probe,
3030 	.remove = mxc_jpeg_remove,
3031 	.driver = {
3032 		.name = "mxc-jpeg",
3033 		.of_match_table = mxc_jpeg_match,
3034 		.pm = pm_ptr(&mxc_jpeg_pm_ops),
3035 	},
3036 };
3037 module_platform_driver(mxc_jpeg_driver);
3038 
3039 MODULE_AUTHOR("Zhengyu Shen <zhengyu.shen_1@nxp.com>");
3040 MODULE_AUTHOR("Mirela Rabulea <mirela.rabulea@nxp.com>");
3041 MODULE_DESCRIPTION("V4L2 driver for i.MX8 QXP/QM JPEG encoder/decoder");
3042 MODULE_LICENSE("GPL v2");
3043