Merge tag 'amd-drm-fixes-5.18-2022-04-06' of https://gitlab.freedesktop.org/agd5f...
[platform/kernel/linux-starfive.git] / drivers / media / platform / chips-media / coda-common.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Coda multi-standard codec IP
4  *
5  * Copyright (C) 2012 Vista Silicon S.L.
6  *    Javier Martin, <javier.martin@vista-silicon.com>
7  *    Xavier Duret
8  */
9
10 #include <linux/clk.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/gcd.h>
15 #include <linux/genalloc.h>
16 #include <linux/idr.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/irq.h>
20 #include <linux/kfifo.h>
21 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
27 #include <linux/of.h>
28 #include <linux/ratelimit.h>
29 #include <linux/reset.h>
30
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-event.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-mem2mem.h>
36 #include <media/videobuf2-v4l2.h>
37 #include <media/videobuf2-dma-contig.h>
38 #include <media/videobuf2-vmalloc.h>
39
40 #include "coda.h"
41 #include "imx-vdoa.h"
42
43 #define CODA_NAME               "coda"
44
45 #define CODADX6_MAX_INSTANCES   4
46 #define CODA_MAX_FORMATS        5
47
48 #define CODA_ISRAM_SIZE (2048 * 2)
49
50 #define MIN_W 48
51 #define MIN_H 16
52
53 #define S_ALIGN         1 /* multiple of 2 */
54 #define W_ALIGN         1 /* multiple of 2 */
55 #define H_ALIGN         1 /* multiple of 2 */
56
57 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
58
59 int coda_debug;
60 module_param(coda_debug, int, 0644);
61 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
62
63 static int disable_tiling;
64 module_param(disable_tiling, int, 0644);
65 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
66
67 static int disable_vdoa;
68 module_param(disable_vdoa, int, 0644);
69 MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
70
71 static int enable_bwb = 0;
72 module_param(enable_bwb, int, 0644);
73 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
74
75 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
76 {
77         v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
78                  "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
79         writel(data, dev->regs_base + reg);
80 }
81
82 unsigned int coda_read(struct coda_dev *dev, u32 reg)
83 {
84         u32 data;
85
86         data = readl(dev->regs_base + reg);
87         v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
88                  "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
89         return data;
90 }
91
92 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
93                      struct vb2_v4l2_buffer *buf, unsigned int reg_y)
94 {
95         u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
96         u32 base_cb, base_cr;
97
98         switch (q_data->fourcc) {
99         case V4L2_PIX_FMT_YUYV:
100                 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
101         case V4L2_PIX_FMT_NV12:
102         case V4L2_PIX_FMT_YUV420:
103         default:
104                 base_cb = base_y + q_data->bytesperline * q_data->height;
105                 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
106                 break;
107         case V4L2_PIX_FMT_YVU420:
108                 /* Switch Cb and Cr for YVU420 format */
109                 base_cr = base_y + q_data->bytesperline * q_data->height;
110                 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
111                 break;
112         case V4L2_PIX_FMT_YUV422P:
113                 base_cb = base_y + q_data->bytesperline * q_data->height;
114                 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
115         }
116
117         coda_write(ctx->dev, base_y, reg_y);
118         coda_write(ctx->dev, base_cb, reg_y + 4);
119         coda_write(ctx->dev, base_cr, reg_y + 8);
120 }
121
122 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
123         { mode, src_fourcc, dst_fourcc, max_w, max_h }
124
125 /*
126  * Arrays of codecs supported by each given version of Coda:
127  *  i.MX27 -> codadx6
128  *  i.MX51 -> codahx4
129  *  i.MX53 -> coda7
130  *  i.MX6  -> coda960
131  * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
132  */
133 static const struct coda_codec codadx6_codecs[] = {
134         CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,  720, 576),
135         CODA_CODEC(CODADX6_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
136 };
137
138 static const struct coda_codec codahx4_codecs[] = {
139         CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   720, 576),
140         CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
141         CODA_CODEC(CODA7_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
142         CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1280, 720),
143 };
144
145 static const struct coda_codec coda7_codecs[] = {
146         CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1280, 720),
147         CODA_CODEC(CODA7_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1280, 720),
148         CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
149         CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
150         CODA_CODEC(CODA7_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
151         CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
152         CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,   V4L2_PIX_FMT_YUV420, 8192, 8192),
153 };
154
155 static const struct coda_codec coda9_codecs[] = {
156         CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1920, 1088),
157         CODA_CODEC(CODA9_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1920, 1088),
158         CODA_CODEC(CODA9_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
159         CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
160         CODA_CODEC(CODA9_MODE_DECODE_MP2,  V4L2_PIX_FMT_MPEG2,  V4L2_PIX_FMT_YUV420, 1920, 1088),
161         CODA_CODEC(CODA9_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
162         CODA_CODEC(CODA9_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,   V4L2_PIX_FMT_YUV420, 8192, 8192),
163 };
164
165 struct coda_video_device {
166         const char *name;
167         enum coda_inst_type type;
168         const struct coda_context_ops *ops;
169         bool direct;
170         u32 src_formats[CODA_MAX_FORMATS];
171         u32 dst_formats[CODA_MAX_FORMATS];
172 };
173
174 static const struct coda_video_device coda_bit_encoder = {
175         .name = "coda-video-encoder",
176         .type = CODA_INST_ENCODER,
177         .ops = &coda_bit_encode_ops,
178         .src_formats = {
179                 V4L2_PIX_FMT_NV12,
180                 V4L2_PIX_FMT_YUV420,
181                 V4L2_PIX_FMT_YVU420,
182         },
183         .dst_formats = {
184                 V4L2_PIX_FMT_H264,
185                 V4L2_PIX_FMT_MPEG4,
186         },
187 };
188
189 static const struct coda_video_device coda_bit_jpeg_encoder = {
190         .name = "coda-jpeg-encoder",
191         .type = CODA_INST_ENCODER,
192         .ops = &coda_bit_encode_ops,
193         .src_formats = {
194                 V4L2_PIX_FMT_NV12,
195                 V4L2_PIX_FMT_YUV420,
196                 V4L2_PIX_FMT_YVU420,
197                 V4L2_PIX_FMT_YUV422P,
198         },
199         .dst_formats = {
200                 V4L2_PIX_FMT_JPEG,
201         },
202 };
203
204 static const struct coda_video_device coda_bit_decoder = {
205         .name = "coda-video-decoder",
206         .type = CODA_INST_DECODER,
207         .ops = &coda_bit_decode_ops,
208         .src_formats = {
209                 V4L2_PIX_FMT_H264,
210                 V4L2_PIX_FMT_MPEG2,
211                 V4L2_PIX_FMT_MPEG4,
212         },
213         .dst_formats = {
214                 V4L2_PIX_FMT_NV12,
215                 V4L2_PIX_FMT_YUV420,
216                 V4L2_PIX_FMT_YVU420,
217                 /*
218                  * If V4L2_PIX_FMT_YUYV should be default,
219                  * set_default_params() must be adjusted.
220                  */
221                 V4L2_PIX_FMT_YUYV,
222         },
223 };
224
225 static const struct coda_video_device coda_bit_jpeg_decoder = {
226         .name = "coda-jpeg-decoder",
227         .type = CODA_INST_DECODER,
228         .ops = &coda_bit_decode_ops,
229         .src_formats = {
230                 V4L2_PIX_FMT_JPEG,
231         },
232         .dst_formats = {
233                 V4L2_PIX_FMT_NV12,
234                 V4L2_PIX_FMT_YUV420,
235                 V4L2_PIX_FMT_YVU420,
236                 V4L2_PIX_FMT_YUV422P,
237         },
238 };
239
240 static const struct coda_video_device coda9_jpeg_encoder = {
241         .name = "coda-jpeg-encoder",
242         .type = CODA_INST_ENCODER,
243         .ops = &coda9_jpeg_encode_ops,
244         .direct = true,
245         .src_formats = {
246                 V4L2_PIX_FMT_NV12,
247                 V4L2_PIX_FMT_YUV420,
248                 V4L2_PIX_FMT_YVU420,
249                 V4L2_PIX_FMT_YUV422P,
250                 V4L2_PIX_FMT_GREY,
251         },
252         .dst_formats = {
253                 V4L2_PIX_FMT_JPEG,
254         },
255 };
256
257 static const struct coda_video_device coda9_jpeg_decoder = {
258         .name = "coda-jpeg-decoder",
259         .type = CODA_INST_DECODER,
260         .ops = &coda9_jpeg_decode_ops,
261         .direct = true,
262         .src_formats = {
263                 V4L2_PIX_FMT_JPEG,
264         },
265         .dst_formats = {
266                 V4L2_PIX_FMT_NV12,
267                 V4L2_PIX_FMT_YUV420,
268                 V4L2_PIX_FMT_YVU420,
269                 V4L2_PIX_FMT_YUV422P,
270         },
271 };
272
273 static const struct coda_video_device *codadx6_video_devices[] = {
274         &coda_bit_encoder,
275 };
276
277 static const struct coda_video_device *codahx4_video_devices[] = {
278         &coda_bit_encoder,
279         &coda_bit_decoder,
280 };
281
282 static const struct coda_video_device *coda7_video_devices[] = {
283         &coda_bit_jpeg_encoder,
284         &coda_bit_jpeg_decoder,
285         &coda_bit_encoder,
286         &coda_bit_decoder,
287 };
288
289 static const struct coda_video_device *coda9_video_devices[] = {
290         &coda9_jpeg_encoder,
291         &coda9_jpeg_decoder,
292         &coda_bit_encoder,
293         &coda_bit_decoder,
294 };
295
296 /*
297  * Normalize all supported YUV 4:2:0 formats to the value used in the codec
298  * tables.
299  */
300 static u32 coda_format_normalize_yuv(u32 fourcc)
301 {
302         switch (fourcc) {
303         case V4L2_PIX_FMT_NV12:
304         case V4L2_PIX_FMT_YUV420:
305         case V4L2_PIX_FMT_YVU420:
306         case V4L2_PIX_FMT_YUV422P:
307         case V4L2_PIX_FMT_YUYV:
308                 return V4L2_PIX_FMT_YUV420;
309         default:
310                 return fourcc;
311         }
312 }
313
314 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
315                                                 int src_fourcc, int dst_fourcc)
316 {
317         const struct coda_codec *codecs = dev->devtype->codecs;
318         int num_codecs = dev->devtype->num_codecs;
319         int k;
320
321         src_fourcc = coda_format_normalize_yuv(src_fourcc);
322         dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
323         if (src_fourcc == dst_fourcc)
324                 return NULL;
325
326         for (k = 0; k < num_codecs; k++) {
327                 if (codecs[k].src_fourcc == src_fourcc &&
328                     codecs[k].dst_fourcc == dst_fourcc)
329                         break;
330         }
331
332         if (k == num_codecs)
333                 return NULL;
334
335         return &codecs[k];
336 }
337
338 static void coda_get_max_dimensions(struct coda_dev *dev,
339                                     const struct coda_codec *codec,
340                                     int *max_w, int *max_h)
341 {
342         const struct coda_codec *codecs = dev->devtype->codecs;
343         int num_codecs = dev->devtype->num_codecs;
344         unsigned int w, h;
345         int k;
346
347         if (codec) {
348                 w = codec->max_w;
349                 h = codec->max_h;
350         } else {
351                 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
352                         w = max(w, codecs[k].max_w);
353                         h = max(h, codecs[k].max_h);
354                 }
355         }
356
357         if (max_w)
358                 *max_w = w;
359         if (max_h)
360                 *max_h = h;
361 }
362
363 static const struct coda_video_device *to_coda_video_device(struct video_device
364                                                             *vdev)
365 {
366         struct coda_dev *dev = video_get_drvdata(vdev);
367         unsigned int i = vdev - dev->vfd;
368
369         if (i >= dev->devtype->num_vdevs)
370                 return NULL;
371
372         return dev->devtype->vdevs[i];
373 }
374
375 const char *coda_product_name(int product)
376 {
377         static char buf[9];
378
379         switch (product) {
380         case CODA_DX6:
381                 return "CodaDx6";
382         case CODA_HX4:
383                 return "CodaHx4";
384         case CODA_7541:
385                 return "CODA7541";
386         case CODA_960:
387                 return "CODA960";
388         default:
389                 snprintf(buf, sizeof(buf), "(0x%04x)", product);
390                 return buf;
391         }
392 }
393
394 static struct vdoa_data *coda_get_vdoa_data(void)
395 {
396         struct device_node *vdoa_node;
397         struct platform_device *vdoa_pdev;
398         struct vdoa_data *vdoa_data = NULL;
399
400         vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
401         if (!vdoa_node)
402                 return NULL;
403
404         vdoa_pdev = of_find_device_by_node(vdoa_node);
405         if (!vdoa_pdev)
406                 goto out;
407
408         vdoa_data = platform_get_drvdata(vdoa_pdev);
409         if (!vdoa_data)
410                 vdoa_data = ERR_PTR(-EPROBE_DEFER);
411
412         put_device(&vdoa_pdev->dev);
413 out:
414         of_node_put(vdoa_node);
415
416         return vdoa_data;
417 }
418
419 /*
420  * V4L2 ioctl() operations.
421  */
422 static int coda_querycap(struct file *file, void *priv,
423                          struct v4l2_capability *cap)
424 {
425         struct coda_ctx *ctx = fh_to_ctx(priv);
426
427         strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
428         strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
429                 sizeof(cap->card));
430         strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
431         return 0;
432 }
433
434 static const u32 coda_formats_420[CODA_MAX_FORMATS] = {
435                 V4L2_PIX_FMT_NV12,
436                 V4L2_PIX_FMT_YUV420,
437                 V4L2_PIX_FMT_YVU420,
438 };
439
440 static int coda_enum_fmt(struct file *file, void *priv,
441                          struct v4l2_fmtdesc *f)
442 {
443         struct video_device *vdev = video_devdata(file);
444         const struct coda_video_device *cvd = to_coda_video_device(vdev);
445         struct coda_ctx *ctx = fh_to_ctx(priv);
446         const u32 *formats;
447
448         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
449                 formats = cvd->src_formats;
450         else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
451                 struct coda_q_data *q_data_src;
452                 struct vb2_queue *src_vq;
453
454                 formats = cvd->dst_formats;
455
456                 /*
457                  * If the source format is already fixed, only allow the same
458                  * chroma subsampling.
459                  */
460                 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
461                 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
462                                          V4L2_BUF_TYPE_VIDEO_OUTPUT);
463                 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
464                     vb2_is_streaming(src_vq)) {
465                         if (ctx->params.jpeg_chroma_subsampling ==
466                             V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
467                                 formats = coda_formats_420;
468                         } else if (ctx->params.jpeg_chroma_subsampling ==
469                                    V4L2_JPEG_CHROMA_SUBSAMPLING_422) {
470                                 f->pixelformat = V4L2_PIX_FMT_YUV422P;
471                                 return f->index ? -EINVAL : 0;
472                         }
473                 }
474         } else {
475                 return -EINVAL;
476         }
477
478         if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
479                 return -EINVAL;
480
481         /* Skip YUYV if the vdoa is not available */
482         if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
483             formats[f->index] == V4L2_PIX_FMT_YUYV)
484                 return -EINVAL;
485
486         f->pixelformat = formats[f->index];
487
488         return 0;
489 }
490
491 static int coda_g_fmt(struct file *file, void *priv,
492                       struct v4l2_format *f)
493 {
494         struct coda_q_data *q_data;
495         struct coda_ctx *ctx = fh_to_ctx(priv);
496
497         q_data = get_q_data(ctx, f->type);
498         if (!q_data)
499                 return -EINVAL;
500
501         f->fmt.pix.field        = V4L2_FIELD_NONE;
502         f->fmt.pix.pixelformat  = q_data->fourcc;
503         f->fmt.pix.width        = q_data->width;
504         f->fmt.pix.height       = q_data->height;
505         f->fmt.pix.bytesperline = q_data->bytesperline;
506
507         f->fmt.pix.sizeimage    = q_data->sizeimage;
508         f->fmt.pix.colorspace   = ctx->colorspace;
509         f->fmt.pix.xfer_func    = ctx->xfer_func;
510         f->fmt.pix.ycbcr_enc    = ctx->ycbcr_enc;
511         f->fmt.pix.quantization = ctx->quantization;
512
513         return 0;
514 }
515
516 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
517 {
518         struct coda_q_data *q_data;
519         const u32 *formats;
520         int i;
521
522         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
523                 formats = ctx->cvd->src_formats;
524         else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
525                 formats = ctx->cvd->dst_formats;
526         else
527                 return -EINVAL;
528
529         for (i = 0; i < CODA_MAX_FORMATS; i++) {
530                 /* Skip YUYV if the vdoa is not available */
531                 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
532                     formats[i] == V4L2_PIX_FMT_YUYV)
533                         continue;
534
535                 if (formats[i] == f->fmt.pix.pixelformat) {
536                         f->fmt.pix.pixelformat = formats[i];
537                         return 0;
538                 }
539         }
540
541         /* Fall back to currently set pixelformat */
542         q_data = get_q_data(ctx, f->type);
543         f->fmt.pix.pixelformat = q_data->fourcc;
544
545         return 0;
546 }
547
548 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
549                              bool *use_vdoa)
550 {
551         int err;
552
553         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
554                 return -EINVAL;
555
556         if (!use_vdoa)
557                 return -EINVAL;
558
559         if (!ctx->vdoa) {
560                 *use_vdoa = false;
561                 return 0;
562         }
563
564         err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
565                                      f->fmt.pix.height, f->fmt.pix.pixelformat);
566         if (err) {
567                 *use_vdoa = false;
568                 return 0;
569         }
570
571         *use_vdoa = true;
572         return 0;
573 }
574
575 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
576                                             u32 width, u32 height)
577 {
578         /*
579          * This is a rough estimate for sensible compressed buffer
580          * sizes (between 1 and 16 bits per pixel). This could be
581          * improved by better format specific worst case estimates.
582          */
583         return round_up(clamp(sizeimage, width * height / 8,
584                                          width * height * 2), PAGE_SIZE);
585 }
586
587 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
588                         struct v4l2_format *f)
589 {
590         struct coda_dev *dev = ctx->dev;
591         unsigned int max_w, max_h;
592         enum v4l2_field field;
593
594         field = f->fmt.pix.field;
595         if (field == V4L2_FIELD_ANY)
596                 field = V4L2_FIELD_NONE;
597         else if (V4L2_FIELD_NONE != field)
598                 return -EINVAL;
599
600         /* V4L2 specification suggests the driver corrects the format struct
601          * if any of the dimensions is unsupported */
602         f->fmt.pix.field = field;
603
604         coda_get_max_dimensions(dev, codec, &max_w, &max_h);
605         v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
606                               &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
607                               S_ALIGN);
608
609         switch (f->fmt.pix.pixelformat) {
610         case V4L2_PIX_FMT_NV12:
611         case V4L2_PIX_FMT_YUV420:
612         case V4L2_PIX_FMT_YVU420:
613                 /*
614                  * Frame stride must be at least multiple of 8,
615                  * but multiple of 16 for h.264 or JPEG 4:2:x
616                  */
617                 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
618                 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
619                                         f->fmt.pix.height * 3 / 2;
620                 break;
621         case V4L2_PIX_FMT_YUYV:
622                 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
623                 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
624                                         f->fmt.pix.height;
625                 break;
626         case V4L2_PIX_FMT_YUV422P:
627                 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
628                 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
629                                         f->fmt.pix.height * 2;
630                 break;
631         case V4L2_PIX_FMT_GREY:
632                 /* keep 16 pixel alignment of 8-bit pixel data */
633                 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
634                 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
635                 break;
636         case V4L2_PIX_FMT_JPEG:
637         case V4L2_PIX_FMT_H264:
638         case V4L2_PIX_FMT_MPEG4:
639         case V4L2_PIX_FMT_MPEG2:
640                 f->fmt.pix.bytesperline = 0;
641                 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
642                                                         f->fmt.pix.sizeimage,
643                                                         f->fmt.pix.width,
644                                                         f->fmt.pix.height);
645                 break;
646         default:
647                 BUG();
648         }
649
650         return 0;
651 }
652
653 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
654                                 struct v4l2_format *f)
655 {
656         struct coda_ctx *ctx = fh_to_ctx(priv);
657         const struct coda_q_data *q_data_src;
658         const struct coda_codec *codec;
659         struct vb2_queue *src_vq;
660         int ret;
661         bool use_vdoa;
662
663         ret = coda_try_pixelformat(ctx, f);
664         if (ret < 0)
665                 return ret;
666
667         q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
668
669         /*
670          * If the source format is already fixed, only allow the same output
671          * resolution. When decoding JPEG images, we also have to make sure to
672          * use the same chroma subsampling.
673          */
674         src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
675         if (vb2_is_streaming(src_vq)) {
676                 f->fmt.pix.width = q_data_src->width;
677                 f->fmt.pix.height = q_data_src->height;
678
679                 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
680                         if (ctx->params.jpeg_chroma_subsampling ==
681                             V4L2_JPEG_CHROMA_SUBSAMPLING_420 &&
682                             f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P)
683                                 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
684                         else if (ctx->params.jpeg_chroma_subsampling ==
685                                  V4L2_JPEG_CHROMA_SUBSAMPLING_422)
686                                 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
687                 }
688         }
689
690         f->fmt.pix.colorspace = ctx->colorspace;
691         f->fmt.pix.xfer_func = ctx->xfer_func;
692         f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
693         f->fmt.pix.quantization = ctx->quantization;
694
695         q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
696         codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
697                                 f->fmt.pix.pixelformat);
698         if (!codec)
699                 return -EINVAL;
700
701         ret = coda_try_fmt(ctx, codec, f);
702         if (ret < 0)
703                 return ret;
704
705         /* The decoders always write complete macroblocks or MCUs */
706         if (ctx->inst_type == CODA_INST_DECODER) {
707                 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
708                 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
709                 if (codec->src_fourcc == V4L2_PIX_FMT_JPEG &&
710                     f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P) {
711                         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
712                                                f->fmt.pix.height * 2;
713                 } else {
714                         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
715                                                f->fmt.pix.height * 3 / 2;
716                 }
717
718                 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
719                 if (ret < 0)
720                         return ret;
721
722                 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
723                         if (!use_vdoa)
724                                 return -EINVAL;
725
726                         f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
727                         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
728                                 f->fmt.pix.height;
729                 }
730         }
731
732         return 0;
733 }
734
735 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
736 {
737         enum v4l2_colorspace colorspace;
738
739         if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
740                 colorspace = V4L2_COLORSPACE_JPEG;
741         else if (fmt->width <= 720 && fmt->height <= 576)
742                 colorspace = V4L2_COLORSPACE_SMPTE170M;
743         else
744                 colorspace = V4L2_COLORSPACE_REC709;
745
746         fmt->colorspace = colorspace;
747         fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
748         fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
749         fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
750 }
751
752 static int coda_try_fmt_vid_out(struct file *file, void *priv,
753                                 struct v4l2_format *f)
754 {
755         struct coda_ctx *ctx = fh_to_ctx(priv);
756         struct coda_dev *dev = ctx->dev;
757         const struct coda_q_data *q_data_dst;
758         const struct coda_codec *codec;
759         int ret;
760
761         ret = coda_try_pixelformat(ctx, f);
762         if (ret < 0)
763                 return ret;
764
765         if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
766                 coda_set_default_colorspace(&f->fmt.pix);
767
768         q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
769         codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
770
771         return coda_try_fmt(ctx, codec, f);
772 }
773
774 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
775                       struct v4l2_rect *r)
776 {
777         struct coda_q_data *q_data;
778         struct vb2_queue *vq;
779
780         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
781         if (!vq)
782                 return -EINVAL;
783
784         q_data = get_q_data(ctx, f->type);
785         if (!q_data)
786                 return -EINVAL;
787
788         if (vb2_is_busy(vq)) {
789                 v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
790                          __func__, v4l2_type_names[f->type], vq->num_buffers);
791                 return -EBUSY;
792         }
793
794         q_data->fourcc = f->fmt.pix.pixelformat;
795         q_data->width = f->fmt.pix.width;
796         q_data->height = f->fmt.pix.height;
797         q_data->bytesperline = f->fmt.pix.bytesperline;
798         q_data->sizeimage = f->fmt.pix.sizeimage;
799         if (r) {
800                 q_data->rect = *r;
801         } else {
802                 q_data->rect.left = 0;
803                 q_data->rect.top = 0;
804                 q_data->rect.width = f->fmt.pix.width;
805                 q_data->rect.height = f->fmt.pix.height;
806         }
807
808         switch (f->fmt.pix.pixelformat) {
809         case V4L2_PIX_FMT_YUYV:
810                 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
811                 break;
812         case V4L2_PIX_FMT_NV12:
813                 if (!disable_tiling && ctx->use_bit &&
814                     ctx->dev->devtype->product == CODA_960) {
815                         ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
816                         break;
817                 }
818                 fallthrough;
819         case V4L2_PIX_FMT_YUV420:
820         case V4L2_PIX_FMT_YVU420:
821         case V4L2_PIX_FMT_YUV422P:
822                 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
823                 break;
824         default:
825                 break;
826         }
827
828         if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
829             !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
830             ctx->use_vdoa)
831                 vdoa_context_configure(ctx->vdoa,
832                                        round_up(f->fmt.pix.width, 16),
833                                        f->fmt.pix.height,
834                                        f->fmt.pix.pixelformat);
835         else
836                 ctx->use_vdoa = false;
837
838         coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
839                  v4l2_type_names[f->type], q_data->width, q_data->height,
840                  (char *)&q_data->fourcc,
841                  (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
842
843         return 0;
844 }
845
846 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
847                               struct v4l2_format *f)
848 {
849         struct coda_ctx *ctx = fh_to_ctx(priv);
850         struct coda_q_data *q_data_src;
851         const struct coda_codec *codec;
852         struct v4l2_rect r;
853         int ret;
854
855         ret = coda_try_fmt_vid_cap(file, priv, f);
856         if (ret)
857                 return ret;
858
859         q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
860         r.left = 0;
861         r.top = 0;
862         r.width = q_data_src->width;
863         r.height = q_data_src->height;
864
865         ret = coda_s_fmt(ctx, f, &r);
866         if (ret)
867                 return ret;
868
869         if (ctx->inst_type != CODA_INST_ENCODER)
870                 return 0;
871
872         /* Setting the coded format determines the selected codec */
873         codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
874                                 f->fmt.pix.pixelformat);
875         if (!codec) {
876                 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
877                 return -EINVAL;
878         }
879         ctx->codec = codec;
880
881         ctx->colorspace = f->fmt.pix.colorspace;
882         ctx->xfer_func = f->fmt.pix.xfer_func;
883         ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
884         ctx->quantization = f->fmt.pix.quantization;
885
886         return 0;
887 }
888
889 static int coda_s_fmt_vid_out(struct file *file, void *priv,
890                               struct v4l2_format *f)
891 {
892         struct coda_ctx *ctx = fh_to_ctx(priv);
893         const struct coda_codec *codec;
894         struct v4l2_format f_cap;
895         struct vb2_queue *dst_vq;
896         int ret;
897
898         ret = coda_try_fmt_vid_out(file, priv, f);
899         if (ret)
900                 return ret;
901
902         ret = coda_s_fmt(ctx, f, NULL);
903         if (ret)
904                 return ret;
905
906         ctx->colorspace = f->fmt.pix.colorspace;
907         ctx->xfer_func = f->fmt.pix.xfer_func;
908         ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
909         ctx->quantization = f->fmt.pix.quantization;
910
911         if (ctx->inst_type != CODA_INST_DECODER)
912                 return 0;
913
914         /* Setting the coded format determines the selected codec */
915         codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
916                                 V4L2_PIX_FMT_YUV420);
917         if (!codec) {
918                 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
919                 return -EINVAL;
920         }
921         ctx->codec = codec;
922
923         dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
924         if (!dst_vq)
925                 return -EINVAL;
926
927         /*
928          * Setting the capture queue format is not possible while the capture
929          * queue is still busy. This is not an error, but the user will have to
930          * make sure themselves that the capture format is set correctly before
931          * starting the output queue again.
932          */
933         if (vb2_is_busy(dst_vq))
934                 return 0;
935
936         memset(&f_cap, 0, sizeof(f_cap));
937         f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
938         coda_g_fmt(file, priv, &f_cap);
939         f_cap.fmt.pix.width = f->fmt.pix.width;
940         f_cap.fmt.pix.height = f->fmt.pix.height;
941
942         return coda_s_fmt_vid_cap(file, priv, &f_cap);
943 }
944
945 static int coda_reqbufs(struct file *file, void *priv,
946                         struct v4l2_requestbuffers *rb)
947 {
948         struct coda_ctx *ctx = fh_to_ctx(priv);
949         int ret;
950
951         ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
952         if (ret)
953                 return ret;
954
955         /*
956          * Allow to allocate instance specific per-context buffers, such as
957          * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
958          */
959         if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
960                 return ctx->ops->reqbufs(ctx, rb);
961
962         return 0;
963 }
964
965 static int coda_qbuf(struct file *file, void *priv,
966                      struct v4l2_buffer *buf)
967 {
968         struct coda_ctx *ctx = fh_to_ctx(priv);
969
970         if (ctx->inst_type == CODA_INST_DECODER &&
971             buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
972                 buf->flags &= ~V4L2_BUF_FLAG_LAST;
973
974         return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
975 }
976
977 static int coda_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
978 {
979         struct coda_ctx *ctx = fh_to_ctx(priv);
980         int ret;
981
982         ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
983
984         if (ctx->inst_type == CODA_INST_DECODER &&
985             buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
986                 buf->flags &= ~V4L2_BUF_FLAG_LAST;
987
988         return ret;
989 }
990
991 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
992                        enum vb2_buffer_state state)
993 {
994         const struct v4l2_event eos_event = {
995                 .type = V4L2_EVENT_EOS
996         };
997
998         if (buf->flags & V4L2_BUF_FLAG_LAST)
999                 v4l2_event_queue_fh(&ctx->fh, &eos_event);
1000
1001         v4l2_m2m_buf_done(buf, state);
1002 }
1003
1004 static int coda_g_selection(struct file *file, void *fh,
1005                             struct v4l2_selection *s)
1006 {
1007         struct coda_ctx *ctx = fh_to_ctx(fh);
1008         struct coda_q_data *q_data;
1009         struct v4l2_rect r, *rsel;
1010
1011         q_data = get_q_data(ctx, s->type);
1012         if (!q_data)
1013                 return -EINVAL;
1014
1015         r.left = 0;
1016         r.top = 0;
1017         r.width = q_data->width;
1018         r.height = q_data->height;
1019         rsel = &q_data->rect;
1020
1021         switch (s->target) {
1022         case V4L2_SEL_TGT_CROP_DEFAULT:
1023         case V4L2_SEL_TGT_CROP_BOUNDS:
1024                 rsel = &r;
1025                 fallthrough;
1026         case V4L2_SEL_TGT_CROP:
1027                 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
1028                     ctx->inst_type == CODA_INST_DECODER)
1029                         return -EINVAL;
1030                 break;
1031         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1032         case V4L2_SEL_TGT_COMPOSE_PADDED:
1033                 rsel = &r;
1034                 fallthrough;
1035         case V4L2_SEL_TGT_COMPOSE:
1036         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1037                 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1038                     ctx->inst_type == CODA_INST_ENCODER)
1039                         return -EINVAL;
1040                 break;
1041         default:
1042                 return -EINVAL;
1043         }
1044
1045         s->r = *rsel;
1046
1047         return 0;
1048 }
1049
1050 static int coda_s_selection(struct file *file, void *fh,
1051                             struct v4l2_selection *s)
1052 {
1053         struct coda_ctx *ctx = fh_to_ctx(fh);
1054         struct coda_q_data *q_data;
1055
1056         switch (s->target) {
1057         case V4L2_SEL_TGT_CROP:
1058                 if (ctx->inst_type == CODA_INST_ENCODER &&
1059                     s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1060                         q_data = get_q_data(ctx, s->type);
1061                         if (!q_data)
1062                                 return -EINVAL;
1063
1064                         s->r.left = 0;
1065                         s->r.top = 0;
1066                         s->r.width = clamp(s->r.width, 2U, q_data->width);
1067                         s->r.height = clamp(s->r.height, 2U, q_data->height);
1068
1069                         if (s->flags & V4L2_SEL_FLAG_LE) {
1070                                 s->r.width = round_up(s->r.width, 2);
1071                                 s->r.height = round_up(s->r.height, 2);
1072                         } else {
1073                                 s->r.width = round_down(s->r.width, 2);
1074                                 s->r.height = round_down(s->r.height, 2);
1075                         }
1076
1077                         q_data->rect = s->r;
1078
1079                         coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
1080                                  s->r.width, s->r.height);
1081
1082                         return 0;
1083                 }
1084                 fallthrough;
1085         case V4L2_SEL_TGT_NATIVE_SIZE:
1086         case V4L2_SEL_TGT_COMPOSE:
1087                 return coda_g_selection(file, fh, s);
1088         default:
1089                 /* v4l2-compliance expects this to fail for read-only targets */
1090                 return -EINVAL;
1091         }
1092 }
1093
1094 static int coda_try_encoder_cmd(struct file *file, void *fh,
1095                                 struct v4l2_encoder_cmd *ec)
1096 {
1097         struct coda_ctx *ctx = fh_to_ctx(fh);
1098
1099         if (ctx->inst_type != CODA_INST_ENCODER)
1100                 return -ENOTTY;
1101
1102         return v4l2_m2m_ioctl_try_encoder_cmd(file, fh, ec);
1103 }
1104
1105 static void coda_wake_up_capture_queue(struct coda_ctx *ctx)
1106 {
1107         struct vb2_queue *dst_vq;
1108
1109         coda_dbg(1, ctx, "waking up capture queue\n");
1110
1111         dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1112         dst_vq->last_buffer_dequeued = true;
1113         wake_up(&dst_vq->done_wq);
1114 }
1115
1116 static int coda_encoder_cmd(struct file *file, void *fh,
1117                             struct v4l2_encoder_cmd *ec)
1118 {
1119         struct coda_ctx *ctx = fh_to_ctx(fh);
1120         struct vb2_v4l2_buffer *buf;
1121         int ret;
1122
1123         ret = coda_try_encoder_cmd(file, fh, ec);
1124         if (ret < 0)
1125                 return ret;
1126
1127         mutex_lock(&ctx->wakeup_mutex);
1128         buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1129         if (buf) {
1130                 /*
1131                  * If the last output buffer is still on the queue, make sure
1132                  * that decoder finish_run will see the last flag and report it
1133                  * to userspace.
1134                  */
1135                 buf->flags |= V4L2_BUF_FLAG_LAST;
1136         } else {
1137                 /* Set the stream-end flag on this context */
1138                 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1139
1140                 /*
1141                  * If the last output buffer has already been taken from the
1142                  * queue, wake up the capture queue and signal end of stream
1143                  * via the -EPIPE mechanism.
1144                  */
1145                 coda_wake_up_capture_queue(ctx);
1146         }
1147         mutex_unlock(&ctx->wakeup_mutex);
1148
1149         return 0;
1150 }
1151
1152 static int coda_try_decoder_cmd(struct file *file, void *fh,
1153                                 struct v4l2_decoder_cmd *dc)
1154 {
1155         struct coda_ctx *ctx = fh_to_ctx(fh);
1156
1157         if (ctx->inst_type != CODA_INST_DECODER)
1158                 return -ENOTTY;
1159
1160         return v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
1161 }
1162
1163 static bool coda_mark_last_meta(struct coda_ctx *ctx)
1164 {
1165         struct coda_buffer_meta *meta;
1166
1167         coda_dbg(1, ctx, "marking last meta\n");
1168
1169         spin_lock(&ctx->buffer_meta_lock);
1170         if (list_empty(&ctx->buffer_meta_list)) {
1171                 spin_unlock(&ctx->buffer_meta_lock);
1172                 return false;
1173         }
1174
1175         meta = list_last_entry(&ctx->buffer_meta_list, struct coda_buffer_meta,
1176                                list);
1177         meta->last = true;
1178
1179         spin_unlock(&ctx->buffer_meta_lock);
1180         return true;
1181 }
1182
1183 static bool coda_mark_last_dst_buf(struct coda_ctx *ctx)
1184 {
1185         struct vb2_v4l2_buffer *buf;
1186         struct vb2_buffer *dst_vb;
1187         struct vb2_queue *dst_vq;
1188         unsigned long flags;
1189
1190         coda_dbg(1, ctx, "marking last capture buffer\n");
1191
1192         dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1193         spin_lock_irqsave(&dst_vq->done_lock, flags);
1194         if (list_empty(&dst_vq->done_list)) {
1195                 spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1196                 return false;
1197         }
1198
1199         dst_vb = list_last_entry(&dst_vq->done_list, struct vb2_buffer,
1200                                  done_entry);
1201         buf = to_vb2_v4l2_buffer(dst_vb);
1202         buf->flags |= V4L2_BUF_FLAG_LAST;
1203
1204         spin_unlock_irqrestore(&dst_vq->done_lock, flags);
1205         return true;
1206 }
1207
1208 static int coda_decoder_cmd(struct file *file, void *fh,
1209                             struct v4l2_decoder_cmd *dc)
1210 {
1211         struct coda_ctx *ctx = fh_to_ctx(fh);
1212         struct coda_dev *dev = ctx->dev;
1213         struct vb2_v4l2_buffer *buf;
1214         struct vb2_queue *dst_vq;
1215         bool stream_end;
1216         bool wakeup;
1217         int ret;
1218
1219         ret = coda_try_decoder_cmd(file, fh, dc);
1220         if (ret < 0)
1221                 return ret;
1222
1223         switch (dc->cmd) {
1224         case V4L2_DEC_CMD_START:
1225                 mutex_lock(&dev->coda_mutex);
1226                 mutex_lock(&ctx->bitstream_mutex);
1227                 coda_bitstream_flush(ctx);
1228                 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1229                                          V4L2_BUF_TYPE_VIDEO_CAPTURE);
1230                 vb2_clear_last_buffer_dequeued(dst_vq);
1231                 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1232                 coda_fill_bitstream(ctx, NULL);
1233                 mutex_unlock(&ctx->bitstream_mutex);
1234                 mutex_unlock(&dev->coda_mutex);
1235                 break;
1236         case V4L2_DEC_CMD_STOP:
1237                 stream_end = false;
1238                 wakeup = false;
1239
1240                 mutex_lock(&ctx->wakeup_mutex);
1241
1242                 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1243                 if (buf) {
1244                         coda_dbg(1, ctx, "marking last pending buffer\n");
1245
1246                         /* Mark last buffer */
1247                         buf->flags |= V4L2_BUF_FLAG_LAST;
1248
1249                         if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) == 0) {
1250                                 coda_dbg(1, ctx, "all remaining buffers queued\n");
1251                                 stream_end = true;
1252                         }
1253                 } else {
1254                         if (ctx->use_bit)
1255                                 if (coda_mark_last_meta(ctx))
1256                                         stream_end = true;
1257                                 else
1258                                         wakeup = true;
1259                         else
1260                                 if (!coda_mark_last_dst_buf(ctx))
1261                                         wakeup = true;
1262                 }
1263
1264                 if (stream_end) {
1265                         coda_dbg(1, ctx, "all remaining buffers queued\n");
1266
1267                         /* Set the stream-end flag on this context */
1268                         coda_bit_stream_end_flag(ctx);
1269                         ctx->hold = false;
1270                         v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1271                 }
1272
1273                 if (wakeup) {
1274                         /* If there is no buffer in flight, wake up */
1275                         coda_wake_up_capture_queue(ctx);
1276                 }
1277
1278                 mutex_unlock(&ctx->wakeup_mutex);
1279                 break;
1280         default:
1281                 return -EINVAL;
1282         }
1283
1284         return 0;
1285 }
1286
1287 static int coda_enum_framesizes(struct file *file, void *fh,
1288                                 struct v4l2_frmsizeenum *fsize)
1289 {
1290         struct coda_ctx *ctx = fh_to_ctx(fh);
1291         struct coda_q_data *q_data_dst;
1292         const struct coda_codec *codec;
1293
1294         if (ctx->inst_type != CODA_INST_ENCODER)
1295                 return -ENOTTY;
1296
1297         if (fsize->index)
1298                 return -EINVAL;
1299
1300         if (coda_format_normalize_yuv(fsize->pixel_format) ==
1301             V4L2_PIX_FMT_YUV420) {
1302                 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1303                 codec = coda_find_codec(ctx->dev, fsize->pixel_format,
1304                                         q_data_dst->fourcc);
1305         } else {
1306                 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1307                                         fsize->pixel_format);
1308         }
1309         if (!codec)
1310                 return -EINVAL;
1311
1312         fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1313         fsize->stepwise.min_width = MIN_W;
1314         fsize->stepwise.max_width = codec->max_w;
1315         fsize->stepwise.step_width = 1;
1316         fsize->stepwise.min_height = MIN_H;
1317         fsize->stepwise.max_height = codec->max_h;
1318         fsize->stepwise.step_height = 1;
1319
1320         return 0;
1321 }
1322
1323 static int coda_enum_frameintervals(struct file *file, void *fh,
1324                                     struct v4l2_frmivalenum *f)
1325 {
1326         struct coda_ctx *ctx = fh_to_ctx(fh);
1327         int i;
1328
1329         if (f->index)
1330                 return -EINVAL;
1331
1332         /* Disallow YUYV if the vdoa is not available */
1333         if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1334                 return -EINVAL;
1335
1336         for (i = 0; i < CODA_MAX_FORMATS; i++) {
1337                 if (f->pixel_format == ctx->cvd->src_formats[i] ||
1338                     f->pixel_format == ctx->cvd->dst_formats[i])
1339                         break;
1340         }
1341         if (i == CODA_MAX_FORMATS)
1342                 return -EINVAL;
1343
1344         f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1345         f->stepwise.min.numerator = 1;
1346         f->stepwise.min.denominator = 65535;
1347         f->stepwise.max.numerator = 65536;
1348         f->stepwise.max.denominator = 1;
1349         f->stepwise.step.numerator = 1;
1350         f->stepwise.step.denominator = 1;
1351
1352         return 0;
1353 }
1354
1355 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1356 {
1357         struct coda_ctx *ctx = fh_to_ctx(fh);
1358         struct v4l2_fract *tpf;
1359
1360         if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1361                 return -EINVAL;
1362
1363         a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1364         tpf = &a->parm.output.timeperframe;
1365         tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1366         tpf->numerator = 1 + (ctx->params.framerate >>
1367                               CODA_FRATE_DIV_OFFSET);
1368
1369         return 0;
1370 }
1371
1372 /*
1373  * Approximate timeperframe v4l2_fract with values that can be written
1374  * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1375  */
1376 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1377 {
1378         struct v4l2_fract s = *timeperframe;
1379         struct v4l2_fract f0;
1380         struct v4l2_fract f1 = { 1, 0 };
1381         struct v4l2_fract f2 = { 0, 1 };
1382         unsigned int i, div, s_denominator;
1383
1384         /* Lower bound is 1/65535 */
1385         if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1386                 timeperframe->numerator = 1;
1387                 timeperframe->denominator = 65535;
1388                 return;
1389         }
1390
1391         /* Upper bound is 65536/1 */
1392         if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1393                 timeperframe->numerator = 65536;
1394                 timeperframe->denominator = 1;
1395                 return;
1396         }
1397
1398         /* Reduce fraction to lowest terms */
1399         div = gcd(s.numerator, s.denominator);
1400         if (div > 1) {
1401                 s.numerator /= div;
1402                 s.denominator /= div;
1403         }
1404
1405         if (s.numerator <= 65536 && s.denominator < 65536) {
1406                 *timeperframe = s;
1407                 return;
1408         }
1409
1410         /* Find successive convergents from continued fraction expansion */
1411         while (f2.numerator <= 65536 && f2.denominator < 65536) {
1412                 f0 = f1;
1413                 f1 = f2;
1414
1415                 /* Stop when f2 exactly equals timeperframe */
1416                 if (s.numerator == 0)
1417                         break;
1418
1419                 i = s.denominator / s.numerator;
1420
1421                 f2.numerator = f0.numerator + i * f1.numerator;
1422                 f2.denominator = f0.denominator + i * f2.denominator;
1423
1424                 s_denominator = s.numerator;
1425                 s.numerator = s.denominator % s.numerator;
1426                 s.denominator = s_denominator;
1427         }
1428
1429         *timeperframe = f1;
1430 }
1431
1432 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1433 {
1434         return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1435                 timeperframe->denominator;
1436 }
1437
1438 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1439 {
1440         struct coda_ctx *ctx = fh_to_ctx(fh);
1441         struct v4l2_fract *tpf;
1442
1443         if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1444                 return -EINVAL;
1445
1446         a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1447         tpf = &a->parm.output.timeperframe;
1448         coda_approximate_timeperframe(tpf);
1449         ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1450         ctx->params.framerate_changed = true;
1451
1452         return 0;
1453 }
1454
1455 static int coda_subscribe_event(struct v4l2_fh *fh,
1456                                 const struct v4l2_event_subscription *sub)
1457 {
1458         struct coda_ctx *ctx = fh_to_ctx(fh);
1459
1460         switch (sub->type) {
1461         case V4L2_EVENT_EOS:
1462                 return v4l2_event_subscribe(fh, sub, 0, NULL);
1463         case V4L2_EVENT_SOURCE_CHANGE:
1464                 if (ctx->inst_type == CODA_INST_DECODER)
1465                         return v4l2_event_subscribe(fh, sub, 0, NULL);
1466                 else
1467                         return -EINVAL;
1468         default:
1469                 return v4l2_ctrl_subscribe_event(fh, sub);
1470         }
1471 }
1472
1473 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1474         .vidioc_querycap        = coda_querycap,
1475
1476         .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1477         .vidioc_g_fmt_vid_cap   = coda_g_fmt,
1478         .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1479         .vidioc_s_fmt_vid_cap   = coda_s_fmt_vid_cap,
1480
1481         .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1482         .vidioc_g_fmt_vid_out   = coda_g_fmt,
1483         .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1484         .vidioc_s_fmt_vid_out   = coda_s_fmt_vid_out,
1485
1486         .vidioc_reqbufs         = coda_reqbufs,
1487         .vidioc_querybuf        = v4l2_m2m_ioctl_querybuf,
1488
1489         .vidioc_qbuf            = coda_qbuf,
1490         .vidioc_expbuf          = v4l2_m2m_ioctl_expbuf,
1491         .vidioc_dqbuf           = coda_dqbuf,
1492         .vidioc_create_bufs     = v4l2_m2m_ioctl_create_bufs,
1493         .vidioc_prepare_buf     = v4l2_m2m_ioctl_prepare_buf,
1494
1495         .vidioc_streamon        = v4l2_m2m_ioctl_streamon,
1496         .vidioc_streamoff       = v4l2_m2m_ioctl_streamoff,
1497
1498         .vidioc_g_selection     = coda_g_selection,
1499         .vidioc_s_selection     = coda_s_selection,
1500
1501         .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1502         .vidioc_encoder_cmd     = coda_encoder_cmd,
1503         .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
1504         .vidioc_decoder_cmd     = coda_decoder_cmd,
1505
1506         .vidioc_g_parm          = coda_g_parm,
1507         .vidioc_s_parm          = coda_s_parm,
1508
1509         .vidioc_enum_framesizes = coda_enum_framesizes,
1510         .vidioc_enum_frameintervals = coda_enum_frameintervals,
1511
1512         .vidioc_subscribe_event = coda_subscribe_event,
1513         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1514 };
1515
1516 /*
1517  * Mem-to-mem operations.
1518  */
1519
1520 static void coda_device_run(void *m2m_priv)
1521 {
1522         struct coda_ctx *ctx = m2m_priv;
1523         struct coda_dev *dev = ctx->dev;
1524
1525         queue_work(dev->workqueue, &ctx->pic_run_work);
1526 }
1527
1528 static void coda_pic_run_work(struct work_struct *work)
1529 {
1530         struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1531         struct coda_dev *dev = ctx->dev;
1532         int ret;
1533
1534         mutex_lock(&ctx->buffer_mutex);
1535         mutex_lock(&dev->coda_mutex);
1536
1537         ret = ctx->ops->prepare_run(ctx);
1538         if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1539                 mutex_unlock(&dev->coda_mutex);
1540                 mutex_unlock(&ctx->buffer_mutex);
1541                 /* job_finish scheduled by prepare_decode */
1542                 return;
1543         }
1544
1545         if (!wait_for_completion_timeout(&ctx->completion,
1546                                          msecs_to_jiffies(1000))) {
1547                 if (ctx->use_bit) {
1548                         dev_err(dev->dev, "CODA PIC_RUN timeout\n");
1549
1550                         ctx->hold = true;
1551
1552                         coda_hw_reset(ctx);
1553                 }
1554
1555                 if (ctx->ops->run_timeout)
1556                         ctx->ops->run_timeout(ctx);
1557         } else {
1558                 ctx->ops->finish_run(ctx);
1559         }
1560
1561         if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1562             ctx->ops->seq_end_work)
1563                 queue_work(dev->workqueue, &ctx->seq_end_work);
1564
1565         mutex_unlock(&dev->coda_mutex);
1566         mutex_unlock(&ctx->buffer_mutex);
1567
1568         v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1569 }
1570
1571 static int coda_job_ready(void *m2m_priv)
1572 {
1573         struct coda_ctx *ctx = m2m_priv;
1574         int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1575
1576         /*
1577          * For both 'P' and 'key' frame cases 1 picture
1578          * and 1 frame are needed. In the decoder case,
1579          * the compressed frame can be in the bitstream.
1580          */
1581         if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1582                 coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
1583                 return 0;
1584         }
1585
1586         if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1587                 coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
1588                 return 0;
1589         }
1590
1591         if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1592                 bool stream_end = ctx->bit_stream_param &
1593                                   CODA_BIT_STREAM_END_FLAG;
1594                 int num_metas = ctx->num_metas;
1595                 struct coda_buffer_meta *meta;
1596                 unsigned int count;
1597
1598                 count = hweight32(ctx->frm_dis_flg);
1599                 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1600                         coda_dbg(1, ctx,
1601                                  "not ready: all internal buffers in use: %d/%d (0x%x)",
1602                                  count, ctx->num_internal_frames,
1603                                  ctx->frm_dis_flg);
1604                         return 0;
1605                 }
1606
1607                 if (ctx->hold && !src_bufs) {
1608                         coda_dbg(1, ctx,
1609                                  "not ready: on hold for more buffers.\n");
1610                         return 0;
1611                 }
1612
1613                 if (!stream_end && (num_metas + src_bufs) < 2) {
1614                         coda_dbg(1, ctx,
1615                                  "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1616                                  num_metas, src_bufs);
1617                         return 0;
1618                 }
1619
1620                 meta = list_first_entry(&ctx->buffer_meta_list,
1621                                         struct coda_buffer_meta, list);
1622                 if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1623                     !stream_end) {
1624                         coda_dbg(1, ctx,
1625                                  "not ready: not enough bitstream data to read past %u (%u)\n",
1626                                  meta->end, ctx->bitstream_fifo.kfifo.in);
1627                         return 0;
1628                 }
1629         }
1630
1631         if (ctx->aborting) {
1632                 coda_dbg(1, ctx, "not ready: aborting\n");
1633                 return 0;
1634         }
1635
1636         coda_dbg(2, ctx, "job ready\n");
1637
1638         return 1;
1639 }
1640
1641 static void coda_job_abort(void *priv)
1642 {
1643         struct coda_ctx *ctx = priv;
1644
1645         ctx->aborting = 1;
1646
1647         coda_dbg(1, ctx, "job abort\n");
1648 }
1649
1650 static const struct v4l2_m2m_ops coda_m2m_ops = {
1651         .device_run     = coda_device_run,
1652         .job_ready      = coda_job_ready,
1653         .job_abort      = coda_job_abort,
1654 };
1655
1656 static void set_default_params(struct coda_ctx *ctx)
1657 {
1658         unsigned int max_w, max_h, usize, csize;
1659
1660         ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1661                                      ctx->cvd->dst_formats[0]);
1662         max_w = min(ctx->codec->max_w, 1920U);
1663         max_h = min(ctx->codec->max_h, 1088U);
1664         usize = max_w * max_h * 3 / 2;
1665         csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1666
1667         ctx->params.codec_mode = ctx->codec->mode;
1668         if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1669                 ctx->colorspace = V4L2_COLORSPACE_JPEG;
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         ctx->params.framerate = 30;
1676
1677         /* Default formats for output and input queues */
1678         ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1679         ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1680         ctx->q_data[V4L2_M2M_SRC].width = max_w;
1681         ctx->q_data[V4L2_M2M_SRC].height = max_h;
1682         ctx->q_data[V4L2_M2M_DST].width = max_w;
1683         ctx->q_data[V4L2_M2M_DST].height = max_h;
1684         if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1685                 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1686                 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1687                 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1688                 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1689         } else {
1690                 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1691                 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1692                 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1693                 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1694         }
1695         ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1696         ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1697         ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1698         ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1699
1700         /*
1701          * Since the RBC2AXI logic only supports a single chroma plane,
1702          * macroblock tiling only works for to NV12 pixel format.
1703          */
1704         ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1705 }
1706
1707 /*
1708  * Queue operations
1709  */
1710 static int coda_queue_setup(struct vb2_queue *vq,
1711                                 unsigned int *nbuffers, unsigned int *nplanes,
1712                                 unsigned int sizes[], struct device *alloc_devs[])
1713 {
1714         struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1715         struct coda_q_data *q_data;
1716         unsigned int size;
1717
1718         q_data = get_q_data(ctx, vq->type);
1719         size = q_data->sizeimage;
1720
1721         if (*nplanes)
1722                 return sizes[0] < size ? -EINVAL : 0;
1723
1724         *nplanes = 1;
1725         sizes[0] = size;
1726
1727         coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1728                  size);
1729
1730         return 0;
1731 }
1732
1733 static int coda_buf_prepare(struct vb2_buffer *vb)
1734 {
1735         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1736         struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1737         struct coda_q_data *q_data;
1738
1739         q_data = get_q_data(ctx, vb->vb2_queue->type);
1740         if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1741                 if (vbuf->field == V4L2_FIELD_ANY)
1742                         vbuf->field = V4L2_FIELD_NONE;
1743                 if (vbuf->field != V4L2_FIELD_NONE) {
1744                         v4l2_warn(&ctx->dev->v4l2_dev,
1745                                   "%s field isn't supported\n", __func__);
1746                         return -EINVAL;
1747                 }
1748         }
1749
1750         if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1751                 v4l2_warn(&ctx->dev->v4l2_dev,
1752                           "%s data will not fit into plane (%lu < %lu)\n",
1753                           __func__, vb2_plane_size(vb, 0),
1754                           (long)q_data->sizeimage);
1755                 return -EINVAL;
1756         }
1757
1758         return 0;
1759 }
1760
1761 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1762 {
1763         if (!ctrl)
1764                 return;
1765
1766         v4l2_ctrl_lock(ctrl);
1767
1768         /*
1769          * Extend the control range if the parsed stream contains a known but
1770          * unsupported value or level.
1771          */
1772         if (value > ctrl->maximum) {
1773                 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1774                         ctrl->menu_skip_mask & ~(1 << value),
1775                         ctrl->default_value);
1776         } else if (value < ctrl->minimum) {
1777                 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1778                         ctrl->menu_skip_mask & ~(1 << value),
1779                         ctrl->default_value);
1780         }
1781
1782         __v4l2_ctrl_s_ctrl(ctrl, value);
1783
1784         v4l2_ctrl_unlock(ctrl);
1785 }
1786
1787 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
1788                                      u8 level_idc)
1789 {
1790         const char * const *profile_names;
1791         const char * const *level_names;
1792         struct v4l2_ctrl *profile_ctrl;
1793         struct v4l2_ctrl *level_ctrl;
1794         const char *codec_name;
1795         u32 profile_cid;
1796         u32 level_cid;
1797         int profile;
1798         int level;
1799
1800         switch (ctx->codec->src_fourcc) {
1801         case V4L2_PIX_FMT_H264:
1802                 codec_name = "H264";
1803                 profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
1804                 level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
1805                 profile_ctrl = ctx->h264_profile_ctrl;
1806                 level_ctrl = ctx->h264_level_ctrl;
1807                 profile = coda_h264_profile(profile_idc);
1808                 level = coda_h264_level(level_idc);
1809                 break;
1810         case V4L2_PIX_FMT_MPEG2:
1811                 codec_name = "MPEG-2";
1812                 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE;
1813                 level_cid = V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL;
1814                 profile_ctrl = ctx->mpeg2_profile_ctrl;
1815                 level_ctrl = ctx->mpeg2_level_ctrl;
1816                 profile = coda_mpeg2_profile(profile_idc);
1817                 level = coda_mpeg2_level(level_idc);
1818                 break;
1819         case V4L2_PIX_FMT_MPEG4:
1820                 codec_name = "MPEG-4";
1821                 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
1822                 level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
1823                 profile_ctrl = ctx->mpeg4_profile_ctrl;
1824                 level_ctrl = ctx->mpeg4_level_ctrl;
1825                 profile = coda_mpeg4_profile(profile_idc);
1826                 level = coda_mpeg4_level(level_idc);
1827                 break;
1828         default:
1829                 return;
1830         }
1831
1832         profile_names = v4l2_ctrl_get_menu(profile_cid);
1833         level_names = v4l2_ctrl_get_menu(level_cid);
1834
1835         if (profile < 0) {
1836                 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s profile: %u\n",
1837                           codec_name, profile_idc);
1838         } else {
1839                 coda_dbg(1, ctx, "Parsed %s profile: %s\n", codec_name,
1840                          profile_names[profile]);
1841                 coda_update_menu_ctrl(profile_ctrl, profile);
1842         }
1843
1844         if (level < 0) {
1845                 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s level: %u\n",
1846                           codec_name, level_idc);
1847         } else {
1848                 coda_dbg(1, ctx, "Parsed %s level: %s\n", codec_name,
1849                          level_names[level]);
1850                 coda_update_menu_ctrl(level_ctrl, level);
1851         }
1852 }
1853
1854 static void coda_queue_source_change_event(struct coda_ctx *ctx)
1855 {
1856         static const struct v4l2_event source_change_event = {
1857                 .type = V4L2_EVENT_SOURCE_CHANGE,
1858                 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1859         };
1860
1861         v4l2_event_queue_fh(&ctx->fh, &source_change_event);
1862 }
1863
1864 static void coda_buf_queue(struct vb2_buffer *vb)
1865 {
1866         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1867         struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1868         struct vb2_queue *vq = vb->vb2_queue;
1869         struct coda_q_data *q_data;
1870
1871         q_data = get_q_data(ctx, vb->vb2_queue->type);
1872
1873         /*
1874          * In the decoder case, immediately try to copy the buffer into the
1875          * bitstream ringbuffer and mark it as ready to be dequeued.
1876          */
1877         if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1878                 /*
1879                  * For backwards compatibility, queuing an empty buffer marks
1880                  * the stream end
1881                  */
1882                 if (vb2_get_plane_payload(vb, 0) == 0)
1883                         coda_bit_stream_end_flag(ctx);
1884
1885                 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1886                         /*
1887                          * Unless already done, try to obtain profile_idc and
1888                          * level_idc from the SPS header. This allows to decide
1889                          * whether to enable reordering during sequence
1890                          * initialization.
1891                          */
1892                         if (!ctx->params.h264_profile_idc) {
1893                                 coda_sps_parse_profile(ctx, vb);
1894                                 coda_update_profile_level_ctrls(ctx,
1895                                                 ctx->params.h264_profile_idc,
1896                                                 ctx->params.h264_level_idc);
1897                         }
1898                 }
1899
1900                 mutex_lock(&ctx->bitstream_mutex);
1901                 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1902                 if (vb2_is_streaming(vb->vb2_queue))
1903                         /* This set buf->sequence = ctx->qsequence++ */
1904                         coda_fill_bitstream(ctx, NULL);
1905                 mutex_unlock(&ctx->bitstream_mutex);
1906
1907                 if (!ctx->initialized) {
1908                         /*
1909                          * Run sequence initialization in case the queued
1910                          * buffer contained headers.
1911                          */
1912                         if (vb2_is_streaming(vb->vb2_queue) &&
1913                             ctx->ops->seq_init_work) {
1914                                 queue_work(ctx->dev->workqueue,
1915                                            &ctx->seq_init_work);
1916                                 flush_work(&ctx->seq_init_work);
1917                         }
1918
1919                         if (ctx->initialized)
1920                                 coda_queue_source_change_event(ctx);
1921                 }
1922         } else {
1923                 if ((ctx->inst_type == CODA_INST_ENCODER || !ctx->use_bit) &&
1924                     vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1925                         vbuf->sequence = ctx->qsequence++;
1926                 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1927         }
1928 }
1929
1930 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1931                        size_t size, const char *name, struct dentry *parent)
1932 {
1933         buf->vaddr = dma_alloc_coherent(dev->dev, size, &buf->paddr,
1934                                         GFP_KERNEL);
1935         if (!buf->vaddr) {
1936                 v4l2_err(&dev->v4l2_dev,
1937                          "Failed to allocate %s buffer of size %zu\n",
1938                          name, size);
1939                 return -ENOMEM;
1940         }
1941
1942         buf->size = size;
1943
1944         if (name && parent) {
1945                 buf->blob.data = buf->vaddr;
1946                 buf->blob.size = size;
1947                 buf->dentry = debugfs_create_blob(name, 0444, parent,
1948                                                   &buf->blob);
1949         }
1950
1951         return 0;
1952 }
1953
1954 void coda_free_aux_buf(struct coda_dev *dev,
1955                        struct coda_aux_buf *buf)
1956 {
1957         if (buf->vaddr) {
1958                 dma_free_coherent(dev->dev, buf->size, buf->vaddr, buf->paddr);
1959                 buf->vaddr = NULL;
1960                 buf->size = 0;
1961                 debugfs_remove(buf->dentry);
1962                 buf->dentry = NULL;
1963         }
1964 }
1965
1966 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1967 {
1968         struct coda_ctx *ctx = vb2_get_drv_priv(q);
1969         struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1970         struct coda_q_data *q_data_src, *q_data_dst;
1971         struct v4l2_m2m_buffer *m2m_buf, *tmp;
1972         struct vb2_v4l2_buffer *buf;
1973         struct list_head list;
1974         int ret = 0;
1975
1976         if (count < 1)
1977                 return -EINVAL;
1978
1979         coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1980
1981         INIT_LIST_HEAD(&list);
1982
1983         q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1984         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1985                 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1986                         /* copy the buffers that were queued before streamon */
1987                         mutex_lock(&ctx->bitstream_mutex);
1988                         coda_fill_bitstream(ctx, &list);
1989                         mutex_unlock(&ctx->bitstream_mutex);
1990
1991                         if (ctx->dev->devtype->product != CODA_960 &&
1992                             coda_get_bitstream_payload(ctx) < 512) {
1993                                 v4l2_err(v4l2_dev, "start payload < 512\n");
1994                                 ret = -EINVAL;
1995                                 goto err;
1996                         }
1997
1998                         if (!ctx->initialized) {
1999                                 /* Run sequence initialization */
2000                                 if (ctx->ops->seq_init_work) {
2001                                         queue_work(ctx->dev->workqueue,
2002                                                    &ctx->seq_init_work);
2003                                         flush_work(&ctx->seq_init_work);
2004                                 }
2005                         }
2006                 }
2007
2008                 /*
2009                  * Check the first input JPEG buffer to determine chroma
2010                  * subsampling.
2011                  */
2012                 if (q_data_src->fourcc == V4L2_PIX_FMT_JPEG) {
2013                         buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
2014                         ret = coda_jpeg_decode_header(ctx, &buf->vb2_buf);
2015                         if (ret < 0) {
2016                                 v4l2_err(v4l2_dev,
2017                                          "failed to decode JPEG header: %d\n",
2018                                          ret);
2019                                 goto err;
2020                         }
2021
2022                         q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2023                         q_data_dst->width = round_up(q_data_src->width, 16);
2024                         q_data_dst->height = round_up(q_data_src->height, 16);
2025                         q_data_dst->bytesperline = q_data_dst->width;
2026                         if (ctx->params.jpeg_chroma_subsampling ==
2027                             V4L2_JPEG_CHROMA_SUBSAMPLING_420) {
2028                                 q_data_dst->sizeimage =
2029                                                 q_data_dst->bytesperline *
2030                                                 q_data_dst->height * 3 / 2;
2031                                 if (q_data_dst->fourcc != V4L2_PIX_FMT_YUV420)
2032                                         q_data_dst->fourcc = V4L2_PIX_FMT_NV12;
2033                         } else {
2034                                 q_data_dst->sizeimage =
2035                                                 q_data_dst->bytesperline *
2036                                                 q_data_dst->height * 2;
2037                                 q_data_dst->fourcc = V4L2_PIX_FMT_YUV422P;
2038                         }
2039                         q_data_dst->rect.left = 0;
2040                         q_data_dst->rect.top = 0;
2041                         q_data_dst->rect.width = q_data_src->width;
2042                         q_data_dst->rect.height = q_data_src->height;
2043                 }
2044                 ctx->streamon_out = 1;
2045         } else {
2046                 ctx->streamon_cap = 1;
2047         }
2048
2049         /* Don't start the coda unless both queues are on */
2050         if (!(ctx->streamon_out && ctx->streamon_cap))
2051                 goto out;
2052
2053         q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2054         if ((q_data_src->rect.width != q_data_dst->width &&
2055              round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
2056             (q_data_src->rect.height != q_data_dst->height &&
2057              round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
2058                 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
2059                          q_data_src->rect.width, q_data_src->rect.height,
2060                          q_data_dst->width, q_data_dst->height);
2061                 ret = -EINVAL;
2062                 goto err;
2063         }
2064
2065         /* Allow BIT decoder device_run with no new buffers queued */
2066         if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2067                 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
2068
2069         ctx->gopcounter = ctx->params.gop_size - 1;
2070
2071         if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
2072                 ctx->params.gop_size = 1;
2073         ctx->gopcounter = ctx->params.gop_size - 1;
2074         /* Only decoders have this control */
2075         if (ctx->mb_err_cnt_ctrl)
2076                 v4l2_ctrl_s_ctrl(ctx->mb_err_cnt_ctrl, 0);
2077
2078         ret = ctx->ops->start_streaming(ctx);
2079         if (ctx->inst_type == CODA_INST_DECODER) {
2080                 if (ret == -EAGAIN)
2081                         goto out;
2082         }
2083         if (ret < 0)
2084                 goto err;
2085
2086 out:
2087         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2088                 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2089                         list_del(&m2m_buf->list);
2090                         v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
2091                 }
2092         }
2093         return 0;
2094
2095 err:
2096         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2097                 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
2098                         list_del(&m2m_buf->list);
2099                         v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
2100                 }
2101                 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2102                         v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2103         } else {
2104                 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2105                         v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
2106         }
2107         return ret;
2108 }
2109
2110 static void coda_stop_streaming(struct vb2_queue *q)
2111 {
2112         struct coda_ctx *ctx = vb2_get_drv_priv(q);
2113         struct coda_dev *dev = ctx->dev;
2114         struct vb2_v4l2_buffer *buf;
2115         bool stop;
2116
2117         stop = ctx->streamon_out && ctx->streamon_cap;
2118
2119         coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
2120
2121         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2122                 ctx->streamon_out = 0;
2123
2124                 coda_bit_stream_end_flag(ctx);
2125
2126                 ctx->qsequence = 0;
2127
2128                 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
2129                         v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2130         } else {
2131                 ctx->streamon_cap = 0;
2132
2133                 ctx->osequence = 0;
2134                 ctx->sequence_offset = 0;
2135
2136                 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
2137                         v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
2138         }
2139
2140         if (stop) {
2141                 struct coda_buffer_meta *meta;
2142
2143                 if (ctx->ops->seq_end_work) {
2144                         queue_work(dev->workqueue, &ctx->seq_end_work);
2145                         flush_work(&ctx->seq_end_work);
2146                 }
2147                 spin_lock(&ctx->buffer_meta_lock);
2148                 while (!list_empty(&ctx->buffer_meta_list)) {
2149                         meta = list_first_entry(&ctx->buffer_meta_list,
2150                                                 struct coda_buffer_meta, list);
2151                         list_del(&meta->list);
2152                         kfree(meta);
2153                 }
2154                 ctx->num_metas = 0;
2155                 spin_unlock(&ctx->buffer_meta_lock);
2156                 kfifo_init(&ctx->bitstream_fifo,
2157                         ctx->bitstream.vaddr, ctx->bitstream.size);
2158                 ctx->runcounter = 0;
2159                 ctx->aborting = 0;
2160                 ctx->hold = false;
2161         }
2162
2163         if (!ctx->streamon_out && !ctx->streamon_cap)
2164                 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
2165 }
2166
2167 static const struct vb2_ops coda_qops = {
2168         .queue_setup            = coda_queue_setup,
2169         .buf_prepare            = coda_buf_prepare,
2170         .buf_queue              = coda_buf_queue,
2171         .start_streaming        = coda_start_streaming,
2172         .stop_streaming         = coda_stop_streaming,
2173         .wait_prepare           = vb2_ops_wait_prepare,
2174         .wait_finish            = vb2_ops_wait_finish,
2175 };
2176
2177 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2178 {
2179         const char * const *val_names = v4l2_ctrl_get_menu(ctrl->id);
2180         struct coda_ctx *ctx =
2181                         container_of(ctrl->handler, struct coda_ctx, ctrls);
2182
2183         if (val_names)
2184                 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d (\"%s\")\n",
2185                          ctrl->id, ctrl->name, ctrl->val, val_names[ctrl->val]);
2186         else
2187                 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
2188                          ctrl->id, ctrl->name, ctrl->val);
2189
2190         switch (ctrl->id) {
2191         case V4L2_CID_HFLIP:
2192                 if (ctrl->val)
2193                         ctx->params.rot_mode |= CODA_MIR_HOR;
2194                 else
2195                         ctx->params.rot_mode &= ~CODA_MIR_HOR;
2196                 break;
2197         case V4L2_CID_VFLIP:
2198                 if (ctrl->val)
2199                         ctx->params.rot_mode |= CODA_MIR_VER;
2200                 else
2201                         ctx->params.rot_mode &= ~CODA_MIR_VER;
2202                 break;
2203         case V4L2_CID_MPEG_VIDEO_BITRATE:
2204                 ctx->params.bitrate = ctrl->val / 1000;
2205                 ctx->params.bitrate_changed = true;
2206                 break;
2207         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2208                 ctx->params.gop_size = ctrl->val;
2209                 break;
2210         case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2211                 ctx->params.h264_intra_qp = ctrl->val;
2212                 ctx->params.h264_intra_qp_changed = true;
2213                 break;
2214         case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2215                 ctx->params.h264_inter_qp = ctrl->val;
2216                 break;
2217         case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2218                 ctx->params.h264_min_qp = ctrl->val;
2219                 break;
2220         case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2221                 ctx->params.h264_max_qp = ctrl->val;
2222                 break;
2223         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2224                 ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
2225                 break;
2226         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2227                 ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
2228                 break;
2229         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2230                 ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
2231                 break;
2232         case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
2233                 ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
2234                 break;
2235         case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
2236                 ctx->params.frame_rc_enable = ctrl->val;
2237                 break;
2238         case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
2239                 ctx->params.mb_rc_enable = ctrl->val;
2240                 break;
2241         case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
2242                 ctx->params.h264_chroma_qp_index_offset = ctrl->val;
2243                 break;
2244         case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
2245                 /* TODO: switch between baseline and constrained baseline */
2246                 if (ctx->inst_type == CODA_INST_ENCODER)
2247                         ctx->params.h264_profile_idc = 66;
2248                 break;
2249         case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2250                 /* nothing to do, this is set by the encoder */
2251                 break;
2252         case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2253                 ctx->params.mpeg4_intra_qp = ctrl->val;
2254                 break;
2255         case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2256                 ctx->params.mpeg4_inter_qp = ctrl->val;
2257                 break;
2258         case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
2259         case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
2260         case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
2261         case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
2262                 /* nothing to do, these are fixed */
2263                 break;
2264         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2265                 ctx->params.slice_mode = ctrl->val;
2266                 ctx->params.slice_mode_changed = true;
2267                 break;
2268         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2269                 ctx->params.slice_max_mb = ctrl->val;
2270                 ctx->params.slice_mode_changed = true;
2271                 break;
2272         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2273                 ctx->params.slice_max_bits = ctrl->val * 8;
2274                 ctx->params.slice_mode_changed = true;
2275                 break;
2276         case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2277                 break;
2278         case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2279                 ctx->params.intra_refresh = ctrl->val;
2280                 ctx->params.intra_refresh_changed = true;
2281                 break;
2282         case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
2283                 ctx->params.force_ipicture = true;
2284                 break;
2285         case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2286                 coda_set_jpeg_compression_quality(ctx, ctrl->val);
2287                 break;
2288         case V4L2_CID_JPEG_RESTART_INTERVAL:
2289                 ctx->params.jpeg_restart_interval = ctrl->val;
2290                 break;
2291         case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
2292                 ctx->params.vbv_delay = ctrl->val;
2293                 break;
2294         case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
2295                 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
2296                 break;
2297         default:
2298                 coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
2299                          ctrl->id, ctrl->val);
2300                 return -EINVAL;
2301         }
2302
2303         return 0;
2304 }
2305
2306 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
2307         .s_ctrl = coda_s_ctrl,
2308 };
2309
2310 static void coda_encode_ctrls(struct coda_ctx *ctx)
2311 {
2312         int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
2313
2314         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2315                 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
2316         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2317                 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
2318         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2319                 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
2320         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2321                 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
2322         if (ctx->dev->devtype->product != CODA_960) {
2323                 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2324                         V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2325         }
2326         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2327                 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
2328         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2329                 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
2330         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2331                 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
2332         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2333                 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2334                 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
2335                 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2336         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2337                 V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
2338                 0);
2339         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2340                 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
2341         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2342                 V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, 0, 1, 1, 1);
2343         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2344                 V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
2345         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2346                 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2347                 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2348                 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2349         if (ctx->dev->devtype->product == CODA_HX4 ||
2350             ctx->dev->devtype->product == CODA_7541) {
2351                 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2352                         V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2353                         V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2354                         ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2355                           (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2356                           (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2357                         V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2358         }
2359         if (ctx->dev->devtype->product == CODA_960) {
2360                 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2361                         V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2362                         V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
2363                         ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2364                           (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2365                           (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2366                           (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2367                           (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
2368                         V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2369         }
2370         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2371                 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2372         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2373                 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2374         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2375                 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2376                 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2377                 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
2378         if (ctx->dev->devtype->product == CODA_HX4 ||
2379             ctx->dev->devtype->product == CODA_7541 ||
2380             ctx->dev->devtype->product == CODA_960) {
2381                 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2382                         V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2383                         V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2384                         ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2385                         V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2386         }
2387         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2388                 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
2389                 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES, 0x0,
2390                 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
2391         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2392                 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
2393         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2394                 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2395                 500);
2396         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2397                 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2398                 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2399                 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2400                 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2401         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2402                 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2403                 1920 * 1088 / 256, 1, 0);
2404         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2405                 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2406         /*
2407          * The maximum VBV size value is 0x7fffffff bits,
2408          * one bit less than 262144 KiB
2409          */
2410         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2411                 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
2412 }
2413
2414 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2415 {
2416         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2417                 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2418         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2419                 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2420 }
2421
2422 static void coda_decode_ctrls(struct coda_ctx *ctx)
2423 {
2424         u8 max;
2425
2426         ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2427                 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2428                 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2429                 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
2430                   (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2431                   (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2432                 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2433         if (ctx->h264_profile_ctrl)
2434                 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2435
2436         if (ctx->dev->devtype->product == CODA_HX4 ||
2437             ctx->dev->devtype->product == CODA_7541)
2438                 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2439         else if (ctx->dev->devtype->product == CODA_960)
2440                 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
2441         else
2442                 return;
2443         ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2444                 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
2445         if (ctx->h264_level_ctrl)
2446                 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2447
2448         ctx->mpeg2_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2449                 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE,
2450                 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH, 0,
2451                 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH);
2452         if (ctx->mpeg2_profile_ctrl)
2453                 ctx->mpeg2_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2454
2455         ctx->mpeg2_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2456                 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL,
2457                 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH, 0,
2458                 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH);
2459         if (ctx->mpeg2_level_ctrl)
2460                 ctx->mpeg2_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2461
2462         ctx->mpeg4_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2463                 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2464                 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY, 0,
2465                 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY);
2466         if (ctx->mpeg4_profile_ctrl)
2467                 ctx->mpeg4_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2468
2469         ctx->mpeg4_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2470                 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2471                 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 0,
2472                 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2473         if (ctx->mpeg4_level_ctrl)
2474                 ctx->mpeg4_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2475 }
2476
2477 static const struct v4l2_ctrl_config coda_mb_err_cnt_ctrl_config = {
2478         .id     = V4L2_CID_CODA_MB_ERR_CNT,
2479         .name   = "Macroblocks Error Count",
2480         .type   = V4L2_CTRL_TYPE_INTEGER,
2481         .min    = 0,
2482         .max    = 0x7fffffff,
2483         .step   = 1,
2484 };
2485
2486 static int coda_ctrls_setup(struct coda_ctx *ctx)
2487 {
2488         v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2489
2490         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2491                 V4L2_CID_HFLIP, 0, 1, 1, 0);
2492         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2493                 V4L2_CID_VFLIP, 0, 1, 1, 0);
2494         if (ctx->inst_type == CODA_INST_ENCODER) {
2495                 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2496                                   V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2497                                   1, 1, 1, 1);
2498                 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2499                         coda_jpeg_encode_ctrls(ctx);
2500                 else
2501                         coda_encode_ctrls(ctx);
2502         } else {
2503                 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2504                                   V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2505                                   1, 1, 1, 1);
2506                 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2507                         coda_decode_ctrls(ctx);
2508
2509                 ctx->mb_err_cnt_ctrl = v4l2_ctrl_new_custom(&ctx->ctrls,
2510                                                 &coda_mb_err_cnt_ctrl_config,
2511                                                 NULL);
2512                 if (ctx->mb_err_cnt_ctrl)
2513                         ctx->mb_err_cnt_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2514         }
2515
2516         if (ctx->ctrls.error) {
2517                 v4l2_err(&ctx->dev->v4l2_dev,
2518                         "control initialization error (%d)",
2519                         ctx->ctrls.error);
2520                 return -EINVAL;
2521         }
2522
2523         return v4l2_ctrl_handler_setup(&ctx->ctrls);
2524 }
2525
2526 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2527 {
2528         vq->drv_priv = ctx;
2529         vq->ops = &coda_qops;
2530         vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2531         vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2532         vq->lock = &ctx->dev->dev_mutex;
2533         /* One way to indicate end-of-stream for coda is to set the
2534          * bytesused == 0. However by default videobuf2 handles bytesused
2535          * equal to 0 as a special case and changes its value to the size
2536          * of the buffer. Set the allow_zero_bytesused flag, so
2537          * that videobuf2 will keep the value of bytesused intact.
2538          */
2539         vq->allow_zero_bytesused = 1;
2540         /*
2541          * We might be fine with no buffers on some of the queues, but that
2542          * would need to be reflected in job_ready(). Currently we expect all
2543          * queues to have at least one buffer queued.
2544          */
2545         vq->min_buffers_needed = 1;
2546         vq->dev = ctx->dev->dev;
2547
2548         return vb2_queue_init(vq);
2549 }
2550
2551 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2552                             struct vb2_queue *dst_vq)
2553 {
2554         int ret;
2555
2556         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2557         src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2558         src_vq->mem_ops = &vb2_dma_contig_memops;
2559
2560         ret = coda_queue_init(priv, src_vq);
2561         if (ret)
2562                 return ret;
2563
2564         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2565         dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2566         dst_vq->mem_ops = &vb2_dma_contig_memops;
2567
2568         return coda_queue_init(priv, dst_vq);
2569 }
2570
2571 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2572                             struct vb2_queue *dst_vq)
2573 {
2574         int ret;
2575
2576         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2577         src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2578         src_vq->mem_ops = &vb2_vmalloc_memops;
2579
2580         ret = coda_queue_init(priv, src_vq);
2581         if (ret)
2582                 return ret;
2583
2584         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2585         dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2586         dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING;
2587         dst_vq->mem_ops = &vb2_dma_contig_memops;
2588
2589         return coda_queue_init(priv, dst_vq);
2590 }
2591
2592 /*
2593  * File operations
2594  */
2595
2596 static int coda_open(struct file *file)
2597 {
2598         struct video_device *vdev = video_devdata(file);
2599         struct coda_dev *dev = video_get_drvdata(vdev);
2600         struct coda_ctx *ctx;
2601         unsigned int max = ~0;
2602         char *name;
2603         int ret;
2604         int idx;
2605
2606         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2607         if (!ctx)
2608                 return -ENOMEM;
2609
2610         if (dev->devtype->product == CODA_DX6)
2611                 max = CODADX6_MAX_INSTANCES - 1;
2612         idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2613         if (idx < 0) {
2614                 ret = idx;
2615                 goto err_coda_max;
2616         }
2617
2618         name = kasprintf(GFP_KERNEL, "context%d", idx);
2619         if (!name) {
2620                 ret = -ENOMEM;
2621                 goto err_coda_name_init;
2622         }
2623
2624         ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2625         kfree(name);
2626
2627         ctx->cvd = to_coda_video_device(vdev);
2628         ctx->inst_type = ctx->cvd->type;
2629         ctx->ops = ctx->cvd->ops;
2630         ctx->use_bit = !ctx->cvd->direct;
2631         init_completion(&ctx->completion);
2632         INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2633         if (ctx->ops->seq_init_work)
2634                 INIT_WORK(&ctx->seq_init_work, ctx->ops->seq_init_work);
2635         if (ctx->ops->seq_end_work)
2636                 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2637         v4l2_fh_init(&ctx->fh, video_devdata(file));
2638         file->private_data = &ctx->fh;
2639         v4l2_fh_add(&ctx->fh);
2640         ctx->dev = dev;
2641         ctx->idx = idx;
2642
2643         coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2644
2645         switch (dev->devtype->product) {
2646         case CODA_960:
2647                 /*
2648                  * Enabling the BWB when decoding can hang the firmware with
2649                  * certain streams. The issue was tracked as ENGR00293425 by
2650                  * Freescale. As a workaround, disable BWB for all decoders.
2651                  * The enable_bwb module parameter allows to override this.
2652                  */
2653                 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2654                         ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2655                 fallthrough;
2656         case CODA_HX4:
2657         case CODA_7541:
2658                 ctx->reg_idx = 0;
2659                 break;
2660         default:
2661                 ctx->reg_idx = idx;
2662         }
2663         if (ctx->dev->vdoa && !disable_vdoa) {
2664                 ctx->vdoa = vdoa_context_create(dev->vdoa);
2665                 if (!ctx->vdoa)
2666                         v4l2_warn(&dev->v4l2_dev,
2667                                   "Failed to create vdoa context: not using vdoa");
2668         }
2669         ctx->use_vdoa = false;
2670
2671         /* Power up and upload firmware if necessary */
2672         ret = pm_runtime_resume_and_get(dev->dev);
2673         if (ret < 0) {
2674                 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2675                 goto err_pm_get;
2676         }
2677
2678         ret = clk_prepare_enable(dev->clk_per);
2679         if (ret)
2680                 goto err_clk_enable;
2681
2682         ret = clk_prepare_enable(dev->clk_ahb);
2683         if (ret)
2684                 goto err_clk_ahb;
2685
2686         set_default_params(ctx);
2687         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2688                                             ctx->ops->queue_init);
2689         if (IS_ERR(ctx->fh.m2m_ctx)) {
2690                 ret = PTR_ERR(ctx->fh.m2m_ctx);
2691
2692                 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2693                          __func__, ret);
2694                 goto err_ctx_init;
2695         }
2696
2697         ret = coda_ctrls_setup(ctx);
2698         if (ret) {
2699                 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2700                 goto err_ctrls_setup;
2701         }
2702
2703         ctx->fh.ctrl_handler = &ctx->ctrls;
2704
2705         mutex_init(&ctx->bitstream_mutex);
2706         mutex_init(&ctx->buffer_mutex);
2707         mutex_init(&ctx->wakeup_mutex);
2708         INIT_LIST_HEAD(&ctx->buffer_meta_list);
2709         spin_lock_init(&ctx->buffer_meta_lock);
2710
2711         return 0;
2712
2713 err_ctrls_setup:
2714         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2715 err_ctx_init:
2716         clk_disable_unprepare(dev->clk_ahb);
2717 err_clk_ahb:
2718         clk_disable_unprepare(dev->clk_per);
2719 err_clk_enable:
2720         pm_runtime_put_sync(dev->dev);
2721 err_pm_get:
2722         v4l2_fh_del(&ctx->fh);
2723         v4l2_fh_exit(&ctx->fh);
2724 err_coda_name_init:
2725         ida_free(&dev->ida, ctx->idx);
2726 err_coda_max:
2727         kfree(ctx);
2728         return ret;
2729 }
2730
2731 static int coda_release(struct file *file)
2732 {
2733         struct coda_dev *dev = video_drvdata(file);
2734         struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2735
2736         coda_dbg(1, ctx, "release instance (%p)\n", ctx);
2737
2738         if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2739                 coda_bit_stream_end_flag(ctx);
2740
2741         /* If this instance is running, call .job_abort and wait for it to end */
2742         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2743
2744         if (ctx->vdoa)
2745                 vdoa_context_destroy(ctx->vdoa);
2746
2747         /* In case the instance was not running, we still need to call SEQ_END */
2748         if (ctx->ops->seq_end_work) {
2749                 queue_work(dev->workqueue, &ctx->seq_end_work);
2750                 flush_work(&ctx->seq_end_work);
2751         }
2752
2753         if (ctx->dev->devtype->product == CODA_DX6)
2754                 coda_free_aux_buf(dev, &ctx->workbuf);
2755
2756         v4l2_ctrl_handler_free(&ctx->ctrls);
2757         clk_disable_unprepare(dev->clk_ahb);
2758         clk_disable_unprepare(dev->clk_per);
2759         pm_runtime_put_sync(dev->dev);
2760         v4l2_fh_del(&ctx->fh);
2761         v4l2_fh_exit(&ctx->fh);
2762         ida_free(&dev->ida, ctx->idx);
2763         if (ctx->ops->release)
2764                 ctx->ops->release(ctx);
2765         debugfs_remove_recursive(ctx->debugfs_entry);
2766         kfree(ctx);
2767
2768         return 0;
2769 }
2770
2771 static const struct v4l2_file_operations coda_fops = {
2772         .owner          = THIS_MODULE,
2773         .open           = coda_open,
2774         .release        = coda_release,
2775         .poll           = v4l2_m2m_fop_poll,
2776         .unlocked_ioctl = video_ioctl2,
2777         .mmap           = v4l2_m2m_fop_mmap,
2778 };
2779
2780 static int coda_hw_init(struct coda_dev *dev)
2781 {
2782         u32 data;
2783         u16 *p;
2784         int i, ret;
2785
2786         ret = clk_prepare_enable(dev->clk_per);
2787         if (ret)
2788                 goto err_clk_per;
2789
2790         ret = clk_prepare_enable(dev->clk_ahb);
2791         if (ret)
2792                 goto err_clk_ahb;
2793
2794         reset_control_reset(dev->rstc);
2795
2796         /*
2797          * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2798          * The 16-bit chars in the code buffer are in memory access
2799          * order, re-sort them to CODA order for register download.
2800          * Data in this SRAM survives a reboot.
2801          */
2802         p = (u16 *)dev->codebuf.vaddr;
2803         if (dev->devtype->product == CODA_DX6) {
2804                 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++)  {
2805                         data = CODA_DOWN_ADDRESS_SET(i) |
2806                                 CODA_DOWN_DATA_SET(p[i ^ 1]);
2807                         coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2808                 }
2809         } else {
2810                 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2811                         data = CODA_DOWN_ADDRESS_SET(i) |
2812                                 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2813                                                         3 - (i % 4)]);
2814                         coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2815                 }
2816         }
2817
2818         /* Clear registers */
2819         for (i = 0; i < 64; i++)
2820                 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2821
2822         /* Tell the BIT where to find everything it needs */
2823         if (dev->devtype->product == CODA_960 ||
2824             dev->devtype->product == CODA_7541 ||
2825             dev->devtype->product == CODA_HX4) {
2826                 coda_write(dev, dev->tempbuf.paddr,
2827                                 CODA_REG_BIT_TEMP_BUF_ADDR);
2828                 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2829         } else {
2830                 coda_write(dev, dev->workbuf.paddr,
2831                               CODA_REG_BIT_WORK_BUF_ADDR);
2832         }
2833         coda_write(dev, dev->codebuf.paddr,
2834                       CODA_REG_BIT_CODE_BUF_ADDR);
2835         coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2836
2837         /* Set default values */
2838         switch (dev->devtype->product) {
2839         case CODA_DX6:
2840                 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2841                            CODA_REG_BIT_STREAM_CTRL);
2842                 break;
2843         default:
2844                 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2845                            CODA_REG_BIT_STREAM_CTRL);
2846         }
2847         if (dev->devtype->product == CODA_960)
2848                 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2849                                 CODA_REG_BIT_FRAME_MEM_CTRL);
2850         else
2851                 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2852
2853         if (dev->devtype->product != CODA_DX6)
2854                 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2855
2856         coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2857                       CODA_REG_BIT_INT_ENABLE);
2858
2859         /* Reset VPU and start processor */
2860         data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2861         data |= CODA_REG_RESET_ENABLE;
2862         coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2863         udelay(10);
2864         data &= ~CODA_REG_RESET_ENABLE;
2865         coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2866         coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2867
2868         clk_disable_unprepare(dev->clk_ahb);
2869         clk_disable_unprepare(dev->clk_per);
2870
2871         return 0;
2872
2873 err_clk_ahb:
2874         clk_disable_unprepare(dev->clk_per);
2875 err_clk_per:
2876         return ret;
2877 }
2878
2879 static int coda_register_device(struct coda_dev *dev, int i)
2880 {
2881         struct video_device *vfd = &dev->vfd[i];
2882         const char *name;
2883         int ret;
2884
2885         if (i >= dev->devtype->num_vdevs)
2886                 return -EINVAL;
2887         name = dev->devtype->vdevs[i]->name;
2888
2889         strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2890         vfd->fops       = &coda_fops;
2891         vfd->ioctl_ops  = &coda_ioctl_ops;
2892         vfd->release    = video_device_release_empty;
2893         vfd->lock       = &dev->dev_mutex;
2894         vfd->v4l2_dev   = &dev->v4l2_dev;
2895         vfd->vfl_dir    = VFL_DIR_M2M;
2896         vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2897         video_set_drvdata(vfd, dev);
2898
2899         /* Not applicable, use the selection API instead */
2900         v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2901         v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2902         v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2903
2904         ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
2905         if (!ret)
2906                 v4l2_info(&dev->v4l2_dev, "%s registered as %s\n",
2907                           name, video_device_node_name(vfd));
2908         return ret;
2909 }
2910
2911 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2912                                size_t size)
2913 {
2914         u32 *src = (u32 *)buf;
2915
2916         /* Check if the firmware has a 16-byte Freescale header, skip it */
2917         if (buf[0] == 'M' && buf[1] == 'X')
2918                 src += 4;
2919         /*
2920          * Check whether the firmware is in native order or pre-reordered for
2921          * memory access. The first instruction opcode always is 0xe40e.
2922          */
2923         if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2924                 u32 *dst = dev->codebuf.vaddr;
2925                 int i;
2926
2927                 /* Firmware in native order, reorder while copying */
2928                 if (dev->devtype->product == CODA_DX6) {
2929                         for (i = 0; i < (size - 16) / 4; i++)
2930                                 dst[i] = (src[i] << 16) | (src[i] >> 16);
2931                 } else {
2932                         for (i = 0; i < (size - 16) / 4; i += 2) {
2933                                 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2934                                 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2935                         }
2936                 }
2937         } else {
2938                 /* Copy the already reordered firmware image */
2939                 memcpy(dev->codebuf.vaddr, src, size);
2940         }
2941 }
2942
2943 static void coda_fw_callback(const struct firmware *fw, void *context);
2944
2945 static int coda_firmware_request(struct coda_dev *dev)
2946 {
2947         char *fw;
2948
2949         if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2950                 return -EINVAL;
2951
2952         fw = dev->devtype->firmware[dev->firmware];
2953
2954         dev_dbg(dev->dev, "requesting firmware '%s' for %s\n", fw,
2955                 coda_product_name(dev->devtype->product));
2956
2957         return request_firmware_nowait(THIS_MODULE, true, fw, dev->dev,
2958                                        GFP_KERNEL, dev, coda_fw_callback);
2959 }
2960
2961 static void coda_fw_callback(const struct firmware *fw, void *context)
2962 {
2963         struct coda_dev *dev = context;
2964         int i, ret;
2965
2966         if (!fw) {
2967                 dev->firmware++;
2968                 ret = coda_firmware_request(dev);
2969                 if (ret < 0) {
2970                         v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2971                         goto put_pm;
2972                 }
2973                 return;
2974         }
2975         if (dev->firmware > 0) {
2976                 /*
2977                  * Since we can't suppress warnings for failed asynchronous
2978                  * firmware requests, report that the fallback firmware was
2979                  * found.
2980                  */
2981                 dev_info(dev->dev, "Using fallback firmware %s\n",
2982                          dev->devtype->firmware[dev->firmware]);
2983         }
2984
2985         /* allocate auxiliary per-device code buffer for the BIT processor */
2986         ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2987                                  dev->debugfs_root);
2988         if (ret < 0)
2989                 goto put_pm;
2990
2991         coda_copy_firmware(dev, fw->data, fw->size);
2992         release_firmware(fw);
2993
2994         ret = coda_hw_init(dev);
2995         if (ret < 0) {
2996                 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2997                 goto put_pm;
2998         }
2999
3000         ret = coda_check_firmware(dev);
3001         if (ret < 0)
3002                 goto put_pm;
3003
3004         dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3005         if (IS_ERR(dev->m2m_dev)) {
3006                 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3007                 goto put_pm;
3008         }
3009
3010         for (i = 0; i < dev->devtype->num_vdevs; i++) {
3011                 ret = coda_register_device(dev, i);
3012                 if (ret) {
3013                         v4l2_err(&dev->v4l2_dev,
3014                                  "Failed to register %s video device: %d\n",
3015                                  dev->devtype->vdevs[i]->name, ret);
3016                         goto rel_vfd;
3017                 }
3018         }
3019
3020         pm_runtime_put_sync(dev->dev);
3021         return;
3022
3023 rel_vfd:
3024         while (--i >= 0)
3025                 video_unregister_device(&dev->vfd[i]);
3026         v4l2_m2m_release(dev->m2m_dev);
3027 put_pm:
3028         pm_runtime_put_sync(dev->dev);
3029 }
3030
3031 enum coda_platform {
3032         CODA_IMX27,
3033         CODA_IMX51,
3034         CODA_IMX53,
3035         CODA_IMX6Q,
3036         CODA_IMX6DL,
3037 };
3038
3039 static const struct coda_devtype coda_devdata[] = {
3040         [CODA_IMX27] = {
3041                 .firmware     = {
3042                         "vpu_fw_imx27_TO2.bin",
3043                         "vpu/vpu_fw_imx27_TO2.bin",
3044                         "v4l-codadx6-imx27.bin"
3045                 },
3046                 .product      = CODA_DX6,
3047                 .codecs       = codadx6_codecs,
3048                 .num_codecs   = ARRAY_SIZE(codadx6_codecs),
3049                 .vdevs        = codadx6_video_devices,
3050                 .num_vdevs    = ARRAY_SIZE(codadx6_video_devices),
3051                 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
3052                 .iram_size    = 0xb000,
3053         },
3054         [CODA_IMX51] = {
3055                 .firmware     = {
3056                         "vpu_fw_imx51.bin",
3057                         "vpu/vpu_fw_imx51.bin",
3058                         "v4l-codahx4-imx51.bin"
3059                 },
3060                 .product      = CODA_HX4,
3061                 .codecs       = codahx4_codecs,
3062                 .num_codecs   = ARRAY_SIZE(codahx4_codecs),
3063                 .vdevs        = codahx4_video_devices,
3064                 .num_vdevs    = ARRAY_SIZE(codahx4_video_devices),
3065                 .workbuf_size = 128 * 1024,
3066                 .tempbuf_size = 304 * 1024,
3067                 .iram_size    = 0x14000,
3068         },
3069         [CODA_IMX53] = {
3070                 .firmware     = {
3071                         "vpu_fw_imx53.bin",
3072                         "vpu/vpu_fw_imx53.bin",
3073                         "v4l-coda7541-imx53.bin"
3074                 },
3075                 .product      = CODA_7541,
3076                 .codecs       = coda7_codecs,
3077                 .num_codecs   = ARRAY_SIZE(coda7_codecs),
3078                 .vdevs        = coda7_video_devices,
3079                 .num_vdevs    = ARRAY_SIZE(coda7_video_devices),
3080                 .workbuf_size = 128 * 1024,
3081                 .tempbuf_size = 304 * 1024,
3082                 .iram_size    = 0x14000,
3083         },
3084         [CODA_IMX6Q] = {
3085                 .firmware     = {
3086                         "vpu_fw_imx6q.bin",
3087                         "vpu/vpu_fw_imx6q.bin",
3088                         "v4l-coda960-imx6q.bin"
3089                 },
3090                 .product      = CODA_960,
3091                 .codecs       = coda9_codecs,
3092                 .num_codecs   = ARRAY_SIZE(coda9_codecs),
3093                 .vdevs        = coda9_video_devices,
3094                 .num_vdevs    = ARRAY_SIZE(coda9_video_devices),
3095                 .workbuf_size = 80 * 1024,
3096                 .tempbuf_size = 204 * 1024,
3097                 .iram_size    = 0x21000,
3098         },
3099         [CODA_IMX6DL] = {
3100                 .firmware     = {
3101                         "vpu_fw_imx6d.bin",
3102                         "vpu/vpu_fw_imx6d.bin",
3103                         "v4l-coda960-imx6dl.bin"
3104                 },
3105                 .product      = CODA_960,
3106                 .codecs       = coda9_codecs,
3107                 .num_codecs   = ARRAY_SIZE(coda9_codecs),
3108                 .vdevs        = coda9_video_devices,
3109                 .num_vdevs    = ARRAY_SIZE(coda9_video_devices),
3110                 .workbuf_size = 80 * 1024,
3111                 .tempbuf_size = 204 * 1024,
3112                 .iram_size    = 0x1f000, /* leave 4k for suspend code */
3113         },
3114 };
3115
3116 static const struct of_device_id coda_dt_ids[] = {
3117         { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
3118         { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
3119         { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
3120         { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3121         { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
3122         { /* sentinel */ }
3123 };
3124 MODULE_DEVICE_TABLE(of, coda_dt_ids);
3125
3126 static int coda_probe(struct platform_device *pdev)
3127 {
3128         struct device_node *np = pdev->dev.of_node;
3129         struct gen_pool *pool;
3130         struct coda_dev *dev;
3131         int ret, irq;
3132
3133         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3134         if (!dev)
3135                 return -ENOMEM;
3136
3137         dev->devtype = of_device_get_match_data(&pdev->dev);
3138
3139         dev->dev = &pdev->dev;
3140         dev->clk_per = devm_clk_get(&pdev->dev, "per");
3141         if (IS_ERR(dev->clk_per)) {
3142                 dev_err(&pdev->dev, "Could not get per clock\n");
3143                 return PTR_ERR(dev->clk_per);
3144         }
3145
3146         dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3147         if (IS_ERR(dev->clk_ahb)) {
3148                 dev_err(&pdev->dev, "Could not get ahb clock\n");
3149                 return PTR_ERR(dev->clk_ahb);
3150         }
3151
3152         /* Get  memory for physical registers */
3153         dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
3154         if (IS_ERR(dev->regs_base))
3155                 return PTR_ERR(dev->regs_base);
3156
3157         /* IRQ */
3158         irq = platform_get_irq_byname(pdev, "bit");
3159         if (irq < 0)
3160                 irq = platform_get_irq(pdev, 0);
3161         if (irq < 0)
3162                 return irq;
3163
3164         ret = devm_request_irq(&pdev->dev, irq, coda_irq_handler, 0,
3165                                CODA_NAME "-video", dev);
3166         if (ret < 0) {
3167                 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3168                 return ret;
3169         }
3170
3171         /* JPEG IRQ */
3172         if (dev->devtype->product == CODA_960) {
3173                 irq = platform_get_irq_byname(pdev, "jpeg");
3174                 if (irq < 0)
3175                         return irq;
3176
3177                 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
3178                                                 coda9_jpeg_irq_handler,
3179                                                 IRQF_ONESHOT, CODA_NAME "-jpeg",
3180                                                 dev);
3181                 if (ret < 0) {
3182                         dev_err(&pdev->dev, "failed to request jpeg irq\n");
3183                         return ret;
3184                 }
3185         }
3186
3187         dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
3188                                                               NULL);
3189         if (IS_ERR(dev->rstc)) {
3190                 ret = PTR_ERR(dev->rstc);
3191                 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3192                 return ret;
3193         }
3194
3195         /* Get IRAM pool from device tree */
3196         pool = of_gen_pool_get(np, "iram", 0);
3197         if (!pool) {
3198                 dev_err(&pdev->dev, "iram pool not available\n");
3199                 return -ENOMEM;
3200         }
3201         dev->iram_pool = pool;
3202
3203         /* Get vdoa_data if supported by the platform */
3204         dev->vdoa = coda_get_vdoa_data();
3205         if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
3206                 return -EPROBE_DEFER;
3207
3208         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3209         if (ret)
3210                 return ret;
3211
3212         ratelimit_default_init(&dev->mb_err_rs);
3213         mutex_init(&dev->dev_mutex);
3214         mutex_init(&dev->coda_mutex);
3215         ida_init(&dev->ida);
3216
3217         dev->debugfs_root = debugfs_create_dir("coda", NULL);
3218
3219         /* allocate auxiliary per-device buffers for the BIT processor */
3220         if (dev->devtype->product == CODA_DX6) {
3221                 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3222                                          dev->devtype->workbuf_size, "workbuf",
3223                                          dev->debugfs_root);
3224                 if (ret < 0)
3225                         goto err_v4l2_register;
3226         }
3227
3228         if (dev->devtype->tempbuf_size) {
3229                 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3230                                          dev->devtype->tempbuf_size, "tempbuf",
3231                                          dev->debugfs_root);
3232                 if (ret < 0)
3233                         goto err_v4l2_register;
3234         }
3235
3236         dev->iram.size = dev->devtype->iram_size;
3237         dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3238                                              &dev->iram.paddr);
3239         if (!dev->iram.vaddr) {
3240                 dev_warn(&pdev->dev, "unable to alloc iram\n");
3241         } else {
3242                 memset(dev->iram.vaddr, 0, dev->iram.size);
3243                 dev->iram.blob.data = dev->iram.vaddr;
3244                 dev->iram.blob.size = dev->iram.size;
3245                 dev->iram.dentry = debugfs_create_blob("iram", 0444,
3246                                                        dev->debugfs_root,
3247                                                        &dev->iram.blob);
3248         }
3249
3250         dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3251         if (!dev->workqueue) {
3252                 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3253                 ret = -ENOMEM;
3254                 goto err_v4l2_register;
3255         }
3256
3257         platform_set_drvdata(pdev, dev);
3258
3259         /*
3260          * Start activated so we can directly call coda_hw_init in
3261          * coda_fw_callback regardless of whether CONFIG_PM is
3262          * enabled or whether the device is associated with a PM domain.
3263          */
3264         pm_runtime_get_noresume(&pdev->dev);
3265         pm_runtime_set_active(&pdev->dev);
3266         pm_runtime_enable(&pdev->dev);
3267
3268         ret = coda_firmware_request(dev);
3269         if (ret)
3270                 goto err_alloc_workqueue;
3271         return 0;
3272
3273 err_alloc_workqueue:
3274         pm_runtime_disable(&pdev->dev);
3275         pm_runtime_put_noidle(&pdev->dev);
3276         destroy_workqueue(dev->workqueue);
3277 err_v4l2_register:
3278         v4l2_device_unregister(&dev->v4l2_dev);
3279         return ret;
3280 }
3281
3282 static int coda_remove(struct platform_device *pdev)
3283 {
3284         struct coda_dev *dev = platform_get_drvdata(pdev);
3285         int i;
3286
3287         for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
3288                 if (video_get_drvdata(&dev->vfd[i]))
3289                         video_unregister_device(&dev->vfd[i]);
3290         }
3291         if (dev->m2m_dev)
3292                 v4l2_m2m_release(dev->m2m_dev);
3293         pm_runtime_disable(&pdev->dev);
3294         v4l2_device_unregister(&dev->v4l2_dev);
3295         destroy_workqueue(dev->workqueue);
3296         if (dev->iram.vaddr)
3297                 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3298                               dev->iram.size);
3299         coda_free_aux_buf(dev, &dev->codebuf);
3300         coda_free_aux_buf(dev, &dev->tempbuf);
3301         coda_free_aux_buf(dev, &dev->workbuf);
3302         debugfs_remove_recursive(dev->debugfs_root);
3303         ida_destroy(&dev->ida);
3304         return 0;
3305 }
3306
3307 #ifdef CONFIG_PM
3308 static int coda_runtime_resume(struct device *dev)
3309 {
3310         struct coda_dev *cdev = dev_get_drvdata(dev);
3311         int ret = 0;
3312
3313         if (dev->pm_domain && cdev->codebuf.vaddr) {
3314                 ret = coda_hw_init(cdev);
3315                 if (ret)
3316                         v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3317         }
3318
3319         return ret;
3320 }
3321 #endif
3322
3323 static const struct dev_pm_ops coda_pm_ops = {
3324         SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3325 };
3326
3327 static struct platform_driver coda_driver = {
3328         .probe  = coda_probe,
3329         .remove = coda_remove,
3330         .driver = {
3331                 .name   = CODA_NAME,
3332                 .of_match_table = coda_dt_ids,
3333                 .pm     = &coda_pm_ops,
3334         },
3335 };
3336
3337 module_platform_driver(coda_driver);
3338
3339 MODULE_LICENSE("GPL");
3340 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3341 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");