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/videobuf2-core.h>
26 #include "s5p_mfc_common.h"
27 #include "s5p_mfc_debug.h"
28 #include "s5p_mfc_dec.h"
29 #include "s5p_mfc_intr.h"
30 #include "s5p_mfc_opr.h"
31 #include "s5p_mfc_pm.h"
33 #define DEF_SRC_FMT_DEC V4L2_PIX_FMT_H264
34 #define DEF_DST_FMT_DEC V4L2_PIX_FMT_NV12MT_16X16
36 static struct s5p_mfc_fmt formats[] = {
38 .name = "4:2:0 2 Planes 16x16 Tiles",
39 .fourcc = V4L2_PIX_FMT_NV12MT_16X16,
40 .codec_mode = S5P_MFC_CODEC_NONE,
45 .name = "4:2:0 2 Planes 64x32 Tiles",
46 .fourcc = V4L2_PIX_FMT_NV12MT,
47 .codec_mode = S5P_MFC_CODEC_NONE,
52 .name = "4:2:0 2 Planes Y/CbCr",
53 .fourcc = V4L2_PIX_FMT_NV12M,
54 .codec_mode = S5P_MFC_CODEC_NONE,
59 .name = "4:2:0 2 Planes Y/CrCb",
60 .fourcc = V4L2_PIX_FMT_NV21M,
61 .codec_mode = S5P_MFC_CODEC_NONE,
66 .name = "H264 Encoded Stream",
67 .fourcc = V4L2_PIX_FMT_H264,
68 .codec_mode = S5P_MFC_CODEC_H264_DEC,
73 .name = "H264/MVC Encoded Stream",
74 .fourcc = V4L2_PIX_FMT_H264_MVC,
75 .codec_mode = S5P_MFC_CODEC_H264_MVC_DEC,
80 .name = "H263 Encoded Stream",
81 .fourcc = V4L2_PIX_FMT_H263,
82 .codec_mode = S5P_MFC_CODEC_H263_DEC,
87 .name = "MPEG1 Encoded Stream",
88 .fourcc = V4L2_PIX_FMT_MPEG1,
89 .codec_mode = S5P_MFC_CODEC_MPEG2_DEC,
94 .name = "MPEG2 Encoded Stream",
95 .fourcc = V4L2_PIX_FMT_MPEG2,
96 .codec_mode = S5P_MFC_CODEC_MPEG2_DEC,
101 .name = "MPEG4 Encoded Stream",
102 .fourcc = V4L2_PIX_FMT_MPEG4,
103 .codec_mode = S5P_MFC_CODEC_MPEG4_DEC,
108 .name = "XviD Encoded Stream",
109 .fourcc = V4L2_PIX_FMT_XVID,
110 .codec_mode = S5P_MFC_CODEC_MPEG4_DEC,
115 .name = "VC1 Encoded Stream",
116 .fourcc = V4L2_PIX_FMT_VC1_ANNEX_G,
117 .codec_mode = S5P_MFC_CODEC_VC1_DEC,
122 .name = "VC1 RCV Encoded Stream",
123 .fourcc = V4L2_PIX_FMT_VC1_ANNEX_L,
124 .codec_mode = S5P_MFC_CODEC_VC1RCV_DEC,
129 .name = "VP8 Encoded Stream",
130 .fourcc = V4L2_PIX_FMT_VP8,
131 .codec_mode = S5P_MFC_CODEC_VP8_DEC,
137 #define NUM_FORMATS ARRAY_SIZE(formats)
139 /* Find selected format description */
140 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
144 for (i = 0; i < NUM_FORMATS; i++) {
145 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
146 formats[i].type == t)
152 static struct mfc_control controls[] = {
154 .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
155 .type = V4L2_CTRL_TYPE_INTEGER,
156 .name = "H264 Display Delay",
163 .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
164 .type = V4L2_CTRL_TYPE_BOOLEAN,
165 .name = "H264 Display Delay Enable",
172 .id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
173 .type = V4L2_CTRL_TYPE_BOOLEAN,
174 .name = "Mpeg4 Loop Filter Enable",
181 .id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
182 .type = V4L2_CTRL_TYPE_BOOLEAN,
183 .name = "Slice Interface Enable",
190 .id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
191 .type = V4L2_CTRL_TYPE_INTEGER,
192 .name = "Minimum number of cap bufs",
201 #define NUM_CTRLS ARRAY_SIZE(controls)
203 /* Check whether a context should be run on hardware */
204 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
206 /* Context is to parse header */
207 if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
209 /* Context is to decode a frame */
210 if (ctx->src_queue_cnt >= 1 &&
211 ctx->state == MFCINST_RUNNING &&
212 ctx->dst_queue_cnt >= ctx->dpb_count)
214 /* Context is to return last frame */
215 if (ctx->state == MFCINST_FINISHING &&
216 ctx->dst_queue_cnt >= ctx->dpb_count)
218 /* Context is to set buffers */
219 if (ctx->src_queue_cnt >= 1 &&
220 ctx->state == MFCINST_HEAD_PARSED &&
221 ctx->capture_state == QUEUE_BUFS_MMAPED)
223 /* Resolution change */
224 if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
225 ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
226 ctx->dst_queue_cnt >= ctx->dpb_count)
228 if (ctx->state == MFCINST_RES_CHANGE_END &&
229 ctx->src_queue_cnt >= 1)
231 mfc_debug(2, "ctx is not ready\n");
235 static struct s5p_mfc_codec_ops decoder_codec_ops = {
236 .pre_seq_start = NULL,
237 .post_seq_start = NULL,
238 .pre_frame_start = NULL,
239 .post_frame_start = NULL,
242 /* Query capabilities of the device */
243 static int vidioc_querycap(struct file *file, void *priv,
244 struct v4l2_capability *cap)
246 struct s5p_mfc_dev *dev = video_drvdata(file);
248 strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
249 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
250 cap->bus_info[0] = 0;
251 cap->version = KERNEL_VERSION(1, 0, 0);
253 * This is only a mem-to-mem video device. The capture and output
254 * device capability flags are left only for backward compatibility
255 * and are scheduled for removal.
257 cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
258 V4L2_CAP_VIDEO_CAPTURE_MPLANE |
259 V4L2_CAP_VIDEO_OUTPUT_MPLANE;
263 /* Enumerate format */
264 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
266 struct s5p_mfc_fmt *fmt;
269 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
270 if (mplane && formats[i].num_planes == 1)
272 else if (!mplane && formats[i].num_planes > 1)
274 if (out && formats[i].type != MFC_FMT_DEC)
276 else if (!out && formats[i].type != MFC_FMT_RAW)
283 if (i == ARRAY_SIZE(formats))
286 strlcpy(f->description, fmt->name, sizeof(f->description));
287 f->pixelformat = fmt->fourcc;
291 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
292 struct v4l2_fmtdesc *f)
294 return vidioc_enum_fmt(f, false, false);
297 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
298 struct v4l2_fmtdesc *f)
300 return vidioc_enum_fmt(f, true, false);
303 static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
304 struct v4l2_fmtdesc *f)
306 return vidioc_enum_fmt(f, false, true);
309 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
310 struct v4l2_fmtdesc *f)
312 return vidioc_enum_fmt(f, true, true);
316 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
318 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
319 struct v4l2_pix_format_mplane *pix_mp;
322 pix_mp = &f->fmt.pix_mp;
323 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
324 (ctx->state == MFCINST_GOT_INST || ctx->state ==
325 MFCINST_RES_CHANGE_END)) {
326 /* If the MFC is parsing the header,
327 * so wait until it is finished */
328 s5p_mfc_clean_ctx_int_flags(ctx);
329 s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_SEQ_DONE_RET,
332 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
333 ctx->state >= MFCINST_HEAD_PARSED &&
334 ctx->state < MFCINST_ABORT) {
335 /* This is run on CAPTURE (decode output) */
336 /* Width and height are set to the dimensions
337 of the movie, the buffer is bigger and
338 further processing stages should crop to this
340 pix_mp->width = ctx->buf_width;
341 pix_mp->height = ctx->buf_height;
342 pix_mp->field = V4L2_FIELD_NONE;
343 pix_mp->num_planes = 2;
344 /* Set pixelformat to the format in which MFC
345 outputs the decoded frame */
346 pix_mp->pixelformat = V4L2_PIX_FMT_NV12MT;
347 pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
348 pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
349 pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
350 pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
351 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
352 /* This is run on OUTPUT
353 The buffer contains compressed image
354 so width and height have no meaning */
357 pix_mp->field = V4L2_FIELD_NONE;
358 pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
359 pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
360 pix_mp->pixelformat = ctx->src_fmt->fourcc;
361 pix_mp->num_planes = ctx->src_fmt->num_planes;
363 mfc_err("Format could not be read\n");
364 mfc_debug(2, "%s-- with error\n", __func__);
372 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
374 struct s5p_mfc_dev *dev = video_drvdata(file);
375 struct s5p_mfc_fmt *fmt;
377 mfc_debug(2, "Type is %d\n", f->type);
378 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
379 fmt = find_format(f, MFC_FMT_DEC);
381 mfc_err("Unsupported format for source.\n");
384 if (!IS_MFCV6(dev) && (fmt->fourcc == V4L2_PIX_FMT_VP8)) {
385 mfc_err("Not supported format.\n");
388 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
389 fmt = find_format(f, MFC_FMT_RAW);
391 mfc_err("Unsupported format for destination.\n");
394 if (IS_MFCV6(dev) && (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
395 mfc_err("Not supported format.\n");
397 } else if (!IS_MFCV6(dev) &&
398 (fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
399 mfc_err("Not supported format.\n");
408 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
410 struct s5p_mfc_dev *dev = video_drvdata(file);
411 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
413 struct s5p_mfc_fmt *fmt;
414 struct v4l2_pix_format_mplane *pix_mp;
417 ret = vidioc_try_fmt(file, priv, f);
418 pix_mp = &f->fmt.pix_mp;
421 if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
422 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
426 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
427 fmt = find_format(f, MFC_FMT_RAW);
429 mfc_err("Unsupported format for source.\n");
432 if (!IS_MFCV6(dev) && (fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
433 mfc_err("Not supported format.\n");
435 } else if (IS_MFCV6(dev) &&
436 (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
437 mfc_err("Not supported format.\n");
443 } else if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
444 mfc_err("Wrong type error for S_FMT : %d", f->type);
447 fmt = find_format(f, MFC_FMT_DEC);
448 if (!fmt || fmt->codec_mode == S5P_MFC_CODEC_NONE) {
449 mfc_err("Unknown codec\n");
453 if (fmt->type != MFC_FMT_DEC) {
454 mfc_err("Wrong format selected, you should choose "
455 "format for decoding\n");
459 if (!IS_MFCV6(dev) && (fmt->fourcc == V4L2_PIX_FMT_VP8)) {
460 mfc_err("Not supported format.\n");
464 ctx->codec_mode = fmt->codec_mode;
465 mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
468 if (pix_mp->plane_fmt[0].sizeimage)
469 ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
471 pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
473 pix_mp->plane_fmt[0].bytesperline = 0;
474 ctx->state = MFCINST_INIT;
480 /* Reqeust buffers */
481 static int vidioc_reqbufs(struct file *file, void *priv,
482 struct v4l2_requestbuffers *reqbufs)
484 struct s5p_mfc_dev *dev = video_drvdata(file);
485 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
488 if (reqbufs->memory != V4L2_MEMORY_MMAP) {
489 mfc_err("Only V4L2_MEMORY_MAP is supported\n");
492 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
493 /* Can only request buffers after an instance has been opened.*/
494 if (ctx->state == MFCINST_INIT) {
495 ctx->src_bufs_cnt = 0;
496 if (reqbufs->count == 0) {
497 mfc_debug(2, "Freeing buffers\n");
499 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
504 if (ctx->output_state != QUEUE_FREE) {
505 mfc_err("Bufs have already been requested\n");
509 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
512 mfc_err("vb2_reqbufs on output failed\n");
515 mfc_debug(2, "vb2_reqbufs: %d\n", ret);
516 ctx->output_state = QUEUE_BUFS_REQUESTED;
518 } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
519 ctx->dst_bufs_cnt = 0;
520 if (reqbufs->count == 0) {
521 mfc_debug(2, "Freeing buffers\n");
523 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
527 if (ctx->capture_state != QUEUE_FREE) {
528 mfc_err("Bufs have already been requested\n");
531 ctx->capture_state = QUEUE_BUFS_REQUESTED;
533 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
536 mfc_err("vb2_reqbufs on capture failed\n");
539 if (reqbufs->count < ctx->dpb_count) {
540 mfc_err("Not enough buffers allocated\n");
543 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
547 ctx->total_dpb_count = reqbufs->count;
548 ret = s5p_mfc_hw_call(dev->mfc_ops, alloc_codec_buffers, ctx);
550 mfc_err("Failed to allocate decoding buffers\n");
553 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
557 if (ctx->dst_bufs_cnt == ctx->total_dpb_count) {
558 ctx->capture_state = QUEUE_BUFS_MMAPED;
560 mfc_err("Not all buffers passed to buf_init\n");
563 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
564 s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
569 if (s5p_mfc_ctx_ready(ctx))
570 set_work_bit_irqsave(ctx);
571 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
572 s5p_mfc_wait_for_done_ctx(ctx,
573 S5P_MFC_R2H_CMD_INIT_BUFFERS_RET, 0);
579 static int vidioc_querybuf(struct file *file, void *priv,
580 struct v4l2_buffer *buf)
582 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
586 if (buf->memory != V4L2_MEMORY_MMAP) {
587 mfc_err("Only mmaped buffers can be used\n");
590 mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
591 if (ctx->state == MFCINST_INIT &&
592 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
593 ret = vb2_querybuf(&ctx->vq_src, buf);
594 } else if (ctx->state == MFCINST_RUNNING &&
595 buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
596 ret = vb2_querybuf(&ctx->vq_dst, buf);
597 for (i = 0; i < buf->length; i++)
598 buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
600 mfc_err("vidioc_querybuf called in an inappropriate state\n");
608 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
610 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
612 if (ctx->state == MFCINST_ERROR) {
613 mfc_err("Call on QBUF after unrecoverable error\n");
616 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
617 return vb2_qbuf(&ctx->vq_src, buf);
618 else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
619 return vb2_qbuf(&ctx->vq_dst, buf);
623 /* Dequeue a buffer */
624 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
626 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
628 if (ctx->state == MFCINST_ERROR) {
629 mfc_err("Call on DQBUF after unrecoverable error\n");
632 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
633 return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
634 else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
635 return vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
639 /* Export DMA buffer */
640 static int vidioc_expbuf(struct file *file, void *priv,
641 struct v4l2_exportbuffer *eb)
643 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
645 if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
646 return vb2_expbuf(&ctx->vq_src, eb);
647 if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
648 return vb2_expbuf(&ctx->vq_dst, eb);
653 static int vidioc_streamon(struct file *file, void *priv,
654 enum v4l2_buf_type type)
656 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
657 struct s5p_mfc_dev *dev = ctx->dev;
661 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
663 if (ctx->state == MFCINST_INIT) {
664 ctx->dst_bufs_cnt = 0;
665 ctx->src_bufs_cnt = 0;
666 ctx->capture_state = QUEUE_FREE;
667 ctx->output_state = QUEUE_FREE;
668 s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer,
670 s5p_mfc_hw_call(dev->mfc_ops, alloc_dec_temp_buffers,
672 set_work_bit_irqsave(ctx);
673 s5p_mfc_clean_ctx_int_flags(ctx);
674 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
676 if (s5p_mfc_wait_for_done_ctx(ctx,
677 S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
678 /* Error or timeout */
679 mfc_err("Error getting instance from hardware\n");
680 s5p_mfc_hw_call(dev->mfc_ops,
681 release_instance_buffer, ctx);
682 s5p_mfc_hw_call(dev->mfc_ops,
683 release_dec_desc_buffer, ctx);
686 mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
688 ret = vb2_streamon(&ctx->vq_src, type);
690 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
691 ret = vb2_streamon(&ctx->vq_dst, type);
696 /* Stream off, which equals to a pause */
697 static int vidioc_streamoff(struct file *file, void *priv,
698 enum v4l2_buf_type type)
700 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
702 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
703 return vb2_streamoff(&ctx->vq_src, type);
704 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
705 return vb2_streamoff(&ctx->vq_dst, type);
709 /* Set controls - v4l2 control framework */
710 static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
712 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
715 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
716 ctx->display_delay = ctrl->val;
718 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
719 ctx->display_delay_enable = ctrl->val;
721 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
722 ctx->loop_filter_mpeg4 = ctrl->val;
724 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
725 ctx->slice_interface = ctrl->val;
728 mfc_err("Invalid control 0x%08x\n", ctrl->id);
734 static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
736 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
737 struct s5p_mfc_dev *dev = ctx->dev;
740 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
741 if (ctx->state >= MFCINST_HEAD_PARSED &&
742 ctx->state < MFCINST_ABORT) {
743 ctrl->val = ctx->dpb_count;
745 } else if (ctx->state != MFCINST_INIT) {
746 v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
749 /* Should wait for the header to be parsed */
750 s5p_mfc_clean_ctx_int_flags(ctx);
751 s5p_mfc_wait_for_done_ctx(ctx,
752 S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
753 if (ctx->state >= MFCINST_HEAD_PARSED &&
754 ctx->state < MFCINST_ABORT) {
755 ctrl->val = ctx->dpb_count;
757 v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
766 static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
767 .s_ctrl = s5p_mfc_dec_s_ctrl,
768 .g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
771 /* Get cropping information */
772 static int vidioc_g_crop(struct file *file, void *priv,
773 struct v4l2_crop *cr)
775 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
776 struct s5p_mfc_dev *dev = ctx->dev;
777 u32 left, right, top, bottom;
779 if (ctx->state != MFCINST_HEAD_PARSED &&
780 ctx->state != MFCINST_RUNNING && ctx->state != MFCINST_FINISHING
781 && ctx->state != MFCINST_FINISHED) {
782 mfc_err("Cannont set crop\n");
785 if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
786 left = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_h, ctx);
787 right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
788 left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
789 top = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_v, ctx);
790 bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
791 top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
794 cr->c.width = ctx->img_width - left - right;
795 cr->c.height = ctx->img_height - top - bottom;
796 mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
797 "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left, top,
798 cr->c.width, cr->c.height, right, bottom,
799 ctx->buf_width, ctx->buf_height);
803 cr->c.width = ctx->img_width;
804 cr->c.height = ctx->img_height;
805 mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
806 "fh=%d\n", cr->c.width, cr->c.height, ctx->buf_width,
813 static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
814 .vidioc_querycap = vidioc_querycap,
815 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
816 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
817 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
818 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
819 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
820 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
821 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
822 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
823 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
824 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
825 .vidioc_reqbufs = vidioc_reqbufs,
826 .vidioc_querybuf = vidioc_querybuf,
827 .vidioc_qbuf = vidioc_qbuf,
828 .vidioc_dqbuf = vidioc_dqbuf,
829 .vidioc_expbuf = vidioc_expbuf,
830 .vidioc_streamon = vidioc_streamon,
831 .vidioc_streamoff = vidioc_streamoff,
832 .vidioc_g_crop = vidioc_g_crop,
835 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
836 const struct v4l2_format *fmt, unsigned int *buf_count,
837 unsigned int *plane_count, unsigned int psize[],
840 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
841 struct s5p_mfc_dev *dev = ctx->dev;
843 /* Video output for decoding (source)
844 * this can be set after getting an instance */
845 if (ctx->state == MFCINST_INIT &&
846 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
847 /* A single plane is required for input */
851 if (*buf_count > MFC_MAX_BUFFERS)
852 *buf_count = MFC_MAX_BUFFERS;
853 /* Video capture for decoding (destination)
854 * this can be set after the header was parsed */
855 } else if (ctx->state == MFCINST_HEAD_PARSED &&
856 vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
857 /* Output plane count is 2 - one for Y and one for CbCr */
859 /* Setup buffer count */
860 if (*buf_count < ctx->dpb_count)
861 *buf_count = ctx->dpb_count;
862 if (*buf_count > ctx->dpb_count + MFC_MAX_EXTRA_DPB)
863 *buf_count = ctx->dpb_count + MFC_MAX_EXTRA_DPB;
864 if (*buf_count > MFC_MAX_BUFFERS)
865 *buf_count = MFC_MAX_BUFFERS;
867 mfc_err("State seems invalid. State = %d, vq->type = %d\n",
868 ctx->state, vq->type);
871 mfc_debug(2, "Buffer count=%d, plane count=%d\n",
872 *buf_count, *plane_count);
873 if (ctx->state == MFCINST_HEAD_PARSED &&
874 vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
875 psize[0] = ctx->luma_size;
876 psize[1] = ctx->chroma_size;
880 ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
883 ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
884 allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
885 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
886 ctx->state == MFCINST_INIT) {
887 psize[0] = ctx->dec_src_buf_size;
888 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
890 mfc_err("This video node is dedicated to decoding. Decoding not initalised\n");
896 static void s5p_mfc_unlock(struct vb2_queue *q)
898 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
899 struct s5p_mfc_dev *dev = ctx->dev;
901 mutex_unlock(&dev->mfc_mutex);
904 static void s5p_mfc_lock(struct vb2_queue *q)
906 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
907 struct s5p_mfc_dev *dev = ctx->dev;
909 mutex_lock(&dev->mfc_mutex);
912 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
914 struct vb2_queue *vq = vb->vb2_queue;
915 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
918 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
919 if (ctx->capture_state == QUEUE_BUFS_MMAPED)
921 for (i = 0; i <= ctx->src_fmt->num_planes ; i++) {
922 if (IS_ERR_OR_NULL(ERR_PTR(
923 vb2_dma_contig_plane_dma_addr(vb, i)))) {
924 mfc_err("Plane mem not allocated\n");
928 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
929 vb2_plane_size(vb, 1) < ctx->chroma_size) {
930 mfc_err("Plane buffer (CAPTURE) is too small\n");
933 i = vb->v4l2_buf.index;
934 ctx->dst_bufs[i].b = vb;
935 ctx->dst_bufs[i].cookie.raw.luma =
936 vb2_dma_contig_plane_dma_addr(vb, 0);
937 ctx->dst_bufs[i].cookie.raw.chroma =
938 vb2_dma_contig_plane_dma_addr(vb, 1);
940 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
941 if (IS_ERR_OR_NULL(ERR_PTR(
942 vb2_dma_contig_plane_dma_addr(vb, 0)))) {
943 mfc_err("Plane memory not allocated\n");
946 if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
947 mfc_err("Plane buffer (OUTPUT) is too small\n");
951 i = vb->v4l2_buf.index;
952 ctx->src_bufs[i].b = vb;
953 ctx->src_bufs[i].cookie.stream =
954 vb2_dma_contig_plane_dma_addr(vb, 0);
957 mfc_err("s5p_mfc_buf_init: unknown queue type\n");
963 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
965 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
966 struct s5p_mfc_dev *dev = ctx->dev;
968 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
969 if (ctx->state == MFCINST_FINISHING ||
970 ctx->state == MFCINST_FINISHED)
971 ctx->state = MFCINST_RUNNING;
972 /* If context is ready then dev = work->data;schedule it to run */
973 if (s5p_mfc_ctx_ready(ctx))
974 set_work_bit_irqsave(ctx);
975 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
979 static int s5p_mfc_stop_streaming(struct vb2_queue *q)
982 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
983 struct s5p_mfc_dev *dev = ctx->dev;
986 if ((ctx->state == MFCINST_FINISHING ||
987 ctx->state == MFCINST_RUNNING) &&
988 dev->curr_ctx == ctx->num && dev->hw_lock) {
989 ctx->state = MFCINST_ABORT;
990 s5p_mfc_wait_for_done_ctx(ctx,
991 S5P_MFC_R2H_CMD_FRAME_DONE_RET, 0);
994 spin_lock_irqsave(&dev->irqlock, flags);
995 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
996 s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
998 INIT_LIST_HEAD(&ctx->dst_queue);
999 ctx->dst_queue_cnt = 0;
1000 ctx->dpb_flush_flag = 1;
1001 ctx->dec_dst_flag = 0;
1003 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1004 s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
1006 INIT_LIST_HEAD(&ctx->src_queue);
1007 ctx->src_queue_cnt = 0;
1010 ctx->state = MFCINST_RUNNING;
1011 spin_unlock_irqrestore(&dev->irqlock, flags);
1016 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1018 struct vb2_queue *vq = vb->vb2_queue;
1019 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1020 struct s5p_mfc_dev *dev = ctx->dev;
1021 unsigned long flags;
1022 struct s5p_mfc_buf *mfc_buf;
1024 if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1025 mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1026 mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1027 spin_lock_irqsave(&dev->irqlock, flags);
1028 list_add_tail(&mfc_buf->list, &ctx->src_queue);
1029 ctx->src_queue_cnt++;
1030 spin_unlock_irqrestore(&dev->irqlock, flags);
1031 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1032 mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1033 mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1034 /* Mark destination as available for use by MFC */
1035 spin_lock_irqsave(&dev->irqlock, flags);
1036 set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
1037 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1038 ctx->dst_queue_cnt++;
1039 spin_unlock_irqrestore(&dev->irqlock, flags);
1041 mfc_err("Unsupported buffer type (%d)\n", vq->type);
1043 if (s5p_mfc_ctx_ready(ctx))
1044 set_work_bit_irqsave(ctx);
1045 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1048 static struct vb2_ops s5p_mfc_dec_qops = {
1049 .queue_setup = s5p_mfc_queue_setup,
1050 .wait_prepare = s5p_mfc_unlock,
1051 .wait_finish = s5p_mfc_lock,
1052 .buf_init = s5p_mfc_buf_init,
1053 .start_streaming = s5p_mfc_start_streaming,
1054 .stop_streaming = s5p_mfc_stop_streaming,
1055 .buf_queue = s5p_mfc_buf_queue,
1058 struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
1060 return &decoder_codec_ops;
1063 struct vb2_ops *get_dec_queue_ops(void)
1065 return &s5p_mfc_dec_qops;
1068 const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1070 return &s5p_mfc_dec_ioctl_ops;
1073 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1074 && V4L2_CTRL_DRIVER_PRIV(x))
1076 int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
1078 struct v4l2_ctrl_config cfg;
1081 v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1082 if (ctx->ctrl_handler.error) {
1083 mfc_err("v4l2_ctrl_handler_init failed\n");
1084 return ctx->ctrl_handler.error;
1087 for (i = 0; i < NUM_CTRLS; i++) {
1088 if (IS_MFC51_PRIV(controls[i].id)) {
1089 memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
1090 cfg.ops = &s5p_mfc_dec_ctrl_ops;
1091 cfg.id = controls[i].id;
1092 cfg.min = controls[i].minimum;
1093 cfg.max = controls[i].maximum;
1094 cfg.def = controls[i].default_value;
1095 cfg.name = controls[i].name;
1096 cfg.type = controls[i].type;
1098 cfg.step = controls[i].step;
1099 cfg.menu_skip_mask = 0;
1101 ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1104 ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1105 &s5p_mfc_dec_ctrl_ops,
1106 controls[i].id, controls[i].minimum,
1107 controls[i].maximum, controls[i].step,
1108 controls[i].default_value);
1110 if (ctx->ctrl_handler.error) {
1111 mfc_err("Adding control (%d) failed\n", i);
1112 return ctx->ctrl_handler.error;
1114 if (controls[i].is_volatile && ctx->ctrls[i])
1115 ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1120 void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
1124 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1125 for (i = 0; i < NUM_CTRLS; i++)
1126 ctx->ctrls[i] = NULL;
1129 void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
1131 struct v4l2_format f;
1132 f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_DEC;
1133 ctx->src_fmt = find_format(&f, MFC_FMT_DEC);
1134 f.fmt.pix_mp.pixelformat = DEF_DST_FMT_DEC;
1135 ctx->dst_fmt = find_format(&f, MFC_FMT_RAW);
1136 mfc_debug(2, "Default src_fmt is %x, dest_fmt is %x\n",
1137 (unsigned int)ctx->src_fmt, (unsigned int)ctx->dst_fmt);