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