linux/drivers/media/platform/qcom/venus/venc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
   4 * Copyright (C) 2017 Linaro Ltd.
   5 */
   6#include <linux/clk.h>
   7#include <linux/module.h>
   8#include <linux/mod_devicetable.h>
   9#include <linux/platform_device.h>
  10#include <linux/pm_runtime.h>
  11#include <linux/slab.h>
  12#include <media/v4l2-mem2mem.h>
  13#include <media/videobuf2-dma-sg.h>
  14#include <media/v4l2-ioctl.h>
  15#include <media/v4l2-event.h>
  16#include <media/v4l2-ctrls.h>
  17
  18#include "hfi_venus_io.h"
  19#include "hfi_parser.h"
  20#include "core.h"
  21#include "helpers.h"
  22#include "venc.h"
  23#include "pm_helpers.h"
  24
  25#define NUM_B_FRAMES_MAX        4
  26
  27/*
  28 * Three resons to keep MPLANE formats (despite that the number of planes
  29 * currently is one):
  30 * - the MPLANE formats allow only one plane to be used
  31 * - the downstream driver use MPLANE formats too
  32 * - future firmware versions could add support for >1 planes
  33 */
  34static const struct venus_format venc_formats[] = {
  35        {
  36                .pixfmt = V4L2_PIX_FMT_NV12,
  37                .num_planes = 1,
  38                .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  39        }, {
  40                .pixfmt = V4L2_PIX_FMT_MPEG4,
  41                .num_planes = 1,
  42                .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  43        }, {
  44                .pixfmt = V4L2_PIX_FMT_H263,
  45                .num_planes = 1,
  46                .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  47        }, {
  48                .pixfmt = V4L2_PIX_FMT_H264,
  49                .num_planes = 1,
  50                .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  51        }, {
  52                .pixfmt = V4L2_PIX_FMT_VP8,
  53                .num_planes = 1,
  54                .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  55        }, {
  56                .pixfmt = V4L2_PIX_FMT_HEVC,
  57                .num_planes = 1,
  58                .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
  59        },
  60};
  61
  62static const struct venus_format *
  63find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
  64{
  65        const struct venus_format *fmt = venc_formats;
  66        unsigned int size = ARRAY_SIZE(venc_formats);
  67        unsigned int i;
  68
  69        for (i = 0; i < size; i++) {
  70                if (fmt[i].pixfmt == pixfmt)
  71                        break;
  72        }
  73
  74        if (i == size || fmt[i].type != type)
  75                return NULL;
  76
  77        if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  78            !venus_helper_check_codec(inst, fmt[i].pixfmt))
  79                return NULL;
  80
  81        return &fmt[i];
  82}
  83
  84static const struct venus_format *
  85find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
  86{
  87        const struct venus_format *fmt = venc_formats;
  88        unsigned int size = ARRAY_SIZE(venc_formats);
  89        unsigned int i, k = 0;
  90
  91        if (index > size)
  92                return NULL;
  93
  94        for (i = 0; i < size; i++) {
  95                bool valid;
  96
  97                if (fmt[i].type != type)
  98                        continue;
  99                valid = type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
 100                        venus_helper_check_codec(inst, fmt[i].pixfmt);
 101                if (k == index && valid)
 102                        break;
 103                if (valid)
 104                        k++;
 105        }
 106
 107        if (i == size)
 108                return NULL;
 109
 110        return &fmt[i];
 111}
 112
 113static int venc_v4l2_to_hfi(int id, int value)
 114{
 115        switch (id) {
 116        case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
 117                switch (value) {
 118                case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0:
 119                default:
 120                        return HFI_MPEG4_LEVEL_0;
 121                case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B:
 122                        return HFI_MPEG4_LEVEL_0b;
 123                case V4L2_MPEG_VIDEO_MPEG4_LEVEL_1:
 124                        return HFI_MPEG4_LEVEL_1;
 125                case V4L2_MPEG_VIDEO_MPEG4_LEVEL_2:
 126                        return HFI_MPEG4_LEVEL_2;
 127                case V4L2_MPEG_VIDEO_MPEG4_LEVEL_3:
 128                        return HFI_MPEG4_LEVEL_3;
 129                case V4L2_MPEG_VIDEO_MPEG4_LEVEL_4:
 130                        return HFI_MPEG4_LEVEL_4;
 131                case V4L2_MPEG_VIDEO_MPEG4_LEVEL_5:
 132                        return HFI_MPEG4_LEVEL_5;
 133                }
 134        case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
 135                switch (value) {
 136                case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
 137                default:
 138                        return HFI_MPEG4_PROFILE_SIMPLE;
 139                case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
 140                        return HFI_MPEG4_PROFILE_ADVANCEDSIMPLE;
 141                }
 142        case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
 143                switch (value) {
 144                case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
 145                        return HFI_H264_PROFILE_BASELINE;
 146                case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
 147                        return HFI_H264_PROFILE_CONSTRAINED_BASE;
 148                case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
 149                        return HFI_H264_PROFILE_MAIN;
 150                case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
 151                default:
 152                        return HFI_H264_PROFILE_HIGH;
 153                }
 154        case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
 155                switch (value) {
 156                case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
 157                        return HFI_H264_LEVEL_1;
 158                case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
 159                        return HFI_H264_LEVEL_1b;
 160                case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
 161                        return HFI_H264_LEVEL_11;
 162                case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
 163                        return HFI_H264_LEVEL_12;
 164                case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
 165                        return HFI_H264_LEVEL_13;
 166                case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
 167                        return HFI_H264_LEVEL_2;
 168                case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
 169                        return HFI_H264_LEVEL_21;
 170                case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
 171                        return HFI_H264_LEVEL_22;
 172                case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
 173                        return HFI_H264_LEVEL_3;
 174                case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
 175                        return HFI_H264_LEVEL_31;
 176                case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
 177                        return HFI_H264_LEVEL_32;
 178                case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
 179                        return HFI_H264_LEVEL_4;
 180                case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
 181                        return HFI_H264_LEVEL_41;
 182                case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
 183                        return HFI_H264_LEVEL_42;
 184                case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
 185                default:
 186                        return HFI_H264_LEVEL_5;
 187                case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
 188                        return HFI_H264_LEVEL_51;
 189                }
 190        case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
 191                switch (value) {
 192                case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC:
 193                default:
 194                        return HFI_H264_ENTROPY_CAVLC;
 195                case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC:
 196                        return HFI_H264_ENTROPY_CABAC;
 197                }
 198        case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
 199                switch (value) {
 200                case 0:
 201                default:
 202                        return HFI_VPX_PROFILE_VERSION_0;
 203                case 1:
 204                        return HFI_VPX_PROFILE_VERSION_1;
 205                case 2:
 206                        return HFI_VPX_PROFILE_VERSION_2;
 207                case 3:
 208                        return HFI_VPX_PROFILE_VERSION_3;
 209                }
 210        case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
 211                switch (value) {
 212                case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED:
 213                default:
 214                        return HFI_H264_DB_MODE_ALL_BOUNDARY;
 215                case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED:
 216                        return HFI_H264_DB_MODE_DISABLE;
 217                case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY:
 218                        return HFI_H264_DB_MODE_SKIP_SLICE_BOUNDARY;
 219                }
 220        case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
 221                switch (value) {
 222                case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
 223                default:
 224                        return HFI_HEVC_PROFILE_MAIN;
 225                case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
 226                        return HFI_HEVC_PROFILE_MAIN_STILL_PIC;
 227                case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
 228                        return HFI_HEVC_PROFILE_MAIN10;
 229                }
 230        case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
 231                switch (value) {
 232                case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
 233                default:
 234                        return HFI_HEVC_LEVEL_1;
 235                case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
 236                        return HFI_HEVC_LEVEL_2;
 237                case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
 238                        return HFI_HEVC_LEVEL_21;
 239                case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
 240                        return HFI_HEVC_LEVEL_3;
 241                case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
 242                        return HFI_HEVC_LEVEL_31;
 243                case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
 244                        return HFI_HEVC_LEVEL_4;
 245                case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
 246                        return HFI_HEVC_LEVEL_41;
 247                case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
 248                        return HFI_HEVC_LEVEL_5;
 249                case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
 250                        return HFI_HEVC_LEVEL_51;
 251                case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2:
 252                        return HFI_HEVC_LEVEL_52;
 253                case V4L2_MPEG_VIDEO_HEVC_LEVEL_6:
 254                        return HFI_HEVC_LEVEL_6;
 255                case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1:
 256                        return HFI_HEVC_LEVEL_61;
 257                case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2:
 258                        return HFI_HEVC_LEVEL_62;
 259                }
 260        }
 261
 262        return 0;
 263}
 264
 265static int
 266venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
 267{
 268        strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
 269        strscpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card));
 270        strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
 271
 272        return 0;
 273}
 274
 275static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
 276{
 277        struct venus_inst *inst = to_inst(file);
 278        const struct venus_format *fmt;
 279
 280        fmt = find_format_by_index(inst, f->index, f->type);
 281
 282        memset(f->reserved, 0, sizeof(f->reserved));
 283
 284        if (!fmt)
 285                return -EINVAL;
 286
 287        f->pixelformat = fmt->pixfmt;
 288
 289        return 0;
 290}
 291
 292static const struct venus_format *
 293venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
 294{
 295        struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
 296        struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
 297        const struct venus_format *fmt;
 298        u32 sizeimage;
 299
 300        memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
 301        memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
 302
 303        fmt = find_format(inst, pixmp->pixelformat, f->type);
 304        if (!fmt) {
 305                if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 306                        pixmp->pixelformat = V4L2_PIX_FMT_H264;
 307                else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 308                        pixmp->pixelformat = V4L2_PIX_FMT_NV12;
 309                else
 310                        return NULL;
 311                fmt = find_format(inst, pixmp->pixelformat, f->type);
 312        }
 313
 314        pixmp->width = clamp(pixmp->width, frame_width_min(inst),
 315                             frame_width_max(inst));
 316        pixmp->height = clamp(pixmp->height, frame_height_min(inst),
 317                              frame_height_max(inst));
 318
 319        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 320                pixmp->height = ALIGN(pixmp->height, 32);
 321
 322        pixmp->width = ALIGN(pixmp->width, 2);
 323        pixmp->height = ALIGN(pixmp->height, 2);
 324
 325        if (pixmp->field == V4L2_FIELD_ANY)
 326                pixmp->field = V4L2_FIELD_NONE;
 327        pixmp->num_planes = fmt->num_planes;
 328        pixmp->flags = 0;
 329
 330        sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
 331                                             pixmp->width,
 332                                             pixmp->height);
 333        pfmt[0].sizeimage = max(ALIGN(pfmt[0].sizeimage, SZ_4K), sizeimage);
 334
 335        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 336                pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
 337        else
 338                pfmt[0].bytesperline = 0;
 339
 340        return fmt;
 341}
 342
 343static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
 344{
 345        struct venus_inst *inst = to_inst(file);
 346
 347        venc_try_fmt_common(inst, f);
 348
 349        return 0;
 350}
 351
 352static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
 353{
 354        struct venus_inst *inst = to_inst(file);
 355        struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
 356        struct v4l2_pix_format_mplane orig_pixmp;
 357        const struct venus_format *fmt;
 358        struct v4l2_format format;
 359        u32 pixfmt_out = 0, pixfmt_cap = 0;
 360
 361        orig_pixmp = *pixmp;
 362
 363        fmt = venc_try_fmt_common(inst, f);
 364        if (!fmt)
 365                return -EINVAL;
 366
 367        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 368                pixfmt_out = pixmp->pixelformat;
 369                pixfmt_cap = inst->fmt_cap->pixfmt;
 370        } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 371                pixfmt_cap = pixmp->pixelformat;
 372                pixfmt_out = inst->fmt_out->pixfmt;
 373        }
 374
 375        memset(&format, 0, sizeof(format));
 376
 377        format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
 378        format.fmt.pix_mp.pixelformat = pixfmt_out;
 379        format.fmt.pix_mp.width = orig_pixmp.width;
 380        format.fmt.pix_mp.height = orig_pixmp.height;
 381        venc_try_fmt_common(inst, &format);
 382
 383        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 384                inst->out_width = format.fmt.pix_mp.width;
 385                inst->out_height = format.fmt.pix_mp.height;
 386                inst->colorspace = pixmp->colorspace;
 387                inst->ycbcr_enc = pixmp->ycbcr_enc;
 388                inst->quantization = pixmp->quantization;
 389                inst->xfer_func = pixmp->xfer_func;
 390        }
 391
 392        memset(&format, 0, sizeof(format));
 393
 394        format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
 395        format.fmt.pix_mp.pixelformat = pixfmt_cap;
 396        format.fmt.pix_mp.width = orig_pixmp.width;
 397        format.fmt.pix_mp.height = orig_pixmp.height;
 398        venc_try_fmt_common(inst, &format);
 399
 400        inst->width = format.fmt.pix_mp.width;
 401        inst->height = format.fmt.pix_mp.height;
 402
 403        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 404                inst->fmt_out = fmt;
 405        else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 406                inst->fmt_cap = fmt;
 407                inst->output_buf_size = pixmp->plane_fmt[0].sizeimage;
 408        }
 409
 410        return 0;
 411}
 412
 413static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
 414{
 415        struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
 416        struct venus_inst *inst = to_inst(file);
 417        const struct venus_format *fmt;
 418
 419        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 420                fmt = inst->fmt_cap;
 421        else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 422                fmt = inst->fmt_out;
 423        else
 424                return -EINVAL;
 425
 426        pixmp->pixelformat = fmt->pixfmt;
 427
 428        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 429                pixmp->width = inst->width;
 430                pixmp->height = inst->height;
 431                pixmp->colorspace = inst->colorspace;
 432                pixmp->ycbcr_enc = inst->ycbcr_enc;
 433                pixmp->quantization = inst->quantization;
 434                pixmp->xfer_func = inst->xfer_func;
 435        } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 436                pixmp->width = inst->out_width;
 437                pixmp->height = inst->out_height;
 438        }
 439
 440        venc_try_fmt_common(inst, f);
 441
 442        return 0;
 443}
 444
 445static int
 446venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
 447{
 448        struct venus_inst *inst = to_inst(file);
 449
 450        if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
 451                return -EINVAL;
 452
 453        switch (s->target) {
 454        case V4L2_SEL_TGT_CROP_DEFAULT:
 455        case V4L2_SEL_TGT_CROP_BOUNDS:
 456                s->r.width = inst->width;
 457                s->r.height = inst->height;
 458                break;
 459        case V4L2_SEL_TGT_CROP:
 460                s->r.width = inst->out_width;
 461                s->r.height = inst->out_height;
 462                break;
 463        default:
 464                return -EINVAL;
 465        }
 466
 467        s->r.top = 0;
 468        s->r.left = 0;
 469
 470        return 0;
 471}
 472
 473static int
 474venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
 475{
 476        struct venus_inst *inst = to_inst(file);
 477
 478        if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
 479                return -EINVAL;
 480
 481        switch (s->target) {
 482        case V4L2_SEL_TGT_CROP:
 483                if (s->r.width != inst->out_width ||
 484                    s->r.height != inst->out_height ||
 485                    s->r.top != 0 || s->r.left != 0)
 486                        return -EINVAL;
 487                break;
 488        default:
 489                return -EINVAL;
 490        }
 491
 492        return 0;
 493}
 494
 495static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
 496{
 497        struct venus_inst *inst = to_inst(file);
 498        struct v4l2_outputparm *out = &a->parm.output;
 499        struct v4l2_fract *timeperframe = &out->timeperframe;
 500        u64 us_per_frame, fps;
 501
 502        if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 503            a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 504                return -EINVAL;
 505
 506        memset(out->reserved, 0, sizeof(out->reserved));
 507
 508        if (!timeperframe->denominator)
 509                timeperframe->denominator = inst->timeperframe.denominator;
 510        if (!timeperframe->numerator)
 511                timeperframe->numerator = inst->timeperframe.numerator;
 512
 513        out->capability = V4L2_CAP_TIMEPERFRAME;
 514
 515        us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
 516        do_div(us_per_frame, timeperframe->denominator);
 517
 518        if (!us_per_frame)
 519                return -EINVAL;
 520
 521        fps = (u64)USEC_PER_SEC;
 522        do_div(fps, us_per_frame);
 523
 524        inst->timeperframe = *timeperframe;
 525        inst->fps = fps;
 526
 527        return 0;
 528}
 529
 530static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
 531{
 532        struct venus_inst *inst = to_inst(file);
 533
 534        if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 535            a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 536                return -EINVAL;
 537
 538        a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
 539        a->parm.output.timeperframe = inst->timeperframe;
 540
 541        return 0;
 542}
 543
 544static int venc_enum_framesizes(struct file *file, void *fh,
 545                                struct v4l2_frmsizeenum *fsize)
 546{
 547        struct venus_inst *inst = to_inst(file);
 548        const struct venus_format *fmt;
 549
 550        fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
 551
 552        fmt = find_format(inst, fsize->pixel_format,
 553                          V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
 554        if (!fmt) {
 555                fmt = find_format(inst, fsize->pixel_format,
 556                                  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
 557                if (!fmt)
 558                        return -EINVAL;
 559        }
 560
 561        if (fsize->index)
 562                return -EINVAL;
 563
 564        fsize->stepwise.min_width = frame_width_min(inst);
 565        fsize->stepwise.max_width = frame_width_max(inst);
 566        fsize->stepwise.step_width = frame_width_step(inst);
 567        fsize->stepwise.min_height = frame_height_min(inst);
 568        fsize->stepwise.max_height = frame_height_max(inst);
 569        fsize->stepwise.step_height = frame_height_step(inst);
 570
 571        return 0;
 572}
 573
 574static int venc_enum_frameintervals(struct file *file, void *fh,
 575                                    struct v4l2_frmivalenum *fival)
 576{
 577        struct venus_inst *inst = to_inst(file);
 578        const struct venus_format *fmt;
 579
 580        fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
 581
 582        fmt = find_format(inst, fival->pixel_format,
 583                          V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
 584        if (!fmt) {
 585                fmt = find_format(inst, fival->pixel_format,
 586                                  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
 587                if (!fmt)
 588                        return -EINVAL;
 589        }
 590
 591        if (fival->index)
 592                return -EINVAL;
 593
 594        if (!fival->width || !fival->height)
 595                return -EINVAL;
 596
 597        if (fival->width > frame_width_max(inst) ||
 598            fival->width < frame_width_min(inst) ||
 599            fival->height > frame_height_max(inst) ||
 600            fival->height < frame_height_min(inst))
 601                return -EINVAL;
 602
 603        fival->stepwise.min.numerator = 1;
 604        fival->stepwise.min.denominator = frate_max(inst);
 605        fival->stepwise.max.numerator = 1;
 606        fival->stepwise.max.denominator = frate_min(inst);
 607        fival->stepwise.step.numerator = 1;
 608        fival->stepwise.step.denominator = frate_max(inst);
 609
 610        return 0;
 611}
 612
 613static const struct v4l2_ioctl_ops venc_ioctl_ops = {
 614        .vidioc_querycap = venc_querycap,
 615        .vidioc_enum_fmt_vid_cap = venc_enum_fmt,
 616        .vidioc_enum_fmt_vid_out = venc_enum_fmt,
 617        .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
 618        .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
 619        .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
 620        .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
 621        .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
 622        .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
 623        .vidioc_g_selection = venc_g_selection,
 624        .vidioc_s_selection = venc_s_selection,
 625        .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
 626        .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
 627        .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
 628        .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
 629        .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
 630        .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
 631        .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
 632        .vidioc_streamon = v4l2_m2m_ioctl_streamon,
 633        .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
 634        .vidioc_s_parm = venc_s_parm,
 635        .vidioc_g_parm = venc_g_parm,
 636        .vidioc_enum_framesizes = venc_enum_framesizes,
 637        .vidioc_enum_frameintervals = venc_enum_frameintervals,
 638        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
 639        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
 640};
 641
 642static int venc_set_properties(struct venus_inst *inst)
 643{
 644        struct venc_controls *ctr = &inst->controls.enc;
 645        struct hfi_intra_period intra_period;
 646        struct hfi_profile_level pl;
 647        struct hfi_framerate frate;
 648        struct hfi_bitrate brate;
 649        struct hfi_idr_period idrp;
 650        struct hfi_quantization quant;
 651        struct hfi_quantization_range quant_range;
 652        u32 ptype, rate_control, bitrate, profile = 0, level = 0;
 653        int ret;
 654
 655        ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
 656        if (ret)
 657                return ret;
 658
 659        ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
 660        frate.buffer_type = HFI_BUFFER_OUTPUT;
 661        frate.framerate = inst->fps * (1 << 16);
 662
 663        ret = hfi_session_set_property(inst, ptype, &frate);
 664        if (ret)
 665                return ret;
 666
 667        if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
 668                struct hfi_h264_vui_timing_info info;
 669                struct hfi_h264_entropy_control entropy;
 670                struct hfi_h264_db_control deblock;
 671
 672                ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
 673                info.enable = 1;
 674                info.fixed_framerate = 1;
 675                info.time_scale = NSEC_PER_SEC;
 676
 677                ret = hfi_session_set_property(inst, ptype, &info);
 678                if (ret)
 679                        return ret;
 680
 681                ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL;
 682                entropy.entropy_mode = venc_v4l2_to_hfi(
 683                                          V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
 684                                          ctr->h264_entropy_mode);
 685                entropy.cabac_model = HFI_H264_CABAC_MODEL_0;
 686
 687                ret = hfi_session_set_property(inst, ptype, &entropy);
 688                if (ret)
 689                        return ret;
 690
 691                ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL;
 692                deblock.mode = venc_v4l2_to_hfi(
 693                                      V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
 694                                      ctr->h264_loop_filter_mode);
 695                deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha;
 696                deblock.slice_beta_offset = ctr->h264_loop_filter_beta;
 697
 698                ret = hfi_session_set_property(inst, ptype, &deblock);
 699                if (ret)
 700                        return ret;
 701        }
 702
 703        /* IDR periodicity, n:
 704         * n = 0 - only the first I-frame is IDR frame
 705         * n = 1 - all I-frames will be IDR frames
 706         * n > 1 - every n-th I-frame will be IDR frame
 707         */
 708        ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
 709        idrp.idr_period = 0;
 710        ret = hfi_session_set_property(inst, ptype, &idrp);
 711        if (ret)
 712                return ret;
 713
 714        if (ctr->num_b_frames) {
 715                u32 max_num_b_frames = NUM_B_FRAMES_MAX;
 716
 717                ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
 718                ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
 719                if (ret)
 720                        return ret;
 721        }
 722
 723        ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
 724        intra_period.pframes = ctr->num_p_frames;
 725        intra_period.bframes = ctr->num_b_frames;
 726
 727        ret = hfi_session_set_property(inst, ptype, &intra_period);
 728        if (ret)
 729                return ret;
 730
 731        if (!ctr->rc_enable)
 732                rate_control = HFI_RATE_CONTROL_OFF;
 733        else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
 734                rate_control = HFI_RATE_CONTROL_VBR_CFR;
 735        else
 736                rate_control = HFI_RATE_CONTROL_CBR_CFR;
 737
 738        ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
 739        ret = hfi_session_set_property(inst, ptype, &rate_control);
 740        if (ret)
 741                return ret;
 742
 743        if (!ctr->bitrate)
 744                bitrate = 64000;
 745        else
 746                bitrate = ctr->bitrate;
 747
 748        ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
 749        brate.bitrate = bitrate;
 750        brate.layer_id = 0;
 751
 752        ret = hfi_session_set_property(inst, ptype, &brate);
 753        if (ret)
 754                return ret;
 755
 756        if (!ctr->bitrate_peak)
 757                bitrate *= 2;
 758        else
 759                bitrate = ctr->bitrate_peak;
 760
 761        ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
 762        brate.bitrate = bitrate;
 763        brate.layer_id = 0;
 764
 765        ret = hfi_session_set_property(inst, ptype, &brate);
 766        if (ret)
 767                return ret;
 768
 769        ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP;
 770        quant.qp_i = ctr->h264_i_qp;
 771        quant.qp_p = ctr->h264_p_qp;
 772        quant.qp_b = ctr->h264_b_qp;
 773        quant.layer_id = 0;
 774        ret = hfi_session_set_property(inst, ptype, &quant);
 775        if (ret)
 776                return ret;
 777
 778        ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE;
 779        quant_range.min_qp = ctr->h264_min_qp;
 780        quant_range.max_qp = ctr->h264_max_qp;
 781        quant_range.layer_id = 0;
 782        ret = hfi_session_set_property(inst, ptype, &quant_range);
 783        if (ret)
 784                return ret;
 785
 786        if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
 787                profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_PROFILE,
 788                                           ctr->profile.h264);
 789                level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_LEVEL,
 790                                         ctr->level.h264);
 791        } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
 792                profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
 793                                           ctr->profile.vpx);
 794                level = 0;
 795        } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_MPEG4) {
 796                profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
 797                                           ctr->profile.mpeg4);
 798                level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
 799                                         ctr->level.mpeg4);
 800        } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H263) {
 801                profile = 0;
 802                level = 0;
 803        } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
 804                profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
 805                                           ctr->profile.hevc);
 806                level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
 807                                         ctr->level.hevc);
 808        }
 809
 810        ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
 811        pl.profile = profile;
 812        pl.level = level;
 813
 814        ret = hfi_session_set_property(inst, ptype, &pl);
 815        if (ret)
 816                return ret;
 817
 818        return 0;
 819}
 820
 821static int venc_init_session(struct venus_inst *inst)
 822{
 823        int ret;
 824
 825        ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
 826        if (ret)
 827                return ret;
 828
 829        ret = venus_helper_set_input_resolution(inst, inst->width,
 830                                                inst->height);
 831        if (ret)
 832                goto deinit;
 833
 834        ret = venus_helper_set_output_resolution(inst, inst->width,
 835                                                 inst->height,
 836                                                 HFI_BUFFER_OUTPUT);
 837        if (ret)
 838                goto deinit;
 839
 840        ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
 841        if (ret)
 842                goto deinit;
 843
 844        ret = venus_helper_init_codec_freq_data(inst);
 845        if (ret)
 846                goto deinit;
 847
 848        ret = venc_set_properties(inst);
 849        if (ret)
 850                goto deinit;
 851
 852        return 0;
 853deinit:
 854        hfi_session_deinit(inst);
 855        return ret;
 856}
 857
 858static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
 859{
 860        struct hfi_buffer_requirements bufreq;
 861        int ret;
 862
 863        ret = venc_init_session(inst);
 864        if (ret)
 865                return ret;
 866
 867        ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
 868
 869        *num = bufreq.count_actual;
 870
 871        hfi_session_deinit(inst);
 872
 873        return ret;
 874}
 875
 876static int venc_queue_setup(struct vb2_queue *q,
 877                            unsigned int *num_buffers, unsigned int *num_planes,
 878                            unsigned int sizes[], struct device *alloc_devs[])
 879{
 880        struct venus_inst *inst = vb2_get_drv_priv(q);
 881        unsigned int num, min = 4;
 882        int ret = 0;
 883
 884        if (*num_planes) {
 885                if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
 886                    *num_planes != inst->fmt_out->num_planes)
 887                        return -EINVAL;
 888
 889                if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 890                    *num_planes != inst->fmt_cap->num_planes)
 891                        return -EINVAL;
 892
 893                if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
 894                    sizes[0] < inst->input_buf_size)
 895                        return -EINVAL;
 896
 897                if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 898                    sizes[0] < inst->output_buf_size)
 899                        return -EINVAL;
 900
 901                return 0;
 902        }
 903
 904        switch (q->type) {
 905        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 906                *num_planes = inst->fmt_out->num_planes;
 907
 908                ret = venc_out_num_buffers(inst, &num);
 909                if (ret)
 910                        break;
 911
 912                num = max(num, min);
 913                *num_buffers = max(*num_buffers, num);
 914                inst->num_input_bufs = *num_buffers;
 915
 916                sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
 917                                                    inst->width,
 918                                                    inst->height);
 919                inst->input_buf_size = sizes[0];
 920                break;
 921        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 922                *num_planes = inst->fmt_cap->num_planes;
 923                *num_buffers = max(*num_buffers, min);
 924                inst->num_output_bufs = *num_buffers;
 925                sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
 926                                                    inst->width,
 927                                                    inst->height);
 928                sizes[0] = max(sizes[0], inst->output_buf_size);
 929                inst->output_buf_size = sizes[0];
 930                break;
 931        default:
 932                ret = -EINVAL;
 933                break;
 934        }
 935
 936        return ret;
 937}
 938
 939static int venc_verify_conf(struct venus_inst *inst)
 940{
 941        enum hfi_version ver = inst->core->res->hfi_version;
 942        struct hfi_buffer_requirements bufreq;
 943        int ret;
 944
 945        if (!inst->num_input_bufs || !inst->num_output_bufs)
 946                return -EINVAL;
 947
 948        ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
 949        if (ret)
 950                return ret;
 951
 952        if (inst->num_output_bufs < bufreq.count_actual ||
 953            inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
 954                return -EINVAL;
 955
 956        ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
 957        if (ret)
 958                return ret;
 959
 960        if (inst->num_input_bufs < bufreq.count_actual ||
 961            inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
 962                return -EINVAL;
 963
 964        return 0;
 965}
 966
 967static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
 968{
 969        struct venus_inst *inst = vb2_get_drv_priv(q);
 970        int ret;
 971
 972        mutex_lock(&inst->lock);
 973
 974        if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 975                inst->streamon_out = 1;
 976        else
 977                inst->streamon_cap = 1;
 978
 979        if (!(inst->streamon_out & inst->streamon_cap)) {
 980                mutex_unlock(&inst->lock);
 981                return 0;
 982        }
 983
 984        venus_helper_init_instance(inst);
 985
 986        inst->sequence_cap = 0;
 987        inst->sequence_out = 0;
 988
 989        ret = venc_init_session(inst);
 990        if (ret)
 991                goto bufs_done;
 992
 993        ret = venus_pm_acquire_core(inst);
 994        if (ret)
 995                goto deinit_sess;
 996
 997        ret = venc_set_properties(inst);
 998        if (ret)
 999                goto deinit_sess;
1000
1001        ret = venc_verify_conf(inst);
1002        if (ret)
1003                goto deinit_sess;
1004
1005        ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1006                                        inst->num_output_bufs, 0);
1007        if (ret)
1008                goto deinit_sess;
1009
1010        ret = venus_helper_vb2_start_streaming(inst);
1011        if (ret)
1012                goto deinit_sess;
1013
1014        mutex_unlock(&inst->lock);
1015
1016        return 0;
1017
1018deinit_sess:
1019        hfi_session_deinit(inst);
1020bufs_done:
1021        venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
1022        if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1023                inst->streamon_out = 0;
1024        else
1025                inst->streamon_cap = 0;
1026        mutex_unlock(&inst->lock);
1027        return ret;
1028}
1029
1030static const struct vb2_ops venc_vb2_ops = {
1031        .queue_setup = venc_queue_setup,
1032        .buf_init = venus_helper_vb2_buf_init,
1033        .buf_prepare = venus_helper_vb2_buf_prepare,
1034        .start_streaming = venc_start_streaming,
1035        .stop_streaming = venus_helper_vb2_stop_streaming,
1036        .buf_queue = venus_helper_vb2_buf_queue,
1037};
1038
1039static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
1040                          u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1041                          u32 hfi_flags, u64 timestamp_us)
1042{
1043        struct vb2_v4l2_buffer *vbuf;
1044        struct vb2_buffer *vb;
1045        unsigned int type;
1046
1047        if (buf_type == HFI_BUFFER_INPUT)
1048                type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1049        else
1050                type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1051
1052        vbuf = venus_helper_find_buf(inst, type, tag);
1053        if (!vbuf)
1054                return;
1055
1056        vbuf->flags = flags;
1057
1058        if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1059                vb = &vbuf->vb2_buf;
1060                vb2_set_plane_payload(vb, 0, bytesused + data_offset);
1061                vb->planes[0].data_offset = data_offset;
1062                vb->timestamp = timestamp_us * NSEC_PER_USEC;
1063                vbuf->sequence = inst->sequence_cap++;
1064        } else {
1065                vbuf->sequence = inst->sequence_out++;
1066        }
1067
1068        v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1069}
1070
1071static void venc_event_notify(struct venus_inst *inst, u32 event,
1072                              struct hfi_event_data *data)
1073{
1074        struct device *dev = inst->core->dev_enc;
1075
1076        if (event == EVT_SESSION_ERROR) {
1077                inst->session_error = true;
1078                dev_err(dev, "enc: event session error %x\n", inst->error);
1079        }
1080}
1081
1082static const struct hfi_inst_ops venc_hfi_ops = {
1083        .buf_done = venc_buf_done,
1084        .event_notify = venc_event_notify,
1085};
1086
1087static const struct v4l2_m2m_ops venc_m2m_ops = {
1088        .device_run = venus_helper_m2m_device_run,
1089        .job_abort = venus_helper_m2m_job_abort,
1090};
1091
1092static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1093                          struct vb2_queue *dst_vq)
1094{
1095        struct venus_inst *inst = priv;
1096        int ret;
1097
1098        src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1099        src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1100        src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1101        src_vq->ops = &venc_vb2_ops;
1102        src_vq->mem_ops = &vb2_dma_sg_memops;
1103        src_vq->drv_priv = inst;
1104        src_vq->buf_struct_size = sizeof(struct venus_buffer);
1105        src_vq->allow_zero_bytesused = 1;
1106        src_vq->min_buffers_needed = 1;
1107        src_vq->dev = inst->core->dev;
1108        if (inst->core->res->hfi_version == HFI_VERSION_1XX)
1109                src_vq->bidirectional = 1;
1110        ret = vb2_queue_init(src_vq);
1111        if (ret)
1112                return ret;
1113
1114        dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1115        dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1116        dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1117        dst_vq->ops = &venc_vb2_ops;
1118        dst_vq->mem_ops = &vb2_dma_sg_memops;
1119        dst_vq->drv_priv = inst;
1120        dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1121        dst_vq->allow_zero_bytesused = 1;
1122        dst_vq->min_buffers_needed = 1;
1123        dst_vq->dev = inst->core->dev;
1124        ret = vb2_queue_init(dst_vq);
1125        if (ret) {
1126                vb2_queue_release(src_vq);
1127                return ret;
1128        }
1129
1130        return 0;
1131}
1132
1133static void venc_inst_init(struct venus_inst *inst)
1134{
1135        inst->fmt_cap = &venc_formats[2];
1136        inst->fmt_out = &venc_formats[0];
1137        inst->width = 1280;
1138        inst->height = ALIGN(720, 32);
1139        inst->out_width = 1280;
1140        inst->out_height = 720;
1141        inst->fps = 15;
1142        inst->timeperframe.numerator = 1;
1143        inst->timeperframe.denominator = 15;
1144        inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1145}
1146
1147static int venc_open(struct file *file)
1148{
1149        struct venus_core *core = video_drvdata(file);
1150        struct venus_inst *inst;
1151        int ret;
1152
1153        inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1154        if (!inst)
1155                return -ENOMEM;
1156
1157        INIT_LIST_HEAD(&inst->dpbbufs);
1158        INIT_LIST_HEAD(&inst->registeredbufs);
1159        INIT_LIST_HEAD(&inst->internalbufs);
1160        INIT_LIST_HEAD(&inst->list);
1161        mutex_init(&inst->lock);
1162
1163        inst->core = core;
1164        inst->session_type = VIDC_SESSION_TYPE_ENC;
1165        inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1166        inst->core_acquired = false;
1167
1168        venus_helper_init_instance(inst);
1169
1170        ret = pm_runtime_get_sync(core->dev_enc);
1171        if (ret < 0)
1172                goto err_free_inst;
1173
1174        ret = venc_ctrl_init(inst);
1175        if (ret)
1176                goto err_put_sync;
1177
1178        ret = hfi_session_create(inst, &venc_hfi_ops);
1179        if (ret)
1180                goto err_ctrl_deinit;
1181
1182        venc_inst_init(inst);
1183
1184        /*
1185         * create m2m device for every instance, the m2m context scheduling
1186         * is made by firmware side so we do not need to care about.
1187         */
1188        inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1189        if (IS_ERR(inst->m2m_dev)) {
1190                ret = PTR_ERR(inst->m2m_dev);
1191                goto err_session_destroy;
1192        }
1193
1194        inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1195        if (IS_ERR(inst->m2m_ctx)) {
1196                ret = PTR_ERR(inst->m2m_ctx);
1197                goto err_m2m_release;
1198        }
1199
1200        v4l2_fh_init(&inst->fh, core->vdev_enc);
1201
1202        inst->fh.ctrl_handler = &inst->ctrl_handler;
1203        v4l2_fh_add(&inst->fh);
1204        inst->fh.m2m_ctx = inst->m2m_ctx;
1205        file->private_data = &inst->fh;
1206
1207        return 0;
1208
1209err_m2m_release:
1210        v4l2_m2m_release(inst->m2m_dev);
1211err_session_destroy:
1212        hfi_session_destroy(inst);
1213err_ctrl_deinit:
1214        venc_ctrl_deinit(inst);
1215err_put_sync:
1216        pm_runtime_put_sync(core->dev_enc);
1217err_free_inst:
1218        kfree(inst);
1219        return ret;
1220}
1221
1222static int venc_close(struct file *file)
1223{
1224        struct venus_inst *inst = to_inst(file);
1225
1226        v4l2_m2m_ctx_release(inst->m2m_ctx);
1227        v4l2_m2m_release(inst->m2m_dev);
1228        venc_ctrl_deinit(inst);
1229        hfi_session_destroy(inst);
1230        mutex_destroy(&inst->lock);
1231        v4l2_fh_del(&inst->fh);
1232        v4l2_fh_exit(&inst->fh);
1233
1234        pm_runtime_put_sync(inst->core->dev_enc);
1235
1236        kfree(inst);
1237        return 0;
1238}
1239
1240static const struct v4l2_file_operations venc_fops = {
1241        .owner = THIS_MODULE,
1242        .open = venc_open,
1243        .release = venc_close,
1244        .unlocked_ioctl = video_ioctl2,
1245        .poll = v4l2_m2m_fop_poll,
1246        .mmap = v4l2_m2m_fop_mmap,
1247};
1248
1249static int venc_probe(struct platform_device *pdev)
1250{
1251        struct device *dev = &pdev->dev;
1252        struct video_device *vdev;
1253        struct venus_core *core;
1254        int ret;
1255
1256        if (!dev->parent)
1257                return -EPROBE_DEFER;
1258
1259        core = dev_get_drvdata(dev->parent);
1260        if (!core)
1261                return -EPROBE_DEFER;
1262
1263        platform_set_drvdata(pdev, core);
1264
1265        if (core->pm_ops->venc_get) {
1266                ret = core->pm_ops->venc_get(dev);
1267                if (ret)
1268                        return ret;
1269        }
1270
1271        vdev = video_device_alloc();
1272        if (!vdev)
1273                return -ENOMEM;
1274
1275        strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1276        vdev->release = video_device_release;
1277        vdev->fops = &venc_fops;
1278        vdev->ioctl_ops = &venc_ioctl_ops;
1279        vdev->vfl_dir = VFL_DIR_M2M;
1280        vdev->v4l2_dev = &core->v4l2_dev;
1281        vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1282
1283        ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1284        if (ret)
1285                goto err_vdev_release;
1286
1287        core->vdev_enc = vdev;
1288        core->dev_enc = dev;
1289
1290        video_set_drvdata(vdev, core);
1291        pm_runtime_enable(dev);
1292
1293        return 0;
1294
1295err_vdev_release:
1296        video_device_release(vdev);
1297        return ret;
1298}
1299
1300static int venc_remove(struct platform_device *pdev)
1301{
1302        struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1303
1304        video_unregister_device(core->vdev_enc);
1305        pm_runtime_disable(core->dev_enc);
1306
1307        if (core->pm_ops->venc_put)
1308                core->pm_ops->venc_put(core->dev_enc);
1309
1310        return 0;
1311}
1312
1313static __maybe_unused int venc_runtime_suspend(struct device *dev)
1314{
1315        struct venus_core *core = dev_get_drvdata(dev);
1316        const struct venus_pm_ops *pm_ops = core->pm_ops;
1317        int ret = 0;
1318
1319        if (pm_ops->venc_power)
1320                ret = pm_ops->venc_power(dev, POWER_OFF);
1321
1322        return ret;
1323}
1324
1325static __maybe_unused int venc_runtime_resume(struct device *dev)
1326{
1327        struct venus_core *core = dev_get_drvdata(dev);
1328        const struct venus_pm_ops *pm_ops = core->pm_ops;
1329        int ret = 0;
1330
1331        if (pm_ops->venc_power)
1332                ret = pm_ops->venc_power(dev, POWER_ON);
1333
1334        return ret;
1335}
1336
1337static const struct dev_pm_ops venc_pm_ops = {
1338        SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1339                                pm_runtime_force_resume)
1340        SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1341};
1342
1343static const struct of_device_id venc_dt_match[] = {
1344        { .compatible = "venus-encoder" },
1345        { }
1346};
1347MODULE_DEVICE_TABLE(of, venc_dt_match);
1348
1349static struct platform_driver qcom_venus_enc_driver = {
1350        .probe = venc_probe,
1351        .remove = venc_remove,
1352        .driver = {
1353                .name = "qcom-venus-encoder",
1354                .of_match_table = venc_dt_match,
1355                .pm = &venc_pm_ops,
1356        },
1357};
1358module_platform_driver(qcom_venus_enc_driver);
1359
1360MODULE_ALIAS("platform:qcom-venus-encoder");
1361MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1362MODULE_LICENSE("GPL v2");
1363