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 vb2_queue *q, u64 ts)
48 struct vb2_buffer *buf;
51 index = vb2_find_timestamp(q, ts, 0);
54 buf = vb2_get_buffer(q, index);
55 return vb2_dma_contig_plane_dma_addr(buf, 0);
59 hantro_enc_buf_finish(struct hantro_ctx *ctx, struct vb2_buffer *buf,
60 unsigned int bytesused)
64 avail_size = vb2_plane_size(buf, 0) - ctx->vpu_dst_fmt->header_size;
65 if (bytesused > avail_size)
68 * The bounce buffer is only for the JPEG encoder.
69 * TODO: Rework the JPEG encoder to eliminate the need
70 * for a bounce buffer.
72 if (ctx->jpeg_enc.bounce_buffer.cpu) {
73 memcpy(vb2_plane_vaddr(buf, 0) +
74 ctx->vpu_dst_fmt->header_size,
75 ctx->jpeg_enc.bounce_buffer.cpu, bytesused);
77 buf->planes[0].bytesused =
78 ctx->vpu_dst_fmt->header_size + bytesused;
83 hantro_dec_buf_finish(struct hantro_ctx *ctx, struct vb2_buffer *buf,
84 unsigned int bytesused)
86 /* For decoders set bytesused as per the output picture. */
87 buf->planes[0].bytesused = ctx->dst_fmt.plane_fmt[0].sizeimage;
91 static void hantro_job_finish(struct hantro_dev *vpu,
92 struct hantro_ctx *ctx,
93 unsigned int bytesused,
94 enum vb2_buffer_state result)
96 struct vb2_v4l2_buffer *src, *dst;
99 pm_runtime_mark_last_busy(vpu->dev);
100 pm_runtime_put_autosuspend(vpu->dev);
101 clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
103 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
104 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
111 src->sequence = ctx->sequence_out++;
112 dst->sequence = ctx->sequence_cap++;
114 v4l2_m2m_buf_copy_metadata(src, dst, true);
116 ret = ctx->buf_finish(ctx, &dst->vb2_buf, bytesused);
118 result = VB2_BUF_STATE_ERROR;
120 v4l2_m2m_buf_done(src, result);
121 v4l2_m2m_buf_done(dst, result);
123 v4l2_m2m_job_finish(vpu->m2m_dev, ctx->fh.m2m_ctx);
126 void hantro_irq_done(struct hantro_dev *vpu, unsigned int bytesused,
127 enum vb2_buffer_state result)
129 struct hantro_ctx *ctx =
130 v4l2_m2m_get_curr_priv(vpu->m2m_dev);
133 * If cancel_delayed_work returns false
134 * the timeout expired. The watchdog is running,
135 * and will take care of finishing the job.
137 if (cancel_delayed_work(&vpu->watchdog_work))
138 hantro_job_finish(vpu, ctx, bytesused, result);
141 void hantro_watchdog(struct work_struct *work)
143 struct hantro_dev *vpu;
144 struct hantro_ctx *ctx;
146 vpu = container_of(to_delayed_work(work),
147 struct hantro_dev, watchdog_work);
148 ctx = v4l2_m2m_get_curr_priv(vpu->m2m_dev);
150 vpu_err("frame processing timed out!\n");
151 ctx->codec_ops->reset(ctx);
152 hantro_job_finish(vpu, ctx, 0, VB2_BUF_STATE_ERROR);
156 static void device_run(void *priv)
158 struct hantro_ctx *ctx = priv;
161 ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
164 ret = pm_runtime_get_sync(ctx->dev->dev);
168 ctx->codec_ops->run(ctx);
172 hantro_job_finish(ctx->dev, ctx, 0, VB2_BUF_STATE_ERROR);
175 bool hantro_is_encoder_ctx(const struct hantro_ctx *ctx)
177 return ctx->buf_finish == hantro_enc_buf_finish;
180 static struct v4l2_m2m_ops vpu_m2m_ops = {
181 .device_run = device_run,
185 queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
187 struct hantro_ctx *ctx = priv;
190 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
191 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
192 src_vq->drv_priv = ctx;
193 src_vq->ops = &hantro_queue_ops;
194 src_vq->mem_ops = &vb2_dma_contig_memops;
197 * Driver does mostly sequential access, so sacrifice TLB efficiency
198 * for faster allocation. Also, no CPU access on the source queue,
199 * so no kernel mapping needed.
201 src_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
202 DMA_ATTR_NO_KERNEL_MAPPING;
203 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
204 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
205 src_vq->lock = &ctx->dev->vpu_mutex;
206 src_vq->dev = ctx->dev->v4l2_dev.dev;
207 src_vq->supports_requests = true;
209 ret = vb2_queue_init(src_vq);
214 * When encoding, the CAPTURE queue doesn't need dma memory,
215 * as the CPU needs to create the JPEG frames, from the
216 * hardware-produced JPEG payload.
218 * For the DMA destination buffer, we use a bounce buffer.
220 if (hantro_is_encoder_ctx(ctx)) {
221 dst_vq->mem_ops = &vb2_vmalloc_memops;
223 dst_vq->bidirectional = true;
224 dst_vq->mem_ops = &vb2_dma_contig_memops;
225 dst_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
226 DMA_ATTR_NO_KERNEL_MAPPING;
229 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
230 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
231 dst_vq->drv_priv = ctx;
232 dst_vq->ops = &hantro_queue_ops;
233 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
234 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
235 dst_vq->lock = &ctx->dev->vpu_mutex;
236 dst_vq->dev = ctx->dev->v4l2_dev.dev;
238 return vb2_queue_init(dst_vq);
241 static int hantro_s_ctrl(struct v4l2_ctrl *ctrl)
243 struct hantro_ctx *ctx;
245 ctx = container_of(ctrl->handler,
246 struct hantro_ctx, ctrl_handler);
248 vpu_debug(1, "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
251 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
252 ctx->jpeg_quality = ctrl->val;
261 static const struct v4l2_ctrl_ops hantro_ctrl_ops = {
262 .s_ctrl = hantro_s_ctrl,
265 static struct hantro_ctrl controls[] = {
267 .id = V4L2_CID_JPEG_COMPRESSION_QUALITY,
268 .codec = HANTRO_JPEG_ENCODER,
276 .id = V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS,
277 .codec = HANTRO_MPEG2_DECODER,
279 .elem_size = sizeof(struct v4l2_ctrl_mpeg2_slice_params),
282 .id = V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION,
283 .codec = HANTRO_MPEG2_DECODER,
285 .elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantization),
290 static int hantro_ctrls_setup(struct hantro_dev *vpu,
291 struct hantro_ctx *ctx,
294 int i, num_ctrls = ARRAY_SIZE(controls);
296 v4l2_ctrl_handler_init(&ctx->ctrl_handler, num_ctrls);
298 for (i = 0; i < num_ctrls; i++) {
299 if (!(allowed_codecs & controls[i].codec))
301 if (!controls[i].cfg.elem_size) {
302 v4l2_ctrl_new_std(&ctx->ctrl_handler,
304 controls[i].id, controls[i].cfg.min,
306 controls[i].cfg.step,
307 controls[i].cfg.def);
309 controls[i].cfg.id = controls[i].id;
310 v4l2_ctrl_new_custom(&ctx->ctrl_handler,
311 &controls[i].cfg, NULL);
314 if (ctx->ctrl_handler.error) {
315 vpu_err("Adding control (%d) failed %d\n",
317 ctx->ctrl_handler.error);
318 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
319 return ctx->ctrl_handler.error;
322 return v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
326 * V4L2 file operations.
329 static int hantro_open(struct file *filp)
331 struct hantro_dev *vpu = video_drvdata(filp);
332 struct video_device *vdev = video_devdata(filp);
333 struct hantro_func *func = hantro_vdev_to_func(vdev);
334 struct hantro_ctx *ctx;
335 int allowed_codecs, ret;
338 * We do not need any extra locking here, because we operate only
339 * on local data here, except reading few fields from dev, which
340 * do not change through device's lifetime (which is guaranteed by
341 * reference on module from open()) and V4L2 internal objects (such
342 * as vdev and ctx->fh), which have proper locking done in respective
343 * helper functions used here.
346 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
351 if (func->id == MEDIA_ENT_F_PROC_VIDEO_ENCODER) {
352 allowed_codecs = vpu->variant->codec & HANTRO_ENCODERS;
353 ctx->buf_finish = hantro_enc_buf_finish;
354 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(vpu->m2m_dev, ctx,
356 } else if (func->id == MEDIA_ENT_F_PROC_VIDEO_DECODER) {
357 allowed_codecs = vpu->variant->codec & HANTRO_DECODERS;
358 ctx->buf_finish = hantro_dec_buf_finish;
359 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(vpu->m2m_dev, ctx,
362 ctx->fh.m2m_ctx = ERR_PTR(-ENODEV);
364 if (IS_ERR(ctx->fh.m2m_ctx)) {
365 ret = PTR_ERR(ctx->fh.m2m_ctx);
370 v4l2_fh_init(&ctx->fh, vdev);
371 filp->private_data = &ctx->fh;
372 v4l2_fh_add(&ctx->fh);
374 hantro_reset_fmts(ctx);
376 ret = hantro_ctrls_setup(vpu, ctx, allowed_codecs);
378 vpu_err("Failed to set up controls\n");
381 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
386 v4l2_fh_del(&ctx->fh);
387 v4l2_fh_exit(&ctx->fh);
392 static int hantro_release(struct file *filp)
394 struct hantro_ctx *ctx =
395 container_of(filp->private_data, struct hantro_ctx, fh);
398 * No need for extra locking because this was the last reference
401 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
402 v4l2_fh_del(&ctx->fh);
403 v4l2_fh_exit(&ctx->fh);
404 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
410 static const struct v4l2_file_operations hantro_fops = {
411 .owner = THIS_MODULE,
413 .release = hantro_release,
414 .poll = v4l2_m2m_fop_poll,
415 .unlocked_ioctl = video_ioctl2,
416 .mmap = v4l2_m2m_fop_mmap,
419 static const struct of_device_id of_hantro_match[] = {
420 #ifdef CONFIG_VIDEO_HANTRO_ROCKCHIP
421 { .compatible = "rockchip,rk3399-vpu", .data = &rk3399_vpu_variant, },
422 { .compatible = "rockchip,rk3288-vpu", .data = &rk3288_vpu_variant, },
426 MODULE_DEVICE_TABLE(of, of_hantro_match);
428 static int hantro_register_entity(struct media_device *mdev,
429 struct media_entity *entity,
430 const char *entity_name,
431 struct media_pad *pads, int num_pads,
432 int function, struct video_device *vdev)
437 entity->obj_type = MEDIA_ENTITY_TYPE_BASE;
438 if (function == MEDIA_ENT_F_IO_V4L) {
439 entity->info.dev.major = VIDEO_MAJOR;
440 entity->info.dev.minor = vdev->minor;
443 name = devm_kasprintf(mdev->dev, GFP_KERNEL, "%s-%s", vdev->name,
449 entity->function = function;
451 ret = media_entity_pads_init(entity, num_pads, pads);
455 ret = media_device_register_entity(mdev, entity);
462 static int hantro_attach_func(struct hantro_dev *vpu,
463 struct hantro_func *func)
465 struct media_device *mdev = &vpu->mdev;
466 struct media_link *link;
469 /* Create the three encoder entities with their pads */
470 func->source_pad.flags = MEDIA_PAD_FL_SOURCE;
471 ret = hantro_register_entity(mdev, &func->vdev.entity, "source",
472 &func->source_pad, 1, MEDIA_ENT_F_IO_V4L,
477 func->proc_pads[0].flags = MEDIA_PAD_FL_SINK;
478 func->proc_pads[1].flags = MEDIA_PAD_FL_SOURCE;
479 ret = hantro_register_entity(mdev, &func->proc, "proc",
480 func->proc_pads, 2, func->id,
483 goto err_rel_entity0;
485 func->sink_pad.flags = MEDIA_PAD_FL_SINK;
486 ret = hantro_register_entity(mdev, &func->sink, "sink",
487 &func->sink_pad, 1, MEDIA_ENT_F_IO_V4L,
490 goto err_rel_entity1;
492 /* Connect the three entities */
493 ret = media_create_pad_link(&func->vdev.entity, 0, &func->proc, 1,
494 MEDIA_LNK_FL_IMMUTABLE |
495 MEDIA_LNK_FL_ENABLED);
497 goto err_rel_entity2;
499 ret = media_create_pad_link(&func->proc, 0, &func->sink, 0,
500 MEDIA_LNK_FL_IMMUTABLE |
501 MEDIA_LNK_FL_ENABLED);
505 /* Create video interface */
506 func->intf_devnode = media_devnode_create(mdev, MEDIA_INTF_T_V4L_VIDEO,
509 if (!func->intf_devnode) {
514 /* Connect the two DMA engines to the interface */
515 link = media_create_intf_link(&func->vdev.entity,
516 &func->intf_devnode->intf,
517 MEDIA_LNK_FL_IMMUTABLE |
518 MEDIA_LNK_FL_ENABLED);
524 link = media_create_intf_link(&func->sink, &func->intf_devnode->intf,
525 MEDIA_LNK_FL_IMMUTABLE |
526 MEDIA_LNK_FL_ENABLED);
534 media_devnode_remove(func->intf_devnode);
537 media_entity_remove_links(&func->sink);
540 media_entity_remove_links(&func->proc);
541 media_entity_remove_links(&func->vdev.entity);
544 media_device_unregister_entity(&func->sink);
547 media_device_unregister_entity(&func->proc);
550 media_device_unregister_entity(&func->vdev.entity);
554 static void hantro_detach_func(struct hantro_func *func)
556 media_devnode_remove(func->intf_devnode);
557 media_entity_remove_links(&func->sink);
558 media_entity_remove_links(&func->proc);
559 media_entity_remove_links(&func->vdev.entity);
560 media_device_unregister_entity(&func->sink);
561 media_device_unregister_entity(&func->proc);
562 media_device_unregister_entity(&func->vdev.entity);
565 static int hantro_add_func(struct hantro_dev *vpu, unsigned int funcid)
567 const struct of_device_id *match;
568 struct hantro_func *func;
569 struct video_device *vfd;
572 match = of_match_node(of_hantro_match, vpu->dev->of_node);
573 func = devm_kzalloc(vpu->dev, sizeof(*func), GFP_KERNEL);
575 v4l2_err(&vpu->v4l2_dev, "Failed to allocate video device\n");
582 vfd->fops = &hantro_fops;
583 vfd->release = video_device_release_empty;
584 vfd->lock = &vpu->vpu_mutex;
585 vfd->v4l2_dev = &vpu->v4l2_dev;
586 vfd->vfl_dir = VFL_DIR_M2M;
587 vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
588 vfd->ioctl_ops = &hantro_ioctl_ops;
589 snprintf(vfd->name, sizeof(vfd->name), "%s-%s", match->compatible,
590 funcid == MEDIA_ENT_F_PROC_VIDEO_ENCODER ? "enc" : "dec");
592 if (funcid == MEDIA_ENT_F_PROC_VIDEO_ENCODER)
597 video_set_drvdata(vfd, vpu);
599 ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
601 v4l2_err(&vpu->v4l2_dev, "Failed to register video device\n");
605 ret = hantro_attach_func(vpu, func);
607 v4l2_err(&vpu->v4l2_dev,
608 "Failed to attach functionality to the media device\n");
612 v4l2_info(&vpu->v4l2_dev, "registered %s as /dev/video%d\n", vfd->name,
618 video_unregister_device(vfd);
622 static int hantro_add_enc_func(struct hantro_dev *vpu)
624 if (!vpu->variant->enc_fmts)
627 return hantro_add_func(vpu, MEDIA_ENT_F_PROC_VIDEO_ENCODER);
630 static int hantro_add_dec_func(struct hantro_dev *vpu)
632 if (!vpu->variant->dec_fmts)
635 return hantro_add_func(vpu, MEDIA_ENT_F_PROC_VIDEO_DECODER);
638 static void hantro_remove_func(struct hantro_dev *vpu,
641 struct hantro_func *func;
643 if (funcid == MEDIA_ENT_F_PROC_VIDEO_ENCODER)
651 hantro_detach_func(func);
652 video_unregister_device(&func->vdev);
655 static void hantro_remove_enc_func(struct hantro_dev *vpu)
657 hantro_remove_func(vpu, MEDIA_ENT_F_PROC_VIDEO_ENCODER);
660 static void hantro_remove_dec_func(struct hantro_dev *vpu)
662 hantro_remove_func(vpu, MEDIA_ENT_F_PROC_VIDEO_DECODER);
665 static const struct media_device_ops hantro_m2m_media_ops = {
666 .req_validate = vb2_request_validate,
667 .req_queue = v4l2_m2m_request_queue,
670 static int hantro_probe(struct platform_device *pdev)
672 const struct of_device_id *match;
673 struct hantro_dev *vpu;
674 struct resource *res;
678 vpu = devm_kzalloc(&pdev->dev, sizeof(*vpu), GFP_KERNEL);
682 vpu->dev = &pdev->dev;
684 mutex_init(&vpu->vpu_mutex);
685 spin_lock_init(&vpu->irqlock);
687 match = of_match_node(of_hantro_match, pdev->dev.of_node);
688 vpu->variant = match->data;
690 INIT_DELAYED_WORK(&vpu->watchdog_work, hantro_watchdog);
692 vpu->clocks = devm_kcalloc(&pdev->dev, vpu->variant->num_clocks,
693 sizeof(*vpu->clocks), GFP_KERNEL);
697 for (i = 0; i < vpu->variant->num_clocks; i++)
698 vpu->clocks[i].id = vpu->variant->clk_names[i];
699 ret = devm_clk_bulk_get(&pdev->dev, vpu->variant->num_clocks,
704 num_bases = vpu->variant->num_regs ?: 1;
705 vpu->reg_bases = devm_kcalloc(&pdev->dev, num_bases,
706 sizeof(*vpu->reg_bases), GFP_KERNEL);
710 for (i = 0; i < num_bases; i++) {
711 res = vpu->variant->reg_names ?
712 platform_get_resource_byname(vpu->pdev, IORESOURCE_MEM,
713 vpu->variant->reg_names[i]) :
714 platform_get_resource(vpu->pdev, IORESOURCE_MEM, 0);
715 vpu->reg_bases[i] = devm_ioremap_resource(vpu->dev, res);
716 if (IS_ERR(vpu->reg_bases[i]))
717 return PTR_ERR(vpu->reg_bases[i]);
719 vpu->enc_base = vpu->reg_bases[0] + vpu->variant->enc_offset;
720 vpu->dec_base = vpu->reg_bases[0] + vpu->variant->dec_offset;
722 ret = dma_set_coherent_mask(vpu->dev, DMA_BIT_MASK(32));
724 dev_err(vpu->dev, "Could not set DMA coherent mask.\n");
728 for (i = 0; i < vpu->variant->num_irqs; i++) {
729 const char *irq_name = vpu->variant->irqs[i].name;
732 if (!vpu->variant->irqs[i].handler)
735 irq = platform_get_irq_byname(vpu->pdev, irq_name);
737 dev_err(vpu->dev, "Could not get %s IRQ.\n", irq_name);
741 ret = devm_request_irq(vpu->dev, irq,
742 vpu->variant->irqs[i].handler, 0,
743 dev_name(vpu->dev), vpu);
745 dev_err(vpu->dev, "Could not request %s IRQ.\n",
751 ret = vpu->variant->init(vpu);
753 dev_err(&pdev->dev, "Failed to init VPU hardware\n");
757 pm_runtime_set_autosuspend_delay(vpu->dev, 100);
758 pm_runtime_use_autosuspend(vpu->dev);
759 pm_runtime_enable(vpu->dev);
761 ret = clk_bulk_prepare(vpu->variant->num_clocks, vpu->clocks);
763 dev_err(&pdev->dev, "Failed to prepare clocks\n");
767 ret = v4l2_device_register(&pdev->dev, &vpu->v4l2_dev);
769 dev_err(&pdev->dev, "Failed to register v4l2 device\n");
770 goto err_clk_unprepare;
772 platform_set_drvdata(pdev, vpu);
774 vpu->m2m_dev = v4l2_m2m_init(&vpu_m2m_ops);
775 if (IS_ERR(vpu->m2m_dev)) {
776 v4l2_err(&vpu->v4l2_dev, "Failed to init mem2mem device\n");
777 ret = PTR_ERR(vpu->m2m_dev);
781 vpu->mdev.dev = vpu->dev;
782 strscpy(vpu->mdev.model, DRIVER_NAME, sizeof(vpu->mdev.model));
783 strscpy(vpu->mdev.bus_info, "platform: " DRIVER_NAME,
784 sizeof(vpu->mdev.model));
785 media_device_init(&vpu->mdev);
786 vpu->mdev.ops = &hantro_m2m_media_ops;
787 vpu->v4l2_dev.mdev = &vpu->mdev;
789 ret = hantro_add_enc_func(vpu);
791 dev_err(&pdev->dev, "Failed to register encoder\n");
795 ret = hantro_add_dec_func(vpu);
797 dev_err(&pdev->dev, "Failed to register decoder\n");
798 goto err_rm_enc_func;
801 ret = media_device_register(&vpu->mdev);
803 v4l2_err(&vpu->v4l2_dev, "Failed to register mem2mem media device\n");
804 goto err_rm_dec_func;
810 hantro_remove_dec_func(vpu);
812 hantro_remove_enc_func(vpu);
814 media_device_cleanup(&vpu->mdev);
815 v4l2_m2m_release(vpu->m2m_dev);
817 v4l2_device_unregister(&vpu->v4l2_dev);
819 clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
820 pm_runtime_dont_use_autosuspend(vpu->dev);
821 pm_runtime_disable(vpu->dev);
825 static int hantro_remove(struct platform_device *pdev)
827 struct hantro_dev *vpu = platform_get_drvdata(pdev);
829 v4l2_info(&vpu->v4l2_dev, "Removing %s\n", pdev->name);
831 media_device_unregister(&vpu->mdev);
832 hantro_remove_dec_func(vpu);
833 hantro_remove_enc_func(vpu);
834 media_device_cleanup(&vpu->mdev);
835 v4l2_m2m_release(vpu->m2m_dev);
836 v4l2_device_unregister(&vpu->v4l2_dev);
837 clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
838 pm_runtime_dont_use_autosuspend(vpu->dev);
839 pm_runtime_disable(vpu->dev);
844 static int hantro_runtime_resume(struct device *dev)
846 struct hantro_dev *vpu = dev_get_drvdata(dev);
848 if (vpu->variant->runtime_resume)
849 return vpu->variant->runtime_resume(vpu);
855 static const struct dev_pm_ops hantro_pm_ops = {
856 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
857 pm_runtime_force_resume)
858 SET_RUNTIME_PM_OPS(NULL, hantro_runtime_resume, NULL)
861 static struct platform_driver hantro_driver = {
862 .probe = hantro_probe,
863 .remove = hantro_remove,
866 .of_match_table = of_match_ptr(of_hantro_match),
867 .pm = &hantro_pm_ops,
870 module_platform_driver(hantro_driver);
872 MODULE_LICENSE("GPL v2");
873 MODULE_AUTHOR("Alpha Lin <Alpha.Lin@Rock-Chips.com>");
874 MODULE_AUTHOR("Tomasz Figa <tfiga@chromium.org>");
875 MODULE_AUTHOR("Ezequiel Garcia <ezequiel@collabora.com>");
876 MODULE_DESCRIPTION("Hantro VPU codec driver");