1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2017 Linaro Ltd.
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>
18 #include "hfi_venus_io.h"
19 #include "hfi_parser.h"
23 #include "pm_helpers.h"
25 #define NUM_B_FRAMES_MAX 4
28 * Three resons to keep MPLANE formats (despite that the number of planes
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
34 static const struct venus_format venc_formats[] = {
36 .pixfmt = V4L2_PIX_FMT_NV12,
38 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
40 .pixfmt = V4L2_PIX_FMT_MPEG4,
42 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
44 .pixfmt = V4L2_PIX_FMT_H263,
46 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
48 .pixfmt = V4L2_PIX_FMT_H264,
50 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
52 .pixfmt = V4L2_PIX_FMT_VP8,
54 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
56 .pixfmt = V4L2_PIX_FMT_HEVC,
58 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
62 static const struct venus_format *
63 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
65 const struct venus_format *fmt = venc_formats;
66 unsigned int size = ARRAY_SIZE(venc_formats);
69 for (i = 0; i < size; i++) {
70 if (fmt[i].pixfmt == pixfmt)
74 if (i == size || fmt[i].type != type)
77 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
78 !venus_helper_check_codec(inst, fmt[i].pixfmt))
84 static const struct venus_format *
85 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
87 const struct venus_format *fmt = venc_formats;
88 unsigned int size = ARRAY_SIZE(venc_formats);
89 unsigned int i, k = 0;
94 for (i = 0; i < size; i++) {
97 if (fmt[i].type != type)
99 valid = type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
100 venus_helper_check_codec(inst, fmt[i].pixfmt);
101 if (k == index && valid)
113 static int venc_v4l2_to_hfi(int id, int value)
116 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
118 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC:
120 return HFI_H264_ENTROPY_CAVLC;
121 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC:
122 return HFI_H264_ENTROPY_CABAC;
124 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
126 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED:
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;
140 venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
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));
149 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
151 struct venus_inst *inst = to_inst(file);
152 const struct venus_format *fmt;
154 fmt = find_format_by_index(inst, f->index, f->type);
156 memset(f->reserved, 0, sizeof(f->reserved));
161 f->pixelformat = fmt->pixfmt;
166 static const struct venus_format *
167 venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
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;
174 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
175 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
177 fmt = find_format(inst, pixmp->pixelformat, f->type);
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;
185 fmt = find_format(inst, pixmp->pixelformat, f->type);
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));
193 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
194 pixmp->height = ALIGN(pixmp->height, 32);
196 pixmp->width = ALIGN(pixmp->width, 2);
197 pixmp->height = ALIGN(pixmp->height, 2);
199 if (pixmp->field == V4L2_FIELD_ANY)
200 pixmp->field = V4L2_FIELD_NONE;
201 pixmp->num_planes = fmt->num_planes;
204 sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
207 pfmt[0].sizeimage = max(ALIGN(pfmt[0].sizeimage, SZ_4K), sizeimage);
209 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
210 pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
212 pfmt[0].bytesperline = 0;
217 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
219 struct venus_inst *inst = to_inst(file);
221 venc_try_fmt_common(inst, f);
226 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
228 struct venus_inst *inst = to_inst(file);
229 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
230 struct v4l2_pix_format_mplane orig_pixmp;
231 const struct venus_format *fmt;
232 struct v4l2_format format;
233 u32 pixfmt_out = 0, pixfmt_cap = 0;
236 q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
245 fmt = venc_try_fmt_common(inst, f);
249 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
250 pixfmt_out = pixmp->pixelformat;
251 pixfmt_cap = inst->fmt_cap->pixfmt;
252 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
253 pixfmt_cap = pixmp->pixelformat;
254 pixfmt_out = inst->fmt_out->pixfmt;
257 memset(&format, 0, sizeof(format));
259 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
260 format.fmt.pix_mp.pixelformat = pixfmt_out;
261 format.fmt.pix_mp.width = orig_pixmp.width;
262 format.fmt.pix_mp.height = orig_pixmp.height;
263 venc_try_fmt_common(inst, &format);
265 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
266 inst->out_width = format.fmt.pix_mp.width;
267 inst->out_height = format.fmt.pix_mp.height;
268 inst->colorspace = pixmp->colorspace;
269 inst->ycbcr_enc = pixmp->ycbcr_enc;
270 inst->quantization = pixmp->quantization;
271 inst->xfer_func = pixmp->xfer_func;
274 memset(&format, 0, sizeof(format));
276 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
277 format.fmt.pix_mp.pixelformat = pixfmt_cap;
278 format.fmt.pix_mp.width = orig_pixmp.width;
279 format.fmt.pix_mp.height = orig_pixmp.height;
280 venc_try_fmt_common(inst, &format);
282 inst->width = format.fmt.pix_mp.width;
283 inst->height = format.fmt.pix_mp.height;
285 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
287 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
289 inst->output_buf_size = pixmp->plane_fmt[0].sizeimage;
295 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
297 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
298 struct venus_inst *inst = to_inst(file);
299 const struct venus_format *fmt;
301 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
303 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
308 pixmp->pixelformat = fmt->pixfmt;
310 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
311 pixmp->width = inst->width;
312 pixmp->height = inst->height;
313 pixmp->colorspace = inst->colorspace;
314 pixmp->ycbcr_enc = inst->ycbcr_enc;
315 pixmp->quantization = inst->quantization;
316 pixmp->xfer_func = inst->xfer_func;
317 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
318 pixmp->width = inst->out_width;
319 pixmp->height = inst->out_height;
322 venc_try_fmt_common(inst, f);
328 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
330 struct venus_inst *inst = to_inst(file);
332 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
336 case V4L2_SEL_TGT_CROP_DEFAULT:
337 case V4L2_SEL_TGT_CROP_BOUNDS:
338 s->r.width = inst->width;
339 s->r.height = inst->height;
341 case V4L2_SEL_TGT_CROP:
342 s->r.width = inst->out_width;
343 s->r.height = inst->out_height;
356 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
358 struct venus_inst *inst = to_inst(file);
360 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
364 case V4L2_SEL_TGT_CROP:
365 if (s->r.width != inst->out_width ||
366 s->r.height != inst->out_height ||
367 s->r.top != 0 || s->r.left != 0)
377 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
379 struct venus_inst *inst = to_inst(file);
380 struct v4l2_outputparm *out = &a->parm.output;
381 struct v4l2_fract *timeperframe = &out->timeperframe;
382 u64 us_per_frame, fps;
384 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
385 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
388 memset(out->reserved, 0, sizeof(out->reserved));
390 if (!timeperframe->denominator)
391 timeperframe->denominator = inst->timeperframe.denominator;
392 if (!timeperframe->numerator)
393 timeperframe->numerator = inst->timeperframe.numerator;
395 out->capability = V4L2_CAP_TIMEPERFRAME;
397 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
398 do_div(us_per_frame, timeperframe->denominator);
403 fps = (u64)USEC_PER_SEC;
404 do_div(fps, us_per_frame);
406 inst->timeperframe = *timeperframe;
412 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
414 struct venus_inst *inst = to_inst(file);
416 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
417 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
420 a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
421 a->parm.output.timeperframe = inst->timeperframe;
426 static int venc_enum_framesizes(struct file *file, void *fh,
427 struct v4l2_frmsizeenum *fsize)
429 struct venus_inst *inst = to_inst(file);
430 const struct venus_format *fmt;
432 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
434 fmt = find_format(inst, fsize->pixel_format,
435 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
437 fmt = find_format(inst, fsize->pixel_format,
438 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
446 fsize->stepwise.min_width = frame_width_min(inst);
447 fsize->stepwise.max_width = frame_width_max(inst);
448 fsize->stepwise.step_width = frame_width_step(inst);
449 fsize->stepwise.min_height = frame_height_min(inst);
450 fsize->stepwise.max_height = frame_height_max(inst);
451 fsize->stepwise.step_height = frame_height_step(inst);
456 static int venc_enum_frameintervals(struct file *file, void *fh,
457 struct v4l2_frmivalenum *fival)
459 struct venus_inst *inst = to_inst(file);
460 const struct venus_format *fmt;
461 unsigned int framerate_factor = 1;
463 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
465 fmt = find_format(inst, fival->pixel_format,
466 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
468 fmt = find_format(inst, fival->pixel_format,
469 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
477 if (!fival->width || !fival->height)
480 if (fival->width > frame_width_max(inst) ||
481 fival->width < frame_width_min(inst) ||
482 fival->height > frame_height_max(inst) ||
483 fival->height < frame_height_min(inst))
486 if (IS_V1(inst->core)) {
487 /* framerate is reported in 1/65535 fps unit */
488 framerate_factor = (1 << 16);
491 fival->stepwise.min.numerator = 1;
492 fival->stepwise.min.denominator = frate_max(inst) / framerate_factor;
493 fival->stepwise.max.numerator = 1;
494 fival->stepwise.max.denominator = frate_min(inst) / framerate_factor;
495 fival->stepwise.step.numerator = 1;
496 fival->stepwise.step.denominator = frate_max(inst) / framerate_factor;
501 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
502 .vidioc_querycap = venc_querycap,
503 .vidioc_enum_fmt_vid_cap = venc_enum_fmt,
504 .vidioc_enum_fmt_vid_out = venc_enum_fmt,
505 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
506 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
507 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
508 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
509 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
510 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
511 .vidioc_g_selection = venc_g_selection,
512 .vidioc_s_selection = venc_s_selection,
513 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
514 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
515 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
516 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
517 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
518 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
519 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
520 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
521 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
522 .vidioc_s_parm = venc_s_parm,
523 .vidioc_g_parm = venc_g_parm,
524 .vidioc_enum_framesizes = venc_enum_framesizes,
525 .vidioc_enum_frameintervals = venc_enum_frameintervals,
526 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
527 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
530 static int venc_set_properties(struct venus_inst *inst)
532 struct venc_controls *ctr = &inst->controls.enc;
533 struct hfi_intra_period intra_period;
534 struct hfi_framerate frate;
535 struct hfi_bitrate brate;
536 struct hfi_idr_period idrp;
537 struct hfi_quantization quant;
538 struct hfi_quantization_range quant_range;
539 u32 ptype, rate_control, bitrate;
542 ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
546 ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
547 frate.buffer_type = HFI_BUFFER_OUTPUT;
548 frate.framerate = inst->fps * (1 << 16);
550 ret = hfi_session_set_property(inst, ptype, &frate);
554 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
555 struct hfi_h264_vui_timing_info info;
556 struct hfi_h264_entropy_control entropy;
557 struct hfi_h264_db_control deblock;
559 ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
561 info.fixed_framerate = 1;
562 info.time_scale = NSEC_PER_SEC;
564 ret = hfi_session_set_property(inst, ptype, &info);
568 ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL;
569 entropy.entropy_mode = venc_v4l2_to_hfi(
570 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
571 ctr->h264_entropy_mode);
572 entropy.cabac_model = HFI_H264_CABAC_MODEL_0;
574 ret = hfi_session_set_property(inst, ptype, &entropy);
578 ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL;
579 deblock.mode = venc_v4l2_to_hfi(
580 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
581 ctr->h264_loop_filter_mode);
582 deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha;
583 deblock.slice_beta_offset = ctr->h264_loop_filter_beta;
585 ret = hfi_session_set_property(inst, ptype, &deblock);
590 /* IDR periodicity, n:
591 * n = 0 - only the first I-frame is IDR frame
592 * n = 1 - all I-frames will be IDR frames
593 * n > 1 - every n-th I-frame will be IDR frame
595 ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
597 ret = hfi_session_set_property(inst, ptype, &idrp);
601 if (ctr->num_b_frames) {
602 u32 max_num_b_frames = NUM_B_FRAMES_MAX;
604 ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
605 ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
610 ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
611 intra_period.pframes = ctr->num_p_frames;
612 intra_period.bframes = ctr->num_b_frames;
614 ret = hfi_session_set_property(inst, ptype, &intra_period);
619 rate_control = HFI_RATE_CONTROL_OFF;
620 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
621 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_VBR_VFR :
622 HFI_RATE_CONTROL_VBR_CFR;
623 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
624 rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_CBR_VFR :
625 HFI_RATE_CONTROL_CBR_CFR;
626 else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
627 rate_control = HFI_RATE_CONTROL_CQ;
629 ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
630 ret = hfi_session_set_property(inst, ptype, &rate_control);
634 if (rate_control == HFI_RATE_CONTROL_CQ && ctr->const_quality) {
635 struct hfi_heic_frame_quality quality = {};
637 ptype = HFI_PROPERTY_CONFIG_HEIC_FRAME_QUALITY;
638 quality.frame_quality = ctr->const_quality;
639 ret = hfi_session_set_property(inst, ptype, &quality);
647 bitrate = ctr->bitrate;
649 ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
650 brate.bitrate = bitrate;
653 ret = hfi_session_set_property(inst, ptype, &brate);
657 if (!ctr->bitrate_peak)
660 bitrate = ctr->bitrate_peak;
662 ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
663 brate.bitrate = bitrate;
666 ret = hfi_session_set_property(inst, ptype, &brate);
670 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP;
671 quant.qp_i = ctr->h264_i_qp;
672 quant.qp_p = ctr->h264_p_qp;
673 quant.qp_b = ctr->h264_b_qp;
675 ret = hfi_session_set_property(inst, ptype, &quant);
679 ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE;
680 quant_range.min_qp = ctr->h264_min_qp;
681 quant_range.max_qp = ctr->h264_max_qp;
682 quant_range.layer_id = 0;
683 ret = hfi_session_set_property(inst, ptype, &quant_range);
687 ret = venus_helper_set_profile_level(inst, ctr->profile, ctr->level);
694 static int venc_init_session(struct venus_inst *inst)
698 ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
702 ret = venus_helper_set_input_resolution(inst, inst->width,
707 ret = venus_helper_set_output_resolution(inst, inst->width,
713 ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
717 ret = venus_helper_init_codec_freq_data(inst);
721 ret = venc_set_properties(inst);
727 hfi_session_deinit(inst);
731 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
733 struct hfi_buffer_requirements bufreq;
736 ret = venc_init_session(inst);
740 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
742 *num = bufreq.count_actual;
744 hfi_session_deinit(inst);
749 static int venc_queue_setup(struct vb2_queue *q,
750 unsigned int *num_buffers, unsigned int *num_planes,
751 unsigned int sizes[], struct device *alloc_devs[])
753 struct venus_inst *inst = vb2_get_drv_priv(q);
754 unsigned int num, min = 4;
758 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
759 *num_planes != inst->fmt_out->num_planes)
762 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
763 *num_planes != inst->fmt_cap->num_planes)
766 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
767 sizes[0] < inst->input_buf_size)
770 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
771 sizes[0] < inst->output_buf_size)
778 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
779 *num_planes = inst->fmt_out->num_planes;
781 ret = venc_out_num_buffers(inst, &num);
786 *num_buffers = max(*num_buffers, num);
787 inst->num_input_bufs = *num_buffers;
789 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
792 inst->input_buf_size = sizes[0];
794 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
795 *num_planes = inst->fmt_cap->num_planes;
796 *num_buffers = max(*num_buffers, min);
797 inst->num_output_bufs = *num_buffers;
798 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
801 sizes[0] = max(sizes[0], inst->output_buf_size);
802 inst->output_buf_size = sizes[0];
812 static int venc_verify_conf(struct venus_inst *inst)
814 enum hfi_version ver = inst->core->res->hfi_version;
815 struct hfi_buffer_requirements bufreq;
818 if (!inst->num_input_bufs || !inst->num_output_bufs)
821 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
825 if (inst->num_output_bufs < bufreq.count_actual ||
826 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
829 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
833 if (inst->num_input_bufs < bufreq.count_actual ||
834 inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
840 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
842 struct venus_inst *inst = vb2_get_drv_priv(q);
845 mutex_lock(&inst->lock);
847 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
848 inst->streamon_out = 1;
850 inst->streamon_cap = 1;
852 if (!(inst->streamon_out & inst->streamon_cap)) {
853 mutex_unlock(&inst->lock);
857 venus_helper_init_instance(inst);
859 inst->sequence_cap = 0;
860 inst->sequence_out = 0;
862 ret = venc_init_session(inst);
866 ret = venus_pm_acquire_core(inst);
870 ret = venc_set_properties(inst);
874 ret = venc_verify_conf(inst);
878 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
879 inst->num_output_bufs, 0);
883 ret = venus_helper_vb2_start_streaming(inst);
887 mutex_unlock(&inst->lock);
892 hfi_session_deinit(inst);
894 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
895 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
896 inst->streamon_out = 0;
898 inst->streamon_cap = 0;
899 mutex_unlock(&inst->lock);
903 static const struct vb2_ops venc_vb2_ops = {
904 .queue_setup = venc_queue_setup,
905 .buf_init = venus_helper_vb2_buf_init,
906 .buf_prepare = venus_helper_vb2_buf_prepare,
907 .start_streaming = venc_start_streaming,
908 .stop_streaming = venus_helper_vb2_stop_streaming,
909 .buf_queue = venus_helper_vb2_buf_queue,
912 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
913 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
914 u32 hfi_flags, u64 timestamp_us)
916 struct vb2_v4l2_buffer *vbuf;
917 struct vb2_buffer *vb;
920 if (buf_type == HFI_BUFFER_INPUT)
921 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
923 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
925 vbuf = venus_helper_find_buf(inst, type, tag);
931 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
933 vb2_set_plane_payload(vb, 0, bytesused + data_offset);
934 vb->planes[0].data_offset = data_offset;
935 vb->timestamp = timestamp_us * NSEC_PER_USEC;
936 vbuf->sequence = inst->sequence_cap++;
938 vbuf->sequence = inst->sequence_out++;
941 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
944 static void venc_event_notify(struct venus_inst *inst, u32 event,
945 struct hfi_event_data *data)
947 struct device *dev = inst->core->dev_enc;
949 if (event == EVT_SESSION_ERROR) {
950 inst->session_error = true;
951 dev_err(dev, "enc: event session error %x\n", inst->error);
955 static const struct hfi_inst_ops venc_hfi_ops = {
956 .buf_done = venc_buf_done,
957 .event_notify = venc_event_notify,
960 static const struct v4l2_m2m_ops venc_m2m_ops = {
961 .device_run = venus_helper_m2m_device_run,
962 .job_abort = venus_helper_m2m_job_abort,
965 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
966 struct vb2_queue *dst_vq)
968 struct venus_inst *inst = priv;
971 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
972 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
973 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
974 src_vq->ops = &venc_vb2_ops;
975 src_vq->mem_ops = &vb2_dma_sg_memops;
976 src_vq->drv_priv = inst;
977 src_vq->buf_struct_size = sizeof(struct venus_buffer);
978 src_vq->allow_zero_bytesused = 1;
979 src_vq->min_buffers_needed = 1;
980 src_vq->dev = inst->core->dev;
981 if (inst->core->res->hfi_version == HFI_VERSION_1XX)
982 src_vq->bidirectional = 1;
983 ret = vb2_queue_init(src_vq);
987 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
988 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
989 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
990 dst_vq->ops = &venc_vb2_ops;
991 dst_vq->mem_ops = &vb2_dma_sg_memops;
992 dst_vq->drv_priv = inst;
993 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
994 dst_vq->allow_zero_bytesused = 1;
995 dst_vq->min_buffers_needed = 1;
996 dst_vq->dev = inst->core->dev;
997 return vb2_queue_init(dst_vq);
1000 static void venc_inst_init(struct venus_inst *inst)
1002 inst->fmt_cap = &venc_formats[2];
1003 inst->fmt_out = &venc_formats[0];
1005 inst->height = ALIGN(720, 32);
1006 inst->out_width = 1280;
1007 inst->out_height = 720;
1009 inst->timeperframe.numerator = 1;
1010 inst->timeperframe.denominator = 15;
1011 inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1014 static int venc_open(struct file *file)
1016 struct venus_core *core = video_drvdata(file);
1017 struct venus_inst *inst;
1020 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1024 INIT_LIST_HEAD(&inst->dpbbufs);
1025 INIT_LIST_HEAD(&inst->registeredbufs);
1026 INIT_LIST_HEAD(&inst->internalbufs);
1027 INIT_LIST_HEAD(&inst->list);
1028 mutex_init(&inst->lock);
1031 inst->session_type = VIDC_SESSION_TYPE_ENC;
1032 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1033 inst->core_acquired = false;
1035 venus_helper_init_instance(inst);
1037 ret = pm_runtime_get_sync(core->dev_enc);
1041 ret = venc_ctrl_init(inst);
1045 ret = hfi_session_create(inst, &venc_hfi_ops);
1047 goto err_ctrl_deinit;
1049 venc_inst_init(inst);
1052 * create m2m device for every instance, the m2m context scheduling
1053 * is made by firmware side so we do not need to care about.
1055 inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1056 if (IS_ERR(inst->m2m_dev)) {
1057 ret = PTR_ERR(inst->m2m_dev);
1058 goto err_session_destroy;
1061 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1062 if (IS_ERR(inst->m2m_ctx)) {
1063 ret = PTR_ERR(inst->m2m_ctx);
1064 goto err_m2m_release;
1067 v4l2_fh_init(&inst->fh, core->vdev_enc);
1069 inst->fh.ctrl_handler = &inst->ctrl_handler;
1070 v4l2_fh_add(&inst->fh);
1071 inst->fh.m2m_ctx = inst->m2m_ctx;
1072 file->private_data = &inst->fh;
1077 v4l2_m2m_release(inst->m2m_dev);
1078 err_session_destroy:
1079 hfi_session_destroy(inst);
1081 venc_ctrl_deinit(inst);
1083 pm_runtime_put_sync(core->dev_enc);
1088 static int venc_close(struct file *file)
1090 struct venus_inst *inst = to_inst(file);
1092 v4l2_m2m_ctx_release(inst->m2m_ctx);
1093 v4l2_m2m_release(inst->m2m_dev);
1094 venc_ctrl_deinit(inst);
1095 hfi_session_destroy(inst);
1096 mutex_destroy(&inst->lock);
1097 v4l2_fh_del(&inst->fh);
1098 v4l2_fh_exit(&inst->fh);
1100 pm_runtime_put_sync(inst->core->dev_enc);
1106 static const struct v4l2_file_operations venc_fops = {
1107 .owner = THIS_MODULE,
1109 .release = venc_close,
1110 .unlocked_ioctl = video_ioctl2,
1111 .poll = v4l2_m2m_fop_poll,
1112 .mmap = v4l2_m2m_fop_mmap,
1115 static int venc_probe(struct platform_device *pdev)
1117 struct device *dev = &pdev->dev;
1118 struct video_device *vdev;
1119 struct venus_core *core;
1123 return -EPROBE_DEFER;
1125 core = dev_get_drvdata(dev->parent);
1127 return -EPROBE_DEFER;
1129 platform_set_drvdata(pdev, core);
1131 if (core->pm_ops->venc_get) {
1132 ret = core->pm_ops->venc_get(dev);
1137 vdev = video_device_alloc();
1141 strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1142 vdev->release = video_device_release;
1143 vdev->fops = &venc_fops;
1144 vdev->ioctl_ops = &venc_ioctl_ops;
1145 vdev->vfl_dir = VFL_DIR_M2M;
1146 vdev->v4l2_dev = &core->v4l2_dev;
1147 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1149 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1151 goto err_vdev_release;
1153 core->vdev_enc = vdev;
1154 core->dev_enc = dev;
1156 video_set_drvdata(vdev, core);
1157 pm_runtime_enable(dev);
1162 video_device_release(vdev);
1166 static int venc_remove(struct platform_device *pdev)
1168 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1170 video_unregister_device(core->vdev_enc);
1171 pm_runtime_disable(core->dev_enc);
1173 if (core->pm_ops->venc_put)
1174 core->pm_ops->venc_put(core->dev_enc);
1179 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1181 struct venus_core *core = dev_get_drvdata(dev);
1182 const struct venus_pm_ops *pm_ops = core->pm_ops;
1185 if (pm_ops->venc_power)
1186 ret = pm_ops->venc_power(dev, POWER_OFF);
1191 static __maybe_unused int venc_runtime_resume(struct device *dev)
1193 struct venus_core *core = dev_get_drvdata(dev);
1194 const struct venus_pm_ops *pm_ops = core->pm_ops;
1197 if (pm_ops->venc_power)
1198 ret = pm_ops->venc_power(dev, POWER_ON);
1203 static const struct dev_pm_ops venc_pm_ops = {
1204 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1205 pm_runtime_force_resume)
1206 SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1209 static const struct of_device_id venc_dt_match[] = {
1210 { .compatible = "venus-encoder" },
1213 MODULE_DEVICE_TABLE(of, venc_dt_match);
1215 static struct platform_driver qcom_venus_enc_driver = {
1216 .probe = venc_probe,
1217 .remove = venc_remove,
1219 .name = "qcom-venus-encoder",
1220 .of_match_table = venc_dt_match,
1224 module_platform_driver(qcom_venus_enc_driver);
1226 MODULE_ALIAS("platform:qcom-venus-encoder");
1227 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1228 MODULE_LICENSE("GPL v2");