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