1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
5 * Rick Chang <rick.chang@mediatek.com>
10 #include <linux/interrupt.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>
26 #include "mtk_jpeg_hw.h"
27 #include "mtk_jpeg_core.h"
28 #include "mtk_jpeg_parse.h"
30 static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
32 .fourcc = V4L2_PIX_FMT_JPEG,
34 .flags = MTK_JPEG_FMT_FLAG_DEC_OUTPUT,
37 .fourcc = V4L2_PIX_FMT_YUV420M,
38 .h_sample = {4, 2, 2},
39 .v_sample = {4, 2, 2},
43 .flags = MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
46 .fourcc = V4L2_PIX_FMT_YUV422M,
47 .h_sample = {4, 2, 2},
48 .v_sample = {4, 4, 4},
52 .flags = MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
56 #define MTK_JPEG_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_formats)
59 MTK_JPEG_BUF_FLAGS_INIT = 0,
60 MTK_JPEG_BUF_FLAGS_LAST_FRAME = 1,
63 struct mtk_jpeg_src_buf {
64 struct vb2_v4l2_buffer b;
65 struct list_head list;
67 struct mtk_jpeg_dec_param dec_param;
71 module_param(debug, int, 0644);
73 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
75 return container_of(fh, struct mtk_jpeg_ctx, fh);
78 static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf(
79 struct vb2_buffer *vb)
81 return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b);
84 static int mtk_jpeg_querycap(struct file *file, void *priv,
85 struct v4l2_capability *cap)
87 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
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",
97 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
98 struct v4l2_fmtdesc *f, u32 type)
102 for (i = 0; i < n; ++i) {
103 if (mtk_jpeg_formats[i].flags & type) {
113 f->pixelformat = mtk_jpeg_formats[i].fourcc;
118 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
119 struct v4l2_fmtdesc *f)
121 return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
122 MTK_JPEG_FMT_FLAG_DEC_CAPTURE);
125 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
126 struct v4l2_fmtdesc *f)
128 return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
129 MTK_JPEG_FMT_FLAG_DEC_OUTPUT);
132 static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
133 enum v4l2_buf_type type)
135 if (V4L2_TYPE_IS_OUTPUT(type))
140 static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx,
142 unsigned int fmt_type)
144 unsigned int k, fmt_flag;
146 fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
147 MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
148 MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
150 for (k = 0; k < MTK_JPEG_NUM_FORMATS; k++) {
151 struct mtk_jpeg_fmt *fmt = &mtk_jpeg_formats[k];
153 if (fmt->fourcc == pixelformat && fmt->flags & fmt_flag)
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)
165 int width, height, w_step, h_step;
169 w_step = 1 << walign;
170 h_step = 1 << halign;
172 v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
173 if (*w < width && (*w + w_step) <= wmax)
175 if (*h < height && (*h + h_step) <= hmax)
179 static void mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx *ctx,
180 struct v4l2_format *f)
182 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
183 struct mtk_jpeg_q_data *q_data;
186 q_data = mtk_jpeg_get_q_data(ctx, f->type);
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;
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];
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)
203 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
204 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
207 memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
208 pix_mp->field = V4L2_FIELD_NONE;
210 if (ctx->state != MTK_JPEG_INIT) {
211 mtk_jpeg_adjust_fmt_mplane(ctx, f);
215 pix_mp->num_planes = fmt->colplanes;
216 pix_mp->pixelformat = fmt->fourcc;
218 if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) {
219 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
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);
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;
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);
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;
246 memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
247 pfmt->bytesperline = stride;
248 pfmt->sizeimage = stride * h;
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",
257 pix_mp->plane_fmt[i].bytesperline,
258 pix_mp->plane_fmt[i].sizeimage);
263 static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv,
264 struct v4l2_format *f)
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;
273 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
277 q_data = mtk_jpeg_get_q_data(ctx, f->type);
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;
290 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n",
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);
298 for (i = 0; i < pix_mp->num_planes; i++) {
299 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
301 pfmt->bytesperline = q_data->bytesperline[i];
302 pfmt->sizeimage = q_data->sizeimage[i];
303 memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
305 v4l2_dbg(1, debug, &jpeg->v4l2_dev,
306 "plane[%d] bpl=%u, size=%u\n",
314 static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
315 struct v4l2_format *f)
317 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
318 struct mtk_jpeg_fmt *fmt;
320 fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
321 MTK_JPEG_FMT_TYPE_CAPTURE);
323 fmt = ctx->cap_q.fmt;
325 v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
327 (fmt->fourcc & 0xff),
328 (fmt->fourcc >> 8 & 0xff),
329 (fmt->fourcc >> 16 & 0xff),
330 (fmt->fourcc >> 24 & 0xff));
332 return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_CAPTURE);
335 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
336 struct v4l2_format *f)
338 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
339 struct mtk_jpeg_fmt *fmt;
341 fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
342 MTK_JPEG_FMT_TYPE_OUTPUT);
344 fmt = ctx->out_q.fmt;
346 v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
348 (fmt->fourcc & 0xff),
349 (fmt->fourcc >> 8 & 0xff),
350 (fmt->fourcc >> 16 & 0xff),
351 (fmt->fourcc >> 24 & 0xff));
353 return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_OUTPUT);
356 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
357 struct v4l2_format *f)
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;
366 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
370 q_data = mtk_jpeg_get_q_data(ctx, f->type);
372 if (vb2_is_busy(vq)) {
373 v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
377 f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
378 MTK_JPEG_FMT_TYPE_OUTPUT : MTK_JPEG_FMT_TYPE_CAPTURE;
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;
388 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n",
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);
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;
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]);
408 static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv,
409 struct v4l2_format *f)
413 ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f);
417 return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f);
420 static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv,
421 struct v4l2_format *f)
425 ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f);
429 return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f);
432 static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx)
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,
440 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
443 static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
444 const struct v4l2_event_subscription *sub)
447 case V4L2_EVENT_SOURCE_CHANGE:
448 return v4l2_src_change_event_subscribe(fh, sub);
454 static int mtk_jpeg_g_selection(struct file *file, void *priv,
455 struct v4l2_selection *s)
457 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
459 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
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;
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;
483 static int mtk_jpeg_s_selection(struct file *file, void *priv,
484 struct v4l2_selection *s)
486 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
488 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
492 case V4L2_SEL_TGT_COMPOSE:
495 s->r.width = ctx->out_q.w;
496 s->r.height = ctx->out_q.h;
504 static int mtk_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
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;
512 if (buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
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");
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;
526 return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
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,
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,
553 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
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[])
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;
567 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
568 q->type, *num_buffers);
570 q_data = mtk_jpeg_get_q_data(ctx, q->type);
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",
584 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
586 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
587 struct mtk_jpeg_q_data *q_data = NULL;
590 q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
594 for (i = 0; i < q_data->fmt->colplanes; i++)
595 vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
600 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
601 struct mtk_jpeg_dec_param *param)
603 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
604 struct mtk_jpeg_q_data *q_data;
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");
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");
621 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
622 struct mtk_jpeg_dec_param *param)
624 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
625 struct mtk_jpeg_q_data *q_data;
628 q_data = &ctx->out_q;
629 q_data->w = param->pic_w;
630 q_data->h = param->pic_h;
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,
637 MTK_JPEG_FMT_TYPE_CAPTURE);
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];
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);
654 static void mtk_jpeg_buf_queue(struct vb2_buffer *vb)
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;
662 v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
663 vb->vb2_queue->type, vb->index, vb);
665 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
668 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
669 param = &jpeg_src_buf->dec_param;
670 memset(param, 0, sizeof(*param));
672 if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
673 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
676 header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
677 vb2_get_plane_payload(vb, 0));
679 v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
680 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
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);
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;
694 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
697 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
698 enum v4l2_buf_type type)
700 if (V4L2_TYPE_IS_OUTPUT(type))
701 return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
703 return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
706 static int mtk_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
708 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
709 struct vb2_v4l2_buffer *vb;
712 ret = pm_runtime_get_sync(ctx->jpeg->dev);
718 while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
719 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_QUEUED);
723 static void mtk_jpeg_stop_streaming(struct vb2_queue *q)
725 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
726 struct vb2_v4l2_buffer *vb;
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.
733 if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
734 !V4L2_TYPE_IS_OUTPUT(q->type)) {
735 struct mtk_jpeg_src_buf *src_buf;
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;
745 while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
746 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
748 pm_runtime_put_sync(ctx->jpeg->dev);
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,
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)
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);
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)
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);
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]);
792 fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
798 static void mtk_jpeg_device_run(void *priv)
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;
805 struct mtk_jpeg_src_buf *jpeg_src_buf;
806 struct mtk_jpeg_bs bs;
807 struct mtk_jpeg_fb fb;
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);
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;
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);
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))
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);
837 mtk_jpeg_dec_start(jpeg->dec_reg_base);
838 spin_unlock_irqrestore(&jpeg->hw_lock, flags);
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);
849 static int mtk_jpeg_job_ready(void *priv)
851 struct mtk_jpeg_ctx *ctx = priv;
853 return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
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,
861 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
862 struct vb2_queue *dst_vq)
864 struct mtk_jpeg_ctx *ctx = priv;
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);
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);
894 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
898 ret = mtk_smi_larb_get(jpeg->larb);
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);
905 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
907 clk_disable_unprepare(jpeg->clk_jdec);
908 clk_disable_unprepare(jpeg->clk_jdec_smi);
909 mtk_smi_larb_put(jpeg->larb);
912 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
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;
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);
927 v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
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);
935 if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
936 mtk_jpeg_dec_reset(jpeg->dec_reg_base);
938 if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
939 dev_err(jpeg->dev, "decode failed\n");
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]);
947 buf_state = VB2_BUF_STATE_DONE;
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);
956 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
958 struct mtk_jpeg_q_data *q = &ctx->out_q;
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;
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;
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;
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;
983 q->bytesperline[i] = stride;
984 q->sizeimage[i] = stride * h;
988 static int mtk_jpeg_open(struct file *file)
990 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
991 struct video_device *vfd = video_devdata(file);
992 struct mtk_jpeg_ctx *ctx;
995 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
999 if (mutex_lock_interruptible(&jpeg->lock)) {
1004 v4l2_fh_init(&ctx->fh, vfd);
1005 file->private_data = &ctx->fh;
1006 v4l2_fh_add(&ctx->fh);
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);
1016 mtk_jpeg_set_default_params(ctx);
1017 mutex_unlock(&jpeg->lock);
1021 v4l2_fh_del(&ctx->fh);
1022 v4l2_fh_exit(&ctx->fh);
1023 mutex_unlock(&jpeg->lock);
1029 static int mtk_jpeg_release(struct file *file)
1031 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1032 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
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);
1039 mutex_unlock(&jpeg->lock);
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,
1052 static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg)
1054 struct device_node *node;
1055 struct platform_device *pdev;
1057 node = of_parse_phandle(jpeg->dev->of_node, "mediatek,larb", 0);
1060 pdev = of_find_device_by_node(node);
1061 if (WARN_ON(!pdev)) {
1067 jpeg->larb = &pdev->dev;
1069 jpeg->clk_jdec = devm_clk_get(jpeg->dev, "jpgdec");
1070 if (IS_ERR(jpeg->clk_jdec))
1071 return PTR_ERR(jpeg->clk_jdec);
1073 jpeg->clk_jdec_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
1074 return PTR_ERR_OR_ZERO(jpeg->clk_jdec_smi);
1077 static int mtk_jpeg_probe(struct platform_device *pdev)
1079 struct mtk_jpeg_dev *jpeg;
1080 struct resource *res;
1084 jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1088 mutex_init(&jpeg->lock);
1089 spin_lock_init(&jpeg->hw_lock);
1090 jpeg->dev = &pdev->dev;
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);
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);
1107 ret = devm_request_irq(&pdev->dev, dec_irq, mtk_jpeg_dec_irq, 0,
1110 dev_err(&pdev->dev, "Failed to request dec_irq %d (%d)\n",
1116 ret = mtk_jpeg_clk_init(jpeg);
1118 dev_err(&pdev->dev, "Failed to init clk, err %d\n", ret);
1122 ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1124 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1126 goto err_dev_register;
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);
1136 jpeg->dec_vdev = video_device_alloc();
1137 if (!jpeg->dec_vdev) {
1139 goto err_dec_vdev_alloc;
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;
1153 ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
1155 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1156 goto err_dec_vdev_register;
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);
1164 platform_set_drvdata(pdev, jpeg);
1166 pm_runtime_enable(&pdev->dev);
1170 err_dec_vdev_register:
1171 video_device_release(jpeg->dec_vdev);
1174 v4l2_m2m_release(jpeg->m2m_dev);
1177 v4l2_device_unregister(&jpeg->v4l2_dev);
1188 static int mtk_jpeg_remove(struct platform_device *pdev)
1190 struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
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);
1201 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1203 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1205 mtk_jpeg_dec_reset(jpeg->dec_reg_base);
1206 mtk_jpeg_clk_off(jpeg);
1211 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1213 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1215 mtk_jpeg_clk_on(jpeg);
1216 mtk_jpeg_dec_reset(jpeg->dec_reg_base);
1221 static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1225 if (pm_runtime_suspended(dev))
1228 ret = mtk_jpeg_pm_suspend(dev);
1232 static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1236 if (pm_runtime_suspended(dev))
1239 ret = mtk_jpeg_pm_resume(dev);
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)
1249 static const struct of_device_id mtk_jpeg_match[] = {
1251 .compatible = "mediatek,mt8173-jpgdec",
1255 .compatible = "mediatek,mt2701-jpgdec",
1261 MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1263 static struct platform_driver mtk_jpeg_driver = {
1264 .probe = mtk_jpeg_probe,
1265 .remove = mtk_jpeg_remove,
1267 .name = MTK_JPEG_NAME,
1268 .of_match_table = mtk_jpeg_match,
1269 .pm = &mtk_jpeg_pm_ops,
1273 module_platform_driver(mtk_jpeg_driver);
1275 MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1276 MODULE_LICENSE("GPL v2");