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 	unsigned int framerate_factor = 1;
588 
589 	fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
590 
591 	fmt = find_format(inst, fival->pixel_format,
592 			  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
593 	if (!fmt) {
594 		fmt = find_format(inst, fival->pixel_format,
595 				  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
596 		if (!fmt)
597 			return -EINVAL;
598 	}
599 
600 	if (fival->index)
601 		return -EINVAL;
602 
603 	if (!fival->width || !fival->height)
604 		return -EINVAL;
605 
606 	if (fival->width > frame_width_max(inst) ||
607 	    fival->width < frame_width_min(inst) ||
608 	    fival->height > frame_height_max(inst) ||
609 	    fival->height < frame_height_min(inst))
610 		return -EINVAL;
611 
612 	if (IS_V1(inst->core)) {
613 		/* framerate is reported in 1/65535 fps unit */
614 		framerate_factor = (1 << 16);
615 	}
616 
617 	fival->stepwise.min.numerator = 1;
618 	fival->stepwise.min.denominator = frate_max(inst) / framerate_factor;
619 	fival->stepwise.max.numerator = 1;
620 	fival->stepwise.max.denominator = frate_min(inst) / framerate_factor;
621 	fival->stepwise.step.numerator = 1;
622 	fival->stepwise.step.denominator = frate_max(inst) / framerate_factor;
623 
624 	return 0;
625 }
626 
627 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
628 	.vidioc_querycap = venc_querycap,
629 	.vidioc_enum_fmt_vid_cap = venc_enum_fmt,
630 	.vidioc_enum_fmt_vid_out = venc_enum_fmt,
631 	.vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
632 	.vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
633 	.vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
634 	.vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
635 	.vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
636 	.vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
637 	.vidioc_g_selection = venc_g_selection,
638 	.vidioc_s_selection = venc_s_selection,
639 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
640 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
641 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
642 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
643 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
644 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
645 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
646 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
647 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
648 	.vidioc_s_parm = venc_s_parm,
649 	.vidioc_g_parm = venc_g_parm,
650 	.vidioc_enum_framesizes = venc_enum_framesizes,
651 	.vidioc_enum_frameintervals = venc_enum_frameintervals,
652 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
653 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
654 };
655 
656 static int venc_set_properties(struct venus_inst *inst)
657 {
658 	struct venc_controls *ctr = &inst->controls.enc;
659 	struct hfi_intra_period intra_period;
660 	struct hfi_profile_level pl;
661 	struct hfi_framerate frate;
662 	struct hfi_bitrate brate;
663 	struct hfi_idr_period idrp;
664 	struct hfi_quantization quant;
665 	struct hfi_quantization_range quant_range;
666 	u32 ptype, rate_control, bitrate, profile = 0, level = 0;
667 	int ret;
668 
669 	ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
670 	if (ret)
671 		return ret;
672 
673 	ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
674 	frate.buffer_type = HFI_BUFFER_OUTPUT;
675 	frate.framerate = inst->fps * (1 << 16);
676 
677 	ret = hfi_session_set_property(inst, ptype, &frate);
678 	if (ret)
679 		return ret;
680 
681 	if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
682 		struct hfi_h264_vui_timing_info info;
683 		struct hfi_h264_entropy_control entropy;
684 		struct hfi_h264_db_control deblock;
685 
686 		ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
687 		info.enable = 1;
688 		info.fixed_framerate = 1;
689 		info.time_scale = NSEC_PER_SEC;
690 
691 		ret = hfi_session_set_property(inst, ptype, &info);
692 		if (ret)
693 			return ret;
694 
695 		ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL;
696 		entropy.entropy_mode = venc_v4l2_to_hfi(
697 					  V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
698 					  ctr->h264_entropy_mode);
699 		entropy.cabac_model = HFI_H264_CABAC_MODEL_0;
700 
701 		ret = hfi_session_set_property(inst, ptype, &entropy);
702 		if (ret)
703 			return ret;
704 
705 		ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL;
706 		deblock.mode = venc_v4l2_to_hfi(
707 				      V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
708 				      ctr->h264_loop_filter_mode);
709 		deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha;
710 		deblock.slice_beta_offset = ctr->h264_loop_filter_beta;
711 
712 		ret = hfi_session_set_property(inst, ptype, &deblock);
713 		if (ret)
714 			return ret;
715 	}
716 
717 	/* IDR periodicity, n:
718 	 * n = 0 - only the first I-frame is IDR frame
719 	 * n = 1 - all I-frames will be IDR frames
720 	 * n > 1 - every n-th I-frame will be IDR frame
721 	 */
722 	ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
723 	idrp.idr_period = 0;
724 	ret = hfi_session_set_property(inst, ptype, &idrp);
725 	if (ret)
726 		return ret;
727 
728 	if (ctr->num_b_frames) {
729 		u32 max_num_b_frames = NUM_B_FRAMES_MAX;
730 
731 		ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
732 		ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
733 		if (ret)
734 			return ret;
735 	}
736 
737 	ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
738 	intra_period.pframes = ctr->num_p_frames;
739 	intra_period.bframes = ctr->num_b_frames;
740 
741 	ret = hfi_session_set_property(inst, ptype, &intra_period);
742 	if (ret)
743 		return ret;
744 
745 	if (!ctr->rc_enable)
746 		rate_control = HFI_RATE_CONTROL_OFF;
747 	else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
748 		rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_VBR_VFR :
749 						      HFI_RATE_CONTROL_VBR_CFR;
750 	else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
751 		rate_control = ctr->frame_skip_mode ? HFI_RATE_CONTROL_CBR_VFR :
752 						      HFI_RATE_CONTROL_CBR_CFR;
753 	else if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)
754 		rate_control = HFI_RATE_CONTROL_CQ;
755 
756 	ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
757 	ret = hfi_session_set_property(inst, ptype, &rate_control);
758 	if (ret)
759 		return ret;
760 
761 	if (rate_control == HFI_RATE_CONTROL_CQ && ctr->const_quality) {
762 		struct hfi_heic_frame_quality quality = {};
763 
764 		ptype = HFI_PROPERTY_CONFIG_HEIC_FRAME_QUALITY;
765 		quality.frame_quality = ctr->const_quality;
766 		ret = hfi_session_set_property(inst, ptype, &quality);
767 		if (ret)
768 			return ret;
769 	}
770 
771 	if (!ctr->bitrate)
772 		bitrate = 64000;
773 	else
774 		bitrate = ctr->bitrate;
775 
776 	ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
777 	brate.bitrate = bitrate;
778 	brate.layer_id = 0;
779 
780 	ret = hfi_session_set_property(inst, ptype, &brate);
781 	if (ret)
782 		return ret;
783 
784 	if (!ctr->bitrate_peak)
785 		bitrate *= 2;
786 	else
787 		bitrate = ctr->bitrate_peak;
788 
789 	ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
790 	brate.bitrate = bitrate;
791 	brate.layer_id = 0;
792 
793 	ret = hfi_session_set_property(inst, ptype, &brate);
794 	if (ret)
795 		return ret;
796 
797 	ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP;
798 	quant.qp_i = ctr->h264_i_qp;
799 	quant.qp_p = ctr->h264_p_qp;
800 	quant.qp_b = ctr->h264_b_qp;
801 	quant.layer_id = 0;
802 	ret = hfi_session_set_property(inst, ptype, &quant);
803 	if (ret)
804 		return ret;
805 
806 	ptype = HFI_PROPERTY_PARAM_VENC_SESSION_QP_RANGE;
807 	quant_range.min_qp = ctr->h264_min_qp;
808 	quant_range.max_qp = ctr->h264_max_qp;
809 	quant_range.layer_id = 0;
810 	ret = hfi_session_set_property(inst, ptype, &quant_range);
811 	if (ret)
812 		return ret;
813 
814 	if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
815 		profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_PROFILE,
816 					   ctr->profile.h264);
817 		level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_LEVEL,
818 					 ctr->level.h264);
819 	} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
820 		profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
821 					   ctr->profile.vpx);
822 		level = 0;
823 	} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_MPEG4) {
824 		profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
825 					   ctr->profile.mpeg4);
826 		level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
827 					 ctr->level.mpeg4);
828 	} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H263) {
829 		profile = 0;
830 		level = 0;
831 	} else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
832 		profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
833 					   ctr->profile.hevc);
834 		level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
835 					 ctr->level.hevc);
836 	}
837 
838 	ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
839 	pl.profile = profile;
840 	pl.level = level;
841 
842 	ret = hfi_session_set_property(inst, ptype, &pl);
843 	if (ret)
844 		return ret;
845 
846 	return 0;
847 }
848 
849 static int venc_init_session(struct venus_inst *inst)
850 {
851 	int ret;
852 
853 	ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
854 	if (ret)
855 		return ret;
856 
857 	ret = venus_helper_set_input_resolution(inst, inst->width,
858 						inst->height);
859 	if (ret)
860 		goto deinit;
861 
862 	ret = venus_helper_set_output_resolution(inst, inst->width,
863 						 inst->height,
864 						 HFI_BUFFER_OUTPUT);
865 	if (ret)
866 		goto deinit;
867 
868 	ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
869 	if (ret)
870 		goto deinit;
871 
872 	ret = venus_helper_init_codec_freq_data(inst);
873 	if (ret)
874 		goto deinit;
875 
876 	ret = venc_set_properties(inst);
877 	if (ret)
878 		goto deinit;
879 
880 	return 0;
881 deinit:
882 	hfi_session_deinit(inst);
883 	return ret;
884 }
885 
886 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
887 {
888 	struct hfi_buffer_requirements bufreq;
889 	int ret;
890 
891 	ret = venc_init_session(inst);
892 	if (ret)
893 		return ret;
894 
895 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
896 
897 	*num = bufreq.count_actual;
898 
899 	hfi_session_deinit(inst);
900 
901 	return ret;
902 }
903 
904 static int venc_queue_setup(struct vb2_queue *q,
905 			    unsigned int *num_buffers, unsigned int *num_planes,
906 			    unsigned int sizes[], struct device *alloc_devs[])
907 {
908 	struct venus_inst *inst = vb2_get_drv_priv(q);
909 	unsigned int num, min = 4;
910 	int ret = 0;
911 
912 	if (*num_planes) {
913 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
914 		    *num_planes != inst->fmt_out->num_planes)
915 			return -EINVAL;
916 
917 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
918 		    *num_planes != inst->fmt_cap->num_planes)
919 			return -EINVAL;
920 
921 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
922 		    sizes[0] < inst->input_buf_size)
923 			return -EINVAL;
924 
925 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
926 		    sizes[0] < inst->output_buf_size)
927 			return -EINVAL;
928 
929 		return 0;
930 	}
931 
932 	switch (q->type) {
933 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
934 		*num_planes = inst->fmt_out->num_planes;
935 
936 		ret = venc_out_num_buffers(inst, &num);
937 		if (ret)
938 			break;
939 
940 		num = max(num, min);
941 		*num_buffers = max(*num_buffers, num);
942 		inst->num_input_bufs = *num_buffers;
943 
944 		sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
945 						    inst->width,
946 						    inst->height);
947 		inst->input_buf_size = sizes[0];
948 		break;
949 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
950 		*num_planes = inst->fmt_cap->num_planes;
951 		*num_buffers = max(*num_buffers, min);
952 		inst->num_output_bufs = *num_buffers;
953 		sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
954 						    inst->width,
955 						    inst->height);
956 		sizes[0] = max(sizes[0], inst->output_buf_size);
957 		inst->output_buf_size = sizes[0];
958 		break;
959 	default:
960 		ret = -EINVAL;
961 		break;
962 	}
963 
964 	return ret;
965 }
966 
967 static int venc_verify_conf(struct venus_inst *inst)
968 {
969 	enum hfi_version ver = inst->core->res->hfi_version;
970 	struct hfi_buffer_requirements bufreq;
971 	int ret;
972 
973 	if (!inst->num_input_bufs || !inst->num_output_bufs)
974 		return -EINVAL;
975 
976 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
977 	if (ret)
978 		return ret;
979 
980 	if (inst->num_output_bufs < bufreq.count_actual ||
981 	    inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
982 		return -EINVAL;
983 
984 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
985 	if (ret)
986 		return ret;
987 
988 	if (inst->num_input_bufs < bufreq.count_actual ||
989 	    inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
990 		return -EINVAL;
991 
992 	return 0;
993 }
994 
995 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
996 {
997 	struct venus_inst *inst = vb2_get_drv_priv(q);
998 	int ret;
999 
1000 	mutex_lock(&inst->lock);
1001 
1002 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1003 		inst->streamon_out = 1;
1004 	else
1005 		inst->streamon_cap = 1;
1006 
1007 	if (!(inst->streamon_out & inst->streamon_cap)) {
1008 		mutex_unlock(&inst->lock);
1009 		return 0;
1010 	}
1011 
1012 	venus_helper_init_instance(inst);
1013 
1014 	inst->sequence_cap = 0;
1015 	inst->sequence_out = 0;
1016 
1017 	ret = venc_init_session(inst);
1018 	if (ret)
1019 		goto bufs_done;
1020 
1021 	ret = venus_pm_acquire_core(inst);
1022 	if (ret)
1023 		goto deinit_sess;
1024 
1025 	ret = venc_set_properties(inst);
1026 	if (ret)
1027 		goto deinit_sess;
1028 
1029 	ret = venc_verify_conf(inst);
1030 	if (ret)
1031 		goto deinit_sess;
1032 
1033 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1034 					inst->num_output_bufs, 0);
1035 	if (ret)
1036 		goto deinit_sess;
1037 
1038 	ret = venus_helper_vb2_start_streaming(inst);
1039 	if (ret)
1040 		goto deinit_sess;
1041 
1042 	mutex_unlock(&inst->lock);
1043 
1044 	return 0;
1045 
1046 deinit_sess:
1047 	hfi_session_deinit(inst);
1048 bufs_done:
1049 	venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1050 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1051 		inst->streamon_out = 0;
1052 	else
1053 		inst->streamon_cap = 0;
1054 	mutex_unlock(&inst->lock);
1055 	return ret;
1056 }
1057 
1058 static const struct vb2_ops venc_vb2_ops = {
1059 	.queue_setup = venc_queue_setup,
1060 	.buf_init = venus_helper_vb2_buf_init,
1061 	.buf_prepare = venus_helper_vb2_buf_prepare,
1062 	.start_streaming = venc_start_streaming,
1063 	.stop_streaming = venus_helper_vb2_stop_streaming,
1064 	.buf_queue = venus_helper_vb2_buf_queue,
1065 };
1066 
1067 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
1068 			  u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1069 			  u32 hfi_flags, u64 timestamp_us)
1070 {
1071 	struct vb2_v4l2_buffer *vbuf;
1072 	struct vb2_buffer *vb;
1073 	unsigned int type;
1074 
1075 	if (buf_type == HFI_BUFFER_INPUT)
1076 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1077 	else
1078 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1079 
1080 	vbuf = venus_helper_find_buf(inst, type, tag);
1081 	if (!vbuf)
1082 		return;
1083 
1084 	vbuf->flags = flags;
1085 
1086 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1087 		vb = &vbuf->vb2_buf;
1088 		vb2_set_plane_payload(vb, 0, bytesused + data_offset);
1089 		vb->planes[0].data_offset = data_offset;
1090 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
1091 		vbuf->sequence = inst->sequence_cap++;
1092 	} else {
1093 		vbuf->sequence = inst->sequence_out++;
1094 	}
1095 
1096 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1097 }
1098 
1099 static void venc_event_notify(struct venus_inst *inst, u32 event,
1100 			      struct hfi_event_data *data)
1101 {
1102 	struct device *dev = inst->core->dev_enc;
1103 
1104 	if (event == EVT_SESSION_ERROR) {
1105 		inst->session_error = true;
1106 		dev_err(dev, "enc: event session error %x\n", inst->error);
1107 	}
1108 }
1109 
1110 static const struct hfi_inst_ops venc_hfi_ops = {
1111 	.buf_done = venc_buf_done,
1112 	.event_notify = venc_event_notify,
1113 };
1114 
1115 static const struct v4l2_m2m_ops venc_m2m_ops = {
1116 	.device_run = venus_helper_m2m_device_run,
1117 	.job_abort = venus_helper_m2m_job_abort,
1118 };
1119 
1120 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1121 			  struct vb2_queue *dst_vq)
1122 {
1123 	struct venus_inst *inst = priv;
1124 	int ret;
1125 
1126 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1127 	src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1128 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1129 	src_vq->ops = &venc_vb2_ops;
1130 	src_vq->mem_ops = &vb2_dma_sg_memops;
1131 	src_vq->drv_priv = inst;
1132 	src_vq->buf_struct_size = sizeof(struct venus_buffer);
1133 	src_vq->allow_zero_bytesused = 1;
1134 	src_vq->min_buffers_needed = 1;
1135 	src_vq->dev = inst->core->dev;
1136 	if (inst->core->res->hfi_version == HFI_VERSION_1XX)
1137 		src_vq->bidirectional = 1;
1138 	ret = vb2_queue_init(src_vq);
1139 	if (ret)
1140 		return ret;
1141 
1142 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1143 	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1144 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1145 	dst_vq->ops = &venc_vb2_ops;
1146 	dst_vq->mem_ops = &vb2_dma_sg_memops;
1147 	dst_vq->drv_priv = inst;
1148 	dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1149 	dst_vq->allow_zero_bytesused = 1;
1150 	dst_vq->min_buffers_needed = 1;
1151 	dst_vq->dev = inst->core->dev;
1152 	return vb2_queue_init(dst_vq);
1153 }
1154 
1155 static void venc_inst_init(struct venus_inst *inst)
1156 {
1157 	inst->fmt_cap = &venc_formats[2];
1158 	inst->fmt_out = &venc_formats[0];
1159 	inst->width = 1280;
1160 	inst->height = ALIGN(720, 32);
1161 	inst->out_width = 1280;
1162 	inst->out_height = 720;
1163 	inst->fps = 15;
1164 	inst->timeperframe.numerator = 1;
1165 	inst->timeperframe.denominator = 15;
1166 	inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1167 }
1168 
1169 static int venc_open(struct file *file)
1170 {
1171 	struct venus_core *core = video_drvdata(file);
1172 	struct venus_inst *inst;
1173 	int ret;
1174 
1175 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1176 	if (!inst)
1177 		return -ENOMEM;
1178 
1179 	INIT_LIST_HEAD(&inst->dpbbufs);
1180 	INIT_LIST_HEAD(&inst->registeredbufs);
1181 	INIT_LIST_HEAD(&inst->internalbufs);
1182 	INIT_LIST_HEAD(&inst->list);
1183 	mutex_init(&inst->lock);
1184 
1185 	inst->core = core;
1186 	inst->session_type = VIDC_SESSION_TYPE_ENC;
1187 	inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1188 	inst->core_acquired = false;
1189 
1190 	venus_helper_init_instance(inst);
1191 
1192 	ret = pm_runtime_get_sync(core->dev_enc);
1193 	if (ret < 0)
1194 		goto err_put_sync;
1195 
1196 	ret = venc_ctrl_init(inst);
1197 	if (ret)
1198 		goto err_put_sync;
1199 
1200 	ret = hfi_session_create(inst, &venc_hfi_ops);
1201 	if (ret)
1202 		goto err_ctrl_deinit;
1203 
1204 	venc_inst_init(inst);
1205 
1206 	/*
1207 	 * create m2m device for every instance, the m2m context scheduling
1208 	 * is made by firmware side so we do not need to care about.
1209 	 */
1210 	inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1211 	if (IS_ERR(inst->m2m_dev)) {
1212 		ret = PTR_ERR(inst->m2m_dev);
1213 		goto err_session_destroy;
1214 	}
1215 
1216 	inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1217 	if (IS_ERR(inst->m2m_ctx)) {
1218 		ret = PTR_ERR(inst->m2m_ctx);
1219 		goto err_m2m_release;
1220 	}
1221 
1222 	v4l2_fh_init(&inst->fh, core->vdev_enc);
1223 
1224 	inst->fh.ctrl_handler = &inst->ctrl_handler;
1225 	v4l2_fh_add(&inst->fh);
1226 	inst->fh.m2m_ctx = inst->m2m_ctx;
1227 	file->private_data = &inst->fh;
1228 
1229 	return 0;
1230 
1231 err_m2m_release:
1232 	v4l2_m2m_release(inst->m2m_dev);
1233 err_session_destroy:
1234 	hfi_session_destroy(inst);
1235 err_ctrl_deinit:
1236 	venc_ctrl_deinit(inst);
1237 err_put_sync:
1238 	pm_runtime_put_sync(core->dev_enc);
1239 	kfree(inst);
1240 	return ret;
1241 }
1242 
1243 static int venc_close(struct file *file)
1244 {
1245 	struct venus_inst *inst = to_inst(file);
1246 
1247 	v4l2_m2m_ctx_release(inst->m2m_ctx);
1248 	v4l2_m2m_release(inst->m2m_dev);
1249 	venc_ctrl_deinit(inst);
1250 	hfi_session_destroy(inst);
1251 	mutex_destroy(&inst->lock);
1252 	v4l2_fh_del(&inst->fh);
1253 	v4l2_fh_exit(&inst->fh);
1254 
1255 	pm_runtime_put_sync(inst->core->dev_enc);
1256 
1257 	kfree(inst);
1258 	return 0;
1259 }
1260 
1261 static const struct v4l2_file_operations venc_fops = {
1262 	.owner = THIS_MODULE,
1263 	.open = venc_open,
1264 	.release = venc_close,
1265 	.unlocked_ioctl = video_ioctl2,
1266 	.poll = v4l2_m2m_fop_poll,
1267 	.mmap = v4l2_m2m_fop_mmap,
1268 };
1269 
1270 static int venc_probe(struct platform_device *pdev)
1271 {
1272 	struct device *dev = &pdev->dev;
1273 	struct video_device *vdev;
1274 	struct venus_core *core;
1275 	int ret;
1276 
1277 	if (!dev->parent)
1278 		return -EPROBE_DEFER;
1279 
1280 	core = dev_get_drvdata(dev->parent);
1281 	if (!core)
1282 		return -EPROBE_DEFER;
1283 
1284 	platform_set_drvdata(pdev, core);
1285 
1286 	if (core->pm_ops->venc_get) {
1287 		ret = core->pm_ops->venc_get(dev);
1288 		if (ret)
1289 			return ret;
1290 	}
1291 
1292 	vdev = video_device_alloc();
1293 	if (!vdev)
1294 		return -ENOMEM;
1295 
1296 	strscpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1297 	vdev->release = video_device_release;
1298 	vdev->fops = &venc_fops;
1299 	vdev->ioctl_ops = &venc_ioctl_ops;
1300 	vdev->vfl_dir = VFL_DIR_M2M;
1301 	vdev->v4l2_dev = &core->v4l2_dev;
1302 	vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1303 
1304 	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1305 	if (ret)
1306 		goto err_vdev_release;
1307 
1308 	core->vdev_enc = vdev;
1309 	core->dev_enc = dev;
1310 
1311 	video_set_drvdata(vdev, core);
1312 	pm_runtime_enable(dev);
1313 
1314 	return 0;
1315 
1316 err_vdev_release:
1317 	video_device_release(vdev);
1318 	return ret;
1319 }
1320 
1321 static int venc_remove(struct platform_device *pdev)
1322 {
1323 	struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1324 
1325 	video_unregister_device(core->vdev_enc);
1326 	pm_runtime_disable(core->dev_enc);
1327 
1328 	if (core->pm_ops->venc_put)
1329 		core->pm_ops->venc_put(core->dev_enc);
1330 
1331 	return 0;
1332 }
1333 
1334 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1335 {
1336 	struct venus_core *core = dev_get_drvdata(dev);
1337 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1338 	int ret = 0;
1339 
1340 	if (pm_ops->venc_power)
1341 		ret = pm_ops->venc_power(dev, POWER_OFF);
1342 
1343 	return ret;
1344 }
1345 
1346 static __maybe_unused int venc_runtime_resume(struct device *dev)
1347 {
1348 	struct venus_core *core = dev_get_drvdata(dev);
1349 	const struct venus_pm_ops *pm_ops = core->pm_ops;
1350 	int ret = 0;
1351 
1352 	if (pm_ops->venc_power)
1353 		ret = pm_ops->venc_power(dev, POWER_ON);
1354 
1355 	return ret;
1356 }
1357 
1358 static const struct dev_pm_ops venc_pm_ops = {
1359 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1360 				pm_runtime_force_resume)
1361 	SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1362 };
1363 
1364 static const struct of_device_id venc_dt_match[] = {
1365 	{ .compatible = "venus-encoder" },
1366 	{ }
1367 };
1368 MODULE_DEVICE_TABLE(of, venc_dt_match);
1369 
1370 static struct platform_driver qcom_venus_enc_driver = {
1371 	.probe = venc_probe,
1372 	.remove = venc_remove,
1373 	.driver = {
1374 		.name = "qcom-venus-encoder",
1375 		.of_match_table = venc_dt_match,
1376 		.pm = &venc_pm_ops,
1377 	},
1378 };
1379 module_platform_driver(qcom_venus_enc_driver);
1380 
1381 MODULE_ALIAS("platform:qcom-venus-encoder");
1382 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1383 MODULE_LICENSE("GPL v2");
1384