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 	0xFC, 0x5F, 0xA2, 0xBF, 0xCA, 0x73, 0xFE, 0xFE,
539 	0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00,
540 	0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02,
541 	0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28,
542 	0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A,
543 	0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0,
544 	0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00,
545 	0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02,
546 	0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28,
547 	0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A,
548 	0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00
549 };
550 
551 static const unsigned char jpeg_eoi[] = {
552 	0xFF, 0xD9
553 };
554 
555 struct mxc_jpeg_src_buf {
556 	/* common v4l buffer stuff -- must be first */
557 	struct vb2_v4l2_buffer	b;
558 	struct list_head	list;
559 
560 	/* mxc-jpeg specific */
561 	bool			dht_needed;
562 	bool			jpeg_parse_error;
563 	const struct mxc_jpeg_fmt	*fmt;
564 	int			w;
565 	int			h;
566 };
567 
568 static inline struct mxc_jpeg_src_buf *vb2_to_mxc_buf(struct vb2_buffer *vb)
569 {
570 	return container_of(to_vb2_v4l2_buffer(vb),
571 			    struct mxc_jpeg_src_buf, b);
572 }
573 
574 static unsigned int debug;
575 module_param(debug, int, 0644);
576 MODULE_PARM_DESC(debug, "Debug level (0-3)");
577 
578 static unsigned int hw_timeout = 2000;
579 module_param(hw_timeout, int, 0644);
580 MODULE_PARM_DESC(hw_timeout, "MXC JPEG hw timeout, the number of milliseconds");
581 
582 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision);
583 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q);
584 
585 static void _bswap16(u16 *a)
586 {
587 	*a = ((*a & 0x00FF) << 8) | ((*a & 0xFF00) >> 8);
588 }
589 
590 static void print_mxc_buf(struct mxc_jpeg_dev *jpeg, struct vb2_buffer *buf,
591 			  unsigned long len)
592 {
593 	unsigned int plane_no;
594 	u32 dma_addr;
595 	void *vaddr;
596 	unsigned long payload;
597 
598 	if (debug < 3)
599 		return;
600 
601 	for (plane_no = 0; plane_no < buf->num_planes; plane_no++) {
602 		payload = vb2_get_plane_payload(buf, plane_no);
603 		if (len == 0)
604 			len = payload;
605 		dma_addr = vb2_dma_contig_plane_dma_addr(buf, plane_no);
606 		vaddr = vb2_plane_vaddr(buf, plane_no);
607 		v4l2_dbg(3, debug, &jpeg->v4l2_dev,
608 			 "plane %d (vaddr=%p dma_addr=%x payload=%ld):",
609 			  plane_no, vaddr, dma_addr, payload);
610 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
611 			       vaddr, len, false);
612 	}
613 }
614 
615 static inline struct mxc_jpeg_ctx *mxc_jpeg_fh_to_ctx(struct v4l2_fh *fh)
616 {
617 	return container_of(fh, struct mxc_jpeg_ctx, fh);
618 }
619 
620 static int enum_fmt(const struct mxc_jpeg_fmt *mxc_formats, int n,
621 		    struct v4l2_fmtdesc *f, u32 type)
622 {
623 	int i, num = 0;
624 
625 	for (i = 0; i < n; ++i) {
626 		if (mxc_formats[i].flags == type) {
627 			/* index-th format of searched type found ? */
628 			if (num == f->index)
629 				break;
630 			/* Correct type but haven't reached our index yet,
631 			 * just increment per-type index
632 			 */
633 			++num;
634 		}
635 	}
636 
637 	/* Format not found */
638 	if (i >= n)
639 		return -EINVAL;
640 
641 	f->pixelformat = mxc_formats[i].fourcc;
642 
643 	return 0;
644 }
645 
646 static const struct mxc_jpeg_fmt *mxc_jpeg_find_format(u32 pixelformat)
647 {
648 	unsigned int k;
649 
650 	for (k = 0; k < MXC_JPEG_NUM_FORMATS; k++) {
651 		const struct mxc_jpeg_fmt *fmt = &mxc_formats[k];
652 
653 		if (fmt->fourcc == pixelformat)
654 			return fmt;
655 	}
656 	return NULL;
657 }
658 
659 static enum mxc_jpeg_image_format mxc_jpeg_fourcc_to_imgfmt(u32 fourcc)
660 {
661 	switch (fourcc) {
662 	case V4L2_PIX_FMT_GREY:
663 	case V4L2_PIX_FMT_Y012:
664 		return MXC_JPEG_GRAY;
665 	case V4L2_PIX_FMT_YUYV:
666 	case V4L2_PIX_FMT_Y212:
667 		return MXC_JPEG_YUV422;
668 	case V4L2_PIX_FMT_NV12:
669 	case V4L2_PIX_FMT_NV12M:
670 	case V4L2_PIX_FMT_P012:
671 	case V4L2_PIX_FMT_P012M:
672 		return MXC_JPEG_YUV420;
673 	case V4L2_PIX_FMT_YUV24:
674 	case V4L2_PIX_FMT_YUV48_12:
675 		return MXC_JPEG_YUV444;
676 	case V4L2_PIX_FMT_BGR24:
677 	case V4L2_PIX_FMT_BGR48_12:
678 		return MXC_JPEG_BGR;
679 	case V4L2_PIX_FMT_ABGR32:
680 	case V4L2_PIX_FMT_ABGR64_12:
681 		return MXC_JPEG_ABGR;
682 	default:
683 		return MXC_JPEG_INVALID;
684 	}
685 }
686 
687 static struct mxc_jpeg_q_data *mxc_jpeg_get_q_data(struct mxc_jpeg_ctx *ctx,
688 						   enum v4l2_buf_type type)
689 {
690 	if (V4L2_TYPE_IS_OUTPUT(type))
691 		return &ctx->out_q;
692 	return &ctx->cap_q;
693 }
694 
695 static void mxc_jpeg_addrs(struct mxc_jpeg_desc *desc,
696 			   struct vb2_buffer *raw_buf,
697 			   struct vb2_buffer *jpeg_buf, int offset)
698 {
699 	int img_fmt = desc->stm_ctrl & STM_CTRL_IMAGE_FORMAT_MASK;
700 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(raw_buf->vb2_queue);
701 	struct mxc_jpeg_q_data *q_data;
702 
703 	q_data = mxc_jpeg_get_q_data(ctx, raw_buf->type);
704 	desc->buf_base0 = vb2_dma_contig_plane_dma_addr(raw_buf, 0);
705 	desc->buf_base1 = 0;
706 	if (img_fmt == STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV420)) {
707 		if (raw_buf->num_planes == 2)
708 			desc->buf_base1 = vb2_dma_contig_plane_dma_addr(raw_buf, 1);
709 		else
710 			desc->buf_base1 = desc->buf_base0 + q_data->sizeimage[0];
711 	}
712 	desc->stm_bufbase = vb2_dma_contig_plane_dma_addr(jpeg_buf, 0) +
713 		offset;
714 }
715 
716 static bool mxc_jpeg_is_extended_sequential(const struct mxc_jpeg_fmt *fmt)
717 {
718 	if (!fmt || !(fmt->flags & MXC_JPEG_FMT_TYPE_RAW))
719 		return false;
720 
721 	if (fmt->precision > 8)
722 		return true;
723 
724 	return false;
725 }
726 
727 static void notify_eos(struct mxc_jpeg_ctx *ctx)
728 {
729 	const struct v4l2_event ev = {
730 		.type = V4L2_EVENT_EOS
731 	};
732 
733 	dev_dbg(ctx->mxc_jpeg->dev, "Notify app event EOS reached");
734 	v4l2_event_queue_fh(&ctx->fh, &ev);
735 }
736 
737 static void notify_src_chg(struct mxc_jpeg_ctx *ctx)
738 {
739 	const struct v4l2_event ev = {
740 		.type = V4L2_EVENT_SOURCE_CHANGE,
741 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
742 	};
743 
744 	dev_dbg(ctx->mxc_jpeg->dev, "Notify app event SRC_CH_RESOLUTION");
745 	v4l2_event_queue_fh(&ctx->fh, &ev);
746 }
747 
748 static int mxc_get_free_slot(struct mxc_jpeg_slot_data slot_data[], int n)
749 {
750 	int free_slot = 0;
751 
752 	while (slot_data[free_slot].used && free_slot < n)
753 		free_slot++;
754 
755 	return free_slot; /* >=n when there are no more free slots */
756 }
757 
758 static bool mxc_jpeg_alloc_slot_data(struct mxc_jpeg_dev *jpeg,
759 				     unsigned int slot)
760 {
761 	struct mxc_jpeg_desc *desc;
762 	struct mxc_jpeg_desc *cfg_desc;
763 	void *cfg_stm;
764 
765 	if (jpeg->slot_data[slot].desc)
766 		goto skip_alloc; /* already allocated, reuse it */
767 
768 	/* allocate descriptor for decoding/encoding phase */
769 	desc = dma_alloc_coherent(jpeg->dev,
770 				  sizeof(struct mxc_jpeg_desc),
771 				  &jpeg->slot_data[slot].desc_handle,
772 				  GFP_ATOMIC);
773 	if (!desc)
774 		goto err;
775 	jpeg->slot_data[slot].desc = desc;
776 
777 	/* allocate descriptor for configuration phase (encoder only) */
778 	cfg_desc = dma_alloc_coherent(jpeg->dev,
779 				      sizeof(struct mxc_jpeg_desc),
780 				      &jpeg->slot_data[slot].cfg_desc_handle,
781 				      GFP_ATOMIC);
782 	if (!cfg_desc)
783 		goto err;
784 	jpeg->slot_data[slot].cfg_desc = cfg_desc;
785 
786 	/* allocate configuration stream */
787 	cfg_stm = dma_alloc_coherent(jpeg->dev,
788 				     MXC_JPEG_MAX_CFG_STREAM,
789 				     &jpeg->slot_data[slot].cfg_stream_handle,
790 				     GFP_ATOMIC);
791 	if (!cfg_stm)
792 		goto err;
793 	jpeg->slot_data[slot].cfg_stream_vaddr = cfg_stm;
794 
795 skip_alloc:
796 	jpeg->slot_data[slot].used = true;
797 
798 	return true;
799 err:
800 	dev_err(jpeg->dev, "Could not allocate descriptors for slot %d", slot);
801 
802 	return false;
803 }
804 
805 static void mxc_jpeg_free_slot_data(struct mxc_jpeg_dev *jpeg,
806 				    unsigned int slot)
807 {
808 	if (slot >= MXC_MAX_SLOTS) {
809 		dev_err(jpeg->dev, "Invalid slot %d, nothing to free.", slot);
810 		return;
811 	}
812 
813 	/* free descriptor for decoding/encoding phase */
814 	dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc),
815 			  jpeg->slot_data[slot].desc,
816 			  jpeg->slot_data[slot].desc_handle);
817 
818 	/* free descriptor for encoder configuration phase / decoder DHT */
819 	dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc),
820 			  jpeg->slot_data[slot].cfg_desc,
821 			  jpeg->slot_data[slot].cfg_desc_handle);
822 
823 	/* free configuration stream */
824 	dma_free_coherent(jpeg->dev, MXC_JPEG_MAX_CFG_STREAM,
825 			  jpeg->slot_data[slot].cfg_stream_vaddr,
826 			  jpeg->slot_data[slot].cfg_stream_handle);
827 
828 	jpeg->slot_data[slot].used = false;
829 }
830 
831 static void mxc_jpeg_check_and_set_last_buffer(struct mxc_jpeg_ctx *ctx,
832 					       struct vb2_v4l2_buffer *src_buf,
833 					       struct vb2_v4l2_buffer *dst_buf)
834 {
835 	if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) {
836 		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
837 		v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx);
838 		notify_eos(ctx);
839 		ctx->header_parsed = false;
840 	}
841 }
842 
843 static void mxc_jpeg_job_finish(struct mxc_jpeg_ctx *ctx, enum vb2_buffer_state state, bool reset)
844 {
845 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
846 	void __iomem *reg = jpeg->base_reg;
847 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
848 
849 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
850 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
851 	mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf);
852 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
853 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
854 	v4l2_m2m_buf_done(src_buf, state);
855 	v4l2_m2m_buf_done(dst_buf, state);
856 
857 	mxc_jpeg_disable_irq(reg, ctx->slot);
858 	ctx->mxc_jpeg->slot_data[ctx->slot].used = false;
859 	if (reset)
860 		mxc_jpeg_sw_reset(reg);
861 }
862 
863 static u32 mxc_jpeg_get_plane_size(struct mxc_jpeg_q_data *q_data, u32 plane_no)
864 {
865 	const struct mxc_jpeg_fmt *fmt = q_data->fmt;
866 	u32 size;
867 	int i;
868 
869 	if (plane_no >= fmt->mem_planes)
870 		return 0;
871 
872 	if (fmt->mem_planes == fmt->comp_planes)
873 		return q_data->sizeimage[plane_no];
874 
875 	if (plane_no < fmt->mem_planes - 1)
876 		return q_data->sizeimage[plane_no];
877 
878 	size = q_data->sizeimage[fmt->mem_planes - 1];
879 
880 	/* Should be impossible given mxc_formats. */
881 	if (WARN_ON_ONCE(fmt->comp_planes > ARRAY_SIZE(q_data->sizeimage)))
882 		return size;
883 
884 	for (i = fmt->mem_planes; i < fmt->comp_planes; i++)
885 		size += q_data->sizeimage[i];
886 
887 	return size;
888 }
889 
890 static irqreturn_t mxc_jpeg_dec_irq(int irq, void *priv)
891 {
892 	struct mxc_jpeg_dev *jpeg = priv;
893 	struct mxc_jpeg_ctx *ctx;
894 	void __iomem *reg = jpeg->base_reg;
895 	struct device *dev = jpeg->dev;
896 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
897 	struct mxc_jpeg_src_buf *jpeg_src_buf;
898 	enum vb2_buffer_state buf_state;
899 	u32 dec_ret, com_status;
900 	unsigned long payload;
901 	struct mxc_jpeg_q_data *q_data;
902 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
903 	unsigned int slot;
904 
905 	spin_lock(&jpeg->hw_lock);
906 
907 	com_status = readl(reg + COM_STATUS);
908 	slot = COM_STATUS_CUR_SLOT(com_status);
909 	dev_dbg(dev, "Irq %d on slot %d.\n", irq, slot);
910 
911 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
912 	if (WARN_ON(!ctx))
913 		goto job_unlock;
914 
915 	if (slot != ctx->slot) {
916 		/* TODO investigate when adding multi-instance support */
917 		dev_warn(dev, "IRQ slot %d != context slot %d.\n",
918 			 slot, ctx->slot);
919 		goto job_unlock;
920 	}
921 
922 	if (!jpeg->slot_data[slot].used)
923 		goto job_unlock;
924 
925 	dec_ret = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS));
926 	writel(dec_ret, reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS)); /* w1c */
927 
928 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
929 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
930 	if (!dst_buf || !src_buf) {
931 		dev_err(dev, "No source or destination buffer.\n");
932 		goto job_unlock;
933 	}
934 	jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf);
935 
936 	if (dec_ret & SLOT_STATUS_ENC_CONFIG_ERR) {
937 		u32 ret = readl(reg + CAST_STATUS12);
938 
939 		dev_err(dev, "Encoder/decoder error, dec_ret = 0x%08x, status=0x%08x",
940 			dec_ret, ret);
941 		mxc_jpeg_clr_desc(reg, slot);
942 		mxc_jpeg_sw_reset(reg);
943 		buf_state = VB2_BUF_STATE_ERROR;
944 		goto buffers_done;
945 	}
946 
947 	if (!(dec_ret & SLOT_STATUS_FRMDONE))
948 		goto job_unlock;
949 
950 	if (jpeg->mode == MXC_JPEG_ENCODE &&
951 	    ctx->enc_state == MXC_JPEG_ENC_CONF) {
952 		q_data = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
953 		ctx->enc_state = MXC_JPEG_ENCODING;
954 		dev_dbg(dev, "Encoder config finished. Start encoding...\n");
955 		mxc_jpeg_enc_set_quality(dev, reg, ctx->jpeg_quality);
956 		mxc_jpeg_enc_mode_go(dev, reg, mxc_jpeg_is_extended_sequential(q_data->fmt));
957 		goto job_unlock;
958 	}
959 	if (jpeg->mode == MXC_JPEG_DECODE && jpeg_src_buf->dht_needed) {
960 		jpeg_src_buf->dht_needed = false;
961 		dev_dbg(dev, "Decoder DHT cfg finished. Start decoding...\n");
962 		goto job_unlock;
963 	}
964 
965 	if (jpeg->mode == MXC_JPEG_ENCODE) {
966 		payload = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_BUF_PTR));
967 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
968 		dev_dbg(dev, "Encoding finished, payload size: %ld\n",
969 			payload);
970 	} else {
971 		q_data = mxc_jpeg_get_q_data(ctx, cap_type);
972 		payload = mxc_jpeg_get_plane_size(q_data, 0);
973 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
974 		vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
975 		if (q_data->fmt->mem_planes == 2) {
976 			payload = mxc_jpeg_get_plane_size(q_data, 1);
977 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, payload);
978 		}
979 		dev_dbg(dev, "Decoding finished, payload size: %ld + %ld\n",
980 			vb2_get_plane_payload(&dst_buf->vb2_buf, 0),
981 			vb2_get_plane_payload(&dst_buf->vb2_buf, 1));
982 	}
983 
984 	/* short preview of the results */
985 	dev_dbg(dev, "src_buf preview: ");
986 	print_mxc_buf(jpeg, &src_buf->vb2_buf, 32);
987 	dev_dbg(dev, "dst_buf preview: ");
988 	print_mxc_buf(jpeg, &dst_buf->vb2_buf, 32);
989 	buf_state = VB2_BUF_STATE_DONE;
990 
991 buffers_done:
992 	mxc_jpeg_job_finish(ctx, buf_state, false);
993 	spin_unlock(&jpeg->hw_lock);
994 	cancel_delayed_work(&ctx->task_timer);
995 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
996 	return IRQ_HANDLED;
997 job_unlock:
998 	spin_unlock(&jpeg->hw_lock);
999 	return IRQ_HANDLED;
1000 }
1001 
1002 static int mxc_jpeg_fixup_sof(struct mxc_jpeg_sof *sof,
1003 			      u32 fourcc,
1004 			      u16 w, u16 h)
1005 {
1006 	int sof_length;
1007 	const struct mxc_jpeg_fmt *fmt = mxc_jpeg_find_format(fourcc);
1008 
1009 	if (fmt)
1010 		sof->precision = fmt->precision;
1011 	else
1012 		sof->precision = 8; /* TODO allow 8/12 bit precision*/
1013 	sof->height = h;
1014 	_bswap16(&sof->height);
1015 	sof->width = w;
1016 	_bswap16(&sof->width);
1017 
1018 	switch (fourcc) {
1019 	case V4L2_PIX_FMT_NV12:
1020 	case V4L2_PIX_FMT_NV12M:
1021 	case V4L2_PIX_FMT_P012:
1022 	case V4L2_PIX_FMT_P012M:
1023 		sof->components_no = 3;
1024 		sof->comp[0].v = 0x2;
1025 		sof->comp[0].h = 0x2;
1026 		break;
1027 	case V4L2_PIX_FMT_YUYV:
1028 	case V4L2_PIX_FMT_Y212:
1029 		sof->components_no = 3;
1030 		sof->comp[0].v = 0x1;
1031 		sof->comp[0].h = 0x2;
1032 		break;
1033 	case V4L2_PIX_FMT_YUV24:
1034 	case V4L2_PIX_FMT_YUV48_12:
1035 	case V4L2_PIX_FMT_BGR24:
1036 	case V4L2_PIX_FMT_BGR48_12:
1037 	default:
1038 		sof->components_no = 3;
1039 		break;
1040 	case V4L2_PIX_FMT_ABGR32:
1041 	case V4L2_PIX_FMT_ABGR64_12:
1042 		sof->components_no = 4;
1043 		break;
1044 	case V4L2_PIX_FMT_GREY:
1045 	case V4L2_PIX_FMT_Y012:
1046 		sof->components_no = 1;
1047 		break;
1048 	}
1049 	sof_length = 8 + 3 * sof->components_no;
1050 	sof->length = sof_length;
1051 	_bswap16(&sof->length);
1052 
1053 	return sof_length; /* not swaped */
1054 }
1055 
1056 static int mxc_jpeg_fixup_sos(struct mxc_jpeg_sos *sos,
1057 			      u32 fourcc)
1058 {
1059 	int sos_length;
1060 	u8 *sof_u8 = (u8 *)sos;
1061 
1062 	switch (fourcc) {
1063 	case V4L2_PIX_FMT_NV12:
1064 	case V4L2_PIX_FMT_NV12M:
1065 	case V4L2_PIX_FMT_P012:
1066 	case V4L2_PIX_FMT_P012M:
1067 		sos->components_no = 3;
1068 		break;
1069 	case V4L2_PIX_FMT_YUYV:
1070 	case V4L2_PIX_FMT_Y212:
1071 		sos->components_no = 3;
1072 		break;
1073 	case V4L2_PIX_FMT_YUV24:
1074 	case V4L2_PIX_FMT_YUV48_12:
1075 	case V4L2_PIX_FMT_BGR24:
1076 	case V4L2_PIX_FMT_BGR48_12:
1077 	default:
1078 		sos->components_no = 3;
1079 		break;
1080 	case V4L2_PIX_FMT_ABGR32:
1081 	case V4L2_PIX_FMT_ABGR64_12:
1082 		sos->components_no = 4;
1083 		break;
1084 	case V4L2_PIX_FMT_GREY:
1085 	case V4L2_PIX_FMT_Y012:
1086 		sos->components_no = 1;
1087 		break;
1088 	}
1089 	sos_length = 6 + 2 * sos->components_no;
1090 	sos->length = sos_length;
1091 	_bswap16(&sos->length);
1092 
1093 	/* SOS ignorable bytes, not so ignorable after all */
1094 	sof_u8[sos_length - 1] = 0x0;
1095 	sof_u8[sos_length - 2] = 0x3f;
1096 	sof_u8[sos_length - 3] = 0x0;
1097 
1098 	return sos_length; /* not swaped */
1099 }
1100 
1101 static unsigned int mxc_jpeg_setup_cfg_stream(void *cfg_stream_vaddr,
1102 					      u32 fourcc,
1103 					      u16 w, u16 h)
1104 {
1105 	/*
1106 	 * There is a hardware issue that first 128 bytes of configuration data
1107 	 * can't be loaded correctly.
1108 	 * To avoid this issue, we need to write the configuration from
1109 	 * an offset which should be no less than 0x80 (128 bytes).
1110 	 */
1111 	unsigned int offset = 0x80;
1112 	u8 *cfg = (u8 *)cfg_stream_vaddr;
1113 	struct mxc_jpeg_sof *sof;
1114 	struct mxc_jpeg_sos *sos;
1115 	const struct mxc_jpeg_fmt *fmt = mxc_jpeg_find_format(fourcc);
1116 
1117 	if (!fmt)
1118 		return 0;
1119 
1120 	memcpy(cfg + offset, jpeg_soi, ARRAY_SIZE(jpeg_soi));
1121 	offset += ARRAY_SIZE(jpeg_soi);
1122 
1123 	if (fmt->is_rgb) {
1124 		memcpy(cfg + offset, jpeg_app14, sizeof(jpeg_app14));
1125 		offset += sizeof(jpeg_app14);
1126 	} else {
1127 		memcpy(cfg + offset, jpeg_app0, sizeof(jpeg_app0));
1128 		offset += sizeof(jpeg_app0);
1129 	}
1130 
1131 	if (mxc_jpeg_is_extended_sequential(fmt)) {
1132 		memcpy(cfg + offset, jpeg_dqt_extseq, sizeof(jpeg_dqt_extseq));
1133 		offset += sizeof(jpeg_dqt_extseq);
1134 
1135 		memcpy(cfg + offset, jpeg_sof_extseq, sizeof(jpeg_sof_extseq));
1136 	} else {
1137 		memcpy(cfg + offset, jpeg_dqt, sizeof(jpeg_dqt));
1138 		offset += sizeof(jpeg_dqt);
1139 
1140 		memcpy(cfg + offset, jpeg_sof_maximal, sizeof(jpeg_sof_maximal));
1141 	}
1142 	offset += 2; /* skip marker ID */
1143 	sof = (struct mxc_jpeg_sof *)(cfg + offset);
1144 	offset += mxc_jpeg_fixup_sof(sof, fourcc, w, h);
1145 
1146 	if (mxc_jpeg_is_extended_sequential(fmt)) {
1147 		memcpy(cfg + offset, jpeg_dht_extseq, sizeof(jpeg_dht_extseq));
1148 		offset += sizeof(jpeg_dht_extseq);
1149 	} else {
1150 		memcpy(cfg + offset, jpeg_dht, sizeof(jpeg_dht));
1151 		offset += sizeof(jpeg_dht);
1152 	}
1153 
1154 	memcpy(cfg + offset, jpeg_dri, sizeof(jpeg_dri));
1155 	offset += sizeof(jpeg_dri);
1156 
1157 	memcpy(cfg + offset, jpeg_sos_maximal, sizeof(jpeg_sos_maximal));
1158 	offset += 2; /* skip marker ID */
1159 	sos = (struct mxc_jpeg_sos *)(cfg + offset);
1160 	offset += mxc_jpeg_fixup_sos(sos, fourcc);
1161 
1162 	memcpy(cfg + offset, jpeg_image_red, sizeof(jpeg_image_red));
1163 	offset += sizeof(jpeg_image_red);
1164 
1165 	memcpy(cfg + offset, jpeg_eoi, sizeof(jpeg_eoi));
1166 	offset += sizeof(jpeg_eoi);
1167 
1168 	return offset;
1169 }
1170 
1171 static void mxc_jpeg_config_dec_desc(struct vb2_buffer *out_buf,
1172 				     struct mxc_jpeg_ctx *ctx,
1173 				     struct vb2_buffer *src_buf,
1174 				     struct vb2_buffer *dst_buf)
1175 {
1176 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1177 	struct mxc_jpeg_q_data *q_data_cap;
1178 	enum mxc_jpeg_image_format img_fmt;
1179 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1180 	void __iomem *reg = jpeg->base_reg;
1181 	unsigned int slot = ctx->slot;
1182 	struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc;
1183 	struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc;
1184 	dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle;
1185 	dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle;
1186 	dma_addr_t cfg_stream_handle = jpeg->slot_data[slot].cfg_stream_handle;
1187 	unsigned int *cfg_size = &jpeg->slot_data[slot].cfg_stream_size;
1188 	void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr;
1189 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1190 
1191 	jpeg_src_buf = vb2_to_mxc_buf(src_buf);
1192 
1193 	/* setup the decoding descriptor */
1194 	desc->next_descpt_ptr = 0; /* end of chain */
1195 	q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type);
1196 	desc->imgsize = q_data_cap->w_adjusted << 16 | q_data_cap->h_adjusted;
1197 	img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data_cap->fmt->fourcc);
1198 	desc->stm_ctrl &= ~STM_CTRL_IMAGE_FORMAT(0xF); /* clear image format */
1199 	desc->stm_ctrl |= STM_CTRL_IMAGE_FORMAT(img_fmt);
1200 	desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
1201 	if (mxc_jpeg_is_extended_sequential(jpeg_src_buf->fmt))
1202 		desc->stm_ctrl |= STM_CTRL_PIXEL_PRECISION;
1203 	else
1204 		desc->stm_ctrl &= ~STM_CTRL_PIXEL_PRECISION;
1205 	desc->line_pitch = q_data_cap->bytesperline[0];
1206 	mxc_jpeg_addrs(desc, dst_buf, src_buf, 0);
1207 	mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(src_buf, 0), 1024));
1208 	print_descriptor_info(jpeg->dev, desc);
1209 
1210 	if (!jpeg_src_buf->dht_needed) {
1211 		/* validate the decoding descriptor */
1212 		mxc_jpeg_set_desc(desc_handle, reg, slot);
1213 		return;
1214 	}
1215 
1216 	/*
1217 	 * if a default huffman table is needed, use the config descriptor to
1218 	 * inject a DHT, by chaining it before the decoding descriptor
1219 	 */
1220 	*cfg_size = mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr,
1221 					      V4L2_PIX_FMT_YUYV,
1222 					      MXC_JPEG_MIN_WIDTH,
1223 					      MXC_JPEG_MIN_HEIGHT);
1224 	cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN;
1225 	cfg_desc->buf_base0 = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1226 	cfg_desc->buf_base1 = 0;
1227 	cfg_desc->imgsize = MXC_JPEG_MIN_WIDTH << 16;
1228 	cfg_desc->imgsize |= MXC_JPEG_MIN_HEIGHT;
1229 	cfg_desc->line_pitch = MXC_JPEG_MIN_WIDTH * 2;
1230 	cfg_desc->stm_ctrl = STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV422);
1231 	cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
1232 	cfg_desc->stm_bufbase = cfg_stream_handle;
1233 	cfg_desc->stm_bufsize = ALIGN(*cfg_size, 1024);
1234 	print_descriptor_info(jpeg->dev, cfg_desc);
1235 
1236 	/* validate the configuration descriptor */
1237 	mxc_jpeg_set_desc(cfg_desc_handle, reg, slot);
1238 }
1239 
1240 static void mxc_jpeg_config_enc_desc(struct vb2_buffer *out_buf,
1241 				     struct mxc_jpeg_ctx *ctx,
1242 				     struct vb2_buffer *src_buf,
1243 				     struct vb2_buffer *dst_buf)
1244 {
1245 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1246 	void __iomem *reg = jpeg->base_reg;
1247 	unsigned int slot = ctx->slot;
1248 	struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc;
1249 	struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc;
1250 	dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle;
1251 	dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle;
1252 	void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr;
1253 	struct mxc_jpeg_q_data *q_data;
1254 	enum mxc_jpeg_image_format img_fmt;
1255 	int w, h;
1256 
1257 	q_data = mxc_jpeg_get_q_data(ctx, src_buf->vb2_queue->type);
1258 
1259 	jpeg->slot_data[slot].cfg_stream_size =
1260 			mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr,
1261 						  q_data->fmt->fourcc,
1262 						  q_data->crop.width,
1263 						  q_data->crop.height);
1264 
1265 	/* chain the config descriptor with the encoding descriptor */
1266 	cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN;
1267 
1268 	cfg_desc->buf_base0 = jpeg->slot_data[slot].cfg_stream_handle;
1269 	cfg_desc->buf_base1 = 0;
1270 	cfg_desc->line_pitch = 0;
1271 	cfg_desc->stm_bufbase = 0; /* no output expected */
1272 	cfg_desc->stm_bufsize = 0x0;
1273 	cfg_desc->imgsize = 0;
1274 	cfg_desc->stm_ctrl = STM_CTRL_CONFIG_MOD(1);
1275 	cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
1276 
1277 	desc->next_descpt_ptr = 0; /* end of chain */
1278 
1279 	/* use adjusted resolution for CAST IP job */
1280 	w = q_data->crop.width;
1281 	h = q_data->crop.height;
1282 	v4l_bound_align_image(&w, w, MXC_JPEG_MAX_WIDTH, q_data->fmt->h_align,
1283 			      &h, h, MXC_JPEG_MAX_HEIGHT, q_data->fmt->v_align, 0);
1284 	mxc_jpeg_set_res(desc, w, h);
1285 	mxc_jpeg_set_line_pitch(desc, q_data->bytesperline[0]);
1286 	mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(dst_buf, 0), 1024));
1287 	img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data->fmt->fourcc);
1288 	if (img_fmt == MXC_JPEG_INVALID)
1289 		dev_err(jpeg->dev, "No valid image format detected\n");
1290 	desc->stm_ctrl = STM_CTRL_CONFIG_MOD(0) |
1291 			 STM_CTRL_IMAGE_FORMAT(img_fmt);
1292 	desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
1293 	if (mxc_jpeg_is_extended_sequential(q_data->fmt))
1294 		desc->stm_ctrl |= STM_CTRL_PIXEL_PRECISION;
1295 	else
1296 		desc->stm_ctrl &= ~STM_CTRL_PIXEL_PRECISION;
1297 	mxc_jpeg_addrs(desc, src_buf, dst_buf, 0);
1298 	dev_dbg(jpeg->dev, "cfg_desc:\n");
1299 	print_descriptor_info(jpeg->dev, cfg_desc);
1300 	dev_dbg(jpeg->dev, "enc desc:\n");
1301 	print_descriptor_info(jpeg->dev, desc);
1302 	print_wrapper_info(jpeg->dev, reg);
1303 	print_cast_status(jpeg->dev, reg, MXC_JPEG_ENCODE);
1304 
1305 	/* validate the configuration descriptor */
1306 	mxc_jpeg_set_desc(cfg_desc_handle, reg, slot);
1307 }
1308 
1309 static const struct mxc_jpeg_fmt *mxc_jpeg_get_sibling_format(const struct mxc_jpeg_fmt *fmt)
1310 {
1311 	int i;
1312 
1313 	for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++) {
1314 		if (mxc_formats[i].subsampling == fmt->subsampling &&
1315 		    mxc_formats[i].nc == fmt->nc &&
1316 		    mxc_formats[i].precision == fmt->precision &&
1317 		    mxc_formats[i].is_rgb == fmt->is_rgb &&
1318 		    mxc_formats[i].fourcc != fmt->fourcc)
1319 			return &mxc_formats[i];
1320 	}
1321 
1322 	return NULL;
1323 }
1324 
1325 static bool mxc_jpeg_compare_format(const struct mxc_jpeg_fmt *fmt1,
1326 				    const struct mxc_jpeg_fmt *fmt2)
1327 {
1328 	if (fmt1 == fmt2)
1329 		return true;
1330 	if (mxc_jpeg_get_sibling_format(fmt1) == fmt2)
1331 		return true;
1332 	return false;
1333 }
1334 
1335 static bool mxc_jpeg_source_change(struct mxc_jpeg_ctx *ctx,
1336 				   struct mxc_jpeg_src_buf *jpeg_src_buf)
1337 {
1338 	struct device *dev = ctx->mxc_jpeg->dev;
1339 	struct mxc_jpeg_q_data *q_data_cap;
1340 
1341 	if (!jpeg_src_buf->fmt)
1342 		return false;
1343 
1344 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1345 	if (mxc_jpeg_compare_format(q_data_cap->fmt, jpeg_src_buf->fmt))
1346 		jpeg_src_buf->fmt = q_data_cap->fmt;
1347 	if (q_data_cap->fmt != jpeg_src_buf->fmt ||
1348 	    q_data_cap->w != jpeg_src_buf->w ||
1349 	    q_data_cap->h != jpeg_src_buf->h) {
1350 		dev_dbg(dev, "Detected jpeg res=(%dx%d)->(%dx%d), pixfmt=%c%c%c%c\n",
1351 			q_data_cap->w, q_data_cap->h,
1352 			jpeg_src_buf->w, jpeg_src_buf->h,
1353 			(jpeg_src_buf->fmt->fourcc & 0xff),
1354 			(jpeg_src_buf->fmt->fourcc >>  8) & 0xff,
1355 			(jpeg_src_buf->fmt->fourcc >> 16) & 0xff,
1356 			(jpeg_src_buf->fmt->fourcc >> 24) & 0xff);
1357 
1358 		/*
1359 		 * set-up the capture queue with the pixelformat and resolution
1360 		 * detected from the jpeg output stream
1361 		 */
1362 		q_data_cap->w = jpeg_src_buf->w;
1363 		q_data_cap->h = jpeg_src_buf->h;
1364 		q_data_cap->fmt = jpeg_src_buf->fmt;
1365 		q_data_cap->w_adjusted = q_data_cap->w;
1366 		q_data_cap->h_adjusted = q_data_cap->h;
1367 		q_data_cap->crop.left = 0;
1368 		q_data_cap->crop.top = 0;
1369 		q_data_cap->crop.width = jpeg_src_buf->w;
1370 		q_data_cap->crop.height = jpeg_src_buf->h;
1371 
1372 		/*
1373 		 * align up the resolution for CAST IP,
1374 		 * but leave the buffer resolution unchanged
1375 		 */
1376 		v4l_bound_align_image(&q_data_cap->w_adjusted,
1377 				      q_data_cap->w_adjusted,  /* adjust up */
1378 				      MXC_JPEG_MAX_WIDTH,
1379 				      q_data_cap->fmt->h_align,
1380 				      &q_data_cap->h_adjusted,
1381 				      q_data_cap->h_adjusted, /* adjust up */
1382 				      MXC_JPEG_MAX_HEIGHT,
1383 				      q_data_cap->fmt->v_align,
1384 				      0);
1385 
1386 		/* setup bytesperline/sizeimage for capture queue */
1387 		mxc_jpeg_bytesperline(q_data_cap, jpeg_src_buf->fmt->precision);
1388 		mxc_jpeg_sizeimage(q_data_cap);
1389 		notify_src_chg(ctx);
1390 		ctx->source_change = 1;
1391 	}
1392 
1393 	return ctx->source_change ? true : false;
1394 }
1395 
1396 static int mxc_jpeg_job_ready(void *priv)
1397 {
1398 	struct mxc_jpeg_ctx *ctx = priv;
1399 
1400 	return ctx->source_change ? 0 : 1;
1401 }
1402 
1403 static void mxc_jpeg_device_run_timeout(struct work_struct *work)
1404 {
1405 	struct delayed_work *dwork = to_delayed_work(work);
1406 	struct mxc_jpeg_ctx *ctx = container_of(dwork, struct mxc_jpeg_ctx, task_timer);
1407 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1408 	unsigned long flags;
1409 
1410 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1411 	if (ctx->slot < MXC_MAX_SLOTS && ctx->mxc_jpeg->slot_data[ctx->slot].used) {
1412 		dev_warn(jpeg->dev, "%s timeout, cancel it\n",
1413 			 ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ? "decode" : "encode");
1414 		mxc_jpeg_job_finish(ctx, VB2_BUF_STATE_ERROR, true);
1415 		v4l2_m2m_job_finish(ctx->mxc_jpeg->m2m_dev, ctx->fh.m2m_ctx);
1416 	}
1417 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1418 }
1419 
1420 static void mxc_jpeg_device_run(void *priv)
1421 {
1422 	struct mxc_jpeg_ctx *ctx = priv;
1423 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1424 	void __iomem *reg = jpeg->base_reg;
1425 	struct device *dev = jpeg->dev;
1426 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1427 	unsigned long flags;
1428 	struct mxc_jpeg_q_data *q_data_cap, *q_data_out;
1429 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1430 
1431 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1432 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1433 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1434 	if (!src_buf || !dst_buf) {
1435 		dev_err(dev, "Null src or dst buf\n");
1436 		goto end;
1437 	}
1438 
1439 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1440 	if (!q_data_cap)
1441 		goto end;
1442 	q_data_out = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1443 	if (!q_data_out)
1444 		goto end;
1445 	src_buf->sequence = q_data_out->sequence++;
1446 	dst_buf->sequence = q_data_cap->sequence++;
1447 
1448 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
1449 
1450 	jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf);
1451 	if (q_data_cap->fmt->mem_planes != dst_buf->vb2_buf.num_planes) {
1452 		dev_err(dev, "Capture format %s has %d planes, but capture buffer has %d planes\n",
1453 			q_data_cap->fmt->name, q_data_cap->fmt->mem_planes,
1454 			dst_buf->vb2_buf.num_planes);
1455 		jpeg_src_buf->jpeg_parse_error = true;
1456 	}
1457 	if (jpeg_src_buf->jpeg_parse_error) {
1458 		mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf);
1459 		v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1460 		v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1461 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1462 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1463 		spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1464 		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1465 
1466 		return;
1467 	}
1468 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) {
1469 		if (ctx->source_change || mxc_jpeg_source_change(ctx, jpeg_src_buf)) {
1470 			spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1471 			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1472 			return;
1473 		}
1474 	}
1475 
1476 	mxc_jpeg_enable(reg);
1477 	mxc_jpeg_set_l_endian(reg, 1);
1478 
1479 	ctx->slot = mxc_get_free_slot(jpeg->slot_data, MXC_MAX_SLOTS);
1480 	if (ctx->slot >= MXC_MAX_SLOTS) {
1481 		dev_err(dev, "No more free slots\n");
1482 		goto end;
1483 	}
1484 	if (!mxc_jpeg_alloc_slot_data(jpeg, ctx->slot)) {
1485 		dev_err(dev, "Cannot allocate slot data\n");
1486 		goto end;
1487 	}
1488 
1489 	mxc_jpeg_enable_slot(reg, ctx->slot);
1490 	mxc_jpeg_enable_irq(reg, ctx->slot);
1491 
1492 	if (jpeg->mode == MXC_JPEG_ENCODE) {
1493 		dev_dbg(dev, "Encoding on slot %d\n", ctx->slot);
1494 		ctx->enc_state = MXC_JPEG_ENC_CONF;
1495 		mxc_jpeg_config_enc_desc(&dst_buf->vb2_buf, ctx,
1496 					 &src_buf->vb2_buf, &dst_buf->vb2_buf);
1497 		/* start config phase */
1498 		mxc_jpeg_enc_mode_conf(dev, reg,
1499 				       mxc_jpeg_is_extended_sequential(q_data_out->fmt));
1500 	} else {
1501 		dev_dbg(dev, "Decoding on slot %d\n", ctx->slot);
1502 		print_mxc_buf(jpeg, &src_buf->vb2_buf, 0);
1503 		mxc_jpeg_config_dec_desc(&dst_buf->vb2_buf, ctx,
1504 					 &src_buf->vb2_buf, &dst_buf->vb2_buf);
1505 		mxc_jpeg_dec_mode_go(dev, reg);
1506 	}
1507 	schedule_delayed_work(&ctx->task_timer, msecs_to_jiffies(hw_timeout));
1508 end:
1509 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1510 }
1511 
1512 static int mxc_jpeg_decoder_cmd(struct file *file, void *priv,
1513 				struct v4l2_decoder_cmd *cmd)
1514 {
1515 	struct v4l2_fh *fh = file->private_data;
1516 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
1517 	unsigned long flags;
1518 	int ret;
1519 
1520 	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
1521 	if (ret < 0)
1522 		return ret;
1523 
1524 	if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx)))
1525 		return 0;
1526 
1527 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1528 	ret = v4l2_m2m_ioctl_decoder_cmd(file, priv, cmd);
1529 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1530 	if (ret < 0)
1531 		return ret;
1532 
1533 	if (cmd->cmd == V4L2_DEC_CMD_STOP &&
1534 	    v4l2_m2m_has_stopped(fh->m2m_ctx)) {
1535 		notify_eos(ctx);
1536 		ctx->header_parsed = false;
1537 	}
1538 
1539 	if (cmd->cmd == V4L2_DEC_CMD_START &&
1540 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1541 		vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q);
1542 	return 0;
1543 }
1544 
1545 static int mxc_jpeg_encoder_cmd(struct file *file, void *priv,
1546 				struct v4l2_encoder_cmd *cmd)
1547 {
1548 	struct v4l2_fh *fh = file->private_data;
1549 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
1550 	unsigned long flags;
1551 	int ret;
1552 
1553 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
1554 	if (ret < 0)
1555 		return ret;
1556 
1557 	if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx)) ||
1558 	    !vb2_is_streaming(v4l2_m2m_get_dst_vq(fh->m2m_ctx)))
1559 		return 0;
1560 
1561 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1562 	ret = v4l2_m2m_ioctl_encoder_cmd(file, fh, cmd);
1563 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1564 	if (ret < 0)
1565 		return 0;
1566 
1567 	if (cmd->cmd == V4L2_ENC_CMD_STOP &&
1568 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1569 		notify_eos(ctx);
1570 
1571 	if (cmd->cmd == V4L2_ENC_CMD_START &&
1572 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1573 		vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q);
1574 
1575 	return 0;
1576 }
1577 
1578 static int mxc_jpeg_queue_setup(struct vb2_queue *q,
1579 				unsigned int *nbuffers,
1580 				unsigned int *nplanes,
1581 				unsigned int sizes[],
1582 				struct device *alloc_ctxs[])
1583 {
1584 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1585 	struct mxc_jpeg_q_data *q_data = NULL;
1586 	int i;
1587 
1588 	q_data = mxc_jpeg_get_q_data(ctx, q->type);
1589 	if (!q_data)
1590 		return -EINVAL;
1591 
1592 	/* Handle CREATE_BUFS situation - *nplanes != 0 */
1593 	if (*nplanes) {
1594 		if (*nplanes != q_data->fmt->mem_planes)
1595 			return -EINVAL;
1596 		for (i = 0; i < *nplanes; i++) {
1597 			if (sizes[i] < mxc_jpeg_get_plane_size(q_data, i))
1598 				return -EINVAL;
1599 		}
1600 		return 0;
1601 	}
1602 
1603 	/* Handle REQBUFS situation */
1604 	*nplanes = q_data->fmt->mem_planes;
1605 	for (i = 0; i < *nplanes; i++)
1606 		sizes[i] = mxc_jpeg_get_plane_size(q_data, i);
1607 
1608 	return 0;
1609 }
1610 
1611 static int mxc_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
1612 {
1613 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1614 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, q->type);
1615 	int ret;
1616 
1617 	v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
1618 
1619 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(q->type))
1620 		ctx->source_change = 0;
1621 	dev_dbg(ctx->mxc_jpeg->dev, "Start streaming ctx=%p", ctx);
1622 	q_data->sequence = 0;
1623 
1624 	ret = pm_runtime_resume_and_get(ctx->mxc_jpeg->dev);
1625 	if (ret < 0) {
1626 		dev_err(ctx->mxc_jpeg->dev, "Failed to power up jpeg\n");
1627 		return ret;
1628 	}
1629 
1630 	return 0;
1631 }
1632 
1633 static void mxc_jpeg_stop_streaming(struct vb2_queue *q)
1634 {
1635 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1636 	struct vb2_v4l2_buffer *vbuf;
1637 
1638 	dev_dbg(ctx->mxc_jpeg->dev, "Stop streaming ctx=%p", ctx);
1639 
1640 	/* Release all active buffers */
1641 	for (;;) {
1642 		if (V4L2_TYPE_IS_OUTPUT(q->type))
1643 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1644 		else
1645 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1646 		if (!vbuf)
1647 			break;
1648 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1649 	}
1650 
1651 	if (V4L2_TYPE_IS_OUTPUT(q->type) || !ctx->source_change)
1652 		v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
1653 	if (V4L2_TYPE_IS_OUTPUT(q->type) &&
1654 	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) {
1655 		notify_eos(ctx);
1656 		ctx->header_parsed = false;
1657 	}
1658 
1659 	pm_runtime_put_sync(&ctx->mxc_jpeg->pdev->dev);
1660 }
1661 
1662 static int mxc_jpeg_valid_comp_id(struct device *dev,
1663 				  struct mxc_jpeg_sof *sof,
1664 				  struct mxc_jpeg_sos *sos)
1665 {
1666 	int valid = 1;
1667 	int i;
1668 
1669 	/*
1670 	 * there's a limitation in the IP that the component IDs must be
1671 	 * between 0..4, if they are not, let's patch them
1672 	 */
1673 	for (i = 0; i < sof->components_no; i++)
1674 		if (sof->comp[i].id > MXC_JPEG_MAX_COMPONENTS) {
1675 			valid = 0;
1676 			dev_err(dev, "Component %d has invalid ID: %d",
1677 				i, sof->comp[i].id);
1678 		}
1679 	if (!valid)
1680 		/* patch all comp IDs if at least one is invalid */
1681 		for (i = 0; i < sof->components_no; i++) {
1682 			dev_warn(dev, "Component %d ID patched to: %d",
1683 				 i, i + 1);
1684 			sof->comp[i].id = i + 1;
1685 			sos->comp[i].id = i + 1;
1686 		}
1687 
1688 	return valid;
1689 }
1690 
1691 static bool mxc_jpeg_match_image_format(const struct mxc_jpeg_fmt *fmt,
1692 					const struct v4l2_jpeg_header *header)
1693 {
1694 	if (fmt->subsampling != header->frame.subsampling ||
1695 	    fmt->nc != header->frame.num_components ||
1696 	    fmt->precision != header->frame.precision)
1697 		return false;
1698 
1699 	/*
1700 	 * If the transform flag from APP14 marker is 0, images that are
1701 	 * encoded with 3 components have RGB colorspace, see Recommendation
1702 	 * ITU-T T.872 chapter 6.5.3 APP14 marker segment for colour encoding
1703 	 */
1704 	if (header->frame.subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) {
1705 		u8 is_rgb = header->app14_tf == V4L2_JPEG_APP14_TF_CMYK_RGB ? 1 : 0;
1706 
1707 		if (is_rgb != fmt->is_rgb)
1708 			return false;
1709 	}
1710 	return true;
1711 }
1712 
1713 static u32 mxc_jpeg_get_image_format(struct device *dev,
1714 				     const struct v4l2_jpeg_header *header)
1715 {
1716 	int i;
1717 	u32 fourcc = 0;
1718 
1719 	for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++) {
1720 		if (mxc_jpeg_match_image_format(&mxc_formats[i], header)) {
1721 			fourcc = mxc_formats[i].fourcc;
1722 			break;
1723 		}
1724 	}
1725 	if (fourcc == 0) {
1726 		dev_err(dev,
1727 			"Could not identify image format nc=%d, subsampling=%d, precision=%d\n",
1728 			header->frame.num_components,
1729 			header->frame.subsampling,
1730 			header->frame.precision);
1731 		return fourcc;
1732 	}
1733 
1734 	return fourcc;
1735 }
1736 
1737 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision)
1738 {
1739 	/* Bytes distance between the leftmost pixels in two adjacent lines */
1740 	if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1741 		/* bytesperline unused for compressed formats */
1742 		q->bytesperline[0] = 0;
1743 		q->bytesperline[1] = 0;
1744 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
1745 		/* When the image format is planar the bytesperline value
1746 		 * applies to the first plane and is divided by the same factor
1747 		 * as the width field for the other planes
1748 		 */
1749 		q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8);
1750 		q->bytesperline[1] = q->bytesperline[0];
1751 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
1752 		q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8) * 2;
1753 		q->bytesperline[1] = 0;
1754 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) {
1755 		q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8) * q->fmt->nc;
1756 		q->bytesperline[1] = 0;
1757 	} else {
1758 		/* grayscale */
1759 		q->bytesperline[0] = q->w_adjusted * DIV_ROUND_UP(precision, 8);
1760 		q->bytesperline[1] = 0;
1761 	}
1762 }
1763 
1764 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q)
1765 {
1766 	if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1767 		/* if no sizeimage from user, assume worst jpeg compression */
1768 		if (!q->sizeimage[0])
1769 			q->sizeimage[0] = 6 * q->w * q->h;
1770 		q->sizeimage[1] = 0;
1771 
1772 		if (q->sizeimage[0] > MXC_JPEG_MAX_SIZEIMAGE)
1773 			q->sizeimage[0] = MXC_JPEG_MAX_SIZEIMAGE;
1774 
1775 		/* jpeg stream size must be multiple of 1K */
1776 		q->sizeimage[0] = ALIGN(q->sizeimage[0], 1024);
1777 	} else {
1778 		q->sizeimage[0] = q->bytesperline[0] * q->h_adjusted;
1779 		q->sizeimage[1] = 0;
1780 		if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420)
1781 			q->sizeimage[1] = q->sizeimage[0] / 2;
1782 	}
1783 }
1784 
1785 static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx, struct vb2_buffer *vb)
1786 {
1787 	struct device *dev = ctx->mxc_jpeg->dev;
1788 	struct mxc_jpeg_q_data *q_data_out;
1789 	struct mxc_jpeg_q_data *q_data_cap;
1790 	u32 fourcc;
1791 	struct v4l2_jpeg_header header;
1792 	struct mxc_jpeg_sof *psof = NULL;
1793 	struct mxc_jpeg_sos *psos = NULL;
1794 	struct mxc_jpeg_src_buf *jpeg_src_buf = vb2_to_mxc_buf(vb);
1795 	u8 *src_addr = (u8 *)vb2_plane_vaddr(vb, 0);
1796 	u32 size = vb2_get_plane_payload(vb, 0);
1797 	int ret;
1798 
1799 	memset(&header, 0, sizeof(header));
1800 	ret = v4l2_jpeg_parse_header((void *)src_addr, size, &header);
1801 	if (ret < 0) {
1802 		dev_err(dev, "Error parsing JPEG stream markers\n");
1803 		return ret;
1804 	}
1805 
1806 	/* if DHT marker present, no need to inject default one */
1807 	jpeg_src_buf->dht_needed = (header.num_dht == 0);
1808 
1809 	q_data_out = mxc_jpeg_get_q_data(ctx,
1810 					 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1811 	if (q_data_out->w == 0 && q_data_out->h == 0) {
1812 		dev_warn(dev, "Invalid user resolution 0x0");
1813 		dev_warn(dev, "Keeping resolution from JPEG: %dx%d",
1814 			 header.frame.width, header.frame.height);
1815 	} else if (header.frame.width != q_data_out->w ||
1816 		   header.frame.height != q_data_out->h) {
1817 		dev_err(dev,
1818 			"Resolution mismatch: %dx%d (JPEG) versus %dx%d(user)",
1819 			header.frame.width, header.frame.height,
1820 			q_data_out->w, q_data_out->h);
1821 	}
1822 	q_data_out->w = header.frame.width;
1823 	q_data_out->h = header.frame.height;
1824 	if (header.frame.width > MXC_JPEG_MAX_WIDTH ||
1825 	    header.frame.height > MXC_JPEG_MAX_HEIGHT) {
1826 		dev_err(dev, "JPEG width or height should be <= 8192: %dx%d\n",
1827 			header.frame.width, header.frame.height);
1828 		return -EINVAL;
1829 	}
1830 	if (header.frame.width < MXC_JPEG_MIN_WIDTH ||
1831 	    header.frame.height < MXC_JPEG_MIN_HEIGHT) {
1832 		dev_err(dev, "JPEG width or height should be > 64: %dx%d\n",
1833 			header.frame.width, header.frame.height);
1834 		return -EINVAL;
1835 	}
1836 	if (header.frame.num_components > V4L2_JPEG_MAX_COMPONENTS) {
1837 		dev_err(dev, "JPEG number of components should be <=%d",
1838 			V4L2_JPEG_MAX_COMPONENTS);
1839 		return -EINVAL;
1840 	}
1841 	/* check and, if necessary, patch component IDs*/
1842 	psof = (struct mxc_jpeg_sof *)header.sof.start;
1843 	psos = (struct mxc_jpeg_sos *)header.sos.start;
1844 	if (!mxc_jpeg_valid_comp_id(dev, psof, psos))
1845 		dev_warn(dev, "JPEG component ids should be 0-3 or 1-4");
1846 
1847 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1848 	if (q_data_cap->fmt && mxc_jpeg_match_image_format(q_data_cap->fmt, &header))
1849 		fourcc = q_data_cap->fmt->fourcc;
1850 	else
1851 		fourcc = mxc_jpeg_get_image_format(dev, &header);
1852 	if (fourcc == 0)
1853 		return -EINVAL;
1854 
1855 	jpeg_src_buf->fmt = mxc_jpeg_find_format(fourcc);
1856 	jpeg_src_buf->w = header.frame.width;
1857 	jpeg_src_buf->h = header.frame.height;
1858 	ctx->header_parsed = true;
1859 
1860 	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx))
1861 		mxc_jpeg_source_change(ctx, jpeg_src_buf);
1862 
1863 	return 0;
1864 }
1865 
1866 static void mxc_jpeg_buf_queue(struct vb2_buffer *vb)
1867 {
1868 	int ret;
1869 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1870 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1871 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1872 
1873 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1874 	    vb2_is_streaming(vb->vb2_queue) &&
1875 	    v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
1876 		struct mxc_jpeg_q_data *q_data;
1877 
1878 		q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type);
1879 		vbuf->field = V4L2_FIELD_NONE;
1880 		vbuf->sequence = q_data->sequence++;
1881 		v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
1882 		notify_eos(ctx);
1883 		ctx->header_parsed = false;
1884 		return;
1885 	}
1886 
1887 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1888 		goto end;
1889 
1890 	/* for V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE */
1891 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
1892 		goto end;
1893 
1894 	jpeg_src_buf = vb2_to_mxc_buf(vb);
1895 	jpeg_src_buf->jpeg_parse_error = false;
1896 	ret = mxc_jpeg_parse(ctx, vb);
1897 	if (ret)
1898 		jpeg_src_buf->jpeg_parse_error = true;
1899 
1900 end:
1901 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1902 }
1903 
1904 static int mxc_jpeg_buf_out_validate(struct vb2_buffer *vb)
1905 {
1906 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1907 
1908 	vbuf->field = V4L2_FIELD_NONE;
1909 
1910 	return 0;
1911 }
1912 
1913 static int mxc_jpeg_buf_prepare(struct vb2_buffer *vb)
1914 {
1915 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1916 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1917 	struct mxc_jpeg_q_data *q_data = NULL;
1918 	struct device *dev = ctx->mxc_jpeg->dev;
1919 	unsigned long sizeimage;
1920 	int i;
1921 
1922 	vbuf->field = V4L2_FIELD_NONE;
1923 
1924 	q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type);
1925 	if (!q_data)
1926 		return -EINVAL;
1927 	for (i = 0; i < q_data->fmt->mem_planes; i++) {
1928 		sizeimage = mxc_jpeg_get_plane_size(q_data, i);
1929 		if (vb2_plane_size(vb, i) < sizeimage) {
1930 			dev_err(dev, "plane %d too small (%lu < %lu)",
1931 				i, vb2_plane_size(vb, i), sizeimage);
1932 			return -EINVAL;
1933 		}
1934 	}
1935 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) {
1936 		vb2_set_plane_payload(vb, 0, 0);
1937 		vb2_set_plane_payload(vb, 1, 0);
1938 	}
1939 	return 0;
1940 }
1941 
1942 static const struct vb2_ops mxc_jpeg_qops = {
1943 	.queue_setup		= mxc_jpeg_queue_setup,
1944 	.wait_prepare		= vb2_ops_wait_prepare,
1945 	.wait_finish		= vb2_ops_wait_finish,
1946 	.buf_out_validate	= mxc_jpeg_buf_out_validate,
1947 	.buf_prepare		= mxc_jpeg_buf_prepare,
1948 	.start_streaming	= mxc_jpeg_start_streaming,
1949 	.stop_streaming		= mxc_jpeg_stop_streaming,
1950 	.buf_queue		= mxc_jpeg_buf_queue,
1951 };
1952 
1953 static int mxc_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
1954 			       struct vb2_queue *dst_vq)
1955 {
1956 	struct mxc_jpeg_ctx *ctx = priv;
1957 	int ret;
1958 
1959 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1960 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1961 	src_vq->drv_priv = ctx;
1962 	src_vq->buf_struct_size = sizeof(struct mxc_jpeg_src_buf);
1963 	src_vq->ops = &mxc_jpeg_qops;
1964 	src_vq->mem_ops = &vb2_dma_contig_memops;
1965 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1966 	src_vq->lock = &ctx->mxc_jpeg->lock;
1967 	src_vq->dev = ctx->mxc_jpeg->dev;
1968 
1969 	ret = vb2_queue_init(src_vq);
1970 	if (ret)
1971 		return ret;
1972 
1973 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1974 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1975 	dst_vq->drv_priv = ctx;
1976 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1977 	dst_vq->ops = &mxc_jpeg_qops;
1978 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1979 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1980 	dst_vq->lock = &ctx->mxc_jpeg->lock;
1981 	dst_vq->dev = ctx->mxc_jpeg->dev;
1982 
1983 	ret = vb2_queue_init(dst_vq);
1984 	return ret;
1985 }
1986 
1987 static void mxc_jpeg_set_default_params(struct mxc_jpeg_ctx *ctx)
1988 {
1989 	struct mxc_jpeg_q_data *out_q = &ctx->out_q;
1990 	struct mxc_jpeg_q_data *cap_q = &ctx->cap_q;
1991 	struct mxc_jpeg_q_data *q[2] = {out_q, cap_q};
1992 	int i;
1993 
1994 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) {
1995 		out_q->fmt = mxc_jpeg_find_format(MXC_JPEG_DEFAULT_PFMT);
1996 		cap_q->fmt = mxc_jpeg_find_format(V4L2_PIX_FMT_JPEG);
1997 	} else {
1998 		out_q->fmt = mxc_jpeg_find_format(V4L2_PIX_FMT_JPEG);
1999 		cap_q->fmt = mxc_jpeg_find_format(MXC_JPEG_DEFAULT_PFMT);
2000 	}
2001 
2002 	for (i = 0; i < 2; i++) {
2003 		q[i]->w = MXC_JPEG_DEFAULT_WIDTH;
2004 		q[i]->h = MXC_JPEG_DEFAULT_HEIGHT;
2005 		q[i]->w_adjusted = MXC_JPEG_DEFAULT_WIDTH;
2006 		q[i]->h_adjusted = MXC_JPEG_DEFAULT_HEIGHT;
2007 		q[i]->crop.left = 0;
2008 		q[i]->crop.top = 0;
2009 		q[i]->crop.width = MXC_JPEG_DEFAULT_WIDTH;
2010 		q[i]->crop.height = MXC_JPEG_DEFAULT_HEIGHT;
2011 		mxc_jpeg_bytesperline(q[i], q[i]->fmt->precision);
2012 		mxc_jpeg_sizeimage(q[i]);
2013 	}
2014 }
2015 
2016 static int mxc_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
2017 {
2018 	struct mxc_jpeg_ctx *ctx =
2019 		container_of(ctrl->handler, struct mxc_jpeg_ctx, ctrl_handler);
2020 
2021 	switch (ctrl->id) {
2022 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2023 		ctx->jpeg_quality = ctrl->val;
2024 		break;
2025 	default:
2026 		dev_err(ctx->mxc_jpeg->dev, "Invalid control, id = %d, val = %d\n",
2027 			ctrl->id, ctrl->val);
2028 		return -EINVAL;
2029 	}
2030 
2031 	return 0;
2032 }
2033 
2034 static const struct v4l2_ctrl_ops mxc_jpeg_ctrl_ops = {
2035 	.s_ctrl = mxc_jpeg_s_ctrl,
2036 };
2037 
2038 static void mxc_jpeg_encode_ctrls(struct mxc_jpeg_ctx *ctx)
2039 {
2040 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &mxc_jpeg_ctrl_ops,
2041 			  V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75);
2042 }
2043 
2044 static int mxc_jpeg_ctrls_setup(struct mxc_jpeg_ctx *ctx)
2045 {
2046 	int err;
2047 
2048 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 2);
2049 
2050 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE)
2051 		mxc_jpeg_encode_ctrls(ctx);
2052 
2053 	if (ctx->ctrl_handler.error) {
2054 		err = ctx->ctrl_handler.error;
2055 
2056 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2057 		return err;
2058 	}
2059 
2060 	err = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
2061 	if (err)
2062 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2063 	return err;
2064 }
2065 
2066 static int mxc_jpeg_open(struct file *file)
2067 {
2068 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
2069 	struct video_device *mxc_vfd = video_devdata(file);
2070 	struct device *dev = mxc_jpeg->dev;
2071 	struct mxc_jpeg_ctx *ctx;
2072 	int ret = 0;
2073 
2074 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2075 	if (!ctx)
2076 		return -ENOMEM;
2077 
2078 	if (mutex_lock_interruptible(&mxc_jpeg->lock)) {
2079 		ret = -ERESTARTSYS;
2080 		goto free;
2081 	}
2082 
2083 	v4l2_fh_init(&ctx->fh, mxc_vfd);
2084 	file->private_data = &ctx->fh;
2085 	v4l2_fh_add(&ctx->fh);
2086 
2087 	ctx->mxc_jpeg = mxc_jpeg;
2088 
2089 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(mxc_jpeg->m2m_dev, ctx,
2090 					    mxc_jpeg_queue_init);
2091 
2092 	if (IS_ERR(ctx->fh.m2m_ctx)) {
2093 		ret = PTR_ERR(ctx->fh.m2m_ctx);
2094 		goto error;
2095 	}
2096 
2097 	ret = mxc_jpeg_ctrls_setup(ctx);
2098 	if (ret) {
2099 		dev_err(ctx->mxc_jpeg->dev, "failed to setup mxc jpeg controls\n");
2100 		goto err_ctrls_setup;
2101 	}
2102 	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
2103 	mxc_jpeg_set_default_params(ctx);
2104 	ctx->slot = MXC_MAX_SLOTS; /* slot not allocated yet */
2105 	INIT_DELAYED_WORK(&ctx->task_timer, mxc_jpeg_device_run_timeout);
2106 
2107 	if (mxc_jpeg->mode == MXC_JPEG_DECODE)
2108 		dev_dbg(dev, "Opened JPEG decoder instance %p\n", ctx);
2109 	else
2110 		dev_dbg(dev, "Opened JPEG encoder instance %p\n", ctx);
2111 	mutex_unlock(&mxc_jpeg->lock);
2112 
2113 	return 0;
2114 
2115 err_ctrls_setup:
2116 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2117 error:
2118 	v4l2_fh_del(&ctx->fh);
2119 	v4l2_fh_exit(&ctx->fh);
2120 	mutex_unlock(&mxc_jpeg->lock);
2121 free:
2122 	kfree(ctx);
2123 	return ret;
2124 }
2125 
2126 static int mxc_jpeg_querycap(struct file *file, void *priv,
2127 			     struct v4l2_capability *cap)
2128 {
2129 	strscpy(cap->driver, MXC_JPEG_NAME " codec", sizeof(cap->driver));
2130 	strscpy(cap->card, MXC_JPEG_NAME " codec", sizeof(cap->card));
2131 	cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
2132 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
2133 
2134 	return 0;
2135 }
2136 
2137 static int mxc_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
2138 				     struct v4l2_fmtdesc *f)
2139 {
2140 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2141 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type);
2142 
2143 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) {
2144 		return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f,
2145 			MXC_JPEG_FMT_TYPE_ENC);
2146 	} else if (!ctx->header_parsed) {
2147 		return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f,
2148 			MXC_JPEG_FMT_TYPE_RAW);
2149 	} else {
2150 		/* For the decoder CAPTURE queue, only enumerate the raw formats
2151 		 * supported for the format currently active on OUTPUT
2152 		 * (more precisely what was propagated on capture queue
2153 		 * after jpeg parse on the output buffer)
2154 		 */
2155 		int ret = -EINVAL;
2156 		const struct mxc_jpeg_fmt *sibling;
2157 
2158 		switch (f->index) {
2159 		case 0:
2160 			f->pixelformat = q_data->fmt->fourcc;
2161 			ret = 0;
2162 			break;
2163 		case 1:
2164 			sibling = mxc_jpeg_get_sibling_format(q_data->fmt);
2165 			if (sibling) {
2166 				f->pixelformat = sibling->fourcc;
2167 				ret = 0;
2168 			}
2169 			break;
2170 		default:
2171 			break;
2172 		}
2173 		return ret;
2174 	}
2175 }
2176 
2177 static int mxc_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
2178 				     struct v4l2_fmtdesc *f)
2179 {
2180 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2181 	u32 type = ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ?  MXC_JPEG_FMT_TYPE_ENC :
2182 							     MXC_JPEG_FMT_TYPE_RAW;
2183 	int ret;
2184 
2185 	ret = enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, type);
2186 	if (ret)
2187 		return ret;
2188 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
2189 		f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION;
2190 	return 0;
2191 }
2192 
2193 static u32 mxc_jpeg_get_fmt_type(struct mxc_jpeg_ctx *ctx, u32 type)
2194 {
2195 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
2196 		return V4L2_TYPE_IS_OUTPUT(type) ? MXC_JPEG_FMT_TYPE_ENC : MXC_JPEG_FMT_TYPE_RAW;
2197 	else
2198 		return V4L2_TYPE_IS_CAPTURE(type) ? MXC_JPEG_FMT_TYPE_ENC : MXC_JPEG_FMT_TYPE_RAW;
2199 }
2200 
2201 static u32 mxc_jpeg_get_default_fourcc(struct mxc_jpeg_ctx *ctx, u32 type)
2202 {
2203 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
2204 		return V4L2_TYPE_IS_OUTPUT(type) ? V4L2_PIX_FMT_JPEG : MXC_JPEG_DEFAULT_PFMT;
2205 	else
2206 		return V4L2_TYPE_IS_CAPTURE(type) ? V4L2_PIX_FMT_JPEG : MXC_JPEG_DEFAULT_PFMT;
2207 }
2208 
2209 static u32 mxc_jpeg_try_fourcc(struct mxc_jpeg_ctx *ctx, u32 fourcc)
2210 {
2211 	const struct mxc_jpeg_fmt *sibling;
2212 	struct mxc_jpeg_q_data *q_data_cap;
2213 
2214 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
2215 		return fourcc;
2216 	if (!ctx->header_parsed)
2217 		return fourcc;
2218 
2219 	q_data_cap = &ctx->cap_q;
2220 	if (q_data_cap->fmt->fourcc == fourcc)
2221 		return fourcc;
2222 
2223 	sibling = mxc_jpeg_get_sibling_format(q_data_cap->fmt);
2224 	if (sibling && sibling->fourcc == fourcc)
2225 		return sibling->fourcc;
2226 
2227 	return q_data_cap->fmt->fourcc;
2228 }
2229 
2230 static int mxc_jpeg_try_fmt(struct v4l2_format *f,
2231 			    struct mxc_jpeg_ctx *ctx, struct mxc_jpeg_q_data *q_data)
2232 {
2233 	const struct mxc_jpeg_fmt *fmt;
2234 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
2235 	struct v4l2_plane_pix_format *pfmt;
2236 	u32 fourcc = f->fmt.pix_mp.pixelformat;
2237 	u32 w = (pix_mp->width < MXC_JPEG_MAX_WIDTH) ?
2238 		 pix_mp->width : MXC_JPEG_MAX_WIDTH;
2239 	u32 h = (pix_mp->height < MXC_JPEG_MAX_HEIGHT) ?
2240 		 pix_mp->height : MXC_JPEG_MAX_HEIGHT;
2241 	int i;
2242 
2243 	fmt = mxc_jpeg_find_format(fourcc);
2244 	if (!fmt || fmt->flags != mxc_jpeg_get_fmt_type(ctx, f->type)) {
2245 		dev_warn(ctx->mxc_jpeg->dev, "Format not supported: %c%c%c%c, use the default.\n",
2246 			 (fourcc & 0xff),
2247 			 (fourcc >>  8) & 0xff,
2248 			 (fourcc >> 16) & 0xff,
2249 			 (fourcc >> 24) & 0xff);
2250 		fourcc = mxc_jpeg_get_default_fourcc(ctx, f->type);
2251 		fmt = mxc_jpeg_find_format(fourcc);
2252 		if (!fmt)
2253 			return -EINVAL;
2254 		f->fmt.pix_mp.pixelformat = fourcc;
2255 	}
2256 	q_data->fmt = fmt;
2257 
2258 	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
2259 	pix_mp->field = V4L2_FIELD_NONE;
2260 	pix_mp->num_planes = fmt->mem_planes;
2261 	pix_mp->pixelformat = fmt->fourcc;
2262 
2263 	q_data->w = w;
2264 	q_data->h = h;
2265 	q_data->w_adjusted = w;
2266 	q_data->h_adjusted = h;
2267 	v4l_bound_align_image(&q_data->w_adjusted,
2268 			      w, /* adjust upwards*/
2269 			      MXC_JPEG_MAX_WIDTH,
2270 			      fmt->h_align,
2271 			      &q_data->h_adjusted,
2272 			      h, /* adjust upwards*/
2273 			      MXC_JPEG_MAX_HEIGHT,
2274 			      fmt->v_align,
2275 			      0);
2276 	for (i = 0; i < pix_mp->num_planes; i++) {
2277 		pfmt = &pix_mp->plane_fmt[i];
2278 		q_data->bytesperline[i] = pfmt->bytesperline;
2279 		q_data->sizeimage[i] = pfmt->sizeimage;
2280 	}
2281 
2282 	/* calculate bytesperline & sizeimage */
2283 	mxc_jpeg_bytesperline(q_data, fmt->precision);
2284 	mxc_jpeg_sizeimage(q_data);
2285 
2286 	/* adjust user format according to our calculations */
2287 	for (i = 0; i < pix_mp->num_planes; i++) {
2288 		pfmt = &pix_mp->plane_fmt[i];
2289 		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
2290 		pfmt->bytesperline = q_data->bytesperline[i];
2291 		pfmt->sizeimage = mxc_jpeg_get_plane_size(q_data, i);
2292 	}
2293 
2294 	/* fix colorspace information to sRGB for both output & capture */
2295 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
2296 	pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601;
2297 	pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB;
2298 	/*
2299 	 * this hardware does not change the range of the samples
2300 	 * but since inside JPEG the YUV quantization is full-range,
2301 	 * this driver will always use full-range for the raw frames, too
2302 	 */
2303 	pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2304 
2305 	if (fmt->flags == MXC_JPEG_FMT_TYPE_RAW) {
2306 		q_data->crop.left = 0;
2307 		q_data->crop.top = 0;
2308 		q_data->crop.width = q_data->w;
2309 		q_data->crop.height = q_data->h;
2310 	}
2311 
2312 	pix_mp->width = q_data->w_adjusted;
2313 	pix_mp->height = q_data->h_adjusted;
2314 
2315 	return 0;
2316 }
2317 
2318 static int mxc_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
2319 				    struct v4l2_format *f)
2320 {
2321 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2322 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
2323 	struct device *dev = jpeg->dev;
2324 	struct mxc_jpeg_q_data tmp_q;
2325 
2326 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
2327 		dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type);
2328 		return -EINVAL;
2329 	}
2330 
2331 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(f->type))
2332 		f->fmt.pix_mp.pixelformat = mxc_jpeg_try_fourcc(ctx, f->fmt.pix_mp.pixelformat);
2333 
2334 	return mxc_jpeg_try_fmt(f, ctx, &tmp_q);
2335 }
2336 
2337 static int mxc_jpeg_try_fmt_vid_out(struct file *file, void *priv,
2338 				    struct v4l2_format *f)
2339 {
2340 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2341 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
2342 	struct device *dev = jpeg->dev;
2343 	struct mxc_jpeg_q_data tmp_q;
2344 
2345 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
2346 		dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type);
2347 		return -EINVAL;
2348 	}
2349 
2350 	return mxc_jpeg_try_fmt(f, ctx, &tmp_q);
2351 }
2352 
2353 static void mxc_jpeg_s_parsed_fmt(struct mxc_jpeg_ctx *ctx, struct v4l2_format *f)
2354 {
2355 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
2356 	struct mxc_jpeg_q_data *q_data_cap;
2357 
2358 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE || !V4L2_TYPE_IS_CAPTURE(f->type))
2359 		return;
2360 	if (!ctx->header_parsed)
2361 		return;
2362 
2363 	q_data_cap = mxc_jpeg_get_q_data(ctx, f->type);
2364 	pix_mp->pixelformat = mxc_jpeg_try_fourcc(ctx, pix_mp->pixelformat);
2365 	pix_mp->width = q_data_cap->w;
2366 	pix_mp->height = q_data_cap->h;
2367 }
2368 
2369 static int mxc_jpeg_s_fmt(struct mxc_jpeg_ctx *ctx,
2370 			  struct v4l2_format *f)
2371 {
2372 	struct vb2_queue *vq;
2373 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
2374 
2375 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
2376 	if (!vq)
2377 		return -EINVAL;
2378 
2379 	if (vb2_is_busy(vq)) {
2380 		v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
2381 		return -EBUSY;
2382 	}
2383 
2384 	mxc_jpeg_s_parsed_fmt(ctx, f);
2385 
2386 	return mxc_jpeg_try_fmt(f, ctx, mxc_jpeg_get_q_data(ctx, f->type));
2387 }
2388 
2389 static int mxc_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
2390 				  struct v4l2_format *f)
2391 {
2392 	return mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f);
2393 }
2394 
2395 static int mxc_jpeg_s_fmt_vid_out(struct file *file, void *priv,
2396 				  struct v4l2_format *f)
2397 {
2398 	int ret;
2399 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2400 	struct vb2_queue *dst_vq;
2401 	struct mxc_jpeg_q_data *q_data_cap;
2402 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
2403 	struct v4l2_format fc;
2404 
2405 	ret = mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f);
2406 	if (ret)
2407 		return ret;
2408 
2409 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
2410 		return 0;
2411 
2412 	dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, cap_type);
2413 	if (!dst_vq)
2414 		return -EINVAL;
2415 
2416 	if (vb2_is_busy(dst_vq))
2417 		return 0;
2418 
2419 	q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type);
2420 	if (q_data_cap->w == f->fmt.pix_mp.width && q_data_cap->h == f->fmt.pix_mp.height)
2421 		return 0;
2422 	memset(&fc, 0, sizeof(fc));
2423 	fc.type = cap_type;
2424 	fc.fmt.pix_mp.pixelformat = q_data_cap->fmt->fourcc;
2425 	fc.fmt.pix_mp.width = f->fmt.pix_mp.width;
2426 	fc.fmt.pix_mp.height = f->fmt.pix_mp.height;
2427 
2428 	return mxc_jpeg_s_fmt_vid_cap(file, priv, &fc);
2429 }
2430 
2431 static int mxc_jpeg_g_fmt_vid(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 v4l2_pix_format_mplane   *pix_mp = &f->fmt.pix_mp;
2438 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type);
2439 	int i;
2440 
2441 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
2442 		dev_err(dev, "G_FMT with Invalid type: %d\n", f->type);
2443 		return -EINVAL;
2444 	}
2445 
2446 	pix_mp->pixelformat = q_data->fmt->fourcc;
2447 	pix_mp->width = q_data->w;
2448 	pix_mp->height = q_data->h;
2449 	pix_mp->field = V4L2_FIELD_NONE;
2450 	if (q_data->fmt->flags == MXC_JPEG_FMT_TYPE_RAW) {
2451 		pix_mp->width = q_data->w_adjusted;
2452 		pix_mp->height = q_data->h_adjusted;
2453 	}
2454 
2455 	/* fix colorspace information to sRGB for both output & capture */
2456 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
2457 	pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601;
2458 	pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB;
2459 	pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2460 
2461 	pix_mp->num_planes = q_data->fmt->mem_planes;
2462 	for (i = 0; i < pix_mp->num_planes; i++) {
2463 		pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i];
2464 		pix_mp->plane_fmt[i].sizeimage = mxc_jpeg_get_plane_size(q_data, i);
2465 	}
2466 
2467 	return 0;
2468 }
2469 
2470 static int mxc_jpeg_dec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2471 {
2472 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
2473 	struct mxc_jpeg_q_data *q_data_cap;
2474 
2475 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2476 		return -EINVAL;
2477 
2478 	q_data_cap = mxc_jpeg_get_q_data(ctx, s->type);
2479 
2480 	switch (s->target) {
2481 	case V4L2_SEL_TGT_COMPOSE:
2482 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
2483 		s->r = q_data_cap->crop;
2484 		break;
2485 	case V4L2_SEL_TGT_COMPOSE_PADDED:
2486 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
2487 		s->r.left = 0;
2488 		s->r.top = 0;
2489 		s->r.width = q_data_cap->w_adjusted;
2490 		s->r.height = q_data_cap->h_adjusted;
2491 		break;
2492 	default:
2493 		return -EINVAL;
2494 	}
2495 
2496 	return 0;
2497 }
2498 
2499 static int mxc_jpeg_enc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2500 {
2501 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
2502 	struct mxc_jpeg_q_data *q_data_out;
2503 
2504 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2505 		return -EINVAL;
2506 
2507 	q_data_out = mxc_jpeg_get_q_data(ctx, s->type);
2508 
2509 	switch (s->target) {
2510 	case V4L2_SEL_TGT_CROP_DEFAULT:
2511 	case V4L2_SEL_TGT_CROP_BOUNDS:
2512 		s->r.left = 0;
2513 		s->r.top = 0;
2514 		s->r.width = q_data_out->w;
2515 		s->r.height = q_data_out->h;
2516 		break;
2517 	case V4L2_SEL_TGT_CROP:
2518 		s->r = q_data_out->crop;
2519 		break;
2520 	default:
2521 		return -EINVAL;
2522 	}
2523 
2524 	return 0;
2525 }
2526 
2527 static int mxc_jpeg_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2528 {
2529 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
2530 
2531 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
2532 		return mxc_jpeg_dec_g_selection(file, fh, s);
2533 	else
2534 		return mxc_jpeg_enc_g_selection(file, fh, s);
2535 }
2536 
2537 static int mxc_jpeg_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
2538 {
2539 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
2540 	struct mxc_jpeg_q_data *q_data_out;
2541 
2542 	if (ctx->mxc_jpeg->mode != MXC_JPEG_ENCODE)
2543 		return -ENOTTY;
2544 
2545 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2546 		return -EINVAL;
2547 	if (s->target != V4L2_SEL_TGT_CROP)
2548 		return -EINVAL;
2549 
2550 	q_data_out = mxc_jpeg_get_q_data(ctx, s->type);
2551 	if (s->r.left || s->r.top)
2552 		return -EINVAL;
2553 	if (s->r.width > q_data_out->w || s->r.height > q_data_out->h)
2554 		return -EINVAL;
2555 
2556 	q_data_out->crop.left = 0;
2557 	q_data_out->crop.top = 0;
2558 	q_data_out->crop.width = s->r.width;
2559 	q_data_out->crop.height = s->r.height;
2560 
2561 	return 0;
2562 }
2563 
2564 static int mxc_jpeg_subscribe_event(struct v4l2_fh *fh,
2565 				    const struct v4l2_event_subscription *sub)
2566 {
2567 	switch (sub->type) {
2568 	case V4L2_EVENT_EOS:
2569 		return v4l2_event_subscribe(fh, sub, 0, NULL);
2570 	case V4L2_EVENT_SOURCE_CHANGE:
2571 		return v4l2_src_change_event_subscribe(fh, sub);
2572 	case V4L2_EVENT_CTRL:
2573 		return v4l2_ctrl_subscribe_event(fh, sub);
2574 	default:
2575 		return -EINVAL;
2576 	}
2577 }
2578 
2579 static const struct v4l2_ioctl_ops mxc_jpeg_ioctl_ops = {
2580 	.vidioc_querycap		= mxc_jpeg_querycap,
2581 	.vidioc_enum_fmt_vid_cap	= mxc_jpeg_enum_fmt_vid_cap,
2582 	.vidioc_enum_fmt_vid_out	= mxc_jpeg_enum_fmt_vid_out,
2583 
2584 	.vidioc_try_fmt_vid_cap_mplane	= mxc_jpeg_try_fmt_vid_cap,
2585 	.vidioc_try_fmt_vid_out_mplane	= mxc_jpeg_try_fmt_vid_out,
2586 
2587 	.vidioc_s_fmt_vid_cap_mplane	= mxc_jpeg_s_fmt_vid_cap,
2588 	.vidioc_s_fmt_vid_out_mplane	= mxc_jpeg_s_fmt_vid_out,
2589 
2590 	.vidioc_g_fmt_vid_cap_mplane	= mxc_jpeg_g_fmt_vid,
2591 	.vidioc_g_fmt_vid_out_mplane	= mxc_jpeg_g_fmt_vid,
2592 
2593 	.vidioc_g_selection		= mxc_jpeg_g_selection,
2594 	.vidioc_s_selection		= mxc_jpeg_s_selection,
2595 
2596 	.vidioc_subscribe_event		= mxc_jpeg_subscribe_event,
2597 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
2598 
2599 	.vidioc_try_decoder_cmd		= v4l2_m2m_ioctl_try_decoder_cmd,
2600 	.vidioc_decoder_cmd		= mxc_jpeg_decoder_cmd,
2601 	.vidioc_try_encoder_cmd		= v4l2_m2m_ioctl_try_encoder_cmd,
2602 	.vidioc_encoder_cmd		= mxc_jpeg_encoder_cmd,
2603 
2604 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
2605 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
2606 
2607 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
2608 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
2609 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
2610 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
2611 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
2612 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
2613 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
2614 };
2615 
2616 static int mxc_jpeg_release(struct file *file)
2617 {
2618 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
2619 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(file->private_data);
2620 	struct device *dev = mxc_jpeg->dev;
2621 
2622 	mutex_lock(&mxc_jpeg->lock);
2623 	if (mxc_jpeg->mode == MXC_JPEG_DECODE)
2624 		dev_dbg(dev, "Release JPEG decoder instance on slot %d.",
2625 			ctx->slot);
2626 	else
2627 		dev_dbg(dev, "Release JPEG encoder instance on slot %d.",
2628 			ctx->slot);
2629 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2630 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2631 	v4l2_fh_del(&ctx->fh);
2632 	v4l2_fh_exit(&ctx->fh);
2633 	kfree(ctx);
2634 	mutex_unlock(&mxc_jpeg->lock);
2635 
2636 	return 0;
2637 }
2638 
2639 static const struct v4l2_file_operations mxc_jpeg_fops = {
2640 	.owner		= THIS_MODULE,
2641 	.open		= mxc_jpeg_open,
2642 	.release	= mxc_jpeg_release,
2643 	.poll		= v4l2_m2m_fop_poll,
2644 	.unlocked_ioctl	= video_ioctl2,
2645 	.mmap		= v4l2_m2m_fop_mmap,
2646 };
2647 
2648 static const struct v4l2_m2m_ops mxc_jpeg_m2m_ops = {
2649 	.job_ready      = mxc_jpeg_job_ready,
2650 	.device_run	= mxc_jpeg_device_run,
2651 };
2652 
2653 static void mxc_jpeg_detach_pm_domains(struct mxc_jpeg_dev *jpeg)
2654 {
2655 	int i;
2656 
2657 	for (i = 0; i < jpeg->num_domains; i++) {
2658 		if (jpeg->pd_link[i] && !IS_ERR(jpeg->pd_link[i]))
2659 			device_link_del(jpeg->pd_link[i]);
2660 		if (jpeg->pd_dev[i] && !IS_ERR(jpeg->pd_dev[i]))
2661 			dev_pm_domain_detach(jpeg->pd_dev[i], true);
2662 		jpeg->pd_dev[i] = NULL;
2663 		jpeg->pd_link[i] = NULL;
2664 	}
2665 }
2666 
2667 static int mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev *jpeg)
2668 {
2669 	struct device *dev = jpeg->dev;
2670 	struct device_node *np = jpeg->pdev->dev.of_node;
2671 	int i;
2672 	int ret;
2673 
2674 	jpeg->num_domains = of_count_phandle_with_args(np, "power-domains",
2675 						       "#power-domain-cells");
2676 	if (jpeg->num_domains < 0) {
2677 		dev_err(dev, "No power domains defined for jpeg node\n");
2678 		return jpeg->num_domains;
2679 	}
2680 
2681 	jpeg->pd_dev = devm_kmalloc_array(dev, jpeg->num_domains,
2682 					  sizeof(*jpeg->pd_dev), GFP_KERNEL);
2683 	if (!jpeg->pd_dev)
2684 		return -ENOMEM;
2685 
2686 	jpeg->pd_link = devm_kmalloc_array(dev, jpeg->num_domains,
2687 					   sizeof(*jpeg->pd_link), GFP_KERNEL);
2688 	if (!jpeg->pd_link)
2689 		return -ENOMEM;
2690 
2691 	for (i = 0; i < jpeg->num_domains; i++) {
2692 		jpeg->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i);
2693 		if (IS_ERR(jpeg->pd_dev[i])) {
2694 			ret = PTR_ERR(jpeg->pd_dev[i]);
2695 			goto fail;
2696 		}
2697 
2698 		jpeg->pd_link[i] = device_link_add(dev, jpeg->pd_dev[i],
2699 						   DL_FLAG_STATELESS |
2700 						   DL_FLAG_PM_RUNTIME);
2701 		if (!jpeg->pd_link[i]) {
2702 			ret = -EINVAL;
2703 			goto fail;
2704 		}
2705 	}
2706 
2707 	return 0;
2708 fail:
2709 	mxc_jpeg_detach_pm_domains(jpeg);
2710 	return ret;
2711 }
2712 
2713 static int mxc_jpeg_probe(struct platform_device *pdev)
2714 {
2715 	struct mxc_jpeg_dev *jpeg;
2716 	struct device *dev = &pdev->dev;
2717 	int dec_irq;
2718 	int ret;
2719 	int mode;
2720 	const struct of_device_id *of_id;
2721 	unsigned int slot;
2722 
2723 	of_id = of_match_node(mxc_jpeg_match, dev->of_node);
2724 	if (!of_id)
2725 		return -ENODEV;
2726 	mode = *(const int *)of_id->data;
2727 
2728 	jpeg = devm_kzalloc(dev, sizeof(struct mxc_jpeg_dev), GFP_KERNEL);
2729 	if (!jpeg)
2730 		return -ENOMEM;
2731 
2732 	mutex_init(&jpeg->lock);
2733 	spin_lock_init(&jpeg->hw_lock);
2734 
2735 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2736 	if (ret) {
2737 		dev_err(&pdev->dev, "No suitable DMA available.\n");
2738 		goto err_irq;
2739 	}
2740 
2741 	jpeg->base_reg = devm_platform_ioremap_resource(pdev, 0);
2742 	if (IS_ERR(jpeg->base_reg))
2743 		return PTR_ERR(jpeg->base_reg);
2744 
2745 	for (slot = 0; slot < MXC_MAX_SLOTS; slot++) {
2746 		dec_irq = platform_get_irq(pdev, slot);
2747 		if (dec_irq < 0) {
2748 			ret = dec_irq;
2749 			goto err_irq;
2750 		}
2751 		ret = devm_request_irq(&pdev->dev, dec_irq, mxc_jpeg_dec_irq,
2752 				       0, pdev->name, jpeg);
2753 		if (ret) {
2754 			dev_err(&pdev->dev, "Failed to request irq %d (%d)\n",
2755 				dec_irq, ret);
2756 			goto err_irq;
2757 		}
2758 	}
2759 
2760 	jpeg->pdev = pdev;
2761 	jpeg->dev = dev;
2762 	jpeg->mode = mode;
2763 
2764 	/* Get clocks */
2765 	ret = devm_clk_bulk_get_all(&pdev->dev, &jpeg->clks);
2766 	if (ret < 0) {
2767 		dev_err(dev, "failed to get clock\n");
2768 		goto err_clk;
2769 	}
2770 	jpeg->num_clks = ret;
2771 
2772 	ret = mxc_jpeg_attach_pm_domains(jpeg);
2773 	if (ret < 0) {
2774 		dev_err(dev, "failed to attach power domains %d\n", ret);
2775 		return ret;
2776 	}
2777 
2778 	/* v4l2 */
2779 	ret = v4l2_device_register(dev, &jpeg->v4l2_dev);
2780 	if (ret) {
2781 		dev_err(dev, "failed to register v4l2 device\n");
2782 		goto err_register;
2783 	}
2784 	jpeg->m2m_dev = v4l2_m2m_init(&mxc_jpeg_m2m_ops);
2785 	if (IS_ERR(jpeg->m2m_dev)) {
2786 		dev_err(dev, "failed to register v4l2 device\n");
2787 		ret = PTR_ERR(jpeg->m2m_dev);
2788 		goto err_m2m;
2789 	}
2790 
2791 	jpeg->dec_vdev = video_device_alloc();
2792 	if (!jpeg->dec_vdev) {
2793 		dev_err(dev, "failed to register v4l2 device\n");
2794 		ret = -ENOMEM;
2795 		goto err_vdev_alloc;
2796 	}
2797 	if (mode == MXC_JPEG_ENCODE)
2798 		snprintf(jpeg->dec_vdev->name,
2799 			 sizeof(jpeg->dec_vdev->name),
2800 			 "%s-enc", MXC_JPEG_NAME);
2801 	else
2802 		snprintf(jpeg->dec_vdev->name,
2803 			 sizeof(jpeg->dec_vdev->name),
2804 			 "%s-dec", MXC_JPEG_NAME);
2805 
2806 	jpeg->dec_vdev->fops = &mxc_jpeg_fops;
2807 	jpeg->dec_vdev->ioctl_ops = &mxc_jpeg_ioctl_ops;
2808 	jpeg->dec_vdev->minor = -1;
2809 	jpeg->dec_vdev->release = video_device_release;
2810 	jpeg->dec_vdev->lock = &jpeg->lock; /* lock for ioctl serialization */
2811 	jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
2812 	jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
2813 	jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
2814 					V4L2_CAP_VIDEO_M2M_MPLANE;
2815 	if (mode == MXC_JPEG_ENCODE) {
2816 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_DECODER_CMD);
2817 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_DECODER_CMD);
2818 	} else {
2819 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_ENCODER_CMD);
2820 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_ENCODER_CMD);
2821 	}
2822 	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_VIDEO, -1);
2823 	if (ret) {
2824 		dev_err(dev, "failed to register video device\n");
2825 		goto err_vdev_register;
2826 	}
2827 	video_set_drvdata(jpeg->dec_vdev, jpeg);
2828 	if (mode == MXC_JPEG_ENCODE)
2829 		v4l2_info(&jpeg->v4l2_dev,
2830 			  "encoder device registered as /dev/video%d (%d,%d)\n",
2831 			  jpeg->dec_vdev->num, VIDEO_MAJOR,
2832 			  jpeg->dec_vdev->minor);
2833 	else
2834 		v4l2_info(&jpeg->v4l2_dev,
2835 			  "decoder device registered as /dev/video%d (%d,%d)\n",
2836 			  jpeg->dec_vdev->num, VIDEO_MAJOR,
2837 			  jpeg->dec_vdev->minor);
2838 
2839 	platform_set_drvdata(pdev, jpeg);
2840 	pm_runtime_enable(dev);
2841 
2842 	return 0;
2843 
2844 err_vdev_register:
2845 	video_device_release(jpeg->dec_vdev);
2846 
2847 err_vdev_alloc:
2848 	v4l2_m2m_release(jpeg->m2m_dev);
2849 
2850 err_m2m:
2851 	v4l2_device_unregister(&jpeg->v4l2_dev);
2852 
2853 err_register:
2854 	mxc_jpeg_detach_pm_domains(jpeg);
2855 
2856 err_irq:
2857 err_clk:
2858 	return ret;
2859 }
2860 
2861 #ifdef CONFIG_PM
2862 static int mxc_jpeg_runtime_resume(struct device *dev)
2863 {
2864 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2865 	int ret;
2866 
2867 	ret = clk_bulk_prepare_enable(jpeg->num_clks, jpeg->clks);
2868 	if (ret < 0) {
2869 		dev_err(dev, "failed to enable clock\n");
2870 		return ret;
2871 	}
2872 
2873 	return 0;
2874 }
2875 
2876 static int mxc_jpeg_runtime_suspend(struct device *dev)
2877 {
2878 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2879 
2880 	clk_bulk_disable_unprepare(jpeg->num_clks, jpeg->clks);
2881 
2882 	return 0;
2883 }
2884 #endif
2885 
2886 #ifdef CONFIG_PM_SLEEP
2887 static int mxc_jpeg_suspend(struct device *dev)
2888 {
2889 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2890 
2891 	v4l2_m2m_suspend(jpeg->m2m_dev);
2892 	return pm_runtime_force_suspend(dev);
2893 }
2894 
2895 static int mxc_jpeg_resume(struct device *dev)
2896 {
2897 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2898 	int ret;
2899 
2900 	ret = pm_runtime_force_resume(dev);
2901 	if (ret < 0)
2902 		return ret;
2903 
2904 	v4l2_m2m_resume(jpeg->m2m_dev);
2905 	return ret;
2906 }
2907 #endif
2908 
2909 static const struct dev_pm_ops	mxc_jpeg_pm_ops = {
2910 	SET_RUNTIME_PM_OPS(mxc_jpeg_runtime_suspend,
2911 			   mxc_jpeg_runtime_resume, NULL)
2912 	SET_SYSTEM_SLEEP_PM_OPS(mxc_jpeg_suspend, mxc_jpeg_resume)
2913 };
2914 
2915 static void mxc_jpeg_remove(struct platform_device *pdev)
2916 {
2917 	unsigned int slot;
2918 	struct mxc_jpeg_dev *jpeg = platform_get_drvdata(pdev);
2919 
2920 	for (slot = 0; slot < MXC_MAX_SLOTS; slot++)
2921 		mxc_jpeg_free_slot_data(jpeg, slot);
2922 
2923 	pm_runtime_disable(&pdev->dev);
2924 	video_unregister_device(jpeg->dec_vdev);
2925 	v4l2_m2m_release(jpeg->m2m_dev);
2926 	v4l2_device_unregister(&jpeg->v4l2_dev);
2927 	mxc_jpeg_detach_pm_domains(jpeg);
2928 }
2929 
2930 MODULE_DEVICE_TABLE(of, mxc_jpeg_match);
2931 
2932 static struct platform_driver mxc_jpeg_driver = {
2933 	.probe = mxc_jpeg_probe,
2934 	.remove_new = mxc_jpeg_remove,
2935 	.driver = {
2936 		.name = "mxc-jpeg",
2937 		.of_match_table = mxc_jpeg_match,
2938 		.pm = &mxc_jpeg_pm_ops,
2939 	},
2940 };
2941 module_platform_driver(mxc_jpeg_driver);
2942 
2943 MODULE_AUTHOR("Zhengyu Shen <zhengyu.shen_1@nxp.com>");
2944 MODULE_AUTHOR("Mirela Rabulea <mirela.rabulea@nxp.com>");
2945 MODULE_DESCRIPTION("V4L2 driver for i.MX8 QXP/QM JPEG encoder/decoder");
2946 MODULE_LICENSE("GPL v2");
2947