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