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