1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Coda multi-standard codec IP
5 * Copyright (C) 2012 Vista Silicon S.L.
6 * Javier Martin, <javier.martin@vista-silicon.com>
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 5
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,
257 static const struct coda_video_device coda9_jpeg_decoder = {
258 .name = "coda-jpeg-decoder",
259 .type = CODA_INST_DECODER,
260 .ops = &coda9_jpeg_decode_ops,
269 V4L2_PIX_FMT_YUV422P,
273 static const struct coda_video_device *codadx6_video_devices[] = {
277 static const struct coda_video_device *codahx4_video_devices[] = {
282 static const struct coda_video_device *coda7_video_devices[] = {
283 &coda_bit_jpeg_encoder,
284 &coda_bit_jpeg_decoder,
289 static const struct coda_video_device *coda9_video_devices[] = {
297 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
300 static u32 coda_format_normalize_yuv(u32 fourcc)
303 case V4L2_PIX_FMT_NV12:
304 case V4L2_PIX_FMT_YUV420:
305 case V4L2_PIX_FMT_YVU420:
306 case V4L2_PIX_FMT_YUV422P:
307 case V4L2_PIX_FMT_YUYV:
308 return V4L2_PIX_FMT_YUV420;
314 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
315 int src_fourcc, int dst_fourcc)
317 const struct coda_codec *codecs = dev->devtype->codecs;
318 int num_codecs = dev->devtype->num_codecs;
321 src_fourcc = coda_format_normalize_yuv(src_fourcc);
322 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
323 if (src_fourcc == dst_fourcc)
326 for (k = 0; k < num_codecs; k++) {
327 if (codecs[k].src_fourcc == src_fourcc &&
328 codecs[k].dst_fourcc == dst_fourcc)
338 static void coda_get_max_dimensions(struct coda_dev *dev,
339 const struct coda_codec *codec,
340 int *max_w, int *max_h)
342 const struct coda_codec *codecs = dev->devtype->codecs;
343 int num_codecs = dev->devtype->num_codecs;
351 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
352 w = max(w, codecs[k].max_w);
353 h = max(h, codecs[k].max_h);
363 static const struct coda_video_device *to_coda_video_device(struct video_device
366 struct coda_dev *dev = video_get_drvdata(vdev);
367 unsigned int i = vdev - dev->vfd;
369 if (i >= dev->devtype->num_vdevs)
372 return dev->devtype->vdevs[i];
375 const char *coda_product_name(int product)
389 snprintf(buf, sizeof(buf), "(0x%04x)", product);
394 static struct vdoa_data *coda_get_vdoa_data(void)
396 struct device_node *vdoa_node;
397 struct platform_device *vdoa_pdev;
398 struct vdoa_data *vdoa_data = NULL;
400 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
404 vdoa_pdev = of_find_device_by_node(vdoa_node);
408 vdoa_data = platform_get_drvdata(vdoa_pdev);
410 vdoa_data = ERR_PTR(-EPROBE_DEFER);
412 put_device(&vdoa_pdev->dev);
414 of_node_put(vdoa_node);
420 * V4L2 ioctl() operations.
422 static int coda_querycap(struct file *file, void *priv,
423 struct v4l2_capability *cap)
425 struct coda_ctx *ctx = fh_to_ctx(priv);
427 strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
428 strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
430 strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
434 static const u32 coda_formats_420[CODA_MAX_FORMATS] = {
440 static int coda_enum_fmt(struct file *file, void *priv,
441 struct v4l2_fmtdesc *f)
443 struct video_device *vdev = video_devdata(file);
444 const struct coda_video_device *cvd = to_coda_video_device(vdev);
445 struct coda_ctx *ctx = fh_to_ctx(priv);
448 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
449 formats = cvd->src_formats;
450 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
451 struct coda_q_data *q_data_src;
452 struct vb2_queue *src_vq;
454 formats = cvd->dst_formats;
457 * If the source format is already fixed, only allow the same
458 * chroma subsampling.
460 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
461 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
462 V4L2_BUF_TYPE_VIDEO_OUTPUT);
463 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
464 vb2_is_streaming(src_vq)) {
465 if (ctx->params.jpeg_chroma_subsampling ==
466 V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
467 formats = coda_formats_420;
468 } else if (ctx->params.jpeg_chroma_subsampling ==
469 V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
470 f->pixelformat = V4L2_PIX_FMT_YUV422P;
471 return f->index ? -EINVAL : 0;
478 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
481 /* Skip YUYV if the vdoa is not available */
482 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
483 formats[f->index] == V4L2_PIX_FMT_YUYV)
486 f->pixelformat = formats[f->index];
491 static int coda_g_fmt(struct file *file, void *priv,
492 struct v4l2_format *f)
494 struct coda_q_data *q_data;
495 struct coda_ctx *ctx = fh_to_ctx(priv);
497 q_data = get_q_data(ctx, f->type);
501 f->fmt.pix.field = V4L2_FIELD_NONE;
502 f->fmt.pix.pixelformat = q_data->fourcc;
503 f->fmt.pix.width = q_data->width;
504 f->fmt.pix.height = q_data->height;
505 f->fmt.pix.bytesperline = q_data->bytesperline;
507 f->fmt.pix.sizeimage = q_data->sizeimage;
508 f->fmt.pix.colorspace = ctx->colorspace;
509 f->fmt.pix.xfer_func = ctx->xfer_func;
510 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
511 f->fmt.pix.quantization = ctx->quantization;
516 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
518 struct coda_q_data *q_data;
522 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
523 formats = ctx->cvd->src_formats;
524 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
525 formats = ctx->cvd->dst_formats;
529 for (i = 0; i < CODA_MAX_FORMATS; i++) {
530 /* Skip YUYV if the vdoa is not available */
531 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
532 formats[i] == V4L2_PIX_FMT_YUYV)
535 if (formats[i] == f->fmt.pix.pixelformat) {
536 f->fmt.pix.pixelformat = formats[i];
541 /* Fall back to currently set pixelformat */
542 q_data = get_q_data(ctx, f->type);
543 f->fmt.pix.pixelformat = q_data->fourcc;
548 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
553 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
564 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
565 f->fmt.pix.height, f->fmt.pix.pixelformat);
575 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
576 u32 width, u32 height)
579 * This is a rough estimate for sensible compressed buffer
580 * sizes (between 1 and 16 bits per pixel). This could be
581 * improved by better format specific worst case estimates.
583 return round_up(clamp(sizeimage, width * height / 8,
584 width * height * 2), PAGE_SIZE);
587 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
588 struct v4l2_format *f)
590 struct coda_dev *dev = ctx->dev;
591 unsigned int max_w, max_h;
592 enum v4l2_field field;
594 field = f->fmt.pix.field;
595 if (field == V4L2_FIELD_ANY)
596 field = V4L2_FIELD_NONE;
597 else if (V4L2_FIELD_NONE != field)
600 /* V4L2 specification suggests the driver corrects the format struct
601 * if any of the dimensions is unsupported */
602 f->fmt.pix.field = field;
604 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
605 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
606 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
609 switch (f->fmt.pix.pixelformat) {
610 case V4L2_PIX_FMT_NV12:
611 case V4L2_PIX_FMT_YUV420:
612 case V4L2_PIX_FMT_YVU420:
614 * Frame stride must be at least multiple of 8,
615 * but multiple of 16 for h.264 or JPEG 4:2:x
617 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
618 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
619 f->fmt.pix.height * 3 / 2;
621 case V4L2_PIX_FMT_YUYV:
622 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
623 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
626 case V4L2_PIX_FMT_YUV422P:
627 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
628 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
629 f->fmt.pix.height * 2;
631 case V4L2_PIX_FMT_GREY:
632 /* keep 16 pixel alignment of 8-bit pixel data */
633 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
634 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
636 case V4L2_PIX_FMT_JPEG:
637 case V4L2_PIX_FMT_H264:
638 case V4L2_PIX_FMT_MPEG4:
639 case V4L2_PIX_FMT_MPEG2:
640 f->fmt.pix.bytesperline = 0;
641 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
642 f->fmt.pix.sizeimage,
653 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
654 struct v4l2_format *f)
656 struct coda_ctx *ctx = fh_to_ctx(priv);
657 const struct coda_q_data *q_data_src;
658 const struct coda_codec *codec;
659 struct vb2_queue *src_vq;
665 ret = coda_try_pixelformat(ctx, f);
669 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
672 * If the source format is already fixed, only allow the same output
673 * resolution. When decoding JPEG images, we also have to make sure to
674 * use the same chroma subsampling.
676 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
677 if (vb2_is_streaming(src_vq)) {
678 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
679 ctx->dev->devtype->product == CODA_960) {
680 hscale = coda_jpeg_scale(q_data_src->width, f->fmt.pix.width);
681 vscale = coda_jpeg_scale(q_data_src->height, f->fmt.pix.height);
683 f->fmt.pix.width = q_data_src->width >> hscale;
684 f->fmt.pix.height = q_data_src->height >> vscale;
686 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
687 if (ctx->params.jpeg_chroma_subsampling ==
688 V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
689 f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P)
690 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
691 else if (ctx->params.jpeg_chroma_subsampling ==
692 V4L2_JPEG_CHROMA_SUBSAMPLING_422)
693 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
697 f->fmt.pix.colorspace = ctx->colorspace;
698 f->fmt.pix.xfer_func = ctx->xfer_func;
699 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
700 f->fmt.pix.quantization = ctx->quantization;
702 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
703 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
704 f->fmt.pix.pixelformat);
708 ret = coda_try_fmt(ctx, codec, f);
712 /* The decoders always write complete macroblocks or MCUs */
713 if (ctx->inst_type == CODA_INST_DECODER) {
714 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16 >> hscale);
715 f->fmt.pix.height = round_up(f->fmt.pix.height, 16 >> vscale);
716 if (codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
717 f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P) {
718 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
719 f->fmt.pix.height * 2;
721 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
722 f->fmt.pix.height * 3 / 2;
725 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
729 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
733 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
734 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
742 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
744 enum v4l2_colorspace colorspace;
746 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
747 colorspace = V4L2_COLORSPACE_JPEG;
748 else if (fmt->width <= 720 && fmt->height <= 576)
749 colorspace = V4L2_COLORSPACE_SMPTE170M;
751 colorspace = V4L2_COLORSPACE_REC709;
753 fmt->colorspace = colorspace;
754 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
755 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
756 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
759 static int coda_try_fmt_vid_out(struct file *file, void *priv,
760 struct v4l2_format *f)
762 struct coda_ctx *ctx = fh_to_ctx(priv);
763 struct coda_dev *dev = ctx->dev;
764 const struct coda_q_data *q_data_dst;
765 const struct coda_codec *codec;
768 ret = coda_try_pixelformat(ctx, f);
772 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
773 coda_set_default_colorspace(&f->fmt.pix);
775 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
776 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
778 return coda_try_fmt(ctx, codec, f);
781 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
784 struct coda_q_data *q_data;
785 struct vb2_queue *vq;
787 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
791 q_data = get_q_data(ctx, f->type);
795 if (vb2_is_busy(vq)) {
796 v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
797 __func__, v4l2_type_names[f->type], vq->num_buffers);
801 q_data->fourcc = f->fmt.pix.pixelformat;
802 q_data->width = f->fmt.pix.width;
803 q_data->height = f->fmt.pix.height;
804 q_data->bytesperline = f->fmt.pix.bytesperline;
805 q_data->sizeimage = f->fmt.pix.sizeimage;
809 q_data->rect.left = 0;
810 q_data->rect.top = 0;
811 q_data->rect.width = f->fmt.pix.width;
812 q_data->rect.height = f->fmt.pix.height;
815 switch (f->fmt.pix.pixelformat) {
816 case V4L2_PIX_FMT_YUYV:
817 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
819 case V4L2_PIX_FMT_NV12:
820 if (!disable_tiling && ctx->use_bit &&
821 ctx->dev->devtype->product == CODA_960) {
822 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
826 case V4L2_PIX_FMT_YUV420:
827 case V4L2_PIX_FMT_YVU420:
828 case V4L2_PIX_FMT_YUV422P:
829 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
835 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
836 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
838 vdoa_context_configure(ctx->vdoa,
839 round_up(f->fmt.pix.width, 16),
841 f->fmt.pix.pixelformat);
843 ctx->use_vdoa = false;
845 coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
846 v4l2_type_names[f->type], q_data->width, q_data->height,
847 (char *)&q_data->fourcc,
848 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
853 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
854 struct v4l2_format *f)
856 struct coda_ctx *ctx = fh_to_ctx(priv);
857 struct coda_q_data *q_data_src;
858 const struct coda_codec *codec;
864 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
866 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
867 ctx->dev->devtype->product == CODA_960) {
868 hscale = coda_jpeg_scale(q_data_src->width, f->fmt.pix.width);
869 vscale = coda_jpeg_scale(q_data_src->height, f->fmt.pix.height);
872 ret = coda_try_fmt_vid_cap(file, priv, f);
878 r.width = q_data_src->width >> hscale;
879 r.height = q_data_src->height >> vscale;
881 ret = coda_s_fmt(ctx, f, &r);
885 if (ctx->inst_type != CODA_INST_ENCODER)
888 /* Setting the coded format determines the selected codec */
889 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
890 f->fmt.pix.pixelformat);
892 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
897 ctx->colorspace = f->fmt.pix.colorspace;
898 ctx->xfer_func = f->fmt.pix.xfer_func;
899 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
900 ctx->quantization = f->fmt.pix.quantization;
905 static int coda_s_fmt_vid_out(struct file *file, void *priv,
906 struct v4l2_format *f)
908 struct coda_ctx *ctx = fh_to_ctx(priv);
909 const struct coda_codec *codec;
910 struct v4l2_format f_cap;
911 struct vb2_queue *dst_vq;
914 ret = coda_try_fmt_vid_out(file, priv, f);
918 ret = coda_s_fmt(ctx, f, NULL);
922 ctx->colorspace = f->fmt.pix.colorspace;
923 ctx->xfer_func = f->fmt.pix.xfer_func;
924 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
925 ctx->quantization = f->fmt.pix.quantization;
927 if (ctx->inst_type != CODA_INST_DECODER)
930 /* Setting the coded format determines the selected codec */
931 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
932 V4L2_PIX_FMT_YUV420);
934 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
939 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
944 * Setting the capture queue format is not possible while the capture
945 * queue is still busy. This is not an error, but the user will have to
946 * make sure themselves that the capture format is set correctly before
947 * starting the output queue again.
949 if (vb2_is_busy(dst_vq))
952 memset(&f_cap, 0, sizeof(f_cap));
953 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
954 coda_g_fmt(file, priv, &f_cap);
955 f_cap.fmt.pix.width = f->fmt.pix.width;
956 f_cap.fmt.pix.height = f->fmt.pix.height;
958 return coda_s_fmt_vid_cap(file, priv, &f_cap);
961 static int coda_reqbufs(struct file *file, void *priv,
962 struct v4l2_requestbuffers *rb)
964 struct coda_ctx *ctx = fh_to_ctx(priv);
967 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
972 * Allow to allocate instance specific per-context buffers, such as
973 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
975 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
976 return ctx->ops->reqbufs(ctx, rb);
981 static int coda_qbuf(struct file *file, void *priv,
982 struct v4l2_buffer *buf)
984 struct coda_ctx *ctx = fh_to_ctx(priv);
986 if (ctx->inst_type == CODA_INST_DECODER &&
987 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
988 buf->flags &= ~V4L2_BUF_FLAG_LAST;
990 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
993 static int coda_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
995 struct coda_ctx *ctx = fh_to_ctx(priv);
998 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
1000 if (ctx->inst_type == CODA_INST_DECODER &&
1001 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1002 buf->flags &= ~V4L2_BUF_FLAG_LAST;
1007 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
1008 enum vb2_buffer_state state)
1010 const struct v4l2_event eos_event = {
1011 .type = V4L2_EVENT_EOS
1014 if (buf->flags & V4L2_BUF_FLAG_LAST)
1015 v4l2_event_queue_fh(&ctx->fh, &eos_event);
1017 v4l2_m2m_buf_done(buf, state);
1020 static int coda_g_selection(struct file *file, void *fh,
1021 struct v4l2_selection *s)
1023 struct coda_ctx *ctx = fh_to_ctx(fh);
1024 struct coda_q_data *q_data;
1025 struct v4l2_rect r, *rsel;
1027 q_data = get_q_data(ctx, s->type);
1033 r.width = q_data->width;
1034 r.height = q_data->height;
1035 rsel = &q_data->rect;
1037 switch (s->target) {
1038 case V4L2_SEL_TGT_CROP_DEFAULT:
1039 case V4L2_SEL_TGT_CROP_BOUNDS:
1042 case V4L2_SEL_TGT_CROP:
1043 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
1044 ctx->inst_type == CODA_INST_DECODER)
1047 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1048 case V4L2_SEL_TGT_COMPOSE_PADDED:
1051 case V4L2_SEL_TGT_COMPOSE:
1052 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1053 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1054 ctx->inst_type == CODA_INST_ENCODER)
1066 static int coda_s_selection(struct file *file, void *fh,
1067 struct v4l2_selection *s)
1069 struct coda_ctx *ctx = fh_to_ctx(fh);
1070 struct coda_q_data *q_data;
1072 switch (s->target) {
1073 case V4L2_SEL_TGT_CROP:
1074 if (ctx->inst_type == CODA_INST_ENCODER &&
1075 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1076 q_data = get_q_data(ctx, s->type);
1082 s->r.width = clamp(s->r.width, 2U, q_data->width);
1083 s->r.height = clamp(s->r.height, 2U, q_data->height);
1085 if (s->flags & V4L2_SEL_FLAG_LE) {
1086 s->r.width = round_up(s->r.width, 2);
1087 s->r.height = round_up(s->r.height, 2);
1089 s->r.width = round_down(s->r.width, 2);
1090 s->r.height = round_down(s->r.height, 2);
1093 q_data->rect = s->r;
1095 coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
1096 s->r.width, s->r.height);
1101 case V4L2_SEL_TGT_NATIVE_SIZE:
1102 case V4L2_SEL_TGT_COMPOSE:
1103 return coda_g_selection(file, fh, s);
1105 /* v4l2-compliance expects this to fail for read-only targets */
1110 static void coda_wake_up_capture_queue(struct coda_ctx *ctx)
1112 struct vb2_queue *dst_vq;
1114 coda_dbg(1, ctx, "waking up capture queue\n");
1116 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1117 dst_vq->last_buffer_dequeued = true;
1118 wake_up(&dst_vq->done_wq);
1121 static int coda_encoder_cmd(struct file *file, void *fh,
1122 struct v4l2_encoder_cmd *ec)
1124 struct coda_ctx *ctx = fh_to_ctx(fh);
1125 struct vb2_v4l2_buffer *buf;
1128 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, ec);
1132 mutex_lock(&ctx->wakeup_mutex);
1133 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1136 * If the last output buffer is still on the queue, make sure
1137 * that decoder finish_run will see the last flag and report it
1140 buf->flags |= V4L2_BUF_FLAG_LAST;
1142 /* Set the stream-end flag on this context */
1143 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1146 * If the last output buffer has already been taken from the
1147 * queue, wake up the capture queue and signal end of stream
1148 * via the -EPIPE mechanism.
1150 coda_wake_up_capture_queue(ctx);
1152 mutex_unlock(&ctx->wakeup_mutex);
1157 static bool coda_mark_last_meta(struct coda_ctx *ctx)
1159 struct coda_buffer_meta *meta;
1161 coda_dbg(1, ctx, "marking last meta\n");
1163 spin_lock(&ctx->buffer_meta_lock);
1164 if (list_empty(&ctx->buffer_meta_list)) {
1165 spin_unlock(&ctx->buffer_meta_lock);
1169 meta = list_last_entry(&ctx->buffer_meta_list, struct coda_buffer_meta,
1173 spin_unlock(&ctx->buffer_meta_lock);
1177 static bool coda_mark_last_dst_buf(struct coda_ctx *ctx)
1179 struct vb2_v4l2_buffer *buf;
1180 struct vb2_buffer *dst_vb;
1181 struct vb2_queue *dst_vq;
1182 unsigned long flags;
1184 coda_dbg(1, ctx, "marking last capture buffer\n");
1186 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1187 spin_lock_irqsave(&dst_vq->done_lock, flags);
1188 if (list_empty(&dst_vq->done_list)) {
1189 spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1193 dst_vb = list_last_entry(&dst_vq->done_list, struct vb2_buffer,
1195 buf = to_vb2_v4l2_buffer(dst_vb);
1196 buf->flags |= V4L2_BUF_FLAG_LAST;
1198 spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1202 static int coda_decoder_cmd(struct file *file, void *fh,
1203 struct v4l2_decoder_cmd *dc)
1205 struct coda_ctx *ctx = fh_to_ctx(fh);
1206 struct coda_dev *dev = ctx->dev;
1207 struct vb2_v4l2_buffer *buf;
1208 struct vb2_queue *dst_vq;
1213 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
1218 case V4L2_DEC_CMD_START:
1219 mutex_lock(&dev->coda_mutex);
1220 mutex_lock(&ctx->bitstream_mutex);
1221 coda_bitstream_flush(ctx);
1222 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1223 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1224 vb2_clear_last_buffer_dequeued(dst_vq);
1225 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1226 coda_fill_bitstream(ctx, NULL);
1227 mutex_unlock(&ctx->bitstream_mutex);
1228 mutex_unlock(&dev->coda_mutex);
1230 case V4L2_DEC_CMD_STOP:
1234 mutex_lock(&ctx->wakeup_mutex);
1236 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1238 coda_dbg(1, ctx, "marking last pending buffer\n");
1240 /* Mark last buffer */
1241 buf->flags |= V4L2_BUF_FLAG_LAST;
1243 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) == 0) {
1244 coda_dbg(1, ctx, "all remaining buffers queued\n");
1249 if (coda_mark_last_meta(ctx))
1254 if (!coda_mark_last_dst_buf(ctx))
1259 coda_dbg(1, ctx, "all remaining buffers queued\n");
1261 /* Set the stream-end flag on this context */
1262 coda_bit_stream_end_flag(ctx);
1264 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1268 /* If there is no buffer in flight, wake up */
1269 coda_wake_up_capture_queue(ctx);
1272 mutex_unlock(&ctx->wakeup_mutex);
1281 static int coda_enum_framesizes(struct file *file, void *fh,
1282 struct v4l2_frmsizeenum *fsize)
1284 struct coda_ctx *ctx = fh_to_ctx(fh);
1285 struct coda_q_data *q_data_dst;
1286 const struct coda_codec *codec;
1291 if (coda_format_normalize_yuv(fsize->pixel_format) ==
1292 V4L2_PIX_FMT_YUV420) {
1293 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1294 codec = coda_find_codec(ctx->dev, fsize->pixel_format,
1295 q_data_dst->fourcc);
1297 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1298 fsize->pixel_format);
1303 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1304 fsize->stepwise.min_width = MIN_W;
1305 fsize->stepwise.max_width = codec->max_w;
1306 fsize->stepwise.step_width = 1;
1307 fsize->stepwise.min_height = MIN_H;
1308 fsize->stepwise.max_height = codec->max_h;
1309 fsize->stepwise.step_height = 1;
1314 static int coda_enum_frameintervals(struct file *file, void *fh,
1315 struct v4l2_frmivalenum *f)
1317 struct coda_ctx *ctx = fh_to_ctx(fh);
1323 /* Disallow YUYV if the vdoa is not available */
1324 if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1327 for (i = 0; i < CODA_MAX_FORMATS; i++) {
1328 if (f->pixel_format == ctx->cvd->src_formats[i] ||
1329 f->pixel_format == ctx->cvd->dst_formats[i])
1332 if (i == CODA_MAX_FORMATS)
1335 f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1336 f->stepwise.min.numerator = 1;
1337 f->stepwise.min.denominator = 65535;
1338 f->stepwise.max.numerator = 65536;
1339 f->stepwise.max.denominator = 1;
1340 f->stepwise.step.numerator = 1;
1341 f->stepwise.step.denominator = 1;
1346 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1348 struct coda_ctx *ctx = fh_to_ctx(fh);
1349 struct v4l2_fract *tpf;
1351 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1354 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1355 tpf = &a->parm.output.timeperframe;
1356 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1357 tpf->numerator = 1 + (ctx->params.framerate >>
1358 CODA_FRATE_DIV_OFFSET);
1364 * Approximate timeperframe v4l2_fract with values that can be written
1365 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1367 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1369 struct v4l2_fract s = *timeperframe;
1370 struct v4l2_fract f0;
1371 struct v4l2_fract f1 = { 1, 0 };
1372 struct v4l2_fract f2 = { 0, 1 };
1373 unsigned int i, div, s_denominator;
1375 /* Lower bound is 1/65535 */
1376 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1377 timeperframe->numerator = 1;
1378 timeperframe->denominator = 65535;
1382 /* Upper bound is 65536/1 */
1383 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1384 timeperframe->numerator = 65536;
1385 timeperframe->denominator = 1;
1389 /* Reduce fraction to lowest terms */
1390 div = gcd(s.numerator, s.denominator);
1393 s.denominator /= div;
1396 if (s.numerator <= 65536 && s.denominator < 65536) {
1401 /* Find successive convergents from continued fraction expansion */
1402 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1406 /* Stop when f2 exactly equals timeperframe */
1407 if (s.numerator == 0)
1410 i = s.denominator / s.numerator;
1412 f2.numerator = f0.numerator + i * f1.numerator;
1413 f2.denominator = f0.denominator + i * f2.denominator;
1415 s_denominator = s.numerator;
1416 s.numerator = s.denominator % s.numerator;
1417 s.denominator = s_denominator;
1423 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1425 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1426 timeperframe->denominator;
1429 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1431 struct coda_ctx *ctx = fh_to_ctx(fh);
1432 struct v4l2_fract *tpf;
1434 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1437 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1438 tpf = &a->parm.output.timeperframe;
1439 coda_approximate_timeperframe(tpf);
1440 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1441 ctx->params.framerate_changed = true;
1446 static int coda_subscribe_event(struct v4l2_fh *fh,
1447 const struct v4l2_event_subscription *sub)
1449 struct coda_ctx *ctx = fh_to_ctx(fh);
1451 switch (sub->type) {
1452 case V4L2_EVENT_EOS:
1453 return v4l2_event_subscribe(fh, sub, 0, NULL);
1454 case V4L2_EVENT_SOURCE_CHANGE:
1455 if (ctx->inst_type == CODA_INST_DECODER)
1456 return v4l2_event_subscribe(fh, sub, 0, NULL);
1460 return v4l2_ctrl_subscribe_event(fh, sub);
1464 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1465 .vidioc_querycap = coda_querycap,
1467 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1468 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1469 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1470 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1472 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1473 .vidioc_g_fmt_vid_out = coda_g_fmt,
1474 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1475 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1477 .vidioc_reqbufs = coda_reqbufs,
1478 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1480 .vidioc_qbuf = coda_qbuf,
1481 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1482 .vidioc_dqbuf = coda_dqbuf,
1483 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1484 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1486 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1487 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1489 .vidioc_g_selection = coda_g_selection,
1490 .vidioc_s_selection = coda_s_selection,
1492 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
1493 .vidioc_encoder_cmd = coda_encoder_cmd,
1494 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
1495 .vidioc_decoder_cmd = coda_decoder_cmd,
1497 .vidioc_g_parm = coda_g_parm,
1498 .vidioc_s_parm = coda_s_parm,
1500 .vidioc_enum_framesizes = coda_enum_framesizes,
1501 .vidioc_enum_frameintervals = coda_enum_frameintervals,
1503 .vidioc_subscribe_event = coda_subscribe_event,
1504 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1508 * Mem-to-mem operations.
1511 static void coda_device_run(void *m2m_priv)
1513 struct coda_ctx *ctx = m2m_priv;
1514 struct coda_dev *dev = ctx->dev;
1516 queue_work(dev->workqueue, &ctx->pic_run_work);
1519 static void coda_pic_run_work(struct work_struct *work)
1521 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1522 struct coda_dev *dev = ctx->dev;
1525 mutex_lock(&ctx->buffer_mutex);
1526 mutex_lock(&dev->coda_mutex);
1528 ret = ctx->ops->prepare_run(ctx);
1529 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER)
1532 if (!wait_for_completion_timeout(&ctx->completion,
1533 msecs_to_jiffies(1000))) {
1535 dev_err(dev->dev, "CODA PIC_RUN timeout\n");
1542 if (ctx->ops->run_timeout)
1543 ctx->ops->run_timeout(ctx);
1545 ctx->ops->finish_run(ctx);
1548 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1549 ctx->ops->seq_end_work)
1550 queue_work(dev->workqueue, &ctx->seq_end_work);
1553 mutex_unlock(&dev->coda_mutex);
1554 mutex_unlock(&ctx->buffer_mutex);
1556 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1559 static int coda_job_ready(void *m2m_priv)
1561 struct coda_ctx *ctx = m2m_priv;
1562 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1565 * For both 'P' and 'key' frame cases 1 picture
1566 * and 1 frame are needed. In the decoder case,
1567 * the compressed frame can be in the bitstream.
1569 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1570 coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
1574 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1575 coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
1579 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1580 bool stream_end = ctx->bit_stream_param &
1581 CODA_BIT_STREAM_END_FLAG;
1582 int num_metas = ctx->num_metas;
1583 struct coda_buffer_meta *meta;
1586 count = hweight32(ctx->frm_dis_flg);
1587 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1589 "not ready: all internal buffers in use: %d/%d (0x%x)",
1590 count, ctx->num_internal_frames,
1595 if (ctx->hold && !src_bufs) {
1597 "not ready: on hold for more buffers.\n");
1601 if (!stream_end && (num_metas + src_bufs) < 2) {
1603 "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1604 num_metas, src_bufs);
1608 meta = list_first_entry(&ctx->buffer_meta_list,
1609 struct coda_buffer_meta, list);
1610 if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1613 "not ready: not enough bitstream data to read past %u (%u)\n",
1614 meta->end, ctx->bitstream_fifo.kfifo.in);
1619 if (ctx->aborting) {
1620 coda_dbg(1, ctx, "not ready: aborting\n");
1624 coda_dbg(2, ctx, "job ready\n");
1629 static void coda_job_abort(void *priv)
1631 struct coda_ctx *ctx = priv;
1635 coda_dbg(1, ctx, "job abort\n");
1638 static const struct v4l2_m2m_ops coda_m2m_ops = {
1639 .device_run = coda_device_run,
1640 .job_ready = coda_job_ready,
1641 .job_abort = coda_job_abort,
1644 static void set_default_params(struct coda_ctx *ctx)
1646 unsigned int max_w, max_h, usize, csize;
1648 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1649 ctx->cvd->dst_formats[0]);
1650 max_w = min(ctx->codec->max_w, 1920U);
1651 max_h = min(ctx->codec->max_h, 1088U);
1652 usize = max_w * max_h * 3 / 2;
1653 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1655 ctx->params.codec_mode = ctx->codec->mode;
1656 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1657 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1659 ctx->colorspace = V4L2_COLORSPACE_REC709;
1660 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1661 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1662 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1663 ctx->params.framerate = 30;
1665 /* Default formats for output and input queues */
1666 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1667 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1668 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1669 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1670 ctx->q_data[V4L2_M2M_DST].width = max_w;
1671 ctx->q_data[V4L2_M2M_DST].height = max_h;
1672 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1673 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1674 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1675 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1676 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1678 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1679 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1680 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1681 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1683 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1684 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1685 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1686 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1689 * Since the RBC2AXI logic only supports a single chroma plane,
1690 * macroblock tiling only works for to NV12 pixel format.
1692 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1698 static int coda_queue_setup(struct vb2_queue *vq,
1699 unsigned int *nbuffers, unsigned int *nplanes,
1700 unsigned int sizes[], struct device *alloc_devs[])
1702 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1703 struct coda_q_data *q_data;
1706 q_data = get_q_data(ctx, vq->type);
1707 size = q_data->sizeimage;
1710 return sizes[0] < size ? -EINVAL : 0;
1715 coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1721 static int coda_buf_prepare(struct vb2_buffer *vb)
1723 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1724 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1725 struct coda_q_data *q_data;
1727 q_data = get_q_data(ctx, vb->vb2_queue->type);
1728 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1729 if (vbuf->field == V4L2_FIELD_ANY)
1730 vbuf->field = V4L2_FIELD_NONE;
1731 if (vbuf->field != V4L2_FIELD_NONE) {
1732 v4l2_warn(&ctx->dev->v4l2_dev,
1733 "%s field isn't supported\n", __func__);
1738 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1739 v4l2_warn(&ctx->dev->v4l2_dev,
1740 "%s data will not fit into plane (%lu < %lu)\n",
1741 __func__, vb2_plane_size(vb, 0),
1742 (long)q_data->sizeimage);
1749 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1754 v4l2_ctrl_lock(ctrl);
1757 * Extend the control range if the parsed stream contains a known but
1758 * unsupported value or level.
1760 if (value > ctrl->maximum) {
1761 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1762 ctrl->menu_skip_mask & ~(1 << value),
1763 ctrl->default_value);
1764 } else if (value < ctrl->minimum) {
1765 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1766 ctrl->menu_skip_mask & ~(1 << value),
1767 ctrl->default_value);
1770 __v4l2_ctrl_s_ctrl(ctrl, value);
1772 v4l2_ctrl_unlock(ctrl);
1775 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
1778 const char * const *profile_names;
1779 const char * const *level_names;
1780 struct v4l2_ctrl *profile_ctrl;
1781 struct v4l2_ctrl *level_ctrl;
1782 const char *codec_name;
1788 switch (ctx->codec->src_fourcc) {
1789 case V4L2_PIX_FMT_H264:
1790 codec_name = "H264";
1791 profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
1792 level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
1793 profile_ctrl = ctx->h264_profile_ctrl;
1794 level_ctrl = ctx->h264_level_ctrl;
1795 profile = coda_h264_profile(profile_idc);
1796 level = coda_h264_level(level_idc);
1798 case V4L2_PIX_FMT_MPEG2:
1799 codec_name = "MPEG-2";
1800 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE;
1801 level_cid = V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL;
1802 profile_ctrl = ctx->mpeg2_profile_ctrl;
1803 level_ctrl = ctx->mpeg2_level_ctrl;
1804 profile = coda_mpeg2_profile(profile_idc);
1805 level = coda_mpeg2_level(level_idc);
1807 case V4L2_PIX_FMT_MPEG4:
1808 codec_name = "MPEG-4";
1809 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
1810 level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
1811 profile_ctrl = ctx->mpeg4_profile_ctrl;
1812 level_ctrl = ctx->mpeg4_level_ctrl;
1813 profile = coda_mpeg4_profile(profile_idc);
1814 level = coda_mpeg4_level(level_idc);
1820 profile_names = v4l2_ctrl_get_menu(profile_cid);
1821 level_names = v4l2_ctrl_get_menu(level_cid);
1824 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s profile: %u\n",
1825 codec_name, profile_idc);
1827 coda_dbg(1, ctx, "Parsed %s profile: %s\n", codec_name,
1828 profile_names[profile]);
1829 coda_update_menu_ctrl(profile_ctrl, profile);
1833 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s level: %u\n",
1834 codec_name, level_idc);
1836 coda_dbg(1, ctx, "Parsed %s level: %s\n", codec_name,
1837 level_names[level]);
1838 coda_update_menu_ctrl(level_ctrl, level);
1842 static void coda_queue_source_change_event(struct coda_ctx *ctx)
1844 static const struct v4l2_event source_change_event = {
1845 .type = V4L2_EVENT_SOURCE_CHANGE,
1846 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1849 v4l2_event_queue_fh(&ctx->fh, &source_change_event);
1852 static void coda_buf_queue(struct vb2_buffer *vb)
1854 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1855 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1856 struct vb2_queue *vq = vb->vb2_queue;
1857 struct coda_q_data *q_data;
1859 q_data = get_q_data(ctx, vb->vb2_queue->type);
1862 * In the decoder case, immediately try to copy the buffer into the
1863 * bitstream ringbuffer and mark it as ready to be dequeued.
1865 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1867 * For backwards compatibility, queuing an empty buffer marks
1870 if (vb2_get_plane_payload(vb, 0) == 0)
1871 coda_bit_stream_end_flag(ctx);
1873 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1875 * Unless already done, try to obtain profile_idc and
1876 * level_idc from the SPS header. This allows to decide
1877 * whether to enable reordering during sequence
1880 if (!ctx->params.h264_profile_idc) {
1881 coda_sps_parse_profile(ctx, vb);
1882 coda_update_profile_level_ctrls(ctx,
1883 ctx->params.h264_profile_idc,
1884 ctx->params.h264_level_idc);
1888 mutex_lock(&ctx->bitstream_mutex);
1889 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1890 if (vb2_is_streaming(vb->vb2_queue))
1891 /* This set buf->sequence = ctx->qsequence++ */
1892 coda_fill_bitstream(ctx, NULL);
1893 mutex_unlock(&ctx->bitstream_mutex);
1895 if (!ctx->initialized) {
1897 * Run sequence initialization in case the queued
1898 * buffer contained headers.
1900 if (vb2_is_streaming(vb->vb2_queue) &&
1901 ctx->ops->seq_init_work) {
1902 queue_work(ctx->dev->workqueue,
1903 &ctx->seq_init_work);
1904 flush_work(&ctx->seq_init_work);
1907 if (ctx->initialized)
1908 coda_queue_source_change_event(ctx);
1911 if ((ctx->inst_type == CODA_INST_ENCODER || !ctx->use_bit) &&
1912 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1913 vbuf->sequence = ctx->qsequence++;
1914 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1918 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1919 size_t size, const char *name, struct dentry *parent)
1921 buf->vaddr = dma_alloc_coherent(dev->dev, size, &buf->paddr,
1924 v4l2_err(&dev->v4l2_dev,
1925 "Failed to allocate %s buffer of size %zu\n",
1932 if (name && parent) {
1933 buf->blob.data = buf->vaddr;
1934 buf->blob.size = size;
1935 buf->dentry = debugfs_create_blob(name, 0444, parent,
1942 void coda_free_aux_buf(struct coda_dev *dev,
1943 struct coda_aux_buf *buf)
1946 dma_free_coherent(dev->dev, buf->size, buf->vaddr, buf->paddr);
1949 debugfs_remove(buf->dentry);
1954 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1956 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1957 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1958 struct coda_q_data *q_data_src, *q_data_dst;
1959 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1960 struct vb2_v4l2_buffer *buf;
1961 struct list_head list;
1967 coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1969 INIT_LIST_HEAD(&list);
1971 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1972 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1973 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1974 /* copy the buffers that were queued before streamon */
1975 mutex_lock(&ctx->bitstream_mutex);
1976 coda_fill_bitstream(ctx, &list);
1977 mutex_unlock(&ctx->bitstream_mutex);
1979 if (ctx->dev->devtype->product != CODA_960 &&
1980 coda_get_bitstream_payload(ctx) < 512) {
1981 v4l2_err(v4l2_dev, "start payload < 512\n");
1986 if (!ctx->initialized) {
1987 /* Run sequence initialization */
1988 if (ctx->ops->seq_init_work) {
1989 queue_work(ctx->dev->workqueue,
1990 &ctx->seq_init_work);
1991 flush_work(&ctx->seq_init_work);
1997 * Check the first input JPEG buffer to determine chroma
2000 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
2001 buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2002 ret = coda_jpeg_decode_header(ctx, &buf->vb2_buf);
2005 "failed to decode JPEG header: %d\n",
2010 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2011 q_data_dst->width = round_up(q_data_src->width, 16);
2012 q_data_dst->height = round_up(q_data_src->height, 16);
2013 q_data_dst->bytesperline = q_data_dst->width;
2014 if (ctx->params.jpeg_chroma_subsampling ==
2015 V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
2016 q_data_dst->sizeimage =
2017 q_data_dst->bytesperline *
2018 q_data_dst->height * 3 / 2;
2019 if (q_data_dst->fourcc != V4L2_PIX_FMT_YUV420)
2020 q_data_dst->fourcc = V4L2_PIX_FMT_NV12;
2022 q_data_dst->sizeimage =
2023 q_data_dst->bytesperline *
2024 q_data_dst->height * 2;
2025 q_data_dst->fourcc = V4L2_PIX_FMT_YUV422P;
2027 q_data_dst->rect.left = 0;
2028 q_data_dst->rect.top = 0;
2029 q_data_dst->rect.width = q_data_src->width;
2030 q_data_dst->rect.height = q_data_src->height;
2032 ctx->streamon_out = 1;
2034 ctx->streamon_cap = 1;
2037 /* Don't start the coda unless both queues are on */
2038 if (!(ctx->streamon_out && ctx->streamon_cap))
2041 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2042 if ((q_data_src->rect.width != q_data_dst->width &&
2043 round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
2044 (q_data_src->rect.height != q_data_dst->height &&
2045 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
2046 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
2047 q_data_src->rect.width, q_data_src->rect.height,
2048 q_data_dst->width, q_data_dst->height);
2053 /* Allow BIT decoder device_run with no new buffers queued */
2054 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2055 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
2057 ctx->gopcounter = ctx->params.gop_size - 1;
2059 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
2060 ctx->params.gop_size = 1;
2061 ctx->gopcounter = ctx->params.gop_size - 1;
2062 /* Only decoders have this control */
2063 if (ctx->mb_err_cnt_ctrl)
2064 v4l2_ctrl_s_ctrl(ctx->mb_err_cnt_ctrl, 0);
2066 ret = ctx->ops->start_streaming(ctx);
2067 if (ctx->inst_type == CODA_INST_DECODER) {
2075 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2076 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2077 list_del(&m2m_buf->list);
2078 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
2084 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2085 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2086 list_del(&m2m_buf->list);
2087 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
2089 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2090 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2092 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2093 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2098 static void coda_stop_streaming(struct vb2_queue *q)
2100 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2101 struct coda_dev *dev = ctx->dev;
2102 struct vb2_v4l2_buffer *buf;
2105 stop = ctx->streamon_out && ctx->streamon_cap;
2107 coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
2109 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2110 ctx->streamon_out = 0;
2112 coda_bit_stream_end_flag(ctx);
2116 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2117 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2119 ctx->streamon_cap = 0;
2122 ctx->sequence_offset = 0;
2124 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2125 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2129 struct coda_buffer_meta *meta;
2131 if (ctx->ops->seq_end_work) {
2132 queue_work(dev->workqueue, &ctx->seq_end_work);
2133 flush_work(&ctx->seq_end_work);
2135 spin_lock(&ctx->buffer_meta_lock);
2136 while (!list_empty(&ctx->buffer_meta_list)) {
2137 meta = list_first_entry(&ctx->buffer_meta_list,
2138 struct coda_buffer_meta, list);
2139 list_del(&meta->list);
2143 spin_unlock(&ctx->buffer_meta_lock);
2144 kfifo_init(&ctx->bitstream_fifo,
2145 ctx->bitstream.vaddr, ctx->bitstream.size);
2146 ctx->runcounter = 0;
2151 if (!ctx->streamon_out && !ctx->streamon_cap)
2152 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
2155 static const struct vb2_ops coda_qops = {
2156 .queue_setup = coda_queue_setup,
2157 .buf_prepare = coda_buf_prepare,
2158 .buf_queue = coda_buf_queue,
2159 .start_streaming = coda_start_streaming,
2160 .stop_streaming = coda_stop_streaming,
2161 .wait_prepare = vb2_ops_wait_prepare,
2162 .wait_finish = vb2_ops_wait_finish,
2165 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2167 const char * const *val_names = v4l2_ctrl_get_menu(ctrl->id);
2168 struct coda_ctx *ctx =
2169 container_of(ctrl->handler, struct coda_ctx, ctrls);
2172 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d (\"%s\")\n",
2173 ctrl->id, ctrl->name, ctrl->val, val_names[ctrl->val]);
2175 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
2176 ctrl->id, ctrl->name, ctrl->val);
2179 case V4L2_CID_HFLIP:
2181 ctx->params.rot_mode |= CODA_MIR_HOR;
2183 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2185 case V4L2_CID_VFLIP:
2187 ctx->params.rot_mode |= CODA_MIR_VER;
2189 ctx->params.rot_mode &= ~CODA_MIR_VER;
2191 case V4L2_CID_MPEG_VIDEO_BITRATE:
2192 ctx->params.bitrate = ctrl->val / 1000;
2193 ctx->params.bitrate_changed = true;
2195 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2196 ctx->params.gop_size = ctrl->val;
2198 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2199 ctx->params.h264_intra_qp = ctrl->val;
2200 ctx->params.h264_intra_qp_changed = true;
2202 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2203 ctx->params.h264_inter_qp = ctrl->val;
2205 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2206 ctx->params.h264_min_qp = ctrl->val;
2208 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2209 ctx->params.h264_max_qp = ctrl->val;
2211 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2212 ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
2214 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2215 ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
2217 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2218 ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
2220 case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
2221 ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
2223 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
2224 ctx->params.frame_rc_enable = ctrl->val;
2226 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
2227 ctx->params.mb_rc_enable = ctrl->val;
2229 case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
2230 ctx->params.h264_chroma_qp_index_offset = ctrl->val;
2232 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
2233 /* TODO: switch between baseline and constrained baseline */
2234 if (ctx->inst_type == CODA_INST_ENCODER)
2235 ctx->params.h264_profile_idc = 66;
2237 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2238 /* nothing to do, this is set by the encoder */
2240 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2241 ctx->params.mpeg4_intra_qp = ctrl->val;
2243 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2244 ctx->params.mpeg4_inter_qp = ctrl->val;
2246 case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
2247 case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
2248 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
2249 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
2250 /* nothing to do, these are fixed */
2252 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2253 ctx->params.slice_mode = ctrl->val;
2254 ctx->params.slice_mode_changed = true;
2256 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2257 ctx->params.slice_max_mb = ctrl->val;
2258 ctx->params.slice_mode_changed = true;
2260 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2261 ctx->params.slice_max_bits = ctrl->val * 8;
2262 ctx->params.slice_mode_changed = true;
2264 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2266 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2267 ctx->params.intra_refresh = ctrl->val;
2268 ctx->params.intra_refresh_changed = true;
2270 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
2271 ctx->params.force_ipicture = true;
2273 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2274 coda_set_jpeg_compression_quality(ctx, ctrl->val);
2276 case V4L2_CID_JPEG_RESTART_INTERVAL:
2277 ctx->params.jpeg_restart_interval = ctrl->val;
2279 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
2280 ctx->params.vbv_delay = ctrl->val;
2282 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
2283 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
2286 coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
2287 ctrl->id, ctrl->val);
2294 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
2295 .s_ctrl = coda_s_ctrl,
2298 static void coda_encode_ctrls(struct coda_ctx *ctx)
2300 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
2302 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2303 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
2304 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2305 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
2306 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2307 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
2308 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2309 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
2310 if (ctx->dev->devtype->product != CODA_960) {
2311 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2312 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2314 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2315 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
2316 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2317 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
2318 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2319 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
2320 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2321 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2322 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
2323 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2324 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2325 V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
2327 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2328 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
2329 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2330 V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, 0, 1, 1, 1);
2331 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2332 V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
2333 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2334 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2335 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2336 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2337 if (ctx->dev->devtype->product == CODA_HX4 ||
2338 ctx->dev->devtype->product == CODA_7541) {
2339 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2340 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2341 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2342 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2343 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2344 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2345 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2347 if (ctx->dev->devtype->product == CODA_960) {
2348 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2349 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2350 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
2351 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2352 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2353 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2354 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2355 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
2356 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2358 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2359 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2360 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2361 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2362 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2363 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2364 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2365 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
2366 if (ctx->dev->devtype->product == CODA_HX4 ||
2367 ctx->dev->devtype->product == CODA_7541 ||
2368 ctx->dev->devtype->product == CODA_960) {
2369 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2370 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2371 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2372 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2373 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2375 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2376 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
2377 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES, 0x0,
2378 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
2379 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2380 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
2381 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2382 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2384 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2385 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2386 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2387 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2388 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2389 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2390 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2391 1920 * 1088 / 256, 1, 0);
2392 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2393 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2395 * The maximum VBV size value is 0x7fffffff bits,
2396 * one bit less than 262144 KiB
2398 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2399 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
2402 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2404 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2405 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2406 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2407 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2410 static void coda_decode_ctrls(struct coda_ctx *ctx)
2414 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2415 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2416 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2417 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
2418 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2419 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2420 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2421 if (ctx->h264_profile_ctrl)
2422 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2424 if (ctx->dev->devtype->product == CODA_HX4 ||
2425 ctx->dev->devtype->product == CODA_7541)
2426 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2427 else if (ctx->dev->devtype->product == CODA_960)
2428 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
2431 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2432 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
2433 if (ctx->h264_level_ctrl)
2434 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2436 ctx->mpeg2_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2437 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE,
2438 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH, 0,
2439 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH);
2440 if (ctx->mpeg2_profile_ctrl)
2441 ctx->mpeg2_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2443 ctx->mpeg2_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2444 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL,
2445 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH, 0,
2446 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH);
2447 if (ctx->mpeg2_level_ctrl)
2448 ctx->mpeg2_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2450 ctx->mpeg4_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2451 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2452 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY, 0,
2453 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY);
2454 if (ctx->mpeg4_profile_ctrl)
2455 ctx->mpeg4_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2457 ctx->mpeg4_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2458 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2459 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 0,
2460 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2461 if (ctx->mpeg4_level_ctrl)
2462 ctx->mpeg4_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2465 static const struct v4l2_ctrl_config coda_mb_err_cnt_ctrl_config = {
2466 .id = V4L2_CID_CODA_MB_ERR_CNT,
2467 .name = "Macroblocks Error Count",
2468 .type = V4L2_CTRL_TYPE_INTEGER,
2474 static int coda_ctrls_setup(struct coda_ctx *ctx)
2476 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2478 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2479 V4L2_CID_HFLIP, 0, 1, 1, 0);
2480 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2481 V4L2_CID_VFLIP, 0, 1, 1, 0);
2482 if (ctx->inst_type == CODA_INST_ENCODER) {
2483 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2484 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2486 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2487 coda_jpeg_encode_ctrls(ctx);
2489 coda_encode_ctrls(ctx);
2491 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2492 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2494 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2495 coda_decode_ctrls(ctx);
2497 ctx->mb_err_cnt_ctrl = v4l2_ctrl_new_custom(&ctx->ctrls,
2498 &coda_mb_err_cnt_ctrl_config,
2500 if (ctx->mb_err_cnt_ctrl)
2501 ctx->mb_err_cnt_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2504 if (ctx->ctrls.error) {
2505 v4l2_err(&ctx->dev->v4l2_dev,
2506 "control initialization error (%d)",
2511 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2514 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2517 vq->ops = &coda_qops;
2518 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2519 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2520 vq->lock = &ctx->dev->dev_mutex;
2521 /* One way to indicate end-of-stream for coda is to set the
2522 * bytesused == 0. However by default videobuf2 handles bytesused
2523 * equal to 0 as a special case and changes its value to the size
2524 * of the buffer. Set the allow_zero_bytesused flag, so
2525 * that videobuf2 will keep the value of bytesused intact.
2527 vq->allow_zero_bytesused = 1;
2529 * We might be fine with no buffers on some of the queues, but that
2530 * would need to be reflected in job_ready(). Currently we expect all
2531 * queues to have at least one buffer queued.
2533 vq->min_buffers_needed = 1;
2534 vq->dev = ctx->dev->dev;
2536 return vb2_queue_init(vq);
2539 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2540 struct vb2_queue *dst_vq)
2544 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2545 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2546 src_vq->mem_ops = &vb2_dma_contig_memops;
2548 ret = coda_queue_init(priv, src_vq);
2552 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2553 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2554 dst_vq->mem_ops = &vb2_dma_contig_memops;
2556 return coda_queue_init(priv, dst_vq);
2559 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2560 struct vb2_queue *dst_vq)
2564 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2565 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2566 src_vq->mem_ops = &vb2_vmalloc_memops;
2568 ret = coda_queue_init(priv, src_vq);
2572 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2573 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2574 dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING;
2575 dst_vq->mem_ops = &vb2_dma_contig_memops;
2577 return coda_queue_init(priv, dst_vq);
2584 static int coda_open(struct file *file)
2586 struct video_device *vdev = video_devdata(file);
2587 struct coda_dev *dev = video_get_drvdata(vdev);
2588 struct coda_ctx *ctx;
2589 unsigned int max = ~0;
2594 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2598 if (dev->devtype->product == CODA_DX6)
2599 max = CODADX6_MAX_INSTANCES - 1;
2600 idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2606 name = kasprintf(GFP_KERNEL, "context%d", idx);
2609 goto err_coda_name_init;
2612 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2615 ctx->cvd = to_coda_video_device(vdev);
2616 ctx->inst_type = ctx->cvd->type;
2617 ctx->ops = ctx->cvd->ops;
2618 ctx->use_bit = !ctx->cvd->direct;
2619 init_completion(&ctx->completion);
2620 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2621 if (ctx->ops->seq_init_work)
2622 INIT_WORK(&ctx->seq_init_work, ctx->ops->seq_init_work);
2623 if (ctx->ops->seq_end_work)
2624 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2625 v4l2_fh_init(&ctx->fh, video_devdata(file));
2626 file->private_data = &ctx->fh;
2627 v4l2_fh_add(&ctx->fh);
2631 coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2633 switch (dev->devtype->product) {
2636 * Enabling the BWB when decoding can hang the firmware with
2637 * certain streams. The issue was tracked as ENGR00293425 by
2638 * Freescale. As a workaround, disable BWB for all decoders.
2639 * The enable_bwb module parameter allows to override this.
2641 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2642 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2651 if (ctx->dev->vdoa && !disable_vdoa) {
2652 ctx->vdoa = vdoa_context_create(dev->vdoa);
2654 v4l2_warn(&dev->v4l2_dev,
2655 "Failed to create vdoa context: not using vdoa");
2657 ctx->use_vdoa = false;
2659 /* Power up and upload firmware if necessary */
2660 ret = pm_runtime_resume_and_get(dev->dev);
2662 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2666 ret = clk_prepare_enable(dev->clk_per);
2668 goto err_clk_enable;
2670 ret = clk_prepare_enable(dev->clk_ahb);
2674 set_default_params(ctx);
2675 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2676 ctx->ops->queue_init);
2677 if (IS_ERR(ctx->fh.m2m_ctx)) {
2678 ret = PTR_ERR(ctx->fh.m2m_ctx);
2680 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2685 ret = coda_ctrls_setup(ctx);
2687 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2688 goto err_ctrls_setup;
2691 ctx->fh.ctrl_handler = &ctx->ctrls;
2693 mutex_init(&ctx->bitstream_mutex);
2694 mutex_init(&ctx->buffer_mutex);
2695 mutex_init(&ctx->wakeup_mutex);
2696 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2697 spin_lock_init(&ctx->buffer_meta_lock);
2702 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2704 clk_disable_unprepare(dev->clk_ahb);
2706 clk_disable_unprepare(dev->clk_per);
2708 pm_runtime_put_sync(dev->dev);
2710 v4l2_fh_del(&ctx->fh);
2711 v4l2_fh_exit(&ctx->fh);
2713 ida_free(&dev->ida, ctx->idx);
2719 static int coda_release(struct file *file)
2721 struct coda_dev *dev = video_drvdata(file);
2722 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2724 coda_dbg(1, ctx, "release instance (%p)\n", ctx);
2726 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2727 coda_bit_stream_end_flag(ctx);
2729 /* If this instance is running, call .job_abort and wait for it to end */
2730 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2733 vdoa_context_destroy(ctx->vdoa);
2735 /* In case the instance was not running, we still need to call SEQ_END */
2736 if (ctx->ops->seq_end_work) {
2737 queue_work(dev->workqueue, &ctx->seq_end_work);
2738 flush_work(&ctx->seq_end_work);
2741 if (ctx->dev->devtype->product == CODA_DX6)
2742 coda_free_aux_buf(dev, &ctx->workbuf);
2744 v4l2_ctrl_handler_free(&ctx->ctrls);
2745 clk_disable_unprepare(dev->clk_ahb);
2746 clk_disable_unprepare(dev->clk_per);
2747 pm_runtime_put_sync(dev->dev);
2748 v4l2_fh_del(&ctx->fh);
2749 v4l2_fh_exit(&ctx->fh);
2750 ida_free(&dev->ida, ctx->idx);
2751 if (ctx->ops->release)
2752 ctx->ops->release(ctx);
2753 debugfs_remove_recursive(ctx->debugfs_entry);
2759 static const struct v4l2_file_operations coda_fops = {
2760 .owner = THIS_MODULE,
2762 .release = coda_release,
2763 .poll = v4l2_m2m_fop_poll,
2764 .unlocked_ioctl = video_ioctl2,
2765 .mmap = v4l2_m2m_fop_mmap,
2768 static int coda_hw_init(struct coda_dev *dev)
2774 ret = clk_prepare_enable(dev->clk_per);
2778 ret = clk_prepare_enable(dev->clk_ahb);
2782 reset_control_reset(dev->rstc);
2785 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2786 * The 16-bit chars in the code buffer are in memory access
2787 * order, re-sort them to CODA order for register download.
2788 * Data in this SRAM survives a reboot.
2790 p = (u16 *)dev->codebuf.vaddr;
2791 if (dev->devtype->product == CODA_DX6) {
2792 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2793 data = CODA_DOWN_ADDRESS_SET(i) |
2794 CODA_DOWN_DATA_SET(p[i ^ 1]);
2795 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2798 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2799 data = CODA_DOWN_ADDRESS_SET(i) |
2800 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2802 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2806 /* Clear registers */
2807 for (i = 0; i < 64; i++)
2808 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2810 /* Tell the BIT where to find everything it needs */
2811 if (dev->devtype->product == CODA_960 ||
2812 dev->devtype->product == CODA_7541 ||
2813 dev->devtype->product == CODA_HX4) {
2814 coda_write(dev, dev->tempbuf.paddr,
2815 CODA_REG_BIT_TEMP_BUF_ADDR);
2816 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2818 coda_write(dev, dev->workbuf.paddr,
2819 CODA_REG_BIT_WORK_BUF_ADDR);
2821 coda_write(dev, dev->codebuf.paddr,
2822 CODA_REG_BIT_CODE_BUF_ADDR);
2823 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2825 /* Set default values */
2826 switch (dev->devtype->product) {
2828 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2829 CODA_REG_BIT_STREAM_CTRL);
2832 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2833 CODA_REG_BIT_STREAM_CTRL);
2835 if (dev->devtype->product == CODA_960)
2836 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2837 CODA_REG_BIT_FRAME_MEM_CTRL);
2839 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2841 if (dev->devtype->product != CODA_DX6)
2842 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2844 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2845 CODA_REG_BIT_INT_ENABLE);
2847 /* Reset VPU and start processor */
2848 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2849 data |= CODA_REG_RESET_ENABLE;
2850 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2852 data &= ~CODA_REG_RESET_ENABLE;
2853 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2854 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2856 clk_disable_unprepare(dev->clk_ahb);
2857 clk_disable_unprepare(dev->clk_per);
2862 clk_disable_unprepare(dev->clk_per);
2867 static int coda_register_device(struct coda_dev *dev, int i)
2869 struct video_device *vfd = &dev->vfd[i];
2873 if (i >= dev->devtype->num_vdevs)
2875 name = dev->devtype->vdevs[i]->name;
2877 strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2878 vfd->fops = &coda_fops;
2879 vfd->ioctl_ops = &coda_ioctl_ops;
2880 vfd->release = video_device_release_empty;
2881 vfd->lock = &dev->dev_mutex;
2882 vfd->v4l2_dev = &dev->v4l2_dev;
2883 vfd->vfl_dir = VFL_DIR_M2M;
2884 vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2885 video_set_drvdata(vfd, dev);
2887 /* Not applicable, use the selection API instead */
2888 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2889 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2890 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2892 if (dev->devtype->vdevs[i]->type == CODA_INST_ENCODER) {
2893 v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
2894 v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
2895 if (dev->devtype->vdevs[i]->dst_formats[0] == V4L2_PIX_FMT_JPEG) {
2896 v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMEINTERVALS);
2897 v4l2_disable_ioctl(vfd, VIDIOC_G_PARM);
2898 v4l2_disable_ioctl(vfd, VIDIOC_S_PARM);
2901 v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
2902 v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
2903 v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMESIZES);
2904 v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMEINTERVALS);
2905 v4l2_disable_ioctl(vfd, VIDIOC_G_PARM);
2906 v4l2_disable_ioctl(vfd, VIDIOC_S_PARM);
2909 ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
2911 v4l2_info(&dev->v4l2_dev, "%s registered as %s\n",
2912 name, video_device_node_name(vfd));
2916 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2919 u32 *src = (u32 *)buf;
2921 /* Check if the firmware has a 16-byte Freescale header, skip it */
2922 if (buf[0] == 'M' && buf[1] == 'X')
2925 * Check whether the firmware is in native order or pre-reordered for
2926 * memory access. The first instruction opcode always is 0xe40e.
2928 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2929 u32 *dst = dev->codebuf.vaddr;
2932 /* Firmware in native order, reorder while copying */
2933 if (dev->devtype->product == CODA_DX6) {
2934 for (i = 0; i < (size - 16) / 4; i++)
2935 dst[i] = (src[i] << 16) | (src[i] >> 16);
2937 for (i = 0; i < (size - 16) / 4; i += 2) {
2938 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2939 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2943 /* Copy the already reordered firmware image */
2944 memcpy(dev->codebuf.vaddr, src, size);
2948 static void coda_fw_callback(const struct firmware *fw, void *context);
2950 static int coda_firmware_request(struct coda_dev *dev)
2954 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2957 fw = dev->devtype->firmware[dev->firmware];
2959 dev_dbg(dev->dev, "requesting firmware '%s' for %s\n", fw,
2960 coda_product_name(dev->devtype->product));
2962 return request_firmware_nowait(THIS_MODULE, true, fw, dev->dev,
2963 GFP_KERNEL, dev, coda_fw_callback);
2966 static void coda_fw_callback(const struct firmware *fw, void *context)
2968 struct coda_dev *dev = context;
2973 ret = coda_firmware_request(dev);
2975 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2980 if (dev->firmware > 0) {
2982 * Since we can't suppress warnings for failed asynchronous
2983 * firmware requests, report that the fallback firmware was
2986 dev_info(dev->dev, "Using fallback firmware %s\n",
2987 dev->devtype->firmware[dev->firmware]);
2990 /* allocate auxiliary per-device code buffer for the BIT processor */
2991 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2996 coda_copy_firmware(dev, fw->data, fw->size);
2997 release_firmware(fw);
2999 ret = coda_hw_init(dev);
3001 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3005 ret = coda_check_firmware(dev);
3009 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3010 if (IS_ERR(dev->m2m_dev)) {
3011 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3015 for (i = 0; i < dev->devtype->num_vdevs; i++) {
3016 ret = coda_register_device(dev, i);
3018 v4l2_err(&dev->v4l2_dev,
3019 "Failed to register %s video device: %d\n",
3020 dev->devtype->vdevs[i]->name, ret);
3025 pm_runtime_put_sync(dev->dev);
3030 video_unregister_device(&dev->vfd[i]);
3031 v4l2_m2m_release(dev->m2m_dev);
3033 pm_runtime_put_sync(dev->dev);
3036 enum coda_platform {
3044 static const struct coda_devtype coda_devdata[] = {
3047 "vpu_fw_imx27_TO2.bin",
3048 "vpu/vpu_fw_imx27_TO2.bin",
3049 "v4l-codadx6-imx27.bin"
3051 .product = CODA_DX6,
3052 .codecs = codadx6_codecs,
3053 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3054 .vdevs = codadx6_video_devices,
3055 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
3056 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
3057 .iram_size = 0xb000,
3062 "vpu/vpu_fw_imx51.bin",
3063 "v4l-codahx4-imx51.bin"
3065 .product = CODA_HX4,
3066 .codecs = codahx4_codecs,
3067 .num_codecs = ARRAY_SIZE(codahx4_codecs),
3068 .vdevs = codahx4_video_devices,
3069 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
3070 .workbuf_size = 128 * 1024,
3071 .tempbuf_size = 304 * 1024,
3072 .iram_size = 0x14000,
3077 "vpu/vpu_fw_imx53.bin",
3078 "v4l-coda7541-imx53.bin"
3080 .product = CODA_7541,
3081 .codecs = coda7_codecs,
3082 .num_codecs = ARRAY_SIZE(coda7_codecs),
3083 .vdevs = coda7_video_devices,
3084 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
3085 .workbuf_size = 128 * 1024,
3086 .tempbuf_size = 304 * 1024,
3087 .iram_size = 0x14000,
3092 "vpu/vpu_fw_imx6q.bin",
3093 "v4l-coda960-imx6q.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 = 0x21000,
3107 "vpu/vpu_fw_imx6d.bin",
3108 "v4l-coda960-imx6dl.bin"
3110 .product = CODA_960,
3111 .codecs = coda9_codecs,
3112 .num_codecs = ARRAY_SIZE(coda9_codecs),
3113 .vdevs = coda9_video_devices,
3114 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
3115 .workbuf_size = 80 * 1024,
3116 .tempbuf_size = 204 * 1024,
3117 .iram_size = 0x1f000, /* leave 4k for suspend code */
3121 static const struct of_device_id coda_dt_ids[] = {
3122 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
3123 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
3124 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
3125 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3126 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
3129 MODULE_DEVICE_TABLE(of, coda_dt_ids);
3131 static int coda_probe(struct platform_device *pdev)
3133 struct device_node *np = pdev->dev.of_node;
3134 struct gen_pool *pool;
3135 struct coda_dev *dev;
3138 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3142 dev->devtype = of_device_get_match_data(&pdev->dev);
3144 dev->dev = &pdev->dev;
3145 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3146 if (IS_ERR(dev->clk_per)) {
3147 dev_err(&pdev->dev, "Could not get per clock\n");
3148 return PTR_ERR(dev->clk_per);
3151 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3152 if (IS_ERR(dev->clk_ahb)) {
3153 dev_err(&pdev->dev, "Could not get ahb clock\n");
3154 return PTR_ERR(dev->clk_ahb);
3157 /* Get memory for physical registers */
3158 dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
3159 if (IS_ERR(dev->regs_base))
3160 return PTR_ERR(dev->regs_base);
3163 irq = platform_get_irq_byname(pdev, "bit");
3165 irq = platform_get_irq(pdev, 0);
3169 ret = devm_request_irq(&pdev->dev, irq, coda_irq_handler, 0,
3170 CODA_NAME "-video", dev);
3172 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3177 if (dev->devtype->product == CODA_960) {
3178 irq = platform_get_irq_byname(pdev, "jpeg");
3182 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
3183 coda9_jpeg_irq_handler,
3184 IRQF_ONESHOT, CODA_NAME "-jpeg",
3187 dev_err(&pdev->dev, "failed to request jpeg irq\n");
3192 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
3194 if (IS_ERR(dev->rstc)) {
3195 ret = PTR_ERR(dev->rstc);
3196 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3200 /* Get IRAM pool from device tree */
3201 pool = of_gen_pool_get(np, "iram", 0);
3203 dev_err(&pdev->dev, "iram pool not available\n");
3206 dev->iram_pool = pool;
3208 /* Get vdoa_data if supported by the platform */
3209 dev->vdoa = coda_get_vdoa_data();
3210 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
3211 return -EPROBE_DEFER;
3213 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3217 ratelimit_default_init(&dev->mb_err_rs);
3218 mutex_init(&dev->dev_mutex);
3219 mutex_init(&dev->coda_mutex);
3220 ida_init(&dev->ida);
3222 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3224 /* allocate auxiliary per-device buffers for the BIT processor */
3225 if (dev->devtype->product == CODA_DX6) {
3226 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3227 dev->devtype->workbuf_size, "workbuf",
3230 goto err_v4l2_register;
3233 if (dev->devtype->tempbuf_size) {
3234 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3235 dev->devtype->tempbuf_size, "tempbuf",
3238 goto err_v4l2_register;
3241 dev->iram.size = dev->devtype->iram_size;
3242 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3244 if (!dev->iram.vaddr) {
3245 dev_warn(&pdev->dev, "unable to alloc iram\n");
3247 memset(dev->iram.vaddr, 0, dev->iram.size);
3248 dev->iram.blob.data = dev->iram.vaddr;
3249 dev->iram.blob.size = dev->iram.size;
3250 dev->iram.dentry = debugfs_create_blob("iram", 0444,
3255 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3256 if (!dev->workqueue) {
3257 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3259 goto err_v4l2_register;
3262 platform_set_drvdata(pdev, dev);
3265 * Start activated so we can directly call coda_hw_init in
3266 * coda_fw_callback regardless of whether CONFIG_PM is
3267 * enabled or whether the device is associated with a PM domain.
3269 pm_runtime_get_noresume(&pdev->dev);
3270 pm_runtime_set_active(&pdev->dev);
3271 pm_runtime_enable(&pdev->dev);
3273 ret = coda_firmware_request(dev);
3275 goto err_alloc_workqueue;
3278 err_alloc_workqueue:
3279 pm_runtime_disable(&pdev->dev);
3280 pm_runtime_put_noidle(&pdev->dev);
3281 destroy_workqueue(dev->workqueue);
3283 v4l2_device_unregister(&dev->v4l2_dev);
3287 static int coda_remove(struct platform_device *pdev)
3289 struct coda_dev *dev = platform_get_drvdata(pdev);
3292 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
3293 if (video_get_drvdata(&dev->vfd[i]))
3294 video_unregister_device(&dev->vfd[i]);
3297 v4l2_m2m_release(dev->m2m_dev);
3298 pm_runtime_disable(&pdev->dev);
3299 v4l2_device_unregister(&dev->v4l2_dev);
3300 destroy_workqueue(dev->workqueue);
3301 if (dev->iram.vaddr)
3302 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3304 coda_free_aux_buf(dev, &dev->codebuf);
3305 coda_free_aux_buf(dev, &dev->tempbuf);
3306 coda_free_aux_buf(dev, &dev->workbuf);
3307 debugfs_remove_recursive(dev->debugfs_root);
3308 ida_destroy(&dev->ida);
3313 static int coda_runtime_resume(struct device *dev)
3315 struct coda_dev *cdev = dev_get_drvdata(dev);
3318 if (dev->pm_domain && cdev->codebuf.vaddr) {
3319 ret = coda_hw_init(cdev);
3321 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3328 static const struct dev_pm_ops coda_pm_ops = {
3329 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3332 static struct platform_driver coda_driver = {
3333 .probe = coda_probe,
3334 .remove = coda_remove,
3337 .of_match_table = coda_dt_ids,
3342 module_platform_driver(coda_driver);
3344 MODULE_LICENSE("GPL");
3345 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3346 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");