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