• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, RGB, ARGB, 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/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		= "RGB", /*RGBRGB packed format*/
77 		.fourcc		= V4L2_PIX_FMT_RGB24,
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		= "ARGB", /* ARGBARGB packed format */
89 		.fourcc		= V4L2_PIX_FMT_ARGB32,
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_eoi[] = {
293 	0xFF, 0xD9
294 };
295 
296 struct mxc_jpeg_src_buf {
297 	/* common v4l buffer stuff -- must be first */
298 	struct vb2_v4l2_buffer	b;
299 	struct list_head	list;
300 
301 	/* mxc-jpeg specific */
302 	bool			dht_needed;
303 	bool			jpeg_parse_error;
304 	const struct mxc_jpeg_fmt	*fmt;
305 	int			w;
306 	int			h;
307 };
308 
vb2_to_mxc_buf(struct vb2_buffer * vb)309 static inline struct mxc_jpeg_src_buf *vb2_to_mxc_buf(struct vb2_buffer *vb)
310 {
311 	return container_of(to_vb2_v4l2_buffer(vb),
312 			    struct mxc_jpeg_src_buf, b);
313 }
314 
315 static unsigned int debug;
316 module_param(debug, int, 0644);
317 MODULE_PARM_DESC(debug, "Debug level (0-3)");
318 
319 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision);
320 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q);
321 
_bswap16(u16 * a)322 static void _bswap16(u16 *a)
323 {
324 	*a = ((*a & 0x00FF) << 8) | ((*a & 0xFF00) >> 8);
325 }
326 
print_mxc_buf(struct mxc_jpeg_dev * jpeg,struct vb2_buffer * buf,unsigned long len)327 static void print_mxc_buf(struct mxc_jpeg_dev *jpeg, struct vb2_buffer *buf,
328 			  unsigned long len)
329 {
330 	unsigned int plane_no;
331 	u32 dma_addr;
332 	void *vaddr;
333 	unsigned long payload;
334 
335 	if (debug < 3)
336 		return;
337 
338 	for (plane_no = 0; plane_no < buf->num_planes; plane_no++) {
339 		payload = vb2_get_plane_payload(buf, plane_no);
340 		if (len == 0)
341 			len = payload;
342 		dma_addr = vb2_dma_contig_plane_dma_addr(buf, plane_no);
343 		vaddr = vb2_plane_vaddr(buf, plane_no);
344 		v4l2_dbg(3, debug, &jpeg->v4l2_dev,
345 			 "plane %d (vaddr=%p dma_addr=%x payload=%ld):",
346 			  plane_no, vaddr, dma_addr, payload);
347 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
348 			       vaddr, len, false);
349 	}
350 }
351 
mxc_jpeg_fh_to_ctx(struct v4l2_fh * fh)352 static inline struct mxc_jpeg_ctx *mxc_jpeg_fh_to_ctx(struct v4l2_fh *fh)
353 {
354 	return container_of(fh, struct mxc_jpeg_ctx, fh);
355 }
356 
enum_fmt(const struct mxc_jpeg_fmt * mxc_formats,int n,struct v4l2_fmtdesc * f,u32 type)357 static int enum_fmt(const struct mxc_jpeg_fmt *mxc_formats, int n,
358 		    struct v4l2_fmtdesc *f, u32 type)
359 {
360 	int i, num = 0;
361 
362 	for (i = 0; i < n; ++i) {
363 		if (mxc_formats[i].flags == type) {
364 			/* index-th format of searched type found ? */
365 			if (num == f->index)
366 				break;
367 			/* Correct type but haven't reached our index yet,
368 			 * just increment per-type index
369 			 */
370 			++num;
371 		}
372 	}
373 
374 	/* Format not found */
375 	if (i >= n)
376 		return -EINVAL;
377 
378 	strscpy(f->description, mxc_formats[i].name, sizeof(f->description));
379 	f->pixelformat = mxc_formats[i].fourcc;
380 
381 	return 0;
382 }
383 
mxc_jpeg_find_format(struct mxc_jpeg_ctx * ctx,u32 pixelformat)384 static const struct mxc_jpeg_fmt *mxc_jpeg_find_format(struct mxc_jpeg_ctx *ctx,
385 						       u32 pixelformat)
386 {
387 	unsigned int k;
388 
389 	for (k = 0; k < MXC_JPEG_NUM_FORMATS; k++) {
390 		const struct mxc_jpeg_fmt *fmt = &mxc_formats[k];
391 
392 		if (fmt->fourcc == pixelformat)
393 			return fmt;
394 	}
395 	return NULL;
396 }
397 
mxc_jpeg_fourcc_to_imgfmt(u32 fourcc)398 static enum mxc_jpeg_image_format mxc_jpeg_fourcc_to_imgfmt(u32 fourcc)
399 {
400 	switch (fourcc) {
401 	case V4L2_PIX_FMT_GREY:
402 		return MXC_JPEG_GRAY;
403 	case V4L2_PIX_FMT_YUYV:
404 		return MXC_JPEG_YUV422;
405 	case V4L2_PIX_FMT_NV12M:
406 		return MXC_JPEG_YUV420;
407 	case V4L2_PIX_FMT_YUV24:
408 		return MXC_JPEG_YUV444;
409 	case V4L2_PIX_FMT_RGB24:
410 		return MXC_JPEG_RGB;
411 	case V4L2_PIX_FMT_ARGB32:
412 		return MXC_JPEG_ARGB;
413 	default:
414 		return MXC_JPEG_INVALID;
415 	}
416 }
417 
mxc_jpeg_get_q_data(struct mxc_jpeg_ctx * ctx,enum v4l2_buf_type type)418 static struct mxc_jpeg_q_data *mxc_jpeg_get_q_data(struct mxc_jpeg_ctx *ctx,
419 						   enum v4l2_buf_type type)
420 {
421 	if (V4L2_TYPE_IS_OUTPUT(type))
422 		return &ctx->out_q;
423 	return &ctx->cap_q;
424 }
425 
mxc_jpeg_addrs(struct mxc_jpeg_desc * desc,struct vb2_buffer * raw_buf,struct vb2_buffer * jpeg_buf,int offset)426 static void mxc_jpeg_addrs(struct mxc_jpeg_desc *desc,
427 			   struct vb2_buffer *raw_buf,
428 			   struct vb2_buffer *jpeg_buf, int offset)
429 {
430 	int img_fmt = desc->stm_ctrl & STM_CTRL_IMAGE_FORMAT_MASK;
431 
432 	desc->buf_base0 = vb2_dma_contig_plane_dma_addr(raw_buf, 0);
433 	desc->buf_base1 = 0;
434 	if (img_fmt == STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV420)) {
435 		WARN_ON(raw_buf->num_planes < 2);
436 		desc->buf_base1 = vb2_dma_contig_plane_dma_addr(raw_buf, 1);
437 	}
438 	desc->stm_bufbase = vb2_dma_contig_plane_dma_addr(jpeg_buf, 0) +
439 		offset;
440 }
441 
notify_eos(struct mxc_jpeg_ctx * ctx)442 static void notify_eos(struct mxc_jpeg_ctx *ctx)
443 {
444 	const struct v4l2_event ev = {
445 		.type = V4L2_EVENT_EOS
446 	};
447 
448 	dev_dbg(ctx->mxc_jpeg->dev, "Notify app event EOS reached");
449 	v4l2_event_queue_fh(&ctx->fh, &ev);
450 }
451 
notify_src_chg(struct mxc_jpeg_ctx * ctx)452 static void notify_src_chg(struct mxc_jpeg_ctx *ctx)
453 {
454 	const struct v4l2_event ev = {
455 			.type = V4L2_EVENT_SOURCE_CHANGE,
456 			.u.src_change.changes =
457 			V4L2_EVENT_SRC_CH_RESOLUTION,
458 		};
459 
460 	dev_dbg(ctx->mxc_jpeg->dev, "Notify app event SRC_CH_RESOLUTION");
461 	v4l2_event_queue_fh(&ctx->fh, &ev);
462 }
463 
mxc_get_free_slot(struct mxc_jpeg_slot_data slot_data[],int n)464 static int mxc_get_free_slot(struct mxc_jpeg_slot_data slot_data[], int n)
465 {
466 	int free_slot = 0;
467 
468 	while (slot_data[free_slot].used && free_slot < n)
469 		free_slot++;
470 
471 	return free_slot; /* >=n when there are no more free slots */
472 }
473 
mxc_jpeg_alloc_slot_data(struct mxc_jpeg_dev * jpeg,unsigned int slot)474 static bool mxc_jpeg_alloc_slot_data(struct mxc_jpeg_dev *jpeg,
475 				     unsigned int slot)
476 {
477 	struct mxc_jpeg_desc *desc;
478 	struct mxc_jpeg_desc *cfg_desc;
479 	void *cfg_stm;
480 
481 	if (jpeg->slot_data[slot].desc)
482 		goto skip_alloc; /* already allocated, reuse it */
483 
484 	/* allocate descriptor for decoding/encoding phase */
485 	desc = dma_alloc_coherent(jpeg->dev,
486 				  sizeof(struct mxc_jpeg_desc),
487 				  &jpeg->slot_data[slot].desc_handle,
488 				  GFP_ATOMIC);
489 	if (!desc)
490 		goto err;
491 	jpeg->slot_data[slot].desc = desc;
492 
493 	/* allocate descriptor for configuration phase (encoder only) */
494 	cfg_desc = dma_alloc_coherent(jpeg->dev,
495 				      sizeof(struct mxc_jpeg_desc),
496 				      &jpeg->slot_data[slot].cfg_desc_handle,
497 				      GFP_ATOMIC);
498 	if (!cfg_desc)
499 		goto err;
500 	jpeg->slot_data[slot].cfg_desc = cfg_desc;
501 
502 	/* allocate configuration stream */
503 	cfg_stm = dma_alloc_coherent(jpeg->dev,
504 				     MXC_JPEG_MAX_CFG_STREAM,
505 				     &jpeg->slot_data[slot].cfg_stream_handle,
506 				     GFP_ATOMIC);
507 	if (!cfg_stm)
508 		goto err;
509 	memset(cfg_stm, 0, MXC_JPEG_MAX_CFG_STREAM);
510 	jpeg->slot_data[slot].cfg_stream_vaddr = cfg_stm;
511 
512 skip_alloc:
513 	jpeg->slot_data[slot].used = true;
514 
515 	return true;
516 err:
517 	dev_err(jpeg->dev, "Could not allocate descriptors for slot %d", slot);
518 
519 	return false;
520 }
521 
mxc_jpeg_free_slot_data(struct mxc_jpeg_dev * jpeg,unsigned int slot)522 static void mxc_jpeg_free_slot_data(struct mxc_jpeg_dev *jpeg,
523 				    unsigned int slot)
524 {
525 	if (slot >= MXC_MAX_SLOTS) {
526 		dev_err(jpeg->dev, "Invalid slot %d, nothing to free.", slot);
527 		return;
528 	}
529 
530 	/* free descriptor for decoding/encoding phase */
531 	dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc),
532 			  jpeg->slot_data[slot].desc,
533 			  jpeg->slot_data[slot].desc_handle);
534 
535 	/* free descriptor for encoder configuration phase / decoder DHT */
536 	dma_free_coherent(jpeg->dev, sizeof(struct mxc_jpeg_desc),
537 			  jpeg->slot_data[slot].cfg_desc,
538 			  jpeg->slot_data[slot].cfg_desc_handle);
539 
540 	/* free configuration stream */
541 	dma_free_coherent(jpeg->dev, MXC_JPEG_MAX_CFG_STREAM,
542 			  jpeg->slot_data[slot].cfg_stream_vaddr,
543 			  jpeg->slot_data[slot].cfg_stream_handle);
544 
545 	jpeg->slot_data[slot].used = false;
546 }
547 
mxc_jpeg_check_and_set_last_buffer(struct mxc_jpeg_ctx * ctx,struct vb2_v4l2_buffer * src_buf,struct vb2_v4l2_buffer * dst_buf)548 static void mxc_jpeg_check_and_set_last_buffer(struct mxc_jpeg_ctx *ctx,
549 					       struct vb2_v4l2_buffer *src_buf,
550 					       struct vb2_v4l2_buffer *dst_buf)
551 {
552 	if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) {
553 		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
554 		v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx);
555 		notify_eos(ctx);
556 		ctx->header_parsed = false;
557 	}
558 }
559 
mxc_jpeg_dec_irq(int irq,void * priv)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 (WARN_ON(!ctx))
583 		goto job_unlock;
584 
585 	if (slot != ctx->slot) {
586 		/* TODO investigate when adding multi-instance support */
587 		dev_warn(dev, "IRQ slot %d != context slot %d.\n",
588 			 slot, ctx->slot);
589 		goto job_unlock;
590 	}
591 
592 	dec_ret = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS));
593 	writel(dec_ret, reg + MXC_SLOT_OFFSET(slot, SLOT_STATUS)); /* w1c */
594 
595 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
596 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
597 	if (!dst_buf || !src_buf) {
598 		dev_err(dev, "No source or destination buffer.\n");
599 		goto job_unlock;
600 	}
601 	jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf);
602 
603 	if (dec_ret & SLOT_STATUS_ENC_CONFIG_ERR) {
604 		u32 ret = readl(reg + CAST_STATUS12);
605 
606 		dev_err(dev, "Encoder/decoder error, status=0x%08x", ret);
607 		mxc_jpeg_sw_reset(reg);
608 		buf_state = VB2_BUF_STATE_ERROR;
609 		goto buffers_done;
610 	}
611 
612 	if (!(dec_ret & SLOT_STATUS_FRMDONE))
613 		goto job_unlock;
614 
615 	if (jpeg->mode == MXC_JPEG_ENCODE &&
616 	    ctx->enc_state == MXC_JPEG_ENC_CONF) {
617 		ctx->enc_state = MXC_JPEG_ENCODING;
618 		dev_dbg(dev, "Encoder config finished. Start encoding...\n");
619 		mxc_jpeg_enc_mode_go(dev, reg);
620 		goto job_unlock;
621 	}
622 	if (jpeg->mode == MXC_JPEG_DECODE && jpeg_src_buf->dht_needed) {
623 		jpeg_src_buf->dht_needed = false;
624 		dev_dbg(dev, "Decoder DHT cfg finished. Start decoding...\n");
625 		goto job_unlock;
626 	}
627 
628 	if (jpeg->mode == MXC_JPEG_ENCODE) {
629 		payload = readl(reg + MXC_SLOT_OFFSET(slot, SLOT_BUF_PTR));
630 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
631 		dev_dbg(dev, "Encoding finished, payload size: %ld\n",
632 			payload);
633 	} else {
634 		q_data = mxc_jpeg_get_q_data(ctx, cap_type);
635 		payload = q_data->sizeimage[0];
636 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, payload);
637 		vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
638 		if (q_data->fmt->colplanes == 2) {
639 			payload = q_data->sizeimage[1];
640 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, payload);
641 		}
642 		dev_dbg(dev, "Decoding finished, payload size: %ld + %ld\n",
643 			vb2_get_plane_payload(&dst_buf->vb2_buf, 0),
644 			vb2_get_plane_payload(&dst_buf->vb2_buf, 1));
645 	}
646 
647 	/* short preview of the results */
648 	dev_dbg(dev, "src_buf preview: ");
649 	print_mxc_buf(jpeg, &src_buf->vb2_buf, 32);
650 	dev_dbg(dev, "dst_buf preview: ");
651 	print_mxc_buf(jpeg, &dst_buf->vb2_buf, 32);
652 	buf_state = VB2_BUF_STATE_DONE;
653 
654 buffers_done:
655 	mxc_jpeg_disable_irq(reg, ctx->slot);
656 	jpeg->slot_data[slot].used = false; /* unused, but don't free */
657 	mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf);
658 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
659 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
660 	v4l2_m2m_buf_done(src_buf, buf_state);
661 	v4l2_m2m_buf_done(dst_buf, buf_state);
662 	spin_unlock(&jpeg->hw_lock);
663 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
664 	return IRQ_HANDLED;
665 job_unlock:
666 	spin_unlock(&jpeg->hw_lock);
667 	return IRQ_HANDLED;
668 }
669 
mxc_jpeg_fixup_sof(struct mxc_jpeg_sof * sof,u32 fourcc,u16 w,u16 h)670 static int mxc_jpeg_fixup_sof(struct mxc_jpeg_sof *sof,
671 			      u32 fourcc,
672 			      u16 w, u16 h)
673 {
674 	int sof_length;
675 
676 	sof->precision = 8; /* TODO allow 8/12 bit precision*/
677 	sof->height = h;
678 	_bswap16(&sof->height);
679 	sof->width = w;
680 	_bswap16(&sof->width);
681 
682 	switch (fourcc) {
683 	case V4L2_PIX_FMT_NV12M:
684 		sof->components_no = 3;
685 		sof->comp[0].v = 0x2;
686 		sof->comp[0].h = 0x2;
687 		break;
688 	case V4L2_PIX_FMT_YUYV:
689 		sof->components_no = 3;
690 		sof->comp[0].v = 0x1;
691 		sof->comp[0].h = 0x2;
692 		break;
693 	case V4L2_PIX_FMT_YUV24:
694 	case V4L2_PIX_FMT_RGB24:
695 	default:
696 		sof->components_no = 3;
697 		break;
698 	case V4L2_PIX_FMT_ARGB32:
699 		sof->components_no = 4;
700 		break;
701 	case V4L2_PIX_FMT_GREY:
702 		sof->components_no = 1;
703 		break;
704 	}
705 	sof_length = 8 + 3 * sof->components_no;
706 	sof->length = sof_length;
707 	_bswap16(&sof->length);
708 
709 	return sof_length; /* not swaped */
710 }
711 
mxc_jpeg_fixup_sos(struct mxc_jpeg_sos * sos,u32 fourcc)712 static int mxc_jpeg_fixup_sos(struct mxc_jpeg_sos *sos,
713 			      u32 fourcc)
714 {
715 	int sos_length;
716 	u8 *sof_u8 = (u8 *)sos;
717 
718 	switch (fourcc) {
719 	case V4L2_PIX_FMT_NV12M:
720 		sos->components_no = 3;
721 		break;
722 	case V4L2_PIX_FMT_YUYV:
723 		sos->components_no = 3;
724 		break;
725 	case V4L2_PIX_FMT_YUV24:
726 	case V4L2_PIX_FMT_RGB24:
727 	default:
728 		sos->components_no = 3;
729 		break;
730 	case V4L2_PIX_FMT_ARGB32:
731 		sos->components_no = 4;
732 		break;
733 	case V4L2_PIX_FMT_GREY:
734 		sos->components_no = 1;
735 		break;
736 	}
737 	sos_length = 6 + 2 * sos->components_no;
738 	sos->length = sos_length;
739 	_bswap16(&sos->length);
740 
741 	/* SOS ignorable bytes, not so ignorable after all */
742 	sof_u8[sos_length - 1] = 0x0;
743 	sof_u8[sos_length - 2] = 0x3f;
744 	sof_u8[sos_length - 3] = 0x0;
745 
746 	return sos_length; /* not swaped */
747 }
748 
mxc_jpeg_setup_cfg_stream(void * cfg_stream_vaddr,u32 fourcc,u16 w,u16 h)749 static unsigned int mxc_jpeg_setup_cfg_stream(void *cfg_stream_vaddr,
750 					      u32 fourcc,
751 					      u16 w, u16 h)
752 {
753 	/*
754 	 * There is a hardware issue that first 128 bytes of configuration data
755 	 * can't be loaded correctly.
756 	 * To avoid this issue, we need to write the configuration from
757 	 * an offset which should be no less than 0x80 (128 bytes).
758 	 */
759 	unsigned int offset = 0x80;
760 	u8 *cfg = (u8 *)cfg_stream_vaddr;
761 	struct mxc_jpeg_sof *sof;
762 	struct mxc_jpeg_sos *sos;
763 
764 	memcpy(cfg + offset, jpeg_soi, ARRAY_SIZE(jpeg_soi));
765 	offset += ARRAY_SIZE(jpeg_soi);
766 
767 	if (fourcc == V4L2_PIX_FMT_RGB24 ||
768 	    fourcc == V4L2_PIX_FMT_ARGB32) {
769 		memcpy(cfg + offset, jpeg_app14, sizeof(jpeg_app14));
770 		offset += sizeof(jpeg_app14);
771 	} else {
772 		memcpy(cfg + offset, jpeg_app0, sizeof(jpeg_app0));
773 		offset += sizeof(jpeg_app0);
774 	}
775 
776 	memcpy(cfg + offset, jpeg_dqt, sizeof(jpeg_dqt));
777 	offset += sizeof(jpeg_dqt);
778 
779 	memcpy(cfg + offset, jpeg_sof_maximal, sizeof(jpeg_sof_maximal));
780 	offset += 2; /* skip marker ID */
781 	sof = (struct mxc_jpeg_sof *)(cfg + offset);
782 	offset += mxc_jpeg_fixup_sof(sof, fourcc, w, h);
783 
784 	memcpy(cfg + offset, jpeg_dht, sizeof(jpeg_dht));
785 	offset += sizeof(jpeg_dht);
786 
787 	memcpy(cfg + offset, jpeg_dri, sizeof(jpeg_dri));
788 	offset += sizeof(jpeg_dri);
789 
790 	memcpy(cfg + offset, jpeg_sos_maximal, sizeof(jpeg_sos_maximal));
791 	offset += 2; /* skip marker ID */
792 	sos = (struct mxc_jpeg_sos *)(cfg + offset);
793 	offset += mxc_jpeg_fixup_sos(sos, fourcc);
794 
795 	memcpy(cfg + offset, jpeg_eoi, sizeof(jpeg_eoi));
796 	offset += sizeof(jpeg_eoi);
797 
798 	return offset;
799 }
800 
mxc_jpeg_config_dec_desc(struct vb2_buffer * out_buf,struct mxc_jpeg_ctx * ctx,struct vb2_buffer * src_buf,struct vb2_buffer * dst_buf)801 static void mxc_jpeg_config_dec_desc(struct vb2_buffer *out_buf,
802 				     struct mxc_jpeg_ctx *ctx,
803 				     struct vb2_buffer *src_buf,
804 				     struct vb2_buffer *dst_buf)
805 {
806 	enum v4l2_buf_type cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
807 	struct mxc_jpeg_q_data *q_data_cap;
808 	enum mxc_jpeg_image_format img_fmt;
809 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
810 	void __iomem *reg = jpeg->base_reg;
811 	unsigned int slot = ctx->slot;
812 	struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc;
813 	struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc;
814 	dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle;
815 	dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle;
816 	dma_addr_t cfg_stream_handle = jpeg->slot_data[slot].cfg_stream_handle;
817 	unsigned int *cfg_size = &jpeg->slot_data[slot].cfg_stream_size;
818 	void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr;
819 	struct mxc_jpeg_src_buf *jpeg_src_buf;
820 
821 	jpeg_src_buf = vb2_to_mxc_buf(src_buf);
822 
823 	/* setup the decoding descriptor */
824 	desc->next_descpt_ptr = 0; /* end of chain */
825 	q_data_cap = mxc_jpeg_get_q_data(ctx, cap_type);
826 	desc->imgsize = q_data_cap->w_adjusted << 16 | q_data_cap->h_adjusted;
827 	img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data_cap->fmt->fourcc);
828 	desc->stm_ctrl &= ~STM_CTRL_IMAGE_FORMAT(0xF); /* clear image format */
829 	desc->stm_ctrl |= STM_CTRL_IMAGE_FORMAT(img_fmt);
830 	desc->line_pitch = q_data_cap->bytesperline[0];
831 	mxc_jpeg_addrs(desc, dst_buf, src_buf, 0);
832 	mxc_jpeg_set_bufsize(desc, ALIGN(vb2_plane_size(src_buf, 0), 1024));
833 	print_descriptor_info(jpeg->dev, desc);
834 
835 	if (!jpeg_src_buf->dht_needed) {
836 		/* validate the decoding descriptor */
837 		mxc_jpeg_set_desc(desc_handle, reg, slot);
838 		return;
839 	}
840 
841 	/*
842 	 * if a default huffman table is needed, use the config descriptor to
843 	 * inject a DHT, by chaining it before the decoding descriptor
844 	 */
845 	*cfg_size = mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr,
846 					      V4L2_PIX_FMT_YUYV,
847 					      MXC_JPEG_MIN_WIDTH,
848 					      MXC_JPEG_MIN_HEIGHT);
849 	cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN;
850 	cfg_desc->buf_base0 = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
851 	cfg_desc->buf_base1 = 0;
852 	cfg_desc->imgsize = MXC_JPEG_MIN_WIDTH << 16;
853 	cfg_desc->imgsize |= MXC_JPEG_MIN_HEIGHT;
854 	cfg_desc->line_pitch = MXC_JPEG_MIN_WIDTH * 2;
855 	cfg_desc->stm_ctrl = STM_CTRL_IMAGE_FORMAT(MXC_JPEG_YUV422);
856 	cfg_desc->stm_bufbase = cfg_stream_handle;
857 	cfg_desc->stm_bufsize = ALIGN(*cfg_size, 1024);
858 	print_descriptor_info(jpeg->dev, cfg_desc);
859 
860 	/* validate the configuration descriptor */
861 	mxc_jpeg_set_desc(cfg_desc_handle, reg, slot);
862 }
863 
mxc_jpeg_config_enc_desc(struct vb2_buffer * out_buf,struct mxc_jpeg_ctx * ctx,struct vb2_buffer * src_buf,struct vb2_buffer * dst_buf)864 static void mxc_jpeg_config_enc_desc(struct vb2_buffer *out_buf,
865 				     struct mxc_jpeg_ctx *ctx,
866 				     struct vb2_buffer *src_buf,
867 				     struct vb2_buffer *dst_buf)
868 {
869 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
870 	void __iomem *reg = jpeg->base_reg;
871 	unsigned int slot = ctx->slot;
872 	struct mxc_jpeg_desc *desc = jpeg->slot_data[slot].desc;
873 	struct mxc_jpeg_desc *cfg_desc = jpeg->slot_data[slot].cfg_desc;
874 	dma_addr_t desc_handle = jpeg->slot_data[slot].desc_handle;
875 	dma_addr_t cfg_desc_handle = jpeg->slot_data[slot].cfg_desc_handle;
876 	void *cfg_stream_vaddr = jpeg->slot_data[slot].cfg_stream_vaddr;
877 	struct mxc_jpeg_q_data *q_data;
878 	enum mxc_jpeg_image_format img_fmt;
879 	int w, h;
880 
881 	q_data = mxc_jpeg_get_q_data(ctx, src_buf->vb2_queue->type);
882 
883 	jpeg->slot_data[slot].cfg_stream_size =
884 			mxc_jpeg_setup_cfg_stream(cfg_stream_vaddr,
885 						  q_data->fmt->fourcc,
886 						  q_data->w,
887 						  q_data->h);
888 
889 	/* chain the config descriptor with the encoding descriptor */
890 	cfg_desc->next_descpt_ptr = desc_handle | MXC_NXT_DESCPT_EN;
891 
892 	cfg_desc->buf_base0 = jpeg->slot_data[slot].cfg_stream_handle;
893 	cfg_desc->buf_base1 = 0;
894 	cfg_desc->line_pitch = 0;
895 	cfg_desc->stm_bufbase = 0; /* no output expected */
896 	cfg_desc->stm_bufsize = 0x0;
897 	cfg_desc->imgsize = 0;
898 	cfg_desc->stm_ctrl = STM_CTRL_CONFIG_MOD(1);
899 
900 	desc->next_descpt_ptr = 0; /* end of chain */
901 
902 	/* use adjusted resolution for CAST IP job */
903 	w = q_data->w_adjusted;
904 	h = q_data->h_adjusted;
905 	mxc_jpeg_set_res(desc, w, h);
906 	mxc_jpeg_set_line_pitch(desc, w * (q_data->fmt->depth / 8));
907 	mxc_jpeg_set_bufsize(desc, desc->line_pitch * h);
908 	img_fmt = mxc_jpeg_fourcc_to_imgfmt(q_data->fmt->fourcc);
909 	if (img_fmt == MXC_JPEG_INVALID)
910 		dev_err(jpeg->dev, "No valid image format detected\n");
911 	desc->stm_ctrl = STM_CTRL_CONFIG_MOD(0) |
912 			 STM_CTRL_IMAGE_FORMAT(img_fmt);
913 	mxc_jpeg_addrs(desc, src_buf, dst_buf, 0);
914 	dev_dbg(jpeg->dev, "cfg_desc:\n");
915 	print_descriptor_info(jpeg->dev, cfg_desc);
916 	dev_dbg(jpeg->dev, "enc desc:\n");
917 	print_descriptor_info(jpeg->dev, desc);
918 	print_wrapper_info(jpeg->dev, reg);
919 	print_cast_status(jpeg->dev, reg, MXC_JPEG_ENCODE);
920 
921 	/* validate the configuration descriptor */
922 	mxc_jpeg_set_desc(cfg_desc_handle, reg, slot);
923 }
924 
mxc_jpeg_source_change(struct mxc_jpeg_ctx * ctx,struct mxc_jpeg_src_buf * jpeg_src_buf)925 static bool mxc_jpeg_source_change(struct mxc_jpeg_ctx *ctx,
926 				   struct mxc_jpeg_src_buf *jpeg_src_buf)
927 {
928 	struct device *dev = ctx->mxc_jpeg->dev;
929 	struct mxc_jpeg_q_data *q_data_cap;
930 
931 	if (!jpeg_src_buf->fmt)
932 		return false;
933 
934 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
935 	if (q_data_cap->fmt != jpeg_src_buf->fmt ||
936 	    q_data_cap->w != jpeg_src_buf->w ||
937 	    q_data_cap->h != jpeg_src_buf->h) {
938 		dev_dbg(dev, "Detected jpeg res=(%dx%d)->(%dx%d), pixfmt=%c%c%c%c\n",
939 			q_data_cap->w, q_data_cap->h,
940 			jpeg_src_buf->w, jpeg_src_buf->h,
941 			(jpeg_src_buf->fmt->fourcc & 0xff),
942 			(jpeg_src_buf->fmt->fourcc >>  8) & 0xff,
943 			(jpeg_src_buf->fmt->fourcc >> 16) & 0xff,
944 			(jpeg_src_buf->fmt->fourcc >> 24) & 0xff);
945 
946 		/*
947 		 * set-up the capture queue with the pixelformat and resolution
948 		 * detected from the jpeg output stream
949 		 */
950 		q_data_cap->w = jpeg_src_buf->w;
951 		q_data_cap->h = jpeg_src_buf->h;
952 		q_data_cap->fmt = jpeg_src_buf->fmt;
953 		q_data_cap->w_adjusted = q_data_cap->w;
954 		q_data_cap->h_adjusted = q_data_cap->h;
955 
956 		/*
957 		 * align up the resolution for CAST IP,
958 		 * but leave the buffer resolution unchanged
959 		 */
960 		v4l_bound_align_image(&q_data_cap->w_adjusted,
961 				      q_data_cap->w_adjusted,  /* adjust up */
962 				      MXC_JPEG_MAX_WIDTH,
963 				      q_data_cap->fmt->h_align,
964 				      &q_data_cap->h_adjusted,
965 				      q_data_cap->h_adjusted, /* adjust up */
966 				      MXC_JPEG_MAX_HEIGHT,
967 				      0,
968 				      0);
969 
970 		/* setup bytesperline/sizeimage for capture queue */
971 		mxc_jpeg_bytesperline(q_data_cap, jpeg_src_buf->fmt->precision);
972 		mxc_jpeg_sizeimage(q_data_cap);
973 		notify_src_chg(ctx);
974 		ctx->source_change = 1;
975 	}
976 	return ctx->source_change ? true : false;
977 }
978 
mxc_jpeg_job_ready(void * priv)979 static int mxc_jpeg_job_ready(void *priv)
980 {
981 	struct mxc_jpeg_ctx *ctx = priv;
982 
983 	return ctx->source_change ? 0 : 1;
984 }
985 
mxc_jpeg_device_run(void * priv)986 static void mxc_jpeg_device_run(void *priv)
987 {
988 	struct mxc_jpeg_ctx *ctx = priv;
989 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
990 	void __iomem *reg = jpeg->base_reg;
991 	struct device *dev = jpeg->dev;
992 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
993 	unsigned long flags;
994 	struct mxc_jpeg_q_data *q_data_cap, *q_data_out;
995 	struct mxc_jpeg_src_buf *jpeg_src_buf;
996 
997 	spin_lock_irqsave(&ctx->mxc_jpeg->hw_lock, flags);
998 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
999 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1000 	if (!src_buf || !dst_buf) {
1001 		dev_err(dev, "Null src or dst buf\n");
1002 		goto end;
1003 	}
1004 
1005 	q_data_cap = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1006 	if (!q_data_cap)
1007 		goto end;
1008 	q_data_out = mxc_jpeg_get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1009 	if (!q_data_out)
1010 		goto end;
1011 	src_buf->sequence = q_data_out->sequence++;
1012 	dst_buf->sequence = q_data_cap->sequence++;
1013 
1014 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
1015 
1016 	jpeg_src_buf = vb2_to_mxc_buf(&src_buf->vb2_buf);
1017 	if (q_data_cap->fmt->colplanes != dst_buf->vb2_buf.num_planes) {
1018 		dev_err(dev, "Capture format %s has %d planes, but capture buffer has %d planes\n",
1019 			q_data_cap->fmt->name, q_data_cap->fmt->colplanes,
1020 			dst_buf->vb2_buf.num_planes);
1021 		jpeg_src_buf->jpeg_parse_error = true;
1022 	}
1023 	if (jpeg_src_buf->jpeg_parse_error) {
1024 		mxc_jpeg_check_and_set_last_buffer(ctx, src_buf, dst_buf);
1025 		v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1026 		v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1027 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1028 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1029 		spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1030 		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1031 
1032 		return;
1033 	}
1034 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE) {
1035 		if (ctx->source_change || mxc_jpeg_source_change(ctx, jpeg_src_buf)) {
1036 			spin_unlock_irqrestore(&ctx->mxc_jpeg->hw_lock, flags);
1037 			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1038 			return;
1039 		}
1040 	}
1041 
1042 	/*
1043 	 * TODO: this reset should be removed, once we figure out
1044 	 * how to overcome hardware issues both on encoder and decoder
1045 	 */
1046 	mxc_jpeg_sw_reset(reg);
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 
mxc_jpeg_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)1080 static int mxc_jpeg_decoder_cmd(struct file *file, void *priv,
1081 				struct v4l2_decoder_cmd *cmd)
1082 {
1083 	struct v4l2_fh *fh = file->private_data;
1084 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
1085 	int ret;
1086 
1087 	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
1088 	if (ret < 0)
1089 		return ret;
1090 
1091 	if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx)))
1092 		return 0;
1093 
1094 	ret = v4l2_m2m_ioctl_decoder_cmd(file, priv, cmd);
1095 	if (ret < 0)
1096 		return ret;
1097 
1098 	if (cmd->cmd == V4L2_DEC_CMD_STOP &&
1099 	    v4l2_m2m_has_stopped(fh->m2m_ctx)) {
1100 		notify_eos(ctx);
1101 		ctx->header_parsed = false;
1102 	}
1103 
1104 	if (cmd->cmd == V4L2_DEC_CMD_START &&
1105 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1106 		vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q);
1107 	return 0;
1108 }
1109 
mxc_jpeg_encoder_cmd(struct file * file,void * priv,struct v4l2_encoder_cmd * cmd)1110 static int mxc_jpeg_encoder_cmd(struct file *file, void *priv,
1111 				struct v4l2_encoder_cmd *cmd)
1112 {
1113 	struct v4l2_fh *fh = file->private_data;
1114 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(fh);
1115 	int ret;
1116 
1117 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
1118 	if (ret < 0)
1119 		return ret;
1120 
1121 	if (!vb2_is_streaming(v4l2_m2m_get_src_vq(fh->m2m_ctx)) ||
1122 	    !vb2_is_streaming(v4l2_m2m_get_dst_vq(fh->m2m_ctx)))
1123 		return 0;
1124 
1125 	ret = v4l2_m2m_ioctl_encoder_cmd(file, fh, cmd);
1126 	if (ret < 0)
1127 		return 0;
1128 
1129 	if (cmd->cmd == V4L2_ENC_CMD_STOP &&
1130 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1131 		notify_eos(ctx);
1132 
1133 	if (cmd->cmd == V4L2_ENC_CMD_START &&
1134 	    v4l2_m2m_has_stopped(fh->m2m_ctx))
1135 		vb2_clear_last_buffer_dequeued(&fh->m2m_ctx->cap_q_ctx.q);
1136 
1137 	return 0;
1138 }
1139 
mxc_jpeg_queue_setup(struct vb2_queue * q,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_ctxs[])1140 static int mxc_jpeg_queue_setup(struct vb2_queue *q,
1141 				unsigned int *nbuffers,
1142 				unsigned int *nplanes,
1143 				unsigned int sizes[],
1144 				struct device *alloc_ctxs[])
1145 {
1146 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1147 	struct mxc_jpeg_q_data *q_data = NULL;
1148 	struct mxc_jpeg_q_data tmp_q;
1149 	int i;
1150 
1151 	q_data = mxc_jpeg_get_q_data(ctx, q->type);
1152 	if (!q_data)
1153 		return -EINVAL;
1154 
1155 	tmp_q.fmt = q_data->fmt;
1156 	tmp_q.w = q_data->w_adjusted;
1157 	tmp_q.h = q_data->h_adjusted;
1158 	for (i = 0; i < MXC_JPEG_MAX_PLANES; i++) {
1159 		tmp_q.bytesperline[i] = q_data->bytesperline[i];
1160 		tmp_q.sizeimage[i] = q_data->sizeimage[i];
1161 	}
1162 	mxc_jpeg_sizeimage(&tmp_q);
1163 	for (i = 0; i < MXC_JPEG_MAX_PLANES; i++)
1164 		tmp_q.sizeimage[i] = max(tmp_q.sizeimage[i], q_data->sizeimage[i]);
1165 
1166 	/* Handle CREATE_BUFS situation - *nplanes != 0 */
1167 	if (*nplanes) {
1168 		for (i = 0; i < *nplanes; i++) {
1169 			if (sizes[i] < tmp_q.sizeimage[i])
1170 				return -EINVAL;
1171 		}
1172 		return 0;
1173 	}
1174 
1175 	/* Handle REQBUFS situation */
1176 	*nplanes = q_data->fmt->colplanes;
1177 	for (i = 0; i < *nplanes; i++)
1178 		sizes[i] = tmp_q.sizeimage[i];
1179 
1180 	return 0;
1181 }
1182 
mxc_jpeg_start_streaming(struct vb2_queue * q,unsigned int count)1183 static int mxc_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
1184 {
1185 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1186 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, q->type);
1187 	int ret;
1188 
1189 	v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
1190 
1191 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE && V4L2_TYPE_IS_CAPTURE(q->type))
1192 		ctx->source_change = 0;
1193 	dev_dbg(ctx->mxc_jpeg->dev, "Start streaming ctx=%p", ctx);
1194 	q_data->sequence = 0;
1195 
1196 	ret = pm_runtime_resume_and_get(ctx->mxc_jpeg->dev);
1197 	if (ret < 0) {
1198 		dev_err(ctx->mxc_jpeg->dev, "Failed to power up jpeg\n");
1199 		return ret;
1200 	}
1201 
1202 	return 0;
1203 }
1204 
mxc_jpeg_stop_streaming(struct vb2_queue * q)1205 static void mxc_jpeg_stop_streaming(struct vb2_queue *q)
1206 {
1207 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(q);
1208 	struct vb2_v4l2_buffer *vbuf;
1209 
1210 	dev_dbg(ctx->mxc_jpeg->dev, "Stop streaming ctx=%p", ctx);
1211 
1212 	/* Release all active buffers */
1213 	for (;;) {
1214 		if (V4L2_TYPE_IS_OUTPUT(q->type))
1215 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1216 		else
1217 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1218 		if (!vbuf)
1219 			break;
1220 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1221 	}
1222 
1223 	v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
1224 	if (V4L2_TYPE_IS_OUTPUT(q->type) &&
1225 	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) {
1226 		notify_eos(ctx);
1227 		ctx->header_parsed = false;
1228 	}
1229 
1230 	pm_runtime_put_sync(&ctx->mxc_jpeg->pdev->dev);
1231 }
1232 
mxc_jpeg_valid_comp_id(struct device * dev,struct mxc_jpeg_sof * sof,struct mxc_jpeg_sos * sos)1233 static int mxc_jpeg_valid_comp_id(struct device *dev,
1234 				  struct mxc_jpeg_sof *sof,
1235 				  struct mxc_jpeg_sos *sos)
1236 {
1237 	int valid = 1;
1238 	int i;
1239 
1240 	/*
1241 	 * there's a limitation in the IP that the component IDs must be
1242 	 * between 0..4, if they are not, let's patch them
1243 	 */
1244 	for (i = 0; i < sof->components_no; i++)
1245 		if (sof->comp[i].id > MXC_JPEG_MAX_COMPONENTS) {
1246 			valid = 0;
1247 			dev_err(dev, "Component %d has invalid ID: %d",
1248 				i, sof->comp[i].id);
1249 		}
1250 	if (!valid)
1251 		/* patch all comp IDs if at least one is invalid */
1252 		for (i = 0; i < sof->components_no; i++) {
1253 			dev_warn(dev, "Component %d ID patched to: %d",
1254 				 i, i + 1);
1255 			sof->comp[i].id = i + 1;
1256 			sos->comp[i].id = i + 1;
1257 		}
1258 
1259 	return valid;
1260 }
1261 
mxc_jpeg_get_image_format(struct device * dev,const struct v4l2_jpeg_header * header)1262 static u32 mxc_jpeg_get_image_format(struct device *dev,
1263 				     const struct v4l2_jpeg_header *header)
1264 {
1265 	int i;
1266 	u32 fourcc = 0;
1267 
1268 	for (i = 0; i < MXC_JPEG_NUM_FORMATS; i++)
1269 		if (mxc_formats[i].subsampling == header->frame.subsampling &&
1270 		    mxc_formats[i].nc == header->frame.num_components &&
1271 		    mxc_formats[i].precision == header->frame.precision) {
1272 			fourcc = mxc_formats[i].fourcc;
1273 			break;
1274 		}
1275 	if (fourcc == 0) {
1276 		dev_err(dev,
1277 			"Could not identify image format nc=%d, subsampling=%d, precision=%d\n",
1278 			header->frame.num_components,
1279 			header->frame.subsampling,
1280 			header->frame.precision);
1281 		return fourcc;
1282 	}
1283 	/*
1284 	 * If the transform flag from APP14 marker is 0, images that are
1285 	 * encoded with 3 components have RGB colorspace, see Recommendation
1286 	 * ITU-T T.872 chapter 6.5.3 APP14 marker segment for colour encoding
1287 	 */
1288 	if (fourcc == V4L2_PIX_FMT_YUV24 || fourcc == V4L2_PIX_FMT_RGB24) {
1289 		if (header->app14_tf == V4L2_JPEG_APP14_TF_CMYK_RGB)
1290 			fourcc = V4L2_PIX_FMT_RGB24;
1291 		else
1292 			fourcc = V4L2_PIX_FMT_YUV24;
1293 	}
1294 
1295 	return fourcc;
1296 }
1297 
mxc_jpeg_bytesperline(struct mxc_jpeg_q_data * q,u32 precision)1298 static void mxc_jpeg_bytesperline(struct mxc_jpeg_q_data *q, u32 precision)
1299 {
1300 	/* Bytes distance between the leftmost pixels in two adjacent lines */
1301 	if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1302 		/* bytesperline unused for compressed formats */
1303 		q->bytesperline[0] = 0;
1304 		q->bytesperline[1] = 0;
1305 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
1306 		/* When the image format is planar the bytesperline value
1307 		 * applies to the first plane and is divided by the same factor
1308 		 * as the width field for the other planes
1309 		 */
1310 		q->bytesperline[0] = q->w * DIV_ROUND_UP(precision, 8);
1311 		q->bytesperline[1] = q->bytesperline[0];
1312 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
1313 		q->bytesperline[0] = q->w * DIV_ROUND_UP(precision, 8) * 2;
1314 		q->bytesperline[1] = 0;
1315 	} else if (q->fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_444) {
1316 		q->bytesperline[0] = q->w * DIV_ROUND_UP(precision, 8) * q->fmt->nc;
1317 		q->bytesperline[1] = 0;
1318 	} else {
1319 		/* grayscale */
1320 		q->bytesperline[0] = q->w * DIV_ROUND_UP(precision, 8);
1321 		q->bytesperline[1] = 0;
1322 	}
1323 }
1324 
mxc_jpeg_sizeimage(struct mxc_jpeg_q_data * q)1325 static void mxc_jpeg_sizeimage(struct mxc_jpeg_q_data *q)
1326 {
1327 	if (q->fmt->fourcc == V4L2_PIX_FMT_JPEG) {
1328 		/* if no sizeimage from user, assume worst jpeg compression */
1329 		if (!q->sizeimage[0])
1330 			q->sizeimage[0] = 6 * q->w * q->h;
1331 		q->sizeimage[1] = 0;
1332 
1333 		if (q->sizeimage[0] > MXC_JPEG_MAX_SIZEIMAGE)
1334 			q->sizeimage[0] = MXC_JPEG_MAX_SIZEIMAGE;
1335 
1336 		/* jpeg stream size must be multiple of 1K */
1337 		q->sizeimage[0] = ALIGN(q->sizeimage[0], 1024);
1338 	} else {
1339 		q->sizeimage[0] = q->bytesperline[0] * q->h;
1340 		q->sizeimage[1] = 0;
1341 		if (q->fmt->fourcc == V4L2_PIX_FMT_NV12M)
1342 			q->sizeimage[1] = q->sizeimage[0] / 2;
1343 	}
1344 }
1345 
mxc_jpeg_parse(struct mxc_jpeg_ctx * ctx,struct vb2_buffer * vb)1346 static int mxc_jpeg_parse(struct mxc_jpeg_ctx *ctx, struct vb2_buffer *vb)
1347 {
1348 	struct device *dev = ctx->mxc_jpeg->dev;
1349 	struct mxc_jpeg_q_data *q_data_out;
1350 	u32 fourcc;
1351 	struct v4l2_jpeg_header header;
1352 	struct mxc_jpeg_sof *psof = NULL;
1353 	struct mxc_jpeg_sos *psos = NULL;
1354 	struct mxc_jpeg_src_buf *jpeg_src_buf = vb2_to_mxc_buf(vb);
1355 	u8 *src_addr = (u8 *)vb2_plane_vaddr(vb, 0);
1356 	u32 size = vb2_get_plane_payload(vb, 0);
1357 	int ret;
1358 
1359 	memset(&header, 0, sizeof(header));
1360 	ret = v4l2_jpeg_parse_header((void *)src_addr, size, &header);
1361 	if (ret < 0) {
1362 		dev_err(dev, "Error parsing JPEG stream markers\n");
1363 		return ret;
1364 	}
1365 
1366 	/* if DHT marker present, no need to inject default one */
1367 	jpeg_src_buf->dht_needed = (header.num_dht == 0);
1368 
1369 	q_data_out = mxc_jpeg_get_q_data(ctx,
1370 					 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1371 	if (q_data_out->w == 0 && q_data_out->h == 0) {
1372 		dev_warn(dev, "Invalid user resolution 0x0");
1373 		dev_warn(dev, "Keeping resolution from JPEG: %dx%d",
1374 			 header.frame.width, header.frame.height);
1375 	} else if (header.frame.width != q_data_out->w ||
1376 		   header.frame.height != q_data_out->h) {
1377 		dev_err(dev,
1378 			"Resolution mismatch: %dx%d (JPEG) versus %dx%d(user)",
1379 			header.frame.width, header.frame.height,
1380 			q_data_out->w, q_data_out->h);
1381 	}
1382 	q_data_out->w = header.frame.width;
1383 	q_data_out->h = header.frame.height;
1384 	if (header.frame.width > MXC_JPEG_MAX_WIDTH ||
1385 	    header.frame.height > MXC_JPEG_MAX_HEIGHT) {
1386 		dev_err(dev, "JPEG width or height should be <= 8192: %dx%d\n",
1387 			header.frame.width, header.frame.height);
1388 		return -EINVAL;
1389 	}
1390 	if (header.frame.width < MXC_JPEG_MIN_WIDTH ||
1391 	    header.frame.height < MXC_JPEG_MIN_HEIGHT) {
1392 		dev_err(dev, "JPEG width or height should be > 64: %dx%d\n",
1393 			header.frame.width, header.frame.height);
1394 		return -EINVAL;
1395 	}
1396 	if (header.frame.num_components > V4L2_JPEG_MAX_COMPONENTS) {
1397 		dev_err(dev, "JPEG number of components should be <=%d",
1398 			V4L2_JPEG_MAX_COMPONENTS);
1399 		return -EINVAL;
1400 	}
1401 	/* check and, if necessary, patch component IDs*/
1402 	psof = (struct mxc_jpeg_sof *)header.sof.start;
1403 	psos = (struct mxc_jpeg_sos *)header.sos.start;
1404 	if (!mxc_jpeg_valid_comp_id(dev, psof, psos))
1405 		dev_warn(dev, "JPEG component ids should be 0-3 or 1-4");
1406 
1407 	fourcc = mxc_jpeg_get_image_format(dev, &header);
1408 	if (fourcc == 0)
1409 		return -EINVAL;
1410 
1411 	jpeg_src_buf->fmt = mxc_jpeg_find_format(ctx, fourcc);
1412 	jpeg_src_buf->w = header.frame.width;
1413 	jpeg_src_buf->h = header.frame.height;
1414 	ctx->header_parsed = true;
1415 
1416 	if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx))
1417 		mxc_jpeg_source_change(ctx, jpeg_src_buf);
1418 
1419 	return 0;
1420 }
1421 
mxc_jpeg_buf_queue(struct vb2_buffer * vb)1422 static void mxc_jpeg_buf_queue(struct vb2_buffer *vb)
1423 {
1424 	int ret;
1425 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1426 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1427 	struct mxc_jpeg_src_buf *jpeg_src_buf;
1428 
1429 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1430 	    vb2_is_streaming(vb->vb2_queue) &&
1431 	    v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
1432 		struct mxc_jpeg_q_data *q_data;
1433 
1434 		q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type);
1435 		vbuf->field = V4L2_FIELD_NONE;
1436 		vbuf->sequence = q_data->sequence++;
1437 		v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
1438 		notify_eos(ctx);
1439 		ctx->header_parsed = false;
1440 		return;
1441 	}
1442 
1443 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1444 		goto end;
1445 
1446 	/* for V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE */
1447 	if (ctx->mxc_jpeg->mode != MXC_JPEG_DECODE)
1448 		goto end;
1449 
1450 	jpeg_src_buf = vb2_to_mxc_buf(vb);
1451 	jpeg_src_buf->jpeg_parse_error = false;
1452 	ret = mxc_jpeg_parse(ctx, vb);
1453 	if (ret)
1454 		jpeg_src_buf->jpeg_parse_error = true;
1455 
1456 end:
1457 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1458 }
1459 
mxc_jpeg_buf_out_validate(struct vb2_buffer * vb)1460 static int mxc_jpeg_buf_out_validate(struct vb2_buffer *vb)
1461 {
1462 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1463 
1464 	vbuf->field = V4L2_FIELD_NONE;
1465 
1466 	return 0;
1467 }
1468 
mxc_jpeg_buf_prepare(struct vb2_buffer * vb)1469 static int mxc_jpeg_buf_prepare(struct vb2_buffer *vb)
1470 {
1471 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1472 	struct mxc_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1473 	struct mxc_jpeg_q_data *q_data = NULL;
1474 	struct device *dev = ctx->mxc_jpeg->dev;
1475 	unsigned long sizeimage;
1476 	int i;
1477 
1478 	vbuf->field = V4L2_FIELD_NONE;
1479 
1480 	q_data = mxc_jpeg_get_q_data(ctx, vb->vb2_queue->type);
1481 	if (!q_data)
1482 		return -EINVAL;
1483 	for (i = 0; i < q_data->fmt->colplanes; i++) {
1484 		sizeimage = q_data->sizeimage[i];
1485 		if (vb2_plane_size(vb, i) < sizeimage) {
1486 			dev_err(dev, "plane %d too small (%lu < %lu)",
1487 				i, vb2_plane_size(vb, i), sizeimage);
1488 			return -EINVAL;
1489 		}
1490 		vb2_set_plane_payload(vb, i, sizeimage);
1491 	}
1492 	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) {
1493 		vb2_set_plane_payload(vb, 0, 0);
1494 		vb2_set_plane_payload(vb, 1, 0);
1495 	}
1496 	return 0;
1497 }
1498 
1499 static const struct vb2_ops mxc_jpeg_qops = {
1500 	.queue_setup		= mxc_jpeg_queue_setup,
1501 	.wait_prepare		= vb2_ops_wait_prepare,
1502 	.wait_finish		= vb2_ops_wait_finish,
1503 	.buf_out_validate	= mxc_jpeg_buf_out_validate,
1504 	.buf_prepare		= mxc_jpeg_buf_prepare,
1505 	.start_streaming	= mxc_jpeg_start_streaming,
1506 	.stop_streaming		= mxc_jpeg_stop_streaming,
1507 	.buf_queue		= mxc_jpeg_buf_queue,
1508 };
1509 
mxc_jpeg_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1510 static int mxc_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
1511 			       struct vb2_queue *dst_vq)
1512 {
1513 	struct mxc_jpeg_ctx *ctx = priv;
1514 	int ret;
1515 
1516 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1517 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1518 	src_vq->drv_priv = ctx;
1519 	src_vq->buf_struct_size = sizeof(struct mxc_jpeg_src_buf);
1520 	src_vq->ops = &mxc_jpeg_qops;
1521 	src_vq->mem_ops = &vb2_dma_contig_memops;
1522 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1523 	src_vq->lock = &ctx->mxc_jpeg->lock;
1524 	src_vq->dev = ctx->mxc_jpeg->dev;
1525 	src_vq->allow_zero_bytesused = 1; /* keep old userspace apps working */
1526 
1527 	ret = vb2_queue_init(src_vq);
1528 	if (ret)
1529 		return ret;
1530 
1531 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1532 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1533 	dst_vq->drv_priv = ctx;
1534 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1535 	dst_vq->ops = &mxc_jpeg_qops;
1536 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1537 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1538 	dst_vq->lock = &ctx->mxc_jpeg->lock;
1539 	dst_vq->dev = ctx->mxc_jpeg->dev;
1540 
1541 	ret = vb2_queue_init(dst_vq);
1542 	return ret;
1543 }
1544 
mxc_jpeg_set_default_params(struct mxc_jpeg_ctx * ctx)1545 static void mxc_jpeg_set_default_params(struct mxc_jpeg_ctx *ctx)
1546 {
1547 	struct mxc_jpeg_q_data *out_q = &ctx->out_q;
1548 	struct mxc_jpeg_q_data *cap_q = &ctx->cap_q;
1549 	struct mxc_jpeg_q_data *q[2] = {out_q, cap_q};
1550 	int i;
1551 
1552 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) {
1553 		out_q->fmt = mxc_jpeg_find_format(ctx, MXC_JPEG_DEFAULT_PFMT);
1554 		cap_q->fmt = mxc_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG);
1555 	} else {
1556 		out_q->fmt = mxc_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG);
1557 		cap_q->fmt = mxc_jpeg_find_format(ctx, MXC_JPEG_DEFAULT_PFMT);
1558 	}
1559 
1560 	for (i = 0; i < 2; i++) {
1561 		q[i]->w = MXC_JPEG_DEFAULT_WIDTH;
1562 		q[i]->h = MXC_JPEG_DEFAULT_HEIGHT;
1563 		q[i]->w_adjusted = MXC_JPEG_DEFAULT_WIDTH;
1564 		q[i]->h_adjusted = MXC_JPEG_DEFAULT_HEIGHT;
1565 		mxc_jpeg_bytesperline(q[i], q[i]->fmt->precision);
1566 		mxc_jpeg_sizeimage(q[i]);
1567 	}
1568 }
1569 
mxc_jpeg_open(struct file * file)1570 static int mxc_jpeg_open(struct file *file)
1571 {
1572 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
1573 	struct video_device *mxc_vfd = video_devdata(file);
1574 	struct device *dev = mxc_jpeg->dev;
1575 	struct mxc_jpeg_ctx *ctx;
1576 	int ret = 0;
1577 
1578 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1579 	if (!ctx)
1580 		return -ENOMEM;
1581 
1582 	if (mutex_lock_interruptible(&mxc_jpeg->lock)) {
1583 		ret = -ERESTARTSYS;
1584 		goto free;
1585 	}
1586 
1587 	v4l2_fh_init(&ctx->fh, mxc_vfd);
1588 	file->private_data = &ctx->fh;
1589 	v4l2_fh_add(&ctx->fh);
1590 
1591 	ctx->mxc_jpeg = mxc_jpeg;
1592 
1593 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(mxc_jpeg->m2m_dev, ctx,
1594 					    mxc_jpeg_queue_init);
1595 
1596 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1597 		ret = PTR_ERR(ctx->fh.m2m_ctx);
1598 		goto error;
1599 	}
1600 
1601 	mxc_jpeg_set_default_params(ctx);
1602 	ctx->slot = MXC_MAX_SLOTS; /* slot not allocated yet */
1603 
1604 	if (mxc_jpeg->mode == MXC_JPEG_DECODE)
1605 		dev_dbg(dev, "Opened JPEG decoder instance %p\n", ctx);
1606 	else
1607 		dev_dbg(dev, "Opened JPEG encoder instance %p\n", ctx);
1608 	mutex_unlock(&mxc_jpeg->lock);
1609 
1610 	return 0;
1611 
1612 error:
1613 	v4l2_fh_del(&ctx->fh);
1614 	v4l2_fh_exit(&ctx->fh);
1615 	mutex_unlock(&mxc_jpeg->lock);
1616 free:
1617 	kfree(ctx);
1618 	return ret;
1619 }
1620 
mxc_jpeg_querycap(struct file * file,void * priv,struct v4l2_capability * cap)1621 static int mxc_jpeg_querycap(struct file *file, void *priv,
1622 			     struct v4l2_capability *cap)
1623 {
1624 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
1625 
1626 	strscpy(cap->driver, MXC_JPEG_NAME " codec", sizeof(cap->driver));
1627 	strscpy(cap->card, MXC_JPEG_NAME " codec", sizeof(cap->card));
1628 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1629 		 dev_name(mxc_jpeg->dev));
1630 	cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
1631 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1632 
1633 	return 0;
1634 }
1635 
mxc_jpeg_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)1636 static int mxc_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
1637 				     struct v4l2_fmtdesc *f)
1638 {
1639 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1640 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type);
1641 
1642 	if (ctx->mxc_jpeg->mode == MXC_JPEG_ENCODE) {
1643 		return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f,
1644 			MXC_JPEG_FMT_TYPE_ENC);
1645 	} else if (!ctx->header_parsed) {
1646 		return enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f,
1647 			MXC_JPEG_FMT_TYPE_RAW);
1648 	} else {
1649 		/* For the decoder CAPTURE queue, only enumerate the raw formats
1650 		 * supported for the format currently active on OUTPUT
1651 		 * (more precisely what was propagated on capture queue
1652 		 * after jpeg parse on the output buffer)
1653 		 */
1654 		if (f->index)
1655 			return -EINVAL;
1656 		f->pixelformat = q_data->fmt->fourcc;
1657 		strscpy(f->description, q_data->fmt->name, sizeof(f->description));
1658 		return 0;
1659 	}
1660 }
1661 
mxc_jpeg_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)1662 static int mxc_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
1663 				     struct v4l2_fmtdesc *f)
1664 {
1665 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1666 	u32 type = ctx->mxc_jpeg->mode == MXC_JPEG_DECODE ?  MXC_JPEG_FMT_TYPE_ENC :
1667 							     MXC_JPEG_FMT_TYPE_RAW;
1668 	int ret;
1669 
1670 	ret = enum_fmt(mxc_formats, MXC_JPEG_NUM_FORMATS, f, type);
1671 	if (ret)
1672 		return ret;
1673 	if (ctx->mxc_jpeg->mode == MXC_JPEG_DECODE)
1674 		f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION;
1675 	return 0;
1676 }
1677 
mxc_jpeg_try_fmt(struct v4l2_format * f,const struct mxc_jpeg_fmt * fmt,struct mxc_jpeg_ctx * ctx,int q_type)1678 static int mxc_jpeg_try_fmt(struct v4l2_format *f, const struct mxc_jpeg_fmt *fmt,
1679 			    struct mxc_jpeg_ctx *ctx, int q_type)
1680 {
1681 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
1682 	struct v4l2_plane_pix_format *pfmt;
1683 	u32 w = (pix_mp->width < MXC_JPEG_MAX_WIDTH) ?
1684 		 pix_mp->width : MXC_JPEG_MAX_WIDTH;
1685 	u32 h = (pix_mp->height < MXC_JPEG_MAX_HEIGHT) ?
1686 		 pix_mp->height : MXC_JPEG_MAX_HEIGHT;
1687 	int i;
1688 	struct mxc_jpeg_q_data tmp_q;
1689 
1690 	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
1691 	pix_mp->field = V4L2_FIELD_NONE;
1692 	pix_mp->num_planes = fmt->colplanes;
1693 	pix_mp->pixelformat = fmt->fourcc;
1694 
1695 	pix_mp->width = w;
1696 	pix_mp->height = h;
1697 	v4l_bound_align_image(&w,
1698 			      w, /* adjust upwards*/
1699 			      MXC_JPEG_MAX_WIDTH,
1700 			      fmt->h_align,
1701 			      &h,
1702 			      h, /* adjust upwards*/
1703 			      MXC_JPEG_MAX_HEIGHT,
1704 			      0,
1705 			      0);
1706 
1707 	/* get user input into the tmp_q */
1708 	tmp_q.w = w;
1709 	tmp_q.h = h;
1710 	tmp_q.fmt = fmt;
1711 	for (i = 0; i < pix_mp->num_planes; i++) {
1712 		pfmt = &pix_mp->plane_fmt[i];
1713 		tmp_q.bytesperline[i] = pfmt->bytesperline;
1714 		tmp_q.sizeimage[i] = pfmt->sizeimage;
1715 	}
1716 
1717 	/* calculate bytesperline & sizeimage into the tmp_q */
1718 	mxc_jpeg_bytesperline(&tmp_q, fmt->precision);
1719 	mxc_jpeg_sizeimage(&tmp_q);
1720 
1721 	/* adjust user format according to our calculations */
1722 	for (i = 0; i < pix_mp->num_planes; i++) {
1723 		pfmt = &pix_mp->plane_fmt[i];
1724 		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
1725 		pfmt->bytesperline = tmp_q.bytesperline[i];
1726 		pfmt->sizeimage = tmp_q.sizeimage[i];
1727 	}
1728 
1729 	/* fix colorspace information to sRGB for both output & capture */
1730 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
1731 	pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601;
1732 	pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB;
1733 	/*
1734 	 * this hardware does not change the range of the samples
1735 	 * but since inside JPEG the YUV quantization is full-range,
1736 	 * this driver will always use full-range for the raw frames, too
1737 	 */
1738 	pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1739 
1740 	return 0;
1741 }
1742 
mxc_jpeg_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1743 static int mxc_jpeg_try_fmt_vid_cap(struct file *file, void *priv,
1744 				    struct v4l2_format *f)
1745 {
1746 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1747 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1748 	struct device *dev = jpeg->dev;
1749 	const struct mxc_jpeg_fmt *fmt;
1750 	u32 fourcc = f->fmt.pix_mp.pixelformat;
1751 
1752 	int q_type = (jpeg->mode == MXC_JPEG_DECODE) ?
1753 		     MXC_JPEG_FMT_TYPE_RAW : MXC_JPEG_FMT_TYPE_ENC;
1754 
1755 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
1756 		dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type);
1757 		return -EINVAL;
1758 	}
1759 
1760 	fmt = mxc_jpeg_find_format(ctx, fourcc);
1761 	if (!fmt || fmt->flags != q_type) {
1762 		dev_warn(dev, "Format not supported: %c%c%c%c, use the default.\n",
1763 			 (fourcc & 0xff),
1764 			 (fourcc >>  8) & 0xff,
1765 			 (fourcc >> 16) & 0xff,
1766 			 (fourcc >> 24) & 0xff);
1767 		f->fmt.pix_mp.pixelformat = (jpeg->mode == MXC_JPEG_DECODE) ?
1768 				MXC_JPEG_DEFAULT_PFMT : V4L2_PIX_FMT_JPEG;
1769 		fmt = mxc_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat);
1770 	}
1771 	return mxc_jpeg_try_fmt(f, fmt, ctx, q_type);
1772 }
1773 
mxc_jpeg_try_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)1774 static int mxc_jpeg_try_fmt_vid_out(struct file *file, void *priv,
1775 				    struct v4l2_format *f)
1776 {
1777 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1778 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1779 	struct device *dev = jpeg->dev;
1780 	const struct mxc_jpeg_fmt *fmt;
1781 	u32 fourcc = f->fmt.pix_mp.pixelformat;
1782 
1783 	int q_type = (jpeg->mode == MXC_JPEG_ENCODE) ?
1784 		     MXC_JPEG_FMT_TYPE_RAW : MXC_JPEG_FMT_TYPE_ENC;
1785 
1786 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
1787 		dev_err(dev, "TRY_FMT with Invalid type: %d\n", f->type);
1788 		return -EINVAL;
1789 	}
1790 
1791 	fmt = mxc_jpeg_find_format(ctx, fourcc);
1792 	if (!fmt || fmt->flags != q_type) {
1793 		dev_warn(dev, "Format not supported: %c%c%c%c, use the default.\n",
1794 			 (fourcc & 0xff),
1795 			 (fourcc >>  8) & 0xff,
1796 			 (fourcc >> 16) & 0xff,
1797 			 (fourcc >> 24) & 0xff);
1798 		f->fmt.pix_mp.pixelformat = (jpeg->mode == MXC_JPEG_ENCODE) ?
1799 				MXC_JPEG_DEFAULT_PFMT : V4L2_PIX_FMT_JPEG;
1800 		fmt = mxc_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat);
1801 	}
1802 	return mxc_jpeg_try_fmt(f, fmt, ctx, q_type);
1803 }
1804 
mxc_jpeg_s_fmt(struct mxc_jpeg_ctx * ctx,struct v4l2_format * f)1805 static int mxc_jpeg_s_fmt(struct mxc_jpeg_ctx *ctx,
1806 			  struct v4l2_format *f)
1807 {
1808 	struct vb2_queue *vq;
1809 	struct mxc_jpeg_q_data *q_data = NULL;
1810 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
1811 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1812 	int i;
1813 
1814 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
1815 	if (!vq)
1816 		return -EINVAL;
1817 
1818 	q_data = mxc_jpeg_get_q_data(ctx, f->type);
1819 
1820 	if (vb2_is_busy(vq)) {
1821 		v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
1822 		return -EBUSY;
1823 	}
1824 
1825 	q_data->fmt = mxc_jpeg_find_format(ctx, pix_mp->pixelformat);
1826 	q_data->w = pix_mp->width;
1827 	q_data->h = pix_mp->height;
1828 
1829 	q_data->w_adjusted = q_data->w;
1830 	q_data->h_adjusted = q_data->h;
1831 	/*
1832 	 * align up the resolution for CAST IP,
1833 	 * but leave the buffer resolution unchanged
1834 	 */
1835 	v4l_bound_align_image(&q_data->w_adjusted,
1836 			      q_data->w_adjusted,  /* adjust upwards */
1837 			      MXC_JPEG_MAX_WIDTH,
1838 			      q_data->fmt->h_align,
1839 			      &q_data->h_adjusted,
1840 			      q_data->h_adjusted, /* adjust upwards */
1841 			      MXC_JPEG_MAX_HEIGHT,
1842 			      q_data->fmt->v_align,
1843 			      0);
1844 
1845 	for (i = 0; i < pix_mp->num_planes; i++) {
1846 		q_data->bytesperline[i] = pix_mp->plane_fmt[i].bytesperline;
1847 		q_data->sizeimage[i] = pix_mp->plane_fmt[i].sizeimage;
1848 	}
1849 
1850 	return 0;
1851 }
1852 
mxc_jpeg_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1853 static int mxc_jpeg_s_fmt_vid_cap(struct file *file, void *priv,
1854 				  struct v4l2_format *f)
1855 {
1856 	int ret;
1857 
1858 	ret = mxc_jpeg_try_fmt_vid_cap(file, priv, f);
1859 	if (ret)
1860 		return ret;
1861 
1862 	return mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f);
1863 }
1864 
mxc_jpeg_s_fmt_vid_out(struct file * file,void * priv,struct v4l2_format * f)1865 static int mxc_jpeg_s_fmt_vid_out(struct file *file, void *priv,
1866 				  struct v4l2_format *f)
1867 {
1868 	int ret;
1869 
1870 	ret = mxc_jpeg_try_fmt_vid_out(file, priv, f);
1871 	if (ret)
1872 		return ret;
1873 
1874 	return mxc_jpeg_s_fmt(mxc_jpeg_fh_to_ctx(priv), f);
1875 }
1876 
mxc_jpeg_g_fmt_vid(struct file * file,void * priv,struct v4l2_format * f)1877 static int mxc_jpeg_g_fmt_vid(struct file *file, void *priv,
1878 			      struct v4l2_format *f)
1879 {
1880 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(priv);
1881 	struct mxc_jpeg_dev *jpeg = ctx->mxc_jpeg;
1882 	struct device *dev = jpeg->dev;
1883 	struct v4l2_pix_format_mplane   *pix_mp = &f->fmt.pix_mp;
1884 	struct mxc_jpeg_q_data *q_data = mxc_jpeg_get_q_data(ctx, f->type);
1885 	int i;
1886 
1887 	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) {
1888 		dev_err(dev, "G_FMT with Invalid type: %d\n", f->type);
1889 		return -EINVAL;
1890 	}
1891 
1892 	pix_mp->pixelformat = q_data->fmt->fourcc;
1893 	pix_mp->width = q_data->w;
1894 	pix_mp->height = q_data->h;
1895 	pix_mp->field = V4L2_FIELD_NONE;
1896 
1897 	/* fix colorspace information to sRGB for both output & capture */
1898 	pix_mp->colorspace = V4L2_COLORSPACE_SRGB;
1899 	pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_601;
1900 	pix_mp->xfer_func = V4L2_XFER_FUNC_SRGB;
1901 	pix_mp->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1902 
1903 	pix_mp->num_planes = q_data->fmt->colplanes;
1904 	for (i = 0; i < pix_mp->num_planes; i++) {
1905 		pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i];
1906 		pix_mp->plane_fmt[i].sizeimage = q_data->sizeimage[i];
1907 	}
1908 
1909 	return 0;
1910 }
1911 
mxc_jpeg_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)1912 static int mxc_jpeg_subscribe_event(struct v4l2_fh *fh,
1913 				    const struct v4l2_event_subscription *sub)
1914 {
1915 	switch (sub->type) {
1916 	case V4L2_EVENT_EOS:
1917 		return v4l2_event_subscribe(fh, sub, 0, NULL);
1918 	case V4L2_EVENT_SOURCE_CHANGE:
1919 		return v4l2_src_change_event_subscribe(fh, sub);
1920 	default:
1921 		return -EINVAL;
1922 	}
1923 }
1924 
1925 static const struct v4l2_ioctl_ops mxc_jpeg_ioctl_ops = {
1926 	.vidioc_querycap		= mxc_jpeg_querycap,
1927 	.vidioc_enum_fmt_vid_cap	= mxc_jpeg_enum_fmt_vid_cap,
1928 	.vidioc_enum_fmt_vid_out	= mxc_jpeg_enum_fmt_vid_out,
1929 
1930 	.vidioc_try_fmt_vid_cap_mplane	= mxc_jpeg_try_fmt_vid_cap,
1931 	.vidioc_try_fmt_vid_out_mplane	= mxc_jpeg_try_fmt_vid_out,
1932 
1933 	.vidioc_s_fmt_vid_cap_mplane	= mxc_jpeg_s_fmt_vid_cap,
1934 	.vidioc_s_fmt_vid_out_mplane	= mxc_jpeg_s_fmt_vid_out,
1935 
1936 	.vidioc_g_fmt_vid_cap_mplane	= mxc_jpeg_g_fmt_vid,
1937 	.vidioc_g_fmt_vid_out_mplane	= mxc_jpeg_g_fmt_vid,
1938 
1939 	.vidioc_subscribe_event		= mxc_jpeg_subscribe_event,
1940 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
1941 
1942 	.vidioc_try_decoder_cmd		= v4l2_m2m_ioctl_try_decoder_cmd,
1943 	.vidioc_decoder_cmd		= mxc_jpeg_decoder_cmd,
1944 	.vidioc_try_encoder_cmd		= v4l2_m2m_ioctl_try_encoder_cmd,
1945 	.vidioc_encoder_cmd		= mxc_jpeg_encoder_cmd,
1946 
1947 	.vidioc_qbuf			= v4l2_m2m_ioctl_qbuf,
1948 	.vidioc_dqbuf			= v4l2_m2m_ioctl_dqbuf,
1949 
1950 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
1951 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
1952 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
1953 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
1954 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
1955 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
1956 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
1957 };
1958 
mxc_jpeg_release(struct file * file)1959 static int mxc_jpeg_release(struct file *file)
1960 {
1961 	struct mxc_jpeg_dev *mxc_jpeg = video_drvdata(file);
1962 	struct mxc_jpeg_ctx *ctx = mxc_jpeg_fh_to_ctx(file->private_data);
1963 	struct device *dev = mxc_jpeg->dev;
1964 
1965 	mutex_lock(&mxc_jpeg->lock);
1966 	if (mxc_jpeg->mode == MXC_JPEG_DECODE)
1967 		dev_dbg(dev, "Release JPEG decoder instance on slot %d.",
1968 			ctx->slot);
1969 	else
1970 		dev_dbg(dev, "Release JPEG encoder instance on slot %d.",
1971 			ctx->slot);
1972 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1973 	v4l2_fh_del(&ctx->fh);
1974 	v4l2_fh_exit(&ctx->fh);
1975 	kfree(ctx);
1976 	mutex_unlock(&mxc_jpeg->lock);
1977 
1978 	return 0;
1979 }
1980 
1981 static const struct v4l2_file_operations mxc_jpeg_fops = {
1982 	.owner		= THIS_MODULE,
1983 	.open		= mxc_jpeg_open,
1984 	.release	= mxc_jpeg_release,
1985 	.poll		= v4l2_m2m_fop_poll,
1986 	.unlocked_ioctl	= video_ioctl2,
1987 	.mmap		= v4l2_m2m_fop_mmap,
1988 };
1989 
1990 static const struct v4l2_m2m_ops mxc_jpeg_m2m_ops = {
1991 	.job_ready      = mxc_jpeg_job_ready,
1992 	.device_run	= mxc_jpeg_device_run,
1993 };
1994 
mxc_jpeg_detach_pm_domains(struct mxc_jpeg_dev * jpeg)1995 static void mxc_jpeg_detach_pm_domains(struct mxc_jpeg_dev *jpeg)
1996 {
1997 	int i;
1998 
1999 	for (i = 0; i < jpeg->num_domains; i++) {
2000 		if (jpeg->pd_link[i] && !IS_ERR(jpeg->pd_link[i]))
2001 			device_link_del(jpeg->pd_link[i]);
2002 		if (jpeg->pd_dev[i] && !IS_ERR(jpeg->pd_dev[i]))
2003 			dev_pm_domain_detach(jpeg->pd_dev[i], true);
2004 		jpeg->pd_dev[i] = NULL;
2005 		jpeg->pd_link[i] = NULL;
2006 	}
2007 }
2008 
mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev * jpeg)2009 static int mxc_jpeg_attach_pm_domains(struct mxc_jpeg_dev *jpeg)
2010 {
2011 	struct device *dev = jpeg->dev;
2012 	struct device_node *np = jpeg->pdev->dev.of_node;
2013 	int i;
2014 	int ret;
2015 
2016 	jpeg->num_domains = of_count_phandle_with_args(np, "power-domains",
2017 						       "#power-domain-cells");
2018 	if (jpeg->num_domains < 0) {
2019 		dev_err(dev, "No power domains defined for jpeg node\n");
2020 		return jpeg->num_domains;
2021 	}
2022 
2023 	jpeg->pd_dev = devm_kmalloc_array(dev, jpeg->num_domains,
2024 					  sizeof(*jpeg->pd_dev), GFP_KERNEL);
2025 	if (!jpeg->pd_dev)
2026 		return -ENOMEM;
2027 
2028 	jpeg->pd_link = devm_kmalloc_array(dev, jpeg->num_domains,
2029 					   sizeof(*jpeg->pd_link), GFP_KERNEL);
2030 	if (!jpeg->pd_link)
2031 		return -ENOMEM;
2032 
2033 	for (i = 0; i < jpeg->num_domains; i++) {
2034 		jpeg->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i);
2035 		if (IS_ERR(jpeg->pd_dev[i])) {
2036 			ret = PTR_ERR(jpeg->pd_dev[i]);
2037 			goto fail;
2038 		}
2039 
2040 		jpeg->pd_link[i] = device_link_add(dev, jpeg->pd_dev[i],
2041 						   DL_FLAG_STATELESS |
2042 						   DL_FLAG_PM_RUNTIME);
2043 		if (!jpeg->pd_link[i]) {
2044 			ret = -EINVAL;
2045 			goto fail;
2046 		}
2047 	}
2048 
2049 	return 0;
2050 fail:
2051 	mxc_jpeg_detach_pm_domains(jpeg);
2052 	return ret;
2053 }
2054 
mxc_jpeg_probe(struct platform_device * pdev)2055 static int mxc_jpeg_probe(struct platform_device *pdev)
2056 {
2057 	struct mxc_jpeg_dev *jpeg;
2058 	struct device *dev = &pdev->dev;
2059 	struct resource *res;
2060 	int dec_irq;
2061 	int ret;
2062 	int mode;
2063 	const struct of_device_id *of_id;
2064 	unsigned int slot;
2065 
2066 	of_id = of_match_node(mxc_jpeg_match, dev->of_node);
2067 	mode = *(const int *)of_id->data;
2068 
2069 	jpeg = devm_kzalloc(dev, sizeof(struct mxc_jpeg_dev), GFP_KERNEL);
2070 	if (!jpeg)
2071 		return -ENOMEM;
2072 
2073 	mutex_init(&jpeg->lock);
2074 	spin_lock_init(&jpeg->hw_lock);
2075 
2076 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2077 	if (ret) {
2078 		dev_err(&pdev->dev, "No suitable DMA available.\n");
2079 		goto err_irq;
2080 	}
2081 
2082 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2083 	jpeg->base_reg = devm_ioremap_resource(&pdev->dev, res);
2084 	if (IS_ERR(jpeg->base_reg))
2085 		return PTR_ERR(jpeg->base_reg);
2086 
2087 	for (slot = 0; slot < MXC_MAX_SLOTS; slot++) {
2088 		dec_irq = platform_get_irq(pdev, slot);
2089 		if (dec_irq < 0) {
2090 			dev_err(&pdev->dev, "Failed to get irq %d\n", dec_irq);
2091 			ret = dec_irq;
2092 			goto err_irq;
2093 		}
2094 		ret = devm_request_irq(&pdev->dev, dec_irq, mxc_jpeg_dec_irq,
2095 				       0, pdev->name, jpeg);
2096 		if (ret) {
2097 			dev_err(&pdev->dev, "Failed to request irq %d (%d)\n",
2098 				dec_irq, ret);
2099 			goto err_irq;
2100 		}
2101 	}
2102 
2103 	jpeg->pdev = pdev;
2104 	jpeg->dev = dev;
2105 	jpeg->mode = mode;
2106 
2107 	/* Get clocks */
2108 	ret = devm_clk_bulk_get_all(&pdev->dev, &jpeg->clks);
2109 	if (ret < 0) {
2110 		dev_err(dev, "failed to get clock\n");
2111 		goto err_clk;
2112 	}
2113 	jpeg->num_clks = ret;
2114 
2115 	ret = mxc_jpeg_attach_pm_domains(jpeg);
2116 	if (ret < 0) {
2117 		dev_err(dev, "failed to attach power domains %d\n", ret);
2118 		return ret;
2119 	}
2120 
2121 	/* v4l2 */
2122 	ret = v4l2_device_register(dev, &jpeg->v4l2_dev);
2123 	if (ret) {
2124 		dev_err(dev, "failed to register v4l2 device\n");
2125 		goto err_register;
2126 	}
2127 	jpeg->m2m_dev = v4l2_m2m_init(&mxc_jpeg_m2m_ops);
2128 	if (IS_ERR(jpeg->m2m_dev)) {
2129 		dev_err(dev, "failed to register v4l2 device\n");
2130 		ret = PTR_ERR(jpeg->m2m_dev);
2131 		goto err_m2m;
2132 	}
2133 
2134 	jpeg->dec_vdev = video_device_alloc();
2135 	if (!jpeg->dec_vdev) {
2136 		dev_err(dev, "failed to register v4l2 device\n");
2137 		ret = -ENOMEM;
2138 		goto err_vdev_alloc;
2139 	}
2140 	if (mode == MXC_JPEG_ENCODE)
2141 		snprintf(jpeg->dec_vdev->name,
2142 			 sizeof(jpeg->dec_vdev->name),
2143 			 "%s-enc", MXC_JPEG_NAME);
2144 	else
2145 		snprintf(jpeg->dec_vdev->name,
2146 			 sizeof(jpeg->dec_vdev->name),
2147 			 "%s-dec", MXC_JPEG_NAME);
2148 
2149 	jpeg->dec_vdev->fops = &mxc_jpeg_fops;
2150 	jpeg->dec_vdev->ioctl_ops = &mxc_jpeg_ioctl_ops;
2151 	jpeg->dec_vdev->minor = -1;
2152 	jpeg->dec_vdev->release = video_device_release;
2153 	jpeg->dec_vdev->lock = &jpeg->lock; /* lock for ioctl serialization */
2154 	jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
2155 	jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
2156 	jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
2157 					V4L2_CAP_VIDEO_M2M_MPLANE;
2158 	if (mode == MXC_JPEG_ENCODE) {
2159 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_DECODER_CMD);
2160 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_DECODER_CMD);
2161 	} else {
2162 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_ENCODER_CMD);
2163 		v4l2_disable_ioctl(jpeg->dec_vdev, VIDIOC_TRY_ENCODER_CMD);
2164 	}
2165 	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_VIDEO, -1);
2166 	if (ret) {
2167 		dev_err(dev, "failed to register video device\n");
2168 		goto err_vdev_register;
2169 	}
2170 	video_set_drvdata(jpeg->dec_vdev, jpeg);
2171 	if (mode == MXC_JPEG_ENCODE)
2172 		v4l2_info(&jpeg->v4l2_dev,
2173 			  "encoder device registered as /dev/video%d (%d,%d)\n",
2174 			  jpeg->dec_vdev->num, VIDEO_MAJOR,
2175 			  jpeg->dec_vdev->minor);
2176 	else
2177 		v4l2_info(&jpeg->v4l2_dev,
2178 			  "decoder device registered as /dev/video%d (%d,%d)\n",
2179 			  jpeg->dec_vdev->num, VIDEO_MAJOR,
2180 			  jpeg->dec_vdev->minor);
2181 
2182 	platform_set_drvdata(pdev, jpeg);
2183 	pm_runtime_enable(dev);
2184 
2185 	return 0;
2186 
2187 err_vdev_register:
2188 	video_device_release(jpeg->dec_vdev);
2189 
2190 err_vdev_alloc:
2191 	v4l2_m2m_release(jpeg->m2m_dev);
2192 
2193 err_m2m:
2194 	v4l2_device_unregister(&jpeg->v4l2_dev);
2195 
2196 err_register:
2197 	mxc_jpeg_detach_pm_domains(jpeg);
2198 
2199 err_irq:
2200 err_clk:
2201 	return ret;
2202 }
2203 
2204 #ifdef CONFIG_PM
mxc_jpeg_runtime_resume(struct device * dev)2205 static int mxc_jpeg_runtime_resume(struct device *dev)
2206 {
2207 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2208 	int ret;
2209 
2210 	ret = clk_bulk_prepare_enable(jpeg->num_clks, jpeg->clks);
2211 	if (ret < 0) {
2212 		dev_err(dev, "failed to enable clock\n");
2213 		return ret;
2214 	}
2215 
2216 	return 0;
2217 }
2218 
mxc_jpeg_runtime_suspend(struct device * dev)2219 static int mxc_jpeg_runtime_suspend(struct device *dev)
2220 {
2221 	struct mxc_jpeg_dev *jpeg = dev_get_drvdata(dev);
2222 
2223 	clk_bulk_disable_unprepare(jpeg->num_clks, jpeg->clks);
2224 
2225 	return 0;
2226 }
2227 #endif
2228 
2229 static const struct dev_pm_ops	mxc_jpeg_pm_ops = {
2230 	SET_RUNTIME_PM_OPS(mxc_jpeg_runtime_suspend,
2231 			   mxc_jpeg_runtime_resume, NULL)
2232 };
2233 
mxc_jpeg_remove(struct platform_device * pdev)2234 static int mxc_jpeg_remove(struct platform_device *pdev)
2235 {
2236 	unsigned int slot;
2237 	struct mxc_jpeg_dev *jpeg = platform_get_drvdata(pdev);
2238 
2239 	for (slot = 0; slot < MXC_MAX_SLOTS; slot++)
2240 		mxc_jpeg_free_slot_data(jpeg, slot);
2241 
2242 	pm_runtime_disable(&pdev->dev);
2243 	video_unregister_device(jpeg->dec_vdev);
2244 	v4l2_m2m_release(jpeg->m2m_dev);
2245 	v4l2_device_unregister(&jpeg->v4l2_dev);
2246 	mxc_jpeg_detach_pm_domains(jpeg);
2247 
2248 	return 0;
2249 }
2250 
2251 MODULE_DEVICE_TABLE(of, mxc_jpeg_match);
2252 
2253 static struct platform_driver mxc_jpeg_driver = {
2254 	.probe = mxc_jpeg_probe,
2255 	.remove = mxc_jpeg_remove,
2256 	.driver = {
2257 		.name = "mxc-jpeg",
2258 		.of_match_table = mxc_jpeg_match,
2259 		.pm = &mxc_jpeg_pm_ops,
2260 	},
2261 };
2262 module_platform_driver(mxc_jpeg_driver);
2263 
2264 MODULE_AUTHOR("Zhengyu Shen <zhengyu.shen_1@nxp.com>");
2265 MODULE_AUTHOR("Mirela Rabulea <mirela.rabulea@nxp.com>");
2266 MODULE_DESCRIPTION("V4L2 driver for i.MX8 QXP/QM JPEG encoder/decoder");
2267 MODULE_LICENSE("GPL v2");
2268