perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / platform / coda / coda-common.c
blob726b3b93a4863184bb84ee792784d69cdcc39333
1 /*
2 * Coda multi-standard codec IP
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
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.
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>
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>
43 #include "coda.h"
44 #include "imx-vdoa.h"
46 #define CODA_NAME "coda"
48 #define CODADX6_MAX_INSTANCES 4
49 #define CODA_MAX_FORMATS 4
51 #define CODA_ISRAM_SIZE (2048 * 2)
53 #define MIN_W 176
54 #define MIN_H 144
56 #define S_ALIGN 1 /* multiple of 2 */
57 #define W_ALIGN 1 /* multiple of 2 */
58 #define H_ALIGN 1 /* multiple of 2 */
60 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
62 int coda_debug;
63 module_param(coda_debug, int, 0644);
64 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
66 static int disable_tiling;
67 module_param(disable_tiling, int, 0644);
68 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
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");
74 static int enable_bwb = 0;
75 module_param(enable_bwb, int, 0644);
76 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
78 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
80 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
81 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
82 writel(data, dev->regs_base + reg);
85 unsigned int coda_read(struct coda_dev *dev, u32 reg)
87 u32 data;
89 data = readl(dev->regs_base + reg);
90 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
91 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
92 return data;
95 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
96 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
98 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
99 u32 base_cb, base_cr;
101 switch (q_data->fourcc) {
102 case V4L2_PIX_FMT_YUYV:
103 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
104 case V4L2_PIX_FMT_NV12:
105 case V4L2_PIX_FMT_YUV420:
106 default:
107 base_cb = base_y + q_data->bytesperline * q_data->height;
108 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
109 break;
110 case V4L2_PIX_FMT_YVU420:
111 /* Switch Cb and Cr for YVU420 format */
112 base_cr = base_y + q_data->bytesperline * q_data->height;
113 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
114 break;
115 case V4L2_PIX_FMT_YUV422P:
116 base_cb = base_y + q_data->bytesperline * q_data->height;
117 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
120 coda_write(ctx->dev, base_y, reg_y);
121 coda_write(ctx->dev, base_cb, reg_y + 4);
122 coda_write(ctx->dev, base_cr, reg_y + 8);
125 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
126 { mode, src_fourcc, dst_fourcc, max_w, max_h }
129 * Arrays of codecs supported by each given version of Coda:
130 * i.MX27 -> codadx6
131 * i.MX51 -> codahx4
132 * i.MX53 -> coda7
133 * i.MX6 -> coda960
134 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
136 static const struct coda_codec codadx6_codecs[] = {
137 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
138 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
141 static const struct coda_codec codahx4_codecs[] = {
142 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
143 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
144 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
145 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
148 static const struct coda_codec coda7_codecs[] = {
149 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
150 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
151 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
152 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
153 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
154 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
155 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
158 static const struct coda_codec coda9_codecs[] = {
159 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
160 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
161 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
162 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
163 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
166 struct coda_video_device {
167 const char *name;
168 enum coda_inst_type type;
169 const struct coda_context_ops *ops;
170 bool direct;
171 u32 src_formats[CODA_MAX_FORMATS];
172 u32 dst_formats[CODA_MAX_FORMATS];
175 static const struct coda_video_device coda_bit_encoder = {
176 .name = "coda-encoder",
177 .type = CODA_INST_ENCODER,
178 .ops = &coda_bit_encode_ops,
179 .src_formats = {
180 V4L2_PIX_FMT_NV12,
181 V4L2_PIX_FMT_YUV420,
182 V4L2_PIX_FMT_YVU420,
184 .dst_formats = {
185 V4L2_PIX_FMT_H264,
186 V4L2_PIX_FMT_MPEG4,
190 static const struct coda_video_device coda_bit_jpeg_encoder = {
191 .name = "coda-jpeg-encoder",
192 .type = CODA_INST_ENCODER,
193 .ops = &coda_bit_encode_ops,
194 .src_formats = {
195 V4L2_PIX_FMT_NV12,
196 V4L2_PIX_FMT_YUV420,
197 V4L2_PIX_FMT_YVU420,
198 V4L2_PIX_FMT_YUV422P,
200 .dst_formats = {
201 V4L2_PIX_FMT_JPEG,
205 static const struct coda_video_device coda_bit_decoder = {
206 .name = "coda-decoder",
207 .type = CODA_INST_DECODER,
208 .ops = &coda_bit_decode_ops,
209 .src_formats = {
210 V4L2_PIX_FMT_H264,
211 V4L2_PIX_FMT_MPEG2,
212 V4L2_PIX_FMT_MPEG4,
214 .dst_formats = {
215 V4L2_PIX_FMT_NV12,
216 V4L2_PIX_FMT_YUV420,
217 V4L2_PIX_FMT_YVU420,
219 * If V4L2_PIX_FMT_YUYV should be default,
220 * set_default_params() must be adjusted.
222 V4L2_PIX_FMT_YUYV,
226 static const struct coda_video_device coda_bit_jpeg_decoder = {
227 .name = "coda-jpeg-decoder",
228 .type = CODA_INST_DECODER,
229 .ops = &coda_bit_decode_ops,
230 .src_formats = {
231 V4L2_PIX_FMT_JPEG,
233 .dst_formats = {
234 V4L2_PIX_FMT_NV12,
235 V4L2_PIX_FMT_YUV420,
236 V4L2_PIX_FMT_YVU420,
237 V4L2_PIX_FMT_YUV422P,
241 static const struct coda_video_device *codadx6_video_devices[] = {
242 &coda_bit_encoder,
245 static const struct coda_video_device *codahx4_video_devices[] = {
246 &coda_bit_encoder,
247 &coda_bit_decoder,
250 static const struct coda_video_device *coda7_video_devices[] = {
251 &coda_bit_jpeg_encoder,
252 &coda_bit_jpeg_decoder,
253 &coda_bit_encoder,
254 &coda_bit_decoder,
257 static const struct coda_video_device *coda9_video_devices[] = {
258 &coda_bit_encoder,
259 &coda_bit_decoder,
263 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
264 * tables.
266 static u32 coda_format_normalize_yuv(u32 fourcc)
268 switch (fourcc) {
269 case V4L2_PIX_FMT_NV12:
270 case V4L2_PIX_FMT_YUV420:
271 case V4L2_PIX_FMT_YVU420:
272 case V4L2_PIX_FMT_YUV422P:
273 case V4L2_PIX_FMT_YUYV:
274 return V4L2_PIX_FMT_YUV420;
275 default:
276 return fourcc;
280 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
281 int src_fourcc, int dst_fourcc)
283 const struct coda_codec *codecs = dev->devtype->codecs;
284 int num_codecs = dev->devtype->num_codecs;
285 int k;
287 src_fourcc = coda_format_normalize_yuv(src_fourcc);
288 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
289 if (src_fourcc == dst_fourcc)
290 return NULL;
292 for (k = 0; k < num_codecs; k++) {
293 if (codecs[k].src_fourcc == src_fourcc &&
294 codecs[k].dst_fourcc == dst_fourcc)
295 break;
298 if (k == num_codecs)
299 return NULL;
301 return &codecs[k];
304 static void coda_get_max_dimensions(struct coda_dev *dev,
305 const struct coda_codec *codec,
306 int *max_w, int *max_h)
308 const struct coda_codec *codecs = dev->devtype->codecs;
309 int num_codecs = dev->devtype->num_codecs;
310 unsigned int w, h;
311 int k;
313 if (codec) {
314 w = codec->max_w;
315 h = codec->max_h;
316 } else {
317 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
318 w = max(w, codecs[k].max_w);
319 h = max(h, codecs[k].max_h);
323 if (max_w)
324 *max_w = w;
325 if (max_h)
326 *max_h = h;
329 static const struct coda_video_device *to_coda_video_device(struct video_device
330 *vdev)
332 struct coda_dev *dev = video_get_drvdata(vdev);
333 unsigned int i = vdev - dev->vfd;
335 if (i >= dev->devtype->num_vdevs)
336 return NULL;
338 return dev->devtype->vdevs[i];
341 const char *coda_product_name(int product)
343 static char buf[9];
345 switch (product) {
346 case CODA_DX6:
347 return "CodaDx6";
348 case CODA_HX4:
349 return "CodaHx4";
350 case CODA_7541:
351 return "CODA7541";
352 case CODA_960:
353 return "CODA960";
354 default:
355 snprintf(buf, sizeof(buf), "(0x%04x)", product);
356 return buf;
360 static struct vdoa_data *coda_get_vdoa_data(void)
362 struct device_node *vdoa_node;
363 struct platform_device *vdoa_pdev;
364 struct vdoa_data *vdoa_data = NULL;
366 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
367 if (!vdoa_node)
368 return NULL;
370 vdoa_pdev = of_find_device_by_node(vdoa_node);
371 if (!vdoa_pdev)
372 goto out;
374 vdoa_data = platform_get_drvdata(vdoa_pdev);
375 if (!vdoa_data)
376 vdoa_data = ERR_PTR(-EPROBE_DEFER);
378 out:
379 if (vdoa_node)
380 of_node_put(vdoa_node);
382 return vdoa_data;
386 * V4L2 ioctl() operations.
388 static int coda_querycap(struct file *file, void *priv,
389 struct v4l2_capability *cap)
391 struct coda_ctx *ctx = fh_to_ctx(priv);
393 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
394 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
395 sizeof(cap->card));
396 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
397 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
398 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
400 return 0;
403 static int coda_enum_fmt(struct file *file, void *priv,
404 struct v4l2_fmtdesc *f)
406 struct video_device *vdev = video_devdata(file);
407 const struct coda_video_device *cvd = to_coda_video_device(vdev);
408 struct coda_ctx *ctx = fh_to_ctx(priv);
409 const u32 *formats;
411 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
412 formats = cvd->src_formats;
413 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
414 formats = cvd->dst_formats;
415 else
416 return -EINVAL;
418 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
419 return -EINVAL;
421 /* Skip YUYV if the vdoa is not available */
422 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
423 formats[f->index] == V4L2_PIX_FMT_YUYV)
424 return -EINVAL;
426 f->pixelformat = formats[f->index];
428 return 0;
431 static int coda_g_fmt(struct file *file, void *priv,
432 struct v4l2_format *f)
434 struct coda_q_data *q_data;
435 struct coda_ctx *ctx = fh_to_ctx(priv);
437 q_data = get_q_data(ctx, f->type);
438 if (!q_data)
439 return -EINVAL;
441 f->fmt.pix.field = V4L2_FIELD_NONE;
442 f->fmt.pix.pixelformat = q_data->fourcc;
443 f->fmt.pix.width = q_data->width;
444 f->fmt.pix.height = q_data->height;
445 f->fmt.pix.bytesperline = q_data->bytesperline;
447 f->fmt.pix.sizeimage = q_data->sizeimage;
448 f->fmt.pix.colorspace = ctx->colorspace;
449 f->fmt.pix.xfer_func = ctx->xfer_func;
450 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
451 f->fmt.pix.quantization = ctx->quantization;
453 return 0;
456 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
458 struct coda_q_data *q_data;
459 const u32 *formats;
460 int i;
462 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
463 formats = ctx->cvd->src_formats;
464 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
465 formats = ctx->cvd->dst_formats;
466 else
467 return -EINVAL;
469 for (i = 0; i < CODA_MAX_FORMATS; i++) {
470 /* Skip YUYV if the vdoa is not available */
471 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
472 formats[i] == V4L2_PIX_FMT_YUYV)
473 continue;
475 if (formats[i] == f->fmt.pix.pixelformat) {
476 f->fmt.pix.pixelformat = formats[i];
477 return 0;
481 /* Fall back to currently set pixelformat */
482 q_data = get_q_data(ctx, f->type);
483 f->fmt.pix.pixelformat = q_data->fourcc;
485 return 0;
488 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
489 bool *use_vdoa)
491 int err;
493 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
494 return -EINVAL;
496 if (!use_vdoa)
497 return -EINVAL;
499 if (!ctx->vdoa) {
500 *use_vdoa = false;
501 return 0;
504 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
505 f->fmt.pix.height, f->fmt.pix.pixelformat);
506 if (err) {
507 *use_vdoa = false;
508 return 0;
511 *use_vdoa = true;
512 return 0;
515 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
516 u32 width, u32 height)
519 * This is a rough estimate for sensible compressed buffer
520 * sizes (between 1 and 16 bits per pixel). This could be
521 * improved by better format specific worst case estimates.
523 return round_up(clamp(sizeimage, width * height / 8,
524 width * height * 2), PAGE_SIZE);
527 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
528 struct v4l2_format *f)
530 struct coda_dev *dev = ctx->dev;
531 unsigned int max_w, max_h;
532 enum v4l2_field field;
534 field = f->fmt.pix.field;
535 if (field == V4L2_FIELD_ANY)
536 field = V4L2_FIELD_NONE;
537 else if (V4L2_FIELD_NONE != field)
538 return -EINVAL;
540 /* V4L2 specification suggests the driver corrects the format struct
541 * if any of the dimensions is unsupported */
542 f->fmt.pix.field = field;
544 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
545 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
546 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
547 S_ALIGN);
549 switch (f->fmt.pix.pixelformat) {
550 case V4L2_PIX_FMT_NV12:
551 case V4L2_PIX_FMT_YUV420:
552 case V4L2_PIX_FMT_YVU420:
554 * Frame stride must be at least multiple of 8,
555 * but multiple of 16 for h.264 or JPEG 4:2:x
557 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
558 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
559 f->fmt.pix.height * 3 / 2;
560 break;
561 case V4L2_PIX_FMT_YUYV:
562 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
563 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
564 f->fmt.pix.height;
565 break;
566 case V4L2_PIX_FMT_YUV422P:
567 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
568 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
569 f->fmt.pix.height * 2;
570 break;
571 case V4L2_PIX_FMT_JPEG:
572 case V4L2_PIX_FMT_H264:
573 case V4L2_PIX_FMT_MPEG4:
574 case V4L2_PIX_FMT_MPEG2:
575 f->fmt.pix.bytesperline = 0;
576 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
577 f->fmt.pix.sizeimage,
578 f->fmt.pix.width,
579 f->fmt.pix.height);
580 break;
581 default:
582 BUG();
585 return 0;
588 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
589 struct v4l2_format *f)
591 struct coda_ctx *ctx = fh_to_ctx(priv);
592 const struct coda_q_data *q_data_src;
593 const struct coda_codec *codec;
594 struct vb2_queue *src_vq;
595 int ret;
596 bool use_vdoa;
598 ret = coda_try_pixelformat(ctx, f);
599 if (ret < 0)
600 return ret;
602 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
605 * If the source format is already fixed, only allow the same output
606 * resolution
608 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
609 if (vb2_is_streaming(src_vq)) {
610 f->fmt.pix.width = q_data_src->width;
611 f->fmt.pix.height = q_data_src->height;
614 f->fmt.pix.colorspace = ctx->colorspace;
615 f->fmt.pix.xfer_func = ctx->xfer_func;
616 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
617 f->fmt.pix.quantization = ctx->quantization;
619 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
620 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
621 f->fmt.pix.pixelformat);
622 if (!codec)
623 return -EINVAL;
625 ret = coda_try_fmt(ctx, codec, f);
626 if (ret < 0)
627 return ret;
629 /* The h.264 decoder only returns complete 16x16 macroblocks */
630 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
631 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
632 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
633 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
634 f->fmt.pix.height * 3 / 2;
636 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
637 if (ret < 0)
638 return ret;
640 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
641 if (!use_vdoa)
642 return -EINVAL;
644 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
645 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
646 f->fmt.pix.height;
650 return 0;
653 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
655 enum v4l2_colorspace colorspace;
657 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
658 colorspace = V4L2_COLORSPACE_JPEG;
659 else if (fmt->width <= 720 && fmt->height <= 576)
660 colorspace = V4L2_COLORSPACE_SMPTE170M;
661 else
662 colorspace = V4L2_COLORSPACE_REC709;
664 fmt->colorspace = colorspace;
665 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
666 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
667 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
670 static int coda_try_fmt_vid_out(struct file *file, void *priv,
671 struct v4l2_format *f)
673 struct coda_ctx *ctx = fh_to_ctx(priv);
674 struct coda_dev *dev = ctx->dev;
675 const struct coda_q_data *q_data_dst;
676 const struct coda_codec *codec;
677 int ret;
679 ret = coda_try_pixelformat(ctx, f);
680 if (ret < 0)
681 return ret;
683 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
684 coda_set_default_colorspace(&f->fmt.pix);
686 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
687 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
689 return coda_try_fmt(ctx, codec, f);
692 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
693 struct v4l2_rect *r)
695 struct coda_q_data *q_data;
696 struct vb2_queue *vq;
698 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
699 if (!vq)
700 return -EINVAL;
702 q_data = get_q_data(ctx, f->type);
703 if (!q_data)
704 return -EINVAL;
706 if (vb2_is_busy(vq)) {
707 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
708 return -EBUSY;
711 q_data->fourcc = f->fmt.pix.pixelformat;
712 q_data->width = f->fmt.pix.width;
713 q_data->height = f->fmt.pix.height;
714 q_data->bytesperline = f->fmt.pix.bytesperline;
715 q_data->sizeimage = f->fmt.pix.sizeimage;
716 if (r) {
717 q_data->rect = *r;
718 } else {
719 q_data->rect.left = 0;
720 q_data->rect.top = 0;
721 q_data->rect.width = f->fmt.pix.width;
722 q_data->rect.height = f->fmt.pix.height;
725 switch (f->fmt.pix.pixelformat) {
726 case V4L2_PIX_FMT_YUYV:
727 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
728 break;
729 case V4L2_PIX_FMT_NV12:
730 if (!disable_tiling) {
731 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
732 break;
734 /* else fall through */
735 case V4L2_PIX_FMT_YUV420:
736 case V4L2_PIX_FMT_YVU420:
737 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
738 break;
739 default:
740 break;
743 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
744 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
745 ctx->use_vdoa)
746 vdoa_context_configure(ctx->vdoa,
747 round_up(f->fmt.pix.width, 16),
748 f->fmt.pix.height,
749 f->fmt.pix.pixelformat);
750 else
751 ctx->use_vdoa = false;
753 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
754 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
755 f->type, q_data->width, q_data->height,
756 (char *)&q_data->fourcc,
757 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
759 return 0;
762 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
763 struct v4l2_format *f)
765 struct coda_ctx *ctx = fh_to_ctx(priv);
766 struct coda_q_data *q_data_src;
767 struct v4l2_rect r;
768 int ret;
770 ret = coda_try_fmt_vid_cap(file, priv, f);
771 if (ret)
772 return ret;
774 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
775 r.left = 0;
776 r.top = 0;
777 r.width = q_data_src->width;
778 r.height = q_data_src->height;
780 ret = coda_s_fmt(ctx, f, &r);
781 if (ret)
782 return ret;
784 if (ctx->inst_type != CODA_INST_ENCODER)
785 return 0;
787 ctx->colorspace = f->fmt.pix.colorspace;
788 ctx->xfer_func = f->fmt.pix.xfer_func;
789 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
790 ctx->quantization = f->fmt.pix.quantization;
792 return 0;
795 static int coda_s_fmt_vid_out(struct file *file, void *priv,
796 struct v4l2_format *f)
798 struct coda_ctx *ctx = fh_to_ctx(priv);
799 struct v4l2_format f_cap;
800 struct vb2_queue *dst_vq;
801 int ret;
803 ret = coda_try_fmt_vid_out(file, priv, f);
804 if (ret)
805 return ret;
807 ret = coda_s_fmt(ctx, f, NULL);
808 if (ret)
809 return ret;
811 if (ctx->inst_type != CODA_INST_DECODER)
812 return 0;
814 ctx->colorspace = f->fmt.pix.colorspace;
815 ctx->xfer_func = f->fmt.pix.xfer_func;
816 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
817 ctx->quantization = f->fmt.pix.quantization;
819 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
820 if (!dst_vq)
821 return -EINVAL;
824 * Setting the capture queue format is not possible while the capture
825 * queue is still busy. This is not an error, but the user will have to
826 * make sure themselves that the capture format is set correctly before
827 * starting the output queue again.
829 if (vb2_is_busy(dst_vq))
830 return 0;
832 memset(&f_cap, 0, sizeof(f_cap));
833 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
834 coda_g_fmt(file, priv, &f_cap);
835 f_cap.fmt.pix.width = f->fmt.pix.width;
836 f_cap.fmt.pix.height = f->fmt.pix.height;
838 return coda_s_fmt_vid_cap(file, priv, &f_cap);
841 static int coda_reqbufs(struct file *file, void *priv,
842 struct v4l2_requestbuffers *rb)
844 struct coda_ctx *ctx = fh_to_ctx(priv);
845 int ret;
847 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
848 if (ret)
849 return ret;
852 * Allow to allocate instance specific per-context buffers, such as
853 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
855 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
856 return ctx->ops->reqbufs(ctx, rb);
858 return 0;
861 static int coda_qbuf(struct file *file, void *priv,
862 struct v4l2_buffer *buf)
864 struct coda_ctx *ctx = fh_to_ctx(priv);
866 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
869 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
870 struct vb2_v4l2_buffer *buf)
872 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
873 (buf->sequence == (ctx->qsequence - 1)));
876 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
877 enum vb2_buffer_state state)
879 const struct v4l2_event eos_event = {
880 .type = V4L2_EVENT_EOS
883 if (coda_buf_is_end_of_stream(ctx, buf)) {
884 buf->flags |= V4L2_BUF_FLAG_LAST;
886 v4l2_event_queue_fh(&ctx->fh, &eos_event);
889 v4l2_m2m_buf_done(buf, state);
892 static int coda_g_selection(struct file *file, void *fh,
893 struct v4l2_selection *s)
895 struct coda_ctx *ctx = fh_to_ctx(fh);
896 struct coda_q_data *q_data;
897 struct v4l2_rect r, *rsel;
899 q_data = get_q_data(ctx, s->type);
900 if (!q_data)
901 return -EINVAL;
903 r.left = 0;
904 r.top = 0;
905 r.width = q_data->width;
906 r.height = q_data->height;
907 rsel = &q_data->rect;
909 switch (s->target) {
910 case V4L2_SEL_TGT_CROP_DEFAULT:
911 case V4L2_SEL_TGT_CROP_BOUNDS:
912 rsel = &r;
913 /* fallthrough */
914 case V4L2_SEL_TGT_CROP:
915 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
916 return -EINVAL;
917 break;
918 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
919 case V4L2_SEL_TGT_COMPOSE_PADDED:
920 rsel = &r;
921 /* fallthrough */
922 case V4L2_SEL_TGT_COMPOSE:
923 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
924 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
925 return -EINVAL;
926 break;
927 default:
928 return -EINVAL;
931 s->r = *rsel;
933 return 0;
936 static int coda_s_selection(struct file *file, void *fh,
937 struct v4l2_selection *s)
939 struct coda_ctx *ctx = fh_to_ctx(fh);
940 struct coda_q_data *q_data;
942 if (ctx->inst_type == CODA_INST_ENCODER &&
943 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
944 s->target == V4L2_SEL_TGT_CROP) {
945 q_data = get_q_data(ctx, s->type);
946 if (!q_data)
947 return -EINVAL;
949 s->r.left = 0;
950 s->r.top = 0;
951 s->r.width = clamp(s->r.width, 2U, q_data->width);
952 s->r.height = clamp(s->r.height, 2U, q_data->height);
954 if (s->flags & V4L2_SEL_FLAG_LE) {
955 s->r.width = round_up(s->r.width, 2);
956 s->r.height = round_up(s->r.height, 2);
957 } else {
958 s->r.width = round_down(s->r.width, 2);
959 s->r.height = round_down(s->r.height, 2);
962 q_data->rect = s->r;
964 return 0;
967 return coda_g_selection(file, fh, s);
970 static int coda_try_encoder_cmd(struct file *file, void *fh,
971 struct v4l2_encoder_cmd *ec)
973 if (ec->cmd != V4L2_ENC_CMD_STOP)
974 return -EINVAL;
976 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
977 return -EINVAL;
979 return 0;
982 static int coda_encoder_cmd(struct file *file, void *fh,
983 struct v4l2_encoder_cmd *ec)
985 struct coda_ctx *ctx = fh_to_ctx(fh);
986 struct vb2_queue *dst_vq;
987 int ret;
989 ret = coda_try_encoder_cmd(file, fh, ec);
990 if (ret < 0)
991 return ret;
993 /* Ignore encoder stop command silently in decoder context */
994 if (ctx->inst_type != CODA_INST_ENCODER)
995 return 0;
997 /* Set the stream-end flag on this context */
998 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1000 /* If there is no buffer in flight, wake up */
1001 if (!ctx->streamon_out || ctx->qsequence == ctx->osequence) {
1002 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1003 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1004 dst_vq->last_buffer_dequeued = true;
1005 wake_up(&dst_vq->done_wq);
1008 return 0;
1011 static int coda_try_decoder_cmd(struct file *file, void *fh,
1012 struct v4l2_decoder_cmd *dc)
1014 if (dc->cmd != V4L2_DEC_CMD_STOP)
1015 return -EINVAL;
1017 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
1018 return -EINVAL;
1020 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
1021 return -EINVAL;
1023 return 0;
1026 static int coda_decoder_cmd(struct file *file, void *fh,
1027 struct v4l2_decoder_cmd *dc)
1029 struct coda_ctx *ctx = fh_to_ctx(fh);
1030 int ret;
1032 ret = coda_try_decoder_cmd(file, fh, dc);
1033 if (ret < 0)
1034 return ret;
1036 /* Ignore decoder stop command silently in encoder context */
1037 if (ctx->inst_type != CODA_INST_DECODER)
1038 return 0;
1040 /* Set the stream-end flag on this context */
1041 coda_bit_stream_end_flag(ctx);
1042 ctx->hold = false;
1043 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1045 return 0;
1048 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1050 struct coda_ctx *ctx = fh_to_ctx(fh);
1051 struct v4l2_fract *tpf;
1053 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1054 return -EINVAL;
1056 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1057 tpf = &a->parm.output.timeperframe;
1058 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1059 tpf->numerator = 1 + (ctx->params.framerate >>
1060 CODA_FRATE_DIV_OFFSET);
1062 return 0;
1066 * Approximate timeperframe v4l2_fract with values that can be written
1067 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1069 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1071 struct v4l2_fract s = *timeperframe;
1072 struct v4l2_fract f0;
1073 struct v4l2_fract f1 = { 1, 0 };
1074 struct v4l2_fract f2 = { 0, 1 };
1075 unsigned int i, div, s_denominator;
1077 /* Lower bound is 1/65535 */
1078 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1079 timeperframe->numerator = 1;
1080 timeperframe->denominator = 65535;
1081 return;
1084 /* Upper bound is 65536/1, map everything above to infinity */
1085 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1086 timeperframe->numerator = 1;
1087 timeperframe->denominator = 0;
1088 return;
1091 /* Reduce fraction to lowest terms */
1092 div = gcd(s.numerator, s.denominator);
1093 if (div > 1) {
1094 s.numerator /= div;
1095 s.denominator /= div;
1098 if (s.numerator <= 65536 && s.denominator < 65536) {
1099 *timeperframe = s;
1100 return;
1103 /* Find successive convergents from continued fraction expansion */
1104 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1105 f0 = f1;
1106 f1 = f2;
1108 /* Stop when f2 exactly equals timeperframe */
1109 if (s.numerator == 0)
1110 break;
1112 i = s.denominator / s.numerator;
1114 f2.numerator = f0.numerator + i * f1.numerator;
1115 f2.denominator = f0.denominator + i * f2.denominator;
1117 s_denominator = s.numerator;
1118 s.numerator = s.denominator % s.numerator;
1119 s.denominator = s_denominator;
1122 *timeperframe = f1;
1125 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1127 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1128 timeperframe->denominator;
1131 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1133 struct coda_ctx *ctx = fh_to_ctx(fh);
1134 struct v4l2_fract *tpf;
1136 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1137 return -EINVAL;
1139 tpf = &a->parm.output.timeperframe;
1140 coda_approximate_timeperframe(tpf);
1141 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1143 return 0;
1146 static int coda_subscribe_event(struct v4l2_fh *fh,
1147 const struct v4l2_event_subscription *sub)
1149 switch (sub->type) {
1150 case V4L2_EVENT_EOS:
1151 return v4l2_event_subscribe(fh, sub, 0, NULL);
1152 default:
1153 return v4l2_ctrl_subscribe_event(fh, sub);
1157 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1158 .vidioc_querycap = coda_querycap,
1160 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1161 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1162 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1163 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1165 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1166 .vidioc_g_fmt_vid_out = coda_g_fmt,
1167 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1168 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1170 .vidioc_reqbufs = coda_reqbufs,
1171 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1173 .vidioc_qbuf = coda_qbuf,
1174 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1175 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
1176 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1177 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1179 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1180 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1182 .vidioc_g_selection = coda_g_selection,
1183 .vidioc_s_selection = coda_s_selection,
1185 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1186 .vidioc_encoder_cmd = coda_encoder_cmd,
1187 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
1188 .vidioc_decoder_cmd = coda_decoder_cmd,
1190 .vidioc_g_parm = coda_g_parm,
1191 .vidioc_s_parm = coda_s_parm,
1193 .vidioc_subscribe_event = coda_subscribe_event,
1194 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1198 * Mem-to-mem operations.
1201 static void coda_device_run(void *m2m_priv)
1203 struct coda_ctx *ctx = m2m_priv;
1204 struct coda_dev *dev = ctx->dev;
1206 queue_work(dev->workqueue, &ctx->pic_run_work);
1209 static void coda_pic_run_work(struct work_struct *work)
1211 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1212 struct coda_dev *dev = ctx->dev;
1213 int ret;
1215 mutex_lock(&ctx->buffer_mutex);
1216 mutex_lock(&dev->coda_mutex);
1218 ret = ctx->ops->prepare_run(ctx);
1219 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1220 mutex_unlock(&dev->coda_mutex);
1221 mutex_unlock(&ctx->buffer_mutex);
1222 /* job_finish scheduled by prepare_decode */
1223 return;
1226 if (!wait_for_completion_timeout(&ctx->completion,
1227 msecs_to_jiffies(1000))) {
1228 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1230 ctx->hold = true;
1232 coda_hw_reset(ctx);
1234 if (ctx->ops->run_timeout)
1235 ctx->ops->run_timeout(ctx);
1236 } else if (!ctx->aborting) {
1237 ctx->ops->finish_run(ctx);
1240 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1241 ctx->ops->seq_end_work)
1242 queue_work(dev->workqueue, &ctx->seq_end_work);
1244 mutex_unlock(&dev->coda_mutex);
1245 mutex_unlock(&ctx->buffer_mutex);
1247 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1250 static int coda_job_ready(void *m2m_priv)
1252 struct coda_ctx *ctx = m2m_priv;
1253 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1256 * For both 'P' and 'key' frame cases 1 picture
1257 * and 1 frame are needed. In the decoder case,
1258 * the compressed frame can be in the bitstream.
1260 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1261 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1262 "not ready: not enough video buffers.\n");
1263 return 0;
1266 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1267 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1268 "not ready: not enough video capture buffers.\n");
1269 return 0;
1272 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1273 bool stream_end = ctx->bit_stream_param &
1274 CODA_BIT_STREAM_END_FLAG;
1275 int num_metas = ctx->num_metas;
1276 unsigned int count;
1278 count = hweight32(ctx->frm_dis_flg);
1279 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1280 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1281 "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
1282 ctx->idx, count, ctx->num_internal_frames,
1283 ctx->frm_dis_flg);
1284 return 0;
1287 if (ctx->hold && !src_bufs) {
1288 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1289 "%d: not ready: on hold for more buffers.\n",
1290 ctx->idx);
1291 return 0;
1294 if (!stream_end && (num_metas + src_bufs) < 2) {
1295 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1296 "%d: not ready: need 2 buffers available (%d, %d)\n",
1297 ctx->idx, num_metas, src_bufs);
1298 return 0;
1302 if (!src_bufs && !stream_end &&
1303 (coda_get_bitstream_payload(ctx) < 512)) {
1304 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1305 "%d: not ready: not enough bitstream data (%d).\n",
1306 ctx->idx, coda_get_bitstream_payload(ctx));
1307 return 0;
1311 if (ctx->aborting) {
1312 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1313 "not ready: aborting\n");
1314 return 0;
1317 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1318 "job ready\n");
1320 return 1;
1323 static void coda_job_abort(void *priv)
1325 struct coda_ctx *ctx = priv;
1327 ctx->aborting = 1;
1329 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1330 "Aborting task\n");
1333 static const struct v4l2_m2m_ops coda_m2m_ops = {
1334 .device_run = coda_device_run,
1335 .job_ready = coda_job_ready,
1336 .job_abort = coda_job_abort,
1339 static void set_default_params(struct coda_ctx *ctx)
1341 unsigned int max_w, max_h, usize, csize;
1343 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1344 ctx->cvd->dst_formats[0]);
1345 max_w = min(ctx->codec->max_w, 1920U);
1346 max_h = min(ctx->codec->max_h, 1088U);
1347 usize = max_w * max_h * 3 / 2;
1348 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1350 ctx->params.codec_mode = ctx->codec->mode;
1351 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1352 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1353 else
1354 ctx->colorspace = V4L2_COLORSPACE_REC709;
1355 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1356 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1357 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1358 ctx->params.framerate = 30;
1360 /* Default formats for output and input queues */
1361 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1362 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1363 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1364 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1365 ctx->q_data[V4L2_M2M_DST].width = max_w;
1366 ctx->q_data[V4L2_M2M_DST].height = max_h;
1367 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1368 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1369 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1370 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1371 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1372 } else {
1373 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1374 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1375 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1376 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1378 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1379 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1380 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1381 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1384 * Since the RBC2AXI logic only supports a single chroma plane,
1385 * macroblock tiling only works for to NV12 pixel format.
1387 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1391 * Queue operations
1393 static int coda_queue_setup(struct vb2_queue *vq,
1394 unsigned int *nbuffers, unsigned int *nplanes,
1395 unsigned int sizes[], struct device *alloc_devs[])
1397 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1398 struct coda_q_data *q_data;
1399 unsigned int size;
1401 q_data = get_q_data(ctx, vq->type);
1402 size = q_data->sizeimage;
1404 *nplanes = 1;
1405 sizes[0] = size;
1407 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1408 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1410 return 0;
1413 static int coda_buf_prepare(struct vb2_buffer *vb)
1415 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1416 struct coda_q_data *q_data;
1418 q_data = get_q_data(ctx, vb->vb2_queue->type);
1420 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1421 v4l2_warn(&ctx->dev->v4l2_dev,
1422 "%s data will not fit into plane (%lu < %lu)\n",
1423 __func__, vb2_plane_size(vb, 0),
1424 (long)q_data->sizeimage);
1425 return -EINVAL;
1428 return 0;
1431 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1433 if (!ctrl)
1434 return;
1436 v4l2_ctrl_lock(ctrl);
1439 * Extend the control range if the parsed stream contains a known but
1440 * unsupported value or level.
1442 if (value > ctrl->maximum) {
1443 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1444 ctrl->menu_skip_mask & ~(1 << value),
1445 ctrl->default_value);
1446 } else if (value < ctrl->minimum) {
1447 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1448 ctrl->menu_skip_mask & ~(1 << value),
1449 ctrl->default_value);
1452 __v4l2_ctrl_s_ctrl(ctrl, value);
1454 v4l2_ctrl_unlock(ctrl);
1457 static void coda_update_h264_profile_ctrl(struct coda_ctx *ctx)
1459 const char * const *profile_names;
1460 int profile;
1462 profile = coda_h264_profile(ctx->params.h264_profile_idc);
1463 if (profile < 0) {
1464 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Profile: %u\n",
1465 ctx->params.h264_profile_idc);
1466 return;
1469 coda_update_menu_ctrl(ctx->h264_profile_ctrl, profile);
1471 profile_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_PROFILE);
1473 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "Parsed H264 Profile: %s\n",
1474 profile_names[profile]);
1477 static void coda_update_h264_level_ctrl(struct coda_ctx *ctx)
1479 const char * const *level_names;
1480 int level;
1482 level = coda_h264_level(ctx->params.h264_level_idc);
1483 if (level < 0) {
1484 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Level: %u\n",
1485 ctx->params.h264_level_idc);
1486 return;
1489 coda_update_menu_ctrl(ctx->h264_level_ctrl, level);
1491 level_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL);
1493 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "Parsed H264 Level: %s\n",
1494 level_names[level]);
1497 static void coda_buf_queue(struct vb2_buffer *vb)
1499 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1500 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1501 struct vb2_queue *vq = vb->vb2_queue;
1502 struct coda_q_data *q_data;
1504 q_data = get_q_data(ctx, vb->vb2_queue->type);
1507 * In the decoder case, immediately try to copy the buffer into the
1508 * bitstream ringbuffer and mark it as ready to be dequeued.
1510 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1512 * For backwards compatibility, queuing an empty buffer marks
1513 * the stream end
1515 if (vb2_get_plane_payload(vb, 0) == 0)
1516 coda_bit_stream_end_flag(ctx);
1518 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1520 * Unless already done, try to obtain profile_idc and
1521 * level_idc from the SPS header. This allows to decide
1522 * whether to enable reordering during sequence
1523 * initialization.
1525 if (!ctx->params.h264_profile_idc) {
1526 coda_sps_parse_profile(ctx, vb);
1527 coda_update_h264_profile_ctrl(ctx);
1528 coda_update_h264_level_ctrl(ctx);
1532 mutex_lock(&ctx->bitstream_mutex);
1533 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1534 if (vb2_is_streaming(vb->vb2_queue))
1535 /* This set buf->sequence = ctx->qsequence++ */
1536 coda_fill_bitstream(ctx, NULL);
1537 mutex_unlock(&ctx->bitstream_mutex);
1538 } else {
1539 if (ctx->inst_type == CODA_INST_ENCODER &&
1540 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1541 vbuf->sequence = ctx->qsequence++;
1542 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1546 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1547 size_t size, const char *name, struct dentry *parent)
1549 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1550 GFP_KERNEL);
1551 if (!buf->vaddr) {
1552 v4l2_err(&dev->v4l2_dev,
1553 "Failed to allocate %s buffer of size %zu\n",
1554 name, size);
1555 return -ENOMEM;
1558 buf->size = size;
1560 if (name && parent) {
1561 buf->blob.data = buf->vaddr;
1562 buf->blob.size = size;
1563 buf->dentry = debugfs_create_blob(name, 0644, parent,
1564 &buf->blob);
1565 if (!buf->dentry)
1566 dev_warn(&dev->plat_dev->dev,
1567 "failed to create debugfs entry %s\n", name);
1570 return 0;
1573 void coda_free_aux_buf(struct coda_dev *dev,
1574 struct coda_aux_buf *buf)
1576 if (buf->vaddr) {
1577 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1578 buf->vaddr, buf->paddr);
1579 buf->vaddr = NULL;
1580 buf->size = 0;
1581 debugfs_remove(buf->dentry);
1582 buf->dentry = NULL;
1586 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1588 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1589 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1590 struct coda_q_data *q_data_src, *q_data_dst;
1591 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1592 struct vb2_v4l2_buffer *buf;
1593 struct list_head list;
1594 int ret = 0;
1596 if (count < 1)
1597 return -EINVAL;
1599 INIT_LIST_HEAD(&list);
1601 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1602 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1603 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1604 /* copy the buffers that were queued before streamon */
1605 mutex_lock(&ctx->bitstream_mutex);
1606 coda_fill_bitstream(ctx, &list);
1607 mutex_unlock(&ctx->bitstream_mutex);
1609 if (coda_get_bitstream_payload(ctx) < 512) {
1610 ret = -EINVAL;
1611 goto err;
1615 ctx->streamon_out = 1;
1616 } else {
1617 ctx->streamon_cap = 1;
1620 /* Don't start the coda unless both queues are on */
1621 if (!(ctx->streamon_out && ctx->streamon_cap))
1622 goto out;
1624 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1625 if ((q_data_src->rect.width != q_data_dst->width &&
1626 round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
1627 (q_data_src->rect.height != q_data_dst->height &&
1628 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
1629 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1630 q_data_src->rect.width, q_data_src->rect.height,
1631 q_data_dst->width, q_data_dst->height);
1632 ret = -EINVAL;
1633 goto err;
1636 /* Allow BIT decoder device_run with no new buffers queued */
1637 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1638 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1640 ctx->gopcounter = ctx->params.gop_size - 1;
1642 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1643 q_data_dst->fourcc);
1644 if (!ctx->codec) {
1645 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1646 ret = -EINVAL;
1647 goto err;
1650 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1651 ctx->params.gop_size = 1;
1652 ctx->gopcounter = ctx->params.gop_size - 1;
1654 ret = ctx->ops->start_streaming(ctx);
1655 if (ctx->inst_type == CODA_INST_DECODER) {
1656 if (ret == -EAGAIN)
1657 goto out;
1659 if (ret < 0)
1660 goto err;
1662 out:
1663 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1664 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1665 list_del(&m2m_buf->list);
1666 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
1669 return 0;
1671 err:
1672 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1673 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1674 list_del(&m2m_buf->list);
1675 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
1677 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1678 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1679 } else {
1680 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1681 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1683 return ret;
1686 static void coda_stop_streaming(struct vb2_queue *q)
1688 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1689 struct coda_dev *dev = ctx->dev;
1690 struct vb2_v4l2_buffer *buf;
1691 unsigned long flags;
1692 bool stop;
1694 stop = ctx->streamon_out && ctx->streamon_cap;
1696 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1697 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1698 "%s: output\n", __func__);
1699 ctx->streamon_out = 0;
1701 coda_bit_stream_end_flag(ctx);
1703 ctx->qsequence = 0;
1705 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1706 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1707 } else {
1708 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1709 "%s: capture\n", __func__);
1710 ctx->streamon_cap = 0;
1712 ctx->osequence = 0;
1713 ctx->sequence_offset = 0;
1715 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1716 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1719 if (stop) {
1720 struct coda_buffer_meta *meta;
1722 if (ctx->ops->seq_end_work) {
1723 queue_work(dev->workqueue, &ctx->seq_end_work);
1724 flush_work(&ctx->seq_end_work);
1726 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1727 while (!list_empty(&ctx->buffer_meta_list)) {
1728 meta = list_first_entry(&ctx->buffer_meta_list,
1729 struct coda_buffer_meta, list);
1730 list_del(&meta->list);
1731 kfree(meta);
1733 ctx->num_metas = 0;
1734 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1735 kfifo_init(&ctx->bitstream_fifo,
1736 ctx->bitstream.vaddr, ctx->bitstream.size);
1737 ctx->runcounter = 0;
1738 ctx->aborting = 0;
1739 ctx->hold = false;
1742 if (!ctx->streamon_out && !ctx->streamon_cap)
1743 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1746 static const struct vb2_ops coda_qops = {
1747 .queue_setup = coda_queue_setup,
1748 .buf_prepare = coda_buf_prepare,
1749 .buf_queue = coda_buf_queue,
1750 .start_streaming = coda_start_streaming,
1751 .stop_streaming = coda_stop_streaming,
1752 .wait_prepare = vb2_ops_wait_prepare,
1753 .wait_finish = vb2_ops_wait_finish,
1756 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1758 struct coda_ctx *ctx =
1759 container_of(ctrl->handler, struct coda_ctx, ctrls);
1761 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1762 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1764 switch (ctrl->id) {
1765 case V4L2_CID_HFLIP:
1766 if (ctrl->val)
1767 ctx->params.rot_mode |= CODA_MIR_HOR;
1768 else
1769 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1770 break;
1771 case V4L2_CID_VFLIP:
1772 if (ctrl->val)
1773 ctx->params.rot_mode |= CODA_MIR_VER;
1774 else
1775 ctx->params.rot_mode &= ~CODA_MIR_VER;
1776 break;
1777 case V4L2_CID_MPEG_VIDEO_BITRATE:
1778 ctx->params.bitrate = ctrl->val / 1000;
1779 break;
1780 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1781 ctx->params.gop_size = ctrl->val;
1782 break;
1783 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1784 ctx->params.h264_intra_qp = ctrl->val;
1785 break;
1786 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1787 ctx->params.h264_inter_qp = ctrl->val;
1788 break;
1789 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1790 ctx->params.h264_min_qp = ctrl->val;
1791 break;
1792 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1793 ctx->params.h264_max_qp = ctrl->val;
1794 break;
1795 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1796 ctx->params.h264_deblk_alpha = ctrl->val;
1797 break;
1798 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1799 ctx->params.h264_deblk_beta = ctrl->val;
1800 break;
1801 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1802 ctx->params.h264_deblk_enabled = (ctrl->val ==
1803 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1804 break;
1805 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1806 /* TODO: switch between baseline and constrained baseline */
1807 ctx->params.h264_profile_idc = 66;
1808 break;
1809 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1810 /* nothing to do, this is set by the encoder */
1811 break;
1812 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1813 ctx->params.mpeg4_intra_qp = ctrl->val;
1814 break;
1815 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1816 ctx->params.mpeg4_inter_qp = ctrl->val;
1817 break;
1818 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1819 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1820 /* nothing to do, these are fixed */
1821 break;
1822 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1823 ctx->params.slice_mode = ctrl->val;
1824 break;
1825 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1826 ctx->params.slice_max_mb = ctrl->val;
1827 break;
1828 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1829 ctx->params.slice_max_bits = ctrl->val * 8;
1830 break;
1831 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1832 break;
1833 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1834 ctx->params.intra_refresh = ctrl->val;
1835 break;
1836 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1837 ctx->params.force_ipicture = true;
1838 break;
1839 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1840 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1841 break;
1842 case V4L2_CID_JPEG_RESTART_INTERVAL:
1843 ctx->params.jpeg_restart_interval = ctrl->val;
1844 break;
1845 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1846 ctx->params.vbv_delay = ctrl->val;
1847 break;
1848 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1849 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1850 break;
1851 default:
1852 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1853 "Invalid control, id=%d, val=%d\n",
1854 ctrl->id, ctrl->val);
1855 return -EINVAL;
1858 return 0;
1861 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1862 .s_ctrl = coda_s_ctrl,
1865 static void coda_encode_ctrls(struct coda_ctx *ctx)
1867 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
1869 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1870 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1871 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1872 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
1873 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1874 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1875 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1876 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1877 if (ctx->dev->devtype->product != CODA_960) {
1878 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1879 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1881 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1882 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1883 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1884 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1885 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1886 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1887 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1888 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1889 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1890 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1891 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1892 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1893 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
1894 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
1895 if (ctx->dev->devtype->product == CODA_HX4 ||
1896 ctx->dev->devtype->product == CODA_7541) {
1897 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1898 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1899 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
1900 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1901 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1902 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
1903 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
1905 if (ctx->dev->devtype->product == CODA_960) {
1906 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1907 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1908 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
1909 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1910 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1911 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
1912 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
1913 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
1914 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1916 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1917 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1918 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1919 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1920 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1921 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
1922 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
1923 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
1924 if (ctx->dev->devtype->product == CODA_HX4 ||
1925 ctx->dev->devtype->product == CODA_7541 ||
1926 ctx->dev->devtype->product == CODA_960) {
1927 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1928 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
1929 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
1930 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
1931 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
1933 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1934 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1935 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1936 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1937 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1938 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1939 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1940 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1941 500);
1942 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1943 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1944 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1945 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1946 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1947 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1948 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1949 1920 * 1088 / 256, 1, 0);
1950 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1951 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1953 * The maximum VBV size value is 0x7fffffff bits,
1954 * one bit less than 262144 KiB
1956 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1957 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
1960 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1962 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1963 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1964 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1965 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1968 static void coda_decode_ctrls(struct coda_ctx *ctx)
1970 u64 mask;
1971 u8 max;
1973 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
1974 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1975 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
1976 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
1977 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
1978 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
1979 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
1980 if (ctx->h264_profile_ctrl)
1981 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1983 if (ctx->dev->devtype->product == CODA_HX4 ||
1984 ctx->dev->devtype->product == CODA_7541) {
1985 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1986 mask = ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1987 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1988 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
1989 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
1990 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0));
1991 } else if (ctx->dev->devtype->product == CODA_960) {
1992 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
1993 mask = ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1994 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1995 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
1996 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
1997 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0) |
1998 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_1));
1999 } else {
2000 return;
2002 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2003 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, mask,
2004 max);
2005 if (ctx->h264_level_ctrl)
2006 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2009 static int coda_ctrls_setup(struct coda_ctx *ctx)
2011 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2013 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2014 V4L2_CID_HFLIP, 0, 1, 1, 0);
2015 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2016 V4L2_CID_VFLIP, 0, 1, 1, 0);
2017 if (ctx->inst_type == CODA_INST_ENCODER) {
2018 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2019 coda_jpeg_encode_ctrls(ctx);
2020 else
2021 coda_encode_ctrls(ctx);
2022 } else {
2023 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2024 coda_decode_ctrls(ctx);
2027 if (ctx->ctrls.error) {
2028 v4l2_err(&ctx->dev->v4l2_dev,
2029 "control initialization error (%d)",
2030 ctx->ctrls.error);
2031 return -EINVAL;
2034 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2037 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2039 vq->drv_priv = ctx;
2040 vq->ops = &coda_qops;
2041 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2042 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2043 vq->lock = &ctx->dev->dev_mutex;
2044 /* One way to indicate end-of-stream for coda is to set the
2045 * bytesused == 0. However by default videobuf2 handles bytesused
2046 * equal to 0 as a special case and changes its value to the size
2047 * of the buffer. Set the allow_zero_bytesused flag, so
2048 * that videobuf2 will keep the value of bytesused intact.
2050 vq->allow_zero_bytesused = 1;
2052 * We might be fine with no buffers on some of the queues, but that
2053 * would need to be reflected in job_ready(). Currently we expect all
2054 * queues to have at least one buffer queued.
2056 vq->min_buffers_needed = 1;
2057 vq->dev = &ctx->dev->plat_dev->dev;
2059 return vb2_queue_init(vq);
2062 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2063 struct vb2_queue *dst_vq)
2065 int ret;
2067 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2068 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2069 src_vq->mem_ops = &vb2_dma_contig_memops;
2071 ret = coda_queue_init(priv, src_vq);
2072 if (ret)
2073 return ret;
2075 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2076 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2077 dst_vq->mem_ops = &vb2_dma_contig_memops;
2079 return coda_queue_init(priv, dst_vq);
2082 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2083 struct vb2_queue *dst_vq)
2085 int ret;
2087 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2088 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2089 src_vq->mem_ops = &vb2_vmalloc_memops;
2091 ret = coda_queue_init(priv, src_vq);
2092 if (ret)
2093 return ret;
2095 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2096 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2097 dst_vq->mem_ops = &vb2_dma_contig_memops;
2099 return coda_queue_init(priv, dst_vq);
2102 static int coda_next_free_instance(struct coda_dev *dev)
2104 int idx = ffz(dev->instance_mask);
2106 if ((idx < 0) ||
2107 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2108 return -EBUSY;
2110 return idx;
2114 * File operations
2117 static int coda_open(struct file *file)
2119 struct video_device *vdev = video_devdata(file);
2120 struct coda_dev *dev = video_get_drvdata(vdev);
2121 struct coda_ctx *ctx = NULL;
2122 char *name;
2123 int ret;
2124 int idx;
2126 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2127 if (!ctx)
2128 return -ENOMEM;
2130 idx = coda_next_free_instance(dev);
2131 if (idx < 0) {
2132 ret = idx;
2133 goto err_coda_max;
2135 set_bit(idx, &dev->instance_mask);
2137 name = kasprintf(GFP_KERNEL, "context%d", idx);
2138 if (!name) {
2139 ret = -ENOMEM;
2140 goto err_coda_name_init;
2143 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2144 kfree(name);
2146 ctx->cvd = to_coda_video_device(vdev);
2147 ctx->inst_type = ctx->cvd->type;
2148 ctx->ops = ctx->cvd->ops;
2149 ctx->use_bit = !ctx->cvd->direct;
2150 init_completion(&ctx->completion);
2151 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2152 if (ctx->ops->seq_end_work)
2153 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2154 v4l2_fh_init(&ctx->fh, video_devdata(file));
2155 file->private_data = &ctx->fh;
2156 v4l2_fh_add(&ctx->fh);
2157 ctx->dev = dev;
2158 ctx->idx = idx;
2159 switch (dev->devtype->product) {
2160 case CODA_960:
2162 * Enabling the BWB when decoding can hang the firmware with
2163 * certain streams. The issue was tracked as ENGR00293425 by
2164 * Freescale. As a workaround, disable BWB for all decoders.
2165 * The enable_bwb module parameter allows to override this.
2167 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2168 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2169 /* fallthrough */
2170 case CODA_HX4:
2171 case CODA_7541:
2172 ctx->reg_idx = 0;
2173 break;
2174 default:
2175 ctx->reg_idx = idx;
2177 if (ctx->dev->vdoa && !disable_vdoa) {
2178 ctx->vdoa = vdoa_context_create(dev->vdoa);
2179 if (!ctx->vdoa)
2180 v4l2_warn(&dev->v4l2_dev,
2181 "Failed to create vdoa context: not using vdoa");
2183 ctx->use_vdoa = false;
2185 /* Power up and upload firmware if necessary */
2186 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2187 if (ret < 0) {
2188 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2189 goto err_pm_get;
2192 ret = clk_prepare_enable(dev->clk_per);
2193 if (ret)
2194 goto err_clk_per;
2196 ret = clk_prepare_enable(dev->clk_ahb);
2197 if (ret)
2198 goto err_clk_ahb;
2200 set_default_params(ctx);
2201 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2202 ctx->ops->queue_init);
2203 if (IS_ERR(ctx->fh.m2m_ctx)) {
2204 ret = PTR_ERR(ctx->fh.m2m_ctx);
2206 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2207 __func__, ret);
2208 goto err_ctx_init;
2211 ret = coda_ctrls_setup(ctx);
2212 if (ret) {
2213 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2214 goto err_ctrls_setup;
2217 ctx->fh.ctrl_handler = &ctx->ctrls;
2219 mutex_init(&ctx->bitstream_mutex);
2220 mutex_init(&ctx->buffer_mutex);
2221 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2222 spin_lock_init(&ctx->buffer_meta_lock);
2224 mutex_lock(&dev->dev_mutex);
2225 list_add(&ctx->list, &dev->instances);
2226 mutex_unlock(&dev->dev_mutex);
2228 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2229 ctx->idx, ctx);
2231 return 0;
2233 err_ctrls_setup:
2234 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2235 err_ctx_init:
2236 clk_disable_unprepare(dev->clk_ahb);
2237 err_clk_ahb:
2238 clk_disable_unprepare(dev->clk_per);
2239 err_clk_per:
2240 pm_runtime_put_sync(&dev->plat_dev->dev);
2241 err_pm_get:
2242 v4l2_fh_del(&ctx->fh);
2243 v4l2_fh_exit(&ctx->fh);
2244 clear_bit(ctx->idx, &dev->instance_mask);
2245 err_coda_name_init:
2246 err_coda_max:
2247 kfree(ctx);
2248 return ret;
2251 static int coda_release(struct file *file)
2253 struct coda_dev *dev = video_drvdata(file);
2254 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2256 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2257 ctx);
2259 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2260 coda_bit_stream_end_flag(ctx);
2262 /* If this instance is running, call .job_abort and wait for it to end */
2263 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2265 if (ctx->vdoa)
2266 vdoa_context_destroy(ctx->vdoa);
2268 /* In case the instance was not running, we still need to call SEQ_END */
2269 if (ctx->ops->seq_end_work) {
2270 queue_work(dev->workqueue, &ctx->seq_end_work);
2271 flush_work(&ctx->seq_end_work);
2274 mutex_lock(&dev->dev_mutex);
2275 list_del(&ctx->list);
2276 mutex_unlock(&dev->dev_mutex);
2278 if (ctx->dev->devtype->product == CODA_DX6)
2279 coda_free_aux_buf(dev, &ctx->workbuf);
2281 v4l2_ctrl_handler_free(&ctx->ctrls);
2282 clk_disable_unprepare(dev->clk_ahb);
2283 clk_disable_unprepare(dev->clk_per);
2284 pm_runtime_put_sync(&dev->plat_dev->dev);
2285 v4l2_fh_del(&ctx->fh);
2286 v4l2_fh_exit(&ctx->fh);
2287 clear_bit(ctx->idx, &dev->instance_mask);
2288 if (ctx->ops->release)
2289 ctx->ops->release(ctx);
2290 debugfs_remove_recursive(ctx->debugfs_entry);
2291 kfree(ctx);
2293 return 0;
2296 static const struct v4l2_file_operations coda_fops = {
2297 .owner = THIS_MODULE,
2298 .open = coda_open,
2299 .release = coda_release,
2300 .poll = v4l2_m2m_fop_poll,
2301 .unlocked_ioctl = video_ioctl2,
2302 .mmap = v4l2_m2m_fop_mmap,
2305 static int coda_hw_init(struct coda_dev *dev)
2307 u32 data;
2308 u16 *p;
2309 int i, ret;
2311 ret = clk_prepare_enable(dev->clk_per);
2312 if (ret)
2313 goto err_clk_per;
2315 ret = clk_prepare_enable(dev->clk_ahb);
2316 if (ret)
2317 goto err_clk_ahb;
2319 reset_control_reset(dev->rstc);
2322 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2323 * The 16-bit chars in the code buffer are in memory access
2324 * order, re-sort them to CODA order for register download.
2325 * Data in this SRAM survives a reboot.
2327 p = (u16 *)dev->codebuf.vaddr;
2328 if (dev->devtype->product == CODA_DX6) {
2329 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2330 data = CODA_DOWN_ADDRESS_SET(i) |
2331 CODA_DOWN_DATA_SET(p[i ^ 1]);
2332 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2334 } else {
2335 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2336 data = CODA_DOWN_ADDRESS_SET(i) |
2337 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2338 3 - (i % 4)]);
2339 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2343 /* Clear registers */
2344 for (i = 0; i < 64; i++)
2345 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2347 /* Tell the BIT where to find everything it needs */
2348 if (dev->devtype->product == CODA_960 ||
2349 dev->devtype->product == CODA_7541 ||
2350 dev->devtype->product == CODA_HX4) {
2351 coda_write(dev, dev->tempbuf.paddr,
2352 CODA_REG_BIT_TEMP_BUF_ADDR);
2353 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2354 } else {
2355 coda_write(dev, dev->workbuf.paddr,
2356 CODA_REG_BIT_WORK_BUF_ADDR);
2358 coda_write(dev, dev->codebuf.paddr,
2359 CODA_REG_BIT_CODE_BUF_ADDR);
2360 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2362 /* Set default values */
2363 switch (dev->devtype->product) {
2364 case CODA_DX6:
2365 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2366 CODA_REG_BIT_STREAM_CTRL);
2367 break;
2368 default:
2369 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2370 CODA_REG_BIT_STREAM_CTRL);
2372 if (dev->devtype->product == CODA_960)
2373 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2374 CODA_REG_BIT_FRAME_MEM_CTRL);
2375 else
2376 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2378 if (dev->devtype->product != CODA_DX6)
2379 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2381 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2382 CODA_REG_BIT_INT_ENABLE);
2384 /* Reset VPU and start processor */
2385 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2386 data |= CODA_REG_RESET_ENABLE;
2387 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2388 udelay(10);
2389 data &= ~CODA_REG_RESET_ENABLE;
2390 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2391 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2393 clk_disable_unprepare(dev->clk_ahb);
2394 clk_disable_unprepare(dev->clk_per);
2396 return 0;
2398 err_clk_ahb:
2399 clk_disable_unprepare(dev->clk_per);
2400 err_clk_per:
2401 return ret;
2404 static int coda_register_device(struct coda_dev *dev, int i)
2406 struct video_device *vfd = &dev->vfd[i];
2408 if (i >= dev->devtype->num_vdevs)
2409 return -EINVAL;
2411 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2412 vfd->fops = &coda_fops;
2413 vfd->ioctl_ops = &coda_ioctl_ops;
2414 vfd->release = video_device_release_empty,
2415 vfd->lock = &dev->dev_mutex;
2416 vfd->v4l2_dev = &dev->v4l2_dev;
2417 vfd->vfl_dir = VFL_DIR_M2M;
2418 video_set_drvdata(vfd, dev);
2420 /* Not applicable, use the selection API instead */
2421 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2422 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2423 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2425 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2428 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2429 size_t size)
2431 u32 *src = (u32 *)buf;
2433 /* Check if the firmware has a 16-byte Freescale header, skip it */
2434 if (buf[0] == 'M' && buf[1] == 'X')
2435 src += 4;
2437 * Check whether the firmware is in native order or pre-reordered for
2438 * memory access. The first instruction opcode always is 0xe40e.
2440 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2441 u32 *dst = dev->codebuf.vaddr;
2442 int i;
2444 /* Firmware in native order, reorder while copying */
2445 if (dev->devtype->product == CODA_DX6) {
2446 for (i = 0; i < (size - 16) / 4; i++)
2447 dst[i] = (src[i] << 16) | (src[i] >> 16);
2448 } else {
2449 for (i = 0; i < (size - 16) / 4; i += 2) {
2450 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2451 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2454 } else {
2455 /* Copy the already reordered firmware image */
2456 memcpy(dev->codebuf.vaddr, src, size);
2460 static void coda_fw_callback(const struct firmware *fw, void *context);
2462 static int coda_firmware_request(struct coda_dev *dev)
2464 char *fw;
2466 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2467 return -EINVAL;
2469 fw = dev->devtype->firmware[dev->firmware];
2471 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2472 coda_product_name(dev->devtype->product));
2474 return request_firmware_nowait(THIS_MODULE, true, fw,
2475 &dev->plat_dev->dev, GFP_KERNEL, dev,
2476 coda_fw_callback);
2479 static void coda_fw_callback(const struct firmware *fw, void *context)
2481 struct coda_dev *dev = context;
2482 struct platform_device *pdev = dev->plat_dev;
2483 int i, ret;
2485 if (!fw) {
2486 dev->firmware++;
2487 ret = coda_firmware_request(dev);
2488 if (ret < 0) {
2489 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2490 goto put_pm;
2492 return;
2494 if (dev->firmware > 0) {
2496 * Since we can't suppress warnings for failed asynchronous
2497 * firmware requests, report that the fallback firmware was
2498 * found.
2500 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2501 dev->devtype->firmware[dev->firmware]);
2504 /* allocate auxiliary per-device code buffer for the BIT processor */
2505 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2506 dev->debugfs_root);
2507 if (ret < 0)
2508 goto put_pm;
2510 coda_copy_firmware(dev, fw->data, fw->size);
2511 release_firmware(fw);
2513 ret = coda_hw_init(dev);
2514 if (ret < 0) {
2515 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2516 goto put_pm;
2519 ret = coda_check_firmware(dev);
2520 if (ret < 0)
2521 goto put_pm;
2523 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2524 if (IS_ERR(dev->m2m_dev)) {
2525 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2526 goto put_pm;
2529 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2530 ret = coda_register_device(dev, i);
2531 if (ret) {
2532 v4l2_err(&dev->v4l2_dev,
2533 "Failed to register %s video device: %d\n",
2534 dev->devtype->vdevs[i]->name, ret);
2535 goto rel_vfd;
2539 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
2540 dev->vfd[0].num, dev->vfd[i - 1].num);
2542 pm_runtime_put_sync(&pdev->dev);
2543 return;
2545 rel_vfd:
2546 while (--i >= 0)
2547 video_unregister_device(&dev->vfd[i]);
2548 v4l2_m2m_release(dev->m2m_dev);
2549 put_pm:
2550 pm_runtime_put_sync(&pdev->dev);
2553 enum coda_platform {
2554 CODA_IMX27,
2555 CODA_IMX51,
2556 CODA_IMX53,
2557 CODA_IMX6Q,
2558 CODA_IMX6DL,
2561 static const struct coda_devtype coda_devdata[] = {
2562 [CODA_IMX27] = {
2563 .firmware = {
2564 "vpu_fw_imx27_TO2.bin",
2565 "vpu/vpu_fw_imx27_TO2.bin",
2566 "v4l-codadx6-imx27.bin"
2568 .product = CODA_DX6,
2569 .codecs = codadx6_codecs,
2570 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2571 .vdevs = codadx6_video_devices,
2572 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2573 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2574 .iram_size = 0xb000,
2576 [CODA_IMX51] = {
2577 .firmware = {
2578 "vpu_fw_imx51.bin",
2579 "vpu/vpu_fw_imx51.bin",
2580 "v4l-codahx4-imx51.bin"
2582 .product = CODA_HX4,
2583 .codecs = codahx4_codecs,
2584 .num_codecs = ARRAY_SIZE(codahx4_codecs),
2585 .vdevs = codahx4_video_devices,
2586 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
2587 .workbuf_size = 128 * 1024,
2588 .tempbuf_size = 304 * 1024,
2589 .iram_size = 0x14000,
2591 [CODA_IMX53] = {
2592 .firmware = {
2593 "vpu_fw_imx53.bin",
2594 "vpu/vpu_fw_imx53.bin",
2595 "v4l-coda7541-imx53.bin"
2597 .product = CODA_7541,
2598 .codecs = coda7_codecs,
2599 .num_codecs = ARRAY_SIZE(coda7_codecs),
2600 .vdevs = coda7_video_devices,
2601 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2602 .workbuf_size = 128 * 1024,
2603 .tempbuf_size = 304 * 1024,
2604 .iram_size = 0x14000,
2606 [CODA_IMX6Q] = {
2607 .firmware = {
2608 "vpu_fw_imx6q.bin",
2609 "vpu/vpu_fw_imx6q.bin",
2610 "v4l-coda960-imx6q.bin"
2612 .product = CODA_960,
2613 .codecs = coda9_codecs,
2614 .num_codecs = ARRAY_SIZE(coda9_codecs),
2615 .vdevs = coda9_video_devices,
2616 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2617 .workbuf_size = 80 * 1024,
2618 .tempbuf_size = 204 * 1024,
2619 .iram_size = 0x21000,
2621 [CODA_IMX6DL] = {
2622 .firmware = {
2623 "vpu_fw_imx6d.bin",
2624 "vpu/vpu_fw_imx6d.bin",
2625 "v4l-coda960-imx6dl.bin"
2627 .product = CODA_960,
2628 .codecs = coda9_codecs,
2629 .num_codecs = ARRAY_SIZE(coda9_codecs),
2630 .vdevs = coda9_video_devices,
2631 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2632 .workbuf_size = 80 * 1024,
2633 .tempbuf_size = 204 * 1024,
2634 .iram_size = 0x1f000, /* leave 4k for suspend code */
2638 static const struct platform_device_id coda_platform_ids[] = {
2639 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2640 { /* sentinel */ }
2642 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2644 #ifdef CONFIG_OF
2645 static const struct of_device_id coda_dt_ids[] = {
2646 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2647 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
2648 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2649 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2650 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2651 { /* sentinel */ }
2653 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2654 #endif
2656 static int coda_probe(struct platform_device *pdev)
2658 const struct of_device_id *of_id =
2659 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2660 const struct platform_device_id *pdev_id;
2661 struct coda_platform_data *pdata = pdev->dev.platform_data;
2662 struct device_node *np = pdev->dev.of_node;
2663 struct gen_pool *pool;
2664 struct coda_dev *dev;
2665 struct resource *res;
2666 int ret, irq;
2668 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2669 if (!dev)
2670 return -ENOMEM;
2672 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2674 if (of_id)
2675 dev->devtype = of_id->data;
2676 else if (pdev_id)
2677 dev->devtype = &coda_devdata[pdev_id->driver_data];
2678 else
2679 return -EINVAL;
2681 spin_lock_init(&dev->irqlock);
2682 INIT_LIST_HEAD(&dev->instances);
2684 dev->plat_dev = pdev;
2685 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2686 if (IS_ERR(dev->clk_per)) {
2687 dev_err(&pdev->dev, "Could not get per clock\n");
2688 return PTR_ERR(dev->clk_per);
2691 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2692 if (IS_ERR(dev->clk_ahb)) {
2693 dev_err(&pdev->dev, "Could not get ahb clock\n");
2694 return PTR_ERR(dev->clk_ahb);
2697 /* Get memory for physical registers */
2698 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2699 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2700 if (IS_ERR(dev->regs_base))
2701 return PTR_ERR(dev->regs_base);
2703 /* IRQ */
2704 irq = platform_get_irq_byname(pdev, "bit");
2705 if (irq < 0)
2706 irq = platform_get_irq(pdev, 0);
2707 if (irq < 0) {
2708 dev_err(&pdev->dev, "failed to get irq resource\n");
2709 return irq;
2712 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2713 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2714 if (ret < 0) {
2715 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2716 return ret;
2719 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
2720 NULL);
2721 if (IS_ERR(dev->rstc)) {
2722 ret = PTR_ERR(dev->rstc);
2723 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
2724 return ret;
2727 /* Get IRAM pool from device tree or platform data */
2728 pool = of_gen_pool_get(np, "iram", 0);
2729 if (!pool && pdata)
2730 pool = gen_pool_get(pdata->iram_dev, NULL);
2731 if (!pool) {
2732 dev_err(&pdev->dev, "iram pool not available\n");
2733 return -ENOMEM;
2735 dev->iram_pool = pool;
2737 /* Get vdoa_data if supported by the platform */
2738 dev->vdoa = coda_get_vdoa_data();
2739 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
2740 return -EPROBE_DEFER;
2742 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2743 if (ret)
2744 return ret;
2746 mutex_init(&dev->dev_mutex);
2747 mutex_init(&dev->coda_mutex);
2749 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2750 if (!dev->debugfs_root)
2751 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2753 /* allocate auxiliary per-device buffers for the BIT processor */
2754 if (dev->devtype->product == CODA_DX6) {
2755 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2756 dev->devtype->workbuf_size, "workbuf",
2757 dev->debugfs_root);
2758 if (ret < 0)
2759 goto err_v4l2_register;
2762 if (dev->devtype->tempbuf_size) {
2763 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2764 dev->devtype->tempbuf_size, "tempbuf",
2765 dev->debugfs_root);
2766 if (ret < 0)
2767 goto err_v4l2_register;
2770 dev->iram.size = dev->devtype->iram_size;
2771 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2772 &dev->iram.paddr);
2773 if (!dev->iram.vaddr) {
2774 dev_warn(&pdev->dev, "unable to alloc iram\n");
2775 } else {
2776 memset(dev->iram.vaddr, 0, dev->iram.size);
2777 dev->iram.blob.data = dev->iram.vaddr;
2778 dev->iram.blob.size = dev->iram.size;
2779 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2780 dev->debugfs_root,
2781 &dev->iram.blob);
2784 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2785 if (!dev->workqueue) {
2786 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2787 ret = -ENOMEM;
2788 goto err_v4l2_register;
2791 platform_set_drvdata(pdev, dev);
2794 * Start activated so we can directly call coda_hw_init in
2795 * coda_fw_callback regardless of whether CONFIG_PM is
2796 * enabled or whether the device is associated with a PM domain.
2798 pm_runtime_get_noresume(&pdev->dev);
2799 pm_runtime_set_active(&pdev->dev);
2800 pm_runtime_enable(&pdev->dev);
2802 ret = coda_firmware_request(dev);
2803 if (ret)
2804 goto err_alloc_workqueue;
2805 return 0;
2807 err_alloc_workqueue:
2808 destroy_workqueue(dev->workqueue);
2809 err_v4l2_register:
2810 v4l2_device_unregister(&dev->v4l2_dev);
2811 return ret;
2814 static int coda_remove(struct platform_device *pdev)
2816 struct coda_dev *dev = platform_get_drvdata(pdev);
2817 int i;
2819 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2820 if (video_get_drvdata(&dev->vfd[i]))
2821 video_unregister_device(&dev->vfd[i]);
2823 if (dev->m2m_dev)
2824 v4l2_m2m_release(dev->m2m_dev);
2825 pm_runtime_disable(&pdev->dev);
2826 v4l2_device_unregister(&dev->v4l2_dev);
2827 destroy_workqueue(dev->workqueue);
2828 if (dev->iram.vaddr)
2829 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2830 dev->iram.size);
2831 coda_free_aux_buf(dev, &dev->codebuf);
2832 coda_free_aux_buf(dev, &dev->tempbuf);
2833 coda_free_aux_buf(dev, &dev->workbuf);
2834 debugfs_remove_recursive(dev->debugfs_root);
2835 return 0;
2838 #ifdef CONFIG_PM
2839 static int coda_runtime_resume(struct device *dev)
2841 struct coda_dev *cdev = dev_get_drvdata(dev);
2842 int ret = 0;
2844 if (dev->pm_domain && cdev->codebuf.vaddr) {
2845 ret = coda_hw_init(cdev);
2846 if (ret)
2847 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2850 return ret;
2852 #endif
2854 static const struct dev_pm_ops coda_pm_ops = {
2855 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2858 static struct platform_driver coda_driver = {
2859 .probe = coda_probe,
2860 .remove = coda_remove,
2861 .driver = {
2862 .name = CODA_NAME,
2863 .of_match_table = of_match_ptr(coda_dt_ids),
2864 .pm = &coda_pm_ops,
2866 .id_table = coda_platform_ids,
2869 module_platform_driver(coda_driver);
2871 MODULE_LICENSE("GPL");
2872 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2873 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");