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-contig.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 */ 34 static 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 62 static const struct venus_format * 63 find_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 84 static const struct venus_format * 85 find_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 113 static int venc_v4l2_to_hfi(int id, int value) 114 { 115 switch (id) { 116 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE: 117 switch (value) { 118 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC: 119 default: 120 return HFI_H264_ENTROPY_CAVLC; 121 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC: 122 return HFI_H264_ENTROPY_CABAC; 123 } 124 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: 125 switch (value) { 126 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED: 127 default: 128 return HFI_H264_DB_MODE_ALL_BOUNDARY; 129 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED: 130 return HFI_H264_DB_MODE_DISABLE; 131 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY: 132 return HFI_H264_DB_MODE_SKIP_SLICE_BOUNDARY; 133 } 134 } 135 136 return 0; 137 } 138 139 static int 140 venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap) 141 { 142 strscpy(cap->driver, "qcom-venus", sizeof(cap->driver)); 143 strscpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card)); 144 strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info)); 145 146 return 0; 147 } 148 149 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f) 150 { 151 struct venus_inst *inst = to_inst(file); 152 const struct venus_format *fmt; 153 154 fmt = find_format_by_index(inst, f->index, f->type); 155 156 memset(f->reserved, 0, sizeof(f->reserved)); 157 158 if (!fmt) 159 return -EINVAL; 160 161 f->pixelformat = fmt->pixfmt; 162 163 return 0; 164 } 165 166 static const struct venus_format * 167 venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f) 168 { 169 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 170 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt; 171 const struct venus_format *fmt; 172 u32 sizeimage; 173 174 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved)); 175 memset(pixmp->reserved, 0, sizeof(pixmp->reserved)); 176 177 fmt = find_format(inst, pixmp->pixelformat, f->type); 178 if (!fmt) { 179 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 180 pixmp->pixelformat = V4L2_PIX_FMT_H264; 181 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 182 pixmp->pixelformat = V4L2_PIX_FMT_NV12; 183 else 184 return NULL; 185 fmt = find_format(inst, pixmp->pixelformat, f->type); 186 } 187 188 pixmp->width = clamp(pixmp->width, frame_width_min(inst), 189 frame_width_max(inst)); 190 pixmp->height = clamp(pixmp->height, frame_height_min(inst), 191 frame_height_max(inst)); 192 193 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 194 pixmp->width = ALIGN(pixmp->width, 128); 195 pixmp->height = ALIGN(pixmp->height, 32); 196 } 197 198 pixmp->width = ALIGN(pixmp->width, 2); 199 pixmp->height = ALIGN(pixmp->height, 2); 200 201 if (pixmp->field == V4L2_FIELD_ANY) 202 pixmp->field = V4L2_FIELD_NONE; 203 pixmp->num_planes = fmt->num_planes; 204 pixmp->flags = 0; 205 206 sizeimage = venus_helper_get_framesz(pixmp->pixelformat, 207 pixmp->width, 208 pixmp->height); 209 pfmt[0].sizeimage = max(ALIGN(pfmt[0].sizeimage, SZ_4K), sizeimage); 210 211 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 212 pfmt[0].bytesperline = ALIGN(pixmp->width, 128); 213 else 214 pfmt[0].bytesperline = 0; 215 216 return fmt; 217 } 218 219 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f) 220 { 221 struct venus_inst *inst = to_inst(file); 222 223 venc_try_fmt_common(inst, f); 224 225 return 0; 226 } 227 228 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f) 229 { 230 struct venus_inst *inst = to_inst(file); 231 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 232 struct v4l2_pix_format_mplane orig_pixmp; 233 const struct venus_format *fmt; 234 struct v4l2_format format; 235 u32 pixfmt_out = 0, pixfmt_cap = 0; 236 struct vb2_queue *q; 237 238 q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type); 239 if (!q) 240 return -EINVAL; 241 242 if (vb2_is_busy(q)) 243 return -EBUSY; 244 245 orig_pixmp = *pixmp; 246 247 fmt = venc_try_fmt_common(inst, f); 248 if (!fmt) 249 return -EINVAL; 250 251 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 252 pixfmt_out = pixmp->pixelformat; 253 pixfmt_cap = inst->fmt_cap->pixfmt; 254 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 255 pixfmt_cap = pixmp->pixelformat; 256 pixfmt_out = inst->fmt_out->pixfmt; 257 } 258 259 memset(&format, 0, sizeof(format)); 260 261 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 262 format.fmt.pix_mp.pixelformat = pixfmt_out; 263 format.fmt.pix_mp.width = orig_pixmp.width; 264 format.fmt.pix_mp.height = orig_pixmp.height; 265 venc_try_fmt_common(inst, &format); 266 267 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 268 inst->out_width = format.fmt.pix_mp.width; 269 inst->out_height = format.fmt.pix_mp.height; 270 inst->colorspace = pixmp->colorspace; 271 inst->ycbcr_enc = pixmp->ycbcr_enc; 272 inst->quantization = pixmp->quantization; 273 inst->xfer_func = pixmp->xfer_func; 274 } 275 276 memset(&format, 0, sizeof(format)); 277 278 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 279 format.fmt.pix_mp.pixelformat = pixfmt_cap; 280 format.fmt.pix_mp.width = orig_pixmp.width; 281 format.fmt.pix_mp.height = orig_pixmp.height; 282 venc_try_fmt_common(inst, &format); 283 284 inst->width = format.fmt.pix_mp.width; 285 inst->height = format.fmt.pix_mp.height; 286 287 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 288 inst->fmt_out = fmt; 289 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 290 inst->fmt_cap = fmt; 291 inst->output_buf_size = pixmp->plane_fmt[0].sizeimage; 292 } 293 294 return 0; 295 } 296 297 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f) 298 { 299 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 300 struct venus_inst *inst = to_inst(file); 301 const struct venus_format *fmt; 302 303 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 304 fmt = inst->fmt_cap; 305 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 306 fmt = inst->fmt_out; 307 else 308 return -EINVAL; 309 310 pixmp->pixelformat = fmt->pixfmt; 311 312 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 313 pixmp->width = inst->width; 314 pixmp->height = inst->height; 315 pixmp->colorspace = inst->colorspace; 316 pixmp->ycbcr_enc = inst->ycbcr_enc; 317 pixmp->quantization = inst->quantization; 318 pixmp->xfer_func = inst->xfer_func; 319 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 320 pixmp->width = inst->out_width; 321 pixmp->height = inst->out_height; 322 } 323 324 venc_try_fmt_common(inst, f); 325 326 return 0; 327 } 328 329 static int 330 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 331 { 332 struct venus_inst *inst = to_inst(file); 333 334 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 335 return -EINVAL; 336 337 switch (s->target) { 338 case V4L2_SEL_TGT_CROP_DEFAULT: 339 case V4L2_SEL_TGT_CROP_BOUNDS: 340 s->r.width = inst->out_width; 341 s->r.height = inst->out_height; 342 break; 343 case V4L2_SEL_TGT_CROP: 344 s->r.width = inst->width; 345 s->r.height = inst->height; 346 break; 347 default: 348 return -EINVAL; 349 } 350 351 s->r.top = 0; 352 s->r.left = 0; 353 354 return 0; 355 } 356 357 static int 358 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s) 359 { 360 struct venus_inst *inst = to_inst(file); 361 362 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 363 return -EINVAL; 364 365 if (s->r.width > inst->out_width || 366 s->r.height > inst->out_height) 367 return -EINVAL; 368 369 s->r.width = ALIGN(s->r.width, 2); 370 s->r.height = ALIGN(s->r.height, 2); 371 372 switch (s->target) { 373 case V4L2_SEL_TGT_CROP: 374 s->r.top = 0; 375 s->r.left = 0; 376 inst->width = s->r.width; 377 inst->height = s->r.height; 378 break; 379 default: 380 return -EINVAL; 381 } 382 383 return 0; 384 } 385 386 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 387 { 388 struct venus_inst *inst = to_inst(file); 389 struct v4l2_outputparm *out = &a->parm.output; 390 struct v4l2_fract *timeperframe = &out->timeperframe; 391 u64 us_per_frame, fps; 392 393 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 394 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 395 return -EINVAL; 396 397 memset(out->reserved, 0, sizeof(out->reserved)); 398 399 if (!timeperframe->denominator) 400 timeperframe->denominator = inst->timeperframe.denominator; 401 if (!timeperframe->numerator) 402 timeperframe->numerator = inst->timeperframe.numerator; 403 404 out->capability = V4L2_CAP_TIMEPERFRAME; 405 406 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC; 407 do_div(us_per_frame, timeperframe->denominator); 408 409 if (!us_per_frame) 410 return -EINVAL; 411 412 fps = (u64)USEC_PER_SEC; 413 do_div(fps, us_per_frame); 414 415 inst->timeperframe = *timeperframe; 416 inst->fps = fps; 417 418 return 0; 419 } 420 421 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 422 { 423 struct venus_inst *inst = to_inst(file); 424 425 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 426 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 427 return -EINVAL; 428 429 a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME; 430 a->parm.output.timeperframe = inst->timeperframe; 431 432 return 0; 433 } 434 435 static int venc_enum_framesizes(struct file *file, void *fh, 436 struct v4l2_frmsizeenum *fsize) 437 { 438 struct venus_inst *inst = to_inst(file); 439 const struct venus_format *fmt; 440 441 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 442 443 fmt = find_format(inst, fsize->pixel_format, 444 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 445 if (!fmt) { 446 fmt = find_format(inst, fsize->pixel_format, 447 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 448 if (!fmt) 449 return -EINVAL; 450 } 451 452 if (fsize->index) 453 return -EINVAL; 454 455 fsize->stepwise.min_width = frame_width_min(inst); 456 fsize->stepwise.max_width = frame_width_max(inst); 457 fsize->stepwise.step_width = frame_width_step(inst); 458 fsize->stepwise.min_height = frame_height_min(inst); 459 fsize->stepwise.max_height = frame_height_max(inst); 460 fsize->stepwise.step_height = frame_height_step(inst); 461 462 return 0; 463 } 464 465 static int venc_enum_frameintervals(struct file *file, void *fh, 466 struct v4l2_frmivalenum *fival) 467 { 468 struct venus_inst *inst = to_inst(file); 469 const struct venus_format *fmt; 470 unsigned int framerate_factor = 1; 471 472 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE; 473 474 fmt = find_format(inst, fival->pixel_format, 475 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 476 if (!fmt) { 477 fmt = find_format(inst, fival->pixel_format, 478 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 479 if (!fmt) 480 return -EINVAL; 481 } 482 483 if (fival->index) 484 return -EINVAL; 485 486 if (!fival->width || !fival->height) 487 return -EINVAL; 488 489 if (fival->width > frame_width_max(inst) || 490 fival->width < frame_width_min(inst) || 491 fival->height > frame_height_max(inst) || 492 fival->height < frame_height_min(inst)) 493 return -EINVAL; 494 495 if (IS_V1(inst->core)) { 496 /* framerate is reported in 1/65535 fps unit */ 497 framerate_factor = (1 << 16); 498 } 499 500 fival->stepwise.min.numerator = 1; 501 fival->stepwise.min.denominator = frate_max(inst) / framerate_factor; 502 fival->stepwise.max.numerator = 1; 503 fival->stepwise.max.denominator = frate_min(inst) / framerate_factor; 504 fival->stepwise.step.numerator = 1; 505 fival->stepwise.step.denominator = frate_max(inst) / framerate_factor; 506 507 return 0; 508 } 509 510 static const struct v4l2_ioctl_ops venc_ioctl_ops = { 511 .vidioc_querycap = venc_querycap, 512 .vidioc_enum_fmt_vid_cap = venc_enum_fmt, 513 .vidioc_enum_fmt_vid_out = venc_enum_fmt, 514 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt, 515 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt, 516 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt, 517 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt, 518 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt, 519 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt, 520 .vidioc_g_selection = venc_g_selection, 521 .vidioc_s_selection = venc_s_selection, 522 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 523 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 524 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 525 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 526 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 527 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 528 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 529 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 530 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 531 .vidioc_s_parm = venc_s_parm, 532 .vidioc_g_parm = venc_g_parm, 533 .vidioc_enum_framesizes = venc_enum_framesizes, 534 .vidioc_enum_frameintervals = venc_enum_frameintervals, 535 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 536 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 537 }; 538 539 static int venc_set_properties(struct venus_inst *inst) 540 { 541 struct venc_controls *ctr = &inst->controls.enc; 542 struct hfi_intra_period intra_period; 543 struct hfi_framerate frate; 544 struct hfi_bitrate brate; 545 struct hfi_idr_period idrp; 546 struct hfi_quantization quant; 547 struct hfi_quantization_range quant_range; 548 struct hfi_enable en; 549 u32 ptype, rate_control, bitrate; 550 u32 profile, level; 551 int ret; 552 553 ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2); 554 if (ret) 555 return ret; 556 557 ptype = HFI_PROPERTY_CONFIG_FRAME_RATE; 558 frate.buffer_type = HFI_BUFFER_OUTPUT; 559 frate.framerate = inst->fps * (1 << 16); 560 561 ret = hfi_session_set_property(inst, ptype, &frate); 562 if (ret) 563 return ret; 564 565 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) { 566 struct hfi_h264_vui_timing_info info; 567 struct hfi_h264_entropy_control entropy; 568 struct hfi_h264_db_control deblock; 569 570 ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO; 571 info.enable = 1; 572 info.fixed_framerate = 1; 573 info.time_scale = NSEC_PER_SEC; 574 575 ret = hfi_session_set_property(inst, ptype, &info); 576 if (ret) 577 return ret; 578 579 ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL; 580 entropy.entropy_mode = venc_v4l2_to_hfi( 581 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE, 582 ctr->h264_entropy_mode); 583 entropy.cabac_model = HFI_H264_CABAC_MODEL_0; 584 585 ret = hfi_session_set_property(inst, ptype, &entropy); 586 if (ret) 587 return ret; 588 589 ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL; 590 deblock.mode = venc_v4l2_to_hfi( 591 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE, 592 ctr->h264_loop_filter_mode); 593 deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha; 594 deblock.slice_beta_offset = ctr->h264_loop_filter_beta; 595 596 ret = hfi_session_set_property(inst, ptype, &deblock); 597 if (ret) 598 return ret; 599 } 600 601 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 || 602 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 603 /* IDR periodicity, n: 604 * n = 0 - only the first I-frame is IDR frame 605 * n = 1 - all I-frames will be IDR frames 606 * n > 1 - every n-th I-frame will be IDR frame 607 */ 608 ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD; 609 idrp.idr_period = 0; 610 ret = hfi_session_set_property(inst, ptype, &idrp); 611 if (ret) 612 return ret; 613 } 614 615 if (ctr->num_b_frames) { 616 u32 max_num_b_frames = NUM_B_FRAMES_MAX; 617 618 ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES; 619 ret = hfi_session_set_property(inst, ptype, &max_num_b_frames); 620 if (ret) 621 return ret; 622 } 623 624 ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD; 625 intra_period.pframes = ctr->num_p_frames; 626 intra_period.bframes = ctr->num_b_frames; 627 628 ret = hfi_session_set_property(inst, ptype, &intra_period); 629 if (ret) 630 return ret; 631 632 if (!ctr->rc_enable) 633 rate_control = HFI_RATE_CONTROL_OFF; 634 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) 635 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_VBR_VFR : 636 HFI_RATE_CONTROL_VBR_CFR; 637 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) 638 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_CBR_VFR : 639 HFI_RATE_CONTROL_CBR_CFR; 640 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ) 641 rate_control = HFI_RATE_CONTROL_CQ; 642 643 ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL; 644 ret = hfi_session_set_property(inst, ptype, &rate_control); 645 if (ret) 646 return ret; 647 648 if (rate_control == HFI_RATE_CONTROL_CQ && ctr->const_quality) { 649 struct hfi_heic_frame_quality quality = {}; 650 651 ptype = HFI_PROPERTY_CONFIG_HEIC_FRAME_QUALITY; 652 quality.frame_quality = ctr->const_quality; 653 ret = hfi_session_set_property(inst, ptype, &quality); 654 if (ret) 655 return ret; 656 } 657 658 if (!ctr->bitrate) 659 bitrate = 64000; 660 else 661 bitrate = ctr->bitrate; 662 663 ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE; 664 brate.bitrate = bitrate; 665 brate.layer_id = 0; 666 667 ret = hfi_session_set_property(inst, ptype, &brate); 668 if (ret) 669 return ret; 670 671 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 || 672 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 673 ptype = HFI_PROPERTY_CONFIG_VENC_SYNC_FRAME_SEQUENCE_HEADER; 674 if (ctr->header_mode == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) 675 en.enable = 0; 676 else 677 en.enable = 1; 678 679 ret = hfi_session_set_property(inst, ptype, &en); 680 if (ret) 681 return ret; 682 } 683 684 if (!ctr->bitrate_peak) 685 bitrate *= 2; 686 else 687 bitrate = ctr->bitrate_peak; 688 689 ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE; 690 brate.bitrate = bitrate; 691 brate.layer_id = 0; 692 693 ret = hfi_session_set_property(inst, ptype, &brate); 694 if (ret) 695 return ret; 696 697 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP; 698 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 699 quant.qp_i = ctr->hevc_i_qp; 700 quant.qp_p = ctr->hevc_p_qp; 701 quant.qp_b = ctr->hevc_b_qp; 702 } else { 703 quant.qp_i = ctr->h264_i_qp; 704 quant.qp_p = ctr->h264_p_qp; 705 quant.qp_b = ctr->h264_b_qp; 706 } 707 quant.layer_id = 0; 708 ret = hfi_session_set_property(inst, ptype, &quant); 709 if (ret) 710 return ret; 711 712 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE; 713 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 714 quant_range.min_qp = ctr->hevc_min_qp; 715 quant_range.max_qp = ctr->hevc_max_qp; 716 } else { 717 quant_range.min_qp = ctr->h264_min_qp; 718 quant_range.max_qp = ctr->h264_max_qp; 719 } 720 quant_range.layer_id = 0; 721 ret = hfi_session_set_property(inst, ptype, &quant_range); 722 if (ret) 723 return ret; 724 725 switch (inst->hfi_codec) { 726 case HFI_VIDEO_CODEC_H264: 727 profile = ctr->profile.h264; 728 level = ctr->level.h264; 729 break; 730 case HFI_VIDEO_CODEC_MPEG4: 731 profile = ctr->profile.mpeg4; 732 level = ctr->level.mpeg4; 733 break; 734 case HFI_VIDEO_CODEC_VP8: 735 profile = ctr->profile.vp8; 736 level = 0; 737 break; 738 case HFI_VIDEO_CODEC_VP9: 739 profile = ctr->profile.vp9; 740 level = ctr->level.vp9; 741 break; 742 case HFI_VIDEO_CODEC_HEVC: 743 profile = ctr->profile.hevc; 744 level = ctr->level.hevc; 745 break; 746 case HFI_VIDEO_CODEC_MPEG2: 747 default: 748 profile = 0; 749 level = 0; 750 break; 751 } 752 753 ret = venus_helper_set_profile_level(inst, profile, level); 754 if (ret) 755 return ret; 756 757 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264 || 758 inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) { 759 struct hfi_enable en = {}; 760 761 ptype = HFI_PROPERTY_PARAM_VENC_H264_GENERATE_AUDNAL; 762 763 if (ctr->aud_enable) 764 en.enable = 1; 765 766 ret = hfi_session_set_property(inst, ptype, &en); 767 if (ret) 768 return ret; 769 } 770 771 return 0; 772 } 773 774 static int venc_init_session(struct venus_inst *inst) 775 { 776 int ret; 777 778 ret = venus_helper_session_init(inst); 779 if (ret == -EALREADY) 780 return 0; 781 else if (ret) 782 return ret; 783 784 ret = venus_helper_set_stride(inst, inst->out_width, 785 inst->out_height); 786 if (ret) 787 goto deinit; 788 789 ret = venus_helper_set_input_resolution(inst, inst->width, 790 inst->height); 791 if (ret) 792 goto deinit; 793 794 ret = venus_helper_set_output_resolution(inst, inst->width, 795 inst->height, 796 HFI_BUFFER_OUTPUT); 797 if (ret) 798 goto deinit; 799 800 ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt); 801 if (ret) 802 goto deinit; 803 804 ret = venc_set_properties(inst); 805 if (ret) 806 goto deinit; 807 808 return 0; 809 deinit: 810 hfi_session_deinit(inst); 811 return ret; 812 } 813 814 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num) 815 { 816 struct hfi_buffer_requirements bufreq; 817 int ret; 818 819 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 820 if (ret) 821 return ret; 822 823 *num = bufreq.count_actual; 824 825 return 0; 826 } 827 828 static int venc_queue_setup(struct vb2_queue *q, 829 unsigned int *num_buffers, unsigned int *num_planes, 830 unsigned int sizes[], struct device *alloc_devs[]) 831 { 832 struct venus_inst *inst = vb2_get_drv_priv(q); 833 unsigned int num, min = 4; 834 int ret; 835 836 if (*num_planes) { 837 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 838 *num_planes != inst->fmt_out->num_planes) 839 return -EINVAL; 840 841 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 842 *num_planes != inst->fmt_cap->num_planes) 843 return -EINVAL; 844 845 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 846 sizes[0] < inst->input_buf_size) 847 return -EINVAL; 848 849 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && 850 sizes[0] < inst->output_buf_size) 851 return -EINVAL; 852 853 return 0; 854 } 855 856 mutex_lock(&inst->lock); 857 ret = venc_init_session(inst); 858 mutex_unlock(&inst->lock); 859 860 if (ret) 861 return ret; 862 863 switch (q->type) { 864 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 865 *num_planes = inst->fmt_out->num_planes; 866 867 ret = venc_out_num_buffers(inst, &num); 868 if (ret) 869 break; 870 871 num = max(num, min); 872 *num_buffers = max(*num_buffers, num); 873 inst->num_input_bufs = *num_buffers; 874 875 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt, 876 inst->out_width, 877 inst->out_height); 878 inst->input_buf_size = sizes[0]; 879 break; 880 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 881 *num_planes = inst->fmt_cap->num_planes; 882 *num_buffers = max(*num_buffers, min); 883 inst->num_output_bufs = *num_buffers; 884 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt, 885 inst->width, 886 inst->height); 887 sizes[0] = max(sizes[0], inst->output_buf_size); 888 inst->output_buf_size = sizes[0]; 889 break; 890 default: 891 ret = -EINVAL; 892 break; 893 } 894 895 return ret; 896 } 897 898 static int venc_buf_init(struct vb2_buffer *vb) 899 { 900 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 901 902 inst->buf_count++; 903 904 return venus_helper_vb2_buf_init(vb); 905 } 906 907 static void venc_release_session(struct venus_inst *inst) 908 { 909 int ret; 910 911 mutex_lock(&inst->lock); 912 913 ret = hfi_session_deinit(inst); 914 if (ret || inst->session_error) 915 hfi_session_abort(inst); 916 917 mutex_unlock(&inst->lock); 918 919 venus_pm_load_scale(inst); 920 INIT_LIST_HEAD(&inst->registeredbufs); 921 venus_pm_release_core(inst); 922 } 923 924 static void venc_buf_cleanup(struct vb2_buffer *vb) 925 { 926 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 927 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 928 struct venus_buffer *buf = to_venus_buffer(vbuf); 929 930 mutex_lock(&inst->lock); 931 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 932 if (!list_empty(&inst->registeredbufs)) 933 list_del_init(&buf->reg_list); 934 mutex_unlock(&inst->lock); 935 936 inst->buf_count--; 937 if (!inst->buf_count) 938 venc_release_session(inst); 939 } 940 941 static int venc_verify_conf(struct venus_inst *inst) 942 { 943 enum hfi_version ver = inst->core->res->hfi_version; 944 struct hfi_buffer_requirements bufreq; 945 int ret; 946 947 if (!inst->num_input_bufs || !inst->num_output_bufs) 948 return -EINVAL; 949 950 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq); 951 if (ret) 952 return ret; 953 954 if (inst->num_output_bufs < bufreq.count_actual || 955 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver)) 956 return -EINVAL; 957 958 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq); 959 if (ret) 960 return ret; 961 962 if (inst->num_input_bufs < bufreq.count_actual || 963 inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver)) 964 return -EINVAL; 965 966 return 0; 967 } 968 969 static int venc_start_streaming(struct vb2_queue *q, unsigned int count) 970 { 971 struct venus_inst *inst = vb2_get_drv_priv(q); 972 int ret; 973 974 mutex_lock(&inst->lock); 975 976 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 977 inst->streamon_out = 1; 978 else 979 inst->streamon_cap = 1; 980 981 if (!(inst->streamon_out & inst->streamon_cap)) { 982 mutex_unlock(&inst->lock); 983 return 0; 984 } 985 986 venus_helper_init_instance(inst); 987 988 inst->sequence_cap = 0; 989 inst->sequence_out = 0; 990 991 ret = venus_pm_acquire_core(inst); 992 if (ret) 993 goto error; 994 995 ret = venc_set_properties(inst); 996 if (ret) 997 goto error; 998 999 ret = venc_verify_conf(inst); 1000 if (ret) 1001 goto error; 1002 1003 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs, 1004 inst->num_output_bufs, 0); 1005 if (ret) 1006 goto error; 1007 1008 ret = venus_helper_vb2_start_streaming(inst); 1009 if (ret) 1010 goto error; 1011 1012 mutex_unlock(&inst->lock); 1013 1014 return 0; 1015 1016 error: 1017 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED); 1018 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 1019 inst->streamon_out = 0; 1020 else 1021 inst->streamon_cap = 0; 1022 mutex_unlock(&inst->lock); 1023 return ret; 1024 } 1025 1026 static void venc_vb2_buf_queue(struct vb2_buffer *vb) 1027 { 1028 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue); 1029 1030 mutex_lock(&inst->lock); 1031 venus_helper_vb2_buf_queue(vb); 1032 mutex_unlock(&inst->lock); 1033 } 1034 1035 static const struct vb2_ops venc_vb2_ops = { 1036 .queue_setup = venc_queue_setup, 1037 .buf_init = venc_buf_init, 1038 .buf_cleanup = venc_buf_cleanup, 1039 .buf_prepare = venus_helper_vb2_buf_prepare, 1040 .start_streaming = venc_start_streaming, 1041 .stop_streaming = venus_helper_vb2_stop_streaming, 1042 .buf_queue = venc_vb2_buf_queue, 1043 }; 1044 1045 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type, 1046 u32 tag, u32 bytesused, u32 data_offset, u32 flags, 1047 u32 hfi_flags, u64 timestamp_us) 1048 { 1049 struct vb2_v4l2_buffer *vbuf; 1050 struct vb2_buffer *vb; 1051 unsigned int type; 1052 1053 if (buf_type == HFI_BUFFER_INPUT) 1054 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1055 else 1056 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1057 1058 vbuf = venus_helper_find_buf(inst, type, tag); 1059 if (!vbuf) 1060 return; 1061 1062 vbuf->flags = flags; 1063 1064 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 1065 vb = &vbuf->vb2_buf; 1066 vb2_set_plane_payload(vb, 0, bytesused + data_offset); 1067 vb->planes[0].data_offset = data_offset; 1068 vb->timestamp = timestamp_us * NSEC_PER_USEC; 1069 vbuf->sequence = inst->sequence_cap++; 1070 } else { 1071 vbuf->sequence = inst->sequence_out++; 1072 } 1073 1074 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE); 1075 } 1076 1077 static void venc_event_notify(struct venus_inst *inst, u32 event, 1078 struct hfi_event_data *data) 1079 { 1080 struct device *dev = inst->core->dev_enc; 1081 1082 if (event == EVT_SESSION_ERROR) { 1083 inst->session_error = true; 1084 dev_err(dev, "enc: event session error %x\n", inst->error); 1085 } 1086 } 1087 1088 static const struct hfi_inst_ops venc_hfi_ops = { 1089 .buf_done = venc_buf_done, 1090 .event_notify = venc_event_notify, 1091 }; 1092 1093 static const struct v4l2_m2m_ops venc_m2m_ops = { 1094 .device_run = venus_helper_m2m_device_run, 1095 .job_abort = venus_helper_m2m_job_abort, 1096 }; 1097 1098 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq, 1099 struct vb2_queue *dst_vq) 1100 { 1101 struct venus_inst *inst = priv; 1102 int ret; 1103 1104 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1105 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1106 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1107 src_vq->ops = &venc_vb2_ops; 1108 src_vq->mem_ops = &vb2_dma_contig_memops; 1109 src_vq->drv_priv = inst; 1110 src_vq->buf_struct_size = sizeof(struct venus_buffer); 1111 src_vq->allow_zero_bytesused = 1; 1112 src_vq->min_buffers_needed = 1; 1113 src_vq->dev = inst->core->dev; 1114 if (inst->core->res->hfi_version == HFI_VERSION_1XX) 1115 src_vq->bidirectional = 1; 1116 ret = vb2_queue_init(src_vq); 1117 if (ret) 1118 return ret; 1119 1120 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1121 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1122 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1123 dst_vq->ops = &venc_vb2_ops; 1124 dst_vq->mem_ops = &vb2_dma_contig_memops; 1125 dst_vq->drv_priv = inst; 1126 dst_vq->buf_struct_size = sizeof(struct venus_buffer); 1127 dst_vq->allow_zero_bytesused = 1; 1128 dst_vq->min_buffers_needed = 1; 1129 dst_vq->dev = inst->core->dev; 1130 return vb2_queue_init(dst_vq); 1131 } 1132 1133 static void venc_inst_init(struct venus_inst *inst) 1134 { 1135 inst->fmt_cap = &venc_formats[3]; 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 1147 static 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_put_sync; 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 1209 err_m2m_release: 1210 v4l2_m2m_release(inst->m2m_dev); 1211 err_session_destroy: 1212 hfi_session_destroy(inst); 1213 err_ctrl_deinit: 1214 venc_ctrl_deinit(inst); 1215 err_put_sync: 1216 pm_runtime_put_sync(core->dev_enc); 1217 kfree(inst); 1218 return ret; 1219 } 1220 1221 static int venc_close(struct file *file) 1222 { 1223 struct venus_inst *inst = to_inst(file); 1224 1225 v4l2_m2m_ctx_release(inst->m2m_ctx); 1226 v4l2_m2m_release(inst->m2m_dev); 1227 venc_ctrl_deinit(inst); 1228 hfi_session_destroy(inst); 1229 mutex_destroy(&inst->lock); 1230 v4l2_fh_del(&inst->fh); 1231 v4l2_fh_exit(&inst->fh); 1232 1233 pm_runtime_put_sync(inst->core->dev_enc); 1234 1235 kfree(inst); 1236 return 0; 1237 } 1238 1239 static const struct v4l2_file_operations venc_fops = { 1240 .owner = THIS_MODULE, 1241 .open = venc_open, 1242 .release = venc_close, 1243 .unlocked_ioctl = video_ioctl2, 1244 .poll = v4l2_m2m_fop_poll, 1245 .mmap = v4l2_m2m_fop_mmap, 1246 }; 1247 1248 static int venc_probe(struct platform_device *pdev) 1249 { 1250 struct device *dev = &pdev->dev; 1251 struct video_device *vdev; 1252 struct venus_core *core; 1253 int ret; 1254 1255 if (!dev->parent) 1256 return -EPROBE_DEFER; 1257 1258 core = dev_get_drvdata(dev->parent); 1259 if (!core) 1260 return -EPROBE_DEFER; 1261 1262 platform_set_drvdata(pdev, core); 1263 1264 if (core->pm_ops->venc_get) { 1265 ret = core->pm_ops->venc_get(dev); 1266 if (ret) 1267 return ret; 1268 } 1269 1270 vdev = video_device_alloc(); 1271 if (!vdev) 1272 return -ENOMEM; 1273 1274 strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name)); 1275 vdev->release = video_device_release; 1276 vdev->fops = &venc_fops; 1277 vdev->ioctl_ops = &venc_ioctl_ops; 1278 vdev->vfl_dir = VFL_DIR_M2M; 1279 vdev->v4l2_dev = &core->v4l2_dev; 1280 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; 1281 1282 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 1283 if (ret) 1284 goto err_vdev_release; 1285 1286 core->vdev_enc = vdev; 1287 core->dev_enc = dev; 1288 1289 video_set_drvdata(vdev, core); 1290 pm_runtime_enable(dev); 1291 1292 return 0; 1293 1294 err_vdev_release: 1295 video_device_release(vdev); 1296 return ret; 1297 } 1298 1299 static int venc_remove(struct platform_device *pdev) 1300 { 1301 struct venus_core *core = dev_get_drvdata(pdev->dev.parent); 1302 1303 video_unregister_device(core->vdev_enc); 1304 pm_runtime_disable(core->dev_enc); 1305 1306 if (core->pm_ops->venc_put) 1307 core->pm_ops->venc_put(core->dev_enc); 1308 1309 return 0; 1310 } 1311 1312 static __maybe_unused int venc_runtime_suspend(struct device *dev) 1313 { 1314 struct venus_core *core = dev_get_drvdata(dev); 1315 const struct venus_pm_ops *pm_ops = core->pm_ops; 1316 int ret = 0; 1317 1318 if (pm_ops->venc_power) 1319 ret = pm_ops->venc_power(dev, POWER_OFF); 1320 1321 return ret; 1322 } 1323 1324 static __maybe_unused int venc_runtime_resume(struct device *dev) 1325 { 1326 struct venus_core *core = dev_get_drvdata(dev); 1327 const struct venus_pm_ops *pm_ops = core->pm_ops; 1328 int ret = 0; 1329 1330 if (pm_ops->venc_power) 1331 ret = pm_ops->venc_power(dev, POWER_ON); 1332 1333 return ret; 1334 } 1335 1336 static const struct dev_pm_ops venc_pm_ops = { 1337 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1338 pm_runtime_force_resume) 1339 SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL) 1340 }; 1341 1342 static const struct of_device_id venc_dt_match[] = { 1343 { .compatible = "venus-encoder" }, 1344 { } 1345 }; 1346 MODULE_DEVICE_TABLE(of, venc_dt_match); 1347 1348 static struct platform_driver qcom_venus_enc_driver = { 1349 .probe = venc_probe, 1350 .remove = venc_remove, 1351 .driver = { 1352 .name = "qcom-venus-encoder", 1353 .of_match_table = venc_dt_match, 1354 .pm = &venc_pm_ops, 1355 }, 1356 }; 1357 module_platform_driver(qcom_venus_enc_driver); 1358 1359 MODULE_ALIAS("platform:qcom-venus-encoder"); 1360 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver"); 1361 MODULE_LICENSE("GPL v2"); 1362