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/version.h>
22 #include <linux/videodev2.h>
23 #include <linux/workqueue.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-event.h>
26 #include <media/videobuf2-core.h>
27 #include "s5p_mfc_common.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 #define DEF_SRC_FMT_DEC V4L2_PIX_FMT_H264
35 #define DEF_DST_FMT_DEC V4L2_PIX_FMT_NV12MT_16X16
37 static struct s5p_mfc_fmt formats
[] = {
39 .name
= "4:2:0 2 Planes 16x16 Tiles",
40 .fourcc
= V4L2_PIX_FMT_NV12MT_16X16
,
41 .codec_mode
= S5P_MFC_CODEC_NONE
,
46 .name
= "4:2:0 2 Planes 64x32 Tiles",
47 .fourcc
= V4L2_PIX_FMT_NV12MT
,
48 .codec_mode
= S5P_MFC_CODEC_NONE
,
53 .name
= "4:2:0 2 Planes Y/CbCr",
54 .fourcc
= V4L2_PIX_FMT_NV12M
,
55 .codec_mode
= S5P_MFC_CODEC_NONE
,
60 .name
= "4:2:0 2 Planes Y/CrCb",
61 .fourcc
= V4L2_PIX_FMT_NV21M
,
62 .codec_mode
= S5P_MFC_CODEC_NONE
,
67 .name
= "H264 Encoded Stream",
68 .fourcc
= V4L2_PIX_FMT_H264
,
69 .codec_mode
= S5P_MFC_CODEC_H264_DEC
,
74 .name
= "H264/MVC Encoded Stream",
75 .fourcc
= V4L2_PIX_FMT_H264_MVC
,
76 .codec_mode
= S5P_MFC_CODEC_H264_MVC_DEC
,
81 .name
= "H263 Encoded Stream",
82 .fourcc
= V4L2_PIX_FMT_H263
,
83 .codec_mode
= S5P_MFC_CODEC_H263_DEC
,
88 .name
= "MPEG1 Encoded Stream",
89 .fourcc
= V4L2_PIX_FMT_MPEG1
,
90 .codec_mode
= S5P_MFC_CODEC_MPEG2_DEC
,
95 .name
= "MPEG2 Encoded Stream",
96 .fourcc
= V4L2_PIX_FMT_MPEG2
,
97 .codec_mode
= S5P_MFC_CODEC_MPEG2_DEC
,
102 .name
= "MPEG4 Encoded Stream",
103 .fourcc
= V4L2_PIX_FMT_MPEG4
,
104 .codec_mode
= S5P_MFC_CODEC_MPEG4_DEC
,
109 .name
= "XviD Encoded Stream",
110 .fourcc
= V4L2_PIX_FMT_XVID
,
111 .codec_mode
= S5P_MFC_CODEC_MPEG4_DEC
,
116 .name
= "VC1 Encoded Stream",
117 .fourcc
= V4L2_PIX_FMT_VC1_ANNEX_G
,
118 .codec_mode
= S5P_MFC_CODEC_VC1_DEC
,
123 .name
= "VC1 RCV Encoded Stream",
124 .fourcc
= V4L2_PIX_FMT_VC1_ANNEX_L
,
125 .codec_mode
= S5P_MFC_CODEC_VC1RCV_DEC
,
130 .name
= "VP8 Encoded Stream",
131 .fourcc
= V4L2_PIX_FMT_VP8
,
132 .codec_mode
= S5P_MFC_CODEC_VP8_DEC
,
138 #define NUM_FORMATS ARRAY_SIZE(formats)
140 /* Find selected format description */
141 static struct s5p_mfc_fmt
*find_format(struct v4l2_format
*f
, unsigned int t
)
145 for (i
= 0; i
< NUM_FORMATS
; i
++) {
146 if (formats
[i
].fourcc
== f
->fmt
.pix_mp
.pixelformat
&&
147 formats
[i
].type
== t
)
153 static struct mfc_control controls
[] = {
155 .id
= V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY
,
156 .type
= V4L2_CTRL_TYPE_INTEGER
,
157 .name
= "H264 Display Delay",
164 .id
= V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE
,
165 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
166 .name
= "H264 Display Delay Enable",
173 .id
= V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER
,
174 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
175 .name
= "Mpeg4 Loop Filter Enable",
182 .id
= V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE
,
183 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
184 .name
= "Slice Interface Enable",
191 .id
= V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
,
192 .type
= V4L2_CTRL_TYPE_INTEGER
,
193 .name
= "Minimum number of cap bufs",
202 #define NUM_CTRLS ARRAY_SIZE(controls)
204 /* Check whether a context should be run on hardware */
205 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx
*ctx
)
207 /* Context is to parse header */
208 if (ctx
->src_queue_cnt
>= 1 && ctx
->state
== MFCINST_GOT_INST
)
210 /* Context is to decode a frame */
211 if (ctx
->src_queue_cnt
>= 1 &&
212 ctx
->state
== MFCINST_RUNNING
&&
213 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
215 /* Context is to return last frame */
216 if (ctx
->state
== MFCINST_FINISHING
&&
217 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
219 /* Context is to set buffers */
220 if (ctx
->src_queue_cnt
>= 1 &&
221 ctx
->state
== MFCINST_HEAD_PARSED
&&
222 ctx
->capture_state
== QUEUE_BUFS_MMAPED
)
224 /* Resolution change */
225 if ((ctx
->state
== MFCINST_RES_CHANGE_INIT
||
226 ctx
->state
== MFCINST_RES_CHANGE_FLUSH
) &&
227 ctx
->dst_queue_cnt
>= ctx
->pb_count
)
229 if (ctx
->state
== MFCINST_RES_CHANGE_END
&&
230 ctx
->src_queue_cnt
>= 1)
232 mfc_debug(2, "ctx is not ready\n");
236 static struct s5p_mfc_codec_ops decoder_codec_ops
= {
237 .pre_seq_start
= NULL
,
238 .post_seq_start
= NULL
,
239 .pre_frame_start
= NULL
,
240 .post_frame_start
= NULL
,
243 /* Query capabilities of the device */
244 static int vidioc_querycap(struct file
*file
, void *priv
,
245 struct v4l2_capability
*cap
)
247 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
249 strncpy(cap
->driver
, dev
->plat_dev
->name
, sizeof(cap
->driver
) - 1);
250 strncpy(cap
->card
, dev
->plat_dev
->name
, sizeof(cap
->card
) - 1);
251 cap
->bus_info
[0] = 0;
252 cap
->version
= KERNEL_VERSION(1, 0, 0);
254 * This is only a mem-to-mem video device. The capture and output
255 * device capability flags are left only for backward compatibility
256 * and are scheduled for removal.
258 cap
->capabilities
= V4L2_CAP_VIDEO_M2M_MPLANE
| V4L2_CAP_STREAMING
|
259 V4L2_CAP_VIDEO_CAPTURE_MPLANE
|
260 V4L2_CAP_VIDEO_OUTPUT_MPLANE
;
264 /* Enumerate format */
265 static int vidioc_enum_fmt(struct v4l2_fmtdesc
*f
, bool mplane
, bool out
)
267 struct s5p_mfc_fmt
*fmt
;
270 for (i
= 0; i
< ARRAY_SIZE(formats
); ++i
) {
271 if (mplane
&& formats
[i
].num_planes
== 1)
273 else if (!mplane
&& formats
[i
].num_planes
> 1)
275 if (out
&& formats
[i
].type
!= MFC_FMT_DEC
)
277 else if (!out
&& formats
[i
].type
!= MFC_FMT_RAW
)
284 if (i
== ARRAY_SIZE(formats
))
287 strlcpy(f
->description
, fmt
->name
, sizeof(f
->description
));
288 f
->pixelformat
= fmt
->fourcc
;
292 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *pirv
,
293 struct v4l2_fmtdesc
*f
)
295 return vidioc_enum_fmt(f
, false, false);
298 static int vidioc_enum_fmt_vid_cap_mplane(struct file
*file
, void *pirv
,
299 struct v4l2_fmtdesc
*f
)
301 return vidioc_enum_fmt(f
, true, false);
304 static int vidioc_enum_fmt_vid_out(struct file
*file
, void *prov
,
305 struct v4l2_fmtdesc
*f
)
307 return vidioc_enum_fmt(f
, false, true);
310 static int vidioc_enum_fmt_vid_out_mplane(struct file
*file
, void *prov
,
311 struct v4l2_fmtdesc
*f
)
313 return vidioc_enum_fmt(f
, true, true);
317 static int vidioc_g_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
319 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
320 struct v4l2_pix_format_mplane
*pix_mp
;
323 pix_mp
= &f
->fmt
.pix_mp
;
324 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
&&
325 (ctx
->state
== MFCINST_GOT_INST
|| ctx
->state
==
326 MFCINST_RES_CHANGE_END
)) {
327 /* If the MFC is parsing the header,
328 * so wait until it is finished */
329 s5p_mfc_clean_ctx_int_flags(ctx
);
330 s5p_mfc_wait_for_done_ctx(ctx
, S5P_MFC_R2H_CMD_SEQ_DONE_RET
,
333 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
&&
334 ctx
->state
>= MFCINST_HEAD_PARSED
&&
335 ctx
->state
< MFCINST_ABORT
) {
336 /* This is run on CAPTURE (decode output) */
337 /* Width and height are set to the dimensions
338 of the movie, the buffer is bigger and
339 further processing stages should crop to this
341 pix_mp
->width
= ctx
->buf_width
;
342 pix_mp
->height
= ctx
->buf_height
;
343 pix_mp
->field
= V4L2_FIELD_NONE
;
344 pix_mp
->num_planes
= 2;
345 /* Set pixelformat to the format in which MFC
346 outputs the decoded frame */
347 pix_mp
->pixelformat
= ctx
->dst_fmt
->fourcc
;
348 pix_mp
->plane_fmt
[0].bytesperline
= ctx
->buf_width
;
349 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->luma_size
;
350 pix_mp
->plane_fmt
[1].bytesperline
= ctx
->buf_width
;
351 pix_mp
->plane_fmt
[1].sizeimage
= ctx
->chroma_size
;
352 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
353 /* This is run on OUTPUT
354 The buffer contains compressed image
355 so width and height have no meaning */
358 pix_mp
->field
= V4L2_FIELD_NONE
;
359 pix_mp
->plane_fmt
[0].bytesperline
= ctx
->dec_src_buf_size
;
360 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->dec_src_buf_size
;
361 pix_mp
->pixelformat
= ctx
->src_fmt
->fourcc
;
362 pix_mp
->num_planes
= ctx
->src_fmt
->num_planes
;
364 mfc_err("Format could not be read\n");
365 mfc_debug(2, "%s-- with error\n", __func__
);
373 static int vidioc_try_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
375 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
376 struct s5p_mfc_fmt
*fmt
;
378 mfc_debug(2, "Type is %d\n", f
->type
);
379 if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
380 fmt
= find_format(f
, MFC_FMT_DEC
);
382 mfc_err("Unsupported format for source.\n");
385 if (fmt
->codec_mode
== S5P_FIMV_CODEC_NONE
) {
386 mfc_err("Unknown codec\n");
389 if (!IS_MFCV6_PLUS(dev
)) {
390 if (fmt
->fourcc
== V4L2_PIX_FMT_VP8
) {
391 mfc_err("Not supported format.\n");
395 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
396 fmt
= find_format(f
, MFC_FMT_RAW
);
398 mfc_err("Unsupported format for destination.\n");
401 if (IS_MFCV6_PLUS(dev
) &&
402 (fmt
->fourcc
== V4L2_PIX_FMT_NV12MT
)) {
403 mfc_err("Not supported format.\n");
405 } else if (!IS_MFCV6_PLUS(dev
) &&
406 (fmt
->fourcc
!= V4L2_PIX_FMT_NV12MT
)) {
407 mfc_err("Not supported format.\n");
416 static int vidioc_s_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
418 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
419 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
421 struct v4l2_pix_format_mplane
*pix_mp
;
424 ret
= vidioc_try_fmt(file
, priv
, f
);
425 pix_mp
= &f
->fmt
.pix_mp
;
428 if (ctx
->vq_src
.streaming
|| ctx
->vq_dst
.streaming
) {
429 v4l2_err(&dev
->v4l2_dev
, "%s queue busy\n", __func__
);
433 if (f
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
434 /* dst_fmt is validated by call to vidioc_try_fmt */
435 ctx
->dst_fmt
= find_format(f
, MFC_FMT_RAW
);
438 } else if (f
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
439 /* src_fmt is validated by call to vidioc_try_fmt */
440 ctx
->src_fmt
= find_format(f
, MFC_FMT_DEC
);
441 ctx
->codec_mode
= ctx
->src_fmt
->codec_mode
;
442 mfc_debug(2, "The codec number is: %d\n", ctx
->codec_mode
);
445 if (pix_mp
->plane_fmt
[0].sizeimage
)
446 ctx
->dec_src_buf_size
= pix_mp
->plane_fmt
[0].sizeimage
;
448 pix_mp
->plane_fmt
[0].sizeimage
= ctx
->dec_src_buf_size
=
450 pix_mp
->plane_fmt
[0].bytesperline
= 0;
451 ctx
->state
= MFCINST_INIT
;
455 mfc_err("Wrong type error for S_FMT : %d", f
->type
);
465 /* Reqeust buffers */
466 static int vidioc_reqbufs(struct file
*file
, void *priv
,
467 struct v4l2_requestbuffers
*reqbufs
)
469 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
470 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
473 if (reqbufs
->memory
!= V4L2_MEMORY_MMAP
) {
474 mfc_err("Only V4L2_MEMORY_MAP is supported\n");
477 if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
478 /* Can only request buffers after an instance has been opened.*/
479 if (ctx
->state
== MFCINST_INIT
) {
480 ctx
->src_bufs_cnt
= 0;
481 if (reqbufs
->count
== 0) {
482 mfc_debug(2, "Freeing buffers\n");
484 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
489 if (ctx
->output_state
!= QUEUE_FREE
) {
490 mfc_err("Bufs have already been requested\n");
494 ret
= vb2_reqbufs(&ctx
->vq_src
, reqbufs
);
497 mfc_err("vb2_reqbufs on output failed\n");
500 mfc_debug(2, "vb2_reqbufs: %d\n", ret
);
501 ctx
->output_state
= QUEUE_BUFS_REQUESTED
;
503 } else if (reqbufs
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
504 ctx
->dst_bufs_cnt
= 0;
505 if (reqbufs
->count
== 0) {
506 mfc_debug(2, "Freeing buffers\n");
508 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
512 if (ctx
->capture_state
!= QUEUE_FREE
) {
513 mfc_err("Bufs have already been requested\n");
516 ctx
->capture_state
= QUEUE_BUFS_REQUESTED
;
518 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
521 mfc_err("vb2_reqbufs on capture failed\n");
524 if (reqbufs
->count
< ctx
->pb_count
) {
525 mfc_err("Not enough buffers allocated\n");
528 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
532 ctx
->total_dpb_count
= reqbufs
->count
;
533 ret
= s5p_mfc_hw_call(dev
->mfc_ops
, alloc_codec_buffers
, ctx
);
535 mfc_err("Failed to allocate decoding buffers\n");
538 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
542 if (ctx
->dst_bufs_cnt
== ctx
->total_dpb_count
) {
543 ctx
->capture_state
= QUEUE_BUFS_MMAPED
;
545 mfc_err("Not all buffers passed to buf_init\n");
548 ret
= vb2_reqbufs(&ctx
->vq_dst
, reqbufs
);
549 s5p_mfc_hw_call(dev
->mfc_ops
, release_codec_buffers
,
554 if (s5p_mfc_ctx_ready(ctx
))
555 set_work_bit_irqsave(ctx
);
556 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
557 s5p_mfc_wait_for_done_ctx(ctx
,
558 S5P_MFC_R2H_CMD_INIT_BUFFERS_RET
, 0);
564 static int vidioc_querybuf(struct file
*file
, void *priv
,
565 struct v4l2_buffer
*buf
)
567 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
571 if (buf
->memory
!= V4L2_MEMORY_MMAP
) {
572 mfc_err("Only mmaped buffers can be used\n");
575 mfc_debug(2, "State: %d, buf->type: %d\n", ctx
->state
, buf
->type
);
576 if (ctx
->state
== MFCINST_INIT
&&
577 buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
578 ret
= vb2_querybuf(&ctx
->vq_src
, buf
);
579 } else if (ctx
->state
== MFCINST_RUNNING
&&
580 buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
581 ret
= vb2_querybuf(&ctx
->vq_dst
, buf
);
582 for (i
= 0; i
< buf
->length
; i
++)
583 buf
->m
.planes
[i
].m
.mem_offset
+= DST_QUEUE_OFF_BASE
;
585 mfc_err("vidioc_querybuf called in an inappropriate state\n");
593 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
595 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
597 if (ctx
->state
== MFCINST_ERROR
) {
598 mfc_err("Call on QBUF after unrecoverable error\n");
601 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
602 return vb2_qbuf(&ctx
->vq_src
, buf
);
603 else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
604 return vb2_qbuf(&ctx
->vq_dst
, buf
);
608 /* Dequeue a buffer */
609 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*buf
)
611 const struct v4l2_event ev
= {
612 .type
= V4L2_EVENT_EOS
614 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
617 if (ctx
->state
== MFCINST_ERROR
) {
618 mfc_err("Call on DQBUF after unrecoverable error\n");
621 if (buf
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
622 ret
= vb2_dqbuf(&ctx
->vq_src
, buf
, file
->f_flags
& O_NONBLOCK
);
623 else if (buf
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
624 ret
= vb2_dqbuf(&ctx
->vq_dst
, buf
, file
->f_flags
& O_NONBLOCK
);
625 if (ret
== 0 && ctx
->state
== MFCINST_FINISHED
&&
626 list_empty(&ctx
->vq_dst
.done_list
))
627 v4l2_event_queue_fh(&ctx
->fh
, &ev
);
634 /* Export DMA buffer */
635 static int vidioc_expbuf(struct file
*file
, void *priv
,
636 struct v4l2_exportbuffer
*eb
)
638 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
640 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
641 return vb2_expbuf(&ctx
->vq_src
, eb
);
642 if (eb
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
643 return vb2_expbuf(&ctx
->vq_dst
, eb
);
648 static int vidioc_streamon(struct file
*file
, void *priv
,
649 enum v4l2_buf_type type
)
651 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
652 struct s5p_mfc_dev
*dev
= ctx
->dev
;
656 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
658 if (ctx
->state
== MFCINST_INIT
) {
659 ctx
->dst_bufs_cnt
= 0;
660 ctx
->src_bufs_cnt
= 0;
661 ctx
->capture_state
= QUEUE_FREE
;
662 ctx
->output_state
= QUEUE_FREE
;
663 s5p_mfc_hw_call(dev
->mfc_ops
, alloc_instance_buffer
,
665 s5p_mfc_hw_call(dev
->mfc_ops
, alloc_dec_temp_buffers
,
667 set_work_bit_irqsave(ctx
);
668 s5p_mfc_clean_ctx_int_flags(ctx
);
669 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
671 if (s5p_mfc_wait_for_done_ctx(ctx
,
672 S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET
, 0)) {
673 /* Error or timeout */
674 mfc_err("Error getting instance from hardware\n");
675 s5p_mfc_hw_call(dev
->mfc_ops
,
676 release_instance_buffer
, ctx
);
677 s5p_mfc_hw_call(dev
->mfc_ops
,
678 release_dec_desc_buffer
, ctx
);
681 mfc_debug(2, "Got instance number: %d\n", ctx
->inst_no
);
683 ret
= vb2_streamon(&ctx
->vq_src
, type
);
685 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
686 ret
= vb2_streamon(&ctx
->vq_dst
, type
);
691 /* Stream off, which equals to a pause */
692 static int vidioc_streamoff(struct file
*file
, void *priv
,
693 enum v4l2_buf_type type
)
695 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
697 if (type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
)
698 return vb2_streamoff(&ctx
->vq_src
, type
);
699 else if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
)
700 return vb2_streamoff(&ctx
->vq_dst
, type
);
704 /* Set controls - v4l2 control framework */
705 static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl
*ctrl
)
707 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
710 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY
:
711 ctx
->display_delay
= ctrl
->val
;
713 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE
:
714 ctx
->display_delay_enable
= ctrl
->val
;
716 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER
:
717 ctx
->loop_filter_mpeg4
= ctrl
->val
;
719 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE
:
720 ctx
->slice_interface
= ctrl
->val
;
723 mfc_err("Invalid control 0x%08x\n", ctrl
->id
);
729 static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl
*ctrl
)
731 struct s5p_mfc_ctx
*ctx
= ctrl_to_ctx(ctrl
);
732 struct s5p_mfc_dev
*dev
= ctx
->dev
;
735 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE
:
736 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
737 ctx
->state
< MFCINST_ABORT
) {
738 ctrl
->val
= ctx
->pb_count
;
740 } else if (ctx
->state
!= MFCINST_INIT
) {
741 v4l2_err(&dev
->v4l2_dev
, "Decoding not initialised\n");
744 /* Should wait for the header to be parsed */
745 s5p_mfc_clean_ctx_int_flags(ctx
);
746 s5p_mfc_wait_for_done_ctx(ctx
,
747 S5P_MFC_R2H_CMD_SEQ_DONE_RET
, 0);
748 if (ctx
->state
>= MFCINST_HEAD_PARSED
&&
749 ctx
->state
< MFCINST_ABORT
) {
750 ctrl
->val
= ctx
->pb_count
;
752 v4l2_err(&dev
->v4l2_dev
, "Decoding not initialised\n");
761 static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops
= {
762 .s_ctrl
= s5p_mfc_dec_s_ctrl
,
763 .g_volatile_ctrl
= s5p_mfc_dec_g_v_ctrl
,
766 /* Get cropping information */
767 static int vidioc_g_crop(struct file
*file
, void *priv
,
768 struct v4l2_crop
*cr
)
770 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
771 struct s5p_mfc_dev
*dev
= ctx
->dev
;
772 u32 left
, right
, top
, bottom
;
774 if (ctx
->state
!= MFCINST_HEAD_PARSED
&&
775 ctx
->state
!= MFCINST_RUNNING
&& ctx
->state
!= MFCINST_FINISHING
776 && ctx
->state
!= MFCINST_FINISHED
) {
777 mfc_err("Cannont set crop\n");
780 if (ctx
->src_fmt
->fourcc
== V4L2_PIX_FMT_H264
) {
781 left
= s5p_mfc_hw_call(dev
->mfc_ops
, get_crop_info_h
, ctx
);
782 right
= left
>> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT
;
783 left
= left
& S5P_FIMV_SHARED_CROP_LEFT_MASK
;
784 top
= s5p_mfc_hw_call(dev
->mfc_ops
, get_crop_info_v
, ctx
);
785 bottom
= top
>> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT
;
786 top
= top
& S5P_FIMV_SHARED_CROP_TOP_MASK
;
789 cr
->c
.width
= ctx
->img_width
- left
- right
;
790 cr
->c
.height
= ctx
->img_height
- top
- bottom
;
791 mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
792 "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left
, top
,
793 cr
->c
.width
, cr
->c
.height
, right
, bottom
,
794 ctx
->buf_width
, ctx
->buf_height
);
798 cr
->c
.width
= ctx
->img_width
;
799 cr
->c
.height
= ctx
->img_height
;
800 mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
801 "fh=%d\n", cr
->c
.width
, cr
->c
.height
, ctx
->buf_width
,
807 int vidioc_decoder_cmd(struct file
*file
, void *priv
,
808 struct v4l2_decoder_cmd
*cmd
)
810 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(priv
);
811 struct s5p_mfc_dev
*dev
= ctx
->dev
;
812 struct s5p_mfc_buf
*buf
;
816 case V4L2_ENC_CMD_STOP
:
820 if (!ctx
->vq_src
.streaming
)
823 spin_lock_irqsave(&dev
->irqlock
, flags
);
824 if (list_empty(&ctx
->src_queue
)) {
825 mfc_err("EOS: empty src queue, entering finishing state");
826 ctx
->state
= MFCINST_FINISHING
;
827 if (s5p_mfc_ctx_ready(ctx
))
828 set_work_bit_irqsave(ctx
);
829 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
830 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
832 mfc_err("EOS: marking last buffer of stream");
833 buf
= list_entry(ctx
->src_queue
.prev
,
834 struct s5p_mfc_buf
, list
);
835 if (buf
->flags
& MFC_BUF_FLAG_USED
)
836 ctx
->state
= MFCINST_FINISHING
;
838 buf
->flags
|= MFC_BUF_FLAG_EOS
;
839 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
848 static int vidioc_subscribe_event(struct v4l2_fh
*fh
,
849 const struct v4l2_event_subscription
*sub
)
853 return v4l2_event_subscribe(fh
, sub
, 2, NULL
);
861 static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops
= {
862 .vidioc_querycap
= vidioc_querycap
,
863 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
864 .vidioc_enum_fmt_vid_cap_mplane
= vidioc_enum_fmt_vid_cap_mplane
,
865 .vidioc_enum_fmt_vid_out
= vidioc_enum_fmt_vid_out
,
866 .vidioc_enum_fmt_vid_out_mplane
= vidioc_enum_fmt_vid_out_mplane
,
867 .vidioc_g_fmt_vid_cap_mplane
= vidioc_g_fmt
,
868 .vidioc_g_fmt_vid_out_mplane
= vidioc_g_fmt
,
869 .vidioc_try_fmt_vid_cap_mplane
= vidioc_try_fmt
,
870 .vidioc_try_fmt_vid_out_mplane
= vidioc_try_fmt
,
871 .vidioc_s_fmt_vid_cap_mplane
= vidioc_s_fmt
,
872 .vidioc_s_fmt_vid_out_mplane
= vidioc_s_fmt
,
873 .vidioc_reqbufs
= vidioc_reqbufs
,
874 .vidioc_querybuf
= vidioc_querybuf
,
875 .vidioc_qbuf
= vidioc_qbuf
,
876 .vidioc_dqbuf
= vidioc_dqbuf
,
877 .vidioc_expbuf
= vidioc_expbuf
,
878 .vidioc_streamon
= vidioc_streamon
,
879 .vidioc_streamoff
= vidioc_streamoff
,
880 .vidioc_g_crop
= vidioc_g_crop
,
881 .vidioc_decoder_cmd
= vidioc_decoder_cmd
,
882 .vidioc_subscribe_event
= vidioc_subscribe_event
,
883 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
886 static int s5p_mfc_queue_setup(struct vb2_queue
*vq
,
887 const struct v4l2_format
*fmt
, unsigned int *buf_count
,
888 unsigned int *plane_count
, unsigned int psize
[],
891 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
892 struct s5p_mfc_dev
*dev
= ctx
->dev
;
894 /* Video output for decoding (source)
895 * this can be set after getting an instance */
896 if (ctx
->state
== MFCINST_INIT
&&
897 vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
898 /* A single plane is required for input */
902 if (*buf_count
> MFC_MAX_BUFFERS
)
903 *buf_count
= MFC_MAX_BUFFERS
;
904 /* Video capture for decoding (destination)
905 * this can be set after the header was parsed */
906 } else if (ctx
->state
== MFCINST_HEAD_PARSED
&&
907 vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
908 /* Output plane count is 2 - one for Y and one for CbCr */
910 /* Setup buffer count */
911 if (*buf_count
< ctx
->pb_count
)
912 *buf_count
= ctx
->pb_count
;
913 if (*buf_count
> ctx
->pb_count
+ MFC_MAX_EXTRA_DPB
)
914 *buf_count
= ctx
->pb_count
+ MFC_MAX_EXTRA_DPB
;
915 if (*buf_count
> MFC_MAX_BUFFERS
)
916 *buf_count
= MFC_MAX_BUFFERS
;
918 mfc_err("State seems invalid. State = %d, vq->type = %d\n",
919 ctx
->state
, vq
->type
);
922 mfc_debug(2, "Buffer count=%d, plane count=%d\n",
923 *buf_count
, *plane_count
);
924 if (ctx
->state
== MFCINST_HEAD_PARSED
&&
925 vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
926 psize
[0] = ctx
->luma_size
;
927 psize
[1] = ctx
->chroma_size
;
929 if (IS_MFCV6_PLUS(dev
))
931 ctx
->dev
->alloc_ctx
[MFC_BANK1_ALLOC_CTX
];
934 ctx
->dev
->alloc_ctx
[MFC_BANK2_ALLOC_CTX
];
935 allocators
[1] = ctx
->dev
->alloc_ctx
[MFC_BANK1_ALLOC_CTX
];
936 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
&&
937 ctx
->state
== MFCINST_INIT
) {
938 psize
[0] = ctx
->dec_src_buf_size
;
939 allocators
[0] = ctx
->dev
->alloc_ctx
[MFC_BANK1_ALLOC_CTX
];
941 mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
947 static void s5p_mfc_unlock(struct vb2_queue
*q
)
949 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
950 struct s5p_mfc_dev
*dev
= ctx
->dev
;
952 mutex_unlock(&dev
->mfc_mutex
);
955 static void s5p_mfc_lock(struct vb2_queue
*q
)
957 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
958 struct s5p_mfc_dev
*dev
= ctx
->dev
;
960 mutex_lock(&dev
->mfc_mutex
);
963 static int s5p_mfc_buf_init(struct vb2_buffer
*vb
)
965 struct vb2_queue
*vq
= vb
->vb2_queue
;
966 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
969 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
970 if (ctx
->capture_state
== QUEUE_BUFS_MMAPED
)
972 for (i
= 0; i
<= ctx
->src_fmt
->num_planes
; i
++) {
973 if (IS_ERR_OR_NULL(ERR_PTR(
974 vb2_dma_contig_plane_dma_addr(vb
, i
)))) {
975 mfc_err("Plane mem not allocated\n");
979 if (vb2_plane_size(vb
, 0) < ctx
->luma_size
||
980 vb2_plane_size(vb
, 1) < ctx
->chroma_size
) {
981 mfc_err("Plane buffer (CAPTURE) is too small\n");
984 i
= vb
->v4l2_buf
.index
;
985 ctx
->dst_bufs
[i
].b
= vb
;
986 ctx
->dst_bufs
[i
].cookie
.raw
.luma
=
987 vb2_dma_contig_plane_dma_addr(vb
, 0);
988 ctx
->dst_bufs
[i
].cookie
.raw
.chroma
=
989 vb2_dma_contig_plane_dma_addr(vb
, 1);
991 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
992 if (IS_ERR_OR_NULL(ERR_PTR(
993 vb2_dma_contig_plane_dma_addr(vb
, 0)))) {
994 mfc_err("Plane memory not allocated\n");
997 if (vb2_plane_size(vb
, 0) < ctx
->dec_src_buf_size
) {
998 mfc_err("Plane buffer (OUTPUT) is too small\n");
1002 i
= vb
->v4l2_buf
.index
;
1003 ctx
->src_bufs
[i
].b
= vb
;
1004 ctx
->src_bufs
[i
].cookie
.stream
=
1005 vb2_dma_contig_plane_dma_addr(vb
, 0);
1006 ctx
->src_bufs_cnt
++;
1008 mfc_err("s5p_mfc_buf_init: unknown queue type\n");
1014 static int s5p_mfc_start_streaming(struct vb2_queue
*q
, unsigned int count
)
1016 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
1017 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1019 v4l2_ctrl_handler_setup(&ctx
->ctrl_handler
);
1020 if (ctx
->state
== MFCINST_FINISHING
||
1021 ctx
->state
== MFCINST_FINISHED
)
1022 ctx
->state
= MFCINST_RUNNING
;
1023 /* If context is ready then dev = work->data;schedule it to run */
1024 if (s5p_mfc_ctx_ready(ctx
))
1025 set_work_bit_irqsave(ctx
);
1026 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
1030 static int s5p_mfc_stop_streaming(struct vb2_queue
*q
)
1032 unsigned long flags
;
1033 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(q
->drv_priv
);
1034 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1037 if ((ctx
->state
== MFCINST_FINISHING
||
1038 ctx
->state
== MFCINST_RUNNING
) &&
1039 dev
->curr_ctx
== ctx
->num
&& dev
->hw_lock
) {
1040 ctx
->state
= MFCINST_ABORT
;
1041 s5p_mfc_wait_for_done_ctx(ctx
,
1042 S5P_MFC_R2H_CMD_FRAME_DONE_RET
, 0);
1045 if (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1046 spin_lock_irqsave(&dev
->irqlock
, flags
);
1047 s5p_mfc_hw_call(dev
->mfc_ops
, cleanup_queue
, &ctx
->dst_queue
,
1049 INIT_LIST_HEAD(&ctx
->dst_queue
);
1050 ctx
->dst_queue_cnt
= 0;
1051 ctx
->dpb_flush_flag
= 1;
1052 ctx
->dec_dst_flag
= 0;
1053 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1054 if (IS_MFCV6_PLUS(dev
) && (ctx
->state
== MFCINST_RUNNING
)) {
1055 ctx
->state
= MFCINST_FLUSH
;
1056 set_work_bit_irqsave(ctx
);
1057 s5p_mfc_clean_ctx_int_flags(ctx
);
1058 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
1059 if (s5p_mfc_wait_for_done_ctx(ctx
,
1060 S5P_MFC_R2H_CMD_DPB_FLUSH_RET
, 0))
1061 mfc_err("Err flushing buffers\n");
1064 if (q
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1065 spin_lock_irqsave(&dev
->irqlock
, flags
);
1066 s5p_mfc_hw_call(dev
->mfc_ops
, cleanup_queue
, &ctx
->src_queue
,
1068 INIT_LIST_HEAD(&ctx
->src_queue
);
1069 ctx
->src_queue_cnt
= 0;
1070 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1073 ctx
->state
= MFCINST_RUNNING
;
1078 static void s5p_mfc_buf_queue(struct vb2_buffer
*vb
)
1080 struct vb2_queue
*vq
= vb
->vb2_queue
;
1081 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(vq
->drv_priv
);
1082 struct s5p_mfc_dev
*dev
= ctx
->dev
;
1083 unsigned long flags
;
1084 struct s5p_mfc_buf
*mfc_buf
;
1086 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
) {
1087 mfc_buf
= &ctx
->src_bufs
[vb
->v4l2_buf
.index
];
1088 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
1089 spin_lock_irqsave(&dev
->irqlock
, flags
);
1090 list_add_tail(&mfc_buf
->list
, &ctx
->src_queue
);
1091 ctx
->src_queue_cnt
++;
1092 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1093 } else if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
) {
1094 mfc_buf
= &ctx
->dst_bufs
[vb
->v4l2_buf
.index
];
1095 mfc_buf
->flags
&= ~MFC_BUF_FLAG_USED
;
1096 /* Mark destination as available for use by MFC */
1097 spin_lock_irqsave(&dev
->irqlock
, flags
);
1098 set_bit(vb
->v4l2_buf
.index
, &ctx
->dec_dst_flag
);
1099 list_add_tail(&mfc_buf
->list
, &ctx
->dst_queue
);
1100 ctx
->dst_queue_cnt
++;
1101 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
1103 mfc_err("Unsupported buffer type (%d)\n", vq
->type
);
1105 if (s5p_mfc_ctx_ready(ctx
))
1106 set_work_bit_irqsave(ctx
);
1107 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
1110 static struct vb2_ops s5p_mfc_dec_qops
= {
1111 .queue_setup
= s5p_mfc_queue_setup
,
1112 .wait_prepare
= s5p_mfc_unlock
,
1113 .wait_finish
= s5p_mfc_lock
,
1114 .buf_init
= s5p_mfc_buf_init
,
1115 .start_streaming
= s5p_mfc_start_streaming
,
1116 .stop_streaming
= s5p_mfc_stop_streaming
,
1117 .buf_queue
= s5p_mfc_buf_queue
,
1120 struct s5p_mfc_codec_ops
*get_dec_codec_ops(void)
1122 return &decoder_codec_ops
;
1125 struct vb2_ops
*get_dec_queue_ops(void)
1127 return &s5p_mfc_dec_qops
;
1130 const struct v4l2_ioctl_ops
*get_dec_v4l2_ioctl_ops(void)
1132 return &s5p_mfc_dec_ioctl_ops
;
1135 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1136 && V4L2_CTRL_DRIVER_PRIV(x))
1138 int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx
*ctx
)
1140 struct v4l2_ctrl_config cfg
;
1143 v4l2_ctrl_handler_init(&ctx
->ctrl_handler
, NUM_CTRLS
);
1144 if (ctx
->ctrl_handler
.error
) {
1145 mfc_err("v4l2_ctrl_handler_init failed\n");
1146 return ctx
->ctrl_handler
.error
;
1149 for (i
= 0; i
< NUM_CTRLS
; i
++) {
1150 if (IS_MFC51_PRIV(controls
[i
].id
)) {
1151 memset(&cfg
, 0, sizeof(struct v4l2_ctrl_config
));
1152 cfg
.ops
= &s5p_mfc_dec_ctrl_ops
;
1153 cfg
.id
= controls
[i
].id
;
1154 cfg
.min
= controls
[i
].minimum
;
1155 cfg
.max
= controls
[i
].maximum
;
1156 cfg
.def
= controls
[i
].default_value
;
1157 cfg
.name
= controls
[i
].name
;
1158 cfg
.type
= controls
[i
].type
;
1160 cfg
.step
= controls
[i
].step
;
1161 cfg
.menu_skip_mask
= 0;
1163 ctx
->ctrls
[i
] = v4l2_ctrl_new_custom(&ctx
->ctrl_handler
,
1166 ctx
->ctrls
[i
] = v4l2_ctrl_new_std(&ctx
->ctrl_handler
,
1167 &s5p_mfc_dec_ctrl_ops
,
1168 controls
[i
].id
, controls
[i
].minimum
,
1169 controls
[i
].maximum
, controls
[i
].step
,
1170 controls
[i
].default_value
);
1172 if (ctx
->ctrl_handler
.error
) {
1173 mfc_err("Adding control (%d) failed\n", i
);
1174 return ctx
->ctrl_handler
.error
;
1176 if (controls
[i
].is_volatile
&& ctx
->ctrls
[i
])
1177 ctx
->ctrls
[i
]->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
1182 void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx
*ctx
)
1186 v4l2_ctrl_handler_free(&ctx
->ctrl_handler
);
1187 for (i
= 0; i
< NUM_CTRLS
; i
++)
1188 ctx
->ctrls
[i
] = NULL
;
1191 void s5p_mfc_dec_init(struct s5p_mfc_ctx
*ctx
)
1193 struct v4l2_format f
;
1194 f
.fmt
.pix_mp
.pixelformat
= DEF_SRC_FMT_DEC
;
1195 ctx
->src_fmt
= find_format(&f
, MFC_FMT_DEC
);
1196 f
.fmt
.pix_mp
.pixelformat
= DEF_DST_FMT_DEC
;
1197 ctx
->dst_fmt
= find_format(&f
, MFC_FMT_RAW
);
1198 mfc_debug(2, "Default src_fmt is %x, dest_fmt is %x\n",
1199 (unsigned int)ctx
->src_fmt
, (unsigned int)ctx
->dst_fmt
);