• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MTK_JPEG_MAX_RETRY_TIME 5000
108 
109 enum {
110 	MTK_JPEG_BUF_FLAGS_INIT			= 0,
111 	MTK_JPEG_BUF_FLAGS_LAST_FRAME		= 1,
112 };
113 
114 static int debug;
115 module_param(debug, int, 0644);
116 
ctrl_to_ctx(struct v4l2_ctrl * ctrl)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 
mtk_jpeg_fh_to_ctx(struct v4l2_fh * fh)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 
mtk_jpeg_vb2_to_srcbuf(struct vb2_buffer * vb)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 
mtk_jpeg_querycap(struct file * file,void * priv,struct v4l2_capability * cap)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 
vidioc_jpeg_enc_s_ctrl(struct v4l2_ctrl * ctrl)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 
mtk_jpeg_enc_ctrls_setup(struct mtk_jpeg_ctx * ctx)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 
mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt * mtk_jpeg_formats,int n,struct v4l2_fmtdesc * f,u32 type)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 
mtk_jpeg_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)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 
mtk_jpeg_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)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 
mtk_jpeg_get_q_data(struct mtk_jpeg_ctx * ctx,enum v4l2_buf_type type)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 *
mtk_jpeg_find_format(struct mtk_jpeg_fmt * mtk_jpeg_formats,int num_formats,u32 pixelformat,unsigned int fmt_type)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 
mtk_jpeg_try_fmt_mplane(struct v4l2_pix_format_mplane * pix_mp,struct mtk_jpeg_fmt * fmt)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 
mtk_jpeg_g_fmt_vid_mplane(struct file * file,void * priv,struct v4l2_format * f)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 
mtk_jpeg_try_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)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 
mtk_jpeg_try_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)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 
mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx * ctx,struct v4l2_format * f,unsigned int fmt_type)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 
mtk_jpeg_s_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)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 
mtk_jpeg_s_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)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 
mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx * ctx)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 
mtk_jpeg_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)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 
mtk_jpeg_enc_g_selection(struct file * file,void * priv,struct v4l2_selection * s)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 
mtk_jpeg_dec_g_selection(struct file * file,void * priv,struct v4l2_selection * s)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 
mtk_jpeg_enc_s_selection(struct file * file,void * priv,struct v4l2_selection * s)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 
mtk_jpeg_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)589 static int mtk_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
590 {
591 	struct v4l2_fh *fh = file->private_data;
592 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
593 	struct vb2_queue *vq;
594 	struct vb2_buffer *vb;
595 	struct mtk_jpeg_src_buf *jpeg_src_buf;
596 
597 	if (buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
598 		goto end;
599 
600 	vq = v4l2_m2m_get_vq(fh->m2m_ctx, buf->type);
601 	vb = vb2_get_buffer(vq, buf->index);
602 	if (!vb) {
603 		dev_err(ctx->jpeg->dev, "buffer not found\n");
604 		return -EINVAL;
605 	}
606 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
607 	jpeg_src_buf->bs_size = buf->m.planes[0].bytesused;
608 
609 end:
610 	return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
611 }
612 
613 static const struct v4l2_ioctl_ops mtk_jpeg_enc_ioctl_ops = {
614 	.vidioc_querycap                = mtk_jpeg_querycap,
615 	.vidioc_enum_fmt_vid_cap	= mtk_jpeg_enum_fmt_vid_cap,
616 	.vidioc_enum_fmt_vid_out	= mtk_jpeg_enum_fmt_vid_out,
617 	.vidioc_try_fmt_vid_cap_mplane	= mtk_jpeg_try_fmt_vid_cap_mplane,
618 	.vidioc_try_fmt_vid_out_mplane	= mtk_jpeg_try_fmt_vid_out_mplane,
619 	.vidioc_g_fmt_vid_cap_mplane    = mtk_jpeg_g_fmt_vid_mplane,
620 	.vidioc_g_fmt_vid_out_mplane    = mtk_jpeg_g_fmt_vid_mplane,
621 	.vidioc_s_fmt_vid_cap_mplane    = mtk_jpeg_s_fmt_vid_cap_mplane,
622 	.vidioc_s_fmt_vid_out_mplane    = mtk_jpeg_s_fmt_vid_out_mplane,
623 	.vidioc_qbuf                    = v4l2_m2m_ioctl_qbuf,
624 	.vidioc_subscribe_event         = mtk_jpeg_subscribe_event,
625 	.vidioc_g_selection		= mtk_jpeg_enc_g_selection,
626 	.vidioc_s_selection		= mtk_jpeg_enc_s_selection,
627 
628 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
629 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
630 	.vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
631 	.vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
632 	.vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
633 	.vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
634 	.vidioc_streamon                = v4l2_m2m_ioctl_streamon,
635 	.vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
636 
637 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
638 
639 	.vidioc_encoder_cmd		= v4l2_m2m_ioctl_encoder_cmd,
640 	.vidioc_try_encoder_cmd		= v4l2_m2m_ioctl_try_encoder_cmd,
641 };
642 
643 static const struct v4l2_ioctl_ops mtk_jpeg_dec_ioctl_ops = {
644 	.vidioc_querycap                = mtk_jpeg_querycap,
645 	.vidioc_enum_fmt_vid_cap	= mtk_jpeg_enum_fmt_vid_cap,
646 	.vidioc_enum_fmt_vid_out	= mtk_jpeg_enum_fmt_vid_out,
647 	.vidioc_try_fmt_vid_cap_mplane	= mtk_jpeg_try_fmt_vid_cap_mplane,
648 	.vidioc_try_fmt_vid_out_mplane	= mtk_jpeg_try_fmt_vid_out_mplane,
649 	.vidioc_g_fmt_vid_cap_mplane    = mtk_jpeg_g_fmt_vid_mplane,
650 	.vidioc_g_fmt_vid_out_mplane    = mtk_jpeg_g_fmt_vid_mplane,
651 	.vidioc_s_fmt_vid_cap_mplane    = mtk_jpeg_s_fmt_vid_cap_mplane,
652 	.vidioc_s_fmt_vid_out_mplane    = mtk_jpeg_s_fmt_vid_out_mplane,
653 	.vidioc_qbuf                    = mtk_jpeg_qbuf,
654 	.vidioc_subscribe_event         = mtk_jpeg_subscribe_event,
655 	.vidioc_g_selection		= mtk_jpeg_dec_g_selection,
656 
657 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
658 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
659 	.vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
660 	.vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
661 	.vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
662 	.vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
663 	.vidioc_streamon                = v4l2_m2m_ioctl_streamon,
664 	.vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
665 
666 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
667 
668 	.vidioc_decoder_cmd = v4l2_m2m_ioctl_decoder_cmd,
669 	.vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
670 };
671 
mtk_jpeg_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_ctxs[])672 static int mtk_jpeg_queue_setup(struct vb2_queue *q,
673 				unsigned int *num_buffers,
674 				unsigned int *num_planes,
675 				unsigned int sizes[],
676 				struct device *alloc_ctxs[])
677 {
678 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
679 	struct mtk_jpeg_q_data *q_data = NULL;
680 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
681 	int i;
682 
683 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
684 		 q->type, *num_buffers);
685 
686 	q_data = mtk_jpeg_get_q_data(ctx, q->type);
687 	if (!q_data)
688 		return -EINVAL;
689 
690 	if (*num_planes) {
691 		for (i = 0; i < *num_planes; i++)
692 			if (sizes[i] < q_data->pix_mp.plane_fmt[i].sizeimage)
693 				return -EINVAL;
694 		return 0;
695 	}
696 
697 	*num_planes = q_data->fmt->colplanes;
698 	for (i = 0; i < q_data->fmt->colplanes; i++) {
699 		sizes[i] =  q_data->pix_mp.plane_fmt[i].sizeimage;
700 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n",
701 			 i, sizes[i]);
702 	}
703 
704 	return 0;
705 }
706 
mtk_jpeg_buf_prepare(struct vb2_buffer * vb)707 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
708 {
709 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
710 	struct mtk_jpeg_q_data *q_data = NULL;
711 	struct v4l2_plane_pix_format plane_fmt = {};
712 	int i;
713 
714 	q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
715 	if (!q_data)
716 		return -EINVAL;
717 
718 	for (i = 0; i < q_data->fmt->colplanes; i++) {
719 		plane_fmt = q_data->pix_mp.plane_fmt[i];
720 		if (ctx->enable_exif &&
721 		    q_data->fmt->fourcc == V4L2_PIX_FMT_JPEG)
722 			vb2_set_plane_payload(vb, i, plane_fmt.sizeimage +
723 					      MTK_JPEG_MAX_EXIF_SIZE);
724 		else
725 			vb2_set_plane_payload(vb, i,  plane_fmt.sizeimage);
726 	}
727 
728 	return 0;
729 }
730 
mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx * ctx,struct mtk_jpeg_dec_param * param)731 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
732 					     struct mtk_jpeg_dec_param *param)
733 {
734 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
735 	struct mtk_jpeg_q_data *q_data;
736 
737 	q_data = &ctx->out_q;
738 	if (q_data->pix_mp.width != param->pic_w ||
739 	    q_data->pix_mp.height != param->pic_h) {
740 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n");
741 		return true;
742 	}
743 
744 	q_data = &ctx->cap_q;
745 	if (q_data->fmt !=
746 	    mtk_jpeg_find_format(jpeg->variant->formats,
747 				 jpeg->variant->num_formats, param->dst_fourcc,
748 				 MTK_JPEG_FMT_FLAG_CAPTURE)) {
749 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n");
750 		return true;
751 	}
752 	return false;
753 }
754 
mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx * ctx,struct mtk_jpeg_dec_param * param)755 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
756 				    struct mtk_jpeg_dec_param *param)
757 {
758 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
759 	struct mtk_jpeg_q_data *q_data;
760 	int i;
761 
762 	q_data = &ctx->out_q;
763 	q_data->pix_mp.width = param->pic_w;
764 	q_data->pix_mp.height = param->pic_h;
765 
766 	q_data = &ctx->cap_q;
767 	q_data->pix_mp.width = param->dec_w;
768 	q_data->pix_mp.height = param->dec_h;
769 	q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
770 					   jpeg->variant->num_formats,
771 					   param->dst_fourcc,
772 					   MTK_JPEG_FMT_FLAG_CAPTURE);
773 
774 	for (i = 0; i < q_data->fmt->colplanes; i++) {
775 		q_data->pix_mp.plane_fmt[i].bytesperline = param->mem_stride[i];
776 		q_data->pix_mp.plane_fmt[i].sizeimage = param->comp_size[i];
777 	}
778 
779 	v4l2_dbg(1, debug, &jpeg->v4l2_dev,
780 		 "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n",
781 		 (param->dst_fourcc & 0xff),
782 		 (param->dst_fourcc >>  8 & 0xff),
783 		 (param->dst_fourcc >> 16 & 0xff),
784 		 (param->dst_fourcc >> 24 & 0xff),
785 		 param->pic_w, param->pic_h,
786 		 param->dec_w, param->dec_h);
787 }
788 
mtk_jpeg_enc_buf_queue(struct vb2_buffer * vb)789 static void mtk_jpeg_enc_buf_queue(struct vb2_buffer *vb)
790 {
791 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
792 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
793 
794 	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
795 		 vb->vb2_queue->type, vb->index, vb);
796 
797 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
798 }
799 
mtk_jpeg_dec_buf_queue(struct vb2_buffer * vb)800 static void mtk_jpeg_dec_buf_queue(struct vb2_buffer *vb)
801 {
802 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
803 	struct mtk_jpeg_dec_param *param;
804 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
805 	struct mtk_jpeg_src_buf *jpeg_src_buf;
806 	bool header_valid;
807 
808 	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
809 		 vb->vb2_queue->type, vb->index, vb);
810 
811 	if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
812 		goto end;
813 
814 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
815 	param = &jpeg_src_buf->dec_param;
816 	memset(param, 0, sizeof(*param));
817 
818 	header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
819 				      vb2_get_plane_payload(vb, 0));
820 	if (!header_valid) {
821 		v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
822 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
823 		return;
824 	}
825 
826 	if (ctx->state == MTK_JPEG_INIT) {
827 		struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
828 			ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
829 
830 		mtk_jpeg_queue_src_chg_event(ctx);
831 		mtk_jpeg_set_queue_data(ctx, param);
832 		ctx->state = vb2_is_streaming(dst_vq) ?
833 				MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
834 	}
835 end:
836 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
837 }
838 
mtk_jpeg_buf_remove(struct mtk_jpeg_ctx * ctx,enum v4l2_buf_type type)839 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
840 				 enum v4l2_buf_type type)
841 {
842 	if (V4L2_TYPE_IS_OUTPUT(type))
843 		return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
844 	else
845 		return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
846 }
847 
mtk_jpeg_enc_stop_streaming(struct vb2_queue * q)848 static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q)
849 {
850 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
851 	struct vb2_v4l2_buffer *vb;
852 
853 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
854 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
855 }
856 
mtk_jpeg_dec_stop_streaming(struct vb2_queue * q)857 static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q)
858 {
859 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
860 	struct vb2_v4l2_buffer *vb;
861 
862 	/*
863 	 * STREAMOFF is an acknowledgment for source change event.
864 	 * Before STREAMOFF, we still have to return the old resolution and
865 	 * subsampling. Update capture queue when the stream is off.
866 	 */
867 	if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
868 	    V4L2_TYPE_IS_CAPTURE(q->type)) {
869 		struct mtk_jpeg_src_buf *src_buf;
870 
871 		vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
872 		src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
873 		mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
874 		ctx->state = MTK_JPEG_RUNNING;
875 	} else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
876 		ctx->state = MTK_JPEG_INIT;
877 	}
878 
879 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
880 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
881 }
882 
883 static const struct vb2_ops mtk_jpeg_dec_qops = {
884 	.queue_setup        = mtk_jpeg_queue_setup,
885 	.buf_prepare        = mtk_jpeg_buf_prepare,
886 	.buf_queue          = mtk_jpeg_dec_buf_queue,
887 	.wait_prepare       = vb2_ops_wait_prepare,
888 	.wait_finish        = vb2_ops_wait_finish,
889 	.stop_streaming     = mtk_jpeg_dec_stop_streaming,
890 };
891 
892 static const struct vb2_ops mtk_jpeg_enc_qops = {
893 	.queue_setup        = mtk_jpeg_queue_setup,
894 	.buf_prepare        = mtk_jpeg_buf_prepare,
895 	.buf_queue          = mtk_jpeg_enc_buf_queue,
896 	.wait_prepare       = vb2_ops_wait_prepare,
897 	.wait_finish        = vb2_ops_wait_finish,
898 	.stop_streaming     = mtk_jpeg_enc_stop_streaming,
899 };
900 
mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx * ctx,struct vb2_buffer * src_buf,struct mtk_jpeg_bs * bs)901 static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
902 				 struct vb2_buffer *src_buf,
903 				 struct mtk_jpeg_bs *bs)
904 {
905 	bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
906 	bs->end_addr = bs->str_addr +
907 		       round_up(vb2_get_plane_payload(src_buf, 0), 16);
908 	bs->size = round_up(vb2_plane_size(src_buf, 0), 128);
909 }
910 
mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx * ctx,struct mtk_jpeg_dec_param * param,struct vb2_buffer * dst_buf,struct mtk_jpeg_fb * fb)911 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
912 				struct mtk_jpeg_dec_param *param,
913 				struct vb2_buffer *dst_buf,
914 				struct mtk_jpeg_fb *fb)
915 {
916 	int i;
917 
918 	if (param->comp_num != dst_buf->num_planes) {
919 		dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n",
920 			param->comp_num, dst_buf->num_planes);
921 		return -EINVAL;
922 	}
923 
924 	for (i = 0; i < dst_buf->num_planes; i++) {
925 		if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) {
926 			dev_err(ctx->jpeg->dev,
927 				"buffer size is underflow (%lu < %u)\n",
928 				vb2_plane_size(dst_buf, 0),
929 				param->comp_size[i]);
930 			return -EINVAL;
931 		}
932 		fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
933 	}
934 
935 	return 0;
936 }
937 
mtk_jpeg_enc_device_run(void * priv)938 static void mtk_jpeg_enc_device_run(void *priv)
939 {
940 	struct mtk_jpeg_ctx *ctx = priv;
941 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
942 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
943 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
944 	unsigned long flags;
945 	int ret;
946 
947 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
948 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
949 
950 	ret = pm_runtime_resume_and_get(jpeg->dev);
951 	if (ret < 0)
952 		goto enc_end;
953 
954 	schedule_delayed_work(&jpeg->job_timeout_work,
955 			msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
956 
957 	spin_lock_irqsave(&jpeg->hw_lock, flags);
958 
959 	/*
960 	 * Resetting the hardware every frame is to ensure that all the
961 	 * registers are cleared. This is a hardware requirement.
962 	 */
963 	mtk_jpeg_enc_reset(jpeg->reg_base);
964 
965 	mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf);
966 	mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf);
967 	mtk_jpeg_set_enc_params(ctx, jpeg->reg_base);
968 	mtk_jpeg_enc_start(jpeg->reg_base);
969 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
970 	return;
971 
972 enc_end:
973 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
974 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
975 	v4l2_m2m_buf_done(src_buf, buf_state);
976 	v4l2_m2m_buf_done(dst_buf, buf_state);
977 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
978 }
979 
mtk_jpeg_multicore_enc_device_run(void * priv)980 static void mtk_jpeg_multicore_enc_device_run(void *priv)
981 {
982 	struct mtk_jpeg_ctx *ctx = priv;
983 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
984 
985 	queue_work(jpeg->workqueue, &ctx->jpeg_work);
986 }
987 
mtk_jpeg_multicore_dec_device_run(void * priv)988 static void mtk_jpeg_multicore_dec_device_run(void *priv)
989 {
990 	struct mtk_jpeg_ctx *ctx = priv;
991 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
992 
993 	queue_work(jpeg->workqueue, &ctx->jpeg_work);
994 }
995 
mtk_jpeg_dec_device_run(void * priv)996 static void mtk_jpeg_dec_device_run(void *priv)
997 {
998 	struct mtk_jpeg_ctx *ctx = priv;
999 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1000 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1001 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1002 	unsigned long flags;
1003 	struct mtk_jpeg_src_buf *jpeg_src_buf;
1004 	struct mtk_jpeg_bs bs;
1005 	struct mtk_jpeg_fb fb;
1006 	int ret;
1007 
1008 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1009 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1010 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1011 
1012 	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
1013 		mtk_jpeg_queue_src_chg_event(ctx);
1014 		ctx->state = MTK_JPEG_SOURCE_CHANGE;
1015 		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1016 		return;
1017 	}
1018 
1019 	ret = pm_runtime_resume_and_get(jpeg->dev);
1020 	if (ret < 0)
1021 		goto dec_end;
1022 
1023 	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
1024 	if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
1025 		goto dec_end;
1026 
1027 	schedule_delayed_work(&jpeg->job_timeout_work,
1028 			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
1029 
1030 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1031 	mtk_jpeg_dec_reset(jpeg->reg_base);
1032 	mtk_jpeg_dec_set_config(jpeg->reg_base,
1033 				&jpeg_src_buf->dec_param,
1034 				jpeg_src_buf->bs_size,
1035 				&bs,
1036 				&fb);
1037 	mtk_jpeg_dec_start(jpeg->reg_base);
1038 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1039 	return;
1040 
1041 dec_end:
1042 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1043 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1044 	v4l2_m2m_buf_done(src_buf, buf_state);
1045 	v4l2_m2m_buf_done(dst_buf, buf_state);
1046 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1047 }
1048 
mtk_jpeg_dec_job_ready(void * priv)1049 static int mtk_jpeg_dec_job_ready(void *priv)
1050 {
1051 	struct mtk_jpeg_ctx *ctx = priv;
1052 
1053 	return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
1054 }
1055 
1056 static const struct v4l2_m2m_ops mtk_jpeg_enc_m2m_ops = {
1057 	.device_run = mtk_jpeg_enc_device_run,
1058 };
1059 
1060 static const struct v4l2_m2m_ops mtk_jpeg_multicore_enc_m2m_ops = {
1061 	.device_run = mtk_jpeg_multicore_enc_device_run,
1062 };
1063 
1064 static const struct v4l2_m2m_ops mtk_jpeg_multicore_dec_m2m_ops = {
1065 	.device_run = mtk_jpeg_multicore_dec_device_run,
1066 };
1067 
1068 static const struct v4l2_m2m_ops mtk_jpeg_dec_m2m_ops = {
1069 	.device_run = mtk_jpeg_dec_device_run,
1070 	.job_ready  = mtk_jpeg_dec_job_ready,
1071 };
1072 
mtk_jpeg_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1073 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
1074 			       struct vb2_queue *dst_vq)
1075 {
1076 	struct mtk_jpeg_ctx *ctx = priv;
1077 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1078 	int ret;
1079 
1080 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1081 	src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1082 	src_vq->drv_priv = ctx;
1083 	src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf);
1084 	src_vq->ops = jpeg->variant->qops;
1085 	src_vq->mem_ops = &vb2_dma_contig_memops;
1086 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1087 	src_vq->lock = &ctx->jpeg->lock;
1088 	src_vq->dev = ctx->jpeg->dev;
1089 	ret = vb2_queue_init(src_vq);
1090 	if (ret)
1091 		return ret;
1092 
1093 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1094 	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1095 	dst_vq->drv_priv = ctx;
1096 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1097 	dst_vq->ops = jpeg->variant->qops;
1098 	dst_vq->mem_ops = &vb2_dma_contig_memops;
1099 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1100 	dst_vq->lock = &ctx->jpeg->lock;
1101 	dst_vq->dev = ctx->jpeg->dev;
1102 	ret = vb2_queue_init(dst_vq);
1103 
1104 	return ret;
1105 }
1106 
mtk_jpeg_clk_on(struct mtk_jpeg_dev * jpeg)1107 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
1108 {
1109 	int ret;
1110 
1111 	ret = clk_bulk_prepare_enable(jpeg->variant->num_clks,
1112 				      jpeg->variant->clks);
1113 	if (ret)
1114 		dev_err(jpeg->dev, "Failed to open jpeg clk: %d\n", ret);
1115 }
1116 
mtk_jpeg_clk_off(struct mtk_jpeg_dev * jpeg)1117 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
1118 {
1119 	clk_bulk_disable_unprepare(jpeg->variant->num_clks,
1120 				   jpeg->variant->clks);
1121 }
1122 
mtk_jpeg_set_default_params(struct mtk_jpeg_ctx * ctx)1123 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
1124 {
1125 	struct mtk_jpeg_q_data *q = &ctx->out_q;
1126 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1127 
1128 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1129 	q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1130 	q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1131 	q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1132 	q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1133 
1134 	q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1135 				      jpeg->variant->num_formats,
1136 				      jpeg->variant->out_q_default_fourcc,
1137 				      MTK_JPEG_FMT_FLAG_OUTPUT);
1138 	q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1139 	q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1140 	mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1141 
1142 	q = &ctx->cap_q;
1143 	q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1144 				      jpeg->variant->num_formats,
1145 				      jpeg->variant->cap_q_default_fourcc,
1146 				      MTK_JPEG_FMT_FLAG_CAPTURE);
1147 	q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1148 	q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1149 	q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1150 	q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1151 	q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1152 	q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1153 
1154 	mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1155 }
1156 
mtk_jpeg_open(struct file * file)1157 static int mtk_jpeg_open(struct file *file)
1158 {
1159 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1160 	struct video_device *vfd = video_devdata(file);
1161 	struct mtk_jpeg_ctx *ctx;
1162 	int ret = 0;
1163 
1164 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1165 	if (!ctx)
1166 		return -ENOMEM;
1167 
1168 	if (mutex_lock_interruptible(&jpeg->lock)) {
1169 		ret = -ERESTARTSYS;
1170 		goto free;
1171 	}
1172 
1173 	INIT_WORK(&ctx->jpeg_work, jpeg->variant->jpeg_worker);
1174 	INIT_LIST_HEAD(&ctx->dst_done_queue);
1175 	spin_lock_init(&ctx->done_queue_lock);
1176 	v4l2_fh_init(&ctx->fh, vfd);
1177 	file->private_data = &ctx->fh;
1178 	v4l2_fh_add(&ctx->fh);
1179 
1180 	ctx->jpeg = jpeg;
1181 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx,
1182 					    mtk_jpeg_queue_init);
1183 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1184 		ret = PTR_ERR(ctx->fh.m2m_ctx);
1185 		goto error;
1186 	}
1187 
1188 	if (jpeg->variant->cap_q_default_fourcc == V4L2_PIX_FMT_JPEG) {
1189 		ret = mtk_jpeg_enc_ctrls_setup(ctx);
1190 		if (ret) {
1191 			v4l2_err(&jpeg->v4l2_dev, "Failed to setup jpeg enc controls\n");
1192 			goto error;
1193 		}
1194 	} else {
1195 		v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 0);
1196 	}
1197 
1198 	mtk_jpeg_set_default_params(ctx);
1199 	mutex_unlock(&jpeg->lock);
1200 	return 0;
1201 
1202 error:
1203 	v4l2_fh_del(&ctx->fh);
1204 	v4l2_fh_exit(&ctx->fh);
1205 	mutex_unlock(&jpeg->lock);
1206 free:
1207 	kfree(ctx);
1208 	return ret;
1209 }
1210 
mtk_jpeg_release(struct file * file)1211 static int mtk_jpeg_release(struct file *file)
1212 {
1213 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1214 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
1215 
1216 	mutex_lock(&jpeg->lock);
1217 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1218 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1219 	v4l2_fh_del(&ctx->fh);
1220 	v4l2_fh_exit(&ctx->fh);
1221 	kfree(ctx);
1222 	mutex_unlock(&jpeg->lock);
1223 	return 0;
1224 }
1225 
1226 static const struct v4l2_file_operations mtk_jpeg_fops = {
1227 	.owner          = THIS_MODULE,
1228 	.open           = mtk_jpeg_open,
1229 	.release        = mtk_jpeg_release,
1230 	.poll           = v4l2_m2m_fop_poll,
1231 	.unlocked_ioctl = video_ioctl2,
1232 	.mmap           = v4l2_m2m_fop_mmap,
1233 };
1234 
mtk_jpeg_job_timeout_work(struct work_struct * work)1235 static void mtk_jpeg_job_timeout_work(struct work_struct *work)
1236 {
1237 	struct mtk_jpeg_dev *jpeg = container_of(work, struct mtk_jpeg_dev,
1238 						 job_timeout_work.work);
1239 	struct mtk_jpeg_ctx *ctx;
1240 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1241 
1242 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1243 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1244 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1245 
1246 	jpeg->variant->hw_reset(jpeg->reg_base);
1247 
1248 	pm_runtime_put(jpeg->dev);
1249 
1250 	v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1251 	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1252 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1253 }
1254 
mtk_jpeg_single_core_init(struct platform_device * pdev,struct mtk_jpeg_dev * jpeg_dev)1255 static int mtk_jpeg_single_core_init(struct platform_device *pdev,
1256 				     struct mtk_jpeg_dev *jpeg_dev)
1257 {
1258 	struct mtk_jpeg_dev *jpeg = jpeg_dev;
1259 	int jpeg_irq, ret;
1260 
1261 	INIT_DELAYED_WORK(&jpeg->job_timeout_work,
1262 			  mtk_jpeg_job_timeout_work);
1263 
1264 	jpeg->reg_base = devm_platform_ioremap_resource(pdev, 0);
1265 	if (IS_ERR(jpeg->reg_base)) {
1266 		ret = PTR_ERR(jpeg->reg_base);
1267 		return ret;
1268 	}
1269 
1270 	jpeg_irq = platform_get_irq(pdev, 0);
1271 	if (jpeg_irq < 0)
1272 		return jpeg_irq;
1273 
1274 	ret = devm_request_irq(&pdev->dev,
1275 			       jpeg_irq,
1276 			       jpeg->variant->irq_handler,
1277 			       0,
1278 			       pdev->name, jpeg);
1279 	if (ret) {
1280 		dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n",
1281 			jpeg_irq, ret);
1282 		return ret;
1283 	}
1284 
1285 	ret = devm_clk_bulk_get(jpeg->dev,
1286 				jpeg->variant->num_clks,
1287 				jpeg->variant->clks);
1288 	if (ret) {
1289 		dev_err(&pdev->dev, "Failed to init clk\n");
1290 		return ret;
1291 	}
1292 
1293 	return 0;
1294 }
1295 
mtk_jpeg_destroy_workqueue(void * data)1296 static void mtk_jpeg_destroy_workqueue(void *data)
1297 {
1298 	destroy_workqueue(data);
1299 }
1300 
mtk_jpeg_probe(struct platform_device * pdev)1301 static int mtk_jpeg_probe(struct platform_device *pdev)
1302 {
1303 	struct mtk_jpeg_dev *jpeg;
1304 	struct device_node *child;
1305 	int num_child = 0;
1306 	int ret;
1307 
1308 	jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1309 	if (!jpeg)
1310 		return -ENOMEM;
1311 
1312 	mutex_init(&jpeg->lock);
1313 	spin_lock_init(&jpeg->hw_lock);
1314 	jpeg->dev = &pdev->dev;
1315 	jpeg->variant = of_device_get_match_data(jpeg->dev);
1316 
1317 	platform_set_drvdata(pdev, jpeg);
1318 
1319 	ret = devm_of_platform_populate(&pdev->dev);
1320 	if (ret) {
1321 		v4l2_err(&jpeg->v4l2_dev, "Master of platform populate failed.");
1322 		return -EINVAL;
1323 	}
1324 
1325 	if (!jpeg->variant->multi_core) {
1326 		ret = mtk_jpeg_single_core_init(pdev, jpeg);
1327 		if (ret) {
1328 			v4l2_err(&jpeg->v4l2_dev, "mtk_jpeg_single_core_init failed.");
1329 			return -EINVAL;
1330 		}
1331 	} else {
1332 		init_waitqueue_head(&jpeg->hw_wq);
1333 
1334 		for_each_child_of_node(pdev->dev.of_node, child)
1335 			num_child++;
1336 
1337 		atomic_set(&jpeg->hw_rdy, num_child);
1338 		atomic_set(&jpeg->hw_index, 0);
1339 
1340 		jpeg->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,
1341 							  WQ_MEM_RECLAIM
1342 							  | WQ_FREEZABLE);
1343 		if (!jpeg->workqueue)
1344 			return -EINVAL;
1345 		ret = devm_add_action_or_reset(&pdev->dev,
1346 					       mtk_jpeg_destroy_workqueue,
1347 					       jpeg->workqueue);
1348 		if (ret)
1349 			return ret;
1350 	}
1351 
1352 	ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1353 	if (ret) {
1354 		dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1355 		return -EINVAL;
1356 	}
1357 
1358 	jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
1359 
1360 	if (IS_ERR(jpeg->m2m_dev)) {
1361 		v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1362 		ret = PTR_ERR(jpeg->m2m_dev);
1363 		goto err_m2m_init;
1364 	}
1365 
1366 	jpeg->vdev = video_device_alloc();
1367 	if (!jpeg->vdev) {
1368 		ret = -ENOMEM;
1369 		goto err_vfd_jpeg_alloc;
1370 	}
1371 	snprintf(jpeg->vdev->name, sizeof(jpeg->vdev->name),
1372 		 "%s", jpeg->variant->dev_name);
1373 	jpeg->vdev->fops = &mtk_jpeg_fops;
1374 	jpeg->vdev->ioctl_ops = jpeg->variant->ioctl_ops;
1375 	jpeg->vdev->minor = -1;
1376 	jpeg->vdev->release = video_device_release;
1377 	jpeg->vdev->lock = &jpeg->lock;
1378 	jpeg->vdev->v4l2_dev = &jpeg->v4l2_dev;
1379 	jpeg->vdev->vfl_dir = VFL_DIR_M2M;
1380 	jpeg->vdev->device_caps = V4L2_CAP_STREAMING |
1381 				  V4L2_CAP_VIDEO_M2M_MPLANE;
1382 
1383 	ret = video_register_device(jpeg->vdev, VFL_TYPE_VIDEO, -1);
1384 	if (ret) {
1385 		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1386 		goto err_vfd_jpeg_register;
1387 	}
1388 
1389 	video_set_drvdata(jpeg->vdev, jpeg);
1390 	v4l2_info(&jpeg->v4l2_dev,
1391 		  "%s device registered as /dev/video%d (%d,%d)\n",
1392 		  jpeg->variant->dev_name, jpeg->vdev->num,
1393 		  VIDEO_MAJOR, jpeg->vdev->minor);
1394 
1395 	pm_runtime_enable(&pdev->dev);
1396 
1397 	return 0;
1398 
1399 err_vfd_jpeg_register:
1400 	video_device_release(jpeg->vdev);
1401 
1402 err_vfd_jpeg_alloc:
1403 	v4l2_m2m_release(jpeg->m2m_dev);
1404 
1405 err_m2m_init:
1406 	v4l2_device_unregister(&jpeg->v4l2_dev);
1407 
1408 	return ret;
1409 }
1410 
mtk_jpeg_remove(struct platform_device * pdev)1411 static void 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 
mtk_jpeg_pm_suspend(struct device * dev)1421 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1422 {
1423 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1424 
1425 	mtk_jpeg_clk_off(jpeg);
1426 
1427 	return 0;
1428 }
1429 
mtk_jpeg_pm_resume(struct device * dev)1430 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1431 {
1432 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1433 
1434 	mtk_jpeg_clk_on(jpeg);
1435 
1436 	return 0;
1437 }
1438 
mtk_jpeg_suspend(struct device * dev)1439 static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1440 {
1441 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1442 
1443 	v4l2_m2m_suspend(jpeg->m2m_dev);
1444 	return pm_runtime_force_suspend(dev);
1445 }
1446 
mtk_jpeg_resume(struct device * dev)1447 static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1448 {
1449 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1450 	int ret;
1451 
1452 	ret = pm_runtime_force_resume(dev);
1453 	if (ret < 0)
1454 		return ret;
1455 
1456 	v4l2_m2m_resume(jpeg->m2m_dev);
1457 	return ret;
1458 }
1459 
1460 static const struct dev_pm_ops mtk_jpeg_pm_ops = {
1461 	SET_SYSTEM_SLEEP_PM_OPS(mtk_jpeg_suspend, mtk_jpeg_resume)
1462 	SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL)
1463 };
1464 
mtk_jpegenc_get_hw(struct mtk_jpeg_ctx * ctx)1465 static int mtk_jpegenc_get_hw(struct mtk_jpeg_ctx *ctx)
1466 {
1467 	struct mtk_jpegenc_comp_dev *comp_jpeg;
1468 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1469 	unsigned long flags;
1470 	int hw_id = -1;
1471 	int i;
1472 
1473 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1474 	for (i = 0; i < MTK_JPEGENC_HW_MAX; i++) {
1475 		comp_jpeg = jpeg->enc_hw_dev[i];
1476 		if (comp_jpeg->hw_state == MTK_JPEG_HW_IDLE) {
1477 			hw_id = i;
1478 			comp_jpeg->hw_state = MTK_JPEG_HW_BUSY;
1479 			break;
1480 		}
1481 	}
1482 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1483 
1484 	return hw_id;
1485 }
1486 
mtk_jpegenc_set_hw_param(struct mtk_jpeg_ctx * ctx,int hw_id,struct vb2_v4l2_buffer * src_buf,struct vb2_v4l2_buffer * dst_buf)1487 static int mtk_jpegenc_set_hw_param(struct mtk_jpeg_ctx *ctx,
1488 				    int hw_id,
1489 				    struct vb2_v4l2_buffer *src_buf,
1490 				    struct vb2_v4l2_buffer *dst_buf)
1491 {
1492 	struct mtk_jpegenc_comp_dev *jpeg = ctx->jpeg->enc_hw_dev[hw_id];
1493 
1494 	jpeg->hw_param.curr_ctx = ctx;
1495 	jpeg->hw_param.src_buffer = src_buf;
1496 	jpeg->hw_param.dst_buffer = dst_buf;
1497 
1498 	return 0;
1499 }
1500 
mtk_jpegenc_put_hw(struct mtk_jpeg_dev * jpeg,int hw_id)1501 static int mtk_jpegenc_put_hw(struct mtk_jpeg_dev *jpeg, int hw_id)
1502 {
1503 	unsigned long flags;
1504 
1505 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1506 	jpeg->enc_hw_dev[hw_id]->hw_state = MTK_JPEG_HW_IDLE;
1507 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1508 
1509 	return 0;
1510 }
1511 
mtk_jpegdec_get_hw(struct mtk_jpeg_ctx * ctx)1512 static int mtk_jpegdec_get_hw(struct mtk_jpeg_ctx *ctx)
1513 {
1514 	struct mtk_jpegdec_comp_dev *comp_jpeg;
1515 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1516 	unsigned long flags;
1517 	int hw_id = -1;
1518 	int i;
1519 
1520 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1521 	for (i = 0; i < MTK_JPEGDEC_HW_MAX; i++) {
1522 		comp_jpeg = jpeg->dec_hw_dev[i];
1523 		if (comp_jpeg->hw_state == MTK_JPEG_HW_IDLE) {
1524 			hw_id = i;
1525 			comp_jpeg->hw_state = MTK_JPEG_HW_BUSY;
1526 			break;
1527 		}
1528 	}
1529 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1530 
1531 	return hw_id;
1532 }
1533 
mtk_jpegdec_put_hw(struct mtk_jpeg_dev * jpeg,int hw_id)1534 static int mtk_jpegdec_put_hw(struct mtk_jpeg_dev *jpeg, int hw_id)
1535 {
1536 	unsigned long flags;
1537 
1538 	spin_lock_irqsave(&jpeg->hw_lock, flags);
1539 	jpeg->dec_hw_dev[hw_id]->hw_state =
1540 		MTK_JPEG_HW_IDLE;
1541 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
1542 
1543 	return 0;
1544 }
1545 
mtk_jpegdec_set_hw_param(struct mtk_jpeg_ctx * ctx,int hw_id,struct vb2_v4l2_buffer * src_buf,struct vb2_v4l2_buffer * dst_buf)1546 static int mtk_jpegdec_set_hw_param(struct mtk_jpeg_ctx *ctx,
1547 				    int hw_id,
1548 				    struct vb2_v4l2_buffer *src_buf,
1549 				    struct vb2_v4l2_buffer *dst_buf)
1550 {
1551 	struct mtk_jpegdec_comp_dev *jpeg =
1552 		ctx->jpeg->dec_hw_dev[hw_id];
1553 
1554 	jpeg->hw_param.curr_ctx = ctx;
1555 	jpeg->hw_param.src_buffer = src_buf;
1556 	jpeg->hw_param.dst_buffer = dst_buf;
1557 
1558 	return 0;
1559 }
1560 
mtk_jpeg_enc_done(struct mtk_jpeg_dev * jpeg)1561 static irqreturn_t mtk_jpeg_enc_done(struct mtk_jpeg_dev *jpeg)
1562 {
1563 	struct mtk_jpeg_ctx *ctx;
1564 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1565 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1566 	u32 result_size;
1567 
1568 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1569 	if (!ctx) {
1570 		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1571 		return IRQ_HANDLED;
1572 	}
1573 
1574 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1575 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1576 
1577 	result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
1578 	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, result_size);
1579 
1580 	buf_state = VB2_BUF_STATE_DONE;
1581 
1582 	v4l2_m2m_buf_done(src_buf, buf_state);
1583 	v4l2_m2m_buf_done(dst_buf, buf_state);
1584 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1585 	pm_runtime_put(ctx->jpeg->dev);
1586 	return IRQ_HANDLED;
1587 }
1588 
mtk_jpegenc_worker(struct work_struct * work)1589 static void mtk_jpegenc_worker(struct work_struct *work)
1590 {
1591 	struct mtk_jpegenc_comp_dev *comp_jpeg[MTK_JPEGENC_HW_MAX];
1592 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1593 	struct mtk_jpeg_src_buf *jpeg_dst_buf;
1594 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1595 	int ret, i, hw_id = 0;
1596 	unsigned long flags;
1597 
1598 	struct mtk_jpeg_ctx *ctx = container_of(work,
1599 		struct mtk_jpeg_ctx,
1600 		jpeg_work);
1601 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1602 
1603 	for (i = 0; i < MTK_JPEGENC_HW_MAX; i++)
1604 		comp_jpeg[i] = jpeg->enc_hw_dev[i];
1605 	i = 0;
1606 
1607 retry_select:
1608 	hw_id = mtk_jpegenc_get_hw(ctx);
1609 	if (hw_id < 0) {
1610 		ret = wait_event_interruptible(jpeg->hw_wq,
1611 					       atomic_read(&jpeg->hw_rdy) > 0);
1612 		if (ret != 0 || (i++ > MTK_JPEG_MAX_RETRY_TIME)) {
1613 			dev_err(jpeg->dev, "%s : %d, all HW are busy\n",
1614 				__func__, __LINE__);
1615 			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1616 			return;
1617 		}
1618 
1619 		goto retry_select;
1620 	}
1621 
1622 	atomic_dec(&jpeg->hw_rdy);
1623 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1624 	if (!src_buf)
1625 		goto getbuf_fail;
1626 
1627 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1628 	if (!dst_buf)
1629 		goto getbuf_fail;
1630 
1631 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
1632 
1633 	mtk_jpegenc_set_hw_param(ctx, hw_id, src_buf, dst_buf);
1634 	ret = pm_runtime_get_sync(comp_jpeg[hw_id]->dev);
1635 	if (ret < 0) {
1636 		dev_err(jpeg->dev, "%s : %d, pm_runtime_get_sync fail !!!\n",
1637 			__func__, __LINE__);
1638 		goto enc_end;
1639 	}
1640 
1641 	ret = clk_prepare_enable(comp_jpeg[hw_id]->venc_clk.clks->clk);
1642 	if (ret) {
1643 		dev_err(jpeg->dev, "%s : %d, jpegenc clk_prepare_enable fail\n",
1644 			__func__, __LINE__);
1645 		goto enc_end;
1646 	}
1647 
1648 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1649 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1650 
1651 	schedule_delayed_work(&comp_jpeg[hw_id]->job_timeout_work,
1652 			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
1653 
1654 	spin_lock_irqsave(&comp_jpeg[hw_id]->hw_lock, flags);
1655 	jpeg_dst_buf = mtk_jpeg_vb2_to_srcbuf(&dst_buf->vb2_buf);
1656 	jpeg_dst_buf->curr_ctx = ctx;
1657 	jpeg_dst_buf->frame_num = ctx->total_frame_num;
1658 	ctx->total_frame_num++;
1659 	mtk_jpeg_enc_reset(comp_jpeg[hw_id]->reg_base);
1660 	mtk_jpeg_set_enc_dst(ctx,
1661 			     comp_jpeg[hw_id]->reg_base,
1662 			     &dst_buf->vb2_buf);
1663 	mtk_jpeg_set_enc_src(ctx,
1664 			     comp_jpeg[hw_id]->reg_base,
1665 			     &src_buf->vb2_buf);
1666 	mtk_jpeg_set_enc_params(ctx, comp_jpeg[hw_id]->reg_base);
1667 	mtk_jpeg_enc_start(comp_jpeg[hw_id]->reg_base);
1668 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1669 	spin_unlock_irqrestore(&comp_jpeg[hw_id]->hw_lock, flags);
1670 
1671 	return;
1672 
1673 enc_end:
1674 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1675 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1676 	v4l2_m2m_buf_done(src_buf, buf_state);
1677 	v4l2_m2m_buf_done(dst_buf, buf_state);
1678 getbuf_fail:
1679 	atomic_inc(&jpeg->hw_rdy);
1680 	mtk_jpegenc_put_hw(jpeg, hw_id);
1681 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1682 }
1683 
mtk_jpegdec_worker(struct work_struct * work)1684 static void mtk_jpegdec_worker(struct work_struct *work)
1685 {
1686 	struct mtk_jpeg_ctx *ctx = container_of(work, struct mtk_jpeg_ctx,
1687 		jpeg_work);
1688 	struct mtk_jpegdec_comp_dev *comp_jpeg[MTK_JPEGDEC_HW_MAX];
1689 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1690 	struct mtk_jpeg_src_buf *jpeg_src_buf, *jpeg_dst_buf;
1691 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1692 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1693 	int ret, i, hw_id = 0;
1694 	struct mtk_jpeg_bs bs;
1695 	struct mtk_jpeg_fb fb;
1696 	unsigned long flags;
1697 
1698 	for (i = 0; i < MTK_JPEGDEC_HW_MAX; i++)
1699 		comp_jpeg[i] = jpeg->dec_hw_dev[i];
1700 	i = 0;
1701 
1702 retry_select:
1703 	hw_id = mtk_jpegdec_get_hw(ctx);
1704 	if (hw_id < 0) {
1705 		ret = wait_event_interruptible_timeout(jpeg->hw_wq,
1706 						       atomic_read(&jpeg->hw_rdy) > 0,
1707 						       MTK_JPEG_HW_TIMEOUT_MSEC);
1708 		if (ret != 0 || (i++ > MTK_JPEG_MAX_RETRY_TIME)) {
1709 			dev_err(jpeg->dev, "%s : %d, all HW are busy\n",
1710 				__func__, __LINE__);
1711 			v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1712 			return;
1713 		}
1714 
1715 		goto retry_select;
1716 	}
1717 
1718 	atomic_dec(&jpeg->hw_rdy);
1719 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1720 	if (!src_buf)
1721 		goto getbuf_fail;
1722 
1723 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
1724 	if (!dst_buf)
1725 		goto getbuf_fail;
1726 
1727 	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
1728 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1729 	jpeg_dst_buf = mtk_jpeg_vb2_to_srcbuf(&dst_buf->vb2_buf);
1730 
1731 	if (mtk_jpeg_check_resolution_change(ctx,
1732 					     &jpeg_src_buf->dec_param)) {
1733 		mtk_jpeg_queue_src_chg_event(ctx);
1734 		ctx->state = MTK_JPEG_SOURCE_CHANGE;
1735 		goto getbuf_fail;
1736 	}
1737 
1738 	jpeg_src_buf->curr_ctx = ctx;
1739 	jpeg_src_buf->frame_num = ctx->total_frame_num;
1740 	jpeg_dst_buf->curr_ctx = ctx;
1741 	jpeg_dst_buf->frame_num = ctx->total_frame_num;
1742 
1743 	mtk_jpegdec_set_hw_param(ctx, hw_id, src_buf, dst_buf);
1744 	ret = pm_runtime_get_sync(comp_jpeg[hw_id]->dev);
1745 	if (ret < 0) {
1746 		dev_err(jpeg->dev, "%s : %d, pm_runtime_get_sync fail !!!\n",
1747 			__func__, __LINE__);
1748 		goto dec_end;
1749 	}
1750 
1751 	ret = clk_prepare_enable(comp_jpeg[hw_id]->jdec_clk.clks->clk);
1752 	if (ret) {
1753 		dev_err(jpeg->dev, "%s : %d, jpegdec clk_prepare_enable fail\n",
1754 			__func__, __LINE__);
1755 		goto clk_end;
1756 	}
1757 
1758 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1759 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1760 
1761 	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
1762 	if (mtk_jpeg_set_dec_dst(ctx,
1763 				 &jpeg_src_buf->dec_param,
1764 				 &dst_buf->vb2_buf, &fb)) {
1765 		dev_err(jpeg->dev, "%s : %d, mtk_jpeg_set_dec_dst fail\n",
1766 			__func__, __LINE__);
1767 		goto setdst_end;
1768 	}
1769 
1770 	schedule_delayed_work(&comp_jpeg[hw_id]->job_timeout_work,
1771 			      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
1772 
1773 	spin_lock_irqsave(&comp_jpeg[hw_id]->hw_lock, flags);
1774 	ctx->total_frame_num++;
1775 	mtk_jpeg_dec_reset(comp_jpeg[hw_id]->reg_base);
1776 	mtk_jpeg_dec_set_config(comp_jpeg[hw_id]->reg_base,
1777 				&jpeg_src_buf->dec_param,
1778 				jpeg_src_buf->bs_size,
1779 				&bs,
1780 				&fb);
1781 	mtk_jpeg_dec_start(comp_jpeg[hw_id]->reg_base);
1782 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1783 	spin_unlock_irqrestore(&comp_jpeg[hw_id]->hw_lock, flags);
1784 
1785 	return;
1786 
1787 setdst_end:
1788 	clk_disable_unprepare(comp_jpeg[hw_id]->jdec_clk.clks->clk);
1789 clk_end:
1790 	pm_runtime_put(comp_jpeg[hw_id]->dev);
1791 dec_end:
1792 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1793 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1794 	v4l2_m2m_buf_done(src_buf, buf_state);
1795 	v4l2_m2m_buf_done(dst_buf, buf_state);
1796 getbuf_fail:
1797 	atomic_inc(&jpeg->hw_rdy);
1798 	mtk_jpegdec_put_hw(jpeg, hw_id);
1799 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1800 }
1801 
mtk_jpeg_enc_irq(int irq,void * priv)1802 static irqreturn_t mtk_jpeg_enc_irq(int irq, void *priv)
1803 {
1804 	struct mtk_jpeg_dev *jpeg = priv;
1805 	u32 irq_status;
1806 	irqreturn_t ret = IRQ_NONE;
1807 
1808 	cancel_delayed_work(&jpeg->job_timeout_work);
1809 
1810 	irq_status = readl(jpeg->reg_base + JPEG_ENC_INT_STS) &
1811 		     JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
1812 	if (irq_status)
1813 		writel(0, jpeg->reg_base + JPEG_ENC_INT_STS);
1814 
1815 	if (!(irq_status & JPEG_ENC_INT_STATUS_DONE))
1816 		return ret;
1817 
1818 	ret = mtk_jpeg_enc_done(jpeg);
1819 	return ret;
1820 }
1821 
mtk_jpeg_dec_irq(int irq,void * priv)1822 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
1823 {
1824 	struct mtk_jpeg_dev *jpeg = priv;
1825 	struct mtk_jpeg_ctx *ctx;
1826 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1827 	struct mtk_jpeg_src_buf *jpeg_src_buf;
1828 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1829 	u32	dec_irq_ret;
1830 	u32 dec_ret;
1831 	int i;
1832 
1833 	cancel_delayed_work(&jpeg->job_timeout_work);
1834 
1835 	dec_ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base);
1836 	dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
1837 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1838 	if (!ctx) {
1839 		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1840 		return IRQ_HANDLED;
1841 	}
1842 
1843 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1844 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1845 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1846 
1847 	if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
1848 		mtk_jpeg_dec_reset(jpeg->reg_base);
1849 
1850 	if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
1851 		dev_err(jpeg->dev, "decode failed\n");
1852 		goto dec_end;
1853 	}
1854 
1855 	for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
1856 		vb2_set_plane_payload(&dst_buf->vb2_buf, i,
1857 				      jpeg_src_buf->dec_param.comp_size[i]);
1858 
1859 	buf_state = VB2_BUF_STATE_DONE;
1860 
1861 dec_end:
1862 	v4l2_m2m_buf_done(src_buf, buf_state);
1863 	v4l2_m2m_buf_done(dst_buf, buf_state);
1864 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1865 	pm_runtime_put(ctx->jpeg->dev);
1866 	return IRQ_HANDLED;
1867 }
1868 
1869 static struct clk_bulk_data mtk_jpeg_clocks[] = {
1870 	{ .id = "jpgenc" },
1871 };
1872 
1873 static struct clk_bulk_data mt8173_jpeg_dec_clocks[] = {
1874 	{ .id = "jpgdec-smi" },
1875 	{ .id = "jpgdec" },
1876 };
1877 
1878 static const struct mtk_jpeg_variant mt8173_jpeg_drvdata = {
1879 	.clks = mt8173_jpeg_dec_clocks,
1880 	.num_clks = ARRAY_SIZE(mt8173_jpeg_dec_clocks),
1881 	.formats = mtk_jpeg_dec_formats,
1882 	.num_formats = MTK_JPEG_DEC_NUM_FORMATS,
1883 	.qops = &mtk_jpeg_dec_qops,
1884 	.irq_handler = mtk_jpeg_dec_irq,
1885 	.hw_reset = mtk_jpeg_dec_reset,
1886 	.m2m_ops = &mtk_jpeg_dec_m2m_ops,
1887 	.dev_name = "mtk-jpeg-dec",
1888 	.ioctl_ops = &mtk_jpeg_dec_ioctl_ops,
1889 	.out_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1890 	.cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M,
1891 };
1892 
1893 static const struct mtk_jpeg_variant mtk_jpeg_drvdata = {
1894 	.clks = mtk_jpeg_clocks,
1895 	.num_clks = ARRAY_SIZE(mtk_jpeg_clocks),
1896 	.formats = mtk_jpeg_enc_formats,
1897 	.num_formats = MTK_JPEG_ENC_NUM_FORMATS,
1898 	.qops = &mtk_jpeg_enc_qops,
1899 	.irq_handler = mtk_jpeg_enc_irq,
1900 	.hw_reset = mtk_jpeg_enc_reset,
1901 	.m2m_ops = &mtk_jpeg_enc_m2m_ops,
1902 	.dev_name = "mtk-jpeg-enc",
1903 	.ioctl_ops = &mtk_jpeg_enc_ioctl_ops,
1904 	.out_q_default_fourcc = V4L2_PIX_FMT_YUYV,
1905 	.cap_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1906 	.multi_core = false,
1907 };
1908 
1909 static struct mtk_jpeg_variant mtk8195_jpegenc_drvdata = {
1910 	.formats = mtk_jpeg_enc_formats,
1911 	.num_formats = MTK_JPEG_ENC_NUM_FORMATS,
1912 	.qops = &mtk_jpeg_enc_qops,
1913 	.m2m_ops = &mtk_jpeg_multicore_enc_m2m_ops,
1914 	.dev_name = "mtk-jpeg-enc",
1915 	.ioctl_ops = &mtk_jpeg_enc_ioctl_ops,
1916 	.out_q_default_fourcc = V4L2_PIX_FMT_YUYV,
1917 	.cap_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1918 	.multi_core = true,
1919 	.jpeg_worker = mtk_jpegenc_worker,
1920 };
1921 
1922 static const struct mtk_jpeg_variant mtk8195_jpegdec_drvdata = {
1923 	.formats = mtk_jpeg_dec_formats,
1924 	.num_formats = MTK_JPEG_DEC_NUM_FORMATS,
1925 	.qops = &mtk_jpeg_dec_qops,
1926 	.m2m_ops = &mtk_jpeg_multicore_dec_m2m_ops,
1927 	.dev_name = "mtk-jpeg-dec",
1928 	.ioctl_ops = &mtk_jpeg_dec_ioctl_ops,
1929 	.out_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1930 	.cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M,
1931 	.multi_core = true,
1932 	.jpeg_worker = mtk_jpegdec_worker,
1933 };
1934 
1935 static const struct of_device_id mtk_jpeg_match[] = {
1936 	{
1937 		.compatible = "mediatek,mt8173-jpgdec",
1938 		.data = &mt8173_jpeg_drvdata,
1939 	},
1940 	{
1941 		.compatible = "mediatek,mt2701-jpgdec",
1942 		.data = &mt8173_jpeg_drvdata,
1943 	},
1944 	{
1945 		.compatible = "mediatek,mtk-jpgenc",
1946 		.data = &mtk_jpeg_drvdata,
1947 	},
1948 	{
1949 		.compatible = "mediatek,mt8195-jpgenc",
1950 		.data = &mtk8195_jpegenc_drvdata,
1951 	},
1952 	{
1953 		.compatible = "mediatek,mt8195-jpgdec",
1954 		.data = &mtk8195_jpegdec_drvdata,
1955 	},
1956 	{},
1957 };
1958 
1959 MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1960 
1961 static struct platform_driver mtk_jpeg_driver = {
1962 	.probe = mtk_jpeg_probe,
1963 	.remove_new = mtk_jpeg_remove,
1964 	.driver = {
1965 		.name           = MTK_JPEG_NAME,
1966 		.of_match_table = mtk_jpeg_match,
1967 		.pm             = &mtk_jpeg_pm_ops,
1968 	},
1969 };
1970 
1971 module_platform_driver(mtk_jpeg_driver);
1972 
1973 MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1974 MODULE_LICENSE("GPL v2");
1975