2 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2017 Linaro Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/slab.h>
21 #include <media/v4l2-mem2mem.h>
22 #include <media/videobuf2-dma-sg.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-ctrls.h>
27 #include "hfi_venus_io.h"
28 #include "hfi_parser.h"
33 #define NUM_B_FRAMES_MAX 4
36 * Three resons to keep MPLANE formats (despite that the number of planes
38 * - the MPLANE formats allow only one plane to be used
39 * - the downstream driver use MPLANE formats too
40 * - future firmware versions could add support for >1 planes
42 static const struct venus_format venc_formats[] = {
44 .pixfmt = V4L2_PIX_FMT_NV12,
46 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
48 .pixfmt = V4L2_PIX_FMT_MPEG4,
50 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
52 .pixfmt = V4L2_PIX_FMT_H263,
54 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
56 .pixfmt = V4L2_PIX_FMT_H264,
58 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
60 .pixfmt = V4L2_PIX_FMT_VP8,
62 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
64 .pixfmt = V4L2_PIX_FMT_HEVC,
66 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
70 static const struct venus_format *
71 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
73 const struct venus_format *fmt = venc_formats;
74 unsigned int size = ARRAY_SIZE(venc_formats);
77 for (i = 0; i < size; i++) {
78 if (fmt[i].pixfmt == pixfmt)
82 if (i == size || fmt[i].type != type)
85 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
86 !venus_helper_check_codec(inst, fmt[i].pixfmt))
92 static const struct venus_format *
93 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
95 const struct venus_format *fmt = venc_formats;
96 unsigned int size = ARRAY_SIZE(venc_formats);
97 unsigned int i, k = 0;
102 for (i = 0; i < size; i++) {
105 if (fmt[i].type != type)
107 valid = type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
108 venus_helper_check_codec(inst, fmt[i].pixfmt);
109 if (k == index && valid)
121 static int venc_v4l2_to_hfi(int id, int value)
124 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
126 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0:
128 return HFI_MPEG4_LEVEL_0;
129 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B:
130 return HFI_MPEG4_LEVEL_0b;
131 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_1:
132 return HFI_MPEG4_LEVEL_1;
133 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_2:
134 return HFI_MPEG4_LEVEL_2;
135 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_3:
136 return HFI_MPEG4_LEVEL_3;
137 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_4:
138 return HFI_MPEG4_LEVEL_4;
139 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_5:
140 return HFI_MPEG4_LEVEL_5;
142 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
144 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
146 return HFI_MPEG4_PROFILE_SIMPLE;
147 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
148 return HFI_MPEG4_PROFILE_ADVANCEDSIMPLE;
150 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
152 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
153 return HFI_H264_PROFILE_BASELINE;
154 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
155 return HFI_H264_PROFILE_CONSTRAINED_BASE;
156 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
157 return HFI_H264_PROFILE_MAIN;
158 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
160 return HFI_H264_PROFILE_HIGH;
162 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
164 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
165 return HFI_H264_LEVEL_1;
166 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
167 return HFI_H264_LEVEL_1b;
168 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
169 return HFI_H264_LEVEL_11;
170 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
171 return HFI_H264_LEVEL_12;
172 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
173 return HFI_H264_LEVEL_13;
174 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
175 return HFI_H264_LEVEL_2;
176 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
177 return HFI_H264_LEVEL_21;
178 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
179 return HFI_H264_LEVEL_22;
180 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
181 return HFI_H264_LEVEL_3;
182 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
183 return HFI_H264_LEVEL_31;
184 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
185 return HFI_H264_LEVEL_32;
186 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
187 return HFI_H264_LEVEL_4;
188 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
189 return HFI_H264_LEVEL_41;
190 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
191 return HFI_H264_LEVEL_42;
192 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
194 return HFI_H264_LEVEL_5;
195 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
196 return HFI_H264_LEVEL_51;
198 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
200 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC:
202 return HFI_H264_ENTROPY_CAVLC;
203 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC:
204 return HFI_H264_ENTROPY_CABAC;
206 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
210 return HFI_VPX_PROFILE_VERSION_0;
212 return HFI_VPX_PROFILE_VERSION_1;
214 return HFI_VPX_PROFILE_VERSION_2;
216 return HFI_VPX_PROFILE_VERSION_3;
218 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
220 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED:
222 return HFI_H264_DB_MODE_ALL_BOUNDARY;
223 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED:
224 return HFI_H264_DB_MODE_DISABLE;
225 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY:
226 return HFI_H264_DB_MODE_SKIP_SLICE_BOUNDARY;
228 case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
230 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
232 return HFI_HEVC_PROFILE_MAIN;
233 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
234 return HFI_HEVC_PROFILE_MAIN_STILL_PIC;
235 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
236 return HFI_HEVC_PROFILE_MAIN10;
238 case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
240 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
242 return HFI_HEVC_LEVEL_1;
243 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
244 return HFI_HEVC_LEVEL_2;
245 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
246 return HFI_HEVC_LEVEL_21;
247 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
248 return HFI_HEVC_LEVEL_3;
249 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
250 return HFI_HEVC_LEVEL_31;
251 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
252 return HFI_HEVC_LEVEL_4;
253 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
254 return HFI_HEVC_LEVEL_41;
255 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
256 return HFI_HEVC_LEVEL_5;
257 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
258 return HFI_HEVC_LEVEL_51;
259 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2:
260 return HFI_HEVC_LEVEL_52;
261 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6:
262 return HFI_HEVC_LEVEL_6;
263 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1:
264 return HFI_HEVC_LEVEL_61;
265 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2:
266 return HFI_HEVC_LEVEL_62;
274 venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
276 strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver));
277 strlcpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card));
278 strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
283 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
285 struct venus_inst *inst = to_inst(file);
286 const struct venus_format *fmt;
288 fmt = find_format_by_index(inst, f->index, f->type);
290 memset(f->reserved, 0, sizeof(f->reserved));
295 f->pixelformat = fmt->pixfmt;
300 static const struct venus_format *
301 venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
303 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
304 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
305 const struct venus_format *fmt;
307 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
308 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
310 fmt = find_format(inst, pixmp->pixelformat, f->type);
312 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
313 pixmp->pixelformat = V4L2_PIX_FMT_H264;
314 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
315 pixmp->pixelformat = V4L2_PIX_FMT_NV12;
318 fmt = find_format(inst, pixmp->pixelformat, f->type);
323 pixmp->width = clamp(pixmp->width, frame_width_min(inst),
324 frame_width_max(inst));
325 pixmp->height = clamp(pixmp->height, frame_height_min(inst),
326 frame_height_max(inst));
328 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
329 pixmp->height = ALIGN(pixmp->height, 32);
331 pixmp->width = ALIGN(pixmp->width, 2);
332 pixmp->height = ALIGN(pixmp->height, 2);
334 if (pixmp->field == V4L2_FIELD_ANY)
335 pixmp->field = V4L2_FIELD_NONE;
336 pixmp->num_planes = fmt->num_planes;
339 pfmt[0].sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
343 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
344 pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
346 pfmt[0].bytesperline = 0;
351 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
353 struct venus_inst *inst = to_inst(file);
355 venc_try_fmt_common(inst, f);
360 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
362 struct venus_inst *inst = to_inst(file);
363 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
364 struct v4l2_pix_format_mplane orig_pixmp;
365 const struct venus_format *fmt;
366 struct v4l2_format format;
367 u32 pixfmt_out = 0, pixfmt_cap = 0;
371 fmt = venc_try_fmt_common(inst, f);
375 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
376 pixfmt_out = pixmp->pixelformat;
377 pixfmt_cap = inst->fmt_cap->pixfmt;
378 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
379 pixfmt_cap = pixmp->pixelformat;
380 pixfmt_out = inst->fmt_out->pixfmt;
383 memset(&format, 0, sizeof(format));
385 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
386 format.fmt.pix_mp.pixelformat = pixfmt_out;
387 format.fmt.pix_mp.width = orig_pixmp.width;
388 format.fmt.pix_mp.height = orig_pixmp.height;
389 venc_try_fmt_common(inst, &format);
391 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
392 inst->out_width = format.fmt.pix_mp.width;
393 inst->out_height = format.fmt.pix_mp.height;
394 inst->colorspace = pixmp->colorspace;
395 inst->ycbcr_enc = pixmp->ycbcr_enc;
396 inst->quantization = pixmp->quantization;
397 inst->xfer_func = pixmp->xfer_func;
400 memset(&format, 0, sizeof(format));
402 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
403 format.fmt.pix_mp.pixelformat = pixfmt_cap;
404 format.fmt.pix_mp.width = orig_pixmp.width;
405 format.fmt.pix_mp.height = orig_pixmp.height;
406 venc_try_fmt_common(inst, &format);
408 inst->width = format.fmt.pix_mp.width;
409 inst->height = format.fmt.pix_mp.height;
411 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
413 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
419 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
421 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
422 struct venus_inst *inst = to_inst(file);
423 const struct venus_format *fmt;
425 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
427 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
432 pixmp->pixelformat = fmt->pixfmt;
434 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
435 pixmp->width = inst->width;
436 pixmp->height = inst->height;
437 pixmp->colorspace = inst->colorspace;
438 pixmp->ycbcr_enc = inst->ycbcr_enc;
439 pixmp->quantization = inst->quantization;
440 pixmp->xfer_func = inst->xfer_func;
441 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
442 pixmp->width = inst->out_width;
443 pixmp->height = inst->out_height;
446 venc_try_fmt_common(inst, f);
452 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
454 struct venus_inst *inst = to_inst(file);
456 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
460 case V4L2_SEL_TGT_CROP_DEFAULT:
461 case V4L2_SEL_TGT_CROP_BOUNDS:
462 s->r.width = inst->width;
463 s->r.height = inst->height;
465 case V4L2_SEL_TGT_CROP:
466 s->r.width = inst->out_width;
467 s->r.height = inst->out_height;
480 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
482 struct venus_inst *inst = to_inst(file);
484 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
488 case V4L2_SEL_TGT_CROP:
489 if (s->r.width != inst->out_width ||
490 s->r.height != inst->out_height ||
491 s->r.top != 0 || s->r.left != 0)
501 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
503 struct venus_inst *inst = to_inst(file);
504 struct v4l2_outputparm *out = &a->parm.output;
505 struct v4l2_fract *timeperframe = &out->timeperframe;
506 u64 us_per_frame, fps;
508 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
509 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
512 memset(out->reserved, 0, sizeof(out->reserved));
514 if (!timeperframe->denominator)
515 timeperframe->denominator = inst->timeperframe.denominator;
516 if (!timeperframe->numerator)
517 timeperframe->numerator = inst->timeperframe.numerator;
519 out->capability = V4L2_CAP_TIMEPERFRAME;
521 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
522 do_div(us_per_frame, timeperframe->denominator);
527 fps = (u64)USEC_PER_SEC;
528 do_div(fps, us_per_frame);
530 inst->timeperframe = *timeperframe;
536 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
538 struct venus_inst *inst = to_inst(file);
540 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
541 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
544 a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
545 a->parm.output.timeperframe = inst->timeperframe;
550 static int venc_enum_framesizes(struct file *file, void *fh,
551 struct v4l2_frmsizeenum *fsize)
553 struct venus_inst *inst = to_inst(file);
554 const struct venus_format *fmt;
556 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
558 fmt = find_format(inst, fsize->pixel_format,
559 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
561 fmt = find_format(inst, fsize->pixel_format,
562 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
570 fsize->stepwise.min_width = frame_width_min(inst);
571 fsize->stepwise.max_width = frame_width_max(inst);
572 fsize->stepwise.step_width = frame_width_step(inst);
573 fsize->stepwise.min_height = frame_height_min(inst);
574 fsize->stepwise.max_height = frame_height_max(inst);
575 fsize->stepwise.step_height = frame_height_step(inst);
580 static int venc_enum_frameintervals(struct file *file, void *fh,
581 struct v4l2_frmivalenum *fival)
583 struct venus_inst *inst = to_inst(file);
584 const struct venus_format *fmt;
586 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
588 fmt = find_format(inst, fival->pixel_format,
589 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
591 fmt = find_format(inst, fival->pixel_format,
592 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
600 if (!fival->width || !fival->height)
603 if (fival->width > frame_width_max(inst) ||
604 fival->width < frame_width_min(inst) ||
605 fival->height > frame_height_max(inst) ||
606 fival->height < frame_height_min(inst))
609 fival->stepwise.min.numerator = 1;
610 fival->stepwise.min.denominator = frate_max(inst);
611 fival->stepwise.max.numerator = 1;
612 fival->stepwise.max.denominator = frate_min(inst);
613 fival->stepwise.step.numerator = 1;
614 fival->stepwise.step.denominator = frate_max(inst);
619 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
620 .vidioc_querycap = venc_querycap,
621 .vidioc_enum_fmt_vid_cap_mplane = venc_enum_fmt,
622 .vidioc_enum_fmt_vid_out_mplane = venc_enum_fmt,
623 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
624 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
625 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
626 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
627 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
628 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
629 .vidioc_g_selection = venc_g_selection,
630 .vidioc_s_selection = venc_s_selection,
631 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
632 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
633 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
634 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
635 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
636 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
637 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
638 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
639 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
640 .vidioc_s_parm = venc_s_parm,
641 .vidioc_g_parm = venc_g_parm,
642 .vidioc_enum_framesizes = venc_enum_framesizes,
643 .vidioc_enum_frameintervals = venc_enum_frameintervals,
644 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
645 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
648 static int venc_set_properties(struct venus_inst *inst)
650 struct venc_controls *ctr = &inst->controls.enc;
651 struct hfi_intra_period intra_period;
652 struct hfi_profile_level pl;
653 struct hfi_framerate frate;
654 struct hfi_bitrate brate;
655 struct hfi_idr_period idrp;
656 u32 ptype, rate_control, bitrate, profile = 0, level = 0;
659 ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
663 ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_2);
667 ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
668 frate.buffer_type = HFI_BUFFER_OUTPUT;
669 frate.framerate = inst->fps * (1 << 16);
671 ret = hfi_session_set_property(inst, ptype, &frate);
675 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
676 struct hfi_h264_vui_timing_info info;
677 struct hfi_h264_entropy_control entropy;
678 struct hfi_h264_db_control deblock;
680 ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
682 info.fixed_framerate = 1;
683 info.time_scale = NSEC_PER_SEC;
685 ret = hfi_session_set_property(inst, ptype, &info);
689 ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL;
690 entropy.entropy_mode = venc_v4l2_to_hfi(
691 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
692 ctr->h264_entropy_mode);
693 entropy.cabac_model = HFI_H264_CABAC_MODEL_0;
695 ret = hfi_session_set_property(inst, ptype, &entropy);
699 ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL;
700 deblock.mode = venc_v4l2_to_hfi(
701 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
702 ctr->h264_loop_filter_mode);
703 deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha;
704 deblock.slice_beta_offset = ctr->h264_loop_filter_beta;
706 ret = hfi_session_set_property(inst, ptype, &deblock);
711 /* IDR periodicity, n:
712 * n = 0 - only the first I-frame is IDR frame
713 * n = 1 - all I-frames will be IDR frames
714 * n > 1 - every n-th I-frame will be IDR frame
716 ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
718 ret = hfi_session_set_property(inst, ptype, &idrp);
722 if (ctr->num_b_frames) {
723 u32 max_num_b_frames = NUM_B_FRAMES_MAX;
725 ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
726 ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
731 ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
732 intra_period.pframes = ctr->num_p_frames;
733 intra_period.bframes = ctr->num_b_frames;
735 ret = hfi_session_set_property(inst, ptype, &intra_period);
739 if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
740 rate_control = HFI_RATE_CONTROL_VBR_CFR;
742 rate_control = HFI_RATE_CONTROL_CBR_CFR;
744 ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
745 ret = hfi_session_set_property(inst, ptype, &rate_control);
752 bitrate = ctr->bitrate;
754 ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
755 brate.bitrate = bitrate;
758 ret = hfi_session_set_property(inst, ptype, &brate);
762 if (!ctr->bitrate_peak)
765 bitrate = ctr->bitrate_peak;
767 ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
768 brate.bitrate = bitrate;
771 ret = hfi_session_set_property(inst, ptype, &brate);
775 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
776 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_PROFILE,
778 level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_LEVEL,
780 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
781 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
784 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_MPEG4) {
785 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
787 level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
789 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H263) {
792 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
793 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
795 level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
799 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
800 pl.profile = profile;
803 ret = hfi_session_set_property(inst, ptype, &pl);
810 static int venc_init_session(struct venus_inst *inst)
814 ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
818 ret = venus_helper_set_input_resolution(inst, inst->width,
823 ret = venus_helper_set_output_resolution(inst, inst->width,
829 ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
833 ret = venc_set_properties(inst);
839 hfi_session_deinit(inst);
843 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
845 struct hfi_buffer_requirements bufreq;
848 ret = venc_init_session(inst);
852 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
854 *num = bufreq.count_actual;
856 hfi_session_deinit(inst);
861 static int venc_queue_setup(struct vb2_queue *q,
862 unsigned int *num_buffers, unsigned int *num_planes,
863 unsigned int sizes[], struct device *alloc_devs[])
865 struct venus_inst *inst = vb2_get_drv_priv(q);
866 unsigned int num, min = 4;
870 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
871 *num_planes != inst->fmt_out->num_planes)
874 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
875 *num_planes != inst->fmt_cap->num_planes)
878 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
879 sizes[0] < inst->input_buf_size)
882 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
883 sizes[0] < inst->output_buf_size)
890 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
891 *num_planes = inst->fmt_out->num_planes;
893 ret = venc_out_num_buffers(inst, &num);
898 *num_buffers = max(*num_buffers, num);
899 inst->num_input_bufs = *num_buffers;
901 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
904 inst->input_buf_size = sizes[0];
906 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
907 *num_planes = inst->fmt_cap->num_planes;
908 *num_buffers = max(*num_buffers, min);
909 inst->num_output_bufs = *num_buffers;
910 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
913 inst->output_buf_size = sizes[0];
923 static int venc_verify_conf(struct venus_inst *inst)
925 enum hfi_version ver = inst->core->res->hfi_version;
926 struct hfi_buffer_requirements bufreq;
929 if (!inst->num_input_bufs || !inst->num_output_bufs)
932 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
936 if (inst->num_output_bufs < bufreq.count_actual ||
937 inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
940 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
944 if (inst->num_input_bufs < bufreq.count_actual ||
945 inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
951 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
953 struct venus_inst *inst = vb2_get_drv_priv(q);
956 mutex_lock(&inst->lock);
958 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
959 inst->streamon_out = 1;
961 inst->streamon_cap = 1;
963 if (!(inst->streamon_out & inst->streamon_cap)) {
964 mutex_unlock(&inst->lock);
968 venus_helper_init_instance(inst);
970 inst->sequence_cap = 0;
971 inst->sequence_out = 0;
973 ret = venc_init_session(inst);
977 ret = venc_set_properties(inst);
981 ret = venc_verify_conf(inst);
985 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
986 inst->num_output_bufs, 0);
990 ret = venus_helper_vb2_start_streaming(inst);
994 mutex_unlock(&inst->lock);
999 hfi_session_deinit(inst);
1001 venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
1002 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1003 inst->streamon_out = 0;
1005 inst->streamon_cap = 0;
1006 mutex_unlock(&inst->lock);
1010 static const struct vb2_ops venc_vb2_ops = {
1011 .queue_setup = venc_queue_setup,
1012 .buf_init = venus_helper_vb2_buf_init,
1013 .buf_prepare = venus_helper_vb2_buf_prepare,
1014 .start_streaming = venc_start_streaming,
1015 .stop_streaming = venus_helper_vb2_stop_streaming,
1016 .buf_queue = venus_helper_vb2_buf_queue,
1019 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
1020 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1021 u32 hfi_flags, u64 timestamp_us)
1023 struct vb2_v4l2_buffer *vbuf;
1024 struct vb2_buffer *vb;
1027 if (buf_type == HFI_BUFFER_INPUT)
1028 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1030 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1032 vbuf = venus_helper_find_buf(inst, type, tag);
1036 vbuf->flags = flags;
1038 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1039 vb = &vbuf->vb2_buf;
1040 vb2_set_plane_payload(vb, 0, bytesused + data_offset);
1041 vb->planes[0].data_offset = data_offset;
1042 vb->timestamp = timestamp_us * NSEC_PER_USEC;
1043 vbuf->sequence = inst->sequence_cap++;
1045 vbuf->sequence = inst->sequence_out++;
1048 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1051 static void venc_event_notify(struct venus_inst *inst, u32 event,
1052 struct hfi_event_data *data)
1054 struct device *dev = inst->core->dev_enc;
1056 if (event == EVT_SESSION_ERROR) {
1057 inst->session_error = true;
1058 dev_err(dev, "enc: event session error %x\n", inst->error);
1062 static const struct hfi_inst_ops venc_hfi_ops = {
1063 .buf_done = venc_buf_done,
1064 .event_notify = venc_event_notify,
1067 static const struct v4l2_m2m_ops venc_m2m_ops = {
1068 .device_run = venus_helper_m2m_device_run,
1069 .job_abort = venus_helper_m2m_job_abort,
1072 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1073 struct vb2_queue *dst_vq)
1075 struct venus_inst *inst = priv;
1078 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1079 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1080 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1081 src_vq->ops = &venc_vb2_ops;
1082 src_vq->mem_ops = &vb2_dma_sg_memops;
1083 src_vq->drv_priv = inst;
1084 src_vq->buf_struct_size = sizeof(struct venus_buffer);
1085 src_vq->allow_zero_bytesused = 1;
1086 src_vq->min_buffers_needed = 1;
1087 src_vq->dev = inst->core->dev;
1088 if (inst->core->res->hfi_version == HFI_VERSION_1XX)
1089 src_vq->bidirectional = 1;
1090 ret = vb2_queue_init(src_vq);
1094 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1095 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1096 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1097 dst_vq->ops = &venc_vb2_ops;
1098 dst_vq->mem_ops = &vb2_dma_sg_memops;
1099 dst_vq->drv_priv = inst;
1100 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1101 dst_vq->allow_zero_bytesused = 1;
1102 dst_vq->min_buffers_needed = 1;
1103 dst_vq->dev = inst->core->dev;
1104 ret = vb2_queue_init(dst_vq);
1106 vb2_queue_release(src_vq);
1113 static void venc_inst_init(struct venus_inst *inst)
1115 inst->fmt_cap = &venc_formats[2];
1116 inst->fmt_out = &venc_formats[0];
1118 inst->height = ALIGN(720, 32);
1119 inst->out_width = 1280;
1120 inst->out_height = 720;
1122 inst->timeperframe.numerator = 1;
1123 inst->timeperframe.denominator = 15;
1124 inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1127 static int venc_open(struct file *file)
1129 struct venus_core *core = video_drvdata(file);
1130 struct venus_inst *inst;
1133 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1137 INIT_LIST_HEAD(&inst->dpbbufs);
1138 INIT_LIST_HEAD(&inst->registeredbufs);
1139 INIT_LIST_HEAD(&inst->internalbufs);
1140 INIT_LIST_HEAD(&inst->list);
1141 mutex_init(&inst->lock);
1144 inst->session_type = VIDC_SESSION_TYPE_ENC;
1146 venus_helper_init_instance(inst);
1148 ret = pm_runtime_get_sync(core->dev_enc);
1152 ret = venc_ctrl_init(inst);
1156 ret = hfi_session_create(inst, &venc_hfi_ops);
1158 goto err_ctrl_deinit;
1160 venc_inst_init(inst);
1163 * create m2m device for every instance, the m2m context scheduling
1164 * is made by firmware side so we do not need to care about.
1166 inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1167 if (IS_ERR(inst->m2m_dev)) {
1168 ret = PTR_ERR(inst->m2m_dev);
1169 goto err_session_destroy;
1172 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1173 if (IS_ERR(inst->m2m_ctx)) {
1174 ret = PTR_ERR(inst->m2m_ctx);
1175 goto err_m2m_release;
1178 v4l2_fh_init(&inst->fh, core->vdev_enc);
1180 inst->fh.ctrl_handler = &inst->ctrl_handler;
1181 v4l2_fh_add(&inst->fh);
1182 inst->fh.m2m_ctx = inst->m2m_ctx;
1183 file->private_data = &inst->fh;
1188 v4l2_m2m_release(inst->m2m_dev);
1189 err_session_destroy:
1190 hfi_session_destroy(inst);
1192 venc_ctrl_deinit(inst);
1194 pm_runtime_put_sync(core->dev_enc);
1200 static int venc_close(struct file *file)
1202 struct venus_inst *inst = to_inst(file);
1204 v4l2_m2m_ctx_release(inst->m2m_ctx);
1205 v4l2_m2m_release(inst->m2m_dev);
1206 venc_ctrl_deinit(inst);
1207 hfi_session_destroy(inst);
1208 mutex_destroy(&inst->lock);
1209 v4l2_fh_del(&inst->fh);
1210 v4l2_fh_exit(&inst->fh);
1212 pm_runtime_put_sync(inst->core->dev_enc);
1218 static const struct v4l2_file_operations venc_fops = {
1219 .owner = THIS_MODULE,
1221 .release = venc_close,
1222 .unlocked_ioctl = video_ioctl2,
1223 .poll = v4l2_m2m_fop_poll,
1224 .mmap = v4l2_m2m_fop_mmap,
1227 static int venc_probe(struct platform_device *pdev)
1229 struct device *dev = &pdev->dev;
1230 struct video_device *vdev;
1231 struct venus_core *core;
1235 return -EPROBE_DEFER;
1237 core = dev_get_drvdata(dev->parent);
1239 return -EPROBE_DEFER;
1241 if (IS_V3(core) || IS_V4(core)) {
1242 core->core1_clk = devm_clk_get(dev, "core");
1243 if (IS_ERR(core->core1_clk))
1244 return PTR_ERR(core->core1_clk);
1248 core->core1_bus_clk = devm_clk_get(dev, "bus");
1249 if (IS_ERR(core->core1_bus_clk))
1250 return PTR_ERR(core->core1_bus_clk);
1253 platform_set_drvdata(pdev, core);
1255 vdev = video_device_alloc();
1259 strlcpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1260 vdev->release = video_device_release;
1261 vdev->fops = &venc_fops;
1262 vdev->ioctl_ops = &venc_ioctl_ops;
1263 vdev->vfl_dir = VFL_DIR_M2M;
1264 vdev->v4l2_dev = &core->v4l2_dev;
1265 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1267 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1269 goto err_vdev_release;
1271 core->vdev_enc = vdev;
1272 core->dev_enc = dev;
1274 video_set_drvdata(vdev, core);
1275 pm_runtime_enable(dev);
1280 video_device_release(vdev);
1284 static int venc_remove(struct platform_device *pdev)
1286 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1288 video_unregister_device(core->vdev_enc);
1289 pm_runtime_disable(core->dev_enc);
1294 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1296 struct venus_core *core = dev_get_drvdata(dev);
1302 ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, true);
1307 clk_disable_unprepare(core->core1_bus_clk);
1309 clk_disable_unprepare(core->core1_clk);
1311 return venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
1314 static __maybe_unused int venc_runtime_resume(struct device *dev)
1316 struct venus_core *core = dev_get_drvdata(dev);
1322 ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, true);
1326 ret = clk_prepare_enable(core->core1_clk);
1328 goto err_power_disable;
1331 ret = clk_prepare_enable(core->core1_bus_clk);
1334 goto err_unprepare_core1;
1336 return venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
1338 err_unprepare_core1:
1339 clk_disable_unprepare(core->core1_clk);
1341 venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
1345 static const struct dev_pm_ops venc_pm_ops = {
1346 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1347 pm_runtime_force_resume)
1348 SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1351 static const struct of_device_id venc_dt_match[] = {
1352 { .compatible = "venus-encoder" },
1355 MODULE_DEVICE_TABLE(of, venc_dt_match);
1357 static struct platform_driver qcom_venus_enc_driver = {
1358 .probe = venc_probe,
1359 .remove = venc_remove,
1361 .name = "qcom-venus-encoder",
1362 .of_match_table = venc_dt_match,
1366 module_platform_driver(qcom_venus_enc_driver);
1368 MODULE_ALIAS("platform:qcom-venus-encoder");
1369 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1370 MODULE_LICENSE("GPL v2");