2 * Coda multi-standard codec IP
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/clk.h>
15 #include <linux/debugfs.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/gcd.h>
19 #include <linux/genalloc.h>
20 #include <linux/idr.h>
21 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/kfifo.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/slab.h>
30 #include <linux/videodev2.h>
32 #include <linux/platform_data/media/coda.h>
33 #include <linux/reset.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-event.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-mem2mem.h>
40 #include <media/videobuf2-v4l2.h>
41 #include <media/videobuf2-dma-contig.h>
42 #include <media/videobuf2-vmalloc.h>
47 #define CODA_NAME "coda"
49 #define CODADX6_MAX_INSTANCES 4
50 #define CODA_MAX_FORMATS 4
52 #define CODA_ISRAM_SIZE (2048 * 2)
57 #define S_ALIGN 1 /* multiple of 2 */
58 #define W_ALIGN 1 /* multiple of 2 */
59 #define H_ALIGN 1 /* multiple of 2 */
61 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
64 module_param(coda_debug, int, 0644);
65 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
67 static int disable_tiling;
68 module_param(disable_tiling, int, 0644);
69 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
71 static int disable_vdoa;
72 module_param(disable_vdoa, int, 0644);
73 MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
75 static int enable_bwb = 0;
76 module_param(enable_bwb, int, 0644);
77 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
79 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
81 v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
82 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
83 writel(data, dev->regs_base + reg);
86 unsigned int coda_read(struct coda_dev *dev, u32 reg)
90 data = readl(dev->regs_base + reg);
91 v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
92 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
96 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
97 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
99 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
100 u32 base_cb, base_cr;
102 switch (q_data->fourcc) {
103 case V4L2_PIX_FMT_YUYV:
104 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
105 case V4L2_PIX_FMT_NV12:
106 case V4L2_PIX_FMT_YUV420:
108 base_cb = base_y + q_data->bytesperline * q_data->height;
109 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
111 case V4L2_PIX_FMT_YVU420:
112 /* Switch Cb and Cr for YVU420 format */
113 base_cr = base_y + q_data->bytesperline * q_data->height;
114 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
116 case V4L2_PIX_FMT_YUV422P:
117 base_cb = base_y + q_data->bytesperline * q_data->height;
118 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
121 coda_write(ctx->dev, base_y, reg_y);
122 coda_write(ctx->dev, base_cb, reg_y + 4);
123 coda_write(ctx->dev, base_cr, reg_y + 8);
126 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
127 { mode, src_fourcc, dst_fourcc, max_w, max_h }
130 * Arrays of codecs supported by each given version of Coda:
135 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
137 static const struct coda_codec codadx6_codecs[] = {
138 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
139 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
142 static const struct coda_codec codahx4_codecs[] = {
143 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
144 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
145 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
146 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
149 static const struct coda_codec coda7_codecs[] = {
150 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
151 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
152 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
153 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
154 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
155 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
156 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
159 static const struct coda_codec coda9_codecs[] = {
160 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
161 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
162 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
163 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
164 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
167 struct coda_video_device {
169 enum coda_inst_type type;
170 const struct coda_context_ops *ops;
172 u32 src_formats[CODA_MAX_FORMATS];
173 u32 dst_formats[CODA_MAX_FORMATS];
176 static const struct coda_video_device coda_bit_encoder = {
177 .name = "coda-encoder",
178 .type = CODA_INST_ENCODER,
179 .ops = &coda_bit_encode_ops,
191 static const struct coda_video_device coda_bit_jpeg_encoder = {
192 .name = "coda-jpeg-encoder",
193 .type = CODA_INST_ENCODER,
194 .ops = &coda_bit_encode_ops,
199 V4L2_PIX_FMT_YUV422P,
206 static const struct coda_video_device coda_bit_decoder = {
207 .name = "coda-decoder",
208 .type = CODA_INST_DECODER,
209 .ops = &coda_bit_decode_ops,
220 * If V4L2_PIX_FMT_YUYV should be default,
221 * set_default_params() must be adjusted.
227 static const struct coda_video_device coda_bit_jpeg_decoder = {
228 .name = "coda-jpeg-decoder",
229 .type = CODA_INST_DECODER,
230 .ops = &coda_bit_decode_ops,
238 V4L2_PIX_FMT_YUV422P,
242 static const struct coda_video_device *codadx6_video_devices[] = {
246 static const struct coda_video_device *codahx4_video_devices[] = {
251 static const struct coda_video_device *coda7_video_devices[] = {
252 &coda_bit_jpeg_encoder,
253 &coda_bit_jpeg_decoder,
258 static const struct coda_video_device *coda9_video_devices[] = {
264 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
267 static u32 coda_format_normalize_yuv(u32 fourcc)
270 case V4L2_PIX_FMT_NV12:
271 case V4L2_PIX_FMT_YUV420:
272 case V4L2_PIX_FMT_YVU420:
273 case V4L2_PIX_FMT_YUV422P:
274 case V4L2_PIX_FMT_YUYV:
275 return V4L2_PIX_FMT_YUV420;
281 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
282 int src_fourcc, int dst_fourcc)
284 const struct coda_codec *codecs = dev->devtype->codecs;
285 int num_codecs = dev->devtype->num_codecs;
288 src_fourcc = coda_format_normalize_yuv(src_fourcc);
289 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
290 if (src_fourcc == dst_fourcc)
293 for (k = 0; k < num_codecs; k++) {
294 if (codecs[k].src_fourcc == src_fourcc &&
295 codecs[k].dst_fourcc == dst_fourcc)
305 static void coda_get_max_dimensions(struct coda_dev *dev,
306 const struct coda_codec *codec,
307 int *max_w, int *max_h)
309 const struct coda_codec *codecs = dev->devtype->codecs;
310 int num_codecs = dev->devtype->num_codecs;
318 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
319 w = max(w, codecs[k].max_w);
320 h = max(h, codecs[k].max_h);
330 static const struct coda_video_device *to_coda_video_device(struct video_device
333 struct coda_dev *dev = video_get_drvdata(vdev);
334 unsigned int i = vdev - dev->vfd;
336 if (i >= dev->devtype->num_vdevs)
339 return dev->devtype->vdevs[i];
342 const char *coda_product_name(int product)
356 snprintf(buf, sizeof(buf), "(0x%04x)", product);
361 static struct vdoa_data *coda_get_vdoa_data(void)
363 struct device_node *vdoa_node;
364 struct platform_device *vdoa_pdev;
365 struct vdoa_data *vdoa_data = NULL;
367 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
371 vdoa_pdev = of_find_device_by_node(vdoa_node);
375 vdoa_data = platform_get_drvdata(vdoa_pdev);
377 vdoa_data = ERR_PTR(-EPROBE_DEFER);
380 of_node_put(vdoa_node);
386 * V4L2 ioctl() operations.
388 static int coda_querycap(struct file *file, void *priv,
389 struct v4l2_capability *cap)
391 struct coda_ctx *ctx = fh_to_ctx(priv);
393 strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
394 strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
396 strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
397 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
398 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
403 static int coda_enum_fmt(struct file *file, void *priv,
404 struct v4l2_fmtdesc *f)
406 struct video_device *vdev = video_devdata(file);
407 const struct coda_video_device *cvd = to_coda_video_device(vdev);
408 struct coda_ctx *ctx = fh_to_ctx(priv);
411 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
412 formats = cvd->src_formats;
413 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
414 formats = cvd->dst_formats;
418 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
421 /* Skip YUYV if the vdoa is not available */
422 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
423 formats[f->index] == V4L2_PIX_FMT_YUYV)
426 f->pixelformat = formats[f->index];
431 static int coda_g_fmt(struct file *file, void *priv,
432 struct v4l2_format *f)
434 struct coda_q_data *q_data;
435 struct coda_ctx *ctx = fh_to_ctx(priv);
437 q_data = get_q_data(ctx, f->type);
441 f->fmt.pix.field = V4L2_FIELD_NONE;
442 f->fmt.pix.pixelformat = q_data->fourcc;
443 f->fmt.pix.width = q_data->width;
444 f->fmt.pix.height = q_data->height;
445 f->fmt.pix.bytesperline = q_data->bytesperline;
447 f->fmt.pix.sizeimage = q_data->sizeimage;
448 f->fmt.pix.colorspace = ctx->colorspace;
449 f->fmt.pix.xfer_func = ctx->xfer_func;
450 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
451 f->fmt.pix.quantization = ctx->quantization;
456 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
458 struct coda_q_data *q_data;
462 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
463 formats = ctx->cvd->src_formats;
464 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
465 formats = ctx->cvd->dst_formats;
469 for (i = 0; i < CODA_MAX_FORMATS; i++) {
470 /* Skip YUYV if the vdoa is not available */
471 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
472 formats[i] == V4L2_PIX_FMT_YUYV)
475 if (formats[i] == f->fmt.pix.pixelformat) {
476 f->fmt.pix.pixelformat = formats[i];
481 /* Fall back to currently set pixelformat */
482 q_data = get_q_data(ctx, f->type);
483 f->fmt.pix.pixelformat = q_data->fourcc;
488 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
493 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
504 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
505 f->fmt.pix.height, f->fmt.pix.pixelformat);
515 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
516 u32 width, u32 height)
519 * This is a rough estimate for sensible compressed buffer
520 * sizes (between 1 and 16 bits per pixel). This could be
521 * improved by better format specific worst case estimates.
523 return round_up(clamp(sizeimage, width * height / 8,
524 width * height * 2), PAGE_SIZE);
527 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
528 struct v4l2_format *f)
530 struct coda_dev *dev = ctx->dev;
531 unsigned int max_w, max_h;
532 enum v4l2_field field;
534 field = f->fmt.pix.field;
535 if (field == V4L2_FIELD_ANY)
536 field = V4L2_FIELD_NONE;
537 else if (V4L2_FIELD_NONE != field)
540 /* V4L2 specification suggests the driver corrects the format struct
541 * if any of the dimensions is unsupported */
542 f->fmt.pix.field = field;
544 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
545 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
546 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
549 switch (f->fmt.pix.pixelformat) {
550 case V4L2_PIX_FMT_NV12:
551 case V4L2_PIX_FMT_YUV420:
552 case V4L2_PIX_FMT_YVU420:
554 * Frame stride must be at least multiple of 8,
555 * but multiple of 16 for h.264 or JPEG 4:2:x
557 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
558 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
559 f->fmt.pix.height * 3 / 2;
561 case V4L2_PIX_FMT_YUYV:
562 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
563 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
566 case V4L2_PIX_FMT_YUV422P:
567 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
568 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
569 f->fmt.pix.height * 2;
571 case V4L2_PIX_FMT_JPEG:
572 case V4L2_PIX_FMT_H264:
573 case V4L2_PIX_FMT_MPEG4:
574 case V4L2_PIX_FMT_MPEG2:
575 f->fmt.pix.bytesperline = 0;
576 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
577 f->fmt.pix.sizeimage,
588 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
589 struct v4l2_format *f)
591 struct coda_ctx *ctx = fh_to_ctx(priv);
592 const struct coda_q_data *q_data_src;
593 const struct coda_codec *codec;
594 struct vb2_queue *src_vq;
598 ret = coda_try_pixelformat(ctx, f);
602 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
605 * If the source format is already fixed, only allow the same output
608 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
609 if (vb2_is_streaming(src_vq)) {
610 f->fmt.pix.width = q_data_src->width;
611 f->fmt.pix.height = q_data_src->height;
614 f->fmt.pix.colorspace = ctx->colorspace;
615 f->fmt.pix.xfer_func = ctx->xfer_func;
616 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
617 f->fmt.pix.quantization = ctx->quantization;
619 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
620 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
621 f->fmt.pix.pixelformat);
625 ret = coda_try_fmt(ctx, codec, f);
629 /* The h.264 decoder only returns complete 16x16 macroblocks */
630 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
631 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
632 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
633 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
634 f->fmt.pix.height * 3 / 2;
636 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
640 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
644 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
645 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
653 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
655 enum v4l2_colorspace colorspace;
657 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
658 colorspace = V4L2_COLORSPACE_JPEG;
659 else if (fmt->width <= 720 && fmt->height <= 576)
660 colorspace = V4L2_COLORSPACE_SMPTE170M;
662 colorspace = V4L2_COLORSPACE_REC709;
664 fmt->colorspace = colorspace;
665 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
666 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
667 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
670 static int coda_try_fmt_vid_out(struct file *file, void *priv,
671 struct v4l2_format *f)
673 struct coda_ctx *ctx = fh_to_ctx(priv);
674 struct coda_dev *dev = ctx->dev;
675 const struct coda_q_data *q_data_dst;
676 const struct coda_codec *codec;
679 ret = coda_try_pixelformat(ctx, f);
683 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
684 coda_set_default_colorspace(&f->fmt.pix);
686 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
687 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
689 return coda_try_fmt(ctx, codec, f);
692 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
695 struct coda_q_data *q_data;
696 struct vb2_queue *vq;
698 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
702 q_data = get_q_data(ctx, f->type);
706 if (vb2_is_busy(vq)) {
707 v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
708 __func__, v4l2_type_names[f->type], vq->num_buffers);
712 q_data->fourcc = f->fmt.pix.pixelformat;
713 q_data->width = f->fmt.pix.width;
714 q_data->height = f->fmt.pix.height;
715 q_data->bytesperline = f->fmt.pix.bytesperline;
716 q_data->sizeimage = f->fmt.pix.sizeimage;
720 q_data->rect.left = 0;
721 q_data->rect.top = 0;
722 q_data->rect.width = f->fmt.pix.width;
723 q_data->rect.height = f->fmt.pix.height;
726 switch (f->fmt.pix.pixelformat) {
727 case V4L2_PIX_FMT_YUYV:
728 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
730 case V4L2_PIX_FMT_NV12:
731 if (!disable_tiling && ctx->dev->devtype->product == CODA_960) {
732 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
735 /* else fall through */
736 case V4L2_PIX_FMT_YUV420:
737 case V4L2_PIX_FMT_YVU420:
738 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
744 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
745 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
747 vdoa_context_configure(ctx->vdoa,
748 round_up(f->fmt.pix.width, 16),
750 f->fmt.pix.pixelformat);
752 ctx->use_vdoa = false;
754 coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
755 v4l2_type_names[f->type], q_data->width, q_data->height,
756 (char *)&q_data->fourcc,
757 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
762 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
763 struct v4l2_format *f)
765 struct coda_ctx *ctx = fh_to_ctx(priv);
766 struct coda_q_data *q_data_src;
767 const struct coda_codec *codec;
771 ret = coda_try_fmt_vid_cap(file, priv, f);
775 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
778 r.width = q_data_src->width;
779 r.height = q_data_src->height;
781 ret = coda_s_fmt(ctx, f, &r);
785 if (ctx->inst_type != CODA_INST_ENCODER)
788 /* Setting the coded format determines the selected codec */
789 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
790 f->fmt.pix.pixelformat);
792 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
797 ctx->colorspace = f->fmt.pix.colorspace;
798 ctx->xfer_func = f->fmt.pix.xfer_func;
799 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
800 ctx->quantization = f->fmt.pix.quantization;
805 static int coda_s_fmt_vid_out(struct file *file, void *priv,
806 struct v4l2_format *f)
808 struct coda_ctx *ctx = fh_to_ctx(priv);
809 const struct coda_codec *codec;
810 struct v4l2_format f_cap;
811 struct vb2_queue *dst_vq;
814 ret = coda_try_fmt_vid_out(file, priv, f);
818 ret = coda_s_fmt(ctx, f, NULL);
822 ctx->colorspace = f->fmt.pix.colorspace;
823 ctx->xfer_func = f->fmt.pix.xfer_func;
824 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
825 ctx->quantization = f->fmt.pix.quantization;
827 if (ctx->inst_type != CODA_INST_DECODER)
830 /* Setting the coded format determines the selected codec */
831 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
832 V4L2_PIX_FMT_YUV420);
834 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
839 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
844 * Setting the capture queue format is not possible while the capture
845 * queue is still busy. This is not an error, but the user will have to
846 * make sure themselves that the capture format is set correctly before
847 * starting the output queue again.
849 if (vb2_is_busy(dst_vq))
852 memset(&f_cap, 0, sizeof(f_cap));
853 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
854 coda_g_fmt(file, priv, &f_cap);
855 f_cap.fmt.pix.width = f->fmt.pix.width;
856 f_cap.fmt.pix.height = f->fmt.pix.height;
858 return coda_s_fmt_vid_cap(file, priv, &f_cap);
861 static int coda_reqbufs(struct file *file, void *priv,
862 struct v4l2_requestbuffers *rb)
864 struct coda_ctx *ctx = fh_to_ctx(priv);
867 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
872 * Allow to allocate instance specific per-context buffers, such as
873 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
875 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
876 return ctx->ops->reqbufs(ctx, rb);
881 static int coda_qbuf(struct file *file, void *priv,
882 struct v4l2_buffer *buf)
884 struct coda_ctx *ctx = fh_to_ctx(priv);
886 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
889 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
890 struct vb2_v4l2_buffer *buf)
892 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
893 (buf->sequence == (ctx->qsequence - 1)));
896 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
897 enum vb2_buffer_state state)
899 const struct v4l2_event eos_event = {
900 .type = V4L2_EVENT_EOS
903 if (coda_buf_is_end_of_stream(ctx, buf)) {
904 buf->flags |= V4L2_BUF_FLAG_LAST;
906 v4l2_event_queue_fh(&ctx->fh, &eos_event);
909 v4l2_m2m_buf_done(buf, state);
912 static int coda_g_selection(struct file *file, void *fh,
913 struct v4l2_selection *s)
915 struct coda_ctx *ctx = fh_to_ctx(fh);
916 struct coda_q_data *q_data;
917 struct v4l2_rect r, *rsel;
919 q_data = get_q_data(ctx, s->type);
925 r.width = q_data->width;
926 r.height = q_data->height;
927 rsel = &q_data->rect;
930 case V4L2_SEL_TGT_CROP_DEFAULT:
931 case V4L2_SEL_TGT_CROP_BOUNDS:
934 case V4L2_SEL_TGT_CROP:
935 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
938 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
939 case V4L2_SEL_TGT_COMPOSE_PADDED:
942 case V4L2_SEL_TGT_COMPOSE:
943 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
944 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
956 static int coda_s_selection(struct file *file, void *fh,
957 struct v4l2_selection *s)
959 struct coda_ctx *ctx = fh_to_ctx(fh);
960 struct coda_q_data *q_data;
963 case V4L2_SEL_TGT_CROP:
964 if (ctx->inst_type == CODA_INST_ENCODER &&
965 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
966 q_data = get_q_data(ctx, s->type);
972 s->r.width = clamp(s->r.width, 2U, q_data->width);
973 s->r.height = clamp(s->r.height, 2U, q_data->height);
975 if (s->flags & V4L2_SEL_FLAG_LE) {
976 s->r.width = round_up(s->r.width, 2);
977 s->r.height = round_up(s->r.height, 2);
979 s->r.width = round_down(s->r.width, 2);
980 s->r.height = round_down(s->r.height, 2);
985 coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
986 s->r.width, s->r.height);
990 /* else fall through */
991 case V4L2_SEL_TGT_NATIVE_SIZE:
992 case V4L2_SEL_TGT_COMPOSE:
993 return coda_g_selection(file, fh, s);
995 /* v4l2-compliance expects this to fail for read-only targets */
1000 static int coda_try_encoder_cmd(struct file *file, void *fh,
1001 struct v4l2_encoder_cmd *ec)
1003 struct coda_ctx *ctx = fh_to_ctx(fh);
1005 if (ctx->inst_type != CODA_INST_ENCODER)
1008 if (ec->cmd != V4L2_ENC_CMD_STOP)
1011 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
1017 static int coda_encoder_cmd(struct file *file, void *fh,
1018 struct v4l2_encoder_cmd *ec)
1020 struct coda_ctx *ctx = fh_to_ctx(fh);
1021 struct vb2_queue *dst_vq;
1024 ret = coda_try_encoder_cmd(file, fh, ec);
1028 /* Set the stream-end flag on this context */
1029 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1031 /* If there is no buffer in flight, wake up */
1032 if (!ctx->streamon_out || ctx->qsequence == ctx->osequence) {
1033 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1034 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1035 dst_vq->last_buffer_dequeued = true;
1036 wake_up(&dst_vq->done_wq);
1042 static int coda_try_decoder_cmd(struct file *file, void *fh,
1043 struct v4l2_decoder_cmd *dc)
1045 struct coda_ctx *ctx = fh_to_ctx(fh);
1047 if (ctx->inst_type != CODA_INST_DECODER)
1050 if (dc->cmd != V4L2_DEC_CMD_STOP)
1053 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
1056 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
1062 static int coda_decoder_cmd(struct file *file, void *fh,
1063 struct v4l2_decoder_cmd *dc)
1065 struct coda_ctx *ctx = fh_to_ctx(fh);
1068 ret = coda_try_decoder_cmd(file, fh, dc);
1072 /* Set the stream-end flag on this context */
1073 coda_bit_stream_end_flag(ctx);
1075 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1080 static int coda_enum_framesizes(struct file *file, void *fh,
1081 struct v4l2_frmsizeenum *fsize)
1083 struct coda_ctx *ctx = fh_to_ctx(fh);
1084 struct coda_q_data *q_data_dst;
1085 const struct coda_codec *codec;
1087 if (ctx->inst_type != CODA_INST_ENCODER)
1093 if (coda_format_normalize_yuv(fsize->pixel_format) ==
1094 V4L2_PIX_FMT_YUV420) {
1095 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1096 codec = coda_find_codec(ctx->dev, fsize->pixel_format,
1097 q_data_dst->fourcc);
1099 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1100 fsize->pixel_format);
1105 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1106 fsize->stepwise.min_width = MIN_W;
1107 fsize->stepwise.max_width = codec->max_w;
1108 fsize->stepwise.step_width = 1;
1109 fsize->stepwise.min_height = MIN_H;
1110 fsize->stepwise.max_height = codec->max_h;
1111 fsize->stepwise.step_height = 1;
1116 static int coda_enum_frameintervals(struct file *file, void *fh,
1117 struct v4l2_frmivalenum *f)
1119 struct coda_ctx *ctx = fh_to_ctx(fh);
1125 /* Disallow YUYV if the vdoa is not available */
1126 if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1129 for (i = 0; i < CODA_MAX_FORMATS; i++) {
1130 if (f->pixel_format == ctx->cvd->src_formats[i] ||
1131 f->pixel_format == ctx->cvd->dst_formats[i])
1134 if (i == CODA_MAX_FORMATS)
1137 f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1138 f->stepwise.min.numerator = 1;
1139 f->stepwise.min.denominator = 65535;
1140 f->stepwise.max.numerator = 65536;
1141 f->stepwise.max.denominator = 1;
1142 f->stepwise.step.numerator = 1;
1143 f->stepwise.step.denominator = 1;
1148 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1150 struct coda_ctx *ctx = fh_to_ctx(fh);
1151 struct v4l2_fract *tpf;
1153 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1156 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1157 tpf = &a->parm.output.timeperframe;
1158 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1159 tpf->numerator = 1 + (ctx->params.framerate >>
1160 CODA_FRATE_DIV_OFFSET);
1166 * Approximate timeperframe v4l2_fract with values that can be written
1167 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1169 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1171 struct v4l2_fract s = *timeperframe;
1172 struct v4l2_fract f0;
1173 struct v4l2_fract f1 = { 1, 0 };
1174 struct v4l2_fract f2 = { 0, 1 };
1175 unsigned int i, div, s_denominator;
1177 /* Lower bound is 1/65535 */
1178 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1179 timeperframe->numerator = 1;
1180 timeperframe->denominator = 65535;
1184 /* Upper bound is 65536/1 */
1185 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1186 timeperframe->numerator = 65536;
1187 timeperframe->denominator = 1;
1191 /* Reduce fraction to lowest terms */
1192 div = gcd(s.numerator, s.denominator);
1195 s.denominator /= div;
1198 if (s.numerator <= 65536 && s.denominator < 65536) {
1203 /* Find successive convergents from continued fraction expansion */
1204 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1208 /* Stop when f2 exactly equals timeperframe */
1209 if (s.numerator == 0)
1212 i = s.denominator / s.numerator;
1214 f2.numerator = f0.numerator + i * f1.numerator;
1215 f2.denominator = f0.denominator + i * f2.denominator;
1217 s_denominator = s.numerator;
1218 s.numerator = s.denominator % s.numerator;
1219 s.denominator = s_denominator;
1225 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1227 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1228 timeperframe->denominator;
1231 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1233 struct coda_ctx *ctx = fh_to_ctx(fh);
1234 struct v4l2_fract *tpf;
1236 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1239 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1240 tpf = &a->parm.output.timeperframe;
1241 coda_approximate_timeperframe(tpf);
1242 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1247 static int coda_subscribe_event(struct v4l2_fh *fh,
1248 const struct v4l2_event_subscription *sub)
1250 switch (sub->type) {
1251 case V4L2_EVENT_EOS:
1252 return v4l2_event_subscribe(fh, sub, 0, NULL);
1254 return v4l2_ctrl_subscribe_event(fh, sub);
1258 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1259 .vidioc_querycap = coda_querycap,
1261 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1262 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1263 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1264 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1266 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1267 .vidioc_g_fmt_vid_out = coda_g_fmt,
1268 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1269 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1271 .vidioc_reqbufs = coda_reqbufs,
1272 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1274 .vidioc_qbuf = coda_qbuf,
1275 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1276 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
1277 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1278 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1280 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1281 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1283 .vidioc_g_selection = coda_g_selection,
1284 .vidioc_s_selection = coda_s_selection,
1286 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1287 .vidioc_encoder_cmd = coda_encoder_cmd,
1288 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
1289 .vidioc_decoder_cmd = coda_decoder_cmd,
1291 .vidioc_g_parm = coda_g_parm,
1292 .vidioc_s_parm = coda_s_parm,
1294 .vidioc_enum_framesizes = coda_enum_framesizes,
1295 .vidioc_enum_frameintervals = coda_enum_frameintervals,
1297 .vidioc_subscribe_event = coda_subscribe_event,
1298 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1302 * Mem-to-mem operations.
1305 static void coda_device_run(void *m2m_priv)
1307 struct coda_ctx *ctx = m2m_priv;
1308 struct coda_dev *dev = ctx->dev;
1310 queue_work(dev->workqueue, &ctx->pic_run_work);
1313 static void coda_pic_run_work(struct work_struct *work)
1315 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1316 struct coda_dev *dev = ctx->dev;
1319 mutex_lock(&ctx->buffer_mutex);
1320 mutex_lock(&dev->coda_mutex);
1322 ret = ctx->ops->prepare_run(ctx);
1323 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1324 mutex_unlock(&dev->coda_mutex);
1325 mutex_unlock(&ctx->buffer_mutex);
1326 /* job_finish scheduled by prepare_decode */
1330 if (!wait_for_completion_timeout(&ctx->completion,
1331 msecs_to_jiffies(1000))) {
1332 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1338 if (ctx->ops->run_timeout)
1339 ctx->ops->run_timeout(ctx);
1340 } else if (!ctx->aborting) {
1341 ctx->ops->finish_run(ctx);
1344 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1345 ctx->ops->seq_end_work)
1346 queue_work(dev->workqueue, &ctx->seq_end_work);
1348 mutex_unlock(&dev->coda_mutex);
1349 mutex_unlock(&ctx->buffer_mutex);
1351 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1354 static int coda_job_ready(void *m2m_priv)
1356 struct coda_ctx *ctx = m2m_priv;
1357 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1360 * For both 'P' and 'key' frame cases 1 picture
1361 * and 1 frame are needed. In the decoder case,
1362 * the compressed frame can be in the bitstream.
1364 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1365 coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
1369 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1370 coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
1374 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1375 bool stream_end = ctx->bit_stream_param &
1376 CODA_BIT_STREAM_END_FLAG;
1377 int num_metas = ctx->num_metas;
1378 struct coda_buffer_meta *meta;
1381 count = hweight32(ctx->frm_dis_flg);
1382 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1384 "not ready: all internal buffers in use: %d/%d (0x%x)",
1385 count, ctx->num_internal_frames,
1390 if (ctx->hold && !src_bufs) {
1392 "not ready: on hold for more buffers.\n");
1396 if (!stream_end && (num_metas + src_bufs) < 2) {
1398 "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1399 num_metas, src_bufs);
1403 meta = list_first_entry(&ctx->buffer_meta_list,
1404 struct coda_buffer_meta, list);
1405 if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1408 "not ready: not enough bitstream data to read past %u (%u)\n",
1409 meta->end, ctx->bitstream_fifo.kfifo.in);
1414 if (ctx->aborting) {
1415 coda_dbg(1, ctx, "not ready: aborting\n");
1419 coda_dbg(2, ctx, "job ready\n");
1424 static void coda_job_abort(void *priv)
1426 struct coda_ctx *ctx = priv;
1430 coda_dbg(1, ctx, "job abort\n");
1433 static const struct v4l2_m2m_ops coda_m2m_ops = {
1434 .device_run = coda_device_run,
1435 .job_ready = coda_job_ready,
1436 .job_abort = coda_job_abort,
1439 static void set_default_params(struct coda_ctx *ctx)
1441 unsigned int max_w, max_h, usize, csize;
1443 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1444 ctx->cvd->dst_formats[0]);
1445 max_w = min(ctx->codec->max_w, 1920U);
1446 max_h = min(ctx->codec->max_h, 1088U);
1447 usize = max_w * max_h * 3 / 2;
1448 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1450 ctx->params.codec_mode = ctx->codec->mode;
1451 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1452 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1454 ctx->colorspace = V4L2_COLORSPACE_REC709;
1455 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1456 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1457 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1458 ctx->params.framerate = 30;
1460 /* Default formats for output and input queues */
1461 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1462 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1463 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1464 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1465 ctx->q_data[V4L2_M2M_DST].width = max_w;
1466 ctx->q_data[V4L2_M2M_DST].height = max_h;
1467 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1468 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1469 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1470 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1471 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1473 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1474 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1475 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1476 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1478 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1479 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1480 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1481 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1484 * Since the RBC2AXI logic only supports a single chroma plane,
1485 * macroblock tiling only works for to NV12 pixel format.
1487 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1493 static int coda_queue_setup(struct vb2_queue *vq,
1494 unsigned int *nbuffers, unsigned int *nplanes,
1495 unsigned int sizes[], struct device *alloc_devs[])
1497 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1498 struct coda_q_data *q_data;
1501 q_data = get_q_data(ctx, vq->type);
1502 size = q_data->sizeimage;
1505 return sizes[0] < size ? -EINVAL : 0;
1510 coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1516 static int coda_buf_prepare(struct vb2_buffer *vb)
1518 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1519 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1520 struct coda_q_data *q_data;
1522 q_data = get_q_data(ctx, vb->vb2_queue->type);
1523 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1524 if (vbuf->field == V4L2_FIELD_ANY)
1525 vbuf->field = V4L2_FIELD_NONE;
1526 if (vbuf->field != V4L2_FIELD_NONE) {
1527 v4l2_warn(&ctx->dev->v4l2_dev,
1528 "%s field isn't supported\n", __func__);
1533 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1534 v4l2_warn(&ctx->dev->v4l2_dev,
1535 "%s data will not fit into plane (%lu < %lu)\n",
1536 __func__, vb2_plane_size(vb, 0),
1537 (long)q_data->sizeimage);
1544 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1549 v4l2_ctrl_lock(ctrl);
1552 * Extend the control range if the parsed stream contains a known but
1553 * unsupported value or level.
1555 if (value > ctrl->maximum) {
1556 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1557 ctrl->menu_skip_mask & ~(1 << value),
1558 ctrl->default_value);
1559 } else if (value < ctrl->minimum) {
1560 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1561 ctrl->menu_skip_mask & ~(1 << value),
1562 ctrl->default_value);
1565 __v4l2_ctrl_s_ctrl(ctrl, value);
1567 v4l2_ctrl_unlock(ctrl);
1570 static void coda_update_h264_profile_ctrl(struct coda_ctx *ctx)
1572 const char * const *profile_names;
1575 profile = coda_h264_profile(ctx->params.h264_profile_idc);
1577 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Profile: %u\n",
1578 ctx->params.h264_profile_idc);
1582 coda_update_menu_ctrl(ctx->h264_profile_ctrl, profile);
1584 profile_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_PROFILE);
1586 coda_dbg(1, ctx, "Parsed H264 Profile: %s\n", profile_names[profile]);
1589 static void coda_update_h264_level_ctrl(struct coda_ctx *ctx)
1591 const char * const *level_names;
1594 level = coda_h264_level(ctx->params.h264_level_idc);
1596 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Level: %u\n",
1597 ctx->params.h264_level_idc);
1601 coda_update_menu_ctrl(ctx->h264_level_ctrl, level);
1603 level_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL);
1605 coda_dbg(1, ctx, "Parsed H264 Level: %s\n", level_names[level]);
1608 static void coda_buf_queue(struct vb2_buffer *vb)
1610 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1611 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1612 struct vb2_queue *vq = vb->vb2_queue;
1613 struct coda_q_data *q_data;
1615 q_data = get_q_data(ctx, vb->vb2_queue->type);
1618 * In the decoder case, immediately try to copy the buffer into the
1619 * bitstream ringbuffer and mark it as ready to be dequeued.
1621 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1623 * For backwards compatibility, queuing an empty buffer marks
1626 if (vb2_get_plane_payload(vb, 0) == 0)
1627 coda_bit_stream_end_flag(ctx);
1629 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1631 * Unless already done, try to obtain profile_idc and
1632 * level_idc from the SPS header. This allows to decide
1633 * whether to enable reordering during sequence
1636 if (!ctx->params.h264_profile_idc) {
1637 coda_sps_parse_profile(ctx, vb);
1638 coda_update_h264_profile_ctrl(ctx);
1639 coda_update_h264_level_ctrl(ctx);
1643 mutex_lock(&ctx->bitstream_mutex);
1644 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1645 if (vb2_is_streaming(vb->vb2_queue))
1646 /* This set buf->sequence = ctx->qsequence++ */
1647 coda_fill_bitstream(ctx, NULL);
1648 mutex_unlock(&ctx->bitstream_mutex);
1650 if (ctx->inst_type == CODA_INST_ENCODER &&
1651 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1652 vbuf->sequence = ctx->qsequence++;
1653 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1657 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1658 size_t size, const char *name, struct dentry *parent)
1660 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1663 v4l2_err(&dev->v4l2_dev,
1664 "Failed to allocate %s buffer of size %zu\n",
1671 if (name && parent) {
1672 buf->blob.data = buf->vaddr;
1673 buf->blob.size = size;
1674 buf->dentry = debugfs_create_blob(name, 0644, parent,
1677 dev_warn(&dev->plat_dev->dev,
1678 "failed to create debugfs entry %s\n", name);
1684 void coda_free_aux_buf(struct coda_dev *dev,
1685 struct coda_aux_buf *buf)
1688 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1689 buf->vaddr, buf->paddr);
1692 debugfs_remove(buf->dentry);
1697 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1699 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1700 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1701 struct coda_q_data *q_data_src, *q_data_dst;
1702 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1703 struct vb2_v4l2_buffer *buf;
1704 struct list_head list;
1710 coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1712 INIT_LIST_HEAD(&list);
1714 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1715 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1716 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1717 /* copy the buffers that were queued before streamon */
1718 mutex_lock(&ctx->bitstream_mutex);
1719 coda_fill_bitstream(ctx, &list);
1720 mutex_unlock(&ctx->bitstream_mutex);
1722 if (coda_get_bitstream_payload(ctx) < 512) {
1728 ctx->streamon_out = 1;
1730 ctx->streamon_cap = 1;
1733 /* Don't start the coda unless both queues are on */
1734 if (!(ctx->streamon_out && ctx->streamon_cap))
1737 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1738 if ((q_data_src->rect.width != q_data_dst->width &&
1739 round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
1740 (q_data_src->rect.height != q_data_dst->height &&
1741 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
1742 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1743 q_data_src->rect.width, q_data_src->rect.height,
1744 q_data_dst->width, q_data_dst->height);
1749 /* Allow BIT decoder device_run with no new buffers queued */
1750 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1751 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1753 ctx->gopcounter = ctx->params.gop_size - 1;
1755 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1756 ctx->params.gop_size = 1;
1757 ctx->gopcounter = ctx->params.gop_size - 1;
1759 ret = ctx->ops->start_streaming(ctx);
1760 if (ctx->inst_type == CODA_INST_DECODER) {
1768 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1769 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1770 list_del(&m2m_buf->list);
1771 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
1777 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1778 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1779 list_del(&m2m_buf->list);
1780 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
1782 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1783 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1785 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1786 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1791 static void coda_stop_streaming(struct vb2_queue *q)
1793 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1794 struct coda_dev *dev = ctx->dev;
1795 struct vb2_v4l2_buffer *buf;
1798 stop = ctx->streamon_out && ctx->streamon_cap;
1800 coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
1802 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1803 ctx->streamon_out = 0;
1805 coda_bit_stream_end_flag(ctx);
1809 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1810 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1812 ctx->streamon_cap = 0;
1815 ctx->sequence_offset = 0;
1817 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1818 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1822 struct coda_buffer_meta *meta;
1824 if (ctx->ops->seq_end_work) {
1825 queue_work(dev->workqueue, &ctx->seq_end_work);
1826 flush_work(&ctx->seq_end_work);
1828 spin_lock(&ctx->buffer_meta_lock);
1829 while (!list_empty(&ctx->buffer_meta_list)) {
1830 meta = list_first_entry(&ctx->buffer_meta_list,
1831 struct coda_buffer_meta, list);
1832 list_del(&meta->list);
1836 spin_unlock(&ctx->buffer_meta_lock);
1837 kfifo_init(&ctx->bitstream_fifo,
1838 ctx->bitstream.vaddr, ctx->bitstream.size);
1839 ctx->runcounter = 0;
1844 if (!ctx->streamon_out && !ctx->streamon_cap)
1845 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1848 static const struct vb2_ops coda_qops = {
1849 .queue_setup = coda_queue_setup,
1850 .buf_prepare = coda_buf_prepare,
1851 .buf_queue = coda_buf_queue,
1852 .start_streaming = coda_start_streaming,
1853 .stop_streaming = coda_stop_streaming,
1854 .wait_prepare = vb2_ops_wait_prepare,
1855 .wait_finish = vb2_ops_wait_finish,
1858 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1860 struct coda_ctx *ctx =
1861 container_of(ctrl->handler, struct coda_ctx, ctrls);
1863 coda_dbg(1, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
1864 ctrl->id, ctrl->name, ctrl->val);
1867 case V4L2_CID_HFLIP:
1869 ctx->params.rot_mode |= CODA_MIR_HOR;
1871 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1873 case V4L2_CID_VFLIP:
1875 ctx->params.rot_mode |= CODA_MIR_VER;
1877 ctx->params.rot_mode &= ~CODA_MIR_VER;
1879 case V4L2_CID_MPEG_VIDEO_BITRATE:
1880 ctx->params.bitrate = ctrl->val / 1000;
1882 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1883 ctx->params.gop_size = ctrl->val;
1885 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1886 ctx->params.h264_intra_qp = ctrl->val;
1888 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1889 ctx->params.h264_inter_qp = ctrl->val;
1891 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1892 ctx->params.h264_min_qp = ctrl->val;
1894 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1895 ctx->params.h264_max_qp = ctrl->val;
1897 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1898 ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
1900 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1901 ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
1903 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1904 ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
1906 case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
1907 ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
1909 case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
1910 ctx->params.h264_chroma_qp_index_offset = ctrl->val;
1912 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1913 /* TODO: switch between baseline and constrained baseline */
1914 if (ctx->inst_type == CODA_INST_ENCODER)
1915 ctx->params.h264_profile_idc = 66;
1917 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1918 /* nothing to do, this is set by the encoder */
1920 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1921 ctx->params.mpeg4_intra_qp = ctrl->val;
1923 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1924 ctx->params.mpeg4_inter_qp = ctrl->val;
1926 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1927 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1928 /* nothing to do, these are fixed */
1930 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1931 ctx->params.slice_mode = ctrl->val;
1933 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1934 ctx->params.slice_max_mb = ctrl->val;
1936 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1937 ctx->params.slice_max_bits = ctrl->val * 8;
1939 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1941 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1942 ctx->params.intra_refresh = ctrl->val;
1944 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1945 ctx->params.force_ipicture = true;
1947 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1948 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1950 case V4L2_CID_JPEG_RESTART_INTERVAL:
1951 ctx->params.jpeg_restart_interval = ctrl->val;
1953 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1954 ctx->params.vbv_delay = ctrl->val;
1956 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1957 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1960 coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
1961 ctrl->id, ctrl->val);
1968 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1969 .s_ctrl = coda_s_ctrl,
1972 static void coda_encode_ctrls(struct coda_ctx *ctx)
1974 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
1976 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1977 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1978 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1979 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
1980 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1981 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1982 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1983 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1984 if (ctx->dev->devtype->product != CODA_960) {
1985 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1986 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1988 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1989 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1990 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1991 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
1992 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1993 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
1994 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1995 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1996 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
1997 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1998 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1999 V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
2001 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2002 V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
2003 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2004 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2005 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2006 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2007 if (ctx->dev->devtype->product == CODA_HX4 ||
2008 ctx->dev->devtype->product == CODA_7541) {
2009 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2010 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2011 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2012 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2013 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2014 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2015 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2017 if (ctx->dev->devtype->product == CODA_960) {
2018 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2019 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2020 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
2021 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2022 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2023 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2024 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2025 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
2026 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2028 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2029 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2030 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2031 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2032 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2033 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2034 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2035 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
2036 if (ctx->dev->devtype->product == CODA_HX4 ||
2037 ctx->dev->devtype->product == CODA_7541 ||
2038 ctx->dev->devtype->product == CODA_960) {
2039 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2040 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2041 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2042 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2043 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2045 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2046 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
2047 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2048 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
2049 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2050 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
2051 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2052 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2054 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2055 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2056 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2057 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2058 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2059 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2060 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2061 1920 * 1088 / 256, 1, 0);
2062 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2063 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2065 * The maximum VBV size value is 0x7fffffff bits,
2066 * one bit less than 262144 KiB
2068 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2069 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
2072 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2074 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2075 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2076 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2077 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2080 static void coda_decode_ctrls(struct coda_ctx *ctx)
2084 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2085 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2086 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2087 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
2088 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2089 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2090 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2091 if (ctx->h264_profile_ctrl)
2092 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2094 if (ctx->dev->devtype->product == CODA_HX4 ||
2095 ctx->dev->devtype->product == CODA_7541)
2096 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2097 else if (ctx->dev->devtype->product == CODA_960)
2098 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
2101 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2102 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
2103 if (ctx->h264_level_ctrl)
2104 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2107 static int coda_ctrls_setup(struct coda_ctx *ctx)
2109 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2111 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2112 V4L2_CID_HFLIP, 0, 1, 1, 0);
2113 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2114 V4L2_CID_VFLIP, 0, 1, 1, 0);
2115 if (ctx->inst_type == CODA_INST_ENCODER) {
2116 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2117 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2119 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2120 coda_jpeg_encode_ctrls(ctx);
2122 coda_encode_ctrls(ctx);
2124 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2125 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2127 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2128 coda_decode_ctrls(ctx);
2131 if (ctx->ctrls.error) {
2132 v4l2_err(&ctx->dev->v4l2_dev,
2133 "control initialization error (%d)",
2138 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2141 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2144 vq->ops = &coda_qops;
2145 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2146 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2147 vq->lock = &ctx->dev->dev_mutex;
2148 /* One way to indicate end-of-stream for coda is to set the
2149 * bytesused == 0. However by default videobuf2 handles bytesused
2150 * equal to 0 as a special case and changes its value to the size
2151 * of the buffer. Set the allow_zero_bytesused flag, so
2152 * that videobuf2 will keep the value of bytesused intact.
2154 vq->allow_zero_bytesused = 1;
2156 * We might be fine with no buffers on some of the queues, but that
2157 * would need to be reflected in job_ready(). Currently we expect all
2158 * queues to have at least one buffer queued.
2160 vq->min_buffers_needed = 1;
2161 vq->dev = &ctx->dev->plat_dev->dev;
2163 return vb2_queue_init(vq);
2166 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2167 struct vb2_queue *dst_vq)
2171 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2172 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2173 src_vq->mem_ops = &vb2_dma_contig_memops;
2175 ret = coda_queue_init(priv, src_vq);
2179 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2180 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2181 dst_vq->mem_ops = &vb2_dma_contig_memops;
2183 return coda_queue_init(priv, dst_vq);
2186 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2187 struct vb2_queue *dst_vq)
2191 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2192 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2193 src_vq->mem_ops = &vb2_vmalloc_memops;
2195 ret = coda_queue_init(priv, src_vq);
2199 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2200 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2201 dst_vq->mem_ops = &vb2_dma_contig_memops;
2203 return coda_queue_init(priv, dst_vq);
2210 static int coda_open(struct file *file)
2212 struct video_device *vdev = video_devdata(file);
2213 struct coda_dev *dev = video_get_drvdata(vdev);
2214 struct coda_ctx *ctx;
2215 unsigned int max = ~0;
2220 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2224 if (dev->devtype->product == CODA_DX6)
2225 max = CODADX6_MAX_INSTANCES - 1;
2226 idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2232 name = kasprintf(GFP_KERNEL, "context%d", idx);
2235 goto err_coda_name_init;
2238 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2241 ctx->cvd = to_coda_video_device(vdev);
2242 ctx->inst_type = ctx->cvd->type;
2243 ctx->ops = ctx->cvd->ops;
2244 ctx->use_bit = !ctx->cvd->direct;
2245 init_completion(&ctx->completion);
2246 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2247 if (ctx->ops->seq_end_work)
2248 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2249 v4l2_fh_init(&ctx->fh, video_devdata(file));
2250 file->private_data = &ctx->fh;
2251 v4l2_fh_add(&ctx->fh);
2255 coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2257 switch (dev->devtype->product) {
2260 * Enabling the BWB when decoding can hang the firmware with
2261 * certain streams. The issue was tracked as ENGR00293425 by
2262 * Freescale. As a workaround, disable BWB for all decoders.
2263 * The enable_bwb module parameter allows to override this.
2265 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2266 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2275 if (ctx->dev->vdoa && !disable_vdoa) {
2276 ctx->vdoa = vdoa_context_create(dev->vdoa);
2278 v4l2_warn(&dev->v4l2_dev,
2279 "Failed to create vdoa context: not using vdoa");
2281 ctx->use_vdoa = false;
2283 /* Power up and upload firmware if necessary */
2284 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2286 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2290 ret = clk_prepare_enable(dev->clk_per);
2294 ret = clk_prepare_enable(dev->clk_ahb);
2298 set_default_params(ctx);
2299 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2300 ctx->ops->queue_init);
2301 if (IS_ERR(ctx->fh.m2m_ctx)) {
2302 ret = PTR_ERR(ctx->fh.m2m_ctx);
2304 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2309 ret = coda_ctrls_setup(ctx);
2311 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2312 goto err_ctrls_setup;
2315 ctx->fh.ctrl_handler = &ctx->ctrls;
2317 mutex_init(&ctx->bitstream_mutex);
2318 mutex_init(&ctx->buffer_mutex);
2319 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2320 spin_lock_init(&ctx->buffer_meta_lock);
2325 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2327 clk_disable_unprepare(dev->clk_ahb);
2329 clk_disable_unprepare(dev->clk_per);
2331 pm_runtime_put_sync(&dev->plat_dev->dev);
2333 v4l2_fh_del(&ctx->fh);
2334 v4l2_fh_exit(&ctx->fh);
2336 ida_free(&dev->ida, ctx->idx);
2342 static int coda_release(struct file *file)
2344 struct coda_dev *dev = video_drvdata(file);
2345 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2347 coda_dbg(1, ctx, "release instance (%p)\n", ctx);
2349 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2350 coda_bit_stream_end_flag(ctx);
2352 /* If this instance is running, call .job_abort and wait for it to end */
2353 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2356 vdoa_context_destroy(ctx->vdoa);
2358 /* In case the instance was not running, we still need to call SEQ_END */
2359 if (ctx->ops->seq_end_work) {
2360 queue_work(dev->workqueue, &ctx->seq_end_work);
2361 flush_work(&ctx->seq_end_work);
2364 if (ctx->dev->devtype->product == CODA_DX6)
2365 coda_free_aux_buf(dev, &ctx->workbuf);
2367 v4l2_ctrl_handler_free(&ctx->ctrls);
2368 clk_disable_unprepare(dev->clk_ahb);
2369 clk_disable_unprepare(dev->clk_per);
2370 pm_runtime_put_sync(&dev->plat_dev->dev);
2371 v4l2_fh_del(&ctx->fh);
2372 v4l2_fh_exit(&ctx->fh);
2373 ida_free(&dev->ida, ctx->idx);
2374 if (ctx->ops->release)
2375 ctx->ops->release(ctx);
2376 debugfs_remove_recursive(ctx->debugfs_entry);
2382 static const struct v4l2_file_operations coda_fops = {
2383 .owner = THIS_MODULE,
2385 .release = coda_release,
2386 .poll = v4l2_m2m_fop_poll,
2387 .unlocked_ioctl = video_ioctl2,
2388 .mmap = v4l2_m2m_fop_mmap,
2391 static int coda_hw_init(struct coda_dev *dev)
2397 ret = clk_prepare_enable(dev->clk_per);
2401 ret = clk_prepare_enable(dev->clk_ahb);
2405 reset_control_reset(dev->rstc);
2408 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2409 * The 16-bit chars in the code buffer are in memory access
2410 * order, re-sort them to CODA order for register download.
2411 * Data in this SRAM survives a reboot.
2413 p = (u16 *)dev->codebuf.vaddr;
2414 if (dev->devtype->product == CODA_DX6) {
2415 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2416 data = CODA_DOWN_ADDRESS_SET(i) |
2417 CODA_DOWN_DATA_SET(p[i ^ 1]);
2418 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2421 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2422 data = CODA_DOWN_ADDRESS_SET(i) |
2423 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2425 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2429 /* Clear registers */
2430 for (i = 0; i < 64; i++)
2431 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2433 /* Tell the BIT where to find everything it needs */
2434 if (dev->devtype->product == CODA_960 ||
2435 dev->devtype->product == CODA_7541 ||
2436 dev->devtype->product == CODA_HX4) {
2437 coda_write(dev, dev->tempbuf.paddr,
2438 CODA_REG_BIT_TEMP_BUF_ADDR);
2439 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2441 coda_write(dev, dev->workbuf.paddr,
2442 CODA_REG_BIT_WORK_BUF_ADDR);
2444 coda_write(dev, dev->codebuf.paddr,
2445 CODA_REG_BIT_CODE_BUF_ADDR);
2446 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2448 /* Set default values */
2449 switch (dev->devtype->product) {
2451 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2452 CODA_REG_BIT_STREAM_CTRL);
2455 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2456 CODA_REG_BIT_STREAM_CTRL);
2458 if (dev->devtype->product == CODA_960)
2459 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2460 CODA_REG_BIT_FRAME_MEM_CTRL);
2462 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2464 if (dev->devtype->product != CODA_DX6)
2465 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2467 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2468 CODA_REG_BIT_INT_ENABLE);
2470 /* Reset VPU and start processor */
2471 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2472 data |= CODA_REG_RESET_ENABLE;
2473 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2475 data &= ~CODA_REG_RESET_ENABLE;
2476 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2477 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2479 clk_disable_unprepare(dev->clk_ahb);
2480 clk_disable_unprepare(dev->clk_per);
2485 clk_disable_unprepare(dev->clk_per);
2490 static int coda_register_device(struct coda_dev *dev, int i)
2492 struct video_device *vfd = &dev->vfd[i];
2494 if (i >= dev->devtype->num_vdevs)
2497 strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2498 vfd->fops = &coda_fops;
2499 vfd->ioctl_ops = &coda_ioctl_ops;
2500 vfd->release = video_device_release_empty,
2501 vfd->lock = &dev->dev_mutex;
2502 vfd->v4l2_dev = &dev->v4l2_dev;
2503 vfd->vfl_dir = VFL_DIR_M2M;
2504 video_set_drvdata(vfd, dev);
2506 /* Not applicable, use the selection API instead */
2507 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2508 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2509 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2511 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2514 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2517 u32 *src = (u32 *)buf;
2519 /* Check if the firmware has a 16-byte Freescale header, skip it */
2520 if (buf[0] == 'M' && buf[1] == 'X')
2523 * Check whether the firmware is in native order or pre-reordered for
2524 * memory access. The first instruction opcode always is 0xe40e.
2526 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2527 u32 *dst = dev->codebuf.vaddr;
2530 /* Firmware in native order, reorder while copying */
2531 if (dev->devtype->product == CODA_DX6) {
2532 for (i = 0; i < (size - 16) / 4; i++)
2533 dst[i] = (src[i] << 16) | (src[i] >> 16);
2535 for (i = 0; i < (size - 16) / 4; i += 2) {
2536 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2537 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2541 /* Copy the already reordered firmware image */
2542 memcpy(dev->codebuf.vaddr, src, size);
2546 static void coda_fw_callback(const struct firmware *fw, void *context);
2548 static int coda_firmware_request(struct coda_dev *dev)
2552 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2555 fw = dev->devtype->firmware[dev->firmware];
2557 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2558 coda_product_name(dev->devtype->product));
2560 return request_firmware_nowait(THIS_MODULE, true, fw,
2561 &dev->plat_dev->dev, GFP_KERNEL, dev,
2565 static void coda_fw_callback(const struct firmware *fw, void *context)
2567 struct coda_dev *dev = context;
2568 struct platform_device *pdev = dev->plat_dev;
2573 ret = coda_firmware_request(dev);
2575 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2580 if (dev->firmware > 0) {
2582 * Since we can't suppress warnings for failed asynchronous
2583 * firmware requests, report that the fallback firmware was
2586 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2587 dev->devtype->firmware[dev->firmware]);
2590 /* allocate auxiliary per-device code buffer for the BIT processor */
2591 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2596 coda_copy_firmware(dev, fw->data, fw->size);
2597 release_firmware(fw);
2599 ret = coda_hw_init(dev);
2601 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2605 ret = coda_check_firmware(dev);
2609 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2610 if (IS_ERR(dev->m2m_dev)) {
2611 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2615 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2616 ret = coda_register_device(dev, i);
2618 v4l2_err(&dev->v4l2_dev,
2619 "Failed to register %s video device: %d\n",
2620 dev->devtype->vdevs[i]->name, ret);
2625 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
2626 dev->vfd[0].num, dev->vfd[i - 1].num);
2628 pm_runtime_put_sync(&pdev->dev);
2633 video_unregister_device(&dev->vfd[i]);
2634 v4l2_m2m_release(dev->m2m_dev);
2636 pm_runtime_put_sync(&pdev->dev);
2639 enum coda_platform {
2647 static const struct coda_devtype coda_devdata[] = {
2650 "vpu_fw_imx27_TO2.bin",
2651 "vpu/vpu_fw_imx27_TO2.bin",
2652 "v4l-codadx6-imx27.bin"
2654 .product = CODA_DX6,
2655 .codecs = codadx6_codecs,
2656 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2657 .vdevs = codadx6_video_devices,
2658 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2659 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2660 .iram_size = 0xb000,
2665 "vpu/vpu_fw_imx51.bin",
2666 "v4l-codahx4-imx51.bin"
2668 .product = CODA_HX4,
2669 .codecs = codahx4_codecs,
2670 .num_codecs = ARRAY_SIZE(codahx4_codecs),
2671 .vdevs = codahx4_video_devices,
2672 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
2673 .workbuf_size = 128 * 1024,
2674 .tempbuf_size = 304 * 1024,
2675 .iram_size = 0x14000,
2680 "vpu/vpu_fw_imx53.bin",
2681 "v4l-coda7541-imx53.bin"
2683 .product = CODA_7541,
2684 .codecs = coda7_codecs,
2685 .num_codecs = ARRAY_SIZE(coda7_codecs),
2686 .vdevs = coda7_video_devices,
2687 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2688 .workbuf_size = 128 * 1024,
2689 .tempbuf_size = 304 * 1024,
2690 .iram_size = 0x14000,
2695 "vpu/vpu_fw_imx6q.bin",
2696 "v4l-coda960-imx6q.bin"
2698 .product = CODA_960,
2699 .codecs = coda9_codecs,
2700 .num_codecs = ARRAY_SIZE(coda9_codecs),
2701 .vdevs = coda9_video_devices,
2702 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2703 .workbuf_size = 80 * 1024,
2704 .tempbuf_size = 204 * 1024,
2705 .iram_size = 0x21000,
2710 "vpu/vpu_fw_imx6d.bin",
2711 "v4l-coda960-imx6dl.bin"
2713 .product = CODA_960,
2714 .codecs = coda9_codecs,
2715 .num_codecs = ARRAY_SIZE(coda9_codecs),
2716 .vdevs = coda9_video_devices,
2717 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2718 .workbuf_size = 80 * 1024,
2719 .tempbuf_size = 204 * 1024,
2720 .iram_size = 0x1f000, /* leave 4k for suspend code */
2724 static const struct platform_device_id coda_platform_ids[] = {
2725 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2728 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2731 static const struct of_device_id coda_dt_ids[] = {
2732 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2733 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
2734 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2735 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2736 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2739 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2742 static int coda_probe(struct platform_device *pdev)
2744 const struct of_device_id *of_id =
2745 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2746 const struct platform_device_id *pdev_id;
2747 struct coda_platform_data *pdata = pdev->dev.platform_data;
2748 struct device_node *np = pdev->dev.of_node;
2749 struct gen_pool *pool;
2750 struct coda_dev *dev;
2751 struct resource *res;
2754 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2758 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2761 dev->devtype = of_id->data;
2763 dev->devtype = &coda_devdata[pdev_id->driver_data];
2767 spin_lock_init(&dev->irqlock);
2769 dev->plat_dev = pdev;
2770 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2771 if (IS_ERR(dev->clk_per)) {
2772 dev_err(&pdev->dev, "Could not get per clock\n");
2773 return PTR_ERR(dev->clk_per);
2776 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2777 if (IS_ERR(dev->clk_ahb)) {
2778 dev_err(&pdev->dev, "Could not get ahb clock\n");
2779 return PTR_ERR(dev->clk_ahb);
2782 /* Get memory for physical registers */
2783 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2784 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2785 if (IS_ERR(dev->regs_base))
2786 return PTR_ERR(dev->regs_base);
2789 irq = platform_get_irq_byname(pdev, "bit");
2791 irq = platform_get_irq(pdev, 0);
2793 dev_err(&pdev->dev, "failed to get irq resource\n");
2797 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2798 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2800 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2804 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
2806 if (IS_ERR(dev->rstc)) {
2807 ret = PTR_ERR(dev->rstc);
2808 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
2812 /* Get IRAM pool from device tree or platform data */
2813 pool = of_gen_pool_get(np, "iram", 0);
2815 pool = gen_pool_get(pdata->iram_dev, NULL);
2817 dev_err(&pdev->dev, "iram pool not available\n");
2820 dev->iram_pool = pool;
2822 /* Get vdoa_data if supported by the platform */
2823 dev->vdoa = coda_get_vdoa_data();
2824 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
2825 return -EPROBE_DEFER;
2827 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2831 mutex_init(&dev->dev_mutex);
2832 mutex_init(&dev->coda_mutex);
2833 ida_init(&dev->ida);
2835 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2836 if (!dev->debugfs_root)
2837 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2839 /* allocate auxiliary per-device buffers for the BIT processor */
2840 if (dev->devtype->product == CODA_DX6) {
2841 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2842 dev->devtype->workbuf_size, "workbuf",
2845 goto err_v4l2_register;
2848 if (dev->devtype->tempbuf_size) {
2849 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2850 dev->devtype->tempbuf_size, "tempbuf",
2853 goto err_v4l2_register;
2856 dev->iram.size = dev->devtype->iram_size;
2857 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2859 if (!dev->iram.vaddr) {
2860 dev_warn(&pdev->dev, "unable to alloc iram\n");
2862 memset(dev->iram.vaddr, 0, dev->iram.size);
2863 dev->iram.blob.data = dev->iram.vaddr;
2864 dev->iram.blob.size = dev->iram.size;
2865 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2870 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2871 if (!dev->workqueue) {
2872 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2874 goto err_v4l2_register;
2877 platform_set_drvdata(pdev, dev);
2880 * Start activated so we can directly call coda_hw_init in
2881 * coda_fw_callback regardless of whether CONFIG_PM is
2882 * enabled or whether the device is associated with a PM domain.
2884 pm_runtime_get_noresume(&pdev->dev);
2885 pm_runtime_set_active(&pdev->dev);
2886 pm_runtime_enable(&pdev->dev);
2888 ret = coda_firmware_request(dev);
2890 goto err_alloc_workqueue;
2893 err_alloc_workqueue:
2894 destroy_workqueue(dev->workqueue);
2896 v4l2_device_unregister(&dev->v4l2_dev);
2900 static int coda_remove(struct platform_device *pdev)
2902 struct coda_dev *dev = platform_get_drvdata(pdev);
2905 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2906 if (video_get_drvdata(&dev->vfd[i]))
2907 video_unregister_device(&dev->vfd[i]);
2910 v4l2_m2m_release(dev->m2m_dev);
2911 pm_runtime_disable(&pdev->dev);
2912 v4l2_device_unregister(&dev->v4l2_dev);
2913 destroy_workqueue(dev->workqueue);
2914 if (dev->iram.vaddr)
2915 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2917 coda_free_aux_buf(dev, &dev->codebuf);
2918 coda_free_aux_buf(dev, &dev->tempbuf);
2919 coda_free_aux_buf(dev, &dev->workbuf);
2920 debugfs_remove_recursive(dev->debugfs_root);
2921 ida_destroy(&dev->ida);
2926 static int coda_runtime_resume(struct device *dev)
2928 struct coda_dev *cdev = dev_get_drvdata(dev);
2931 if (dev->pm_domain && cdev->codebuf.vaddr) {
2932 ret = coda_hw_init(cdev);
2934 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2941 static const struct dev_pm_ops coda_pm_ops = {
2942 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2945 static struct platform_driver coda_driver = {
2946 .probe = coda_probe,
2947 .remove = coda_remove,
2950 .of_match_table = of_match_ptr(coda_dt_ids),
2953 .id_table = coda_platform_ids,
2956 module_platform_driver(coda_driver);
2958 MODULE_LICENSE("GPL");
2959 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2960 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");