xref: /linux/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c (revision 58f6259b7a08f8d47d4629609703d358b042f0fd)
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 #if defined(CONFIG_OF)
32 static struct mtk_jpeg_fmt mtk_jpeg_enc_formats[] = {
33 	{
34 		.fourcc		= V4L2_PIX_FMT_JPEG,
35 		.colplanes	= 1,
36 		.flags		= MTK_JPEG_FMT_FLAG_CAPTURE,
37 	},
38 	{
39 		.fourcc		= V4L2_PIX_FMT_NV12M,
40 		.hw_format	= JPEG_ENC_YUV_FORMAT_NV12,
41 		.h_sample	= {4, 4},
42 		.v_sample	= {4, 2},
43 		.colplanes	= 2,
44 		.h_align	= 4,
45 		.v_align	= 4,
46 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
47 	},
48 	{
49 		.fourcc		= V4L2_PIX_FMT_NV21M,
50 		.hw_format	= JEPG_ENC_YUV_FORMAT_NV21,
51 		.h_sample	= {4, 4},
52 		.v_sample	= {4, 2},
53 		.colplanes	= 2,
54 		.h_align	= 4,
55 		.v_align	= 4,
56 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
57 	},
58 	{
59 		.fourcc		= V4L2_PIX_FMT_YUYV,
60 		.hw_format	= JPEG_ENC_YUV_FORMAT_YUYV,
61 		.h_sample	= {8},
62 		.v_sample	= {4},
63 		.colplanes	= 1,
64 		.h_align	= 5,
65 		.v_align	= 3,
66 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
67 	},
68 	{
69 		.fourcc		= V4L2_PIX_FMT_YVYU,
70 		.hw_format	= JPEG_ENC_YUV_FORMAT_YVYU,
71 		.h_sample	= {8},
72 		.v_sample	= {4},
73 		.colplanes	= 1,
74 		.h_align	= 5,
75 		.v_align	= 3,
76 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
77 	},
78 };
79 
80 static struct mtk_jpeg_fmt mtk_jpeg_dec_formats[] = {
81 	{
82 		.fourcc		= V4L2_PIX_FMT_JPEG,
83 		.colplanes	= 1,
84 		.flags		= MTK_JPEG_FMT_FLAG_OUTPUT,
85 	},
86 	{
87 		.fourcc		= V4L2_PIX_FMT_YUV420M,
88 		.h_sample	= {4, 2, 2},
89 		.v_sample	= {4, 2, 2},
90 		.colplanes	= 3,
91 		.h_align	= 5,
92 		.v_align	= 4,
93 		.flags		= MTK_JPEG_FMT_FLAG_CAPTURE,
94 	},
95 	{
96 		.fourcc		= V4L2_PIX_FMT_YUV422M,
97 		.h_sample	= {4, 2, 2},
98 		.v_sample	= {4, 4, 4},
99 		.colplanes	= 3,
100 		.h_align	= 5,
101 		.v_align	= 3,
102 		.flags		= MTK_JPEG_FMT_FLAG_CAPTURE,
103 	},
104 };
105 #endif
106 
107 #define MTK_JPEG_ENC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_enc_formats)
108 #define MTK_JPEG_DEC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_dec_formats)
109 #define MTK_JPEG_MAX_RETRY_TIME 5000
110 
111 enum {
112 	MTK_JPEG_BUF_FLAGS_INIT			= 0,
113 	MTK_JPEG_BUF_FLAGS_LAST_FRAME		= 1,
114 };
115 
116 static int debug;
117 module_param(debug, int, 0644);
118 
119 static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
120 {
121 	return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl);
122 }
123 
124 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
125 {
126 	return container_of(fh, struct mtk_jpeg_ctx, fh);
127 }
128 
129 static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf(
130 							struct vb2_buffer *vb)
131 {
132 	return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b);
133 }
134 
135 static int mtk_jpeg_querycap(struct file *file, void *priv,
136 			     struct v4l2_capability *cap)
137 {
138 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
139 
140 	strscpy(cap->driver, jpeg->variant->dev_name, sizeof(cap->driver));
141 	strscpy(cap->card, jpeg->variant->dev_name, sizeof(cap->card));
142 
143 	return 0;
144 }
145 
146 static int vidioc_jpeg_enc_s_ctrl(struct v4l2_ctrl *ctrl)
147 {
148 	struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
149 
150 	switch (ctrl->id) {
151 	case V4L2_CID_JPEG_RESTART_INTERVAL:
152 		ctx->restart_interval = ctrl->val;
153 		break;
154 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
155 		ctx->enc_quality = ctrl->val;
156 		break;
157 	case V4L2_CID_JPEG_ACTIVE_MARKER:
158 		ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1;
159 		break;
160 	}
161 
162 	return 0;
163 }
164 
165 static const struct v4l2_ctrl_ops mtk_jpeg_enc_ctrl_ops = {
166 	.s_ctrl = vidioc_jpeg_enc_s_ctrl,
167 };
168 
169 static int mtk_jpeg_enc_ctrls_setup(struct mtk_jpeg_ctx *ctx)
170 {
171 	const struct v4l2_ctrl_ops *ops = &mtk_jpeg_enc_ctrl_ops;
172 	struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
173 
174 	v4l2_ctrl_handler_init(handler, 3);
175 
176 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100,
177 			  1, 0);
178 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_COMPRESSION_QUALITY, 48,
179 			  100, 1, 90);
180 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0,
181 			  V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0);
182 
183 	if (handler->error) {
184 		v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
185 		return handler->error;
186 	}
187 
188 	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
189 
190 	return 0;
191 }
192 
193 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
194 			     struct v4l2_fmtdesc *f, u32 type)
195 {
196 	int i, num = 0;
197 
198 	for (i = 0; i < n; ++i) {
199 		if (mtk_jpeg_formats[i].flags & type) {
200 			if (num == f->index)
201 				break;
202 			++num;
203 		}
204 	}
205 
206 	if (i >= n)
207 		return -EINVAL;
208 
209 	f->pixelformat = mtk_jpeg_formats[i].fourcc;
210 
211 	return 0;
212 }
213 
214 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
215 				     struct v4l2_fmtdesc *f)
216 {
217 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
218 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
219 
220 	return mtk_jpeg_enum_fmt(jpeg->variant->formats,
221 				 jpeg->variant->num_formats, f,
222 				 MTK_JPEG_FMT_FLAG_CAPTURE);
223 }
224 
225 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
226 				     struct v4l2_fmtdesc *f)
227 {
228 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
229 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
230 
231 	return mtk_jpeg_enum_fmt(jpeg->variant->formats,
232 				 jpeg->variant->num_formats, f,
233 				 MTK_JPEG_FMT_FLAG_OUTPUT);
234 }
235 
236 static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
237 						   enum v4l2_buf_type type)
238 {
239 	if (V4L2_TYPE_IS_OUTPUT(type))
240 		return &ctx->out_q;
241 	return &ctx->cap_q;
242 }
243 
244 static struct mtk_jpeg_fmt *
245 mtk_jpeg_find_format(struct mtk_jpeg_fmt *mtk_jpeg_formats, int num_formats,
246 		     u32 pixelformat, unsigned int fmt_type)
247 {
248 	unsigned int k;
249 	struct mtk_jpeg_fmt *fmt;
250 
251 	for (k = 0; k < num_formats; k++) {
252 		fmt = &mtk_jpeg_formats[k];
253 
254 		if (fmt->fourcc == pixelformat && fmt->flags & fmt_type)
255 			return fmt;
256 	}
257 
258 	return NULL;
259 }
260 
261 static int mtk_jpeg_try_fmt_mplane(struct v4l2_pix_format_mplane *pix_mp,
262 				   struct mtk_jpeg_fmt *fmt)
263 {
264 	int i;
265 
266 	pix_mp->field = V4L2_FIELD_NONE;
267 
268 	pix_mp->num_planes = fmt->colplanes;
269 	pix_mp->pixelformat = fmt->fourcc;
270 
271 	if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
272 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
273 
274 		pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT,
275 				       MTK_JPEG_MAX_HEIGHT);
276 		pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH,
277 				      MTK_JPEG_MAX_WIDTH);
278 
279 		pfmt->bytesperline = 0;
280 		/* Source size must be aligned to 128 */
281 		pfmt->sizeimage = round_up(pfmt->sizeimage, 128);
282 		if (pfmt->sizeimage == 0)
283 			pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE;
284 		return 0;
285 	}
286 
287 	/* other fourcc */
288 	pix_mp->height = clamp(round_up(pix_mp->height, fmt->v_align),
289 			       MTK_JPEG_MIN_HEIGHT, MTK_JPEG_MAX_HEIGHT);
290 	pix_mp->width = clamp(round_up(pix_mp->width, fmt->h_align),
291 			      MTK_JPEG_MIN_WIDTH, MTK_JPEG_MAX_WIDTH);
292 
293 	for (i = 0; i < fmt->colplanes; i++) {
294 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
295 		u32 stride = pix_mp->width * fmt->h_sample[i] / 4;
296 		u32 h = pix_mp->height * fmt->v_sample[i] / 4;
297 
298 		pfmt->bytesperline = stride;
299 		pfmt->sizeimage = stride * h;
300 	}
301 	return 0;
302 }
303 
304 static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv,
305 				     struct v4l2_format *f)
306 {
307 	struct vb2_queue *vq;
308 	struct mtk_jpeg_q_data *q_data = NULL;
309 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
310 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
311 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
312 	int i;
313 
314 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
315 	if (!vq)
316 		return -EINVAL;
317 
318 	q_data = mtk_jpeg_get_q_data(ctx, f->type);
319 
320 	pix_mp->width = q_data->pix_mp.width;
321 	pix_mp->height = q_data->pix_mp.height;
322 	pix_mp->field = V4L2_FIELD_NONE;
323 	pix_mp->pixelformat = q_data->fmt->fourcc;
324 	pix_mp->num_planes = q_data->fmt->colplanes;
325 	pix_mp->colorspace = q_data->pix_mp.colorspace;
326 	pix_mp->ycbcr_enc = q_data->pix_mp.ycbcr_enc;
327 	pix_mp->xfer_func = q_data->pix_mp.xfer_func;
328 	pix_mp->quantization = q_data->pix_mp.quantization;
329 
330 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n",
331 		 f->type,
332 		 (pix_mp->pixelformat & 0xff),
333 		 (pix_mp->pixelformat >>  8 & 0xff),
334 		 (pix_mp->pixelformat >> 16 & 0xff),
335 		 (pix_mp->pixelformat >> 24 & 0xff),
336 		 pix_mp->width, pix_mp->height);
337 
338 	for (i = 0; i < pix_mp->num_planes; i++) {
339 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
340 
341 		pfmt->bytesperline = q_data->pix_mp.plane_fmt[i].bytesperline;
342 		pfmt->sizeimage = q_data->pix_mp.plane_fmt[i].sizeimage;
343 
344 		v4l2_dbg(1, debug, &jpeg->v4l2_dev,
345 			 "plane[%d] bpl=%u, size=%u\n",
346 			 i,
347 			 pfmt->bytesperline,
348 			 pfmt->sizeimage);
349 	}
350 	return 0;
351 }
352 
353 static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
354 					   struct v4l2_format *f)
355 {
356 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
357 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
358 	struct mtk_jpeg_fmt *fmt;
359 
360 	fmt = mtk_jpeg_find_format(jpeg->variant->formats,
361 				   jpeg->variant->num_formats,
362 				   f->fmt.pix_mp.pixelformat,
363 				   MTK_JPEG_FMT_FLAG_CAPTURE);
364 	if (!fmt)
365 		fmt = ctx->cap_q.fmt;
366 
367 	v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
368 		 f->type,
369 		 (fmt->fourcc & 0xff),
370 		 (fmt->fourcc >>  8 & 0xff),
371 		 (fmt->fourcc >> 16 & 0xff),
372 		 (fmt->fourcc >> 24 & 0xff));
373 
374 	if (ctx->state != MTK_JPEG_INIT) {
375 		mtk_jpeg_g_fmt_vid_mplane(file, priv, f);
376 		return 0;
377 	}
378 
379 	return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt);
380 }
381 
382 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
383 					   struct v4l2_format *f)
384 {
385 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
386 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
387 	struct mtk_jpeg_fmt *fmt;
388 
389 	fmt = mtk_jpeg_find_format(jpeg->variant->formats,
390 				   jpeg->variant->num_formats,
391 				   f->fmt.pix_mp.pixelformat,
392 				   MTK_JPEG_FMT_FLAG_OUTPUT);
393 	if (!fmt)
394 		fmt = ctx->out_q.fmt;
395 
396 	v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
397 		 f->type,
398 		 (fmt->fourcc & 0xff),
399 		 (fmt->fourcc >>  8 & 0xff),
400 		 (fmt->fourcc >> 16 & 0xff),
401 		 (fmt->fourcc >> 24 & 0xff));
402 
403 	if (ctx->state != MTK_JPEG_INIT) {
404 		mtk_jpeg_g_fmt_vid_mplane(file, priv, f);
405 		return 0;
406 	}
407 
408 	return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt);
409 }
410 
411 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
412 				 struct v4l2_format *f, unsigned int fmt_type)
413 {
414 	struct vb2_queue *vq;
415 	struct mtk_jpeg_q_data *q_data = NULL;
416 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
417 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
418 	int i;
419 
420 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
421 	if (!vq)
422 		return -EINVAL;
423 
424 	q_data = mtk_jpeg_get_q_data(ctx, f->type);
425 
426 	if (vb2_is_busy(vq)) {
427 		v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
428 		return -EBUSY;
429 	}
430 
431 	q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
432 					   jpeg->variant->num_formats,
433 					   pix_mp->pixelformat, fmt_type);
434 	q_data->pix_mp.width = pix_mp->width;
435 	q_data->pix_mp.height = pix_mp->height;
436 	q_data->enc_crop_rect.width = pix_mp->width;
437 	q_data->enc_crop_rect.height = pix_mp->height;
438 	q_data->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
439 	q_data->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
440 	q_data->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
441 	q_data->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
442 
443 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n",
444 		 f->type,
445 		 (q_data->fmt->fourcc & 0xff),
446 		 (q_data->fmt->fourcc >>  8 & 0xff),
447 		 (q_data->fmt->fourcc >> 16 & 0xff),
448 		 (q_data->fmt->fourcc >> 24 & 0xff),
449 		 q_data->pix_mp.width, q_data->pix_mp.height);
450 
451 	for (i = 0; i < q_data->fmt->colplanes; i++) {
452 		q_data->pix_mp.plane_fmt[i].bytesperline =
453 					pix_mp->plane_fmt[i].bytesperline;
454 		q_data->pix_mp.plane_fmt[i].sizeimage =
455 					pix_mp->plane_fmt[i].sizeimage;
456 
457 		v4l2_dbg(1, debug, &jpeg->v4l2_dev,
458 			 "plane[%d] bpl=%u, size=%u\n",
459 			 i, q_data->pix_mp.plane_fmt[i].bytesperline,
460 			 q_data->pix_mp.plane_fmt[i].sizeimage);
461 	}
462 
463 	return 0;
464 }
465 
466 static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv,
467 					 struct v4l2_format *f)
468 {
469 	int ret;
470 
471 	ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f);
472 	if (ret)
473 		return ret;
474 
475 	return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f,
476 				     MTK_JPEG_FMT_FLAG_OUTPUT);
477 }
478 
479 static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv,
480 					 struct v4l2_format *f)
481 {
482 	int ret;
483 
484 	ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f);
485 	if (ret)
486 		return ret;
487 
488 	return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f,
489 				     MTK_JPEG_FMT_FLAG_CAPTURE);
490 }
491 
492 static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx)
493 {
494 	static const struct v4l2_event ev_src_ch = {
495 		.type = V4L2_EVENT_SOURCE_CHANGE,
496 		.u.src_change.changes =
497 		V4L2_EVENT_SRC_CH_RESOLUTION,
498 	};
499 
500 	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
501 }
502 
503 static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
504 				    const struct v4l2_event_subscription *sub)
505 {
506 	switch (sub->type) {
507 	case V4L2_EVENT_SOURCE_CHANGE:
508 		return v4l2_src_change_event_subscribe(fh, sub);
509 	}
510 
511 	return v4l2_ctrl_subscribe_event(fh, sub);
512 }
513 
514 static int mtk_jpeg_enc_g_selection(struct file *file, void *priv,
515 				    struct v4l2_selection *s)
516 {
517 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
518 
519 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
520 		return -EINVAL;
521 
522 	switch (s->target) {
523 	case V4L2_SEL_TGT_CROP:
524 		s->r = ctx->out_q.enc_crop_rect;
525 		break;
526 	case V4L2_SEL_TGT_CROP_BOUNDS:
527 	case V4L2_SEL_TGT_CROP_DEFAULT:
528 		s->r.width = ctx->out_q.pix_mp.width;
529 		s->r.height = ctx->out_q.pix_mp.height;
530 		s->r.left = 0;
531 		s->r.top = 0;
532 		break;
533 	default:
534 		return -EINVAL;
535 	}
536 	return 0;
537 }
538 
539 static int mtk_jpeg_dec_g_selection(struct file *file, void *priv,
540 				    struct v4l2_selection *s)
541 {
542 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
543 
544 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
545 		return -EINVAL;
546 
547 	switch (s->target) {
548 	case V4L2_SEL_TGT_COMPOSE:
549 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
550 		s->r.width = ctx->out_q.pix_mp.width;
551 		s->r.height = ctx->out_q.pix_mp.height;
552 		s->r.left = 0;
553 		s->r.top = 0;
554 		break;
555 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
556 	case V4L2_SEL_TGT_COMPOSE_PADDED:
557 		s->r.width = ctx->cap_q.pix_mp.width;
558 		s->r.height = ctx->cap_q.pix_mp.height;
559 		s->r.left = 0;
560 		s->r.top = 0;
561 		break;
562 	default:
563 		return -EINVAL;
564 	}
565 	return 0;
566 }
567 
568 static int mtk_jpeg_enc_s_selection(struct file *file, void *priv,
569 				    struct v4l2_selection *s)
570 {
571 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
572 
573 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
574 		return -EINVAL;
575 
576 	switch (s->target) {
577 	case V4L2_SEL_TGT_CROP:
578 		s->r.left = 0;
579 		s->r.top = 0;
580 		s->r.width = min(s->r.width, ctx->out_q.pix_mp.width);
581 		s->r.height = min(s->r.height, ctx->out_q.pix_mp.height);
582 		ctx->out_q.enc_crop_rect = s->r;
583 		break;
584 	default:
585 		return -EINVAL;
586 	}
587 
588 	return 0;
589 }
590 
591 static int mtk_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
592 {
593 	struct v4l2_fh *fh = file->private_data;
594 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
595 	struct vb2_queue *vq;
596 	struct vb2_buffer *vb;
597 	struct mtk_jpeg_src_buf *jpeg_src_buf;
598 
599 	if (buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
600 		goto end;
601 
602 	vq = v4l2_m2m_get_vq(fh->m2m_ctx, buf->type);
603 	if (buf->index >= vq->num_buffers) {
604 		dev_err(ctx->jpeg->dev, "buffer index out of range\n");
605 		return -EINVAL;
606 	}
607 
608 	vb = vq->bufs[buf->index];
609 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
610 	jpeg_src_buf->bs_size = buf->m.planes[0].bytesused;
611 
612 end:
613 	return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
614 }
615 
616 static const struct v4l2_ioctl_ops mtk_jpeg_enc_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_enc_g_selection,
629 	.vidioc_s_selection		= mtk_jpeg_enc_s_selection,
630 
631 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
632 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
633 	.vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
634 	.vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
635 	.vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
636 	.vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
637 	.vidioc_streamon                = v4l2_m2m_ioctl_streamon,
638 	.vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
639 
640 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
641 
642 	.vidioc_encoder_cmd		= v4l2_m2m_ioctl_encoder_cmd,
643 	.vidioc_try_encoder_cmd		= v4l2_m2m_ioctl_try_encoder_cmd,
644 };
645 
646 static const struct v4l2_ioctl_ops mtk_jpeg_dec_ioctl_ops = {
647 	.vidioc_querycap                = mtk_jpeg_querycap,
648 	.vidioc_enum_fmt_vid_cap	= mtk_jpeg_enum_fmt_vid_cap,
649 	.vidioc_enum_fmt_vid_out	= mtk_jpeg_enum_fmt_vid_out,
650 	.vidioc_try_fmt_vid_cap_mplane	= mtk_jpeg_try_fmt_vid_cap_mplane,
651 	.vidioc_try_fmt_vid_out_mplane	= mtk_jpeg_try_fmt_vid_out_mplane,
652 	.vidioc_g_fmt_vid_cap_mplane    = mtk_jpeg_g_fmt_vid_mplane,
653 	.vidioc_g_fmt_vid_out_mplane    = mtk_jpeg_g_fmt_vid_mplane,
654 	.vidioc_s_fmt_vid_cap_mplane    = mtk_jpeg_s_fmt_vid_cap_mplane,
655 	.vidioc_s_fmt_vid_out_mplane    = mtk_jpeg_s_fmt_vid_out_mplane,
656 	.vidioc_qbuf                    = mtk_jpeg_qbuf,
657 	.vidioc_subscribe_event         = mtk_jpeg_subscribe_event,
658 	.vidioc_g_selection		= mtk_jpeg_dec_g_selection,
659 
660 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
661 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
662 	.vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
663 	.vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
664 	.vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
665 	.vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
666 	.vidioc_streamon                = v4l2_m2m_ioctl_streamon,
667 	.vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
668 
669 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
670 
671 	.vidioc_decoder_cmd = v4l2_m2m_ioctl_decoder_cmd,
672 	.vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
673 };
674 
675 static int mtk_jpeg_queue_setup(struct vb2_queue *q,
676 				unsigned int *num_buffers,
677 				unsigned int *num_planes,
678 				unsigned int sizes[],
679 				struct device *alloc_ctxs[])
680 {
681 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
682 	struct mtk_jpeg_q_data *q_data = NULL;
683 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
684 	int i;
685 
686 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
687 		 q->type, *num_buffers);
688 
689 	q_data = mtk_jpeg_get_q_data(ctx, q->type);
690 	if (!q_data)
691 		return -EINVAL;
692 
693 	if (*num_planes) {
694 		for (i = 0; i < *num_planes; i++)
695 			if (sizes[i] < q_data->pix_mp.plane_fmt[i].sizeimage)
696 				return -EINVAL;
697 		return 0;
698 	}
699 
700 	*num_planes = q_data->fmt->colplanes;
701 	for (i = 0; i < q_data->fmt->colplanes; i++) {
702 		sizes[i] =  q_data->pix_mp.plane_fmt[i].sizeimage;
703 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n",
704 			 i, sizes[i]);
705 	}
706 
707 	return 0;
708 }
709 
710 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
711 {
712 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
713 	struct mtk_jpeg_q_data *q_data = NULL;
714 	struct v4l2_plane_pix_format plane_fmt = {};
715 	int i;
716 
717 	q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
718 	if (!q_data)
719 		return -EINVAL;
720 
721 	for (i = 0; i < q_data->fmt->colplanes; i++) {
722 		plane_fmt = q_data->pix_mp.plane_fmt[i];
723 		if (ctx->enable_exif &&
724 		    q_data->fmt->fourcc == V4L2_PIX_FMT_JPEG)
725 			vb2_set_plane_payload(vb, i, plane_fmt.sizeimage +
726 					      MTK_JPEG_MAX_EXIF_SIZE);
727 		else
728 			vb2_set_plane_payload(vb, i,  plane_fmt.sizeimage);
729 	}
730 
731 	return 0;
732 }
733 
734 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
735 					     struct mtk_jpeg_dec_param *param)
736 {
737 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
738 	struct mtk_jpeg_q_data *q_data;
739 
740 	q_data = &ctx->out_q;
741 	if (q_data->pix_mp.width != param->pic_w ||
742 	    q_data->pix_mp.height != param->pic_h) {
743 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n");
744 		return true;
745 	}
746 
747 	q_data = &ctx->cap_q;
748 	if (q_data->fmt !=
749 	    mtk_jpeg_find_format(jpeg->variant->formats,
750 				 jpeg->variant->num_formats, param->dst_fourcc,
751 				 MTK_JPEG_FMT_FLAG_CAPTURE)) {
752 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n");
753 		return true;
754 	}
755 	return false;
756 }
757 
758 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
759 				    struct mtk_jpeg_dec_param *param)
760 {
761 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
762 	struct mtk_jpeg_q_data *q_data;
763 	int i;
764 
765 	q_data = &ctx->out_q;
766 	q_data->pix_mp.width = param->pic_w;
767 	q_data->pix_mp.height = param->pic_h;
768 
769 	q_data = &ctx->cap_q;
770 	q_data->pix_mp.width = param->dec_w;
771 	q_data->pix_mp.height = param->dec_h;
772 	q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
773 					   jpeg->variant->num_formats,
774 					   param->dst_fourcc,
775 					   MTK_JPEG_FMT_FLAG_CAPTURE);
776 
777 	for (i = 0; i < q_data->fmt->colplanes; i++) {
778 		q_data->pix_mp.plane_fmt[i].bytesperline = param->mem_stride[i];
779 		q_data->pix_mp.plane_fmt[i].sizeimage = param->comp_size[i];
780 	}
781 
782 	v4l2_dbg(1, debug, &jpeg->v4l2_dev,
783 		 "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n",
784 		 (param->dst_fourcc & 0xff),
785 		 (param->dst_fourcc >>  8 & 0xff),
786 		 (param->dst_fourcc >> 16 & 0xff),
787 		 (param->dst_fourcc >> 24 & 0xff),
788 		 param->pic_w, param->pic_h,
789 		 param->dec_w, param->dec_h);
790 }
791 
792 static void mtk_jpeg_enc_buf_queue(struct vb2_buffer *vb)
793 {
794 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
795 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
796 
797 	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
798 		 vb->vb2_queue->type, vb->index, vb);
799 
800 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
801 }
802 
803 static void mtk_jpeg_dec_buf_queue(struct vb2_buffer *vb)
804 {
805 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
806 	struct mtk_jpeg_dec_param *param;
807 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
808 	struct mtk_jpeg_src_buf *jpeg_src_buf;
809 	bool header_valid;
810 
811 	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
812 		 vb->vb2_queue->type, vb->index, vb);
813 
814 	if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
815 		goto end;
816 
817 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
818 	param = &jpeg_src_buf->dec_param;
819 	memset(param, 0, sizeof(*param));
820 
821 	header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
822 				      vb2_get_plane_payload(vb, 0));
823 	if (!header_valid) {
824 		v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
825 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
826 		return;
827 	}
828 
829 	if (ctx->state == MTK_JPEG_INIT) {
830 		struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
831 			ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
832 
833 		mtk_jpeg_queue_src_chg_event(ctx);
834 		mtk_jpeg_set_queue_data(ctx, param);
835 		ctx->state = vb2_is_streaming(dst_vq) ?
836 				MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
837 	}
838 end:
839 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
840 }
841 
842 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
843 				 enum v4l2_buf_type type)
844 {
845 	if (V4L2_TYPE_IS_OUTPUT(type))
846 		return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
847 	else
848 		return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
849 }
850 
851 static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q)
852 {
853 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
854 	struct vb2_v4l2_buffer *vb;
855 
856 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
857 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
858 }
859 
860 static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q)
861 {
862 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
863 	struct vb2_v4l2_buffer *vb;
864 
865 	/*
866 	 * STREAMOFF is an acknowledgment for source change event.
867 	 * Before STREAMOFF, we still have to return the old resolution and
868 	 * subsampling. Update capture queue when the stream is off.
869 	 */
870 	if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
871 	    V4L2_TYPE_IS_CAPTURE(q->type)) {
872 		struct mtk_jpeg_src_buf *src_buf;
873 
874 		vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
875 		src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
876 		mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
877 		ctx->state = MTK_JPEG_RUNNING;
878 	} else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
879 		ctx->state = MTK_JPEG_INIT;
880 	}
881 
882 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
883 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
884 }
885 
886 static const struct vb2_ops mtk_jpeg_dec_qops = {
887 	.queue_setup        = mtk_jpeg_queue_setup,
888 	.buf_prepare        = mtk_jpeg_buf_prepare,
889 	.buf_queue          = mtk_jpeg_dec_buf_queue,
890 	.wait_prepare       = vb2_ops_wait_prepare,
891 	.wait_finish        = vb2_ops_wait_finish,
892 	.stop_streaming     = mtk_jpeg_dec_stop_streaming,
893 };
894 
895 static const struct vb2_ops mtk_jpeg_enc_qops = {
896 	.queue_setup        = mtk_jpeg_queue_setup,
897 	.buf_prepare        = mtk_jpeg_buf_prepare,
898 	.buf_queue          = mtk_jpeg_enc_buf_queue,
899 	.wait_prepare       = vb2_ops_wait_prepare,
900 	.wait_finish        = vb2_ops_wait_finish,
901 	.stop_streaming     = mtk_jpeg_enc_stop_streaming,
902 };
903 
904 static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
905 				 struct vb2_buffer *src_buf,
906 				 struct mtk_jpeg_bs *bs)
907 {
908 	bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
909 	bs->end_addr = bs->str_addr +
910 		       round_up(vb2_get_plane_payload(src_buf, 0), 16);
911 	bs->size = round_up(vb2_plane_size(src_buf, 0), 128);
912 }
913 
914 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
915 				struct mtk_jpeg_dec_param *param,
916 				struct vb2_buffer *dst_buf,
917 				struct mtk_jpeg_fb *fb)
918 {
919 	int i;
920 
921 	if (param->comp_num != dst_buf->num_planes) {
922 		dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n",
923 			param->comp_num, dst_buf->num_planes);
924 		return -EINVAL;
925 	}
926 
927 	for (i = 0; i < dst_buf->num_planes; i++) {
928 		if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) {
929 			dev_err(ctx->jpeg->dev,
930 				"buffer size is underflow (%lu < %u)\n",
931 				vb2_plane_size(dst_buf, 0),
932 				param->comp_size[i]);
933 			return -EINVAL;
934 		}
935 		fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
936 	}
937 
938 	return 0;
939 }
940 
941 static void mtk_jpeg_enc_device_run(void *priv)
942 {
943 	struct mtk_jpeg_ctx *ctx = priv;
944 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
945 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
946 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
947 	unsigned long flags;
948 	int ret;
949 
950 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
951 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
952 
953 	ret = pm_runtime_resume_and_get(jpeg->dev);
954 	if (ret < 0)
955 		goto enc_end;
956 
957 	schedule_delayed_work(&jpeg->job_timeout_work,
958 			msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
959 
960 	spin_lock_irqsave(&jpeg->hw_lock, flags);
961 
962 	/*
963 	 * Resetting the hardware every frame is to ensure that all the
964 	 * registers are cleared. This is a hardware requirement.
965 	 */
966 	mtk_jpeg_enc_reset(jpeg->reg_base);
967 
968 	mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf);
969 	mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf);
970 	mtk_jpeg_set_enc_params(ctx, jpeg->reg_base);
971 	mtk_jpeg_enc_start(jpeg->reg_base);
972 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
973 	return;
974 
975 enc_end:
976 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
977 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
978 	v4l2_m2m_buf_done(src_buf, buf_state);
979 	v4l2_m2m_buf_done(dst_buf, buf_state);
980 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
981 }
982 
983 static void mtk_jpeg_multicore_enc_device_run(void *priv)
984 {
985 	struct mtk_jpeg_ctx *ctx = priv;
986 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
987 
988 	queue_work(jpeg->workqueue, &ctx->jpeg_work);
989 }
990 
991 static void mtk_jpeg_multicore_dec_device_run(void *priv)
992 {
993 	struct mtk_jpeg_ctx *ctx = priv;
994 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
995 
996 	queue_work(jpeg->workqueue, &ctx->jpeg_work);
997 }
998 
999 static void mtk_jpeg_dec_device_run(void *priv)
1000 {
1001 	struct mtk_jpeg_ctx *ctx = priv;
1002 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1003 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1004 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1005 	unsigned long flags;
1006 	struct mtk_jpeg_src_buf *jpeg_src_buf;
1007 	struct mtk_jpeg_bs bs;
1008 	struct mtk_jpeg_fb fb;
1009 	int ret;
1010 
1011 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1012 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1013 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1014 
1015 	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
1016 		mtk_jpeg_queue_src_chg_event(ctx);
1017 		ctx->state = MTK_JPEG_SOURCE_CHANGE;
1018 		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1019 		return;
1020 	}
1021 
1022 	ret = pm_runtime_resume_and_get(jpeg->dev);
1023 	if (ret < 0)
1024 		goto dec_end;
1025 
1026 	schedule_delayed_work(&jpeg->job_timeout_work,
1027 			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
1028 
1029 	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
1030 	if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
1031 		goto dec_end;
1032 
1033 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1034 	mtk_jpeg_dec_reset(jpeg->reg_base);
1035 	mtk_jpeg_dec_set_config(jpeg->reg_base,
1036 				&jpeg_src_buf->dec_param,
1037 				jpeg_src_buf->bs_size,
1038 				&bs,
1039 				&fb);
1040 	mtk_jpeg_dec_start(jpeg->reg_base);
1041 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1042 	return;
1043 
1044 dec_end:
1045 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1046 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1047 	v4l2_m2m_buf_done(src_buf, buf_state);
1048 	v4l2_m2m_buf_done(dst_buf, buf_state);
1049 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1050 }
1051 
1052 static int mtk_jpeg_dec_job_ready(void *priv)
1053 {
1054 	struct mtk_jpeg_ctx *ctx = priv;
1055 
1056 	return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
1057 }
1058 
1059 static const struct v4l2_m2m_ops mtk_jpeg_enc_m2m_ops = {
1060 	.device_run = mtk_jpeg_enc_device_run,
1061 };
1062 
1063 static const struct v4l2_m2m_ops mtk_jpeg_multicore_enc_m2m_ops = {
1064 	.device_run = mtk_jpeg_multicore_enc_device_run,
1065 };
1066 
1067 static const struct v4l2_m2m_ops mtk_jpeg_multicore_dec_m2m_ops = {
1068 	.device_run = mtk_jpeg_multicore_dec_device_run,
1069 };
1070 
1071 static const struct v4l2_m2m_ops mtk_jpeg_dec_m2m_ops = {
1072 	.device_run = mtk_jpeg_dec_device_run,
1073 	.job_ready  = mtk_jpeg_dec_job_ready,
1074 };
1075 
1076 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
1077 			       struct vb2_queue *dst_vq)
1078 {
1079 	struct mtk_jpeg_ctx *ctx = priv;
1080 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1081 	int ret;
1082 
1083 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1084 	src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1085 	src_vq->drv_priv = ctx;
1086 	src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf);
1087 	src_vq->ops = jpeg->variant->qops;
1088 	src_vq->mem_ops = &vb2_dma_contig_memops;
1089 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1090 	src_vq->lock = &ctx->jpeg->lock;
1091 	src_vq->dev = ctx->jpeg->dev;
1092 	ret = vb2_queue_init(src_vq);
1093 	if (ret)
1094 		return ret;
1095 
1096 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1097 	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1098 	dst_vq->drv_priv = ctx;
1099 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1100 	dst_vq->ops = jpeg->variant->qops;
1101 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1102 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1103 	dst_vq->lock = &ctx->jpeg->lock;
1104 	dst_vq->dev = ctx->jpeg->dev;
1105 	ret = vb2_queue_init(dst_vq);
1106 
1107 	return ret;
1108 }
1109 
1110 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
1111 {
1112 	int ret;
1113 
1114 	ret = clk_bulk_prepare_enable(jpeg->variant->num_clks,
1115 				      jpeg->variant->clks);
1116 	if (ret)
1117 		dev_err(jpeg->dev, "Failed to open jpeg clk: %d\n", ret);
1118 }
1119 
1120 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
1121 {
1122 	clk_bulk_disable_unprepare(jpeg->variant->num_clks,
1123 				   jpeg->variant->clks);
1124 }
1125 
1126 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
1127 {
1128 	struct mtk_jpeg_q_data *q = &ctx->out_q;
1129 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1130 
1131 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1132 	q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1133 	q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1134 	q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1135 	q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1136 
1137 	q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1138 				      jpeg->variant->num_formats,
1139 				      jpeg->variant->out_q_default_fourcc,
1140 				      MTK_JPEG_FMT_FLAG_OUTPUT);
1141 	q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1142 	q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1143 	mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1144 
1145 	q = &ctx->cap_q;
1146 	q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1147 				      jpeg->variant->num_formats,
1148 				      jpeg->variant->cap_q_default_fourcc,
1149 				      MTK_JPEG_FMT_FLAG_CAPTURE);
1150 	q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1151 	q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1152 	q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1153 	q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1154 	q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1155 	q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1156 
1157 	mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1158 }
1159 
1160 static int mtk_jpeg_open(struct file *file)
1161 {
1162 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1163 	struct video_device *vfd = video_devdata(file);
1164 	struct mtk_jpeg_ctx *ctx;
1165 	int ret = 0;
1166 
1167 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1168 	if (!ctx)
1169 		return -ENOMEM;
1170 
1171 	if (mutex_lock_interruptible(&jpeg->lock)) {
1172 		ret = -ERESTARTSYS;
1173 		goto free;
1174 	}
1175 
1176 	INIT_WORK(&ctx->jpeg_work, jpeg->variant->jpeg_worker);
1177 	INIT_LIST_HEAD(&ctx->dst_done_queue);
1178 	spin_lock_init(&ctx->done_queue_lock);
1179 	v4l2_fh_init(&ctx->fh, vfd);
1180 	file->private_data = &ctx->fh;
1181 	v4l2_fh_add(&ctx->fh);
1182 
1183 	ctx->jpeg = jpeg;
1184 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx,
1185 					    mtk_jpeg_queue_init);
1186 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1187 		ret = PTR_ERR(ctx->fh.m2m_ctx);
1188 		goto error;
1189 	}
1190 
1191 	if (jpeg->variant->cap_q_default_fourcc == V4L2_PIX_FMT_JPEG) {
1192 		ret = mtk_jpeg_enc_ctrls_setup(ctx);
1193 		if (ret) {
1194 			v4l2_err(&jpeg->v4l2_dev, "Failed to setup jpeg enc controls\n");
1195 			goto error;
1196 		}
1197 	} else {
1198 		v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 0);
1199 	}
1200 
1201 	mtk_jpeg_set_default_params(ctx);
1202 	mutex_unlock(&jpeg->lock);
1203 	return 0;
1204 
1205 error:
1206 	v4l2_fh_del(&ctx->fh);
1207 	v4l2_fh_exit(&ctx->fh);
1208 	mutex_unlock(&jpeg->lock);
1209 free:
1210 	kfree(ctx);
1211 	return ret;
1212 }
1213 
1214 static int mtk_jpeg_release(struct file *file)
1215 {
1216 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1217 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
1218 
1219 	mutex_lock(&jpeg->lock);
1220 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1221 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1222 	v4l2_fh_del(&ctx->fh);
1223 	v4l2_fh_exit(&ctx->fh);
1224 	kfree(ctx);
1225 	mutex_unlock(&jpeg->lock);
1226 	return 0;
1227 }
1228 
1229 static const struct v4l2_file_operations mtk_jpeg_fops = {
1230 	.owner          = THIS_MODULE,
1231 	.open           = mtk_jpeg_open,
1232 	.release        = mtk_jpeg_release,
1233 	.poll           = v4l2_m2m_fop_poll,
1234 	.unlocked_ioctl = video_ioctl2,
1235 	.mmap           = v4l2_m2m_fop_mmap,
1236 };
1237 
1238 static void mtk_jpeg_job_timeout_work(struct work_struct *work)
1239 {
1240 	struct mtk_jpeg_dev *jpeg = container_of(work, struct mtk_jpeg_dev,
1241 						 job_timeout_work.work);
1242 	struct mtk_jpeg_ctx *ctx;
1243 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1244 
1245 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1246 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1247 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1248 
1249 	jpeg->variant->hw_reset(jpeg->reg_base);
1250 
1251 	pm_runtime_put(jpeg->dev);
1252 
1253 	v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1254 	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1255 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1256 }
1257 
1258 static int mtk_jpeg_single_core_init(struct platform_device *pdev,
1259 				     struct mtk_jpeg_dev *jpeg_dev)
1260 {
1261 	struct mtk_jpeg_dev *jpeg = jpeg_dev;
1262 	int jpeg_irq, ret;
1263 
1264 	INIT_DELAYED_WORK(&jpeg->job_timeout_work,
1265 			  mtk_jpeg_job_timeout_work);
1266 
1267 	jpeg->reg_base = devm_platform_ioremap_resource(pdev, 0);
1268 	if (IS_ERR(jpeg->reg_base)) {
1269 		ret = PTR_ERR(jpeg->reg_base);
1270 		return ret;
1271 	}
1272 
1273 	jpeg_irq = platform_get_irq(pdev, 0);
1274 	if (jpeg_irq < 0)
1275 		return jpeg_irq;
1276 
1277 	ret = devm_request_irq(&pdev->dev,
1278 			       jpeg_irq,
1279 			       jpeg->variant->irq_handler,
1280 			       0,
1281 			       pdev->name, jpeg);
1282 	if (ret) {
1283 		dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n",
1284 			jpeg_irq, ret);
1285 		return ret;
1286 	}
1287 
1288 	ret = devm_clk_bulk_get(jpeg->dev,
1289 				jpeg->variant->num_clks,
1290 				jpeg->variant->clks);
1291 	if (ret) {
1292 		dev_err(&pdev->dev, "Failed to init clk\n");
1293 		return ret;
1294 	}
1295 
1296 	return 0;
1297 }
1298 
1299 static int mtk_jpeg_probe(struct platform_device *pdev)
1300 {
1301 	struct mtk_jpeg_dev *jpeg;
1302 	struct device_node *child;
1303 	int num_child = 0;
1304 	int ret;
1305 
1306 	jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1307 	if (!jpeg)
1308 		return -ENOMEM;
1309 
1310 	mutex_init(&jpeg->lock);
1311 	spin_lock_init(&jpeg->hw_lock);
1312 	jpeg->dev = &pdev->dev;
1313 	jpeg->variant = of_device_get_match_data(jpeg->dev);
1314 
1315 	ret = devm_of_platform_populate(&pdev->dev);
1316 	if (ret) {
1317 		v4l2_err(&jpeg->v4l2_dev, "Master of platform populate failed.");
1318 		return -EINVAL;
1319 	}
1320 
1321 	if (!jpeg->variant->multi_core) {
1322 		ret = mtk_jpeg_single_core_init(pdev, jpeg);
1323 		if (ret) {
1324 			v4l2_err(&jpeg->v4l2_dev, "mtk_jpeg_single_core_init failed.");
1325 			return -EINVAL;
1326 		}
1327 	} else {
1328 		init_waitqueue_head(&jpeg->hw_wq);
1329 
1330 		for_each_child_of_node(pdev->dev.of_node, child)
1331 			num_child++;
1332 
1333 		atomic_set(&jpeg->hw_rdy, num_child);
1334 		atomic_set(&jpeg->hw_index, 0);
1335 
1336 		jpeg->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,
1337 							  WQ_MEM_RECLAIM
1338 							  | WQ_FREEZABLE);
1339 		if (!jpeg->workqueue)
1340 			return -EINVAL;
1341 	}
1342 
1343 	ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1344 	if (ret) {
1345 		dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1346 		return -EINVAL;
1347 	}
1348 
1349 	jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
1350 
1351 	if (IS_ERR(jpeg->m2m_dev)) {
1352 		v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1353 		ret = PTR_ERR(jpeg->m2m_dev);
1354 		goto err_m2m_init;
1355 	}
1356 
1357 	jpeg->vdev = video_device_alloc();
1358 	if (!jpeg->vdev) {
1359 		ret = -ENOMEM;
1360 		goto err_vfd_jpeg_alloc;
1361 	}
1362 	snprintf(jpeg->vdev->name, sizeof(jpeg->vdev->name),
1363 		 "%s", jpeg->variant->dev_name);
1364 	jpeg->vdev->fops = &mtk_jpeg_fops;
1365 	jpeg->vdev->ioctl_ops = jpeg->variant->ioctl_ops;
1366 	jpeg->vdev->minor = -1;
1367 	jpeg->vdev->release = video_device_release;
1368 	jpeg->vdev->lock = &jpeg->lock;
1369 	jpeg->vdev->v4l2_dev = &jpeg->v4l2_dev;
1370 	jpeg->vdev->vfl_dir = VFL_DIR_M2M;
1371 	jpeg->vdev->device_caps = V4L2_CAP_STREAMING |
1372 				  V4L2_CAP_VIDEO_M2M_MPLANE;
1373 
1374 	ret = video_register_device(jpeg->vdev, VFL_TYPE_VIDEO, -1);
1375 	if (ret) {
1376 		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1377 		goto err_vfd_jpeg_register;
1378 	}
1379 
1380 	video_set_drvdata(jpeg->vdev, jpeg);
1381 	v4l2_info(&jpeg->v4l2_dev,
1382 		  "%s device registered as /dev/video%d (%d,%d)\n",
1383 		  jpeg->variant->dev_name, jpeg->vdev->num,
1384 		  VIDEO_MAJOR, jpeg->vdev->minor);
1385 
1386 	platform_set_drvdata(pdev, jpeg);
1387 
1388 	pm_runtime_enable(&pdev->dev);
1389 
1390 	return 0;
1391 
1392 err_vfd_jpeg_register:
1393 	video_device_release(jpeg->vdev);
1394 
1395 err_vfd_jpeg_alloc:
1396 	v4l2_m2m_release(jpeg->m2m_dev);
1397 
1398 err_m2m_init:
1399 	v4l2_device_unregister(&jpeg->v4l2_dev);
1400 
1401 	return ret;
1402 }
1403 
1404 static void mtk_jpeg_remove(struct platform_device *pdev)
1405 {
1406 	struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
1407 
1408 	pm_runtime_disable(&pdev->dev);
1409 	video_unregister_device(jpeg->vdev);
1410 	v4l2_m2m_release(jpeg->m2m_dev);
1411 	v4l2_device_unregister(&jpeg->v4l2_dev);
1412 }
1413 
1414 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1415 {
1416 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1417 
1418 	mtk_jpeg_clk_off(jpeg);
1419 
1420 	return 0;
1421 }
1422 
1423 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1424 {
1425 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1426 
1427 	mtk_jpeg_clk_on(jpeg);
1428 
1429 	return 0;
1430 }
1431 
1432 static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1433 {
1434 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1435 
1436 	v4l2_m2m_suspend(jpeg->m2m_dev);
1437 	return pm_runtime_force_suspend(dev);
1438 }
1439 
1440 static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1441 {
1442 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1443 	int ret;
1444 
1445 	ret = pm_runtime_force_resume(dev);
1446 	if (ret < 0)
1447 		return ret;
1448 
1449 	v4l2_m2m_resume(jpeg->m2m_dev);
1450 	return ret;
1451 }
1452 
1453 static const struct dev_pm_ops mtk_jpeg_pm_ops = {
1454 	SET_SYSTEM_SLEEP_PM_OPS(mtk_jpeg_suspend, mtk_jpeg_resume)
1455 	SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL)
1456 };
1457 
1458 #if defined(CONFIG_OF)
1459 static int mtk_jpegenc_get_hw(struct mtk_jpeg_ctx *ctx)
1460 {
1461 	struct mtk_jpegenc_comp_dev *comp_jpeg;
1462 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1463 	unsigned long flags;
1464 	int hw_id = -1;
1465 	int i;
1466 
1467 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1468 	for (i = 0; i < MTK_JPEGENC_HW_MAX; i++) {
1469 		comp_jpeg = jpeg->enc_hw_dev[i];
1470 		if (comp_jpeg->hw_state == MTK_JPEG_HW_IDLE) {
1471 			hw_id = i;
1472 			comp_jpeg->hw_state = MTK_JPEG_HW_BUSY;
1473 			break;
1474 		}
1475 	}
1476 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1477 
1478 	return hw_id;
1479 }
1480 
1481 static int mtk_jpegenc_set_hw_param(struct mtk_jpeg_ctx *ctx,
1482 				    int hw_id,
1483 				    struct vb2_v4l2_buffer *src_buf,
1484 				    struct vb2_v4l2_buffer *dst_buf)
1485 {
1486 	struct mtk_jpegenc_comp_dev *jpeg = ctx->jpeg->enc_hw_dev[hw_id];
1487 
1488 	jpeg->hw_param.curr_ctx = ctx;
1489 	jpeg->hw_param.src_buffer = src_buf;
1490 	jpeg->hw_param.dst_buffer = dst_buf;
1491 
1492 	return 0;
1493 }
1494 
1495 static int mtk_jpegenc_put_hw(struct mtk_jpeg_dev *jpeg, int hw_id)
1496 {
1497 	unsigned long flags;
1498 
1499 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1500 	jpeg->enc_hw_dev[hw_id]->hw_state = MTK_JPEG_HW_IDLE;
1501 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1502 
1503 	return 0;
1504 }
1505 
1506 static int mtk_jpegdec_get_hw(struct mtk_jpeg_ctx *ctx)
1507 {
1508 	struct mtk_jpegdec_comp_dev *comp_jpeg;
1509 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1510 	unsigned long flags;
1511 	int hw_id = -1;
1512 	int i;
1513 
1514 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1515 	for (i = 0; i < MTK_JPEGDEC_HW_MAX; i++) {
1516 		comp_jpeg = jpeg->dec_hw_dev[i];
1517 		if (comp_jpeg->hw_state == MTK_JPEG_HW_IDLE) {
1518 			hw_id = i;
1519 			comp_jpeg->hw_state = MTK_JPEG_HW_BUSY;
1520 			break;
1521 		}
1522 	}
1523 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1524 
1525 	return hw_id;
1526 }
1527 
1528 static int mtk_jpegdec_put_hw(struct mtk_jpeg_dev *jpeg, int hw_id)
1529 {
1530 	unsigned long flags;
1531 
1532 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1533 	jpeg->dec_hw_dev[hw_id]->hw_state =
1534 		MTK_JPEG_HW_IDLE;
1535 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1536 
1537 	return 0;
1538 }
1539 
1540 static int mtk_jpegdec_set_hw_param(struct mtk_jpeg_ctx *ctx,
1541 				    int hw_id,
1542 				    struct vb2_v4l2_buffer *src_buf,
1543 				    struct vb2_v4l2_buffer *dst_buf)
1544 {
1545 	struct mtk_jpegdec_comp_dev *jpeg =
1546 		ctx->jpeg->dec_hw_dev[hw_id];
1547 
1548 	jpeg->hw_param.curr_ctx = ctx;
1549 	jpeg->hw_param.src_buffer = src_buf;
1550 	jpeg->hw_param.dst_buffer = dst_buf;
1551 
1552 	return 0;
1553 }
1554 
1555 static irqreturn_t mtk_jpeg_enc_done(struct mtk_jpeg_dev *jpeg)
1556 {
1557 	struct mtk_jpeg_ctx *ctx;
1558 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1559 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1560 	u32 result_size;
1561 
1562 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1563 	if (!ctx) {
1564 		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1565 		return IRQ_HANDLED;
1566 	}
1567 
1568 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1569 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1570 
1571 	result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
1572 	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, result_size);
1573 
1574 	buf_state = VB2_BUF_STATE_DONE;
1575 
1576 	v4l2_m2m_buf_done(src_buf, buf_state);
1577 	v4l2_m2m_buf_done(dst_buf, buf_state);
1578 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1579 	pm_runtime_put(ctx->jpeg->dev);
1580 	return IRQ_HANDLED;
1581 }
1582 
1583 static void mtk_jpegenc_worker(struct work_struct *work)
1584 {
1585 	struct mtk_jpegenc_comp_dev *comp_jpeg[MTK_JPEGENC_HW_MAX];
1586 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1587 	struct mtk_jpeg_src_buf *jpeg_dst_buf;
1588 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1589 	int ret, i, hw_id = 0;
1590 	unsigned long flags;
1591 
1592 	struct mtk_jpeg_ctx *ctx = container_of(work,
1593 		struct mtk_jpeg_ctx,
1594 		jpeg_work);
1595 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1596 
1597 	for (i = 0; i < MTK_JPEGENC_HW_MAX; i++)
1598 		comp_jpeg[i] = jpeg->enc_hw_dev[i];
1599 	i = 0;
1600 
1601 retry_select:
1602 	hw_id = mtk_jpegenc_get_hw(ctx);
1603 	if (hw_id < 0) {
1604 		ret = wait_event_interruptible(jpeg->hw_wq,
1605 					       atomic_read(&jpeg->hw_rdy) > 0);
1606 		if (ret != 0 || (i++ > MTK_JPEG_MAX_RETRY_TIME)) {
1607 			dev_err(jpeg->dev, "%s : %d, all HW are busy\n",
1608 				__func__, __LINE__);
1609 			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1610 			return;
1611 		}
1612 
1613 		goto retry_select;
1614 	}
1615 
1616 	atomic_dec(&jpeg->hw_rdy);
1617 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1618 	if (!src_buf)
1619 		goto getbuf_fail;
1620 
1621 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1622 	if (!dst_buf)
1623 		goto getbuf_fail;
1624 
1625 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
1626 
1627 	mtk_jpegenc_set_hw_param(ctx, hw_id, src_buf, dst_buf);
1628 	ret = pm_runtime_get_sync(comp_jpeg[hw_id]->dev);
1629 	if (ret < 0) {
1630 		dev_err(jpeg->dev, "%s : %d, pm_runtime_get_sync fail !!!\n",
1631 			__func__, __LINE__);
1632 		goto enc_end;
1633 	}
1634 
1635 	ret = clk_prepare_enable(comp_jpeg[hw_id]->venc_clk.clks->clk);
1636 	if (ret) {
1637 		dev_err(jpeg->dev, "%s : %d, jpegenc clk_prepare_enable fail\n",
1638 			__func__, __LINE__);
1639 		goto enc_end;
1640 	}
1641 
1642 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1643 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1644 
1645 	schedule_delayed_work(&comp_jpeg[hw_id]->job_timeout_work,
1646 			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
1647 
1648 	spin_lock_irqsave(&comp_jpeg[hw_id]->hw_lock, flags);
1649 	jpeg_dst_buf = mtk_jpeg_vb2_to_srcbuf(&dst_buf->vb2_buf);
1650 	jpeg_dst_buf->curr_ctx = ctx;
1651 	jpeg_dst_buf->frame_num = ctx->total_frame_num;
1652 	ctx->total_frame_num++;
1653 	mtk_jpeg_enc_reset(comp_jpeg[hw_id]->reg_base);
1654 	mtk_jpeg_set_enc_dst(ctx,
1655 			     comp_jpeg[hw_id]->reg_base,
1656 			     &dst_buf->vb2_buf);
1657 	mtk_jpeg_set_enc_src(ctx,
1658 			     comp_jpeg[hw_id]->reg_base,
1659 			     &src_buf->vb2_buf);
1660 	mtk_jpeg_set_enc_params(ctx, comp_jpeg[hw_id]->reg_base);
1661 	mtk_jpeg_enc_start(comp_jpeg[hw_id]->reg_base);
1662 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1663 	spin_unlock_irqrestore(&comp_jpeg[hw_id]->hw_lock, flags);
1664 
1665 	return;
1666 
1667 enc_end:
1668 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1669 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1670 	v4l2_m2m_buf_done(src_buf, buf_state);
1671 	v4l2_m2m_buf_done(dst_buf, buf_state);
1672 getbuf_fail:
1673 	atomic_inc(&jpeg->hw_rdy);
1674 	mtk_jpegenc_put_hw(jpeg, hw_id);
1675 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1676 }
1677 
1678 static void mtk_jpegdec_worker(struct work_struct *work)
1679 {
1680 	struct mtk_jpeg_ctx *ctx = container_of(work, struct mtk_jpeg_ctx,
1681 		jpeg_work);
1682 	struct mtk_jpegdec_comp_dev *comp_jpeg[MTK_JPEGDEC_HW_MAX];
1683 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1684 	struct mtk_jpeg_src_buf *jpeg_src_buf, *jpeg_dst_buf;
1685 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1686 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1687 	int ret, i, hw_id = 0;
1688 	struct mtk_jpeg_bs bs;
1689 	struct mtk_jpeg_fb fb;
1690 	unsigned long flags;
1691 
1692 	for (i = 0; i < MTK_JPEGDEC_HW_MAX; i++)
1693 		comp_jpeg[i] = jpeg->dec_hw_dev[i];
1694 	i = 0;
1695 
1696 retry_select:
1697 	hw_id = mtk_jpegdec_get_hw(ctx);
1698 	if (hw_id < 0) {
1699 		ret = wait_event_interruptible_timeout(jpeg->hw_wq,
1700 						       atomic_read(&jpeg->hw_rdy) > 0,
1701 						       MTK_JPEG_HW_TIMEOUT_MSEC);
1702 		if (ret != 0 || (i++ > MTK_JPEG_MAX_RETRY_TIME)) {
1703 			dev_err(jpeg->dev, "%s : %d, all HW are busy\n",
1704 				__func__, __LINE__);
1705 			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1706 			return;
1707 		}
1708 
1709 		goto retry_select;
1710 	}
1711 
1712 	atomic_dec(&jpeg->hw_rdy);
1713 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1714 	if (!src_buf)
1715 		goto getbuf_fail;
1716 
1717 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1718 	if (!dst_buf)
1719 		goto getbuf_fail;
1720 
1721 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
1722 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1723 	jpeg_dst_buf = mtk_jpeg_vb2_to_srcbuf(&dst_buf->vb2_buf);
1724 
1725 	if (mtk_jpeg_check_resolution_change(ctx,
1726 					     &jpeg_src_buf->dec_param)) {
1727 		mtk_jpeg_queue_src_chg_event(ctx);
1728 		ctx->state = MTK_JPEG_SOURCE_CHANGE;
1729 		goto getbuf_fail;
1730 	}
1731 
1732 	jpeg_src_buf->curr_ctx = ctx;
1733 	jpeg_src_buf->frame_num = ctx->total_frame_num;
1734 	jpeg_dst_buf->curr_ctx = ctx;
1735 	jpeg_dst_buf->frame_num = ctx->total_frame_num;
1736 
1737 	mtk_jpegdec_set_hw_param(ctx, hw_id, src_buf, dst_buf);
1738 	ret = pm_runtime_get_sync(comp_jpeg[hw_id]->dev);
1739 	if (ret < 0) {
1740 		dev_err(jpeg->dev, "%s : %d, pm_runtime_get_sync fail !!!\n",
1741 			__func__, __LINE__);
1742 		goto dec_end;
1743 	}
1744 
1745 	ret = clk_prepare_enable(comp_jpeg[hw_id]->jdec_clk.clks->clk);
1746 	if (ret) {
1747 		dev_err(jpeg->dev, "%s : %d, jpegdec clk_prepare_enable fail\n",
1748 			__func__, __LINE__);
1749 		goto clk_end;
1750 	}
1751 
1752 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1753 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1754 
1755 	schedule_delayed_work(&comp_jpeg[hw_id]->job_timeout_work,
1756 			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
1757 
1758 	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
1759 	if (mtk_jpeg_set_dec_dst(ctx,
1760 				 &jpeg_src_buf->dec_param,
1761 				 &dst_buf->vb2_buf, &fb)) {
1762 		dev_err(jpeg->dev, "%s : %d, mtk_jpeg_set_dec_dst fail\n",
1763 			__func__, __LINE__);
1764 		goto setdst_end;
1765 	}
1766 
1767 	spin_lock_irqsave(&comp_jpeg[hw_id]->hw_lock, flags);
1768 	ctx->total_frame_num++;
1769 	mtk_jpeg_dec_reset(comp_jpeg[hw_id]->reg_base);
1770 	mtk_jpeg_dec_set_config(comp_jpeg[hw_id]->reg_base,
1771 				&jpeg_src_buf->dec_param,
1772 				jpeg_src_buf->bs_size,
1773 				&bs,
1774 				&fb);
1775 	mtk_jpeg_dec_start(comp_jpeg[hw_id]->reg_base);
1776 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1777 	spin_unlock_irqrestore(&comp_jpeg[hw_id]->hw_lock, flags);
1778 
1779 	return;
1780 
1781 setdst_end:
1782 	clk_disable_unprepare(comp_jpeg[hw_id]->jdec_clk.clks->clk);
1783 clk_end:
1784 	pm_runtime_put(comp_jpeg[hw_id]->dev);
1785 dec_end:
1786 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1787 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1788 	v4l2_m2m_buf_done(src_buf, buf_state);
1789 	v4l2_m2m_buf_done(dst_buf, buf_state);
1790 getbuf_fail:
1791 	atomic_inc(&jpeg->hw_rdy);
1792 	mtk_jpegdec_put_hw(jpeg, hw_id);
1793 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1794 }
1795 
1796 static irqreturn_t mtk_jpeg_enc_irq(int irq, void *priv)
1797 {
1798 	struct mtk_jpeg_dev *jpeg = priv;
1799 	u32 irq_status;
1800 	irqreturn_t ret = IRQ_NONE;
1801 
1802 	cancel_delayed_work(&jpeg->job_timeout_work);
1803 
1804 	irq_status = readl(jpeg->reg_base + JPEG_ENC_INT_STS) &
1805 		     JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
1806 	if (irq_status)
1807 		writel(0, jpeg->reg_base + JPEG_ENC_INT_STS);
1808 
1809 	if (!(irq_status & JPEG_ENC_INT_STATUS_DONE))
1810 		return ret;
1811 
1812 	ret = mtk_jpeg_enc_done(jpeg);
1813 	return ret;
1814 }
1815 
1816 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
1817 {
1818 	struct mtk_jpeg_dev *jpeg = priv;
1819 	struct mtk_jpeg_ctx *ctx;
1820 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1821 	struct mtk_jpeg_src_buf *jpeg_src_buf;
1822 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1823 	u32	dec_irq_ret;
1824 	u32 dec_ret;
1825 	int i;
1826 
1827 	cancel_delayed_work(&jpeg->job_timeout_work);
1828 
1829 	dec_ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base);
1830 	dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
1831 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1832 	if (!ctx) {
1833 		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1834 		return IRQ_HANDLED;
1835 	}
1836 
1837 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1838 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1839 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1840 
1841 	if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
1842 		mtk_jpeg_dec_reset(jpeg->reg_base);
1843 
1844 	if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
1845 		dev_err(jpeg->dev, "decode failed\n");
1846 		goto dec_end;
1847 	}
1848 
1849 	for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
1850 		vb2_set_plane_payload(&dst_buf->vb2_buf, i,
1851 				      jpeg_src_buf->dec_param.comp_size[i]);
1852 
1853 	buf_state = VB2_BUF_STATE_DONE;
1854 
1855 dec_end:
1856 	v4l2_m2m_buf_done(src_buf, buf_state);
1857 	v4l2_m2m_buf_done(dst_buf, buf_state);
1858 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1859 	pm_runtime_put(ctx->jpeg->dev);
1860 	return IRQ_HANDLED;
1861 }
1862 
1863 static struct clk_bulk_data mtk_jpeg_clocks[] = {
1864 	{ .id = "jpgenc" },
1865 };
1866 
1867 static struct clk_bulk_data mt8173_jpeg_dec_clocks[] = {
1868 	{ .id = "jpgdec-smi" },
1869 	{ .id = "jpgdec" },
1870 };
1871 
1872 static const struct mtk_jpeg_variant mt8173_jpeg_drvdata = {
1873 	.clks = mt8173_jpeg_dec_clocks,
1874 	.num_clks = ARRAY_SIZE(mt8173_jpeg_dec_clocks),
1875 	.formats = mtk_jpeg_dec_formats,
1876 	.num_formats = MTK_JPEG_DEC_NUM_FORMATS,
1877 	.qops = &mtk_jpeg_dec_qops,
1878 	.irq_handler = mtk_jpeg_dec_irq,
1879 	.hw_reset = mtk_jpeg_dec_reset,
1880 	.m2m_ops = &mtk_jpeg_dec_m2m_ops,
1881 	.dev_name = "mtk-jpeg-dec",
1882 	.ioctl_ops = &mtk_jpeg_dec_ioctl_ops,
1883 	.out_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1884 	.cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M,
1885 };
1886 
1887 static const struct mtk_jpeg_variant mtk_jpeg_drvdata = {
1888 	.clks = mtk_jpeg_clocks,
1889 	.num_clks = ARRAY_SIZE(mtk_jpeg_clocks),
1890 	.formats = mtk_jpeg_enc_formats,
1891 	.num_formats = MTK_JPEG_ENC_NUM_FORMATS,
1892 	.qops = &mtk_jpeg_enc_qops,
1893 	.irq_handler = mtk_jpeg_enc_irq,
1894 	.hw_reset = mtk_jpeg_enc_reset,
1895 	.m2m_ops = &mtk_jpeg_enc_m2m_ops,
1896 	.dev_name = "mtk-jpeg-enc",
1897 	.ioctl_ops = &mtk_jpeg_enc_ioctl_ops,
1898 	.out_q_default_fourcc = V4L2_PIX_FMT_YUYV,
1899 	.cap_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1900 	.multi_core = false,
1901 };
1902 
1903 static struct mtk_jpeg_variant mtk8195_jpegenc_drvdata = {
1904 	.formats = mtk_jpeg_enc_formats,
1905 	.num_formats = MTK_JPEG_ENC_NUM_FORMATS,
1906 	.qops = &mtk_jpeg_enc_qops,
1907 	.m2m_ops = &mtk_jpeg_multicore_enc_m2m_ops,
1908 	.dev_name = "mtk-jpeg-enc",
1909 	.ioctl_ops = &mtk_jpeg_enc_ioctl_ops,
1910 	.out_q_default_fourcc = V4L2_PIX_FMT_YUYV,
1911 	.cap_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1912 	.multi_core = true,
1913 	.jpeg_worker = mtk_jpegenc_worker,
1914 };
1915 
1916 static const struct mtk_jpeg_variant mtk8195_jpegdec_drvdata = {
1917 	.formats = mtk_jpeg_dec_formats,
1918 	.num_formats = MTK_JPEG_DEC_NUM_FORMATS,
1919 	.qops = &mtk_jpeg_dec_qops,
1920 	.m2m_ops = &mtk_jpeg_multicore_dec_m2m_ops,
1921 	.dev_name = "mtk-jpeg-dec",
1922 	.ioctl_ops = &mtk_jpeg_dec_ioctl_ops,
1923 	.out_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1924 	.cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M,
1925 	.multi_core = true,
1926 	.jpeg_worker = mtk_jpegdec_worker,
1927 };
1928 
1929 static const struct of_device_id mtk_jpeg_match[] = {
1930 	{
1931 		.compatible = "mediatek,mt8173-jpgdec",
1932 		.data = &mt8173_jpeg_drvdata,
1933 	},
1934 	{
1935 		.compatible = "mediatek,mt2701-jpgdec",
1936 		.data = &mt8173_jpeg_drvdata,
1937 	},
1938 	{
1939 		.compatible = "mediatek,mtk-jpgenc",
1940 		.data = &mtk_jpeg_drvdata,
1941 	},
1942 	{
1943 		.compatible = "mediatek,mt8195-jpgenc",
1944 		.data = &mtk8195_jpegenc_drvdata,
1945 	},
1946 	{
1947 		.compatible = "mediatek,mt8195-jpgdec",
1948 		.data = &mtk8195_jpegdec_drvdata,
1949 	},
1950 	{},
1951 };
1952 
1953 MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1954 #endif
1955 
1956 static struct platform_driver mtk_jpeg_driver = {
1957 	.probe = mtk_jpeg_probe,
1958 	.remove_new = mtk_jpeg_remove,
1959 	.driver = {
1960 		.name           = MTK_JPEG_NAME,
1961 		.of_match_table = of_match_ptr(mtk_jpeg_match),
1962 		.pm             = &mtk_jpeg_pm_ops,
1963 	},
1964 };
1965 
1966 module_platform_driver(mtk_jpeg_driver);
1967 
1968 MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1969 MODULE_LICENSE("GPL v2");
1970