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