2 * Rockchip RK3288 VPU codec driver
4 * Copyright (C) 2014 Rockchip Electronics Co., Ltd.
5 * Alpha Lin <Alpha.Lin@rock-chips.com>
6 * Jeffy Chen <jeffy.chen@rock-chips.com>
8 * Copyright (C) 2014 Google, Inc.
9 * Tomasz Figa <tfiga@chromium.org>
11 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
13 * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
15 * This software is licensed under the terms of the GNU General Public
16 * License version 2, as published by the Free Software Foundation, and
17 * may be copied, distributed, and modified under those terms.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
25 #include "rk3288_vpu_common.h"
27 #include <linux/clk.h>
28 #include <linux/interrupt.h>
30 #include <linux/module.h>
31 #include <linux/platform_device.h>
32 #include <linux/sched.h>
33 #include <linux/version.h>
34 #include <linux/videodev2.h>
35 #include <media/v4l2-event.h>
36 #include <linux/workqueue.h>
37 #include <media/v4l2-ctrls.h>
38 #include <media/videobuf2-core.h>
39 #include <media/videobuf2-dma-sg.h>
41 #include "rk3288_vpu_enc.h"
42 #include "rk3288_vpu_hw.h"
44 #define DEF_SRC_FMT_ENC V4L2_PIX_FMT_NV12
45 #define DEF_DST_FMT_ENC V4L2_PIX_FMT_VP8
47 #define RK3288_ENC_MIN_WIDTH 96U
48 #define RK3288_ENC_MAX_WIDTH 1920U
49 #define RK3288_ENC_MIN_HEIGHT 96U
50 #define RK3288_ENC_MAX_HEIGHT 1088U
52 #define V4L2_CID_PRIVATE_RK3288_HEADER (V4L2_CID_CUSTOM_BASE + 0)
53 #define V4L2_CID_PRIVATE_RK3288_REG_PARAMS (V4L2_CID_CUSTOM_BASE + 1)
54 #define V4L2_CID_PRIVATE_RK3288_HW_PARAMS (V4L2_CID_CUSTOM_BASE + 2)
55 #define V4L2_CID_PRIVATE_RK3288_RET_PARAMS (V4L2_CID_CUSTOM_BASE + 3)
57 static struct rk3288_vpu_fmt formats[] = {
60 .name = "4:2:0 3 planes Y/Cb/Cr",
61 .fourcc = V4L2_PIX_FMT_YUV420M,
62 .codec_mode = RK_VPU_CODEC_NONE,
65 .enc_fmt = RK3288_VPU_ENC_FMT_YUV420P,
68 .name = "4:2:0 2 plane Y/CbCr",
69 .fourcc = V4L2_PIX_FMT_NV12M,
70 .codec_mode = RK_VPU_CODEC_NONE,
73 .enc_fmt = RK3288_VPU_ENC_FMT_YUV420SP,
76 .name = "4:2:2 1 plane YUYV",
77 .fourcc = V4L2_PIX_FMT_YUYV,
78 .codec_mode = RK_VPU_CODEC_NONE,
81 .enc_fmt = RK3288_VPU_ENC_FMT_YUYV422,
84 .name = "4:2:2 1 plane UYVY",
85 .fourcc = V4L2_PIX_FMT_UYVY,
86 .codec_mode = RK_VPU_CODEC_NONE,
89 .enc_fmt = RK3288_VPU_ENC_FMT_UYVY422,
91 /* Destination formats. */
93 .name = "VP8 Encoded Stream",
94 .fourcc = V4L2_PIX_FMT_VP8,
95 .codec_mode = RK_VPU_CODEC_VP8E,
100 static struct rk3288_vpu_fmt *find_format(struct v4l2_format *f, bool bitstream)
106 for (i = 0; i < ARRAY_SIZE(formats); i++) {
107 if (formats[i].fourcc != f->fmt.pix_mp.pixelformat)
109 if (bitstream && formats[i].codec_mode != RK_VPU_CODEC_NONE)
111 if (!bitstream && formats[i].codec_mode == RK_VPU_CODEC_NONE)
119 * Indices of controls that need to be accessed directly, i.e. through
120 * p_cur.p pointer of their v4l2_ctrl structs.
123 RK3288_VPU_ENC_CTRL_HEADER,
124 RK3288_VPU_ENC_CTRL_REG_PARAMS,
125 RK3288_VPU_ENC_CTRL_HW_PARAMS,
126 RK3288_VPU_ENC_CTRL_RET_PARAMS,
129 static struct rk3288_vpu_control controls[] = {
130 /* Private, per-frame controls. */
131 [RK3288_VPU_ENC_CTRL_HEADER] = {
132 .id = V4L2_CID_PRIVATE_RK3288_HEADER,
133 .type = V4L2_CTRL_TYPE_PRIVATE,
134 .name = "Rk3288 Private Header",
135 .elem_size = RK3288_HEADER_SIZE,
136 .max_stores = VIDEO_MAX_FRAME,
139 [RK3288_VPU_ENC_CTRL_REG_PARAMS] = {
140 .id = V4L2_CID_PRIVATE_RK3288_REG_PARAMS,
141 .type = V4L2_CTRL_TYPE_PRIVATE,
142 .name = "Rk3288 Private Reg Params",
143 .elem_size = sizeof(struct rk3288_vp8e_reg_params),
144 .max_stores = VIDEO_MAX_FRAME,
147 [RK3288_VPU_ENC_CTRL_HW_PARAMS] = {
148 .id = V4L2_CID_PRIVATE_RK3288_HW_PARAMS,
149 .type = V4L2_CTRL_TYPE_PRIVATE,
150 .name = "Rk3288 Private Hw Params",
151 .elem_size = RK3288_HW_PARAMS_SIZE,
152 .max_stores = VIDEO_MAX_FRAME,
155 [RK3288_VPU_ENC_CTRL_RET_PARAMS] = {
156 .id = V4L2_CID_PRIVATE_RK3288_RET_PARAMS,
157 .type = V4L2_CTRL_TYPE_PRIVATE,
158 .name = "Rk3288 Private Ret Params",
160 .is_read_only = true,
161 .max_stores = VIDEO_MAX_FRAME,
162 .elem_size = RK3288_RET_PARAMS_SIZE,
164 /* Generic controls. (currently ignored) */
166 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
167 .type = V4L2_CTRL_TYPE_INTEGER,
174 .id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
175 .type = V4L2_CTRL_TYPE_BOOLEAN,
182 .id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
183 .type = V4L2_CTRL_TYPE_BOOLEAN,
190 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
191 .type = V4L2_CTRL_TYPE_INTEGER,
193 .maximum = 288000000,
195 .default_value = 2097152,
198 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
199 .type = V4L2_CTRL_TYPE_MENU,
200 .minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
201 .maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
202 .default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
204 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
205 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
206 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
210 .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
211 .type = V4L2_CTRL_TYPE_MENU,
212 .minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
213 .maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_1,
214 .default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
217 .id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
218 .type = V4L2_CTRL_TYPE_INTEGER,
225 .id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
226 .type = V4L2_CTRL_TYPE_INTEGER,
233 .id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
234 .type = V4L2_CTRL_TYPE_BOOLEAN,
241 .id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
242 .type = V4L2_CTRL_TYPE_INTEGER,
246 .default_value = 30000,
249 .id = V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING,
250 .type = V4L2_CTRL_TYPE_BOOLEAN,
257 .id = V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE,
258 .type = V4L2_CTRL_TYPE_MENU,
259 .name = "Force frame type",
260 .minimum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
261 .maximum = V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_NOT_CODED,
263 V4L2_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE_DISABLED,
267 * This hardware does not support features provided by controls
268 * below, but they are required for compatibility with certain
269 * userspace software.
272 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
273 .type = V4L2_CTRL_TYPE_INTEGER,
274 .name = "Rate Control Reaction Coeff.",
276 .maximum = (1 << 16) - 1,
281 .id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
282 .type = V4L2_CTRL_TYPE_MENU,
283 .minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
284 .maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
285 .default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
289 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
290 .type = V4L2_CTRL_TYPE_BOOLEAN,
291 .name = "Fixed Target Bit Enable",
299 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
300 .type = V4L2_CTRL_TYPE_INTEGER,
307 .id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
308 .type = V4L2_CTRL_TYPE_INTEGER,
316 static inline const void *get_ctrl_ptr(struct rk3288_vpu_ctx *ctx, unsigned id)
318 struct v4l2_ctrl *ctrl = ctx->ctrls[id];
320 return ctrl->p_cur.p;
323 static const char *const *rk3288_vpu_enc_get_menu(u32 id)
325 static const char *const vpu_video_frame_skip[] = {
331 static const char *const vpu_video_force_frame[] = {
339 case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
340 return vpu_video_frame_skip;
341 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
342 return vpu_video_force_frame;
348 static int vidioc_querycap(struct file *file, void *priv,
349 struct v4l2_capability *cap)
351 struct rk3288_vpu_dev *dev = video_drvdata(file);
355 strlcpy(cap->driver, RK3288_VPU_ENC_NAME, sizeof(cap->driver));
356 strlcpy(cap->card, dev->pdev->name, sizeof(cap->card));
357 strlcpy(cap->bus_info, "platform:" RK3288_VPU_NAME,
358 sizeof(cap->bus_info));
361 * This is only a mem-to-mem video device. The capture and output
362 * device capability flags are left only for backward compatibility
363 * and are scheduled for removal.
365 cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
366 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
373 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool out)
375 struct rk3288_vpu_fmt *fmt;
380 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
381 if (out && formats[i].codec_mode != RK_VPU_CODEC_NONE)
383 else if (!out && formats[i].codec_mode == RK_VPU_CODEC_NONE)
388 strlcpy(f->description, fmt->name,
389 sizeof(f->description));
390 f->pixelformat = fmt->fourcc;
393 if (formats[i].codec_mode != RK_VPU_CODEC_NONE)
394 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
409 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
410 struct v4l2_fmtdesc *f)
412 return vidioc_enum_fmt(f, false);
415 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *priv,
416 struct v4l2_fmtdesc *f)
418 return vidioc_enum_fmt(f, true);
421 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
423 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
427 vpu_debug(4, "f->type = %d\n", f->type);
430 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
431 f->fmt.pix_mp = ctx->dst_fmt;
434 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
435 f->fmt.pix_mp = ctx->src_fmt;
439 vpu_err("invalid buf type\n");
448 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
450 struct rk3288_vpu_fmt *fmt;
451 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
457 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
458 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
460 fmt = find_format(f, true);
462 vpu_err("failed to try capture format\n");
466 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
467 vpu_err("must be set encoding output size\n");
471 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
474 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
475 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
477 fmt = find_format(f, false);
479 vpu_err("failed to try output format\n");
483 if (fmt->num_planes != pix_fmt_mp->num_planes) {
484 vpu_err("plane number mismatches on output format\n");
488 /* Limit to hardware min/max. */
489 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
490 RK3288_ENC_MIN_WIDTH, RK3288_ENC_MAX_WIDTH);
491 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
492 RK3288_ENC_MIN_HEIGHT, RK3288_ENC_MAX_HEIGHT);
493 /* Round up to macroblocks. */
494 pix_fmt_mp->width = round_up(pix_fmt_mp->width, MB_DIM);
495 pix_fmt_mp->height = round_up(pix_fmt_mp->height, MB_DIM);
499 vpu_err("invalid buf type\n");
508 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
510 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
511 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
512 unsigned int mb_width, mb_height;
513 struct rk3288_vpu_fmt *fmt;
519 /* Change not allowed if any queue is streaming. */
520 if (vb2_is_streaming(&ctx->vq_src) || vb2_is_streaming(&ctx->vq_dst)) {
526 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
528 * Pixel format change is not allowed when the other queue has
531 if (vb2_is_busy(&ctx->vq_src)
532 && pix_fmt_mp->pixelformat != ctx->dst_fmt.pixelformat) {
537 ret = vidioc_try_fmt(file, priv, f);
541 ctx->vpu_dst_fmt = find_format(f, true);
542 ctx->dst_fmt = *pix_fmt_mp;
545 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
547 * Pixel format change is not allowed when the other queue has
550 if (vb2_is_busy(&ctx->vq_dst)
551 && pix_fmt_mp->pixelformat != ctx->src_fmt.pixelformat) {
556 ret = vidioc_try_fmt(file, priv, f);
560 fmt = find_format(f, false);
561 ctx->vpu_src_fmt = fmt;
563 mb_width = MB_WIDTH(pix_fmt_mp->width);
564 mb_height = MB_HEIGHT(pix_fmt_mp->height);
566 vpu_debug(0, "OUTPUT codec mode: %d\n", fmt->codec_mode);
567 vpu_debug(0, "fmt - w: %d, h: %d, mb - w: %d, h: %d\n",
568 pix_fmt_mp->width, pix_fmt_mp->height,
569 mb_width, mb_height);
571 for (i = 0; i < fmt->num_planes; ++i) {
572 pix_fmt_mp->plane_fmt[i].bytesperline =
573 mb_width * MB_DIM * fmt->depth[i] / 8;
574 pix_fmt_mp->plane_fmt[i].sizeimage =
575 pix_fmt_mp->plane_fmt[i].bytesperline
576 * mb_height * MB_DIM;
578 * All of multiplanar formats we support have chroma
579 * planes subsampled by 2.
582 pix_fmt_mp->plane_fmt[i].sizeimage /= 2;
585 /* Reset crop rectangle. */
586 ctx->src_crop.width = pix_fmt_mp->width;
587 ctx->src_crop.height = pix_fmt_mp->height;
589 ctx->src_fmt = *pix_fmt_mp;
593 vpu_err("invalid buf type\n");
603 static int vidioc_reqbufs(struct file *file, void *priv,
604 struct v4l2_requestbuffers *reqbufs)
606 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
611 switch (reqbufs->type) {
612 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
615 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
617 vpu_err("error in vb2_reqbufs() for E(D)\n");
622 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
623 vpu_debug(4, "memory type %d\n", reqbufs->memory);
625 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
627 vpu_err("error in vb2_reqbufs() for E(S)\n");
633 vpu_err("invalid buf type\n");
644 static int vidioc_querybuf(struct file *file, void *priv,
645 struct v4l2_buffer *buf)
647 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
653 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
654 ret = vb2_querybuf(&ctx->vq_dst, buf);
656 vpu_err("error in vb2_querybuf() for E(D)\n");
660 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
663 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
664 ret = vb2_querybuf(&ctx->vq_src, buf);
666 vpu_err("error in vb2_querybuf() for E(S)\n");
672 vpu_err("invalid buf type\n");
683 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
685 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
691 for (i = 0; i < buf->length; i++) {
692 vpu_debug(4, "plane[%d]->length %d bytesused %d\n",
693 i, buf->m.planes[i].length,
694 buf->m.planes[i].bytesused);
698 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
699 ret = vb2_qbuf(&ctx->vq_src, buf);
700 vpu_debug(4, "vb2_qbuf return %d\n", ret);
703 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
704 ret = vb2_qbuf(&ctx->vq_dst, buf);
705 vpu_debug(4, "vb2_qbuf return %d\n", ret);
717 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
719 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
725 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
726 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
729 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
730 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
742 static int vidioc_expbuf(struct file *file, void *priv,
743 struct v4l2_exportbuffer *eb)
745 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
751 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
752 ret = vb2_expbuf(&ctx->vq_src, eb);
755 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
756 ret = vb2_expbuf(&ctx->vq_dst, eb);
768 static int vidioc_streamon(struct file *file, void *priv,
769 enum v4l2_buf_type type)
771 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
777 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
778 ret = vb2_streamon(&ctx->vq_src, type);
781 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
782 ret = vb2_streamon(&ctx->vq_dst, type);
794 static int vidioc_streamoff(struct file *file, void *priv,
795 enum v4l2_buf_type type)
797 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
803 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
804 ret = vb2_streamoff(&ctx->vq_src, type);
807 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
808 ret = vb2_streamoff(&ctx->vq_dst, type);
820 static int rk3288_vpu_enc_s_ctrl(struct v4l2_ctrl *ctrl)
822 struct rk3288_vpu_ctx *ctx = ctrl_to_ctx(ctrl);
823 struct rk3288_vpu_dev *dev = ctx->dev;
828 vpu_debug(4, "ctrl id %d\n", ctrl->id);
831 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
832 case V4L2_CID_MPEG_VIDEO_BITRATE:
833 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
834 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
835 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
836 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
837 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
838 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
839 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
840 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
841 case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:
842 case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
843 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
844 case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
845 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
846 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
847 case V4L2_CID_MPEG_MFC51_VIDEO_FORCE_FRAME_TYPE:
848 /* Ignore these controls for now. (FIXME?) */
851 case V4L2_CID_PRIVATE_RK3288_HEADER:
852 case V4L2_CID_PRIVATE_RK3288_REG_PARAMS:
853 case V4L2_CID_PRIVATE_RK3288_HW_PARAMS:
854 /* Nothing to do here. The control is used directly. */
858 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
859 ctrl->id, ctrl->val);
868 static int rk3288_vpu_enc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
870 struct rk3288_vpu_ctx *ctx = ctrl_to_ctx(ctrl);
871 struct rk3288_vpu_dev *dev = ctx->dev;
876 vpu_debug(4, "ctrl id %d\n", ctrl->id);
879 case V4L2_CID_PRIVATE_RK3288_RET_PARAMS:
880 memcpy(ctrl->p_new.p, ctx->run.priv_dst.cpu,
881 RK3288_RET_PARAMS_SIZE);
885 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
886 ctrl->id, ctrl->val);
895 static const struct v4l2_ctrl_ops rk3288_vpu_enc_ctrl_ops = {
896 .s_ctrl = rk3288_vpu_enc_s_ctrl,
897 .g_volatile_ctrl = rk3288_vpu_enc_g_volatile_ctrl,
900 static int vidioc_cropcap(struct file *file, void *priv,
901 struct v4l2_cropcap *cap)
903 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
904 struct v4l2_pix_format_mplane *fmt = &ctx->src_fmt;
909 /* Crop only supported on source. */
910 if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
915 cap->bounds.left = 0;
917 cap->bounds.width = fmt->width;
918 cap->bounds.height = fmt->height;
919 cap->defrect = cap->bounds;
920 cap->pixelaspect.numerator = 1;
921 cap->pixelaspect.denominator = 1;
929 static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
931 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
936 /* Crop only supported on source. */
937 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
942 crop->c = ctx->src_crop;
950 static int vidioc_s_crop(struct file *file, void *priv,
951 const struct v4l2_crop *crop)
953 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
954 struct v4l2_pix_format_mplane *fmt = &ctx->src_fmt;
955 const struct v4l2_rect *rect = &crop->c;
960 /* Crop only supported on source. */
961 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
966 /* Change not allowed if the queue is streaming. */
967 if (vb2_is_streaming(&ctx->vq_src)) {
972 /* We do not support offsets. */
973 if (rect->left != 0 || rect->top != 0)
976 /* We can crop only inside right- or bottom-most macroblocks. */
977 if (round_up(rect->width, MB_DIM) != fmt->width
978 || round_up(rect->height, MB_DIM) != fmt->height)
981 /* We support widths aligned to 4 pixels and arbitrary heights. */
982 ctx->src_crop.width = round_up(rect->width, 4);
983 ctx->src_crop.height = rect->height;
990 /* Default to full frame for incorrect settings. */
991 ctx->src_crop.width = fmt->width;
992 ctx->src_crop.height = fmt->height;
1000 static const struct v4l2_ioctl_ops rk3288_vpu_enc_ioctl_ops = {
1001 .vidioc_querycap = vidioc_querycap,
1002 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1003 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1004 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1005 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1006 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1007 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1008 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1009 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1010 .vidioc_reqbufs = vidioc_reqbufs,
1011 .vidioc_querybuf = vidioc_querybuf,
1012 .vidioc_qbuf = vidioc_qbuf,
1013 .vidioc_dqbuf = vidioc_dqbuf,
1014 .vidioc_expbuf = vidioc_expbuf,
1015 .vidioc_streamon = vidioc_streamon,
1016 .vidioc_streamoff = vidioc_streamoff,
1017 .vidioc_cropcap = vidioc_cropcap,
1018 .vidioc_g_crop = vidioc_g_crop,
1019 .vidioc_s_crop = vidioc_s_crop,
1022 static int rk3288_vpu_queue_setup(struct vb2_queue *vq,
1023 const struct v4l2_format *fmt,
1024 unsigned int *buf_count,
1025 unsigned int *plane_count,
1026 unsigned int psize[], void *allocators[])
1028 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1035 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1036 *plane_count = ctx->vpu_dst_fmt->num_planes;
1041 if (*buf_count > VIDEO_MAX_FRAME)
1042 *buf_count = VIDEO_MAX_FRAME;
1044 psize[0] = ctx->dst_fmt.plane_fmt[0].sizeimage;
1045 /* Kernel mapping necessary for bitstream post processing. */
1046 allocators[0] = ctx->dev->alloc_ctx_vm;
1047 vpu_debug(0, "capture psize[%d]: %d\n", 0, psize[0]);
1050 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1051 *plane_count = ctx->vpu_src_fmt->num_planes;
1056 if (*buf_count > VIDEO_MAX_FRAME)
1057 *buf_count = VIDEO_MAX_FRAME;
1059 for (i = 0; i < ctx->vpu_src_fmt->num_planes; ++i) {
1060 psize[i] = ctx->src_fmt.plane_fmt[i].sizeimage;
1061 vpu_debug(0, "output psize[%d]: %d\n", i, psize[i]);
1062 allocators[i] = ctx->dev->alloc_ctx;
1067 vpu_err("invalid queue type: %d\n", vq->type);
1076 static int rk3288_vpu_buf_prepare(struct vb2_buffer *vb)
1078 struct vb2_queue *vq = vb->vb2_queue;
1079 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1086 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1087 vpu_debug(4, "plane size: %ld, dst size: %d\n",
1088 vb2_plane_size(vb, 0),
1089 ctx->dst_fmt.plane_fmt[0].sizeimage);
1091 if (vb2_plane_size(vb, 0)
1092 < ctx->dst_fmt.plane_fmt[0].sizeimage) {
1093 vpu_err("plane size is too small for capture\n");
1098 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1099 for (i = 0; i < ctx->vpu_src_fmt->num_planes; ++i) {
1100 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n", i,
1101 vb2_plane_size(vb, i),
1102 ctx->src_fmt.plane_fmt[i].sizeimage);
1104 if (vb2_plane_size(vb, i)
1105 < ctx->src_fmt.plane_fmt[i].sizeimage) {
1106 vpu_err("size of plane %d is too small for output\n",
1112 if (i != ctx->vpu_src_fmt->num_planes)
1117 vpu_err("invalid queue type: %d\n", vq->type);
1126 static void rk3288_vpu_buf_finish(struct vb2_buffer *vb)
1128 struct vb2_queue *vq = vb->vb2_queue;
1129 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1133 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
1134 && vb->state == VB2_BUF_STATE_DONE
1135 && ctx->vpu_dst_fmt->fourcc == V4L2_PIX_FMT_VP8) {
1136 struct rk3288_vpu_buf *buf;
1138 buf = vb_to_buf(vb);
1139 rk3288_vpu_vp8e_assemble_bitstream(ctx, buf);
1145 static int rk3288_vpu_start_streaming(struct vb2_queue *q, unsigned int count)
1148 struct rk3288_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
1149 struct rk3288_vpu_dev *dev = ctx->dev;
1154 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1155 ret = rk3288_vpu_init(ctx);
1157 vpu_err("rk3288_vpu_init failed\n");
1161 ready = vb2_is_streaming(&ctx->vq_src);
1162 } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1163 ready = vb2_is_streaming(&ctx->vq_dst);
1167 rk3288_vpu_try_context(dev, ctx);
1174 static void rk3288_vpu_stop_streaming(struct vb2_queue *q)
1176 unsigned long flags;
1177 struct rk3288_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
1178 struct rk3288_vpu_dev *dev = ctx->dev;
1179 struct rk3288_vpu_buf *b;
1185 spin_lock_irqsave(&dev->irqlock, flags);
1187 list_del_init(&ctx->list);
1190 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1191 list_splice_init(&ctx->dst_queue, &queue);
1194 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1195 list_splice_init(&ctx->src_queue, &queue);
1202 spin_unlock_irqrestore(&dev->irqlock, flags);
1204 wait_event(dev->run_wq, dev->current_ctx != ctx);
1206 while (!list_empty(&queue)) {
1207 b = list_first_entry(&queue, struct rk3288_vpu_buf, list);
1208 for (i = 0; i < b->b.num_planes; i++)
1209 vb2_set_plane_payload(&b->b, i, 0);
1210 vb2_buffer_done(&b->b, VB2_BUF_STATE_ERROR);
1214 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1215 rk3288_vpu_deinit(ctx);
1220 static void rk3288_vpu_buf_queue(struct vb2_buffer *vb)
1222 struct vb2_queue *vq = vb->vb2_queue;
1223 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1224 struct rk3288_vpu_dev *dev = ctx->dev;
1225 struct rk3288_vpu_buf *vpu_buf;
1226 unsigned long flags;
1231 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1232 vpu_buf = vb_to_buf(vb);
1234 /* Mark destination as available for use by VPU */
1235 spin_lock_irqsave(&dev->irqlock, flags);
1237 list_add_tail(&vpu_buf->list, &ctx->dst_queue);
1239 spin_unlock_irqrestore(&dev->irqlock, flags);
1242 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1243 vpu_buf = vb_to_buf(vb);
1245 spin_lock_irqsave(&dev->irqlock, flags);
1247 list_add_tail(&vpu_buf->list, &ctx->src_queue);
1249 spin_unlock_irqrestore(&dev->irqlock, flags);
1253 vpu_err("unsupported buffer type (%d)\n", vq->type);
1256 if (vb2_is_streaming(&ctx->vq_src) && vb2_is_streaming(&ctx->vq_dst))
1257 rk3288_vpu_try_context(dev, ctx);
1262 static struct vb2_ops rk3288_vpu_enc_qops = {
1263 .queue_setup = rk3288_vpu_queue_setup,
1264 .wait_prepare = vb2_ops_wait_prepare,
1265 .wait_finish = vb2_ops_wait_finish,
1266 .buf_prepare = rk3288_vpu_buf_prepare,
1267 .buf_finish = rk3288_vpu_buf_finish,
1268 .start_streaming = rk3288_vpu_start_streaming,
1269 .stop_streaming = rk3288_vpu_stop_streaming,
1270 .buf_queue = rk3288_vpu_buf_queue,
1273 struct vb2_ops *get_enc_queue_ops(void)
1275 return &rk3288_vpu_enc_qops;
1278 const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
1280 return &rk3288_vpu_enc_ioctl_ops;
1283 static void rk3288_vpu_enc_prepare_run(struct rk3288_vpu_ctx *ctx)
1285 struct vb2_buffer *vb2_src = &ctx->run.src->b;
1286 unsigned config_store = vb2_src->v4l2_buf.config_store;
1288 v4l2_ctrl_apply_store(&ctx->ctrl_handler, config_store);
1290 memcpy(ctx->run.dst->vp8e.header,
1291 get_ctrl_ptr(ctx, RK3288_VPU_ENC_CTRL_HEADER),
1292 RK3288_HEADER_SIZE);
1293 ctx->run.vp8e.reg_params = get_ctrl_ptr(ctx,
1294 RK3288_VPU_ENC_CTRL_REG_PARAMS);
1295 memcpy(ctx->run.priv_src.cpu,
1296 get_ctrl_ptr(ctx, RK3288_VPU_ENC_CTRL_HW_PARAMS),
1297 RK3288_HW_PARAMS_SIZE);
1300 static const struct rk3288_vpu_run_ops rk3288_vpu_enc_run_ops = {
1301 .prepare_run = rk3288_vpu_enc_prepare_run,
1304 int rk3288_vpu_enc_init(struct rk3288_vpu_ctx *ctx)
1306 struct rk3288_vpu_dev *vpu = ctx->dev;
1307 struct v4l2_format f;
1310 f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_ENC;
1311 ctx->vpu_src_fmt = find_format(&f, false);
1312 f.fmt.pix_mp.pixelformat = DEF_DST_FMT_ENC;
1313 ctx->vpu_dst_fmt = find_format(&f, true);
1315 ret = rk3288_vpu_aux_buf_alloc(vpu, &ctx->run.priv_src,
1316 RK3288_HW_PARAMS_SIZE);
1318 vpu_err("Failed to allocate private source buffer.\n");
1323 ret = rk3288_vpu_aux_buf_alloc(vpu, &ctx->run.priv_dst,
1324 RK3288_RET_PARAMS_SIZE);
1326 vpu_err("Failed to allocate private destination buffer.\n");
1330 ret = rk3288_vpu_ctrls_setup(ctx, &rk3288_vpu_enc_ctrl_ops,
1331 controls, ARRAY_SIZE(controls),
1332 rk3288_vpu_enc_get_menu);
1334 vpu_err("Failed to set up controls\n");
1338 ctx->run_ops = &rk3288_vpu_enc_run_ops;
1343 rk3288_vpu_aux_buf_free(vpu, &ctx->run.priv_dst);
1345 rk3288_vpu_aux_buf_free(vpu, &ctx->run.priv_src);
1350 void rk3288_vpu_enc_exit(struct rk3288_vpu_ctx *ctx)
1352 struct rk3288_vpu_dev *vpu = ctx->dev;
1354 rk3288_vpu_ctrls_delete(ctx);
1356 rk3288_vpu_aux_buf_free(vpu, &ctx->run.priv_dst);
1357 rk3288_vpu_aux_buf_free(vpu, &ctx->run.priv_src);