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-ioctl.h>
13 #include <media/v4l2-event.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-mem2mem.h>
16 #include <media/videobuf2-dma-contig.h>
17
18 #include "hfi_venus_io.h"
19 #include "hfi_parser.h"
20 #include "core.h"
21 #include "helpers.h"
22 #include "vdec.h"
23 #include "pm_helpers.h"
24
25 /*
26 * Three resons to keep MPLANE formats (despite that the number of planes
27 * currently is one):
28 * - the MPLANE formats allow only one plane to be used
29 * - the downstream driver use MPLANE formats too
30 * - future firmware versions could add support for >1 planes
31 */
32 static const struct venus_format vdec_formats[] = {
33 [VENUS_FMT_NV12] = {
34 .pixfmt = V4L2_PIX_FMT_NV12,
35 .num_planes = 1,
36 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
37 },
38 [VENUS_FMT_QC08C] = {
39 .pixfmt = V4L2_PIX_FMT_QC08C,
40 .num_planes = 1,
41 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
42 },
43 [VENUS_FMT_QC10C] = {
44 .pixfmt = V4L2_PIX_FMT_QC10C,
45 .num_planes = 1,
46 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
47 },
48 [VENUS_FMT_P010] = {
49 .pixfmt = V4L2_PIX_FMT_P010,
50 .num_planes = 1,
51 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
52 },
53 [VENUS_FMT_H264] = {
54 .pixfmt = V4L2_PIX_FMT_H264,
55 .num_planes = 1,
56 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
57 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
58 },
59 [VENUS_FMT_VP8] = {
60 .pixfmt = V4L2_PIX_FMT_VP8,
61 .num_planes = 1,
62 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
63 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
64 },
65 [VENUS_FMT_VP9] = {
66 .pixfmt = V4L2_PIX_FMT_VP9,
67 .num_planes = 1,
68 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
69 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
70 },
71 [VENUS_FMT_HEVC] = {
72 .pixfmt = V4L2_PIX_FMT_HEVC,
73 .num_planes = 1,
74 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
75 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
76 },
77 [VENUS_FMT_VC1_ANNEX_G] = {
78 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
79 .num_planes = 1,
80 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
81 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
82 },
83 [VENUS_FMT_VC1_ANNEX_L] = {
84 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
85 .num_planes = 1,
86 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
87 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
88 },
89 [VENUS_FMT_MPEG4] = {
90 .pixfmt = V4L2_PIX_FMT_MPEG4,
91 .num_planes = 1,
92 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
93 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
94 },
95 [VENUS_FMT_MPEG2] = {
96 .pixfmt = V4L2_PIX_FMT_MPEG2,
97 .num_planes = 1,
98 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
99 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
100 },
101 [VENUS_FMT_H263] = {
102 .pixfmt = V4L2_PIX_FMT_H263,
103 .num_planes = 1,
104 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
105 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
106 },
107 [VENUS_FMT_XVID] = {
108 .pixfmt = V4L2_PIX_FMT_XVID,
109 .num_planes = 1,
110 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
111 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
112 },
113
114 };
115
116 static const struct venus_format *
find_format(struct venus_inst * inst,u32 pixfmt,u32 type)117 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
118 {
119 const struct venus_format *fmt = vdec_formats;
120 unsigned int size = ARRAY_SIZE(vdec_formats);
121 unsigned int i;
122
123 for (i = 0; i < size; i++) {
124 if (fmt[i].pixfmt == pixfmt)
125 break;
126 }
127
128 if (i == size || fmt[i].type != type)
129 return NULL;
130
131 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
132 !venus_helper_check_codec(inst, fmt[i].pixfmt))
133 return NULL;
134
135 if (V4L2_TYPE_IS_CAPTURE(type) &&
136 !venus_helper_check_format(inst, fmt[i].pixfmt))
137 return NULL;
138
139 if (V4L2_TYPE_IS_CAPTURE(type) && fmt[i].pixfmt == V4L2_PIX_FMT_QC10C &&
140 !(inst->bit_depth == VIDC_BITDEPTH_10))
141 return NULL;
142
143 return &fmt[i];
144 }
145
146 static const struct venus_format *
find_format_by_index(struct venus_inst * inst,unsigned int index,u32 type)147 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
148 {
149 const struct venus_format *fmt = vdec_formats;
150 unsigned int size = ARRAY_SIZE(vdec_formats);
151 unsigned int i, k = 0;
152
153 if (index > size)
154 return NULL;
155
156 for (i = 0; i < size; i++) {
157 bool valid;
158
159 if (fmt[i].type != type)
160 continue;
161
162 if (V4L2_TYPE_IS_OUTPUT(type)) {
163 valid = venus_helper_check_codec(inst, fmt[i].pixfmt);
164 } else if (V4L2_TYPE_IS_CAPTURE(type)) {
165 valid = venus_helper_check_format(inst, fmt[i].pixfmt);
166
167 if (fmt[i].pixfmt == V4L2_PIX_FMT_QC10C &&
168 !(inst->bit_depth == VIDC_BITDEPTH_10))
169 valid = false;
170 }
171
172 if (k == index && valid)
173 break;
174 if (valid)
175 k++;
176 }
177
178 if (i == size)
179 return NULL;
180
181 return &fmt[i];
182 }
183
184 static const struct venus_format *
vdec_try_fmt_common(struct venus_inst * inst,struct v4l2_format * f)185 vdec_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
186 {
187 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
188 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
189 const struct venus_format *fmt;
190 u32 szimage;
191
192 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
193 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
194
195 fmt = find_format(inst, pixmp->pixelformat, f->type);
196 if (!fmt) {
197 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
198 pixmp->pixelformat = V4L2_PIX_FMT_NV12;
199 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
200 pixmp->pixelformat = V4L2_PIX_FMT_H264;
201 else
202 return NULL;
203 fmt = find_format(inst, pixmp->pixelformat, f->type);
204 if (!fmt)
205 return NULL;
206 }
207
208 pixmp->width = clamp(pixmp->width, frame_width_min(inst),
209 frame_width_max(inst));
210 pixmp->height = clamp(pixmp->height, frame_height_min(inst),
211 frame_height_max(inst));
212
213 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
214 pixmp->height = ALIGN(pixmp->height, 32);
215
216 if (pixmp->field == V4L2_FIELD_ANY)
217 pixmp->field = V4L2_FIELD_NONE;
218 pixmp->num_planes = fmt->num_planes;
219 pixmp->flags = 0;
220
221 szimage = venus_helper_get_framesz(pixmp->pixelformat, pixmp->width,
222 pixmp->height);
223
224 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
225 unsigned int stride = pixmp->width;
226
227 if (pixmp->pixelformat == V4L2_PIX_FMT_P010)
228 stride *= 2;
229
230 pfmt[0].sizeimage = szimage;
231 pfmt[0].bytesperline = ALIGN(stride, 128);
232 } else {
233 pfmt[0].sizeimage = clamp_t(u32, pfmt[0].sizeimage, 0, SZ_8M);
234 pfmt[0].sizeimage = max(pfmt[0].sizeimage, szimage);
235 pfmt[0].bytesperline = 0;
236 }
237
238 return fmt;
239 }
240
vdec_try_fmt(struct file * file,void * fh,struct v4l2_format * f)241 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
242 {
243 struct venus_inst *inst = to_inst(file);
244
245 vdec_try_fmt_common(inst, f);
246
247 return 0;
248 }
249
vdec_check_src_change(struct venus_inst * inst)250 static int vdec_check_src_change(struct venus_inst *inst)
251 {
252 int ret;
253
254 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE &&
255 inst->codec_state == VENUS_DEC_STATE_INIT &&
256 !inst->reconfig)
257 return -EINVAL;
258
259 if (inst->subscriptions & V4L2_EVENT_SOURCE_CHANGE)
260 return 0;
261
262 /*
263 * The code snippet below is a workaround for backward compatibility
264 * with applications which doesn't support V4L2 events. It will be
265 * dropped in future once those applications are fixed.
266 */
267
268 if (inst->codec_state != VENUS_DEC_STATE_INIT)
269 goto done;
270
271 ret = wait_event_timeout(inst->reconf_wait, inst->reconfig,
272 msecs_to_jiffies(100));
273 if (!ret)
274 return -EINVAL;
275
276 if (!(inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) ||
277 !inst->reconfig)
278 dev_dbg(inst->core->dev, VDBGH "wrong state\n");
279
280 done:
281 return 0;
282 }
283
vdec_g_fmt(struct file * file,void * fh,struct v4l2_format * f)284 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
285 {
286 struct venus_inst *inst = to_inst(file);
287 const struct venus_format *fmt = NULL;
288 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
289 int ret;
290
291 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
292 fmt = inst->fmt_cap;
293 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
294 fmt = inst->fmt_out;
295
296 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
297 ret = vdec_check_src_change(inst);
298 if (ret)
299 return ret;
300 }
301
302 pixmp->pixelformat = fmt->pixfmt;
303
304 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
305 pixmp->width = inst->width;
306 pixmp->height = inst->height;
307 pixmp->colorspace = inst->colorspace;
308 pixmp->ycbcr_enc = inst->ycbcr_enc;
309 pixmp->quantization = inst->quantization;
310 pixmp->xfer_func = inst->xfer_func;
311 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
312 pixmp->width = inst->out_width;
313 pixmp->height = inst->out_height;
314 }
315
316 vdec_try_fmt_common(inst, f);
317
318 return 0;
319 }
320
vdec_s_fmt(struct file * file,void * fh,struct v4l2_format * f)321 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
322 {
323 struct venus_inst *inst = to_inst(file);
324 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
325 struct v4l2_pix_format_mplane orig_pixmp;
326 const struct venus_format *fmt;
327 struct v4l2_format format;
328 u32 pixfmt_out = 0, pixfmt_cap = 0;
329 struct vb2_queue *q;
330
331 q = v4l2_m2m_get_vq(inst->m2m_ctx, f->type);
332 if (!q)
333 return -EINVAL;
334
335 if (vb2_is_busy(q))
336 return -EBUSY;
337
338 orig_pixmp = *pixmp;
339
340 fmt = vdec_try_fmt_common(inst, f);
341
342 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
343 pixfmt_out = pixmp->pixelformat;
344 pixfmt_cap = inst->fmt_cap->pixfmt;
345 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
346 pixfmt_cap = pixmp->pixelformat;
347 pixfmt_out = inst->fmt_out->pixfmt;
348 }
349
350 memset(&format, 0, sizeof(format));
351
352 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
353 format.fmt.pix_mp.pixelformat = pixfmt_out;
354 format.fmt.pix_mp.width = orig_pixmp.width;
355 format.fmt.pix_mp.height = orig_pixmp.height;
356 vdec_try_fmt_common(inst, &format);
357
358 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
359 inst->out_width = format.fmt.pix_mp.width;
360 inst->out_height = format.fmt.pix_mp.height;
361 inst->colorspace = pixmp->colorspace;
362 inst->ycbcr_enc = pixmp->ycbcr_enc;
363 inst->quantization = pixmp->quantization;
364 inst->xfer_func = pixmp->xfer_func;
365 inst->input_buf_size = pixmp->plane_fmt[0].sizeimage;
366 }
367
368 memset(&format, 0, sizeof(format));
369
370 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
371 format.fmt.pix_mp.pixelformat = pixfmt_cap;
372 format.fmt.pix_mp.width = orig_pixmp.width;
373 format.fmt.pix_mp.height = orig_pixmp.height;
374 vdec_try_fmt_common(inst, &format);
375
376 inst->width = format.fmt.pix_mp.width;
377 inst->height = format.fmt.pix_mp.height;
378 inst->crop.top = 0;
379 inst->crop.left = 0;
380 inst->crop.width = inst->width;
381 inst->crop.height = inst->height;
382
383 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
384 inst->fmt_out = fmt;
385 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
386 inst->fmt_cap = fmt;
387 inst->output2_buf_size =
388 venus_helper_get_framesz(pixfmt_cap, orig_pixmp.width, orig_pixmp.height);
389 }
390
391 return 0;
392 }
393
394 static int
vdec_g_selection(struct file * file,void * fh,struct v4l2_selection * s)395 vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
396 {
397 struct venus_inst *inst = to_inst(file);
398
399 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
400 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
401 return -EINVAL;
402
403 s->r.top = 0;
404 s->r.left = 0;
405
406 switch (s->target) {
407 case V4L2_SEL_TGT_CROP_BOUNDS:
408 case V4L2_SEL_TGT_CROP_DEFAULT:
409 case V4L2_SEL_TGT_CROP:
410 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
411 return -EINVAL;
412 s->r.width = inst->out_width;
413 s->r.height = inst->out_height;
414 break;
415 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
416 case V4L2_SEL_TGT_COMPOSE_PADDED:
417 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
418 return -EINVAL;
419 s->r.width = inst->width;
420 s->r.height = inst->height;
421 break;
422 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
423 case V4L2_SEL_TGT_COMPOSE:
424 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
425 return -EINVAL;
426 s->r = inst->crop;
427 break;
428 default:
429 return -EINVAL;
430 }
431
432 return 0;
433 }
434
435 static int
vdec_querycap(struct file * file,void * fh,struct v4l2_capability * cap)436 vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
437 {
438 strscpy(cap->driver, "qcom-venus", sizeof(cap->driver));
439 strscpy(cap->card, "Qualcomm Venus video decoder", sizeof(cap->card));
440 strscpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
441
442 return 0;
443 }
444
vdec_enum_fmt(struct file * file,void * fh,struct v4l2_fmtdesc * f)445 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
446 {
447 struct venus_inst *inst = to_inst(file);
448 const struct venus_format *fmt;
449
450 memset(f->reserved, 0, sizeof(f->reserved));
451
452 fmt = find_format_by_index(inst, f->index, f->type);
453 if (!fmt)
454 return -EINVAL;
455
456 f->pixelformat = fmt->pixfmt;
457 f->flags = fmt->flags;
458
459 return 0;
460 }
461
vdec_s_parm(struct file * file,void * fh,struct v4l2_streamparm * a)462 static int vdec_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
463 {
464 struct venus_inst *inst = to_inst(file);
465 struct v4l2_captureparm *cap = &a->parm.capture;
466 struct v4l2_fract *timeperframe = &cap->timeperframe;
467 u64 us_per_frame, fps;
468
469 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
470 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
471 return -EINVAL;
472
473 memset(cap->reserved, 0, sizeof(cap->reserved));
474 if (!timeperframe->denominator)
475 timeperframe->denominator = inst->timeperframe.denominator;
476 if (!timeperframe->numerator)
477 timeperframe->numerator = inst->timeperframe.numerator;
478 cap->readbuffers = 0;
479 cap->extendedmode = 0;
480 cap->capability = V4L2_CAP_TIMEPERFRAME;
481 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
482 do_div(us_per_frame, timeperframe->denominator);
483
484 us_per_frame = clamp(us_per_frame, 1, USEC_PER_SEC);
485 fps = (u64)USEC_PER_SEC;
486 do_div(fps, us_per_frame);
487 fps = min(VENUS_MAX_FPS, fps);
488
489 inst->fps = fps;
490 inst->timeperframe = *timeperframe;
491
492 return 0;
493 }
494
vdec_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)495 static int vdec_enum_framesizes(struct file *file, void *fh,
496 struct v4l2_frmsizeenum *fsize)
497 {
498 struct venus_inst *inst = to_inst(file);
499 const struct venus_format *fmt;
500
501 fmt = find_format(inst, fsize->pixel_format,
502 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
503 if (!fmt) {
504 fmt = find_format(inst, fsize->pixel_format,
505 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
506 if (!fmt)
507 return -EINVAL;
508 }
509
510 if (fsize->index)
511 return -EINVAL;
512
513 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
514
515 fsize->stepwise.min_width = frame_width_min(inst);
516 fsize->stepwise.max_width = frame_width_max(inst);
517 fsize->stepwise.step_width = frame_width_step(inst);
518 fsize->stepwise.min_height = frame_height_min(inst);
519 fsize->stepwise.max_height = frame_height_max(inst);
520 fsize->stepwise.step_height = frame_height_step(inst);
521
522 return 0;
523 }
524
vdec_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)525 static int vdec_subscribe_event(struct v4l2_fh *fh,
526 const struct v4l2_event_subscription *sub)
527 {
528 struct venus_inst *inst = container_of(fh, struct venus_inst, fh);
529 int ret;
530
531 switch (sub->type) {
532 case V4L2_EVENT_EOS:
533 return v4l2_event_subscribe(fh, sub, 2, NULL);
534 case V4L2_EVENT_SOURCE_CHANGE:
535 ret = v4l2_src_change_event_subscribe(fh, sub);
536 if (ret)
537 return ret;
538 inst->subscriptions |= V4L2_EVENT_SOURCE_CHANGE;
539 return 0;
540 case V4L2_EVENT_CTRL:
541 return v4l2_ctrl_subscribe_event(fh, sub);
542 default:
543 return -EINVAL;
544 }
545 }
546
547 static int
vdec_decoder_cmd(struct file * file,void * fh,struct v4l2_decoder_cmd * cmd)548 vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
549 {
550 struct venus_inst *inst = to_inst(file);
551 struct vb2_queue *dst_vq;
552 struct hfi_frame_data fdata = {0};
553 int ret;
554
555 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
556 if (ret)
557 return ret;
558
559 mutex_lock(&inst->lock);
560
561 if (cmd->cmd == V4L2_DEC_CMD_STOP) {
562 /*
563 * Implement V4L2_DEC_CMD_STOP by enqueue an empty buffer on
564 * decoder input to signal EOS.
565 */
566 if (!(inst->streamon_out && inst->streamon_cap))
567 goto unlock;
568
569 fdata.buffer_type = HFI_BUFFER_INPUT;
570 fdata.flags |= HFI_BUFFERFLAG_EOS;
571 if (IS_V6(inst->core) && is_fw_rev_or_older(inst->core, 1, 0, 87))
572 fdata.device_addr = 0;
573 else
574 fdata.device_addr = 0xdeadb000;
575
576 ret = hfi_session_process_buf(inst, &fdata);
577
578 if (!ret && inst->codec_state == VENUS_DEC_STATE_DECODING) {
579 inst->codec_state = VENUS_DEC_STATE_DRAIN;
580 inst->drain_active = true;
581 }
582 } else if (cmd->cmd == V4L2_DEC_CMD_START &&
583 inst->codec_state == VENUS_DEC_STATE_STOPPED) {
584 dst_vq = v4l2_m2m_get_vq(inst->fh.m2m_ctx,
585 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
586 vb2_clear_last_buffer_dequeued(dst_vq);
587
588 inst->codec_state = VENUS_DEC_STATE_DECODING;
589 }
590
591 unlock:
592 mutex_unlock(&inst->lock);
593 return ret;
594 }
595
596 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
597 .vidioc_querycap = vdec_querycap,
598 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt,
599 .vidioc_enum_fmt_vid_out = vdec_enum_fmt,
600 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt,
601 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt,
602 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt,
603 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt,
604 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
605 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
606 .vidioc_g_selection = vdec_g_selection,
607 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
608 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
609 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
610 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
611 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
612 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
613 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
614 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
615 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
616 .vidioc_s_parm = vdec_s_parm,
617 .vidioc_enum_framesizes = vdec_enum_framesizes,
618 .vidioc_subscribe_event = vdec_subscribe_event,
619 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
620 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
621 .vidioc_decoder_cmd = vdec_decoder_cmd,
622 };
623
vdec_pm_get(struct venus_inst * inst)624 static int vdec_pm_get(struct venus_inst *inst)
625 {
626 struct venus_core *core = inst->core;
627 struct device *dev = core->dev_dec;
628 int ret;
629
630 mutex_lock(&core->pm_lock);
631 ret = pm_runtime_resume_and_get(dev);
632 mutex_unlock(&core->pm_lock);
633
634 return ret;
635 }
636
vdec_pm_put(struct venus_inst * inst,bool autosuspend)637 static int vdec_pm_put(struct venus_inst *inst, bool autosuspend)
638 {
639 struct venus_core *core = inst->core;
640 struct device *dev = core->dev_dec;
641 int ret;
642
643 mutex_lock(&core->pm_lock);
644
645 if (autosuspend)
646 ret = pm_runtime_put_autosuspend(dev);
647 else
648 ret = pm_runtime_put_sync(dev);
649
650 mutex_unlock(&core->pm_lock);
651
652 return ret < 0 ? ret : 0;
653 }
654
vdec_pm_get_put(struct venus_inst * inst)655 static int vdec_pm_get_put(struct venus_inst *inst)
656 {
657 struct venus_core *core = inst->core;
658 struct device *dev = core->dev_dec;
659 int ret = 0;
660
661 mutex_lock(&core->pm_lock);
662
663 if (pm_runtime_suspended(dev)) {
664 ret = pm_runtime_resume_and_get(dev);
665 if (ret < 0)
666 goto error;
667
668 ret = pm_runtime_put_autosuspend(dev);
669 }
670
671 error:
672 mutex_unlock(&core->pm_lock);
673
674 return ret < 0 ? ret : 0;
675 }
676
vdec_pm_touch(struct venus_inst * inst)677 static void vdec_pm_touch(struct venus_inst *inst)
678 {
679 pm_runtime_mark_last_busy(inst->core->dev_dec);
680 }
681
vdec_set_properties(struct venus_inst * inst)682 static int vdec_set_properties(struct venus_inst *inst)
683 {
684 struct vdec_controls *ctr = &inst->controls.dec;
685 struct hfi_enable en = { .enable = 1 };
686 u32 ptype, decode_order, conceal;
687 int ret;
688
689 if (ctr->post_loop_deb_mode) {
690 ptype = HFI_PROPERTY_CONFIG_VDEC_POST_LOOP_DEBLOCKER;
691 ret = hfi_session_set_property(inst, ptype, &en);
692 if (ret)
693 return ret;
694 }
695
696 if (ctr->display_delay_enable && ctr->display_delay == 0) {
697 ptype = HFI_PROPERTY_PARAM_VDEC_OUTPUT_ORDER;
698 decode_order = HFI_OUTPUT_ORDER_DECODE;
699 ret = hfi_session_set_property(inst, ptype, &decode_order);
700 if (ret)
701 return ret;
702 }
703
704 /* Enabling sufficient sequence change support for VP9 */
705 if (is_fw_rev_or_newer(inst->core, 5, 4, 51)) {
706 ptype = HFI_PROPERTY_PARAM_VDEC_ENABLE_SUFFICIENT_SEQCHANGE_EVENT;
707 ret = hfi_session_set_property(inst, ptype, &en);
708 if (ret)
709 return ret;
710 }
711
712 ptype = HFI_PROPERTY_PARAM_VDEC_CONCEAL_COLOR;
713 conceal = ctr->conceal_color & 0xffff;
714 conceal |= ((ctr->conceal_color >> 16) & 0xffff) << 10;
715 conceal |= ((ctr->conceal_color >> 32) & 0xffff) << 20;
716
717 ret = hfi_session_set_property(inst, ptype, &conceal);
718 if (ret)
719 return ret;
720
721 return 0;
722 }
723
vdec_set_work_route(struct venus_inst * inst)724 static int vdec_set_work_route(struct venus_inst *inst)
725 {
726 u32 ptype = HFI_PROPERTY_PARAM_WORK_ROUTE;
727 struct hfi_video_work_route wr;
728
729 if (!(IS_IRIS2(inst->core) || IS_IRIS2_1(inst->core)))
730 return 0;
731
732 wr.video_work_route = inst->core->res->num_vpp_pipes;
733
734 return hfi_session_set_property(inst, ptype, &wr);
735 }
736
737 #define is_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_UBWC_BASE))
738 #define is_10bit_ubwc_fmt(fmt) (!!((fmt) & HFI_COLOR_FORMAT_10_BIT_BASE & \
739 HFI_COLOR_FORMAT_UBWC_BASE))
740
741
vdec_output_conf(struct venus_inst * inst)742 static int vdec_output_conf(struct venus_inst *inst)
743 {
744 struct venus_core *core = inst->core;
745 struct hfi_enable en = { .enable = 1 };
746 struct hfi_buffer_requirements bufreq;
747 u32 width = inst->width;
748 u32 height = inst->height;
749 u32 out_fmt, out2_fmt;
750 bool ubwc = false;
751 u32 ptype;
752 int ret;
753
754 ret = venus_helper_set_work_mode(inst);
755 if (ret)
756 return ret;
757
758 if (core->res->hfi_version == HFI_VERSION_1XX) {
759 ptype = HFI_PROPERTY_PARAM_VDEC_CONTINUE_DATA_TRANSFER;
760 ret = hfi_session_set_property(inst, ptype, &en);
761 if (ret)
762 return ret;
763 }
764
765 /* Force searching UBWC formats for bigger then HD resolutions */
766 if (width > 1920 && height > ALIGN(1080, 32))
767 ubwc = true;
768
769 /* For Venus v4/v6 UBWC format is mandatory */
770 if (IS_V4(core) || IS_V6(core))
771 ubwc = true;
772
773 ret = venus_helper_get_out_fmts(inst, inst->fmt_cap->pixfmt, &out_fmt,
774 &out2_fmt, ubwc);
775 if (ret)
776 return ret;
777
778 inst->output_buf_size =
779 venus_helper_get_framesz_raw(out_fmt, width, height);
780 inst->output2_buf_size =
781 venus_helper_get_framesz_raw(out2_fmt, width, height);
782
783 if (is_ubwc_fmt(out_fmt)) {
784 inst->opb_buftype = HFI_BUFFER_OUTPUT2;
785 inst->opb_fmt = out2_fmt;
786 inst->dpb_buftype = HFI_BUFFER_OUTPUT;
787 inst->dpb_fmt = out_fmt;
788 } else if (is_ubwc_fmt(out2_fmt) || is_10bit_ubwc_fmt(out_fmt)) {
789 inst->opb_buftype = HFI_BUFFER_OUTPUT;
790 inst->opb_fmt = out_fmt;
791 inst->dpb_buftype = HFI_BUFFER_OUTPUT2;
792 inst->dpb_fmt = out2_fmt;
793 } else {
794 inst->opb_buftype = HFI_BUFFER_OUTPUT;
795 inst->opb_fmt = out_fmt;
796 inst->dpb_buftype = 0;
797 inst->dpb_fmt = 0;
798 }
799
800 ret = venus_helper_set_raw_format(inst, inst->opb_fmt,
801 inst->opb_buftype);
802 if (ret)
803 return ret;
804
805 ret = venus_helper_set_format_constraints(inst);
806 if (ret)
807 return ret;
808
809 if (inst->dpb_fmt) {
810 ret = venus_helper_set_multistream(inst, false, true);
811 if (ret)
812 return ret;
813
814 ret = venus_helper_set_raw_format(inst, inst->dpb_fmt,
815 inst->dpb_buftype);
816 if (ret)
817 return ret;
818
819 ret = venus_helper_set_output_resolution(inst, width, height,
820 HFI_BUFFER_OUTPUT2);
821 if (ret)
822 return ret;
823 }
824
825 if (IS_V3(core) || IS_V4(core) || IS_V6(core)) {
826 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
827 if (ret)
828 return ret;
829
830 if (bufreq.size > inst->output_buf_size)
831 return -EINVAL;
832
833 if (inst->dpb_fmt) {
834 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT2,
835 &bufreq);
836 if (ret)
837 return ret;
838
839 if (bufreq.size > inst->output2_buf_size)
840 return -EINVAL;
841 }
842
843 if (inst->output2_buf_size) {
844 ret = venus_helper_set_bufsize(inst,
845 inst->output2_buf_size,
846 HFI_BUFFER_OUTPUT2);
847 if (ret)
848 return ret;
849 }
850
851 if (inst->output_buf_size) {
852 ret = venus_helper_set_bufsize(inst,
853 inst->output_buf_size,
854 HFI_BUFFER_OUTPUT);
855 if (ret)
856 return ret;
857 }
858 }
859
860 ret = venus_helper_set_dyn_bufmode(inst);
861 if (ret)
862 return ret;
863
864 return 0;
865 }
866
vdec_session_init(struct venus_inst * inst)867 static int vdec_session_init(struct venus_inst *inst)
868 {
869 int ret;
870
871 ret = venus_helper_session_init(inst);
872 if (ret == -EALREADY)
873 return 0;
874 else if (ret)
875 return ret;
876
877 ret = venus_helper_set_input_resolution(inst, frame_width_min(inst),
878 frame_height_min(inst));
879 if (ret)
880 goto deinit;
881
882 return 0;
883 deinit:
884 hfi_session_deinit(inst);
885 return ret;
886 }
887
vdec_num_buffers(struct venus_inst * inst,unsigned int * in_num,unsigned int * out_num)888 static int vdec_num_buffers(struct venus_inst *inst, unsigned int *in_num,
889 unsigned int *out_num)
890 {
891 enum hfi_version ver = inst->core->res->hfi_version;
892 struct hfi_buffer_requirements bufreq;
893 int ret;
894
895 *in_num = *out_num = 0;
896
897 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
898 if (ret)
899 return ret;
900
901 *in_num = hfi_bufreq_get_count_min(&bufreq, ver);
902
903 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
904 if (ret)
905 return ret;
906
907 *out_num = hfi_bufreq_get_count_min(&bufreq, ver);
908
909 return 0;
910 }
911
vdec_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])912 static int vdec_queue_setup(struct vb2_queue *q,
913 unsigned int *num_buffers, unsigned int *num_planes,
914 unsigned int sizes[], struct device *alloc_devs[])
915 {
916 struct venus_inst *inst = vb2_get_drv_priv(q);
917 struct venus_core *core = inst->core;
918 unsigned int in_num, out_num;
919 int ret = 0;
920
921 if (*num_planes) {
922 unsigned int output_buf_size = venus_helper_get_opb_size(inst);
923
924 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
925 *num_planes != inst->fmt_out->num_planes)
926 return -EINVAL;
927
928 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
929 *num_planes != inst->fmt_cap->num_planes)
930 return -EINVAL;
931
932 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
933 sizes[0] < inst->input_buf_size)
934 return -EINVAL;
935
936 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
937 sizes[0] < output_buf_size)
938 return -EINVAL;
939
940 return 0;
941 }
942
943 if (test_bit(0, &core->sys_error)) {
944 if (inst->nonblock)
945 return -EAGAIN;
946
947 ret = wait_event_interruptible(core->sys_err_done,
948 !test_bit(0, &core->sys_error));
949 if (ret)
950 return ret;
951 }
952
953 ret = vdec_pm_get(inst);
954 if (ret)
955 return ret;
956
957 ret = vdec_session_init(inst);
958 if (ret)
959 goto put_power;
960
961 ret = vdec_num_buffers(inst, &in_num, &out_num);
962 if (ret)
963 goto put_power;
964
965 ret = vdec_pm_put(inst, false);
966 if (ret)
967 return ret;
968
969 switch (q->type) {
970 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
971 *num_planes = inst->fmt_out->num_planes;
972 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
973 inst->out_width,
974 inst->out_height);
975 sizes[0] = max(sizes[0], inst->input_buf_size);
976 inst->input_buf_size = sizes[0];
977 *num_buffers = max(*num_buffers, in_num);
978 inst->num_input_bufs = *num_buffers;
979 inst->num_output_bufs = out_num;
980 break;
981 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
982 *num_planes = inst->fmt_cap->num_planes;
983 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
984 inst->width,
985 inst->height);
986 inst->output_buf_size = sizes[0];
987 *num_buffers = max(*num_buffers, out_num);
988 inst->num_output_bufs = *num_buffers;
989
990 mutex_lock(&inst->lock);
991 if (inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP)
992 inst->codec_state = VENUS_DEC_STATE_STOPPED;
993 mutex_unlock(&inst->lock);
994 break;
995 default:
996 ret = -EINVAL;
997 break;
998 }
999
1000 return ret;
1001
1002 put_power:
1003 vdec_pm_put(inst, false);
1004 return ret;
1005 }
1006
vdec_verify_conf(struct venus_inst * inst)1007 static int vdec_verify_conf(struct venus_inst *inst)
1008 {
1009 enum hfi_version ver = inst->core->res->hfi_version;
1010 struct hfi_buffer_requirements bufreq;
1011 int ret;
1012
1013 if (!inst->num_input_bufs || !inst->num_output_bufs)
1014 return -EINVAL;
1015
1016 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
1017 if (ret)
1018 return ret;
1019
1020 if (inst->num_output_bufs < bufreq.count_actual ||
1021 inst->num_output_bufs < hfi_bufreq_get_count_min(&bufreq, ver))
1022 return -EINVAL;
1023
1024 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
1025 if (ret)
1026 return ret;
1027
1028 if (inst->num_input_bufs < hfi_bufreq_get_count_min(&bufreq, ver))
1029 return -EINVAL;
1030
1031 return 0;
1032 }
1033
vdec_start_capture(struct venus_inst * inst)1034 static int vdec_start_capture(struct venus_inst *inst)
1035 {
1036 int ret;
1037
1038 if (!inst->streamon_out)
1039 return 0;
1040
1041 if (inst->codec_state == VENUS_DEC_STATE_DECODING) {
1042 if (inst->reconfig)
1043 goto reconfigure;
1044
1045 venus_helper_queue_dpb_bufs(inst);
1046 venus_helper_process_initial_cap_bufs(inst);
1047 inst->streamon_cap = 1;
1048 return 0;
1049 }
1050
1051 if (inst->codec_state != VENUS_DEC_STATE_STOPPED)
1052 return 0;
1053
1054 reconfigure:
1055 ret = vdec_output_conf(inst);
1056 if (ret)
1057 return ret;
1058
1059 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1060 VB2_MAX_FRAME, VB2_MAX_FRAME);
1061 if (ret)
1062 return ret;
1063
1064 ret = venus_helper_intbufs_realloc(inst);
1065 if (ret)
1066 goto err;
1067
1068 venus_pm_load_scale(inst);
1069
1070 inst->next_buf_last = false;
1071
1072 ret = venus_helper_alloc_dpb_bufs(inst);
1073 if (ret)
1074 goto err;
1075
1076 ret = hfi_session_continue(inst);
1077 if (ret)
1078 goto free_dpb_bufs;
1079
1080 ret = venus_helper_queue_dpb_bufs(inst);
1081 if (ret)
1082 goto free_dpb_bufs;
1083
1084 ret = venus_helper_process_initial_cap_bufs(inst);
1085 if (ret)
1086 goto free_dpb_bufs;
1087
1088 inst->codec_state = VENUS_DEC_STATE_DECODING;
1089
1090 if (inst->drain_active)
1091 inst->codec_state = VENUS_DEC_STATE_DRAIN;
1092
1093 inst->streamon_cap = 1;
1094 inst->sequence_cap = 0;
1095 inst->reconfig = false;
1096 inst->drain_active = false;
1097
1098 return 0;
1099
1100 free_dpb_bufs:
1101 venus_helper_free_dpb_bufs(inst);
1102 err:
1103 return ret;
1104 }
1105
vdec_start_output(struct venus_inst * inst)1106 static int vdec_start_output(struct venus_inst *inst)
1107 {
1108 int ret;
1109
1110 if (inst->codec_state == VENUS_DEC_STATE_SEEK) {
1111 ret = venus_helper_process_initial_out_bufs(inst);
1112 if (inst->next_buf_last)
1113 inst->codec_state = VENUS_DEC_STATE_DRC;
1114 else
1115 inst->codec_state = VENUS_DEC_STATE_DECODING;
1116 goto done;
1117 }
1118
1119 if (inst->codec_state == VENUS_DEC_STATE_INIT ||
1120 inst->codec_state == VENUS_DEC_STATE_CAPTURE_SETUP) {
1121 ret = venus_helper_process_initial_out_bufs(inst);
1122 goto done;
1123 }
1124
1125 if (inst->codec_state != VENUS_DEC_STATE_DEINIT)
1126 return -EINVAL;
1127
1128 venus_helper_init_instance(inst);
1129 inst->sequence_out = 0;
1130 inst->reconfig = false;
1131 inst->next_buf_last = false;
1132
1133 ret = vdec_set_properties(inst);
1134 if (ret)
1135 return ret;
1136
1137 ret = vdec_set_work_route(inst);
1138 if (ret)
1139 return ret;
1140
1141 ret = vdec_output_conf(inst);
1142 if (ret)
1143 return ret;
1144
1145 ret = vdec_verify_conf(inst);
1146 if (ret)
1147 return ret;
1148
1149 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
1150 VB2_MAX_FRAME, VB2_MAX_FRAME);
1151 if (ret)
1152 return ret;
1153
1154 ret = venus_helper_vb2_start_streaming(inst);
1155 if (ret)
1156 return ret;
1157
1158 ret = venus_helper_process_initial_out_bufs(inst);
1159 if (ret)
1160 return ret;
1161
1162 inst->codec_state = VENUS_DEC_STATE_INIT;
1163
1164 done:
1165 inst->streamon_out = 1;
1166 return ret;
1167 }
1168
vdec_start_streaming(struct vb2_queue * q,unsigned int count)1169 static int vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1170 {
1171 struct venus_inst *inst = vb2_get_drv_priv(q);
1172 int ret;
1173
1174 mutex_lock(&inst->lock);
1175
1176 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1177 ret = vdec_start_capture(inst);
1178 } else {
1179 ret = vdec_pm_get(inst);
1180 if (ret)
1181 goto error;
1182
1183 ret = venus_pm_acquire_core(inst);
1184 if (ret)
1185 goto put_power;
1186
1187 ret = vdec_pm_put(inst, true);
1188 if (ret)
1189 goto error;
1190
1191 ret = vdec_start_output(inst);
1192 }
1193
1194 if (ret)
1195 goto error;
1196
1197 mutex_unlock(&inst->lock);
1198 return 0;
1199
1200 put_power:
1201 vdec_pm_put(inst, false);
1202 error:
1203 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_QUEUED);
1204 mutex_unlock(&inst->lock);
1205 return ret;
1206 }
1207
vdec_cancel_dst_buffers(struct venus_inst * inst)1208 static void vdec_cancel_dst_buffers(struct venus_inst *inst)
1209 {
1210 struct vb2_v4l2_buffer *buf;
1211
1212 while ((buf = v4l2_m2m_dst_buf_remove(inst->m2m_ctx)))
1213 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1214 }
1215
vdec_stop_capture(struct venus_inst * inst)1216 static int vdec_stop_capture(struct venus_inst *inst)
1217 {
1218 int ret = 0;
1219
1220 switch (inst->codec_state) {
1221 case VENUS_DEC_STATE_DECODING:
1222 ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1223 fallthrough;
1224 case VENUS_DEC_STATE_DRAIN:
1225 inst->codec_state = VENUS_DEC_STATE_STOPPED;
1226 inst->drain_active = false;
1227 fallthrough;
1228 case VENUS_DEC_STATE_SEEK:
1229 vdec_cancel_dst_buffers(inst);
1230 break;
1231 case VENUS_DEC_STATE_DRC:
1232 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, true);
1233 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1234 venus_helper_free_dpb_bufs(inst);
1235 break;
1236 default:
1237 break;
1238 }
1239
1240 return ret;
1241 }
1242
vdec_stop_output(struct venus_inst * inst)1243 static int vdec_stop_output(struct venus_inst *inst)
1244 {
1245 int ret = 0;
1246
1247 switch (inst->codec_state) {
1248 case VENUS_DEC_STATE_DECODING:
1249 case VENUS_DEC_STATE_DRAIN:
1250 case VENUS_DEC_STATE_STOPPED:
1251 case VENUS_DEC_STATE_DRC:
1252 ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1253 inst->codec_state = VENUS_DEC_STATE_SEEK;
1254 break;
1255 case VENUS_DEC_STATE_INIT:
1256 case VENUS_DEC_STATE_CAPTURE_SETUP:
1257 ret = hfi_session_flush(inst, HFI_FLUSH_ALL, true);
1258 break;
1259 default:
1260 break;
1261 }
1262
1263 return ret;
1264 }
1265
vdec_stop_streaming(struct vb2_queue * q)1266 static void vdec_stop_streaming(struct vb2_queue *q)
1267 {
1268 struct venus_inst *inst = vb2_get_drv_priv(q);
1269 int ret = -EINVAL;
1270
1271 vdec_pm_get_put(inst);
1272
1273 mutex_lock(&inst->lock);
1274
1275 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1276 ret = vdec_stop_capture(inst);
1277 else
1278 ret = vdec_stop_output(inst);
1279
1280 venus_helper_buffers_done(inst, q->type, VB2_BUF_STATE_ERROR);
1281
1282 inst->session_error = 0;
1283
1284 if (ret)
1285 goto unlock;
1286
1287 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1288 inst->streamon_out = 0;
1289 else
1290 inst->streamon_cap = 0;
1291
1292 unlock:
1293 mutex_unlock(&inst->lock);
1294 }
1295
vdec_session_release(struct venus_inst * inst)1296 static void vdec_session_release(struct venus_inst *inst)
1297 {
1298 struct venus_core *core = inst->core;
1299 int ret, abort = 0;
1300
1301 vdec_pm_get(inst);
1302
1303 mutex_lock(&inst->lock);
1304 inst->codec_state = VENUS_DEC_STATE_DEINIT;
1305
1306 ret = hfi_session_stop(inst);
1307 abort = (ret && ret != -EINVAL) ? 1 : 0;
1308 ret = hfi_session_unload_res(inst);
1309 abort = (ret && ret != -EINVAL) ? 1 : 0;
1310 ret = venus_helper_unregister_bufs(inst);
1311 abort = (ret && ret != -EINVAL) ? 1 : 0;
1312 ret = venus_helper_intbufs_free(inst);
1313 abort = (ret && ret != -EINVAL) ? 1 : 0;
1314 ret = hfi_session_deinit(inst);
1315 abort = (ret && ret != -EINVAL) ? 1 : 0;
1316
1317 if (inst->session_error || test_bit(0, &core->sys_error))
1318 abort = 1;
1319
1320 if (abort)
1321 hfi_session_abort(inst);
1322
1323 venus_helper_free_dpb_bufs(inst);
1324 venus_pm_load_scale(inst);
1325 INIT_LIST_HEAD(&inst->registeredbufs);
1326 mutex_unlock(&inst->lock);
1327
1328 venus_pm_release_core(inst);
1329 vdec_pm_put(inst, false);
1330 }
1331
vdec_buf_init(struct vb2_buffer * vb)1332 static int vdec_buf_init(struct vb2_buffer *vb)
1333 {
1334 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1335
1336 inst->buf_count++;
1337
1338 return venus_helper_vb2_buf_init(vb);
1339 }
1340
vdec_buf_cleanup(struct vb2_buffer * vb)1341 static void vdec_buf_cleanup(struct vb2_buffer *vb)
1342 {
1343 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1344 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1345 struct venus_buffer *buf = to_venus_buffer(vbuf);
1346
1347 mutex_lock(&inst->lock);
1348 if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1349 if (!list_empty(&inst->registeredbufs))
1350 list_del_init(&buf->reg_list);
1351 mutex_unlock(&inst->lock);
1352
1353 inst->buf_count--;
1354 if (!inst->buf_count)
1355 vdec_session_release(inst);
1356 }
1357
vdec_vb2_buf_queue(struct vb2_buffer * vb)1358 static void vdec_vb2_buf_queue(struct vb2_buffer *vb)
1359 {
1360 struct venus_inst *inst = vb2_get_drv_priv(vb->vb2_queue);
1361 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1362 static const struct v4l2_event eos = { .type = V4L2_EVENT_EOS };
1363
1364 vdec_pm_get_put(inst);
1365
1366 mutex_lock(&inst->lock);
1367
1368 if (inst->next_buf_last && V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1369 inst->codec_state == VENUS_DEC_STATE_DRC) {
1370 vbuf->flags |= V4L2_BUF_FLAG_LAST;
1371 vbuf->sequence = inst->sequence_cap++;
1372 vbuf->field = V4L2_FIELD_NONE;
1373 vb2_set_plane_payload(vb, 0, 0);
1374 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1375 v4l2_event_queue_fh(&inst->fh, &eos);
1376 inst->next_buf_last = false;
1377 mutex_unlock(&inst->lock);
1378 return;
1379 }
1380
1381 venus_helper_vb2_buf_queue(vb);
1382 mutex_unlock(&inst->lock);
1383 }
1384
1385 static const struct vb2_ops vdec_vb2_ops = {
1386 .queue_setup = vdec_queue_setup,
1387 .buf_init = vdec_buf_init,
1388 .buf_cleanup = vdec_buf_cleanup,
1389 .buf_prepare = venus_helper_vb2_buf_prepare,
1390 .start_streaming = vdec_start_streaming,
1391 .stop_streaming = vdec_stop_streaming,
1392 .buf_queue = vdec_vb2_buf_queue,
1393 };
1394
vdec_buf_done(struct venus_inst * inst,unsigned int buf_type,u32 tag,u32 bytesused,u32 data_offset,u32 flags,u32 hfi_flags,u64 timestamp_us)1395 static void vdec_buf_done(struct venus_inst *inst, unsigned int buf_type,
1396 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1397 u32 hfi_flags, u64 timestamp_us)
1398 {
1399 enum vb2_buffer_state state = VB2_BUF_STATE_DONE;
1400 struct vb2_v4l2_buffer *vbuf;
1401 struct vb2_buffer *vb;
1402 unsigned int type;
1403
1404 vdec_pm_touch(inst);
1405
1406 if (buf_type == HFI_BUFFER_INPUT)
1407 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1408 else
1409 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1410
1411 vbuf = venus_helper_find_buf(inst, type, tag);
1412 if (!vbuf) {
1413 venus_helper_change_dpb_owner(inst, vbuf, type, buf_type, tag);
1414 return;
1415 }
1416
1417 vbuf->flags = flags;
1418 vbuf->field = V4L2_FIELD_NONE;
1419 vb = &vbuf->vb2_buf;
1420
1421 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1422 vb2_set_plane_payload(vb, 0, bytesused);
1423 vb->planes[0].data_offset = data_offset;
1424 vb->timestamp = timestamp_us * NSEC_PER_USEC;
1425 vbuf->sequence = inst->sequence_cap++;
1426
1427 if (vbuf->flags & V4L2_BUF_FLAG_LAST) {
1428 const struct v4l2_event ev = { .type = V4L2_EVENT_EOS };
1429
1430 v4l2_event_queue_fh(&inst->fh, &ev);
1431
1432 if (inst->codec_state == VENUS_DEC_STATE_DRAIN) {
1433 inst->drain_active = false;
1434 inst->codec_state = VENUS_DEC_STATE_STOPPED;
1435 }
1436 }
1437
1438 if (!bytesused)
1439 state = VB2_BUF_STATE_ERROR;
1440 } else {
1441 vbuf->sequence = inst->sequence_out++;
1442 }
1443
1444 venus_helper_get_ts_metadata(inst, timestamp_us, vbuf);
1445
1446 if (hfi_flags & HFI_BUFFERFLAG_READONLY)
1447 venus_helper_acquire_buf_ref(vbuf);
1448
1449 if (hfi_flags & HFI_BUFFERFLAG_DATACORRUPT)
1450 state = VB2_BUF_STATE_ERROR;
1451
1452 if (hfi_flags & HFI_BUFFERFLAG_DROP_FRAME) {
1453 state = VB2_BUF_STATE_ERROR;
1454 vb2_set_plane_payload(vb, 0, 0);
1455 vb->timestamp = 0;
1456 }
1457
1458 v4l2_m2m_buf_done(vbuf, state);
1459 }
1460
vdec_event_change(struct venus_inst * inst,struct hfi_event_data * ev_data,bool sufficient)1461 static void vdec_event_change(struct venus_inst *inst,
1462 struct hfi_event_data *ev_data, bool sufficient)
1463 {
1464 static const struct v4l2_event ev = {
1465 .type = V4L2_EVENT_SOURCE_CHANGE,
1466 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION };
1467 struct device *dev = inst->core->dev_dec;
1468 struct v4l2_format format = {};
1469
1470 mutex_lock(&inst->lock);
1471
1472 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1473 format.fmt.pix_mp.pixelformat = inst->fmt_cap->pixfmt;
1474 format.fmt.pix_mp.width = ev_data->width;
1475 format.fmt.pix_mp.height = ev_data->height;
1476
1477 vdec_try_fmt_common(inst, &format);
1478
1479 inst->width = format.fmt.pix_mp.width;
1480 inst->height = format.fmt.pix_mp.height;
1481 /*
1482 * Some versions of the firmware do not report crop information for
1483 * all codecs. For these cases, set the crop to the coded resolution.
1484 */
1485 if (ev_data->input_crop.width > 0 && ev_data->input_crop.height > 0) {
1486 inst->crop.left = ev_data->input_crop.left;
1487 inst->crop.top = ev_data->input_crop.top;
1488 inst->crop.width = ev_data->input_crop.width;
1489 inst->crop.height = ev_data->input_crop.height;
1490 } else {
1491 inst->crop.left = 0;
1492 inst->crop.top = 0;
1493 inst->crop.width = ev_data->width;
1494 inst->crop.height = ev_data->height;
1495 }
1496
1497 inst->fw_min_cnt = ev_data->buf_count;
1498 /* overwriting this to 11 for vp9 due to fw bug */
1499 if (inst->hfi_codec == HFI_VIDEO_CODEC_VP9)
1500 inst->fw_min_cnt = 11;
1501
1502 inst->out_width = ev_data->width;
1503 inst->out_height = ev_data->height;
1504
1505 if (inst->bit_depth != ev_data->bit_depth) {
1506 inst->bit_depth = ev_data->bit_depth;
1507 if (inst->bit_depth == VIDC_BITDEPTH_10)
1508 inst->fmt_cap = &vdec_formats[VENUS_FMT_P010];
1509 else
1510 inst->fmt_cap = &vdec_formats[VENUS_FMT_NV12];
1511 }
1512
1513 if (inst->pic_struct != ev_data->pic_struct)
1514 inst->pic_struct = ev_data->pic_struct;
1515
1516 dev_dbg(dev, VDBGM "event %s sufficient resources (%ux%u)\n",
1517 sufficient ? "" : "not", ev_data->width, ev_data->height);
1518
1519 switch (inst->codec_state) {
1520 case VENUS_DEC_STATE_INIT:
1521 inst->codec_state = VENUS_DEC_STATE_CAPTURE_SETUP;
1522 break;
1523 case VENUS_DEC_STATE_DECODING:
1524 case VENUS_DEC_STATE_DRAIN:
1525 inst->codec_state = VENUS_DEC_STATE_DRC;
1526 break;
1527 default:
1528 break;
1529 }
1530
1531 /*
1532 * The assumption is that the firmware have to return the last buffer
1533 * before this event is received in the v4l2 driver. Also the firmware
1534 * itself doesn't mark the last decoder output buffer with HFI EOS flag.
1535 */
1536
1537 if (inst->codec_state == VENUS_DEC_STATE_DRC) {
1538 int ret;
1539
1540 inst->next_buf_last = true;
1541
1542 ret = hfi_session_flush(inst, HFI_FLUSH_OUTPUT, false);
1543 if (ret)
1544 dev_dbg(dev, VDBGH "flush output error %d\n", ret);
1545 }
1546
1547 inst->next_buf_last = true;
1548 inst->reconfig = true;
1549 v4l2_event_queue_fh(&inst->fh, &ev);
1550 wake_up(&inst->reconf_wait);
1551
1552 mutex_unlock(&inst->lock);
1553 }
1554
vdec_event_notify(struct venus_inst * inst,u32 event,struct hfi_event_data * data)1555 static void vdec_event_notify(struct venus_inst *inst, u32 event,
1556 struct hfi_event_data *data)
1557 {
1558 struct venus_core *core = inst->core;
1559 struct device *dev = core->dev_dec;
1560
1561 vdec_pm_touch(inst);
1562
1563 switch (event) {
1564 case EVT_SESSION_ERROR:
1565 inst->session_error = true;
1566 venus_helper_vb2_queue_error(inst);
1567 dev_err(dev, "dec: event session error %x\n", inst->error);
1568 break;
1569 case EVT_SYS_EVENT_CHANGE:
1570 switch (data->event_type) {
1571 case HFI_EVENT_DATA_SEQUENCE_CHANGED_SUFFICIENT_BUF_RESOURCES:
1572 vdec_event_change(inst, data, true);
1573 break;
1574 case HFI_EVENT_DATA_SEQUENCE_CHANGED_INSUFFICIENT_BUF_RESOURCES:
1575 vdec_event_change(inst, data, false);
1576 break;
1577 case HFI_EVENT_RELEASE_BUFFER_REFERENCE:
1578 venus_helper_release_buf_ref(inst, data->tag);
1579 break;
1580 default:
1581 break;
1582 }
1583 break;
1584 default:
1585 break;
1586 }
1587 }
1588
vdec_flush_done(struct venus_inst * inst)1589 static void vdec_flush_done(struct venus_inst *inst)
1590 {
1591 dev_dbg(inst->core->dev_dec, VDBGH "flush done\n");
1592 }
1593
1594 static const struct hfi_inst_ops vdec_hfi_ops = {
1595 .buf_done = vdec_buf_done,
1596 .event_notify = vdec_event_notify,
1597 .flush_done = vdec_flush_done,
1598 };
1599
vdec_inst_init(struct venus_inst * inst)1600 static void vdec_inst_init(struct venus_inst *inst)
1601 {
1602 inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1603 inst->fmt_out = &vdec_formats[VENUS_FMT_H264];
1604 inst->fmt_cap = &vdec_formats[VENUS_FMT_NV12];
1605 inst->width = frame_width_min(inst);
1606 inst->height = ALIGN(frame_height_min(inst), 32);
1607 inst->crop.left = 0;
1608 inst->crop.top = 0;
1609 inst->crop.width = inst->width;
1610 inst->crop.height = inst->height;
1611 inst->fw_min_cnt = 8;
1612 inst->out_width = frame_width_min(inst);
1613 inst->out_height = frame_height_min(inst);
1614 inst->fps = 30;
1615 inst->timeperframe.numerator = 1;
1616 inst->timeperframe.denominator = 30;
1617 inst->opb_buftype = HFI_BUFFER_OUTPUT;
1618 }
1619
vdec_m2m_device_run(void * priv)1620 static void vdec_m2m_device_run(void *priv)
1621 {
1622 }
1623
1624 static const struct v4l2_m2m_ops vdec_m2m_ops = {
1625 .device_run = vdec_m2m_device_run,
1626 .job_abort = venus_helper_m2m_job_abort,
1627 };
1628
m2m_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1629 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1630 struct vb2_queue *dst_vq)
1631 {
1632 struct venus_inst *inst = priv;
1633 int ret;
1634
1635 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1636 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1637 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1638 src_vq->ops = &vdec_vb2_ops;
1639 src_vq->mem_ops = &vb2_dma_contig_memops;
1640 src_vq->drv_priv = inst;
1641 src_vq->buf_struct_size = sizeof(struct venus_buffer);
1642 src_vq->allow_zero_bytesused = 1;
1643 src_vq->min_queued_buffers = 0;
1644 src_vq->dev = inst->core->dev;
1645 src_vq->lock = &inst->ctx_q_lock;
1646 ret = vb2_queue_init(src_vq);
1647 if (ret)
1648 return ret;
1649
1650 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1651 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1652 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1653 dst_vq->ops = &vdec_vb2_ops;
1654 dst_vq->mem_ops = &vb2_dma_contig_memops;
1655 dst_vq->drv_priv = inst;
1656 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1657 dst_vq->allow_zero_bytesused = 1;
1658 dst_vq->min_queued_buffers = 0;
1659 dst_vq->dev = inst->core->dev;
1660 dst_vq->lock = &inst->ctx_q_lock;
1661 return vb2_queue_init(dst_vq);
1662 }
1663
vdec_open(struct file * file)1664 static int vdec_open(struct file *file)
1665 {
1666 struct venus_core *core = video_drvdata(file);
1667 struct venus_inst *inst;
1668 int ret;
1669
1670 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1671 if (!inst)
1672 return -ENOMEM;
1673
1674 INIT_LIST_HEAD(&inst->dpbbufs);
1675 INIT_LIST_HEAD(&inst->registeredbufs);
1676 INIT_LIST_HEAD(&inst->internalbufs);
1677 INIT_LIST_HEAD(&inst->list);
1678 mutex_init(&inst->lock);
1679 mutex_init(&inst->ctx_q_lock);
1680
1681 inst->core = core;
1682 inst->session_type = VIDC_SESSION_TYPE_DEC;
1683 inst->num_output_bufs = 1;
1684 inst->codec_state = VENUS_DEC_STATE_DEINIT;
1685 inst->buf_count = 0;
1686 inst->clk_data.core_id = VIDC_CORE_ID_DEFAULT;
1687 inst->core_acquired = false;
1688 inst->bit_depth = VIDC_BITDEPTH_8;
1689 inst->pic_struct = HFI_INTERLACE_FRAME_PROGRESSIVE;
1690 init_waitqueue_head(&inst->reconf_wait);
1691 inst->nonblock = file->f_flags & O_NONBLOCK;
1692
1693 venus_helper_init_instance(inst);
1694
1695 ret = vdec_ctrl_init(inst);
1696 if (ret)
1697 goto err_free;
1698
1699 ret = hfi_session_create(inst, &vdec_hfi_ops);
1700 if (ret)
1701 goto err_ctrl_deinit;
1702
1703 vdec_inst_init(inst);
1704
1705 ida_init(&inst->dpb_ids);
1706
1707 /*
1708 * create m2m device for every instance, the m2m context scheduling
1709 * is made by firmware side so we do not need to care about.
1710 */
1711 inst->m2m_dev = v4l2_m2m_init(&vdec_m2m_ops);
1712 if (IS_ERR(inst->m2m_dev)) {
1713 ret = PTR_ERR(inst->m2m_dev);
1714 goto err_session_destroy;
1715 }
1716
1717 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1718 if (IS_ERR(inst->m2m_ctx)) {
1719 ret = PTR_ERR(inst->m2m_ctx);
1720 goto err_m2m_release;
1721 }
1722
1723 v4l2_fh_init(&inst->fh, core->vdev_dec);
1724
1725 inst->fh.ctrl_handler = &inst->ctrl_handler;
1726 v4l2_fh_add(&inst->fh);
1727 inst->fh.m2m_ctx = inst->m2m_ctx;
1728 file->private_data = &inst->fh;
1729
1730 return 0;
1731
1732 err_m2m_release:
1733 v4l2_m2m_release(inst->m2m_dev);
1734 err_session_destroy:
1735 hfi_session_destroy(inst);
1736 err_ctrl_deinit:
1737 vdec_ctrl_deinit(inst);
1738 err_free:
1739 kfree(inst);
1740 return ret;
1741 }
1742
vdec_close(struct file * file)1743 static int vdec_close(struct file *file)
1744 {
1745 struct venus_inst *inst = to_inst(file);
1746
1747 vdec_pm_get(inst);
1748
1749 cancel_work_sync(&inst->delayed_process_work);
1750 v4l2_m2m_ctx_release(inst->m2m_ctx);
1751 v4l2_m2m_release(inst->m2m_dev);
1752 vdec_ctrl_deinit(inst);
1753 ida_destroy(&inst->dpb_ids);
1754 hfi_session_destroy(inst);
1755 mutex_destroy(&inst->lock);
1756 mutex_destroy(&inst->ctx_q_lock);
1757 v4l2_fh_del(&inst->fh);
1758 v4l2_fh_exit(&inst->fh);
1759
1760 vdec_pm_put(inst, false);
1761
1762 kfree(inst);
1763 return 0;
1764 }
1765
1766 static const struct v4l2_file_operations vdec_fops = {
1767 .owner = THIS_MODULE,
1768 .open = vdec_open,
1769 .release = vdec_close,
1770 .unlocked_ioctl = video_ioctl2,
1771 .poll = v4l2_m2m_fop_poll,
1772 .mmap = v4l2_m2m_fop_mmap,
1773 };
1774
vdec_probe(struct platform_device * pdev)1775 static int vdec_probe(struct platform_device *pdev)
1776 {
1777 struct device *dev = &pdev->dev;
1778 struct video_device *vdev;
1779 struct venus_core *core;
1780 int ret;
1781
1782 if (!dev->parent)
1783 return -EPROBE_DEFER;
1784
1785 core = dev_get_drvdata(dev->parent);
1786 if (!core)
1787 return -EPROBE_DEFER;
1788
1789 platform_set_drvdata(pdev, core);
1790
1791 if (core->pm_ops->vdec_get) {
1792 ret = core->pm_ops->vdec_get(dev);
1793 if (ret)
1794 return ret;
1795 }
1796
1797 vdev = video_device_alloc();
1798 if (!vdev)
1799 return -ENOMEM;
1800
1801 strscpy(vdev->name, "qcom-venus-decoder", sizeof(vdev->name));
1802 vdev->release = video_device_release;
1803 vdev->fops = &vdec_fops;
1804 vdev->ioctl_ops = &vdec_ioctl_ops;
1805 vdev->vfl_dir = VFL_DIR_M2M;
1806 vdev->v4l2_dev = &core->v4l2_dev;
1807 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1808
1809 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1810 if (ret)
1811 goto err_vdev_release;
1812
1813 core->vdev_dec = vdev;
1814 core->dev_dec = dev;
1815
1816 video_set_drvdata(vdev, core);
1817 pm_runtime_set_autosuspend_delay(dev, 2000);
1818 pm_runtime_use_autosuspend(dev);
1819 pm_runtime_enable(dev);
1820
1821 return 0;
1822
1823 err_vdev_release:
1824 video_device_release(vdev);
1825 return ret;
1826 }
1827
vdec_remove(struct platform_device * pdev)1828 static void vdec_remove(struct platform_device *pdev)
1829 {
1830 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1831
1832 video_unregister_device(core->vdev_dec);
1833 pm_runtime_disable(core->dev_dec);
1834
1835 if (core->pm_ops->vdec_put)
1836 core->pm_ops->vdec_put(core->dev_dec);
1837 }
1838
vdec_runtime_suspend(struct device * dev)1839 static __maybe_unused int vdec_runtime_suspend(struct device *dev)
1840 {
1841 struct venus_core *core = dev_get_drvdata(dev);
1842 const struct venus_pm_ops *pm_ops = core->pm_ops;
1843 int ret = 0;
1844
1845 if (pm_ops->vdec_power)
1846 ret = pm_ops->vdec_power(dev, POWER_OFF);
1847
1848 return ret;
1849 }
1850
vdec_runtime_resume(struct device * dev)1851 static __maybe_unused int vdec_runtime_resume(struct device *dev)
1852 {
1853 struct venus_core *core = dev_get_drvdata(dev);
1854 const struct venus_pm_ops *pm_ops = core->pm_ops;
1855 int ret = 0;
1856
1857 if (pm_ops->vdec_power)
1858 ret = pm_ops->vdec_power(dev, POWER_ON);
1859
1860 return ret;
1861 }
1862
1863 static const struct dev_pm_ops vdec_pm_ops = {
1864 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1865 pm_runtime_force_resume)
1866 SET_RUNTIME_PM_OPS(vdec_runtime_suspend, vdec_runtime_resume, NULL)
1867 };
1868
1869 static const struct of_device_id vdec_dt_match[] = {
1870 { .compatible = "venus-decoder" },
1871 { }
1872 };
1873 MODULE_DEVICE_TABLE(of, vdec_dt_match);
1874
1875 static struct platform_driver qcom_venus_dec_driver = {
1876 .probe = vdec_probe,
1877 .remove_new = vdec_remove,
1878 .driver = {
1879 .name = "qcom-venus-decoder",
1880 .of_match_table = vdec_dt_match,
1881 .pm = &vdec_pm_ops,
1882 },
1883 };
1884 module_platform_driver(qcom_venus_dec_driver);
1885
1886 MODULE_ALIAS("platform:qcom-venus-decoder");
1887 MODULE_DESCRIPTION("Qualcomm Venus video decoder driver");
1888 MODULE_LICENSE("GPL v2");
1889