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  */
7 
8 #include <linux/clk.h>
9 #include <linux/err.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-mem2mem.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-core.h>
23 #include <media/videobuf2-dma-contig.h>
24 #include <soc/mediatek/smi.h>
25 
26 #include "mtk_jpeg_hw.h"
27 #include "mtk_jpeg_core.h"
28 #include "mtk_jpeg_parse.h"
29 
30 static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
31 	{
32 		.fourcc		= V4L2_PIX_FMT_JPEG,
33 		.colplanes	= 1,
34 		.flags		= MTK_JPEG_FMT_FLAG_DEC_OUTPUT,
35 	},
36 	{
37 		.fourcc		= V4L2_PIX_FMT_YUV420M,
38 		.h_sample	= {4, 2, 2},
39 		.v_sample	= {4, 2, 2},
40 		.colplanes	= 3,
41 		.h_align	= 5,
42 		.v_align	= 4,
43 		.flags		= MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
44 	},
45 	{
46 		.fourcc		= V4L2_PIX_FMT_YUV422M,
47 		.h_sample	= {4, 2, 2},
48 		.v_sample	= {4, 4, 4},
49 		.colplanes	= 3,
50 		.h_align	= 5,
51 		.v_align	= 3,
52 		.flags		= MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
53 	},
54 };
55 
56 #define MTK_JPEG_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_formats)
57 
58 enum {
59 	MTK_JPEG_BUF_FLAGS_INIT			= 0,
60 	MTK_JPEG_BUF_FLAGS_LAST_FRAME		= 1,
61 };
62 
63 struct mtk_jpeg_src_buf {
64 	struct vb2_v4l2_buffer b;
65 	struct list_head list;
66 	int flags;
67 	struct mtk_jpeg_dec_param dec_param;
68 };
69 
70 static int debug;
71 module_param(debug, int, 0644);
72 
mtk_jpeg_fh_to_ctx(struct v4l2_fh * fh)73 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
74 {
75 	return container_of(fh, struct mtk_jpeg_ctx, fh);
76 }
77 
mtk_jpeg_vb2_to_srcbuf(struct vb2_buffer * vb)78 static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf(
79 							struct vb2_buffer *vb)
80 {
81 	return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b);
82 }
83 
mtk_jpeg_querycap(struct file * file,void * priv,struct v4l2_capability * cap)84 static int mtk_jpeg_querycap(struct file *file, void *priv,
85 			     struct v4l2_capability *cap)
86 {
87 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
88 
89 	strscpy(cap->driver, MTK_JPEG_NAME " decoder", sizeof(cap->driver));
90 	strscpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
91 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
92 		 dev_name(jpeg->dev));
93 
94 	return 0;
95 }
96 
mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt * mtk_jpeg_formats,int n,struct v4l2_fmtdesc * f,u32 type)97 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
98 			     struct v4l2_fmtdesc *f, u32 type)
99 {
100 	int i, num = 0;
101 
102 	for (i = 0; i < n; ++i) {
103 		if (mtk_jpeg_formats[i].flags & type) {
104 			if (num == f->index)
105 				break;
106 			++num;
107 		}
108 	}
109 
110 	if (i >= n)
111 		return -EINVAL;
112 
113 	f->pixelformat = mtk_jpeg_formats[i].fourcc;
114 
115 	return 0;
116 }
117 
mtk_jpeg_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)118 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
119 				     struct v4l2_fmtdesc *f)
120 {
121 	return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
122 				 MTK_JPEG_FMT_FLAG_DEC_CAPTURE);
123 }
124 
mtk_jpeg_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)125 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
126 				     struct v4l2_fmtdesc *f)
127 {
128 	return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
129 				 MTK_JPEG_FMT_FLAG_DEC_OUTPUT);
130 }
131 
mtk_jpeg_get_q_data(struct mtk_jpeg_ctx * ctx,enum v4l2_buf_type type)132 static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
133 						   enum v4l2_buf_type type)
134 {
135 	if (V4L2_TYPE_IS_OUTPUT(type))
136 		return &ctx->out_q;
137 	return &ctx->cap_q;
138 }
139 
mtk_jpeg_find_format(struct mtk_jpeg_ctx * ctx,u32 pixelformat,unsigned int fmt_type)140 static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx,
141 						 u32 pixelformat,
142 						 unsigned int fmt_type)
143 {
144 	unsigned int k, fmt_flag;
145 
146 	fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
147 		   MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
148 		   MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
149 
150 	for (k = 0; k < MTK_JPEG_NUM_FORMATS; k++) {
151 		struct mtk_jpeg_fmt *fmt = &mtk_jpeg_formats[k];
152 
153 		if (fmt->fourcc == pixelformat && fmt->flags & fmt_flag)
154 			return fmt;
155 	}
156 
157 	return NULL;
158 }
159 
mtk_jpeg_bound_align_image(u32 * w,unsigned int wmin,unsigned int wmax,unsigned int walign,u32 * h,unsigned int hmin,unsigned int hmax,unsigned int halign)160 static void mtk_jpeg_bound_align_image(u32 *w, unsigned int wmin,
161 				       unsigned int wmax, unsigned int walign,
162 				       u32 *h, unsigned int hmin,
163 				       unsigned int hmax, unsigned int halign)
164 {
165 	int width, height, w_step, h_step;
166 
167 	width = *w;
168 	height = *h;
169 	w_step = 1 << walign;
170 	h_step = 1 << halign;
171 
172 	v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
173 	if (*w < width && (*w + w_step) <= wmax)
174 		*w += w_step;
175 	if (*h < height && (*h + h_step) <= hmax)
176 		*h += h_step;
177 }
178 
mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx * ctx,struct v4l2_format * f)179 static void mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx *ctx,
180 				       struct v4l2_format *f)
181 {
182 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
183 	struct mtk_jpeg_q_data *q_data;
184 	int i;
185 
186 	q_data = mtk_jpeg_get_q_data(ctx, f->type);
187 
188 	pix_mp->width = q_data->w;
189 	pix_mp->height = q_data->h;
190 	pix_mp->pixelformat = q_data->fmt->fourcc;
191 	pix_mp->num_planes = q_data->fmt->colplanes;
192 
193 	for (i = 0; i < pix_mp->num_planes; i++) {
194 		pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i];
195 		pix_mp->plane_fmt[i].sizeimage = q_data->sizeimage[i];
196 	}
197 }
198 
mtk_jpeg_try_fmt_mplane(struct v4l2_format * f,struct mtk_jpeg_fmt * fmt,struct mtk_jpeg_ctx * ctx,int q_type)199 static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
200 				   struct mtk_jpeg_fmt *fmt,
201 				   struct mtk_jpeg_ctx *ctx, int q_type)
202 {
203 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
204 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
205 	int i;
206 
207 	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
208 	pix_mp->field = V4L2_FIELD_NONE;
209 
210 	if (ctx->state != MTK_JPEG_INIT) {
211 		mtk_jpeg_adjust_fmt_mplane(ctx, f);
212 		goto end;
213 	}
214 
215 	pix_mp->num_planes = fmt->colplanes;
216 	pix_mp->pixelformat = fmt->fourcc;
217 
218 	if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) {
219 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
220 
221 		mtk_jpeg_bound_align_image(&pix_mp->width, MTK_JPEG_MIN_WIDTH,
222 					   MTK_JPEG_MAX_WIDTH, 0,
223 					   &pix_mp->height, MTK_JPEG_MIN_HEIGHT,
224 					   MTK_JPEG_MAX_HEIGHT, 0);
225 
226 		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
227 		pfmt->bytesperline = 0;
228 		/* Source size must be aligned to 128 */
229 		pfmt->sizeimage = mtk_jpeg_align(pfmt->sizeimage, 128);
230 		if (pfmt->sizeimage == 0)
231 			pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE;
232 		goto end;
233 	}
234 
235 	/* type is MTK_JPEG_FMT_TYPE_CAPTURE */
236 	mtk_jpeg_bound_align_image(&pix_mp->width, MTK_JPEG_MIN_WIDTH,
237 				   MTK_JPEG_MAX_WIDTH, fmt->h_align,
238 				   &pix_mp->height, MTK_JPEG_MIN_HEIGHT,
239 				   MTK_JPEG_MAX_HEIGHT, fmt->v_align);
240 
241 	for (i = 0; i < fmt->colplanes; i++) {
242 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
243 		u32 stride = pix_mp->width * fmt->h_sample[i] / 4;
244 		u32 h = pix_mp->height * fmt->v_sample[i] / 4;
245 
246 		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
247 		pfmt->bytesperline = stride;
248 		pfmt->sizeimage = stride * h;
249 	}
250 end:
251 	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "wxh:%ux%u\n",
252 		 pix_mp->width, pix_mp->height);
253 	for (i = 0; i < pix_mp->num_planes; i++) {
254 		v4l2_dbg(2, debug, &jpeg->v4l2_dev,
255 			 "plane[%d] bpl=%u, size=%u\n",
256 			 i,
257 			 pix_mp->plane_fmt[i].bytesperline,
258 			 pix_mp->plane_fmt[i].sizeimage);
259 	}
260 	return 0;
261 }
262 
mtk_jpeg_g_fmt_vid_mplane(struct file * file,void * priv,struct v4l2_format * f)263 static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv,
264 				     struct v4l2_format *f)
265 {
266 	struct vb2_queue *vq;
267 	struct mtk_jpeg_q_data *q_data = NULL;
268 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
269 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
270 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
271 	int i;
272 
273 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
274 	if (!vq)
275 		return -EINVAL;
276 
277 	q_data = mtk_jpeg_get_q_data(ctx, f->type);
278 
279 	memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
280 	pix_mp->width = q_data->w;
281 	pix_mp->height = q_data->h;
282 	pix_mp->field = V4L2_FIELD_NONE;
283 	pix_mp->pixelformat = q_data->fmt->fourcc;
284 	pix_mp->num_planes = q_data->fmt->colplanes;
285 	pix_mp->colorspace = ctx->colorspace;
286 	pix_mp->ycbcr_enc = ctx->ycbcr_enc;
287 	pix_mp->xfer_func = ctx->xfer_func;
288 	pix_mp->quantization = ctx->quantization;
289 
290 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n",
291 		 f->type,
292 		 (pix_mp->pixelformat & 0xff),
293 		 (pix_mp->pixelformat >>  8 & 0xff),
294 		 (pix_mp->pixelformat >> 16 & 0xff),
295 		 (pix_mp->pixelformat >> 24 & 0xff),
296 		 pix_mp->width, pix_mp->height);
297 
298 	for (i = 0; i < pix_mp->num_planes; i++) {
299 		struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
300 
301 		pfmt->bytesperline = q_data->bytesperline[i];
302 		pfmt->sizeimage = q_data->sizeimage[i];
303 		memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
304 
305 		v4l2_dbg(1, debug, &jpeg->v4l2_dev,
306 			 "plane[%d] bpl=%u, size=%u\n",
307 			 i,
308 			 pfmt->bytesperline,
309 			 pfmt->sizeimage);
310 	}
311 	return 0;
312 }
313 
mtk_jpeg_try_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)314 static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
315 					   struct v4l2_format *f)
316 {
317 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
318 	struct mtk_jpeg_fmt *fmt;
319 
320 	fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
321 				   MTK_JPEG_FMT_TYPE_CAPTURE);
322 	if (!fmt)
323 		fmt = ctx->cap_q.fmt;
324 
325 	v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
326 		 f->type,
327 		 (fmt->fourcc & 0xff),
328 		 (fmt->fourcc >>  8 & 0xff),
329 		 (fmt->fourcc >> 16 & 0xff),
330 		 (fmt->fourcc >> 24 & 0xff));
331 
332 	return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_CAPTURE);
333 }
334 
mtk_jpeg_try_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)335 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
336 					   struct v4l2_format *f)
337 {
338 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
339 	struct mtk_jpeg_fmt *fmt;
340 
341 	fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
342 				   MTK_JPEG_FMT_TYPE_OUTPUT);
343 	if (!fmt)
344 		fmt = ctx->out_q.fmt;
345 
346 	v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
347 		 f->type,
348 		 (fmt->fourcc & 0xff),
349 		 (fmt->fourcc >>  8 & 0xff),
350 		 (fmt->fourcc >> 16 & 0xff),
351 		 (fmt->fourcc >> 24 & 0xff));
352 
353 	return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_OUTPUT);
354 }
355 
mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx * ctx,struct v4l2_format * f)356 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
357 				 struct v4l2_format *f)
358 {
359 	struct vb2_queue *vq;
360 	struct mtk_jpeg_q_data *q_data = NULL;
361 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
362 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
363 	unsigned int f_type;
364 	int i;
365 
366 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
367 	if (!vq)
368 		return -EINVAL;
369 
370 	q_data = mtk_jpeg_get_q_data(ctx, f->type);
371 
372 	if (vb2_is_busy(vq)) {
373 		v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
374 		return -EBUSY;
375 	}
376 
377 	f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
378 			 MTK_JPEG_FMT_TYPE_OUTPUT : MTK_JPEG_FMT_TYPE_CAPTURE;
379 
380 	q_data->fmt = mtk_jpeg_find_format(ctx, pix_mp->pixelformat, f_type);
381 	q_data->w = pix_mp->width;
382 	q_data->h = pix_mp->height;
383 	ctx->colorspace = pix_mp->colorspace;
384 	ctx->ycbcr_enc = pix_mp->ycbcr_enc;
385 	ctx->xfer_func = pix_mp->xfer_func;
386 	ctx->quantization = pix_mp->quantization;
387 
388 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n",
389 		 f->type,
390 		 (q_data->fmt->fourcc & 0xff),
391 		 (q_data->fmt->fourcc >>  8 & 0xff),
392 		 (q_data->fmt->fourcc >> 16 & 0xff),
393 		 (q_data->fmt->fourcc >> 24 & 0xff),
394 		 q_data->w, q_data->h);
395 
396 	for (i = 0; i < q_data->fmt->colplanes; i++) {
397 		q_data->bytesperline[i] = pix_mp->plane_fmt[i].bytesperline;
398 		q_data->sizeimage[i] = pix_mp->plane_fmt[i].sizeimage;
399 
400 		v4l2_dbg(1, debug, &jpeg->v4l2_dev,
401 			 "plane[%d] bpl=%u, size=%u\n",
402 			 i, q_data->bytesperline[i], q_data->sizeimage[i]);
403 	}
404 
405 	return 0;
406 }
407 
mtk_jpeg_s_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)408 static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv,
409 					 struct v4l2_format *f)
410 {
411 	int ret;
412 
413 	ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f);
414 	if (ret)
415 		return ret;
416 
417 	return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f);
418 }
419 
mtk_jpeg_s_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)420 static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv,
421 					 struct v4l2_format *f)
422 {
423 	int ret;
424 
425 	ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f);
426 	if (ret)
427 		return ret;
428 
429 	return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f);
430 }
431 
mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx * ctx)432 static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx)
433 {
434 	static const struct v4l2_event ev_src_ch = {
435 		.type = V4L2_EVENT_SOURCE_CHANGE,
436 		.u.src_change.changes =
437 		V4L2_EVENT_SRC_CH_RESOLUTION,
438 	};
439 
440 	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
441 }
442 
mtk_jpeg_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)443 static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
444 				    const struct v4l2_event_subscription *sub)
445 {
446 	switch (sub->type) {
447 	case V4L2_EVENT_SOURCE_CHANGE:
448 		return v4l2_src_change_event_subscribe(fh, sub);
449 	default:
450 		return -EINVAL;
451 	}
452 }
453 
mtk_jpeg_g_selection(struct file * file,void * priv,struct v4l2_selection * s)454 static int mtk_jpeg_g_selection(struct file *file, void *priv,
455 				struct v4l2_selection *s)
456 {
457 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
458 
459 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
460 		return -EINVAL;
461 
462 	switch (s->target) {
463 	case V4L2_SEL_TGT_COMPOSE:
464 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
465 		s->r.width = ctx->out_q.w;
466 		s->r.height = ctx->out_q.h;
467 		s->r.left = 0;
468 		s->r.top = 0;
469 		break;
470 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
471 	case V4L2_SEL_TGT_COMPOSE_PADDED:
472 		s->r.width = ctx->cap_q.w;
473 		s->r.height = ctx->cap_q.h;
474 		s->r.left = 0;
475 		s->r.top = 0;
476 		break;
477 	default:
478 		return -EINVAL;
479 	}
480 	return 0;
481 }
482 
mtk_jpeg_s_selection(struct file * file,void * priv,struct v4l2_selection * s)483 static int mtk_jpeg_s_selection(struct file *file, void *priv,
484 				struct v4l2_selection *s)
485 {
486 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
487 
488 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
489 		return -EINVAL;
490 
491 	switch (s->target) {
492 	case V4L2_SEL_TGT_COMPOSE:
493 		s->r.left = 0;
494 		s->r.top = 0;
495 		s->r.width = ctx->out_q.w;
496 		s->r.height = ctx->out_q.h;
497 		break;
498 	default:
499 		return -EINVAL;
500 	}
501 	return 0;
502 }
503 
mtk_jpeg_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)504 static int mtk_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
505 {
506 	struct v4l2_fh *fh = file->private_data;
507 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
508 	struct vb2_queue *vq;
509 	struct vb2_buffer *vb;
510 	struct mtk_jpeg_src_buf *jpeg_src_buf;
511 
512 	if (buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
513 		goto end;
514 
515 	vq = v4l2_m2m_get_vq(fh->m2m_ctx, buf->type);
516 	if (buf->index >= vq->num_buffers) {
517 		dev_err(ctx->jpeg->dev, "buffer index out of range\n");
518 		return -EINVAL;
519 	}
520 
521 	vb = vb2_get_buffer(vq, buf->index);
522 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
523 	jpeg_src_buf->flags = (buf->m.planes[0].bytesused == 0) ?
524 		MTK_JPEG_BUF_FLAGS_LAST_FRAME : MTK_JPEG_BUF_FLAGS_INIT;
525 end:
526 	return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
527 }
528 
529 static const struct v4l2_ioctl_ops mtk_jpeg_ioctl_ops = {
530 	.vidioc_querycap                = mtk_jpeg_querycap,
531 	.vidioc_enum_fmt_vid_cap	= mtk_jpeg_enum_fmt_vid_cap,
532 	.vidioc_enum_fmt_vid_out	= mtk_jpeg_enum_fmt_vid_out,
533 	.vidioc_try_fmt_vid_cap_mplane	= mtk_jpeg_try_fmt_vid_cap_mplane,
534 	.vidioc_try_fmt_vid_out_mplane	= mtk_jpeg_try_fmt_vid_out_mplane,
535 	.vidioc_g_fmt_vid_cap_mplane    = mtk_jpeg_g_fmt_vid_mplane,
536 	.vidioc_g_fmt_vid_out_mplane    = mtk_jpeg_g_fmt_vid_mplane,
537 	.vidioc_s_fmt_vid_cap_mplane    = mtk_jpeg_s_fmt_vid_cap_mplane,
538 	.vidioc_s_fmt_vid_out_mplane    = mtk_jpeg_s_fmt_vid_out_mplane,
539 	.vidioc_qbuf                    = mtk_jpeg_qbuf,
540 	.vidioc_subscribe_event         = mtk_jpeg_subscribe_event,
541 	.vidioc_g_selection		= mtk_jpeg_g_selection,
542 	.vidioc_s_selection		= mtk_jpeg_s_selection,
543 
544 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
545 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
546 	.vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
547 	.vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
548 	.vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
549 	.vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
550 	.vidioc_streamon                = v4l2_m2m_ioctl_streamon,
551 	.vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
552 
553 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
554 };
555 
mtk_jpeg_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_ctxs[])556 static int mtk_jpeg_queue_setup(struct vb2_queue *q,
557 				unsigned int *num_buffers,
558 				unsigned int *num_planes,
559 				unsigned int sizes[],
560 				struct device *alloc_ctxs[])
561 {
562 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
563 	struct mtk_jpeg_q_data *q_data = NULL;
564 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
565 	int i;
566 
567 	v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
568 		 q->type, *num_buffers);
569 
570 	q_data = mtk_jpeg_get_q_data(ctx, q->type);
571 	if (!q_data)
572 		return -EINVAL;
573 
574 	*num_planes = q_data->fmt->colplanes;
575 	for (i = 0; i < q_data->fmt->colplanes; i++) {
576 		sizes[i] = q_data->sizeimage[i];
577 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n",
578 			 i, sizes[i]);
579 	}
580 
581 	return 0;
582 }
583 
mtk_jpeg_buf_prepare(struct vb2_buffer * vb)584 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
585 {
586 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
587 	struct mtk_jpeg_q_data *q_data = NULL;
588 	int i;
589 
590 	q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
591 	if (!q_data)
592 		return -EINVAL;
593 
594 	for (i = 0; i < q_data->fmt->colplanes; i++)
595 		vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
596 
597 	return 0;
598 }
599 
mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx * ctx,struct mtk_jpeg_dec_param * param)600 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
601 					     struct mtk_jpeg_dec_param *param)
602 {
603 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
604 	struct mtk_jpeg_q_data *q_data;
605 
606 	q_data = &ctx->out_q;
607 	if (q_data->w != param->pic_w || q_data->h != param->pic_h) {
608 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n");
609 		return true;
610 	}
611 
612 	q_data = &ctx->cap_q;
613 	if (q_data->fmt != mtk_jpeg_find_format(ctx, param->dst_fourcc,
614 						MTK_JPEG_FMT_TYPE_CAPTURE)) {
615 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n");
616 		return true;
617 	}
618 	return false;
619 }
620 
mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx * ctx,struct mtk_jpeg_dec_param * param)621 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
622 				    struct mtk_jpeg_dec_param *param)
623 {
624 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
625 	struct mtk_jpeg_q_data *q_data;
626 	int i;
627 
628 	q_data = &ctx->out_q;
629 	q_data->w = param->pic_w;
630 	q_data->h = param->pic_h;
631 
632 	q_data = &ctx->cap_q;
633 	q_data->w = param->dec_w;
634 	q_data->h = param->dec_h;
635 	q_data->fmt = mtk_jpeg_find_format(ctx,
636 					   param->dst_fourcc,
637 					   MTK_JPEG_FMT_TYPE_CAPTURE);
638 
639 	for (i = 0; i < q_data->fmt->colplanes; i++) {
640 		q_data->bytesperline[i] = param->mem_stride[i];
641 		q_data->sizeimage[i] = param->comp_size[i];
642 	}
643 
644 	v4l2_dbg(1, debug, &jpeg->v4l2_dev,
645 		 "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n",
646 		 (param->dst_fourcc & 0xff),
647 		 (param->dst_fourcc >>  8 & 0xff),
648 		 (param->dst_fourcc >> 16 & 0xff),
649 		 (param->dst_fourcc >> 24 & 0xff),
650 		 param->pic_w, param->pic_h,
651 		 param->dec_w, param->dec_h);
652 }
653 
mtk_jpeg_buf_queue(struct vb2_buffer * vb)654 static void mtk_jpeg_buf_queue(struct vb2_buffer *vb)
655 {
656 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
657 	struct mtk_jpeg_dec_param *param;
658 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
659 	struct mtk_jpeg_src_buf *jpeg_src_buf;
660 	bool header_valid;
661 
662 	v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
663 		 vb->vb2_queue->type, vb->index, vb);
664 
665 	if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
666 		goto end;
667 
668 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
669 	param = &jpeg_src_buf->dec_param;
670 	memset(param, 0, sizeof(*param));
671 
672 	if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
673 		v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
674 		goto end;
675 	}
676 	header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
677 				      vb2_get_plane_payload(vb, 0));
678 	if (!header_valid) {
679 		v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
680 		vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
681 		return;
682 	}
683 
684 	if (ctx->state == MTK_JPEG_INIT) {
685 		struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
686 			ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
687 
688 		mtk_jpeg_queue_src_chg_event(ctx);
689 		mtk_jpeg_set_queue_data(ctx, param);
690 		ctx->state = vb2_is_streaming(dst_vq) ?
691 				MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
692 	}
693 end:
694 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
695 }
696 
mtk_jpeg_buf_remove(struct mtk_jpeg_ctx * ctx,enum v4l2_buf_type type)697 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
698 				 enum v4l2_buf_type type)
699 {
700 	if (V4L2_TYPE_IS_OUTPUT(type))
701 		return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
702 	else
703 		return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
704 }
705 
mtk_jpeg_start_streaming(struct vb2_queue * q,unsigned int count)706 static int mtk_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
707 {
708 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
709 	struct vb2_v4l2_buffer *vb;
710 	int ret = 0;
711 
712 	ret = pm_runtime_get_sync(ctx->jpeg->dev);
713 	if (ret < 0)
714 		goto err;
715 
716 	return 0;
717 err:
718 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
719 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_QUEUED);
720 	return ret;
721 }
722 
mtk_jpeg_stop_streaming(struct vb2_queue * q)723 static void mtk_jpeg_stop_streaming(struct vb2_queue *q)
724 {
725 	struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
726 	struct vb2_v4l2_buffer *vb;
727 
728 	/*
729 	 * STREAMOFF is an acknowledgment for source change event.
730 	 * Before STREAMOFF, we still have to return the old resolution and
731 	 * subsampling. Update capture queue when the stream is off.
732 	 */
733 	if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
734 	    !V4L2_TYPE_IS_OUTPUT(q->type)) {
735 		struct mtk_jpeg_src_buf *src_buf;
736 
737 		vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
738 		src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
739 		mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
740 		ctx->state = MTK_JPEG_RUNNING;
741 	} else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
742 		ctx->state = MTK_JPEG_INIT;
743 	}
744 
745 	while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
746 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
747 
748 	pm_runtime_put_sync(ctx->jpeg->dev);
749 }
750 
751 static const struct vb2_ops mtk_jpeg_qops = {
752 	.queue_setup        = mtk_jpeg_queue_setup,
753 	.buf_prepare        = mtk_jpeg_buf_prepare,
754 	.buf_queue          = mtk_jpeg_buf_queue,
755 	.wait_prepare       = vb2_ops_wait_prepare,
756 	.wait_finish        = vb2_ops_wait_finish,
757 	.start_streaming    = mtk_jpeg_start_streaming,
758 	.stop_streaming     = mtk_jpeg_stop_streaming,
759 };
760 
mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx * ctx,struct vb2_buffer * src_buf,struct mtk_jpeg_bs * bs)761 static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
762 				 struct vb2_buffer *src_buf,
763 				 struct mtk_jpeg_bs *bs)
764 {
765 	bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
766 	bs->end_addr = bs->str_addr +
767 			 mtk_jpeg_align(vb2_get_plane_payload(src_buf, 0), 16);
768 	bs->size = mtk_jpeg_align(vb2_plane_size(src_buf, 0), 128);
769 }
770 
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)771 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
772 				struct mtk_jpeg_dec_param *param,
773 				struct vb2_buffer *dst_buf,
774 				struct mtk_jpeg_fb *fb)
775 {
776 	int i;
777 
778 	if (param->comp_num != dst_buf->num_planes) {
779 		dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n",
780 			param->comp_num, dst_buf->num_planes);
781 		return -EINVAL;
782 	}
783 
784 	for (i = 0; i < dst_buf->num_planes; i++) {
785 		if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) {
786 			dev_err(ctx->jpeg->dev,
787 				"buffer size is underflow (%lu < %u)\n",
788 				vb2_plane_size(dst_buf, 0),
789 				param->comp_size[i]);
790 			return -EINVAL;
791 		}
792 		fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
793 	}
794 
795 	return 0;
796 }
797 
mtk_jpeg_device_run(void * priv)798 static void mtk_jpeg_device_run(void *priv)
799 {
800 	struct mtk_jpeg_ctx *ctx = priv;
801 	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
802 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
803 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
804 	unsigned long flags;
805 	struct mtk_jpeg_src_buf *jpeg_src_buf;
806 	struct mtk_jpeg_bs bs;
807 	struct mtk_jpeg_fb fb;
808 	int i;
809 
810 	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
811 	dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
812 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
813 
814 	if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
815 		for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
816 			vb2_set_plane_payload(&dst_buf->vb2_buf, i, 0);
817 		buf_state = VB2_BUF_STATE_DONE;
818 		goto dec_end;
819 	}
820 
821 	if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
822 		mtk_jpeg_queue_src_chg_event(ctx);
823 		ctx->state = MTK_JPEG_SOURCE_CHANGE;
824 		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
825 		return;
826 	}
827 
828 	mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
829 	if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
830 		goto dec_end;
831 
832 	spin_lock_irqsave(&jpeg->hw_lock, flags);
833 	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
834 	mtk_jpeg_dec_set_config(jpeg->dec_reg_base,
835 				&jpeg_src_buf->dec_param, &bs, &fb);
836 
837 	mtk_jpeg_dec_start(jpeg->dec_reg_base);
838 	spin_unlock_irqrestore(&jpeg->hw_lock, flags);
839 	return;
840 
841 dec_end:
842 	v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
843 	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
844 	v4l2_m2m_buf_done(src_buf, buf_state);
845 	v4l2_m2m_buf_done(dst_buf, buf_state);
846 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
847 }
848 
mtk_jpeg_job_ready(void * priv)849 static int mtk_jpeg_job_ready(void *priv)
850 {
851 	struct mtk_jpeg_ctx *ctx = priv;
852 
853 	return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
854 }
855 
856 static const struct v4l2_m2m_ops mtk_jpeg_m2m_ops = {
857 	.device_run = mtk_jpeg_device_run,
858 	.job_ready  = mtk_jpeg_job_ready,
859 };
860 
mtk_jpeg_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)861 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
862 			       struct vb2_queue *dst_vq)
863 {
864 	struct mtk_jpeg_ctx *ctx = priv;
865 	int ret;
866 
867 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
868 	src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
869 	src_vq->drv_priv = ctx;
870 	src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf);
871 	src_vq->ops = &mtk_jpeg_qops;
872 	src_vq->mem_ops = &vb2_dma_contig_memops;
873 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
874 	src_vq->lock = &ctx->jpeg->lock;
875 	src_vq->dev = ctx->jpeg->dev;
876 	ret = vb2_queue_init(src_vq);
877 	if (ret)
878 		return ret;
879 
880 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
881 	dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
882 	dst_vq->drv_priv = ctx;
883 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
884 	dst_vq->ops = &mtk_jpeg_qops;
885 	dst_vq->mem_ops = &vb2_dma_contig_memops;
886 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
887 	dst_vq->lock = &ctx->jpeg->lock;
888 	dst_vq->dev = ctx->jpeg->dev;
889 	ret = vb2_queue_init(dst_vq);
890 
891 	return ret;
892 }
893 
mtk_jpeg_clk_on(struct mtk_jpeg_dev * jpeg)894 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
895 {
896 	int ret;
897 
898 	ret = mtk_smi_larb_get(jpeg->larb);
899 	if (ret)
900 		dev_err(jpeg->dev, "mtk_smi_larb_get larbvdec fail %d\n", ret);
901 	clk_prepare_enable(jpeg->clk_jdec_smi);
902 	clk_prepare_enable(jpeg->clk_jdec);
903 }
904 
mtk_jpeg_clk_off(struct mtk_jpeg_dev * jpeg)905 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
906 {
907 	clk_disable_unprepare(jpeg->clk_jdec);
908 	clk_disable_unprepare(jpeg->clk_jdec_smi);
909 	mtk_smi_larb_put(jpeg->larb);
910 }
911 
mtk_jpeg_dec_irq(int irq,void * priv)912 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
913 {
914 	struct mtk_jpeg_dev *jpeg = priv;
915 	struct mtk_jpeg_ctx *ctx;
916 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
917 	struct mtk_jpeg_src_buf *jpeg_src_buf;
918 	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
919 	u32	dec_irq_ret;
920 	u32 dec_ret;
921 	int i;
922 
923 	dec_ret = mtk_jpeg_dec_get_int_status(jpeg->dec_reg_base);
924 	dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
925 	ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
926 	if (!ctx) {
927 		v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
928 		return IRQ_HANDLED;
929 	}
930 
931 	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
932 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
933 	jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
934 
935 	if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
936 		mtk_jpeg_dec_reset(jpeg->dec_reg_base);
937 
938 	if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
939 		dev_err(jpeg->dev, "decode failed\n");
940 		goto dec_end;
941 	}
942 
943 	for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
944 		vb2_set_plane_payload(&dst_buf->vb2_buf, i,
945 				      jpeg_src_buf->dec_param.comp_size[i]);
946 
947 	buf_state = VB2_BUF_STATE_DONE;
948 
949 dec_end:
950 	v4l2_m2m_buf_done(src_buf, buf_state);
951 	v4l2_m2m_buf_done(dst_buf, buf_state);
952 	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
953 	return IRQ_HANDLED;
954 }
955 
mtk_jpeg_set_default_params(struct mtk_jpeg_ctx * ctx)956 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
957 {
958 	struct mtk_jpeg_q_data *q = &ctx->out_q;
959 	int i;
960 
961 	ctx->colorspace = V4L2_COLORSPACE_JPEG,
962 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
963 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
964 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
965 
966 	q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
967 					      MTK_JPEG_FMT_TYPE_OUTPUT);
968 	q->w = MTK_JPEG_MIN_WIDTH;
969 	q->h = MTK_JPEG_MIN_HEIGHT;
970 	q->bytesperline[0] = 0;
971 	q->sizeimage[0] = MTK_JPEG_DEFAULT_SIZEIMAGE;
972 
973 	q = &ctx->cap_q;
974 	q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_YUV420M,
975 					      MTK_JPEG_FMT_TYPE_CAPTURE);
976 	q->w = MTK_JPEG_MIN_WIDTH;
977 	q->h = MTK_JPEG_MIN_HEIGHT;
978 
979 	for (i = 0; i < q->fmt->colplanes; i++) {
980 		u32 stride = q->w * q->fmt->h_sample[i] / 4;
981 		u32 h = q->h * q->fmt->v_sample[i] / 4;
982 
983 		q->bytesperline[i] = stride;
984 		q->sizeimage[i] = stride * h;
985 	}
986 }
987 
mtk_jpeg_open(struct file * file)988 static int mtk_jpeg_open(struct file *file)
989 {
990 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
991 	struct video_device *vfd = video_devdata(file);
992 	struct mtk_jpeg_ctx *ctx;
993 	int ret = 0;
994 
995 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
996 	if (!ctx)
997 		return -ENOMEM;
998 
999 	if (mutex_lock_interruptible(&jpeg->lock)) {
1000 		ret = -ERESTARTSYS;
1001 		goto free;
1002 	}
1003 
1004 	v4l2_fh_init(&ctx->fh, vfd);
1005 	file->private_data = &ctx->fh;
1006 	v4l2_fh_add(&ctx->fh);
1007 
1008 	ctx->jpeg = jpeg;
1009 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx,
1010 					    mtk_jpeg_queue_init);
1011 	if (IS_ERR(ctx->fh.m2m_ctx)) {
1012 		ret = PTR_ERR(ctx->fh.m2m_ctx);
1013 		goto error;
1014 	}
1015 
1016 	mtk_jpeg_set_default_params(ctx);
1017 	mutex_unlock(&jpeg->lock);
1018 	return 0;
1019 
1020 error:
1021 	v4l2_fh_del(&ctx->fh);
1022 	v4l2_fh_exit(&ctx->fh);
1023 	mutex_unlock(&jpeg->lock);
1024 free:
1025 	kfree(ctx);
1026 	return ret;
1027 }
1028 
mtk_jpeg_release(struct file * file)1029 static int mtk_jpeg_release(struct file *file)
1030 {
1031 	struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1032 	struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
1033 
1034 	mutex_lock(&jpeg->lock);
1035 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1036 	v4l2_fh_del(&ctx->fh);
1037 	v4l2_fh_exit(&ctx->fh);
1038 	kfree(ctx);
1039 	mutex_unlock(&jpeg->lock);
1040 	return 0;
1041 }
1042 
1043 static const struct v4l2_file_operations mtk_jpeg_fops = {
1044 	.owner          = THIS_MODULE,
1045 	.open           = mtk_jpeg_open,
1046 	.release        = mtk_jpeg_release,
1047 	.poll           = v4l2_m2m_fop_poll,
1048 	.unlocked_ioctl = video_ioctl2,
1049 	.mmap           = v4l2_m2m_fop_mmap,
1050 };
1051 
mtk_jpeg_clk_init(struct mtk_jpeg_dev * jpeg)1052 static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg)
1053 {
1054 	struct device_node *node;
1055 	struct platform_device *pdev;
1056 
1057 	node = of_parse_phandle(jpeg->dev->of_node, "mediatek,larb", 0);
1058 	if (!node)
1059 		return -EINVAL;
1060 	pdev = of_find_device_by_node(node);
1061 	if (WARN_ON(!pdev)) {
1062 		of_node_put(node);
1063 		return -EINVAL;
1064 	}
1065 	of_node_put(node);
1066 
1067 	jpeg->larb = &pdev->dev;
1068 
1069 	jpeg->clk_jdec = devm_clk_get(jpeg->dev, "jpgdec");
1070 	if (IS_ERR(jpeg->clk_jdec))
1071 		return PTR_ERR(jpeg->clk_jdec);
1072 
1073 	jpeg->clk_jdec_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
1074 	return PTR_ERR_OR_ZERO(jpeg->clk_jdec_smi);
1075 }
1076 
mtk_jpeg_probe(struct platform_device * pdev)1077 static int mtk_jpeg_probe(struct platform_device *pdev)
1078 {
1079 	struct mtk_jpeg_dev *jpeg;
1080 	struct resource *res;
1081 	int dec_irq;
1082 	int ret;
1083 
1084 	jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1085 	if (!jpeg)
1086 		return -ENOMEM;
1087 
1088 	mutex_init(&jpeg->lock);
1089 	spin_lock_init(&jpeg->hw_lock);
1090 	jpeg->dev = &pdev->dev;
1091 
1092 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1093 	jpeg->dec_reg_base = devm_ioremap_resource(&pdev->dev, res);
1094 	if (IS_ERR(jpeg->dec_reg_base)) {
1095 		ret = PTR_ERR(jpeg->dec_reg_base);
1096 		return ret;
1097 	}
1098 
1099 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1100 	dec_irq = platform_get_irq(pdev, 0);
1101 	if (!res || dec_irq < 0) {
1102 		dev_err(&pdev->dev, "Failed to get dec_irq %d.\n", dec_irq);
1103 		ret = -EINVAL;
1104 		return ret;
1105 	}
1106 
1107 	ret = devm_request_irq(&pdev->dev, dec_irq, mtk_jpeg_dec_irq, 0,
1108 			       pdev->name, jpeg);
1109 	if (ret) {
1110 		dev_err(&pdev->dev, "Failed to request dec_irq %d (%d)\n",
1111 			dec_irq, ret);
1112 		ret = -EINVAL;
1113 		goto err_req_irq;
1114 	}
1115 
1116 	ret = mtk_jpeg_clk_init(jpeg);
1117 	if (ret) {
1118 		dev_err(&pdev->dev, "Failed to init clk, err %d\n", ret);
1119 		goto err_clk_init;
1120 	}
1121 
1122 	ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1123 	if (ret) {
1124 		dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1125 		ret = -EINVAL;
1126 		goto err_dev_register;
1127 	}
1128 
1129 	jpeg->m2m_dev = v4l2_m2m_init(&mtk_jpeg_m2m_ops);
1130 	if (IS_ERR(jpeg->m2m_dev)) {
1131 		v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1132 		ret = PTR_ERR(jpeg->m2m_dev);
1133 		goto err_m2m_init;
1134 	}
1135 
1136 	jpeg->dec_vdev = video_device_alloc();
1137 	if (!jpeg->dec_vdev) {
1138 		ret = -ENOMEM;
1139 		goto err_dec_vdev_alloc;
1140 	}
1141 	snprintf(jpeg->dec_vdev->name, sizeof(jpeg->dec_vdev->name),
1142 		 "%s-dec", MTK_JPEG_NAME);
1143 	jpeg->dec_vdev->fops = &mtk_jpeg_fops;
1144 	jpeg->dec_vdev->ioctl_ops = &mtk_jpeg_ioctl_ops;
1145 	jpeg->dec_vdev->minor = -1;
1146 	jpeg->dec_vdev->release = video_device_release;
1147 	jpeg->dec_vdev->lock = &jpeg->lock;
1148 	jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
1149 	jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
1150 	jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
1151 				      V4L2_CAP_VIDEO_M2M_MPLANE;
1152 
1153 	ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
1154 	if (ret) {
1155 		v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1156 		goto err_dec_vdev_register;
1157 	}
1158 
1159 	video_set_drvdata(jpeg->dec_vdev, jpeg);
1160 	v4l2_info(&jpeg->v4l2_dev,
1161 		  "decoder device registered as /dev/video%d (%d,%d)\n",
1162 		  jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor);
1163 
1164 	platform_set_drvdata(pdev, jpeg);
1165 
1166 	pm_runtime_enable(&pdev->dev);
1167 
1168 	return 0;
1169 
1170 err_dec_vdev_register:
1171 	video_device_release(jpeg->dec_vdev);
1172 
1173 err_dec_vdev_alloc:
1174 	v4l2_m2m_release(jpeg->m2m_dev);
1175 
1176 err_m2m_init:
1177 	v4l2_device_unregister(&jpeg->v4l2_dev);
1178 
1179 err_dev_register:
1180 
1181 err_clk_init:
1182 
1183 err_req_irq:
1184 
1185 	return ret;
1186 }
1187 
mtk_jpeg_remove(struct platform_device * pdev)1188 static int mtk_jpeg_remove(struct platform_device *pdev)
1189 {
1190 	struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
1191 
1192 	pm_runtime_disable(&pdev->dev);
1193 	video_unregister_device(jpeg->dec_vdev);
1194 	video_device_release(jpeg->dec_vdev);
1195 	v4l2_m2m_release(jpeg->m2m_dev);
1196 	v4l2_device_unregister(&jpeg->v4l2_dev);
1197 
1198 	return 0;
1199 }
1200 
mtk_jpeg_pm_suspend(struct device * dev)1201 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1202 {
1203 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1204 
1205 	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
1206 	mtk_jpeg_clk_off(jpeg);
1207 
1208 	return 0;
1209 }
1210 
mtk_jpeg_pm_resume(struct device * dev)1211 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1212 {
1213 	struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1214 
1215 	mtk_jpeg_clk_on(jpeg);
1216 	mtk_jpeg_dec_reset(jpeg->dec_reg_base);
1217 
1218 	return 0;
1219 }
1220 
mtk_jpeg_suspend(struct device * dev)1221 static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1222 {
1223 	int ret;
1224 
1225 	if (pm_runtime_suspended(dev))
1226 		return 0;
1227 
1228 	ret = mtk_jpeg_pm_suspend(dev);
1229 	return ret;
1230 }
1231 
mtk_jpeg_resume(struct device * dev)1232 static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1233 {
1234 	int ret;
1235 
1236 	if (pm_runtime_suspended(dev))
1237 		return 0;
1238 
1239 	ret = mtk_jpeg_pm_resume(dev);
1240 
1241 	return ret;
1242 }
1243 
1244 static const struct dev_pm_ops mtk_jpeg_pm_ops = {
1245 	SET_SYSTEM_SLEEP_PM_OPS(mtk_jpeg_suspend, mtk_jpeg_resume)
1246 	SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL)
1247 };
1248 
1249 static const struct of_device_id mtk_jpeg_match[] = {
1250 	{
1251 		.compatible = "mediatek,mt8173-jpgdec",
1252 		.data       = NULL,
1253 	},
1254 	{
1255 		.compatible = "mediatek,mt2701-jpgdec",
1256 		.data       = NULL,
1257 	},
1258 	{},
1259 };
1260 
1261 MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1262 
1263 static struct platform_driver mtk_jpeg_driver = {
1264 	.probe = mtk_jpeg_probe,
1265 	.remove = mtk_jpeg_remove,
1266 	.driver = {
1267 		.name           = MTK_JPEG_NAME,
1268 		.of_match_table = mtk_jpeg_match,
1269 		.pm             = &mtk_jpeg_pm_ops,
1270 	},
1271 };
1272 
1273 module_platform_driver(mtk_jpeg_driver);
1274 
1275 MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1276 MODULE_LICENSE("GPL v2");
1277