1 // SPDX-License-Identifier: GPL-2.0
3 * Hantro VPU codec driver
5 * Copyright (C) 2018 Collabora, Ltd.
6 * Copyright 2018 Google LLC.
7 * Tomasz Figa <tfiga@chromium.org>
9 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
10 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
13 #include <linux/clk.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>
27 #include "hantro_v4l2.h"
29 #include "hantro_hw.h"
31 #define DRIVER_NAME "hantro-vpu"
34 module_param_named(debug, hantro_debug, int, 0644);
35 MODULE_PARM_DESC(debug,
36 "Debug level - higher value produces more verbose messages");
38 void *hantro_get_ctrl(struct hantro_ctx *ctx, u32 id)
40 struct v4l2_ctrl *ctrl;
42 ctrl = v4l2_ctrl_find(&ctx->ctrl_handler, id);
43 return ctrl ? ctrl->p_cur.p : NULL;
46 dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts)
48 struct vb2_queue *q = v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx);
49 struct vb2_buffer *buf;
52 index = vb2_find_timestamp(q, ts, 0);
55 buf = vb2_get_buffer(q, index);
56 return hantro_get_dec_buf_addr(ctx, buf);
59 static void hantro_job_finish_no_pm(struct hantro_dev *vpu,
60 struct hantro_ctx *ctx,
61 enum vb2_buffer_state result)
63 struct vb2_v4l2_buffer *src, *dst;
65 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
66 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
73 src->sequence = ctx->sequence_out++;
74 dst->sequence = ctx->sequence_cap++;
76 v4l2_m2m_buf_done_and_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx,
80 static void hantro_job_finish(struct hantro_dev *vpu,
81 struct hantro_ctx *ctx,
82 enum vb2_buffer_state result)
84 pm_runtime_mark_last_busy(vpu->dev);
85 pm_runtime_put_autosuspend(vpu->dev);
87 clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
89 hantro_job_finish_no_pm(vpu, ctx, result);
92 void hantro_irq_done(struct hantro_dev *vpu,
93 enum vb2_buffer_state result)
95 struct hantro_ctx *ctx =
96 v4l2_m2m_get_curr_priv(vpu->m2m_dev);
99 * If cancel_delayed_work returns false
100 * the timeout expired. The watchdog is running,
101 * and will take care of finishing the job.
103 if (cancel_delayed_work(&vpu->watchdog_work)) {
104 if (result == VB2_BUF_STATE_DONE && ctx->codec_ops->done)
105 ctx->codec_ops->done(ctx);
106 hantro_job_finish(vpu, ctx, result);
110 void hantro_watchdog(struct work_struct *work)
112 struct hantro_dev *vpu;
113 struct hantro_ctx *ctx;
115 vpu = container_of(to_delayed_work(work),
116 struct hantro_dev, watchdog_work);
117 ctx = v4l2_m2m_get_curr_priv(vpu->m2m_dev);
119 vpu_err("frame processing timed out!\n");
120 ctx->codec_ops->reset(ctx);
121 hantro_job_finish(vpu, ctx, VB2_BUF_STATE_ERROR);
125 void hantro_start_prepare_run(struct hantro_ctx *ctx)
127 struct vb2_v4l2_buffer *src_buf;
129 src_buf = hantro_get_src_buf(ctx);
130 v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
133 if (!ctx->is_encoder) {
134 if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt))
135 hantro_postproc_enable(ctx);
137 hantro_postproc_disable(ctx);
141 void hantro_end_prepare_run(struct hantro_ctx *ctx)
143 struct vb2_v4l2_buffer *src_buf;
145 src_buf = hantro_get_src_buf(ctx);
146 v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
149 /* Kick the watchdog. */
150 schedule_delayed_work(&ctx->dev->watchdog_work,
151 msecs_to_jiffies(2000));
154 static void device_run(void *priv)
156 struct hantro_ctx *ctx = priv;
157 struct vb2_v4l2_buffer *src, *dst;
160 src = hantro_get_src_buf(ctx);
161 dst = hantro_get_dst_buf(ctx);
163 ret = pm_runtime_get_sync(ctx->dev->dev);
165 pm_runtime_put_noidle(ctx->dev->dev);
169 ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
173 v4l2_m2m_buf_copy_metadata(src, dst, true);
175 ctx->codec_ops->run(ctx);
179 hantro_job_finish_no_pm(ctx->dev, ctx, VB2_BUF_STATE_ERROR);
182 static struct v4l2_m2m_ops vpu_m2m_ops = {
183 .device_run = device_run,
187 queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
189 struct hantro_ctx *ctx = priv;
192 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
193 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
194 src_vq->drv_priv = ctx;
195 src_vq->ops = &hantro_queue_ops;
196 src_vq->mem_ops = &vb2_dma_contig_memops;
199 * Driver does mostly sequential access, so sacrifice TLB efficiency
200 * for faster allocation. Also, no CPU access on the source queue,
201 * so no kernel mapping needed.
203 src_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
204 DMA_ATTR_NO_KERNEL_MAPPING;
205 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
206 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
207 src_vq->lock = &ctx->dev->vpu_mutex;
208 src_vq->dev = ctx->dev->v4l2_dev.dev;
209 src_vq->supports_requests = true;
211 ret = vb2_queue_init(src_vq);
216 * When encoding, the CAPTURE queue doesn't need dma memory,
217 * as the CPU needs to create the JPEG frames, from the
218 * hardware-produced JPEG payload.
220 * For the DMA destination buffer, we use a bounce buffer.
222 if (ctx->is_encoder) {
223 dst_vq->mem_ops = &vb2_vmalloc_memops;
225 dst_vq->bidirectional = true;
226 dst_vq->mem_ops = &vb2_dma_contig_memops;
227 dst_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
228 DMA_ATTR_NO_KERNEL_MAPPING;
231 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
232 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
233 dst_vq->drv_priv = ctx;
234 dst_vq->ops = &hantro_queue_ops;
235 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
236 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
237 dst_vq->lock = &ctx->dev->vpu_mutex;
238 dst_vq->dev = ctx->dev->v4l2_dev.dev;
240 return vb2_queue_init(dst_vq);
243 static int hantro_try_ctrl(struct v4l2_ctrl *ctrl)
245 if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_SPS) {
246 const struct v4l2_ctrl_h264_sps *sps = ctrl->p_new.p_h264_sps;
248 if (sps->chroma_format_idc > 1)
249 /* Only 4:0:0 and 4:2:0 are supported */
251 if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8)
252 /* Luma and chroma bit depth mismatch */
254 if (sps->bit_depth_luma_minus8 != 0)
255 /* Only 8-bit is supported */
261 static int hantro_jpeg_s_ctrl(struct v4l2_ctrl *ctrl)
263 struct hantro_ctx *ctx;
265 ctx = container_of(ctrl->handler,
266 struct hantro_ctx, ctrl_handler);
268 vpu_debug(1, "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
271 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
272 ctx->jpeg_quality = ctrl->val;
281 static const struct v4l2_ctrl_ops hantro_ctrl_ops = {
282 .try_ctrl = hantro_try_ctrl,
285 static const struct v4l2_ctrl_ops hantro_jpeg_ctrl_ops = {
286 .s_ctrl = hantro_jpeg_s_ctrl,
289 static const struct hantro_ctrl controls[] = {
291 .codec = HANTRO_JPEG_ENCODER,
293 .id = V4L2_CID_JPEG_COMPRESSION_QUALITY,
298 .ops = &hantro_jpeg_ctrl_ops,
301 .codec = HANTRO_MPEG2_DECODER,
303 .id = V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS,
306 .codec = HANTRO_MPEG2_DECODER,
308 .id = V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION,
311 .codec = HANTRO_VP8_DECODER,
313 .id = V4L2_CID_MPEG_VIDEO_VP8_FRAME_HEADER,
316 .codec = HANTRO_H264_DECODER,
318 .id = V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAMS,
321 .codec = HANTRO_H264_DECODER,
323 .id = V4L2_CID_MPEG_VIDEO_H264_SPS,
324 .ops = &hantro_ctrl_ops,
327 .codec = HANTRO_H264_DECODER,
329 .id = V4L2_CID_MPEG_VIDEO_H264_PPS,
332 .codec = HANTRO_H264_DECODER,
334 .id = V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX,
337 .codec = HANTRO_H264_DECODER,
339 .id = V4L2_CID_MPEG_VIDEO_H264_DECODE_MODE,
340 .min = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
341 .def = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
342 .max = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
345 .codec = HANTRO_H264_DECODER,
347 .id = V4L2_CID_MPEG_VIDEO_H264_START_CODE,
348 .min = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
349 .def = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
350 .max = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
353 .codec = HANTRO_H264_DECODER,
355 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
356 .min = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
357 .max = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
359 BIT(V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED),
360 .def = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
366 static int hantro_ctrls_setup(struct hantro_dev *vpu,
367 struct hantro_ctx *ctx,
370 int i, num_ctrls = ARRAY_SIZE(controls);
372 v4l2_ctrl_handler_init(&ctx->ctrl_handler, num_ctrls);
374 for (i = 0; i < num_ctrls; i++) {
375 if (!(allowed_codecs & controls[i].codec))
378 v4l2_ctrl_new_custom(&ctx->ctrl_handler,
379 &controls[i].cfg, NULL);
380 if (ctx->ctrl_handler.error) {
381 vpu_err("Adding control (%d) failed %d\n",
383 ctx->ctrl_handler.error);
384 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
385 return ctx->ctrl_handler.error;
388 return v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
392 * V4L2 file operations.
395 static int hantro_open(struct file *filp)
397 struct hantro_dev *vpu = video_drvdata(filp);
398 struct video_device *vdev = video_devdata(filp);
399 struct hantro_func *func = hantro_vdev_to_func(vdev);
400 struct hantro_ctx *ctx;
401 int allowed_codecs, ret;
404 * We do not need any extra locking here, because we operate only
405 * on local data here, except reading few fields from dev, which
406 * do not change through device's lifetime (which is guaranteed by
407 * reference on module from open()) and V4L2 internal objects (such
408 * as vdev and ctx->fh), which have proper locking done in respective
409 * helper functions used here.
412 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
417 if (func->id == MEDIA_ENT_F_PROC_VIDEO_ENCODER) {
418 allowed_codecs = vpu->variant->codec & HANTRO_ENCODERS;
419 ctx->is_encoder = true;
420 } else if (func->id == MEDIA_ENT_F_PROC_VIDEO_DECODER) {
421 allowed_codecs = vpu->variant->codec & HANTRO_DECODERS;
422 ctx->is_encoder = false;
428 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(vpu->m2m_dev, ctx, queue_init);
429 if (IS_ERR(ctx->fh.m2m_ctx)) {
430 ret = PTR_ERR(ctx->fh.m2m_ctx);
434 v4l2_fh_init(&ctx->fh, vdev);
435 filp->private_data = &ctx->fh;
436 v4l2_fh_add(&ctx->fh);
438 hantro_reset_fmts(ctx);
440 ret = hantro_ctrls_setup(vpu, ctx, allowed_codecs);
442 vpu_err("Failed to set up controls\n");
445 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
450 v4l2_fh_del(&ctx->fh);
451 v4l2_fh_exit(&ctx->fh);
457 static int hantro_release(struct file *filp)
459 struct hantro_ctx *ctx =
460 container_of(filp->private_data, struct hantro_ctx, fh);
463 * No need for extra locking because this was the last reference
466 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
467 v4l2_fh_del(&ctx->fh);
468 v4l2_fh_exit(&ctx->fh);
469 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
475 static const struct v4l2_file_operations hantro_fops = {
476 .owner = THIS_MODULE,
478 .release = hantro_release,
479 .poll = v4l2_m2m_fop_poll,
480 .unlocked_ioctl = video_ioctl2,
481 .mmap = v4l2_m2m_fop_mmap,
484 static const struct of_device_id of_hantro_match[] = {
485 #ifdef CONFIG_VIDEO_HANTRO_ROCKCHIP
486 { .compatible = "rockchip,rk3399-vpu", .data = &rk3399_vpu_variant, },
487 { .compatible = "rockchip,rk3328-vpu", .data = &rk3328_vpu_variant, },
488 { .compatible = "rockchip,rk3288-vpu", .data = &rk3288_vpu_variant, },
490 #ifdef CONFIG_VIDEO_HANTRO_IMX8M
491 { .compatible = "nxp,imx8mq-vpu", .data = &imx8mq_vpu_variant, },
495 MODULE_DEVICE_TABLE(of, of_hantro_match);
497 static int hantro_register_entity(struct media_device *mdev,
498 struct media_entity *entity,
499 const char *entity_name,
500 struct media_pad *pads, int num_pads,
501 int function, struct video_device *vdev)
506 entity->obj_type = MEDIA_ENTITY_TYPE_BASE;
507 if (function == MEDIA_ENT_F_IO_V4L) {
508 entity->info.dev.major = VIDEO_MAJOR;
509 entity->info.dev.minor = vdev->minor;
512 name = devm_kasprintf(mdev->dev, GFP_KERNEL, "%s-%s", vdev->name,
518 entity->function = function;
520 ret = media_entity_pads_init(entity, num_pads, pads);
524 ret = media_device_register_entity(mdev, entity);
531 static int hantro_attach_func(struct hantro_dev *vpu,
532 struct hantro_func *func)
534 struct media_device *mdev = &vpu->mdev;
535 struct media_link *link;
538 /* Create the three encoder entities with their pads */
539 func->source_pad.flags = MEDIA_PAD_FL_SOURCE;
540 ret = hantro_register_entity(mdev, &func->vdev.entity, "source",
541 &func->source_pad, 1, MEDIA_ENT_F_IO_V4L,
546 func->proc_pads[0].flags = MEDIA_PAD_FL_SINK;
547 func->proc_pads[1].flags = MEDIA_PAD_FL_SOURCE;
548 ret = hantro_register_entity(mdev, &func->proc, "proc",
549 func->proc_pads, 2, func->id,
552 goto err_rel_entity0;
554 func->sink_pad.flags = MEDIA_PAD_FL_SINK;
555 ret = hantro_register_entity(mdev, &func->sink, "sink",
556 &func->sink_pad, 1, MEDIA_ENT_F_IO_V4L,
559 goto err_rel_entity1;
561 /* Connect the three entities */
562 ret = media_create_pad_link(&func->vdev.entity, 0, &func->proc, 0,
563 MEDIA_LNK_FL_IMMUTABLE |
564 MEDIA_LNK_FL_ENABLED);
566 goto err_rel_entity2;
568 ret = media_create_pad_link(&func->proc, 1, &func->sink, 0,
569 MEDIA_LNK_FL_IMMUTABLE |
570 MEDIA_LNK_FL_ENABLED);
574 /* Create video interface */
575 func->intf_devnode = media_devnode_create(mdev, MEDIA_INTF_T_V4L_VIDEO,
578 if (!func->intf_devnode) {
583 /* Connect the two DMA engines to the interface */
584 link = media_create_intf_link(&func->vdev.entity,
585 &func->intf_devnode->intf,
586 MEDIA_LNK_FL_IMMUTABLE |
587 MEDIA_LNK_FL_ENABLED);
593 link = media_create_intf_link(&func->sink, &func->intf_devnode->intf,
594 MEDIA_LNK_FL_IMMUTABLE |
595 MEDIA_LNK_FL_ENABLED);
603 media_devnode_remove(func->intf_devnode);
606 media_entity_remove_links(&func->sink);
609 media_entity_remove_links(&func->proc);
610 media_entity_remove_links(&func->vdev.entity);
613 media_device_unregister_entity(&func->sink);
616 media_device_unregister_entity(&func->proc);
619 media_device_unregister_entity(&func->vdev.entity);
623 static void hantro_detach_func(struct hantro_func *func)
625 media_devnode_remove(func->intf_devnode);
626 media_entity_remove_links(&func->sink);
627 media_entity_remove_links(&func->proc);
628 media_entity_remove_links(&func->vdev.entity);
629 media_device_unregister_entity(&func->sink);
630 media_device_unregister_entity(&func->proc);
631 media_device_unregister_entity(&func->vdev.entity);
634 static int hantro_add_func(struct hantro_dev *vpu, unsigned int funcid)
636 const struct of_device_id *match;
637 struct hantro_func *func;
638 struct video_device *vfd;
641 match = of_match_node(of_hantro_match, vpu->dev->of_node);
642 func = devm_kzalloc(vpu->dev, sizeof(*func), GFP_KERNEL);
644 v4l2_err(&vpu->v4l2_dev, "Failed to allocate video device\n");
651 vfd->fops = &hantro_fops;
652 vfd->release = video_device_release_empty;
653 vfd->lock = &vpu->vpu_mutex;
654 vfd->v4l2_dev = &vpu->v4l2_dev;
655 vfd->vfl_dir = VFL_DIR_M2M;
656 vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
657 vfd->ioctl_ops = &hantro_ioctl_ops;
658 snprintf(vfd->name, sizeof(vfd->name), "%s-%s", match->compatible,
659 funcid == MEDIA_ENT_F_PROC_VIDEO_ENCODER ? "enc" : "dec");
661 if (funcid == MEDIA_ENT_F_PROC_VIDEO_ENCODER)
666 video_set_drvdata(vfd, vpu);
668 ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
670 v4l2_err(&vpu->v4l2_dev, "Failed to register video device\n");
674 ret = hantro_attach_func(vpu, func);
676 v4l2_err(&vpu->v4l2_dev,
677 "Failed to attach functionality to the media device\n");
681 v4l2_info(&vpu->v4l2_dev, "registered %s as /dev/video%d\n", vfd->name,
687 video_unregister_device(vfd);
691 static int hantro_add_enc_func(struct hantro_dev *vpu)
693 if (!vpu->variant->enc_fmts)
696 return hantro_add_func(vpu, MEDIA_ENT_F_PROC_VIDEO_ENCODER);
699 static int hantro_add_dec_func(struct hantro_dev *vpu)
701 if (!vpu->variant->dec_fmts)
704 return hantro_add_func(vpu, MEDIA_ENT_F_PROC_VIDEO_DECODER);
707 static void hantro_remove_func(struct hantro_dev *vpu,
710 struct hantro_func *func;
712 if (funcid == MEDIA_ENT_F_PROC_VIDEO_ENCODER)
720 hantro_detach_func(func);
721 video_unregister_device(&func->vdev);
724 static void hantro_remove_enc_func(struct hantro_dev *vpu)
726 hantro_remove_func(vpu, MEDIA_ENT_F_PROC_VIDEO_ENCODER);
729 static void hantro_remove_dec_func(struct hantro_dev *vpu)
731 hantro_remove_func(vpu, MEDIA_ENT_F_PROC_VIDEO_DECODER);
734 static const struct media_device_ops hantro_m2m_media_ops = {
735 .req_validate = vb2_request_validate,
736 .req_queue = v4l2_m2m_request_queue,
739 static int hantro_probe(struct platform_device *pdev)
741 const struct of_device_id *match;
742 struct hantro_dev *vpu;
743 struct resource *res;
747 vpu = devm_kzalloc(&pdev->dev, sizeof(*vpu), GFP_KERNEL);
751 vpu->dev = &pdev->dev;
753 mutex_init(&vpu->vpu_mutex);
754 spin_lock_init(&vpu->irqlock);
756 match = of_match_node(of_hantro_match, pdev->dev.of_node);
757 vpu->variant = match->data;
759 INIT_DELAYED_WORK(&vpu->watchdog_work, hantro_watchdog);
761 vpu->clocks = devm_kcalloc(&pdev->dev, vpu->variant->num_clocks,
762 sizeof(*vpu->clocks), GFP_KERNEL);
766 for (i = 0; i < vpu->variant->num_clocks; i++)
767 vpu->clocks[i].id = vpu->variant->clk_names[i];
768 ret = devm_clk_bulk_get(&pdev->dev, vpu->variant->num_clocks,
773 num_bases = vpu->variant->num_regs ?: 1;
774 vpu->reg_bases = devm_kcalloc(&pdev->dev, num_bases,
775 sizeof(*vpu->reg_bases), GFP_KERNEL);
779 for (i = 0; i < num_bases; i++) {
780 res = vpu->variant->reg_names ?
781 platform_get_resource_byname(vpu->pdev, IORESOURCE_MEM,
782 vpu->variant->reg_names[i]) :
783 platform_get_resource(vpu->pdev, IORESOURCE_MEM, 0);
784 vpu->reg_bases[i] = devm_ioremap_resource(vpu->dev, res);
785 if (IS_ERR(vpu->reg_bases[i]))
786 return PTR_ERR(vpu->reg_bases[i]);
788 vpu->enc_base = vpu->reg_bases[0] + vpu->variant->enc_offset;
789 vpu->dec_base = vpu->reg_bases[0] + vpu->variant->dec_offset;
791 ret = dma_set_coherent_mask(vpu->dev, DMA_BIT_MASK(32));
793 dev_err(vpu->dev, "Could not set DMA coherent mask.\n");
796 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
798 for (i = 0; i < vpu->variant->num_irqs; i++) {
799 const char *irq_name = vpu->variant->irqs[i].name;
802 if (!vpu->variant->irqs[i].handler)
805 irq = platform_get_irq_byname(vpu->pdev, irq_name);
809 ret = devm_request_irq(vpu->dev, irq,
810 vpu->variant->irqs[i].handler, 0,
811 dev_name(vpu->dev), vpu);
813 dev_err(vpu->dev, "Could not request %s IRQ.\n",
819 ret = vpu->variant->init(vpu);
821 dev_err(&pdev->dev, "Failed to init VPU hardware\n");
825 pm_runtime_set_autosuspend_delay(vpu->dev, 100);
826 pm_runtime_use_autosuspend(vpu->dev);
827 pm_runtime_enable(vpu->dev);
829 ret = clk_bulk_prepare(vpu->variant->num_clocks, vpu->clocks);
831 dev_err(&pdev->dev, "Failed to prepare clocks\n");
835 ret = v4l2_device_register(&pdev->dev, &vpu->v4l2_dev);
837 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
838 goto err_clk_unprepare;
840 platform_set_drvdata(pdev, vpu);
842 vpu->m2m_dev = v4l2_m2m_init(&vpu_m2m_ops);
843 if (IS_ERR(vpu->m2m_dev)) {
844 v4l2_err(&vpu->v4l2_dev, "Failed to init mem2mem device\n");
845 ret = PTR_ERR(vpu->m2m_dev);
849 vpu->mdev.dev = vpu->dev;
850 strscpy(vpu->mdev.model, DRIVER_NAME, sizeof(vpu->mdev.model));
851 strscpy(vpu->mdev.bus_info, "platform: " DRIVER_NAME,
852 sizeof(vpu->mdev.model));
853 media_device_init(&vpu->mdev);
854 vpu->mdev.ops = &hantro_m2m_media_ops;
855 vpu->v4l2_dev.mdev = &vpu->mdev;
857 ret = hantro_add_enc_func(vpu);
859 dev_err(&pdev->dev, "Failed to register encoder\n");
863 ret = hantro_add_dec_func(vpu);
865 dev_err(&pdev->dev, "Failed to register decoder\n");
866 goto err_rm_enc_func;
869 ret = media_device_register(&vpu->mdev);
871 v4l2_err(&vpu->v4l2_dev, "Failed to register mem2mem media device\n");
872 goto err_rm_dec_func;
878 hantro_remove_dec_func(vpu);
880 hantro_remove_enc_func(vpu);
882 media_device_cleanup(&vpu->mdev);
883 v4l2_m2m_release(vpu->m2m_dev);
885 v4l2_device_unregister(&vpu->v4l2_dev);
887 clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
889 pm_runtime_dont_use_autosuspend(vpu->dev);
890 pm_runtime_disable(vpu->dev);
894 static int hantro_remove(struct platform_device *pdev)
896 struct hantro_dev *vpu = platform_get_drvdata(pdev);
898 v4l2_info(&vpu->v4l2_dev, "Removing %s\n", pdev->name);
900 media_device_unregister(&vpu->mdev);
901 hantro_remove_dec_func(vpu);
902 hantro_remove_enc_func(vpu);
903 media_device_cleanup(&vpu->mdev);
904 v4l2_m2m_release(vpu->m2m_dev);
905 v4l2_device_unregister(&vpu->v4l2_dev);
906 clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
907 pm_runtime_dont_use_autosuspend(vpu->dev);
908 pm_runtime_disable(vpu->dev);
913 static int hantro_runtime_resume(struct device *dev)
915 struct hantro_dev *vpu = dev_get_drvdata(dev);
917 if (vpu->variant->runtime_resume)
918 return vpu->variant->runtime_resume(vpu);
924 static const struct dev_pm_ops hantro_pm_ops = {
925 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
926 pm_runtime_force_resume)
927 SET_RUNTIME_PM_OPS(NULL, hantro_runtime_resume, NULL)
930 static struct platform_driver hantro_driver = {
931 .probe = hantro_probe,
932 .remove = hantro_remove,
935 .of_match_table = of_match_ptr(of_hantro_match),
936 .pm = &hantro_pm_ops,
939 module_platform_driver(hantro_driver);
941 MODULE_LICENSE("GPL v2");
942 MODULE_AUTHOR("Alpha Lin <Alpha.Lin@Rock-Chips.com>");
943 MODULE_AUTHOR("Tomasz Figa <tfiga@chromium.org>");
944 MODULE_AUTHOR("Ezequiel Garcia <ezequiel@collabora.com>");
945 MODULE_DESCRIPTION("Hantro VPU codec driver");