linux/drivers/staging/media/hantro/hantro_drv.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Hantro VPU codec driver
   4 *
   5 * Copyright (C) 2018 Collabora, Ltd.
   6 * Copyright 2018 Google LLC.
   7 *      Tomasz Figa <tfiga@chromium.org>
   8 *
   9 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
  10 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  11 */
  12
  13#include <linux/clk.h>
  14#include <linux/module.h>
  15#include <linux/of.h>
  16#include <linux/platform_device.h>
  17#include <linux/pm.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>
  26
  27#include "hantro_v4l2.h"
  28#include "hantro.h"
  29#include "hantro_hw.h"
  30
  31#define DRIVER_NAME "hantro-vpu"
  32
  33int hantro_debug;
  34module_param_named(debug, hantro_debug, int, 0644);
  35MODULE_PARM_DESC(debug,
  36                 "Debug level - higher value produces more verbose messages");
  37
  38void *hantro_get_ctrl(struct hantro_ctx *ctx, u32 id)
  39{
  40        struct v4l2_ctrl *ctrl;
  41
  42        ctrl = v4l2_ctrl_find(&ctx->ctrl_handler, id);
  43        return ctrl ? ctrl->p_cur.p : NULL;
  44}
  45
  46dma_addr_t hantro_get_ref(struct vb2_queue *q, u64 ts)
  47{
  48        struct vb2_buffer *buf;
  49        int index;
  50
  51        index = vb2_find_timestamp(q, ts, 0);
  52        if (index < 0)
  53                return 0;
  54        buf = vb2_get_buffer(q, index);
  55        return vb2_dma_contig_plane_dma_addr(buf, 0);
  56}
  57
  58static int
  59hantro_enc_buf_finish(struct hantro_ctx *ctx, struct vb2_buffer *buf,
  60                      unsigned int bytesused)
  61{
  62        size_t avail_size;
  63
  64        avail_size = vb2_plane_size(buf, 0) - ctx->vpu_dst_fmt->header_size;
  65        if (bytesused > avail_size)
  66                return -EINVAL;
  67        /*
  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.
  71         */
  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);
  76        }
  77        buf->planes[0].bytesused =
  78                ctx->vpu_dst_fmt->header_size + bytesused;
  79        return 0;
  80}
  81
  82static int
  83hantro_dec_buf_finish(struct hantro_ctx *ctx, struct vb2_buffer *buf,
  84                      unsigned int bytesused)
  85{
  86        /* For decoders set bytesused as per the output picture. */
  87        buf->planes[0].bytesused = ctx->dst_fmt.plane_fmt[0].sizeimage;
  88        return 0;
  89}
  90
  91static void hantro_job_finish(struct hantro_dev *vpu,
  92                              struct hantro_ctx *ctx,
  93                              unsigned int bytesused,
  94                              enum vb2_buffer_state result)
  95{
  96        struct vb2_v4l2_buffer *src, *dst;
  97        int ret;
  98
  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);
 102
 103        src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 104        dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 105
 106        if (WARN_ON(!src))
 107                return;
 108        if (WARN_ON(!dst))
 109                return;
 110
 111        src->sequence = ctx->sequence_out++;
 112        dst->sequence = ctx->sequence_cap++;
 113
 114        v4l2_m2m_buf_copy_metadata(src, dst, true);
 115
 116        ret = ctx->buf_finish(ctx, &dst->vb2_buf, bytesused);
 117        if (ret)
 118                result = VB2_BUF_STATE_ERROR;
 119
 120        v4l2_m2m_buf_done(src, result);
 121        v4l2_m2m_buf_done(dst, result);
 122
 123        v4l2_m2m_job_finish(vpu->m2m_dev, ctx->fh.m2m_ctx);
 124}
 125
 126void hantro_irq_done(struct hantro_dev *vpu, unsigned int bytesused,
 127                     enum vb2_buffer_state result)
 128{
 129        struct hantro_ctx *ctx =
 130                v4l2_m2m_get_curr_priv(vpu->m2m_dev);
 131
 132        /*
 133         * If cancel_delayed_work returns false
 134         * the timeout expired. The watchdog is running,
 135         * and will take care of finishing the job.
 136         */
 137        if (cancel_delayed_work(&vpu->watchdog_work))
 138                hantro_job_finish(vpu, ctx, bytesused, result);
 139}
 140
 141void hantro_watchdog(struct work_struct *work)
 142{
 143        struct hantro_dev *vpu;
 144        struct hantro_ctx *ctx;
 145
 146        vpu = container_of(to_delayed_work(work),
 147                           struct hantro_dev, watchdog_work);
 148        ctx = v4l2_m2m_get_curr_priv(vpu->m2m_dev);
 149        if (ctx) {
 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);
 153        }
 154}
 155
 156static void device_run(void *priv)
 157{
 158        struct hantro_ctx *ctx = priv;
 159        int ret;
 160
 161        ret = clk_bulk_enable(ctx->dev->variant->num_clocks, ctx->dev->clocks);
 162        if (ret)
 163                goto err_cancel_job;
 164        ret = pm_runtime_get_sync(ctx->dev->dev);
 165        if (ret < 0)
 166                goto err_cancel_job;
 167
 168        ctx->codec_ops->run(ctx);
 169        return;
 170
 171err_cancel_job:
 172        hantro_job_finish(ctx->dev, ctx, 0, VB2_BUF_STATE_ERROR);
 173}
 174
 175bool hantro_is_encoder_ctx(const struct hantro_ctx *ctx)
 176{
 177        return ctx->buf_finish == hantro_enc_buf_finish;
 178}
 179
 180static struct v4l2_m2m_ops vpu_m2m_ops = {
 181        .device_run = device_run,
 182};
 183
 184static int
 185queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
 186{
 187        struct hantro_ctx *ctx = priv;
 188        int ret;
 189
 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;
 195
 196        /*
 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.
 200         */
 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;
 208
 209        ret = vb2_queue_init(src_vq);
 210        if (ret)
 211                return ret;
 212
 213        /*
 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.
 217         *
 218         * For the DMA destination buffer, we use a bounce buffer.
 219         */
 220        if (hantro_is_encoder_ctx(ctx)) {
 221                dst_vq->mem_ops = &vb2_vmalloc_memops;
 222        } else {
 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;
 227        }
 228
 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;
 237
 238        return vb2_queue_init(dst_vq);
 239}
 240
 241static int hantro_s_ctrl(struct v4l2_ctrl *ctrl)
 242{
 243        struct hantro_ctx *ctx;
 244
 245        ctx = container_of(ctrl->handler,
 246                           struct hantro_ctx, ctrl_handler);
 247
 248        vpu_debug(1, "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
 249
 250        switch (ctrl->id) {
 251        case V4L2_CID_JPEG_COMPRESSION_QUALITY:
 252                ctx->jpeg_quality = ctrl->val;
 253                break;
 254        default:
 255                return -EINVAL;
 256        }
 257
 258        return 0;
 259}
 260
 261static const struct v4l2_ctrl_ops hantro_ctrl_ops = {
 262        .s_ctrl = hantro_s_ctrl,
 263};
 264
 265static struct hantro_ctrl controls[] = {
 266        {
 267                .id = V4L2_CID_JPEG_COMPRESSION_QUALITY,
 268                .codec = HANTRO_JPEG_ENCODER,
 269                .cfg = {
 270                        .min = 5,
 271                        .max = 100,
 272                        .step = 1,
 273                        .def = 50,
 274                },
 275        }, {
 276                .id = V4L2_CID_MPEG_VIDEO_MPEG2_SLICE_PARAMS,
 277                .codec = HANTRO_MPEG2_DECODER,
 278                .cfg = {
 279                        .elem_size = sizeof(struct v4l2_ctrl_mpeg2_slice_params),
 280                },
 281        }, {
 282                .id = V4L2_CID_MPEG_VIDEO_MPEG2_QUANTIZATION,
 283                .codec = HANTRO_MPEG2_DECODER,
 284                .cfg = {
 285                        .elem_size = sizeof(struct v4l2_ctrl_mpeg2_quantization),
 286                },
 287        },
 288};
 289
 290static int hantro_ctrls_setup(struct hantro_dev *vpu,
 291                              struct hantro_ctx *ctx,
 292                              int allowed_codecs)
 293{
 294        int i, num_ctrls = ARRAY_SIZE(controls);
 295
 296        v4l2_ctrl_handler_init(&ctx->ctrl_handler, num_ctrls);
 297
 298        for (i = 0; i < num_ctrls; i++) {
 299                if (!(allowed_codecs & controls[i].codec))
 300                        continue;
 301                if (!controls[i].cfg.elem_size) {
 302                        v4l2_ctrl_new_std(&ctx->ctrl_handler,
 303                                          &hantro_ctrl_ops,
 304                                          controls[i].id, controls[i].cfg.min,
 305                                          controls[i].cfg.max,
 306                                          controls[i].cfg.step,
 307                                          controls[i].cfg.def);
 308                } else {
 309                        controls[i].cfg.id = controls[i].id;
 310                        v4l2_ctrl_new_custom(&ctx->ctrl_handler,
 311                                             &controls[i].cfg, NULL);
 312                }
 313
 314                if (ctx->ctrl_handler.error) {
 315                        vpu_err("Adding control (%d) failed %d\n",
 316                                controls[i].id,
 317                                ctx->ctrl_handler.error);
 318                        v4l2_ctrl_handler_free(&ctx->ctrl_handler);
 319                        return ctx->ctrl_handler.error;
 320                }
 321        }
 322        return v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
 323}
 324
 325/*
 326 * V4L2 file operations.
 327 */
 328
 329static int hantro_open(struct file *filp)
 330{
 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;
 336
 337        /*
 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.
 344         */
 345
 346        ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 347        if (!ctx)
 348                return -ENOMEM;
 349
 350        ctx->dev = vpu;
 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,
 355                                                    queue_init);
 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,
 360                                                    queue_init);
 361        } else {
 362                ctx->fh.m2m_ctx = ERR_PTR(-ENODEV);
 363        }
 364        if (IS_ERR(ctx->fh.m2m_ctx)) {
 365                ret = PTR_ERR(ctx->fh.m2m_ctx);
 366                kfree(ctx);
 367                return ret;
 368        }
 369
 370        v4l2_fh_init(&ctx->fh, vdev);
 371        filp->private_data = &ctx->fh;
 372        v4l2_fh_add(&ctx->fh);
 373
 374        hantro_reset_fmts(ctx);
 375
 376        ret = hantro_ctrls_setup(vpu, ctx, allowed_codecs);
 377        if (ret) {
 378                vpu_err("Failed to set up controls\n");
 379                goto err_fh_free;
 380        }
 381        ctx->fh.ctrl_handler = &ctx->ctrl_handler;
 382
 383        return 0;
 384
 385err_fh_free:
 386        v4l2_fh_del(&ctx->fh);
 387        v4l2_fh_exit(&ctx->fh);
 388        kfree(ctx);
 389        return ret;
 390}
 391
 392static int hantro_release(struct file *filp)
 393{
 394        struct hantro_ctx *ctx =
 395                container_of(filp->private_data, struct hantro_ctx, fh);
 396
 397        /*
 398         * No need for extra locking because this was the last reference
 399         * to this file.
 400         */
 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);
 405        kfree(ctx);
 406
 407        return 0;
 408}
 409
 410static const struct v4l2_file_operations hantro_fops = {
 411        .owner = THIS_MODULE,
 412        .open = hantro_open,
 413        .release = hantro_release,
 414        .poll = v4l2_m2m_fop_poll,
 415        .unlocked_ioctl = video_ioctl2,
 416        .mmap = v4l2_m2m_fop_mmap,
 417};
 418
 419static 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, },
 423#endif
 424        { /* sentinel */ }
 425};
 426MODULE_DEVICE_TABLE(of, of_hantro_match);
 427
 428static 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)
 433{
 434        char *name;
 435        int ret;
 436
 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;
 441        }
 442
 443        name = devm_kasprintf(mdev->dev, GFP_KERNEL, "%s-%s", vdev->name,
 444                              entity_name);
 445        if (!name)
 446                return -ENOMEM;
 447
 448        entity->name = name;
 449        entity->function = function;
 450
 451        ret = media_entity_pads_init(entity, num_pads, pads);
 452        if (ret)
 453                return ret;
 454
 455        ret = media_device_register_entity(mdev, entity);
 456        if (ret)
 457                return ret;
 458
 459        return 0;
 460}
 461
 462static int hantro_attach_func(struct hantro_dev *vpu,
 463                              struct hantro_func *func)
 464{
 465        struct media_device *mdev = &vpu->mdev;
 466        struct media_link *link;
 467        int ret;
 468
 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,
 473                                     &func->vdev);
 474        if (ret)
 475                return ret;
 476
 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,
 481                                     &func->vdev);
 482        if (ret)
 483                goto err_rel_entity0;
 484
 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,
 488                                     &func->vdev);
 489        if (ret)
 490                goto err_rel_entity1;
 491
 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);
 496        if (ret)
 497                goto err_rel_entity2;
 498
 499        ret = media_create_pad_link(&func->proc, 0, &func->sink, 0,
 500                                    MEDIA_LNK_FL_IMMUTABLE |
 501                                    MEDIA_LNK_FL_ENABLED);
 502        if (ret)
 503                goto err_rm_links0;
 504
 505        /* Create video interface */
 506        func->intf_devnode = media_devnode_create(mdev, MEDIA_INTF_T_V4L_VIDEO,
 507                                                  0, VIDEO_MAJOR,
 508                                                  func->vdev.minor);
 509        if (!func->intf_devnode) {
 510                ret = -ENOMEM;
 511                goto err_rm_links1;
 512        }
 513
 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);
 519        if (!link) {
 520                ret = -ENOMEM;
 521                goto err_rm_devnode;
 522        }
 523
 524        link = media_create_intf_link(&func->sink, &func->intf_devnode->intf,
 525                                      MEDIA_LNK_FL_IMMUTABLE |
 526                                      MEDIA_LNK_FL_ENABLED);
 527        if (!link) {
 528                ret = -ENOMEM;
 529                goto err_rm_devnode;
 530        }
 531        return 0;
 532
 533err_rm_devnode:
 534        media_devnode_remove(func->intf_devnode);
 535
 536err_rm_links1:
 537        media_entity_remove_links(&func->sink);
 538
 539err_rm_links0:
 540        media_entity_remove_links(&func->proc);
 541        media_entity_remove_links(&func->vdev.entity);
 542
 543err_rel_entity2:
 544        media_device_unregister_entity(&func->sink);
 545
 546err_rel_entity1:
 547        media_device_unregister_entity(&func->proc);
 548
 549err_rel_entity0:
 550        media_device_unregister_entity(&func->vdev.entity);
 551        return ret;
 552}
 553
 554static void hantro_detach_func(struct hantro_func *func)
 555{
 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);
 563}
 564
 565static int hantro_add_func(struct hantro_dev *vpu, unsigned int funcid)
 566{
 567        const struct of_device_id *match;
 568        struct hantro_func *func;
 569        struct video_device *vfd;
 570        int ret;
 571
 572        match = of_match_node(of_hantro_match, vpu->dev->of_node);
 573        func = devm_kzalloc(vpu->dev, sizeof(*func), GFP_KERNEL);
 574        if (!func) {
 575                v4l2_err(&vpu->v4l2_dev, "Failed to allocate video device\n");
 576                return -ENOMEM;
 577        }
 578
 579        func->id = funcid;
 580
 581        vfd = &func->vdev;
 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");
 591
 592        if (funcid == MEDIA_ENT_F_PROC_VIDEO_ENCODER)
 593                vpu->encoder = func;
 594        else
 595                vpu->decoder = func;
 596
 597        video_set_drvdata(vfd, vpu);
 598
 599        ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
 600        if (ret) {
 601                v4l2_err(&vpu->v4l2_dev, "Failed to register video device\n");
 602                return ret;
 603        }
 604
 605        ret = hantro_attach_func(vpu, func);
 606        if (ret) {
 607                v4l2_err(&vpu->v4l2_dev,
 608                         "Failed to attach functionality to the media device\n");
 609                goto err_unreg_dev;
 610        }
 611
 612        v4l2_info(&vpu->v4l2_dev, "registered %s as /dev/video%d\n", vfd->name,
 613                  vfd->num);
 614
 615        return 0;
 616
 617err_unreg_dev:
 618        video_unregister_device(vfd);
 619        return ret;
 620}
 621
 622static int hantro_add_enc_func(struct hantro_dev *vpu)
 623{
 624        if (!vpu->variant->enc_fmts)
 625                return 0;
 626
 627        return hantro_add_func(vpu, MEDIA_ENT_F_PROC_VIDEO_ENCODER);
 628}
 629
 630static int hantro_add_dec_func(struct hantro_dev *vpu)
 631{
 632        if (!vpu->variant->dec_fmts)
 633                return 0;
 634
 635        return hantro_add_func(vpu, MEDIA_ENT_F_PROC_VIDEO_DECODER);
 636}
 637
 638static void hantro_remove_func(struct hantro_dev *vpu,
 639                               unsigned int funcid)
 640{
 641        struct hantro_func *func;
 642
 643        if (funcid == MEDIA_ENT_F_PROC_VIDEO_ENCODER)
 644                func = vpu->encoder;
 645        else
 646                func = vpu->decoder;
 647
 648        if (!func)
 649                return;
 650
 651        hantro_detach_func(func);
 652        video_unregister_device(&func->vdev);
 653}
 654
 655static void hantro_remove_enc_func(struct hantro_dev *vpu)
 656{
 657        hantro_remove_func(vpu, MEDIA_ENT_F_PROC_VIDEO_ENCODER);
 658}
 659
 660static void hantro_remove_dec_func(struct hantro_dev *vpu)
 661{
 662        hantro_remove_func(vpu, MEDIA_ENT_F_PROC_VIDEO_DECODER);
 663}
 664
 665static const struct media_device_ops hantro_m2m_media_ops = {
 666        .req_validate = vb2_request_validate,
 667        .req_queue = v4l2_m2m_request_queue,
 668};
 669
 670static int hantro_probe(struct platform_device *pdev)
 671{
 672        const struct of_device_id *match;
 673        struct hantro_dev *vpu;
 674        struct resource *res;
 675        int num_bases;
 676        int i, ret;
 677
 678        vpu = devm_kzalloc(&pdev->dev, sizeof(*vpu), GFP_KERNEL);
 679        if (!vpu)
 680                return -ENOMEM;
 681
 682        vpu->dev = &pdev->dev;
 683        vpu->pdev = pdev;
 684        mutex_init(&vpu->vpu_mutex);
 685        spin_lock_init(&vpu->irqlock);
 686
 687        match = of_match_node(of_hantro_match, pdev->dev.of_node);
 688        vpu->variant = match->data;
 689
 690        INIT_DELAYED_WORK(&vpu->watchdog_work, hantro_watchdog);
 691
 692        vpu->clocks = devm_kcalloc(&pdev->dev, vpu->variant->num_clocks,
 693                                   sizeof(*vpu->clocks), GFP_KERNEL);
 694        if (!vpu->clocks)
 695                return -ENOMEM;
 696
 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,
 700                                vpu->clocks);
 701        if (ret)
 702                return ret;
 703
 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);
 707        if (!vpu->reg_bases)
 708                return -ENOMEM;
 709
 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]);
 718        }
 719        vpu->enc_base = vpu->reg_bases[0] + vpu->variant->enc_offset;
 720        vpu->dec_base = vpu->reg_bases[0] + vpu->variant->dec_offset;
 721
 722        ret = dma_set_coherent_mask(vpu->dev, DMA_BIT_MASK(32));
 723        if (ret) {
 724                dev_err(vpu->dev, "Could not set DMA coherent mask.\n");
 725                return ret;
 726        }
 727
 728        for (i = 0; i < vpu->variant->num_irqs; i++) {
 729                const char *irq_name = vpu->variant->irqs[i].name;
 730                int irq;
 731
 732                if (!vpu->variant->irqs[i].handler)
 733                        continue;
 734
 735                irq = platform_get_irq_byname(vpu->pdev, irq_name);
 736                if (irq <= 0) {
 737                        dev_err(vpu->dev, "Could not get %s IRQ.\n", irq_name);
 738                        return -ENXIO;
 739                }
 740
 741                ret = devm_request_irq(vpu->dev, irq,
 742                                       vpu->variant->irqs[i].handler, 0,
 743                                       dev_name(vpu->dev), vpu);
 744                if (ret) {
 745                        dev_err(vpu->dev, "Could not request %s IRQ.\n",
 746                                irq_name);
 747                        return ret;
 748                }
 749        }
 750
 751        ret = vpu->variant->init(vpu);
 752        if (ret) {
 753                dev_err(&pdev->dev, "Failed to init VPU hardware\n");
 754                return ret;
 755        }
 756
 757        pm_runtime_set_autosuspend_delay(vpu->dev, 100);
 758        pm_runtime_use_autosuspend(vpu->dev);
 759        pm_runtime_enable(vpu->dev);
 760
 761        ret = clk_bulk_prepare(vpu->variant->num_clocks, vpu->clocks);
 762        if (ret) {
 763                dev_err(&pdev->dev, "Failed to prepare clocks\n");
 764                return ret;
 765        }
 766
 767        ret = v4l2_device_register(&pdev->dev, &vpu->v4l2_dev);
 768        if (ret) {
 769                dev_err(&pdev->dev, "Failed to register v4l2 device\n");
 770                goto err_clk_unprepare;
 771        }
 772        platform_set_drvdata(pdev, vpu);
 773
 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);
 778                goto err_v4l2_unreg;
 779        }
 780
 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;
 788
 789        ret = hantro_add_enc_func(vpu);
 790        if (ret) {
 791                dev_err(&pdev->dev, "Failed to register encoder\n");
 792                goto err_m2m_rel;
 793        }
 794
 795        ret = hantro_add_dec_func(vpu);
 796        if (ret) {
 797                dev_err(&pdev->dev, "Failed to register decoder\n");
 798                goto err_rm_enc_func;
 799        }
 800
 801        ret = media_device_register(&vpu->mdev);
 802        if (ret) {
 803                v4l2_err(&vpu->v4l2_dev, "Failed to register mem2mem media device\n");
 804                goto err_rm_dec_func;
 805        }
 806
 807        return 0;
 808
 809err_rm_dec_func:
 810        hantro_remove_dec_func(vpu);
 811err_rm_enc_func:
 812        hantro_remove_enc_func(vpu);
 813err_m2m_rel:
 814        media_device_cleanup(&vpu->mdev);
 815        v4l2_m2m_release(vpu->m2m_dev);
 816err_v4l2_unreg:
 817        v4l2_device_unregister(&vpu->v4l2_dev);
 818err_clk_unprepare:
 819        clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
 820        pm_runtime_dont_use_autosuspend(vpu->dev);
 821        pm_runtime_disable(vpu->dev);
 822        return ret;
 823}
 824
 825static int hantro_remove(struct platform_device *pdev)
 826{
 827        struct hantro_dev *vpu = platform_get_drvdata(pdev);
 828
 829        v4l2_info(&vpu->v4l2_dev, "Removing %s\n", pdev->name);
 830
 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);
 840        return 0;
 841}
 842
 843#ifdef CONFIG_PM
 844static int hantro_runtime_resume(struct device *dev)
 845{
 846        struct hantro_dev *vpu = dev_get_drvdata(dev);
 847
 848        if (vpu->variant->runtime_resume)
 849                return vpu->variant->runtime_resume(vpu);
 850
 851        return 0;
 852}
 853#endif
 854
 855static 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)
 859};
 860
 861static struct platform_driver hantro_driver = {
 862        .probe = hantro_probe,
 863        .remove = hantro_remove,
 864        .driver = {
 865                   .name = DRIVER_NAME,
 866                   .of_match_table = of_match_ptr(of_hantro_match),
 867                   .pm = &hantro_pm_ops,
 868        },
 869};
 870module_platform_driver(hantro_driver);
 871
 872MODULE_LICENSE("GPL v2");
 873MODULE_AUTHOR("Alpha Lin <Alpha.Lin@Rock-Chips.com>");
 874MODULE_AUTHOR("Tomasz Figa <tfiga@chromium.org>");
 875MODULE_AUTHOR("Ezequiel Garcia <ezequiel@collabora.com>");
 876MODULE_DESCRIPTION("Hantro VPU codec driver");
 877