1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Coda multi-standard codec IP
5 * Copyright (C) 2012 Vista Silicon S.L.
10 #include <linux/clk.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/gcd.h>
15 #include <linux/genalloc.h>
16 #include <linux/idr.h>
17 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/kfifo.h>
21 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
28 #include <linux/ratelimit.h>
29 #include <linux/reset.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-event.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-mem2mem.h>
36 #include <media/videobuf2-v4l2.h>
37 #include <media/videobuf2-dma-contig.h>
38 #include <media/videobuf2-vmalloc.h>
43 #define CODA_NAME "coda"
45 #define CODADX6_MAX_INSTANCES 4
46 #define CODA_MAX_FORMATS 4
48 #define CODA_ISRAM_SIZE (2048 * 2)
53 #define S_ALIGN 1 /* multiple of 2 */
54 #define W_ALIGN 1 /* multiple of 2 */
55 #define H_ALIGN 1 /* multiple of 2 */
57 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
60 module_param(coda_debug, int, 0644);
61 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
63 static int disable_tiling;
64 module_param(disable_tiling, int, 0644);
65 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
67 static int disable_vdoa;
68 module_param(disable_vdoa, int, 0644);
69 MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
71 static int enable_bwb = 0;
72 module_param(enable_bwb, int, 0644);
73 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
75 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
77 v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
78 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
79 writel(data, dev->regs_base + reg);
82 unsigned int coda_read(struct coda_dev *dev, u32 reg)
86 data = readl(dev->regs_base + reg);
87 v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
88 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
92 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
93 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
95 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
98 switch (q_data->fourcc) {
99 case V4L2_PIX_FMT_YUYV:
100 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
101 case V4L2_PIX_FMT_NV12:
102 case V4L2_PIX_FMT_YUV420:
104 base_cb = base_y + q_data->bytesperline * q_data->height;
105 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
107 case V4L2_PIX_FMT_YVU420:
108 /* Switch Cb and Cr for YVU420 format */
109 base_cr = base_y + q_data->bytesperline * q_data->height;
110 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
112 case V4L2_PIX_FMT_YUV422P:
113 base_cb = base_y + q_data->bytesperline * q_data->height;
114 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
117 coda_write(ctx->dev, base_y, reg_y);
118 coda_write(ctx->dev, base_cb, reg_y + 4);
119 coda_write(ctx->dev, base_cr, reg_y + 8);
122 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
123 { mode, src_fourcc, dst_fourcc, max_w, max_h }
126 * Arrays of codecs supported by each given version of Coda:
131 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
133 static const struct coda_codec codadx6_codecs[] = {
134 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
135 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
138 static const struct coda_codec codahx4_codecs[] = {
139 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
140 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
141 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
142 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
145 static const struct coda_codec coda7_codecs[] = {
146 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
147 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
148 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
149 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
150 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
151 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
152 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
155 static const struct coda_codec coda9_codecs[] = {
156 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
157 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
158 CODA_CODEC(CODA9_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
159 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
160 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
161 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
162 CODA_CODEC(CODA9_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
165 struct coda_video_device {
167 enum coda_inst_type type;
168 const struct coda_context_ops *ops;
170 u32 src_formats[CODA_MAX_FORMATS];
171 u32 dst_formats[CODA_MAX_FORMATS];
174 static const struct coda_video_device coda_bit_encoder = {
175 .name = "coda-video-encoder",
176 .type = CODA_INST_ENCODER,
177 .ops = &coda_bit_encode_ops,
189 static const struct coda_video_device coda_bit_jpeg_encoder = {
190 .name = "coda-jpeg-encoder",
191 .type = CODA_INST_ENCODER,
192 .ops = &coda_bit_encode_ops,
197 V4L2_PIX_FMT_YUV422P,
204 static const struct coda_video_device coda_bit_decoder = {
205 .name = "coda-video-decoder",
206 .type = CODA_INST_DECODER,
207 .ops = &coda_bit_decode_ops,
218 * If V4L2_PIX_FMT_YUYV should be default,
219 * set_default_params() must be adjusted.
225 static const struct coda_video_device coda_bit_jpeg_decoder = {
226 .name = "coda-jpeg-decoder",
227 .type = CODA_INST_DECODER,
228 .ops = &coda_bit_decode_ops,
236 V4L2_PIX_FMT_YUV422P,
240 static const struct coda_video_device coda9_jpeg_encoder = {
241 .name = "coda-jpeg-encoder",
242 .type = CODA_INST_ENCODER,
243 .ops = &coda9_jpeg_encode_ops,
249 V4L2_PIX_FMT_YUV422P,
256 static const struct coda_video_device coda9_jpeg_decoder = {
257 .name = "coda-jpeg-decoder",
258 .type = CODA_INST_DECODER,
259 .ops = &coda9_jpeg_decode_ops,
268 V4L2_PIX_FMT_YUV422P,
272 static const struct coda_video_device *codadx6_video_devices[] = {
276 static const struct coda_video_device *codahx4_video_devices[] = {
281 static const struct coda_video_device *coda7_video_devices[] = {
282 &coda_bit_jpeg_encoder,
283 &coda_bit_jpeg_decoder,
288 static const struct coda_video_device *coda9_video_devices[] = {
296 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
299 static u32 coda_format_normalize_yuv(u32 fourcc)
302 case V4L2_PIX_FMT_NV12:
303 case V4L2_PIX_FMT_YUV420:
304 case V4L2_PIX_FMT_YVU420:
305 case V4L2_PIX_FMT_YUV422P:
306 case V4L2_PIX_FMT_YUYV:
307 return V4L2_PIX_FMT_YUV420;
313 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
314 int src_fourcc, int dst_fourcc)
316 const struct coda_codec *codecs = dev->devtype->codecs;
317 int num_codecs = dev->devtype->num_codecs;
320 src_fourcc = coda_format_normalize_yuv(src_fourcc);
321 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
322 if (src_fourcc == dst_fourcc)
325 for (k = 0; k < num_codecs; k++) {
326 if (codecs[k].src_fourcc == src_fourcc &&
327 codecs[k].dst_fourcc == dst_fourcc)
337 static void coda_get_max_dimensions(struct coda_dev *dev,
338 const struct coda_codec *codec,
339 int *max_w, int *max_h)
341 const struct coda_codec *codecs = dev->devtype->codecs;
342 int num_codecs = dev->devtype->num_codecs;
350 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
351 w = max(w, codecs[k].max_w);
352 h = max(h, codecs[k].max_h);
362 static const struct coda_video_device *to_coda_video_device(struct video_device
365 struct coda_dev *dev = video_get_drvdata(vdev);
366 unsigned int i = vdev - dev->vfd;
368 if (i >= dev->devtype->num_vdevs)
371 return dev->devtype->vdevs[i];
374 const char *coda_product_name(int product)
388 snprintf(buf, sizeof(buf), "(0x%04x)", product);
393 static struct vdoa_data *coda_get_vdoa_data(void)
395 struct device_node *vdoa_node;
396 struct platform_device *vdoa_pdev;
397 struct vdoa_data *vdoa_data = NULL;
399 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
403 vdoa_pdev = of_find_device_by_node(vdoa_node);
407 vdoa_data = platform_get_drvdata(vdoa_pdev);
409 vdoa_data = ERR_PTR(-EPROBE_DEFER);
412 of_node_put(vdoa_node);
418 * V4L2 ioctl() operations.
420 static int coda_querycap(struct file *file, void *priv,
421 struct v4l2_capability *cap)
423 struct coda_ctx *ctx = fh_to_ctx(priv);
425 strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
426 strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
428 strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
432 static const u32 coda_formats_420[CODA_MAX_FORMATS] = {
438 static int coda_enum_fmt(struct file *file, void *priv,
439 struct v4l2_fmtdesc *f)
441 struct video_device *vdev = video_devdata(file);
442 const struct coda_video_device *cvd = to_coda_video_device(vdev);
443 struct coda_ctx *ctx = fh_to_ctx(priv);
446 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
447 formats = cvd->src_formats;
448 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
449 struct coda_q_data *q_data_src;
450 struct vb2_queue *src_vq;
452 formats = cvd->dst_formats;
455 * If the source format is already fixed, only allow the same
456 * chroma subsampling.
458 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
459 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
460 V4L2_BUF_TYPE_VIDEO_OUTPUT);
461 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
462 vb2_is_streaming(src_vq)) {
463 if (ctx->params.jpeg_chroma_subsampling ==
464 V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
465 formats = coda_formats_420;
466 } else if (ctx->params.jpeg_chroma_subsampling ==
467 V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
468 f->pixelformat = V4L2_PIX_FMT_YUV422P;
469 return f->index ? -EINVAL : 0;
476 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
479 /* Skip YUYV if the vdoa is not available */
480 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
481 formats[f->index] == V4L2_PIX_FMT_YUYV)
484 f->pixelformat = formats[f->index];
489 static int coda_g_fmt(struct file *file, void *priv,
490 struct v4l2_format *f)
492 struct coda_q_data *q_data;
493 struct coda_ctx *ctx = fh_to_ctx(priv);
495 q_data = get_q_data(ctx, f->type);
499 f->fmt.pix.field = V4L2_FIELD_NONE;
500 f->fmt.pix.pixelformat = q_data->fourcc;
501 f->fmt.pix.width = q_data->width;
502 f->fmt.pix.height = q_data->height;
503 f->fmt.pix.bytesperline = q_data->bytesperline;
505 f->fmt.pix.sizeimage = q_data->sizeimage;
506 f->fmt.pix.colorspace = ctx->colorspace;
507 f->fmt.pix.xfer_func = ctx->xfer_func;
508 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
509 f->fmt.pix.quantization = ctx->quantization;
514 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
516 struct coda_q_data *q_data;
520 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
521 formats = ctx->cvd->src_formats;
522 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
523 formats = ctx->cvd->dst_formats;
527 for (i = 0; i < CODA_MAX_FORMATS; i++) {
528 /* Skip YUYV if the vdoa is not available */
529 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
530 formats[i] == V4L2_PIX_FMT_YUYV)
533 if (formats[i] == f->fmt.pix.pixelformat) {
534 f->fmt.pix.pixelformat = formats[i];
539 /* Fall back to currently set pixelformat */
540 q_data = get_q_data(ctx, f->type);
541 f->fmt.pix.pixelformat = q_data->fourcc;
546 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
551 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
562 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
563 f->fmt.pix.height, f->fmt.pix.pixelformat);
573 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
574 u32 width, u32 height)
577 * This is a rough estimate for sensible compressed buffer
578 * sizes (between 1 and 16 bits per pixel). This could be
579 * improved by better format specific worst case estimates.
581 return round_up(clamp(sizeimage, width * height / 8,
582 width * height * 2), PAGE_SIZE);
585 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
586 struct v4l2_format *f)
588 struct coda_dev *dev = ctx->dev;
589 unsigned int max_w, max_h;
590 enum v4l2_field field;
592 field = f->fmt.pix.field;
593 if (field == V4L2_FIELD_ANY)
594 field = V4L2_FIELD_NONE;
595 else if (V4L2_FIELD_NONE != field)
598 /* V4L2 specification suggests the driver corrects the format struct
599 * if any of the dimensions is unsupported */
600 f->fmt.pix.field = field;
602 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
603 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
604 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
607 switch (f->fmt.pix.pixelformat) {
608 case V4L2_PIX_FMT_NV12:
609 case V4L2_PIX_FMT_YUV420:
610 case V4L2_PIX_FMT_YVU420:
612 * Frame stride must be at least multiple of 8,
613 * but multiple of 16 for h.264 or JPEG 4:2:x
615 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
616 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
617 f->fmt.pix.height * 3 / 2;
619 case V4L2_PIX_FMT_YUYV:
620 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
621 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
624 case V4L2_PIX_FMT_YUV422P:
625 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
626 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
627 f->fmt.pix.height * 2;
629 case V4L2_PIX_FMT_JPEG:
630 case V4L2_PIX_FMT_H264:
631 case V4L2_PIX_FMT_MPEG4:
632 case V4L2_PIX_FMT_MPEG2:
633 f->fmt.pix.bytesperline = 0;
634 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
635 f->fmt.pix.sizeimage,
646 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
647 struct v4l2_format *f)
649 struct coda_ctx *ctx = fh_to_ctx(priv);
650 const struct coda_q_data *q_data_src;
651 const struct coda_codec *codec;
652 struct vb2_queue *src_vq;
656 ret = coda_try_pixelformat(ctx, f);
660 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
663 * If the source format is already fixed, only allow the same output
664 * resolution. When decoding JPEG images, we also have to make sure to
665 * use the same chroma subsampling.
667 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
668 if (vb2_is_streaming(src_vq)) {
669 f->fmt.pix.width = q_data_src->width;
670 f->fmt.pix.height = q_data_src->height;
672 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
673 if (ctx->params.jpeg_chroma_subsampling ==
674 V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
675 f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P)
676 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
677 else if (ctx->params.jpeg_chroma_subsampling ==
678 V4L2_JPEG_CHROMA_SUBSAMPLING_422)
679 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
683 f->fmt.pix.colorspace = ctx->colorspace;
684 f->fmt.pix.xfer_func = ctx->xfer_func;
685 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
686 f->fmt.pix.quantization = ctx->quantization;
688 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
689 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
690 f->fmt.pix.pixelformat);
694 ret = coda_try_fmt(ctx, codec, f);
698 /* The decoders always write complete macroblocks or MCUs */
699 if (ctx->inst_type == CODA_INST_DECODER) {
700 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
701 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
702 if (codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
703 f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P) {
704 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
705 f->fmt.pix.height * 2;
707 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
708 f->fmt.pix.height * 3 / 2;
711 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
715 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
719 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
720 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
728 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
730 enum v4l2_colorspace colorspace;
732 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
733 colorspace = V4L2_COLORSPACE_JPEG;
734 else if (fmt->width <= 720 && fmt->height <= 576)
735 colorspace = V4L2_COLORSPACE_SMPTE170M;
737 colorspace = V4L2_COLORSPACE_REC709;
739 fmt->colorspace = colorspace;
740 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
741 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
742 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
745 static int coda_try_fmt_vid_out(struct file *file, void *priv,
746 struct v4l2_format *f)
748 struct coda_ctx *ctx = fh_to_ctx(priv);
749 struct coda_dev *dev = ctx->dev;
750 const struct coda_q_data *q_data_dst;
751 const struct coda_codec *codec;
754 ret = coda_try_pixelformat(ctx, f);
758 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
759 coda_set_default_colorspace(&f->fmt.pix);
761 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
762 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
764 return coda_try_fmt(ctx, codec, f);
767 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
770 struct coda_q_data *q_data;
771 struct vb2_queue *vq;
773 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
777 q_data = get_q_data(ctx, f->type);
781 if (vb2_is_busy(vq)) {
782 v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
783 __func__, v4l2_type_names[f->type], vq->num_buffers);
787 q_data->fourcc = f->fmt.pix.pixelformat;
788 q_data->width = f->fmt.pix.width;
789 q_data->height = f->fmt.pix.height;
790 q_data->bytesperline = f->fmt.pix.bytesperline;
791 q_data->sizeimage = f->fmt.pix.sizeimage;
795 q_data->rect.left = 0;
796 q_data->rect.top = 0;
797 q_data->rect.width = f->fmt.pix.width;
798 q_data->rect.height = f->fmt.pix.height;
801 switch (f->fmt.pix.pixelformat) {
802 case V4L2_PIX_FMT_YUYV:
803 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
805 case V4L2_PIX_FMT_NV12:
806 if (!disable_tiling && ctx->use_bit &&
807 ctx->dev->devtype->product == CODA_960) {
808 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
812 case V4L2_PIX_FMT_YUV420:
813 case V4L2_PIX_FMT_YVU420:
814 case V4L2_PIX_FMT_YUV422P:
815 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
821 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
822 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
824 vdoa_context_configure(ctx->vdoa,
825 round_up(f->fmt.pix.width, 16),
827 f->fmt.pix.pixelformat);
829 ctx->use_vdoa = false;
831 coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
832 v4l2_type_names[f->type], q_data->width, q_data->height,
833 (char *)&q_data->fourcc,
834 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
839 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
840 struct v4l2_format *f)
842 struct coda_ctx *ctx = fh_to_ctx(priv);
843 struct coda_q_data *q_data_src;
844 const struct coda_codec *codec;
848 ret = coda_try_fmt_vid_cap(file, priv, f);
852 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
855 r.width = q_data_src->width;
856 r.height = q_data_src->height;
858 ret = coda_s_fmt(ctx, f, &r);
862 if (ctx->inst_type != CODA_INST_ENCODER)
865 /* Setting the coded format determines the selected codec */
866 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
867 f->fmt.pix.pixelformat);
869 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
874 ctx->colorspace = f->fmt.pix.colorspace;
875 ctx->xfer_func = f->fmt.pix.xfer_func;
876 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
877 ctx->quantization = f->fmt.pix.quantization;
882 static int coda_s_fmt_vid_out(struct file *file, void *priv,
883 struct v4l2_format *f)
885 struct coda_ctx *ctx = fh_to_ctx(priv);
886 const struct coda_codec *codec;
887 struct v4l2_format f_cap;
888 struct vb2_queue *dst_vq;
891 ret = coda_try_fmt_vid_out(file, priv, f);
895 ret = coda_s_fmt(ctx, f, NULL);
899 ctx->colorspace = f->fmt.pix.colorspace;
900 ctx->xfer_func = f->fmt.pix.xfer_func;
901 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
902 ctx->quantization = f->fmt.pix.quantization;
904 if (ctx->inst_type != CODA_INST_DECODER)
907 /* Setting the coded format determines the selected codec */
908 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
909 V4L2_PIX_FMT_YUV420);
911 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
916 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
921 * Setting the capture queue format is not possible while the capture
922 * queue is still busy. This is not an error, but the user will have to
923 * make sure themselves that the capture format is set correctly before
924 * starting the output queue again.
926 if (vb2_is_busy(dst_vq))
929 memset(&f_cap, 0, sizeof(f_cap));
930 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
931 coda_g_fmt(file, priv, &f_cap);
932 f_cap.fmt.pix.width = f->fmt.pix.width;
933 f_cap.fmt.pix.height = f->fmt.pix.height;
935 return coda_s_fmt_vid_cap(file, priv, &f_cap);
938 static int coda_reqbufs(struct file *file, void *priv,
939 struct v4l2_requestbuffers *rb)
941 struct coda_ctx *ctx = fh_to_ctx(priv);
944 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
949 * Allow to allocate instance specific per-context buffers, such as
950 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
952 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
953 return ctx->ops->reqbufs(ctx, rb);
958 static int coda_qbuf(struct file *file, void *priv,
959 struct v4l2_buffer *buf)
961 struct coda_ctx *ctx = fh_to_ctx(priv);
963 if (ctx->inst_type == CODA_INST_DECODER &&
964 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
965 buf->flags &= ~V4L2_BUF_FLAG_LAST;
967 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
970 static int coda_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
972 struct coda_ctx *ctx = fh_to_ctx(priv);
975 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
977 if (ctx->inst_type == CODA_INST_DECODER &&
978 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
979 buf->flags &= ~V4L2_BUF_FLAG_LAST;
984 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
985 enum vb2_buffer_state state)
987 const struct v4l2_event eos_event = {
988 .type = V4L2_EVENT_EOS
991 if (buf->flags & V4L2_BUF_FLAG_LAST)
992 v4l2_event_queue_fh(&ctx->fh, &eos_event);
994 v4l2_m2m_buf_done(buf, state);
997 static int coda_g_selection(struct file *file, void *fh,
998 struct v4l2_selection *s)
1000 struct coda_ctx *ctx = fh_to_ctx(fh);
1001 struct coda_q_data *q_data;
1002 struct v4l2_rect r, *rsel;
1004 q_data = get_q_data(ctx, s->type);
1010 r.width = q_data->width;
1011 r.height = q_data->height;
1012 rsel = &q_data->rect;
1014 switch (s->target) {
1015 case V4L2_SEL_TGT_CROP_DEFAULT:
1016 case V4L2_SEL_TGT_CROP_BOUNDS:
1019 case V4L2_SEL_TGT_CROP:
1020 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
1021 ctx->inst_type == CODA_INST_DECODER)
1024 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1025 case V4L2_SEL_TGT_COMPOSE_PADDED:
1028 case V4L2_SEL_TGT_COMPOSE:
1029 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1030 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1031 ctx->inst_type == CODA_INST_ENCODER)
1043 static int coda_s_selection(struct file *file, void *fh,
1044 struct v4l2_selection *s)
1046 struct coda_ctx *ctx = fh_to_ctx(fh);
1047 struct coda_q_data *q_data;
1049 switch (s->target) {
1050 case V4L2_SEL_TGT_CROP:
1051 if (ctx->inst_type == CODA_INST_ENCODER &&
1052 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1053 q_data = get_q_data(ctx, s->type);
1059 s->r.width = clamp(s->r.width, 2U, q_data->width);
1060 s->r.height = clamp(s->r.height, 2U, q_data->height);
1062 if (s->flags & V4L2_SEL_FLAG_LE) {
1063 s->r.width = round_up(s->r.width, 2);
1064 s->r.height = round_up(s->r.height, 2);
1066 s->r.width = round_down(s->r.width, 2);
1067 s->r.height = round_down(s->r.height, 2);
1070 q_data->rect = s->r;
1072 coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
1073 s->r.width, s->r.height);
1078 case V4L2_SEL_TGT_NATIVE_SIZE:
1079 case V4L2_SEL_TGT_COMPOSE:
1080 return coda_g_selection(file, fh, s);
1082 /* v4l2-compliance expects this to fail for read-only targets */
1087 static int coda_try_encoder_cmd(struct file *file, void *fh,
1088 struct v4l2_encoder_cmd *ec)
1090 struct coda_ctx *ctx = fh_to_ctx(fh);
1092 if (ctx->inst_type != CODA_INST_ENCODER)
1095 return v4l2_m2m_ioctl_try_encoder_cmd(file, fh, ec);
1098 static void coda_wake_up_capture_queue(struct coda_ctx *ctx)
1100 struct vb2_queue *dst_vq;
1102 coda_dbg(1, ctx, "waking up capture queue\n");
1104 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1105 dst_vq->last_buffer_dequeued = true;
1106 wake_up(&dst_vq->done_wq);
1109 static int coda_encoder_cmd(struct file *file, void *fh,
1110 struct v4l2_encoder_cmd *ec)
1112 struct coda_ctx *ctx = fh_to_ctx(fh);
1113 struct vb2_v4l2_buffer *buf;
1116 ret = coda_try_encoder_cmd(file, fh, ec);
1120 mutex_lock(&ctx->wakeup_mutex);
1121 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1124 * If the last output buffer is still on the queue, make sure
1125 * that decoder finish_run will see the last flag and report it
1128 buf->flags |= V4L2_BUF_FLAG_LAST;
1130 /* Set the stream-end flag on this context */
1131 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1134 * If the last output buffer has already been taken from the
1135 * queue, wake up the capture queue and signal end of stream
1136 * via the -EPIPE mechanism.
1138 coda_wake_up_capture_queue(ctx);
1140 mutex_unlock(&ctx->wakeup_mutex);
1145 static int coda_try_decoder_cmd(struct file *file, void *fh,
1146 struct v4l2_decoder_cmd *dc)
1148 struct coda_ctx *ctx = fh_to_ctx(fh);
1150 if (ctx->inst_type != CODA_INST_DECODER)
1153 return v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
1156 static bool coda_mark_last_meta(struct coda_ctx *ctx)
1158 struct coda_buffer_meta *meta;
1160 coda_dbg(1, ctx, "marking last meta\n");
1162 spin_lock(&ctx->buffer_meta_lock);
1163 if (list_empty(&ctx->buffer_meta_list)) {
1164 spin_unlock(&ctx->buffer_meta_lock);
1168 meta = list_last_entry(&ctx->buffer_meta_list, struct coda_buffer_meta,
1172 spin_unlock(&ctx->buffer_meta_lock);
1176 static bool coda_mark_last_dst_buf(struct coda_ctx *ctx)
1178 struct vb2_v4l2_buffer *buf;
1179 struct vb2_buffer *dst_vb;
1180 struct vb2_queue *dst_vq;
1181 unsigned long flags;
1183 coda_dbg(1, ctx, "marking last capture buffer\n");
1185 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1186 spin_lock_irqsave(&dst_vq->done_lock, flags);
1187 if (list_empty(&dst_vq->done_list)) {
1188 spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1192 dst_vb = list_last_entry(&dst_vq->done_list, struct vb2_buffer,
1194 buf = to_vb2_v4l2_buffer(dst_vb);
1195 buf->flags |= V4L2_BUF_FLAG_LAST;
1197 spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1201 static int coda_decoder_cmd(struct file *file, void *fh,
1202 struct v4l2_decoder_cmd *dc)
1204 struct coda_ctx *ctx = fh_to_ctx(fh);
1205 struct coda_dev *dev = ctx->dev;
1206 struct vb2_v4l2_buffer *buf;
1207 struct vb2_queue *dst_vq;
1212 ret = coda_try_decoder_cmd(file, fh, dc);
1217 case V4L2_DEC_CMD_START:
1218 mutex_lock(&dev->coda_mutex);
1219 mutex_lock(&ctx->bitstream_mutex);
1220 coda_bitstream_flush(ctx);
1221 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1222 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1223 vb2_clear_last_buffer_dequeued(dst_vq);
1224 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1225 coda_fill_bitstream(ctx, NULL);
1226 mutex_unlock(&ctx->bitstream_mutex);
1227 mutex_unlock(&dev->coda_mutex);
1229 case V4L2_DEC_CMD_STOP:
1233 mutex_lock(&ctx->wakeup_mutex);
1235 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1237 coda_dbg(1, ctx, "marking last pending buffer\n");
1239 /* Mark last buffer */
1240 buf->flags |= V4L2_BUF_FLAG_LAST;
1242 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) == 0) {
1243 coda_dbg(1, ctx, "all remaining buffers queued\n");
1248 if (coda_mark_last_meta(ctx))
1253 if (!coda_mark_last_dst_buf(ctx))
1258 coda_dbg(1, ctx, "all remaining buffers queued\n");
1260 /* Set the stream-end flag on this context */
1261 coda_bit_stream_end_flag(ctx);
1263 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1267 /* If there is no buffer in flight, wake up */
1268 coda_wake_up_capture_queue(ctx);
1271 mutex_unlock(&ctx->wakeup_mutex);
1280 static int coda_enum_framesizes(struct file *file, void *fh,
1281 struct v4l2_frmsizeenum *fsize)
1283 struct coda_ctx *ctx = fh_to_ctx(fh);
1284 struct coda_q_data *q_data_dst;
1285 const struct coda_codec *codec;
1287 if (ctx->inst_type != CODA_INST_ENCODER)
1293 if (coda_format_normalize_yuv(fsize->pixel_format) ==
1294 V4L2_PIX_FMT_YUV420) {
1295 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1296 codec = coda_find_codec(ctx->dev, fsize->pixel_format,
1297 q_data_dst->fourcc);
1299 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1300 fsize->pixel_format);
1305 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1306 fsize->stepwise.min_width = MIN_W;
1307 fsize->stepwise.max_width = codec->max_w;
1308 fsize->stepwise.step_width = 1;
1309 fsize->stepwise.min_height = MIN_H;
1310 fsize->stepwise.max_height = codec->max_h;
1311 fsize->stepwise.step_height = 1;
1316 static int coda_enum_frameintervals(struct file *file, void *fh,
1317 struct v4l2_frmivalenum *f)
1319 struct coda_ctx *ctx = fh_to_ctx(fh);
1325 /* Disallow YUYV if the vdoa is not available */
1326 if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1329 for (i = 0; i < CODA_MAX_FORMATS; i++) {
1330 if (f->pixel_format == ctx->cvd->src_formats[i] ||
1331 f->pixel_format == ctx->cvd->dst_formats[i])
1334 if (i == CODA_MAX_FORMATS)
1337 f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1338 f->stepwise.min.numerator = 1;
1339 f->stepwise.min.denominator = 65535;
1340 f->stepwise.max.numerator = 65536;
1341 f->stepwise.max.denominator = 1;
1342 f->stepwise.step.numerator = 1;
1343 f->stepwise.step.denominator = 1;
1348 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1350 struct coda_ctx *ctx = fh_to_ctx(fh);
1351 struct v4l2_fract *tpf;
1353 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1356 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1357 tpf = &a->parm.output.timeperframe;
1358 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1359 tpf->numerator = 1 + (ctx->params.framerate >>
1360 CODA_FRATE_DIV_OFFSET);
1366 * Approximate timeperframe v4l2_fract with values that can be written
1367 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1369 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1371 struct v4l2_fract s = *timeperframe;
1372 struct v4l2_fract f0;
1373 struct v4l2_fract f1 = { 1, 0 };
1374 struct v4l2_fract f2 = { 0, 1 };
1375 unsigned int i, div, s_denominator;
1377 /* Lower bound is 1/65535 */
1378 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1379 timeperframe->numerator = 1;
1380 timeperframe->denominator = 65535;
1384 /* Upper bound is 65536/1 */
1385 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1386 timeperframe->numerator = 65536;
1387 timeperframe->denominator = 1;
1391 /* Reduce fraction to lowest terms */
1392 div = gcd(s.numerator, s.denominator);
1395 s.denominator /= div;
1398 if (s.numerator <= 65536 && s.denominator < 65536) {
1403 /* Find successive convergents from continued fraction expansion */
1404 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1408 /* Stop when f2 exactly equals timeperframe */
1409 if (s.numerator == 0)
1412 i = s.denominator / s.numerator;
1414 f2.numerator = f0.numerator + i * f1.numerator;
1415 f2.denominator = f0.denominator + i * f2.denominator;
1417 s_denominator = s.numerator;
1418 s.numerator = s.denominator % s.numerator;
1419 s.denominator = s_denominator;
1425 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1427 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1428 timeperframe->denominator;
1431 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1433 struct coda_ctx *ctx = fh_to_ctx(fh);
1434 struct v4l2_fract *tpf;
1436 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1439 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1440 tpf = &a->parm.output.timeperframe;
1441 coda_approximate_timeperframe(tpf);
1442 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1443 ctx->params.framerate_changed = true;
1448 static int coda_subscribe_event(struct v4l2_fh *fh,
1449 const struct v4l2_event_subscription *sub)
1451 struct coda_ctx *ctx = fh_to_ctx(fh);
1453 switch (sub->type) {
1454 case V4L2_EVENT_EOS:
1455 return v4l2_event_subscribe(fh, sub, 0, NULL);
1456 case V4L2_EVENT_SOURCE_CHANGE:
1457 if (ctx->inst_type == CODA_INST_DECODER)
1458 return v4l2_event_subscribe(fh, sub, 0, NULL);
1462 return v4l2_ctrl_subscribe_event(fh, sub);
1466 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1467 .vidioc_querycap = coda_querycap,
1469 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1470 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1471 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1472 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1474 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1475 .vidioc_g_fmt_vid_out = coda_g_fmt,
1476 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1477 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1479 .vidioc_reqbufs = coda_reqbufs,
1480 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1482 .vidioc_qbuf = coda_qbuf,
1483 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1484 .vidioc_dqbuf = coda_dqbuf,
1485 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1486 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1488 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1489 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1491 .vidioc_g_selection = coda_g_selection,
1492 .vidioc_s_selection = coda_s_selection,
1494 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1495 .vidioc_encoder_cmd = coda_encoder_cmd,
1496 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
1497 .vidioc_decoder_cmd = coda_decoder_cmd,
1499 .vidioc_g_parm = coda_g_parm,
1500 .vidioc_s_parm = coda_s_parm,
1502 .vidioc_enum_framesizes = coda_enum_framesizes,
1503 .vidioc_enum_frameintervals = coda_enum_frameintervals,
1505 .vidioc_subscribe_event = coda_subscribe_event,
1506 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1510 * Mem-to-mem operations.
1513 static void coda_device_run(void *m2m_priv)
1515 struct coda_ctx *ctx = m2m_priv;
1516 struct coda_dev *dev = ctx->dev;
1518 queue_work(dev->workqueue, &ctx->pic_run_work);
1521 static void coda_pic_run_work(struct work_struct *work)
1523 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1524 struct coda_dev *dev = ctx->dev;
1527 mutex_lock(&ctx->buffer_mutex);
1528 mutex_lock(&dev->coda_mutex);
1530 ret = ctx->ops->prepare_run(ctx);
1531 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1532 mutex_unlock(&dev->coda_mutex);
1533 mutex_unlock(&ctx->buffer_mutex);
1534 /* job_finish scheduled by prepare_decode */
1538 if (!wait_for_completion_timeout(&ctx->completion,
1539 msecs_to_jiffies(1000))) {
1540 dev_err(dev->dev, "CODA PIC_RUN timeout\n");
1546 if (ctx->ops->run_timeout)
1547 ctx->ops->run_timeout(ctx);
1549 ctx->ops->finish_run(ctx);
1552 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1553 ctx->ops->seq_end_work)
1554 queue_work(dev->workqueue, &ctx->seq_end_work);
1556 mutex_unlock(&dev->coda_mutex);
1557 mutex_unlock(&ctx->buffer_mutex);
1559 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1562 static int coda_job_ready(void *m2m_priv)
1564 struct coda_ctx *ctx = m2m_priv;
1565 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1568 * For both 'P' and 'key' frame cases 1 picture
1569 * and 1 frame are needed. In the decoder case,
1570 * the compressed frame can be in the bitstream.
1572 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1573 coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
1577 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1578 coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
1582 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1583 bool stream_end = ctx->bit_stream_param &
1584 CODA_BIT_STREAM_END_FLAG;
1585 int num_metas = ctx->num_metas;
1586 struct coda_buffer_meta *meta;
1589 count = hweight32(ctx->frm_dis_flg);
1590 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1592 "not ready: all internal buffers in use: %d/%d (0x%x)",
1593 count, ctx->num_internal_frames,
1598 if (ctx->hold && !src_bufs) {
1600 "not ready: on hold for more buffers.\n");
1604 if (!stream_end && (num_metas + src_bufs) < 2) {
1606 "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1607 num_metas, src_bufs);
1611 meta = list_first_entry(&ctx->buffer_meta_list,
1612 struct coda_buffer_meta, list);
1613 if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1616 "not ready: not enough bitstream data to read past %u (%u)\n",
1617 meta->end, ctx->bitstream_fifo.kfifo.in);
1622 if (ctx->aborting) {
1623 coda_dbg(1, ctx, "not ready: aborting\n");
1627 coda_dbg(2, ctx, "job ready\n");
1632 static void coda_job_abort(void *priv)
1634 struct coda_ctx *ctx = priv;
1638 coda_dbg(1, ctx, "job abort\n");
1641 static const struct v4l2_m2m_ops coda_m2m_ops = {
1642 .device_run = coda_device_run,
1643 .job_ready = coda_job_ready,
1644 .job_abort = coda_job_abort,
1647 static void set_default_params(struct coda_ctx *ctx)
1649 unsigned int max_w, max_h, usize, csize;
1651 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1652 ctx->cvd->dst_formats[0]);
1653 max_w = min(ctx->codec->max_w, 1920U);
1654 max_h = min(ctx->codec->max_h, 1088U);
1655 usize = max_w * max_h * 3 / 2;
1656 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1658 ctx->params.codec_mode = ctx->codec->mode;
1659 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1660 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1662 ctx->colorspace = V4L2_COLORSPACE_REC709;
1663 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1664 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1665 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1666 ctx->params.framerate = 30;
1668 /* Default formats for output and input queues */
1669 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1670 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1671 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1672 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1673 ctx->q_data[V4L2_M2M_DST].width = max_w;
1674 ctx->q_data[V4L2_M2M_DST].height = max_h;
1675 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1676 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1677 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1678 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1679 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1681 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1682 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1683 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1684 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1686 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1687 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1688 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1689 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1692 * Since the RBC2AXI logic only supports a single chroma plane,
1693 * macroblock tiling only works for to NV12 pixel format.
1695 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1701 static int coda_queue_setup(struct vb2_queue *vq,
1702 unsigned int *nbuffers, unsigned int *nplanes,
1703 unsigned int sizes[], struct device *alloc_devs[])
1705 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1706 struct coda_q_data *q_data;
1709 q_data = get_q_data(ctx, vq->type);
1710 size = q_data->sizeimage;
1713 return sizes[0] < size ? -EINVAL : 0;
1718 coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1724 static int coda_buf_prepare(struct vb2_buffer *vb)
1726 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1727 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1728 struct coda_q_data *q_data;
1730 q_data = get_q_data(ctx, vb->vb2_queue->type);
1731 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1732 if (vbuf->field == V4L2_FIELD_ANY)
1733 vbuf->field = V4L2_FIELD_NONE;
1734 if (vbuf->field != V4L2_FIELD_NONE) {
1735 v4l2_warn(&ctx->dev->v4l2_dev,
1736 "%s field isn't supported\n", __func__);
1741 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1742 v4l2_warn(&ctx->dev->v4l2_dev,
1743 "%s data will not fit into plane (%lu < %lu)\n",
1744 __func__, vb2_plane_size(vb, 0),
1745 (long)q_data->sizeimage);
1752 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1757 v4l2_ctrl_lock(ctrl);
1760 * Extend the control range if the parsed stream contains a known but
1761 * unsupported value or level.
1763 if (value > ctrl->maximum) {
1764 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1765 ctrl->menu_skip_mask & ~(1 << value),
1766 ctrl->default_value);
1767 } else if (value < ctrl->minimum) {
1768 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1769 ctrl->menu_skip_mask & ~(1 << value),
1770 ctrl->default_value);
1773 __v4l2_ctrl_s_ctrl(ctrl, value);
1775 v4l2_ctrl_unlock(ctrl);
1778 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
1781 const char * const *profile_names;
1782 const char * const *level_names;
1783 struct v4l2_ctrl *profile_ctrl;
1784 struct v4l2_ctrl *level_ctrl;
1785 const char *codec_name;
1791 switch (ctx->codec->src_fourcc) {
1792 case V4L2_PIX_FMT_H264:
1793 codec_name = "H264";
1794 profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
1795 level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
1796 profile_ctrl = ctx->h264_profile_ctrl;
1797 level_ctrl = ctx->h264_level_ctrl;
1798 profile = coda_h264_profile(profile_idc);
1799 level = coda_h264_level(level_idc);
1801 case V4L2_PIX_FMT_MPEG2:
1802 codec_name = "MPEG-2";
1803 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE;
1804 level_cid = V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL;
1805 profile_ctrl = ctx->mpeg2_profile_ctrl;
1806 level_ctrl = ctx->mpeg2_level_ctrl;
1807 profile = coda_mpeg2_profile(profile_idc);
1808 level = coda_mpeg2_level(level_idc);
1810 case V4L2_PIX_FMT_MPEG4:
1811 codec_name = "MPEG-4";
1812 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
1813 level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
1814 profile_ctrl = ctx->mpeg4_profile_ctrl;
1815 level_ctrl = ctx->mpeg4_level_ctrl;
1816 profile = coda_mpeg4_profile(profile_idc);
1817 level = coda_mpeg4_level(level_idc);
1823 profile_names = v4l2_ctrl_get_menu(profile_cid);
1824 level_names = v4l2_ctrl_get_menu(level_cid);
1827 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s profile: %u\n",
1828 codec_name, profile_idc);
1830 coda_dbg(1, ctx, "Parsed %s profile: %s\n", codec_name,
1831 profile_names[profile]);
1832 coda_update_menu_ctrl(profile_ctrl, profile);
1836 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s level: %u\n",
1837 codec_name, level_idc);
1839 coda_dbg(1, ctx, "Parsed %s level: %s\n", codec_name,
1840 level_names[level]);
1841 coda_update_menu_ctrl(level_ctrl, level);
1845 static void coda_queue_source_change_event(struct coda_ctx *ctx)
1847 static const struct v4l2_event source_change_event = {
1848 .type = V4L2_EVENT_SOURCE_CHANGE,
1849 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1852 v4l2_event_queue_fh(&ctx->fh, &source_change_event);
1855 static void coda_buf_queue(struct vb2_buffer *vb)
1857 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1858 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1859 struct vb2_queue *vq = vb->vb2_queue;
1860 struct coda_q_data *q_data;
1862 q_data = get_q_data(ctx, vb->vb2_queue->type);
1865 * In the decoder case, immediately try to copy the buffer into the
1866 * bitstream ringbuffer and mark it as ready to be dequeued.
1868 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1870 * For backwards compatibility, queuing an empty buffer marks
1873 if (vb2_get_plane_payload(vb, 0) == 0)
1874 coda_bit_stream_end_flag(ctx);
1876 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1878 * Unless already done, try to obtain profile_idc and
1879 * level_idc from the SPS header. This allows to decide
1880 * whether to enable reordering during sequence
1883 if (!ctx->params.h264_profile_idc) {
1884 coda_sps_parse_profile(ctx, vb);
1885 coda_update_profile_level_ctrls(ctx,
1886 ctx->params.h264_profile_idc,
1887 ctx->params.h264_level_idc);
1891 mutex_lock(&ctx->bitstream_mutex);
1892 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1893 if (vb2_is_streaming(vb->vb2_queue))
1894 /* This set buf->sequence = ctx->qsequence++ */
1895 coda_fill_bitstream(ctx, NULL);
1896 mutex_unlock(&ctx->bitstream_mutex);
1898 if (!ctx->initialized) {
1900 * Run sequence initialization in case the queued
1901 * buffer contained headers.
1903 if (vb2_is_streaming(vb->vb2_queue) &&
1904 ctx->ops->seq_init_work) {
1905 queue_work(ctx->dev->workqueue,
1906 &ctx->seq_init_work);
1907 flush_work(&ctx->seq_init_work);
1910 if (ctx->initialized)
1911 coda_queue_source_change_event(ctx);
1914 if ((ctx->inst_type == CODA_INST_ENCODER || !ctx->use_bit) &&
1915 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1916 vbuf->sequence = ctx->qsequence++;
1917 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1921 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1922 size_t size, const char *name, struct dentry *parent)
1924 buf->vaddr = dma_alloc_coherent(dev->dev, size, &buf->paddr,
1927 v4l2_err(&dev->v4l2_dev,
1928 "Failed to allocate %s buffer of size %zu\n",
1935 if (name && parent) {
1936 buf->blob.data = buf->vaddr;
1937 buf->blob.size = size;
1938 buf->dentry = debugfs_create_blob(name, 0644, parent,
1945 void coda_free_aux_buf(struct coda_dev *dev,
1946 struct coda_aux_buf *buf)
1949 dma_free_coherent(dev->dev, buf->size, buf->vaddr, buf->paddr);
1952 debugfs_remove(buf->dentry);
1957 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1959 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1960 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1961 struct coda_q_data *q_data_src, *q_data_dst;
1962 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1963 struct vb2_v4l2_buffer *buf;
1964 struct list_head list;
1970 coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1972 INIT_LIST_HEAD(&list);
1974 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1975 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1976 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1977 /* copy the buffers that were queued before streamon */
1978 mutex_lock(&ctx->bitstream_mutex);
1979 coda_fill_bitstream(ctx, &list);
1980 mutex_unlock(&ctx->bitstream_mutex);
1982 if (ctx->dev->devtype->product != CODA_960 &&
1983 coda_get_bitstream_payload(ctx) < 512) {
1984 v4l2_err(v4l2_dev, "start payload < 512\n");
1989 if (!ctx->initialized) {
1990 /* Run sequence initialization */
1991 if (ctx->ops->seq_init_work) {
1992 queue_work(ctx->dev->workqueue,
1993 &ctx->seq_init_work);
1994 flush_work(&ctx->seq_init_work);
2000 * Check the first input JPEG buffer to determine chroma
2003 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
2004 buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2005 ret = coda_jpeg_decode_header(ctx, &buf->vb2_buf);
2008 "failed to decode JPEG header: %d\n",
2013 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2014 q_data_dst->width = round_up(q_data_src->width, 16);
2015 q_data_dst->height = round_up(q_data_src->height, 16);
2016 q_data_dst->bytesperline = q_data_dst->width;
2017 if (ctx->params.jpeg_chroma_subsampling ==
2018 V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
2019 q_data_dst->sizeimage =
2020 q_data_dst->bytesperline *
2021 q_data_dst->height * 3 / 2;
2022 if (q_data_dst->fourcc != V4L2_PIX_FMT_YUV420)
2023 q_data_dst->fourcc = V4L2_PIX_FMT_NV12;
2025 q_data_dst->sizeimage =
2026 q_data_dst->bytesperline *
2027 q_data_dst->height * 2;
2028 q_data_dst->fourcc = V4L2_PIX_FMT_YUV422P;
2030 q_data_dst->rect.left = 0;
2031 q_data_dst->rect.top = 0;
2032 q_data_dst->rect.width = q_data_src->width;
2033 q_data_dst->rect.height = q_data_src->height;
2035 ctx->streamon_out = 1;
2037 ctx->streamon_cap = 1;
2040 /* Don't start the coda unless both queues are on */
2041 if (!(ctx->streamon_out && ctx->streamon_cap))
2044 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2045 if ((q_data_src->rect.width != q_data_dst->width &&
2046 round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
2047 (q_data_src->rect.height != q_data_dst->height &&
2048 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
2049 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
2050 q_data_src->rect.width, q_data_src->rect.height,
2051 q_data_dst->width, q_data_dst->height);
2056 /* Allow BIT decoder device_run with no new buffers queued */
2057 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2058 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
2060 ctx->gopcounter = ctx->params.gop_size - 1;
2062 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
2063 ctx->params.gop_size = 1;
2064 ctx->gopcounter = ctx->params.gop_size - 1;
2065 /* Only decoders have this control */
2066 if (ctx->mb_err_cnt_ctrl)
2067 v4l2_ctrl_s_ctrl(ctx->mb_err_cnt_ctrl, 0);
2069 ret = ctx->ops->start_streaming(ctx);
2070 if (ctx->inst_type == CODA_INST_DECODER) {
2078 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2079 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2080 list_del(&m2m_buf->list);
2081 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
2087 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2088 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2089 list_del(&m2m_buf->list);
2090 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
2092 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2093 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2095 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2096 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2101 static void coda_stop_streaming(struct vb2_queue *q)
2103 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2104 struct coda_dev *dev = ctx->dev;
2105 struct vb2_v4l2_buffer *buf;
2108 stop = ctx->streamon_out && ctx->streamon_cap;
2110 coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
2112 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2113 ctx->streamon_out = 0;
2115 coda_bit_stream_end_flag(ctx);
2119 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2120 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2122 ctx->streamon_cap = 0;
2125 ctx->sequence_offset = 0;
2127 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2128 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2132 struct coda_buffer_meta *meta;
2134 if (ctx->ops->seq_end_work) {
2135 queue_work(dev->workqueue, &ctx->seq_end_work);
2136 flush_work(&ctx->seq_end_work);
2138 spin_lock(&ctx->buffer_meta_lock);
2139 while (!list_empty(&ctx->buffer_meta_list)) {
2140 meta = list_first_entry(&ctx->buffer_meta_list,
2141 struct coda_buffer_meta, list);
2142 list_del(&meta->list);
2146 spin_unlock(&ctx->buffer_meta_lock);
2147 kfifo_init(&ctx->bitstream_fifo,
2148 ctx->bitstream.vaddr, ctx->bitstream.size);
2149 ctx->runcounter = 0;
2154 if (!ctx->streamon_out && !ctx->streamon_cap)
2155 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
2158 static const struct vb2_ops coda_qops = {
2159 .queue_setup = coda_queue_setup,
2160 .buf_prepare = coda_buf_prepare,
2161 .buf_queue = coda_buf_queue,
2162 .start_streaming = coda_start_streaming,
2163 .stop_streaming = coda_stop_streaming,
2164 .wait_prepare = vb2_ops_wait_prepare,
2165 .wait_finish = vb2_ops_wait_finish,
2168 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2170 const char * const *val_names = v4l2_ctrl_get_menu(ctrl->id);
2171 struct coda_ctx *ctx =
2172 container_of(ctrl->handler, struct coda_ctx, ctrls);
2175 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d (\"%s\")\n",
2176 ctrl->id, ctrl->name, ctrl->val, val_names[ctrl->val]);
2178 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
2179 ctrl->id, ctrl->name, ctrl->val);
2182 case V4L2_CID_HFLIP:
2184 ctx->params.rot_mode |= CODA_MIR_HOR;
2186 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2188 case V4L2_CID_VFLIP:
2190 ctx->params.rot_mode |= CODA_MIR_VER;
2192 ctx->params.rot_mode &= ~CODA_MIR_VER;
2194 case V4L2_CID_MPEG_VIDEO_BITRATE:
2195 ctx->params.bitrate = ctrl->val / 1000;
2196 ctx->params.bitrate_changed = true;
2198 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2199 ctx->params.gop_size = ctrl->val;
2201 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2202 ctx->params.h264_intra_qp = ctrl->val;
2203 ctx->params.h264_intra_qp_changed = true;
2205 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2206 ctx->params.h264_inter_qp = ctrl->val;
2208 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2209 ctx->params.h264_min_qp = ctrl->val;
2211 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2212 ctx->params.h264_max_qp = ctrl->val;
2214 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2215 ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
2217 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2218 ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
2220 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2221 ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
2223 case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
2224 ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
2226 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
2227 ctx->params.frame_rc_enable = ctrl->val;
2229 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
2230 ctx->params.mb_rc_enable = ctrl->val;
2232 case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
2233 ctx->params.h264_chroma_qp_index_offset = ctrl->val;
2235 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
2236 /* TODO: switch between baseline and constrained baseline */
2237 if (ctx->inst_type == CODA_INST_ENCODER)
2238 ctx->params.h264_profile_idc = 66;
2240 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2241 /* nothing to do, this is set by the encoder */
2243 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2244 ctx->params.mpeg4_intra_qp = ctrl->val;
2246 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2247 ctx->params.mpeg4_inter_qp = ctrl->val;
2249 case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
2250 case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
2251 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
2252 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
2253 /* nothing to do, these are fixed */
2255 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2256 ctx->params.slice_mode = ctrl->val;
2257 ctx->params.slice_mode_changed = true;
2259 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2260 ctx->params.slice_max_mb = ctrl->val;
2261 ctx->params.slice_mode_changed = true;
2263 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2264 ctx->params.slice_max_bits = ctrl->val * 8;
2265 ctx->params.slice_mode_changed = true;
2267 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2269 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2270 ctx->params.intra_refresh = ctrl->val;
2271 ctx->params.intra_refresh_changed = true;
2273 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
2274 ctx->params.force_ipicture = true;
2276 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2277 coda_set_jpeg_compression_quality(ctx, ctrl->val);
2279 case V4L2_CID_JPEG_RESTART_INTERVAL:
2280 ctx->params.jpeg_restart_interval = ctrl->val;
2282 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
2283 ctx->params.vbv_delay = ctrl->val;
2285 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
2286 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
2289 coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
2290 ctrl->id, ctrl->val);
2297 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
2298 .s_ctrl = coda_s_ctrl,
2301 static void coda_encode_ctrls(struct coda_ctx *ctx)
2303 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
2305 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2306 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
2307 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2308 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
2309 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2310 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
2311 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2312 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
2313 if (ctx->dev->devtype->product != CODA_960) {
2314 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2315 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2317 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2318 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
2319 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2320 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
2321 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2322 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
2323 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2324 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2325 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
2326 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2327 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2328 V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
2330 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2331 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
2332 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2333 V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, 0, 1, 1, 1);
2334 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2335 V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
2336 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2337 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2338 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2339 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2340 if (ctx->dev->devtype->product == CODA_HX4 ||
2341 ctx->dev->devtype->product == CODA_7541) {
2342 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2343 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2344 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2345 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2346 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2347 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2348 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2350 if (ctx->dev->devtype->product == CODA_960) {
2351 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2352 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2353 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
2354 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2355 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2356 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2357 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2358 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
2359 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2361 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2362 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2363 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2364 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2365 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2366 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2367 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2368 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
2369 if (ctx->dev->devtype->product == CODA_HX4 ||
2370 ctx->dev->devtype->product == CODA_7541 ||
2371 ctx->dev->devtype->product == CODA_960) {
2372 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2373 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2374 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2375 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2376 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2378 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2379 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
2380 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES, 0x0,
2381 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
2382 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2383 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
2384 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2385 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2387 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2388 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2389 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2390 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2391 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2392 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2393 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2394 1920 * 1088 / 256, 1, 0);
2395 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2396 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2398 * The maximum VBV size value is 0x7fffffff bits,
2399 * one bit less than 262144 KiB
2401 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2402 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
2405 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2407 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2408 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2409 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2410 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2413 static void coda_decode_ctrls(struct coda_ctx *ctx)
2417 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2418 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2419 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2420 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
2421 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2422 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2423 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2424 if (ctx->h264_profile_ctrl)
2425 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2427 if (ctx->dev->devtype->product == CODA_HX4 ||
2428 ctx->dev->devtype->product == CODA_7541)
2429 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2430 else if (ctx->dev->devtype->product == CODA_960)
2431 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
2434 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2435 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
2436 if (ctx->h264_level_ctrl)
2437 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2439 ctx->mpeg2_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2440 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE,
2441 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH, 0,
2442 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH);
2443 if (ctx->mpeg2_profile_ctrl)
2444 ctx->mpeg2_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2446 ctx->mpeg2_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2447 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL,
2448 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH, 0,
2449 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH);
2450 if (ctx->mpeg2_level_ctrl)
2451 ctx->mpeg2_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2453 ctx->mpeg4_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2454 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2455 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY, 0,
2456 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY);
2457 if (ctx->mpeg4_profile_ctrl)
2458 ctx->mpeg4_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2460 ctx->mpeg4_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2461 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2462 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 0,
2463 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2464 if (ctx->mpeg4_level_ctrl)
2465 ctx->mpeg4_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2468 static const struct v4l2_ctrl_config coda_mb_err_cnt_ctrl_config = {
2469 .id = V4L2_CID_CODA_MB_ERR_CNT,
2470 .name = "Macroblocks Error Count",
2471 .type = V4L2_CTRL_TYPE_INTEGER,
2477 static int coda_ctrls_setup(struct coda_ctx *ctx)
2479 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2481 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2482 V4L2_CID_HFLIP, 0, 1, 1, 0);
2483 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2484 V4L2_CID_VFLIP, 0, 1, 1, 0);
2485 if (ctx->inst_type == CODA_INST_ENCODER) {
2486 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2487 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2489 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2490 coda_jpeg_encode_ctrls(ctx);
2492 coda_encode_ctrls(ctx);
2494 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2495 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2497 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2498 coda_decode_ctrls(ctx);
2500 ctx->mb_err_cnt_ctrl = v4l2_ctrl_new_custom(&ctx->ctrls,
2501 &coda_mb_err_cnt_ctrl_config,
2503 if (ctx->mb_err_cnt_ctrl)
2504 ctx->mb_err_cnt_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2507 if (ctx->ctrls.error) {
2508 v4l2_err(&ctx->dev->v4l2_dev,
2509 "control initialization error (%d)",
2514 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2517 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2520 vq->ops = &coda_qops;
2521 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2522 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2523 vq->lock = &ctx->dev->dev_mutex;
2524 /* One way to indicate end-of-stream for coda is to set the
2525 * bytesused == 0. However by default videobuf2 handles bytesused
2526 * equal to 0 as a special case and changes its value to the size
2527 * of the buffer. Set the allow_zero_bytesused flag, so
2528 * that videobuf2 will keep the value of bytesused intact.
2530 vq->allow_zero_bytesused = 1;
2532 * We might be fine with no buffers on some of the queues, but that
2533 * would need to be reflected in job_ready(). Currently we expect all
2534 * queues to have at least one buffer queued.
2536 vq->min_buffers_needed = 1;
2537 vq->dev = ctx->dev->dev;
2539 return vb2_queue_init(vq);
2542 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2543 struct vb2_queue *dst_vq)
2547 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2548 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2549 src_vq->mem_ops = &vb2_dma_contig_memops;
2551 ret = coda_queue_init(priv, src_vq);
2555 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2556 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2557 dst_vq->mem_ops = &vb2_dma_contig_memops;
2559 return coda_queue_init(priv, dst_vq);
2562 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2563 struct vb2_queue *dst_vq)
2567 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2568 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2569 src_vq->mem_ops = &vb2_vmalloc_memops;
2571 ret = coda_queue_init(priv, src_vq);
2575 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2576 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2577 dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING;
2578 dst_vq->mem_ops = &vb2_dma_contig_memops;
2580 return coda_queue_init(priv, dst_vq);
2587 static int coda_open(struct file *file)
2589 struct video_device *vdev = video_devdata(file);
2590 struct coda_dev *dev = video_get_drvdata(vdev);
2591 struct coda_ctx *ctx;
2592 unsigned int max = ~0;
2597 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2601 if (dev->devtype->product == CODA_DX6)
2602 max = CODADX6_MAX_INSTANCES - 1;
2603 idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2609 name = kasprintf(GFP_KERNEL, "context%d", idx);
2612 goto err_coda_name_init;
2615 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2618 ctx->cvd = to_coda_video_device(vdev);
2619 ctx->inst_type = ctx->cvd->type;
2620 ctx->ops = ctx->cvd->ops;
2621 ctx->use_bit = !ctx->cvd->direct;
2622 init_completion(&ctx->completion);
2623 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2624 if (ctx->ops->seq_init_work)
2625 INIT_WORK(&ctx->seq_init_work, ctx->ops->seq_init_work);
2626 if (ctx->ops->seq_end_work)
2627 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2628 v4l2_fh_init(&ctx->fh, video_devdata(file));
2629 file->private_data = &ctx->fh;
2630 v4l2_fh_add(&ctx->fh);
2634 coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2636 switch (dev->devtype->product) {
2639 * Enabling the BWB when decoding can hang the firmware with
2640 * certain streams. The issue was tracked as ENGR00293425 by
2641 * Freescale. As a workaround, disable BWB for all decoders.
2642 * The enable_bwb module parameter allows to override this.
2644 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2645 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2654 if (ctx->dev->vdoa && !disable_vdoa) {
2655 ctx->vdoa = vdoa_context_create(dev->vdoa);
2657 v4l2_warn(&dev->v4l2_dev,
2658 "Failed to create vdoa context: not using vdoa");
2660 ctx->use_vdoa = false;
2662 /* Power up and upload firmware if necessary */
2663 ret = pm_runtime_get_sync(dev->dev);
2665 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2669 ret = clk_prepare_enable(dev->clk_per);
2673 ret = clk_prepare_enable(dev->clk_ahb);
2677 set_default_params(ctx);
2678 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2679 ctx->ops->queue_init);
2680 if (IS_ERR(ctx->fh.m2m_ctx)) {
2681 ret = PTR_ERR(ctx->fh.m2m_ctx);
2683 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2688 ret = coda_ctrls_setup(ctx);
2690 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2691 goto err_ctrls_setup;
2694 ctx->fh.ctrl_handler = &ctx->ctrls;
2696 mutex_init(&ctx->bitstream_mutex);
2697 mutex_init(&ctx->buffer_mutex);
2698 mutex_init(&ctx->wakeup_mutex);
2699 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2700 spin_lock_init(&ctx->buffer_meta_lock);
2705 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2707 clk_disable_unprepare(dev->clk_ahb);
2709 clk_disable_unprepare(dev->clk_per);
2711 pm_runtime_put_sync(dev->dev);
2712 v4l2_fh_del(&ctx->fh);
2713 v4l2_fh_exit(&ctx->fh);
2715 ida_free(&dev->ida, ctx->idx);
2721 static int coda_release(struct file *file)
2723 struct coda_dev *dev = video_drvdata(file);
2724 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2726 coda_dbg(1, ctx, "release instance (%p)\n", ctx);
2728 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2729 coda_bit_stream_end_flag(ctx);
2731 /* If this instance is running, call .job_abort and wait for it to end */
2732 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2735 vdoa_context_destroy(ctx->vdoa);
2737 /* In case the instance was not running, we still need to call SEQ_END */
2738 if (ctx->ops->seq_end_work) {
2739 queue_work(dev->workqueue, &ctx->seq_end_work);
2740 flush_work(&ctx->seq_end_work);
2743 if (ctx->dev->devtype->product == CODA_DX6)
2744 coda_free_aux_buf(dev, &ctx->workbuf);
2746 v4l2_ctrl_handler_free(&ctx->ctrls);
2747 clk_disable_unprepare(dev->clk_ahb);
2748 clk_disable_unprepare(dev->clk_per);
2749 pm_runtime_put_sync(dev->dev);
2750 v4l2_fh_del(&ctx->fh);
2751 v4l2_fh_exit(&ctx->fh);
2752 ida_free(&dev->ida, ctx->idx);
2753 if (ctx->ops->release)
2754 ctx->ops->release(ctx);
2755 debugfs_remove_recursive(ctx->debugfs_entry);
2761 static const struct v4l2_file_operations coda_fops = {
2762 .owner = THIS_MODULE,
2764 .release = coda_release,
2765 .poll = v4l2_m2m_fop_poll,
2766 .unlocked_ioctl = video_ioctl2,
2767 .mmap = v4l2_m2m_fop_mmap,
2770 static int coda_hw_init(struct coda_dev *dev)
2776 ret = clk_prepare_enable(dev->clk_per);
2780 ret = clk_prepare_enable(dev->clk_ahb);
2784 reset_control_reset(dev->rstc);
2787 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2788 * The 16-bit chars in the code buffer are in memory access
2789 * order, re-sort them to CODA order for register download.
2790 * Data in this SRAM survives a reboot.
2792 p = (u16 *)dev->codebuf.vaddr;
2793 if (dev->devtype->product == CODA_DX6) {
2794 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2795 data = CODA_DOWN_ADDRESS_SET(i) |
2796 CODA_DOWN_DATA_SET(p[i ^ 1]);
2797 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2800 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2801 data = CODA_DOWN_ADDRESS_SET(i) |
2802 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2804 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2808 /* Clear registers */
2809 for (i = 0; i < 64; i++)
2810 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2812 /* Tell the BIT where to find everything it needs */
2813 if (dev->devtype->product == CODA_960 ||
2814 dev->devtype->product == CODA_7541 ||
2815 dev->devtype->product == CODA_HX4) {
2816 coda_write(dev, dev->tempbuf.paddr,
2817 CODA_REG_BIT_TEMP_BUF_ADDR);
2818 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2820 coda_write(dev, dev->workbuf.paddr,
2821 CODA_REG_BIT_WORK_BUF_ADDR);
2823 coda_write(dev, dev->codebuf.paddr,
2824 CODA_REG_BIT_CODE_BUF_ADDR);
2825 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2827 /* Set default values */
2828 switch (dev->devtype->product) {
2830 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2831 CODA_REG_BIT_STREAM_CTRL);
2834 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2835 CODA_REG_BIT_STREAM_CTRL);
2837 if (dev->devtype->product == CODA_960)
2838 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2839 CODA_REG_BIT_FRAME_MEM_CTRL);
2841 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2843 if (dev->devtype->product != CODA_DX6)
2844 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2846 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2847 CODA_REG_BIT_INT_ENABLE);
2849 /* Reset VPU and start processor */
2850 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2851 data |= CODA_REG_RESET_ENABLE;
2852 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2854 data &= ~CODA_REG_RESET_ENABLE;
2855 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2856 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2858 clk_disable_unprepare(dev->clk_ahb);
2859 clk_disable_unprepare(dev->clk_per);
2864 clk_disable_unprepare(dev->clk_per);
2869 static int coda_register_device(struct coda_dev *dev, int i)
2871 struct video_device *vfd = &dev->vfd[i];
2875 if (i >= dev->devtype->num_vdevs)
2877 name = dev->devtype->vdevs[i]->name;
2879 strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2880 vfd->fops = &coda_fops;
2881 vfd->ioctl_ops = &coda_ioctl_ops;
2882 vfd->release = video_device_release_empty;
2883 vfd->lock = &dev->dev_mutex;
2884 vfd->v4l2_dev = &dev->v4l2_dev;
2885 vfd->vfl_dir = VFL_DIR_M2M;
2886 vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2887 video_set_drvdata(vfd, dev);
2889 /* Not applicable, use the selection API instead */
2890 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2891 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2892 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2894 ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
2896 v4l2_info(&dev->v4l2_dev, "%s registered as %s\n",
2897 name, video_device_node_name(vfd));
2901 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2904 u32 *src = (u32 *)buf;
2906 /* Check if the firmware has a 16-byte Freescale header, skip it */
2907 if (buf[0] == 'M' && buf[1] == 'X')
2910 * Check whether the firmware is in native order or pre-reordered for
2911 * memory access. The first instruction opcode always is 0xe40e.
2913 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2914 u32 *dst = dev->codebuf.vaddr;
2917 /* Firmware in native order, reorder while copying */
2918 if (dev->devtype->product == CODA_DX6) {
2919 for (i = 0; i < (size - 16) / 4; i++)
2920 dst[i] = (src[i] << 16) | (src[i] >> 16);
2922 for (i = 0; i < (size - 16) / 4; i += 2) {
2923 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2924 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2928 /* Copy the already reordered firmware image */
2929 memcpy(dev->codebuf.vaddr, src, size);
2933 static void coda_fw_callback(const struct firmware *fw, void *context);
2935 static int coda_firmware_request(struct coda_dev *dev)
2939 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2942 fw = dev->devtype->firmware[dev->firmware];
2944 dev_dbg(dev->dev, "requesting firmware '%s' for %s\n", fw,
2945 coda_product_name(dev->devtype->product));
2947 return request_firmware_nowait(THIS_MODULE, true, fw, dev->dev,
2948 GFP_KERNEL, dev, coda_fw_callback);
2951 static void coda_fw_callback(const struct firmware *fw, void *context)
2953 struct coda_dev *dev = context;
2958 ret = coda_firmware_request(dev);
2960 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2965 if (dev->firmware > 0) {
2967 * Since we can't suppress warnings for failed asynchronous
2968 * firmware requests, report that the fallback firmware was
2971 dev_info(dev->dev, "Using fallback firmware %s\n",
2972 dev->devtype->firmware[dev->firmware]);
2975 /* allocate auxiliary per-device code buffer for the BIT processor */
2976 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2981 coda_copy_firmware(dev, fw->data, fw->size);
2982 release_firmware(fw);
2984 ret = coda_hw_init(dev);
2986 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2990 ret = coda_check_firmware(dev);
2994 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2995 if (IS_ERR(dev->m2m_dev)) {
2996 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3000 for (i = 0; i < dev->devtype->num_vdevs; i++) {
3001 ret = coda_register_device(dev, i);
3003 v4l2_err(&dev->v4l2_dev,
3004 "Failed to register %s video device: %d\n",
3005 dev->devtype->vdevs[i]->name, ret);
3010 pm_runtime_put_sync(dev->dev);
3015 video_unregister_device(&dev->vfd[i]);
3016 v4l2_m2m_release(dev->m2m_dev);
3018 pm_runtime_put_sync(dev->dev);
3021 enum coda_platform {
3029 static const struct coda_devtype coda_devdata[] = {
3032 "vpu_fw_imx27_TO2.bin",
3033 "vpu/vpu_fw_imx27_TO2.bin",
3034 "v4l-codadx6-imx27.bin"
3036 .product = CODA_DX6,
3037 .codecs = codadx6_codecs,
3038 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3039 .vdevs = codadx6_video_devices,
3040 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
3041 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
3042 .iram_size = 0xb000,
3047 "vpu/vpu_fw_imx51.bin",
3048 "v4l-codahx4-imx51.bin"
3050 .product = CODA_HX4,
3051 .codecs = codahx4_codecs,
3052 .num_codecs = ARRAY_SIZE(codahx4_codecs),
3053 .vdevs = codahx4_video_devices,
3054 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
3055 .workbuf_size = 128 * 1024,
3056 .tempbuf_size = 304 * 1024,
3057 .iram_size = 0x14000,
3062 "vpu/vpu_fw_imx53.bin",
3063 "v4l-coda7541-imx53.bin"
3065 .product = CODA_7541,
3066 .codecs = coda7_codecs,
3067 .num_codecs = ARRAY_SIZE(coda7_codecs),
3068 .vdevs = coda7_video_devices,
3069 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
3070 .workbuf_size = 128 * 1024,
3071 .tempbuf_size = 304 * 1024,
3072 .iram_size = 0x14000,
3077 "vpu/vpu_fw_imx6q.bin",
3078 "v4l-coda960-imx6q.bin"
3080 .product = CODA_960,
3081 .codecs = coda9_codecs,
3082 .num_codecs = ARRAY_SIZE(coda9_codecs),
3083 .vdevs = coda9_video_devices,
3084 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
3085 .workbuf_size = 80 * 1024,
3086 .tempbuf_size = 204 * 1024,
3087 .iram_size = 0x21000,
3092 "vpu/vpu_fw_imx6d.bin",
3093 "v4l-coda960-imx6dl.bin"
3095 .product = CODA_960,
3096 .codecs = coda9_codecs,
3097 .num_codecs = ARRAY_SIZE(coda9_codecs),
3098 .vdevs = coda9_video_devices,
3099 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
3100 .workbuf_size = 80 * 1024,
3101 .tempbuf_size = 204 * 1024,
3102 .iram_size = 0x1f000, /* leave 4k for suspend code */
3106 static const struct of_device_id coda_dt_ids[] = {
3107 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
3108 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
3109 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
3110 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3111 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
3114 MODULE_DEVICE_TABLE(of, coda_dt_ids);
3116 static int coda_probe(struct platform_device *pdev)
3118 struct device_node *np = pdev->dev.of_node;
3119 struct gen_pool *pool;
3120 struct coda_dev *dev;
3123 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3127 dev->devtype = of_device_get_match_data(&pdev->dev);
3129 dev->dev = &pdev->dev;
3130 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3131 if (IS_ERR(dev->clk_per)) {
3132 dev_err(&pdev->dev, "Could not get per clock\n");
3133 return PTR_ERR(dev->clk_per);
3136 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3137 if (IS_ERR(dev->clk_ahb)) {
3138 dev_err(&pdev->dev, "Could not get ahb clock\n");
3139 return PTR_ERR(dev->clk_ahb);
3142 /* Get memory for physical registers */
3143 dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
3144 if (IS_ERR(dev->regs_base))
3145 return PTR_ERR(dev->regs_base);
3148 irq = platform_get_irq_byname(pdev, "bit");
3150 irq = platform_get_irq(pdev, 0);
3154 ret = devm_request_irq(&pdev->dev, irq, coda_irq_handler, 0,
3155 CODA_NAME "-video", dev);
3157 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3162 if (dev->devtype->product == CODA_960) {
3163 irq = platform_get_irq_byname(pdev, "jpeg");
3167 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
3168 coda9_jpeg_irq_handler,
3169 IRQF_ONESHOT, CODA_NAME "-jpeg",
3172 dev_err(&pdev->dev, "failed to request jpeg irq\n");
3177 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
3179 if (IS_ERR(dev->rstc)) {
3180 ret = PTR_ERR(dev->rstc);
3181 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3185 /* Get IRAM pool from device tree */
3186 pool = of_gen_pool_get(np, "iram", 0);
3188 dev_err(&pdev->dev, "iram pool not available\n");
3191 dev->iram_pool = pool;
3193 /* Get vdoa_data if supported by the platform */
3194 dev->vdoa = coda_get_vdoa_data();
3195 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
3196 return -EPROBE_DEFER;
3198 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3202 ratelimit_default_init(&dev->mb_err_rs);
3203 mutex_init(&dev->dev_mutex);
3204 mutex_init(&dev->coda_mutex);
3205 ida_init(&dev->ida);
3207 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3209 /* allocate auxiliary per-device buffers for the BIT processor */
3210 if (dev->devtype->product == CODA_DX6) {
3211 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3212 dev->devtype->workbuf_size, "workbuf",
3215 goto err_v4l2_register;
3218 if (dev->devtype->tempbuf_size) {
3219 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3220 dev->devtype->tempbuf_size, "tempbuf",
3223 goto err_v4l2_register;
3226 dev->iram.size = dev->devtype->iram_size;
3227 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3229 if (!dev->iram.vaddr) {
3230 dev_warn(&pdev->dev, "unable to alloc iram\n");
3232 memset(dev->iram.vaddr, 0, dev->iram.size);
3233 dev->iram.blob.data = dev->iram.vaddr;
3234 dev->iram.blob.size = dev->iram.size;
3235 dev->iram.dentry = debugfs_create_blob("iram", 0644,
3240 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3241 if (!dev->workqueue) {
3242 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3244 goto err_v4l2_register;
3247 platform_set_drvdata(pdev, dev);
3250 * Start activated so we can directly call coda_hw_init in
3251 * coda_fw_callback regardless of whether CONFIG_PM is
3252 * enabled or whether the device is associated with a PM domain.
3254 pm_runtime_get_noresume(&pdev->dev);
3255 pm_runtime_set_active(&pdev->dev);
3256 pm_runtime_enable(&pdev->dev);
3258 ret = coda_firmware_request(dev);
3260 goto err_alloc_workqueue;
3263 err_alloc_workqueue:
3264 pm_runtime_disable(&pdev->dev);
3265 pm_runtime_put_noidle(&pdev->dev);
3266 destroy_workqueue(dev->workqueue);
3268 v4l2_device_unregister(&dev->v4l2_dev);
3272 static int coda_remove(struct platform_device *pdev)
3274 struct coda_dev *dev = platform_get_drvdata(pdev);
3277 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
3278 if (video_get_drvdata(&dev->vfd[i]))
3279 video_unregister_device(&dev->vfd[i]);
3282 v4l2_m2m_release(dev->m2m_dev);
3283 pm_runtime_disable(&pdev->dev);
3284 v4l2_device_unregister(&dev->v4l2_dev);
3285 destroy_workqueue(dev->workqueue);
3286 if (dev->iram.vaddr)
3287 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3289 coda_free_aux_buf(dev, &dev->codebuf);
3290 coda_free_aux_buf(dev, &dev->tempbuf);
3291 coda_free_aux_buf(dev, &dev->workbuf);
3292 debugfs_remove_recursive(dev->debugfs_root);
3293 ida_destroy(&dev->ida);
3298 static int coda_runtime_resume(struct device *dev)
3300 struct coda_dev *cdev = dev_get_drvdata(dev);
3303 if (dev->pm_domain && cdev->codebuf.vaddr) {
3304 ret = coda_hw_init(cdev);
3306 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3313 static const struct dev_pm_ops coda_pm_ops = {
3314 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3317 static struct platform_driver coda_driver = {
3318 .probe = coda_probe,
3319 .remove = coda_remove,
3322 .of_match_table = coda_dt_ids,
3327 module_platform_driver(coda_driver);
3329 MODULE_LICENSE("GPL");
3331 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");