c82ce562d95a0c868e18b14c50b26224976ffe74
[cascardo/linux.git] / drivers / media / platform / coda / coda-common.c
1 /*
2  * Coda multi-standard codec IP
3  *
4  * Copyright (C) 2012 Vista Silicon S.L.
5  *    Javier Martin, <javier.martin@vista-silicon.com>
6  *    Xavier Duret
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/debugfs.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/genalloc.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/irq.h>
22 #include <linux/kfifo.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/videodev2.h>
29 #include <linux/of.h>
30 #include <linux/platform_data/coda.h>
31 #include <linux/reset.h>
32
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-event.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-mem2mem.h>
38 #include <media/videobuf2-core.h>
39 #include <media/videobuf2-dma-contig.h>
40 #include <media/videobuf2-vmalloc.h>
41
42 #include "coda.h"
43
44 #define CODA_NAME               "coda"
45
46 #define CODADX6_MAX_INSTANCES   4
47 #define CODA_MAX_FORMATS        4
48
49 #define CODA_PARA_BUF_SIZE      (10 * 1024)
50 #define CODA_ISRAM_SIZE (2048 * 2)
51
52 #define MIN_W 176
53 #define MIN_H 144
54
55 #define S_ALIGN         1 /* multiple of 2 */
56 #define W_ALIGN         1 /* multiple of 2 */
57 #define H_ALIGN         1 /* multiple of 2 */
58
59 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
60
61 int coda_debug;
62 module_param(coda_debug, int, 0644);
63 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
64
65 struct coda_fmt {
66         char *name;
67         u32 fourcc;
68 };
69
70 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
71 {
72         v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
73                  "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
74         writel(data, dev->regs_base + reg);
75 }
76
77 unsigned int coda_read(struct coda_dev *dev, u32 reg)
78 {
79         u32 data;
80
81         data = readl(dev->regs_base + reg);
82         v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
83                  "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
84         return data;
85 }
86
87 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
88                      struct vb2_buffer *buf, unsigned int reg_y)
89 {
90         u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
91         u32 base_cb, base_cr;
92
93         switch (q_data->fourcc) {
94         case V4L2_PIX_FMT_YVU420:
95                 /* Switch Cb and Cr for YVU420 format */
96                 base_cr = base_y + q_data->bytesperline * q_data->height;
97                 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
98                 break;
99         case V4L2_PIX_FMT_YUV420:
100         case V4L2_PIX_FMT_NV12:
101         default:
102                 base_cb = base_y + q_data->bytesperline * q_data->height;
103                 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
104                 break;
105         case V4L2_PIX_FMT_YUV422P:
106                 base_cb = base_y + q_data->bytesperline * q_data->height;
107                 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
108         }
109
110         coda_write(ctx->dev, base_y, reg_y);
111         coda_write(ctx->dev, base_cb, reg_y + 4);
112         coda_write(ctx->dev, base_cr, reg_y + 8);
113 }
114
115 /*
116  * Array of all formats supported by any version of Coda:
117  */
118 static const struct coda_fmt coda_formats[] = {
119         {
120                 .name = "YUV 4:2:0 Planar, YCbCr",
121                 .fourcc = V4L2_PIX_FMT_YUV420,
122         },
123         {
124                 .name = "YUV 4:2:0 Planar, YCrCb",
125                 .fourcc = V4L2_PIX_FMT_YVU420,
126         },
127         {
128                 .name = "YUV 4:2:0 Partial interleaved Y/CbCr",
129                 .fourcc = V4L2_PIX_FMT_NV12,
130         },
131         {
132                 .name = "YUV 4:2:2 Planar, YCbCr",
133                 .fourcc = V4L2_PIX_FMT_YUV422P,
134         },
135         {
136                 .name = "H264 Encoded Stream",
137                 .fourcc = V4L2_PIX_FMT_H264,
138         },
139         {
140                 .name = "MPEG4 Encoded Stream",
141                 .fourcc = V4L2_PIX_FMT_MPEG4,
142         },
143         {
144                 .name = "JPEG Encoded Images",
145                 .fourcc = V4L2_PIX_FMT_JPEG,
146         },
147 };
148
149 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
150         { mode, src_fourcc, dst_fourcc, max_w, max_h }
151
152 /*
153  * Arrays of codecs supported by each given version of Coda:
154  *  i.MX27 -> codadx6
155  *  i.MX5x -> coda7
156  *  i.MX6  -> coda960
157  * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
158  */
159 static const struct coda_codec codadx6_codecs[] = {
160         CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,  720, 576),
161         CODA_CODEC(CODADX6_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
162 };
163
164 static const struct coda_codec coda7_codecs[] = {
165         CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1280, 720),
166         CODA_CODEC(CODA7_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1280, 720),
167         CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG,   8192, 8192),
168         CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
169         CODA_CODEC(CODA7_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
170         CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG,   V4L2_PIX_FMT_YUV420, 8192, 8192),
171 };
172
173 static const struct coda_codec coda9_codecs[] = {
174         CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264,   1920, 1088),
175         CODA_CODEC(CODA9_MODE_ENCODE_MP4,  V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4,  1920, 1088),
176         CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264,   V4L2_PIX_FMT_YUV420, 1920, 1088),
177         CODA_CODEC(CODA9_MODE_DECODE_MP4,  V4L2_PIX_FMT_MPEG4,  V4L2_PIX_FMT_YUV420, 1920, 1088),
178 };
179
180 struct coda_video_device {
181         const char *name;
182         enum coda_inst_type type;
183         const struct coda_context_ops *ops;
184         bool direct;
185         u32 src_formats[CODA_MAX_FORMATS];
186         u32 dst_formats[CODA_MAX_FORMATS];
187 };
188
189 static const struct coda_video_device coda_bit_encoder = {
190         .name = "coda-encoder",
191         .type = CODA_INST_ENCODER,
192         .ops = &coda_bit_encode_ops,
193         .src_formats = {
194                 V4L2_PIX_FMT_YUV420,
195                 V4L2_PIX_FMT_YVU420,
196                 V4L2_PIX_FMT_NV12,
197         },
198         .dst_formats = {
199                 V4L2_PIX_FMT_H264,
200                 V4L2_PIX_FMT_MPEG4,
201         },
202 };
203
204 static const struct coda_video_device coda_bit_jpeg_encoder = {
205         .name = "coda-jpeg-encoder",
206         .type = CODA_INST_ENCODER,
207         .ops = &coda_bit_encode_ops,
208         .src_formats = {
209                 V4L2_PIX_FMT_YUV420,
210                 V4L2_PIX_FMT_YVU420,
211                 V4L2_PIX_FMT_NV12,
212                 V4L2_PIX_FMT_YUV422P,
213         },
214         .dst_formats = {
215                 V4L2_PIX_FMT_JPEG,
216         },
217 };
218
219 static const struct coda_video_device coda_bit_decoder = {
220         .name = "coda-decoder",
221         .type = CODA_INST_DECODER,
222         .ops = &coda_bit_decode_ops,
223         .src_formats = {
224                 V4L2_PIX_FMT_H264,
225                 V4L2_PIX_FMT_MPEG4,
226         },
227         .dst_formats = {
228                 V4L2_PIX_FMT_YUV420,
229                 V4L2_PIX_FMT_YVU420,
230                 V4L2_PIX_FMT_NV12,
231         },
232 };
233
234 static const struct coda_video_device coda_bit_jpeg_decoder = {
235         .name = "coda-jpeg-decoder",
236         .type = CODA_INST_DECODER,
237         .ops = &coda_bit_decode_ops,
238         .src_formats = {
239                 V4L2_PIX_FMT_JPEG,
240         },
241         .dst_formats = {
242                 V4L2_PIX_FMT_YUV420,
243                 V4L2_PIX_FMT_YVU420,
244                 V4L2_PIX_FMT_NV12,
245                 V4L2_PIX_FMT_YUV422P,
246         },
247 };
248
249 static const struct coda_video_device *codadx6_video_devices[] = {
250         &coda_bit_encoder,
251 };
252
253 static const struct coda_video_device *coda7_video_devices[] = {
254         &coda_bit_jpeg_encoder,
255         &coda_bit_jpeg_decoder,
256         &coda_bit_encoder,
257         &coda_bit_decoder,
258 };
259
260 static const struct coda_video_device *coda9_video_devices[] = {
261         &coda_bit_encoder,
262         &coda_bit_decoder,
263 };
264
265 static bool coda_format_is_yuv(u32 fourcc)
266 {
267         switch (fourcc) {
268         case V4L2_PIX_FMT_YUV420:
269         case V4L2_PIX_FMT_YVU420:
270         case V4L2_PIX_FMT_NV12:
271         case V4L2_PIX_FMT_YUV422P:
272                 return true;
273         default:
274                 return false;
275         }
276 }
277
278 static const char *coda_format_name(u32 fourcc)
279 {
280         int i;
281
282         for (i = 0; i < ARRAY_SIZE(coda_formats); i++) {
283                 if (coda_formats[i].fourcc == fourcc)
284                         return coda_formats[i].name;
285         }
286
287         return NULL;
288 }
289
290 /*
291  * Normalize all supported YUV 4:2:0 formats to the value used in the codec
292  * tables.
293  */
294 static u32 coda_format_normalize_yuv(u32 fourcc)
295 {
296         return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
297 }
298
299 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
300                                                 int src_fourcc, int dst_fourcc)
301 {
302         const struct coda_codec *codecs = dev->devtype->codecs;
303         int num_codecs = dev->devtype->num_codecs;
304         int k;
305
306         src_fourcc = coda_format_normalize_yuv(src_fourcc);
307         dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
308         if (src_fourcc == dst_fourcc)
309                 return NULL;
310
311         for (k = 0; k < num_codecs; k++) {
312                 if (codecs[k].src_fourcc == src_fourcc &&
313                     codecs[k].dst_fourcc == dst_fourcc)
314                         break;
315         }
316
317         if (k == num_codecs)
318                 return NULL;
319
320         return &codecs[k];
321 }
322
323 static void coda_get_max_dimensions(struct coda_dev *dev,
324                                     const struct coda_codec *codec,
325                                     int *max_w, int *max_h)
326 {
327         const struct coda_codec *codecs = dev->devtype->codecs;
328         int num_codecs = dev->devtype->num_codecs;
329         unsigned int w, h;
330         int k;
331
332         if (codec) {
333                 w = codec->max_w;
334                 h = codec->max_h;
335         } else {
336                 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
337                         w = max(w, codecs[k].max_w);
338                         h = max(h, codecs[k].max_h);
339                 }
340         }
341
342         if (max_w)
343                 *max_w = w;
344         if (max_h)
345                 *max_h = h;
346 }
347
348 const struct coda_video_device *to_coda_video_device(struct video_device *vdev)
349 {
350         struct coda_dev *dev = video_get_drvdata(vdev);
351         unsigned int i = vdev - dev->vfd;
352
353         if (i >= dev->devtype->num_vdevs)
354                 return NULL;
355
356         return dev->devtype->vdevs[i];
357 }
358
359 const char *coda_product_name(int product)
360 {
361         static char buf[9];
362
363         switch (product) {
364         case CODA_DX6:
365                 return "CodaDx6";
366         case CODA_7541:
367                 return "CODA7541";
368         case CODA_960:
369                 return "CODA960";
370         default:
371                 snprintf(buf, sizeof(buf), "(0x%04x)", product);
372                 return buf;
373         }
374 }
375
376 /*
377  * V4L2 ioctl() operations.
378  */
379 static int coda_querycap(struct file *file, void *priv,
380                          struct v4l2_capability *cap)
381 {
382         struct coda_ctx *ctx = fh_to_ctx(priv);
383
384         strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
385         strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
386                 sizeof(cap->card));
387         strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
388         cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
389         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
390
391         return 0;
392 }
393
394 static int coda_enum_fmt(struct file *file, void *priv,
395                          struct v4l2_fmtdesc *f)
396 {
397         struct video_device *vdev = video_devdata(file);
398         const struct coda_video_device *cvd = to_coda_video_device(vdev);
399         const u32 *formats;
400         const char *name;
401
402         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
403                 formats = cvd->src_formats;
404         else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
405                 formats = cvd->dst_formats;
406         else
407                 return -EINVAL;
408
409         if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
410                 return -EINVAL;
411
412         name = coda_format_name(formats[f->index]);
413         strlcpy(f->description, name, sizeof(f->description));
414         f->pixelformat = formats[f->index];
415         if (!coda_format_is_yuv(formats[f->index]))
416                 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
417
418         return 0;
419 }
420
421 static int coda_g_fmt(struct file *file, void *priv,
422                       struct v4l2_format *f)
423 {
424         struct coda_q_data *q_data;
425         struct coda_ctx *ctx = fh_to_ctx(priv);
426
427         q_data = get_q_data(ctx, f->type);
428         if (!q_data)
429                 return -EINVAL;
430
431         f->fmt.pix.field        = V4L2_FIELD_NONE;
432         f->fmt.pix.pixelformat  = q_data->fourcc;
433         f->fmt.pix.width        = q_data->width;
434         f->fmt.pix.height       = q_data->height;
435         f->fmt.pix.bytesperline = q_data->bytesperline;
436
437         f->fmt.pix.sizeimage    = q_data->sizeimage;
438         if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
439                 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
440         else
441                 f->fmt.pix.colorspace = ctx->colorspace;
442
443         return 0;
444 }
445
446 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
447 {
448         struct coda_q_data *q_data;
449         const u32 *formats;
450         int i;
451
452         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
453                 formats = ctx->cvd->src_formats;
454         else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
455                 formats = ctx->cvd->dst_formats;
456         else
457                 return -EINVAL;
458
459         for (i = 0; i < CODA_MAX_FORMATS; i++) {
460                 if (formats[i] == f->fmt.pix.pixelformat) {
461                         f->fmt.pix.pixelformat = formats[i];
462                         return 0;
463                 }
464         }
465
466         /* Fall back to currently set pixelformat */
467         q_data = get_q_data(ctx, f->type);
468         f->fmt.pix.pixelformat = q_data->fourcc;
469
470         return 0;
471 }
472
473 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
474                                             u32 width, u32 height)
475 {
476         /*
477          * This is a rough estimate for sensible compressed buffer
478          * sizes (between 1 and 16 bits per pixel). This could be
479          * improved by better format specific worst case estimates.
480          */
481         return round_up(clamp(sizeimage, width * height / 8,
482                                          width * height * 2), PAGE_SIZE);
483 }
484
485 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
486                         struct v4l2_format *f)
487 {
488         struct coda_dev *dev = ctx->dev;
489         unsigned int max_w, max_h;
490         enum v4l2_field field;
491
492         field = f->fmt.pix.field;
493         if (field == V4L2_FIELD_ANY)
494                 field = V4L2_FIELD_NONE;
495         else if (V4L2_FIELD_NONE != field)
496                 return -EINVAL;
497
498         /* V4L2 specification suggests the driver corrects the format struct
499          * if any of the dimensions is unsupported */
500         f->fmt.pix.field = field;
501
502         coda_get_max_dimensions(dev, codec, &max_w, &max_h);
503         v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
504                               &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
505                               S_ALIGN);
506
507         switch (f->fmt.pix.pixelformat) {
508         case V4L2_PIX_FMT_YUV420:
509         case V4L2_PIX_FMT_YVU420:
510         case V4L2_PIX_FMT_NV12:
511                 /*
512                  * Frame stride must be at least multiple of 8,
513                  * but multiple of 16 for h.264 or JPEG 4:2:x
514                  */
515                 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
516                 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
517                                         f->fmt.pix.height * 3 / 2;
518                 break;
519         case V4L2_PIX_FMT_YUV422P:
520                 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
521                 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
522                                         f->fmt.pix.height * 2;
523                 break;
524         case V4L2_PIX_FMT_JPEG:
525                 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
526                 /* fallthrough */
527         case V4L2_PIX_FMT_H264:
528         case V4L2_PIX_FMT_MPEG4:
529                 f->fmt.pix.bytesperline = 0;
530                 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
531                                                         f->fmt.pix.sizeimage,
532                                                         f->fmt.pix.width,
533                                                         f->fmt.pix.height);
534                 break;
535         default:
536                 BUG();
537         }
538
539         return 0;
540 }
541
542 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
543                                 struct v4l2_format *f)
544 {
545         struct coda_ctx *ctx = fh_to_ctx(priv);
546         const struct coda_q_data *q_data_src;
547         const struct coda_codec *codec;
548         struct vb2_queue *src_vq;
549         int ret;
550
551         ret = coda_try_pixelformat(ctx, f);
552         if (ret < 0)
553                 return ret;
554
555         q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
556
557         /*
558          * If the source format is already fixed, only allow the same output
559          * resolution
560          */
561         src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
562         if (vb2_is_streaming(src_vq)) {
563                 f->fmt.pix.width = q_data_src->width;
564                 f->fmt.pix.height = q_data_src->height;
565         }
566
567         f->fmt.pix.colorspace = ctx->colorspace;
568
569         q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
570         codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
571                                 f->fmt.pix.pixelformat);
572         if (!codec)
573                 return -EINVAL;
574
575         ret = coda_try_fmt(ctx, codec, f);
576         if (ret < 0)
577                 return ret;
578
579         /* The h.264 decoder only returns complete 16x16 macroblocks */
580         if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
581                 f->fmt.pix.width = f->fmt.pix.width;
582                 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
583                 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
584                 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
585                                        f->fmt.pix.height * 3 / 2;
586         }
587
588         return 0;
589 }
590
591 static int coda_try_fmt_vid_out(struct file *file, void *priv,
592                                 struct v4l2_format *f)
593 {
594         struct coda_ctx *ctx = fh_to_ctx(priv);
595         struct coda_dev *dev = ctx->dev;
596         const struct coda_q_data *q_data_dst;
597         const struct coda_codec *codec;
598         int ret;
599
600         ret = coda_try_pixelformat(ctx, f);
601         if (ret < 0)
602                 return ret;
603
604         switch (f->fmt.pix.colorspace) {
605         case V4L2_COLORSPACE_REC709:
606         case V4L2_COLORSPACE_JPEG:
607                 break;
608         default:
609                 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
610                         f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
611                 else
612                         f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
613         }
614
615         q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
616         codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
617
618         return coda_try_fmt(ctx, codec, f);
619 }
620
621 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
622 {
623         struct coda_q_data *q_data;
624         struct vb2_queue *vq;
625
626         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
627         if (!vq)
628                 return -EINVAL;
629
630         q_data = get_q_data(ctx, f->type);
631         if (!q_data)
632                 return -EINVAL;
633
634         if (vb2_is_busy(vq)) {
635                 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
636                 return -EBUSY;
637         }
638
639         q_data->fourcc = f->fmt.pix.pixelformat;
640         q_data->width = f->fmt.pix.width;
641         q_data->height = f->fmt.pix.height;
642         q_data->bytesperline = f->fmt.pix.bytesperline;
643         q_data->sizeimage = f->fmt.pix.sizeimage;
644         q_data->rect.left = 0;
645         q_data->rect.top = 0;
646         q_data->rect.width = f->fmt.pix.width;
647         q_data->rect.height = f->fmt.pix.height;
648
649         v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
650                 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
651                 f->type, q_data->width, q_data->height, q_data->fourcc);
652
653         return 0;
654 }
655
656 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
657                               struct v4l2_format *f)
658 {
659         struct coda_ctx *ctx = fh_to_ctx(priv);
660         int ret;
661
662         ret = coda_try_fmt_vid_cap(file, priv, f);
663         if (ret)
664                 return ret;
665
666         return coda_s_fmt(ctx, f);
667 }
668
669 static int coda_s_fmt_vid_out(struct file *file, void *priv,
670                               struct v4l2_format *f)
671 {
672         struct coda_ctx *ctx = fh_to_ctx(priv);
673         struct v4l2_format f_cap;
674         int ret;
675
676         ret = coda_try_fmt_vid_out(file, priv, f);
677         if (ret)
678                 return ret;
679
680         ret = coda_s_fmt(ctx, f);
681         if (ret)
682                 return ret;
683
684         ctx->colorspace = f->fmt.pix.colorspace;
685
686         memset(&f_cap, 0, sizeof(f_cap));
687         f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
688         coda_g_fmt(file, priv, &f_cap);
689         f_cap.fmt.pix.width = f->fmt.pix.width;
690         f_cap.fmt.pix.height = f->fmt.pix.height;
691
692         ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
693         if (ret)
694                 return ret;
695
696         return coda_s_fmt(ctx, &f_cap);
697 }
698
699 static int coda_qbuf(struct file *file, void *priv,
700                      struct v4l2_buffer *buf)
701 {
702         struct coda_ctx *ctx = fh_to_ctx(priv);
703
704         return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
705 }
706
707 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
708                                       struct v4l2_buffer *buf)
709 {
710         struct vb2_queue *src_vq;
711
712         src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
713
714         return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
715                 (buf->sequence == (ctx->qsequence - 1)));
716 }
717
718 static int coda_dqbuf(struct file *file, void *priv,
719                       struct v4l2_buffer *buf)
720 {
721         struct coda_ctx *ctx = fh_to_ctx(priv);
722         int ret;
723
724         ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
725
726         /* If this is the last capture buffer, emit an end-of-stream event */
727         if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
728             coda_buf_is_end_of_stream(ctx, buf)) {
729                 const struct v4l2_event eos_event = {
730                         .type = V4L2_EVENT_EOS
731                 };
732
733                 v4l2_event_queue_fh(&ctx->fh, &eos_event);
734         }
735
736         return ret;
737 }
738
739 static int coda_g_selection(struct file *file, void *fh,
740                             struct v4l2_selection *s)
741 {
742         struct coda_ctx *ctx = fh_to_ctx(fh);
743         struct coda_q_data *q_data;
744         struct v4l2_rect r, *rsel;
745
746         q_data = get_q_data(ctx, s->type);
747         if (!q_data)
748                 return -EINVAL;
749
750         r.left = 0;
751         r.top = 0;
752         r.width = q_data->width;
753         r.height = q_data->height;
754         rsel = &q_data->rect;
755
756         switch (s->target) {
757         case V4L2_SEL_TGT_CROP_DEFAULT:
758         case V4L2_SEL_TGT_CROP_BOUNDS:
759                 rsel = &r;
760                 /* fallthrough */
761         case V4L2_SEL_TGT_CROP:
762                 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
763                         return -EINVAL;
764                 break;
765         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
766         case V4L2_SEL_TGT_COMPOSE_PADDED:
767                 rsel = &r;
768                 /* fallthrough */
769         case V4L2_SEL_TGT_COMPOSE:
770         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
771                 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
772                         return -EINVAL;
773                 break;
774         default:
775                 return -EINVAL;
776         }
777
778         s->r = *rsel;
779
780         return 0;
781 }
782
783 static int coda_try_decoder_cmd(struct file *file, void *fh,
784                                 struct v4l2_decoder_cmd *dc)
785 {
786         if (dc->cmd != V4L2_DEC_CMD_STOP)
787                 return -EINVAL;
788
789         if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
790                 return -EINVAL;
791
792         if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
793                 return -EINVAL;
794
795         return 0;
796 }
797
798 static int coda_decoder_cmd(struct file *file, void *fh,
799                             struct v4l2_decoder_cmd *dc)
800 {
801         struct coda_ctx *ctx = fh_to_ctx(fh);
802         int ret;
803
804         ret = coda_try_decoder_cmd(file, fh, dc);
805         if (ret < 0)
806                 return ret;
807
808         /* Ignore decoder stop command silently in encoder context */
809         if (ctx->inst_type != CODA_INST_DECODER)
810                 return 0;
811
812         /* Set the stream-end flag on this context */
813         coda_bit_stream_end_flag(ctx);
814         ctx->hold = false;
815         v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
816
817         return 0;
818 }
819
820 static int coda_subscribe_event(struct v4l2_fh *fh,
821                                 const struct v4l2_event_subscription *sub)
822 {
823         switch (sub->type) {
824         case V4L2_EVENT_EOS:
825                 return v4l2_event_subscribe(fh, sub, 0, NULL);
826         default:
827                 return v4l2_ctrl_subscribe_event(fh, sub);
828         }
829 }
830
831 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
832         .vidioc_querycap        = coda_querycap,
833
834         .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
835         .vidioc_g_fmt_vid_cap   = coda_g_fmt,
836         .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
837         .vidioc_s_fmt_vid_cap   = coda_s_fmt_vid_cap,
838
839         .vidioc_enum_fmt_vid_out = coda_enum_fmt,
840         .vidioc_g_fmt_vid_out   = coda_g_fmt,
841         .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
842         .vidioc_s_fmt_vid_out   = coda_s_fmt_vid_out,
843
844         .vidioc_reqbufs         = v4l2_m2m_ioctl_reqbufs,
845         .vidioc_querybuf        = v4l2_m2m_ioctl_querybuf,
846
847         .vidioc_qbuf            = coda_qbuf,
848         .vidioc_expbuf          = v4l2_m2m_ioctl_expbuf,
849         .vidioc_dqbuf           = coda_dqbuf,
850         .vidioc_create_bufs     = v4l2_m2m_ioctl_create_bufs,
851
852         .vidioc_streamon        = v4l2_m2m_ioctl_streamon,
853         .vidioc_streamoff       = v4l2_m2m_ioctl_streamoff,
854
855         .vidioc_g_selection     = coda_g_selection,
856
857         .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
858         .vidioc_decoder_cmd     = coda_decoder_cmd,
859
860         .vidioc_subscribe_event = coda_subscribe_event,
861         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
862 };
863
864 void coda_set_gdi_regs(struct coda_ctx *ctx)
865 {
866         struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
867         struct coda_dev *dev = ctx->dev;
868         int i;
869
870         for (i = 0; i < 16; i++)
871                 coda_write(dev, tiled_map->xy2ca_map[i],
872                                 CODA9_GDI_XY2_CAS_0 + 4 * i);
873         for (i = 0; i < 4; i++)
874                 coda_write(dev, tiled_map->xy2ba_map[i],
875                                 CODA9_GDI_XY2_BA_0 + 4 * i);
876         for (i = 0; i < 16; i++)
877                 coda_write(dev, tiled_map->xy2ra_map[i],
878                                 CODA9_GDI_XY2_RAS_0 + 4 * i);
879         coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
880         for (i = 0; i < 32; i++)
881                 coda_write(dev, tiled_map->rbc2axi_map[i],
882                                 CODA9_GDI_RBC2_AXI_0 + 4 * i);
883 }
884
885 /*
886  * Mem-to-mem operations.
887  */
888
889 static void coda_device_run(void *m2m_priv)
890 {
891         struct coda_ctx *ctx = m2m_priv;
892         struct coda_dev *dev = ctx->dev;
893
894         queue_work(dev->workqueue, &ctx->pic_run_work);
895 }
896
897 static void coda_pic_run_work(struct work_struct *work)
898 {
899         struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
900         struct coda_dev *dev = ctx->dev;
901         int ret;
902
903         mutex_lock(&ctx->buffer_mutex);
904         mutex_lock(&dev->coda_mutex);
905
906         ret = ctx->ops->prepare_run(ctx);
907         if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
908                 mutex_unlock(&dev->coda_mutex);
909                 mutex_unlock(&ctx->buffer_mutex);
910                 /* job_finish scheduled by prepare_decode */
911                 return;
912         }
913
914         if (!wait_for_completion_timeout(&ctx->completion,
915                                          msecs_to_jiffies(1000))) {
916                 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
917
918                 ctx->hold = true;
919
920                 coda_hw_reset(ctx);
921         } else if (!ctx->aborting) {
922                 ctx->ops->finish_run(ctx);
923         }
924
925         if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
926             ctx->ops->seq_end_work)
927                 queue_work(dev->workqueue, &ctx->seq_end_work);
928
929         mutex_unlock(&dev->coda_mutex);
930         mutex_unlock(&ctx->buffer_mutex);
931
932         v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
933 }
934
935 static int coda_job_ready(void *m2m_priv)
936 {
937         struct coda_ctx *ctx = m2m_priv;
938
939         /*
940          * For both 'P' and 'key' frame cases 1 picture
941          * and 1 frame are needed. In the decoder case,
942          * the compressed frame can be in the bitstream.
943          */
944         if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
945             ctx->inst_type != CODA_INST_DECODER) {
946                 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
947                          "not ready: not enough video buffers.\n");
948                 return 0;
949         }
950
951         if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
952                 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
953                          "not ready: not enough video capture buffers.\n");
954                 return 0;
955         }
956
957         if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
958                 struct list_head *meta;
959                 bool stream_end;
960                 int num_metas;
961                 int src_bufs;
962
963                 if (ctx->hold && !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
964                         v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
965                                  "%d: not ready: on hold for more buffers.\n",
966                                  ctx->idx);
967                         return 0;
968                 }
969
970                 stream_end = ctx->bit_stream_param &
971                              CODA_BIT_STREAM_END_FLAG;
972
973                 num_metas = 0;
974                 list_for_each(meta, &ctx->buffer_meta_list)
975                         num_metas++;
976
977                 src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
978
979                 if (!stream_end && (num_metas + src_bufs) < 2) {
980                         v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
981                                  "%d: not ready: need 2 buffers available (%d, %d)\n",
982                                  ctx->idx, num_metas, src_bufs);
983                         return 0;
984                 }
985
986
987                 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
988                     !stream_end && (coda_get_bitstream_payload(ctx) < 512)) {
989                         v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
990                                  "%d: not ready: not enough bitstream data (%d).\n",
991                                  ctx->idx, coda_get_bitstream_payload(ctx));
992                         return 0;
993                 }
994         }
995
996         if (ctx->aborting) {
997                 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
998                          "not ready: aborting\n");
999                 return 0;
1000         }
1001
1002         v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1003                         "job ready\n");
1004         return 1;
1005 }
1006
1007 static void coda_job_abort(void *priv)
1008 {
1009         struct coda_ctx *ctx = priv;
1010
1011         ctx->aborting = 1;
1012
1013         v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1014                  "Aborting task\n");
1015 }
1016
1017 static void coda_lock(void *m2m_priv)
1018 {
1019         struct coda_ctx *ctx = m2m_priv;
1020         struct coda_dev *pcdev = ctx->dev;
1021
1022         mutex_lock(&pcdev->dev_mutex);
1023 }
1024
1025 static void coda_unlock(void *m2m_priv)
1026 {
1027         struct coda_ctx *ctx = m2m_priv;
1028         struct coda_dev *pcdev = ctx->dev;
1029
1030         mutex_unlock(&pcdev->dev_mutex);
1031 }
1032
1033 static const struct v4l2_m2m_ops coda_m2m_ops = {
1034         .device_run     = coda_device_run,
1035         .job_ready      = coda_job_ready,
1036         .job_abort      = coda_job_abort,
1037         .lock           = coda_lock,
1038         .unlock         = coda_unlock,
1039 };
1040
1041 static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1042 {
1043         struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1044         int luma_map, chro_map, i;
1045
1046         memset(tiled_map, 0, sizeof(*tiled_map));
1047
1048         luma_map = 64;
1049         chro_map = 64;
1050         tiled_map->map_type = tiled_map_type;
1051         for (i = 0; i < 16; i++)
1052                 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1053         for (i = 0; i < 4; i++)
1054                 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1055         for (i = 0; i < 16; i++)
1056                 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1057
1058         if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1059                 tiled_map->xy2rbc_config = 0;
1060         } else {
1061                 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1062                         tiled_map_type);
1063                 return;
1064         }
1065 }
1066
1067 static void set_default_params(struct coda_ctx *ctx)
1068 {
1069         unsigned int max_w, max_h, usize, csize;
1070
1071         ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1072                                      ctx->cvd->dst_formats[0]);
1073         max_w = min(ctx->codec->max_w, 1920U);
1074         max_h = min(ctx->codec->max_h, 1088U);
1075         usize = max_w * max_h * 3 / 2;
1076         csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1077
1078         ctx->params.codec_mode = ctx->codec->mode;
1079         ctx->colorspace = V4L2_COLORSPACE_REC709;
1080         ctx->params.framerate = 30;
1081
1082         /* Default formats for output and input queues */
1083         ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1084         ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1085         ctx->q_data[V4L2_M2M_SRC].width = max_w;
1086         ctx->q_data[V4L2_M2M_SRC].height = max_h;
1087         ctx->q_data[V4L2_M2M_DST].width = max_w;
1088         ctx->q_data[V4L2_M2M_DST].height = max_h;
1089         if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1090                 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1091                 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1092                 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1093                 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1094         } else {
1095                 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1096                 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1097                 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1098                 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1099         }
1100         ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1101         ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1102         ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1103         ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1104
1105         if (ctx->dev->devtype->product == CODA_960)
1106                 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
1107 }
1108
1109 /*
1110  * Queue operations
1111  */
1112 static int coda_queue_setup(struct vb2_queue *vq,
1113                                 const struct v4l2_format *fmt,
1114                                 unsigned int *nbuffers, unsigned int *nplanes,
1115                                 unsigned int sizes[], void *alloc_ctxs[])
1116 {
1117         struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1118         struct coda_q_data *q_data;
1119         unsigned int size;
1120
1121         q_data = get_q_data(ctx, vq->type);
1122         size = q_data->sizeimage;
1123
1124         *nplanes = 1;
1125         sizes[0] = size;
1126
1127         /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1128         alloc_ctxs[0] = ctx->dev->alloc_ctx;
1129
1130         v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1131                  "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1132
1133         return 0;
1134 }
1135
1136 static int coda_buf_prepare(struct vb2_buffer *vb)
1137 {
1138         struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1139         struct coda_q_data *q_data;
1140
1141         q_data = get_q_data(ctx, vb->vb2_queue->type);
1142
1143         if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1144                 v4l2_warn(&ctx->dev->v4l2_dev,
1145                           "%s data will not fit into plane (%lu < %lu)\n",
1146                           __func__, vb2_plane_size(vb, 0),
1147                           (long)q_data->sizeimage);
1148                 return -EINVAL;
1149         }
1150
1151         return 0;
1152 }
1153
1154 static void coda_buf_queue(struct vb2_buffer *vb)
1155 {
1156         struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1157         struct vb2_queue *vq = vb->vb2_queue;
1158         struct coda_q_data *q_data;
1159
1160         q_data = get_q_data(ctx, vb->vb2_queue->type);
1161
1162         /*
1163          * In the decoder case, immediately try to copy the buffer into the
1164          * bitstream ringbuffer and mark it as ready to be dequeued.
1165          */
1166         if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1167                 /*
1168                  * For backwards compatibility, queuing an empty buffer marks
1169                  * the stream end
1170                  */
1171                 if (vb2_get_plane_payload(vb, 0) == 0)
1172                         coda_bit_stream_end_flag(ctx);
1173                 mutex_lock(&ctx->bitstream_mutex);
1174                 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1175                 if (vb2_is_streaming(vb->vb2_queue))
1176                         coda_fill_bitstream(ctx);
1177                 mutex_unlock(&ctx->bitstream_mutex);
1178         } else {
1179                 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1180         }
1181 }
1182
1183 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1184                        size_t size, const char *name, struct dentry *parent)
1185 {
1186         buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1187                                         GFP_KERNEL);
1188         if (!buf->vaddr) {
1189                 v4l2_err(&dev->v4l2_dev,
1190                          "Failed to allocate %s buffer of size %u\n",
1191                          name, size);
1192                 return -ENOMEM;
1193         }
1194
1195         buf->size = size;
1196
1197         if (name && parent) {
1198                 buf->blob.data = buf->vaddr;
1199                 buf->blob.size = size;
1200                 buf->dentry = debugfs_create_blob(name, 0644, parent,
1201                                                   &buf->blob);
1202                 if (!buf->dentry)
1203                         dev_warn(&dev->plat_dev->dev,
1204                                  "failed to create debugfs entry %s\n", name);
1205         }
1206
1207         return 0;
1208 }
1209
1210 void coda_free_aux_buf(struct coda_dev *dev,
1211                        struct coda_aux_buf *buf)
1212 {
1213         if (buf->vaddr) {
1214                 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1215                                   buf->vaddr, buf->paddr);
1216                 buf->vaddr = NULL;
1217                 buf->size = 0;
1218         }
1219         debugfs_remove(buf->dentry);
1220 }
1221
1222 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1223 {
1224         struct coda_ctx *ctx = vb2_get_drv_priv(q);
1225         struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1226         struct coda_q_data *q_data_src, *q_data_dst;
1227         struct vb2_buffer *buf;
1228         int ret = 0;
1229
1230         q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1231         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1232                 if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
1233                     (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
1234                      ctx->dev->devtype->product == CODA_7541)) {
1235                         /* copy the buffers that where queued before streamon */
1236                         mutex_lock(&ctx->bitstream_mutex);
1237                         coda_fill_bitstream(ctx);
1238                         mutex_unlock(&ctx->bitstream_mutex);
1239
1240                         if (coda_get_bitstream_payload(ctx) < 512) {
1241                                 ret = -EINVAL;
1242                                 goto err;
1243                         }
1244                 } else {
1245                         if (count < 1) {
1246                                 ret = -EINVAL;
1247                                 goto err;
1248                         }
1249                 }
1250
1251                 ctx->streamon_out = 1;
1252         } else {
1253                 if (count < 1) {
1254                         ret = -EINVAL;
1255                         goto err;
1256                 }
1257
1258                 ctx->streamon_cap = 1;
1259         }
1260
1261         /* Don't start the coda unless both queues are on */
1262         if (!(ctx->streamon_out & ctx->streamon_cap))
1263                 return 0;
1264
1265         /* Allow BIT decoder device_run with no new buffers queued */
1266         if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1267                 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1268
1269         ctx->gopcounter = ctx->params.gop_size - 1;
1270         q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1271
1272         ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1273                                      q_data_dst->fourcc);
1274         if (!ctx->codec) {
1275                 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1276                 ret = -EINVAL;
1277                 goto err;
1278         }
1279
1280         if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1281                 ctx->params.gop_size = 1;
1282         ctx->gopcounter = ctx->params.gop_size - 1;
1283
1284         ret = ctx->ops->start_streaming(ctx);
1285         if (ctx->inst_type == CODA_INST_DECODER) {
1286                 if (ret == -EAGAIN)
1287                         return 0;
1288                 else if (ret < 0)
1289                         goto err;
1290         }
1291
1292         ctx->initialized = 1;
1293         return ret;
1294
1295 err:
1296         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1297                 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1298                         v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1299         } else {
1300                 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1301                         v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1302         }
1303         return ret;
1304 }
1305
1306 static void coda_stop_streaming(struct vb2_queue *q)
1307 {
1308         struct coda_ctx *ctx = vb2_get_drv_priv(q);
1309         struct coda_dev *dev = ctx->dev;
1310         struct vb2_buffer *buf;
1311
1312         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1313                 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1314                          "%s: output\n", __func__);
1315                 ctx->streamon_out = 0;
1316
1317                 coda_bit_stream_end_flag(ctx);
1318
1319                 ctx->qsequence = 0;
1320
1321                 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1322                         v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1323         } else {
1324                 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1325                          "%s: capture\n", __func__);
1326                 ctx->streamon_cap = 0;
1327
1328                 ctx->osequence = 0;
1329                 ctx->sequence_offset = 0;
1330
1331                 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1332                         v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1333         }
1334
1335         if (!ctx->streamon_out && !ctx->streamon_cap) {
1336                 struct coda_buffer_meta *meta;
1337
1338                 if (ctx->ops->seq_end_work) {
1339                         queue_work(dev->workqueue, &ctx->seq_end_work);
1340                         flush_work(&ctx->seq_end_work);
1341                 }
1342                 mutex_lock(&ctx->bitstream_mutex);
1343                 while (!list_empty(&ctx->buffer_meta_list)) {
1344                         meta = list_first_entry(&ctx->buffer_meta_list,
1345                                                 struct coda_buffer_meta, list);
1346                         list_del(&meta->list);
1347                         kfree(meta);
1348                 }
1349                 mutex_unlock(&ctx->bitstream_mutex);
1350                 kfifo_init(&ctx->bitstream_fifo,
1351                         ctx->bitstream.vaddr, ctx->bitstream.size);
1352                 ctx->initialized = 0;
1353                 ctx->runcounter = 0;
1354                 ctx->aborting = 0;
1355         }
1356 }
1357
1358 static const struct vb2_ops coda_qops = {
1359         .queue_setup            = coda_queue_setup,
1360         .buf_prepare            = coda_buf_prepare,
1361         .buf_queue              = coda_buf_queue,
1362         .start_streaming        = coda_start_streaming,
1363         .stop_streaming         = coda_stop_streaming,
1364         .wait_prepare           = vb2_ops_wait_prepare,
1365         .wait_finish            = vb2_ops_wait_finish,
1366 };
1367
1368 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1369 {
1370         struct coda_ctx *ctx =
1371                         container_of(ctrl->handler, struct coda_ctx, ctrls);
1372
1373         v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1374                  "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1375
1376         switch (ctrl->id) {
1377         case V4L2_CID_HFLIP:
1378                 if (ctrl->val)
1379                         ctx->params.rot_mode |= CODA_MIR_HOR;
1380                 else
1381                         ctx->params.rot_mode &= ~CODA_MIR_HOR;
1382                 break;
1383         case V4L2_CID_VFLIP:
1384                 if (ctrl->val)
1385                         ctx->params.rot_mode |= CODA_MIR_VER;
1386                 else
1387                         ctx->params.rot_mode &= ~CODA_MIR_VER;
1388                 break;
1389         case V4L2_CID_MPEG_VIDEO_BITRATE:
1390                 ctx->params.bitrate = ctrl->val / 1000;
1391                 break;
1392         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1393                 ctx->params.gop_size = ctrl->val;
1394                 break;
1395         case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1396                 ctx->params.h264_intra_qp = ctrl->val;
1397                 break;
1398         case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1399                 ctx->params.h264_inter_qp = ctrl->val;
1400                 break;
1401         case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1402                 ctx->params.h264_min_qp = ctrl->val;
1403                 break;
1404         case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1405                 ctx->params.h264_max_qp = ctrl->val;
1406                 break;
1407         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1408                 ctx->params.h264_deblk_alpha = ctrl->val;
1409                 break;
1410         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1411                 ctx->params.h264_deblk_beta = ctrl->val;
1412                 break;
1413         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1414                 ctx->params.h264_deblk_enabled = (ctrl->val ==
1415                                 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1416                 break;
1417         case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1418                 ctx->params.mpeg4_intra_qp = ctrl->val;
1419                 break;
1420         case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1421                 ctx->params.mpeg4_inter_qp = ctrl->val;
1422                 break;
1423         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1424                 ctx->params.slice_mode = ctrl->val;
1425                 break;
1426         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1427                 ctx->params.slice_max_mb = ctrl->val;
1428                 break;
1429         case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1430                 ctx->params.slice_max_bits = ctrl->val * 8;
1431                 break;
1432         case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1433                 break;
1434         case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1435                 ctx->params.intra_refresh = ctrl->val;
1436                 break;
1437         case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1438                 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1439                 break;
1440         case V4L2_CID_JPEG_RESTART_INTERVAL:
1441                 ctx->params.jpeg_restart_interval = ctrl->val;
1442                 break;
1443         default:
1444                 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1445                         "Invalid control, id=%d, val=%d\n",
1446                         ctrl->id, ctrl->val);
1447                 return -EINVAL;
1448         }
1449
1450         return 0;
1451 }
1452
1453 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1454         .s_ctrl = coda_s_ctrl,
1455 };
1456
1457 static void coda_encode_ctrls(struct coda_ctx *ctx)
1458 {
1459         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1460                 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1461         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1462                 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1463         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1464                 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1465         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1466                 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1467         if (ctx->dev->devtype->product != CODA_960) {
1468                 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1469                         V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1470         }
1471         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1472                 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1473         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1474                 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1475         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1476                 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1477         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1478                 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1479                 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1480                 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1481         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1482                 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1483         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1484                 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1485         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1486                 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1487                 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1488                 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1489         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1490                 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1491         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1492                 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1493                 500);
1494         v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1495                 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1496                 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1497                 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1498                 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1499         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1500                 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1501                 1920 * 1088 / 256, 1, 0);
1502 }
1503
1504 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1505 {
1506         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1507                 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1508         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1509                 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1510 }
1511
1512 static int coda_ctrls_setup(struct coda_ctx *ctx)
1513 {
1514         v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1515
1516         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1517                 V4L2_CID_HFLIP, 0, 1, 1, 0);
1518         v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1519                 V4L2_CID_VFLIP, 0, 1, 1, 0);
1520         if (ctx->inst_type == CODA_INST_ENCODER) {
1521                 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1522                         coda_jpeg_encode_ctrls(ctx);
1523                 else
1524                         coda_encode_ctrls(ctx);
1525         }
1526
1527         if (ctx->ctrls.error) {
1528                 v4l2_err(&ctx->dev->v4l2_dev,
1529                         "control initialization error (%d)",
1530                         ctx->ctrls.error);
1531                 return -EINVAL;
1532         }
1533
1534         return v4l2_ctrl_handler_setup(&ctx->ctrls);
1535 }
1536
1537 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1538 {
1539         vq->drv_priv = ctx;
1540         vq->ops = &coda_qops;
1541         vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1542         vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1543         vq->lock = &ctx->dev->dev_mutex;
1544         /* One way to indicate end-of-stream for coda is to set the
1545          * bytesused == 0. However by default videobuf2 handles bytesused
1546          * equal to 0 as a special case and changes its value to the size
1547          * of the buffer. Set the allow_zero_bytesused flag, so
1548          * that videobuf2 will keep the value of bytesused intact.
1549          */
1550         vq->allow_zero_bytesused = 1;
1551
1552         return vb2_queue_init(vq);
1553 }
1554
1555 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1556                             struct vb2_queue *dst_vq)
1557 {
1558         int ret;
1559
1560         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1561         src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1562         src_vq->mem_ops = &vb2_dma_contig_memops;
1563
1564         ret = coda_queue_init(priv, src_vq);
1565         if (ret)
1566                 return ret;
1567
1568         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1569         dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1570         dst_vq->mem_ops = &vb2_dma_contig_memops;
1571
1572         return coda_queue_init(priv, dst_vq);
1573 }
1574
1575 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1576                             struct vb2_queue *dst_vq)
1577 {
1578         int ret;
1579
1580         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1581         src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1582         src_vq->mem_ops = &vb2_vmalloc_memops;
1583
1584         ret = coda_queue_init(priv, src_vq);
1585         if (ret)
1586                 return ret;
1587
1588         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1589         dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1590         dst_vq->mem_ops = &vb2_dma_contig_memops;
1591
1592         return coda_queue_init(priv, dst_vq);
1593 }
1594
1595 static int coda_next_free_instance(struct coda_dev *dev)
1596 {
1597         int idx = ffz(dev->instance_mask);
1598
1599         if ((idx < 0) ||
1600             (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1601                 return -EBUSY;
1602
1603         return idx;
1604 }
1605
1606 /*
1607  * File operations
1608  */
1609
1610 static int coda_open(struct file *file)
1611 {
1612         struct video_device *vdev = video_devdata(file);
1613         struct coda_dev *dev = video_get_drvdata(vdev);
1614         struct coda_ctx *ctx = NULL;
1615         char *name;
1616         int ret;
1617         int idx;
1618
1619         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1620         if (!ctx)
1621                 return -ENOMEM;
1622
1623         idx = coda_next_free_instance(dev);
1624         if (idx < 0) {
1625                 ret = idx;
1626                 goto err_coda_max;
1627         }
1628         set_bit(idx, &dev->instance_mask);
1629
1630         name = kasprintf(GFP_KERNEL, "context%d", idx);
1631         if (!name) {
1632                 ret = -ENOMEM;
1633                 goto err_coda_name_init;
1634         }
1635
1636         ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1637         kfree(name);
1638
1639         ctx->cvd = to_coda_video_device(vdev);
1640         ctx->inst_type = ctx->cvd->type;
1641         ctx->ops = ctx->cvd->ops;
1642         ctx->use_bit = !ctx->cvd->direct;
1643         init_completion(&ctx->completion);
1644         INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
1645         if (ctx->ops->seq_end_work)
1646                 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1647         v4l2_fh_init(&ctx->fh, video_devdata(file));
1648         file->private_data = &ctx->fh;
1649         v4l2_fh_add(&ctx->fh);
1650         ctx->dev = dev;
1651         ctx->idx = idx;
1652         switch (dev->devtype->product) {
1653         case CODA_960:
1654                 ctx->frame_mem_ctrl = 1 << 12;
1655                 /* fallthrough */
1656         case CODA_7541:
1657                 ctx->reg_idx = 0;
1658                 break;
1659         default:
1660                 ctx->reg_idx = idx;
1661         }
1662
1663         /* Power up and upload firmware if necessary */
1664         ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1665         if (ret < 0) {
1666                 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1667                 goto err_pm_get;
1668         }
1669
1670         ret = clk_prepare_enable(dev->clk_per);
1671         if (ret)
1672                 goto err_clk_per;
1673
1674         ret = clk_prepare_enable(dev->clk_ahb);
1675         if (ret)
1676                 goto err_clk_ahb;
1677
1678         set_default_params(ctx);
1679         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1680                                             ctx->ops->queue_init);
1681         if (IS_ERR(ctx->fh.m2m_ctx)) {
1682                 ret = PTR_ERR(ctx->fh.m2m_ctx);
1683
1684                 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1685                          __func__, ret);
1686                 goto err_ctx_init;
1687         }
1688
1689         ret = coda_ctrls_setup(ctx);
1690         if (ret) {
1691                 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1692                 goto err_ctrls_setup;
1693         }
1694
1695         ctx->fh.ctrl_handler = &ctx->ctrls;
1696
1697         if (ctx->use_bit) {
1698                 ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
1699                                              CODA_PARA_BUF_SIZE, "parabuf");
1700                 if (ret < 0) {
1701                         v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1702                         goto err_dma_alloc;
1703                 }
1704         }
1705         if (ctx->use_bit && ctx->inst_type == CODA_INST_DECODER) {
1706                 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1707                 ctx->bitstream.vaddr = dma_alloc_writecombine(
1708                                 &dev->plat_dev->dev, ctx->bitstream.size,
1709                                 &ctx->bitstream.paddr, GFP_KERNEL);
1710                 if (!ctx->bitstream.vaddr) {
1711                         v4l2_err(&dev->v4l2_dev,
1712                                  "failed to allocate bitstream ringbuffer");
1713                         ret = -ENOMEM;
1714                         goto err_dma_writecombine;
1715                 }
1716         }
1717         kfifo_init(&ctx->bitstream_fifo,
1718                 ctx->bitstream.vaddr, ctx->bitstream.size);
1719         mutex_init(&ctx->bitstream_mutex);
1720         mutex_init(&ctx->buffer_mutex);
1721         INIT_LIST_HEAD(&ctx->buffer_meta_list);
1722
1723         coda_lock(ctx);
1724         list_add(&ctx->list, &dev->instances);
1725         coda_unlock(ctx);
1726
1727         v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1728                  ctx->idx, ctx);
1729
1730         return 0;
1731
1732 err_dma_writecombine:
1733         if (ctx->dev->devtype->product == CODA_DX6)
1734                 coda_free_aux_buf(dev, &ctx->workbuf);
1735         coda_free_aux_buf(dev, &ctx->parabuf);
1736 err_dma_alloc:
1737         v4l2_ctrl_handler_free(&ctx->ctrls);
1738 err_ctrls_setup:
1739         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1740 err_ctx_init:
1741         clk_disable_unprepare(dev->clk_ahb);
1742 err_clk_ahb:
1743         clk_disable_unprepare(dev->clk_per);
1744 err_clk_per:
1745         pm_runtime_put_sync(&dev->plat_dev->dev);
1746 err_pm_get:
1747         v4l2_fh_del(&ctx->fh);
1748         v4l2_fh_exit(&ctx->fh);
1749         clear_bit(ctx->idx, &dev->instance_mask);
1750 err_coda_name_init:
1751 err_coda_max:
1752         kfree(ctx);
1753         return ret;
1754 }
1755
1756 static int coda_release(struct file *file)
1757 {
1758         struct coda_dev *dev = video_drvdata(file);
1759         struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1760
1761         v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1762                  ctx);
1763
1764         if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1765                 coda_bit_stream_end_flag(ctx);
1766
1767         /* If this instance is running, call .job_abort and wait for it to end */
1768         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1769
1770         /* In case the instance was not running, we still need to call SEQ_END */
1771         if (ctx->initialized && ctx->ops->seq_end_work) {
1772                 queue_work(dev->workqueue, &ctx->seq_end_work);
1773                 flush_work(&ctx->seq_end_work);
1774         }
1775
1776         coda_lock(ctx);
1777         list_del(&ctx->list);
1778         coda_unlock(ctx);
1779
1780         if (ctx->bitstream.vaddr) {
1781                 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1782                         ctx->bitstream.vaddr, ctx->bitstream.paddr);
1783         }
1784         if (ctx->dev->devtype->product == CODA_DX6)
1785                 coda_free_aux_buf(dev, &ctx->workbuf);
1786
1787         coda_free_aux_buf(dev, &ctx->parabuf);
1788         v4l2_ctrl_handler_free(&ctx->ctrls);
1789         clk_disable_unprepare(dev->clk_ahb);
1790         clk_disable_unprepare(dev->clk_per);
1791         pm_runtime_put_sync(&dev->plat_dev->dev);
1792         v4l2_fh_del(&ctx->fh);
1793         v4l2_fh_exit(&ctx->fh);
1794         clear_bit(ctx->idx, &dev->instance_mask);
1795         if (ctx->ops->release)
1796                 ctx->ops->release(ctx);
1797         debugfs_remove_recursive(ctx->debugfs_entry);
1798         kfree(ctx);
1799
1800         return 0;
1801 }
1802
1803 static const struct v4l2_file_operations coda_fops = {
1804         .owner          = THIS_MODULE,
1805         .open           = coda_open,
1806         .release        = coda_release,
1807         .poll           = v4l2_m2m_fop_poll,
1808         .unlocked_ioctl = video_ioctl2,
1809         .mmap           = v4l2_m2m_fop_mmap,
1810 };
1811
1812 static int coda_hw_init(struct coda_dev *dev)
1813 {
1814         u32 data;
1815         u16 *p;
1816         int i, ret;
1817
1818         ret = clk_prepare_enable(dev->clk_per);
1819         if (ret)
1820                 goto err_clk_per;
1821
1822         ret = clk_prepare_enable(dev->clk_ahb);
1823         if (ret)
1824                 goto err_clk_ahb;
1825
1826         if (dev->rstc)
1827                 reset_control_reset(dev->rstc);
1828
1829         /*
1830          * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1831          * The 16-bit chars in the code buffer are in memory access
1832          * order, re-sort them to CODA order for register download.
1833          * Data in this SRAM survives a reboot.
1834          */
1835         p = (u16 *)dev->codebuf.vaddr;
1836         if (dev->devtype->product == CODA_DX6) {
1837                 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++)  {
1838                         data = CODA_DOWN_ADDRESS_SET(i) |
1839                                 CODA_DOWN_DATA_SET(p[i ^ 1]);
1840                         coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1841                 }
1842         } else {
1843                 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1844                         data = CODA_DOWN_ADDRESS_SET(i) |
1845                                 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1846                                                         3 - (i % 4)]);
1847                         coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1848                 }
1849         }
1850
1851         /* Clear registers */
1852         for (i = 0; i < 64; i++)
1853                 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1854
1855         /* Tell the BIT where to find everything it needs */
1856         if (dev->devtype->product == CODA_960 ||
1857             dev->devtype->product == CODA_7541) {
1858                 coda_write(dev, dev->tempbuf.paddr,
1859                                 CODA_REG_BIT_TEMP_BUF_ADDR);
1860                 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1861         } else {
1862                 coda_write(dev, dev->workbuf.paddr,
1863                               CODA_REG_BIT_WORK_BUF_ADDR);
1864         }
1865         coda_write(dev, dev->codebuf.paddr,
1866                       CODA_REG_BIT_CODE_BUF_ADDR);
1867         coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1868
1869         /* Set default values */
1870         switch (dev->devtype->product) {
1871         case CODA_DX6:
1872                 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1873                            CODA_REG_BIT_STREAM_CTRL);
1874                 break;
1875         default:
1876                 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1877                            CODA_REG_BIT_STREAM_CTRL);
1878         }
1879         if (dev->devtype->product == CODA_960)
1880                 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1881         else
1882                 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1883
1884         if (dev->devtype->product != CODA_DX6)
1885                 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1886
1887         coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1888                       CODA_REG_BIT_INT_ENABLE);
1889
1890         /* Reset VPU and start processor */
1891         data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1892         data |= CODA_REG_RESET_ENABLE;
1893         coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1894         udelay(10);
1895         data &= ~CODA_REG_RESET_ENABLE;
1896         coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1897         coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1898
1899         clk_disable_unprepare(dev->clk_ahb);
1900         clk_disable_unprepare(dev->clk_per);
1901
1902         return 0;
1903
1904 err_clk_ahb:
1905         clk_disable_unprepare(dev->clk_per);
1906 err_clk_per:
1907         return ret;
1908 }
1909
1910 static int coda_register_device(struct coda_dev *dev, int i)
1911 {
1912         struct video_device *vfd = &dev->vfd[i];
1913
1914         if (i >= dev->devtype->num_vdevs)
1915                 return -EINVAL;
1916
1917         snprintf(vfd->name, sizeof(vfd->name), "%s",
1918                  dev->devtype->vdevs[i]->name);
1919         vfd->fops       = &coda_fops;
1920         vfd->ioctl_ops  = &coda_ioctl_ops;
1921         vfd->release    = video_device_release_empty,
1922         vfd->lock       = &dev->dev_mutex;
1923         vfd->v4l2_dev   = &dev->v4l2_dev;
1924         vfd->vfl_dir    = VFL_DIR_M2M;
1925         video_set_drvdata(vfd, dev);
1926
1927         /* Not applicable, use the selection API instead */
1928         v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1929         v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1930         v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1931
1932         return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1933 }
1934
1935 static void coda_fw_callback(const struct firmware *fw, void *context)
1936 {
1937         struct coda_dev *dev = context;
1938         struct platform_device *pdev = dev->plat_dev;
1939         int i, ret;
1940
1941         if (!fw) {
1942                 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1943                 goto put_pm;
1944         }
1945
1946         /* allocate auxiliary per-device code buffer for the BIT processor */
1947         ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1948                                  dev->debugfs_root);
1949         if (ret < 0) {
1950                 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1951                 goto put_pm;
1952         }
1953
1954         /* Copy the whole firmware image to the code buffer */
1955         memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1956         release_firmware(fw);
1957
1958         ret = coda_hw_init(dev);
1959         if (ret < 0) {
1960                 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1961                 goto put_pm;
1962         }
1963
1964         ret = coda_check_firmware(dev);
1965         if (ret < 0)
1966                 goto put_pm;
1967
1968         dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1969         if (IS_ERR(dev->alloc_ctx)) {
1970                 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1971                 goto put_pm;
1972         }
1973
1974         dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1975         if (IS_ERR(dev->m2m_dev)) {
1976                 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1977                 goto rel_ctx;
1978         }
1979
1980         for (i = 0; i < dev->devtype->num_vdevs; i++) {
1981                 ret = coda_register_device(dev, i);
1982                 if (ret) {
1983                         v4l2_err(&dev->v4l2_dev,
1984                                  "Failed to register %s video device: %d\n",
1985                                  dev->devtype->vdevs[i]->name, ret);
1986                         goto rel_vfd;
1987                 }
1988         }
1989
1990         v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1991                   dev->vfd[0].num, dev->vfd[i - 1].num);
1992
1993         pm_runtime_put_sync(&pdev->dev);
1994         return;
1995
1996 rel_vfd:
1997         while (--i >= 0)
1998                 video_unregister_device(&dev->vfd[i]);
1999         v4l2_m2m_release(dev->m2m_dev);
2000 rel_ctx:
2001         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2002 put_pm:
2003         pm_runtime_put_sync(&pdev->dev);
2004 }
2005
2006 static int coda_firmware_request(struct coda_dev *dev)
2007 {
2008         char *fw = dev->devtype->firmware;
2009
2010         dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2011                 coda_product_name(dev->devtype->product));
2012
2013         return request_firmware_nowait(THIS_MODULE, true,
2014                 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2015 }
2016
2017 enum coda_platform {
2018         CODA_IMX27,
2019         CODA_IMX53,
2020         CODA_IMX6Q,
2021         CODA_IMX6DL,
2022 };
2023
2024 static const struct coda_devtype coda_devdata[] = {
2025         [CODA_IMX27] = {
2026                 .firmware     = "v4l-codadx6-imx27.bin",
2027                 .product      = CODA_DX6,
2028                 .codecs       = codadx6_codecs,
2029                 .num_codecs   = ARRAY_SIZE(codadx6_codecs),
2030                 .vdevs        = codadx6_video_devices,
2031                 .num_vdevs    = ARRAY_SIZE(codadx6_video_devices),
2032                 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2033                 .iram_size    = 0xb000,
2034         },
2035         [CODA_IMX53] = {
2036                 .firmware     = "v4l-coda7541-imx53.bin",
2037                 .product      = CODA_7541,
2038                 .codecs       = coda7_codecs,
2039                 .num_codecs   = ARRAY_SIZE(coda7_codecs),
2040                 .vdevs        = coda7_video_devices,
2041                 .num_vdevs    = ARRAY_SIZE(coda7_video_devices),
2042                 .workbuf_size = 128 * 1024,
2043                 .tempbuf_size = 304 * 1024,
2044                 .iram_size    = 0x14000,
2045         },
2046         [CODA_IMX6Q] = {
2047                 .firmware     = "v4l-coda960-imx6q.bin",
2048                 .product      = CODA_960,
2049                 .codecs       = coda9_codecs,
2050                 .num_codecs   = ARRAY_SIZE(coda9_codecs),
2051                 .vdevs        = coda9_video_devices,
2052                 .num_vdevs    = ARRAY_SIZE(coda9_video_devices),
2053                 .workbuf_size = 80 * 1024,
2054                 .tempbuf_size = 204 * 1024,
2055                 .iram_size    = 0x21000,
2056         },
2057         [CODA_IMX6DL] = {
2058                 .firmware     = "v4l-coda960-imx6dl.bin",
2059                 .product      = CODA_960,
2060                 .codecs       = coda9_codecs,
2061                 .num_codecs   = ARRAY_SIZE(coda9_codecs),
2062                 .vdevs        = coda9_video_devices,
2063                 .num_vdevs    = ARRAY_SIZE(coda9_video_devices),
2064                 .workbuf_size = 80 * 1024,
2065                 .tempbuf_size = 204 * 1024,
2066                 .iram_size    = 0x20000,
2067         },
2068 };
2069
2070 static struct platform_device_id coda_platform_ids[] = {
2071         { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2072         { /* sentinel */ }
2073 };
2074 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2075
2076 #ifdef CONFIG_OF
2077 static const struct of_device_id coda_dt_ids[] = {
2078         { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2079         { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2080         { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2081         { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2082         { /* sentinel */ }
2083 };
2084 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2085 #endif
2086
2087 static int coda_probe(struct platform_device *pdev)
2088 {
2089         const struct of_device_id *of_id =
2090                         of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2091         const struct platform_device_id *pdev_id;
2092         struct coda_platform_data *pdata = pdev->dev.platform_data;
2093         struct device_node *np = pdev->dev.of_node;
2094         struct gen_pool *pool;
2095         struct coda_dev *dev;
2096         struct resource *res;
2097         int ret, irq;
2098
2099         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2100         if (!dev)
2101                 return -ENOMEM;
2102
2103         pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2104
2105         if (of_id) {
2106                 dev->devtype = of_id->data;
2107         } else if (pdev_id) {
2108                 dev->devtype = &coda_devdata[pdev_id->driver_data];
2109         } else {
2110                 ret = -EINVAL;
2111                 goto err_v4l2_register;
2112         }
2113
2114         spin_lock_init(&dev->irqlock);
2115         INIT_LIST_HEAD(&dev->instances);
2116
2117         dev->plat_dev = pdev;
2118         dev->clk_per = devm_clk_get(&pdev->dev, "per");
2119         if (IS_ERR(dev->clk_per)) {
2120                 dev_err(&pdev->dev, "Could not get per clock\n");
2121                 return PTR_ERR(dev->clk_per);
2122         }
2123
2124         dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2125         if (IS_ERR(dev->clk_ahb)) {
2126                 dev_err(&pdev->dev, "Could not get ahb clock\n");
2127                 return PTR_ERR(dev->clk_ahb);
2128         }
2129
2130         /* Get  memory for physical registers */
2131         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2132         dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2133         if (IS_ERR(dev->regs_base))
2134                 return PTR_ERR(dev->regs_base);
2135
2136         /* IRQ */
2137         irq = platform_get_irq_byname(pdev, "bit");
2138         if (irq < 0)
2139                 irq = platform_get_irq(pdev, 0);
2140         if (irq < 0) {
2141                 dev_err(&pdev->dev, "failed to get irq resource\n");
2142                 return irq;
2143         }
2144
2145         ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2146                         IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2147         if (ret < 0) {
2148                 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2149                 return ret;
2150         }
2151
2152         dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
2153         if (IS_ERR(dev->rstc)) {
2154                 ret = PTR_ERR(dev->rstc);
2155                 if (ret == -ENOENT || ret == -ENOSYS) {
2156                         dev->rstc = NULL;
2157                 } else {
2158                         dev_err(&pdev->dev, "failed get reset control: %d\n",
2159                                 ret);
2160                         return ret;
2161                 }
2162         }
2163
2164         /* Get IRAM pool from device tree or platform data */
2165         pool = of_get_named_gen_pool(np, "iram", 0);
2166         if (!pool && pdata)
2167                 pool = dev_get_gen_pool(pdata->iram_dev);
2168         if (!pool) {
2169                 dev_err(&pdev->dev, "iram pool not available\n");
2170                 return -ENOMEM;
2171         }
2172         dev->iram_pool = pool;
2173
2174         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2175         if (ret)
2176                 return ret;
2177
2178         mutex_init(&dev->dev_mutex);
2179         mutex_init(&dev->coda_mutex);
2180
2181         dev->debugfs_root = debugfs_create_dir("coda", NULL);
2182         if (!dev->debugfs_root)
2183                 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2184
2185         /* allocate auxiliary per-device buffers for the BIT processor */
2186         if (dev->devtype->product == CODA_DX6) {
2187                 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2188                                          dev->devtype->workbuf_size, "workbuf",
2189                                          dev->debugfs_root);
2190                 if (ret < 0) {
2191                         dev_err(&pdev->dev, "failed to allocate work buffer\n");
2192                         goto err_v4l2_register;
2193                 }
2194         }
2195
2196         if (dev->devtype->tempbuf_size) {
2197                 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2198                                          dev->devtype->tempbuf_size, "tempbuf",
2199                                          dev->debugfs_root);
2200                 if (ret < 0) {
2201                         dev_err(&pdev->dev, "failed to allocate temp buffer\n");
2202                         goto err_v4l2_register;
2203                 }
2204         }
2205
2206         dev->iram.size = dev->devtype->iram_size;
2207         dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2208                                              &dev->iram.paddr);
2209         if (!dev->iram.vaddr) {
2210                 dev_warn(&pdev->dev, "unable to alloc iram\n");
2211         } else {
2212                 memset(dev->iram.vaddr, 0, dev->iram.size);
2213                 dev->iram.blob.data = dev->iram.vaddr;
2214                 dev->iram.blob.size = dev->iram.size;
2215                 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2216                                                        dev->debugfs_root,
2217                                                        &dev->iram.blob);
2218         }
2219
2220         dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2221         if (!dev->workqueue) {
2222                 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2223                 ret = -ENOMEM;
2224                 goto err_v4l2_register;
2225         }
2226
2227         platform_set_drvdata(pdev, dev);
2228
2229         /*
2230          * Start activated so we can directly call coda_hw_init in
2231          * coda_fw_callback regardless of whether CONFIG_PM is
2232          * enabled or whether the device is associated with a PM domain.
2233          */
2234         pm_runtime_get_noresume(&pdev->dev);
2235         pm_runtime_set_active(&pdev->dev);
2236         pm_runtime_enable(&pdev->dev);
2237
2238         return coda_firmware_request(dev);
2239
2240 err_v4l2_register:
2241         v4l2_device_unregister(&dev->v4l2_dev);
2242         return ret;
2243 }
2244
2245 static int coda_remove(struct platform_device *pdev)
2246 {
2247         struct coda_dev *dev = platform_get_drvdata(pdev);
2248         int i;
2249
2250         for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2251                 if (video_get_drvdata(&dev->vfd[i]))
2252                         video_unregister_device(&dev->vfd[i]);
2253         }
2254         if (dev->m2m_dev)
2255                 v4l2_m2m_release(dev->m2m_dev);
2256         pm_runtime_disable(&pdev->dev);
2257         if (dev->alloc_ctx)
2258                 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2259         v4l2_device_unregister(&dev->v4l2_dev);
2260         destroy_workqueue(dev->workqueue);
2261         if (dev->iram.vaddr)
2262                 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2263                               dev->iram.size);
2264         coda_free_aux_buf(dev, &dev->codebuf);
2265         coda_free_aux_buf(dev, &dev->tempbuf);
2266         coda_free_aux_buf(dev, &dev->workbuf);
2267         debugfs_remove_recursive(dev->debugfs_root);
2268         return 0;
2269 }
2270
2271 #ifdef CONFIG_PM
2272 static int coda_runtime_resume(struct device *dev)
2273 {
2274         struct coda_dev *cdev = dev_get_drvdata(dev);
2275         int ret = 0;
2276
2277         if (dev->pm_domain && cdev->codebuf.vaddr) {
2278                 ret = coda_hw_init(cdev);
2279                 if (ret)
2280                         v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2281         }
2282
2283         return ret;
2284 }
2285 #endif
2286
2287 static const struct dev_pm_ops coda_pm_ops = {
2288         SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2289 };
2290
2291 static struct platform_driver coda_driver = {
2292         .probe  = coda_probe,
2293         .remove = coda_remove,
2294         .driver = {
2295                 .name   = CODA_NAME,
2296                 .of_match_table = of_match_ptr(coda_dt_ids),
2297                 .pm     = &coda_pm_ops,
2298         },
2299         .id_table = coda_platform_ids,
2300 };
2301
2302 module_platform_driver(coda_driver);
2303
2304 MODULE_LICENSE("GPL");
2305 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2306 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");