Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Coda multi-standard codec IP
0004  *
0005  * Copyright (C) 2012 Vista Silicon S.L.
0006  *    Javier Martin, <javier.martin@vista-silicon.com>
0007  *    Xavier Duret
0008  */
0009 
0010 #include <linux/clk.h>
0011 #include <linux/debugfs.h>
0012 #include <linux/delay.h>
0013 #include <linux/firmware.h>
0014 #include <linux/gcd.h>
0015 #include <linux/genalloc.h>
0016 #include <linux/idr.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/io.h>
0019 #include <linux/irq.h>
0020 #include <linux/kfifo.h>
0021 #include <linux/module.h>
0022 #include <linux/of_device.h>
0023 #include <linux/platform_device.h>
0024 #include <linux/pm_runtime.h>
0025 #include <linux/slab.h>
0026 #include <linux/videodev2.h>
0027 #include <linux/of.h>
0028 #include <linux/ratelimit.h>
0029 #include <linux/reset.h>
0030 
0031 #include <media/v4l2-ctrls.h>
0032 #include <media/v4l2-device.h>
0033 #include <media/v4l2-event.h>
0034 #include <media/v4l2-ioctl.h>
0035 #include <media/v4l2-mem2mem.h>
0036 #include <media/videobuf2-v4l2.h>
0037 #include <media/videobuf2-dma-contig.h>
0038 #include <media/videobuf2-vmalloc.h>
0039 
0040 #include "coda.h"
0041 #include "imx-vdoa.h"
0042 
0043 #define CODA_NAME       "coda"
0044 
0045 #define CODADX6_MAX_INSTANCES   4
0046 #define CODA_MAX_FORMATS    5
0047 
0048 #define CODA_ISRAM_SIZE (2048 * 2)
0049 
0050 #define MIN_W 48
0051 #define MIN_H 16
0052 
0053 #define S_ALIGN     1 /* multiple of 2 */
0054 #define W_ALIGN     1 /* multiple of 2 */
0055 #define H_ALIGN     1 /* multiple of 2 */
0056 
0057 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
0058 
0059 int coda_debug;
0060 module_param(coda_debug, int, 0644);
0061 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
0062 
0063 static int disable_tiling;
0064 module_param(disable_tiling, int, 0644);
0065 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
0066 
0067 static int disable_vdoa;
0068 module_param(disable_vdoa, int, 0644);
0069 MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
0070 
0071 static int enable_bwb = 0;
0072 module_param(enable_bwb, int, 0644);
0073 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
0074 
0075 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
0076 {
0077     v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
0078          "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
0079     writel(data, dev->regs_base + reg);
0080 }
0081 
0082 unsigned int coda_read(struct coda_dev *dev, u32 reg)
0083 {
0084     u32 data;
0085 
0086     data = readl(dev->regs_base + reg);
0087     v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
0088          "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
0089     return data;
0090 }
0091 
0092 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
0093              struct vb2_v4l2_buffer *buf, unsigned int reg_y)
0094 {
0095     u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
0096     u32 base_cb, base_cr;
0097 
0098     switch (q_data->fourcc) {
0099     case V4L2_PIX_FMT_YUYV:
0100         /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
0101     case V4L2_PIX_FMT_NV12:
0102     case V4L2_PIX_FMT_YUV420:
0103     default:
0104         base_cb = base_y + q_data->bytesperline * q_data->height;
0105         base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
0106         break;
0107     case V4L2_PIX_FMT_YVU420:
0108         /* Switch Cb and Cr for YVU420 format */
0109         base_cr = base_y + q_data->bytesperline * q_data->height;
0110         base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
0111         break;
0112     case V4L2_PIX_FMT_YUV422P:
0113         base_cb = base_y + q_data->bytesperline * q_data->height;
0114         base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
0115     }
0116 
0117     coda_write(ctx->dev, base_y, reg_y);
0118     coda_write(ctx->dev, base_cb, reg_y + 4);
0119     coda_write(ctx->dev, base_cr, reg_y + 8);
0120 }
0121 
0122 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
0123     { mode, src_fourcc, dst_fourcc, max_w, max_h }
0124 
0125 /*
0126  * Arrays of codecs supported by each given version of Coda:
0127  *  i.MX27 -> codadx6
0128  *  i.MX51 -> codahx4
0129  *  i.MX53 -> coda7
0130  *  i.MX6  -> coda960
0131  * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
0132  */
0133 static const struct coda_codec codadx6_codecs[] = {
0134     CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,  720, 576),
0135     CODA_CODEC(CODADX6_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
0136 };
0137 
0138 static const struct coda_codec codahx4_codecs[] = {
0139     CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   720, 576),
0140     CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
0141     CODA_CODEC(CODA7_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
0142     CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1280, 720),
0143 };
0144 
0145 static const struct coda_codec coda7_codecs[] = {
0146     CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1280, 720),
0147     CODA_CODEC(CODA7_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1280, 720),
0148     CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
0149     CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
0150     CODA_CODEC(CODA7_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
0151     CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
0152     CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,   V4L2_PIX_FMT_YUV420, 8192, 8192),
0153 };
0154 
0155 static const struct coda_codec coda9_codecs[] = {
0156     CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1920, 1088),
0157     CODA_CODEC(CODA9_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1920, 1088),
0158     CODA_CODEC(CODA9_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
0159     CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
0160     CODA_CODEC(CODA9_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
0161     CODA_CODEC(CODA9_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
0162     CODA_CODEC(CODA9_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,   V4L2_PIX_FMT_YUV420, 8192, 8192),
0163 };
0164 
0165 struct coda_video_device {
0166     const char *name;
0167     enum coda_inst_type type;
0168     const struct coda_context_ops *ops;
0169     bool direct;
0170     u32 src_formats[CODA_MAX_FORMATS];
0171     u32 dst_formats[CODA_MAX_FORMATS];
0172 };
0173 
0174 static const struct coda_video_device coda_bit_encoder = {
0175     .name = "coda-video-encoder",
0176     .type = CODA_INST_ENCODER,
0177     .ops = &coda_bit_encode_ops,
0178     .src_formats = {
0179         V4L2_PIX_FMT_NV12,
0180         V4L2_PIX_FMT_YUV420,
0181         V4L2_PIX_FMT_YVU420,
0182     },
0183     .dst_formats = {
0184         V4L2_PIX_FMT_H264,
0185         V4L2_PIX_FMT_MPEG4,
0186     },
0187 };
0188 
0189 static const struct coda_video_device coda_bit_jpeg_encoder = {
0190     .name = "coda-jpeg-encoder",
0191     .type = CODA_INST_ENCODER,
0192     .ops = &coda_bit_encode_ops,
0193     .src_formats = {
0194         V4L2_PIX_FMT_NV12,
0195         V4L2_PIX_FMT_YUV420,
0196         V4L2_PIX_FMT_YVU420,
0197         V4L2_PIX_FMT_YUV422P,
0198     },
0199     .dst_formats = {
0200         V4L2_PIX_FMT_JPEG,
0201     },
0202 };
0203 
0204 static const struct coda_video_device coda_bit_decoder = {
0205     .name = "coda-video-decoder",
0206     .type = CODA_INST_DECODER,
0207     .ops = &coda_bit_decode_ops,
0208     .src_formats = {
0209         V4L2_PIX_FMT_H264,
0210         V4L2_PIX_FMT_MPEG2,
0211         V4L2_PIX_FMT_MPEG4,
0212     },
0213     .dst_formats = {
0214         V4L2_PIX_FMT_NV12,
0215         V4L2_PIX_FMT_YUV420,
0216         V4L2_PIX_FMT_YVU420,
0217         /*
0218          * If V4L2_PIX_FMT_YUYV should be default,
0219          * set_default_params() must be adjusted.
0220          */
0221         V4L2_PIX_FMT_YUYV,
0222     },
0223 };
0224 
0225 static const struct coda_video_device coda_bit_jpeg_decoder = {
0226     .name = "coda-jpeg-decoder",
0227     .type = CODA_INST_DECODER,
0228     .ops = &coda_bit_decode_ops,
0229     .src_formats = {
0230         V4L2_PIX_FMT_JPEG,
0231     },
0232     .dst_formats = {
0233         V4L2_PIX_FMT_NV12,
0234         V4L2_PIX_FMT_YUV420,
0235         V4L2_PIX_FMT_YVU420,
0236         V4L2_PIX_FMT_YUV422P,
0237     },
0238 };
0239 
0240 static const struct coda_video_device coda9_jpeg_encoder = {
0241     .name = "coda-jpeg-encoder",
0242     .type = CODA_INST_ENCODER,
0243     .ops = &coda9_jpeg_encode_ops,
0244     .direct = true,
0245     .src_formats = {
0246         V4L2_PIX_FMT_NV12,
0247         V4L2_PIX_FMT_YUV420,
0248         V4L2_PIX_FMT_YVU420,
0249         V4L2_PIX_FMT_YUV422P,
0250         V4L2_PIX_FMT_GREY,
0251     },
0252     .dst_formats = {
0253         V4L2_PIX_FMT_JPEG,
0254     },
0255 };
0256 
0257 static const struct coda_video_device coda9_jpeg_decoder = {
0258     .name = "coda-jpeg-decoder",
0259     .type = CODA_INST_DECODER,
0260     .ops = &coda9_jpeg_decode_ops,
0261     .direct = true,
0262     .src_formats = {
0263         V4L2_PIX_FMT_JPEG,
0264     },
0265     .dst_formats = {
0266         V4L2_PIX_FMT_NV12,
0267         V4L2_PIX_FMT_YUV420,
0268         V4L2_PIX_FMT_YVU420,
0269         V4L2_PIX_FMT_YUV422P,
0270     },
0271 };
0272 
0273 static const struct coda_video_device *codadx6_video_devices[] = {
0274     &coda_bit_encoder,
0275 };
0276 
0277 static const struct coda_video_device *codahx4_video_devices[] = {
0278     &coda_bit_encoder,
0279     &coda_bit_decoder,
0280 };
0281 
0282 static const struct coda_video_device *coda7_video_devices[] = {
0283     &coda_bit_jpeg_encoder,
0284     &coda_bit_jpeg_decoder,
0285     &coda_bit_encoder,
0286     &coda_bit_decoder,
0287 };
0288 
0289 static const struct coda_video_device *coda9_video_devices[] = {
0290     &coda9_jpeg_encoder,
0291     &coda9_jpeg_decoder,
0292     &coda_bit_encoder,
0293     &coda_bit_decoder,
0294 };
0295 
0296 /*
0297  * Normalize all supported YUV 4:2:0 formats to the value used in the codec
0298  * tables.
0299  */
0300 static u32 coda_format_normalize_yuv(u32 fourcc)
0301 {
0302     switch (fourcc) {
0303     case V4L2_PIX_FMT_NV12:
0304     case V4L2_PIX_FMT_YUV420:
0305     case V4L2_PIX_FMT_YVU420:
0306     case V4L2_PIX_FMT_YUV422P:
0307     case V4L2_PIX_FMT_YUYV:
0308         return V4L2_PIX_FMT_YUV420;
0309     default:
0310         return fourcc;
0311     }
0312 }
0313 
0314 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
0315                         int src_fourcc, int dst_fourcc)
0316 {
0317     const struct coda_codec *codecs = dev->devtype->codecs;
0318     int num_codecs = dev->devtype->num_codecs;
0319     int k;
0320 
0321     src_fourcc = coda_format_normalize_yuv(src_fourcc);
0322     dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
0323     if (src_fourcc == dst_fourcc)
0324         return NULL;
0325 
0326     for (k = 0; k < num_codecs; k++) {
0327         if (codecs[k].src_fourcc == src_fourcc &&
0328             codecs[k].dst_fourcc == dst_fourcc)
0329             break;
0330     }
0331 
0332     if (k == num_codecs)
0333         return NULL;
0334 
0335     return &codecs[k];
0336 }
0337 
0338 static void coda_get_max_dimensions(struct coda_dev *dev,
0339                     const struct coda_codec *codec,
0340                     int *max_w, int *max_h)
0341 {
0342     const struct coda_codec *codecs = dev->devtype->codecs;
0343     int num_codecs = dev->devtype->num_codecs;
0344     unsigned int w, h;
0345     int k;
0346 
0347     if (codec) {
0348         w = codec->max_w;
0349         h = codec->max_h;
0350     } else {
0351         for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
0352             w = max(w, codecs[k].max_w);
0353             h = max(h, codecs[k].max_h);
0354         }
0355     }
0356 
0357     if (max_w)
0358         *max_w = w;
0359     if (max_h)
0360         *max_h = h;
0361 }
0362 
0363 static const struct coda_video_device *to_coda_video_device(struct video_device
0364                                 *vdev)
0365 {
0366     struct coda_dev *dev = video_get_drvdata(vdev);
0367     unsigned int i = vdev - dev->vfd;
0368 
0369     if (i >= dev->devtype->num_vdevs)
0370         return NULL;
0371 
0372     return dev->devtype->vdevs[i];
0373 }
0374 
0375 const char *coda_product_name(int product)
0376 {
0377     static char buf[9];
0378 
0379     switch (product) {
0380     case CODA_DX6:
0381         return "CodaDx6";
0382     case CODA_HX4:
0383         return "CodaHx4";
0384     case CODA_7541:
0385         return "CODA7541";
0386     case CODA_960:
0387         return "CODA960";
0388     default:
0389         snprintf(buf, sizeof(buf), "(0x%04x)", product);
0390         return buf;
0391     }
0392 }
0393 
0394 static struct vdoa_data *coda_get_vdoa_data(void)
0395 {
0396     struct device_node *vdoa_node;
0397     struct platform_device *vdoa_pdev;
0398     struct vdoa_data *vdoa_data = NULL;
0399 
0400     vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
0401     if (!vdoa_node)
0402         return NULL;
0403 
0404     vdoa_pdev = of_find_device_by_node(vdoa_node);
0405     if (!vdoa_pdev)
0406         goto out;
0407 
0408     vdoa_data = platform_get_drvdata(vdoa_pdev);
0409     if (!vdoa_data)
0410         vdoa_data = ERR_PTR(-EPROBE_DEFER);
0411 
0412     put_device(&vdoa_pdev->dev);
0413 out:
0414     of_node_put(vdoa_node);
0415 
0416     return vdoa_data;
0417 }
0418 
0419 /*
0420  * V4L2 ioctl() operations.
0421  */
0422 static int coda_querycap(struct file *file, void *priv,
0423              struct v4l2_capability *cap)
0424 {
0425     struct coda_ctx *ctx = fh_to_ctx(priv);
0426 
0427     strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
0428     strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
0429         sizeof(cap->card));
0430     strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
0431     return 0;
0432 }
0433 
0434 static const u32 coda_formats_420[CODA_MAX_FORMATS] = {
0435         V4L2_PIX_FMT_NV12,
0436         V4L2_PIX_FMT_YUV420,
0437         V4L2_PIX_FMT_YVU420,
0438 };
0439 
0440 static int coda_enum_fmt(struct file *file, void *priv,
0441              struct v4l2_fmtdesc *f)
0442 {
0443     struct video_device *vdev = video_devdata(file);
0444     const struct coda_video_device *cvd = to_coda_video_device(vdev);
0445     struct coda_ctx *ctx = fh_to_ctx(priv);
0446     const u32 *formats;
0447 
0448     if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
0449         formats = cvd->src_formats;
0450     else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
0451         struct coda_q_data *q_data_src;
0452         struct vb2_queue *src_vq;
0453 
0454         formats = cvd->dst_formats;
0455 
0456         /*
0457          * If the source format is already fixed, only allow the same
0458          * chroma subsampling.
0459          */
0460         q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
0461         src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
0462                      V4L2_BUF_TYPE_VIDEO_OUTPUT);
0463         if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
0464             vb2_is_streaming(src_vq)) {
0465             if (ctx->params.jpeg_chroma_subsampling ==
0466                 V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
0467                 formats = coda_formats_420;
0468             } else if (ctx->params.jpeg_chroma_subsampling ==
0469                    V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
0470                 f->pixelformat = V4L2_PIX_FMT_YUV422P;
0471                 return f->index ? -EINVAL : 0;
0472             }
0473         }
0474     } else {
0475         return -EINVAL;
0476     }
0477 
0478     if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
0479         return -EINVAL;
0480 
0481     /* Skip YUYV if the vdoa is not available */
0482     if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
0483         formats[f->index] == V4L2_PIX_FMT_YUYV)
0484         return -EINVAL;
0485 
0486     f->pixelformat = formats[f->index];
0487 
0488     return 0;
0489 }
0490 
0491 static int coda_g_fmt(struct file *file, void *priv,
0492               struct v4l2_format *f)
0493 {
0494     struct coda_q_data *q_data;
0495     struct coda_ctx *ctx = fh_to_ctx(priv);
0496 
0497     q_data = get_q_data(ctx, f->type);
0498     if (!q_data)
0499         return -EINVAL;
0500 
0501     f->fmt.pix.field    = V4L2_FIELD_NONE;
0502     f->fmt.pix.pixelformat  = q_data->fourcc;
0503     f->fmt.pix.width    = q_data->width;
0504     f->fmt.pix.height   = q_data->height;
0505     f->fmt.pix.bytesperline = q_data->bytesperline;
0506 
0507     f->fmt.pix.sizeimage    = q_data->sizeimage;
0508     f->fmt.pix.colorspace   = ctx->colorspace;
0509     f->fmt.pix.xfer_func    = ctx->xfer_func;
0510     f->fmt.pix.ycbcr_enc    = ctx->ycbcr_enc;
0511     f->fmt.pix.quantization = ctx->quantization;
0512 
0513     return 0;
0514 }
0515 
0516 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
0517 {
0518     struct coda_q_data *q_data;
0519     const u32 *formats;
0520     int i;
0521 
0522     if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
0523         formats = ctx->cvd->src_formats;
0524     else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
0525         formats = ctx->cvd->dst_formats;
0526     else
0527         return -EINVAL;
0528 
0529     for (i = 0; i < CODA_MAX_FORMATS; i++) {
0530         /* Skip YUYV if the vdoa is not available */
0531         if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
0532             formats[i] == V4L2_PIX_FMT_YUYV)
0533             continue;
0534 
0535         if (formats[i] == f->fmt.pix.pixelformat) {
0536             f->fmt.pix.pixelformat = formats[i];
0537             return 0;
0538         }
0539     }
0540 
0541     /* Fall back to currently set pixelformat */
0542     q_data = get_q_data(ctx, f->type);
0543     f->fmt.pix.pixelformat = q_data->fourcc;
0544 
0545     return 0;
0546 }
0547 
0548 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
0549                  bool *use_vdoa)
0550 {
0551     int err;
0552 
0553     if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
0554         return -EINVAL;
0555 
0556     if (!use_vdoa)
0557         return -EINVAL;
0558 
0559     if (!ctx->vdoa) {
0560         *use_vdoa = false;
0561         return 0;
0562     }
0563 
0564     err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
0565                      f->fmt.pix.height, f->fmt.pix.pixelformat);
0566     if (err) {
0567         *use_vdoa = false;
0568         return 0;
0569     }
0570 
0571     *use_vdoa = true;
0572     return 0;
0573 }
0574 
0575 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
0576                         u32 width, u32 height)
0577 {
0578     /*
0579      * This is a rough estimate for sensible compressed buffer
0580      * sizes (between 1 and 16 bits per pixel). This could be
0581      * improved by better format specific worst case estimates.
0582      */
0583     return round_up(clamp(sizeimage, width * height / 8,
0584                      width * height * 2), PAGE_SIZE);
0585 }
0586 
0587 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
0588             struct v4l2_format *f)
0589 {
0590     struct coda_dev *dev = ctx->dev;
0591     unsigned int max_w, max_h;
0592     enum v4l2_field field;
0593 
0594     field = f->fmt.pix.field;
0595     if (field == V4L2_FIELD_ANY)
0596         field = V4L2_FIELD_NONE;
0597     else if (V4L2_FIELD_NONE != field)
0598         return -EINVAL;
0599 
0600     /* V4L2 specification suggests the driver corrects the format struct
0601      * if any of the dimensions is unsupported */
0602     f->fmt.pix.field = field;
0603 
0604     coda_get_max_dimensions(dev, codec, &max_w, &max_h);
0605     v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
0606                   &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
0607                   S_ALIGN);
0608 
0609     switch (f->fmt.pix.pixelformat) {
0610     case V4L2_PIX_FMT_NV12:
0611     case V4L2_PIX_FMT_YUV420:
0612     case V4L2_PIX_FMT_YVU420:
0613         /*
0614          * Frame stride must be at least multiple of 8,
0615          * but multiple of 16 for h.264 or JPEG 4:2:x
0616          */
0617         f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
0618         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
0619                     f->fmt.pix.height * 3 / 2;
0620         break;
0621     case V4L2_PIX_FMT_YUYV:
0622         f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
0623         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
0624                     f->fmt.pix.height;
0625         break;
0626     case V4L2_PIX_FMT_YUV422P:
0627         f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
0628         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
0629                     f->fmt.pix.height * 2;
0630         break;
0631     case V4L2_PIX_FMT_GREY:
0632         /* keep 16 pixel alignment of 8-bit pixel data */
0633         f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
0634         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
0635         break;
0636     case V4L2_PIX_FMT_JPEG:
0637     case V4L2_PIX_FMT_H264:
0638     case V4L2_PIX_FMT_MPEG4:
0639     case V4L2_PIX_FMT_MPEG2:
0640         f->fmt.pix.bytesperline = 0;
0641         f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
0642                             f->fmt.pix.sizeimage,
0643                             f->fmt.pix.width,
0644                             f->fmt.pix.height);
0645         break;
0646     default:
0647         BUG();
0648     }
0649 
0650     return 0;
0651 }
0652 
0653 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
0654                 struct v4l2_format *f)
0655 {
0656     struct coda_ctx *ctx = fh_to_ctx(priv);
0657     const struct coda_q_data *q_data_src;
0658     const struct coda_codec *codec;
0659     struct vb2_queue *src_vq;
0660     int hscale = 0;
0661     int vscale = 0;
0662     int ret;
0663     bool use_vdoa;
0664 
0665     ret = coda_try_pixelformat(ctx, f);
0666     if (ret < 0)
0667         return ret;
0668 
0669     q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
0670 
0671     /*
0672      * If the source format is already fixed, only allow the same output
0673      * resolution. When decoding JPEG images, we also have to make sure to
0674      * use the same chroma subsampling.
0675      */
0676     src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
0677     if (vb2_is_streaming(src_vq)) {
0678         if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
0679             ctx->dev->devtype->product == CODA_960) {
0680             hscale = coda_jpeg_scale(q_data_src->width, f->fmt.pix.width);
0681             vscale = coda_jpeg_scale(q_data_src->height, f->fmt.pix.height);
0682         }
0683         f->fmt.pix.width = q_data_src->width >> hscale;
0684         f->fmt.pix.height = q_data_src->height >> vscale;
0685 
0686         if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
0687             if (ctx->params.jpeg_chroma_subsampling ==
0688                 V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
0689                 f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P)
0690                 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
0691             else if (ctx->params.jpeg_chroma_subsampling ==
0692                  V4L2_JPEG_CHROMA_SUBSAMPLING_422)
0693                 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
0694         }
0695     }
0696 
0697     f->fmt.pix.colorspace = ctx->colorspace;
0698     f->fmt.pix.xfer_func = ctx->xfer_func;
0699     f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
0700     f->fmt.pix.quantization = ctx->quantization;
0701 
0702     q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
0703     codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
0704                 f->fmt.pix.pixelformat);
0705     if (!codec)
0706         return -EINVAL;
0707 
0708     ret = coda_try_fmt(ctx, codec, f);
0709     if (ret < 0)
0710         return ret;
0711 
0712     /* The decoders always write complete macroblocks or MCUs */
0713     if (ctx->inst_type == CODA_INST_DECODER) {
0714         f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16 >> hscale);
0715         f->fmt.pix.height = round_up(f->fmt.pix.height, 16 >> vscale);
0716         if (codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
0717             f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P) {
0718             f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
0719                            f->fmt.pix.height * 2;
0720         } else {
0721             f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
0722                            f->fmt.pix.height * 3 / 2;
0723         }
0724 
0725         ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
0726         if (ret < 0)
0727             return ret;
0728 
0729         if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
0730             if (!use_vdoa)
0731                 return -EINVAL;
0732 
0733             f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
0734             f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
0735                 f->fmt.pix.height;
0736         }
0737     }
0738 
0739     return 0;
0740 }
0741 
0742 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
0743 {
0744     enum v4l2_colorspace colorspace;
0745 
0746     if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
0747         colorspace = V4L2_COLORSPACE_JPEG;
0748     else if (fmt->width <= 720 && fmt->height <= 576)
0749         colorspace = V4L2_COLORSPACE_SMPTE170M;
0750     else
0751         colorspace = V4L2_COLORSPACE_REC709;
0752 
0753     fmt->colorspace = colorspace;
0754     fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
0755     fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
0756     fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
0757 }
0758 
0759 static int coda_try_fmt_vid_out(struct file *file, void *priv,
0760                 struct v4l2_format *f)
0761 {
0762     struct coda_ctx *ctx = fh_to_ctx(priv);
0763     struct coda_dev *dev = ctx->dev;
0764     const struct coda_q_data *q_data_dst;
0765     const struct coda_codec *codec;
0766     int ret;
0767 
0768     ret = coda_try_pixelformat(ctx, f);
0769     if (ret < 0)
0770         return ret;
0771 
0772     if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
0773         coda_set_default_colorspace(&f->fmt.pix);
0774 
0775     q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
0776     codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
0777 
0778     return coda_try_fmt(ctx, codec, f);
0779 }
0780 
0781 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
0782               struct v4l2_rect *r)
0783 {
0784     struct coda_q_data *q_data;
0785     struct vb2_queue *vq;
0786 
0787     vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
0788     if (!vq)
0789         return -EINVAL;
0790 
0791     q_data = get_q_data(ctx, f->type);
0792     if (!q_data)
0793         return -EINVAL;
0794 
0795     if (vb2_is_busy(vq)) {
0796         v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
0797              __func__, v4l2_type_names[f->type], vq->num_buffers);
0798         return -EBUSY;
0799     }
0800 
0801     q_data->fourcc = f->fmt.pix.pixelformat;
0802     q_data->width = f->fmt.pix.width;
0803     q_data->height = f->fmt.pix.height;
0804     q_data->bytesperline = f->fmt.pix.bytesperline;
0805     q_data->sizeimage = f->fmt.pix.sizeimage;
0806     if (r) {
0807         q_data->rect = *r;
0808     } else {
0809         q_data->rect.left = 0;
0810         q_data->rect.top = 0;
0811         q_data->rect.width = f->fmt.pix.width;
0812         q_data->rect.height = f->fmt.pix.height;
0813     }
0814 
0815     switch (f->fmt.pix.pixelformat) {
0816     case V4L2_PIX_FMT_YUYV:
0817         ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
0818         break;
0819     case V4L2_PIX_FMT_NV12:
0820         if (!disable_tiling && ctx->use_bit &&
0821             ctx->dev->devtype->product == CODA_960) {
0822             ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
0823             break;
0824         }
0825         fallthrough;
0826     case V4L2_PIX_FMT_YUV420:
0827     case V4L2_PIX_FMT_YVU420:
0828     case V4L2_PIX_FMT_YUV422P:
0829         ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
0830         break;
0831     default:
0832         break;
0833     }
0834 
0835     if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
0836         !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
0837         ctx->use_vdoa)
0838         vdoa_context_configure(ctx->vdoa,
0839                        round_up(f->fmt.pix.width, 16),
0840                        f->fmt.pix.height,
0841                        f->fmt.pix.pixelformat);
0842     else
0843         ctx->use_vdoa = false;
0844 
0845     coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
0846          v4l2_type_names[f->type], q_data->width, q_data->height,
0847          (char *)&q_data->fourcc,
0848          (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
0849 
0850     return 0;
0851 }
0852 
0853 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
0854                   struct v4l2_format *f)
0855 {
0856     struct coda_ctx *ctx = fh_to_ctx(priv);
0857     struct coda_q_data *q_data_src;
0858     const struct coda_codec *codec;
0859     struct v4l2_rect r;
0860     int hscale = 0;
0861     int vscale = 0;
0862     int ret;
0863 
0864     q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
0865 
0866     if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
0867         ctx->dev->devtype->product == CODA_960) {
0868         hscale = coda_jpeg_scale(q_data_src->width, f->fmt.pix.width);
0869         vscale = coda_jpeg_scale(q_data_src->height, f->fmt.pix.height);
0870     }
0871 
0872     ret = coda_try_fmt_vid_cap(file, priv, f);
0873     if (ret)
0874         return ret;
0875 
0876     r.left = 0;
0877     r.top = 0;
0878     r.width = q_data_src->width >> hscale;
0879     r.height = q_data_src->height >> vscale;
0880 
0881     ret = coda_s_fmt(ctx, f, &r);
0882     if (ret)
0883         return ret;
0884 
0885     if (ctx->inst_type != CODA_INST_ENCODER)
0886         return 0;
0887 
0888     /* Setting the coded format determines the selected codec */
0889     codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
0890                 f->fmt.pix.pixelformat);
0891     if (!codec) {
0892         v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
0893         return -EINVAL;
0894     }
0895     ctx->codec = codec;
0896 
0897     ctx->colorspace = f->fmt.pix.colorspace;
0898     ctx->xfer_func = f->fmt.pix.xfer_func;
0899     ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
0900     ctx->quantization = f->fmt.pix.quantization;
0901 
0902     return 0;
0903 }
0904 
0905 static int coda_s_fmt_vid_out(struct file *file, void *priv,
0906                   struct v4l2_format *f)
0907 {
0908     struct coda_ctx *ctx = fh_to_ctx(priv);
0909     const struct coda_codec *codec;
0910     struct v4l2_format f_cap;
0911     struct vb2_queue *dst_vq;
0912     int ret;
0913 
0914     ret = coda_try_fmt_vid_out(file, priv, f);
0915     if (ret)
0916         return ret;
0917 
0918     ret = coda_s_fmt(ctx, f, NULL);
0919     if (ret)
0920         return ret;
0921 
0922     ctx->colorspace = f->fmt.pix.colorspace;
0923     ctx->xfer_func = f->fmt.pix.xfer_func;
0924     ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
0925     ctx->quantization = f->fmt.pix.quantization;
0926 
0927     if (ctx->inst_type != CODA_INST_DECODER)
0928         return 0;
0929 
0930     /* Setting the coded format determines the selected codec */
0931     codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
0932                 V4L2_PIX_FMT_YUV420);
0933     if (!codec) {
0934         v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
0935         return -EINVAL;
0936     }
0937     ctx->codec = codec;
0938 
0939     dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
0940     if (!dst_vq)
0941         return -EINVAL;
0942 
0943     /*
0944      * Setting the capture queue format is not possible while the capture
0945      * queue is still busy. This is not an error, but the user will have to
0946      * make sure themselves that the capture format is set correctly before
0947      * starting the output queue again.
0948      */
0949     if (vb2_is_busy(dst_vq))
0950         return 0;
0951 
0952     memset(&f_cap, 0, sizeof(f_cap));
0953     f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
0954     coda_g_fmt(file, priv, &f_cap);
0955     f_cap.fmt.pix.width = f->fmt.pix.width;
0956     f_cap.fmt.pix.height = f->fmt.pix.height;
0957 
0958     return coda_s_fmt_vid_cap(file, priv, &f_cap);
0959 }
0960 
0961 static int coda_reqbufs(struct file *file, void *priv,
0962             struct v4l2_requestbuffers *rb)
0963 {
0964     struct coda_ctx *ctx = fh_to_ctx(priv);
0965     int ret;
0966 
0967     ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
0968     if (ret)
0969         return ret;
0970 
0971     /*
0972      * Allow to allocate instance specific per-context buffers, such as
0973      * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
0974      */
0975     if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
0976         return ctx->ops->reqbufs(ctx, rb);
0977 
0978     return 0;
0979 }
0980 
0981 static int coda_qbuf(struct file *file, void *priv,
0982              struct v4l2_buffer *buf)
0983 {
0984     struct coda_ctx *ctx = fh_to_ctx(priv);
0985 
0986     if (ctx->inst_type == CODA_INST_DECODER &&
0987         buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
0988         buf->flags &= ~V4L2_BUF_FLAG_LAST;
0989 
0990     return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
0991 }
0992 
0993 static int coda_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
0994 {
0995     struct coda_ctx *ctx = fh_to_ctx(priv);
0996     int ret;
0997 
0998     ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
0999 
1000     if (ctx->inst_type == CODA_INST_DECODER &&
1001         buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1002         buf->flags &= ~V4L2_BUF_FLAG_LAST;
1003 
1004     return ret;
1005 }
1006 
1007 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
1008                enum vb2_buffer_state state)
1009 {
1010     const struct v4l2_event eos_event = {
1011         .type = V4L2_EVENT_EOS
1012     };
1013 
1014     if (buf->flags & V4L2_BUF_FLAG_LAST)
1015         v4l2_event_queue_fh(&ctx->fh, &eos_event);
1016 
1017     v4l2_m2m_buf_done(buf, state);
1018 }
1019 
1020 static int coda_g_selection(struct file *file, void *fh,
1021                 struct v4l2_selection *s)
1022 {
1023     struct coda_ctx *ctx = fh_to_ctx(fh);
1024     struct coda_q_data *q_data;
1025     struct v4l2_rect r, *rsel;
1026 
1027     q_data = get_q_data(ctx, s->type);
1028     if (!q_data)
1029         return -EINVAL;
1030 
1031     r.left = 0;
1032     r.top = 0;
1033     r.width = q_data->width;
1034     r.height = q_data->height;
1035     rsel = &q_data->rect;
1036 
1037     switch (s->target) {
1038     case V4L2_SEL_TGT_CROP_DEFAULT:
1039     case V4L2_SEL_TGT_CROP_BOUNDS:
1040         rsel = &r;
1041         fallthrough;
1042     case V4L2_SEL_TGT_CROP:
1043         if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
1044             ctx->inst_type == CODA_INST_DECODER)
1045             return -EINVAL;
1046         break;
1047     case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1048     case V4L2_SEL_TGT_COMPOSE_PADDED:
1049         rsel = &r;
1050         fallthrough;
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)
1055             return -EINVAL;
1056         break;
1057     default:
1058         return -EINVAL;
1059     }
1060 
1061     s->r = *rsel;
1062 
1063     return 0;
1064 }
1065 
1066 static int coda_s_selection(struct file *file, void *fh,
1067                 struct v4l2_selection *s)
1068 {
1069     struct coda_ctx *ctx = fh_to_ctx(fh);
1070     struct coda_q_data *q_data;
1071 
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);
1077             if (!q_data)
1078                 return -EINVAL;
1079 
1080             s->r.left = 0;
1081             s->r.top = 0;
1082             s->r.width = clamp(s->r.width, 2U, q_data->width);
1083             s->r.height = clamp(s->r.height, 2U, q_data->height);
1084 
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);
1088             } else {
1089                 s->r.width = round_down(s->r.width, 2);
1090                 s->r.height = round_down(s->r.height, 2);
1091             }
1092 
1093             q_data->rect = s->r;
1094 
1095             coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
1096                  s->r.width, s->r.height);
1097 
1098             return 0;
1099         }
1100         fallthrough;
1101     case V4L2_SEL_TGT_NATIVE_SIZE:
1102     case V4L2_SEL_TGT_COMPOSE:
1103         return coda_g_selection(file, fh, s);
1104     default:
1105         /* v4l2-compliance expects this to fail for read-only targets */
1106         return -EINVAL;
1107     }
1108 }
1109 
1110 static void coda_wake_up_capture_queue(struct coda_ctx *ctx)
1111 {
1112     struct vb2_queue *dst_vq;
1113 
1114     coda_dbg(1, ctx, "waking up capture queue\n");
1115 
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);
1119 }
1120 
1121 static int coda_encoder_cmd(struct file *file, void *fh,
1122                 struct v4l2_encoder_cmd *ec)
1123 {
1124     struct coda_ctx *ctx = fh_to_ctx(fh);
1125     struct vb2_v4l2_buffer *buf;
1126     int ret;
1127 
1128     ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, ec);
1129     if (ret < 0)
1130         return ret;
1131 
1132     mutex_lock(&ctx->wakeup_mutex);
1133     buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1134     if (buf) {
1135         /*
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
1138          * to userspace.
1139          */
1140         buf->flags |= V4L2_BUF_FLAG_LAST;
1141     } else {
1142         /* Set the stream-end flag on this context */
1143         ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1144 
1145         /*
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.
1149          */
1150         coda_wake_up_capture_queue(ctx);
1151     }
1152     mutex_unlock(&ctx->wakeup_mutex);
1153 
1154     return 0;
1155 }
1156 
1157 static bool coda_mark_last_meta(struct coda_ctx *ctx)
1158 {
1159     struct coda_buffer_meta *meta;
1160 
1161     coda_dbg(1, ctx, "marking last meta\n");
1162 
1163     spin_lock(&ctx->buffer_meta_lock);
1164     if (list_empty(&ctx->buffer_meta_list)) {
1165         spin_unlock(&ctx->buffer_meta_lock);
1166         return false;
1167     }
1168 
1169     meta = list_last_entry(&ctx->buffer_meta_list, struct coda_buffer_meta,
1170                    list);
1171     meta->last = true;
1172 
1173     spin_unlock(&ctx->buffer_meta_lock);
1174     return true;
1175 }
1176 
1177 static bool coda_mark_last_dst_buf(struct coda_ctx *ctx)
1178 {
1179     struct vb2_v4l2_buffer *buf;
1180     struct vb2_buffer *dst_vb;
1181     struct vb2_queue *dst_vq;
1182     unsigned long flags;
1183 
1184     coda_dbg(1, ctx, "marking last capture buffer\n");
1185 
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);
1190         return false;
1191     }
1192 
1193     dst_vb = list_last_entry(&dst_vq->done_list, struct vb2_buffer,
1194                  done_entry);
1195     buf = to_vb2_v4l2_buffer(dst_vb);
1196     buf->flags |= V4L2_BUF_FLAG_LAST;
1197 
1198     spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1199     return true;
1200 }
1201 
1202 static int coda_decoder_cmd(struct file *file, void *fh,
1203                 struct v4l2_decoder_cmd *dc)
1204 {
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;
1209     bool stream_end;
1210     bool wakeup;
1211     int ret;
1212 
1213     ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
1214     if (ret < 0)
1215         return ret;
1216 
1217     switch (dc->cmd) {
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);
1229         break;
1230     case V4L2_DEC_CMD_STOP:
1231         stream_end = false;
1232         wakeup = false;
1233 
1234         mutex_lock(&ctx->wakeup_mutex);
1235 
1236         buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1237         if (buf) {
1238             coda_dbg(1, ctx, "marking last pending buffer\n");
1239 
1240             /* Mark last buffer */
1241             buf->flags |= V4L2_BUF_FLAG_LAST;
1242 
1243             if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) == 0) {
1244                 coda_dbg(1, ctx, "all remaining buffers queued\n");
1245                 stream_end = true;
1246             }
1247         } else {
1248             if (ctx->use_bit)
1249                 if (coda_mark_last_meta(ctx))
1250                     stream_end = true;
1251                 else
1252                     wakeup = true;
1253             else
1254                 if (!coda_mark_last_dst_buf(ctx))
1255                     wakeup = true;
1256         }
1257 
1258         if (stream_end) {
1259             coda_dbg(1, ctx, "all remaining buffers queued\n");
1260 
1261             /* Set the stream-end flag on this context */
1262             coda_bit_stream_end_flag(ctx);
1263             ctx->hold = false;
1264             v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1265         }
1266 
1267         if (wakeup) {
1268             /* If there is no buffer in flight, wake up */
1269             coda_wake_up_capture_queue(ctx);
1270         }
1271 
1272         mutex_unlock(&ctx->wakeup_mutex);
1273         break;
1274     default:
1275         return -EINVAL;
1276     }
1277 
1278     return 0;
1279 }
1280 
1281 static int coda_enum_framesizes(struct file *file, void *fh,
1282                 struct v4l2_frmsizeenum *fsize)
1283 {
1284     struct coda_ctx *ctx = fh_to_ctx(fh);
1285     struct coda_q_data *q_data_dst;
1286     const struct coda_codec *codec;
1287 
1288     if (fsize->index)
1289         return -EINVAL;
1290 
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);
1296     } else {
1297         codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1298                     fsize->pixel_format);
1299     }
1300     if (!codec)
1301         return -EINVAL;
1302 
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;
1310 
1311     return 0;
1312 }
1313 
1314 static int coda_enum_frameintervals(struct file *file, void *fh,
1315                     struct v4l2_frmivalenum *f)
1316 {
1317     struct coda_ctx *ctx = fh_to_ctx(fh);
1318     struct coda_q_data *q_data;
1319     const struct coda_codec *codec;
1320 
1321     if (f->index)
1322         return -EINVAL;
1323 
1324     /* Disallow YUYV if the vdoa is not available */
1325     if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1326         return -EINVAL;
1327 
1328     if (coda_format_normalize_yuv(f->pixel_format) == V4L2_PIX_FMT_YUV420) {
1329         q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1330         codec = coda_find_codec(ctx->dev, f->pixel_format,
1331                     q_data->fourcc);
1332     } else {
1333         codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1334                     f->pixel_format);
1335     }
1336     if (!codec)
1337         return -EINVAL;
1338 
1339     if (f->width < MIN_W || f->width > codec->max_w ||
1340         f->height < MIN_H || f->height > codec->max_h)
1341         return -EINVAL;
1342 
1343     f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1344     f->stepwise.min.numerator = 1;
1345     f->stepwise.min.denominator = 65535;
1346     f->stepwise.max.numerator = 65536;
1347     f->stepwise.max.denominator = 1;
1348     f->stepwise.step.numerator = 1;
1349     f->stepwise.step.denominator = 1;
1350 
1351     return 0;
1352 }
1353 
1354 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1355 {
1356     struct coda_ctx *ctx = fh_to_ctx(fh);
1357     struct v4l2_fract *tpf;
1358 
1359     if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1360         return -EINVAL;
1361 
1362     a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1363     tpf = &a->parm.output.timeperframe;
1364     tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1365     tpf->numerator = 1 + (ctx->params.framerate >>
1366                   CODA_FRATE_DIV_OFFSET);
1367 
1368     return 0;
1369 }
1370 
1371 /*
1372  * Approximate timeperframe v4l2_fract with values that can be written
1373  * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1374  */
1375 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1376 {
1377     struct v4l2_fract s = *timeperframe;
1378     struct v4l2_fract f0;
1379     struct v4l2_fract f1 = { 1, 0 };
1380     struct v4l2_fract f2 = { 0, 1 };
1381     unsigned int i, div, s_denominator;
1382 
1383     /* Lower bound is 1/65535 */
1384     if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1385         timeperframe->numerator = 1;
1386         timeperframe->denominator = 65535;
1387         return;
1388     }
1389 
1390     /* Upper bound is 65536/1 */
1391     if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1392         timeperframe->numerator = 65536;
1393         timeperframe->denominator = 1;
1394         return;
1395     }
1396 
1397     /* Reduce fraction to lowest terms */
1398     div = gcd(s.numerator, s.denominator);
1399     if (div > 1) {
1400         s.numerator /= div;
1401         s.denominator /= div;
1402     }
1403 
1404     if (s.numerator <= 65536 && s.denominator < 65536) {
1405         *timeperframe = s;
1406         return;
1407     }
1408 
1409     /* Find successive convergents from continued fraction expansion */
1410     while (f2.numerator <= 65536 && f2.denominator < 65536) {
1411         f0 = f1;
1412         f1 = f2;
1413 
1414         /* Stop when f2 exactly equals timeperframe */
1415         if (s.numerator == 0)
1416             break;
1417 
1418         i = s.denominator / s.numerator;
1419 
1420         f2.numerator = f0.numerator + i * f1.numerator;
1421         f2.denominator = f0.denominator + i * f2.denominator;
1422 
1423         s_denominator = s.numerator;
1424         s.numerator = s.denominator % s.numerator;
1425         s.denominator = s_denominator;
1426     }
1427 
1428     *timeperframe = f1;
1429 }
1430 
1431 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1432 {
1433     return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1434         timeperframe->denominator;
1435 }
1436 
1437 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1438 {
1439     struct coda_ctx *ctx = fh_to_ctx(fh);
1440     struct v4l2_fract *tpf;
1441 
1442     if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1443         return -EINVAL;
1444 
1445     a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1446     tpf = &a->parm.output.timeperframe;
1447     coda_approximate_timeperframe(tpf);
1448     ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1449     ctx->params.framerate_changed = true;
1450 
1451     return 0;
1452 }
1453 
1454 static int coda_subscribe_event(struct v4l2_fh *fh,
1455                 const struct v4l2_event_subscription *sub)
1456 {
1457     struct coda_ctx *ctx = fh_to_ctx(fh);
1458 
1459     switch (sub->type) {
1460     case V4L2_EVENT_EOS:
1461         return v4l2_event_subscribe(fh, sub, 0, NULL);
1462     case V4L2_EVENT_SOURCE_CHANGE:
1463         if (ctx->inst_type == CODA_INST_DECODER)
1464             return v4l2_event_subscribe(fh, sub, 0, NULL);
1465         else
1466             return -EINVAL;
1467     default:
1468         return v4l2_ctrl_subscribe_event(fh, sub);
1469     }
1470 }
1471 
1472 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1473     .vidioc_querycap    = coda_querycap,
1474 
1475     .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1476     .vidioc_g_fmt_vid_cap   = coda_g_fmt,
1477     .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1478     .vidioc_s_fmt_vid_cap   = coda_s_fmt_vid_cap,
1479 
1480     .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1481     .vidioc_g_fmt_vid_out   = coda_g_fmt,
1482     .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1483     .vidioc_s_fmt_vid_out   = coda_s_fmt_vid_out,
1484 
1485     .vidioc_reqbufs     = coda_reqbufs,
1486     .vidioc_querybuf    = v4l2_m2m_ioctl_querybuf,
1487 
1488     .vidioc_qbuf        = coda_qbuf,
1489     .vidioc_expbuf      = v4l2_m2m_ioctl_expbuf,
1490     .vidioc_dqbuf       = coda_dqbuf,
1491     .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1492     .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1493 
1494     .vidioc_streamon    = v4l2_m2m_ioctl_streamon,
1495     .vidioc_streamoff   = v4l2_m2m_ioctl_streamoff,
1496 
1497     .vidioc_g_selection = coda_g_selection,
1498     .vidioc_s_selection = coda_s_selection,
1499 
1500     .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
1501     .vidioc_encoder_cmd = coda_encoder_cmd,
1502     .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd,
1503     .vidioc_decoder_cmd = coda_decoder_cmd,
1504 
1505     .vidioc_g_parm      = coda_g_parm,
1506     .vidioc_s_parm      = coda_s_parm,
1507 
1508     .vidioc_enum_framesizes = coda_enum_framesizes,
1509     .vidioc_enum_frameintervals = coda_enum_frameintervals,
1510 
1511     .vidioc_subscribe_event = coda_subscribe_event,
1512     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1513 };
1514 
1515 /*
1516  * Mem-to-mem operations.
1517  */
1518 
1519 static void coda_device_run(void *m2m_priv)
1520 {
1521     struct coda_ctx *ctx = m2m_priv;
1522     struct coda_dev *dev = ctx->dev;
1523 
1524     queue_work(dev->workqueue, &ctx->pic_run_work);
1525 }
1526 
1527 static void coda_pic_run_work(struct work_struct *work)
1528 {
1529     struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1530     struct coda_dev *dev = ctx->dev;
1531     int ret;
1532 
1533     mutex_lock(&ctx->buffer_mutex);
1534     mutex_lock(&dev->coda_mutex);
1535 
1536     ret = ctx->ops->prepare_run(ctx);
1537     if (ret < 0 && ctx->inst_type == CODA_INST_DECODER)
1538         goto out;
1539 
1540     if (!wait_for_completion_timeout(&ctx->completion,
1541                      msecs_to_jiffies(1000))) {
1542         if (ctx->use_bit) {
1543             dev_err(dev->dev, "CODA PIC_RUN timeout\n");
1544 
1545             ctx->hold = true;
1546 
1547             coda_hw_reset(ctx);
1548         }
1549 
1550         if (ctx->ops->run_timeout)
1551             ctx->ops->run_timeout(ctx);
1552     } else {
1553         ctx->ops->finish_run(ctx);
1554     }
1555 
1556     if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1557         ctx->ops->seq_end_work)
1558         queue_work(dev->workqueue, &ctx->seq_end_work);
1559 
1560 out:
1561     mutex_unlock(&dev->coda_mutex);
1562     mutex_unlock(&ctx->buffer_mutex);
1563 
1564     v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1565 }
1566 
1567 static int coda_job_ready(void *m2m_priv)
1568 {
1569     struct coda_ctx *ctx = m2m_priv;
1570     int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1571 
1572     /*
1573      * For both 'P' and 'key' frame cases 1 picture
1574      * and 1 frame are needed. In the decoder case,
1575      * the compressed frame can be in the bitstream.
1576      */
1577     if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1578         coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
1579         return 0;
1580     }
1581 
1582     if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1583         coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
1584         return 0;
1585     }
1586 
1587     if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1588         bool stream_end = ctx->bit_stream_param &
1589                   CODA_BIT_STREAM_END_FLAG;
1590         int num_metas = ctx->num_metas;
1591         struct coda_buffer_meta *meta;
1592         unsigned int count;
1593 
1594         count = hweight32(ctx->frm_dis_flg);
1595         if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1596             coda_dbg(1, ctx,
1597                  "not ready: all internal buffers in use: %d/%d (0x%x)",
1598                  count, ctx->num_internal_frames,
1599                  ctx->frm_dis_flg);
1600             return 0;
1601         }
1602 
1603         if (ctx->hold && !src_bufs) {
1604             coda_dbg(1, ctx,
1605                  "not ready: on hold for more buffers.\n");
1606             return 0;
1607         }
1608 
1609         if (!stream_end && (num_metas + src_bufs) < 2) {
1610             coda_dbg(1, ctx,
1611                  "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1612                  num_metas, src_bufs);
1613             return 0;
1614         }
1615 
1616         meta = list_first_entry(&ctx->buffer_meta_list,
1617                     struct coda_buffer_meta, list);
1618         if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1619             !stream_end) {
1620             coda_dbg(1, ctx,
1621                  "not ready: not enough bitstream data to read past %u (%u)\n",
1622                  meta->end, ctx->bitstream_fifo.kfifo.in);
1623             return 0;
1624         }
1625     }
1626 
1627     if (ctx->aborting) {
1628         coda_dbg(1, ctx, "not ready: aborting\n");
1629         return 0;
1630     }
1631 
1632     coda_dbg(2, ctx, "job ready\n");
1633 
1634     return 1;
1635 }
1636 
1637 static void coda_job_abort(void *priv)
1638 {
1639     struct coda_ctx *ctx = priv;
1640 
1641     ctx->aborting = 1;
1642 
1643     coda_dbg(1, ctx, "job abort\n");
1644 }
1645 
1646 static const struct v4l2_m2m_ops coda_m2m_ops = {
1647     .device_run = coda_device_run,
1648     .job_ready  = coda_job_ready,
1649     .job_abort  = coda_job_abort,
1650 };
1651 
1652 static void set_default_params(struct coda_ctx *ctx)
1653 {
1654     unsigned int max_w, max_h, usize, csize;
1655 
1656     ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1657                      ctx->cvd->dst_formats[0]);
1658     max_w = min(ctx->codec->max_w, 1920U);
1659     max_h = min(ctx->codec->max_h, 1088U);
1660     usize = max_w * max_h * 3 / 2;
1661     csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1662 
1663     ctx->params.codec_mode = ctx->codec->mode;
1664     if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG ||
1665         ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG) {
1666         ctx->colorspace = V4L2_COLORSPACE_SRGB;
1667         ctx->xfer_func = V4L2_XFER_FUNC_SRGB;
1668         ctx->ycbcr_enc = V4L2_YCBCR_ENC_601;
1669         ctx->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1670     } else {
1671         ctx->colorspace = V4L2_COLORSPACE_REC709;
1672         ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1673         ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1674         ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1675     }
1676     ctx->params.framerate = 30;
1677 
1678     /* Default formats for output and input queues */
1679     ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1680     ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1681     ctx->q_data[V4L2_M2M_SRC].width = max_w;
1682     ctx->q_data[V4L2_M2M_SRC].height = max_h;
1683     ctx->q_data[V4L2_M2M_DST].width = max_w;
1684     ctx->q_data[V4L2_M2M_DST].height = max_h;
1685     if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1686         ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1687         ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1688         ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1689         ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1690     } else {
1691         ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1692         ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1693         ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1694         ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1695     }
1696     ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1697     ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1698     ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1699     ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1700 
1701     /*
1702      * Since the RBC2AXI logic only supports a single chroma plane,
1703      * macroblock tiling only works for to NV12 pixel format.
1704      */
1705     ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1706 }
1707 
1708 /*
1709  * Queue operations
1710  */
1711 static int coda_queue_setup(struct vb2_queue *vq,
1712                 unsigned int *nbuffers, unsigned int *nplanes,
1713                 unsigned int sizes[], struct device *alloc_devs[])
1714 {
1715     struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1716     struct coda_q_data *q_data;
1717     unsigned int size;
1718 
1719     q_data = get_q_data(ctx, vq->type);
1720     size = q_data->sizeimage;
1721 
1722     if (*nplanes)
1723         return sizes[0] < size ? -EINVAL : 0;
1724 
1725     *nplanes = 1;
1726     sizes[0] = size;
1727 
1728     coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1729          size);
1730 
1731     return 0;
1732 }
1733 
1734 static int coda_buf_prepare(struct vb2_buffer *vb)
1735 {
1736     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1737     struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1738     struct coda_q_data *q_data;
1739 
1740     q_data = get_q_data(ctx, vb->vb2_queue->type);
1741     if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1742         if (vbuf->field == V4L2_FIELD_ANY)
1743             vbuf->field = V4L2_FIELD_NONE;
1744         if (vbuf->field != V4L2_FIELD_NONE) {
1745             v4l2_warn(&ctx->dev->v4l2_dev,
1746                   "%s field isn't supported\n", __func__);
1747             return -EINVAL;
1748         }
1749     }
1750 
1751     if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1752         v4l2_warn(&ctx->dev->v4l2_dev,
1753               "%s data will not fit into plane (%lu < %lu)\n",
1754               __func__, vb2_plane_size(vb, 0),
1755               (long)q_data->sizeimage);
1756         return -EINVAL;
1757     }
1758 
1759     return 0;
1760 }
1761 
1762 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1763 {
1764     if (!ctrl)
1765         return;
1766 
1767     v4l2_ctrl_lock(ctrl);
1768 
1769     /*
1770      * Extend the control range if the parsed stream contains a known but
1771      * unsupported value or level.
1772      */
1773     if (value > ctrl->maximum) {
1774         __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1775             ctrl->menu_skip_mask & ~(1 << value),
1776             ctrl->default_value);
1777     } else if (value < ctrl->minimum) {
1778         __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1779             ctrl->menu_skip_mask & ~(1 << value),
1780             ctrl->default_value);
1781     }
1782 
1783     __v4l2_ctrl_s_ctrl(ctrl, value);
1784 
1785     v4l2_ctrl_unlock(ctrl);
1786 }
1787 
1788 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
1789                      u8 level_idc)
1790 {
1791     const char * const *profile_names;
1792     const char * const *level_names;
1793     struct v4l2_ctrl *profile_ctrl;
1794     struct v4l2_ctrl *level_ctrl;
1795     const char *codec_name;
1796     u32 profile_cid;
1797     u32 level_cid;
1798     int profile;
1799     int level;
1800 
1801     switch (ctx->codec->src_fourcc) {
1802     case V4L2_PIX_FMT_H264:
1803         codec_name = "H264";
1804         profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
1805         level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
1806         profile_ctrl = ctx->h264_profile_ctrl;
1807         level_ctrl = ctx->h264_level_ctrl;
1808         profile = coda_h264_profile(profile_idc);
1809         level = coda_h264_level(level_idc);
1810         break;
1811     case V4L2_PIX_FMT_MPEG2:
1812         codec_name = "MPEG-2";
1813         profile_cid = V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE;
1814         level_cid = V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL;
1815         profile_ctrl = ctx->mpeg2_profile_ctrl;
1816         level_ctrl = ctx->mpeg2_level_ctrl;
1817         profile = coda_mpeg2_profile(profile_idc);
1818         level = coda_mpeg2_level(level_idc);
1819         break;
1820     case V4L2_PIX_FMT_MPEG4:
1821         codec_name = "MPEG-4";
1822         profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
1823         level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
1824         profile_ctrl = ctx->mpeg4_profile_ctrl;
1825         level_ctrl = ctx->mpeg4_level_ctrl;
1826         profile = coda_mpeg4_profile(profile_idc);
1827         level = coda_mpeg4_level(level_idc);
1828         break;
1829     default:
1830         return;
1831     }
1832 
1833     profile_names = v4l2_ctrl_get_menu(profile_cid);
1834     level_names = v4l2_ctrl_get_menu(level_cid);
1835 
1836     if (profile < 0) {
1837         v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s profile: %u\n",
1838               codec_name, profile_idc);
1839     } else {
1840         coda_dbg(1, ctx, "Parsed %s profile: %s\n", codec_name,
1841              profile_names[profile]);
1842         coda_update_menu_ctrl(profile_ctrl, profile);
1843     }
1844 
1845     if (level < 0) {
1846         v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s level: %u\n",
1847               codec_name, level_idc);
1848     } else {
1849         coda_dbg(1, ctx, "Parsed %s level: %s\n", codec_name,
1850              level_names[level]);
1851         coda_update_menu_ctrl(level_ctrl, level);
1852     }
1853 }
1854 
1855 static void coda_queue_source_change_event(struct coda_ctx *ctx)
1856 {
1857     static const struct v4l2_event source_change_event = {
1858         .type = V4L2_EVENT_SOURCE_CHANGE,
1859         .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1860     };
1861 
1862     v4l2_event_queue_fh(&ctx->fh, &source_change_event);
1863 }
1864 
1865 static void coda_buf_queue(struct vb2_buffer *vb)
1866 {
1867     struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1868     struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1869     struct vb2_queue *vq = vb->vb2_queue;
1870     struct coda_q_data *q_data;
1871 
1872     q_data = get_q_data(ctx, vb->vb2_queue->type);
1873 
1874     /*
1875      * In the decoder case, immediately try to copy the buffer into the
1876      * bitstream ringbuffer and mark it as ready to be dequeued.
1877      */
1878     if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1879         /*
1880          * For backwards compatibility, queuing an empty buffer marks
1881          * the stream end
1882          */
1883         if (vb2_get_plane_payload(vb, 0) == 0)
1884             coda_bit_stream_end_flag(ctx);
1885 
1886         if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1887             /*
1888              * Unless already done, try to obtain profile_idc and
1889              * level_idc from the SPS header. This allows to decide
1890              * whether to enable reordering during sequence
1891              * initialization.
1892              */
1893             if (!ctx->params.h264_profile_idc) {
1894                 coda_sps_parse_profile(ctx, vb);
1895                 coda_update_profile_level_ctrls(ctx,
1896                         ctx->params.h264_profile_idc,
1897                         ctx->params.h264_level_idc);
1898             }
1899         }
1900 
1901         mutex_lock(&ctx->bitstream_mutex);
1902         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1903         if (vb2_is_streaming(vb->vb2_queue))
1904             /* This set buf->sequence = ctx->qsequence++ */
1905             coda_fill_bitstream(ctx, NULL);
1906         mutex_unlock(&ctx->bitstream_mutex);
1907 
1908         if (!ctx->initialized) {
1909             /*
1910              * Run sequence initialization in case the queued
1911              * buffer contained headers.
1912              */
1913             if (vb2_is_streaming(vb->vb2_queue) &&
1914                 ctx->ops->seq_init_work) {
1915                 queue_work(ctx->dev->workqueue,
1916                        &ctx->seq_init_work);
1917                 flush_work(&ctx->seq_init_work);
1918             }
1919 
1920             if (ctx->initialized)
1921                 coda_queue_source_change_event(ctx);
1922         }
1923     } else {
1924         if ((ctx->inst_type == CODA_INST_ENCODER || !ctx->use_bit) &&
1925             vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1926             vbuf->sequence = ctx->qsequence++;
1927         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1928     }
1929 }
1930 
1931 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1932                size_t size, const char *name, struct dentry *parent)
1933 {
1934     buf->vaddr = dma_alloc_coherent(dev->dev, size, &buf->paddr,
1935                     GFP_KERNEL);
1936     if (!buf->vaddr) {
1937         v4l2_err(&dev->v4l2_dev,
1938              "Failed to allocate %s buffer of size %zu\n",
1939              name, size);
1940         return -ENOMEM;
1941     }
1942 
1943     buf->size = size;
1944 
1945     if (name && parent) {
1946         buf->blob.data = buf->vaddr;
1947         buf->blob.size = size;
1948         buf->dentry = debugfs_create_blob(name, 0444, parent,
1949                           &buf->blob);
1950     }
1951 
1952     return 0;
1953 }
1954 
1955 void coda_free_aux_buf(struct coda_dev *dev,
1956                struct coda_aux_buf *buf)
1957 {
1958     if (buf->vaddr) {
1959         dma_free_coherent(dev->dev, buf->size, buf->vaddr, buf->paddr);
1960         buf->vaddr = NULL;
1961         buf->size = 0;
1962         debugfs_remove(buf->dentry);
1963         buf->dentry = NULL;
1964     }
1965 }
1966 
1967 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1968 {
1969     struct coda_ctx *ctx = vb2_get_drv_priv(q);
1970     struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1971     struct coda_q_data *q_data_src, *q_data_dst;
1972     struct v4l2_m2m_buffer *m2m_buf, *tmp;
1973     struct vb2_v4l2_buffer *buf;
1974     struct list_head list;
1975     int ret = 0;
1976 
1977     if (count < 1)
1978         return -EINVAL;
1979 
1980     coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1981 
1982     INIT_LIST_HEAD(&list);
1983 
1984     q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1985     if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1986         if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1987             /* copy the buffers that were queued before streamon */
1988             mutex_lock(&ctx->bitstream_mutex);
1989             coda_fill_bitstream(ctx, &list);
1990             mutex_unlock(&ctx->bitstream_mutex);
1991 
1992             if (ctx->dev->devtype->product != CODA_960 &&
1993                 coda_get_bitstream_payload(ctx) < 512) {
1994                 v4l2_err(v4l2_dev, "start payload < 512\n");
1995                 ret = -EINVAL;
1996                 goto err;
1997             }
1998 
1999             if (!ctx->initialized) {
2000                 /* Run sequence initialization */
2001                 if (ctx->ops->seq_init_work) {
2002                     queue_work(ctx->dev->workqueue,
2003                            &ctx->seq_init_work);
2004                     flush_work(&ctx->seq_init_work);
2005                 }
2006             }
2007         }
2008 
2009         /*
2010          * Check the first input JPEG buffer to determine chroma
2011          * subsampling.
2012          */
2013         if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
2014             buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2015             coda_jpeg_decode_header(ctx, &buf->vb2_buf);
2016             /*
2017              * We have to start streaming even if the first buffer
2018              * does not contain a valid JPEG image. The error will
2019              * be caught during device run and will be signalled
2020              * via the capture buffer error flag.
2021              */
2022 
2023             q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2024             q_data_dst->width = round_up(q_data_src->width, 16);
2025             q_data_dst->height = round_up(q_data_src->height, 16);
2026             q_data_dst->bytesperline = q_data_dst->width;
2027             if (ctx->params.jpeg_chroma_subsampling ==
2028                 V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
2029                 q_data_dst->sizeimage =
2030                         q_data_dst->bytesperline *
2031                         q_data_dst->height * 3 / 2;
2032                 if (q_data_dst->fourcc != V4L2_PIX_FMT_YUV420)
2033                     q_data_dst->fourcc = V4L2_PIX_FMT_NV12;
2034             } else {
2035                 q_data_dst->sizeimage =
2036                         q_data_dst->bytesperline *
2037                         q_data_dst->height * 2;
2038                 q_data_dst->fourcc = V4L2_PIX_FMT_YUV422P;
2039             }
2040             q_data_dst->rect.left = 0;
2041             q_data_dst->rect.top = 0;
2042             q_data_dst->rect.width = q_data_src->width;
2043             q_data_dst->rect.height = q_data_src->height;
2044         }
2045         ctx->streamon_out = 1;
2046     } else {
2047         ctx->streamon_cap = 1;
2048     }
2049 
2050     /* Don't start the coda unless both queues are on */
2051     if (!(ctx->streamon_out && ctx->streamon_cap))
2052         goto out;
2053 
2054     q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2055     if ((q_data_src->rect.width != q_data_dst->width &&
2056          round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
2057         (q_data_src->rect.height != q_data_dst->height &&
2058          round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
2059         v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
2060              q_data_src->rect.width, q_data_src->rect.height,
2061              q_data_dst->width, q_data_dst->height);
2062         ret = -EINVAL;
2063         goto err;
2064     }
2065 
2066     /* Allow BIT decoder device_run with no new buffers queued */
2067     if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2068         v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
2069 
2070     ctx->gopcounter = ctx->params.gop_size - 1;
2071 
2072     if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
2073         ctx->params.gop_size = 1;
2074     ctx->gopcounter = ctx->params.gop_size - 1;
2075     /* Only decoders have this control */
2076     if (ctx->mb_err_cnt_ctrl)
2077         v4l2_ctrl_s_ctrl(ctx->mb_err_cnt_ctrl, 0);
2078 
2079     ret = ctx->ops->start_streaming(ctx);
2080     if (ctx->inst_type == CODA_INST_DECODER) {
2081         if (ret == -EAGAIN)
2082             goto out;
2083     }
2084     if (ret < 0)
2085         goto err;
2086 
2087 out:
2088     if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2089         list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2090             list_del(&m2m_buf->list);
2091             v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
2092         }
2093     }
2094     return 0;
2095 
2096 err:
2097     if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2098         list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2099             list_del(&m2m_buf->list);
2100             v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
2101         }
2102         while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2103             v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2104     } else {
2105         while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2106             v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2107     }
2108     return ret;
2109 }
2110 
2111 static void coda_stop_streaming(struct vb2_queue *q)
2112 {
2113     struct coda_ctx *ctx = vb2_get_drv_priv(q);
2114     struct coda_dev *dev = ctx->dev;
2115     struct vb2_v4l2_buffer *buf;
2116     bool stop;
2117 
2118     stop = ctx->streamon_out && ctx->streamon_cap;
2119 
2120     coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
2121 
2122     if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2123         ctx->streamon_out = 0;
2124 
2125         coda_bit_stream_end_flag(ctx);
2126 
2127         ctx->qsequence = 0;
2128 
2129         while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2130             v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2131     } else {
2132         ctx->streamon_cap = 0;
2133 
2134         ctx->osequence = 0;
2135         ctx->sequence_offset = 0;
2136 
2137         while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2138             v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2139     }
2140 
2141     if (stop) {
2142         struct coda_buffer_meta *meta;
2143 
2144         if (ctx->ops->seq_end_work) {
2145             queue_work(dev->workqueue, &ctx->seq_end_work);
2146             flush_work(&ctx->seq_end_work);
2147         }
2148         spin_lock(&ctx->buffer_meta_lock);
2149         while (!list_empty(&ctx->buffer_meta_list)) {
2150             meta = list_first_entry(&ctx->buffer_meta_list,
2151                         struct coda_buffer_meta, list);
2152             list_del(&meta->list);
2153             kfree(meta);
2154         }
2155         ctx->num_metas = 0;
2156         spin_unlock(&ctx->buffer_meta_lock);
2157         kfifo_init(&ctx->bitstream_fifo,
2158             ctx->bitstream.vaddr, ctx->bitstream.size);
2159         ctx->runcounter = 0;
2160         ctx->aborting = 0;
2161         ctx->hold = false;
2162     }
2163 
2164     if (!ctx->streamon_out && !ctx->streamon_cap)
2165         ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
2166 }
2167 
2168 static const struct vb2_ops coda_qops = {
2169     .queue_setup        = coda_queue_setup,
2170     .buf_prepare        = coda_buf_prepare,
2171     .buf_queue      = coda_buf_queue,
2172     .start_streaming    = coda_start_streaming,
2173     .stop_streaming     = coda_stop_streaming,
2174     .wait_prepare       = vb2_ops_wait_prepare,
2175     .wait_finish        = vb2_ops_wait_finish,
2176 };
2177 
2178 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2179 {
2180     const char * const *val_names = v4l2_ctrl_get_menu(ctrl->id);
2181     struct coda_ctx *ctx =
2182             container_of(ctrl->handler, struct coda_ctx, ctrls);
2183 
2184     if (val_names)
2185         coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d (\"%s\")\n",
2186              ctrl->id, ctrl->name, ctrl->val, val_names[ctrl->val]);
2187     else
2188         coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
2189              ctrl->id, ctrl->name, ctrl->val);
2190 
2191     switch (ctrl->id) {
2192     case V4L2_CID_HFLIP:
2193         if (ctrl->val)
2194             ctx->params.rot_mode |= CODA_MIR_HOR;
2195         else
2196             ctx->params.rot_mode &= ~CODA_MIR_HOR;
2197         break;
2198     case V4L2_CID_VFLIP:
2199         if (ctrl->val)
2200             ctx->params.rot_mode |= CODA_MIR_VER;
2201         else
2202             ctx->params.rot_mode &= ~CODA_MIR_VER;
2203         break;
2204     case V4L2_CID_MPEG_VIDEO_BITRATE:
2205         ctx->params.bitrate = ctrl->val / 1000;
2206         ctx->params.bitrate_changed = true;
2207         break;
2208     case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2209         ctx->params.gop_size = ctrl->val;
2210         break;
2211     case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2212         ctx->params.h264_intra_qp = ctrl->val;
2213         ctx->params.h264_intra_qp_changed = true;
2214         break;
2215     case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2216         ctx->params.h264_inter_qp = ctrl->val;
2217         break;
2218     case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2219         ctx->params.h264_min_qp = ctrl->val;
2220         break;
2221     case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2222         ctx->params.h264_max_qp = ctrl->val;
2223         break;
2224     case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2225         ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
2226         break;
2227     case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2228         ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
2229         break;
2230     case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2231         ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
2232         break;
2233     case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
2234         ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
2235         break;
2236     case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
2237         ctx->params.frame_rc_enable = ctrl->val;
2238         break;
2239     case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
2240         ctx->params.mb_rc_enable = ctrl->val;
2241         break;
2242     case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
2243         ctx->params.h264_chroma_qp_index_offset = ctrl->val;
2244         break;
2245     case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
2246         /* TODO: switch between baseline and constrained baseline */
2247         if (ctx->inst_type == CODA_INST_ENCODER)
2248             ctx->params.h264_profile_idc = 66;
2249         break;
2250     case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2251         /* nothing to do, this is set by the encoder */
2252         break;
2253     case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2254         ctx->params.mpeg4_intra_qp = ctrl->val;
2255         break;
2256     case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2257         ctx->params.mpeg4_inter_qp = ctrl->val;
2258         break;
2259     case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
2260     case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
2261     case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
2262     case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
2263         /* nothing to do, these are fixed */
2264         break;
2265     case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2266         ctx->params.slice_mode = ctrl->val;
2267         ctx->params.slice_mode_changed = true;
2268         break;
2269     case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2270         ctx->params.slice_max_mb = ctrl->val;
2271         ctx->params.slice_mode_changed = true;
2272         break;
2273     case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2274         ctx->params.slice_max_bits = ctrl->val * 8;
2275         ctx->params.slice_mode_changed = true;
2276         break;
2277     case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2278         break;
2279     case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2280         ctx->params.intra_refresh = ctrl->val;
2281         ctx->params.intra_refresh_changed = true;
2282         break;
2283     case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
2284         ctx->params.force_ipicture = true;
2285         break;
2286     case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2287         coda_set_jpeg_compression_quality(ctx, ctrl->val);
2288         break;
2289     case V4L2_CID_JPEG_RESTART_INTERVAL:
2290         ctx->params.jpeg_restart_interval = ctrl->val;
2291         break;
2292     case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
2293         ctx->params.vbv_delay = ctrl->val;
2294         break;
2295     case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
2296         ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
2297         break;
2298     default:
2299         coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
2300              ctrl->id, ctrl->val);
2301         return -EINVAL;
2302     }
2303 
2304     return 0;
2305 }
2306 
2307 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
2308     .s_ctrl = coda_s_ctrl,
2309 };
2310 
2311 static void coda_encode_ctrls(struct coda_ctx *ctx)
2312 {
2313     int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
2314 
2315     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2316         V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
2317     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2318         V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
2319     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2320         V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
2321     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2322         V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
2323     if (ctx->dev->devtype->product != CODA_960) {
2324         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2325             V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2326     }
2327     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2328         V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
2329     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2330         V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
2331     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2332         V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
2333     v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2334         V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2335         V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
2336         0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2337     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2338         V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
2339         0);
2340     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2341         V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
2342     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2343         V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, 0, 1, 1, 1);
2344     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2345         V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
2346     v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2347         V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2348         V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE, 0x0,
2349         V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE);
2350     if (ctx->dev->devtype->product == CODA_HX4 ||
2351         ctx->dev->devtype->product == CODA_7541) {
2352         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2353             V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2354             V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2355             ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2356               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2357               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2358             V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2359     }
2360     if (ctx->dev->devtype->product == CODA_960) {
2361         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2362             V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2363             V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
2364             ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
2365               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2366               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2367               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2368               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2369               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0) |
2370               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_1) |
2371               (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_2)),
2372             V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2373     }
2374     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2375         V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2376     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2377         V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2378     v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2379         V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2380         V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2381         V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
2382     if (ctx->dev->devtype->product == CODA_HX4 ||
2383         ctx->dev->devtype->product == CODA_7541 ||
2384         ctx->dev->devtype->product == CODA_960) {
2385         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2386             V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2387             V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2388             ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2389             V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2390     }
2391     v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2392         V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
2393         V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES, 0x0,
2394         V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
2395     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2396         V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
2397     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2398         V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2399         500);
2400     v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2401         V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2402         V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2403         (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2404         V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2405     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2406         V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2407         1920 * 1088 / 256, 1, 0);
2408     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2409         V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2410     /*
2411      * The maximum VBV size value is 0x7fffffff bits,
2412      * one bit less than 262144 KiB
2413      */
2414     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2415         V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
2416 }
2417 
2418 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2419 {
2420     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2421         V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2422     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2423         V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2424 }
2425 
2426 static void coda_decode_ctrls(struct coda_ctx *ctx)
2427 {
2428     u8 max;
2429 
2430     ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2431         &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2432         V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2433         ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
2434           (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2435           (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2436         V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2437     if (ctx->h264_profile_ctrl)
2438         ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2439 
2440     if (ctx->dev->devtype->product == CODA_HX4 ||
2441         ctx->dev->devtype->product == CODA_7541)
2442         max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2443     else if (ctx->dev->devtype->product == CODA_960)
2444         max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
2445     else
2446         return;
2447     ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2448         &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
2449     if (ctx->h264_level_ctrl)
2450         ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2451 
2452     ctx->mpeg2_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2453         &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE,
2454         V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH, 0,
2455         V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH);
2456     if (ctx->mpeg2_profile_ctrl)
2457         ctx->mpeg2_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2458 
2459     ctx->mpeg2_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2460         &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL,
2461         V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH, 0,
2462         V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH);
2463     if (ctx->mpeg2_level_ctrl)
2464         ctx->mpeg2_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2465 
2466     ctx->mpeg4_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2467         &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2468         V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY, 0,
2469         V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY);
2470     if (ctx->mpeg4_profile_ctrl)
2471         ctx->mpeg4_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2472 
2473     ctx->mpeg4_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2474         &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2475         V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 0,
2476         V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2477     if (ctx->mpeg4_level_ctrl)
2478         ctx->mpeg4_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2479 }
2480 
2481 static const struct v4l2_ctrl_config coda_mb_err_cnt_ctrl_config = {
2482     .id = V4L2_CID_CODA_MB_ERR_CNT,
2483     .name   = "Macroblocks Error Count",
2484     .type   = V4L2_CTRL_TYPE_INTEGER,
2485     .min    = 0,
2486     .max    = 0x7fffffff,
2487     .step   = 1,
2488 };
2489 
2490 static int coda_ctrls_setup(struct coda_ctx *ctx)
2491 {
2492     v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2493 
2494     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2495         V4L2_CID_HFLIP, 0, 1, 1, 0);
2496     v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2497         V4L2_CID_VFLIP, 0, 1, 1, 0);
2498     if (ctx->inst_type == CODA_INST_ENCODER) {
2499         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2500                   V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2501                   1, 1, 1, 1);
2502         if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2503             coda_jpeg_encode_ctrls(ctx);
2504         else
2505             coda_encode_ctrls(ctx);
2506     } else {
2507         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2508                   V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2509                   1, 1, 1, 1);
2510         if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2511             coda_decode_ctrls(ctx);
2512 
2513         ctx->mb_err_cnt_ctrl = v4l2_ctrl_new_custom(&ctx->ctrls,
2514                         &coda_mb_err_cnt_ctrl_config,
2515                         NULL);
2516         if (ctx->mb_err_cnt_ctrl)
2517             ctx->mb_err_cnt_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2518     }
2519 
2520     if (ctx->ctrls.error) {
2521         v4l2_err(&ctx->dev->v4l2_dev,
2522             "control initialization error (%d)",
2523             ctx->ctrls.error);
2524         return -EINVAL;
2525     }
2526 
2527     return v4l2_ctrl_handler_setup(&ctx->ctrls);
2528 }
2529 
2530 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2531 {
2532     vq->drv_priv = ctx;
2533     vq->ops = &coda_qops;
2534     vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2535     vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2536     vq->lock = &ctx->dev->dev_mutex;
2537     /* One way to indicate end-of-stream for coda is to set the
2538      * bytesused == 0. However by default videobuf2 handles bytesused
2539      * equal to 0 as a special case and changes its value to the size
2540      * of the buffer. Set the allow_zero_bytesused flag, so
2541      * that videobuf2 will keep the value of bytesused intact.
2542      */
2543     vq->allow_zero_bytesused = 1;
2544     /*
2545      * We might be fine with no buffers on some of the queues, but that
2546      * would need to be reflected in job_ready(). Currently we expect all
2547      * queues to have at least one buffer queued.
2548      */
2549     vq->min_buffers_needed = 1;
2550     vq->dev = ctx->dev->dev;
2551 
2552     return vb2_queue_init(vq);
2553 }
2554 
2555 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2556                 struct vb2_queue *dst_vq)
2557 {
2558     int ret;
2559 
2560     src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2561     src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2562     src_vq->mem_ops = &vb2_dma_contig_memops;
2563 
2564     ret = coda_queue_init(priv, src_vq);
2565     if (ret)
2566         return ret;
2567 
2568     dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2569     dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2570     dst_vq->mem_ops = &vb2_dma_contig_memops;
2571 
2572     return coda_queue_init(priv, dst_vq);
2573 }
2574 
2575 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2576                 struct vb2_queue *dst_vq)
2577 {
2578     int ret;
2579 
2580     src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2581     src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2582     src_vq->mem_ops = &vb2_vmalloc_memops;
2583 
2584     ret = coda_queue_init(priv, src_vq);
2585     if (ret)
2586         return ret;
2587 
2588     dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2589     dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2590     dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING;
2591     dst_vq->mem_ops = &vb2_dma_contig_memops;
2592 
2593     return coda_queue_init(priv, dst_vq);
2594 }
2595 
2596 /*
2597  * File operations
2598  */
2599 
2600 static int coda_open(struct file *file)
2601 {
2602     struct video_device *vdev = video_devdata(file);
2603     struct coda_dev *dev = video_get_drvdata(vdev);
2604     struct coda_ctx *ctx;
2605     unsigned int max = ~0;
2606     char *name;
2607     int ret;
2608     int idx;
2609 
2610     ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2611     if (!ctx)
2612         return -ENOMEM;
2613 
2614     if (dev->devtype->product == CODA_DX6)
2615         max = CODADX6_MAX_INSTANCES - 1;
2616     idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2617     if (idx < 0) {
2618         ret = idx;
2619         goto err_coda_max;
2620     }
2621 
2622     name = kasprintf(GFP_KERNEL, "context%d", idx);
2623     if (!name) {
2624         ret = -ENOMEM;
2625         goto err_coda_name_init;
2626     }
2627 
2628     ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2629     kfree(name);
2630 
2631     ctx->cvd = to_coda_video_device(vdev);
2632     ctx->inst_type = ctx->cvd->type;
2633     ctx->ops = ctx->cvd->ops;
2634     ctx->use_bit = !ctx->cvd->direct;
2635     init_completion(&ctx->completion);
2636     INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2637     if (ctx->ops->seq_init_work)
2638         INIT_WORK(&ctx->seq_init_work, ctx->ops->seq_init_work);
2639     if (ctx->ops->seq_end_work)
2640         INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2641     v4l2_fh_init(&ctx->fh, video_devdata(file));
2642     file->private_data = &ctx->fh;
2643     v4l2_fh_add(&ctx->fh);
2644     ctx->dev = dev;
2645     ctx->idx = idx;
2646 
2647     coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2648 
2649     switch (dev->devtype->product) {
2650     case CODA_960:
2651         /*
2652          * Enabling the BWB when decoding can hang the firmware with
2653          * certain streams. The issue was tracked as ENGR00293425 by
2654          * Freescale. As a workaround, disable BWB for all decoders.
2655          * The enable_bwb module parameter allows to override this.
2656          */
2657         if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2658             ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2659         fallthrough;
2660     case CODA_HX4:
2661     case CODA_7541:
2662         ctx->reg_idx = 0;
2663         break;
2664     default:
2665         ctx->reg_idx = idx;
2666     }
2667     if (ctx->dev->vdoa && !disable_vdoa) {
2668         ctx->vdoa = vdoa_context_create(dev->vdoa);
2669         if (!ctx->vdoa)
2670             v4l2_warn(&dev->v4l2_dev,
2671                   "Failed to create vdoa context: not using vdoa");
2672     }
2673     ctx->use_vdoa = false;
2674 
2675     /* Power up and upload firmware if necessary */
2676     ret = pm_runtime_resume_and_get(dev->dev);
2677     if (ret < 0) {
2678         v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2679         goto err_pm_get;
2680     }
2681 
2682     ret = clk_prepare_enable(dev->clk_per);
2683     if (ret)
2684         goto err_clk_enable;
2685 
2686     ret = clk_prepare_enable(dev->clk_ahb);
2687     if (ret)
2688         goto err_clk_ahb;
2689 
2690     set_default_params(ctx);
2691     ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2692                         ctx->ops->queue_init);
2693     if (IS_ERR(ctx->fh.m2m_ctx)) {
2694         ret = PTR_ERR(ctx->fh.m2m_ctx);
2695 
2696         v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2697              __func__, ret);
2698         goto err_ctx_init;
2699     }
2700 
2701     ret = coda_ctrls_setup(ctx);
2702     if (ret) {
2703         v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2704         goto err_ctrls_setup;
2705     }
2706 
2707     ctx->fh.ctrl_handler = &ctx->ctrls;
2708 
2709     mutex_init(&ctx->bitstream_mutex);
2710     mutex_init(&ctx->buffer_mutex);
2711     mutex_init(&ctx->wakeup_mutex);
2712     INIT_LIST_HEAD(&ctx->buffer_meta_list);
2713     spin_lock_init(&ctx->buffer_meta_lock);
2714 
2715     return 0;
2716 
2717 err_ctrls_setup:
2718     v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2719 err_ctx_init:
2720     clk_disable_unprepare(dev->clk_ahb);
2721 err_clk_ahb:
2722     clk_disable_unprepare(dev->clk_per);
2723 err_clk_enable:
2724     pm_runtime_put_sync(dev->dev);
2725 err_pm_get:
2726     v4l2_fh_del(&ctx->fh);
2727     v4l2_fh_exit(&ctx->fh);
2728 err_coda_name_init:
2729     ida_free(&dev->ida, ctx->idx);
2730 err_coda_max:
2731     kfree(ctx);
2732     return ret;
2733 }
2734 
2735 static int coda_release(struct file *file)
2736 {
2737     struct coda_dev *dev = video_drvdata(file);
2738     struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2739 
2740     coda_dbg(1, ctx, "release instance (%p)\n", ctx);
2741 
2742     if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2743         coda_bit_stream_end_flag(ctx);
2744 
2745     /* If this instance is running, call .job_abort and wait for it to end */
2746     v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2747 
2748     if (ctx->vdoa)
2749         vdoa_context_destroy(ctx->vdoa);
2750 
2751     /* In case the instance was not running, we still need to call SEQ_END */
2752     if (ctx->ops->seq_end_work) {
2753         queue_work(dev->workqueue, &ctx->seq_end_work);
2754         flush_work(&ctx->seq_end_work);
2755     }
2756 
2757     if (ctx->dev->devtype->product == CODA_DX6)
2758         coda_free_aux_buf(dev, &ctx->workbuf);
2759 
2760     v4l2_ctrl_handler_free(&ctx->ctrls);
2761     clk_disable_unprepare(dev->clk_ahb);
2762     clk_disable_unprepare(dev->clk_per);
2763     pm_runtime_put_sync(dev->dev);
2764     v4l2_fh_del(&ctx->fh);
2765     v4l2_fh_exit(&ctx->fh);
2766     ida_free(&dev->ida, ctx->idx);
2767     if (ctx->ops->release)
2768         ctx->ops->release(ctx);
2769     debugfs_remove_recursive(ctx->debugfs_entry);
2770     kfree(ctx);
2771 
2772     return 0;
2773 }
2774 
2775 static const struct v4l2_file_operations coda_fops = {
2776     .owner      = THIS_MODULE,
2777     .open       = coda_open,
2778     .release    = coda_release,
2779     .poll       = v4l2_m2m_fop_poll,
2780     .unlocked_ioctl = video_ioctl2,
2781     .mmap       = v4l2_m2m_fop_mmap,
2782 };
2783 
2784 static int coda_hw_init(struct coda_dev *dev)
2785 {
2786     u32 data;
2787     u16 *p;
2788     int i, ret;
2789 
2790     ret = clk_prepare_enable(dev->clk_per);
2791     if (ret)
2792         goto err_clk_per;
2793 
2794     ret = clk_prepare_enable(dev->clk_ahb);
2795     if (ret)
2796         goto err_clk_ahb;
2797 
2798     reset_control_reset(dev->rstc);
2799 
2800     /*
2801      * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2802      * The 16-bit chars in the code buffer are in memory access
2803      * order, re-sort them to CODA order for register download.
2804      * Data in this SRAM survives a reboot.
2805      */
2806     p = (u16 *)dev->codebuf.vaddr;
2807     if (dev->devtype->product == CODA_DX6) {
2808         for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++)  {
2809             data = CODA_DOWN_ADDRESS_SET(i) |
2810                 CODA_DOWN_DATA_SET(p[i ^ 1]);
2811             coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2812         }
2813     } else {
2814         for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2815             data = CODA_DOWN_ADDRESS_SET(i) |
2816                 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2817                             3 - (i % 4)]);
2818             coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2819         }
2820     }
2821 
2822     /* Clear registers */
2823     for (i = 0; i < 64; i++)
2824         coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2825 
2826     /* Tell the BIT where to find everything it needs */
2827     if (dev->devtype->product == CODA_960 ||
2828         dev->devtype->product == CODA_7541 ||
2829         dev->devtype->product == CODA_HX4) {
2830         coda_write(dev, dev->tempbuf.paddr,
2831                 CODA_REG_BIT_TEMP_BUF_ADDR);
2832         coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2833     } else {
2834         coda_write(dev, dev->workbuf.paddr,
2835                   CODA_REG_BIT_WORK_BUF_ADDR);
2836     }
2837     coda_write(dev, dev->codebuf.paddr,
2838               CODA_REG_BIT_CODE_BUF_ADDR);
2839     coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2840 
2841     /* Set default values */
2842     switch (dev->devtype->product) {
2843     case CODA_DX6:
2844         coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2845                CODA_REG_BIT_STREAM_CTRL);
2846         break;
2847     default:
2848         coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2849                CODA_REG_BIT_STREAM_CTRL);
2850     }
2851     if (dev->devtype->product == CODA_960)
2852         coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2853                 CODA_REG_BIT_FRAME_MEM_CTRL);
2854     else
2855         coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2856 
2857     if (dev->devtype->product != CODA_DX6)
2858         coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2859 
2860     coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2861               CODA_REG_BIT_INT_ENABLE);
2862 
2863     /* Reset VPU and start processor */
2864     data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2865     data |= CODA_REG_RESET_ENABLE;
2866     coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2867     udelay(10);
2868     data &= ~CODA_REG_RESET_ENABLE;
2869     coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2870     coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2871 
2872     clk_disable_unprepare(dev->clk_ahb);
2873     clk_disable_unprepare(dev->clk_per);
2874 
2875     return 0;
2876 
2877 err_clk_ahb:
2878     clk_disable_unprepare(dev->clk_per);
2879 err_clk_per:
2880     return ret;
2881 }
2882 
2883 static int coda_register_device(struct coda_dev *dev, int i)
2884 {
2885     struct video_device *vfd = &dev->vfd[i];
2886     const char *name;
2887     int ret;
2888 
2889     if (i >= dev->devtype->num_vdevs)
2890         return -EINVAL;
2891     name = dev->devtype->vdevs[i]->name;
2892 
2893     strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2894     vfd->fops   = &coda_fops;
2895     vfd->ioctl_ops  = &coda_ioctl_ops;
2896     vfd->release    = video_device_release_empty;
2897     vfd->lock   = &dev->dev_mutex;
2898     vfd->v4l2_dev   = &dev->v4l2_dev;
2899     vfd->vfl_dir    = VFL_DIR_M2M;
2900     vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2901     video_set_drvdata(vfd, dev);
2902 
2903     /* Not applicable, use the selection API instead */
2904     v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2905     v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2906     v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2907 
2908     if (dev->devtype->vdevs[i]->type == CODA_INST_ENCODER) {
2909         v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
2910         v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
2911         if (dev->devtype->vdevs[i]->dst_formats[0] == V4L2_PIX_FMT_JPEG) {
2912             v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMEINTERVALS);
2913             v4l2_disable_ioctl(vfd, VIDIOC_G_PARM);
2914             v4l2_disable_ioctl(vfd, VIDIOC_S_PARM);
2915         }
2916     } else {
2917         v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
2918         v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
2919         v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMESIZES);
2920         v4l2_disable_ioctl(vfd, VIDIOC_ENUM_FRAMEINTERVALS);
2921         v4l2_disable_ioctl(vfd, VIDIOC_G_PARM);
2922         v4l2_disable_ioctl(vfd, VIDIOC_S_PARM);
2923     }
2924 
2925     ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
2926     if (!ret)
2927         v4l2_info(&dev->v4l2_dev, "%s registered as %s\n",
2928               name, video_device_node_name(vfd));
2929     return ret;
2930 }
2931 
2932 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2933                    size_t size)
2934 {
2935     u32 *src = (u32 *)buf;
2936 
2937     /* Check if the firmware has a 16-byte Freescale header, skip it */
2938     if (buf[0] == 'M' && buf[1] == 'X')
2939         src += 4;
2940     /*
2941      * Check whether the firmware is in native order or pre-reordered for
2942      * memory access. The first instruction opcode always is 0xe40e.
2943      */
2944     if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2945         u32 *dst = dev->codebuf.vaddr;
2946         int i;
2947 
2948         /* Firmware in native order, reorder while copying */
2949         if (dev->devtype->product == CODA_DX6) {
2950             for (i = 0; i < (size - 16) / 4; i++)
2951                 dst[i] = (src[i] << 16) | (src[i] >> 16);
2952         } else {
2953             for (i = 0; i < (size - 16) / 4; i += 2) {
2954                 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2955                 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2956             }
2957         }
2958     } else {
2959         /* Copy the already reordered firmware image */
2960         memcpy(dev->codebuf.vaddr, src, size);
2961     }
2962 }
2963 
2964 static void coda_fw_callback(const struct firmware *fw, void *context);
2965 
2966 static int coda_firmware_request(struct coda_dev *dev)
2967 {
2968     char *fw;
2969 
2970     if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2971         return -EINVAL;
2972 
2973     fw = dev->devtype->firmware[dev->firmware];
2974 
2975     dev_dbg(dev->dev, "requesting firmware '%s' for %s\n", fw,
2976         coda_product_name(dev->devtype->product));
2977 
2978     return request_firmware_nowait(THIS_MODULE, true, fw, dev->dev,
2979                        GFP_KERNEL, dev, coda_fw_callback);
2980 }
2981 
2982 static void coda_fw_callback(const struct firmware *fw, void *context)
2983 {
2984     struct coda_dev *dev = context;
2985     int i, ret;
2986 
2987     if (!fw) {
2988         dev->firmware++;
2989         ret = coda_firmware_request(dev);
2990         if (ret < 0) {
2991             v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2992             goto put_pm;
2993         }
2994         return;
2995     }
2996     if (dev->firmware > 0) {
2997         /*
2998          * Since we can't suppress warnings for failed asynchronous
2999          * firmware requests, report that the fallback firmware was
3000          * found.
3001          */
3002         dev_info(dev->dev, "Using fallback firmware %s\n",
3003              dev->devtype->firmware[dev->firmware]);
3004     }
3005 
3006     /* allocate auxiliary per-device code buffer for the BIT processor */
3007     ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3008                  dev->debugfs_root);
3009     if (ret < 0)
3010         goto put_pm;
3011 
3012     coda_copy_firmware(dev, fw->data, fw->size);
3013     release_firmware(fw);
3014 
3015     ret = coda_hw_init(dev);
3016     if (ret < 0) {
3017         v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3018         goto put_pm;
3019     }
3020 
3021     ret = coda_check_firmware(dev);
3022     if (ret < 0)
3023         goto put_pm;
3024 
3025     dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3026     if (IS_ERR(dev->m2m_dev)) {
3027         v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3028         goto put_pm;
3029     }
3030 
3031     for (i = 0; i < dev->devtype->num_vdevs; i++) {
3032         ret = coda_register_device(dev, i);
3033         if (ret) {
3034             v4l2_err(&dev->v4l2_dev,
3035                  "Failed to register %s video device: %d\n",
3036                  dev->devtype->vdevs[i]->name, ret);
3037             goto rel_vfd;
3038         }
3039     }
3040 
3041     pm_runtime_put_sync(dev->dev);
3042     return;
3043 
3044 rel_vfd:
3045     while (--i >= 0)
3046         video_unregister_device(&dev->vfd[i]);
3047     v4l2_m2m_release(dev->m2m_dev);
3048 put_pm:
3049     pm_runtime_put_sync(dev->dev);
3050 }
3051 
3052 enum coda_platform {
3053     CODA_IMX27,
3054     CODA_IMX51,
3055     CODA_IMX53,
3056     CODA_IMX6Q,
3057     CODA_IMX6DL,
3058 };
3059 
3060 static const struct coda_devtype coda_devdata[] = {
3061     [CODA_IMX27] = {
3062         .firmware     = {
3063             "vpu_fw_imx27_TO2.bin",
3064             "vpu/vpu_fw_imx27_TO2.bin",
3065             "v4l-codadx6-imx27.bin"
3066         },
3067         .product      = CODA_DX6,
3068         .codecs       = codadx6_codecs,
3069         .num_codecs   = ARRAY_SIZE(codadx6_codecs),
3070         .vdevs        = codadx6_video_devices,
3071         .num_vdevs    = ARRAY_SIZE(codadx6_video_devices),
3072         .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
3073         .iram_size    = 0xb000,
3074     },
3075     [CODA_IMX51] = {
3076         .firmware     = {
3077             "vpu_fw_imx51.bin",
3078             "vpu/vpu_fw_imx51.bin",
3079             "v4l-codahx4-imx51.bin"
3080         },
3081         .product      = CODA_HX4,
3082         .codecs       = codahx4_codecs,
3083         .num_codecs   = ARRAY_SIZE(codahx4_codecs),
3084         .vdevs        = codahx4_video_devices,
3085         .num_vdevs    = ARRAY_SIZE(codahx4_video_devices),
3086         .workbuf_size = 128 * 1024,
3087         .tempbuf_size = 304 * 1024,
3088         .iram_size    = 0x14000,
3089     },
3090     [CODA_IMX53] = {
3091         .firmware     = {
3092             "vpu_fw_imx53.bin",
3093             "vpu/vpu_fw_imx53.bin",
3094             "v4l-coda7541-imx53.bin"
3095         },
3096         .product      = CODA_7541,
3097         .codecs       = coda7_codecs,
3098         .num_codecs   = ARRAY_SIZE(coda7_codecs),
3099         .vdevs        = coda7_video_devices,
3100         .num_vdevs    = ARRAY_SIZE(coda7_video_devices),
3101         .workbuf_size = 128 * 1024,
3102         .tempbuf_size = 304 * 1024,
3103         .iram_size    = 0x14000,
3104     },
3105     [CODA_IMX6Q] = {
3106         .firmware     = {
3107             "vpu_fw_imx6q.bin",
3108             "vpu/vpu_fw_imx6q.bin",
3109             "v4l-coda960-imx6q.bin"
3110         },
3111         .product      = CODA_960,
3112         .codecs       = coda9_codecs,
3113         .num_codecs   = ARRAY_SIZE(coda9_codecs),
3114         .vdevs        = coda9_video_devices,
3115         .num_vdevs    = ARRAY_SIZE(coda9_video_devices),
3116         .workbuf_size = 80 * 1024,
3117         .tempbuf_size = 204 * 1024,
3118         .iram_size    = 0x21000,
3119     },
3120     [CODA_IMX6DL] = {
3121         .firmware     = {
3122             "vpu_fw_imx6d.bin",
3123             "vpu/vpu_fw_imx6d.bin",
3124             "v4l-coda960-imx6dl.bin"
3125         },
3126         .product      = CODA_960,
3127         .codecs       = coda9_codecs,
3128         .num_codecs   = ARRAY_SIZE(coda9_codecs),
3129         .vdevs        = coda9_video_devices,
3130         .num_vdevs    = ARRAY_SIZE(coda9_video_devices),
3131         .workbuf_size = 80 * 1024,
3132         .tempbuf_size = 204 * 1024,
3133         .iram_size    = 0x1f000, /* leave 4k for suspend code */
3134     },
3135 };
3136 
3137 static const struct of_device_id coda_dt_ids[] = {
3138     { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
3139     { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
3140     { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
3141     { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3142     { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
3143     { /* sentinel */ }
3144 };
3145 MODULE_DEVICE_TABLE(of, coda_dt_ids);
3146 
3147 static int coda_probe(struct platform_device *pdev)
3148 {
3149     struct device_node *np = pdev->dev.of_node;
3150     struct gen_pool *pool;
3151     struct coda_dev *dev;
3152     int ret, irq;
3153 
3154     dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3155     if (!dev)
3156         return -ENOMEM;
3157 
3158     dev->devtype = of_device_get_match_data(&pdev->dev);
3159 
3160     dev->dev = &pdev->dev;
3161     dev->clk_per = devm_clk_get(&pdev->dev, "per");
3162     if (IS_ERR(dev->clk_per)) {
3163         dev_err(&pdev->dev, "Could not get per clock\n");
3164         return PTR_ERR(dev->clk_per);
3165     }
3166 
3167     dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3168     if (IS_ERR(dev->clk_ahb)) {
3169         dev_err(&pdev->dev, "Could not get ahb clock\n");
3170         return PTR_ERR(dev->clk_ahb);
3171     }
3172 
3173     /* Get  memory for physical registers */
3174     dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
3175     if (IS_ERR(dev->regs_base))
3176         return PTR_ERR(dev->regs_base);
3177 
3178     /* IRQ */
3179     irq = platform_get_irq_byname(pdev, "bit");
3180     if (irq < 0)
3181         irq = platform_get_irq(pdev, 0);
3182     if (irq < 0)
3183         return irq;
3184 
3185     ret = devm_request_irq(&pdev->dev, irq, coda_irq_handler, 0,
3186                    CODA_NAME "-video", dev);
3187     if (ret < 0) {
3188         dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3189         return ret;
3190     }
3191 
3192     /* JPEG IRQ */
3193     if (dev->devtype->product == CODA_960) {
3194         irq = platform_get_irq_byname(pdev, "jpeg");
3195         if (irq < 0)
3196             return irq;
3197 
3198         ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
3199                         coda9_jpeg_irq_handler,
3200                         IRQF_ONESHOT, CODA_NAME "-jpeg",
3201                         dev);
3202         if (ret < 0) {
3203             dev_err(&pdev->dev, "failed to request jpeg irq\n");
3204             return ret;
3205         }
3206     }
3207 
3208     dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
3209                                   NULL);
3210     if (IS_ERR(dev->rstc)) {
3211         ret = PTR_ERR(dev->rstc);
3212         dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3213         return ret;
3214     }
3215 
3216     /* Get IRAM pool from device tree */
3217     pool = of_gen_pool_get(np, "iram", 0);
3218     if (!pool) {
3219         dev_err(&pdev->dev, "iram pool not available\n");
3220         return -ENOMEM;
3221     }
3222     dev->iram_pool = pool;
3223 
3224     /* Get vdoa_data if supported by the platform */
3225     dev->vdoa = coda_get_vdoa_data();
3226     if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
3227         return -EPROBE_DEFER;
3228 
3229     ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3230     if (ret)
3231         return ret;
3232 
3233     ratelimit_default_init(&dev->mb_err_rs);
3234     mutex_init(&dev->dev_mutex);
3235     mutex_init(&dev->coda_mutex);
3236     ida_init(&dev->ida);
3237 
3238     dev->debugfs_root = debugfs_create_dir("coda", NULL);
3239 
3240     /* allocate auxiliary per-device buffers for the BIT processor */
3241     if (dev->devtype->product == CODA_DX6) {
3242         ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3243                      dev->devtype->workbuf_size, "workbuf",
3244                      dev->debugfs_root);
3245         if (ret < 0)
3246             goto err_v4l2_register;
3247     }
3248 
3249     if (dev->devtype->tempbuf_size) {
3250         ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3251                      dev->devtype->tempbuf_size, "tempbuf",
3252                      dev->debugfs_root);
3253         if (ret < 0)
3254             goto err_v4l2_register;
3255     }
3256 
3257     dev->iram.size = dev->devtype->iram_size;
3258     dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3259                          &dev->iram.paddr);
3260     if (!dev->iram.vaddr) {
3261         dev_warn(&pdev->dev, "unable to alloc iram\n");
3262     } else {
3263         memset(dev->iram.vaddr, 0, dev->iram.size);
3264         dev->iram.blob.data = dev->iram.vaddr;
3265         dev->iram.blob.size = dev->iram.size;
3266         dev->iram.dentry = debugfs_create_blob("iram", 0444,
3267                                dev->debugfs_root,
3268                                &dev->iram.blob);
3269     }
3270 
3271     dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3272     if (!dev->workqueue) {
3273         dev_err(&pdev->dev, "unable to alloc workqueue\n");
3274         ret = -ENOMEM;
3275         goto err_v4l2_register;
3276     }
3277 
3278     platform_set_drvdata(pdev, dev);
3279 
3280     /*
3281      * Start activated so we can directly call coda_hw_init in
3282      * coda_fw_callback regardless of whether CONFIG_PM is
3283      * enabled or whether the device is associated with a PM domain.
3284      */
3285     pm_runtime_get_noresume(&pdev->dev);
3286     pm_runtime_set_active(&pdev->dev);
3287     pm_runtime_enable(&pdev->dev);
3288 
3289     ret = coda_firmware_request(dev);
3290     if (ret)
3291         goto err_alloc_workqueue;
3292     return 0;
3293 
3294 err_alloc_workqueue:
3295     pm_runtime_disable(&pdev->dev);
3296     pm_runtime_put_noidle(&pdev->dev);
3297     destroy_workqueue(dev->workqueue);
3298 err_v4l2_register:
3299     v4l2_device_unregister(&dev->v4l2_dev);
3300     return ret;
3301 }
3302 
3303 static int coda_remove(struct platform_device *pdev)
3304 {
3305     struct coda_dev *dev = platform_get_drvdata(pdev);
3306     int i;
3307 
3308     for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
3309         if (video_get_drvdata(&dev->vfd[i]))
3310             video_unregister_device(&dev->vfd[i]);
3311     }
3312     if (dev->m2m_dev)
3313         v4l2_m2m_release(dev->m2m_dev);
3314     pm_runtime_disable(&pdev->dev);
3315     v4l2_device_unregister(&dev->v4l2_dev);
3316     destroy_workqueue(dev->workqueue);
3317     if (dev->iram.vaddr)
3318         gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3319                   dev->iram.size);
3320     coda_free_aux_buf(dev, &dev->codebuf);
3321     coda_free_aux_buf(dev, &dev->tempbuf);
3322     coda_free_aux_buf(dev, &dev->workbuf);
3323     debugfs_remove_recursive(dev->debugfs_root);
3324     ida_destroy(&dev->ida);
3325     return 0;
3326 }
3327 
3328 #ifdef CONFIG_PM
3329 static int coda_runtime_resume(struct device *dev)
3330 {
3331     struct coda_dev *cdev = dev_get_drvdata(dev);
3332     int ret = 0;
3333 
3334     if (dev->pm_domain && cdev->codebuf.vaddr) {
3335         ret = coda_hw_init(cdev);
3336         if (ret)
3337             v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3338     }
3339 
3340     return ret;
3341 }
3342 #endif
3343 
3344 static const struct dev_pm_ops coda_pm_ops = {
3345     SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3346 };
3347 
3348 static struct platform_driver coda_driver = {
3349     .probe  = coda_probe,
3350     .remove = coda_remove,
3351     .driver = {
3352         .name   = CODA_NAME,
3353         .of_match_table = coda_dt_ids,
3354         .pm = &coda_pm_ops,
3355     },
3356 };
3357 
3358 module_platform_driver(coda_driver);
3359 
3360 MODULE_LICENSE("GPL");
3361 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3362 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");