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 		.colplanes	= 1,
73 		.flags		= MXC_JPEG_FMT_TYPE_ENC,
74 	},
75 	{
76 		.name		= "BGR", /*BGR packed format*/
77 		.fourcc		= V4L2_PIX_FMT_BGR24,
78 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
79 		.nc		= 3,
80 		.depth		= 24,
81 		.colplanes	= 1,
82 		.h_align	= 3,
83 		.v_align	= 3,
84 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
85 		.precision	= 8,
86 	},
87 	{
88 		.name		= "ABGR", /* ABGR packed format */
89 		.fourcc		= V4L2_PIX_FMT_ABGR32,
90 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
91 		.nc		= 4,
92 		.depth		= 32,
93 		.colplanes	= 1,
94 		.h_align	= 3,
95 		.v_align	= 3,
96 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
97 		.precision	= 8,
98 	},
99 	{
100 		.name		= "YUV420", /* 1st plane = Y, 2nd plane = UV */
101 		.fourcc		= V4L2_PIX_FMT_NV12M,
102 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_420,
103 		.nc		= 3,
104 		.depth		= 12, /* 6 bytes (4Y + UV) for 4 pixels */
105 		.colplanes	= 2, /* 1 plane Y, 1 plane UV interleaved */
106 		.h_align	= 4,
107 		.v_align	= 4,
108 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
109 		.precision	= 8,
110 	},
111 	{
112 		.name		= "YUV422", /* YUYV */
113 		.fourcc		= V4L2_PIX_FMT_YUYV,
114 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_422,
115 		.nc		= 3,
116 		.depth		= 16,
117 		.colplanes	= 1,
118 		.h_align	= 4,
119 		.v_align	= 3,
120 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
121 		.precision	= 8,
122 	},
123 	{
124 		.name		= "YUV444", /* YUVYUV */
125 		.fourcc		= V4L2_PIX_FMT_YUV24,
126 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_444,
127 		.nc		= 3,
128 		.depth		= 24,
129 		.colplanes	= 1,
130 		.h_align	= 3,
131 		.v_align	= 3,
132 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
133 		.precision	= 8,
134 	},
135 	{
136 		.name		= "Gray", /* Gray (Y8/Y12) or Single Comp */
137 		.fourcc		= V4L2_PIX_FMT_GREY,
138 		.subsampling	= V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY,
139 		.nc		= 1,
140 		.depth		= 8,
141 		.colplanes	= 1,
142 		.h_align	= 3,
143 		.v_align	= 3,
144 		.flags		= MXC_JPEG_FMT_TYPE_RAW,
145 		.precision	= 8,
146 	},
147 };
148 
149 #define MXC_JPEG_NUM_FORMATS ARRAY_SIZE(mxc_formats)
150 
151 static const int mxc_decode_mode = MXC_JPEG_DECODE;
152 static const int mxc_encode_mode = MXC_JPEG_ENCODE;
153 
154 static const struct of_device_id mxc_jpeg_match[] = {
155 	{
156 		.compatible = "nxp,imx8qxp-jpgdec",
157 		.data       = &mxc_decode_mode,
158 	},
159 	{
160 		.compatible = "nxp,imx8qxp-jpgenc",
161 		.data       = &mxc_encode_mode,
162 	},
163 	{ },
164 };
165 
166 /*
167  * default configuration stream, 64x64 yuv422
168  * split by JPEG marker, so it's easier to modify & use
169  */
170 static const unsigned char jpeg_soi[] = {
171 	0xFF, 0xD8
172 };
173 
174 static const unsigned char jpeg_app0[] = {
175 	0xFF, 0xE0,
176 	0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00,
177 	0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01,
178 	0x00, 0x00
179 };
180 
181 static const unsigned char jpeg_app14[] = {
182 	0xFF, 0xEE,
183 	0x00, 0x0E, 0x41, 0x64, 0x6F, 0x62, 0x65,
184 	0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00
185 };
186 
187 static const unsigned char jpeg_dqt[] = {
188 	0xFF, 0xDB,
189 	0x00, 0x84, 0x00, 0x10, 0x0B, 0x0C, 0x0E,
190 	0x0C, 0x0A, 0x10, 0x0E, 0x0D, 0x0E, 0x12,
191 	0x11, 0x10, 0x13, 0x18, 0x28, 0x1A, 0x18,
192 	0x16, 0x16, 0x18, 0x31, 0x23, 0x25, 0x1D,
193 	0x28, 0x3A, 0x33, 0x3D, 0x3C, 0x39, 0x33,
194 	0x38, 0x37, 0x40, 0x48, 0x5C, 0x4E, 0x40,
195 	0x44, 0x57, 0x45, 0x37, 0x38, 0x50, 0x6D,
196 	0x51, 0x57, 0x5F, 0x62, 0x67, 0x68, 0x67,
197 	0x3E, 0x4D, 0x71, 0x79, 0x70, 0x64, 0x78,
198 	0x5C, 0x65, 0x67, 0x63, 0x01, 0x11, 0x12,
199 	0x12, 0x18, 0x15, 0x18, 0x2F, 0x1A, 0x1A,
200 	0x2F, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
201 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
202 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
203 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
204 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
205 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
206 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
207 	0x63, 0x63, 0x63, 0x63, 0x63, 0x63
208 };
209 
210 static const unsigned char jpeg_sof_maximal[] = {
211 	0xFF, 0xC0,
212 	0x00, 0x14, 0x08, 0x00, 0x40, 0x00, 0x40,
213 	0x04, 0x01, 0x11, 0x00, 0x02, 0x11, 0x01,
214 	0x03, 0x11, 0x01, 0x04, 0x11, 0x01
215 };
216 
217 static const unsigned char jpeg_dht[] = {
218 	0xFF, 0xC4,
219 	0x01, 0xA2, 0x00, 0x00, 0x01, 0x05, 0x01,
220 	0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
221 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
222 	0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
223 	0x09, 0x0A, 0x0B, 0x10, 0x00, 0x02, 0x01,
224 	0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05,
225 	0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
226 	0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
227 	0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
228 	0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91,
229 	0xA1, 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15,
230 	0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72,
231 	0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19,
232 	0x1A, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
233 	0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
234 	0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
235 	0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
236 	0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
237 	0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76,
238 	0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85,
239 	0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93,
240 	0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A,
241 	0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
242 	0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
243 	0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4,
244 	0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
245 	0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
246 	0xDA, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
247 	0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
248 	0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA,
249 	0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01,
250 	0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
251 	0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
252 	0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
253 	0x0B, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
254 	0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,
255 	0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02,
256 	0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06,
257 	0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13,
258 	0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
259 	0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52,
260 	0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
261 	0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18,
262 	0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A,
263 	0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43,
264 	0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A,
265 	0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
266 	0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
267 	0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77,
268 	0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85,
269 	0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93,
270 	0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A,
271 	0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
272 	0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
273 	0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4,
274 	0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
275 	0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
276 	0xDA, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
277 	0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5,
278 	0xF6, 0xF7, 0xF8, 0xF9, 0xFA
279 };
280 
281 static const unsigned char jpeg_dri[] = {
282 	0xFF, 0xDD,
283 	0x00, 0x04, 0x00, 0x20
284 };
285 
286 static const unsigned char jpeg_sos_maximal[] = {
287 	0xFF, 0xDA,
288 	0x00, 0x0C, 0x04, 0x01, 0x00, 0x02, 0x11, 0x03,
289 	0x11, 0x04, 0x11, 0x00, 0x3F, 0x00
290 };
291 
292 static const unsigned char jpeg_image_red[] = {
293 	0xFC, 0x5F, 0xA2, 0xBF, 0xCA, 0x73, 0xFE, 0xFE,
294 	0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00,
295 	0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02,
296 	0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28,
297 	0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A,
298 	0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0,
299 	0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00,
300 	0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02,
301 	0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00, 0x28,
302 	0xA0, 0x02, 0x8A, 0x00, 0x28, 0xA0, 0x02, 0x8A,
303 	0x00, 0x28, 0xA0, 0x02, 0x8A, 0x00
304 };
305 
306 static const unsigned char jpeg_eoi[] = {
307 	0xFF, 0xD9
308 };
309 
310 struct mxc_jpeg_src_buf {
311 	/* common v4l buffer stuff -- must be first */
312 	struct vb2_v4l2_buffer	b;
313 	struct list_head	list;
314 
315 	/* mxc-jpeg specific */
316 	bool			dht_needed;
317 	bool			jpeg_parse_error;
318 	const struct mxc_jpeg_fmt	*fmt;
319 	int			w;
320 	int			h;
321 };
322 
323 static inline struct mxc_jpeg_src_buf *vb2_to_mxc_buf(struct vb2_buffer *vb)
324 {
325 	return container_of(to_vb2_v4l2_buffer(vb),
326 			    struct mxc_jpeg_src_buf, b);
327 }
328 
329 static unsigned int debug;
330 module_param(debug, int, 0644);
331 MODULE_PARM_DESC(debug, "Debug level (0-3)");
332 
333 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision);
334 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q);
335 
336 static void _bswap16(u16 *a)
337 {
338 	*a = ((*a & 0x00FF) << 8) | ((*a & 0xFF00) >> 8);
339 }
340 
341 static void print_mxc_buf(struct mxc_jpeg_dev *jpeg, struct vb2_buffer *buf,
342 			  unsigned long len)
343 {
344 	unsigned int plane_no;
345 	u32 dma_addr;
346 	void *vaddr;
347 	unsigned long payload;
348 
349 	if (debug < 3)
350 		return;
351 
352 	for (plane_no = 0; plane_no < buf->num_planes; plane_no++) {
353 		payload = vb2_get_plane_payload(buf, plane_no);
354 		if (len == 0)
355 			len = payload;
356 		dma_addr = vb2_dma_contig_plane_dma_addr(buf, plane_no);
357 		vaddr = vb2_plane_vaddr(buf, plane_no);
358 		v4l2_dbg(3, debug, &jpeg->v4l2_dev,
359 			 "plane %d (vaddr=%p dma_addr=%x payload=%ld):",
360 			  plane_no, vaddr, dma_addr, payload);
361 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
362 			       vaddr, len, false);
363 	}
364 }
365 
366 static inline struct mxc_jpeg_ctx *mxc_jpeg_fh_to_ctx(struct v4l2_fh *fh)
367 {
368 	return container_of(fh, struct mxc_jpeg_ctx, fh);
369 }
370 
371 static int enum_fmt(const struct mxc_jpeg_fmt *mxc_formats, int n,
372 		    struct v4l2_fmtdesc *f, u32 type)
373 {
374 	int i, num = 0;
375 
376 	for (i = 0; i < n; ++i) {
377 		if (mxc_formats[i].flags == type) {
378 			/* index-th format of searched type found ? */
379 			if (num == f->index)
380 				break;
381 			/* Correct type but haven't reached our index yet,
382 			 * just increment per-type index
383 			 */
384 			++num;
385 		}
386 	}
387 
388 	/* Format not found */
389 	if (i >= n)
390 		return -EINVAL;
391 
392 	f->pixelformat = mxc_formats[i].fourcc;
393 
394 	return 0;
395 }
396 
397 static const struct mxc_jpeg_fmt *mxc_jpeg_find_format(struct mxc_jpeg_ctx *ctx,
398 						       u32 pixelformat)
399 {
400 	unsigned int k;
401 
402 	for (k = 0; k < MXC_JPEG_NUM_FORMATS; k++) {
403 		const struct mxc_jpeg_fmt *fmt = &mxc_formats[k];
404 
405 		if (fmt->fourcc == pixelformat)
406 			return fmt;
407 	}
408 	return NULL;
409 }
410 
411 static enum mxc_jpeg_image_format mxc_jpeg_fourcc_to_imgfmt(u32 fourcc)
412 {
413 	switch (fourcc) {
414 	case V4L2_PIX_FMT_GREY:
415 		return MXC_JPEG_GRAY;
416 	case V4L2_PIX_FMT_YUYV:
417 		return MXC_JPEG_YUV422;
418 	case V4L2_PIX_FMT_NV12M:
419 		return MXC_JPEG_YUV420;
420 	case V4L2_PIX_FMT_YUV24:
421 		return MXC_JPEG_YUV444;
422 	case V4L2_PIX_FMT_BGR24:
423 		return MXC_JPEG_BGR;
424 	case V4L2_PIX_FMT_ABGR32:
425 		return MXC_JPEG_ABGR;
426 	default:
427 		return MXC_JPEG_INVALID;
428 	}
429 }
430 
431 static struct mxc_jpeg_q_data *mxc_jpeg_get_q_data(struct mxc_jpeg_ctx *ctx,
432 						   enum v4l2_buf_type type)
433 {
434 	if (V4L2_TYPE_IS_OUTPUT(type))
435 		return &ctx->out_q;
436 	return &ctx->cap_q;
437 }
438 
439 static void mxc_jpeg_addrs(struct mxc_jpeg_desc *desc,
440 			   struct vb2_buffer *raw_buf,
441 			   struct vb2_buffer *jpeg_buf, int offset)
442 {
443 	int img_fmt = desc->stm_ctrl & STM_CTRL_IMAGE_FORMAT_MASK;
444 
445 	desc->buf_base0 = vb2_dma_contig_plane_dma_addr(raw_buf, 0);
446 	desc->buf_base1 = 0;
447 	if (img_fmt == STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV420)) {
448 		WARN_ON(raw_buf->num_planes < 2);
449 		desc->buf_base1 = vb2_dma_contig_plane_dma_addr(raw_buf, 1);
450 	}
451 	desc->stm_bufbase = vb2_dma_contig_plane_dma_addr(jpeg_buf, 0) +
452 		offset;
453 }
454 
455 static void notify_eos(struct mxc_jpeg_ctx *ctx)
456 {
457 	const struct v4l2_event ev = {
458 		.type = V4L2_EVENT_EOS
459 	};
460 
461 	dev_dbg(ctx->mxc_jpeg->dev, "Notify app event EOS reached");
462 	v4l2_event_queue_fh(&ctx->fh, &ev);
463 }
464 
465 static void notify_src_chg(struct mxc_jpeg_ctx *ctx)
466 {
467 	const struct v4l2_event ev = {
468 			.type = V4L2_EVENT_SOURCE_CHANGE,
469 			.u.src_change.changes =
470 			V4L2_EVENT_SRC_CH_RESOLUTION,
471 		};
472 
473 	dev_dbg(ctx->mxc_jpeg->dev, "Notify app event SRC_CH_RESOLUTION");
474 	v4l2_event_queue_fh(&ctx->fh, &ev);
475 }
476 
477 static int mxc_get_free_slot(struct mxc_jpeg_slot_data slot_data[], int n)
478 {
479 	int free_slot = 0;
480 
481 	while (slot_data[free_slot].used && free_slot < n)
482 		free_slot++;
483 
484 	return free_slot; /* >=n when there are no more free slots */
485 }
486 
487 static bool mxc_jpeg_alloc_slot_data(struct mxc_jpeg_dev *jpeg,
488 				     unsigned int slot)
489 {
490 	struct mxc_jpeg_desc *desc;
491 	struct mxc_jpeg_desc *cfg_desc;
492 	void *cfg_stm;
493 
494 	if (jpeg->slot_data[slot].desc)
495 		goto skip_alloc; /* already allocated, reuse it */
496 
497 	/* allocate descriptor for decoding/encoding phase */
498 	desc = dma_alloc_coherent(jpeg->dev,
499 				  sizeof(struct mxc_jpeg_desc),
500 				  &jpeg->slot_data[slot].desc_handle,
501 				  GFP_ATOMIC);
502 	if (!desc)
503 		goto err;
504 	jpeg->slot_data[slot].desc = desc;
505 
506 	/* allocate descriptor for configuration phase (encoder only) */
507 	cfg_desc = dma_alloc_coherent(jpeg->dev,
508 				      sizeof(struct mxc_jpeg_desc),
509 				      &jpeg->slot_data[slot].cfg_desc_handle,
510 				      GFP_ATOMIC);
511 	if (!cfg_desc)
512 		goto err;
513 	jpeg->slot_data[slot].cfg_desc = cfg_desc;
514 
515 	/* allocate configuration stream */
516 	cfg_stm = dma_alloc_coherent(jpeg->dev,
517 				     MXC_JPEG_MAX_CFG_STREAM,
518 				     &jpeg->slot_data[slot].cfg_stream_handle,
519 				     GFP_ATOMIC);
520 	if (!cfg_stm)
521 		goto err;
522 	jpeg->slot_data[slot].cfg_stream_vaddr = cfg_stm;
523 
524 skip_alloc:
525 	jpeg->slot_data[slot].used = true;
526 
527 	return true;
528 err:
529 	dev_err(jpeg->dev, "Could not allocate descriptors for slot %d", slot);
530 
531 	return false;
532 }
533 
534 static void mxc_jpeg_free_slot_data(struct mxc_jpeg_dev *jpeg,
535 				    unsigned int slot)
536 {
537 	if (slot >= MXC_MAX_SLOTS) {
538 		dev_err(jpeg->dev, "Invalid slot %d, nothing to free.", slot);
539 		return;
540 	}
541 
542 	/* free descriptor for decoding/encoding phase */
543 	dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc),
544 			  jpeg->slot_data[slot].desc,
545 			  jpeg->slot_data[slot].desc_handle);
546 
547 	/* free descriptor for encoder configuration phase / decoder DHT */
548 	dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc),
549 			  jpeg->slot_data[slot].cfg_desc,
550 			  jpeg->slot_data[slot].cfg_desc_handle);
551 
552 	/* free configuration stream */
553 	dma_free_coherent(jpeg->dev, MXC_JPEG_MAX_CFG_STREAM,
554 			  jpeg->slot_data[slot].cfg_stream_vaddr,
555 			  jpeg->slot_data[slot].cfg_stream_handle);
556 
557 	jpeg->slot_data[slot].used = false;
558 }
559 
560 static irqreturn_t mxc_jpeg_dec_irq(int irq, void *priv)
561 {
562 	struct mxc_jpeg_dev *jpeg = priv;
563 	struct mxc_jpeg_ctx *ctx;
564 	void __iomem *reg = jpeg->base_reg;
565 	struct device *dev = jpeg->dev;
566 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
567 	struct mxc_jpeg_src_buf *jpeg_src_buf;
568 	enum vb2_buffer_state buf_state;
569 	u32 dec_ret, com_status;
570 	unsigned long payload;
571 	struct mxc_jpeg_q_data *q_data;
572 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
573 	unsigned int slot;
574 
575 	spin_lock(&jpeg->hw_lock);
576 
577 	com_status = readl(reg + COM_STATUS);
578 	slot = COM_STATUS_CUR_SLOT(com_status);
579 	dev_dbg(dev, "Irq %d on slot %d.\n", irq, slot);
580 
581 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
582 	if (!ctx) {
583 		dev_err(dev,
584 			"Instance released before the end of transaction.\n");
585 		/* soft reset only resets internal state, not registers */
586 		mxc_jpeg_sw_reset(reg);
587 		/* clear all interrupts */
588 		writel(0xFFFFFFFF, reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS));
589 		goto job_unlock;
590 	}
591 
592 	if (slot != ctx->slot) {
593 		/* TODO investigate when adding multi-instance support */
594 		dev_warn(dev, "IRQ slot %d != context slot %d.\n",
595 			 slot, ctx->slot);
596 		goto job_unlock;
597 	}
598 
599 	dec_ret = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS));
600 	writel(dec_ret, reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS)); /* w1c */
601 
602 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
603 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
604 	if (!dst_buf || !src_buf) {
605 		dev_err(dev, "No source or destination buffer.\n");
606 		goto job_unlock;
607 	}
608 	jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf);
609 
610 	if (dec_ret & SLOT_STATUS_ENC_CONFIG_ERR) {
611 		u32 ret = readl(reg + CAST_STATUS12);
612 
613 		dev_err(dev, "Encoder/decoder error, status=0x%08x", ret);
614 		mxc_jpeg_sw_reset(reg);
615 		buf_state = VB2_BUF_STATE_ERROR;
616 		goto buffers_done;
617 	}
618 
619 	if (!(dec_ret & SLOT_STATUS_FRMDONE))
620 		goto job_unlock;
621 
622 	if (jpeg->mode == MXC_JPEG_ENCODE &&
623 	    ctx->enc_state == MXC_JPEG_ENC_CONF) {
624 		ctx->enc_state = MXC_JPEG_ENCODING;
625 		dev_dbg(dev, "Encoder config finished. Start encoding...\n");
626 		mxc_jpeg_enc_set_quality(dev, reg, ctx->jpeg_quality);
627 		mxc_jpeg_enc_mode_go(dev, reg);
628 		goto job_unlock;
629 	}
630 	if (jpeg->mode == MXC_JPEG_DECODE && jpeg_src_buf->dht_needed) {
631 		jpeg_src_buf->dht_needed = false;
632 		dev_dbg(dev, "Decoder DHT cfg finished. Start decoding...\n");
633 		goto job_unlock;
634 	}
635 	if (jpeg->mode == MXC_JPEG_ENCODE) {
636 		payload = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_BUF_PTR));
637 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
638 		dev_dbg(dev, "Encoding finished, payload size: %ld\n",
639 			payload);
640 	} else {
641 		q_data = mxc_jpeg_get_q_data(ctx, cap_type);
642 		payload = q_data->sizeimage[0];
643 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
644 		vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
645 		if (q_data->fmt->colplanes == 2) {
646 			payload = q_data->sizeimage[1];
647 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, payload);
648 		}
649 		dev_dbg(dev, "Decoding finished, payload size: %ld + %ld\n",
650 			vb2_get_plane_payload(&dst_buf->vb2_buf, 0),
651 			vb2_get_plane_payload(&dst_buf->vb2_buf, 1));
652 	}
653 
654 	/* short preview of the results */
655 	dev_dbg(dev, "src_buf preview: ");
656 	print_mxc_buf(jpeg, &src_buf->vb2_buf, 32);
657 	dev_dbg(dev, "dst_buf preview: ");
658 	print_mxc_buf(jpeg, &dst_buf->vb2_buf, 32);
659 	buf_state = VB2_BUF_STATE_DONE;
660 
661 buffers_done:
662 	jpeg->slot_data[slot].used = false; /* unused, but don't free */
663 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
664 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
665 	v4l2_m2m_buf_done(src_buf, buf_state);
666 	v4l2_m2m_buf_done(dst_buf, buf_state);
667 	spin_unlock(&jpeg->hw_lock);
668 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
669 	return IRQ_HANDLED;
670 job_unlock:
671 	spin_unlock(&jpeg->hw_lock);
672 	return IRQ_HANDLED;
673 }
674 
675 static int mxc_jpeg_fixup_sof(struct mxc_jpeg_sof *sof,
676 			      u32 fourcc,
677 			      u16 w, u16 h)
678 {
679 	int sof_length;
680 
681 	sof->precision = 8; /* TODO allow 8/12 bit precision*/
682 	sof->height = h;
683 	_bswap16(&sof->height);
684 	sof->width = w;
685 	_bswap16(&sof->width);
686 
687 	switch (fourcc) {
688 	case V4L2_PIX_FMT_NV12M:
689 		sof->components_no = 3;
690 		sof->comp[0].v = 0x2;
691 		sof->comp[0].h = 0x2;
692 		break;
693 	case V4L2_PIX_FMT_YUYV:
694 		sof->components_no = 3;
695 		sof->comp[0].v = 0x1;
696 		sof->comp[0].h = 0x2;
697 		break;
698 	case V4L2_PIX_FMT_YUV24:
699 	case V4L2_PIX_FMT_BGR24:
700 	default:
701 		sof->components_no = 3;
702 		break;
703 	case V4L2_PIX_FMT_ABGR32:
704 		sof->components_no = 4;
705 		break;
706 	case V4L2_PIX_FMT_GREY:
707 		sof->components_no = 1;
708 		break;
709 	}
710 	sof_length = 8 + 3 * sof->components_no;
711 	sof->length = sof_length;
712 	_bswap16(&sof->length);
713 
714 	return sof_length; /* not swaped */
715 }
716 
717 static int mxc_jpeg_fixup_sos(struct mxc_jpeg_sos *sos,
718 			      u32 fourcc)
719 {
720 	int sos_length;
721 	u8 *sof_u8 = (u8 *)sos;
722 
723 	switch (fourcc) {
724 	case V4L2_PIX_FMT_NV12M:
725 		sos->components_no = 3;
726 		break;
727 	case V4L2_PIX_FMT_YUYV:
728 		sos->components_no = 3;
729 		break;
730 	case V4L2_PIX_FMT_YUV24:
731 	case V4L2_PIX_FMT_BGR24:
732 	default:
733 		sos->components_no = 3;
734 		break;
735 	case V4L2_PIX_FMT_ABGR32:
736 		sos->components_no = 4;
737 		break;
738 	case V4L2_PIX_FMT_GREY:
739 		sos->components_no = 1;
740 		break;
741 	}
742 	sos_length = 6 + 2 * sos->components_no;
743 	sos->length = sos_length;
744 	_bswap16(&sos->length);
745 
746 	/* SOS ignorable bytes, not so ignorable after all */
747 	sof_u8[sos_length - 1] = 0x0;
748 	sof_u8[sos_length - 2] = 0x3f;
749 	sof_u8[sos_length - 3] = 0x0;
750 
751 	return sos_length; /* not swaped */
752 }
753 
754 static unsigned int mxc_jpeg_setup_cfg_stream(void *cfg_stream_vaddr,
755 					      u32 fourcc,
756 					      u16 w, u16 h)
757 {
758 	unsigned int offset = 0;
759 	u8 *cfg = (u8 *)cfg_stream_vaddr;
760 	struct mxc_jpeg_sof *sof;
761 	struct mxc_jpeg_sos *sos;
762 
763 	memcpy(cfg + offset, jpeg_soi, ARRAY_SIZE(jpeg_soi));
764 	offset += ARRAY_SIZE(jpeg_soi);
765 
766 	if (fourcc == V4L2_PIX_FMT_BGR24 ||
767 	    fourcc == V4L2_PIX_FMT_ABGR32) {
768 		memcpy(cfg + offset, jpeg_app14, sizeof(jpeg_app14));
769 		offset += sizeof(jpeg_app14);
770 	} else {
771 		memcpy(cfg + offset, jpeg_app0, sizeof(jpeg_app0));
772 		offset += sizeof(jpeg_app0);
773 	}
774 
775 	memcpy(cfg + offset, jpeg_dqt, sizeof(jpeg_dqt));
776 	offset += sizeof(jpeg_dqt);
777 
778 	memcpy(cfg + offset, jpeg_sof_maximal, sizeof(jpeg_sof_maximal));
779 	offset += 2; /* skip marker ID */
780 	sof = (struct mxc_jpeg_sof *)(cfg + offset);
781 	offset += mxc_jpeg_fixup_sof(sof, fourcc, w, h);
782 
783 	memcpy(cfg + offset, jpeg_dht, sizeof(jpeg_dht));
784 	offset += sizeof(jpeg_dht);
785 
786 	memcpy(cfg + offset, jpeg_dri, sizeof(jpeg_dri));
787 	offset += sizeof(jpeg_dri);
788 
789 	memcpy(cfg + offset, jpeg_sos_maximal, sizeof(jpeg_sos_maximal));
790 	offset += 2; /* skip marker ID */
791 	sos = (struct mxc_jpeg_sos *)(cfg + offset);
792 	offset += mxc_jpeg_fixup_sos(sos, fourcc);
793 
794 	memcpy(cfg + offset, jpeg_image_red, sizeof(jpeg_image_red));
795 	offset += sizeof(jpeg_image_red);
796 
797 	memcpy(cfg + offset, jpeg_eoi, sizeof(jpeg_eoi));
798 	offset += sizeof(jpeg_eoi);
799 
800 	return offset;
801 }
802 
803 static void mxc_jpeg_config_dec_desc(struct vb2_buffer *out_buf,
804 				     struct mxc_jpeg_ctx *ctx,
805 				     struct vb2_buffer *src_buf,
806 				     struct vb2_buffer *dst_buf)
807 {
808 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
809 	struct mxc_jpeg_q_data *q_data_cap;
810 	enum mxc_jpeg_image_format img_fmt;
811 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
812 	void __iomem *reg = jpeg->base_reg;
813 	unsigned int slot = ctx->slot;
814 	struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc;
815 	struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc;
816 	dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle;
817 	dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle;
818 	dma_addr_t cfg_stream_handle = jpeg->slot_data[slot].cfg_stream_handle;
819 	unsigned int *cfg_size = &jpeg->slot_data[slot].cfg_stream_size;
820 	void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr;
821 	struct mxc_jpeg_src_buf *jpeg_src_buf;
822 
823 	jpeg_src_buf = vb2_to_mxc_buf(src_buf);
824 
825 	/* setup the decoding descriptor */
826 	desc->next_descpt_ptr = 0; /* end of chain */
827 	q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type);
828 	desc->imgsize = q_data_cap->w_adjusted << 16 | q_data_cap->h_adjusted;
829 	img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data_cap->fmt->fourcc);
830 	desc->stm_ctrl &= ~STM_CTRL_IMAGE_FORMAT(0xF); /* clear image format */
831 	desc->stm_ctrl |= STM_CTRL_IMAGE_FORMAT(img_fmt);
832 	desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
833 	desc->line_pitch = q_data_cap->bytesperline[0];
834 	mxc_jpeg_addrs(desc, dst_buf, src_buf, 0);
835 	mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(src_buf, 0), 1024));
836 	print_descriptor_info(jpeg->dev, desc);
837 
838 	if (!jpeg_src_buf->dht_needed) {
839 		/* validate the decoding descriptor */
840 		mxc_jpeg_set_desc(desc_handle, reg, slot);
841 		return;
842 	}
843 
844 	/*
845 	 * if a default huffman table is needed, use the config descriptor to
846 	 * inject a DHT, by chaining it before the decoding descriptor
847 	 */
848 	*cfg_size = mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr,
849 					      V4L2_PIX_FMT_YUYV,
850 					      MXC_JPEG_MIN_WIDTH,
851 					      MXC_JPEG_MIN_HEIGHT);
852 	cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN;
853 	cfg_desc->buf_base0 = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
854 	cfg_desc->buf_base1 = 0;
855 	cfg_desc->imgsize = MXC_JPEG_MIN_WIDTH << 16;
856 	cfg_desc->imgsize |= MXC_JPEG_MIN_HEIGHT;
857 	cfg_desc->line_pitch = MXC_JPEG_MIN_WIDTH * 2;
858 	cfg_desc->stm_ctrl = STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV422);
859 	cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
860 	cfg_desc->stm_bufbase = cfg_stream_handle;
861 	cfg_desc->stm_bufsize = ALIGN(*cfg_size, 1024);
862 	print_descriptor_info(jpeg->dev, cfg_desc);
863 
864 	/* validate the configuration descriptor */
865 	mxc_jpeg_set_desc(cfg_desc_handle, reg, slot);
866 }
867 
868 static void mxc_jpeg_config_enc_desc(struct vb2_buffer *out_buf,
869 				     struct mxc_jpeg_ctx *ctx,
870 				     struct vb2_buffer *src_buf,
871 				     struct vb2_buffer *dst_buf)
872 {
873 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
874 	void __iomem *reg = jpeg->base_reg;
875 	unsigned int slot = ctx->slot;
876 	struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc;
877 	struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc;
878 	dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle;
879 	dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle;
880 	void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr;
881 	struct mxc_jpeg_q_data *q_data;
882 	enum mxc_jpeg_image_format img_fmt;
883 	int w, h;
884 
885 	q_data = mxc_jpeg_get_q_data(ctx, src_buf->vb2_queue->type);
886 
887 	jpeg->slot_data[slot].cfg_stream_size =
888 			mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr,
889 						  q_data->fmt->fourcc,
890 						  q_data->w_adjusted,
891 						  q_data->h_adjusted);
892 
893 	/* chain the config descriptor with the encoding descriptor */
894 	cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN;
895 
896 	cfg_desc->buf_base0 = jpeg->slot_data[slot].cfg_stream_handle;
897 	cfg_desc->buf_base1 = 0;
898 	cfg_desc->line_pitch = 0;
899 	cfg_desc->stm_bufbase = 0; /* no output expected */
900 	cfg_desc->stm_bufsize = 0x0;
901 	cfg_desc->imgsize = 0;
902 	cfg_desc->stm_ctrl = STM_CTRL_CONFIG_MOD(1);
903 	cfg_desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
904 
905 	desc->next_descpt_ptr = 0; /* end of chain */
906 
907 	/* use adjusted resolution for CAST IP job */
908 	w = q_data->w_adjusted;
909 	h = q_data->h_adjusted;
910 	mxc_jpeg_set_res(desc, w, h);
911 	mxc_jpeg_set_line_pitch(desc, w * (q_data->fmt->depth / 8));
912 	mxc_jpeg_set_bufsize(desc, desc->line_pitch * h);
913 	img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data->fmt->fourcc);
914 	if (img_fmt == MXC_JPEG_INVALID)
915 		dev_err(jpeg->dev, "No valid image format detected\n");
916 	desc->stm_ctrl = STM_CTRL_CONFIG_MOD(0) |
917 			 STM_CTRL_IMAGE_FORMAT(img_fmt);
918 	desc->stm_ctrl |= STM_CTRL_BITBUF_PTR_CLR(1);
919 	mxc_jpeg_addrs(desc, src_buf, dst_buf, 0);
920 	dev_dbg(jpeg->dev, "cfg_desc:\n");
921 	print_descriptor_info(jpeg->dev, cfg_desc);
922 	dev_dbg(jpeg->dev, "enc desc:\n");
923 	print_descriptor_info(jpeg->dev, desc);
924 	print_wrapper_info(jpeg->dev, reg);
925 	print_cast_status(jpeg->dev, reg, MXC_JPEG_ENCODE);
926 
927 	/* validate the configuration descriptor */
928 	mxc_jpeg_set_desc(cfg_desc_handle, reg, slot);
929 }
930 
931 static bool mxc_jpeg_source_change(struct mxc_jpeg_ctx *ctx,
932 				   struct mxc_jpeg_src_buf *jpeg_src_buf)
933 {
934 	struct device *dev = ctx->mxc_jpeg->dev;
935 	struct mxc_jpeg_q_data *q_data_cap;
936 
937 	if (!jpeg_src_buf->fmt)
938 		return false;
939 
940 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
941 	if (q_data_cap->fmt != jpeg_src_buf->fmt ||
942 	    q_data_cap->w != jpeg_src_buf->w ||
943 	    q_data_cap->h != jpeg_src_buf->h) {
944 		dev_dbg(dev, "Detected jpeg res=(%dx%d)->(%dx%d), pixfmt=%c%c%c%c\n",
945 			q_data_cap->w, q_data_cap->h,
946 			jpeg_src_buf->w, jpeg_src_buf->h,
947 			(jpeg_src_buf->fmt->fourcc & 0xff),
948 			(jpeg_src_buf->fmt->fourcc >>  8) & 0xff,
949 			(jpeg_src_buf->fmt->fourcc >> 16) & 0xff,
950 			(jpeg_src_buf->fmt->fourcc >> 24) & 0xff);
951 
952 		/*
953 		 * set-up the capture queue with the pixelformat and resolution
954 		 * detected from the jpeg output stream
955 		 */
956 		q_data_cap->w = jpeg_src_buf->w;
957 		q_data_cap->h = jpeg_src_buf->h;
958 		q_data_cap->fmt = jpeg_src_buf->fmt;
959 		q_data_cap->w_adjusted = q_data_cap->w;
960 		q_data_cap->h_adjusted = q_data_cap->h;
961 
962 		/*
963 		 * align up the resolution for CAST IP,
964 		 * but leave the buffer resolution unchanged
965 		 */
966 		v4l_bound_align_image(&q_data_cap->w_adjusted,
967 				      q_data_cap->w_adjusted,  /* adjust up */
968 				      MXC_JPEG_MAX_WIDTH,
969 				      q_data_cap->fmt->h_align,
970 				      &q_data_cap->h_adjusted,
971 				      q_data_cap->h_adjusted, /* adjust up */
972 				      MXC_JPEG_MAX_HEIGHT,
973 				      q_data_cap->fmt->v_align,
974 				      0);
975 
976 		/* setup bytesperline/sizeimage for capture queue */
977 		mxc_jpeg_bytesperline(q_data_cap, jpeg_src_buf->fmt->precision);
978 		mxc_jpeg_sizeimage(q_data_cap);
979 		notify_src_chg(ctx);
980 		ctx->source_change = 1;
981 	}
982 	return ctx->source_change ? true : false;
983 }
984 
985 static int mxc_jpeg_job_ready(void *priv)
986 {
987 	struct mxc_jpeg_ctx *ctx = priv;
988 
989 	return ctx->source_change ? 0 : 1;
990 }
991 
992 static void mxc_jpeg_device_run(void *priv)
993 {
994 	struct mxc_jpeg_ctx *ctx = priv;
995 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
996 	void __iomem *reg = jpeg->base_reg;
997 	struct device *dev = jpeg->dev;
998 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
999 	unsigned long flags;
1000 	struct mxc_jpeg_q_data *q_data_cap, *q_data_out;
1001 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1002 
1003 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
1004 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1005 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1006 	if (!src_buf || !dst_buf) {
1007 		dev_err(dev, "Null src or dst buf\n");
1008 		goto end;
1009 	}
1010 
1011 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1012 	if (!q_data_cap)
1013 		goto end;
1014 	q_data_out = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1015 	if (!q_data_out)
1016 		goto end;
1017 	src_buf->sequence = q_data_out->sequence++;
1018 	dst_buf->sequence = q_data_cap->sequence++;
1019 
1020 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
1021 
1022 	jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf);
1023 	if (q_data_cap->fmt->colplanes != dst_buf->vb2_buf.num_planes) {
1024 		dev_err(dev, "Capture format %s has %d planes, but capture buffer has %d planes\n",
1025 			q_data_cap->fmt->name, q_data_cap->fmt->colplanes,
1026 			dst_buf->vb2_buf.num_planes);
1027 		jpeg_src_buf->jpeg_parse_error = true;
1028 	}
1029 	if (jpeg_src_buf->jpeg_parse_error) {
1030 		v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1031 		v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1032 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1033 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1034 		spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1035 		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1036 
1037 		return;
1038 	}
1039 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) {
1040 		if (ctx->source_change || mxc_jpeg_source_change(ctx, jpeg_src_buf)) {
1041 			spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1042 			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1043 			return;
1044 		}
1045 	}
1046 
1047 	mxc_jpeg_enable(reg);
1048 	mxc_jpeg_set_l_endian(reg, 1);
1049 
1050 	ctx->slot = mxc_get_free_slot(jpeg->slot_data, MXC_MAX_SLOTS);
1051 	if (ctx->slot >= MXC_MAX_SLOTS) {
1052 		dev_err(dev, "No more free slots\n");
1053 		goto end;
1054 	}
1055 	if (!mxc_jpeg_alloc_slot_data(jpeg, ctx->slot)) {
1056 		dev_err(dev, "Cannot allocate slot data\n");
1057 		goto end;
1058 	}
1059 
1060 	mxc_jpeg_enable_slot(reg, ctx->slot);
1061 	mxc_jpeg_enable_irq(reg, ctx->slot);
1062 
1063 	if (jpeg->mode == MXC_JPEG_ENCODE) {
1064 		dev_dbg(dev, "Encoding on slot %d\n", ctx->slot);
1065 		ctx->enc_state = MXC_JPEG_ENC_CONF;
1066 		mxc_jpeg_config_enc_desc(&dst_buf->vb2_buf, ctx,
1067 					 &src_buf->vb2_buf, &dst_buf->vb2_buf);
1068 		mxc_jpeg_enc_mode_conf(dev, reg); /* start config phase */
1069 	} else {
1070 		dev_dbg(dev, "Decoding on slot %d\n", ctx->slot);
1071 		print_mxc_buf(jpeg, &src_buf->vb2_buf, 0);
1072 		mxc_jpeg_config_dec_desc(&dst_buf->vb2_buf, ctx,
1073 					 &src_buf->vb2_buf, &dst_buf->vb2_buf);
1074 		mxc_jpeg_dec_mode_go(dev, reg);
1075 	}
1076 end:
1077 	spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1078 }
1079 
1080 static void mxc_jpeg_set_last_buffer_dequeued(struct mxc_jpeg_ctx *ctx)
1081 {
1082 	struct vb2_queue *q;
1083 
1084 	ctx->stopped = 1;
1085 	q = v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx);
1086 	if (!list_empty(&q->done_list))
1087 		return;
1088 
1089 	q->last_buffer_dequeued = true;
1090 	wake_up(&q->done_wq);
1091 	ctx->stopped = 0;
1092 	ctx->header_parsed = false;
1093 }
1094 
1095 static int mxc_jpeg_decoder_cmd(struct file *file, void *priv,
1096 				struct v4l2_decoder_cmd *cmd)
1097 {
1098 	struct v4l2_fh *fh = file->private_data;
1099 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
1100 	struct device *dev = ctx->mxc_jpeg->dev;
1101 	int ret;
1102 
1103 	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
1104 	if (ret < 0)
1105 		return ret;
1106 
1107 	if (cmd->cmd == V4L2_DEC_CMD_STOP) {
1108 		dev_dbg(dev, "Received V4L2_DEC_CMD_STOP");
1109 		if (v4l2_m2m_num_src_bufs_ready(fh->m2m_ctx) == 0) {
1110 			/* No more src bufs, notify app EOS */
1111 			notify_eos(ctx);
1112 			mxc_jpeg_set_last_buffer_dequeued(ctx);
1113 		} else {
1114 			/* will send EOS later*/
1115 			ctx->stopping = 1;
1116 		}
1117 	}
1118 
1119 	return 0;
1120 }
1121 
1122 static int mxc_jpeg_encoder_cmd(struct file *file, void *priv,
1123 				struct v4l2_encoder_cmd *cmd)
1124 {
1125 	struct v4l2_fh *fh = file->private_data;
1126 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
1127 	struct device *dev = ctx->mxc_jpeg->dev;
1128 	int ret;
1129 
1130 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
1131 	if (ret < 0)
1132 		return ret;
1133 
1134 	if (cmd->cmd == V4L2_ENC_CMD_STOP) {
1135 		dev_dbg(dev, "Received V4L2_ENC_CMD_STOP");
1136 		if (v4l2_m2m_num_src_bufs_ready(fh->m2m_ctx) == 0) {
1137 			/* No more src bufs, notify app EOS */
1138 			notify_eos(ctx);
1139 			mxc_jpeg_set_last_buffer_dequeued(ctx);
1140 		} else {
1141 			/* will send EOS later*/
1142 			ctx->stopping = 1;
1143 		}
1144 	}
1145 
1146 	return 0;
1147 }
1148 
1149 static int mxc_jpeg_queue_setup(struct vb2_queue *q,
1150 				unsigned int *nbuffers,
1151 				unsigned int *nplanes,
1152 				unsigned int sizes[],
1153 				struct device *alloc_ctxs[])
1154 {
1155 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1156 	struct mxc_jpeg_q_data *q_data = NULL;
1157 	int i;
1158 
1159 	q_data = mxc_jpeg_get_q_data(ctx, q->type);
1160 	if (!q_data)
1161 		return -EINVAL;
1162 
1163 	/* Handle CREATE_BUFS situation - *nplanes != 0 */
1164 	if (*nplanes) {
1165 		if (*nplanes != q_data->fmt->colplanes)
1166 			return -EINVAL;
1167 		for (i = 0; i < *nplanes; i++) {
1168 			if (sizes[i] < q_data->sizeimage[i])
1169 				return -EINVAL;
1170 		}
1171 		return 0;
1172 	}
1173 
1174 	/* Handle REQBUFS situation */
1175 	*nplanes = q_data->fmt->colplanes;
1176 	for (i = 0; i < *nplanes; i++)
1177 		sizes[i] = q_data->sizeimage[i];
1178 
1179 	return 0;
1180 }
1181 
1182 static int mxc_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
1183 {
1184 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1185 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, q->type);
1186 	int ret;
1187 
1188 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(q->type))
1189 		ctx->source_change = 0;
1190 	dev_dbg(ctx->mxc_jpeg->dev, "Start streaming ctx=%p", ctx);
1191 	q_data->sequence = 0;
1192 
1193 	ret = pm_runtime_resume_and_get(ctx->mxc_jpeg->dev);
1194 	if (ret < 0) {
1195 		dev_err(ctx->mxc_jpeg->dev, "Failed to power up jpeg\n");
1196 		return ret;
1197 	}
1198 
1199 	return 0;
1200 }
1201 
1202 static void mxc_jpeg_stop_streaming(struct vb2_queue *q)
1203 {
1204 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1205 	struct vb2_v4l2_buffer *vbuf;
1206 
1207 	dev_dbg(ctx->mxc_jpeg->dev, "Stop streaming ctx=%p", ctx);
1208 
1209 	/* Release all active buffers */
1210 	for (;;) {
1211 		if (V4L2_TYPE_IS_OUTPUT(q->type))
1212 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1213 		else
1214 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1215 		if (!vbuf)
1216 			break;
1217 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1218 	}
1219 	pm_runtime_put_sync(&ctx->mxc_jpeg->pdev->dev);
1220 	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
1221 		ctx->stopping = 0;
1222 		ctx->stopped = 0;
1223 	}
1224 }
1225 
1226 static int mxc_jpeg_valid_comp_id(struct device *dev,
1227 				  struct mxc_jpeg_sof *sof,
1228 				  struct mxc_jpeg_sos *sos)
1229 {
1230 	int valid = 1;
1231 	int i;
1232 
1233 	/*
1234 	 * there's a limitation in the IP that the component IDs must be
1235 	 * between 0..4, if they are not, let's patch them
1236 	 */
1237 	for (i = 0; i < sof->components_no; i++)
1238 		if (sof->comp[i].id > MXC_JPEG_MAX_COMPONENTS) {
1239 			valid = 0;
1240 			dev_err(dev, "Component %d has invalid ID: %d",
1241 				i, sof->comp[i].id);
1242 		}
1243 	if (!valid)
1244 		/* patch all comp IDs if at least one is invalid */
1245 		for (i = 0; i < sof->components_no; i++) {
1246 			dev_warn(dev, "Component %d ID patched to: %d",
1247 				 i, i + 1);
1248 			sof->comp[i].id = i + 1;
1249 			sos->comp[i].id = i + 1;
1250 		}
1251 
1252 	return valid;
1253 }
1254 
1255 static u32 mxc_jpeg_get_image_format(struct device *dev,
1256 				     const struct v4l2_jpeg_header *header)
1257 {
1258 	int i;
1259 	u32 fourcc = 0;
1260 
1261 	for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++)
1262 		if (mxc_formats[i].subsampling == header->frame.subsampling &&
1263 		    mxc_formats[i].nc == header->frame.num_components &&
1264 		    mxc_formats[i].precision == header->frame.precision) {
1265 			fourcc = mxc_formats[i].fourcc;
1266 			break;
1267 		}
1268 	if (fourcc == 0) {
1269 		dev_err(dev,
1270 			"Could not identify image format nc=%d, subsampling=%d, precision=%d\n",
1271 			header->frame.num_components,
1272 			header->frame.subsampling,
1273 			header->frame.precision);
1274 		return fourcc;
1275 	}
1276 	/*
1277 	 * If the transform flag from APP14 marker is 0, images that are
1278 	 * encoded with 3 components have RGB colorspace, see Recommendation
1279 	 * ITU-T T.872 chapter 6.5.3 APP14 marker segment for colour encoding
1280 	 */
1281 	if (fourcc == V4L2_PIX_FMT_YUV24 || fourcc == V4L2_PIX_FMT_BGR24) {
1282 		if (header->app14_tf == V4L2_JPEG_APP14_TF_CMYK_RGB)
1283 			fourcc = V4L2_PIX_FMT_BGR24;
1284 		else
1285 			fourcc = V4L2_PIX_FMT_YUV24;
1286 	}
1287 
1288 	return fourcc;
1289 }
1290 
1291 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision)
1292 {
1293 	/* Bytes distance between the leftmost pixels in two adjacent lines */
1294 	if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1295 		/* bytesperline unused for compressed formats */
1296 		q->bytesperline[0] = 0;
1297 		q->bytesperline[1] = 0;
1298 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
1299 		/* When the image format is planar the bytesperline value
1300 		 * applies to the first plane and is divided by the same factor
1301 		 * as the width field for the other planes
1302 		 */
1303 		q->bytesperline[0] = q->w * DIV_ROUND_UP(precision, 8);
1304 		q->bytesperline[1] = q->bytesperline[0];
1305 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
1306 		q->bytesperline[0] = q->w * DIV_ROUND_UP(precision, 8) * 2;
1307 		q->bytesperline[1] = 0;
1308 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) {
1309 		q->bytesperline[0] = q->w * DIV_ROUND_UP(precision, 8) * q->fmt->nc;
1310 		q->bytesperline[1] = 0;
1311 	} else {
1312 		/* grayscale */
1313 		q->bytesperline[0] = q->w * DIV_ROUND_UP(precision, 8);
1314 		q->bytesperline[1] = 0;
1315 	}
1316 }
1317 
1318 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q)
1319 {
1320 	if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1321 		/* if no sizeimage from user, assume worst jpeg compression */
1322 		if (!q->sizeimage[0])
1323 			q->sizeimage[0] = 6 * q->w * q->h;
1324 		q->sizeimage[1] = 0;
1325 
1326 		if (q->sizeimage[0] > MXC_JPEG_MAX_SIZEIMAGE)
1327 			q->sizeimage[0] = MXC_JPEG_MAX_SIZEIMAGE;
1328 
1329 		/* jpeg stream size must be multiple of 1K */
1330 		q->sizeimage[0] = ALIGN(q->sizeimage[0], 1024);
1331 	} else {
1332 		q->sizeimage[0] = q->bytesperline[0] * q->h;
1333 		q->sizeimage[1] = 0;
1334 		if (q->fmt->fourcc == V4L2_PIX_FMT_NV12M)
1335 			q->sizeimage[1] = q->sizeimage[0] / 2;
1336 	}
1337 }
1338 
1339 static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx, struct vb2_buffer *vb)
1340 {
1341 	struct device *dev = ctx->mxc_jpeg->dev;
1342 	struct mxc_jpeg_q_data *q_data_out;
1343 	u32 fourcc;
1344 	struct v4l2_jpeg_header header;
1345 	struct mxc_jpeg_sof *psof = NULL;
1346 	struct mxc_jpeg_sos *psos = NULL;
1347 	struct mxc_jpeg_src_buf *jpeg_src_buf = vb2_to_mxc_buf(vb);
1348 	u8 *src_addr = (u8 *)vb2_plane_vaddr(vb, 0);
1349 	u32 size = vb2_get_plane_payload(vb, 0);
1350 	int ret;
1351 
1352 	memset(&header, 0, sizeof(header));
1353 	ret = v4l2_jpeg_parse_header((void *)src_addr, size, &header);
1354 	if (ret < 0) {
1355 		dev_err(dev, "Error parsing JPEG stream markers\n");
1356 		return ret;
1357 	}
1358 
1359 	/* if DHT marker present, no need to inject default one */
1360 	jpeg_src_buf->dht_needed = (header.num_dht == 0);
1361 
1362 	q_data_out = mxc_jpeg_get_q_data(ctx,
1363 					 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1364 	if (q_data_out->w == 0 && q_data_out->h == 0) {
1365 		dev_warn(dev, "Invalid user resolution 0x0");
1366 		dev_warn(dev, "Keeping resolution from JPEG: %dx%d",
1367 			 header.frame.width, header.frame.height);
1368 	} else if (header.frame.width != q_data_out->w ||
1369 		   header.frame.height != q_data_out->h) {
1370 		dev_err(dev,
1371 			"Resolution mismatch: %dx%d (JPEG) versus %dx%d(user)",
1372 			header.frame.width, header.frame.height,
1373 			q_data_out->w, q_data_out->h);
1374 	}
1375 	q_data_out->w = header.frame.width;
1376 	q_data_out->h = header.frame.height;
1377 	if (header.frame.width % 8 != 0 || header.frame.height % 8 != 0) {
1378 		dev_err(dev, "JPEG width or height not multiple of 8: %dx%d\n",
1379 			header.frame.width, header.frame.height);
1380 		return -EINVAL;
1381 	}
1382 	if (header.frame.width > MXC_JPEG_MAX_WIDTH ||
1383 	    header.frame.height > MXC_JPEG_MAX_HEIGHT) {
1384 		dev_err(dev, "JPEG width or height should be <= 8192: %dx%d\n",
1385 			header.frame.width, header.frame.height);
1386 		return -EINVAL;
1387 	}
1388 	if (header.frame.width < MXC_JPEG_MIN_WIDTH ||
1389 	    header.frame.height < MXC_JPEG_MIN_HEIGHT) {
1390 		dev_err(dev, "JPEG width or height should be > 64: %dx%d\n",
1391 			header.frame.width, header.frame.height);
1392 		return -EINVAL;
1393 	}
1394 	if (header.frame.num_components > V4L2_JPEG_MAX_COMPONENTS) {
1395 		dev_err(dev, "JPEG number of components should be <=%d",
1396 			V4L2_JPEG_MAX_COMPONENTS);
1397 		return -EINVAL;
1398 	}
1399 	/* check and, if necessary, patch component IDs*/
1400 	psof = (struct mxc_jpeg_sof *)header.sof.start;
1401 	psos = (struct mxc_jpeg_sos *)header.sos.start;
1402 	if (!mxc_jpeg_valid_comp_id(dev, psof, psos))
1403 		dev_warn(dev, "JPEG component ids should be 0-3 or 1-4");
1404 
1405 	fourcc = mxc_jpeg_get_image_format(dev, &header);
1406 	if (fourcc == 0)
1407 		return -EINVAL;
1408 
1409 	jpeg_src_buf->fmt = mxc_jpeg_find_format(ctx, fourcc);
1410 	jpeg_src_buf->w = header.frame.width;
1411 	jpeg_src_buf->h = header.frame.height;
1412 	ctx->header_parsed = true;
1413 
1414 	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx))
1415 		mxc_jpeg_source_change(ctx, jpeg_src_buf);
1416 
1417 	return 0;
1418 }
1419 
1420 static void mxc_jpeg_buf_queue(struct vb2_buffer *vb)
1421 {
1422 	int ret;
1423 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1424 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1425 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1426 
1427 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1428 		goto end;
1429 
1430 	/* for V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE */
1431 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
1432 		goto end;
1433 
1434 	jpeg_src_buf = vb2_to_mxc_buf(vb);
1435 	jpeg_src_buf->jpeg_parse_error = false;
1436 	ret = mxc_jpeg_parse(ctx, vb);
1437 	if (ret)
1438 		jpeg_src_buf->jpeg_parse_error = true;
1439 
1440 end:
1441 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1442 }
1443 
1444 static int mxc_jpeg_buf_out_validate(struct vb2_buffer *vb)
1445 {
1446 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1447 
1448 	vbuf->field = V4L2_FIELD_NONE;
1449 
1450 	return 0;
1451 }
1452 
1453 static int mxc_jpeg_buf_prepare(struct vb2_buffer *vb)
1454 {
1455 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1456 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1457 	struct mxc_jpeg_q_data *q_data = NULL;
1458 	struct device *dev = ctx->mxc_jpeg->dev;
1459 	unsigned long sizeimage;
1460 	int i;
1461 
1462 	vbuf->field = V4L2_FIELD_NONE;
1463 
1464 	q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type);
1465 	if (!q_data)
1466 		return -EINVAL;
1467 	for (i = 0; i < q_data->fmt->colplanes; i++) {
1468 		sizeimage = q_data->sizeimage[i];
1469 		if (vb2_plane_size(vb, i) < sizeimage) {
1470 			dev_err(dev, "plane %d too small (%lu < %lu)",
1471 				i, vb2_plane_size(vb, i), sizeimage);
1472 			return -EINVAL;
1473 		}
1474 	}
1475 	return 0;
1476 }
1477 
1478 static void mxc_jpeg_buf_finish(struct vb2_buffer *vb)
1479 {
1480 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1481 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1482 	struct vb2_queue *q = vb->vb2_queue;
1483 
1484 	if (V4L2_TYPE_IS_OUTPUT(vb->type))
1485 		return;
1486 	if (!ctx->stopped)
1487 		return;
1488 	if (list_empty(&q->done_list)) {
1489 		vbuf->flags |= V4L2_BUF_FLAG_LAST;
1490 		ctx->stopped = 0;
1491 		ctx->header_parsed = false;
1492 	}
1493 }
1494 
1495 static const struct vb2_ops mxc_jpeg_qops = {
1496 	.queue_setup		= mxc_jpeg_queue_setup,
1497 	.wait_prepare		= vb2_ops_wait_prepare,
1498 	.wait_finish		= vb2_ops_wait_finish,
1499 	.buf_out_validate	= mxc_jpeg_buf_out_validate,
1500 	.buf_prepare		= mxc_jpeg_buf_prepare,
1501 	.buf_finish             = mxc_jpeg_buf_finish,
1502 	.start_streaming	= mxc_jpeg_start_streaming,
1503 	.stop_streaming		= mxc_jpeg_stop_streaming,
1504 	.buf_queue		= mxc_jpeg_buf_queue,
1505 };
1506 
1507 static int mxc_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
1508 			       struct vb2_queue *dst_vq)
1509 {
1510 	struct mxc_jpeg_ctx *ctx = priv;
1511 	int ret;
1512 
1513 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1514 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1515 	src_vq->drv_priv = ctx;
1516 	src_vq->buf_struct_size = sizeof(struct mxc_jpeg_src_buf);
1517 	src_vq->ops = &mxc_jpeg_qops;
1518 	src_vq->mem_ops = &vb2_dma_contig_memops;
1519 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1520 	src_vq->lock = &ctx->mxc_jpeg->lock;
1521 	src_vq->dev = ctx->mxc_jpeg->dev;
1522 
1523 	ret = vb2_queue_init(src_vq);
1524 	if (ret)
1525 		return ret;
1526 
1527 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1528 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1529 	dst_vq->drv_priv = ctx;
1530 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1531 	dst_vq->ops = &mxc_jpeg_qops;
1532 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1533 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1534 	dst_vq->lock = &ctx->mxc_jpeg->lock;
1535 	dst_vq->dev = ctx->mxc_jpeg->dev;
1536 
1537 	ret = vb2_queue_init(dst_vq);
1538 	return ret;
1539 }
1540 
1541 static void mxc_jpeg_set_default_params(struct mxc_jpeg_ctx *ctx)
1542 {
1543 	struct mxc_jpeg_q_data *out_q = &ctx->out_q;
1544 	struct mxc_jpeg_q_data *cap_q = &ctx->cap_q;
1545 	struct mxc_jpeg_q_data *q[2] = {out_q, cap_q};
1546 	int i;
1547 
1548 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) {
1549 		out_q->fmt = mxc_jpeg_find_format(ctx, MXC_JPEG_DEFAULT_PFMT);
1550 		cap_q->fmt = mxc_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG);
1551 	} else {
1552 		out_q->fmt = mxc_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG);
1553 		cap_q->fmt = mxc_jpeg_find_format(ctx, MXC_JPEG_DEFAULT_PFMT);
1554 	}
1555 
1556 	for (i = 0; i < 2; i++) {
1557 		q[i]->w = MXC_JPEG_DEFAULT_WIDTH;
1558 		q[i]->h = MXC_JPEG_DEFAULT_HEIGHT;
1559 		q[i]->w_adjusted = MXC_JPEG_DEFAULT_WIDTH;
1560 		q[i]->h_adjusted = MXC_JPEG_DEFAULT_HEIGHT;
1561 		mxc_jpeg_bytesperline(q[i], q[i]->fmt->precision);
1562 		mxc_jpeg_sizeimage(q[i]);
1563 	}
1564 }
1565 
1566 static int mxc_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
1567 {
1568 	struct mxc_jpeg_ctx *ctx =
1569 		container_of(ctrl->handler, struct mxc_jpeg_ctx, ctrl_handler);
1570 
1571 	switch (ctrl->id) {
1572 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1573 		ctx->jpeg_quality = ctrl->val;
1574 		break;
1575 	default:
1576 		dev_err(ctx->mxc_jpeg->dev, "Invalid control, id = %d, val = %d\n",
1577 			ctrl->id, ctrl->val);
1578 		return -EINVAL;
1579 	}
1580 
1581 	return 0;
1582 }
1583 
1584 static const struct v4l2_ctrl_ops mxc_jpeg_ctrl_ops = {
1585 	.s_ctrl = mxc_jpeg_s_ctrl,
1586 };
1587 
1588 static void mxc_jpeg_encode_ctrls(struct mxc_jpeg_ctx *ctx)
1589 {
1590 	v4l2_ctrl_new_std(&ctx->ctrl_handler, &mxc_jpeg_ctrl_ops,
1591 			  V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75);
1592 }
1593 
1594 static int mxc_jpeg_ctrls_setup(struct mxc_jpeg_ctx *ctx)
1595 {
1596 	int err;
1597 
1598 	v4l2_ctrl_handler_init(&ctx->ctrl_handler, 2);
1599 
1600 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE)
1601 		mxc_jpeg_encode_ctrls(ctx);
1602 
1603 	if (ctx->ctrl_handler.error) {
1604 		err = ctx->ctrl_handler.error;
1605 
1606 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1607 		return err;
1608 	}
1609 
1610 	err = v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1611 	if (err)
1612 		v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1613 	return err;
1614 }
1615 
1616 static int mxc_jpeg_open(struct file *file)
1617 {
1618 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
1619 	struct video_device *mxc_vfd = video_devdata(file);
1620 	struct device *dev = mxc_jpeg->dev;
1621 	struct mxc_jpeg_ctx *ctx;
1622 	int ret = 0;
1623 
1624 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1625 	if (!ctx)
1626 		return -ENOMEM;
1627 
1628 	if (mutex_lock_interruptible(&mxc_jpeg->lock)) {
1629 		ret = -ERESTARTSYS;
1630 		goto free;
1631 	}
1632 
1633 	v4l2_fh_init(&ctx->fh, mxc_vfd);
1634 	file->private_data = &ctx->fh;
1635 	v4l2_fh_add(&ctx->fh);
1636 
1637 	ctx->mxc_jpeg = mxc_jpeg;
1638 
1639 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(mxc_jpeg->m2m_dev, ctx,
1640 					    mxc_jpeg_queue_init);
1641 
1642 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1643 		ret = PTR_ERR(ctx->fh.m2m_ctx);
1644 		goto error;
1645 	}
1646 
1647 	ret = mxc_jpeg_ctrls_setup(ctx);
1648 	if (ret) {
1649 		dev_err(ctx->mxc_jpeg->dev, "failed to setup mxc jpeg controls\n");
1650 		goto err_ctrls_setup;
1651 	}
1652 	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
1653 	mxc_jpeg_set_default_params(ctx);
1654 	ctx->slot = MXC_MAX_SLOTS; /* slot not allocated yet */
1655 
1656 	if (mxc_jpeg->mode == MXC_JPEG_DECODE)
1657 		dev_dbg(dev, "Opened JPEG decoder instance %p\n", ctx);
1658 	else
1659 		dev_dbg(dev, "Opened JPEG encoder instance %p\n", ctx);
1660 	mutex_unlock(&mxc_jpeg->lock);
1661 
1662 	return 0;
1663 
1664 err_ctrls_setup:
1665 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1666 error:
1667 	v4l2_fh_del(&ctx->fh);
1668 	v4l2_fh_exit(&ctx->fh);
1669 	mutex_unlock(&mxc_jpeg->lock);
1670 free:
1671 	kfree(ctx);
1672 	return ret;
1673 }
1674 
1675 static int mxc_jpeg_querycap(struct file *file, void *priv,
1676 			     struct v4l2_capability *cap)
1677 {
1678 	strscpy(cap->driver, MXC_JPEG_NAME " codec", sizeof(cap->driver));
1679 	strscpy(cap->card, MXC_JPEG_NAME " codec", sizeof(cap->card));
1680 	cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
1681 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1682 
1683 	return 0;
1684 }
1685 
1686 static int mxc_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
1687 				     struct v4l2_fmtdesc *f)
1688 {
1689 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1690 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type);
1691 
1692 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) {
1693 		return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f,
1694 			MXC_JPEG_FMT_TYPE_ENC);
1695 	} else if (!ctx->header_parsed) {
1696 		return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f,
1697 			MXC_JPEG_FMT_TYPE_RAW);
1698 	} else {
1699 		/* For the decoder CAPTURE queue, only enumerate the raw formats
1700 		 * supported for the format currently active on OUTPUT
1701 		 * (more precisely what was propagated on capture queue
1702 		 * after jpeg parse on the output buffer)
1703 		 */
1704 		if (f->index)
1705 			return -EINVAL;
1706 		f->pixelformat = q_data->fmt->fourcc;
1707 		return 0;
1708 	}
1709 }
1710 
1711 static int mxc_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
1712 				     struct v4l2_fmtdesc *f)
1713 {
1714 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1715 	u32 type = ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ?  MXC_JPEG_FMT_TYPE_ENC :
1716 							     MXC_JPEG_FMT_TYPE_RAW;
1717 	int ret;
1718 
1719 	ret = enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, type);
1720 	if (ret)
1721 		return ret;
1722 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
1723 		f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION;
1724 	return 0;
1725 }
1726 
1727 static int mxc_jpeg_try_fmt(struct v4l2_format *f, const struct mxc_jpeg_fmt *fmt,
1728 			    struct mxc_jpeg_ctx *ctx, int q_type)
1729 {
1730 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
1731 	struct v4l2_plane_pix_format *pfmt;
1732 	u32 w = (pix_mp->width < MXC_JPEG_MAX_WIDTH) ?
1733 		 pix_mp->width : MXC_JPEG_MAX_WIDTH;
1734 	u32 h = (pix_mp->height < MXC_JPEG_MAX_HEIGHT) ?
1735 		 pix_mp->height : MXC_JPEG_MAX_HEIGHT;
1736 	int i;
1737 	struct mxc_jpeg_q_data tmp_q;
1738 
1739 	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
1740 	pix_mp->field = V4L2_FIELD_NONE;
1741 	pix_mp->num_planes = fmt->colplanes;
1742 	pix_mp->pixelformat = fmt->fourcc;
1743 
1744 	/*
1745 	 * use MXC_JPEG_H_ALIGN instead of fmt->v_align, for vertical
1746 	 * alignment, to loosen up the alignment to multiple of 8,
1747 	 * otherwise NV12-1080p fails as 1080 is not a multiple of 16
1748 	 */
1749 	v4l_bound_align_image(&w,
1750 			      MXC_JPEG_MIN_WIDTH,
1751 			      w, /* adjust downwards*/
1752 			      fmt->h_align,
1753 			      &h,
1754 			      MXC_JPEG_MIN_HEIGHT,
1755 			      h, /* adjust downwards*/
1756 			      MXC_JPEG_H_ALIGN,
1757 			      0);
1758 	pix_mp->width = w; /* negotiate the width */
1759 	pix_mp->height = h; /* negotiate the height */
1760 
1761 	/* get user input into the tmp_q */
1762 	tmp_q.w = w;
1763 	tmp_q.h = h;
1764 	tmp_q.fmt = fmt;
1765 	for (i = 0; i < pix_mp->num_planes; i++) {
1766 		pfmt = &pix_mp->plane_fmt[i];
1767 		tmp_q.bytesperline[i] = pfmt->bytesperline;
1768 		tmp_q.sizeimage[i] = pfmt->sizeimage;
1769 	}
1770 
1771 	/* calculate bytesperline & sizeimage into the tmp_q */
1772 	mxc_jpeg_bytesperline(&tmp_q, fmt->precision);
1773 	mxc_jpeg_sizeimage(&tmp_q);
1774 
1775 	/* adjust user format according to our calculations */
1776 	for (i = 0; i < pix_mp->num_planes; i++) {
1777 		pfmt = &pix_mp->plane_fmt[i];
1778 		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
1779 		pfmt->bytesperline = tmp_q.bytesperline[i];
1780 		pfmt->sizeimage = tmp_q.sizeimage[i];
1781 	}
1782 
1783 	/* fix colorspace information to sRGB for both output & capture */
1784 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
1785 	pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601;
1786 	pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB;
1787 	/*
1788 	 * this hardware does not change the range of the samples
1789 	 * but since inside JPEG the YUV quantization is full-range,
1790 	 * this driver will always use full-range for the raw frames, too
1791 	 */
1792 	pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1793 
1794 	return 0;
1795 }
1796 
1797 static int mxc_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
1798 				    struct v4l2_format *f)
1799 {
1800 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1801 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1802 	struct device *dev = jpeg->dev;
1803 	const struct mxc_jpeg_fmt *fmt;
1804 	u32 fourcc = f->fmt.pix_mp.pixelformat;
1805 
1806 	int q_type = (jpeg->mode == MXC_JPEG_DECODE) ?
1807 		     MXC_JPEG_FMT_TYPE_RAW : MXC_JPEG_FMT_TYPE_ENC;
1808 
1809 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
1810 		dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type);
1811 		return -EINVAL;
1812 	}
1813 
1814 	fmt = mxc_jpeg_find_format(ctx, fourcc);
1815 	if (!fmt || fmt->flags != q_type) {
1816 		dev_warn(dev, "Format not supported: %c%c%c%c, use the default.\n",
1817 			 (fourcc & 0xff),
1818 			 (fourcc >>  8) & 0xff,
1819 			 (fourcc >> 16) & 0xff,
1820 			 (fourcc >> 24) & 0xff);
1821 		f->fmt.pix_mp.pixelformat = (jpeg->mode == MXC_JPEG_DECODE) ?
1822 				MXC_JPEG_DEFAULT_PFMT : V4L2_PIX_FMT_JPEG;
1823 		fmt = mxc_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat);
1824 	}
1825 	return mxc_jpeg_try_fmt(f, fmt, ctx, q_type);
1826 }
1827 
1828 static int mxc_jpeg_try_fmt_vid_out(struct file *file, void *priv,
1829 				    struct v4l2_format *f)
1830 {
1831 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1832 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1833 	struct device *dev = jpeg->dev;
1834 	const struct mxc_jpeg_fmt *fmt;
1835 	u32 fourcc = f->fmt.pix_mp.pixelformat;
1836 
1837 	int q_type = (jpeg->mode == MXC_JPEG_ENCODE) ?
1838 		     MXC_JPEG_FMT_TYPE_RAW : MXC_JPEG_FMT_TYPE_ENC;
1839 
1840 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
1841 		dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type);
1842 		return -EINVAL;
1843 	}
1844 
1845 	fmt = mxc_jpeg_find_format(ctx, fourcc);
1846 	if (!fmt || fmt->flags != q_type) {
1847 		dev_warn(dev, "Format not supported: %c%c%c%c, use the default.\n",
1848 			 (fourcc & 0xff),
1849 			 (fourcc >>  8) & 0xff,
1850 			 (fourcc >> 16) & 0xff,
1851 			 (fourcc >> 24) & 0xff);
1852 		f->fmt.pix_mp.pixelformat = (jpeg->mode == MXC_JPEG_ENCODE) ?
1853 				MXC_JPEG_DEFAULT_PFMT : V4L2_PIX_FMT_JPEG;
1854 		fmt = mxc_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat);
1855 	}
1856 	return mxc_jpeg_try_fmt(f, fmt, ctx, q_type);
1857 }
1858 
1859 static int mxc_jpeg_s_fmt(struct mxc_jpeg_ctx *ctx,
1860 			  struct v4l2_format *f)
1861 {
1862 	struct vb2_queue *vq;
1863 	struct mxc_jpeg_q_data *q_data = NULL;
1864 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
1865 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1866 	int i;
1867 
1868 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
1869 	if (!vq)
1870 		return -EINVAL;
1871 
1872 	q_data = mxc_jpeg_get_q_data(ctx, f->type);
1873 
1874 	if (vb2_is_busy(vq)) {
1875 		v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
1876 		return -EBUSY;
1877 	}
1878 
1879 	q_data->fmt = mxc_jpeg_find_format(ctx, pix_mp->pixelformat);
1880 	q_data->w = pix_mp->width;
1881 	q_data->h = pix_mp->height;
1882 
1883 	q_data->w_adjusted = q_data->w;
1884 	q_data->h_adjusted = q_data->h;
1885 	if (jpeg->mode == MXC_JPEG_DECODE) {
1886 		/*
1887 		 * align up the resolution for CAST IP,
1888 		 * but leave the buffer resolution unchanged
1889 		 */
1890 		v4l_bound_align_image(&q_data->w_adjusted,
1891 				      q_data->w_adjusted,  /* adjust upwards */
1892 				      MXC_JPEG_MAX_WIDTH,
1893 				      q_data->fmt->h_align,
1894 				      &q_data->h_adjusted,
1895 				      q_data->h_adjusted, /* adjust upwards */
1896 				      MXC_JPEG_MAX_HEIGHT,
1897 				      q_data->fmt->v_align,
1898 				      0);
1899 	} else {
1900 		/*
1901 		 * align down the resolution for CAST IP,
1902 		 * but leave the buffer resolution unchanged
1903 		 */
1904 		v4l_bound_align_image(&q_data->w_adjusted,
1905 				      MXC_JPEG_MIN_WIDTH,
1906 				      q_data->w_adjusted, /* adjust downwards*/
1907 				      q_data->fmt->h_align,
1908 				      &q_data->h_adjusted,
1909 				      MXC_JPEG_MIN_HEIGHT,
1910 				      q_data->h_adjusted, /* adjust downwards*/
1911 				      q_data->fmt->v_align,
1912 				      0);
1913 	}
1914 
1915 	for (i = 0; i < pix_mp->num_planes; i++) {
1916 		q_data->bytesperline[i] = pix_mp->plane_fmt[i].bytesperline;
1917 		q_data->sizeimage[i] = pix_mp->plane_fmt[i].sizeimage;
1918 	}
1919 
1920 	return 0;
1921 }
1922 
1923 static int mxc_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
1924 				  struct v4l2_format *f)
1925 {
1926 	int ret;
1927 
1928 	ret = mxc_jpeg_try_fmt_vid_cap(file, priv, f);
1929 	if (ret)
1930 		return ret;
1931 
1932 	return mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f);
1933 }
1934 
1935 static int mxc_jpeg_s_fmt_vid_out(struct file *file, void *priv,
1936 				  struct v4l2_format *f)
1937 {
1938 	int ret;
1939 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1940 	struct vb2_queue *dst_vq;
1941 	struct mxc_jpeg_q_data *q_data_cap;
1942 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1943 	struct v4l2_format fc;
1944 
1945 	ret = mxc_jpeg_try_fmt_vid_out(file, priv, f);
1946 	if (ret)
1947 		return ret;
1948 
1949 	ret = mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f);
1950 	if (ret)
1951 		return ret;
1952 
1953 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
1954 		return 0;
1955 
1956 	dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, cap_type);
1957 	if (!dst_vq)
1958 		return -EINVAL;
1959 
1960 	if (vb2_is_busy(dst_vq))
1961 		return 0;
1962 
1963 	q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type);
1964 	if (q_data_cap->w == f->fmt.pix_mp.width && q_data_cap->h == f->fmt.pix_mp.height)
1965 		return 0;
1966 	memset(&fc, 0, sizeof(fc));
1967 	fc.type = cap_type;
1968 	fc.fmt.pix_mp.pixelformat = q_data_cap->fmt->fourcc;
1969 	fc.fmt.pix_mp.width = f->fmt.pix_mp.width;
1970 	fc.fmt.pix_mp.height = f->fmt.pix_mp.height;
1971 
1972 	return mxc_jpeg_s_fmt_vid_cap(file, priv, &fc);
1973 }
1974 
1975 static int mxc_jpeg_g_fmt_vid(struct file *file, void *priv,
1976 			      struct v4l2_format *f)
1977 {
1978 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1979 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1980 	struct device *dev = jpeg->dev;
1981 	struct v4l2_pix_format_mplane   *pix_mp = &f->fmt.pix_mp;
1982 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type);
1983 	int i;
1984 
1985 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
1986 		dev_err(dev, "G_FMT with Invalid type: %d\n", f->type);
1987 		return -EINVAL;
1988 	}
1989 
1990 	pix_mp->pixelformat = q_data->fmt->fourcc;
1991 	pix_mp->width = q_data->w;
1992 	pix_mp->height = q_data->h;
1993 	pix_mp->field = V4L2_FIELD_NONE;
1994 
1995 	/* fix colorspace information to sRGB for both output & capture */
1996 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
1997 	pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601;
1998 	pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB;
1999 	pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2000 
2001 	pix_mp->num_planes = q_data->fmt->colplanes;
2002 	for (i = 0; i < pix_mp->num_planes; i++) {
2003 		pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i];
2004 		pix_mp->plane_fmt[i].sizeimage = q_data->sizeimage[i];
2005 	}
2006 
2007 	return 0;
2008 }
2009 
2010 static int mxc_jpeg_subscribe_event(struct v4l2_fh *fh,
2011 				    const struct v4l2_event_subscription *sub)
2012 {
2013 	switch (sub->type) {
2014 	case V4L2_EVENT_EOS:
2015 		return v4l2_event_subscribe(fh, sub, 0, NULL);
2016 	case V4L2_EVENT_SOURCE_CHANGE:
2017 		return v4l2_src_change_event_subscribe(fh, sub);
2018 	case V4L2_EVENT_CTRL:
2019 		return v4l2_ctrl_subscribe_event(fh, sub);
2020 	default:
2021 		return -EINVAL;
2022 	}
2023 }
2024 
2025 static int mxc_jpeg_dqbuf(struct file *file, void *priv,
2026 			  struct v4l2_buffer *buf)
2027 {
2028 	struct v4l2_fh *fh = file->private_data;
2029 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
2030 	struct device *dev = ctx->mxc_jpeg->dev;
2031 	int num_src_ready = v4l2_m2m_num_src_bufs_ready(fh->m2m_ctx);
2032 	int ret;
2033 
2034 	dev_dbg(dev, "DQBUF type=%d, index=%d", buf->type, buf->index);
2035 	if (ctx->stopping == 1 && num_src_ready == 0) {
2036 		/* No more src bufs, notify app EOS */
2037 		notify_eos(ctx);
2038 		ctx->stopping = 0;
2039 		mxc_jpeg_set_last_buffer_dequeued(ctx);
2040 	}
2041 
2042 	ret = v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf);
2043 	return ret;
2044 }
2045 
2046 static const struct v4l2_ioctl_ops mxc_jpeg_ioctl_ops = {
2047 	.vidioc_querycap		= mxc_jpeg_querycap,
2048 	.vidioc_enum_fmt_vid_cap	= mxc_jpeg_enum_fmt_vid_cap,
2049 	.vidioc_enum_fmt_vid_out	= mxc_jpeg_enum_fmt_vid_out,
2050 
2051 	.vidioc_try_fmt_vid_cap_mplane	= mxc_jpeg_try_fmt_vid_cap,
2052 	.vidioc_try_fmt_vid_out_mplane	= mxc_jpeg_try_fmt_vid_out,
2053 
2054 	.vidioc_s_fmt_vid_cap_mplane	= mxc_jpeg_s_fmt_vid_cap,
2055 	.vidioc_s_fmt_vid_out_mplane	= mxc_jpeg_s_fmt_vid_out,
2056 
2057 	.vidioc_g_fmt_vid_cap_mplane	= mxc_jpeg_g_fmt_vid,
2058 	.vidioc_g_fmt_vid_out_mplane	= mxc_jpeg_g_fmt_vid,
2059 
2060 	.vidioc_subscribe_event		= mxc_jpeg_subscribe_event,
2061 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
2062 
2063 	.vidioc_try_decoder_cmd		= v4l2_m2m_ioctl_try_decoder_cmd,
2064 	.vidioc_decoder_cmd		= mxc_jpeg_decoder_cmd,
2065 	.vidioc_try_encoder_cmd		= v4l2_m2m_ioctl_try_encoder_cmd,
2066 	.vidioc_encoder_cmd		= mxc_jpeg_encoder_cmd,
2067 
2068 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
2069 	.vidioc_dqbuf			= mxc_jpeg_dqbuf,
2070 
2071 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
2072 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
2073 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
2074 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
2075 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
2076 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
2077 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
2078 };
2079 
2080 static int mxc_jpeg_release(struct file *file)
2081 {
2082 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
2083 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(file->private_data);
2084 	struct device *dev = mxc_jpeg->dev;
2085 
2086 	mutex_lock(&mxc_jpeg->lock);
2087 	if (mxc_jpeg->mode == MXC_JPEG_DECODE)
2088 		dev_dbg(dev, "Release JPEG decoder instance on slot %d.",
2089 			ctx->slot);
2090 	else
2091 		dev_dbg(dev, "Release JPEG encoder instance on slot %d.",
2092 			ctx->slot);
2093 	v4l2_ctrl_handler_free(&ctx->ctrl_handler);
2094 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2095 	v4l2_fh_del(&ctx->fh);
2096 	v4l2_fh_exit(&ctx->fh);
2097 	kfree(ctx);
2098 	mutex_unlock(&mxc_jpeg->lock);
2099 
2100 	return 0;
2101 }
2102 
2103 static const struct v4l2_file_operations mxc_jpeg_fops = {
2104 	.owner		= THIS_MODULE,
2105 	.open		= mxc_jpeg_open,
2106 	.release	= mxc_jpeg_release,
2107 	.poll		= v4l2_m2m_fop_poll,
2108 	.unlocked_ioctl	= video_ioctl2,
2109 	.mmap		= v4l2_m2m_fop_mmap,
2110 };
2111 
2112 static const struct v4l2_m2m_ops mxc_jpeg_m2m_ops = {
2113 	.job_ready      = mxc_jpeg_job_ready,
2114 	.device_run	= mxc_jpeg_device_run,
2115 };
2116 
2117 static void mxc_jpeg_detach_pm_domains(struct mxc_jpeg_dev *jpeg)
2118 {
2119 	int i;
2120 
2121 	for (i = 0; i < jpeg->num_domains; i++) {
2122 		if (jpeg->pd_link[i] && !IS_ERR(jpeg->pd_link[i]))
2123 			device_link_del(jpeg->pd_link[i]);
2124 		if (jpeg->pd_dev[i] && !IS_ERR(jpeg->pd_dev[i]))
2125 			dev_pm_domain_detach(jpeg->pd_dev[i], true);
2126 		jpeg->pd_dev[i] = NULL;
2127 		jpeg->pd_link[i] = NULL;
2128 	}
2129 }
2130 
2131 static int mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev *jpeg)
2132 {
2133 	struct device *dev = jpeg->dev;
2134 	struct device_node *np = jpeg->pdev->dev.of_node;
2135 	int i;
2136 	int ret;
2137 
2138 	jpeg->num_domains = of_count_phandle_with_args(np, "power-domains",
2139 						       "#power-domain-cells");
2140 	if (jpeg->num_domains < 0) {
2141 		dev_err(dev, "No power domains defined for jpeg node\n");
2142 		return jpeg->num_domains;
2143 	}
2144 
2145 	jpeg->pd_dev = devm_kmalloc_array(dev, jpeg->num_domains,
2146 					  sizeof(*jpeg->pd_dev), GFP_KERNEL);
2147 	if (!jpeg->pd_dev)
2148 		return -ENOMEM;
2149 
2150 	jpeg->pd_link = devm_kmalloc_array(dev, jpeg->num_domains,
2151 					   sizeof(*jpeg->pd_link), GFP_KERNEL);
2152 	if (!jpeg->pd_link)
2153 		return -ENOMEM;
2154 
2155 	for (i = 0; i < jpeg->num_domains; i++) {
2156 		jpeg->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i);
2157 		if (IS_ERR(jpeg->pd_dev[i])) {
2158 			ret = PTR_ERR(jpeg->pd_dev[i]);
2159 			goto fail;
2160 		}
2161 
2162 		jpeg->pd_link[i] = device_link_add(dev, jpeg->pd_dev[i],
2163 						   DL_FLAG_STATELESS |
2164 						   DL_FLAG_PM_RUNTIME);
2165 		if (!jpeg->pd_link[i]) {
2166 			ret = -EINVAL;
2167 			goto fail;
2168 		}
2169 	}
2170 
2171 	return 0;
2172 fail:
2173 	mxc_jpeg_detach_pm_domains(jpeg);
2174 	return ret;
2175 }
2176 
2177 static int mxc_jpeg_probe(struct platform_device *pdev)
2178 {
2179 	struct mxc_jpeg_dev *jpeg;
2180 	struct device *dev = &pdev->dev;
2181 	int dec_irq;
2182 	int ret;
2183 	int mode;
2184 	const struct of_device_id *of_id;
2185 	unsigned int slot;
2186 
2187 	of_id = of_match_node(mxc_jpeg_match, dev->of_node);
2188 	mode = *(const int *)of_id->data;
2189 
2190 	jpeg = devm_kzalloc(dev, sizeof(struct mxc_jpeg_dev), GFP_KERNEL);
2191 	if (!jpeg)
2192 		return -ENOMEM;
2193 
2194 	mutex_init(&jpeg->lock);
2195 	spin_lock_init(&jpeg->hw_lock);
2196 
2197 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2198 	if (ret) {
2199 		dev_err(&pdev->dev, "No suitable DMA available.\n");
2200 		goto err_irq;
2201 	}
2202 
2203 	jpeg->base_reg = devm_platform_ioremap_resource(pdev, 0);
2204 	if (IS_ERR(jpeg->base_reg))
2205 		return PTR_ERR(jpeg->base_reg);
2206 
2207 	for (slot = 0; slot < MXC_MAX_SLOTS; slot++) {
2208 		dec_irq = platform_get_irq(pdev, slot);
2209 		if (dec_irq < 0) {
2210 			ret = dec_irq;
2211 			goto err_irq;
2212 		}
2213 		ret = devm_request_irq(&pdev->dev, dec_irq, mxc_jpeg_dec_irq,
2214 				       0, pdev->name, jpeg);
2215 		if (ret) {
2216 			dev_err(&pdev->dev, "Failed to request irq %d (%d)\n",
2217 				dec_irq, ret);
2218 			goto err_irq;
2219 		}
2220 	}
2221 
2222 	jpeg->pdev = pdev;
2223 	jpeg->dev = dev;
2224 	jpeg->mode = mode;
2225 
2226 	/* Get clocks */
2227 	jpeg->clk_ipg = devm_clk_get(dev, "ipg");
2228 	if (IS_ERR(jpeg->clk_ipg)) {
2229 		dev_err(dev, "failed to get clock: ipg\n");
2230 		goto err_clk;
2231 	}
2232 
2233 	jpeg->clk_per = devm_clk_get(dev, "per");
2234 	if (IS_ERR(jpeg->clk_per)) {
2235 		dev_err(dev, "failed to get clock: per\n");
2236 		goto err_clk;
2237 	}
2238 
2239 	ret = mxc_jpeg_attach_pm_domains(jpeg);
2240 	if (ret < 0) {
2241 		dev_err(dev, "failed to attach power domains %d\n", ret);
2242 		return ret;
2243 	}
2244 
2245 	/* v4l2 */
2246 	ret = v4l2_device_register(dev, &jpeg->v4l2_dev);
2247 	if (ret) {
2248 		dev_err(dev, "failed to register v4l2 device\n");
2249 		goto err_register;
2250 	}
2251 	jpeg->m2m_dev = v4l2_m2m_init(&mxc_jpeg_m2m_ops);
2252 	if (IS_ERR(jpeg->m2m_dev)) {
2253 		dev_err(dev, "failed to register v4l2 device\n");
2254 		ret = PTR_ERR(jpeg->m2m_dev);
2255 		goto err_m2m;
2256 	}
2257 
2258 	jpeg->dec_vdev = video_device_alloc();
2259 	if (!jpeg->dec_vdev) {
2260 		dev_err(dev, "failed to register v4l2 device\n");
2261 		ret = -ENOMEM;
2262 		goto err_vdev_alloc;
2263 	}
2264 	if (mode == MXC_JPEG_ENCODE)
2265 		snprintf(jpeg->dec_vdev->name,
2266 			 sizeof(jpeg->dec_vdev->name),
2267 			 "%s-enc", MXC_JPEG_NAME);
2268 	else
2269 		snprintf(jpeg->dec_vdev->name,
2270 			 sizeof(jpeg->dec_vdev->name),
2271 			 "%s-dec", MXC_JPEG_NAME);
2272 
2273 	jpeg->dec_vdev->fops = &mxc_jpeg_fops;
2274 	jpeg->dec_vdev->ioctl_ops = &mxc_jpeg_ioctl_ops;
2275 	jpeg->dec_vdev->minor = -1;
2276 	jpeg->dec_vdev->release = video_device_release;
2277 	jpeg->dec_vdev->lock = &jpeg->lock; /* lock for ioctl serialization */
2278 	jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
2279 	jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
2280 	jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
2281 					V4L2_CAP_VIDEO_M2M_MPLANE;
2282 	if (mode == MXC_JPEG_ENCODE) {
2283 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_DECODER_CMD);
2284 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_DECODER_CMD);
2285 	} else {
2286 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_ENCODER_CMD);
2287 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_ENCODER_CMD);
2288 	}
2289 	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_VIDEO, -1);
2290 	if (ret) {
2291 		dev_err(dev, "failed to register video device\n");
2292 		goto err_vdev_register;
2293 	}
2294 	video_set_drvdata(jpeg->dec_vdev, jpeg);
2295 	if (mode == MXC_JPEG_ENCODE)
2296 		v4l2_info(&jpeg->v4l2_dev,
2297 			  "encoder device registered as /dev/video%d (%d,%d)\n",
2298 			  jpeg->dec_vdev->num, VIDEO_MAJOR,
2299 			  jpeg->dec_vdev->minor);
2300 	else
2301 		v4l2_info(&jpeg->v4l2_dev,
2302 			  "decoder device registered as /dev/video%d (%d,%d)\n",
2303 			  jpeg->dec_vdev->num, VIDEO_MAJOR,
2304 			  jpeg->dec_vdev->minor);
2305 
2306 	platform_set_drvdata(pdev, jpeg);
2307 	pm_runtime_enable(dev);
2308 
2309 	return 0;
2310 
2311 err_vdev_register:
2312 	video_device_release(jpeg->dec_vdev);
2313 
2314 err_vdev_alloc:
2315 	v4l2_m2m_release(jpeg->m2m_dev);
2316 
2317 err_m2m:
2318 	v4l2_device_unregister(&jpeg->v4l2_dev);
2319 
2320 err_register:
2321 	mxc_jpeg_detach_pm_domains(jpeg);
2322 
2323 err_irq:
2324 err_clk:
2325 	return ret;
2326 }
2327 
2328 #ifdef CONFIG_PM
2329 static int mxc_jpeg_runtime_resume(struct device *dev)
2330 {
2331 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2332 	int ret;
2333 
2334 	ret = clk_prepare_enable(jpeg->clk_ipg);
2335 	if (ret < 0) {
2336 		dev_err(dev, "failed to enable clock: ipg\n");
2337 		goto err_ipg;
2338 	}
2339 
2340 	ret = clk_prepare_enable(jpeg->clk_per);
2341 	if (ret < 0) {
2342 		dev_err(dev, "failed to enable clock: per\n");
2343 		goto err_per;
2344 	}
2345 
2346 	return 0;
2347 
2348 err_per:
2349 	clk_disable_unprepare(jpeg->clk_ipg);
2350 err_ipg:
2351 	return ret;
2352 }
2353 
2354 static int mxc_jpeg_runtime_suspend(struct device *dev)
2355 {
2356 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2357 
2358 	clk_disable_unprepare(jpeg->clk_ipg);
2359 	clk_disable_unprepare(jpeg->clk_per);
2360 
2361 	return 0;
2362 }
2363 #endif
2364 
2365 #ifdef CONFIG_PM_SLEEP
2366 static int mxc_jpeg_suspend(struct device *dev)
2367 {
2368 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2369 
2370 	v4l2_m2m_suspend(jpeg->m2m_dev);
2371 	return pm_runtime_force_suspend(dev);
2372 }
2373 
2374 static int mxc_jpeg_resume(struct device *dev)
2375 {
2376 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2377 	int ret;
2378 
2379 	ret = pm_runtime_force_resume(dev);
2380 	if (ret < 0)
2381 		return ret;
2382 
2383 	v4l2_m2m_resume(jpeg->m2m_dev);
2384 	return ret;
2385 }
2386 #endif
2387 
2388 static const struct dev_pm_ops	mxc_jpeg_pm_ops = {
2389 	SET_RUNTIME_PM_OPS(mxc_jpeg_runtime_suspend,
2390 			   mxc_jpeg_runtime_resume, NULL)
2391 	SET_SYSTEM_SLEEP_PM_OPS(mxc_jpeg_suspend, mxc_jpeg_resume)
2392 };
2393 
2394 static int mxc_jpeg_remove(struct platform_device *pdev)
2395 {
2396 	unsigned int slot;
2397 	struct mxc_jpeg_dev *jpeg = platform_get_drvdata(pdev);
2398 
2399 	for (slot = 0; slot < MXC_MAX_SLOTS; slot++)
2400 		mxc_jpeg_free_slot_data(jpeg, slot);
2401 
2402 	pm_runtime_disable(&pdev->dev);
2403 	video_unregister_device(jpeg->dec_vdev);
2404 	v4l2_m2m_release(jpeg->m2m_dev);
2405 	v4l2_device_unregister(&jpeg->v4l2_dev);
2406 	mxc_jpeg_detach_pm_domains(jpeg);
2407 
2408 	return 0;
2409 }
2410 
2411 MODULE_DEVICE_TABLE(of, mxc_jpeg_match);
2412 
2413 static struct platform_driver mxc_jpeg_driver = {
2414 	.probe = mxc_jpeg_probe,
2415 	.remove = mxc_jpeg_remove,
2416 	.driver = {
2417 		.name = "mxc-jpeg",
2418 		.of_match_table = mxc_jpeg_match,
2419 		.pm = &mxc_jpeg_pm_ops,
2420 	},
2421 };
2422 module_platform_driver(mxc_jpeg_driver);
2423 
2424 MODULE_AUTHOR("Zhengyu Shen <zhengyu.shen_1@nxp.com>");
2425 MODULE_AUTHOR("Mirela Rabulea <mirela.rabulea@nxp.com>");
2426 MODULE_DESCRIPTION("V4L2 driver for i.MX8 QXP/QM JPEG encoder/decoder");
2427 MODULE_LICENSE("GPL v2");
2428