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/mod_devicetable.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/slab.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-mem2mem.h>
25 #include <media/videobuf2-dma-sg.h>
26 
27 #include "hfi_venus_io.h"
28 #include "hfi_parser.h"
29 #include "core.h"
30 #include "helpers.h"
31 #include "vdec.h"
32 
33 /*
34  * Three resons to keep MPLANE formats (despite that the number of planes
35  * currently is one):
36  * - the MPLANE formats allow only one plane to be used
37  * - the downstream driver use MPLANE formats too
38  * - future firmware versions could add support for >1 planes
39  */
40 static const struct venus_format vdec_formats[] = {
41 	{
42 		.pixfmt = V4L2_PIX_FMT_NV12,
43 		.num_planes = 1,
44 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
45 	}, {
46 		.pixfmt = V4L2_PIX_FMT_MPEG4,
47 		.num_planes = 1,
48 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
49 	}, {
50 		.pixfmt = V4L2_PIX_FMT_MPEG2,
51 		.num_planes = 1,
52 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
53 	}, {
54 		.pixfmt = V4L2_PIX_FMT_H263,
55 		.num_planes = 1,
56 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
57 	}, {
58 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
59 		.num_planes = 1,
60 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
61 	}, {
62 		.pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
63 		.num_planes = 1,
64 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
65 	}, {
66 		.pixfmt = V4L2_PIX_FMT_H264,
67 		.num_planes = 1,
68 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
69 	}, {
70 		.pixfmt = V4L2_PIX_FMT_VP8,
71 		.num_planes = 1,
72 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
73 	}, {
74 		.pixfmt = V4L2_PIX_FMT_VP9,
75 		.num_planes = 1,
76 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
77 	}, {
78 		.pixfmt = V4L2_PIX_FMT_XVID,
79 		.num_planes = 1,
80 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
81 	}, {
82 		.pixfmt = V4L2_PIX_FMT_HEVC,
83 		.num_planes = 1,
84 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
85 	},
86 };
87 
88 static const struct venus_format *
89 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
90 {
91 	const struct venus_format *fmt = vdec_formats;
92 	unsigned int size = ARRAY_SIZE(vdec_formats);
93 	unsigned int i;
94 
95 	for (i = 0; i < size; i++) {
96 		if (fmt[i].pixfmt == pixfmt)
97 			break;
98 	}
99 
100 	if (i == size || fmt[i].type != type)
101 		return NULL;
102 
103 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
104 	    !venus_helper_check_codec(inst, fmt[i].pixfmt))
105 		return NULL;
106 
107 	return &fmt[i];
108 }
109 
110 static const struct venus_format *
111 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
112 {
113 	const struct venus_format *fmt = vdec_formats;
114 	unsigned int size = ARRAY_SIZE(vdec_formats);
115 	unsigned int i, k = 0;
116 
117 	if (index > size)
118 		return NULL;
119 
120 	for (i = 0; i < size; i++) {
121 		bool valid;
122 
123 		if (fmt[i].type != type)
124 			continue;
125 		valid = type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
126 			venus_helper_check_codec(inst, fmt[i].pixfmt);
127 		if (k == index && valid)
128 			break;
129 		if (valid)
130 			k++;
131 	}
132 
133 	if (i == size)
134 		return NULL;
135 
136 	return &fmt[i];
137 }
138 
139 static const struct venus_format *
140 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
141 {
142 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
143 	struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
144 	const struct venus_format *fmt;
145 
146 	memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
147 	memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
148 
149 	fmt = find_format(inst, pixmp->pixelformat, f->type);
150 	if (!fmt) {
151 		if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
152 			pixmp->pixelformat = V4L2_PIX_FMT_NV12;
153 		else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
154 			pixmp->pixelformat = V4L2_PIX_FMT_H264;
155 		else
156 			return NULL;
157 		fmt = find_format(inst, pixmp->pixelformat, f->type);
158 	}
159 
160 	pixmp->width = clamp(pixmp->width, frame_width_min(inst),
161 			     frame_width_max(inst));
162 	pixmp->height = clamp(pixmp->height, frame_height_min(inst),
163 			      frame_height_max(inst));
164 
165 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
166 		pixmp->height = ALIGN(pixmp->height, 32);
167 
168 	if (pixmp->field == V4L2_FIELD_ANY)
169 		pixmp->field = V4L2_FIELD_NONE;
170 	pixmp->num_planes = fmt->num_planes;
171 	pixmp->flags = 0;
172 
173 	pfmt[0].sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
174 						     pixmp->width,
175 						     pixmp->height);
176 
177 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
178 		pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
179 	else
180 		pfmt[0].bytesperline = 0;
181 
182 	return fmt;
183 }
184 
185 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
186 {
187 	struct venus_inst *inst = to_inst(file);
188 
189 	vdec_try_fmt_common(inst, f);
190 
191 	return 0;
192 }
193 
194 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
195 {
196 	struct venus_inst *inst = to_inst(file);
197 	const struct venus_format *fmt = NULL;
198 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
199 
200 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
201 		fmt = inst->fmt_cap;
202 	else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
203 		fmt = inst->fmt_out;
204 
205 	if (inst->reconfig) {
206 		struct v4l2_format format = {};
207 
208 		inst->out_width = inst->reconfig_width;
209 		inst->out_height = inst->reconfig_height;
210 		inst->reconfig = false;
211 
212 		format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
213 		format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
214 		format.fmt.pix_mp.width = inst->out_width;
215 		format.fmt.pix_mp.height = inst->out_height;
216 
217 		vdec_try_fmt_common(inst, &format);
218 
219 		inst->width = format.fmt.pix_mp.width;
220 		inst->height = format.fmt.pix_mp.height;
221 	}
222 
223 	pixmp->pixelformat = fmt->pixfmt;
224 
225 	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
226 		pixmp->width = inst->width;
227 		pixmp->height = inst->height;
228 		pixmp->colorspace = inst->colorspace;
229 		pixmp->ycbcr_enc = inst->ycbcr_enc;
230 		pixmp->quantization = inst->quantization;
231 		pixmp->xfer_func = inst->xfer_func;
232 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
233 		pixmp->width = inst->out_width;
234 		pixmp->height = inst->out_height;
235 	}
236 
237 	vdec_try_fmt_common(inst, f);
238 
239 	return 0;
240 }
241 
242 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
243 {
244 	struct venus_inst *inst = to_inst(file);
245 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
246 	struct v4l2_pix_format_mplane orig_pixmp;
247 	const struct venus_format *fmt;
248 	struct v4l2_format format;
249 	u32 pixfmt_out = 0, pixfmt_cap = 0;
250 
251 	orig_pixmp = *pixmp;
252 
253 	fmt = vdec_try_fmt_common(inst, f);
254 
255 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
256 		pixfmt_out = pixmp->pixelformat;
257 		pixfmt_cap = inst->fmt_cap->pixfmt;
258 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
259 		pixfmt_cap = pixmp->pixelformat;
260 		pixfmt_out = inst->fmt_out->pixfmt;
261 	}
262 
263 	memset(&format, 0, sizeof(format));
264 
265 	format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
266 	format.fmt.pix_mp.pixelformat = pixfmt_out;
267 	format.fmt.pix_mp.width = orig_pixmp.width;
268 	format.fmt.pix_mp.height = orig_pixmp.height;
269 	vdec_try_fmt_common(inst, &format);
270 
271 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
272 		inst->out_width = format.fmt.pix_mp.width;
273 		inst->out_height = format.fmt.pix_mp.height;
274 		inst->colorspace = pixmp->colorspace;
275 		inst->ycbcr_enc = pixmp->ycbcr_enc;
276 		inst->quantization = pixmp->quantization;
277 		inst->xfer_func = pixmp->xfer_func;
278 	}
279 
280 	memset(&format, 0, sizeof(format));
281 
282 	format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
283 	format.fmt.pix_mp.pixelformat = pixfmt_cap;
284 	format.fmt.pix_mp.width = orig_pixmp.width;
285 	format.fmt.pix_mp.height = orig_pixmp.height;
286 	vdec_try_fmt_common(inst, &format);
287 
288 	inst->width = format.fmt.pix_mp.width;
289 	inst->height = format.fmt.pix_mp.height;
290 
291 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
292 		inst->fmt_out = fmt;
293 	else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
294 		inst->fmt_cap = fmt;
295 
296 	return 0;
297 }
298 
299 static int
300 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
301 {
302 	struct venus_inst *inst = to_inst(file);
303 
304 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
305 	    s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
306 		return -EINVAL;
307 
308 	switch (s->target) {
309 	case V4L2_SEL_TGT_CROP_BOUNDS:
310 	case V4L2_SEL_TGT_CROP_DEFAULT:
311 	case V4L2_SEL_TGT_CROP:
312 		if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
313 			return -EINVAL;
314 		s->r.width = inst->out_width;
315 		s->r.height = inst->out_height;
316 		break;
317 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
318 	case V4L2_SEL_TGT_COMPOSE_PADDED:
319 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
320 			return -EINVAL;
321 		s->r.width = inst->width;
322 		s->r.height = inst->height;
323 		break;
324 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
325 	case V4L2_SEL_TGT_COMPOSE:
326 		if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
327 			return -EINVAL;
328 		s->r.width = inst->out_width;
329 		s->r.height = inst->out_height;
330 		break;
331 	default:
332 		return -EINVAL;
333 	}
334 
335 	s->r.top = 0;
336 	s->r.left = 0;
337 
338 	return 0;
339 }
340 
341 static int
342 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
343 {
344 	strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
345 	strscpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
346 	strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
347 
348 	return 0;
349 }
350 
351 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
352 {
353 	struct venus_inst *inst = to_inst(file);
354 	const struct venus_format *fmt;
355 
356 	memset(f->reserved, 0, sizeof(f->reserved));
357 
358 	fmt = find_format_by_index(inst, f->index, f->type);
359 	if (!fmt)
360 		return -EINVAL;
361 
362 	f->pixelformat = fmt->pixfmt;
363 
364 	return 0;
365 }
366 
367 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
368 {
369 	struct venus_inst *inst = to_inst(file);
370 	struct v4l2_captureparm *cap = &a->parm.capture;
371 	struct v4l2_fract *timeperframe = &cap->timeperframe;
372 	u64 us_per_frame, fps;
373 
374 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
375 	    a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
376 		return -EINVAL;
377 
378 	memset(cap->reserved, 0, sizeof(cap->reserved));
379 	if (!timeperframe->denominator)
380 		timeperframe->denominator = inst->timeperframe.denominator;
381 	if (!timeperframe->numerator)
382 		timeperframe->numerator = inst->timeperframe.numerator;
383 	cap->readbuffers = 0;
384 	cap->extendedmode = 0;
385 	cap->capability = V4L2_CAP_TIMEPERFRAME;
386 	us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
387 	do_div(us_per_frame, timeperframe->denominator);
388 
389 	if (!us_per_frame)
390 		return -EINVAL;
391 
392 	fps = (u64)USEC_PER_SEC;
393 	do_div(fps, us_per_frame);
394 
395 	inst->fps = fps;
396 	inst->timeperframe = *timeperframe;
397 
398 	return 0;
399 }
400 
401 static int vdec_enum_framesizes(struct file *file, void *fh,
402 				struct v4l2_frmsizeenum *fsize)
403 {
404 	struct venus_inst *inst = to_inst(file);
405 	const struct venus_format *fmt;
406 
407 	fmt = find_format(inst, fsize->pixel_format,
408 			  V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
409 	if (!fmt) {
410 		fmt = find_format(inst, fsize->pixel_format,
411 				  V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
412 		if (!fmt)
413 			return -EINVAL;
414 	}
415 
416 	if (fsize->index)
417 		return -EINVAL;
418 
419 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
420 
421 	fsize->stepwise.min_width = frame_width_min(inst);
422 	fsize->stepwise.max_width = frame_width_max(inst);
423 	fsize->stepwise.step_width = frame_width_step(inst);
424 	fsize->stepwise.min_height = frame_height_min(inst);
425 	fsize->stepwise.max_height = frame_height_max(inst);
426 	fsize->stepwise.step_height = frame_height_step(inst);
427 
428 	return 0;
429 }
430 
431 static int vdec_subscribe_event(struct v4l2_fh *fh,
432 				const struct v4l2_event_subscription *sub)
433 {
434 	switch (sub->type) {
435 	case V4L2_EVENT_EOS:
436 		return v4l2_event_subscribe(fh, sub, 2, NULL);
437 	case V4L2_EVENT_SOURCE_CHANGE:
438 		return v4l2_src_change_event_subscribe(fh, sub);
439 	case V4L2_EVENT_CTRL:
440 		return v4l2_ctrl_subscribe_event(fh, sub);
441 	default:
442 		return -EINVAL;
443 	}
444 }
445 
446 static int
447 vdec_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
448 {
449 	switch (cmd->cmd) {
450 	case V4L2_DEC_CMD_STOP:
451 		if (cmd->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
452 			return -EINVAL;
453 		break;
454 	default:
455 		return -EINVAL;
456 	}
457 
458 	return 0;
459 }
460 
461 static int
462 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
463 {
464 	struct venus_inst *inst = to_inst(file);
465 	struct hfi_frame_data fdata = {0};
466 	int ret;
467 
468 	ret = vdec_try_decoder_cmd(file, fh, cmd);
469 	if (ret)
470 		return ret;
471 
472 	mutex_lock(&inst->lock);
473 
474 	/*
475 	 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on decoder
476 	 * input to signal EOS.
477 	 */
478 	if (!(inst->streamon_out & inst->streamon_cap))
479 		goto unlock;
480 
481 	fdata.buffer_type = HFI_BUFFER_INPUT;
482 	fdata.flags |= HFI_BUFFERFLAG_EOS;
483 	fdata.device_addr = 0xdeadbeef;
484 
485 	ret = hfi_session_process_buf(inst, &fdata);
486 
487 unlock:
488 	mutex_unlock(&inst->lock);
489 	return ret;
490 }
491 
492 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
493 	.vidioc_querycap = vdec_querycap,
494 	.vidioc_enum_fmt_vid_cap_mplane = vdec_enum_fmt,
495 	.vidioc_enum_fmt_vid_out_mplane = vdec_enum_fmt,
496 	.vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
497 	.vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
498 	.vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
499 	.vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
500 	.vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
501 	.vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
502 	.vidioc_g_selection = vdec_g_selection,
503 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
504 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
505 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
506 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
507 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
508 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
509 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
510 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
511 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
512 	.vidioc_s_parm = vdec_s_parm,
513 	.vidioc_enum_framesizes = vdec_enum_framesizes,
514 	.vidioc_subscribe_event = vdec_subscribe_event,
515 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
516 	.vidioc_try_decoder_cmd = vdec_try_decoder_cmd,
517 	.vidioc_decoder_cmd = vdec_decoder_cmd,
518 };
519 
520 static int vdec_set_properties(struct venus_inst *inst)
521 {
522 	struct vdec_controls *ctr = &inst->controls.dec;
523 	struct hfi_enable en = { .enable = 1 };
524 	u32 ptype;
525 	int ret;
526 
527 	if (ctr->post_loop_deb_mode) {
528 		ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
529 		ret = hfi_session_set_property(inst, ptype, &en);
530 		if (ret)
531 			return ret;
532 	}
533 
534 	return 0;
535 }
536 
537 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
538 
539 static int vdec_output_conf(struct venus_inst *inst)
540 {
541 	struct venus_core *core = inst->core;
542 	struct hfi_enable en = { .enable = 1 };
543 	u32 width = inst->out_width;
544 	u32 height = inst->out_height;
545 	u32 out_fmt, out2_fmt;
546 	bool ubwc = false;
547 	u32 ptype;
548 	int ret;
549 
550 	ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
551 	if (ret)
552 		return ret;
553 
554 	ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_1);
555 	if (ret)
556 		return ret;
557 
558 	if (core->res->hfi_version == HFI_VERSION_1XX) {
559 		ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
560 		ret = hfi_session_set_property(inst, ptype, &en);
561 		if (ret)
562 			return ret;
563 	}
564 
565 	/* Force searching UBWC formats for bigger then HD resolutions */
566 	if (width > 1920 && height > ALIGN(1080, 32))
567 		ubwc = true;
568 
569 	/* For Venus v4 UBWC format is mandatory */
570 	if (IS_V4(core))
571 		ubwc = true;
572 
573 	ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
574 					&out2_fmt, ubwc);
575 	if (ret)
576 		return ret;
577 
578 	inst->output_buf_size =
579 			venus_helper_get_framesz_raw(out_fmt, width, height);
580 	inst->output2_buf_size =
581 			venus_helper_get_framesz_raw(out2_fmt, width, height);
582 
583 	if (is_ubwc_fmt(out_fmt)) {
584 		inst->opb_buftype = HFI_BUFFER_OUTPUT2;
585 		inst->opb_fmt = out2_fmt;
586 		inst->dpb_buftype = HFI_BUFFER_OUTPUT;
587 		inst->dpb_fmt = out_fmt;
588 	} else if (is_ubwc_fmt(out2_fmt)) {
589 		inst->opb_buftype = HFI_BUFFER_OUTPUT;
590 		inst->opb_fmt = out_fmt;
591 		inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
592 		inst->dpb_fmt = out2_fmt;
593 	} else {
594 		inst->opb_buftype = HFI_BUFFER_OUTPUT;
595 		inst->opb_fmt = out_fmt;
596 		inst->dpb_buftype = 0;
597 		inst->dpb_fmt = 0;
598 	}
599 
600 	ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
601 					  inst->opb_buftype);
602 	if (ret)
603 		return ret;
604 
605 	if (inst->dpb_fmt) {
606 		ret = venus_helper_set_multistream(inst, false, true);
607 		if (ret)
608 			return ret;
609 
610 		ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
611 						  inst->dpb_buftype);
612 		if (ret)
613 			return ret;
614 
615 		ret = venus_helper_set_output_resolution(inst, width, height,
616 							 HFI_BUFFER_OUTPUT2);
617 		if (ret)
618 			return ret;
619 	}
620 
621 	if (IS_V3(core) || IS_V4(core)) {
622 		if (inst->output2_buf_size) {
623 			ret = venus_helper_set_bufsize(inst,
624 						       inst->output2_buf_size,
625 						       HFI_BUFFER_OUTPUT2);
626 			if (ret)
627 				return ret;
628 		}
629 
630 		if (inst->output_buf_size) {
631 			ret = venus_helper_set_bufsize(inst,
632 						       inst->output_buf_size,
633 						       HFI_BUFFER_OUTPUT);
634 			if (ret)
635 				return ret;
636 		}
637 	}
638 
639 	ret = venus_helper_set_dyn_bufmode(inst);
640 	if (ret)
641 		return ret;
642 
643 	return 0;
644 }
645 
646 static int vdec_init_session(struct venus_inst *inst)
647 {
648 	int ret;
649 
650 	ret = hfi_session_init(inst, inst->fmt_out->pixfmt);
651 	if (ret)
652 		return ret;
653 
654 	ret = venus_helper_set_input_resolution(inst, inst->out_width,
655 						inst->out_height);
656 	if (ret)
657 		goto deinit;
658 
659 	ret = venus_helper_set_color_format(inst, inst->fmt_cap->pixfmt);
660 	if (ret)
661 		goto deinit;
662 
663 	return 0;
664 deinit:
665 	hfi_session_deinit(inst);
666 	return ret;
667 }
668 
669 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
670 			    unsigned int *out_num)
671 {
672 	enum hfi_version ver = inst->core->res->hfi_version;
673 	struct hfi_buffer_requirements bufreq;
674 	int ret;
675 
676 	*in_num = *out_num = 0;
677 
678 	ret = vdec_init_session(inst);
679 	if (ret)
680 		return ret;
681 
682 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
683 	if (ret)
684 		goto deinit;
685 
686 	*in_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
687 
688 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
689 	if (ret)
690 		goto deinit;
691 
692 	*out_num = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
693 
694 deinit:
695 	hfi_session_deinit(inst);
696 
697 	return ret;
698 }
699 
700 static int vdec_queue_setup(struct vb2_queue *q,
701 			    unsigned int *num_buffers, unsigned int *num_planes,
702 			    unsigned int sizes[], struct device *alloc_devs[])
703 {
704 	struct venus_inst *inst = vb2_get_drv_priv(q);
705 	unsigned int in_num, out_num;
706 	int ret = 0;
707 
708 	if (*num_planes) {
709 		unsigned int output_buf_size = venus_helper_get_opb_size(inst);
710 
711 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
712 		    *num_planes != inst->fmt_out->num_planes)
713 			return -EINVAL;
714 
715 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
716 		    *num_planes != inst->fmt_cap->num_planes)
717 			return -EINVAL;
718 
719 		if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
720 		    sizes[0] < inst->input_buf_size)
721 			return -EINVAL;
722 
723 		if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
724 		    sizes[0] < output_buf_size)
725 			return -EINVAL;
726 
727 		return 0;
728 	}
729 
730 	ret = vdec_num_buffers(inst, &in_num, &out_num);
731 	if (ret)
732 		return ret;
733 
734 	switch (q->type) {
735 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
736 		*num_planes = inst->fmt_out->num_planes;
737 		sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
738 						    inst->out_width,
739 						    inst->out_height);
740 		inst->input_buf_size = sizes[0];
741 		*num_buffers = max(*num_buffers, in_num);
742 		inst->num_input_bufs = *num_buffers;
743 		inst->num_output_bufs = out_num;
744 		break;
745 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
746 		*num_planes = inst->fmt_cap->num_planes;
747 		sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
748 						    inst->width,
749 						    inst->height);
750 		inst->output_buf_size = sizes[0];
751 		*num_buffers = max(*num_buffers, out_num);
752 		inst->num_output_bufs = *num_buffers;
753 		break;
754 	default:
755 		ret = -EINVAL;
756 		break;
757 	}
758 
759 	return ret;
760 }
761 
762 static int vdec_verify_conf(struct venus_inst *inst)
763 {
764 	enum hfi_version ver = inst->core->res->hfi_version;
765 	struct hfi_buffer_requirements bufreq;
766 	int ret;
767 
768 	if (!inst->num_input_bufs || !inst->num_output_bufs)
769 		return -EINVAL;
770 
771 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
772 	if (ret)
773 		return ret;
774 
775 	if (inst->num_output_bufs < bufreq.count_actual ||
776 	    inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
777 		return -EINVAL;
778 
779 	ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
780 	if (ret)
781 		return ret;
782 
783 	if (inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
784 		return -EINVAL;
785 
786 	return 0;
787 }
788 
789 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
790 {
791 	struct venus_inst *inst = vb2_get_drv_priv(q);
792 	int ret;
793 
794 	mutex_lock(&inst->lock);
795 
796 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
797 		inst->streamon_out = 1;
798 	else
799 		inst->streamon_cap = 1;
800 
801 	if (!(inst->streamon_out & inst->streamon_cap)) {
802 		mutex_unlock(&inst->lock);
803 		return 0;
804 	}
805 
806 	venus_helper_init_instance(inst);
807 
808 	inst->reconfig = false;
809 	inst->sequence_cap = 0;
810 	inst->sequence_out = 0;
811 
812 	ret = vdec_init_session(inst);
813 	if (ret)
814 		goto bufs_done;
815 
816 	ret = vdec_set_properties(inst);
817 	if (ret)
818 		goto deinit_sess;
819 
820 	ret = vdec_output_conf(inst);
821 	if (ret)
822 		goto deinit_sess;
823 
824 	ret = vdec_verify_conf(inst);
825 	if (ret)
826 		goto deinit_sess;
827 
828 	ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
829 					VB2_MAX_FRAME, VB2_MAX_FRAME);
830 	if (ret)
831 		goto deinit_sess;
832 
833 	ret = venus_helper_alloc_dpb_bufs(inst);
834 	if (ret)
835 		goto deinit_sess;
836 
837 	ret = venus_helper_vb2_start_streaming(inst);
838 	if (ret)
839 		goto deinit_sess;
840 
841 	mutex_unlock(&inst->lock);
842 
843 	return 0;
844 
845 deinit_sess:
846 	hfi_session_deinit(inst);
847 bufs_done:
848 	venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
849 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
850 		inst->streamon_out = 0;
851 	else
852 		inst->streamon_cap = 0;
853 	mutex_unlock(&inst->lock);
854 	return ret;
855 }
856 
857 static const struct vb2_ops vdec_vb2_ops = {
858 	.queue_setup = vdec_queue_setup,
859 	.buf_init = venus_helper_vb2_buf_init,
860 	.buf_prepare = venus_helper_vb2_buf_prepare,
861 	.start_streaming = vdec_start_streaming,
862 	.stop_streaming = venus_helper_vb2_stop_streaming,
863 	.buf_queue = venus_helper_vb2_buf_queue,
864 };
865 
866 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
867 			  u32 tag, u32 bytesused, u32 data_offset, u32 flags,
868 			  u32 hfi_flags, u64 timestamp_us)
869 {
870 	enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
871 	struct vb2_v4l2_buffer *vbuf;
872 	struct vb2_buffer *vb;
873 	unsigned int type;
874 
875 	if (buf_type == HFI_BUFFER_INPUT)
876 		type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
877 	else
878 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
879 
880 	vbuf = venus_helper_find_buf(inst, type, tag);
881 	if (!vbuf)
882 		return;
883 
884 	vbuf->flags = flags;
885 	vbuf->field = V4L2_FIELD_NONE;
886 
887 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
888 		vb = &vbuf->vb2_buf;
889 		vb2_set_plane_payload(vb, 0, bytesused);
890 		vb->planes[0].data_offset = data_offset;
891 		vb->timestamp = timestamp_us * NSEC_PER_USEC;
892 		vbuf->sequence = inst->sequence_cap++;
893 
894 		if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
895 			const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
896 
897 			v4l2_event_queue_fh(&inst->fh, &ev);
898 		}
899 	} else {
900 		vbuf->sequence = inst->sequence_out++;
901 	}
902 
903 	if (hfi_flags & HFI_BUFFERFLAG_READONLY)
904 		venus_helper_acquire_buf_ref(vbuf);
905 
906 	if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
907 		state = VB2_BUF_STATE_ERROR;
908 
909 	v4l2_m2m_buf_done(vbuf, state);
910 }
911 
912 static void vdec_event_notify(struct venus_inst *inst, u32 event,
913 			      struct hfi_event_data *data)
914 {
915 	struct venus_core *core = inst->core;
916 	struct device *dev = core->dev_dec;
917 	static const struct v4l2_event ev = {
918 		.type = V4L2_EVENT_SOURCE_CHANGE,
919 		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
920 
921 	switch (event) {
922 	case EVT_SESSION_ERROR:
923 		inst->session_error = true;
924 		dev_err(dev, "dec: event session error %x\n", inst->error);
925 		break;
926 	case EVT_SYS_EVENT_CHANGE:
927 		switch (data->event_type) {
928 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
929 			hfi_session_continue(inst);
930 			dev_dbg(dev, "event sufficient resources\n");
931 			break;
932 		case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
933 			inst->reconfig_height = data->height;
934 			inst->reconfig_width = data->width;
935 			inst->reconfig = true;
936 
937 			v4l2_event_queue_fh(&inst->fh, &ev);
938 
939 			dev_dbg(dev, "event not sufficient resources (%ux%u)\n",
940 				data->width, data->height);
941 			break;
942 		case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
943 			venus_helper_release_buf_ref(inst, data->tag);
944 			break;
945 		default:
946 			break;
947 		}
948 		break;
949 	default:
950 		break;
951 	}
952 }
953 
954 static const struct hfi_inst_ops vdec_hfi_ops = {
955 	.buf_done = vdec_buf_done,
956 	.event_notify = vdec_event_notify,
957 };
958 
959 static void vdec_inst_init(struct venus_inst *inst)
960 {
961 	inst->fmt_out = &vdec_formats[6];
962 	inst->fmt_cap = &vdec_formats[0];
963 	inst->width = 1280;
964 	inst->height = ALIGN(720, 32);
965 	inst->out_width = 1280;
966 	inst->out_height = 720;
967 	inst->fps = 30;
968 	inst->timeperframe.numerator = 1;
969 	inst->timeperframe.denominator = 30;
970 	inst->hfi_codec = HFI_VIDEO_CODEC_H264;
971 }
972 
973 static const struct v4l2_m2m_ops vdec_m2m_ops = {
974 	.device_run = venus_helper_m2m_device_run,
975 	.job_abort = venus_helper_m2m_job_abort,
976 };
977 
978 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
979 			  struct vb2_queue *dst_vq)
980 {
981 	struct venus_inst *inst = priv;
982 	int ret;
983 
984 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
985 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
986 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
987 	src_vq->ops = &vdec_vb2_ops;
988 	src_vq->mem_ops = &vb2_dma_sg_memops;
989 	src_vq->drv_priv = inst;
990 	src_vq->buf_struct_size = sizeof(struct venus_buffer);
991 	src_vq->allow_zero_bytesused = 1;
992 	src_vq->min_buffers_needed = 1;
993 	src_vq->dev = inst->core->dev;
994 	ret = vb2_queue_init(src_vq);
995 	if (ret)
996 		return ret;
997 
998 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
999 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1000 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1001 	dst_vq->ops = &vdec_vb2_ops;
1002 	dst_vq->mem_ops = &vb2_dma_sg_memops;
1003 	dst_vq->drv_priv = inst;
1004 	dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1005 	dst_vq->allow_zero_bytesused = 1;
1006 	dst_vq->min_buffers_needed = 1;
1007 	dst_vq->dev = inst->core->dev;
1008 	ret = vb2_queue_init(dst_vq);
1009 	if (ret) {
1010 		vb2_queue_release(src_vq);
1011 		return ret;
1012 	}
1013 
1014 	return 0;
1015 }
1016 
1017 static int vdec_open(struct file *file)
1018 {
1019 	struct venus_core *core = video_drvdata(file);
1020 	struct venus_inst *inst;
1021 	int ret;
1022 
1023 	inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1024 	if (!inst)
1025 		return -ENOMEM;
1026 
1027 	INIT_LIST_HEAD(&inst->dpbbufs);
1028 	INIT_LIST_HEAD(&inst->registeredbufs);
1029 	INIT_LIST_HEAD(&inst->internalbufs);
1030 	INIT_LIST_HEAD(&inst->list);
1031 	mutex_init(&inst->lock);
1032 
1033 	inst->core = core;
1034 	inst->session_type = VIDC_SESSION_TYPE_DEC;
1035 	inst->num_output_bufs = 1;
1036 
1037 	venus_helper_init_instance(inst);
1038 
1039 	ret = pm_runtime_get_sync(core->dev_dec);
1040 	if (ret < 0)
1041 		goto err_free_inst;
1042 
1043 	ret = vdec_ctrl_init(inst);
1044 	if (ret)
1045 		goto err_put_sync;
1046 
1047 	ret = hfi_session_create(inst, &vdec_hfi_ops);
1048 	if (ret)
1049 		goto err_ctrl_deinit;
1050 
1051 	vdec_inst_init(inst);
1052 
1053 	/*
1054 	 * create m2m device for every instance, the m2m context scheduling
1055 	 * is made by firmware side so we do not need to care about.
1056 	 */
1057 	inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1058 	if (IS_ERR(inst->m2m_dev)) {
1059 		ret = PTR_ERR(inst->m2m_dev);
1060 		goto err_session_destroy;
1061 	}
1062 
1063 	inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1064 	if (IS_ERR(inst->m2m_ctx)) {
1065 		ret = PTR_ERR(inst->m2m_ctx);
1066 		goto err_m2m_release;
1067 	}
1068 
1069 	v4l2_fh_init(&inst->fh, core->vdev_dec);
1070 
1071 	inst->fh.ctrl_handler = &inst->ctrl_handler;
1072 	v4l2_fh_add(&inst->fh);
1073 	inst->fh.m2m_ctx = inst->m2m_ctx;
1074 	file->private_data = &inst->fh;
1075 
1076 	return 0;
1077 
1078 err_m2m_release:
1079 	v4l2_m2m_release(inst->m2m_dev);
1080 err_session_destroy:
1081 	hfi_session_destroy(inst);
1082 err_ctrl_deinit:
1083 	vdec_ctrl_deinit(inst);
1084 err_put_sync:
1085 	pm_runtime_put_sync(core->dev_dec);
1086 err_free_inst:
1087 	kfree(inst);
1088 	return ret;
1089 }
1090 
1091 static int vdec_close(struct file *file)
1092 {
1093 	struct venus_inst *inst = to_inst(file);
1094 
1095 	v4l2_m2m_ctx_release(inst->m2m_ctx);
1096 	v4l2_m2m_release(inst->m2m_dev);
1097 	vdec_ctrl_deinit(inst);
1098 	hfi_session_destroy(inst);
1099 	mutex_destroy(&inst->lock);
1100 	v4l2_fh_del(&inst->fh);
1101 	v4l2_fh_exit(&inst->fh);
1102 
1103 	pm_runtime_put_sync(inst->core->dev_dec);
1104 
1105 	kfree(inst);
1106 	return 0;
1107 }
1108 
1109 static const struct v4l2_file_operations vdec_fops = {
1110 	.owner = THIS_MODULE,
1111 	.open = vdec_open,
1112 	.release = vdec_close,
1113 	.unlocked_ioctl = video_ioctl2,
1114 	.poll = v4l2_m2m_fop_poll,
1115 	.mmap = v4l2_m2m_fop_mmap,
1116 #ifdef CONFIG_COMPAT
1117 	.compat_ioctl32 = v4l2_compat_ioctl32,
1118 #endif
1119 };
1120 
1121 static int vdec_probe(struct platform_device *pdev)
1122 {
1123 	struct device *dev = &pdev->dev;
1124 	struct video_device *vdev;
1125 	struct venus_core *core;
1126 	int ret;
1127 
1128 	if (!dev->parent)
1129 		return -EPROBE_DEFER;
1130 
1131 	core = dev_get_drvdata(dev->parent);
1132 	if (!core)
1133 		return -EPROBE_DEFER;
1134 
1135 	if (IS_V3(core) || IS_V4(core)) {
1136 		core->core0_clk = devm_clk_get(dev, "core");
1137 		if (IS_ERR(core->core0_clk))
1138 			return PTR_ERR(core->core0_clk);
1139 	}
1140 
1141 	if (IS_V4(core)) {
1142 		core->core0_bus_clk = devm_clk_get(dev, "bus");
1143 		if (IS_ERR(core->core0_bus_clk))
1144 			return PTR_ERR(core->core0_bus_clk);
1145 	}
1146 
1147 	platform_set_drvdata(pdev, core);
1148 
1149 	vdev = video_device_alloc();
1150 	if (!vdev)
1151 		return -ENOMEM;
1152 
1153 	strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1154 	vdev->release = video_device_release;
1155 	vdev->fops = &vdec_fops;
1156 	vdev->ioctl_ops = &vdec_ioctl_ops;
1157 	vdev->vfl_dir = VFL_DIR_M2M;
1158 	vdev->v4l2_dev = &core->v4l2_dev;
1159 	vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1160 
1161 	ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1162 	if (ret)
1163 		goto err_vdev_release;
1164 
1165 	core->vdev_dec = vdev;
1166 	core->dev_dec = dev;
1167 
1168 	video_set_drvdata(vdev, core);
1169 	pm_runtime_enable(dev);
1170 
1171 	return 0;
1172 
1173 err_vdev_release:
1174 	video_device_release(vdev);
1175 	return ret;
1176 }
1177 
1178 static int vdec_remove(struct platform_device *pdev)
1179 {
1180 	struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1181 
1182 	video_unregister_device(core->vdev_dec);
1183 	pm_runtime_disable(core->dev_dec);
1184 
1185 	return 0;
1186 }
1187 
1188 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1189 {
1190 	struct venus_core *core = dev_get_drvdata(dev);
1191 	int ret;
1192 
1193 	if (IS_V1(core))
1194 		return 0;
1195 
1196 	ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true);
1197 	if (ret)
1198 		return ret;
1199 
1200 	if (IS_V4(core))
1201 		clk_disable_unprepare(core->core0_bus_clk);
1202 
1203 	clk_disable_unprepare(core->core0_clk);
1204 
1205 	return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
1206 }
1207 
1208 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1209 {
1210 	struct venus_core *core = dev_get_drvdata(dev);
1211 	int ret;
1212 
1213 	if (IS_V1(core))
1214 		return 0;
1215 
1216 	ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, true);
1217 	if (ret)
1218 		return ret;
1219 
1220 	ret = clk_prepare_enable(core->core0_clk);
1221 	if (ret)
1222 		goto err_power_disable;
1223 
1224 	if (IS_V4(core))
1225 		ret = clk_prepare_enable(core->core0_bus_clk);
1226 
1227 	if (ret)
1228 		goto err_unprepare_core0;
1229 
1230 	return venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
1231 
1232 err_unprepare_core0:
1233 	clk_disable_unprepare(core->core0_clk);
1234 err_power_disable:
1235 	venus_helper_power_enable(core, VIDC_SESSION_TYPE_DEC, false);
1236 	return ret;
1237 }
1238 
1239 static const struct dev_pm_ops vdec_pm_ops = {
1240 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1241 				pm_runtime_force_resume)
1242 	SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1243 };
1244 
1245 static const struct of_device_id vdec_dt_match[] = {
1246 	{ .compatible = "venus-decoder" },
1247 	{ }
1248 };
1249 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1250 
1251 static struct platform_driver qcom_venus_dec_driver = {
1252 	.probe = vdec_probe,
1253 	.remove = vdec_remove,
1254 	.driver = {
1255 		.name = "qcom-venus-decoder",
1256 		.of_match_table = vdec_dt_match,
1257 		.pm = &vdec_pm_ops,
1258 	},
1259 };
1260 module_platform_driver(qcom_venus_dec_driver);
1261 
1262 MODULE_ALIAS("platform:qcom-venus-decoder");
1263 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1264 MODULE_LICENSE("GPL v2");
1265