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