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>
6 * Xia Jiang <xia.jiang@mediatek.com>
10 #include <linux/err.h>
11 #include <linux/interrupt.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>
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"
31 static struct mtk_jpeg_fmt mtk_jpeg_enc_formats[] = {
33 .fourcc = V4L2_PIX_FMT_JPEG,
35 .flags = MTK_JPEG_FMT_FLAG_CAPTURE,
38 .fourcc = V4L2_PIX_FMT_NV12M,
39 .hw_format = JPEG_ENC_YUV_FORMAT_NV12,
45 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
48 .fourcc = V4L2_PIX_FMT_NV21M,
49 .hw_format = JEPG_ENC_YUV_FORMAT_NV21,
55 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
58 .fourcc = V4L2_PIX_FMT_YUYV,
59 .hw_format = JPEG_ENC_YUV_FORMAT_YUYV,
65 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
68 .fourcc = V4L2_PIX_FMT_YVYU,
69 .hw_format = JPEG_ENC_YUV_FORMAT_YVYU,
75 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
79 static struct mtk_jpeg_fmt mtk_jpeg_dec_formats[] = {
81 .fourcc = V4L2_PIX_FMT_JPEG,
83 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
86 .fourcc = V4L2_PIX_FMT_YUV420M,
87 .h_sample = {4, 2, 2},
88 .v_sample = {4, 2, 2},
92 .flags = MTK_JPEG_FMT_FLAG_CAPTURE,
95 .fourcc = V4L2_PIX_FMT_YUV422M,
96 .h_sample = {4, 2, 2},
97 .v_sample = {4, 4, 4},
101 .flags = MTK_JPEG_FMT_FLAG_CAPTURE,
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)
108 struct mtk_jpeg_src_buf {
109 struct vb2_v4l2_buffer b;
110 struct list_head list;
111 struct mtk_jpeg_dec_param dec_param;
115 module_param(debug, int, 0644);
117 static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
119 return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl);
122 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
124 return container_of(fh, struct mtk_jpeg_ctx, fh);
127 static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf(
128 struct vb2_buffer *vb)
130 return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b);
133 static int mtk_jpeg_querycap(struct file *file, void *priv,
134 struct v4l2_capability *cap)
136 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
138 strscpy(cap->driver, jpeg->variant->dev_name, sizeof(cap->driver));
139 strscpy(cap->card, jpeg->variant->dev_name, sizeof(cap->card));
144 static int vidioc_jpeg_enc_s_ctrl(struct v4l2_ctrl *ctrl)
146 struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
149 case V4L2_CID_JPEG_RESTART_INTERVAL:
150 ctx->restart_interval = ctrl->val;
152 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
153 ctx->enc_quality = ctrl->val;
155 case V4L2_CID_JPEG_ACTIVE_MARKER:
156 ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1;
163 static const struct v4l2_ctrl_ops mtk_jpeg_enc_ctrl_ops = {
164 .s_ctrl = vidioc_jpeg_enc_s_ctrl,
167 static int mtk_jpeg_enc_ctrls_setup(struct mtk_jpeg_ctx *ctx)
169 const struct v4l2_ctrl_ops *ops = &mtk_jpeg_enc_ctrl_ops;
170 struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
172 v4l2_ctrl_handler_init(handler, 3);
174 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100,
176 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_COMPRESSION_QUALITY, 48,
178 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0,
179 V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0);
181 if (handler->error) {
182 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
183 return handler->error;
186 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
191 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
192 struct v4l2_fmtdesc *f, u32 type)
196 for (i = 0; i < n; ++i) {
197 if (mtk_jpeg_formats[i].flags & type) {
207 f->pixelformat = mtk_jpeg_formats[i].fourcc;
212 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
213 struct v4l2_fmtdesc *f)
215 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
216 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
218 return mtk_jpeg_enum_fmt(jpeg->variant->formats,
219 jpeg->variant->num_formats, f,
220 MTK_JPEG_FMT_FLAG_CAPTURE);
223 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
224 struct v4l2_fmtdesc *f)
226 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
227 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
229 return mtk_jpeg_enum_fmt(jpeg->variant->formats,
230 jpeg->variant->num_formats, f,
231 MTK_JPEG_FMT_FLAG_OUTPUT);
234 static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
235 enum v4l2_buf_type type)
237 if (V4L2_TYPE_IS_OUTPUT(type))
242 static struct mtk_jpeg_fmt *
243 mtk_jpeg_find_format(struct mtk_jpeg_fmt *mtk_jpeg_formats, int num_formats,
244 u32 pixelformat, unsigned int fmt_type)
247 struct mtk_jpeg_fmt *fmt;
249 for (k = 0; k < num_formats; k++) {
250 fmt = &mtk_jpeg_formats[k];
252 if (fmt->fourcc == pixelformat && fmt->flags & fmt_type)
259 static int mtk_jpeg_try_fmt_mplane(struct v4l2_pix_format_mplane *pix_mp,
260 struct mtk_jpeg_fmt *fmt)
264 pix_mp->field = V4L2_FIELD_NONE;
266 pix_mp->num_planes = fmt->colplanes;
267 pix_mp->pixelformat = fmt->fourcc;
269 if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
270 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
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,
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;
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);
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;
296 pfmt->bytesperline = stride;
297 pfmt->sizeimage = stride * h;
302 static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv,
303 struct v4l2_format *f)
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;
312 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
316 q_data = mtk_jpeg_get_q_data(ctx, f->type);
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;
328 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n",
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);
336 for (i = 0; i < pix_mp->num_planes; i++) {
337 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
339 pfmt->bytesperline = q_data->pix_mp.plane_fmt[i].bytesperline;
340 pfmt->sizeimage = q_data->pix_mp.plane_fmt[i].sizeimage;
342 v4l2_dbg(1, debug, &jpeg->v4l2_dev,
343 "plane[%d] bpl=%u, size=%u\n",
351 static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
352 struct v4l2_format *f)
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;
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);
363 fmt = ctx->cap_q.fmt;
365 v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
367 (fmt->fourcc & 0xff),
368 (fmt->fourcc >> 8 & 0xff),
369 (fmt->fourcc >> 16 & 0xff),
370 (fmt->fourcc >> 24 & 0xff));
372 if (ctx->state != MTK_JPEG_INIT) {
373 mtk_jpeg_g_fmt_vid_mplane(file, priv, f);
377 return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt);
380 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
381 struct v4l2_format *f)
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;
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);
392 fmt = ctx->out_q.fmt;
394 v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
396 (fmt->fourcc & 0xff),
397 (fmt->fourcc >> 8 & 0xff),
398 (fmt->fourcc >> 16 & 0xff),
399 (fmt->fourcc >> 24 & 0xff));
401 if (ctx->state != MTK_JPEG_INIT) {
402 mtk_jpeg_g_fmt_vid_mplane(file, priv, f);
406 return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt);
409 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
410 struct v4l2_format *f, unsigned int fmt_type)
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;
418 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
422 q_data = mtk_jpeg_get_q_data(ctx, f->type);
424 if (vb2_is_busy(vq)) {
425 v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
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;
441 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n",
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);
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;
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);
464 static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv,
465 struct v4l2_format *f)
469 ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f);
473 return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f,
474 MTK_JPEG_FMT_FLAG_OUTPUT);
477 static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv,
478 struct v4l2_format *f)
482 ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f);
486 return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f,
487 MTK_JPEG_FMT_FLAG_CAPTURE);
490 static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx)
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,
498 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
501 static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
502 const struct v4l2_event_subscription *sub)
505 case V4L2_EVENT_SOURCE_CHANGE:
506 return v4l2_src_change_event_subscribe(fh, sub);
509 return v4l2_ctrl_subscribe_event(fh, sub);
512 static int mtk_jpeg_enc_g_selection(struct file *file, void *priv,
513 struct v4l2_selection *s)
515 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
517 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
521 case V4L2_SEL_TGT_CROP:
522 s->r = ctx->out_q.enc_crop_rect;
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;
537 static int mtk_jpeg_dec_g_selection(struct file *file, void *priv,
538 struct v4l2_selection *s)
540 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
542 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
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;
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;
566 static int mtk_jpeg_enc_s_selection(struct file *file, void *priv,
567 struct v4l2_selection *s)
569 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
571 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
575 case V4L2_SEL_TGT_CROP:
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;
589 static const struct v4l2_ioctl_ops mtk_jpeg_enc_ioctl_ops = {
590 .vidioc_querycap = mtk_jpeg_querycap,
591 .vidioc_enum_fmt_vid_cap = mtk_jpeg_enum_fmt_vid_cap,
592 .vidioc_enum_fmt_vid_out = mtk_jpeg_enum_fmt_vid_out,
593 .vidioc_try_fmt_vid_cap_mplane = mtk_jpeg_try_fmt_vid_cap_mplane,
594 .vidioc_try_fmt_vid_out_mplane = mtk_jpeg_try_fmt_vid_out_mplane,
595 .vidioc_g_fmt_vid_cap_mplane = mtk_jpeg_g_fmt_vid_mplane,
596 .vidioc_g_fmt_vid_out_mplane = mtk_jpeg_g_fmt_vid_mplane,
597 .vidioc_s_fmt_vid_cap_mplane = mtk_jpeg_s_fmt_vid_cap_mplane,
598 .vidioc_s_fmt_vid_out_mplane = mtk_jpeg_s_fmt_vid_out_mplane,
599 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
600 .vidioc_subscribe_event = mtk_jpeg_subscribe_event,
601 .vidioc_g_selection = mtk_jpeg_enc_g_selection,
602 .vidioc_s_selection = mtk_jpeg_enc_s_selection,
604 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
605 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
606 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
607 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
608 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
609 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
610 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
611 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
613 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
616 static const struct v4l2_ioctl_ops mtk_jpeg_dec_ioctl_ops = {
617 .vidioc_querycap = mtk_jpeg_querycap,
618 .vidioc_enum_fmt_vid_cap = mtk_jpeg_enum_fmt_vid_cap,
619 .vidioc_enum_fmt_vid_out = mtk_jpeg_enum_fmt_vid_out,
620 .vidioc_try_fmt_vid_cap_mplane = mtk_jpeg_try_fmt_vid_cap_mplane,
621 .vidioc_try_fmt_vid_out_mplane = mtk_jpeg_try_fmt_vid_out_mplane,
622 .vidioc_g_fmt_vid_cap_mplane = mtk_jpeg_g_fmt_vid_mplane,
623 .vidioc_g_fmt_vid_out_mplane = mtk_jpeg_g_fmt_vid_mplane,
624 .vidioc_s_fmt_vid_cap_mplane = mtk_jpeg_s_fmt_vid_cap_mplane,
625 .vidioc_s_fmt_vid_out_mplane = mtk_jpeg_s_fmt_vid_out_mplane,
626 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
627 .vidioc_subscribe_event = mtk_jpeg_subscribe_event,
628 .vidioc_g_selection = mtk_jpeg_dec_g_selection,
630 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
631 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
632 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
633 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
634 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
635 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
636 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
637 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
639 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
642 static int mtk_jpeg_queue_setup(struct vb2_queue *q,
643 unsigned int *num_buffers,
644 unsigned int *num_planes,
645 unsigned int sizes[],
646 struct device *alloc_ctxs[])
648 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
649 struct mtk_jpeg_q_data *q_data = NULL;
650 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
653 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
654 q->type, *num_buffers);
656 q_data = mtk_jpeg_get_q_data(ctx, q->type);
661 for (i = 0; i < *num_planes; i++)
662 if (sizes[i] < q_data->pix_mp.plane_fmt[i].sizeimage)
667 *num_planes = q_data->fmt->colplanes;
668 for (i = 0; i < q_data->fmt->colplanes; i++) {
669 sizes[i] = q_data->pix_mp.plane_fmt[i].sizeimage;
670 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n",
677 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
679 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
680 struct mtk_jpeg_q_data *q_data = NULL;
681 struct v4l2_plane_pix_format plane_fmt;
684 q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
688 for (i = 0; i < q_data->fmt->colplanes; i++) {
689 plane_fmt = q_data->pix_mp.plane_fmt[i];
690 if (ctx->enable_exif &&
691 q_data->fmt->fourcc == V4L2_PIX_FMT_JPEG)
692 vb2_set_plane_payload(vb, i, plane_fmt.sizeimage +
693 MTK_JPEG_MAX_EXIF_SIZE);
695 vb2_set_plane_payload(vb, i, plane_fmt.sizeimage);
701 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
702 struct mtk_jpeg_dec_param *param)
704 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
705 struct mtk_jpeg_q_data *q_data;
707 q_data = &ctx->out_q;
708 if (q_data->pix_mp.width != param->pic_w ||
709 q_data->pix_mp.height != param->pic_h) {
710 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n");
714 q_data = &ctx->cap_q;
716 mtk_jpeg_find_format(jpeg->variant->formats,
717 jpeg->variant->num_formats, param->dst_fourcc,
718 MTK_JPEG_FMT_FLAG_CAPTURE)) {
719 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n");
725 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
726 struct mtk_jpeg_dec_param *param)
728 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
729 struct mtk_jpeg_q_data *q_data;
732 q_data = &ctx->out_q;
733 q_data->pix_mp.width = param->pic_w;
734 q_data->pix_mp.height = param->pic_h;
736 q_data = &ctx->cap_q;
737 q_data->pix_mp.width = param->dec_w;
738 q_data->pix_mp.height = param->dec_h;
739 q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
740 jpeg->variant->num_formats,
742 MTK_JPEG_FMT_FLAG_CAPTURE);
744 for (i = 0; i < q_data->fmt->colplanes; i++) {
745 q_data->pix_mp.plane_fmt[i].bytesperline = param->mem_stride[i];
746 q_data->pix_mp.plane_fmt[i].sizeimage = param->comp_size[i];
749 v4l2_dbg(1, debug, &jpeg->v4l2_dev,
750 "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n",
751 (param->dst_fourcc & 0xff),
752 (param->dst_fourcc >> 8 & 0xff),
753 (param->dst_fourcc >> 16 & 0xff),
754 (param->dst_fourcc >> 24 & 0xff),
755 param->pic_w, param->pic_h,
756 param->dec_w, param->dec_h);
759 static void mtk_jpeg_enc_buf_queue(struct vb2_buffer *vb)
761 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
762 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
764 v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
765 vb->vb2_queue->type, vb->index, vb);
767 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
770 static void mtk_jpeg_dec_buf_queue(struct vb2_buffer *vb)
772 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
773 struct mtk_jpeg_dec_param *param;
774 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
775 struct mtk_jpeg_src_buf *jpeg_src_buf;
778 v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
779 vb->vb2_queue->type, vb->index, vb);
781 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
784 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
785 param = &jpeg_src_buf->dec_param;
786 memset(param, 0, sizeof(*param));
788 header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
789 vb2_get_plane_payload(vb, 0));
791 v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
792 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
796 if (ctx->state == MTK_JPEG_INIT) {
797 struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
798 ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
800 mtk_jpeg_queue_src_chg_event(ctx);
801 mtk_jpeg_set_queue_data(ctx, param);
802 ctx->state = vb2_is_streaming(dst_vq) ?
803 MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
806 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
809 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
810 enum v4l2_buf_type type)
812 if (V4L2_TYPE_IS_OUTPUT(type))
813 return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
815 return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
818 static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q)
820 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
821 struct vb2_v4l2_buffer *vb;
823 while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
824 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
827 static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q)
829 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
830 struct vb2_v4l2_buffer *vb;
833 * STREAMOFF is an acknowledgment for source change event.
834 * Before STREAMOFF, we still have to return the old resolution and
835 * subsampling. Update capture queue when the stream is off.
837 if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
838 V4L2_TYPE_IS_CAPTURE(q->type)) {
839 struct mtk_jpeg_src_buf *src_buf;
841 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
842 src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
843 mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
844 ctx->state = MTK_JPEG_RUNNING;
845 } else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
846 ctx->state = MTK_JPEG_INIT;
849 while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
850 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
853 static const struct vb2_ops mtk_jpeg_dec_qops = {
854 .queue_setup = mtk_jpeg_queue_setup,
855 .buf_prepare = mtk_jpeg_buf_prepare,
856 .buf_queue = mtk_jpeg_dec_buf_queue,
857 .wait_prepare = vb2_ops_wait_prepare,
858 .wait_finish = vb2_ops_wait_finish,
859 .stop_streaming = mtk_jpeg_dec_stop_streaming,
862 static const struct vb2_ops mtk_jpeg_enc_qops = {
863 .queue_setup = mtk_jpeg_queue_setup,
864 .buf_prepare = mtk_jpeg_buf_prepare,
865 .buf_queue = mtk_jpeg_enc_buf_queue,
866 .wait_prepare = vb2_ops_wait_prepare,
867 .wait_finish = vb2_ops_wait_finish,
868 .stop_streaming = mtk_jpeg_enc_stop_streaming,
871 static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
872 struct vb2_buffer *src_buf,
873 struct mtk_jpeg_bs *bs)
875 bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
876 bs->end_addr = bs->str_addr +
877 round_up(vb2_get_plane_payload(src_buf, 0), 16);
878 bs->size = round_up(vb2_plane_size(src_buf, 0), 128);
881 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
882 struct mtk_jpeg_dec_param *param,
883 struct vb2_buffer *dst_buf,
884 struct mtk_jpeg_fb *fb)
888 if (param->comp_num != dst_buf->num_planes) {
889 dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n",
890 param->comp_num, dst_buf->num_planes);
894 for (i = 0; i < dst_buf->num_planes; i++) {
895 if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) {
896 dev_err(ctx->jpeg->dev,
897 "buffer size is underflow (%lu < %u)\n",
898 vb2_plane_size(dst_buf, 0),
899 param->comp_size[i]);
902 fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
908 static void mtk_jpeg_enc_device_run(void *priv)
910 struct mtk_jpeg_ctx *ctx = priv;
911 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
912 struct vb2_v4l2_buffer *src_buf, *dst_buf;
913 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
917 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
918 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
920 ret = pm_runtime_resume_and_get(jpeg->dev);
924 schedule_delayed_work(&jpeg->job_timeout_work,
925 msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
927 spin_lock_irqsave(&jpeg->hw_lock, flags);
930 * Resetting the hardware every frame is to ensure that all the
931 * registers are cleared. This is a hardware requirement.
933 mtk_jpeg_enc_reset(jpeg->reg_base);
935 mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf);
936 mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf);
937 mtk_jpeg_set_enc_params(ctx, jpeg->reg_base);
938 mtk_jpeg_enc_start(jpeg->reg_base);
939 spin_unlock_irqrestore(&jpeg->hw_lock, flags);
943 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
944 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
945 v4l2_m2m_buf_done(src_buf, buf_state);
946 v4l2_m2m_buf_done(dst_buf, buf_state);
947 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
950 static void mtk_jpeg_dec_device_run(void *priv)
952 struct mtk_jpeg_ctx *ctx = priv;
953 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
954 struct vb2_v4l2_buffer *src_buf, *dst_buf;
955 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
957 struct mtk_jpeg_src_buf *jpeg_src_buf;
958 struct mtk_jpeg_bs bs;
959 struct mtk_jpeg_fb fb;
962 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
963 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
964 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
966 if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
967 mtk_jpeg_queue_src_chg_event(ctx);
968 ctx->state = MTK_JPEG_SOURCE_CHANGE;
969 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
973 ret = pm_runtime_resume_and_get(jpeg->dev);
977 schedule_delayed_work(&jpeg->job_timeout_work,
978 msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
980 mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
981 if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
984 spin_lock_irqsave(&jpeg->hw_lock, flags);
985 mtk_jpeg_dec_reset(jpeg->reg_base);
986 mtk_jpeg_dec_set_config(jpeg->reg_base,
987 &jpeg_src_buf->dec_param, &bs, &fb);
989 mtk_jpeg_dec_start(jpeg->reg_base);
990 spin_unlock_irqrestore(&jpeg->hw_lock, flags);
994 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
995 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
996 v4l2_m2m_buf_done(src_buf, buf_state);
997 v4l2_m2m_buf_done(dst_buf, buf_state);
998 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1001 static int mtk_jpeg_dec_job_ready(void *priv)
1003 struct mtk_jpeg_ctx *ctx = priv;
1005 return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
1008 static const struct v4l2_m2m_ops mtk_jpeg_enc_m2m_ops = {
1009 .device_run = mtk_jpeg_enc_device_run,
1012 static const struct v4l2_m2m_ops mtk_jpeg_dec_m2m_ops = {
1013 .device_run = mtk_jpeg_dec_device_run,
1014 .job_ready = mtk_jpeg_dec_job_ready,
1017 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
1018 struct vb2_queue *dst_vq)
1020 struct mtk_jpeg_ctx *ctx = priv;
1021 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1024 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1025 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1026 src_vq->drv_priv = ctx;
1027 src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf);
1028 src_vq->ops = jpeg->variant->qops;
1029 src_vq->mem_ops = &vb2_dma_contig_memops;
1030 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1031 src_vq->lock = &ctx->jpeg->lock;
1032 src_vq->dev = ctx->jpeg->dev;
1033 ret = vb2_queue_init(src_vq);
1037 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1038 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1039 dst_vq->drv_priv = ctx;
1040 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1041 dst_vq->ops = jpeg->variant->qops;
1042 dst_vq->mem_ops = &vb2_dma_contig_memops;
1043 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1044 dst_vq->lock = &ctx->jpeg->lock;
1045 dst_vq->dev = ctx->jpeg->dev;
1046 ret = vb2_queue_init(dst_vq);
1051 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
1055 ret = clk_bulk_prepare_enable(jpeg->variant->num_clks,
1056 jpeg->variant->clks);
1058 dev_err(jpeg->dev, "Failed to open jpeg clk: %d\n", ret);
1061 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
1063 clk_bulk_disable_unprepare(jpeg->variant->num_clks,
1064 jpeg->variant->clks);
1067 static irqreturn_t mtk_jpeg_enc_done(struct mtk_jpeg_dev *jpeg)
1069 struct mtk_jpeg_ctx *ctx;
1070 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1071 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1074 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1076 v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1080 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1081 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1083 result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
1084 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, result_size);
1086 buf_state = VB2_BUF_STATE_DONE;
1088 v4l2_m2m_buf_done(src_buf, buf_state);
1089 v4l2_m2m_buf_done(dst_buf, buf_state);
1090 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1091 pm_runtime_put(ctx->jpeg->dev);
1095 static irqreturn_t mtk_jpeg_enc_irq(int irq, void *priv)
1097 struct mtk_jpeg_dev *jpeg = priv;
1099 irqreturn_t ret = IRQ_NONE;
1101 cancel_delayed_work(&jpeg->job_timeout_work);
1103 irq_status = readl(jpeg->reg_base + JPEG_ENC_INT_STS) &
1104 JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
1106 writel(0, jpeg->reg_base + JPEG_ENC_INT_STS);
1108 if (!(irq_status & JPEG_ENC_INT_STATUS_DONE))
1111 ret = mtk_jpeg_enc_done(jpeg);
1115 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
1117 struct mtk_jpeg_dev *jpeg = priv;
1118 struct mtk_jpeg_ctx *ctx;
1119 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1120 struct mtk_jpeg_src_buf *jpeg_src_buf;
1121 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1126 cancel_delayed_work(&jpeg->job_timeout_work);
1128 dec_ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base);
1129 dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
1130 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1132 v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1136 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1137 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1138 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1140 if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
1141 mtk_jpeg_dec_reset(jpeg->reg_base);
1143 if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
1144 dev_err(jpeg->dev, "decode failed\n");
1148 for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
1149 vb2_set_plane_payload(&dst_buf->vb2_buf, i,
1150 jpeg_src_buf->dec_param.comp_size[i]);
1152 buf_state = VB2_BUF_STATE_DONE;
1155 v4l2_m2m_buf_done(src_buf, buf_state);
1156 v4l2_m2m_buf_done(dst_buf, buf_state);
1157 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1158 pm_runtime_put(ctx->jpeg->dev);
1162 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
1164 struct mtk_jpeg_q_data *q = &ctx->out_q;
1165 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1167 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1168 q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1169 q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1170 q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1171 q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1173 q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1174 jpeg->variant->num_formats,
1175 jpeg->variant->out_q_default_fourcc,
1176 MTK_JPEG_FMT_FLAG_OUTPUT);
1177 q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1178 q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1179 mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1182 q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1183 jpeg->variant->num_formats,
1184 jpeg->variant->cap_q_default_fourcc,
1185 MTK_JPEG_FMT_FLAG_CAPTURE);
1186 q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1187 q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1188 q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1189 q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1190 q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1191 q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1193 mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1196 static int mtk_jpeg_open(struct file *file)
1198 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1199 struct video_device *vfd = video_devdata(file);
1200 struct mtk_jpeg_ctx *ctx;
1203 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1207 if (mutex_lock_interruptible(&jpeg->lock)) {
1212 v4l2_fh_init(&ctx->fh, vfd);
1213 file->private_data = &ctx->fh;
1214 v4l2_fh_add(&ctx->fh);
1217 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx,
1218 mtk_jpeg_queue_init);
1219 if (IS_ERR(ctx->fh.m2m_ctx)) {
1220 ret = PTR_ERR(ctx->fh.m2m_ctx);
1224 if (jpeg->variant->cap_q_default_fourcc == V4L2_PIX_FMT_JPEG) {
1225 ret = mtk_jpeg_enc_ctrls_setup(ctx);
1227 v4l2_err(&jpeg->v4l2_dev, "Failed to setup jpeg enc controls\n");
1231 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 0);
1233 mtk_jpeg_set_default_params(ctx);
1234 mutex_unlock(&jpeg->lock);
1238 v4l2_fh_del(&ctx->fh);
1239 v4l2_fh_exit(&ctx->fh);
1240 mutex_unlock(&jpeg->lock);
1246 static int mtk_jpeg_release(struct file *file)
1248 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1249 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
1251 mutex_lock(&jpeg->lock);
1252 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1253 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1254 v4l2_fh_del(&ctx->fh);
1255 v4l2_fh_exit(&ctx->fh);
1257 mutex_unlock(&jpeg->lock);
1261 static const struct v4l2_file_operations mtk_jpeg_fops = {
1262 .owner = THIS_MODULE,
1263 .open = mtk_jpeg_open,
1264 .release = mtk_jpeg_release,
1265 .poll = v4l2_m2m_fop_poll,
1266 .unlocked_ioctl = video_ioctl2,
1267 .mmap = v4l2_m2m_fop_mmap,
1270 static struct clk_bulk_data mt8173_jpeg_dec_clocks[] = {
1271 { .id = "jpgdec-smi" },
1275 static struct clk_bulk_data mtk_jpeg_clocks[] = {
1279 static void mtk_jpeg_job_timeout_work(struct work_struct *work)
1281 struct mtk_jpeg_dev *jpeg = container_of(work, struct mtk_jpeg_dev,
1282 job_timeout_work.work);
1283 struct mtk_jpeg_ctx *ctx;
1284 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1286 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1287 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1288 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1290 jpeg->variant->hw_reset(jpeg->reg_base);
1292 pm_runtime_put(jpeg->dev);
1294 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1295 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1296 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1299 static int mtk_jpeg_probe(struct platform_device *pdev)
1301 struct mtk_jpeg_dev *jpeg;
1305 jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1309 mutex_init(&jpeg->lock);
1310 spin_lock_init(&jpeg->hw_lock);
1311 jpeg->dev = &pdev->dev;
1312 jpeg->variant = of_device_get_match_data(jpeg->dev);
1313 INIT_DELAYED_WORK(&jpeg->job_timeout_work, mtk_jpeg_job_timeout_work);
1315 jpeg->reg_base = devm_platform_ioremap_resource(pdev, 0);
1316 if (IS_ERR(jpeg->reg_base)) {
1317 ret = PTR_ERR(jpeg->reg_base);
1321 jpeg_irq = platform_get_irq(pdev, 0);
1325 ret = devm_request_irq(&pdev->dev, jpeg_irq,
1326 jpeg->variant->irq_handler, 0, pdev->name, jpeg);
1328 dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n",
1333 ret = devm_clk_bulk_get(jpeg->dev, jpeg->variant->num_clks,
1334 jpeg->variant->clks);
1336 dev_err(&pdev->dev, "Failed to init clk, err %d\n", ret);
1340 ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1342 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1344 goto err_dev_register;
1347 jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
1349 if (IS_ERR(jpeg->m2m_dev)) {
1350 v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1351 ret = PTR_ERR(jpeg->m2m_dev);
1355 jpeg->vdev = video_device_alloc();
1358 goto err_vfd_jpeg_alloc;
1360 snprintf(jpeg->vdev->name, sizeof(jpeg->vdev->name),
1361 "%s", jpeg->variant->dev_name);
1362 jpeg->vdev->fops = &mtk_jpeg_fops;
1363 jpeg->vdev->ioctl_ops = jpeg->variant->ioctl_ops;
1364 jpeg->vdev->minor = -1;
1365 jpeg->vdev->release = video_device_release;
1366 jpeg->vdev->lock = &jpeg->lock;
1367 jpeg->vdev->v4l2_dev = &jpeg->v4l2_dev;
1368 jpeg->vdev->vfl_dir = VFL_DIR_M2M;
1369 jpeg->vdev->device_caps = V4L2_CAP_STREAMING |
1370 V4L2_CAP_VIDEO_M2M_MPLANE;
1372 ret = video_register_device(jpeg->vdev, VFL_TYPE_VIDEO, -1);
1374 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1375 goto err_vfd_jpeg_register;
1378 video_set_drvdata(jpeg->vdev, jpeg);
1379 v4l2_info(&jpeg->v4l2_dev,
1380 "%s device registered as /dev/video%d (%d,%d)\n",
1381 jpeg->variant->dev_name, jpeg->vdev->num,
1382 VIDEO_MAJOR, jpeg->vdev->minor);
1384 platform_set_drvdata(pdev, jpeg);
1386 pm_runtime_enable(&pdev->dev);
1390 err_vfd_jpeg_register:
1391 video_device_release(jpeg->vdev);
1394 v4l2_m2m_release(jpeg->m2m_dev);
1397 v4l2_device_unregister(&jpeg->v4l2_dev);
1408 static int mtk_jpeg_remove(struct platform_device *pdev)
1410 struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
1412 pm_runtime_disable(&pdev->dev);
1413 video_unregister_device(jpeg->vdev);
1414 video_device_release(jpeg->vdev);
1415 v4l2_m2m_release(jpeg->m2m_dev);
1416 v4l2_device_unregister(&jpeg->v4l2_dev);
1421 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1423 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1425 mtk_jpeg_clk_off(jpeg);
1430 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1432 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1434 mtk_jpeg_clk_on(jpeg);
1439 static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1441 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1443 v4l2_m2m_suspend(jpeg->m2m_dev);
1444 return pm_runtime_force_suspend(dev);
1447 static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1449 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1452 ret = pm_runtime_force_resume(dev);
1456 v4l2_m2m_resume(jpeg->m2m_dev);
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)
1465 static const struct mtk_jpeg_variant mt8173_jpeg_drvdata = {
1466 .clks = mt8173_jpeg_dec_clocks,
1467 .num_clks = ARRAY_SIZE(mt8173_jpeg_dec_clocks),
1468 .formats = mtk_jpeg_dec_formats,
1469 .num_formats = MTK_JPEG_DEC_NUM_FORMATS,
1470 .qops = &mtk_jpeg_dec_qops,
1471 .irq_handler = mtk_jpeg_dec_irq,
1472 .hw_reset = mtk_jpeg_dec_reset,
1473 .m2m_ops = &mtk_jpeg_dec_m2m_ops,
1474 .dev_name = "mtk-jpeg-dec",
1475 .ioctl_ops = &mtk_jpeg_dec_ioctl_ops,
1476 .out_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1477 .cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M,
1480 static const struct mtk_jpeg_variant mtk_jpeg_drvdata = {
1481 .clks = mtk_jpeg_clocks,
1482 .num_clks = ARRAY_SIZE(mtk_jpeg_clocks),
1483 .formats = mtk_jpeg_enc_formats,
1484 .num_formats = MTK_JPEG_ENC_NUM_FORMATS,
1485 .qops = &mtk_jpeg_enc_qops,
1486 .irq_handler = mtk_jpeg_enc_irq,
1487 .hw_reset = mtk_jpeg_enc_reset,
1488 .m2m_ops = &mtk_jpeg_enc_m2m_ops,
1489 .dev_name = "mtk-jpeg-enc",
1490 .ioctl_ops = &mtk_jpeg_enc_ioctl_ops,
1491 .out_q_default_fourcc = V4L2_PIX_FMT_YUYV,
1492 .cap_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1495 static const struct of_device_id mtk_jpeg_match[] = {
1497 .compatible = "mediatek,mt8173-jpgdec",
1498 .data = &mt8173_jpeg_drvdata,
1501 .compatible = "mediatek,mt2701-jpgdec",
1502 .data = &mt8173_jpeg_drvdata,
1505 .compatible = "mediatek,mtk-jpgenc",
1506 .data = &mtk_jpeg_drvdata,
1511 MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1513 static struct platform_driver mtk_jpeg_driver = {
1514 .probe = mtk_jpeg_probe,
1515 .remove = mtk_jpeg_remove,
1517 .name = MTK_JPEG_NAME,
1518 .of_match_table = mtk_jpeg_match,
1519 .pm = &mtk_jpeg_pm_ops,
1523 module_platform_driver(mtk_jpeg_driver);
1525 MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1526 MODULE_LICENSE("GPL v2");