1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 MediaTek Inc.
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>
25 #include <soc/mediatek/smi.h>
27 #include "mtk_jpeg_enc_hw.h"
28 #include "mtk_jpeg_dec_hw.h"
29 #include "mtk_jpeg_core.h"
30 #include "mtk_jpeg_dec_parse.h"
32 static struct mtk_jpeg_fmt mtk_jpeg_enc_formats[] = {
34 .fourcc = V4L2_PIX_FMT_JPEG,
36 .flags = MTK_JPEG_FMT_FLAG_CAPTURE,
39 .fourcc = V4L2_PIX_FMT_NV12M,
40 .hw_format = JPEG_ENC_YUV_FORMAT_NV12,
46 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
49 .fourcc = V4L2_PIX_FMT_NV21M,
50 .hw_format = JEPG_ENC_YUV_FORMAT_NV21,
56 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
59 .fourcc = V4L2_PIX_FMT_YUYV,
60 .hw_format = JPEG_ENC_YUV_FORMAT_YUYV,
66 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
69 .fourcc = V4L2_PIX_FMT_YVYU,
70 .hw_format = JPEG_ENC_YUV_FORMAT_YVYU,
76 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
80 static struct mtk_jpeg_fmt mtk_jpeg_dec_formats[] = {
82 .fourcc = V4L2_PIX_FMT_JPEG,
84 .flags = MTK_JPEG_FMT_FLAG_OUTPUT,
87 .fourcc = V4L2_PIX_FMT_YUV420M,
88 .h_sample = {4, 2, 2},
89 .v_sample = {4, 2, 2},
93 .flags = MTK_JPEG_FMT_FLAG_CAPTURE,
96 .fourcc = V4L2_PIX_FMT_YUV422M,
97 .h_sample = {4, 2, 2},
98 .v_sample = {4, 4, 4},
102 .flags = MTK_JPEG_FMT_FLAG_CAPTURE,
106 #define MTK_JPEG_ENC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_enc_formats)
107 #define MTK_JPEG_DEC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_dec_formats)
109 struct mtk_jpeg_src_buf {
110 struct vb2_v4l2_buffer b;
111 struct list_head list;
112 struct mtk_jpeg_dec_param dec_param;
116 module_param(debug, int, 0644);
118 static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
120 return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl);
123 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
125 return container_of(fh, struct mtk_jpeg_ctx, fh);
128 static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf(
129 struct vb2_buffer *vb)
131 return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b);
134 static int mtk_jpeg_querycap(struct file *file, void *priv,
135 struct v4l2_capability *cap)
137 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
139 strscpy(cap->driver, jpeg->variant->dev_name, sizeof(cap->driver));
140 strscpy(cap->card, jpeg->variant->dev_name, sizeof(cap->card));
141 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
142 dev_name(jpeg->dev));
147 static int vidioc_jpeg_enc_s_ctrl(struct v4l2_ctrl *ctrl)
149 struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl);
152 case V4L2_CID_JPEG_RESTART_INTERVAL:
153 ctx->restart_interval = ctrl->val;
155 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
156 ctx->enc_quality = ctrl->val;
158 case V4L2_CID_JPEG_ACTIVE_MARKER:
159 ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1;
166 static const struct v4l2_ctrl_ops mtk_jpeg_enc_ctrl_ops = {
167 .s_ctrl = vidioc_jpeg_enc_s_ctrl,
170 static int mtk_jpeg_enc_ctrls_setup(struct mtk_jpeg_ctx *ctx)
172 const struct v4l2_ctrl_ops *ops = &mtk_jpeg_enc_ctrl_ops;
173 struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
175 v4l2_ctrl_handler_init(handler, 3);
177 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100,
179 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_COMPRESSION_QUALITY, 48,
181 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0,
182 V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0);
184 if (handler->error) {
185 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
186 return handler->error;
189 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
194 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
195 struct v4l2_fmtdesc *f, u32 type)
199 for (i = 0; i < n; ++i) {
200 if (mtk_jpeg_formats[i].flags & type) {
210 f->pixelformat = mtk_jpeg_formats[i].fourcc;
215 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
216 struct v4l2_fmtdesc *f)
218 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
219 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
221 return mtk_jpeg_enum_fmt(jpeg->variant->formats,
222 jpeg->variant->num_formats, f,
223 MTK_JPEG_FMT_FLAG_CAPTURE);
226 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
227 struct v4l2_fmtdesc *f)
229 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
230 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
232 return mtk_jpeg_enum_fmt(jpeg->variant->formats,
233 jpeg->variant->num_formats, f,
234 MTK_JPEG_FMT_FLAG_OUTPUT);
237 static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
238 enum v4l2_buf_type type)
240 if (V4L2_TYPE_IS_OUTPUT(type))
245 static struct mtk_jpeg_fmt *
246 mtk_jpeg_find_format(struct mtk_jpeg_fmt *mtk_jpeg_formats, int num_formats,
247 u32 pixelformat, unsigned int fmt_type)
250 struct mtk_jpeg_fmt *fmt;
252 for (k = 0; k < num_formats; k++) {
253 fmt = &mtk_jpeg_formats[k];
255 if (fmt->fourcc == pixelformat && fmt->flags & fmt_type)
262 static int mtk_jpeg_try_fmt_mplane(struct v4l2_pix_format_mplane *pix_mp,
263 struct mtk_jpeg_fmt *fmt)
267 pix_mp->field = V4L2_FIELD_NONE;
269 pix_mp->num_planes = fmt->colplanes;
270 pix_mp->pixelformat = fmt->fourcc;
272 if (fmt->fourcc == V4L2_PIX_FMT_JPEG) {
273 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
275 pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT,
276 MTK_JPEG_MAX_HEIGHT);
277 pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH,
280 pfmt->bytesperline = 0;
281 /* Source size must be aligned to 128 */
282 pfmt->sizeimage = round_up(pfmt->sizeimage, 128);
283 if (pfmt->sizeimage == 0)
284 pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE;
289 pix_mp->height = clamp(round_up(pix_mp->height, fmt->v_align),
290 MTK_JPEG_MIN_HEIGHT, MTK_JPEG_MAX_HEIGHT);
291 pix_mp->width = clamp(round_up(pix_mp->width, fmt->h_align),
292 MTK_JPEG_MIN_WIDTH, MTK_JPEG_MAX_WIDTH);
294 for (i = 0; i < fmt->colplanes; i++) {
295 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
296 u32 stride = pix_mp->width * fmt->h_sample[i] / 4;
297 u32 h = pix_mp->height * fmt->v_sample[i] / 4;
299 pfmt->bytesperline = stride;
300 pfmt->sizeimage = stride * h;
305 static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv,
306 struct v4l2_format *f)
308 struct vb2_queue *vq;
309 struct mtk_jpeg_q_data *q_data = NULL;
310 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
311 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
312 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
315 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
319 q_data = mtk_jpeg_get_q_data(ctx, f->type);
321 pix_mp->width = q_data->pix_mp.width;
322 pix_mp->height = q_data->pix_mp.height;
323 pix_mp->field = V4L2_FIELD_NONE;
324 pix_mp->pixelformat = q_data->fmt->fourcc;
325 pix_mp->num_planes = q_data->fmt->colplanes;
326 pix_mp->colorspace = q_data->pix_mp.colorspace;
327 pix_mp->ycbcr_enc = q_data->pix_mp.ycbcr_enc;
328 pix_mp->xfer_func = q_data->pix_mp.xfer_func;
329 pix_mp->quantization = q_data->pix_mp.quantization;
331 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n",
333 (pix_mp->pixelformat & 0xff),
334 (pix_mp->pixelformat >> 8 & 0xff),
335 (pix_mp->pixelformat >> 16 & 0xff),
336 (pix_mp->pixelformat >> 24 & 0xff),
337 pix_mp->width, pix_mp->height);
339 for (i = 0; i < pix_mp->num_planes; i++) {
340 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
342 pfmt->bytesperline = q_data->pix_mp.plane_fmt[i].bytesperline;
343 pfmt->sizeimage = q_data->pix_mp.plane_fmt[i].sizeimage;
345 v4l2_dbg(1, debug, &jpeg->v4l2_dev,
346 "plane[%d] bpl=%u, size=%u\n",
354 static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
355 struct v4l2_format *f)
357 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
358 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
359 struct mtk_jpeg_fmt *fmt;
361 fmt = mtk_jpeg_find_format(jpeg->variant->formats,
362 jpeg->variant->num_formats,
363 f->fmt.pix_mp.pixelformat,
364 MTK_JPEG_FMT_FLAG_CAPTURE);
366 fmt = ctx->cap_q.fmt;
368 v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
370 (fmt->fourcc & 0xff),
371 (fmt->fourcc >> 8 & 0xff),
372 (fmt->fourcc >> 16 & 0xff),
373 (fmt->fourcc >> 24 & 0xff));
375 if (ctx->state != MTK_JPEG_INIT) {
376 mtk_jpeg_g_fmt_vid_mplane(file, priv, f);
380 return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt);
383 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
384 struct v4l2_format *f)
386 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
387 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
388 struct mtk_jpeg_fmt *fmt;
390 fmt = mtk_jpeg_find_format(jpeg->variant->formats,
391 jpeg->variant->num_formats,
392 f->fmt.pix_mp.pixelformat,
393 MTK_JPEG_FMT_FLAG_OUTPUT);
395 fmt = ctx->out_q.fmt;
397 v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
399 (fmt->fourcc & 0xff),
400 (fmt->fourcc >> 8 & 0xff),
401 (fmt->fourcc >> 16 & 0xff),
402 (fmt->fourcc >> 24 & 0xff));
404 if (ctx->state != MTK_JPEG_INIT) {
405 mtk_jpeg_g_fmt_vid_mplane(file, priv, f);
409 return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt);
412 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
413 struct v4l2_format *f, unsigned int fmt_type)
415 struct vb2_queue *vq;
416 struct mtk_jpeg_q_data *q_data = NULL;
417 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
418 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
421 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
425 q_data = mtk_jpeg_get_q_data(ctx, f->type);
427 if (vb2_is_busy(vq)) {
428 v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
432 q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
433 jpeg->variant->num_formats,
434 pix_mp->pixelformat, fmt_type);
435 q_data->pix_mp.width = pix_mp->width;
436 q_data->pix_mp.height = pix_mp->height;
437 q_data->enc_crop_rect.width = pix_mp->width;
438 q_data->enc_crop_rect.height = pix_mp->height;
439 q_data->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
440 q_data->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
441 q_data->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
442 q_data->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
444 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n",
446 (q_data->fmt->fourcc & 0xff),
447 (q_data->fmt->fourcc >> 8 & 0xff),
448 (q_data->fmt->fourcc >> 16 & 0xff),
449 (q_data->fmt->fourcc >> 24 & 0xff),
450 q_data->pix_mp.width, q_data->pix_mp.height);
452 for (i = 0; i < q_data->fmt->colplanes; i++) {
453 q_data->pix_mp.plane_fmt[i].bytesperline =
454 pix_mp->plane_fmt[i].bytesperline;
455 q_data->pix_mp.plane_fmt[i].sizeimage =
456 pix_mp->plane_fmt[i].sizeimage;
458 v4l2_dbg(1, debug, &jpeg->v4l2_dev,
459 "plane[%d] bpl=%u, size=%u\n",
460 i, q_data->pix_mp.plane_fmt[i].bytesperline,
461 q_data->pix_mp.plane_fmt[i].sizeimage);
467 static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv,
468 struct v4l2_format *f)
472 ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f);
476 return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f,
477 MTK_JPEG_FMT_FLAG_OUTPUT);
480 static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv,
481 struct v4l2_format *f)
485 ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f);
489 return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f,
490 MTK_JPEG_FMT_FLAG_CAPTURE);
493 static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx)
495 static const struct v4l2_event ev_src_ch = {
496 .type = V4L2_EVENT_SOURCE_CHANGE,
497 .u.src_change.changes =
498 V4L2_EVENT_SRC_CH_RESOLUTION,
501 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
504 static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
505 const struct v4l2_event_subscription *sub)
508 case V4L2_EVENT_SOURCE_CHANGE:
509 return v4l2_src_change_event_subscribe(fh, sub);
512 return v4l2_ctrl_subscribe_event(fh, sub);
515 static int mtk_jpeg_enc_g_selection(struct file *file, void *priv,
516 struct v4l2_selection *s)
518 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
520 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
524 case V4L2_SEL_TGT_CROP:
525 s->r = ctx->out_q.enc_crop_rect;
527 case V4L2_SEL_TGT_CROP_BOUNDS:
528 case V4L2_SEL_TGT_CROP_DEFAULT:
529 s->r.width = ctx->out_q.pix_mp.width;
530 s->r.height = ctx->out_q.pix_mp.height;
540 static int mtk_jpeg_dec_g_selection(struct file *file, void *priv,
541 struct v4l2_selection *s)
543 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
545 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
549 case V4L2_SEL_TGT_COMPOSE:
550 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
551 s->r.width = ctx->out_q.pix_mp.width;
552 s->r.height = ctx->out_q.pix_mp.height;
556 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
557 case V4L2_SEL_TGT_COMPOSE_PADDED:
558 s->r.width = ctx->cap_q.pix_mp.width;
559 s->r.height = ctx->cap_q.pix_mp.height;
569 static int mtk_jpeg_enc_s_selection(struct file *file, void *priv,
570 struct v4l2_selection *s)
572 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
574 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
578 case V4L2_SEL_TGT_CROP:
581 s->r.width = min(s->r.width, ctx->out_q.pix_mp.width);
582 s->r.height = min(s->r.height, ctx->out_q.pix_mp.height);
583 ctx->out_q.enc_crop_rect = s->r;
592 static const struct v4l2_ioctl_ops mtk_jpeg_enc_ioctl_ops = {
593 .vidioc_querycap = mtk_jpeg_querycap,
594 .vidioc_enum_fmt_vid_cap = mtk_jpeg_enum_fmt_vid_cap,
595 .vidioc_enum_fmt_vid_out = mtk_jpeg_enum_fmt_vid_out,
596 .vidioc_try_fmt_vid_cap_mplane = mtk_jpeg_try_fmt_vid_cap_mplane,
597 .vidioc_try_fmt_vid_out_mplane = mtk_jpeg_try_fmt_vid_out_mplane,
598 .vidioc_g_fmt_vid_cap_mplane = mtk_jpeg_g_fmt_vid_mplane,
599 .vidioc_g_fmt_vid_out_mplane = mtk_jpeg_g_fmt_vid_mplane,
600 .vidioc_s_fmt_vid_cap_mplane = mtk_jpeg_s_fmt_vid_cap_mplane,
601 .vidioc_s_fmt_vid_out_mplane = mtk_jpeg_s_fmt_vid_out_mplane,
602 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
603 .vidioc_subscribe_event = mtk_jpeg_subscribe_event,
604 .vidioc_g_selection = mtk_jpeg_enc_g_selection,
605 .vidioc_s_selection = mtk_jpeg_enc_s_selection,
607 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
608 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
609 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
610 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
611 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
612 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
613 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
614 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
616 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
619 static const struct v4l2_ioctl_ops mtk_jpeg_dec_ioctl_ops = {
620 .vidioc_querycap = mtk_jpeg_querycap,
621 .vidioc_enum_fmt_vid_cap = mtk_jpeg_enum_fmt_vid_cap,
622 .vidioc_enum_fmt_vid_out = mtk_jpeg_enum_fmt_vid_out,
623 .vidioc_try_fmt_vid_cap_mplane = mtk_jpeg_try_fmt_vid_cap_mplane,
624 .vidioc_try_fmt_vid_out_mplane = mtk_jpeg_try_fmt_vid_out_mplane,
625 .vidioc_g_fmt_vid_cap_mplane = mtk_jpeg_g_fmt_vid_mplane,
626 .vidioc_g_fmt_vid_out_mplane = mtk_jpeg_g_fmt_vid_mplane,
627 .vidioc_s_fmt_vid_cap_mplane = mtk_jpeg_s_fmt_vid_cap_mplane,
628 .vidioc_s_fmt_vid_out_mplane = mtk_jpeg_s_fmt_vid_out_mplane,
629 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
630 .vidioc_subscribe_event = mtk_jpeg_subscribe_event,
631 .vidioc_g_selection = mtk_jpeg_dec_g_selection,
633 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
634 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
635 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
636 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
637 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
638 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
639 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
640 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
642 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
645 static int mtk_jpeg_queue_setup(struct vb2_queue *q,
646 unsigned int *num_buffers,
647 unsigned int *num_planes,
648 unsigned int sizes[],
649 struct device *alloc_ctxs[])
651 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
652 struct mtk_jpeg_q_data *q_data = NULL;
653 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
656 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
657 q->type, *num_buffers);
659 q_data = mtk_jpeg_get_q_data(ctx, q->type);
664 for (i = 0; i < *num_planes; i++)
665 if (sizes[i] < q_data->pix_mp.plane_fmt[i].sizeimage)
670 *num_planes = q_data->fmt->colplanes;
671 for (i = 0; i < q_data->fmt->colplanes; i++) {
672 sizes[i] = q_data->pix_mp.plane_fmt[i].sizeimage;
673 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n",
680 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
682 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
683 struct mtk_jpeg_q_data *q_data = NULL;
684 struct v4l2_plane_pix_format plane_fmt = {};
687 q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
691 for (i = 0; i < q_data->fmt->colplanes; i++) {
692 plane_fmt = q_data->pix_mp.plane_fmt[i];
693 if (ctx->enable_exif &&
694 q_data->fmt->fourcc == V4L2_PIX_FMT_JPEG)
695 vb2_set_plane_payload(vb, i, plane_fmt.sizeimage +
696 MTK_JPEG_MAX_EXIF_SIZE);
698 vb2_set_plane_payload(vb, i, plane_fmt.sizeimage);
704 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
705 struct mtk_jpeg_dec_param *param)
707 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
708 struct mtk_jpeg_q_data *q_data;
710 q_data = &ctx->out_q;
711 if (q_data->pix_mp.width != param->pic_w ||
712 q_data->pix_mp.height != param->pic_h) {
713 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n");
717 q_data = &ctx->cap_q;
719 mtk_jpeg_find_format(jpeg->variant->formats,
720 jpeg->variant->num_formats, param->dst_fourcc,
721 MTK_JPEG_FMT_FLAG_CAPTURE)) {
722 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n");
728 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
729 struct mtk_jpeg_dec_param *param)
731 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
732 struct mtk_jpeg_q_data *q_data;
735 q_data = &ctx->out_q;
736 q_data->pix_mp.width = param->pic_w;
737 q_data->pix_mp.height = param->pic_h;
739 q_data = &ctx->cap_q;
740 q_data->pix_mp.width = param->dec_w;
741 q_data->pix_mp.height = param->dec_h;
742 q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
743 jpeg->variant->num_formats,
745 MTK_JPEG_FMT_FLAG_CAPTURE);
747 for (i = 0; i < q_data->fmt->colplanes; i++) {
748 q_data->pix_mp.plane_fmt[i].bytesperline = param->mem_stride[i];
749 q_data->pix_mp.plane_fmt[i].sizeimage = param->comp_size[i];
752 v4l2_dbg(1, debug, &jpeg->v4l2_dev,
753 "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n",
754 (param->dst_fourcc & 0xff),
755 (param->dst_fourcc >> 8 & 0xff),
756 (param->dst_fourcc >> 16 & 0xff),
757 (param->dst_fourcc >> 24 & 0xff),
758 param->pic_w, param->pic_h,
759 param->dec_w, param->dec_h);
762 static void mtk_jpeg_enc_buf_queue(struct vb2_buffer *vb)
764 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
765 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
767 v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
768 vb->vb2_queue->type, vb->index, vb);
770 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
773 static void mtk_jpeg_dec_buf_queue(struct vb2_buffer *vb)
775 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
776 struct mtk_jpeg_dec_param *param;
777 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
778 struct mtk_jpeg_src_buf *jpeg_src_buf;
781 v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
782 vb->vb2_queue->type, vb->index, vb);
784 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
787 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
788 param = &jpeg_src_buf->dec_param;
789 memset(param, 0, sizeof(*param));
791 header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
792 vb2_get_plane_payload(vb, 0));
794 v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
795 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
799 if (ctx->state == MTK_JPEG_INIT) {
800 struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
801 ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
803 mtk_jpeg_queue_src_chg_event(ctx);
804 mtk_jpeg_set_queue_data(ctx, param);
805 ctx->state = vb2_is_streaming(dst_vq) ?
806 MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
809 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
812 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
813 enum v4l2_buf_type type)
815 if (V4L2_TYPE_IS_OUTPUT(type))
816 return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
818 return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
821 static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q)
823 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
824 struct vb2_v4l2_buffer *vb;
826 while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
827 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
830 static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q)
832 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
833 struct vb2_v4l2_buffer *vb;
836 * STREAMOFF is an acknowledgment for source change event.
837 * Before STREAMOFF, we still have to return the old resolution and
838 * subsampling. Update capture queue when the stream is off.
840 if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
841 V4L2_TYPE_IS_CAPTURE(q->type)) {
842 struct mtk_jpeg_src_buf *src_buf;
844 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
845 src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf);
846 mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
847 ctx->state = MTK_JPEG_RUNNING;
848 } else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
849 ctx->state = MTK_JPEG_INIT;
852 while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
853 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
856 static const struct vb2_ops mtk_jpeg_dec_qops = {
857 .queue_setup = mtk_jpeg_queue_setup,
858 .buf_prepare = mtk_jpeg_buf_prepare,
859 .buf_queue = mtk_jpeg_dec_buf_queue,
860 .wait_prepare = vb2_ops_wait_prepare,
861 .wait_finish = vb2_ops_wait_finish,
862 .stop_streaming = mtk_jpeg_dec_stop_streaming,
865 static const struct vb2_ops mtk_jpeg_enc_qops = {
866 .queue_setup = mtk_jpeg_queue_setup,
867 .buf_prepare = mtk_jpeg_buf_prepare,
868 .buf_queue = mtk_jpeg_enc_buf_queue,
869 .wait_prepare = vb2_ops_wait_prepare,
870 .wait_finish = vb2_ops_wait_finish,
871 .stop_streaming = mtk_jpeg_enc_stop_streaming,
874 static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
875 struct vb2_buffer *src_buf,
876 struct mtk_jpeg_bs *bs)
878 bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
879 bs->end_addr = bs->str_addr +
880 round_up(vb2_get_plane_payload(src_buf, 0), 16);
881 bs->size = round_up(vb2_plane_size(src_buf, 0), 128);
884 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
885 struct mtk_jpeg_dec_param *param,
886 struct vb2_buffer *dst_buf,
887 struct mtk_jpeg_fb *fb)
891 if (param->comp_num != dst_buf->num_planes) {
892 dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n",
893 param->comp_num, dst_buf->num_planes);
897 for (i = 0; i < dst_buf->num_planes; i++) {
898 if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) {
899 dev_err(ctx->jpeg->dev,
900 "buffer size is underflow (%lu < %u)\n",
901 vb2_plane_size(dst_buf, 0),
902 param->comp_size[i]);
905 fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
911 static void mtk_jpeg_enc_device_run(void *priv)
913 struct mtk_jpeg_ctx *ctx = priv;
914 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
915 struct vb2_v4l2_buffer *src_buf, *dst_buf;
916 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
920 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
921 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
923 ret = pm_runtime_get_sync(jpeg->dev);
927 schedule_delayed_work(&jpeg->job_timeout_work,
928 msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
930 spin_lock_irqsave(&jpeg->hw_lock, flags);
933 * Resetting the hardware every frame is to ensure that all the
934 * registers are cleared. This is a hardware requirement.
936 mtk_jpeg_enc_reset(jpeg->reg_base);
938 mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf);
939 mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf);
940 mtk_jpeg_set_enc_params(ctx, jpeg->reg_base);
941 mtk_jpeg_enc_start(jpeg->reg_base);
942 spin_unlock_irqrestore(&jpeg->hw_lock, flags);
946 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
947 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
948 v4l2_m2m_buf_done(src_buf, buf_state);
949 v4l2_m2m_buf_done(dst_buf, buf_state);
950 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
953 static void mtk_jpeg_dec_device_run(void *priv)
955 struct mtk_jpeg_ctx *ctx = priv;
956 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
957 struct vb2_v4l2_buffer *src_buf, *dst_buf;
958 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
960 struct mtk_jpeg_src_buf *jpeg_src_buf;
961 struct mtk_jpeg_bs bs;
962 struct mtk_jpeg_fb fb;
965 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
966 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
967 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
969 if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
970 mtk_jpeg_queue_src_chg_event(ctx);
971 ctx->state = MTK_JPEG_SOURCE_CHANGE;
972 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
976 ret = pm_runtime_get_sync(jpeg->dev);
980 schedule_delayed_work(&jpeg->job_timeout_work,
981 msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
983 mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs);
984 if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb))
987 spin_lock_irqsave(&jpeg->hw_lock, flags);
988 mtk_jpeg_dec_reset(jpeg->reg_base);
989 mtk_jpeg_dec_set_config(jpeg->reg_base,
990 &jpeg_src_buf->dec_param, &bs, &fb);
992 mtk_jpeg_dec_start(jpeg->reg_base);
993 spin_unlock_irqrestore(&jpeg->hw_lock, flags);
997 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
998 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
999 v4l2_m2m_buf_done(src_buf, buf_state);
1000 v4l2_m2m_buf_done(dst_buf, buf_state);
1001 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1004 static int mtk_jpeg_dec_job_ready(void *priv)
1006 struct mtk_jpeg_ctx *ctx = priv;
1008 return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
1011 static const struct v4l2_m2m_ops mtk_jpeg_enc_m2m_ops = {
1012 .device_run = mtk_jpeg_enc_device_run,
1015 static const struct v4l2_m2m_ops mtk_jpeg_dec_m2m_ops = {
1016 .device_run = mtk_jpeg_dec_device_run,
1017 .job_ready = mtk_jpeg_dec_job_ready,
1020 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
1021 struct vb2_queue *dst_vq)
1023 struct mtk_jpeg_ctx *ctx = priv;
1024 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1027 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1028 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1029 src_vq->drv_priv = ctx;
1030 src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf);
1031 src_vq->ops = jpeg->variant->qops;
1032 src_vq->mem_ops = &vb2_dma_contig_memops;
1033 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1034 src_vq->lock = &ctx->jpeg->lock;
1035 src_vq->dev = ctx->jpeg->dev;
1036 ret = vb2_queue_init(src_vq);
1040 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1041 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1042 dst_vq->drv_priv = ctx;
1043 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1044 dst_vq->ops = jpeg->variant->qops;
1045 dst_vq->mem_ops = &vb2_dma_contig_memops;
1046 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1047 dst_vq->lock = &ctx->jpeg->lock;
1048 dst_vq->dev = ctx->jpeg->dev;
1049 ret = vb2_queue_init(dst_vq);
1054 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
1058 ret = mtk_smi_larb_get(jpeg->larb);
1060 dev_err(jpeg->dev, "mtk_smi_larb_get larbvdec fail %d\n", ret);
1062 ret = clk_bulk_prepare_enable(jpeg->variant->num_clks,
1063 jpeg->variant->clks);
1065 dev_err(jpeg->dev, "Failed to open jpeg clk: %d\n", ret);
1068 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
1070 clk_bulk_disable_unprepare(jpeg->variant->num_clks,
1071 jpeg->variant->clks);
1072 mtk_smi_larb_put(jpeg->larb);
1075 static irqreturn_t mtk_jpeg_enc_done(struct mtk_jpeg_dev *jpeg)
1077 struct mtk_jpeg_ctx *ctx;
1078 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1079 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1082 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1084 v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1088 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1089 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1091 result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
1092 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, result_size);
1094 buf_state = VB2_BUF_STATE_DONE;
1096 v4l2_m2m_buf_done(src_buf, buf_state);
1097 v4l2_m2m_buf_done(dst_buf, buf_state);
1098 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1099 pm_runtime_put(ctx->jpeg->dev);
1103 static irqreturn_t mtk_jpeg_enc_irq(int irq, void *priv)
1105 struct mtk_jpeg_dev *jpeg = priv;
1107 irqreturn_t ret = IRQ_NONE;
1109 cancel_delayed_work(&jpeg->job_timeout_work);
1111 irq_status = readl(jpeg->reg_base + JPEG_ENC_INT_STS) &
1112 JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
1114 writel(0, jpeg->reg_base + JPEG_ENC_INT_STS);
1116 if (!(irq_status & JPEG_ENC_INT_STATUS_DONE))
1119 ret = mtk_jpeg_enc_done(jpeg);
1123 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
1125 struct mtk_jpeg_dev *jpeg = priv;
1126 struct mtk_jpeg_ctx *ctx;
1127 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1128 struct mtk_jpeg_src_buf *jpeg_src_buf;
1129 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
1134 cancel_delayed_work(&jpeg->job_timeout_work);
1136 dec_ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base);
1137 dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
1138 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1140 v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
1144 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1145 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1146 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf);
1148 if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
1149 mtk_jpeg_dec_reset(jpeg->reg_base);
1151 if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
1152 dev_err(jpeg->dev, "decode failed\n");
1156 for (i = 0; i < dst_buf->vb2_buf.num_planes; i++)
1157 vb2_set_plane_payload(&dst_buf->vb2_buf, i,
1158 jpeg_src_buf->dec_param.comp_size[i]);
1160 buf_state = VB2_BUF_STATE_DONE;
1163 v4l2_m2m_buf_done(src_buf, buf_state);
1164 v4l2_m2m_buf_done(dst_buf, buf_state);
1165 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1166 pm_runtime_put(ctx->jpeg->dev);
1170 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
1172 struct mtk_jpeg_q_data *q = &ctx->out_q;
1173 struct mtk_jpeg_dev *jpeg = ctx->jpeg;
1175 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1176 q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1177 q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1178 q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1179 q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1181 q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1182 jpeg->variant->num_formats,
1183 jpeg->variant->out_q_default_fourcc,
1184 MTK_JPEG_FMT_FLAG_OUTPUT);
1185 q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1186 q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1187 mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1190 q->fmt = mtk_jpeg_find_format(jpeg->variant->formats,
1191 jpeg->variant->num_formats,
1192 jpeg->variant->cap_q_default_fourcc,
1193 MTK_JPEG_FMT_FLAG_CAPTURE);
1194 q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
1195 q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601;
1196 q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE;
1197 q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB;
1198 q->pix_mp.width = MTK_JPEG_MIN_WIDTH;
1199 q->pix_mp.height = MTK_JPEG_MIN_HEIGHT;
1201 mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt);
1204 static int mtk_jpeg_open(struct file *file)
1206 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1207 struct video_device *vfd = video_devdata(file);
1208 struct mtk_jpeg_ctx *ctx;
1211 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1215 if (mutex_lock_interruptible(&jpeg->lock)) {
1220 v4l2_fh_init(&ctx->fh, vfd);
1221 file->private_data = &ctx->fh;
1222 v4l2_fh_add(&ctx->fh);
1225 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx,
1226 mtk_jpeg_queue_init);
1227 if (IS_ERR(ctx->fh.m2m_ctx)) {
1228 ret = PTR_ERR(ctx->fh.m2m_ctx);
1232 if (jpeg->variant->cap_q_default_fourcc == V4L2_PIX_FMT_JPEG) {
1233 ret = mtk_jpeg_enc_ctrls_setup(ctx);
1235 v4l2_err(&jpeg->v4l2_dev, "Failed to setup jpeg enc controls\n");
1239 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 0);
1241 mtk_jpeg_set_default_params(ctx);
1242 mutex_unlock(&jpeg->lock);
1246 v4l2_fh_del(&ctx->fh);
1247 v4l2_fh_exit(&ctx->fh);
1248 mutex_unlock(&jpeg->lock);
1254 static int mtk_jpeg_release(struct file *file)
1256 struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1257 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
1259 mutex_lock(&jpeg->lock);
1260 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1261 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1262 v4l2_fh_del(&ctx->fh);
1263 v4l2_fh_exit(&ctx->fh);
1265 mutex_unlock(&jpeg->lock);
1269 static const struct v4l2_file_operations mtk_jpeg_fops = {
1270 .owner = THIS_MODULE,
1271 .open = mtk_jpeg_open,
1272 .release = mtk_jpeg_release,
1273 .poll = v4l2_m2m_fop_poll,
1274 .unlocked_ioctl = video_ioctl2,
1275 .mmap = v4l2_m2m_fop_mmap,
1278 static struct clk_bulk_data mt8173_jpeg_dec_clocks[] = {
1279 { .id = "jpgdec-smi" },
1283 static struct clk_bulk_data mtk_jpeg_clocks[] = {
1287 static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg)
1289 struct device_node *node;
1290 struct platform_device *pdev;
1293 node = of_parse_phandle(jpeg->dev->of_node, "mediatek,larb", 0);
1296 pdev = of_find_device_by_node(node);
1297 if (WARN_ON(!pdev)) {
1303 jpeg->larb = &pdev->dev;
1305 ret = devm_clk_bulk_get(jpeg->dev, jpeg->variant->num_clks,
1306 jpeg->variant->clks);
1308 dev_err(&pdev->dev, "failed to get jpeg clock:%d\n", ret);
1309 put_device(&pdev->dev);
1316 static void mtk_jpeg_job_timeout_work(struct work_struct *work)
1318 struct mtk_jpeg_dev *jpeg = container_of(work, struct mtk_jpeg_dev,
1319 job_timeout_work.work);
1320 struct mtk_jpeg_ctx *ctx;
1321 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1323 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
1324 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1325 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1327 jpeg->variant->hw_reset(jpeg->reg_base);
1329 pm_runtime_put(jpeg->dev);
1331 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1332 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1333 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
1336 static inline void mtk_jpeg_clk_release(struct mtk_jpeg_dev *jpeg)
1338 put_device(jpeg->larb);
1341 static int mtk_jpeg_probe(struct platform_device *pdev)
1343 struct mtk_jpeg_dev *jpeg;
1344 struct resource *res;
1348 jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1352 mutex_init(&jpeg->lock);
1353 spin_lock_init(&jpeg->hw_lock);
1354 jpeg->dev = &pdev->dev;
1355 jpeg->variant = of_device_get_match_data(jpeg->dev);
1356 INIT_DELAYED_WORK(&jpeg->job_timeout_work, mtk_jpeg_job_timeout_work);
1358 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1359 jpeg->reg_base = devm_ioremap_resource(&pdev->dev, res);
1360 if (IS_ERR(jpeg->reg_base)) {
1361 ret = PTR_ERR(jpeg->reg_base);
1365 jpeg_irq = platform_get_irq(pdev, 0);
1367 dev_err(&pdev->dev, "Failed to get jpeg_irq %d.\n", jpeg_irq);
1371 ret = devm_request_irq(&pdev->dev, jpeg_irq,
1372 jpeg->variant->irq_handler, 0, pdev->name, jpeg);
1374 dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n",
1379 ret = mtk_jpeg_clk_init(jpeg);
1381 dev_err(&pdev->dev, "Failed to init clk, err %d\n", ret);
1385 ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1387 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1389 goto err_dev_register;
1392 jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops);
1394 if (IS_ERR(jpeg->m2m_dev)) {
1395 v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1396 ret = PTR_ERR(jpeg->m2m_dev);
1400 jpeg->vdev = video_device_alloc();
1403 goto err_vfd_jpeg_alloc;
1405 snprintf(jpeg->vdev->name, sizeof(jpeg->vdev->name),
1406 "%s", jpeg->variant->dev_name);
1407 jpeg->vdev->fops = &mtk_jpeg_fops;
1408 jpeg->vdev->ioctl_ops = jpeg->variant->ioctl_ops;
1409 jpeg->vdev->minor = -1;
1410 jpeg->vdev->release = video_device_release;
1411 jpeg->vdev->lock = &jpeg->lock;
1412 jpeg->vdev->v4l2_dev = &jpeg->v4l2_dev;
1413 jpeg->vdev->vfl_dir = VFL_DIR_M2M;
1414 jpeg->vdev->device_caps = V4L2_CAP_STREAMING |
1415 V4L2_CAP_VIDEO_M2M_MPLANE;
1417 ret = video_register_device(jpeg->vdev, VFL_TYPE_VIDEO, -1);
1419 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1420 goto err_vfd_jpeg_register;
1423 video_set_drvdata(jpeg->vdev, jpeg);
1424 v4l2_info(&jpeg->v4l2_dev,
1425 "%s device registered as /dev/video%d (%d,%d)\n",
1426 jpeg->variant->dev_name, jpeg->vdev->num,
1427 VIDEO_MAJOR, jpeg->vdev->minor);
1429 platform_set_drvdata(pdev, jpeg);
1431 pm_runtime_enable(&pdev->dev);
1435 err_vfd_jpeg_register:
1436 video_device_release(jpeg->vdev);
1439 v4l2_m2m_release(jpeg->m2m_dev);
1442 v4l2_device_unregister(&jpeg->v4l2_dev);
1445 mtk_jpeg_clk_release(jpeg);
1454 static int mtk_jpeg_remove(struct platform_device *pdev)
1456 struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
1458 pm_runtime_disable(&pdev->dev);
1459 video_unregister_device(jpeg->vdev);
1460 video_device_release(jpeg->vdev);
1461 v4l2_m2m_release(jpeg->m2m_dev);
1462 v4l2_device_unregister(&jpeg->v4l2_dev);
1463 mtk_jpeg_clk_release(jpeg);
1468 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1470 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1472 mtk_jpeg_clk_off(jpeg);
1477 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1479 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1481 mtk_jpeg_clk_on(jpeg);
1486 static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1488 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1490 v4l2_m2m_suspend(jpeg->m2m_dev);
1491 return pm_runtime_force_suspend(dev);
1494 static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1496 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1499 ret = pm_runtime_force_resume(dev);
1503 v4l2_m2m_resume(jpeg->m2m_dev);
1507 static const struct dev_pm_ops mtk_jpeg_pm_ops = {
1508 SET_SYSTEM_SLEEP_PM_OPS(mtk_jpeg_suspend, mtk_jpeg_resume)
1509 SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL)
1512 static const struct mtk_jpeg_variant mt8173_jpeg_drvdata = {
1513 .clks = mt8173_jpeg_dec_clocks,
1514 .num_clks = ARRAY_SIZE(mt8173_jpeg_dec_clocks),
1515 .formats = mtk_jpeg_dec_formats,
1516 .num_formats = MTK_JPEG_DEC_NUM_FORMATS,
1517 .qops = &mtk_jpeg_dec_qops,
1518 .irq_handler = mtk_jpeg_dec_irq,
1519 .hw_reset = mtk_jpeg_dec_reset,
1520 .m2m_ops = &mtk_jpeg_dec_m2m_ops,
1521 .dev_name = "mtk-jpeg-dec",
1522 .ioctl_ops = &mtk_jpeg_dec_ioctl_ops,
1523 .out_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1524 .cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M,
1527 static const struct mtk_jpeg_variant mtk_jpeg_drvdata = {
1528 .clks = mtk_jpeg_clocks,
1529 .num_clks = ARRAY_SIZE(mtk_jpeg_clocks),
1530 .formats = mtk_jpeg_enc_formats,
1531 .num_formats = MTK_JPEG_ENC_NUM_FORMATS,
1532 .qops = &mtk_jpeg_enc_qops,
1533 .irq_handler = mtk_jpeg_enc_irq,
1534 .hw_reset = mtk_jpeg_enc_reset,
1535 .m2m_ops = &mtk_jpeg_enc_m2m_ops,
1536 .dev_name = "mtk-jpeg-enc",
1537 .ioctl_ops = &mtk_jpeg_enc_ioctl_ops,
1538 .out_q_default_fourcc = V4L2_PIX_FMT_YUYV,
1539 .cap_q_default_fourcc = V4L2_PIX_FMT_JPEG,
1542 static const struct of_device_id mtk_jpeg_match[] = {
1544 .compatible = "mediatek,mt8173-jpgdec",
1545 .data = &mt8173_jpeg_drvdata,
1548 .compatible = "mediatek,mt2701-jpgdec",
1549 .data = &mt8173_jpeg_drvdata,
1552 .compatible = "mediatek,mtk-jpgenc",
1553 .data = &mtk_jpeg_drvdata,
1558 MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1560 static struct platform_driver mtk_jpeg_driver = {
1561 .probe = mtk_jpeg_probe,
1562 .remove = mtk_jpeg_remove,
1564 .name = MTK_JPEG_NAME,
1565 .of_match_table = mtk_jpeg_match,
1566 .pm = &mtk_jpeg_pm_ops,
1570 module_platform_driver(mtk_jpeg_driver);
1572 MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1573 MODULE_LICENSE("GPL v2");