1 // SPDX-License-Identifier: GPL-2.0
3 * Rockchip Video Decoder driver
5 * Copyright (C) 2019 Collabora, Ltd.
7 * Based on rkvdec driver by Google LLC. (Tomasz Figa <tfiga@chromium.org>)
8 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
9 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/videodev2.h>
21 #include <linux/workqueue.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/videobuf2-core.h>
25 #include <media/videobuf2-vmalloc.h>
28 #include "rkvdec-regs.h"
30 static int rkvdec_try_ctrl(struct v4l2_ctrl *ctrl)
32 if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_SPS) {
33 const struct v4l2_ctrl_h264_sps *sps = ctrl->p_new.p_h264_sps;
35 * TODO: The hardware supports 10-bit and 4:2:2 profiles,
36 * but it's currently broken in the driver.
37 * Reject them for now, until it's fixed.
39 if (sps->chroma_format_idc > 1)
40 /* Only 4:0:0 and 4:2:0 are supported */
42 if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8)
43 /* Luma and chroma bit depth mismatch */
45 if (sps->bit_depth_luma_minus8 != 0)
46 /* Only 8-bit is supported */
52 static const struct v4l2_ctrl_ops rkvdec_ctrl_ops = {
53 .try_ctrl = rkvdec_try_ctrl,
56 static const struct rkvdec_ctrl_desc rkvdec_h264_ctrl_descs[] = {
59 .cfg.id = V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAMS,
63 .cfg.id = V4L2_CID_MPEG_VIDEO_H264_SPS,
64 .cfg.ops = &rkvdec_ctrl_ops,
68 .cfg.id = V4L2_CID_MPEG_VIDEO_H264_PPS,
71 .cfg.id = V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX,
74 .cfg.id = V4L2_CID_MPEG_VIDEO_H264_DECODE_MODE,
75 .cfg.min = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
76 .cfg.max = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
77 .cfg.def = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
80 .cfg.id = V4L2_CID_MPEG_VIDEO_H264_START_CODE,
81 .cfg.min = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
82 .cfg.def = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
83 .cfg.max = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
87 static const struct rkvdec_ctrls rkvdec_h264_ctrls = {
88 .ctrls = rkvdec_h264_ctrl_descs,
89 .num_ctrls = ARRAY_SIZE(rkvdec_h264_ctrl_descs),
92 static const u32 rkvdec_h264_decoded_fmts[] = {
96 static const struct rkvdec_coded_fmt_desc rkvdec_coded_fmts[] = {
98 .fourcc = V4L2_PIX_FMT_H264_SLICE,
107 .ctrls = &rkvdec_h264_ctrls,
108 .ops = &rkvdec_h264_fmt_ops,
109 .num_decoded_fmts = ARRAY_SIZE(rkvdec_h264_decoded_fmts),
110 .decoded_fmts = rkvdec_h264_decoded_fmts,
114 static const struct rkvdec_coded_fmt_desc *
115 rkvdec_find_coded_fmt_desc(u32 fourcc)
119 for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++) {
120 if (rkvdec_coded_fmts[i].fourcc == fourcc)
121 return &rkvdec_coded_fmts[i];
127 static void rkvdec_reset_fmt(struct rkvdec_ctx *ctx, struct v4l2_format *f,
130 memset(f, 0, sizeof(*f));
131 f->fmt.pix_mp.pixelformat = fourcc;
132 f->fmt.pix_mp.field = V4L2_FIELD_NONE;
133 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709,
134 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
135 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
136 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
139 static void rkvdec_reset_coded_fmt(struct rkvdec_ctx *ctx)
141 struct v4l2_format *f = &ctx->coded_fmt;
143 ctx->coded_fmt_desc = &rkvdec_coded_fmts[0];
144 rkvdec_reset_fmt(ctx, f, ctx->coded_fmt_desc->fourcc);
146 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
147 f->fmt.pix_mp.width = ctx->coded_fmt_desc->frmsize.min_width;
148 f->fmt.pix_mp.height = ctx->coded_fmt_desc->frmsize.min_height;
150 if (ctx->coded_fmt_desc->ops->adjust_fmt)
151 ctx->coded_fmt_desc->ops->adjust_fmt(ctx, f);
154 static void rkvdec_reset_decoded_fmt(struct rkvdec_ctx *ctx)
156 struct v4l2_format *f = &ctx->decoded_fmt;
158 rkvdec_reset_fmt(ctx, f, ctx->coded_fmt_desc->decoded_fmts[0]);
159 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
160 v4l2_fill_pixfmt_mp(&f->fmt.pix_mp,
161 ctx->coded_fmt_desc->decoded_fmts[0],
162 ctx->coded_fmt.fmt.pix_mp.width,
163 ctx->coded_fmt.fmt.pix_mp.height);
164 f->fmt.pix_mp.plane_fmt[0].sizeimage += 128 *
165 DIV_ROUND_UP(f->fmt.pix_mp.width, 16) *
166 DIV_ROUND_UP(f->fmt.pix_mp.height, 16);
169 static int rkvdec_enum_framesizes(struct file *file, void *priv,
170 struct v4l2_frmsizeenum *fsize)
172 const struct rkvdec_coded_fmt_desc *fmt;
174 if (fsize->index != 0)
177 fmt = rkvdec_find_coded_fmt_desc(fsize->pixel_format);
181 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
182 fsize->stepwise = fmt->frmsize;
186 static int rkvdec_querycap(struct file *file, void *priv,
187 struct v4l2_capability *cap)
189 struct rkvdec_dev *rkvdec = video_drvdata(file);
190 struct video_device *vdev = video_devdata(file);
192 strscpy(cap->driver, rkvdec->dev->driver->name,
193 sizeof(cap->driver));
194 strscpy(cap->card, vdev->name, sizeof(cap->card));
195 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
196 rkvdec->dev->driver->name);
200 static int rkvdec_try_capture_fmt(struct file *file, void *priv,
201 struct v4l2_format *f)
203 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
204 struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
205 const struct rkvdec_coded_fmt_desc *coded_desc;
209 * The codec context should point to a coded format desc, if the format
210 * on the coded end has not been set yet, it should point to the
213 coded_desc = ctx->coded_fmt_desc;
214 if (WARN_ON(!coded_desc))
217 for (i = 0; i < coded_desc->num_decoded_fmts; i++) {
218 if (coded_desc->decoded_fmts[i] == pix_mp->pixelformat)
222 if (i == coded_desc->num_decoded_fmts)
223 pix_mp->pixelformat = coded_desc->decoded_fmts[0];
225 /* Always apply the frmsize constraint of the coded end. */
226 v4l2_apply_frmsize_constraints(&pix_mp->width,
228 &coded_desc->frmsize);
230 v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat,
231 pix_mp->width, pix_mp->height);
232 pix_mp->plane_fmt[0].sizeimage +=
234 DIV_ROUND_UP(pix_mp->width, 16) *
235 DIV_ROUND_UP(pix_mp->height, 16);
236 pix_mp->field = V4L2_FIELD_NONE;
241 static int rkvdec_try_output_fmt(struct file *file, void *priv,
242 struct v4l2_format *f)
244 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
245 struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
246 const struct rkvdec_coded_fmt_desc *desc;
248 desc = rkvdec_find_coded_fmt_desc(pix_mp->pixelformat);
250 pix_mp->pixelformat = rkvdec_coded_fmts[0].fourcc;
251 desc = &rkvdec_coded_fmts[0];
254 v4l2_apply_frmsize_constraints(&pix_mp->width,
258 pix_mp->field = V4L2_FIELD_NONE;
259 /* All coded formats are considered single planar for now. */
260 pix_mp->num_planes = 1;
262 if (desc->ops->adjust_fmt) {
265 ret = desc->ops->adjust_fmt(ctx, f);
273 static int rkvdec_s_capture_fmt(struct file *file, void *priv,
274 struct v4l2_format *f)
276 struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
277 struct vb2_queue *vq;
280 /* Change not allowed if queue is busy */
281 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
282 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
286 ret = rkvdec_try_capture_fmt(file, priv, f);
290 ctx->decoded_fmt = *f;
294 static int rkvdec_s_output_fmt(struct file *file, void *priv,
295 struct v4l2_format *f)
297 struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
298 struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
299 const struct rkvdec_coded_fmt_desc *desc;
300 struct v4l2_format *cap_fmt;
301 struct vb2_queue *peer_vq, *vq;
305 * In order to support dynamic resolution change, the decoder admits
306 * a resolution change, as long as the pixelformat remains. Can't be
309 vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
310 if (vb2_is_streaming(vq) ||
312 f->fmt.pix_mp.pixelformat != ctx->coded_fmt.fmt.pix_mp.pixelformat))
316 * Since format change on the OUTPUT queue will reset the CAPTURE
317 * queue, we can't allow doing so when the CAPTURE queue has buffers
320 peer_vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
321 if (vb2_is_busy(peer_vq))
324 ret = rkvdec_try_output_fmt(file, priv, f);
328 desc = rkvdec_find_coded_fmt_desc(f->fmt.pix_mp.pixelformat);
331 ctx->coded_fmt_desc = desc;
335 * Current decoded format might have become invalid with newly
336 * selected codec, so reset it to default just to be safe and
337 * keep internal driver state sane. User is mandated to set
338 * the decoded format again after we return, so we don't need
341 * Note that this will propagates any size changes to the decoded format.
343 rkvdec_reset_decoded_fmt(ctx);
345 /* Propagate colorspace information to capture. */
346 cap_fmt = &ctx->decoded_fmt;
347 cap_fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
348 cap_fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
349 cap_fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
350 cap_fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
355 static int rkvdec_g_output_fmt(struct file *file, void *priv,
356 struct v4l2_format *f)
358 struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
364 static int rkvdec_g_capture_fmt(struct file *file, void *priv,
365 struct v4l2_format *f)
367 struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
369 *f = ctx->decoded_fmt;
373 static int rkvdec_enum_output_fmt(struct file *file, void *priv,
374 struct v4l2_fmtdesc *f)
376 if (f->index >= ARRAY_SIZE(rkvdec_coded_fmts))
379 f->pixelformat = rkvdec_coded_fmts[f->index].fourcc;
383 static int rkvdec_enum_capture_fmt(struct file *file, void *priv,
384 struct v4l2_fmtdesc *f)
386 struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
388 if (WARN_ON(!ctx->coded_fmt_desc))
391 if (f->index >= ctx->coded_fmt_desc->num_decoded_fmts)
394 f->pixelformat = ctx->coded_fmt_desc->decoded_fmts[f->index];
398 static const struct v4l2_ioctl_ops rkvdec_ioctl_ops = {
399 .vidioc_querycap = rkvdec_querycap,
400 .vidioc_enum_framesizes = rkvdec_enum_framesizes,
402 .vidioc_try_fmt_vid_cap_mplane = rkvdec_try_capture_fmt,
403 .vidioc_try_fmt_vid_out_mplane = rkvdec_try_output_fmt,
404 .vidioc_s_fmt_vid_out_mplane = rkvdec_s_output_fmt,
405 .vidioc_s_fmt_vid_cap_mplane = rkvdec_s_capture_fmt,
406 .vidioc_g_fmt_vid_out_mplane = rkvdec_g_output_fmt,
407 .vidioc_g_fmt_vid_cap_mplane = rkvdec_g_capture_fmt,
408 .vidioc_enum_fmt_vid_out = rkvdec_enum_output_fmt,
409 .vidioc_enum_fmt_vid_cap = rkvdec_enum_capture_fmt,
411 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
412 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
413 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
414 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
415 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
416 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
417 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
419 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
420 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
422 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
423 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
426 static int rkvdec_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
427 unsigned int *num_planes, unsigned int sizes[],
428 struct device *alloc_devs[])
430 struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
431 struct v4l2_format *f;
434 if (V4L2_TYPE_IS_OUTPUT(vq->type))
437 f = &ctx->decoded_fmt;
440 if (*num_planes != f->fmt.pix_mp.num_planes)
443 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
444 if (sizes[i] < f->fmt.pix_mp.plane_fmt[i].sizeimage)
448 *num_planes = f->fmt.pix_mp.num_planes;
449 for (i = 0; i < f->fmt.pix_mp.num_planes; i++)
450 sizes[i] = f->fmt.pix_mp.plane_fmt[i].sizeimage;
456 static int rkvdec_buf_prepare(struct vb2_buffer *vb)
458 struct vb2_queue *vq = vb->vb2_queue;
459 struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
460 struct v4l2_format *f;
463 if (V4L2_TYPE_IS_OUTPUT(vq->type))
466 f = &ctx->decoded_fmt;
468 for (i = 0; i < f->fmt.pix_mp.num_planes; ++i) {
469 u32 sizeimage = f->fmt.pix_mp.plane_fmt[i].sizeimage;
471 if (vb2_plane_size(vb, i) < sizeimage)
476 * Buffer's bytesused must be written by driver for CAPTURE buffers.
477 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
478 * it to buffer length).
480 if (V4L2_TYPE_IS_CAPTURE(vq->type))
481 vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
486 static void rkvdec_buf_queue(struct vb2_buffer *vb)
488 struct rkvdec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
489 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
491 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
494 static int rkvdec_buf_out_validate(struct vb2_buffer *vb)
496 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
498 vbuf->field = V4L2_FIELD_NONE;
502 static void rkvdec_buf_request_complete(struct vb2_buffer *vb)
504 struct rkvdec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
506 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_hdl);
509 static int rkvdec_start_streaming(struct vb2_queue *q, unsigned int count)
511 struct rkvdec_ctx *ctx = vb2_get_drv_priv(q);
512 const struct rkvdec_coded_fmt_desc *desc;
515 if (V4L2_TYPE_IS_CAPTURE(q->type))
518 desc = ctx->coded_fmt_desc;
522 if (desc->ops->start) {
523 ret = desc->ops->start(ctx);
531 static void rkvdec_queue_cleanup(struct vb2_queue *vq, u32 state)
533 struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
536 struct vb2_v4l2_buffer *vbuf;
538 if (V4L2_TYPE_IS_OUTPUT(vq->type))
539 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
541 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
546 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
548 v4l2_m2m_buf_done(vbuf, state);
552 static void rkvdec_stop_streaming(struct vb2_queue *q)
554 struct rkvdec_ctx *ctx = vb2_get_drv_priv(q);
556 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
557 const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
563 desc->ops->stop(ctx);
566 rkvdec_queue_cleanup(q, VB2_BUF_STATE_ERROR);
569 static const struct vb2_ops rkvdec_queue_ops = {
570 .queue_setup = rkvdec_queue_setup,
571 .buf_prepare = rkvdec_buf_prepare,
572 .buf_queue = rkvdec_buf_queue,
573 .buf_out_validate = rkvdec_buf_out_validate,
574 .buf_request_complete = rkvdec_buf_request_complete,
575 .start_streaming = rkvdec_start_streaming,
576 .stop_streaming = rkvdec_stop_streaming,
577 .wait_prepare = vb2_ops_wait_prepare,
578 .wait_finish = vb2_ops_wait_finish,
581 static int rkvdec_request_validate(struct media_request *req)
583 struct media_request_object *obj;
584 const struct rkvdec_ctrls *ctrls;
585 struct v4l2_ctrl_handler *hdl;
586 struct rkvdec_ctx *ctx = NULL;
587 unsigned int count, i;
590 list_for_each_entry(obj, &req->objects, list) {
591 if (vb2_request_object_is_buffer(obj)) {
592 struct vb2_buffer *vb;
594 vb = container_of(obj, struct vb2_buffer, req_obj);
595 ctx = vb2_get_drv_priv(vb->vb2_queue);
603 count = vb2_request_buffer_cnt(req);
609 hdl = v4l2_ctrl_request_hdl_find(req, &ctx->ctrl_hdl);
614 ctrls = ctx->coded_fmt_desc->ctrls;
615 for (i = 0; ctrls && i < ctrls->num_ctrls; i++) {
616 u32 id = ctrls->ctrls[i].cfg.id;
617 struct v4l2_ctrl *ctrl;
619 if (!ctrls->ctrls[i].mandatory)
622 ctrl = v4l2_ctrl_request_hdl_ctrl_find(hdl, id);
629 v4l2_ctrl_request_hdl_put(hdl);
634 return vb2_request_validate(req);
637 static const struct media_device_ops rkvdec_media_ops = {
638 .req_validate = rkvdec_request_validate,
639 .req_queue = v4l2_m2m_request_queue,
642 static void rkvdec_job_finish_no_pm(struct rkvdec_ctx *ctx,
643 enum vb2_buffer_state result)
645 if (ctx->coded_fmt_desc->ops->done) {
646 struct vb2_v4l2_buffer *src_buf, *dst_buf;
648 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
649 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
650 ctx->coded_fmt_desc->ops->done(ctx, src_buf, dst_buf, result);
653 v4l2_m2m_buf_done_and_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx,
657 static void rkvdec_job_finish(struct rkvdec_ctx *ctx,
658 enum vb2_buffer_state result)
660 struct rkvdec_dev *rkvdec = ctx->dev;
662 pm_runtime_mark_last_busy(rkvdec->dev);
663 pm_runtime_put_autosuspend(rkvdec->dev);
664 rkvdec_job_finish_no_pm(ctx, result);
667 void rkvdec_run_preamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run)
669 struct media_request *src_req;
671 memset(run, 0, sizeof(*run));
673 run->bufs.src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
674 run->bufs.dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
676 /* Apply request(s) controls if needed. */
677 src_req = run->bufs.src->vb2_buf.req_obj.req;
679 v4l2_ctrl_request_setup(src_req, &ctx->ctrl_hdl);
681 v4l2_m2m_buf_copy_metadata(run->bufs.src, run->bufs.dst, true);
684 void rkvdec_run_postamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run)
686 struct media_request *src_req = run->bufs.src->vb2_buf.req_obj.req;
689 v4l2_ctrl_request_complete(src_req, &ctx->ctrl_hdl);
692 static void rkvdec_device_run(void *priv)
694 struct rkvdec_ctx *ctx = priv;
695 struct rkvdec_dev *rkvdec = ctx->dev;
696 const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
702 ret = pm_runtime_resume_and_get(rkvdec->dev);
704 rkvdec_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
708 ret = desc->ops->run(ctx);
710 rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
713 static struct v4l2_m2m_ops rkvdec_m2m_ops = {
714 .device_run = rkvdec_device_run,
717 static int rkvdec_queue_init(void *priv,
718 struct vb2_queue *src_vq,
719 struct vb2_queue *dst_vq)
721 struct rkvdec_ctx *ctx = priv;
722 struct rkvdec_dev *rkvdec = ctx->dev;
725 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
726 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
727 src_vq->drv_priv = ctx;
728 src_vq->ops = &rkvdec_queue_ops;
729 src_vq->mem_ops = &vb2_dma_contig_memops;
732 * Driver does mostly sequential access, so sacrifice TLB efficiency
733 * for faster allocation. Also, no CPU access on the source queue,
734 * so no kernel mapping needed.
736 src_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
737 DMA_ATTR_NO_KERNEL_MAPPING;
738 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
739 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
740 src_vq->lock = &rkvdec->vdev_lock;
741 src_vq->dev = rkvdec->v4l2_dev.dev;
742 src_vq->supports_requests = true;
743 src_vq->requires_requests = true;
745 ret = vb2_queue_init(src_vq);
749 dst_vq->bidirectional = true;
750 dst_vq->mem_ops = &vb2_dma_contig_memops;
751 dst_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
752 DMA_ATTR_NO_KERNEL_MAPPING;
753 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
754 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
755 dst_vq->drv_priv = ctx;
756 dst_vq->ops = &rkvdec_queue_ops;
757 dst_vq->buf_struct_size = sizeof(struct rkvdec_decoded_buffer);
758 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
759 dst_vq->lock = &rkvdec->vdev_lock;
760 dst_vq->dev = rkvdec->v4l2_dev.dev;
762 return vb2_queue_init(dst_vq);
765 static int rkvdec_add_ctrls(struct rkvdec_ctx *ctx,
766 const struct rkvdec_ctrls *ctrls)
770 for (i = 0; i < ctrls->num_ctrls; i++) {
771 const struct v4l2_ctrl_config *cfg = &ctrls->ctrls[i].cfg;
773 v4l2_ctrl_new_custom(&ctx->ctrl_hdl, cfg, ctx);
774 if (ctx->ctrl_hdl.error)
775 return ctx->ctrl_hdl.error;
781 static int rkvdec_init_ctrls(struct rkvdec_ctx *ctx)
783 unsigned int i, nctrls = 0;
786 for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++)
787 nctrls += rkvdec_coded_fmts[i].ctrls->num_ctrls;
789 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, nctrls);
791 for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++) {
792 ret = rkvdec_add_ctrls(ctx, rkvdec_coded_fmts[i].ctrls);
794 goto err_free_handler;
797 ret = v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
799 goto err_free_handler;
801 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
805 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
809 static int rkvdec_open(struct file *filp)
811 struct rkvdec_dev *rkvdec = video_drvdata(filp);
812 struct rkvdec_ctx *ctx;
815 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
820 rkvdec_reset_coded_fmt(ctx);
821 rkvdec_reset_decoded_fmt(ctx);
822 v4l2_fh_init(&ctx->fh, video_devdata(filp));
824 ret = rkvdec_init_ctrls(ctx);
828 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(rkvdec->m2m_dev, ctx,
830 if (IS_ERR(ctx->fh.m2m_ctx)) {
831 ret = PTR_ERR(ctx->fh.m2m_ctx);
832 goto err_cleanup_ctrls;
835 filp->private_data = &ctx->fh;
836 v4l2_fh_add(&ctx->fh);
841 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
848 static int rkvdec_release(struct file *filp)
850 struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(filp->private_data);
852 v4l2_fh_del(&ctx->fh);
853 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
854 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
855 v4l2_fh_exit(&ctx->fh);
861 static const struct v4l2_file_operations rkvdec_fops = {
862 .owner = THIS_MODULE,
864 .release = rkvdec_release,
865 .poll = v4l2_m2m_fop_poll,
866 .unlocked_ioctl = video_ioctl2,
867 .mmap = v4l2_m2m_fop_mmap,
870 static int rkvdec_v4l2_init(struct rkvdec_dev *rkvdec)
874 ret = v4l2_device_register(rkvdec->dev, &rkvdec->v4l2_dev);
876 dev_err(rkvdec->dev, "Failed to register V4L2 device\n");
880 rkvdec->m2m_dev = v4l2_m2m_init(&rkvdec_m2m_ops);
881 if (IS_ERR(rkvdec->m2m_dev)) {
882 v4l2_err(&rkvdec->v4l2_dev, "Failed to init mem2mem device\n");
883 ret = PTR_ERR(rkvdec->m2m_dev);
884 goto err_unregister_v4l2;
887 rkvdec->mdev.dev = rkvdec->dev;
888 strscpy(rkvdec->mdev.model, "rkvdec", sizeof(rkvdec->mdev.model));
889 strscpy(rkvdec->mdev.bus_info, "platform:rkvdec",
890 sizeof(rkvdec->mdev.bus_info));
891 media_device_init(&rkvdec->mdev);
892 rkvdec->mdev.ops = &rkvdec_media_ops;
893 rkvdec->v4l2_dev.mdev = &rkvdec->mdev;
895 rkvdec->vdev.lock = &rkvdec->vdev_lock;
896 rkvdec->vdev.v4l2_dev = &rkvdec->v4l2_dev;
897 rkvdec->vdev.fops = &rkvdec_fops;
898 rkvdec->vdev.release = video_device_release_empty;
899 rkvdec->vdev.vfl_dir = VFL_DIR_M2M;
900 rkvdec->vdev.device_caps = V4L2_CAP_STREAMING |
901 V4L2_CAP_VIDEO_M2M_MPLANE;
902 rkvdec->vdev.ioctl_ops = &rkvdec_ioctl_ops;
903 video_set_drvdata(&rkvdec->vdev, rkvdec);
904 strscpy(rkvdec->vdev.name, "rkvdec", sizeof(rkvdec->vdev.name));
906 ret = video_register_device(&rkvdec->vdev, VFL_TYPE_VIDEO, -1);
908 v4l2_err(&rkvdec->v4l2_dev, "Failed to register video device\n");
912 ret = v4l2_m2m_register_media_controller(rkvdec->m2m_dev, &rkvdec->vdev,
913 MEDIA_ENT_F_PROC_VIDEO_DECODER);
915 v4l2_err(&rkvdec->v4l2_dev,
916 "Failed to initialize V4L2 M2M media controller\n");
917 goto err_unregister_vdev;
920 ret = media_device_register(&rkvdec->mdev);
922 v4l2_err(&rkvdec->v4l2_dev, "Failed to register media device\n");
923 goto err_unregister_mc;
929 v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
932 video_unregister_device(&rkvdec->vdev);
935 media_device_cleanup(&rkvdec->mdev);
936 v4l2_m2m_release(rkvdec->m2m_dev);
939 v4l2_device_unregister(&rkvdec->v4l2_dev);
943 static void rkvdec_v4l2_cleanup(struct rkvdec_dev *rkvdec)
945 media_device_unregister(&rkvdec->mdev);
946 v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
947 video_unregister_device(&rkvdec->vdev);
948 media_device_cleanup(&rkvdec->mdev);
949 v4l2_m2m_release(rkvdec->m2m_dev);
950 v4l2_device_unregister(&rkvdec->v4l2_dev);
953 static irqreturn_t rkvdec_irq_handler(int irq, void *priv)
955 struct rkvdec_dev *rkvdec = priv;
956 enum vb2_buffer_state state;
959 status = readl(rkvdec->regs + RKVDEC_REG_INTERRUPT);
960 state = (status & RKVDEC_RDY_STA) ?
961 VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
963 writel(0, rkvdec->regs + RKVDEC_REG_INTERRUPT);
964 if (cancel_delayed_work(&rkvdec->watchdog_work)) {
965 struct rkvdec_ctx *ctx;
967 ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
968 rkvdec_job_finish(ctx, state);
974 static void rkvdec_watchdog_func(struct work_struct *work)
976 struct rkvdec_dev *rkvdec;
977 struct rkvdec_ctx *ctx;
979 rkvdec = container_of(to_delayed_work(work), struct rkvdec_dev,
981 ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
983 dev_err(rkvdec->dev, "Frame processing timed out!\n");
984 writel(RKVDEC_IRQ_DIS, rkvdec->regs + RKVDEC_REG_INTERRUPT);
985 writel(0, rkvdec->regs + RKVDEC_REG_SYSCTRL);
986 rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
990 static const struct of_device_id of_rkvdec_match[] = {
991 { .compatible = "rockchip,rk3399-vdec" },
994 MODULE_DEVICE_TABLE(of, of_rkvdec_match);
996 static const char * const rkvdec_clk_names[] = {
997 "axi", "ahb", "cabac", "core"
1000 static int rkvdec_probe(struct platform_device *pdev)
1002 struct rkvdec_dev *rkvdec;
1006 rkvdec = devm_kzalloc(&pdev->dev, sizeof(*rkvdec), GFP_KERNEL);
1010 platform_set_drvdata(pdev, rkvdec);
1011 rkvdec->dev = &pdev->dev;
1012 mutex_init(&rkvdec->vdev_lock);
1013 INIT_DELAYED_WORK(&rkvdec->watchdog_work, rkvdec_watchdog_func);
1015 rkvdec->clocks = devm_kcalloc(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
1016 sizeof(*rkvdec->clocks), GFP_KERNEL);
1017 if (!rkvdec->clocks)
1020 for (i = 0; i < ARRAY_SIZE(rkvdec_clk_names); i++)
1021 rkvdec->clocks[i].id = rkvdec_clk_names[i];
1023 ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
1029 * Bump ACLK to max. possible freq. (500 MHz) to improve performance
1030 * When 4k video playback.
1032 clk_set_rate(rkvdec->clocks[0].clk, 500 * 1000 * 1000);
1034 rkvdec->regs = devm_platform_ioremap_resource(pdev, 0);
1035 if (IS_ERR(rkvdec->regs))
1036 return PTR_ERR(rkvdec->regs);
1038 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1040 dev_err(&pdev->dev, "Could not set DMA coherent mask.\n");
1044 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
1046 irq = platform_get_irq(pdev, 0);
1048 dev_err(&pdev->dev, "Could not get vdec IRQ\n");
1052 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1053 rkvdec_irq_handler, IRQF_ONESHOT,
1054 dev_name(&pdev->dev), rkvdec);
1056 dev_err(&pdev->dev, "Could not request vdec IRQ\n");
1060 pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
1061 pm_runtime_use_autosuspend(&pdev->dev);
1062 pm_runtime_enable(&pdev->dev);
1064 ret = rkvdec_v4l2_init(rkvdec);
1066 goto err_disable_runtime_pm;
1070 err_disable_runtime_pm:
1071 pm_runtime_dont_use_autosuspend(&pdev->dev);
1072 pm_runtime_disable(&pdev->dev);
1076 static int rkvdec_remove(struct platform_device *pdev)
1078 struct rkvdec_dev *rkvdec = platform_get_drvdata(pdev);
1080 cancel_delayed_work_sync(&rkvdec->watchdog_work);
1082 rkvdec_v4l2_cleanup(rkvdec);
1083 pm_runtime_disable(&pdev->dev);
1084 pm_runtime_dont_use_autosuspend(&pdev->dev);
1089 static int rkvdec_runtime_resume(struct device *dev)
1091 struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
1093 return clk_bulk_prepare_enable(ARRAY_SIZE(rkvdec_clk_names),
1097 static int rkvdec_runtime_suspend(struct device *dev)
1099 struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
1101 clk_bulk_disable_unprepare(ARRAY_SIZE(rkvdec_clk_names),
1107 static const struct dev_pm_ops rkvdec_pm_ops = {
1108 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1109 pm_runtime_force_resume)
1110 SET_RUNTIME_PM_OPS(rkvdec_runtime_suspend, rkvdec_runtime_resume, NULL)
1113 static struct platform_driver rkvdec_driver = {
1114 .probe = rkvdec_probe,
1115 .remove = rkvdec_remove,
1118 .of_match_table = of_rkvdec_match,
1119 .pm = &rkvdec_pm_ops,
1122 module_platform_driver(rkvdec_driver);
1124 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@collabora.com>");
1125 MODULE_DESCRIPTION("Rockchip Video Decoder driver");
1126 MODULE_LICENSE("GPL v2");