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 	struct vb2_queue *q;
361 
362 	q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
363 	if (!q)
364 		return -EINVAL;
365 
366 	if (vb2_is_busy(q))
367 		return -EBUSY;
368 
369 	orig_pixmp = *pixmp;
370 
371 	fmt = venc_try_fmt_common(inst, f);
372 	if (!fmt)
373 		return -EINVAL;
374 
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;
381 	}
382 
383 	memset(&format, 0, sizeof(format));
384 
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);
390 
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;
398 	}
399 
400 	memset(&format, 0, sizeof(format));
401 
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);
407 
408 	inst->width = format.fmt.pix_mp.width;
409 	inst->height = format.fmt.pix_mp.height;
410 
411 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
412 		inst->fmt_out = fmt;
413 	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
414 		inst->fmt_cap = fmt;
415 		inst->output_buf_size = pixmp->plane_fmt[0].sizeimage;
416 	}
417 
418 	return 0;
419 }
420 
421 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
422 {
423 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
424 	struct venus_inst *inst = to_inst(file);
425 	const struct venus_format *fmt;
426 
427 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
428 		fmt = inst->fmt_cap;
429 	else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
430 		fmt = inst->fmt_out;
431 	else
432 		return -EINVAL;
433 
434 	pixmp->pixelformat = fmt->pixfmt;
435 
436 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
437 		pixmp->width = inst->width;
438 		pixmp->height = inst->height;
439 		pixmp->colorspace = inst->colorspace;
440 		pixmp->ycbcr_enc = inst->ycbcr_enc;
441 		pixmp->quantization = inst->quantization;
442 		pixmp->xfer_func = inst->xfer_func;
443 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
444 		pixmp->width = inst->out_width;
445 		pixmp->height = inst->out_height;
446 	}
447 
448 	venc_try_fmt_common(inst, f);
449 
450 	return 0;
451 }
452 
453 static int
454 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
455 {
456 	struct venus_inst *inst = to_inst(file);
457 
458 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
459 		return -EINVAL;
460 
461 	switch (s->target) {
462 	case V4L2_SEL_TGT_CROP_DEFAULT:
463 	case V4L2_SEL_TGT_CROP_BOUNDS:
464 		s->r.width = inst->width;
465 		s->r.height = inst->height;
466 		break;
467 	case V4L2_SEL_TGT_CROP:
468 		s->r.width = inst->out_width;
469 		s->r.height = inst->out_height;
470 		break;
471 	default:
472 		return -EINVAL;
473 	}
474 
475 	s->r.top = 0;
476 	s->r.left = 0;
477 
478 	return 0;
479 }
480 
481 static int
482 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
483 {
484 	struct venus_inst *inst = to_inst(file);
485 
486 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
487 		return -EINVAL;
488 
489 	switch (s->target) {
490 	case V4L2_SEL_TGT_CROP:
491 		if (s->r.width != inst->out_width ||
492 		    s->r.height != inst->out_height ||
493 		    s->r.top != 0 || s->r.left != 0)
494 			return -EINVAL;
495 		break;
496 	default:
497 		return -EINVAL;
498 	}
499 
500 	return 0;
501 }
502 
503 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
504 {
505 	struct venus_inst *inst = to_inst(file);
506 	struct v4l2_outputparm *out = &a->parm.output;
507 	struct v4l2_fract *timeperframe = &out->timeperframe;
508 	u64 us_per_frame, fps;
509 
510 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
511 	    a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
512 		return -EINVAL;
513 
514 	memset(out->reserved, 0, sizeof(out->reserved));
515 
516 	if (!timeperframe->denominator)
517 		timeperframe->denominator = inst->timeperframe.denominator;
518 	if (!timeperframe->numerator)
519 		timeperframe->numerator = inst->timeperframe.numerator;
520 
521 	out->capability = V4L2_CAP_TIMEPERFRAME;
522 
523 	us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
524 	do_div(us_per_frame, timeperframe->denominator);
525 
526 	if (!us_per_frame)
527 		return -EINVAL;
528 
529 	fps = (u64)USEC_PER_SEC;
530 	do_div(fps, us_per_frame);
531 
532 	inst->timeperframe = *timeperframe;
533 	inst->fps = fps;
534 
535 	return 0;
536 }
537 
538 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
539 {
540 	struct venus_inst *inst = to_inst(file);
541 
542 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
543 	    a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
544 		return -EINVAL;
545 
546 	a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
547 	a->parm.output.timeperframe = inst->timeperframe;
548 
549 	return 0;
550 }
551 
552 static int venc_enum_framesizes(struct file *file, void *fh,
553 				struct v4l2_frmsizeenum *fsize)
554 {
555 	struct venus_inst *inst = to_inst(file);
556 	const struct venus_format *fmt;
557 
558 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
559 
560 	fmt = find_format(inst, fsize->pixel_format,
561 			  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
562 	if (!fmt) {
563 		fmt = find_format(inst, fsize->pixel_format,
564 				  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
565 		if (!fmt)
566 			return -EINVAL;
567 	}
568 
569 	if (fsize->index)
570 		return -EINVAL;
571 
572 	fsize->stepwise.min_width = frame_width_min(inst);
573 	fsize->stepwise.max_width = frame_width_max(inst);
574 	fsize->stepwise.step_width = frame_width_step(inst);
575 	fsize->stepwise.min_height = frame_height_min(inst);
576 	fsize->stepwise.max_height = frame_height_max(inst);
577 	fsize->stepwise.step_height = frame_height_step(inst);
578 
579 	return 0;
580 }
581 
582 static int venc_enum_frameintervals(struct file *file, void *fh,
583 				    struct v4l2_frmivalenum *fival)
584 {
585 	struct venus_inst *inst = to_inst(file);
586 	const struct venus_format *fmt;
587 
588 	fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
589 
590 	fmt = find_format(inst, fival->pixel_format,
591 			  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
592 	if (!fmt) {
593 		fmt = find_format(inst, fival->pixel_format,
594 				  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
595 		if (!fmt)
596 			return -EINVAL;
597 	}
598 
599 	if (fival->index)
600 		return -EINVAL;
601 
602 	if (!fival->width || !fival->height)
603 		return -EINVAL;
604 
605 	if (fival->width > frame_width_max(inst) ||
606 	    fival->width < frame_width_min(inst) ||
607 	    fival->height > frame_height_max(inst) ||
608 	    fival->height < frame_height_min(inst))
609 		return -EINVAL;
610 
611 	fival->stepwise.min.numerator = 1;
612 	fival->stepwise.min.denominator = frate_max(inst);
613 	fival->stepwise.max.numerator = 1;
614 	fival->stepwise.max.denominator = frate_min(inst);
615 	fival->stepwise.step.numerator = 1;
616 	fival->stepwise.step.denominator = frate_max(inst);
617 
618 	return 0;
619 }
620 
621 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
622 	.vidioc_querycap = venc_querycap,
623 	.vidioc_enum_fmt_vid_cap = venc_enum_fmt,
624 	.vidioc_enum_fmt_vid_out = venc_enum_fmt,
625 	.vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
626 	.vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
627 	.vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
628 	.vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
629 	.vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
630 	.vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
631 	.vidioc_g_selection = venc_g_selection,
632 	.vidioc_s_selection = venc_s_selection,
633 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
634 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
635 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
636 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
637 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
638 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
639 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
640 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
641 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
642 	.vidioc_s_parm = venc_s_parm,
643 	.vidioc_g_parm = venc_g_parm,
644 	.vidioc_enum_framesizes = venc_enum_framesizes,
645 	.vidioc_enum_frameintervals = venc_enum_frameintervals,
646 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
647 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
648 };
649 
650 static int venc_set_properties(struct venus_inst *inst)
651 {
652 	struct venc_controls *ctr = &inst->controls.enc;
653 	struct hfi_intra_period intra_period;
654 	struct hfi_profile_level pl;
655 	struct hfi_framerate frate;
656 	struct hfi_bitrate brate;
657 	struct hfi_idr_period idrp;
658 	struct hfi_quantization quant;
659 	struct hfi_quantization_range quant_range;
660 	u32 ptype, rate_control, bitrate, profile = 0, level = 0;
661 	int ret;
662 
663 	ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
664 	if (ret)
665 		return ret;
666 
667 	ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
668 	frate.buffer_type = HFI_BUFFER_OUTPUT;
669 	frate.framerate = inst->fps * (1 << 16);
670 
671 	ret = hfi_session_set_property(inst, ptype, &frate);
672 	if (ret)
673 		return ret;
674 
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;
679 
680 		ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
681 		info.enable = 1;
682 		info.fixed_framerate = 1;
683 		info.time_scale = NSEC_PER_SEC;
684 
685 		ret = hfi_session_set_property(inst, ptype, &info);
686 		if (ret)
687 			return ret;
688 
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;
694 
695 		ret = hfi_session_set_property(inst, ptype, &entropy);
696 		if (ret)
697 			return ret;
698 
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;
705 
706 		ret = hfi_session_set_property(inst, ptype, &deblock);
707 		if (ret)
708 			return ret;
709 	}
710 
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
715 	 */
716 	ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
717 	idrp.idr_period = 0;
718 	ret = hfi_session_set_property(inst, ptype, &idrp);
719 	if (ret)
720 		return ret;
721 
722 	if (ctr->num_b_frames) {
723 		u32 max_num_b_frames = NUM_B_FRAMES_MAX;
724 
725 		ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
726 		ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
727 		if (ret)
728 			return ret;
729 	}
730 
731 	ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
732 	intra_period.pframes = ctr->num_p_frames;
733 	intra_period.bframes = ctr->num_b_frames;
734 
735 	ret = hfi_session_set_property(inst, ptype, &intra_period);
736 	if (ret)
737 		return ret;
738 
739 	if (!ctr->rc_enable)
740 		rate_control = HFI_RATE_CONTROL_OFF;
741 	else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
742 		rate_control = HFI_RATE_CONTROL_VBR_CFR;
743 	else
744 		rate_control = HFI_RATE_CONTROL_CBR_CFR;
745 
746 	ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
747 	ret = hfi_session_set_property(inst, ptype, &rate_control);
748 	if (ret)
749 		return ret;
750 
751 	if (!ctr->bitrate)
752 		bitrate = 64000;
753 	else
754 		bitrate = ctr->bitrate;
755 
756 	ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
757 	brate.bitrate = bitrate;
758 	brate.layer_id = 0;
759 
760 	ret = hfi_session_set_property(inst, ptype, &brate);
761 	if (ret)
762 		return ret;
763 
764 	if (!ctr->bitrate_peak)
765 		bitrate *= 2;
766 	else
767 		bitrate = ctr->bitrate_peak;
768 
769 	ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
770 	brate.bitrate = bitrate;
771 	brate.layer_id = 0;
772 
773 	ret = hfi_session_set_property(inst, ptype, &brate);
774 	if (ret)
775 		return ret;
776 
777 	ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP;
778 	quant.qp_i = ctr->h264_i_qp;
779 	quant.qp_p = ctr->h264_p_qp;
780 	quant.qp_b = ctr->h264_b_qp;
781 	quant.layer_id = 0;
782 	ret = hfi_session_set_property(inst, ptype, &quant);
783 	if (ret)
784 		return ret;
785 
786 	ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE;
787 	quant_range.min_qp = ctr->h264_min_qp;
788 	quant_range.max_qp = ctr->h264_max_qp;
789 	quant_range.layer_id = 0;
790 	ret = hfi_session_set_property(inst, ptype, &quant_range);
791 	if (ret)
792 		return ret;
793 
794 	if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
795 		profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_PROFILE,
796 					   ctr->profile.h264);
797 		level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_LEVEL,
798 					 ctr->level.h264);
799 	} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
800 		profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
801 					   ctr->profile.vpx);
802 		level = 0;
803 	} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_MPEG4) {
804 		profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
805 					   ctr->profile.mpeg4);
806 		level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
807 					 ctr->level.mpeg4);
808 	} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H263) {
809 		profile = 0;
810 		level = 0;
811 	} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
812 		profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
813 					   ctr->profile.hevc);
814 		level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
815 					 ctr->level.hevc);
816 	}
817 
818 	ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
819 	pl.profile = profile;
820 	pl.level = level;
821 
822 	ret = hfi_session_set_property(inst, ptype, &pl);
823 	if (ret)
824 		return ret;
825 
826 	return 0;
827 }
828 
829 static int venc_init_session(struct venus_inst *inst)
830 {
831 	int ret;
832 
833 	ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
834 	if (ret)
835 		return ret;
836 
837 	ret = venus_helper_set_input_resolution(inst, inst->width,
838 						inst->height);
839 	if (ret)
840 		goto deinit;
841 
842 	ret = venus_helper_set_output_resolution(inst, inst->width,
843 						 inst->height,
844 						 HFI_BUFFER_OUTPUT);
845 	if (ret)
846 		goto deinit;
847 
848 	ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
849 	if (ret)
850 		goto deinit;
851 
852 	ret = venus_helper_init_codec_freq_data(inst);
853 	if (ret)
854 		goto deinit;
855 
856 	ret = venc_set_properties(inst);
857 	if (ret)
858 		goto deinit;
859 
860 	return 0;
861 deinit:
862 	hfi_session_deinit(inst);
863 	return ret;
864 }
865 
866 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
867 {
868 	struct hfi_buffer_requirements bufreq;
869 	int ret;
870 
871 	ret = venc_init_session(inst);
872 	if (ret)
873 		return ret;
874 
875 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
876 
877 	*num = bufreq.count_actual;
878 
879 	hfi_session_deinit(inst);
880 
881 	return ret;
882 }
883 
884 static int venc_queue_setup(struct vb2_queue *q,
885 			    unsigned int *num_buffers, unsigned int *num_planes,
886 			    unsigned int sizes[], struct device *alloc_devs[])
887 {
888 	struct venus_inst *inst = vb2_get_drv_priv(q);
889 	unsigned int num, min = 4;
890 	int ret = 0;
891 
892 	if (*num_planes) {
893 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
894 		    *num_planes != inst->fmt_out->num_planes)
895 			return -EINVAL;
896 
897 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
898 		    *num_planes != inst->fmt_cap->num_planes)
899 			return -EINVAL;
900 
901 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
902 		    sizes[0] < inst->input_buf_size)
903 			return -EINVAL;
904 
905 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
906 		    sizes[0] < inst->output_buf_size)
907 			return -EINVAL;
908 
909 		return 0;
910 	}
911 
912 	switch (q->type) {
913 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
914 		*num_planes = inst->fmt_out->num_planes;
915 
916 		ret = venc_out_num_buffers(inst, &num);
917 		if (ret)
918 			break;
919 
920 		num = max(num, min);
921 		*num_buffers = max(*num_buffers, num);
922 		inst->num_input_bufs = *num_buffers;
923 
924 		sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
925 						    inst->width,
926 						    inst->height);
927 		inst->input_buf_size = sizes[0];
928 		break;
929 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
930 		*num_planes = inst->fmt_cap->num_planes;
931 		*num_buffers = max(*num_buffers, min);
932 		inst->num_output_bufs = *num_buffers;
933 		sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
934 						    inst->width,
935 						    inst->height);
936 		sizes[0] = max(sizes[0], inst->output_buf_size);
937 		inst->output_buf_size = sizes[0];
938 		break;
939 	default:
940 		ret = -EINVAL;
941 		break;
942 	}
943 
944 	return ret;
945 }
946 
947 static int venc_verify_conf(struct venus_inst *inst)
948 {
949 	enum hfi_version ver = inst->core->res->hfi_version;
950 	struct hfi_buffer_requirements bufreq;
951 	int ret;
952 
953 	if (!inst->num_input_bufs || !inst->num_output_bufs)
954 		return -EINVAL;
955 
956 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
957 	if (ret)
958 		return ret;
959 
960 	if (inst->num_output_bufs < bufreq.count_actual ||
961 	    inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
962 		return -EINVAL;
963 
964 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
965 	if (ret)
966 		return ret;
967 
968 	if (inst->num_input_bufs < bufreq.count_actual ||
969 	    inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
970 		return -EINVAL;
971 
972 	return 0;
973 }
974 
975 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
976 {
977 	struct venus_inst *inst = vb2_get_drv_priv(q);
978 	int ret;
979 
980 	mutex_lock(&inst->lock);
981 
982 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
983 		inst->streamon_out = 1;
984 	else
985 		inst->streamon_cap = 1;
986 
987 	if (!(inst->streamon_out & inst->streamon_cap)) {
988 		mutex_unlock(&inst->lock);
989 		return 0;
990 	}
991 
992 	venus_helper_init_instance(inst);
993 
994 	inst->sequence_cap = 0;
995 	inst->sequence_out = 0;
996 
997 	ret = venc_init_session(inst);
998 	if (ret)
999 		goto bufs_done;
1000 
1001 	ret = venus_pm_acquire_core(inst);
1002 	if (ret)
1003 		goto deinit_sess;
1004 
1005 	ret = venc_set_properties(inst);
1006 	if (ret)
1007 		goto deinit_sess;
1008 
1009 	ret = venc_verify_conf(inst);
1010 	if (ret)
1011 		goto deinit_sess;
1012 
1013 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1014 					inst->num_output_bufs, 0);
1015 	if (ret)
1016 		goto deinit_sess;
1017 
1018 	ret = venus_helper_vb2_start_streaming(inst);
1019 	if (ret)
1020 		goto deinit_sess;
1021 
1022 	mutex_unlock(&inst->lock);
1023 
1024 	return 0;
1025 
1026 deinit_sess:
1027 	hfi_session_deinit(inst);
1028 bufs_done:
1029 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1030 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1031 		inst->streamon_out = 0;
1032 	else
1033 		inst->streamon_cap = 0;
1034 	mutex_unlock(&inst->lock);
1035 	return ret;
1036 }
1037 
1038 static const struct vb2_ops venc_vb2_ops = {
1039 	.queue_setup = venc_queue_setup,
1040 	.buf_init = venus_helper_vb2_buf_init,
1041 	.buf_prepare = venus_helper_vb2_buf_prepare,
1042 	.start_streaming = venc_start_streaming,
1043 	.stop_streaming = venus_helper_vb2_stop_streaming,
1044 	.buf_queue = venus_helper_vb2_buf_queue,
1045 };
1046 
1047 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
1048 			  u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1049 			  u32 hfi_flags, u64 timestamp_us)
1050 {
1051 	struct vb2_v4l2_buffer *vbuf;
1052 	struct vb2_buffer *vb;
1053 	unsigned int type;
1054 
1055 	if (buf_type == HFI_BUFFER_INPUT)
1056 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1057 	else
1058 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1059 
1060 	vbuf = venus_helper_find_buf(inst, type, tag);
1061 	if (!vbuf)
1062 		return;
1063 
1064 	vbuf->flags = flags;
1065 
1066 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1067 		vb = &vbuf->vb2_buf;
1068 		vb2_set_plane_payload(vb, 0, bytesused + data_offset);
1069 		vb->planes[0].data_offset = data_offset;
1070 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
1071 		vbuf->sequence = inst->sequence_cap++;
1072 	} else {
1073 		vbuf->sequence = inst->sequence_out++;
1074 	}
1075 
1076 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1077 }
1078 
1079 static void venc_event_notify(struct venus_inst *inst, u32 event,
1080 			      struct hfi_event_data *data)
1081 {
1082 	struct device *dev = inst->core->dev_enc;
1083 
1084 	if (event == EVT_SESSION_ERROR) {
1085 		inst->session_error = true;
1086 		dev_err(dev, "enc: event session error %x\n", inst->error);
1087 	}
1088 }
1089 
1090 static const struct hfi_inst_ops venc_hfi_ops = {
1091 	.buf_done = venc_buf_done,
1092 	.event_notify = venc_event_notify,
1093 };
1094 
1095 static const struct v4l2_m2m_ops venc_m2m_ops = {
1096 	.device_run = venus_helper_m2m_device_run,
1097 	.job_abort = venus_helper_m2m_job_abort,
1098 };
1099 
1100 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1101 			  struct vb2_queue *dst_vq)
1102 {
1103 	struct venus_inst *inst = priv;
1104 	int ret;
1105 
1106 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1107 	src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1108 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1109 	src_vq->ops = &venc_vb2_ops;
1110 	src_vq->mem_ops = &vb2_dma_sg_memops;
1111 	src_vq->drv_priv = inst;
1112 	src_vq->buf_struct_size = sizeof(struct venus_buffer);
1113 	src_vq->allow_zero_bytesused = 1;
1114 	src_vq->min_buffers_needed = 1;
1115 	src_vq->dev = inst->core->dev;
1116 	if (inst->core->res->hfi_version == HFI_VERSION_1XX)
1117 		src_vq->bidirectional = 1;
1118 	ret = vb2_queue_init(src_vq);
1119 	if (ret)
1120 		return ret;
1121 
1122 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1123 	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1124 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1125 	dst_vq->ops = &venc_vb2_ops;
1126 	dst_vq->mem_ops = &vb2_dma_sg_memops;
1127 	dst_vq->drv_priv = inst;
1128 	dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1129 	dst_vq->allow_zero_bytesused = 1;
1130 	dst_vq->min_buffers_needed = 1;
1131 	dst_vq->dev = inst->core->dev;
1132 	ret = vb2_queue_init(dst_vq);
1133 	if (ret) {
1134 		vb2_queue_release(src_vq);
1135 		return ret;
1136 	}
1137 
1138 	return 0;
1139 }
1140 
1141 static void venc_inst_init(struct venus_inst *inst)
1142 {
1143 	inst->fmt_cap = &venc_formats[2];
1144 	inst->fmt_out = &venc_formats[0];
1145 	inst->width = 1280;
1146 	inst->height = ALIGN(720, 32);
1147 	inst->out_width = 1280;
1148 	inst->out_height = 720;
1149 	inst->fps = 15;
1150 	inst->timeperframe.numerator = 1;
1151 	inst->timeperframe.denominator = 15;
1152 	inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1153 }
1154 
1155 static int venc_open(struct file *file)
1156 {
1157 	struct venus_core *core = video_drvdata(file);
1158 	struct venus_inst *inst;
1159 	int ret;
1160 
1161 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1162 	if (!inst)
1163 		return -ENOMEM;
1164 
1165 	INIT_LIST_HEAD(&inst->dpbbufs);
1166 	INIT_LIST_HEAD(&inst->registeredbufs);
1167 	INIT_LIST_HEAD(&inst->internalbufs);
1168 	INIT_LIST_HEAD(&inst->list);
1169 	mutex_init(&inst->lock);
1170 
1171 	inst->core = core;
1172 	inst->session_type = VIDC_SESSION_TYPE_ENC;
1173 	inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1174 	inst->core_acquired = false;
1175 
1176 	venus_helper_init_instance(inst);
1177 
1178 	ret = pm_runtime_get_sync(core->dev_enc);
1179 	if (ret < 0)
1180 		goto err_free_inst;
1181 
1182 	ret = venc_ctrl_init(inst);
1183 	if (ret)
1184 		goto err_put_sync;
1185 
1186 	ret = hfi_session_create(inst, &venc_hfi_ops);
1187 	if (ret)
1188 		goto err_ctrl_deinit;
1189 
1190 	venc_inst_init(inst);
1191 
1192 	/*
1193 	 * create m2m device for every instance, the m2m context scheduling
1194 	 * is made by firmware side so we do not need to care about.
1195 	 */
1196 	inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1197 	if (IS_ERR(inst->m2m_dev)) {
1198 		ret = PTR_ERR(inst->m2m_dev);
1199 		goto err_session_destroy;
1200 	}
1201 
1202 	inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1203 	if (IS_ERR(inst->m2m_ctx)) {
1204 		ret = PTR_ERR(inst->m2m_ctx);
1205 		goto err_m2m_release;
1206 	}
1207 
1208 	v4l2_fh_init(&inst->fh, core->vdev_enc);
1209 
1210 	inst->fh.ctrl_handler = &inst->ctrl_handler;
1211 	v4l2_fh_add(&inst->fh);
1212 	inst->fh.m2m_ctx = inst->m2m_ctx;
1213 	file->private_data = &inst->fh;
1214 
1215 	return 0;
1216 
1217 err_m2m_release:
1218 	v4l2_m2m_release(inst->m2m_dev);
1219 err_session_destroy:
1220 	hfi_session_destroy(inst);
1221 err_ctrl_deinit:
1222 	venc_ctrl_deinit(inst);
1223 err_put_sync:
1224 	pm_runtime_put_sync(core->dev_enc);
1225 err_free_inst:
1226 	kfree(inst);
1227 	return ret;
1228 }
1229 
1230 static int venc_close(struct file *file)
1231 {
1232 	struct venus_inst *inst = to_inst(file);
1233 
1234 	v4l2_m2m_ctx_release(inst->m2m_ctx);
1235 	v4l2_m2m_release(inst->m2m_dev);
1236 	venc_ctrl_deinit(inst);
1237 	hfi_session_destroy(inst);
1238 	mutex_destroy(&inst->lock);
1239 	v4l2_fh_del(&inst->fh);
1240 	v4l2_fh_exit(&inst->fh);
1241 
1242 	pm_runtime_put_sync(inst->core->dev_enc);
1243 
1244 	kfree(inst);
1245 	return 0;
1246 }
1247 
1248 static const struct v4l2_file_operations venc_fops = {
1249 	.owner = THIS_MODULE,
1250 	.open = venc_open,
1251 	.release = venc_close,
1252 	.unlocked_ioctl = video_ioctl2,
1253 	.poll = v4l2_m2m_fop_poll,
1254 	.mmap = v4l2_m2m_fop_mmap,
1255 };
1256 
1257 static int venc_probe(struct platform_device *pdev)
1258 {
1259 	struct device *dev = &pdev->dev;
1260 	struct video_device *vdev;
1261 	struct venus_core *core;
1262 	int ret;
1263 
1264 	if (!dev->parent)
1265 		return -EPROBE_DEFER;
1266 
1267 	core = dev_get_drvdata(dev->parent);
1268 	if (!core)
1269 		return -EPROBE_DEFER;
1270 
1271 	platform_set_drvdata(pdev, core);
1272 
1273 	if (core->pm_ops->venc_get) {
1274 		ret = core->pm_ops->venc_get(dev);
1275 		if (ret)
1276 			return ret;
1277 	}
1278 
1279 	vdev = video_device_alloc();
1280 	if (!vdev)
1281 		return -ENOMEM;
1282 
1283 	strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1284 	vdev->release = video_device_release;
1285 	vdev->fops = &venc_fops;
1286 	vdev->ioctl_ops = &venc_ioctl_ops;
1287 	vdev->vfl_dir = VFL_DIR_M2M;
1288 	vdev->v4l2_dev = &core->v4l2_dev;
1289 	vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1290 
1291 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1292 	if (ret)
1293 		goto err_vdev_release;
1294 
1295 	core->vdev_enc = vdev;
1296 	core->dev_enc = dev;
1297 
1298 	video_set_drvdata(vdev, core);
1299 	pm_runtime_enable(dev);
1300 
1301 	return 0;
1302 
1303 err_vdev_release:
1304 	video_device_release(vdev);
1305 	return ret;
1306 }
1307 
1308 static int venc_remove(struct platform_device *pdev)
1309 {
1310 	struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1311 
1312 	video_unregister_device(core->vdev_enc);
1313 	pm_runtime_disable(core->dev_enc);
1314 
1315 	if (core->pm_ops->venc_put)
1316 		core->pm_ops->venc_put(core->dev_enc);
1317 
1318 	return 0;
1319 }
1320 
1321 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1322 {
1323 	struct venus_core *core = dev_get_drvdata(dev);
1324 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1325 	int ret = 0;
1326 
1327 	if (pm_ops->venc_power)
1328 		ret = pm_ops->venc_power(dev, POWER_OFF);
1329 
1330 	return ret;
1331 }
1332 
1333 static __maybe_unused int venc_runtime_resume(struct device *dev)
1334 {
1335 	struct venus_core *core = dev_get_drvdata(dev);
1336 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1337 	int ret = 0;
1338 
1339 	if (pm_ops->venc_power)
1340 		ret = pm_ops->venc_power(dev, POWER_ON);
1341 
1342 	return ret;
1343 }
1344 
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)
1349 };
1350 
1351 static const struct of_device_id venc_dt_match[] = {
1352 	{ .compatible = "venus-encoder" },
1353 	{ }
1354 };
1355 MODULE_DEVICE_TABLE(of, venc_dt_match);
1356 
1357 static struct platform_driver qcom_venus_enc_driver = {
1358 	.probe = venc_probe,
1359 	.remove = venc_remove,
1360 	.driver = {
1361 		.name = "qcom-venus-encoder",
1362 		.of_match_table = venc_dt_match,
1363 		.pm = &venc_pm_ops,
1364 	},
1365 };
1366 module_platform_driver(qcom_venus_enc_driver);
1367 
1368 MODULE_ALIAS("platform:qcom-venus-encoder");
1369 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1370 MODULE_LICENSE("GPL v2");
1371