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