2 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 * Kamil Debski, <k.debski@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/clk.h>
15 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/videodev2.h>
22 #include <linux/workqueue.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-event.h>
25 #include <media/videobuf2-v4l2.h>
26 #include "s5p_mfc_common.h"
27 #include "s5p_mfc_ctrl.h"
28 #include "s5p_mfc_debug.h"
29 #include "s5p_mfc_dec.h"
30 #include "s5p_mfc_intr.h"
31 #include "s5p_mfc_opr.h"
32 #include "s5p_mfc_pm.h"
34 static struct s5p_mfc_fmt formats
[] = {
36 .name
= "4:2:0 2 Planes 16x16 Tiles",
37 .fourcc
= V4L2_PIX_FMT_NV12MT_16X16
,
38 .codec_mode
= S5P_MFC_CODEC_NONE
,
41 .versions
= MFC_V6_BIT
| MFC_V7_BIT
,
44 .name
= "4:2:0 2 Planes 64x32 Tiles",
45 .fourcc
= V4L2_PIX_FMT_NV12MT
,
46 .codec_mode
= S5P_MFC_CODEC_NONE
,
49 .versions
= MFC_V5_BIT
,
52 .name
= "4:2:0 2 Planes Y/CbCr",
53 .fourcc
= V4L2_PIX_FMT_NV12M
,
54 .codec_mode
= S5P_MFC_CODEC_NONE
,
57 .versions
= MFC_V6PLUS_BITS
,
60 .name
= "4:2:0 2 Planes Y/CrCb",
61 .fourcc
= V4L2_PIX_FMT_NV21M
,
62 .codec_mode
= S5P_MFC_CODEC_NONE
,
65 .versions
= MFC_V6PLUS_BITS
,
68 .name
= "H264 Encoded Stream",
69 .fourcc
= V4L2_PIX_FMT_H264
,
70 .codec_mode
= S5P_MFC_CODEC_H264_DEC
,
73 .versions
= MFC_V5PLUS_BITS
,
76 .name
= "H264/MVC Encoded Stream",
77 .fourcc
= V4L2_PIX_FMT_H264_MVC
,
78 .codec_mode
= S5P_MFC_CODEC_H264_MVC_DEC
,
81 .versions
= MFC_V6PLUS_BITS
,
84 .name
= "H263 Encoded Stream",
85 .fourcc
= V4L2_PIX_FMT_H263
,
86 .codec_mode
= S5P_MFC_CODEC_H263_DEC
,
89 .versions
= MFC_V5PLUS_BITS
,
92 .name
= "MPEG1 Encoded Stream",
93 .fourcc
= V4L2_PIX_FMT_MPEG1
,
94 .codec_mode
= S5P_MFC_CODEC_MPEG2_DEC
,
97 .versions
= MFC_V5PLUS_BITS
,
100 .name
= "MPEG2 Encoded Stream",
101 .fourcc
= V4L2_PIX_FMT_MPEG2
,
102 .codec_mode
= S5P_MFC_CODEC_MPEG2_DEC
,
105 .versions
= MFC_V5PLUS_BITS
,
108 .name
= "MPEG4 Encoded Stream",
109 .fourcc
= V4L2_PIX_FMT_MPEG4
,
110 .codec_mode
= S5P_MFC_CODEC_MPEG4_DEC
,
113 .versions
= MFC_V5PLUS_BITS
,
116 .name
= "XviD Encoded Stream",
117 .fourcc
= V4L2_PIX_FMT_XVID
,
118 .codec_mode
= S5P_MFC_CODEC_MPEG4_DEC
,
121 .versions
= MFC_V5PLUS_BITS
,
124 .name
= "VC1 Encoded Stream",
125 .fourcc
= V4L2_PIX_FMT_VC1_ANNEX_G
,
126 .codec_mode
= S5P_MFC_CODEC_VC1_DEC
,
129 .versions
= MFC_V5PLUS_BITS
,
132 .name
= "VC1 RCV Encoded Stream",
133 .fourcc
= V4L2_PIX_FMT_VC1_ANNEX_L
,
134 .codec_mode
= S5P_MFC_CODEC_VC1RCV_DEC
,
137 .versions
= MFC_V5PLUS_BITS
,
140 .name
= "VP8 Encoded Stream",
141 .fourcc
= V4L2_PIX_FMT_VP8
,
142 .codec_mode
= S5P_MFC_CODEC_VP8_DEC
,
145 .versions
= MFC_V6PLUS_BITS
,
148 .fourcc
= V4L2_PIX_FMT_HEVC
,
149 .codec_mode
= S5P_FIMV_CODEC_HEVC_DEC
,
152 .versions
= MFC_V10_BIT
,
155 .fourcc
= V4L2_PIX_FMT_VP9
,
156 .codec_mode
= S5P_FIMV_CODEC_VP9_DEC
,
159 .versions
= MFC_V10_BIT
,
163 #define NUM_FORMATS ARRAY_SIZE(formats)
165 /* Find selected format description */
166 static struct s5p_mfc_fmt
*find_format(struct v4l2_format
*f
, unsigned int t
)
170 for (i
= 0; i
< NUM_FORMATS
; i
++) {
171 if (formats
[i
].fourcc
== f
->fmt
.pix_mp
.pixelformat
&&
172 formats
[i
].type
== t
)
178 static struct mfc_control controls
[] = {
180 .id
= V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY
,
181 .type
= V4L2_CTRL_TYPE_INTEGER
,
182 .name
= "H264 Display Delay",
189 .id
= V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE
,
190 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
191 .name
= "H264 Display Delay Enable",
198 .id
= V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER
,
199 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
200 .name
= "Mpeg4 Loop Filter Enable",
207 .id
= V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE
,
208 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
209 .name
= "Slice Interface Enable",
216 .id
= V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
,
217 .type
= V4L2_CTRL_TYPE_INTEGER
,
218 .name
= "Minimum number of cap bufs",
227 #define NUM_CTRLS ARRAY_SIZE(controls)
229 /* Check whether a context should be run on hardware */
230 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx
*ctx
)
232 /* Context is to parse header */
233 if (ctx
->src_queue_cnt
>= 1 && ctx
->state
== MFCINST_GOT_INST
)
235 /* Context is to decode a frame */
236 if (ctx
->src_queue_cnt
>= 1 &&
237 ctx
->state
== MFCINST_RUNNING
&&
238 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
240 /* Context is to return last frame */
241 if (ctx
->state
== MFCINST_FINISHING
&&
242 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
244 /* Context is to set buffers */
245 if (ctx
->src_queue_cnt
>= 1 &&
246 ctx
->state
== MFCINST_HEAD_PARSED
&&
247 ctx
->capture_state
== QUEUE_BUFS_MMAPED
)
249 /* Resolution change */
250 if ((ctx
->state
== MFCINST_RES_CHANGE_INIT
||
251 ctx
->state
== MFCINST_RES_CHANGE_FLUSH
) &&
252 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
254 if (ctx
->state
== MFCINST_RES_CHANGE_END
&&
255 ctx
->src_queue_cnt
>= 1)
257 mfc_debug(2, "ctx is not ready\n");
261 static const struct s5p_mfc_codec_ops decoder_codec_ops
= {
262 .pre_seq_start
= NULL
,
263 .post_seq_start
= NULL
,
264 .pre_frame_start
= NULL
,
265 .post_frame_start
= NULL
,
268 /* Query capabilities of the device */
269 static int vidioc_querycap(struct file
*file
, void *priv
,
270 struct v4l2_capability
*cap
)
272 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
274 strlcpy(cap
->driver
, S5P_MFC_NAME
, sizeof(cap
->driver
));
275 strlcpy(cap
->card
, dev
->vfd_dec
->name
, sizeof(cap
->card
));
276 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
277 dev_name(&dev
->plat_dev
->dev
));
279 * This is only a mem-to-mem video device. The capture and output
280 * device capability flags are left only for backward compatibility
281 * and are scheduled for removal.
283 cap
->device_caps
= V4L2_CAP_VIDEO_M2M_MPLANE
| V4L2_CAP_STREAMING
;
284 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
288 /* Enumerate format */
289 static int vidioc_enum_fmt(struct file
*file
, struct v4l2_fmtdesc
*f
,
292 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
293 struct s5p_mfc_fmt
*fmt
;
296 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
297 if (out
&& formats
[i
].type
!= MFC_FMT_DEC
)
299 else if (!out
&& formats
[i
].type
!= MFC_FMT_RAW
)
301 else if ((dev
->variant
->version_bit
& formats
[i
].versions
) == 0)
308 if (i
== ARRAY_SIZE(formats
))
311 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
312 f
->pixelformat
= fmt
->fourcc
;
316 static int vidioc_enum_fmt_vid_cap_mplane(struct file
*file
, void *pirv
,
317 struct v4l2_fmtdesc
*f
)
319 return vidioc_enum_fmt(file
, f
, false);
322 static int vidioc_enum_fmt_vid_out_mplane(struct file
*file
, void *priv
,
323 struct v4l2_fmtdesc
*f
)
325 return vidioc_enum_fmt(file
, f
, true);
329 static int vidioc_g_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
331 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
332 struct v4l2_pix_format_mplane
*pix_mp
;
335 pix_mp
= &f
->fmt
.pix_mp
;
336 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
&&
337 (ctx
->state
== MFCINST_GOT_INST
|| ctx
->state
==
338 MFCINST_RES_CHANGE_END
)) {
339 /* If the MFC is parsing the header,
340 * so wait until it is finished */
341 s5p_mfc_wait_for_done_ctx(ctx
, S5P_MFC_R2H_CMD_SEQ_DONE_RET
,
344 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
&&
345 ctx
->state
>= MFCINST_HEAD_PARSED
&&
346 ctx
->state
< MFCINST_ABORT
) {
347 /* This is run on CAPTURE (decode output) */
348 /* Width and height are set to the dimensions
349 of the movie, the buffer is bigger and
350 further processing stages should crop to this
352 pix_mp
->width
= ctx
->buf_width
;
353 pix_mp
->height
= ctx
->buf_height
;
354 pix_mp
->field
= V4L2_FIELD_NONE
;
355 pix_mp
->num_planes
= 2;
356 /* Set pixelformat to the format in which MFC
357 outputs the decoded frame */
358 pix_mp
->pixelformat
= ctx
->dst_fmt
->fourcc
;
359 pix_mp
->plane_fmt
[0].bytesperline
= ctx
->buf_width
;
360 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->luma_size
;
361 pix_mp
->plane_fmt
[1].bytesperline
= ctx
->buf_width
;
362 pix_mp
->plane_fmt
[1].sizeimage
= ctx
->chroma_size
;
363 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
364 /* This is run on OUTPUT
365 The buffer contains compressed image
366 so width and height have no meaning */
369 pix_mp
->field
= V4L2_FIELD_NONE
;
370 pix_mp
->plane_fmt
[0].bytesperline
= ctx
->dec_src_buf_size
;
371 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->dec_src_buf_size
;
372 pix_mp
->pixelformat
= ctx
->src_fmt
->fourcc
;
373 pix_mp
->num_planes
= ctx
->src_fmt
->num_planes
;
375 mfc_err("Format could not be read\n");
376 mfc_debug(2, "%s-- with error\n", __func__
);
384 static int vidioc_try_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
386 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
387 struct s5p_mfc_fmt
*fmt
;
389 mfc_debug(2, "Type is %d\n", f
->type
);
390 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
391 fmt
= find_format(f
, MFC_FMT_DEC
);
393 mfc_err("Unsupported format for source.\n");
396 if (fmt
->codec_mode
== S5P_FIMV_CODEC_NONE
) {
397 mfc_err("Unknown codec\n");
400 if ((dev
->variant
->version_bit
& fmt
->versions
) == 0) {
401 mfc_err("Unsupported format by this MFC version.\n");
404 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
405 fmt
= find_format(f
, MFC_FMT_RAW
);
407 mfc_err("Unsupported format for destination.\n");
410 if ((dev
->variant
->version_bit
& fmt
->versions
) == 0) {
411 mfc_err("Unsupported format by this MFC version.\n");
420 static int vidioc_s_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
422 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
423 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
425 struct v4l2_pix_format_mplane
*pix_mp
;
426 struct s5p_mfc_buf_size
*buf_size
= dev
->variant
->buf_size
;
429 ret
= vidioc_try_fmt(file
, priv
, f
);
430 pix_mp
= &f
->fmt
.pix_mp
;
433 if (vb2_is_streaming(&ctx
->vq_src
) || vb2_is_streaming(&ctx
->vq_dst
)) {
434 v4l2_err(&dev
->v4l2_dev
, "%s queue busy\n", __func__
);
438 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
439 /* dst_fmt is validated by call to vidioc_try_fmt */
440 ctx
->dst_fmt
= find_format(f
, MFC_FMT_RAW
);
443 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
444 /* src_fmt is validated by call to vidioc_try_fmt */
445 ctx
->src_fmt
= find_format(f
, MFC_FMT_DEC
);
446 ctx
->codec_mode
= ctx
->src_fmt
->codec_mode
;
447 mfc_debug(2, "The codec number is: %d\n", ctx
->codec_mode
);
450 if (pix_mp
->plane_fmt
[0].sizeimage
== 0)
451 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->dec_src_buf_size
=
453 else if (pix_mp
->plane_fmt
[0].sizeimage
> buf_size
->cpb
)
454 ctx
->dec_src_buf_size
= buf_size
->cpb
;
456 ctx
->dec_src_buf_size
= pix_mp
->plane_fmt
[0].sizeimage
;
457 pix_mp
->plane_fmt
[0].bytesperline
= 0;
458 ctx
->state
= MFCINST_INIT
;
462 mfc_err("Wrong type error for S_FMT : %d", f
->type
);
472 static int reqbufs_output(struct s5p_mfc_dev
*dev
, struct s5p_mfc_ctx
*ctx
,
473 struct v4l2_requestbuffers
*reqbufs
)
479 if (reqbufs
->count
== 0) {
480 mfc_debug(2, "Freeing buffers\n");
481 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
484 ctx
->src_bufs_cnt
= 0;
485 ctx
->output_state
= QUEUE_FREE
;
486 } else if (ctx
->output_state
== QUEUE_FREE
) {
487 /* Can only request buffers when we have a valid format set. */
488 WARN_ON(ctx
->src_bufs_cnt
!= 0);
489 if (ctx
->state
!= MFCINST_INIT
) {
490 mfc_err("Reqbufs called in an invalid state\n");
495 mfc_debug(2, "Allocating %d buffers for OUTPUT queue\n",
497 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
501 ret
= s5p_mfc_open_mfc_inst(dev
, ctx
);
504 vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
508 ctx
->output_state
= QUEUE_BUFS_REQUESTED
;
510 mfc_err("Buffers have already been requested\n");
516 mfc_err("Failed allocating buffers for OUTPUT queue\n");
520 static int reqbufs_capture(struct s5p_mfc_dev
*dev
, struct s5p_mfc_ctx
*ctx
,
521 struct v4l2_requestbuffers
*reqbufs
)
527 if (reqbufs
->count
== 0) {
528 mfc_debug(2, "Freeing buffers\n");
529 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
532 s5p_mfc_hw_call(dev
->mfc_ops
, release_codec_buffers
, ctx
);
533 ctx
->dst_bufs_cnt
= 0;
534 } else if (ctx
->capture_state
== QUEUE_FREE
) {
535 WARN_ON(ctx
->dst_bufs_cnt
!= 0);
536 mfc_debug(2, "Allocating %d buffers for CAPTURE queue\n",
538 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
542 ctx
->capture_state
= QUEUE_BUFS_REQUESTED
;
543 ctx
->total_dpb_count
= reqbufs
->count
;
545 ret
= s5p_mfc_hw_call(dev
->mfc_ops
, alloc_codec_buffers
, ctx
);
547 mfc_err("Failed to allocate decoding buffers\n");
549 vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
551 ctx
->capture_state
= QUEUE_FREE
;
555 WARN_ON(ctx
->dst_bufs_cnt
!= ctx
->total_dpb_count
);
556 ctx
->capture_state
= QUEUE_BUFS_MMAPED
;
558 if (s5p_mfc_ctx_ready(ctx
))
559 set_work_bit_irqsave(ctx
);
560 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
561 s5p_mfc_wait_for_done_ctx(ctx
, S5P_MFC_R2H_CMD_INIT_BUFFERS_RET
,
564 mfc_err("Buffers have already been requested\n");
570 mfc_err("Failed allocating buffers for CAPTURE queue\n");
574 /* Request buffers */
575 static int vidioc_reqbufs(struct file
*file
, void *priv
,
576 struct v4l2_requestbuffers
*reqbufs
)
578 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
579 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
581 if (reqbufs
->memory
!= V4L2_MEMORY_MMAP
) {
582 mfc_debug(2, "Only V4L2_MEMORY_MMAP is supported\n");
586 if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
587 return reqbufs_output(dev
, ctx
, reqbufs
);
588 } else if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
589 return reqbufs_capture(dev
, ctx
, reqbufs
);
591 mfc_err("Invalid type requested\n");
597 static int vidioc_querybuf(struct file
*file
, void *priv
,
598 struct v4l2_buffer
*buf
)
600 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
604 if (buf
->memory
!= V4L2_MEMORY_MMAP
) {
605 mfc_err("Only mmaped buffers can be used\n");
608 mfc_debug(2, "State: %d, buf->type: %d\n", ctx
->state
, buf
->type
);
609 if (ctx
->state
== MFCINST_GOT_INST
&&
610 buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
611 ret
= vb2_querybuf(&ctx
->vq_src
, buf
);
612 } else if (ctx
->state
== MFCINST_RUNNING
&&
613 buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
614 ret
= vb2_querybuf(&ctx
->vq_dst
, buf
);
615 for (i
= 0; i
< buf
->length
; i
++)
616 buf
->m
.planes
[i
].m
.mem_offset
+= DST_QUEUE_OFF_BASE
;
618 mfc_err("vidioc_querybuf called in an inappropriate state\n");
626 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
628 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
630 if (ctx
->state
== MFCINST_ERROR
) {
631 mfc_err("Call on QBUF after unrecoverable error\n");
634 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
635 return vb2_qbuf(&ctx
->vq_src
, buf
);
636 else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
637 return vb2_qbuf(&ctx
->vq_dst
, buf
);
641 /* Dequeue a buffer */
642 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
644 const struct v4l2_event ev
= {
645 .type
= V4L2_EVENT_EOS
647 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
650 if (ctx
->state
== MFCINST_ERROR
) {
651 mfc_err_limited("Call on DQBUF after unrecoverable error\n");
656 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
:
657 return vb2_dqbuf(&ctx
->vq_src
, buf
, file
->f_flags
& O_NONBLOCK
);
658 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
659 ret
= vb2_dqbuf(&ctx
->vq_dst
, buf
, file
->f_flags
& O_NONBLOCK
);
663 if (ctx
->state
== MFCINST_FINISHED
&&
664 (ctx
->dst_bufs
[buf
->index
].flags
& MFC_BUF_FLAG_EOS
))
665 v4l2_event_queue_fh(&ctx
->fh
, &ev
);
672 /* Export DMA buffer */
673 static int vidioc_expbuf(struct file
*file
, void *priv
,
674 struct v4l2_exportbuffer
*eb
)
676 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
678 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
679 return vb2_expbuf(&ctx
->vq_src
, eb
);
680 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
681 return vb2_expbuf(&ctx
->vq_dst
, eb
);
686 static int vidioc_streamon(struct file
*file
, void *priv
,
687 enum v4l2_buf_type type
)
689 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
693 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
694 ret
= vb2_streamon(&ctx
->vq_src
, type
);
695 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
696 ret
= vb2_streamon(&ctx
->vq_dst
, type
);
701 /* Stream off, which equals to a pause */
702 static int vidioc_streamoff(struct file
*file
, void *priv
,
703 enum v4l2_buf_type type
)
705 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
707 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
708 return vb2_streamoff(&ctx
->vq_src
, type
);
709 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
710 return vb2_streamoff(&ctx
->vq_dst
, type
);
714 /* Set controls - v4l2 control framework */
715 static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl
*ctrl
)
717 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
720 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY
:
721 ctx
->display_delay
= ctrl
->val
;
723 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE
:
724 ctx
->display_delay_enable
= ctrl
->val
;
726 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER
:
727 ctx
->loop_filter_mpeg4
= ctrl
->val
;
729 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE
:
730 ctx
->slice_interface
= ctrl
->val
;
733 mfc_err("Invalid control 0x%08x\n", ctrl
->id
);
739 static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl
*ctrl
)
741 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
742 struct s5p_mfc_dev
*dev
= ctx
->dev
;
745 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
:
746 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
747 ctx
->state
< MFCINST_ABORT
) {
748 ctrl
->val
= ctx
->pb_count
;
750 } else if (ctx
->state
!= MFCINST_INIT
&&
751 ctx
->state
!= MFCINST_RES_CHANGE_END
) {
752 v4l2_err(&dev
->v4l2_dev
, "Decoding not initialised\n");
755 /* Should wait for the header to be parsed */
756 s5p_mfc_wait_for_done_ctx(ctx
,
757 S5P_MFC_R2H_CMD_SEQ_DONE_RET
, 0);
758 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
759 ctx
->state
< MFCINST_ABORT
) {
760 ctrl
->val
= ctx
->pb_count
;
762 v4l2_err(&dev
->v4l2_dev
, "Decoding not initialised\n");
771 static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops
= {
772 .s_ctrl
= s5p_mfc_dec_s_ctrl
,
773 .g_volatile_ctrl
= s5p_mfc_dec_g_v_ctrl
,
776 /* Get cropping information */
777 static int vidioc_g_crop(struct file
*file
, void *priv
,
778 struct v4l2_crop
*cr
)
780 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
781 struct s5p_mfc_dev
*dev
= ctx
->dev
;
782 u32 left
, right
, top
, bottom
;
784 if (ctx
->state
!= MFCINST_HEAD_PARSED
&&
785 ctx
->state
!= MFCINST_RUNNING
&&
786 ctx
->state
!= MFCINST_FINISHING
&&
787 ctx
->state
!= MFCINST_FINISHED
) {
788 mfc_err("Can not get crop information\n");
791 if (ctx
->src_fmt
->fourcc
== V4L2_PIX_FMT_H264
) {
792 left
= s5p_mfc_hw_call(dev
->mfc_ops
, get_crop_info_h
, ctx
);
793 right
= left
>> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT
;
794 left
= left
& S5P_FIMV_SHARED_CROP_LEFT_MASK
;
795 top
= s5p_mfc_hw_call(dev
->mfc_ops
, get_crop_info_v
, ctx
);
796 bottom
= top
>> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT
;
797 top
= top
& S5P_FIMV_SHARED_CROP_TOP_MASK
;
800 cr
->c
.width
= ctx
->img_width
- left
- right
;
801 cr
->c
.height
= ctx
->img_height
- top
- bottom
;
802 mfc_debug(2, "Cropping info [h264]: l=%d t=%d w=%d h=%d (r=%d b=%d fw=%d fh=%d\n",
803 left
, top
, cr
->c
.width
, cr
->c
.height
, right
, bottom
,
804 ctx
->buf_width
, ctx
->buf_height
);
808 cr
->c
.width
= ctx
->img_width
;
809 cr
->c
.height
= ctx
->img_height
;
810 mfc_debug(2, "Cropping info: w=%d h=%d fw=%d fh=%d\n",
811 cr
->c
.width
, cr
->c
.height
, ctx
->buf_width
,
817 static int vidioc_decoder_cmd(struct file
*file
, void *priv
,
818 struct v4l2_decoder_cmd
*cmd
)
820 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
821 struct s5p_mfc_dev
*dev
= ctx
->dev
;
822 struct s5p_mfc_buf
*buf
;
826 case V4L2_DEC_CMD_STOP
:
830 if (!vb2_is_streaming(&ctx
->vq_src
))
833 spin_lock_irqsave(&dev
->irqlock
, flags
);
834 if (list_empty(&ctx
->src_queue
)) {
835 mfc_err("EOS: empty src queue, entering finishing state");
836 ctx
->state
= MFCINST_FINISHING
;
837 if (s5p_mfc_ctx_ready(ctx
))
838 set_work_bit_irqsave(ctx
);
839 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
840 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
842 mfc_err("EOS: marking last buffer of stream");
843 buf
= list_entry(ctx
->src_queue
.prev
,
844 struct s5p_mfc_buf
, list
);
845 if (buf
->flags
& MFC_BUF_FLAG_USED
)
846 ctx
->state
= MFCINST_FINISHING
;
848 buf
->flags
|= MFC_BUF_FLAG_EOS
;
849 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
858 static int vidioc_subscribe_event(struct v4l2_fh
*fh
,
859 const struct v4l2_event_subscription
*sub
)
863 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
864 case V4L2_EVENT_SOURCE_CHANGE
:
865 return v4l2_src_change_event_subscribe(fh
, sub
);
873 static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops
= {
874 .vidioc_querycap
= vidioc_querycap
,
875 .vidioc_enum_fmt_vid_cap_mplane
= vidioc_enum_fmt_vid_cap_mplane
,
876 .vidioc_enum_fmt_vid_out_mplane
= vidioc_enum_fmt_vid_out_mplane
,
877 .vidioc_g_fmt_vid_cap_mplane
= vidioc_g_fmt
,
878 .vidioc_g_fmt_vid_out_mplane
= vidioc_g_fmt
,
879 .vidioc_try_fmt_vid_cap_mplane
= vidioc_try_fmt
,
880 .vidioc_try_fmt_vid_out_mplane
= vidioc_try_fmt
,
881 .vidioc_s_fmt_vid_cap_mplane
= vidioc_s_fmt
,
882 .vidioc_s_fmt_vid_out_mplane
= vidioc_s_fmt
,
883 .vidioc_reqbufs
= vidioc_reqbufs
,
884 .vidioc_querybuf
= vidioc_querybuf
,
885 .vidioc_qbuf
= vidioc_qbuf
,
886 .vidioc_dqbuf
= vidioc_dqbuf
,
887 .vidioc_expbuf
= vidioc_expbuf
,
888 .vidioc_streamon
= vidioc_streamon
,
889 .vidioc_streamoff
= vidioc_streamoff
,
890 .vidioc_g_crop
= vidioc_g_crop
,
891 .vidioc_decoder_cmd
= vidioc_decoder_cmd
,
892 .vidioc_subscribe_event
= vidioc_subscribe_event
,
893 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
896 static int s5p_mfc_queue_setup(struct vb2_queue
*vq
,
897 unsigned int *buf_count
,
898 unsigned int *plane_count
, unsigned int psize
[],
899 struct device
*alloc_devs
[])
901 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
902 struct s5p_mfc_dev
*dev
= ctx
->dev
;
904 /* Video output for decoding (source)
905 * this can be set after getting an instance */
906 if (ctx
->state
== MFCINST_INIT
&&
907 vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
908 /* A single plane is required for input */
912 if (*buf_count
> MFC_MAX_BUFFERS
)
913 *buf_count
= MFC_MAX_BUFFERS
;
914 /* Video capture for decoding (destination)
915 * this can be set after the header was parsed */
916 } else if (ctx
->state
== MFCINST_HEAD_PARSED
&&
917 vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
918 /* Output plane count is 2 - one for Y and one for CbCr */
920 /* Setup buffer count */
921 if (*buf_count
< ctx
->pb_count
)
922 *buf_count
= ctx
->pb_count
;
923 if (*buf_count
> ctx
->pb_count
+ MFC_MAX_EXTRA_DPB
)
924 *buf_count
= ctx
->pb_count
+ MFC_MAX_EXTRA_DPB
;
925 if (*buf_count
> MFC_MAX_BUFFERS
)
926 *buf_count
= MFC_MAX_BUFFERS
;
928 mfc_err("State seems invalid. State = %d, vq->type = %d\n",
929 ctx
->state
, vq
->type
);
932 mfc_debug(2, "Buffer count=%d, plane count=%d\n",
933 *buf_count
, *plane_count
);
934 if (ctx
->state
== MFCINST_HEAD_PARSED
&&
935 vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
936 psize
[0] = ctx
->luma_size
;
937 psize
[1] = ctx
->chroma_size
;
939 if (IS_MFCV6_PLUS(dev
))
940 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
942 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_R_CTX
];
943 alloc_devs
[1] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
944 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
&&
945 ctx
->state
== MFCINST_INIT
) {
946 psize
[0] = ctx
->dec_src_buf_size
;
947 alloc_devs
[0] = ctx
->dev
->mem_dev
[BANK_L_CTX
];
949 mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
955 static int s5p_mfc_buf_init(struct vb2_buffer
*vb
)
957 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
958 struct vb2_queue
*vq
= vb
->vb2_queue
;
959 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
962 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
963 if (ctx
->capture_state
== QUEUE_BUFS_MMAPED
)
965 for (i
= 0; i
< ctx
->dst_fmt
->num_planes
; i
++) {
966 if (IS_ERR_OR_NULL(ERR_PTR(
967 vb2_dma_contig_plane_dma_addr(vb
, i
)))) {
968 mfc_err("Plane mem not allocated\n");
972 if (vb2_plane_size(vb
, 0) < ctx
->luma_size
||
973 vb2_plane_size(vb
, 1) < ctx
->chroma_size
) {
974 mfc_err("Plane buffer (CAPTURE) is too small\n");
978 ctx
->dst_bufs
[i
].b
= vbuf
;
979 ctx
->dst_bufs
[i
].cookie
.raw
.luma
=
980 vb2_dma_contig_plane_dma_addr(vb
, 0);
981 ctx
->dst_bufs
[i
].cookie
.raw
.chroma
=
982 vb2_dma_contig_plane_dma_addr(vb
, 1);
984 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
985 if (IS_ERR_OR_NULL(ERR_PTR(
986 vb2_dma_contig_plane_dma_addr(vb
, 0)))) {
987 mfc_err("Plane memory not allocated\n");
990 if (vb2_plane_size(vb
, 0) < ctx
->dec_src_buf_size
) {
991 mfc_err("Plane buffer (OUTPUT) is too small\n");
996 ctx
->src_bufs
[i
].b
= vbuf
;
997 ctx
->src_bufs
[i
].cookie
.stream
=
998 vb2_dma_contig_plane_dma_addr(vb
, 0);
1001 mfc_err("s5p_mfc_buf_init: unknown queue type\n");
1007 static int s5p_mfc_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1009 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
1010 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1012 v4l2_ctrl_handler_setup(&ctx
->ctrl_handler
);
1013 if (ctx
->state
== MFCINST_FINISHING
||
1014 ctx
->state
== MFCINST_FINISHED
)
1015 ctx
->state
= MFCINST_RUNNING
;
1016 /* If context is ready then dev = work->data;schedule it to run */
1017 if (s5p_mfc_ctx_ready(ctx
))
1018 set_work_bit_irqsave(ctx
);
1019 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
1023 static void s5p_mfc_stop_streaming(struct vb2_queue
*q
)
1025 unsigned long flags
;
1026 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
1027 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1030 spin_lock_irqsave(&dev
->irqlock
, flags
);
1031 if ((ctx
->state
== MFCINST_FINISHING
||
1032 ctx
->state
== MFCINST_RUNNING
) &&
1033 dev
->curr_ctx
== ctx
->num
&& dev
->hw_lock
) {
1034 ctx
->state
= MFCINST_ABORT
;
1035 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1036 s5p_mfc_wait_for_done_ctx(ctx
,
1037 S5P_MFC_R2H_CMD_FRAME_DONE_RET
, 0);
1039 spin_lock_irqsave(&dev
->irqlock
, flags
);
1041 if (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1042 s5p_mfc_cleanup_queue(&ctx
->dst_queue
, &ctx
->vq_dst
);
1043 INIT_LIST_HEAD(&ctx
->dst_queue
);
1044 ctx
->dst_queue_cnt
= 0;
1045 ctx
->dpb_flush_flag
= 1;
1046 ctx
->dec_dst_flag
= 0;
1047 if (IS_MFCV6_PLUS(dev
) && (ctx
->state
== MFCINST_RUNNING
)) {
1048 ctx
->state
= MFCINST_FLUSH
;
1049 set_work_bit_irqsave(ctx
);
1050 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
1051 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1052 if (s5p_mfc_wait_for_done_ctx(ctx
,
1053 S5P_MFC_R2H_CMD_DPB_FLUSH_RET
, 0))
1054 mfc_err("Err flushing buffers\n");
1055 spin_lock_irqsave(&dev
->irqlock
, flags
);
1057 } else if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1058 s5p_mfc_cleanup_queue(&ctx
->src_queue
, &ctx
->vq_src
);
1059 INIT_LIST_HEAD(&ctx
->src_queue
);
1060 ctx
->src_queue_cnt
= 0;
1063 ctx
->state
= MFCINST_RUNNING
;
1064 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1068 static void s5p_mfc_buf_queue(struct vb2_buffer
*vb
)
1070 struct vb2_queue
*vq
= vb
->vb2_queue
;
1071 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
1072 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1073 unsigned long flags
;
1074 struct s5p_mfc_buf
*mfc_buf
;
1076 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1077 mfc_buf
= &ctx
->src_bufs
[vb
->index
];
1078 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
1079 spin_lock_irqsave(&dev
->irqlock
, flags
);
1080 list_add_tail(&mfc_buf
->list
, &ctx
->src_queue
);
1081 ctx
->src_queue_cnt
++;
1082 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1083 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1084 mfc_buf
= &ctx
->dst_bufs
[vb
->index
];
1085 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
1086 /* Mark destination as available for use by MFC */
1087 spin_lock_irqsave(&dev
->irqlock
, flags
);
1088 set_bit(vb
->index
, &ctx
->dec_dst_flag
);
1089 list_add_tail(&mfc_buf
->list
, &ctx
->dst_queue
);
1090 ctx
->dst_queue_cnt
++;
1091 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1093 mfc_err("Unsupported buffer type (%d)\n", vq
->type
);
1095 if (s5p_mfc_ctx_ready(ctx
))
1096 set_work_bit_irqsave(ctx
);
1097 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
1100 static struct vb2_ops s5p_mfc_dec_qops
= {
1101 .queue_setup
= s5p_mfc_queue_setup
,
1102 .wait_prepare
= vb2_ops_wait_prepare
,
1103 .wait_finish
= vb2_ops_wait_finish
,
1104 .buf_init
= s5p_mfc_buf_init
,
1105 .start_streaming
= s5p_mfc_start_streaming
,
1106 .stop_streaming
= s5p_mfc_stop_streaming
,
1107 .buf_queue
= s5p_mfc_buf_queue
,
1110 const struct s5p_mfc_codec_ops
*get_dec_codec_ops(void)
1112 return &decoder_codec_ops
;
1115 struct vb2_ops
*get_dec_queue_ops(void)
1117 return &s5p_mfc_dec_qops
;
1120 const struct v4l2_ioctl_ops
*get_dec_v4l2_ioctl_ops(void)
1122 return &s5p_mfc_dec_ioctl_ops
;
1125 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2WHICH(x) == V4L2_CTRL_CLASS_MPEG) \
1126 && V4L2_CTRL_DRIVER_PRIV(x))
1128 int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx
*ctx
)
1130 struct v4l2_ctrl_config cfg
;
1133 v4l2_ctrl_handler_init(&ctx
->ctrl_handler
, NUM_CTRLS
);
1134 if (ctx
->ctrl_handler
.error
) {
1135 mfc_err("v4l2_ctrl_handler_init failed\n");
1136 return ctx
->ctrl_handler
.error
;
1139 for (i
= 0; i
< NUM_CTRLS
; i
++) {
1140 if (IS_MFC51_PRIV(controls
[i
].id
)) {
1141 memset(&cfg
, 0, sizeof(struct v4l2_ctrl_config
));
1142 cfg
.ops
= &s5p_mfc_dec_ctrl_ops
;
1143 cfg
.id
= controls
[i
].id
;
1144 cfg
.min
= controls
[i
].minimum
;
1145 cfg
.max
= controls
[i
].maximum
;
1146 cfg
.def
= controls
[i
].default_value
;
1147 cfg
.name
= controls
[i
].name
;
1148 cfg
.type
= controls
[i
].type
;
1150 cfg
.step
= controls
[i
].step
;
1151 cfg
.menu_skip_mask
= 0;
1153 ctx
->ctrls
[i
] = v4l2_ctrl_new_custom(&ctx
->ctrl_handler
,
1156 ctx
->ctrls
[i
] = v4l2_ctrl_new_std(&ctx
->ctrl_handler
,
1157 &s5p_mfc_dec_ctrl_ops
,
1158 controls
[i
].id
, controls
[i
].minimum
,
1159 controls
[i
].maximum
, controls
[i
].step
,
1160 controls
[i
].default_value
);
1162 if (ctx
->ctrl_handler
.error
) {
1163 mfc_err("Adding control (%d) failed\n", i
);
1164 return ctx
->ctrl_handler
.error
;
1166 if (controls
[i
].is_volatile
&& ctx
->ctrls
[i
])
1167 ctx
->ctrls
[i
]->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
1172 void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx
*ctx
)
1176 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
1177 for (i
= 0; i
< NUM_CTRLS
; i
++)
1178 ctx
->ctrls
[i
] = NULL
;
1181 void s5p_mfc_dec_init(struct s5p_mfc_ctx
*ctx
)
1183 struct v4l2_format f
;
1184 f
.fmt
.pix_mp
.pixelformat
= V4L2_PIX_FMT_H264
;
1185 ctx
->src_fmt
= find_format(&f
, MFC_FMT_DEC
);
1186 if (IS_MFCV8_PLUS(ctx
->dev
))
1187 f
.fmt
.pix_mp
.pixelformat
= V4L2_PIX_FMT_NV12M
;
1188 else if (IS_MFCV6_PLUS(ctx
->dev
))
1189 f
.fmt
.pix_mp
.pixelformat
= V4L2_PIX_FMT_NV12MT_16X16
;
1191 f
.fmt
.pix_mp
.pixelformat
= V4L2_PIX_FMT_NV12MT
;
1192 ctx
->dst_fmt
= find_format(&f
, MFC_FMT_RAW
);
1193 mfc_debug(2, "Default src_fmt is %p, dest_fmt is %p\n",
1194 ctx
->src_fmt
, ctx
->dst_fmt
);