2 * linux/drivers/media/video/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_pm.h"
35 static struct s5p_mfc_fmt formats[] = {
37 .name = "4:2:0 2 Planes 16x16 Tiles",
38 .fourcc = V4L2_PIX_FMT_NV12MT_16X16,
39 .codec_mode = S5P_FIMV_CODEC_NONE,
44 .name = "4:2:0 2 Planes 64x32 Tiles",
45 .fourcc = V4L2_PIX_FMT_NV12MT,
46 .codec_mode = S5P_FIMV_CODEC_NONE,
51 .name = "4:2:0 2 Planes Y/CbCr",
52 .fourcc = V4L2_PIX_FMT_NV12M,
53 .codec_mode = S5P_FIMV_CODEC_NONE,
58 .name = "4:2:0 2 Planes Y/CrCb",
59 .fourcc = V4L2_PIX_FMT_NV21M,
60 .codec_mode = S5P_FIMV_CODEC_NONE,
65 .name = "H264 Encoded Stream",
66 .fourcc = V4L2_PIX_FMT_H264,
67 .codec_mode = S5P_FIMV_CODEC_H264_DEC,
72 .name = "H264/MVC Encoded Stream",
73 .fourcc = V4L2_PIX_FMT_H264_MVC,
74 .codec_mode = S5P_FIMV_CODEC_H264_MVC_DEC,
79 .name = "H263 Encoded Stream",
80 .fourcc = V4L2_PIX_FMT_H263,
81 .codec_mode = S5P_FIMV_CODEC_H263_DEC,
86 .name = "MPEG1 Encoded Stream",
87 .fourcc = V4L2_PIX_FMT_MPEG1,
88 .codec_mode = S5P_FIMV_CODEC_MPEG2_DEC,
93 .name = "MPEG2 Encoded Stream",
94 .fourcc = V4L2_PIX_FMT_MPEG2,
95 .codec_mode = S5P_FIMV_CODEC_MPEG2_DEC,
100 .name = "MPEG4 Encoded Stream",
101 .fourcc = V4L2_PIX_FMT_MPEG4,
102 .codec_mode = S5P_FIMV_CODEC_MPEG4_DEC,
107 .name = "XviD Encoded Stream",
108 .fourcc = V4L2_PIX_FMT_XVID,
109 .codec_mode = S5P_FIMV_CODEC_MPEG4_DEC,
114 .name = "VC1 Encoded Stream",
115 .fourcc = V4L2_PIX_FMT_VC1_ANNEX_G,
116 .codec_mode = S5P_FIMV_CODEC_VC1_DEC,
121 .name = "VC1 RCV Encoded Stream",
122 .fourcc = V4L2_PIX_FMT_VC1_ANNEX_L,
123 .codec_mode = S5P_FIMV_CODEC_VC1RCV_DEC,
128 .name = "VC8 Encoded Stream",
129 .fourcc = V4L2_PIX_FMT_VP8,
130 .codec_mode = S5P_FIMV_CODEC_VP8_DEC,
136 #define NUM_FORMATS ARRAY_SIZE(formats)
138 /* Find selected format description */
139 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
143 for (i = 0; i < NUM_FORMATS; i++) {
144 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
145 formats[i].type == t)
151 static struct mfc_control controls[] = {
153 .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
154 .type = V4L2_CTRL_TYPE_INTEGER,
155 .name = "H264 Display Delay",
162 .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
163 .type = V4L2_CTRL_TYPE_BOOLEAN,
164 .name = "H264 Display Delay Enable",
171 .id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
172 .type = V4L2_CTRL_TYPE_BOOLEAN,
173 .name = "Mpeg4 Loop Filter Enable",
180 .id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
181 .type = V4L2_CTRL_TYPE_BOOLEAN,
182 .name = "Slice Interface Enable",
189 .id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
190 .type = V4L2_CTRL_TYPE_INTEGER,
191 .name = "Minimum number of cap bufs",
199 .id = V4L2_CID_CODEC_DISPLAY_STATUS,
200 .type = V4L2_CTRL_TYPE_INTEGER,
201 .name = "Display Status",
210 #define NUM_CTRLS ARRAY_SIZE(controls)
212 /* Check whether a context should be run on hardware */
213 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
215 /* Context is to parse header */
216 if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
218 /* Context is to decode a frame */
219 if (ctx->src_queue_cnt >= 1 &&
220 ctx->state == MFCINST_RUNNING &&
221 ctx->dst_queue_cnt >= ctx->dpb_count)
223 /* Context is to return last frame */
224 if (ctx->state == MFCINST_FINISHING &&
225 ctx->dst_queue_cnt >= ctx->dpb_count)
227 /* Context is to set buffers */
228 if (ctx->src_queue_cnt >= 1 &&
229 ctx->state == MFCINST_HEAD_PARSED &&
230 ctx->capture_state == QUEUE_BUFS_MMAPED)
232 /* Resolution change */
233 if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
234 ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
235 ctx->dst_queue_cnt >= ctx->dpb_count)
237 if (ctx->state == MFCINST_RES_CHANGE_END &&
238 ctx->src_queue_cnt >= 1)
240 mfc_debug(2, "ctx is not ready\n");
244 static struct s5p_mfc_codec_ops decoder_codec_ops = {
245 .pre_seq_start = NULL,
246 .post_seq_start = NULL,
247 .pre_frame_start = NULL,
248 .post_frame_start = NULL,
251 /* Query capabilities of the device */
252 static int vidioc_querycap(struct file *file, void *priv,
253 struct v4l2_capability *cap)
255 struct s5p_mfc_dev *dev = video_drvdata(file);
257 strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
258 strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
259 cap->bus_info[0] = 0;
260 cap->version = KERNEL_VERSION(1, 0, 0);
261 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
262 V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_STREAMING;
266 /* Enumerate format */
267 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
269 struct s5p_mfc_fmt *fmt;
272 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
273 if (mplane && formats[i].num_planes == 1)
275 else if (!mplane && formats[i].num_planes > 1)
277 if (out && formats[i].type != MFC_FMT_DEC)
279 else if (!out && formats[i].type != MFC_FMT_RAW)
286 if (i == ARRAY_SIZE(formats))
289 strlcpy(f->description, fmt->name, sizeof(f->description));
290 f->pixelformat = fmt->fourcc;
294 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
295 struct v4l2_fmtdesc *f)
297 return vidioc_enum_fmt(f, false, false);
300 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
301 struct v4l2_fmtdesc *f)
303 return vidioc_enum_fmt(f, true, false);
306 static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
307 struct v4l2_fmtdesc *f)
309 return vidioc_enum_fmt(f, false, true);
312 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
313 struct v4l2_fmtdesc *f)
315 return vidioc_enum_fmt(f, true, true);
319 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
321 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
322 struct v4l2_pix_format_mplane *pix_mp;
325 pix_mp = &f->fmt.pix_mp;
326 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
327 (ctx->state == MFCINST_GOT_INST || ctx->state ==
328 MFCINST_RES_CHANGE_END)) {
329 /* If the MFC is parsing the header,
330 * so wait until it is finished */
331 s5p_mfc_clean_ctx_int_flags(ctx);
332 s5p_mfc_wait_for_done_ctx(ctx, S5P_FIMV_R2H_CMD_SEQ_DONE_RET,
335 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
336 ctx->state >= MFCINST_HEAD_PARSED &&
337 ctx->state < MFCINST_ABORT) {
338 /* This is run on CAPTURE (decode output) */
339 /* Width and height are set to the dimensions
340 of the movie, the buffer is bigger and
341 further processing stages should crop to this
343 pix_mp->width = ctx->buf_width;
344 pix_mp->height = ctx->buf_height;
345 pix_mp->field = V4L2_FIELD_NONE;
346 pix_mp->num_planes = 2;
347 /* Set pixelformat to the format in which MFC
348 outputs the decoded frame */
349 pix_mp->pixelformat = V4L2_PIX_FMT_NV12MT_16X16;
350 pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
351 pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
352 pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
353 pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
354 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
355 /* This is run on OUTPUT
356 The buffer contains compressed image
357 so width and height have no meaning */
360 pix_mp->field = V4L2_FIELD_NONE;
361 pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
362 pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
363 pix_mp->pixelformat = ctx->src_fmt->fourcc;
364 pix_mp->num_planes = ctx->src_fmt->num_planes;
366 mfc_err("Format could not be read\n");
367 mfc_debug(2, "%s-- with error\n", __func__);
375 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
377 struct s5p_mfc_dev *dev = video_drvdata(file);
378 struct s5p_mfc_fmt *fmt;
380 mfc_debug(2, "Type is %d\n", f->type);
381 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
382 fmt = find_format(f, MFC_FMT_DEC);
384 mfc_err("Unsupported format for source.\n");
387 if (!IS_MFCV6(dev)) {
388 if (fmt->fourcc == V4L2_PIX_FMT_VP8) {
389 mfc_err("Not supported format.\n");
393 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
394 fmt = find_format(f, MFC_FMT_RAW);
396 mfc_err("Unsupported format for destination.\n");
400 if (fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
401 mfc_err("Not supported format.\n");
405 if (fmt->fourcc != V4L2_PIX_FMT_NV12MT) {
406 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 s5p_mfc_fmt *fmt;
422 struct v4l2_pix_format_mplane *pix_mp;
425 ret = vidioc_try_fmt(file, priv, f);
426 pix_mp = &f->fmt.pix_mp;
429 if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
430 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
434 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
435 fmt = find_format(f, MFC_FMT_RAW);
437 mfc_err("Unsupported format for source.\n");
440 if (!IS_MFCV6(dev)) {
441 if (fmt->fourcc != V4L2_PIX_FMT_NV12MT) {
442 mfc_err("Not supported format.\n");
445 } else if (IS_MFCV6(dev)) {
446 if (fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
447 mfc_err("Not supported format.\n");
454 } else if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
455 mfc_err("Wrong type error for S_FMT : %d", f->type);
458 fmt = find_format(f, MFC_FMT_DEC);
459 if (!fmt || fmt->codec_mode == S5P_FIMV_CODEC_NONE) {
460 mfc_err("Unknown codec\n");
464 if (fmt->type != MFC_FMT_DEC) {
465 mfc_err("Wrong format selected, you should choose "
466 "format for decoding\n");
470 if (!IS_MFCV6(dev)) {
471 if (fmt->fourcc == V4L2_PIX_FMT_VP8) {
472 mfc_err("Not supported format.\n");
477 ctx->codec_mode = fmt->codec_mode;
478 mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
481 if (pix_mp->plane_fmt[0].sizeimage)
482 ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
484 pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
486 pix_mp->plane_fmt[0].bytesperline = 0;
487 ctx->state = MFCINST_INIT;
493 /* Reqeust buffers */
494 static int vidioc_reqbufs(struct file *file, void *priv,
495 struct v4l2_requestbuffers *reqbufs)
497 struct s5p_mfc_dev *dev = video_drvdata(file);
498 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
502 if (reqbufs->memory != V4L2_MEMORY_MMAP) {
503 mfc_err("Only V4L2_MEMORY_MAP is supported\n");
506 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
507 /* Can only request buffers after an instance has been opened.*/
508 if (ctx->state == MFCINST_INIT) {
509 ctx->src_bufs_cnt = 0;
510 if (reqbufs->count == 0) {
511 mfc_debug(2, "Freeing buffers\n");
513 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
518 if (ctx->output_state != QUEUE_FREE) {
519 mfc_err("Bufs have already been requested\n");
523 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
526 mfc_err("vb2_reqbufs on output failed\n");
529 mfc_debug(2, "vb2_reqbufs: %d\n", ret);
530 ctx->output_state = QUEUE_BUFS_REQUESTED;
532 } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
533 ctx->dst_bufs_cnt = 0;
534 if (reqbufs->count == 0) {
535 mfc_debug(2, "Freeing buffers\n");
537 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
541 if (ctx->capture_state != QUEUE_FREE) {
542 mfc_err("Bufs have already been requested\n");
545 ctx->capture_state = QUEUE_BUFS_REQUESTED;
547 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
550 mfc_err("vb2_reqbufs on capture failed\n");
553 if (reqbufs->count < ctx->dpb_count) {
554 mfc_err("Not enough buffers allocated\n");
557 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
561 ctx->total_dpb_count = reqbufs->count;
562 ret = s5p_mfc_alloc_codec_buffers(ctx);
564 mfc_err("Failed to allocate decoding buffers\n");
567 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
571 if (ctx->dst_bufs_cnt == ctx->total_dpb_count) {
572 ctx->capture_state = QUEUE_BUFS_MMAPED;
574 mfc_err("Not all buffers passed to buf_init\n");
577 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
578 s5p_mfc_release_codec_buffers(ctx);
582 if (s5p_mfc_ctx_ready(ctx)) {
583 spin_lock_irqsave(&dev->condlock, flags);
584 set_bit(ctx->num, &dev->ctx_work_bits);
585 spin_unlock_irqrestore(&dev->condlock, flags);
587 s5p_mfc_try_run(dev);
588 s5p_mfc_wait_for_done_ctx(ctx,
589 S5P_FIMV_R2H_CMD_INIT_BUFFERS_RET, 0);
595 static int vidioc_querybuf(struct file *file, void *priv,
596 struct v4l2_buffer *buf)
598 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
602 if (buf->memory != V4L2_MEMORY_MMAP) {
603 mfc_err("Only mmaped buffers can be used\n");
606 mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
607 if (ctx->state == MFCINST_INIT &&
608 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
609 ret = vb2_querybuf(&ctx->vq_src, buf);
610 } else if (ctx->state == MFCINST_RUNNING &&
611 buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
612 ret = vb2_querybuf(&ctx->vq_dst, buf);
613 for (i = 0; i < buf->length; i++)
614 buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
616 mfc_err("vidioc_querybuf called in an inappropriate state\n");
624 static int vidioc_qbuf(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 QBUF after unrecoverable error\n");
632 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
633 return vb2_qbuf(&ctx->vq_src, buf);
634 else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
635 return vb2_qbuf(&ctx->vq_dst, buf);
639 /* Dequeue a buffer */
640 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
642 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
644 if (ctx->state == MFCINST_ERROR) {
645 mfc_err("Call on DQBUF after unrecoverable error\n");
648 if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
649 return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
650 else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
651 return vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
655 /* Export DMA buffer */
656 static int vidioc_expbuf(struct file *file, void *priv,
657 struct v4l2_exportbuffer *eb)
659 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
662 if (eb->mem_offset < DST_QUEUE_OFF_BASE)
663 return vb2_expbuf(&ctx->vq_src, eb);
665 eb->mem_offset -= DST_QUEUE_OFF_BASE;
666 ret = vb2_expbuf(&ctx->vq_dst, eb);
667 eb->mem_offset += DST_QUEUE_OFF_BASE;
673 static int vidioc_streamon(struct file *file, void *priv,
674 enum v4l2_buf_type type)
676 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
677 struct s5p_mfc_dev *dev = ctx->dev;
682 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
684 if (ctx->state == MFCINST_INIT) {
685 ctx->dst_bufs_cnt = 0;
686 ctx->src_bufs_cnt = 0;
687 ctx->capture_state = QUEUE_FREE;
688 ctx->output_state = QUEUE_FREE;
689 s5p_mfc_alloc_instance_buffer(ctx);
690 s5p_mfc_alloc_dec_temp_buffers(ctx);
691 spin_lock_irqsave(&dev->condlock, flags);
692 set_bit(ctx->num, &dev->ctx_work_bits);
693 spin_unlock_irqrestore(&dev->condlock, flags);
694 s5p_mfc_clean_ctx_int_flags(ctx);
695 s5p_mfc_try_run(dev);
697 if (s5p_mfc_wait_for_done_ctx(ctx,
698 S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
699 /* Error or timeout */
700 mfc_err("Error getting instance from hardware\n");
701 s5p_mfc_release_instance_buffer(ctx);
702 s5p_mfc_release_dec_desc_buffer(ctx);
705 mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
707 ret = vb2_streamon(&ctx->vq_src, type);
709 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
710 ret = vb2_streamon(&ctx->vq_dst, type);
715 /* Stream off, which equals to a pause */
716 static int vidioc_streamoff(struct file *file, void *priv,
717 enum v4l2_buf_type type)
719 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
721 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
722 return vb2_streamoff(&ctx->vq_src, type);
723 else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
724 return vb2_streamoff(&ctx->vq_dst, type);
728 /* Set controls - v4l2 control framework */
729 static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
731 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
734 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
735 ctx->loop_filter_mpeg4 = ctrl->val;
737 case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
738 ctx->display_delay_enable = ctrl->val;
740 case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
741 ctx->display_delay = ctrl->val;
743 case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
744 ctx->slice_interface = ctrl->val;
747 mfc_err("Invalid control 0x%08x\n", ctrl->id);
753 static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
755 struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
756 struct s5p_mfc_dev *dev = ctx->dev;
759 case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
760 if (ctx->state >= MFCINST_HEAD_PARSED &&
761 ctx->state < MFCINST_ABORT) {
762 ctrl->val = ctx->dpb_count;
764 } else if (ctx->state != MFCINST_INIT) {
765 v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
768 /* Should wait for the header to be parsed */
769 s5p_mfc_clean_ctx_int_flags(ctx);
770 s5p_mfc_wait_for_done_ctx(ctx,
771 S5P_FIMV_R2H_CMD_SEQ_DONE_RET, 0);
772 if (ctx->state >= MFCINST_HEAD_PARSED &&
773 ctx->state < MFCINST_ABORT) {
774 ctrl->val = ctx->dpb_count;
776 v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
780 case V4L2_CID_CODEC_DISPLAY_STATUS:
781 ctrl->val = s5p_mfc_get_dspl_status()
782 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
789 static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
790 .s_ctrl = s5p_mfc_dec_s_ctrl,
791 .g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
794 /* Get cropping information */
795 static int vidioc_g_crop(struct file *file, void *priv,
796 struct v4l2_crop *cr)
798 struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
799 u32 left, right, top, bottom;
801 if (ctx->state != MFCINST_HEAD_PARSED &&
802 ctx->state != MFCINST_RUNNING && ctx->state != MFCINST_FINISHING
803 && ctx->state != MFCINST_FINISHED) {
804 mfc_err("Cannont set crop\n");
807 if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
808 left = s5p_mfc_read_info(ctx, CROP_INFO_H);
809 right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
810 left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
811 top = s5p_mfc_read_info(ctx, CROP_INFO_V);
812 bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
813 top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
816 cr->c.width = ctx->img_width - left - right;
817 cr->c.height = ctx->img_height - top - bottom;
818 mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
819 "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left, top,
820 cr->c.width, cr->c.height, right, bottom,
821 ctx->buf_width, ctx->buf_height);
825 cr->c.width = ctx->img_width;
826 cr->c.height = ctx->img_height;
827 mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
828 "fh=%d\n", cr->c.width, cr->c.height, ctx->buf_width,
835 static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
836 .vidioc_querycap = vidioc_querycap,
837 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
838 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
839 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
840 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
841 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
842 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
843 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
844 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
845 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
846 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
847 .vidioc_reqbufs = vidioc_reqbufs,
848 .vidioc_querybuf = vidioc_querybuf,
849 .vidioc_qbuf = vidioc_qbuf,
850 .vidioc_dqbuf = vidioc_dqbuf,
851 .vidioc_expbuf = vidioc_expbuf,
852 .vidioc_streamon = vidioc_streamon,
853 .vidioc_streamoff = vidioc_streamoff,
854 .vidioc_g_crop = vidioc_g_crop,
857 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
858 const struct v4l2_format *fmt, unsigned int *buf_count,
859 unsigned int *plane_count, unsigned int psize[],
862 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
863 struct s5p_mfc_dev *dev = ctx->dev;
865 /* Video output for decoding (source)
866 * this can be set after getting an instance */
867 if (ctx->state == MFCINST_INIT &&
868 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
869 /* A single plane is required for input */
873 if (*buf_count > MFC_MAX_BUFFERS)
874 *buf_count = MFC_MAX_BUFFERS;
875 /* Video capture for decoding (destination)
876 * this can be set after the header was parsed */
877 } else if (ctx->state == MFCINST_HEAD_PARSED &&
878 vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
879 /* Output plane count is 2 - one for Y and one for CbCr */
881 /* Setup buffer count */
882 if (*buf_count < ctx->dpb_count)
883 *buf_count = ctx->dpb_count;
884 if (*buf_count > ctx->dpb_count + MFC_MAX_EXTRA_DPB)
885 *buf_count = ctx->dpb_count + MFC_MAX_EXTRA_DPB;
886 if (*buf_count > MFC_MAX_BUFFERS)
887 *buf_count = MFC_MAX_BUFFERS;
889 mfc_err("State seems invalid. State = %d, vq->type = %d\n",
890 ctx->state, vq->type);
893 mfc_debug(2, "Buffer count=%d, plane count=%d\n",
894 *buf_count, *plane_count);
895 if (ctx->state == MFCINST_HEAD_PARSED &&
896 vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
897 psize[0] = ctx->luma_size;
898 psize[1] = ctx->chroma_size;
901 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
903 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
904 allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
905 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
906 ctx->state == MFCINST_INIT) {
907 psize[0] = ctx->dec_src_buf_size;
908 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
910 mfc_err("This video node is dedicated to decoding. Decoding not initalised\n");
916 static void s5p_mfc_unlock(struct vb2_queue *q)
918 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
919 struct s5p_mfc_dev *dev = ctx->dev;
921 mutex_unlock(&dev->mfc_mutex);
924 static void s5p_mfc_lock(struct vb2_queue *q)
926 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
927 struct s5p_mfc_dev *dev = ctx->dev;
929 mutex_lock(&dev->mfc_mutex);
932 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
934 struct vb2_queue *vq = vb->vb2_queue;
935 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
938 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
939 if (ctx->capture_state == QUEUE_BUFS_MMAPED)
941 for (i = 0; i <= ctx->src_fmt->num_planes; i++) {
942 if (IS_ERR_OR_NULL(ERR_PTR(
943 vb2_dma_contig_plane_dma_addr(vb, i)))) {
944 mfc_err("Plane mem not allocated\n");
948 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
949 vb2_plane_size(vb, 1) < ctx->chroma_size) {
950 mfc_err("Plane buffer (CAPTURE) is too small\n");
953 i = vb->v4l2_buf.index;
954 ctx->dst_bufs[i].b = vb;
955 ctx->dst_bufs[i].cookie.raw.luma =
956 vb2_dma_contig_plane_dma_addr(vb, 0);
957 ctx->dst_bufs[i].cookie.raw.chroma =
958 vb2_dma_contig_plane_dma_addr(vb, 1);
960 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
961 if (IS_ERR_OR_NULL(ERR_PTR(
962 vb2_dma_contig_plane_dma_addr(vb, 0)))) {
963 mfc_err("Plane memory not allocated\n");
966 if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
967 mfc_err("Plane buffer (OUTPUT) is too small\n");
971 i = vb->v4l2_buf.index;
972 ctx->src_bufs[i].b = vb;
973 ctx->src_bufs[i].cookie.stream =
974 vb2_dma_contig_plane_dma_addr(vb, 0);
977 mfc_err("s5p_mfc_buf_init: unknown queue type\n");
983 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
985 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
986 struct s5p_mfc_dev *dev = ctx->dev;
989 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
990 if (ctx->state == MFCINST_FINISHING ||
991 ctx->state == MFCINST_FINISHED)
992 ctx->state = MFCINST_RUNNING;
993 /* If context is ready then dev = work->data;schedule it to run */
994 if (s5p_mfc_ctx_ready(ctx)) {
995 spin_lock_irqsave(&dev->condlock, flags);
996 set_bit(ctx->num, &dev->ctx_work_bits);
997 spin_unlock_irqrestore(&dev->condlock, flags);
999 s5p_mfc_try_run(dev);
1003 static int s5p_mfc_stop_streaming(struct vb2_queue *q)
1005 unsigned long flags;
1006 struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1007 struct s5p_mfc_dev *dev = ctx->dev;
1010 if ((ctx->state == MFCINST_FINISHING ||
1011 ctx->state == MFCINST_RUNNING) &&
1012 dev->curr_ctx == ctx->num && dev->hw_lock) {
1013 ctx->state = MFCINST_ABORT;
1014 s5p_mfc_wait_for_done_ctx(ctx,
1015 S5P_FIMV_R2H_CMD_FRAME_DONE_RET, 0);
1018 spin_lock_irqsave(&dev->irqlock, flags);
1019 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1020 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
1021 INIT_LIST_HEAD(&ctx->dst_queue);
1022 ctx->dst_queue_cnt = 0;
1024 ctx->dec_dst_flag = 0;
1026 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1027 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
1028 INIT_LIST_HEAD(&ctx->src_queue);
1029 ctx->src_queue_cnt = 0;
1032 ctx->state = MFCINST_RUNNING;
1033 spin_unlock_irqrestore(&dev->irqlock, flags);
1038 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1040 struct vb2_queue *vq = vb->vb2_queue;
1041 struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1042 struct s5p_mfc_dev *dev = ctx->dev;
1043 unsigned long flags;
1044 struct s5p_mfc_buf *mfc_buf;
1046 if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1047 mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1049 spin_lock_irqsave(&dev->irqlock, flags);
1050 list_add_tail(&mfc_buf->list, &ctx->src_queue);
1051 ctx->src_queue_cnt++;
1052 spin_unlock_irqrestore(&dev->irqlock, flags);
1053 } else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1054 mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1056 /* Mark destination as available for use by MFC */
1057 spin_lock_irqsave(&dev->irqlock, flags);
1058 set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
1059 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1060 ctx->dst_queue_cnt++;
1061 spin_unlock_irqrestore(&dev->irqlock, flags);
1063 mfc_err("Unsupported buffer type (%d)\n", vq->type);
1065 if (s5p_mfc_ctx_ready(ctx)) {
1066 spin_lock_irqsave(&dev->condlock, flags);
1067 set_bit(ctx->num, &dev->ctx_work_bits);
1068 spin_unlock_irqrestore(&dev->condlock, flags);
1070 s5p_mfc_try_run(dev);
1073 static struct vb2_ops s5p_mfc_dec_qops = {
1074 .queue_setup = s5p_mfc_queue_setup,
1075 .wait_prepare = s5p_mfc_unlock,
1076 .wait_finish = s5p_mfc_lock,
1077 .buf_init = s5p_mfc_buf_init,
1078 .start_streaming = s5p_mfc_start_streaming,
1079 .stop_streaming = s5p_mfc_stop_streaming,
1080 .buf_queue = s5p_mfc_buf_queue,
1083 struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
1085 return &decoder_codec_ops;
1088 struct vb2_ops *get_dec_queue_ops(void)
1090 return &s5p_mfc_dec_qops;
1093 const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1095 return &s5p_mfc_dec_ioctl_ops;
1098 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1099 && V4L2_CTRL_DRIVER_PRIV(x))
1101 int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
1103 struct v4l2_ctrl_config cfg;
1106 v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1107 if (ctx->ctrl_handler.error) {
1108 mfc_err("v4l2_ctrl_handler_init failed\n");
1109 return ctx->ctrl_handler.error;
1112 for (i = 0; i < NUM_CTRLS; i++) {
1113 if (IS_MFC51_PRIV(controls[i].id)) {
1114 cfg.ops = &s5p_mfc_dec_ctrl_ops;
1115 cfg.id = controls[i].id;
1116 cfg.min = controls[i].minimum;
1117 cfg.max = controls[i].maximum;
1118 cfg.def = controls[i].default_value;
1119 cfg.name = controls[i].name;
1120 cfg.type = controls[i].type;
1122 cfg.step = controls[i].step;
1123 cfg.menu_skip_mask = 0;
1125 ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1128 ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1129 &s5p_mfc_dec_ctrl_ops,
1130 controls[i].id, controls[i].minimum,
1131 controls[i].maximum, controls[i].step,
1132 controls[i].default_value);
1134 if (ctx->ctrl_handler.error) {
1135 mfc_err("Adding control (%d) failed\n", i);
1136 return ctx->ctrl_handler.error;
1138 if (controls[i].is_volatile && ctx->ctrls[i])
1139 ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1144 void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
1148 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1149 for (i = 0; i < NUM_CTRLS; i++)
1150 ctx->ctrls[i] = NULL;
1153 void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
1155 ctx->src_fmt = &formats[DEF_SRC_FMT];
1156 ctx->dst_fmt = &formats[DEF_DST_FMT];