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