• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Rockchip Video Decoder driver
4  *
5  * Copyright (C) 2019 Collabora, Ltd.
6  *
7  * Based on rkvdec driver by Google LLC. (Tomasz Figa <tfiga@chromium.org>)
8  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
9  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/videodev2.h>
21 #include <linux/workqueue.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-mem2mem.h>
24 #include <media/videobuf2-core.h>
25 #include <media/videobuf2-vmalloc.h>
26 
27 #include "rkvdec.h"
28 #include "rkvdec-regs.h"
29 
rkvdec_try_ctrl(struct v4l2_ctrl * ctrl)30 static int rkvdec_try_ctrl(struct v4l2_ctrl *ctrl)
31 {
32 	struct rkvdec_ctx *ctx = container_of(ctrl->handler, struct rkvdec_ctx, ctrl_hdl);
33 	const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
34 
35 	if (desc->ops->try_ctrl)
36 		return desc->ops->try_ctrl(ctx, ctrl);
37 
38 	return 0;
39 }
40 
41 static const struct v4l2_ctrl_ops rkvdec_ctrl_ops = {
42 	.try_ctrl = rkvdec_try_ctrl,
43 };
44 
45 static const struct rkvdec_ctrl_desc rkvdec_h264_ctrl_descs[] = {
46 	{
47 		.cfg.id = V4L2_CID_STATELESS_H264_DECODE_PARAMS,
48 	},
49 	{
50 		.cfg.id = V4L2_CID_STATELESS_H264_SPS,
51 		.cfg.ops = &rkvdec_ctrl_ops,
52 	},
53 	{
54 		.cfg.id = V4L2_CID_STATELESS_H264_PPS,
55 	},
56 	{
57 		.cfg.id = V4L2_CID_STATELESS_H264_SCALING_MATRIX,
58 	},
59 	{
60 		.cfg.id = V4L2_CID_STATELESS_H264_DECODE_MODE,
61 		.cfg.min = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
62 		.cfg.max = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
63 		.cfg.def = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED,
64 	},
65 	{
66 		.cfg.id = V4L2_CID_STATELESS_H264_START_CODE,
67 		.cfg.min = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
68 		.cfg.def = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
69 		.cfg.max = V4L2_STATELESS_H264_START_CODE_ANNEX_B,
70 	},
71 	{
72 		.cfg.id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
73 		.cfg.min = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
74 		.cfg.max = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
75 		.cfg.menu_skip_mask =
76 			BIT(V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED),
77 		.cfg.def = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN,
78 	},
79 	{
80 		.cfg.id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
81 		.cfg.min = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
82 		.cfg.max = V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
83 	},
84 };
85 
86 static const struct rkvdec_ctrls rkvdec_h264_ctrls = {
87 	.ctrls = rkvdec_h264_ctrl_descs,
88 	.num_ctrls = ARRAY_SIZE(rkvdec_h264_ctrl_descs),
89 };
90 
91 static const u32 rkvdec_h264_vp9_decoded_fmts[] = {
92 	V4L2_PIX_FMT_NV12,
93 };
94 
95 static const struct rkvdec_ctrl_desc rkvdec_vp9_ctrl_descs[] = {
96 	{
97 		.cfg.id = V4L2_CID_STATELESS_VP9_FRAME,
98 	},
99 	{
100 		.cfg.id = V4L2_CID_STATELESS_VP9_COMPRESSED_HDR,
101 	},
102 	{
103 		.cfg.id = V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
104 		.cfg.min = V4L2_MPEG_VIDEO_VP9_PROFILE_0,
105 		.cfg.max = V4L2_MPEG_VIDEO_VP9_PROFILE_0,
106 		.cfg.def = V4L2_MPEG_VIDEO_VP9_PROFILE_0,
107 	},
108 };
109 
110 static const struct rkvdec_ctrls rkvdec_vp9_ctrls = {
111 	.ctrls = rkvdec_vp9_ctrl_descs,
112 	.num_ctrls = ARRAY_SIZE(rkvdec_vp9_ctrl_descs),
113 };
114 
115 static const struct rkvdec_coded_fmt_desc rkvdec_coded_fmts[] = {
116 	{
117 		.fourcc = V4L2_PIX_FMT_H264_SLICE,
118 		.frmsize = {
119 			.min_width = 48,
120 			.max_width = 4096,
121 			.step_width = 16,
122 			.min_height = 48,
123 			.max_height = 2560,
124 			.step_height = 16,
125 		},
126 		.ctrls = &rkvdec_h264_ctrls,
127 		.ops = &rkvdec_h264_fmt_ops,
128 		.num_decoded_fmts = ARRAY_SIZE(rkvdec_h264_vp9_decoded_fmts),
129 		.decoded_fmts = rkvdec_h264_vp9_decoded_fmts,
130 		.subsystem_flags = VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF,
131 	},
132 	{
133 		.fourcc = V4L2_PIX_FMT_VP9_FRAME,
134 		.frmsize = {
135 			.min_width = 64,
136 			.max_width = 4096,
137 			.step_width = 64,
138 			.min_height = 64,
139 			.max_height = 2304,
140 			.step_height = 64,
141 		},
142 		.ctrls = &rkvdec_vp9_ctrls,
143 		.ops = &rkvdec_vp9_fmt_ops,
144 		.num_decoded_fmts = ARRAY_SIZE(rkvdec_h264_vp9_decoded_fmts),
145 		.decoded_fmts = rkvdec_h264_vp9_decoded_fmts,
146 	}
147 };
148 
149 static const struct rkvdec_coded_fmt_desc *
rkvdec_find_coded_fmt_desc(u32 fourcc)150 rkvdec_find_coded_fmt_desc(u32 fourcc)
151 {
152 	unsigned int i;
153 
154 	for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++) {
155 		if (rkvdec_coded_fmts[i].fourcc == fourcc)
156 			return &rkvdec_coded_fmts[i];
157 	}
158 
159 	return NULL;
160 }
161 
rkvdec_reset_fmt(struct rkvdec_ctx * ctx,struct v4l2_format * f,u32 fourcc)162 static void rkvdec_reset_fmt(struct rkvdec_ctx *ctx, struct v4l2_format *f,
163 			     u32 fourcc)
164 {
165 	memset(f, 0, sizeof(*f));
166 	f->fmt.pix_mp.pixelformat = fourcc;
167 	f->fmt.pix_mp.field = V4L2_FIELD_NONE;
168 	f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
169 	f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
170 	f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
171 	f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
172 }
173 
rkvdec_reset_coded_fmt(struct rkvdec_ctx * ctx)174 static void rkvdec_reset_coded_fmt(struct rkvdec_ctx *ctx)
175 {
176 	struct v4l2_format *f = &ctx->coded_fmt;
177 
178 	ctx->coded_fmt_desc = &rkvdec_coded_fmts[0];
179 	rkvdec_reset_fmt(ctx, f, ctx->coded_fmt_desc->fourcc);
180 
181 	f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
182 	f->fmt.pix_mp.width = ctx->coded_fmt_desc->frmsize.min_width;
183 	f->fmt.pix_mp.height = ctx->coded_fmt_desc->frmsize.min_height;
184 
185 	if (ctx->coded_fmt_desc->ops->adjust_fmt)
186 		ctx->coded_fmt_desc->ops->adjust_fmt(ctx, f);
187 }
188 
rkvdec_reset_decoded_fmt(struct rkvdec_ctx * ctx)189 static void rkvdec_reset_decoded_fmt(struct rkvdec_ctx *ctx)
190 {
191 	struct v4l2_format *f = &ctx->decoded_fmt;
192 
193 	rkvdec_reset_fmt(ctx, f, ctx->coded_fmt_desc->decoded_fmts[0]);
194 	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
195 	v4l2_fill_pixfmt_mp(&f->fmt.pix_mp,
196 			    ctx->coded_fmt_desc->decoded_fmts[0],
197 			    ctx->coded_fmt.fmt.pix_mp.width,
198 			    ctx->coded_fmt.fmt.pix_mp.height);
199 	f->fmt.pix_mp.plane_fmt[0].sizeimage += 128 *
200 		DIV_ROUND_UP(f->fmt.pix_mp.width, 16) *
201 		DIV_ROUND_UP(f->fmt.pix_mp.height, 16);
202 }
203 
rkvdec_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)204 static int rkvdec_enum_framesizes(struct file *file, void *priv,
205 				  struct v4l2_frmsizeenum *fsize)
206 {
207 	const struct rkvdec_coded_fmt_desc *fmt;
208 
209 	if (fsize->index != 0)
210 		return -EINVAL;
211 
212 	fmt = rkvdec_find_coded_fmt_desc(fsize->pixel_format);
213 	if (!fmt)
214 		return -EINVAL;
215 
216 	fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
217 	fsize->stepwise.min_width = 1;
218 	fsize->stepwise.max_width = fmt->frmsize.max_width;
219 	fsize->stepwise.step_width = 1;
220 	fsize->stepwise.min_height = 1;
221 	fsize->stepwise.max_height = fmt->frmsize.max_height;
222 	fsize->stepwise.step_height = 1;
223 
224 	return 0;
225 }
226 
rkvdec_querycap(struct file * file,void * priv,struct v4l2_capability * cap)227 static int rkvdec_querycap(struct file *file, void *priv,
228 			   struct v4l2_capability *cap)
229 {
230 	struct rkvdec_dev *rkvdec = video_drvdata(file);
231 	struct video_device *vdev = video_devdata(file);
232 
233 	strscpy(cap->driver, rkvdec->dev->driver->name,
234 		sizeof(cap->driver));
235 	strscpy(cap->card, vdev->name, sizeof(cap->card));
236 	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
237 		 rkvdec->dev->driver->name);
238 	return 0;
239 }
240 
rkvdec_try_capture_fmt(struct file * file,void * priv,struct v4l2_format * f)241 static int rkvdec_try_capture_fmt(struct file *file, void *priv,
242 				  struct v4l2_format *f)
243 {
244 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
245 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
246 	const struct rkvdec_coded_fmt_desc *coded_desc;
247 	unsigned int i;
248 
249 	/*
250 	 * The codec context should point to a coded format desc, if the format
251 	 * on the coded end has not been set yet, it should point to the
252 	 * default value.
253 	 */
254 	coded_desc = ctx->coded_fmt_desc;
255 	if (WARN_ON(!coded_desc))
256 		return -EINVAL;
257 
258 	for (i = 0; i < coded_desc->num_decoded_fmts; i++) {
259 		if (coded_desc->decoded_fmts[i] == pix_mp->pixelformat)
260 			break;
261 	}
262 
263 	if (i == coded_desc->num_decoded_fmts)
264 		pix_mp->pixelformat = coded_desc->decoded_fmts[0];
265 
266 	/* Always apply the frmsize constraint of the coded end. */
267 	pix_mp->width = max(pix_mp->width, ctx->coded_fmt.fmt.pix_mp.width);
268 	pix_mp->height = max(pix_mp->height, ctx->coded_fmt.fmt.pix_mp.height);
269 	v4l2_apply_frmsize_constraints(&pix_mp->width,
270 				       &pix_mp->height,
271 				       &coded_desc->frmsize);
272 
273 	v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat,
274 			    pix_mp->width, pix_mp->height);
275 	pix_mp->plane_fmt[0].sizeimage +=
276 		128 *
277 		DIV_ROUND_UP(pix_mp->width, 16) *
278 		DIV_ROUND_UP(pix_mp->height, 16);
279 	pix_mp->field = V4L2_FIELD_NONE;
280 
281 	return 0;
282 }
283 
rkvdec_try_output_fmt(struct file * file,void * priv,struct v4l2_format * f)284 static int rkvdec_try_output_fmt(struct file *file, void *priv,
285 				 struct v4l2_format *f)
286 {
287 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
288 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
289 	const struct rkvdec_coded_fmt_desc *desc;
290 
291 	desc = rkvdec_find_coded_fmt_desc(pix_mp->pixelformat);
292 	if (!desc) {
293 		pix_mp->pixelformat = rkvdec_coded_fmts[0].fourcc;
294 		desc = &rkvdec_coded_fmts[0];
295 	}
296 
297 	v4l2_apply_frmsize_constraints(&pix_mp->width,
298 				       &pix_mp->height,
299 				       &desc->frmsize);
300 
301 	pix_mp->field = V4L2_FIELD_NONE;
302 	/* All coded formats are considered single planar for now. */
303 	pix_mp->num_planes = 1;
304 
305 	if (desc->ops->adjust_fmt) {
306 		int ret;
307 
308 		ret = desc->ops->adjust_fmt(ctx, f);
309 		if (ret)
310 			return ret;
311 	}
312 
313 	return 0;
314 }
315 
rkvdec_s_capture_fmt(struct file * file,void * priv,struct v4l2_format * f)316 static int rkvdec_s_capture_fmt(struct file *file, void *priv,
317 				struct v4l2_format *f)
318 {
319 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
320 	struct vb2_queue *vq;
321 	int ret;
322 
323 	/* Change not allowed if queue is busy */
324 	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
325 			     V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
326 	if (vb2_is_busy(vq))
327 		return -EBUSY;
328 
329 	ret = rkvdec_try_capture_fmt(file, priv, f);
330 	if (ret)
331 		return ret;
332 
333 	ctx->decoded_fmt = *f;
334 	return 0;
335 }
336 
rkvdec_s_output_fmt(struct file * file,void * priv,struct v4l2_format * f)337 static int rkvdec_s_output_fmt(struct file *file, void *priv,
338 			       struct v4l2_format *f)
339 {
340 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
341 	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
342 	const struct rkvdec_coded_fmt_desc *desc;
343 	struct v4l2_format *cap_fmt;
344 	struct vb2_queue *peer_vq, *vq;
345 	int ret;
346 
347 	/*
348 	 * In order to support dynamic resolution change, the decoder admits
349 	 * a resolution change, as long as the pixelformat remains. Can't be
350 	 * done if streaming.
351 	 */
352 	vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
353 	if (vb2_is_streaming(vq) ||
354 	    (vb2_is_busy(vq) &&
355 	     f->fmt.pix_mp.pixelformat != ctx->coded_fmt.fmt.pix_mp.pixelformat))
356 		return -EBUSY;
357 
358 	/*
359 	 * Since format change on the OUTPUT queue will reset the CAPTURE
360 	 * queue, we can't allow doing so when the CAPTURE queue has buffers
361 	 * allocated.
362 	 */
363 	peer_vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
364 	if (vb2_is_busy(peer_vq))
365 		return -EBUSY;
366 
367 	ret = rkvdec_try_output_fmt(file, priv, f);
368 	if (ret)
369 		return ret;
370 
371 	desc = rkvdec_find_coded_fmt_desc(f->fmt.pix_mp.pixelformat);
372 	if (!desc)
373 		return -EINVAL;
374 	ctx->coded_fmt_desc = desc;
375 	ctx->coded_fmt = *f;
376 
377 	/*
378 	 * Current decoded format might have become invalid with newly
379 	 * selected codec, so reset it to default just to be safe and
380 	 * keep internal driver state sane. User is mandated to set
381 	 * the decoded format again after we return, so we don't need
382 	 * anything smarter.
383 	 *
384 	 * Note that this will propagates any size changes to the decoded format.
385 	 */
386 	rkvdec_reset_decoded_fmt(ctx);
387 
388 	/* Propagate colorspace information to capture. */
389 	cap_fmt = &ctx->decoded_fmt;
390 	cap_fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
391 	cap_fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
392 	cap_fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
393 	cap_fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
394 
395 	/* Enable format specific queue features */
396 	vq->subsystem_flags |= desc->subsystem_flags;
397 
398 	return 0;
399 }
400 
rkvdec_g_output_fmt(struct file * file,void * priv,struct v4l2_format * f)401 static int rkvdec_g_output_fmt(struct file *file, void *priv,
402 			       struct v4l2_format *f)
403 {
404 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
405 
406 	*f = ctx->coded_fmt;
407 	return 0;
408 }
409 
rkvdec_g_capture_fmt(struct file * file,void * priv,struct v4l2_format * f)410 static int rkvdec_g_capture_fmt(struct file *file, void *priv,
411 				struct v4l2_format *f)
412 {
413 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
414 
415 	*f = ctx->decoded_fmt;
416 	return 0;
417 }
418 
rkvdec_enum_output_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)419 static int rkvdec_enum_output_fmt(struct file *file, void *priv,
420 				  struct v4l2_fmtdesc *f)
421 {
422 	if (f->index >= ARRAY_SIZE(rkvdec_coded_fmts))
423 		return -EINVAL;
424 
425 	f->pixelformat = rkvdec_coded_fmts[f->index].fourcc;
426 	return 0;
427 }
428 
rkvdec_enum_capture_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)429 static int rkvdec_enum_capture_fmt(struct file *file, void *priv,
430 				   struct v4l2_fmtdesc *f)
431 {
432 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
433 
434 	if (WARN_ON(!ctx->coded_fmt_desc))
435 		return -EINVAL;
436 
437 	if (f->index >= ctx->coded_fmt_desc->num_decoded_fmts)
438 		return -EINVAL;
439 
440 	f->pixelformat = ctx->coded_fmt_desc->decoded_fmts[f->index];
441 	return 0;
442 }
443 
444 static const struct v4l2_ioctl_ops rkvdec_ioctl_ops = {
445 	.vidioc_querycap = rkvdec_querycap,
446 	.vidioc_enum_framesizes = rkvdec_enum_framesizes,
447 
448 	.vidioc_try_fmt_vid_cap_mplane = rkvdec_try_capture_fmt,
449 	.vidioc_try_fmt_vid_out_mplane = rkvdec_try_output_fmt,
450 	.vidioc_s_fmt_vid_out_mplane = rkvdec_s_output_fmt,
451 	.vidioc_s_fmt_vid_cap_mplane = rkvdec_s_capture_fmt,
452 	.vidioc_g_fmt_vid_out_mplane = rkvdec_g_output_fmt,
453 	.vidioc_g_fmt_vid_cap_mplane = rkvdec_g_capture_fmt,
454 	.vidioc_enum_fmt_vid_out = rkvdec_enum_output_fmt,
455 	.vidioc_enum_fmt_vid_cap = rkvdec_enum_capture_fmt,
456 
457 	.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
458 	.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
459 	.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
460 	.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
461 	.vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
462 	.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
463 	.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
464 
465 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
466 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
467 
468 	.vidioc_streamon = v4l2_m2m_ioctl_streamon,
469 	.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
470 
471 	.vidioc_decoder_cmd = v4l2_m2m_ioctl_stateless_decoder_cmd,
472 	.vidioc_try_decoder_cmd = v4l2_m2m_ioctl_stateless_try_decoder_cmd,
473 };
474 
rkvdec_queue_setup(struct vb2_queue * vq,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])475 static int rkvdec_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
476 			      unsigned int *num_planes, unsigned int sizes[],
477 			      struct device *alloc_devs[])
478 {
479 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
480 	struct v4l2_format *f;
481 	unsigned int i;
482 
483 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
484 		f = &ctx->coded_fmt;
485 	else
486 		f = &ctx->decoded_fmt;
487 
488 	if (*num_planes) {
489 		if (*num_planes != f->fmt.pix_mp.num_planes)
490 			return -EINVAL;
491 
492 		for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
493 			if (sizes[i] < f->fmt.pix_mp.plane_fmt[i].sizeimage)
494 				return -EINVAL;
495 		}
496 	} else {
497 		*num_planes = f->fmt.pix_mp.num_planes;
498 		for (i = 0; i < f->fmt.pix_mp.num_planes; i++)
499 			sizes[i] = f->fmt.pix_mp.plane_fmt[i].sizeimage;
500 	}
501 
502 	return 0;
503 }
504 
rkvdec_buf_prepare(struct vb2_buffer * vb)505 static int rkvdec_buf_prepare(struct vb2_buffer *vb)
506 {
507 	struct vb2_queue *vq = vb->vb2_queue;
508 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
509 	struct v4l2_format *f;
510 	unsigned int i;
511 
512 	if (V4L2_TYPE_IS_OUTPUT(vq->type))
513 		f = &ctx->coded_fmt;
514 	else
515 		f = &ctx->decoded_fmt;
516 
517 	for (i = 0; i < f->fmt.pix_mp.num_planes; ++i) {
518 		u32 sizeimage = f->fmt.pix_mp.plane_fmt[i].sizeimage;
519 
520 		if (vb2_plane_size(vb, i) < sizeimage)
521 			return -EINVAL;
522 	}
523 
524 	/*
525 	 * Buffer's bytesused must be written by driver for CAPTURE buffers.
526 	 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
527 	 * it to buffer length).
528 	 */
529 	if (V4L2_TYPE_IS_CAPTURE(vq->type))
530 		vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage);
531 
532 	return 0;
533 }
534 
rkvdec_buf_queue(struct vb2_buffer * vb)535 static void rkvdec_buf_queue(struct vb2_buffer *vb)
536 {
537 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
538 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
539 
540 	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
541 }
542 
rkvdec_buf_out_validate(struct vb2_buffer * vb)543 static int rkvdec_buf_out_validate(struct vb2_buffer *vb)
544 {
545 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
546 
547 	vbuf->field = V4L2_FIELD_NONE;
548 	return 0;
549 }
550 
rkvdec_buf_request_complete(struct vb2_buffer * vb)551 static void rkvdec_buf_request_complete(struct vb2_buffer *vb)
552 {
553 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
554 
555 	v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_hdl);
556 }
557 
rkvdec_start_streaming(struct vb2_queue * q,unsigned int count)558 static int rkvdec_start_streaming(struct vb2_queue *q, unsigned int count)
559 {
560 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(q);
561 	const struct rkvdec_coded_fmt_desc *desc;
562 	int ret;
563 
564 	if (V4L2_TYPE_IS_CAPTURE(q->type))
565 		return 0;
566 
567 	desc = ctx->coded_fmt_desc;
568 	if (WARN_ON(!desc))
569 		return -EINVAL;
570 
571 	if (desc->ops->start) {
572 		ret = desc->ops->start(ctx);
573 		if (ret)
574 			return ret;
575 	}
576 
577 	return 0;
578 }
579 
rkvdec_queue_cleanup(struct vb2_queue * vq,u32 state)580 static void rkvdec_queue_cleanup(struct vb2_queue *vq, u32 state)
581 {
582 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
583 
584 	while (true) {
585 		struct vb2_v4l2_buffer *vbuf;
586 
587 		if (V4L2_TYPE_IS_OUTPUT(vq->type))
588 			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
589 		else
590 			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
591 
592 		if (!vbuf)
593 			break;
594 
595 		v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
596 					   &ctx->ctrl_hdl);
597 		v4l2_m2m_buf_done(vbuf, state);
598 	}
599 }
600 
rkvdec_stop_streaming(struct vb2_queue * q)601 static void rkvdec_stop_streaming(struct vb2_queue *q)
602 {
603 	struct rkvdec_ctx *ctx = vb2_get_drv_priv(q);
604 
605 	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
606 		const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
607 
608 		if (WARN_ON(!desc))
609 			return;
610 
611 		if (desc->ops->stop)
612 			desc->ops->stop(ctx);
613 	}
614 
615 	rkvdec_queue_cleanup(q, VB2_BUF_STATE_ERROR);
616 }
617 
618 static const struct vb2_ops rkvdec_queue_ops = {
619 	.queue_setup = rkvdec_queue_setup,
620 	.buf_prepare = rkvdec_buf_prepare,
621 	.buf_queue = rkvdec_buf_queue,
622 	.buf_out_validate = rkvdec_buf_out_validate,
623 	.buf_request_complete = rkvdec_buf_request_complete,
624 	.start_streaming = rkvdec_start_streaming,
625 	.stop_streaming = rkvdec_stop_streaming,
626 	.wait_prepare = vb2_ops_wait_prepare,
627 	.wait_finish = vb2_ops_wait_finish,
628 };
629 
rkvdec_request_validate(struct media_request * req)630 static int rkvdec_request_validate(struct media_request *req)
631 {
632 	unsigned int count;
633 
634 	count = vb2_request_buffer_cnt(req);
635 	if (!count)
636 		return -ENOENT;
637 	else if (count > 1)
638 		return -EINVAL;
639 
640 	return vb2_request_validate(req);
641 }
642 
643 static const struct media_device_ops rkvdec_media_ops = {
644 	.req_validate = rkvdec_request_validate,
645 	.req_queue = v4l2_m2m_request_queue,
646 };
647 
rkvdec_job_finish_no_pm(struct rkvdec_ctx * ctx,enum vb2_buffer_state result)648 static void rkvdec_job_finish_no_pm(struct rkvdec_ctx *ctx,
649 				    enum vb2_buffer_state result)
650 {
651 	if (ctx->coded_fmt_desc->ops->done) {
652 		struct vb2_v4l2_buffer *src_buf, *dst_buf;
653 
654 		src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
655 		dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
656 		ctx->coded_fmt_desc->ops->done(ctx, src_buf, dst_buf, result);
657 	}
658 
659 	v4l2_m2m_buf_done_and_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx,
660 					 result);
661 }
662 
rkvdec_job_finish(struct rkvdec_ctx * ctx,enum vb2_buffer_state result)663 static void rkvdec_job_finish(struct rkvdec_ctx *ctx,
664 			      enum vb2_buffer_state result)
665 {
666 	struct rkvdec_dev *rkvdec = ctx->dev;
667 
668 	pm_runtime_mark_last_busy(rkvdec->dev);
669 	pm_runtime_put_autosuspend(rkvdec->dev);
670 	rkvdec_job_finish_no_pm(ctx, result);
671 }
672 
rkvdec_run_preamble(struct rkvdec_ctx * ctx,struct rkvdec_run * run)673 void rkvdec_run_preamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run)
674 {
675 	struct media_request *src_req;
676 
677 	memset(run, 0, sizeof(*run));
678 
679 	run->bufs.src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
680 	run->bufs.dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
681 
682 	/* Apply request(s) controls if needed. */
683 	src_req = run->bufs.src->vb2_buf.req_obj.req;
684 	if (src_req)
685 		v4l2_ctrl_request_setup(src_req, &ctx->ctrl_hdl);
686 
687 	v4l2_m2m_buf_copy_metadata(run->bufs.src, run->bufs.dst, true);
688 }
689 
rkvdec_run_postamble(struct rkvdec_ctx * ctx,struct rkvdec_run * run)690 void rkvdec_run_postamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run)
691 {
692 	struct media_request *src_req = run->bufs.src->vb2_buf.req_obj.req;
693 
694 	if (src_req)
695 		v4l2_ctrl_request_complete(src_req, &ctx->ctrl_hdl);
696 }
697 
rkvdec_device_run(void * priv)698 static void rkvdec_device_run(void *priv)
699 {
700 	struct rkvdec_ctx *ctx = priv;
701 	struct rkvdec_dev *rkvdec = ctx->dev;
702 	const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
703 	int ret;
704 
705 	if (WARN_ON(!desc))
706 		return;
707 
708 	ret = pm_runtime_resume_and_get(rkvdec->dev);
709 	if (ret < 0) {
710 		rkvdec_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
711 		return;
712 	}
713 
714 	ret = desc->ops->run(ctx);
715 	if (ret)
716 		rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
717 }
718 
719 static const struct v4l2_m2m_ops rkvdec_m2m_ops = {
720 	.device_run = rkvdec_device_run,
721 };
722 
rkvdec_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)723 static int rkvdec_queue_init(void *priv,
724 			     struct vb2_queue *src_vq,
725 			     struct vb2_queue *dst_vq)
726 {
727 	struct rkvdec_ctx *ctx = priv;
728 	struct rkvdec_dev *rkvdec = ctx->dev;
729 	int ret;
730 
731 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
732 	src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
733 	src_vq->drv_priv = ctx;
734 	src_vq->ops = &rkvdec_queue_ops;
735 	src_vq->mem_ops = &vb2_dma_contig_memops;
736 
737 	/*
738 	 * Driver does mostly sequential access, so sacrifice TLB efficiency
739 	 * for faster allocation. Also, no CPU access on the source queue,
740 	 * so no kernel mapping needed.
741 	 */
742 	src_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
743 			    DMA_ATTR_NO_KERNEL_MAPPING;
744 	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
745 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
746 	src_vq->lock = &rkvdec->vdev_lock;
747 	src_vq->dev = rkvdec->v4l2_dev.dev;
748 	src_vq->supports_requests = true;
749 	src_vq->requires_requests = true;
750 
751 	ret = vb2_queue_init(src_vq);
752 	if (ret)
753 		return ret;
754 
755 	dst_vq->bidirectional = true;
756 	dst_vq->mem_ops = &vb2_dma_contig_memops;
757 	dst_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
758 			    DMA_ATTR_NO_KERNEL_MAPPING;
759 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
760 	dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
761 	dst_vq->drv_priv = ctx;
762 	dst_vq->ops = &rkvdec_queue_ops;
763 	dst_vq->buf_struct_size = sizeof(struct rkvdec_decoded_buffer);
764 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
765 	dst_vq->lock = &rkvdec->vdev_lock;
766 	dst_vq->dev = rkvdec->v4l2_dev.dev;
767 
768 	return vb2_queue_init(dst_vq);
769 }
770 
rkvdec_add_ctrls(struct rkvdec_ctx * ctx,const struct rkvdec_ctrls * ctrls)771 static int rkvdec_add_ctrls(struct rkvdec_ctx *ctx,
772 			    const struct rkvdec_ctrls *ctrls)
773 {
774 	unsigned int i;
775 
776 	for (i = 0; i < ctrls->num_ctrls; i++) {
777 		const struct v4l2_ctrl_config *cfg = &ctrls->ctrls[i].cfg;
778 
779 		v4l2_ctrl_new_custom(&ctx->ctrl_hdl, cfg, ctx);
780 		if (ctx->ctrl_hdl.error)
781 			return ctx->ctrl_hdl.error;
782 	}
783 
784 	return 0;
785 }
786 
rkvdec_init_ctrls(struct rkvdec_ctx * ctx)787 static int rkvdec_init_ctrls(struct rkvdec_ctx *ctx)
788 {
789 	unsigned int i, nctrls = 0;
790 	int ret;
791 
792 	for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++)
793 		nctrls += rkvdec_coded_fmts[i].ctrls->num_ctrls;
794 
795 	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, nctrls);
796 
797 	for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++) {
798 		ret = rkvdec_add_ctrls(ctx, rkvdec_coded_fmts[i].ctrls);
799 		if (ret)
800 			goto err_free_handler;
801 	}
802 
803 	ret = v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
804 	if (ret)
805 		goto err_free_handler;
806 
807 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
808 	return 0;
809 
810 err_free_handler:
811 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
812 	return ret;
813 }
814 
rkvdec_open(struct file * filp)815 static int rkvdec_open(struct file *filp)
816 {
817 	struct rkvdec_dev *rkvdec = video_drvdata(filp);
818 	struct rkvdec_ctx *ctx;
819 	int ret;
820 
821 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
822 	if (!ctx)
823 		return -ENOMEM;
824 
825 	ctx->dev = rkvdec;
826 	rkvdec_reset_coded_fmt(ctx);
827 	rkvdec_reset_decoded_fmt(ctx);
828 	v4l2_fh_init(&ctx->fh, video_devdata(filp));
829 
830 	ret = rkvdec_init_ctrls(ctx);
831 	if (ret)
832 		goto err_free_ctx;
833 
834 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(rkvdec->m2m_dev, ctx,
835 					    rkvdec_queue_init);
836 	if (IS_ERR(ctx->fh.m2m_ctx)) {
837 		ret = PTR_ERR(ctx->fh.m2m_ctx);
838 		goto err_cleanup_ctrls;
839 	}
840 
841 	filp->private_data = &ctx->fh;
842 	v4l2_fh_add(&ctx->fh);
843 
844 	return 0;
845 
846 err_cleanup_ctrls:
847 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
848 
849 err_free_ctx:
850 	kfree(ctx);
851 	return ret;
852 }
853 
rkvdec_release(struct file * filp)854 static int rkvdec_release(struct file *filp)
855 {
856 	struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(filp->private_data);
857 
858 	v4l2_fh_del(&ctx->fh);
859 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
860 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
861 	v4l2_fh_exit(&ctx->fh);
862 	kfree(ctx);
863 
864 	return 0;
865 }
866 
867 static const struct v4l2_file_operations rkvdec_fops = {
868 	.owner = THIS_MODULE,
869 	.open = rkvdec_open,
870 	.release = rkvdec_release,
871 	.poll = v4l2_m2m_fop_poll,
872 	.unlocked_ioctl = video_ioctl2,
873 	.mmap = v4l2_m2m_fop_mmap,
874 };
875 
rkvdec_v4l2_init(struct rkvdec_dev * rkvdec)876 static int rkvdec_v4l2_init(struct rkvdec_dev *rkvdec)
877 {
878 	int ret;
879 
880 	ret = v4l2_device_register(rkvdec->dev, &rkvdec->v4l2_dev);
881 	if (ret) {
882 		dev_err(rkvdec->dev, "Failed to register V4L2 device\n");
883 		return ret;
884 	}
885 
886 	rkvdec->m2m_dev = v4l2_m2m_init(&rkvdec_m2m_ops);
887 	if (IS_ERR(rkvdec->m2m_dev)) {
888 		v4l2_err(&rkvdec->v4l2_dev, "Failed to init mem2mem device\n");
889 		ret = PTR_ERR(rkvdec->m2m_dev);
890 		goto err_unregister_v4l2;
891 	}
892 
893 	rkvdec->mdev.dev = rkvdec->dev;
894 	strscpy(rkvdec->mdev.model, "rkvdec", sizeof(rkvdec->mdev.model));
895 	strscpy(rkvdec->mdev.bus_info, "platform:rkvdec",
896 		sizeof(rkvdec->mdev.bus_info));
897 	media_device_init(&rkvdec->mdev);
898 	rkvdec->mdev.ops = &rkvdec_media_ops;
899 	rkvdec->v4l2_dev.mdev = &rkvdec->mdev;
900 
901 	rkvdec->vdev.lock = &rkvdec->vdev_lock;
902 	rkvdec->vdev.v4l2_dev = &rkvdec->v4l2_dev;
903 	rkvdec->vdev.fops = &rkvdec_fops;
904 	rkvdec->vdev.release = video_device_release_empty;
905 	rkvdec->vdev.vfl_dir = VFL_DIR_M2M;
906 	rkvdec->vdev.device_caps = V4L2_CAP_STREAMING |
907 				   V4L2_CAP_VIDEO_M2M_MPLANE;
908 	rkvdec->vdev.ioctl_ops = &rkvdec_ioctl_ops;
909 	video_set_drvdata(&rkvdec->vdev, rkvdec);
910 	strscpy(rkvdec->vdev.name, "rkvdec", sizeof(rkvdec->vdev.name));
911 
912 	ret = video_register_device(&rkvdec->vdev, VFL_TYPE_VIDEO, -1);
913 	if (ret) {
914 		v4l2_err(&rkvdec->v4l2_dev, "Failed to register video device\n");
915 		goto err_cleanup_mc;
916 	}
917 
918 	ret = v4l2_m2m_register_media_controller(rkvdec->m2m_dev, &rkvdec->vdev,
919 						 MEDIA_ENT_F_PROC_VIDEO_DECODER);
920 	if (ret) {
921 		v4l2_err(&rkvdec->v4l2_dev,
922 			 "Failed to initialize V4L2 M2M media controller\n");
923 		goto err_unregister_vdev;
924 	}
925 
926 	ret = media_device_register(&rkvdec->mdev);
927 	if (ret) {
928 		v4l2_err(&rkvdec->v4l2_dev, "Failed to register media device\n");
929 		goto err_unregister_mc;
930 	}
931 
932 	return 0;
933 
934 err_unregister_mc:
935 	v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
936 
937 err_unregister_vdev:
938 	video_unregister_device(&rkvdec->vdev);
939 
940 err_cleanup_mc:
941 	media_device_cleanup(&rkvdec->mdev);
942 	v4l2_m2m_release(rkvdec->m2m_dev);
943 
944 err_unregister_v4l2:
945 	v4l2_device_unregister(&rkvdec->v4l2_dev);
946 	return ret;
947 }
948 
rkvdec_v4l2_cleanup(struct rkvdec_dev * rkvdec)949 static void rkvdec_v4l2_cleanup(struct rkvdec_dev *rkvdec)
950 {
951 	media_device_unregister(&rkvdec->mdev);
952 	v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
953 	video_unregister_device(&rkvdec->vdev);
954 	media_device_cleanup(&rkvdec->mdev);
955 	v4l2_m2m_release(rkvdec->m2m_dev);
956 	v4l2_device_unregister(&rkvdec->v4l2_dev);
957 }
958 
rkvdec_irq_handler(int irq,void * priv)959 static irqreturn_t rkvdec_irq_handler(int irq, void *priv)
960 {
961 	struct rkvdec_dev *rkvdec = priv;
962 	enum vb2_buffer_state state;
963 	u32 status;
964 
965 	status = readl(rkvdec->regs + RKVDEC_REG_INTERRUPT);
966 	state = (status & RKVDEC_RDY_STA) ?
967 		VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
968 
969 	writel(0, rkvdec->regs + RKVDEC_REG_INTERRUPT);
970 	if (cancel_delayed_work(&rkvdec->watchdog_work)) {
971 		struct rkvdec_ctx *ctx;
972 
973 		ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
974 		rkvdec_job_finish(ctx, state);
975 	}
976 
977 	return IRQ_HANDLED;
978 }
979 
rkvdec_watchdog_func(struct work_struct * work)980 static void rkvdec_watchdog_func(struct work_struct *work)
981 {
982 	struct rkvdec_dev *rkvdec;
983 	struct rkvdec_ctx *ctx;
984 
985 	rkvdec = container_of(to_delayed_work(work), struct rkvdec_dev,
986 			      watchdog_work);
987 	ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
988 	if (ctx) {
989 		dev_err(rkvdec->dev, "Frame processing timed out!\n");
990 		writel(RKVDEC_IRQ_DIS, rkvdec->regs + RKVDEC_REG_INTERRUPT);
991 		writel(0, rkvdec->regs + RKVDEC_REG_SYSCTRL);
992 		rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
993 	}
994 }
995 
996 static const struct of_device_id of_rkvdec_match[] = {
997 	{ .compatible = "rockchip,rk3399-vdec" },
998 	{ /* sentinel */ }
999 };
1000 MODULE_DEVICE_TABLE(of, of_rkvdec_match);
1001 
1002 static const char * const rkvdec_clk_names[] = {
1003 	"axi", "ahb", "cabac", "core"
1004 };
1005 
rkvdec_probe(struct platform_device * pdev)1006 static int rkvdec_probe(struct platform_device *pdev)
1007 {
1008 	struct rkvdec_dev *rkvdec;
1009 	unsigned int i;
1010 	int ret, irq;
1011 
1012 	rkvdec = devm_kzalloc(&pdev->dev, sizeof(*rkvdec), GFP_KERNEL);
1013 	if (!rkvdec)
1014 		return -ENOMEM;
1015 
1016 	platform_set_drvdata(pdev, rkvdec);
1017 	rkvdec->dev = &pdev->dev;
1018 	mutex_init(&rkvdec->vdev_lock);
1019 	INIT_DELAYED_WORK(&rkvdec->watchdog_work, rkvdec_watchdog_func);
1020 
1021 	rkvdec->clocks = devm_kcalloc(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
1022 				      sizeof(*rkvdec->clocks), GFP_KERNEL);
1023 	if (!rkvdec->clocks)
1024 		return -ENOMEM;
1025 
1026 	for (i = 0; i < ARRAY_SIZE(rkvdec_clk_names); i++)
1027 		rkvdec->clocks[i].id = rkvdec_clk_names[i];
1028 
1029 	ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
1030 				rkvdec->clocks);
1031 	if (ret)
1032 		return ret;
1033 
1034 	rkvdec->regs = devm_platform_ioremap_resource(pdev, 0);
1035 	if (IS_ERR(rkvdec->regs))
1036 		return PTR_ERR(rkvdec->regs);
1037 
1038 	ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1039 	if (ret) {
1040 		dev_err(&pdev->dev, "Could not set DMA coherent mask.\n");
1041 		return ret;
1042 	}
1043 
1044 	vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
1045 
1046 	irq = platform_get_irq(pdev, 0);
1047 	if (irq <= 0)
1048 		return -ENXIO;
1049 
1050 	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
1051 					rkvdec_irq_handler, IRQF_ONESHOT,
1052 					dev_name(&pdev->dev), rkvdec);
1053 	if (ret) {
1054 		dev_err(&pdev->dev, "Could not request vdec IRQ\n");
1055 		return ret;
1056 	}
1057 
1058 	pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
1059 	pm_runtime_use_autosuspend(&pdev->dev);
1060 	pm_runtime_enable(&pdev->dev);
1061 
1062 	ret = rkvdec_v4l2_init(rkvdec);
1063 	if (ret)
1064 		goto err_disable_runtime_pm;
1065 
1066 	return 0;
1067 
1068 err_disable_runtime_pm:
1069 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1070 	pm_runtime_disable(&pdev->dev);
1071 	return ret;
1072 }
1073 
rkvdec_remove(struct platform_device * pdev)1074 static void rkvdec_remove(struct platform_device *pdev)
1075 {
1076 	struct rkvdec_dev *rkvdec = platform_get_drvdata(pdev);
1077 
1078 	cancel_delayed_work_sync(&rkvdec->watchdog_work);
1079 
1080 	rkvdec_v4l2_cleanup(rkvdec);
1081 	pm_runtime_disable(&pdev->dev);
1082 	pm_runtime_dont_use_autosuspend(&pdev->dev);
1083 }
1084 
1085 #ifdef CONFIG_PM
rkvdec_runtime_resume(struct device * dev)1086 static int rkvdec_runtime_resume(struct device *dev)
1087 {
1088 	struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
1089 
1090 	return clk_bulk_prepare_enable(ARRAY_SIZE(rkvdec_clk_names),
1091 				       rkvdec->clocks);
1092 }
1093 
rkvdec_runtime_suspend(struct device * dev)1094 static int rkvdec_runtime_suspend(struct device *dev)
1095 {
1096 	struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
1097 
1098 	clk_bulk_disable_unprepare(ARRAY_SIZE(rkvdec_clk_names),
1099 				   rkvdec->clocks);
1100 	return 0;
1101 }
1102 #endif
1103 
1104 static const struct dev_pm_ops rkvdec_pm_ops = {
1105 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1106 				pm_runtime_force_resume)
1107 	SET_RUNTIME_PM_OPS(rkvdec_runtime_suspend, rkvdec_runtime_resume, NULL)
1108 };
1109 
1110 static struct platform_driver rkvdec_driver = {
1111 	.probe = rkvdec_probe,
1112 	.remove_new = rkvdec_remove,
1113 	.driver = {
1114 		   .name = "rkvdec",
1115 		   .of_match_table = of_rkvdec_match,
1116 		   .pm = &rkvdec_pm_ops,
1117 	},
1118 };
1119 module_platform_driver(rkvdec_driver);
1120 
1121 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@collabora.com>");
1122 MODULE_DESCRIPTION("Rockchip Video Decoder driver");
1123 MODULE_LICENSE("GPL v2");
1124