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