1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016 MediaTek Inc.
4  * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
5  *         Rick Chang <rick.chang@mediatek.com>
6  *         Xia Jiang <xia.jiang@mediatek.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/err.h>
11 #include <linux/interrupt.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <media/v4l2-event.h>
21 #include <media/v4l2-mem2mem.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/videobuf2-core.h>
24 #include <media/videobuf2-dma-contig.h>
25 
26 #include "mtk_jpeg_enc_hw.h"
27 #include "mtk_jpeg_dec_hw.h"
28 #include "mtk_jpeg_core.h"
29 #include "mtk_jpeg_dec_parse.h"
30 
31 static struct mtk_jpeg_fmt mtk_jpeg_enc_formats[] = {
32 	{
33 		.fourcc		= V4L2_PIX_FMT_JPEG,
34 		.colplanes	= 1,
35 		.flags		= MTK_JPEG_FMT_FLAG_CAPTURE,
36 	},
37 	{
38 		.fourcc		= V4L2_PIX_FMT_NV12M,
39 		.hw_format	= JPEG_ENC_YUV_FORMAT_NV12,
40 		.h_sample	= {4, 4},
41 		.v_sample	= {4, 2},
42 		.colplanes	= 2,
43 		.h_align	= 4,
44 		.v_align	= 4,
45 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
46 	},
47 	{
48 		.fourcc		= V4L2_PIX_FMT_NV21M,
49 		.hw_format	= JEPG_ENC_YUV_FORMAT_NV21,
50 		.h_sample	= {4, 4},
51 		.v_sample	= {4, 2},
52 		.colplanes	= 2,
53 		.h_align	= 4,
54 		.v_align	= 4,
55 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
56 	},
57 	{
58 		.fourcc		= V4L2_PIX_FMT_YUYV,
59 		.hw_format	= JPEG_ENC_YUV_FORMAT_YUYV,
60 		.h_sample	= {8},
61 		.v_sample	= {4},
62 		.colplanes	= 1,
63 		.h_align	= 5,
64 		.v_align	= 3,
65 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
66 	},
67 	{
68 		.fourcc		= V4L2_PIX_FMT_YVYU,
69 		.hw_format	= JPEG_ENC_YUV_FORMAT_YVYU,
70 		.h_sample	= {8},
71 		.v_sample	= {4},
72 		.colplanes	= 1,
73 		.h_align	= 5,
74 		.v_align	= 3,
75 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
76 	},
77 };
78 
79 static struct mtk_jpeg_fmt mtk_jpeg_dec_formats[] = {
80 	{
81 		.fourcc		= V4L2_PIX_FMT_JPEG,
82 		.colplanes	= 1,
83 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
84 	},
85 	{
86 		.fourcc		= V4L2_PIX_FMT_YUV420M,
87 		.h_sample	= {4, 2, 2},
88 		.v_sample	= {4, 2, 2},
89 		.colplanes	= 3,
90 		.h_align	= 5,
91 		.v_align	= 4,
92 		.flags		= MTK_JPEG_FMT_FLAG_CAPTURE,
93 	},
94 	{
95 		.fourcc		= V4L2_PIX_FMT_YUV422M,
96 		.h_sample	= {4, 2, 2},
97 		.v_sample	= {4, 4, 4},
98 		.colplanes	= 3,
99 		.h_align	= 5,
100 		.v_align	= 3,
101 		.flags		= MTK_JPEG_FMT_FLAG_CAPTURE,
102 	},
103 };
104 
105 #define MTK_JPEG_ENC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_enc_formats)
106 #define MTK_JPEG_DEC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_dec_formats)
107 
108 struct mtk_jpeg_src_buf {
109 	struct vb2_v4l2_buffer b;
110 	struct list_head list;
111 	struct mtk_jpeg_dec_param dec_param;
112 };
113 
114 static int debug;
115 module_param(debug, int, 0644);
116 
117 static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
118 {
119 	return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl);
120 }
121 
122 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
123 {
124 	return container_of(fh, struct mtk_jpeg_ctx, fh);
125 }
126 
127 static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf(
128 							struct vb2_buffer *vb)
129 {
130 	return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b);
131 }
132 
133 static int mtk_jpeg_querycap(struct file *file, void *priv,
134 			     struct v4l2_capability *cap)
135 {
136 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
137 
138 	strscpy(cap->driver, jpeg->variant->dev_name, sizeof(cap->driver));
139 	strscpy(cap->card, jpeg->variant->dev_name, sizeof(cap->card));
140 
141 	return 0;
142 }
143 
144 static int vidioc_jpeg_enc_s_ctrl(struct v4l2_ctrl *ctrl)
145 {
146 	struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
147 
148 	switch (ctrl->id) {
149 	case V4L2_CID_JPEG_RESTART_INTERVAL:
150 		ctx->restart_interval = ctrl->val;
151 		break;
152 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
153 		ctx->enc_quality = ctrl->val;
154 		break;
155 	case V4L2_CID_JPEG_ACTIVE_MARKER:
156 		ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1;
157 		break;
158 	}
159 
160 	return 0;
161 }
162 
163 static const struct v4l2_ctrl_ops mtk_jpeg_enc_ctrl_ops = {
164 	.s_ctrl = vidioc_jpeg_enc_s_ctrl,
165 };
166 
167 static int mtk_jpeg_enc_ctrls_setup(struct mtk_jpeg_ctx *ctx)
168 {
169 	const struct v4l2_ctrl_ops *ops = &mtk_jpeg_enc_ctrl_ops;
170 	struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
171 
172 	v4l2_ctrl_handler_init(handler, 3);
173 
174 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100,
175 			  1, 0);
176 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_COMPRESSION_QUALITY, 48,
177 			  100, 1, 90);
178 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0,
179 			  V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0);
180 
181 	if (handler->error) {
182 		v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
183 		return handler->error;
184 	}
185 
186 	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
187 
188 	return 0;
189 }
190 
191 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
192 			     struct v4l2_fmtdesc *f, u32 type)
193 {
194 	int i, num = 0;
195 
196 	for (i = 0; i < n; ++i) {
197 		if (mtk_jpeg_formats[i].flags & type) {
198 			if (num == f->index)
199 				break;
200 			++num;
201 		}
202 	}
203 
204 	if (i >= n)
205 		return -EINVAL;
206 
207 	f->pixelformat = mtk_jpeg_formats[i].fourcc;
208 
209 	return 0;
210 }
211 
212 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
213 				     struct v4l2_fmtdesc *f)
214 {
215 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
216 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
217 
218 	return mtk_jpeg_enum_fmt(jpeg->variant->formats,
219 				 jpeg->variant->num_formats, f,
220 				 MTK_JPEG_FMT_FLAG_CAPTURE);
221 }
222 
223 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
224 				     struct v4l2_fmtdesc *f)
225 {
226 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
227 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
228 
229 	return mtk_jpeg_enum_fmt(jpeg->variant->formats,
230 				 jpeg->variant->num_formats, f,
231 				 MTK_JPEG_FMT_FLAG_OUTPUT);
232 }
233 
234 static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
235 						   enum v4l2_buf_type type)
236 {
237 	if (V4L2_TYPE_IS_OUTPUT(type))
238 		return &ctx->out_q;
239 	return &ctx->cap_q;
240 }
241 
242 static struct mtk_jpeg_fmt *
243 mtk_jpeg_find_format(struct mtk_jpeg_fmt *mtk_jpeg_formats, int num_formats,
244 		     u32 pixelformat, unsigned int fmt_type)
245 {
246 	unsigned int k;
247 	struct mtk_jpeg_fmt *fmt;
248 
249 	for (k = 0; k < num_formats; k++) {
250 		fmt = &mtk_jpeg_formats[k];
251 
252 		if (fmt->fourcc == pixelformat && fmt->flags & fmt_type)
253 			return fmt;
254 	}
255 
256 	return NULL;
257 }
258 
259 static int mtk_jpeg_try_fmt_mplane(struct v4l2_pix_format_mplane *pix_mp,
260 				   struct mtk_jpeg_fmt *fmt)
261 {
262 	int i;
263 
264 	pix_mp->field = V4L2_FIELD_NONE;
265 
266 	pix_mp->num_planes = fmt->colplanes;
267 	pix_mp->pixelformat = fmt->fourcc;
268 
269 	if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
270 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
271 
272 		pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT,
273 				       MTK_JPEG_MAX_HEIGHT);
274 		pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH,
275 				      MTK_JPEG_MAX_WIDTH);
276 
277 		pfmt->bytesperline = 0;
278 		/* Source size must be aligned to 128 */
279 		pfmt->sizeimage = round_up(pfmt->sizeimage, 128);
280 		if (pfmt->sizeimage == 0)
281 			pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE;
282 		return 0;
283 	}
284 
285 	/* other fourcc */
286 	pix_mp->height = clamp(round_up(pix_mp->height, fmt->v_align),
287 			       MTK_JPEG_MIN_HEIGHT, MTK_JPEG_MAX_HEIGHT);
288 	pix_mp->width = clamp(round_up(pix_mp->width, fmt->h_align),
289 			      MTK_JPEG_MIN_WIDTH, MTK_JPEG_MAX_WIDTH);
290 
291 	for (i = 0; i < fmt->colplanes; i++) {
292 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
293 		u32 stride = pix_mp->width * fmt->h_sample[i] / 4;
294 		u32 h = pix_mp->height * fmt->v_sample[i] / 4;
295 
296 		pfmt->bytesperline = stride;
297 		pfmt->sizeimage = stride * h;
298 	}
299 	return 0;
300 }
301 
302 static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv,
303 				     struct v4l2_format *f)
304 {
305 	struct vb2_queue *vq;
306 	struct mtk_jpeg_q_data *q_data = NULL;
307 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
308 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
309 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
310 	int i;
311 
312 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
313 	if (!vq)
314 		return -EINVAL;
315 
316 	q_data = mtk_jpeg_get_q_data(ctx, f->type);
317 
318 	pix_mp->width = q_data->pix_mp.width;
319 	pix_mp->height = q_data->pix_mp.height;
320 	pix_mp->field = V4L2_FIELD_NONE;
321 	pix_mp->pixelformat = q_data->fmt->fourcc;
322 	pix_mp->num_planes = q_data->fmt->colplanes;
323 	pix_mp->colorspace = q_data->pix_mp.colorspace;
324 	pix_mp->ycbcr_enc = q_data->pix_mp.ycbcr_enc;
325 	pix_mp->xfer_func = q_data->pix_mp.xfer_func;
326 	pix_mp->quantization = q_data->pix_mp.quantization;
327 
328 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n",
329 		 f->type,
330 		 (pix_mp->pixelformat & 0xff),
331 		 (pix_mp->pixelformat >>  8 & 0xff),
332 		 (pix_mp->pixelformat >> 16 & 0xff),
333 		 (pix_mp->pixelformat >> 24 & 0xff),
334 		 pix_mp->width, pix_mp->height);
335 
336 	for (i = 0; i < pix_mp->num_planes; i++) {
337 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
338 
339 		pfmt->bytesperline = q_data->pix_mp.plane_fmt[i].bytesperline;
340 		pfmt->sizeimage = q_data->pix_mp.plane_fmt[i].sizeimage;
341 
342 		v4l2_dbg(1, debug, &jpeg->v4l2_dev,
343 			 "plane[%d] bpl=%u, size=%u\n",
344 			 i,
345 			 pfmt->bytesperline,
346 			 pfmt->sizeimage);
347 	}
348 	return 0;
349 }
350 
351 static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
352 					   struct v4l2_format *f)
353 {
354 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
355 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
356 	struct mtk_jpeg_fmt *fmt;
357 
358 	fmt = mtk_jpeg_find_format(jpeg->variant->formats,
359 				   jpeg->variant->num_formats,
360 				   f->fmt.pix_mp.pixelformat,
361 				   MTK_JPEG_FMT_FLAG_CAPTURE);
362 	if (!fmt)
363 		fmt = ctx->cap_q.fmt;
364 
365 	v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
366 		 f->type,
367 		 (fmt->fourcc & 0xff),
368 		 (fmt->fourcc >>  8 & 0xff),
369 		 (fmt->fourcc >> 16 & 0xff),
370 		 (fmt->fourcc >> 24 & 0xff));
371 
372 	if (ctx->state != MTK_JPEG_INIT) {
373 		mtk_jpeg_g_fmt_vid_mplane(file, priv, f);
374 		return 0;
375 	}
376 
377 	return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt);
378 }
379 
380 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
381 					   struct v4l2_format *f)
382 {
383 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
384 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
385 	struct mtk_jpeg_fmt *fmt;
386 
387 	fmt = mtk_jpeg_find_format(jpeg->variant->formats,
388 				   jpeg->variant->num_formats,
389 				   f->fmt.pix_mp.pixelformat,
390 				   MTK_JPEG_FMT_FLAG_OUTPUT);
391 	if (!fmt)
392 		fmt = ctx->out_q.fmt;
393 
394 	v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
395 		 f->type,
396 		 (fmt->fourcc & 0xff),
397 		 (fmt->fourcc >>  8 & 0xff),
398 		 (fmt->fourcc >> 16 & 0xff),
399 		 (fmt->fourcc >> 24 & 0xff));
400 
401 	if (ctx->state != MTK_JPEG_INIT) {
402 		mtk_jpeg_g_fmt_vid_mplane(file, priv, f);
403 		return 0;
404 	}
405 
406 	return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt);
407 }
408 
409 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
410 				 struct v4l2_format *f, unsigned int fmt_type)
411 {
412 	struct vb2_queue *vq;
413 	struct mtk_jpeg_q_data *q_data = NULL;
414 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
415 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
416 	int i;
417 
418 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
419 	if (!vq)
420 		return -EINVAL;
421 
422 	q_data = mtk_jpeg_get_q_data(ctx, f->type);
423 
424 	if (vb2_is_busy(vq)) {
425 		v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
426 		return -EBUSY;
427 	}
428 
429 	q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
430 					   jpeg->variant->num_formats,
431 					   pix_mp->pixelformat, fmt_type);
432 	q_data->pix_mp.width = pix_mp->width;
433 	q_data->pix_mp.height = pix_mp->height;
434 	q_data->enc_crop_rect.width = pix_mp->width;
435 	q_data->enc_crop_rect.height = pix_mp->height;
436 	q_data->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
437 	q_data->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
438 	q_data->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
439 	q_data->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
440 
441 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n",
442 		 f->type,
443 		 (q_data->fmt->fourcc & 0xff),
444 		 (q_data->fmt->fourcc >>  8 & 0xff),
445 		 (q_data->fmt->fourcc >> 16 & 0xff),
446 		 (q_data->fmt->fourcc >> 24 & 0xff),
447 		 q_data->pix_mp.width, q_data->pix_mp.height);
448 
449 	for (i = 0; i < q_data->fmt->colplanes; i++) {
450 		q_data->pix_mp.plane_fmt[i].bytesperline =
451 					pix_mp->plane_fmt[i].bytesperline;
452 		q_data->pix_mp.plane_fmt[i].sizeimage =
453 					pix_mp->plane_fmt[i].sizeimage;
454 
455 		v4l2_dbg(1, debug, &jpeg->v4l2_dev,
456 			 "plane[%d] bpl=%u, size=%u\n",
457 			 i, q_data->pix_mp.plane_fmt[i].bytesperline,
458 			 q_data->pix_mp.plane_fmt[i].sizeimage);
459 	}
460 
461 	return 0;
462 }
463 
464 static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv,
465 					 struct v4l2_format *f)
466 {
467 	int ret;
468 
469 	ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f);
470 	if (ret)
471 		return ret;
472 
473 	return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f,
474 				     MTK_JPEG_FMT_FLAG_OUTPUT);
475 }
476 
477 static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv,
478 					 struct v4l2_format *f)
479 {
480 	int ret;
481 
482 	ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f);
483 	if (ret)
484 		return ret;
485 
486 	return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f,
487 				     MTK_JPEG_FMT_FLAG_CAPTURE);
488 }
489 
490 static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx)
491 {
492 	static const struct v4l2_event ev_src_ch = {
493 		.type = V4L2_EVENT_SOURCE_CHANGE,
494 		.u.src_change.changes =
495 		V4L2_EVENT_SRC_CH_RESOLUTION,
496 	};
497 
498 	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
499 }
500 
501 static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
502 				    const struct v4l2_event_subscription *sub)
503 {
504 	switch (sub->type) {
505 	case V4L2_EVENT_SOURCE_CHANGE:
506 		return v4l2_src_change_event_subscribe(fh, sub);
507 	}
508 
509 	return v4l2_ctrl_subscribe_event(fh, sub);
510 }
511 
512 static int mtk_jpeg_enc_g_selection(struct file *file, void *priv,
513 				    struct v4l2_selection *s)
514 {
515 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
516 
517 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
518 		return -EINVAL;
519 
520 	switch (s->target) {
521 	case V4L2_SEL_TGT_CROP:
522 		s->r = ctx->out_q.enc_crop_rect;
523 		break;
524 	case V4L2_SEL_TGT_CROP_BOUNDS:
525 	case V4L2_SEL_TGT_CROP_DEFAULT:
526 		s->r.width = ctx->out_q.pix_mp.width;
527 		s->r.height = ctx->out_q.pix_mp.height;
528 		s->r.left = 0;
529 		s->r.top = 0;
530 		break;
531 	default:
532 		return -EINVAL;
533 	}
534 	return 0;
535 }
536 
537 static int mtk_jpeg_dec_g_selection(struct file *file, void *priv,
538 				    struct v4l2_selection *s)
539 {
540 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
541 
542 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
543 		return -EINVAL;
544 
545 	switch (s->target) {
546 	case V4L2_SEL_TGT_COMPOSE:
547 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
548 		s->r.width = ctx->out_q.pix_mp.width;
549 		s->r.height = ctx->out_q.pix_mp.height;
550 		s->r.left = 0;
551 		s->r.top = 0;
552 		break;
553 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
554 	case V4L2_SEL_TGT_COMPOSE_PADDED:
555 		s->r.width = ctx->cap_q.pix_mp.width;
556 		s->r.height = ctx->cap_q.pix_mp.height;
557 		s->r.left = 0;
558 		s->r.top = 0;
559 		break;
560 	default:
561 		return -EINVAL;
562 	}
563 	return 0;
564 }
565 
566 static int mtk_jpeg_enc_s_selection(struct file *file, void *priv,
567 				    struct v4l2_selection *s)
568 {
569 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
570 
571 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
572 		return -EINVAL;
573 
574 	switch (s->target) {
575 	case V4L2_SEL_TGT_CROP:
576 		s->r.left = 0;
577 		s->r.top = 0;
578 		s->r.width = min(s->r.width, ctx->out_q.pix_mp.width);
579 		s->r.height = min(s->r.height, ctx->out_q.pix_mp.height);
580 		ctx->out_q.enc_crop_rect = s->r;
581 		break;
582 	default:
583 		return -EINVAL;
584 	}
585 
586 	return 0;
587 }
588 
589 static const struct v4l2_ioctl_ops mtk_jpeg_enc_ioctl_ops = {
590 	.vidioc_querycap                = mtk_jpeg_querycap,
591 	.vidioc_enum_fmt_vid_cap	= mtk_jpeg_enum_fmt_vid_cap,
592 	.vidioc_enum_fmt_vid_out	= mtk_jpeg_enum_fmt_vid_out,
593 	.vidioc_try_fmt_vid_cap_mplane	= mtk_jpeg_try_fmt_vid_cap_mplane,
594 	.vidioc_try_fmt_vid_out_mplane	= mtk_jpeg_try_fmt_vid_out_mplane,
595 	.vidioc_g_fmt_vid_cap_mplane    = mtk_jpeg_g_fmt_vid_mplane,
596 	.vidioc_g_fmt_vid_out_mplane    = mtk_jpeg_g_fmt_vid_mplane,
597 	.vidioc_s_fmt_vid_cap_mplane    = mtk_jpeg_s_fmt_vid_cap_mplane,
598 	.vidioc_s_fmt_vid_out_mplane    = mtk_jpeg_s_fmt_vid_out_mplane,
599 	.vidioc_qbuf                    = v4l2_m2m_ioctl_qbuf,
600 	.vidioc_subscribe_event         = mtk_jpeg_subscribe_event,
601 	.vidioc_g_selection		= mtk_jpeg_enc_g_selection,
602 	.vidioc_s_selection		= mtk_jpeg_enc_s_selection,
603 
604 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
605 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
606 	.vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
607 	.vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
608 	.vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
609 	.vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
610 	.vidioc_streamon                = v4l2_m2m_ioctl_streamon,
611 	.vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
612 
613 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
614 };
615 
616 static const struct v4l2_ioctl_ops mtk_jpeg_dec_ioctl_ops = {
617 	.vidioc_querycap                = mtk_jpeg_querycap,
618 	.vidioc_enum_fmt_vid_cap	= mtk_jpeg_enum_fmt_vid_cap,
619 	.vidioc_enum_fmt_vid_out	= mtk_jpeg_enum_fmt_vid_out,
620 	.vidioc_try_fmt_vid_cap_mplane	= mtk_jpeg_try_fmt_vid_cap_mplane,
621 	.vidioc_try_fmt_vid_out_mplane	= mtk_jpeg_try_fmt_vid_out_mplane,
622 	.vidioc_g_fmt_vid_cap_mplane    = mtk_jpeg_g_fmt_vid_mplane,
623 	.vidioc_g_fmt_vid_out_mplane    = mtk_jpeg_g_fmt_vid_mplane,
624 	.vidioc_s_fmt_vid_cap_mplane    = mtk_jpeg_s_fmt_vid_cap_mplane,
625 	.vidioc_s_fmt_vid_out_mplane    = mtk_jpeg_s_fmt_vid_out_mplane,
626 	.vidioc_qbuf                    = v4l2_m2m_ioctl_qbuf,
627 	.vidioc_subscribe_event         = mtk_jpeg_subscribe_event,
628 	.vidioc_g_selection		= mtk_jpeg_dec_g_selection,
629 
630 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
631 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
632 	.vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
633 	.vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
634 	.vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
635 	.vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
636 	.vidioc_streamon                = v4l2_m2m_ioctl_streamon,
637 	.vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
638 
639 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
640 };
641 
642 static int mtk_jpeg_queue_setup(struct vb2_queue *q,
643 				unsigned int *num_buffers,
644 				unsigned int *num_planes,
645 				unsigned int sizes[],
646 				struct device *alloc_ctxs[])
647 {
648 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
649 	struct mtk_jpeg_q_data *q_data = NULL;
650 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
651 	int i;
652 
653 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
654 		 q->type, *num_buffers);
655 
656 	q_data = mtk_jpeg_get_q_data(ctx, q->type);
657 	if (!q_data)
658 		return -EINVAL;
659 
660 	if (*num_planes) {
661 		for (i = 0; i < *num_planes; i++)
662 			if (sizes[i] < q_data->pix_mp.plane_fmt[i].sizeimage)
663 				return -EINVAL;
664 		return 0;
665 	}
666 
667 	*num_planes = q_data->fmt->colplanes;
668 	for (i = 0; i < q_data->fmt->colplanes; i++) {
669 		sizes[i] =  q_data->pix_mp.plane_fmt[i].sizeimage;
670 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n",
671 			 i, sizes[i]);
672 	}
673 
674 	return 0;
675 }
676 
677 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
678 {
679 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
680 	struct mtk_jpeg_q_data *q_data = NULL;
681 	struct v4l2_plane_pix_format plane_fmt;
682 	int i;
683 
684 	q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
685 	if (!q_data)
686 		return -EINVAL;
687 
688 	for (i = 0; i < q_data->fmt->colplanes; i++) {
689 		plane_fmt = q_data->pix_mp.plane_fmt[i];
690 		if (ctx->enable_exif &&
691 		    q_data->fmt->fourcc == V4L2_PIX_FMT_JPEG)
692 			vb2_set_plane_payload(vb, i, plane_fmt.sizeimage +
693 					      MTK_JPEG_MAX_EXIF_SIZE);
694 		else
695 			vb2_set_plane_payload(vb, i,  plane_fmt.sizeimage);
696 	}
697 
698 	return 0;
699 }
700 
701 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
702 					     struct mtk_jpeg_dec_param *param)
703 {
704 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
705 	struct mtk_jpeg_q_data *q_data;
706 
707 	q_data = &ctx->out_q;
708 	if (q_data->pix_mp.width != param->pic_w ||
709 	    q_data->pix_mp.height != param->pic_h) {
710 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n");
711 		return true;
712 	}
713 
714 	q_data = &ctx->cap_q;
715 	if (q_data->fmt !=
716 	    mtk_jpeg_find_format(jpeg->variant->formats,
717 				 jpeg->variant->num_formats, param->dst_fourcc,
718 				 MTK_JPEG_FMT_FLAG_CAPTURE)) {
719 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n");
720 		return true;
721 	}
722 	return false;
723 }
724 
725 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
726 				    struct mtk_jpeg_dec_param *param)
727 {
728 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
729 	struct mtk_jpeg_q_data *q_data;
730 	int i;
731 
732 	q_data = &ctx->out_q;
733 	q_data->pix_mp.width = param->pic_w;
734 	q_data->pix_mp.height = param->pic_h;
735 
736 	q_data = &ctx->cap_q;
737 	q_data->pix_mp.width = param->dec_w;
738 	q_data->pix_mp.height = param->dec_h;
739 	q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
740 					   jpeg->variant->num_formats,
741 					   param->dst_fourcc,
742 					   MTK_JPEG_FMT_FLAG_CAPTURE);
743 
744 	for (i = 0; i < q_data->fmt->colplanes; i++) {
745 		q_data->pix_mp.plane_fmt[i].bytesperline = param->mem_stride[i];
746 		q_data->pix_mp.plane_fmt[i].sizeimage = param->comp_size[i];
747 	}
748 
749 	v4l2_dbg(1, debug, &jpeg->v4l2_dev,
750 		 "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n",
751 		 (param->dst_fourcc & 0xff),
752 		 (param->dst_fourcc >>  8 & 0xff),
753 		 (param->dst_fourcc >> 16 & 0xff),
754 		 (param->dst_fourcc >> 24 & 0xff),
755 		 param->pic_w, param->pic_h,
756 		 param->dec_w, param->dec_h);
757 }
758 
759 static void mtk_jpeg_enc_buf_queue(struct vb2_buffer *vb)
760 {
761 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
762 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
763 
764 	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
765 		 vb->vb2_queue->type, vb->index, vb);
766 
767 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
768 }
769 
770 static void mtk_jpeg_dec_buf_queue(struct vb2_buffer *vb)
771 {
772 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
773 	struct mtk_jpeg_dec_param *param;
774 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
775 	struct mtk_jpeg_src_buf *jpeg_src_buf;
776 	bool header_valid;
777 
778 	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
779 		 vb->vb2_queue->type, vb->index, vb);
780 
781 	if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
782 		goto end;
783 
784 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
785 	param = &jpeg_src_buf->dec_param;
786 	memset(param, 0, sizeof(*param));
787 
788 	header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
789 				      vb2_get_plane_payload(vb, 0));
790 	if (!header_valid) {
791 		v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
792 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
793 		return;
794 	}
795 
796 	if (ctx->state == MTK_JPEG_INIT) {
797 		struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
798 			ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
799 
800 		mtk_jpeg_queue_src_chg_event(ctx);
801 		mtk_jpeg_set_queue_data(ctx, param);
802 		ctx->state = vb2_is_streaming(dst_vq) ?
803 				MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
804 	}
805 end:
806 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
807 }
808 
809 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
810 				 enum v4l2_buf_type type)
811 {
812 	if (V4L2_TYPE_IS_OUTPUT(type))
813 		return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
814 	else
815 		return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
816 }
817 
818 static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q)
819 {
820 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
821 	struct vb2_v4l2_buffer *vb;
822 
823 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
824 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
825 }
826 
827 static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q)
828 {
829 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
830 	struct vb2_v4l2_buffer *vb;
831 
832 	/*
833 	 * STREAMOFF is an acknowledgment for source change event.
834 	 * Before STREAMOFF, we still have to return the old resolution and
835 	 * subsampling. Update capture queue when the stream is off.
836 	 */
837 	if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
838 	    V4L2_TYPE_IS_CAPTURE(q->type)) {
839 		struct mtk_jpeg_src_buf *src_buf;
840 
841 		vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
842 		src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
843 		mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
844 		ctx->state = MTK_JPEG_RUNNING;
845 	} else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
846 		ctx->state = MTK_JPEG_INIT;
847 	}
848 
849 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
850 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
851 }
852 
853 static const struct vb2_ops mtk_jpeg_dec_qops = {
854 	.queue_setup        = mtk_jpeg_queue_setup,
855 	.buf_prepare        = mtk_jpeg_buf_prepare,
856 	.buf_queue          = mtk_jpeg_dec_buf_queue,
857 	.wait_prepare       = vb2_ops_wait_prepare,
858 	.wait_finish        = vb2_ops_wait_finish,
859 	.stop_streaming     = mtk_jpeg_dec_stop_streaming,
860 };
861 
862 static const struct vb2_ops mtk_jpeg_enc_qops = {
863 	.queue_setup        = mtk_jpeg_queue_setup,
864 	.buf_prepare        = mtk_jpeg_buf_prepare,
865 	.buf_queue          = mtk_jpeg_enc_buf_queue,
866 	.wait_prepare       = vb2_ops_wait_prepare,
867 	.wait_finish        = vb2_ops_wait_finish,
868 	.stop_streaming     = mtk_jpeg_enc_stop_streaming,
869 };
870 
871 static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
872 				 struct vb2_buffer *src_buf,
873 				 struct mtk_jpeg_bs *bs)
874 {
875 	bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
876 	bs->end_addr = bs->str_addr +
877 		       round_up(vb2_get_plane_payload(src_buf, 0), 16);
878 	bs->size = round_up(vb2_plane_size(src_buf, 0), 128);
879 }
880 
881 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
882 				struct mtk_jpeg_dec_param *param,
883 				struct vb2_buffer *dst_buf,
884 				struct mtk_jpeg_fb *fb)
885 {
886 	int i;
887 
888 	if (param->comp_num != dst_buf->num_planes) {
889 		dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n",
890 			param->comp_num, dst_buf->num_planes);
891 		return -EINVAL;
892 	}
893 
894 	for (i = 0; i < dst_buf->num_planes; i++) {
895 		if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) {
896 			dev_err(ctx->jpeg->dev,
897 				"buffer size is underflow (%lu < %u)\n",
898 				vb2_plane_size(dst_buf, 0),
899 				param->comp_size[i]);
900 			return -EINVAL;
901 		}
902 		fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
903 	}
904 
905 	return 0;
906 }
907 
908 static void mtk_jpeg_enc_device_run(void *priv)
909 {
910 	struct mtk_jpeg_ctx *ctx = priv;
911 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
912 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
913 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
914 	unsigned long flags;
915 	int ret;
916 
917 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
918 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
919 
920 	ret = pm_runtime_resume_and_get(jpeg->dev);
921 	if (ret < 0)
922 		goto enc_end;
923 
924 	schedule_delayed_work(&jpeg->job_timeout_work,
925 			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
926 
927 	spin_lock_irqsave(&jpeg->hw_lock, flags);
928 
929 	/*
930 	 * Resetting the hardware every frame is to ensure that all the
931 	 * registers are cleared. This is a hardware requirement.
932 	 */
933 	mtk_jpeg_enc_reset(jpeg->reg_base);
934 
935 	mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf);
936 	mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf);
937 	mtk_jpeg_set_enc_params(ctx, jpeg->reg_base);
938 	mtk_jpeg_enc_start(jpeg->reg_base);
939 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
940 	return;
941 
942 enc_end:
943 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
944 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
945 	v4l2_m2m_buf_done(src_buf, buf_state);
946 	v4l2_m2m_buf_done(dst_buf, buf_state);
947 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
948 }
949 
950 static void mtk_jpeg_dec_device_run(void *priv)
951 {
952 	struct mtk_jpeg_ctx *ctx = priv;
953 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
954 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
955 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
956 	unsigned long flags;
957 	struct mtk_jpeg_src_buf *jpeg_src_buf;
958 	struct mtk_jpeg_bs bs;
959 	struct mtk_jpeg_fb fb;
960 	int ret;
961 
962 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
963 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
964 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
965 
966 	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
967 		mtk_jpeg_queue_src_chg_event(ctx);
968 		ctx->state = MTK_JPEG_SOURCE_CHANGE;
969 		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
970 		return;
971 	}
972 
973 	ret = pm_runtime_resume_and_get(jpeg->dev);
974 	if (ret < 0)
975 		goto dec_end;
976 
977 	schedule_delayed_work(&jpeg->job_timeout_work,
978 			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
979 
980 	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
981 	if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
982 		goto dec_end;
983 
984 	spin_lock_irqsave(&jpeg->hw_lock, flags);
985 	mtk_jpeg_dec_reset(jpeg->reg_base);
986 	mtk_jpeg_dec_set_config(jpeg->reg_base,
987 				&jpeg_src_buf->dec_param, &bs, &fb);
988 
989 	mtk_jpeg_dec_start(jpeg->reg_base);
990 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
991 	return;
992 
993 dec_end:
994 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
995 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
996 	v4l2_m2m_buf_done(src_buf, buf_state);
997 	v4l2_m2m_buf_done(dst_buf, buf_state);
998 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
999 }
1000 
1001 static int mtk_jpeg_dec_job_ready(void *priv)
1002 {
1003 	struct mtk_jpeg_ctx *ctx = priv;
1004 
1005 	return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
1006 }
1007 
1008 static const struct v4l2_m2m_ops mtk_jpeg_enc_m2m_ops = {
1009 	.device_run = mtk_jpeg_enc_device_run,
1010 };
1011 
1012 static const struct v4l2_m2m_ops mtk_jpeg_dec_m2m_ops = {
1013 	.device_run = mtk_jpeg_dec_device_run,
1014 	.job_ready  = mtk_jpeg_dec_job_ready,
1015 };
1016 
1017 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
1018 			       struct vb2_queue *dst_vq)
1019 {
1020 	struct mtk_jpeg_ctx *ctx = priv;
1021 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1022 	int ret;
1023 
1024 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1025 	src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1026 	src_vq->drv_priv = ctx;
1027 	src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf);
1028 	src_vq->ops = jpeg->variant->qops;
1029 	src_vq->mem_ops = &vb2_dma_contig_memops;
1030 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1031 	src_vq->lock = &ctx->jpeg->lock;
1032 	src_vq->dev = ctx->jpeg->dev;
1033 	ret = vb2_queue_init(src_vq);
1034 	if (ret)
1035 		return ret;
1036 
1037 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1038 	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1039 	dst_vq->drv_priv = ctx;
1040 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1041 	dst_vq->ops = jpeg->variant->qops;
1042 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1043 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1044 	dst_vq->lock = &ctx->jpeg->lock;
1045 	dst_vq->dev = ctx->jpeg->dev;
1046 	ret = vb2_queue_init(dst_vq);
1047 
1048 	return ret;
1049 }
1050 
1051 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
1052 {
1053 	int ret;
1054 
1055 	ret = clk_bulk_prepare_enable(jpeg->variant->num_clks,
1056 				      jpeg->variant->clks);
1057 	if (ret)
1058 		dev_err(jpeg->dev, "Failed to open jpeg clk: %d\n", ret);
1059 }
1060 
1061 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
1062 {
1063 	clk_bulk_disable_unprepare(jpeg->variant->num_clks,
1064 				   jpeg->variant->clks);
1065 }
1066 
1067 static irqreturn_t mtk_jpeg_enc_done(struct mtk_jpeg_dev *jpeg)
1068 {
1069 	struct mtk_jpeg_ctx *ctx;
1070 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1071 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1072 	u32 result_size;
1073 
1074 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1075 	if (!ctx) {
1076 		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1077 		return IRQ_HANDLED;
1078 	}
1079 
1080 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1081 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1082 
1083 	result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
1084 	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, result_size);
1085 
1086 	buf_state = VB2_BUF_STATE_DONE;
1087 
1088 	v4l2_m2m_buf_done(src_buf, buf_state);
1089 	v4l2_m2m_buf_done(dst_buf, buf_state);
1090 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1091 	pm_runtime_put(ctx->jpeg->dev);
1092 	return IRQ_HANDLED;
1093 }
1094 
1095 static irqreturn_t mtk_jpeg_enc_irq(int irq, void *priv)
1096 {
1097 	struct mtk_jpeg_dev *jpeg = priv;
1098 	u32 irq_status;
1099 	irqreturn_t ret = IRQ_NONE;
1100 
1101 	cancel_delayed_work(&jpeg->job_timeout_work);
1102 
1103 	irq_status = readl(jpeg->reg_base + JPEG_ENC_INT_STS) &
1104 		     JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
1105 	if (irq_status)
1106 		writel(0, jpeg->reg_base + JPEG_ENC_INT_STS);
1107 
1108 	if (!(irq_status & JPEG_ENC_INT_STATUS_DONE))
1109 		return ret;
1110 
1111 	ret = mtk_jpeg_enc_done(jpeg);
1112 	return ret;
1113 }
1114 
1115 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
1116 {
1117 	struct mtk_jpeg_dev *jpeg = priv;
1118 	struct mtk_jpeg_ctx *ctx;
1119 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1120 	struct mtk_jpeg_src_buf *jpeg_src_buf;
1121 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1122 	u32	dec_irq_ret;
1123 	u32 dec_ret;
1124 	int i;
1125 
1126 	cancel_delayed_work(&jpeg->job_timeout_work);
1127 
1128 	dec_ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base);
1129 	dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
1130 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1131 	if (!ctx) {
1132 		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1133 		return IRQ_HANDLED;
1134 	}
1135 
1136 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1137 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1138 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1139 
1140 	if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
1141 		mtk_jpeg_dec_reset(jpeg->reg_base);
1142 
1143 	if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
1144 		dev_err(jpeg->dev, "decode failed\n");
1145 		goto dec_end;
1146 	}
1147 
1148 	for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
1149 		vb2_set_plane_payload(&dst_buf->vb2_buf, i,
1150 				      jpeg_src_buf->dec_param.comp_size[i]);
1151 
1152 	buf_state = VB2_BUF_STATE_DONE;
1153 
1154 dec_end:
1155 	v4l2_m2m_buf_done(src_buf, buf_state);
1156 	v4l2_m2m_buf_done(dst_buf, buf_state);
1157 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1158 	pm_runtime_put(ctx->jpeg->dev);
1159 	return IRQ_HANDLED;
1160 }
1161 
1162 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
1163 {
1164 	struct mtk_jpeg_q_data *q = &ctx->out_q;
1165 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1166 
1167 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1168 	q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1169 	q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1170 	q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1171 	q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1172 
1173 	q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1174 				      jpeg->variant->num_formats,
1175 				      jpeg->variant->out_q_default_fourcc,
1176 				      MTK_JPEG_FMT_FLAG_OUTPUT);
1177 	q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1178 	q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1179 	mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1180 
1181 	q = &ctx->cap_q;
1182 	q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1183 				      jpeg->variant->num_formats,
1184 				      jpeg->variant->cap_q_default_fourcc,
1185 				      MTK_JPEG_FMT_FLAG_CAPTURE);
1186 	q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1187 	q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1188 	q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1189 	q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1190 	q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1191 	q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1192 
1193 	mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1194 }
1195 
1196 static int mtk_jpeg_open(struct file *file)
1197 {
1198 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1199 	struct video_device *vfd = video_devdata(file);
1200 	struct mtk_jpeg_ctx *ctx;
1201 	int ret = 0;
1202 
1203 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1204 	if (!ctx)
1205 		return -ENOMEM;
1206 
1207 	if (mutex_lock_interruptible(&jpeg->lock)) {
1208 		ret = -ERESTARTSYS;
1209 		goto free;
1210 	}
1211 
1212 	v4l2_fh_init(&ctx->fh, vfd);
1213 	file->private_data = &ctx->fh;
1214 	v4l2_fh_add(&ctx->fh);
1215 
1216 	ctx->jpeg = jpeg;
1217 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx,
1218 					    mtk_jpeg_queue_init);
1219 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1220 		ret = PTR_ERR(ctx->fh.m2m_ctx);
1221 		goto error;
1222 	}
1223 
1224 	if (jpeg->variant->cap_q_default_fourcc == V4L2_PIX_FMT_JPEG) {
1225 		ret = mtk_jpeg_enc_ctrls_setup(ctx);
1226 		if (ret) {
1227 			v4l2_err(&jpeg->v4l2_dev, "Failed to setup jpeg enc controls\n");
1228 			goto error;
1229 		}
1230 	} else {
1231 		v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 0);
1232 	}
1233 	mtk_jpeg_set_default_params(ctx);
1234 	mutex_unlock(&jpeg->lock);
1235 	return 0;
1236 
1237 error:
1238 	v4l2_fh_del(&ctx->fh);
1239 	v4l2_fh_exit(&ctx->fh);
1240 	mutex_unlock(&jpeg->lock);
1241 free:
1242 	kfree(ctx);
1243 	return ret;
1244 }
1245 
1246 static int mtk_jpeg_release(struct file *file)
1247 {
1248 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1249 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
1250 
1251 	mutex_lock(&jpeg->lock);
1252 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1253 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1254 	v4l2_fh_del(&ctx->fh);
1255 	v4l2_fh_exit(&ctx->fh);
1256 	kfree(ctx);
1257 	mutex_unlock(&jpeg->lock);
1258 	return 0;
1259 }
1260 
1261 static const struct v4l2_file_operations mtk_jpeg_fops = {
1262 	.owner          = THIS_MODULE,
1263 	.open           = mtk_jpeg_open,
1264 	.release        = mtk_jpeg_release,
1265 	.poll           = v4l2_m2m_fop_poll,
1266 	.unlocked_ioctl = video_ioctl2,
1267 	.mmap           = v4l2_m2m_fop_mmap,
1268 };
1269 
1270 static struct clk_bulk_data mt8173_jpeg_dec_clocks[] = {
1271 	{ .id = "jpgdec-smi" },
1272 	{ .id = "jpgdec" },
1273 };
1274 
1275 static struct clk_bulk_data mtk_jpeg_clocks[] = {
1276 	{ .id = "jpgenc" },
1277 };
1278 
1279 static void mtk_jpeg_job_timeout_work(struct work_struct *work)
1280 {
1281 	struct mtk_jpeg_dev *jpeg = container_of(work, struct mtk_jpeg_dev,
1282 						 job_timeout_work.work);
1283 	struct mtk_jpeg_ctx *ctx;
1284 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1285 
1286 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1287 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1288 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1289 
1290 	jpeg->variant->hw_reset(jpeg->reg_base);
1291 
1292 	pm_runtime_put(jpeg->dev);
1293 
1294 	v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1295 	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1296 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1297 }
1298 
1299 static int mtk_jpeg_probe(struct platform_device *pdev)
1300 {
1301 	struct mtk_jpeg_dev *jpeg;
1302 	int jpeg_irq;
1303 	int ret;
1304 
1305 	jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1306 	if (!jpeg)
1307 		return -ENOMEM;
1308 
1309 	mutex_init(&jpeg->lock);
1310 	spin_lock_init(&jpeg->hw_lock);
1311 	jpeg->dev = &pdev->dev;
1312 	jpeg->variant = of_device_get_match_data(jpeg->dev);
1313 	INIT_DELAYED_WORK(&jpeg->job_timeout_work, mtk_jpeg_job_timeout_work);
1314 
1315 	jpeg->reg_base = devm_platform_ioremap_resource(pdev, 0);
1316 	if (IS_ERR(jpeg->reg_base)) {
1317 		ret = PTR_ERR(jpeg->reg_base);
1318 		return ret;
1319 	}
1320 
1321 	jpeg_irq = platform_get_irq(pdev, 0);
1322 	if (jpeg_irq < 0)
1323 		return jpeg_irq;
1324 
1325 	ret = devm_request_irq(&pdev->dev, jpeg_irq,
1326 			       jpeg->variant->irq_handler, 0, pdev->name, jpeg);
1327 	if (ret) {
1328 		dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n",
1329 			jpeg_irq, ret);
1330 		goto err_req_irq;
1331 	}
1332 
1333 	ret = devm_clk_bulk_get(jpeg->dev, jpeg->variant->num_clks,
1334 				jpeg->variant->clks);
1335 	if (ret) {
1336 		dev_err(&pdev->dev, "Failed to init clk, err %d\n", ret);
1337 		goto err_clk_init;
1338 	}
1339 
1340 	ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1341 	if (ret) {
1342 		dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1343 		ret = -EINVAL;
1344 		goto err_dev_register;
1345 	}
1346 
1347 	jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
1348 
1349 	if (IS_ERR(jpeg->m2m_dev)) {
1350 		v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1351 		ret = PTR_ERR(jpeg->m2m_dev);
1352 		goto err_m2m_init;
1353 	}
1354 
1355 	jpeg->vdev = video_device_alloc();
1356 	if (!jpeg->vdev) {
1357 		ret = -ENOMEM;
1358 		goto err_vfd_jpeg_alloc;
1359 	}
1360 	snprintf(jpeg->vdev->name, sizeof(jpeg->vdev->name),
1361 		 "%s", jpeg->variant->dev_name);
1362 	jpeg->vdev->fops = &mtk_jpeg_fops;
1363 	jpeg->vdev->ioctl_ops = jpeg->variant->ioctl_ops;
1364 	jpeg->vdev->minor = -1;
1365 	jpeg->vdev->release = video_device_release;
1366 	jpeg->vdev->lock = &jpeg->lock;
1367 	jpeg->vdev->v4l2_dev = &jpeg->v4l2_dev;
1368 	jpeg->vdev->vfl_dir = VFL_DIR_M2M;
1369 	jpeg->vdev->device_caps = V4L2_CAP_STREAMING |
1370 				  V4L2_CAP_VIDEO_M2M_MPLANE;
1371 
1372 	if (of_get_property(pdev->dev.of_node, "dma-ranges", NULL))
1373 		dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(34));
1374 
1375 	ret = video_register_device(jpeg->vdev, VFL_TYPE_VIDEO, -1);
1376 	if (ret) {
1377 		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1378 		goto err_vfd_jpeg_register;
1379 	}
1380 
1381 	video_set_drvdata(jpeg->vdev, jpeg);
1382 	v4l2_info(&jpeg->v4l2_dev,
1383 		  "%s device registered as /dev/video%d (%d,%d)\n",
1384 		  jpeg->variant->dev_name, jpeg->vdev->num,
1385 		  VIDEO_MAJOR, jpeg->vdev->minor);
1386 
1387 	platform_set_drvdata(pdev, jpeg);
1388 
1389 	pm_runtime_enable(&pdev->dev);
1390 
1391 	return 0;
1392 
1393 err_vfd_jpeg_register:
1394 	video_device_release(jpeg->vdev);
1395 
1396 err_vfd_jpeg_alloc:
1397 	v4l2_m2m_release(jpeg->m2m_dev);
1398 
1399 err_m2m_init:
1400 	v4l2_device_unregister(&jpeg->v4l2_dev);
1401 
1402 err_dev_register:
1403 
1404 err_clk_init:
1405 
1406 err_req_irq:
1407 
1408 	return ret;
1409 }
1410 
1411 static int mtk_jpeg_remove(struct platform_device *pdev)
1412 {
1413 	struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
1414 
1415 	pm_runtime_disable(&pdev->dev);
1416 	video_unregister_device(jpeg->vdev);
1417 	v4l2_m2m_release(jpeg->m2m_dev);
1418 	v4l2_device_unregister(&jpeg->v4l2_dev);
1419 
1420 	return 0;
1421 }
1422 
1423 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1424 {
1425 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1426 
1427 	mtk_jpeg_clk_off(jpeg);
1428 
1429 	return 0;
1430 }
1431 
1432 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1433 {
1434 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1435 
1436 	mtk_jpeg_clk_on(jpeg);
1437 
1438 	return 0;
1439 }
1440 
1441 static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1442 {
1443 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1444 
1445 	v4l2_m2m_suspend(jpeg->m2m_dev);
1446 	return pm_runtime_force_suspend(dev);
1447 }
1448 
1449 static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1450 {
1451 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1452 	int ret;
1453 
1454 	ret = pm_runtime_force_resume(dev);
1455 	if (ret < 0)
1456 		return ret;
1457 
1458 	v4l2_m2m_resume(jpeg->m2m_dev);
1459 	return ret;
1460 }
1461 
1462 static const struct dev_pm_ops mtk_jpeg_pm_ops = {
1463 	SET_SYSTEM_SLEEP_PM_OPS(mtk_jpeg_suspend, mtk_jpeg_resume)
1464 	SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL)
1465 };
1466 
1467 static const struct mtk_jpeg_variant mt8173_jpeg_drvdata = {
1468 	.clks = mt8173_jpeg_dec_clocks,
1469 	.num_clks = ARRAY_SIZE(mt8173_jpeg_dec_clocks),
1470 	.formats = mtk_jpeg_dec_formats,
1471 	.num_formats = MTK_JPEG_DEC_NUM_FORMATS,
1472 	.qops = &mtk_jpeg_dec_qops,
1473 	.irq_handler = mtk_jpeg_dec_irq,
1474 	.hw_reset = mtk_jpeg_dec_reset,
1475 	.m2m_ops = &mtk_jpeg_dec_m2m_ops,
1476 	.dev_name = "mtk-jpeg-dec",
1477 	.ioctl_ops = &mtk_jpeg_dec_ioctl_ops,
1478 	.out_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1479 	.cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M,
1480 };
1481 
1482 static const struct mtk_jpeg_variant mtk_jpeg_drvdata = {
1483 	.clks = mtk_jpeg_clocks,
1484 	.num_clks = ARRAY_SIZE(mtk_jpeg_clocks),
1485 	.formats = mtk_jpeg_enc_formats,
1486 	.num_formats = MTK_JPEG_ENC_NUM_FORMATS,
1487 	.qops = &mtk_jpeg_enc_qops,
1488 	.irq_handler = mtk_jpeg_enc_irq,
1489 	.hw_reset = mtk_jpeg_enc_reset,
1490 	.m2m_ops = &mtk_jpeg_enc_m2m_ops,
1491 	.dev_name = "mtk-jpeg-enc",
1492 	.ioctl_ops = &mtk_jpeg_enc_ioctl_ops,
1493 	.out_q_default_fourcc = V4L2_PIX_FMT_YUYV,
1494 	.cap_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1495 };
1496 
1497 static const struct of_device_id mtk_jpeg_match[] = {
1498 	{
1499 		.compatible = "mediatek,mt8173-jpgdec",
1500 		.data = &mt8173_jpeg_drvdata,
1501 	},
1502 	{
1503 		.compatible = "mediatek,mt2701-jpgdec",
1504 		.data = &mt8173_jpeg_drvdata,
1505 	},
1506 	{
1507 		.compatible = "mediatek,mtk-jpgenc",
1508 		.data = &mtk_jpeg_drvdata,
1509 	},
1510 	{},
1511 };
1512 
1513 MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1514 
1515 static struct platform_driver mtk_jpeg_driver = {
1516 	.probe = mtk_jpeg_probe,
1517 	.remove = mtk_jpeg_remove,
1518 	.driver = {
1519 		.name           = MTK_JPEG_NAME,
1520 		.of_match_table = mtk_jpeg_match,
1521 		.pm             = &mtk_jpeg_pm_ops,
1522 	},
1523 };
1524 
1525 module_platform_driver(mtk_jpeg_driver);
1526 
1527 MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1528 MODULE_LICENSE("GPL v2");
1529