Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / platform / coda / coda-common.c
blobe8a7554a61d2400d5e58434b48c58088c7abf343
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.MX5x -> coda7
132 * i.MX6 -> coda960
133 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
135 static const struct coda_codec codadx6_codecs[] = {
136 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
137 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
140 static const struct coda_codec coda7_codecs[] = {
141 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
142 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
143 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
144 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
145 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
146 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
147 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
150 static const struct coda_codec coda9_codecs[] = {
151 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
152 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
153 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
154 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
155 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
158 struct coda_video_device {
159 const char *name;
160 enum coda_inst_type type;
161 const struct coda_context_ops *ops;
162 bool direct;
163 u32 src_formats[CODA_MAX_FORMATS];
164 u32 dst_formats[CODA_MAX_FORMATS];
167 static const struct coda_video_device coda_bit_encoder = {
168 .name = "coda-encoder",
169 .type = CODA_INST_ENCODER,
170 .ops = &coda_bit_encode_ops,
171 .src_formats = {
172 V4L2_PIX_FMT_NV12,
173 V4L2_PIX_FMT_YUV420,
174 V4L2_PIX_FMT_YVU420,
176 .dst_formats = {
177 V4L2_PIX_FMT_H264,
178 V4L2_PIX_FMT_MPEG4,
182 static const struct coda_video_device coda_bit_jpeg_encoder = {
183 .name = "coda-jpeg-encoder",
184 .type = CODA_INST_ENCODER,
185 .ops = &coda_bit_encode_ops,
186 .src_formats = {
187 V4L2_PIX_FMT_NV12,
188 V4L2_PIX_FMT_YUV420,
189 V4L2_PIX_FMT_YVU420,
190 V4L2_PIX_FMT_YUV422P,
192 .dst_formats = {
193 V4L2_PIX_FMT_JPEG,
197 static const struct coda_video_device coda_bit_decoder = {
198 .name = "coda-decoder",
199 .type = CODA_INST_DECODER,
200 .ops = &coda_bit_decode_ops,
201 .src_formats = {
202 V4L2_PIX_FMT_H264,
203 V4L2_PIX_FMT_MPEG2,
204 V4L2_PIX_FMT_MPEG4,
206 .dst_formats = {
207 V4L2_PIX_FMT_NV12,
208 V4L2_PIX_FMT_YUV420,
209 V4L2_PIX_FMT_YVU420,
211 * If V4L2_PIX_FMT_YUYV should be default,
212 * set_default_params() must be adjusted.
214 V4L2_PIX_FMT_YUYV,
218 static const struct coda_video_device coda_bit_jpeg_decoder = {
219 .name = "coda-jpeg-decoder",
220 .type = CODA_INST_DECODER,
221 .ops = &coda_bit_decode_ops,
222 .src_formats = {
223 V4L2_PIX_FMT_JPEG,
225 .dst_formats = {
226 V4L2_PIX_FMT_NV12,
227 V4L2_PIX_FMT_YUV420,
228 V4L2_PIX_FMT_YVU420,
229 V4L2_PIX_FMT_YUV422P,
233 static const struct coda_video_device *codadx6_video_devices[] = {
234 &coda_bit_encoder,
237 static const struct coda_video_device *coda7_video_devices[] = {
238 &coda_bit_jpeg_encoder,
239 &coda_bit_jpeg_decoder,
240 &coda_bit_encoder,
241 &coda_bit_decoder,
244 static const struct coda_video_device *coda9_video_devices[] = {
245 &coda_bit_encoder,
246 &coda_bit_decoder,
250 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
251 * tables.
253 static u32 coda_format_normalize_yuv(u32 fourcc)
255 switch (fourcc) {
256 case V4L2_PIX_FMT_NV12:
257 case V4L2_PIX_FMT_YUV420:
258 case V4L2_PIX_FMT_YVU420:
259 case V4L2_PIX_FMT_YUV422P:
260 case V4L2_PIX_FMT_YUYV:
261 return V4L2_PIX_FMT_YUV420;
262 default:
263 return fourcc;
267 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
268 int src_fourcc, int dst_fourcc)
270 const struct coda_codec *codecs = dev->devtype->codecs;
271 int num_codecs = dev->devtype->num_codecs;
272 int k;
274 src_fourcc = coda_format_normalize_yuv(src_fourcc);
275 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
276 if (src_fourcc == dst_fourcc)
277 return NULL;
279 for (k = 0; k < num_codecs; k++) {
280 if (codecs[k].src_fourcc == src_fourcc &&
281 codecs[k].dst_fourcc == dst_fourcc)
282 break;
285 if (k == num_codecs)
286 return NULL;
288 return &codecs[k];
291 static void coda_get_max_dimensions(struct coda_dev *dev,
292 const struct coda_codec *codec,
293 int *max_w, int *max_h)
295 const struct coda_codec *codecs = dev->devtype->codecs;
296 int num_codecs = dev->devtype->num_codecs;
297 unsigned int w, h;
298 int k;
300 if (codec) {
301 w = codec->max_w;
302 h = codec->max_h;
303 } else {
304 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
305 w = max(w, codecs[k].max_w);
306 h = max(h, codecs[k].max_h);
310 if (max_w)
311 *max_w = w;
312 if (max_h)
313 *max_h = h;
316 static const struct coda_video_device *to_coda_video_device(struct video_device
317 *vdev)
319 struct coda_dev *dev = video_get_drvdata(vdev);
320 unsigned int i = vdev - dev->vfd;
322 if (i >= dev->devtype->num_vdevs)
323 return NULL;
325 return dev->devtype->vdevs[i];
328 const char *coda_product_name(int product)
330 static char buf[9];
332 switch (product) {
333 case CODA_DX6:
334 return "CodaDx6";
335 case CODA_7541:
336 return "CODA7541";
337 case CODA_960:
338 return "CODA960";
339 default:
340 snprintf(buf, sizeof(buf), "(0x%04x)", product);
341 return buf;
345 static struct vdoa_data *coda_get_vdoa_data(void)
347 struct device_node *vdoa_node;
348 struct platform_device *vdoa_pdev;
349 struct vdoa_data *vdoa_data = NULL;
351 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
352 if (!vdoa_node)
353 return NULL;
355 vdoa_pdev = of_find_device_by_node(vdoa_node);
356 if (!vdoa_pdev)
357 goto out;
359 vdoa_data = platform_get_drvdata(vdoa_pdev);
360 if (!vdoa_data)
361 vdoa_data = ERR_PTR(-EPROBE_DEFER);
363 out:
364 if (vdoa_node)
365 of_node_put(vdoa_node);
367 return vdoa_data;
371 * V4L2 ioctl() operations.
373 static int coda_querycap(struct file *file, void *priv,
374 struct v4l2_capability *cap)
376 struct coda_ctx *ctx = fh_to_ctx(priv);
378 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
379 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
380 sizeof(cap->card));
381 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
382 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
383 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
385 return 0;
388 static int coda_enum_fmt(struct file *file, void *priv,
389 struct v4l2_fmtdesc *f)
391 struct video_device *vdev = video_devdata(file);
392 const struct coda_video_device *cvd = to_coda_video_device(vdev);
393 struct coda_ctx *ctx = fh_to_ctx(priv);
394 const u32 *formats;
396 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
397 formats = cvd->src_formats;
398 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
399 formats = cvd->dst_formats;
400 else
401 return -EINVAL;
403 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
404 return -EINVAL;
406 /* Skip YUYV if the vdoa is not available */
407 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
408 formats[f->index] == V4L2_PIX_FMT_YUYV)
409 return -EINVAL;
411 f->pixelformat = formats[f->index];
413 return 0;
416 static int coda_g_fmt(struct file *file, void *priv,
417 struct v4l2_format *f)
419 struct coda_q_data *q_data;
420 struct coda_ctx *ctx = fh_to_ctx(priv);
422 q_data = get_q_data(ctx, f->type);
423 if (!q_data)
424 return -EINVAL;
426 f->fmt.pix.field = V4L2_FIELD_NONE;
427 f->fmt.pix.pixelformat = q_data->fourcc;
428 f->fmt.pix.width = q_data->width;
429 f->fmt.pix.height = q_data->height;
430 f->fmt.pix.bytesperline = q_data->bytesperline;
432 f->fmt.pix.sizeimage = q_data->sizeimage;
433 f->fmt.pix.colorspace = ctx->colorspace;
434 f->fmt.pix.xfer_func = ctx->xfer_func;
435 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
436 f->fmt.pix.quantization = ctx->quantization;
438 return 0;
441 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
443 struct coda_q_data *q_data;
444 const u32 *formats;
445 int i;
447 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
448 formats = ctx->cvd->src_formats;
449 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
450 formats = ctx->cvd->dst_formats;
451 else
452 return -EINVAL;
454 for (i = 0; i < CODA_MAX_FORMATS; i++) {
455 /* Skip YUYV if the vdoa is not available */
456 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
457 formats[i] == V4L2_PIX_FMT_YUYV)
458 continue;
460 if (formats[i] == f->fmt.pix.pixelformat) {
461 f->fmt.pix.pixelformat = formats[i];
462 return 0;
466 /* Fall back to currently set pixelformat */
467 q_data = get_q_data(ctx, f->type);
468 f->fmt.pix.pixelformat = q_data->fourcc;
470 return 0;
473 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
474 bool *use_vdoa)
476 int err;
478 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
479 return -EINVAL;
481 if (!use_vdoa)
482 return -EINVAL;
484 if (!ctx->vdoa) {
485 *use_vdoa = false;
486 return 0;
489 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
490 f->fmt.pix.height, f->fmt.pix.pixelformat);
491 if (err) {
492 *use_vdoa = false;
493 return 0;
496 *use_vdoa = true;
497 return 0;
500 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
501 u32 width, u32 height)
504 * This is a rough estimate for sensible compressed buffer
505 * sizes (between 1 and 16 bits per pixel). This could be
506 * improved by better format specific worst case estimates.
508 return round_up(clamp(sizeimage, width * height / 8,
509 width * height * 2), PAGE_SIZE);
512 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
513 struct v4l2_format *f)
515 struct coda_dev *dev = ctx->dev;
516 unsigned int max_w, max_h;
517 enum v4l2_field field;
519 field = f->fmt.pix.field;
520 if (field == V4L2_FIELD_ANY)
521 field = V4L2_FIELD_NONE;
522 else if (V4L2_FIELD_NONE != field)
523 return -EINVAL;
525 /* V4L2 specification suggests the driver corrects the format struct
526 * if any of the dimensions is unsupported */
527 f->fmt.pix.field = field;
529 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
530 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
531 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
532 S_ALIGN);
534 switch (f->fmt.pix.pixelformat) {
535 case V4L2_PIX_FMT_NV12:
536 case V4L2_PIX_FMT_YUV420:
537 case V4L2_PIX_FMT_YVU420:
539 * Frame stride must be at least multiple of 8,
540 * but multiple of 16 for h.264 or JPEG 4:2:x
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 * 3 / 2;
545 break;
546 case V4L2_PIX_FMT_YUYV:
547 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
548 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
549 f->fmt.pix.height;
550 break;
551 case V4L2_PIX_FMT_YUV422P:
552 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
553 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
554 f->fmt.pix.height * 2;
555 break;
556 case V4L2_PIX_FMT_JPEG:
557 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
558 /* fallthrough */
559 case V4L2_PIX_FMT_H264:
560 case V4L2_PIX_FMT_MPEG4:
561 case V4L2_PIX_FMT_MPEG2:
562 f->fmt.pix.bytesperline = 0;
563 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
564 f->fmt.pix.sizeimage,
565 f->fmt.pix.width,
566 f->fmt.pix.height);
567 break;
568 default:
569 BUG();
572 return 0;
575 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
576 struct v4l2_format *f)
578 struct coda_ctx *ctx = fh_to_ctx(priv);
579 const struct coda_q_data *q_data_src;
580 const struct coda_codec *codec;
581 struct vb2_queue *src_vq;
582 int ret;
583 bool use_vdoa;
585 ret = coda_try_pixelformat(ctx, f);
586 if (ret < 0)
587 return ret;
589 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
592 * If the source format is already fixed, only allow the same output
593 * resolution
595 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
596 if (vb2_is_streaming(src_vq)) {
597 f->fmt.pix.width = q_data_src->width;
598 f->fmt.pix.height = q_data_src->height;
601 f->fmt.pix.colorspace = ctx->colorspace;
602 f->fmt.pix.xfer_func = ctx->xfer_func;
603 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
604 f->fmt.pix.quantization = ctx->quantization;
606 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
607 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
608 f->fmt.pix.pixelformat);
609 if (!codec)
610 return -EINVAL;
612 ret = coda_try_fmt(ctx, codec, f);
613 if (ret < 0)
614 return ret;
616 /* The h.264 decoder only returns complete 16x16 macroblocks */
617 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
618 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
619 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
620 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
621 f->fmt.pix.height * 3 / 2;
623 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
624 if (ret < 0)
625 return ret;
627 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
628 if (!use_vdoa)
629 return -EINVAL;
631 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
632 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
633 f->fmt.pix.height;
637 return 0;
640 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
642 enum v4l2_colorspace colorspace;
644 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
645 colorspace = V4L2_COLORSPACE_JPEG;
646 else if (fmt->width <= 720 && fmt->height <= 576)
647 colorspace = V4L2_COLORSPACE_SMPTE170M;
648 else
649 colorspace = V4L2_COLORSPACE_REC709;
651 fmt->colorspace = colorspace;
652 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
653 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
654 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
657 static int coda_try_fmt_vid_out(struct file *file, void *priv,
658 struct v4l2_format *f)
660 struct coda_ctx *ctx = fh_to_ctx(priv);
661 struct coda_dev *dev = ctx->dev;
662 const struct coda_q_data *q_data_dst;
663 const struct coda_codec *codec;
664 int ret;
666 ret = coda_try_pixelformat(ctx, f);
667 if (ret < 0)
668 return ret;
670 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
671 coda_set_default_colorspace(&f->fmt.pix);
673 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
674 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
676 return coda_try_fmt(ctx, codec, f);
679 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
680 struct v4l2_rect *r)
682 struct coda_q_data *q_data;
683 struct vb2_queue *vq;
685 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
686 if (!vq)
687 return -EINVAL;
689 q_data = get_q_data(ctx, f->type);
690 if (!q_data)
691 return -EINVAL;
693 if (vb2_is_busy(vq)) {
694 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
695 return -EBUSY;
698 q_data->fourcc = f->fmt.pix.pixelformat;
699 q_data->width = f->fmt.pix.width;
700 q_data->height = f->fmt.pix.height;
701 q_data->bytesperline = f->fmt.pix.bytesperline;
702 q_data->sizeimage = f->fmt.pix.sizeimage;
703 if (r) {
704 q_data->rect = *r;
705 } else {
706 q_data->rect.left = 0;
707 q_data->rect.top = 0;
708 q_data->rect.width = f->fmt.pix.width;
709 q_data->rect.height = f->fmt.pix.height;
712 switch (f->fmt.pix.pixelformat) {
713 case V4L2_PIX_FMT_YUYV:
714 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
715 break;
716 case V4L2_PIX_FMT_NV12:
717 if (!disable_tiling) {
718 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
719 break;
721 /* else fall through */
722 case V4L2_PIX_FMT_YUV420:
723 case V4L2_PIX_FMT_YVU420:
724 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
725 break;
726 default:
727 break;
730 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
731 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
732 ctx->use_vdoa)
733 vdoa_context_configure(ctx->vdoa,
734 round_up(f->fmt.pix.width, 16),
735 f->fmt.pix.height,
736 f->fmt.pix.pixelformat);
737 else
738 ctx->use_vdoa = false;
740 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
741 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
742 f->type, q_data->width, q_data->height,
743 (char *)&q_data->fourcc,
744 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
746 return 0;
749 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
750 struct v4l2_format *f)
752 struct coda_ctx *ctx = fh_to_ctx(priv);
753 struct coda_q_data *q_data_src;
754 struct v4l2_rect r;
755 int ret;
757 ret = coda_try_fmt_vid_cap(file, priv, f);
758 if (ret)
759 return ret;
761 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
762 r.left = 0;
763 r.top = 0;
764 r.width = q_data_src->width;
765 r.height = q_data_src->height;
767 return coda_s_fmt(ctx, f, &r);
770 static int coda_s_fmt_vid_out(struct file *file, void *priv,
771 struct v4l2_format *f)
773 struct coda_ctx *ctx = fh_to_ctx(priv);
774 struct coda_q_data *q_data_src;
775 struct v4l2_format f_cap;
776 struct v4l2_rect r;
777 int ret;
779 ret = coda_try_fmt_vid_out(file, priv, f);
780 if (ret)
781 return ret;
783 ret = coda_s_fmt(ctx, f, NULL);
784 if (ret)
785 return ret;
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 memset(&f_cap, 0, sizeof(f_cap));
793 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
794 coda_g_fmt(file, priv, &f_cap);
795 f_cap.fmt.pix.width = f->fmt.pix.width;
796 f_cap.fmt.pix.height = f->fmt.pix.height;
798 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
799 if (ret)
800 return ret;
802 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
803 r.left = 0;
804 r.top = 0;
805 r.width = q_data_src->width;
806 r.height = q_data_src->height;
808 return coda_s_fmt(ctx, &f_cap, &r);
811 static int coda_reqbufs(struct file *file, void *priv,
812 struct v4l2_requestbuffers *rb)
814 struct coda_ctx *ctx = fh_to_ctx(priv);
815 int ret;
817 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
818 if (ret)
819 return ret;
822 * Allow to allocate instance specific per-context buffers, such as
823 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
825 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
826 return ctx->ops->reqbufs(ctx, rb);
828 return 0;
831 static int coda_qbuf(struct file *file, void *priv,
832 struct v4l2_buffer *buf)
834 struct coda_ctx *ctx = fh_to_ctx(priv);
836 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
839 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
840 struct vb2_v4l2_buffer *buf)
842 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
843 (buf->sequence == (ctx->qsequence - 1)));
846 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
847 enum vb2_buffer_state state)
849 const struct v4l2_event eos_event = {
850 .type = V4L2_EVENT_EOS
853 if (coda_buf_is_end_of_stream(ctx, buf)) {
854 buf->flags |= V4L2_BUF_FLAG_LAST;
856 v4l2_event_queue_fh(&ctx->fh, &eos_event);
859 v4l2_m2m_buf_done(buf, state);
862 static int coda_g_selection(struct file *file, void *fh,
863 struct v4l2_selection *s)
865 struct coda_ctx *ctx = fh_to_ctx(fh);
866 struct coda_q_data *q_data;
867 struct v4l2_rect r, *rsel;
869 q_data = get_q_data(ctx, s->type);
870 if (!q_data)
871 return -EINVAL;
873 r.left = 0;
874 r.top = 0;
875 r.width = q_data->width;
876 r.height = q_data->height;
877 rsel = &q_data->rect;
879 switch (s->target) {
880 case V4L2_SEL_TGT_CROP_DEFAULT:
881 case V4L2_SEL_TGT_CROP_BOUNDS:
882 rsel = &r;
883 /* fallthrough */
884 case V4L2_SEL_TGT_CROP:
885 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
886 return -EINVAL;
887 break;
888 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
889 case V4L2_SEL_TGT_COMPOSE_PADDED:
890 rsel = &r;
891 /* fallthrough */
892 case V4L2_SEL_TGT_COMPOSE:
893 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
894 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
895 return -EINVAL;
896 break;
897 default:
898 return -EINVAL;
901 s->r = *rsel;
903 return 0;
906 static int coda_try_encoder_cmd(struct file *file, void *fh,
907 struct v4l2_encoder_cmd *ec)
909 if (ec->cmd != V4L2_ENC_CMD_STOP)
910 return -EINVAL;
912 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
913 return -EINVAL;
915 return 0;
918 static int coda_encoder_cmd(struct file *file, void *fh,
919 struct v4l2_encoder_cmd *ec)
921 struct coda_ctx *ctx = fh_to_ctx(fh);
922 struct vb2_queue *dst_vq;
923 int ret;
925 ret = coda_try_encoder_cmd(file, fh, ec);
926 if (ret < 0)
927 return ret;
929 /* Ignore encoder stop command silently in decoder context */
930 if (ctx->inst_type != CODA_INST_ENCODER)
931 return 0;
933 /* Set the stream-end flag on this context */
934 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
936 /* If there is no buffer in flight, wake up */
937 if (!ctx->streamon_out || ctx->qsequence == ctx->osequence) {
938 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
939 V4L2_BUF_TYPE_VIDEO_CAPTURE);
940 dst_vq->last_buffer_dequeued = true;
941 wake_up(&dst_vq->done_wq);
944 return 0;
947 static int coda_try_decoder_cmd(struct file *file, void *fh,
948 struct v4l2_decoder_cmd *dc)
950 if (dc->cmd != V4L2_DEC_CMD_STOP)
951 return -EINVAL;
953 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
954 return -EINVAL;
956 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
957 return -EINVAL;
959 return 0;
962 static int coda_decoder_cmd(struct file *file, void *fh,
963 struct v4l2_decoder_cmd *dc)
965 struct coda_ctx *ctx = fh_to_ctx(fh);
966 int ret;
968 ret = coda_try_decoder_cmd(file, fh, dc);
969 if (ret < 0)
970 return ret;
972 /* Ignore decoder stop command silently in encoder context */
973 if (ctx->inst_type != CODA_INST_DECODER)
974 return 0;
976 /* Set the stream-end flag on this context */
977 coda_bit_stream_end_flag(ctx);
978 ctx->hold = false;
979 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
981 return 0;
984 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
986 struct coda_ctx *ctx = fh_to_ctx(fh);
987 struct v4l2_fract *tpf;
989 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
990 return -EINVAL;
992 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
993 tpf = &a->parm.output.timeperframe;
994 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
995 tpf->numerator = 1 + (ctx->params.framerate >>
996 CODA_FRATE_DIV_OFFSET);
998 return 0;
1002 * Approximate timeperframe v4l2_fract with values that can be written
1003 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1005 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1007 struct v4l2_fract s = *timeperframe;
1008 struct v4l2_fract f0;
1009 struct v4l2_fract f1 = { 1, 0 };
1010 struct v4l2_fract f2 = { 0, 1 };
1011 unsigned int i, div, s_denominator;
1013 /* Lower bound is 1/65535 */
1014 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1015 timeperframe->numerator = 1;
1016 timeperframe->denominator = 65535;
1017 return;
1020 /* Upper bound is 65536/1, map everything above to infinity */
1021 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1022 timeperframe->numerator = 1;
1023 timeperframe->denominator = 0;
1024 return;
1027 /* Reduce fraction to lowest terms */
1028 div = gcd(s.numerator, s.denominator);
1029 if (div > 1) {
1030 s.numerator /= div;
1031 s.denominator /= div;
1034 if (s.numerator <= 65536 && s.denominator < 65536) {
1035 *timeperframe = s;
1036 return;
1039 /* Find successive convergents from continued fraction expansion */
1040 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1041 f0 = f1;
1042 f1 = f2;
1044 /* Stop when f2 exactly equals timeperframe */
1045 if (s.numerator == 0)
1046 break;
1048 i = s.denominator / s.numerator;
1050 f2.numerator = f0.numerator + i * f1.numerator;
1051 f2.denominator = f0.denominator + i * f2.denominator;
1053 s_denominator = s.numerator;
1054 s.numerator = s.denominator % s.numerator;
1055 s.denominator = s_denominator;
1058 *timeperframe = f1;
1061 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1063 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1064 timeperframe->denominator;
1067 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1069 struct coda_ctx *ctx = fh_to_ctx(fh);
1070 struct v4l2_fract *tpf;
1072 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1073 return -EINVAL;
1075 tpf = &a->parm.output.timeperframe;
1076 coda_approximate_timeperframe(tpf);
1077 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1079 return 0;
1082 static int coda_subscribe_event(struct v4l2_fh *fh,
1083 const struct v4l2_event_subscription *sub)
1085 switch (sub->type) {
1086 case V4L2_EVENT_EOS:
1087 return v4l2_event_subscribe(fh, sub, 0, NULL);
1088 default:
1089 return v4l2_ctrl_subscribe_event(fh, sub);
1093 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1094 .vidioc_querycap = coda_querycap,
1096 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1097 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1098 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1099 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1101 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1102 .vidioc_g_fmt_vid_out = coda_g_fmt,
1103 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1104 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1106 .vidioc_reqbufs = coda_reqbufs,
1107 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1109 .vidioc_qbuf = coda_qbuf,
1110 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1111 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
1112 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1113 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1115 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1116 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1118 .vidioc_g_selection = coda_g_selection,
1120 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1121 .vidioc_encoder_cmd = coda_encoder_cmd,
1122 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
1123 .vidioc_decoder_cmd = coda_decoder_cmd,
1125 .vidioc_g_parm = coda_g_parm,
1126 .vidioc_s_parm = coda_s_parm,
1128 .vidioc_subscribe_event = coda_subscribe_event,
1129 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1133 * Mem-to-mem operations.
1136 static void coda_device_run(void *m2m_priv)
1138 struct coda_ctx *ctx = m2m_priv;
1139 struct coda_dev *dev = ctx->dev;
1141 queue_work(dev->workqueue, &ctx->pic_run_work);
1144 static void coda_pic_run_work(struct work_struct *work)
1146 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1147 struct coda_dev *dev = ctx->dev;
1148 int ret;
1150 mutex_lock(&ctx->buffer_mutex);
1151 mutex_lock(&dev->coda_mutex);
1153 ret = ctx->ops->prepare_run(ctx);
1154 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1155 mutex_unlock(&dev->coda_mutex);
1156 mutex_unlock(&ctx->buffer_mutex);
1157 /* job_finish scheduled by prepare_decode */
1158 return;
1161 if (!wait_for_completion_timeout(&ctx->completion,
1162 msecs_to_jiffies(1000))) {
1163 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1165 ctx->hold = true;
1167 coda_hw_reset(ctx);
1169 if (ctx->ops->run_timeout)
1170 ctx->ops->run_timeout(ctx);
1171 } else if (!ctx->aborting) {
1172 ctx->ops->finish_run(ctx);
1175 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1176 ctx->ops->seq_end_work)
1177 queue_work(dev->workqueue, &ctx->seq_end_work);
1179 mutex_unlock(&dev->coda_mutex);
1180 mutex_unlock(&ctx->buffer_mutex);
1182 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1185 static int coda_job_ready(void *m2m_priv)
1187 struct coda_ctx *ctx = m2m_priv;
1188 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1191 * For both 'P' and 'key' frame cases 1 picture
1192 * and 1 frame are needed. In the decoder case,
1193 * the compressed frame can be in the bitstream.
1195 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1196 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1197 "not ready: not enough video buffers.\n");
1198 return 0;
1201 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1202 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1203 "not ready: not enough video capture buffers.\n");
1204 return 0;
1207 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1208 bool stream_end = ctx->bit_stream_param &
1209 CODA_BIT_STREAM_END_FLAG;
1210 int num_metas = ctx->num_metas;
1211 unsigned int count;
1213 count = hweight32(ctx->frm_dis_flg);
1214 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1215 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1216 "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
1217 ctx->idx, count, ctx->num_internal_frames,
1218 ctx->frm_dis_flg);
1219 return 0;
1222 if (ctx->hold && !src_bufs) {
1223 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1224 "%d: not ready: on hold for more buffers.\n",
1225 ctx->idx);
1226 return 0;
1229 if (!stream_end && (num_metas + src_bufs) < 2) {
1230 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1231 "%d: not ready: need 2 buffers available (%d, %d)\n",
1232 ctx->idx, num_metas, src_bufs);
1233 return 0;
1237 if (!src_bufs && !stream_end &&
1238 (coda_get_bitstream_payload(ctx) < 512)) {
1239 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1240 "%d: not ready: not enough bitstream data (%d).\n",
1241 ctx->idx, coda_get_bitstream_payload(ctx));
1242 return 0;
1246 if (ctx->aborting) {
1247 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1248 "not ready: aborting\n");
1249 return 0;
1252 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1253 "job ready\n");
1255 return 1;
1258 static void coda_job_abort(void *priv)
1260 struct coda_ctx *ctx = priv;
1262 ctx->aborting = 1;
1264 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1265 "Aborting task\n");
1268 static void coda_lock(void *m2m_priv)
1270 struct coda_ctx *ctx = m2m_priv;
1271 struct coda_dev *pcdev = ctx->dev;
1273 mutex_lock(&pcdev->dev_mutex);
1276 static void coda_unlock(void *m2m_priv)
1278 struct coda_ctx *ctx = m2m_priv;
1279 struct coda_dev *pcdev = ctx->dev;
1281 mutex_unlock(&pcdev->dev_mutex);
1284 static const struct v4l2_m2m_ops coda_m2m_ops = {
1285 .device_run = coda_device_run,
1286 .job_ready = coda_job_ready,
1287 .job_abort = coda_job_abort,
1288 .lock = coda_lock,
1289 .unlock = coda_unlock,
1292 static void set_default_params(struct coda_ctx *ctx)
1294 unsigned int max_w, max_h, usize, csize;
1296 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1297 ctx->cvd->dst_formats[0]);
1298 max_w = min(ctx->codec->max_w, 1920U);
1299 max_h = min(ctx->codec->max_h, 1088U);
1300 usize = max_w * max_h * 3 / 2;
1301 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1303 ctx->params.codec_mode = ctx->codec->mode;
1304 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1305 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1306 else
1307 ctx->colorspace = V4L2_COLORSPACE_REC709;
1308 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1309 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1310 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1311 ctx->params.framerate = 30;
1313 /* Default formats for output and input queues */
1314 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1315 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1316 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1317 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1318 ctx->q_data[V4L2_M2M_DST].width = max_w;
1319 ctx->q_data[V4L2_M2M_DST].height = max_h;
1320 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1321 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1322 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1323 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1324 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1325 } else {
1326 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1327 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1328 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1329 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1331 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1332 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1333 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1334 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1337 * Since the RBC2AXI logic only supports a single chroma plane,
1338 * macroblock tiling only works for to NV12 pixel format.
1340 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1344 * Queue operations
1346 static int coda_queue_setup(struct vb2_queue *vq,
1347 unsigned int *nbuffers, unsigned int *nplanes,
1348 unsigned int sizes[], struct device *alloc_devs[])
1350 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1351 struct coda_q_data *q_data;
1352 unsigned int size;
1354 q_data = get_q_data(ctx, vq->type);
1355 size = q_data->sizeimage;
1357 *nplanes = 1;
1358 sizes[0] = size;
1360 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1361 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1363 return 0;
1366 static int coda_buf_prepare(struct vb2_buffer *vb)
1368 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1369 struct coda_q_data *q_data;
1371 q_data = get_q_data(ctx, vb->vb2_queue->type);
1373 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1374 v4l2_warn(&ctx->dev->v4l2_dev,
1375 "%s data will not fit into plane (%lu < %lu)\n",
1376 __func__, vb2_plane_size(vb, 0),
1377 (long)q_data->sizeimage);
1378 return -EINVAL;
1381 return 0;
1384 static void coda_buf_queue(struct vb2_buffer *vb)
1386 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1387 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1388 struct vb2_queue *vq = vb->vb2_queue;
1389 struct coda_q_data *q_data;
1391 q_data = get_q_data(ctx, vb->vb2_queue->type);
1394 * In the decoder case, immediately try to copy the buffer into the
1395 * bitstream ringbuffer and mark it as ready to be dequeued.
1397 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1399 * For backwards compatibility, queuing an empty buffer marks
1400 * the stream end
1402 if (vb2_get_plane_payload(vb, 0) == 0)
1403 coda_bit_stream_end_flag(ctx);
1405 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1407 * Unless already done, try to obtain profile_idc and
1408 * level_idc from the SPS header. This allows to decide
1409 * whether to enable reordering during sequence
1410 * initialization.
1412 if (!ctx->params.h264_profile_idc)
1413 coda_sps_parse_profile(ctx, vb);
1416 mutex_lock(&ctx->bitstream_mutex);
1417 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1418 if (vb2_is_streaming(vb->vb2_queue))
1419 /* This set buf->sequence = ctx->qsequence++ */
1420 coda_fill_bitstream(ctx, NULL);
1421 mutex_unlock(&ctx->bitstream_mutex);
1422 } else {
1423 if (ctx->inst_type == CODA_INST_ENCODER &&
1424 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1425 vbuf->sequence = ctx->qsequence++;
1426 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1430 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1431 size_t size, const char *name, struct dentry *parent)
1433 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1434 GFP_KERNEL);
1435 if (!buf->vaddr) {
1436 v4l2_err(&dev->v4l2_dev,
1437 "Failed to allocate %s buffer of size %zu\n",
1438 name, size);
1439 return -ENOMEM;
1442 buf->size = size;
1444 if (name && parent) {
1445 buf->blob.data = buf->vaddr;
1446 buf->blob.size = size;
1447 buf->dentry = debugfs_create_blob(name, 0644, parent,
1448 &buf->blob);
1449 if (!buf->dentry)
1450 dev_warn(&dev->plat_dev->dev,
1451 "failed to create debugfs entry %s\n", name);
1454 return 0;
1457 void coda_free_aux_buf(struct coda_dev *dev,
1458 struct coda_aux_buf *buf)
1460 if (buf->vaddr) {
1461 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1462 buf->vaddr, buf->paddr);
1463 buf->vaddr = NULL;
1464 buf->size = 0;
1465 debugfs_remove(buf->dentry);
1466 buf->dentry = NULL;
1470 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1472 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1473 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1474 struct coda_q_data *q_data_src, *q_data_dst;
1475 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1476 struct vb2_v4l2_buffer *buf;
1477 struct list_head list;
1478 int ret = 0;
1480 if (count < 1)
1481 return -EINVAL;
1483 INIT_LIST_HEAD(&list);
1485 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1486 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1487 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1488 /* copy the buffers that were queued before streamon */
1489 mutex_lock(&ctx->bitstream_mutex);
1490 coda_fill_bitstream(ctx, &list);
1491 mutex_unlock(&ctx->bitstream_mutex);
1493 if (coda_get_bitstream_payload(ctx) < 512) {
1494 ret = -EINVAL;
1495 goto err;
1499 ctx->streamon_out = 1;
1500 } else {
1501 ctx->streamon_cap = 1;
1504 /* Don't start the coda unless both queues are on */
1505 if (!(ctx->streamon_out && ctx->streamon_cap))
1506 goto out;
1508 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1509 if ((q_data_src->width != q_data_dst->width &&
1510 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1511 (q_data_src->height != q_data_dst->height &&
1512 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1513 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1514 q_data_src->width, q_data_src->height,
1515 q_data_dst->width, q_data_dst->height);
1516 ret = -EINVAL;
1517 goto err;
1520 /* Allow BIT decoder device_run with no new buffers queued */
1521 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1522 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1524 ctx->gopcounter = ctx->params.gop_size - 1;
1526 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1527 q_data_dst->fourcc);
1528 if (!ctx->codec) {
1529 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1530 ret = -EINVAL;
1531 goto err;
1534 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1535 ctx->params.gop_size = 1;
1536 ctx->gopcounter = ctx->params.gop_size - 1;
1538 ret = ctx->ops->start_streaming(ctx);
1539 if (ctx->inst_type == CODA_INST_DECODER) {
1540 if (ret == -EAGAIN)
1541 goto out;
1543 if (ret < 0)
1544 goto err;
1546 out:
1547 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1548 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1549 list_del(&m2m_buf->list);
1550 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
1553 return 0;
1555 err:
1556 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1557 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1558 list_del(&m2m_buf->list);
1559 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
1561 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1562 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1563 } else {
1564 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1565 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1567 return ret;
1570 static void coda_stop_streaming(struct vb2_queue *q)
1572 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1573 struct coda_dev *dev = ctx->dev;
1574 struct vb2_v4l2_buffer *buf;
1575 unsigned long flags;
1576 bool stop;
1578 stop = ctx->streamon_out && ctx->streamon_cap;
1580 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1581 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1582 "%s: output\n", __func__);
1583 ctx->streamon_out = 0;
1585 coda_bit_stream_end_flag(ctx);
1587 ctx->qsequence = 0;
1589 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1590 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1591 } else {
1592 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1593 "%s: capture\n", __func__);
1594 ctx->streamon_cap = 0;
1596 ctx->osequence = 0;
1597 ctx->sequence_offset = 0;
1599 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1600 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1603 if (stop) {
1604 struct coda_buffer_meta *meta;
1606 if (ctx->ops->seq_end_work) {
1607 queue_work(dev->workqueue, &ctx->seq_end_work);
1608 flush_work(&ctx->seq_end_work);
1610 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1611 while (!list_empty(&ctx->buffer_meta_list)) {
1612 meta = list_first_entry(&ctx->buffer_meta_list,
1613 struct coda_buffer_meta, list);
1614 list_del(&meta->list);
1615 kfree(meta);
1617 ctx->num_metas = 0;
1618 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1619 kfifo_init(&ctx->bitstream_fifo,
1620 ctx->bitstream.vaddr, ctx->bitstream.size);
1621 ctx->runcounter = 0;
1622 ctx->aborting = 0;
1625 if (!ctx->streamon_out && !ctx->streamon_cap)
1626 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1629 static const struct vb2_ops coda_qops = {
1630 .queue_setup = coda_queue_setup,
1631 .buf_prepare = coda_buf_prepare,
1632 .buf_queue = coda_buf_queue,
1633 .start_streaming = coda_start_streaming,
1634 .stop_streaming = coda_stop_streaming,
1635 .wait_prepare = vb2_ops_wait_prepare,
1636 .wait_finish = vb2_ops_wait_finish,
1639 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1641 struct coda_ctx *ctx =
1642 container_of(ctrl->handler, struct coda_ctx, ctrls);
1644 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1645 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1647 switch (ctrl->id) {
1648 case V4L2_CID_HFLIP:
1649 if (ctrl->val)
1650 ctx->params.rot_mode |= CODA_MIR_HOR;
1651 else
1652 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1653 break;
1654 case V4L2_CID_VFLIP:
1655 if (ctrl->val)
1656 ctx->params.rot_mode |= CODA_MIR_VER;
1657 else
1658 ctx->params.rot_mode &= ~CODA_MIR_VER;
1659 break;
1660 case V4L2_CID_MPEG_VIDEO_BITRATE:
1661 ctx->params.bitrate = ctrl->val / 1000;
1662 break;
1663 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1664 ctx->params.gop_size = ctrl->val;
1665 break;
1666 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1667 ctx->params.h264_intra_qp = ctrl->val;
1668 break;
1669 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1670 ctx->params.h264_inter_qp = ctrl->val;
1671 break;
1672 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1673 ctx->params.h264_min_qp = ctrl->val;
1674 break;
1675 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1676 ctx->params.h264_max_qp = ctrl->val;
1677 break;
1678 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1679 ctx->params.h264_deblk_alpha = ctrl->val;
1680 break;
1681 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1682 ctx->params.h264_deblk_beta = ctrl->val;
1683 break;
1684 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1685 ctx->params.h264_deblk_enabled = (ctrl->val ==
1686 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1687 break;
1688 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1689 /* TODO: switch between baseline and constrained baseline */
1690 ctx->params.h264_profile_idc = 66;
1691 break;
1692 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1693 /* nothing to do, this is set by the encoder */
1694 break;
1695 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1696 ctx->params.mpeg4_intra_qp = ctrl->val;
1697 break;
1698 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1699 ctx->params.mpeg4_inter_qp = ctrl->val;
1700 break;
1701 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1702 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1703 /* nothing to do, these are fixed */
1704 break;
1705 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1706 ctx->params.slice_mode = ctrl->val;
1707 break;
1708 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1709 ctx->params.slice_max_mb = ctrl->val;
1710 break;
1711 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1712 ctx->params.slice_max_bits = ctrl->val * 8;
1713 break;
1714 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1715 break;
1716 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1717 ctx->params.intra_refresh = ctrl->val;
1718 break;
1719 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1720 ctx->params.force_ipicture = true;
1721 break;
1722 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1723 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1724 break;
1725 case V4L2_CID_JPEG_RESTART_INTERVAL:
1726 ctx->params.jpeg_restart_interval = ctrl->val;
1727 break;
1728 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1729 ctx->params.vbv_delay = ctrl->val;
1730 break;
1731 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1732 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1733 break;
1734 default:
1735 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1736 "Invalid control, id=%d, val=%d\n",
1737 ctrl->id, ctrl->val);
1738 return -EINVAL;
1741 return 0;
1744 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1745 .s_ctrl = coda_s_ctrl,
1748 static void coda_encode_ctrls(struct coda_ctx *ctx)
1750 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
1752 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1753 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1754 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1755 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
1756 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1757 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1758 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1759 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1760 if (ctx->dev->devtype->product != CODA_960) {
1761 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1762 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1764 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1765 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1766 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1767 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1768 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1769 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1770 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1771 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1772 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1773 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1774 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1775 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1776 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
1777 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
1778 if (ctx->dev->devtype->product == CODA_7541) {
1779 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1780 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1781 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
1782 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1783 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1784 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
1785 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
1787 if (ctx->dev->devtype->product == CODA_960) {
1788 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1789 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1790 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
1791 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1792 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1793 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
1794 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
1795 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
1796 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1798 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1799 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1800 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1801 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1802 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1803 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
1804 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
1805 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
1806 if (ctx->dev->devtype->product == CODA_7541 ||
1807 ctx->dev->devtype->product == CODA_960) {
1808 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1809 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
1810 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
1811 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
1812 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
1814 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1815 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1816 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1817 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1818 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1819 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1820 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1821 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1822 500);
1823 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1824 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1825 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1826 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1827 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1828 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1829 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1830 1920 * 1088 / 256, 1, 0);
1831 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1832 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1834 * The maximum VBV size value is 0x7fffffff bits,
1835 * one bit less than 262144 KiB
1837 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1838 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
1841 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1843 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1844 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1845 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1846 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1849 static int coda_ctrls_setup(struct coda_ctx *ctx)
1851 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1853 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1854 V4L2_CID_HFLIP, 0, 1, 1, 0);
1855 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1856 V4L2_CID_VFLIP, 0, 1, 1, 0);
1857 if (ctx->inst_type == CODA_INST_ENCODER) {
1858 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1859 coda_jpeg_encode_ctrls(ctx);
1860 else
1861 coda_encode_ctrls(ctx);
1864 if (ctx->ctrls.error) {
1865 v4l2_err(&ctx->dev->v4l2_dev,
1866 "control initialization error (%d)",
1867 ctx->ctrls.error);
1868 return -EINVAL;
1871 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1874 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1876 vq->drv_priv = ctx;
1877 vq->ops = &coda_qops;
1878 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1879 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1880 vq->lock = &ctx->dev->dev_mutex;
1881 /* One way to indicate end-of-stream for coda is to set the
1882 * bytesused == 0. However by default videobuf2 handles bytesused
1883 * equal to 0 as a special case and changes its value to the size
1884 * of the buffer. Set the allow_zero_bytesused flag, so
1885 * that videobuf2 will keep the value of bytesused intact.
1887 vq->allow_zero_bytesused = 1;
1889 * We might be fine with no buffers on some of the queues, but that
1890 * would need to be reflected in job_ready(). Currently we expect all
1891 * queues to have at least one buffer queued.
1893 vq->min_buffers_needed = 1;
1894 vq->dev = &ctx->dev->plat_dev->dev;
1896 return vb2_queue_init(vq);
1899 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1900 struct vb2_queue *dst_vq)
1902 int ret;
1904 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1905 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1906 src_vq->mem_ops = &vb2_dma_contig_memops;
1908 ret = coda_queue_init(priv, src_vq);
1909 if (ret)
1910 return ret;
1912 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1913 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1914 dst_vq->mem_ops = &vb2_dma_contig_memops;
1916 return coda_queue_init(priv, dst_vq);
1919 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1920 struct vb2_queue *dst_vq)
1922 int ret;
1924 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1925 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1926 src_vq->mem_ops = &vb2_vmalloc_memops;
1928 ret = coda_queue_init(priv, src_vq);
1929 if (ret)
1930 return ret;
1932 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1933 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1934 dst_vq->mem_ops = &vb2_dma_contig_memops;
1936 return coda_queue_init(priv, dst_vq);
1939 static int coda_next_free_instance(struct coda_dev *dev)
1941 int idx = ffz(dev->instance_mask);
1943 if ((idx < 0) ||
1944 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1945 return -EBUSY;
1947 return idx;
1951 * File operations
1954 static int coda_open(struct file *file)
1956 struct video_device *vdev = video_devdata(file);
1957 struct coda_dev *dev = video_get_drvdata(vdev);
1958 struct coda_ctx *ctx = NULL;
1959 char *name;
1960 int ret;
1961 int idx;
1963 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1964 if (!ctx)
1965 return -ENOMEM;
1967 idx = coda_next_free_instance(dev);
1968 if (idx < 0) {
1969 ret = idx;
1970 goto err_coda_max;
1972 set_bit(idx, &dev->instance_mask);
1974 name = kasprintf(GFP_KERNEL, "context%d", idx);
1975 if (!name) {
1976 ret = -ENOMEM;
1977 goto err_coda_name_init;
1980 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1981 kfree(name);
1983 ctx->cvd = to_coda_video_device(vdev);
1984 ctx->inst_type = ctx->cvd->type;
1985 ctx->ops = ctx->cvd->ops;
1986 ctx->use_bit = !ctx->cvd->direct;
1987 init_completion(&ctx->completion);
1988 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
1989 if (ctx->ops->seq_end_work)
1990 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1991 v4l2_fh_init(&ctx->fh, video_devdata(file));
1992 file->private_data = &ctx->fh;
1993 v4l2_fh_add(&ctx->fh);
1994 ctx->dev = dev;
1995 ctx->idx = idx;
1996 switch (dev->devtype->product) {
1997 case CODA_960:
1999 * Enabling the BWB when decoding can hang the firmware with
2000 * certain streams. The issue was tracked as ENGR00293425 by
2001 * Freescale. As a workaround, disable BWB for all decoders.
2002 * The enable_bwb module parameter allows to override this.
2004 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2005 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2006 /* fallthrough */
2007 case CODA_7541:
2008 ctx->reg_idx = 0;
2009 break;
2010 default:
2011 ctx->reg_idx = idx;
2013 if (ctx->dev->vdoa && !disable_vdoa) {
2014 ctx->vdoa = vdoa_context_create(dev->vdoa);
2015 if (!ctx->vdoa)
2016 v4l2_warn(&dev->v4l2_dev,
2017 "Failed to create vdoa context: not using vdoa");
2019 ctx->use_vdoa = false;
2021 /* Power up and upload firmware if necessary */
2022 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2023 if (ret < 0) {
2024 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2025 goto err_pm_get;
2028 ret = clk_prepare_enable(dev->clk_per);
2029 if (ret)
2030 goto err_clk_per;
2032 ret = clk_prepare_enable(dev->clk_ahb);
2033 if (ret)
2034 goto err_clk_ahb;
2036 set_default_params(ctx);
2037 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2038 ctx->ops->queue_init);
2039 if (IS_ERR(ctx->fh.m2m_ctx)) {
2040 ret = PTR_ERR(ctx->fh.m2m_ctx);
2042 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2043 __func__, ret);
2044 goto err_ctx_init;
2047 ret = coda_ctrls_setup(ctx);
2048 if (ret) {
2049 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2050 goto err_ctrls_setup;
2053 ctx->fh.ctrl_handler = &ctx->ctrls;
2055 mutex_init(&ctx->bitstream_mutex);
2056 mutex_init(&ctx->buffer_mutex);
2057 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2058 spin_lock_init(&ctx->buffer_meta_lock);
2060 coda_lock(ctx);
2061 list_add(&ctx->list, &dev->instances);
2062 coda_unlock(ctx);
2064 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2065 ctx->idx, ctx);
2067 return 0;
2069 err_ctrls_setup:
2070 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2071 err_ctx_init:
2072 clk_disable_unprepare(dev->clk_ahb);
2073 err_clk_ahb:
2074 clk_disable_unprepare(dev->clk_per);
2075 err_clk_per:
2076 pm_runtime_put_sync(&dev->plat_dev->dev);
2077 err_pm_get:
2078 v4l2_fh_del(&ctx->fh);
2079 v4l2_fh_exit(&ctx->fh);
2080 clear_bit(ctx->idx, &dev->instance_mask);
2081 err_coda_name_init:
2082 err_coda_max:
2083 kfree(ctx);
2084 return ret;
2087 static int coda_release(struct file *file)
2089 struct coda_dev *dev = video_drvdata(file);
2090 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2092 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2093 ctx);
2095 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2096 coda_bit_stream_end_flag(ctx);
2098 /* If this instance is running, call .job_abort and wait for it to end */
2099 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2101 if (ctx->vdoa)
2102 vdoa_context_destroy(ctx->vdoa);
2104 /* In case the instance was not running, we still need to call SEQ_END */
2105 if (ctx->ops->seq_end_work) {
2106 queue_work(dev->workqueue, &ctx->seq_end_work);
2107 flush_work(&ctx->seq_end_work);
2110 coda_lock(ctx);
2111 list_del(&ctx->list);
2112 coda_unlock(ctx);
2114 if (ctx->dev->devtype->product == CODA_DX6)
2115 coda_free_aux_buf(dev, &ctx->workbuf);
2117 v4l2_ctrl_handler_free(&ctx->ctrls);
2118 clk_disable_unprepare(dev->clk_ahb);
2119 clk_disable_unprepare(dev->clk_per);
2120 pm_runtime_put_sync(&dev->plat_dev->dev);
2121 v4l2_fh_del(&ctx->fh);
2122 v4l2_fh_exit(&ctx->fh);
2123 clear_bit(ctx->idx, &dev->instance_mask);
2124 if (ctx->ops->release)
2125 ctx->ops->release(ctx);
2126 debugfs_remove_recursive(ctx->debugfs_entry);
2127 kfree(ctx);
2129 return 0;
2132 static const struct v4l2_file_operations coda_fops = {
2133 .owner = THIS_MODULE,
2134 .open = coda_open,
2135 .release = coda_release,
2136 .poll = v4l2_m2m_fop_poll,
2137 .unlocked_ioctl = video_ioctl2,
2138 .mmap = v4l2_m2m_fop_mmap,
2141 static int coda_hw_init(struct coda_dev *dev)
2143 u32 data;
2144 u16 *p;
2145 int i, ret;
2147 ret = clk_prepare_enable(dev->clk_per);
2148 if (ret)
2149 goto err_clk_per;
2151 ret = clk_prepare_enable(dev->clk_ahb);
2152 if (ret)
2153 goto err_clk_ahb;
2155 reset_control_reset(dev->rstc);
2158 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2159 * The 16-bit chars in the code buffer are in memory access
2160 * order, re-sort them to CODA order for register download.
2161 * Data in this SRAM survives a reboot.
2163 p = (u16 *)dev->codebuf.vaddr;
2164 if (dev->devtype->product == CODA_DX6) {
2165 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2166 data = CODA_DOWN_ADDRESS_SET(i) |
2167 CODA_DOWN_DATA_SET(p[i ^ 1]);
2168 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2170 } else {
2171 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2172 data = CODA_DOWN_ADDRESS_SET(i) |
2173 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2174 3 - (i % 4)]);
2175 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2179 /* Clear registers */
2180 for (i = 0; i < 64; i++)
2181 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2183 /* Tell the BIT where to find everything it needs */
2184 if (dev->devtype->product == CODA_960 ||
2185 dev->devtype->product == CODA_7541) {
2186 coda_write(dev, dev->tempbuf.paddr,
2187 CODA_REG_BIT_TEMP_BUF_ADDR);
2188 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2189 } else {
2190 coda_write(dev, dev->workbuf.paddr,
2191 CODA_REG_BIT_WORK_BUF_ADDR);
2193 coda_write(dev, dev->codebuf.paddr,
2194 CODA_REG_BIT_CODE_BUF_ADDR);
2195 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2197 /* Set default values */
2198 switch (dev->devtype->product) {
2199 case CODA_DX6:
2200 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2201 CODA_REG_BIT_STREAM_CTRL);
2202 break;
2203 default:
2204 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2205 CODA_REG_BIT_STREAM_CTRL);
2207 if (dev->devtype->product == CODA_960)
2208 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2209 CODA_REG_BIT_FRAME_MEM_CTRL);
2210 else
2211 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2213 if (dev->devtype->product != CODA_DX6)
2214 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2216 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2217 CODA_REG_BIT_INT_ENABLE);
2219 /* Reset VPU and start processor */
2220 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2221 data |= CODA_REG_RESET_ENABLE;
2222 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2223 udelay(10);
2224 data &= ~CODA_REG_RESET_ENABLE;
2225 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2226 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2228 clk_disable_unprepare(dev->clk_ahb);
2229 clk_disable_unprepare(dev->clk_per);
2231 return 0;
2233 err_clk_ahb:
2234 clk_disable_unprepare(dev->clk_per);
2235 err_clk_per:
2236 return ret;
2239 static int coda_register_device(struct coda_dev *dev, int i)
2241 struct video_device *vfd = &dev->vfd[i];
2243 if (i >= dev->devtype->num_vdevs)
2244 return -EINVAL;
2246 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2247 vfd->fops = &coda_fops;
2248 vfd->ioctl_ops = &coda_ioctl_ops;
2249 vfd->release = video_device_release_empty,
2250 vfd->lock = &dev->dev_mutex;
2251 vfd->v4l2_dev = &dev->v4l2_dev;
2252 vfd->vfl_dir = VFL_DIR_M2M;
2253 video_set_drvdata(vfd, dev);
2255 /* Not applicable, use the selection API instead */
2256 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2257 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2258 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2260 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2263 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2264 size_t size)
2266 u32 *src = (u32 *)buf;
2268 /* Check if the firmware has a 16-byte Freescale header, skip it */
2269 if (buf[0] == 'M' && buf[1] == 'X')
2270 src += 4;
2272 * Check whether the firmware is in native order or pre-reordered for
2273 * memory access. The first instruction opcode always is 0xe40e.
2275 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2276 u32 *dst = dev->codebuf.vaddr;
2277 int i;
2279 /* Firmware in native order, reorder while copying */
2280 if (dev->devtype->product == CODA_DX6) {
2281 for (i = 0; i < (size - 16) / 4; i++)
2282 dst[i] = (src[i] << 16) | (src[i] >> 16);
2283 } else {
2284 for (i = 0; i < (size - 16) / 4; i += 2) {
2285 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2286 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2289 } else {
2290 /* Copy the already reordered firmware image */
2291 memcpy(dev->codebuf.vaddr, src, size);
2295 static void coda_fw_callback(const struct firmware *fw, void *context);
2297 static int coda_firmware_request(struct coda_dev *dev)
2299 char *fw;
2301 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2302 return -EINVAL;
2304 fw = dev->devtype->firmware[dev->firmware];
2306 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2307 coda_product_name(dev->devtype->product));
2309 return request_firmware_nowait(THIS_MODULE, true, fw,
2310 &dev->plat_dev->dev, GFP_KERNEL, dev,
2311 coda_fw_callback);
2314 static void coda_fw_callback(const struct firmware *fw, void *context)
2316 struct coda_dev *dev = context;
2317 struct platform_device *pdev = dev->plat_dev;
2318 int i, ret;
2320 if (!fw) {
2321 dev->firmware++;
2322 ret = coda_firmware_request(dev);
2323 if (ret < 0) {
2324 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2325 goto put_pm;
2327 return;
2329 if (dev->firmware > 0) {
2331 * Since we can't suppress warnings for failed asynchronous
2332 * firmware requests, report that the fallback firmware was
2333 * found.
2335 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2336 dev->devtype->firmware[dev->firmware]);
2339 /* allocate auxiliary per-device code buffer for the BIT processor */
2340 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2341 dev->debugfs_root);
2342 if (ret < 0)
2343 goto put_pm;
2345 coda_copy_firmware(dev, fw->data, fw->size);
2346 release_firmware(fw);
2348 ret = coda_hw_init(dev);
2349 if (ret < 0) {
2350 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2351 goto put_pm;
2354 ret = coda_check_firmware(dev);
2355 if (ret < 0)
2356 goto put_pm;
2358 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2359 if (IS_ERR(dev->m2m_dev)) {
2360 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2361 goto put_pm;
2364 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2365 ret = coda_register_device(dev, i);
2366 if (ret) {
2367 v4l2_err(&dev->v4l2_dev,
2368 "Failed to register %s video device: %d\n",
2369 dev->devtype->vdevs[i]->name, ret);
2370 goto rel_vfd;
2374 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
2375 dev->vfd[0].num, dev->vfd[i - 1].num);
2377 pm_runtime_put_sync(&pdev->dev);
2378 return;
2380 rel_vfd:
2381 while (--i >= 0)
2382 video_unregister_device(&dev->vfd[i]);
2383 v4l2_m2m_release(dev->m2m_dev);
2384 put_pm:
2385 pm_runtime_put_sync(&pdev->dev);
2388 enum coda_platform {
2389 CODA_IMX27,
2390 CODA_IMX53,
2391 CODA_IMX6Q,
2392 CODA_IMX6DL,
2395 static const struct coda_devtype coda_devdata[] = {
2396 [CODA_IMX27] = {
2397 .firmware = {
2398 "vpu_fw_imx27_TO2.bin",
2399 "vpu/vpu_fw_imx27_TO2.bin",
2400 "v4l-codadx6-imx27.bin"
2402 .product = CODA_DX6,
2403 .codecs = codadx6_codecs,
2404 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2405 .vdevs = codadx6_video_devices,
2406 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2407 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2408 .iram_size = 0xb000,
2410 [CODA_IMX53] = {
2411 .firmware = {
2412 "vpu_fw_imx53.bin",
2413 "vpu/vpu_fw_imx53.bin",
2414 "v4l-coda7541-imx53.bin"
2416 .product = CODA_7541,
2417 .codecs = coda7_codecs,
2418 .num_codecs = ARRAY_SIZE(coda7_codecs),
2419 .vdevs = coda7_video_devices,
2420 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2421 .workbuf_size = 128 * 1024,
2422 .tempbuf_size = 304 * 1024,
2423 .iram_size = 0x14000,
2425 [CODA_IMX6Q] = {
2426 .firmware = {
2427 "vpu_fw_imx6q.bin",
2428 "vpu/vpu_fw_imx6q.bin",
2429 "v4l-coda960-imx6q.bin"
2431 .product = CODA_960,
2432 .codecs = coda9_codecs,
2433 .num_codecs = ARRAY_SIZE(coda9_codecs),
2434 .vdevs = coda9_video_devices,
2435 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2436 .workbuf_size = 80 * 1024,
2437 .tempbuf_size = 204 * 1024,
2438 .iram_size = 0x21000,
2440 [CODA_IMX6DL] = {
2441 .firmware = {
2442 "vpu_fw_imx6d.bin",
2443 "vpu/vpu_fw_imx6d.bin",
2444 "v4l-coda960-imx6dl.bin"
2446 .product = CODA_960,
2447 .codecs = coda9_codecs,
2448 .num_codecs = ARRAY_SIZE(coda9_codecs),
2449 .vdevs = coda9_video_devices,
2450 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2451 .workbuf_size = 80 * 1024,
2452 .tempbuf_size = 204 * 1024,
2453 .iram_size = 0x1f000, /* leave 4k for suspend code */
2457 static const struct platform_device_id coda_platform_ids[] = {
2458 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2459 { /* sentinel */ }
2461 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2463 #ifdef CONFIG_OF
2464 static const struct of_device_id coda_dt_ids[] = {
2465 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2466 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2467 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2468 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2469 { /* sentinel */ }
2471 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2472 #endif
2474 static int coda_probe(struct platform_device *pdev)
2476 const struct of_device_id *of_id =
2477 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2478 const struct platform_device_id *pdev_id;
2479 struct coda_platform_data *pdata = pdev->dev.platform_data;
2480 struct device_node *np = pdev->dev.of_node;
2481 struct gen_pool *pool;
2482 struct coda_dev *dev;
2483 struct resource *res;
2484 int ret, irq;
2486 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2487 if (!dev)
2488 return -ENOMEM;
2490 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2492 if (of_id)
2493 dev->devtype = of_id->data;
2494 else if (pdev_id)
2495 dev->devtype = &coda_devdata[pdev_id->driver_data];
2496 else
2497 return -EINVAL;
2499 spin_lock_init(&dev->irqlock);
2500 INIT_LIST_HEAD(&dev->instances);
2502 dev->plat_dev = pdev;
2503 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2504 if (IS_ERR(dev->clk_per)) {
2505 dev_err(&pdev->dev, "Could not get per clock\n");
2506 return PTR_ERR(dev->clk_per);
2509 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2510 if (IS_ERR(dev->clk_ahb)) {
2511 dev_err(&pdev->dev, "Could not get ahb clock\n");
2512 return PTR_ERR(dev->clk_ahb);
2515 /* Get memory for physical registers */
2516 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2517 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2518 if (IS_ERR(dev->regs_base))
2519 return PTR_ERR(dev->regs_base);
2521 /* IRQ */
2522 irq = platform_get_irq_byname(pdev, "bit");
2523 if (irq < 0)
2524 irq = platform_get_irq(pdev, 0);
2525 if (irq < 0) {
2526 dev_err(&pdev->dev, "failed to get irq resource\n");
2527 return irq;
2530 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2531 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2532 if (ret < 0) {
2533 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2534 return ret;
2537 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
2538 NULL);
2539 if (IS_ERR(dev->rstc)) {
2540 ret = PTR_ERR(dev->rstc);
2541 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
2542 return ret;
2545 /* Get IRAM pool from device tree or platform data */
2546 pool = of_gen_pool_get(np, "iram", 0);
2547 if (!pool && pdata)
2548 pool = gen_pool_get(pdata->iram_dev, NULL);
2549 if (!pool) {
2550 dev_err(&pdev->dev, "iram pool not available\n");
2551 return -ENOMEM;
2553 dev->iram_pool = pool;
2555 /* Get vdoa_data if supported by the platform */
2556 dev->vdoa = coda_get_vdoa_data();
2557 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
2558 return -EPROBE_DEFER;
2560 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2561 if (ret)
2562 return ret;
2564 mutex_init(&dev->dev_mutex);
2565 mutex_init(&dev->coda_mutex);
2567 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2568 if (!dev->debugfs_root)
2569 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2571 /* allocate auxiliary per-device buffers for the BIT processor */
2572 if (dev->devtype->product == CODA_DX6) {
2573 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2574 dev->devtype->workbuf_size, "workbuf",
2575 dev->debugfs_root);
2576 if (ret < 0)
2577 goto err_v4l2_register;
2580 if (dev->devtype->tempbuf_size) {
2581 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2582 dev->devtype->tempbuf_size, "tempbuf",
2583 dev->debugfs_root);
2584 if (ret < 0)
2585 goto err_v4l2_register;
2588 dev->iram.size = dev->devtype->iram_size;
2589 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2590 &dev->iram.paddr);
2591 if (!dev->iram.vaddr) {
2592 dev_warn(&pdev->dev, "unable to alloc iram\n");
2593 } else {
2594 memset(dev->iram.vaddr, 0, dev->iram.size);
2595 dev->iram.blob.data = dev->iram.vaddr;
2596 dev->iram.blob.size = dev->iram.size;
2597 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2598 dev->debugfs_root,
2599 &dev->iram.blob);
2602 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2603 if (!dev->workqueue) {
2604 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2605 ret = -ENOMEM;
2606 goto err_v4l2_register;
2609 platform_set_drvdata(pdev, dev);
2612 * Start activated so we can directly call coda_hw_init in
2613 * coda_fw_callback regardless of whether CONFIG_PM is
2614 * enabled or whether the device is associated with a PM domain.
2616 pm_runtime_get_noresume(&pdev->dev);
2617 pm_runtime_set_active(&pdev->dev);
2618 pm_runtime_enable(&pdev->dev);
2620 ret = coda_firmware_request(dev);
2621 if (ret)
2622 goto err_alloc_workqueue;
2623 return 0;
2625 err_alloc_workqueue:
2626 destroy_workqueue(dev->workqueue);
2627 err_v4l2_register:
2628 v4l2_device_unregister(&dev->v4l2_dev);
2629 return ret;
2632 static int coda_remove(struct platform_device *pdev)
2634 struct coda_dev *dev = platform_get_drvdata(pdev);
2635 int i;
2637 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2638 if (video_get_drvdata(&dev->vfd[i]))
2639 video_unregister_device(&dev->vfd[i]);
2641 if (dev->m2m_dev)
2642 v4l2_m2m_release(dev->m2m_dev);
2643 pm_runtime_disable(&pdev->dev);
2644 v4l2_device_unregister(&dev->v4l2_dev);
2645 destroy_workqueue(dev->workqueue);
2646 if (dev->iram.vaddr)
2647 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2648 dev->iram.size);
2649 coda_free_aux_buf(dev, &dev->codebuf);
2650 coda_free_aux_buf(dev, &dev->tempbuf);
2651 coda_free_aux_buf(dev, &dev->workbuf);
2652 debugfs_remove_recursive(dev->debugfs_root);
2653 return 0;
2656 #ifdef CONFIG_PM
2657 static int coda_runtime_resume(struct device *dev)
2659 struct coda_dev *cdev = dev_get_drvdata(dev);
2660 int ret = 0;
2662 if (dev->pm_domain && cdev->codebuf.vaddr) {
2663 ret = coda_hw_init(cdev);
2664 if (ret)
2665 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2668 return ret;
2670 #endif
2672 static const struct dev_pm_ops coda_pm_ops = {
2673 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2676 static struct platform_driver coda_driver = {
2677 .probe = coda_probe,
2678 .remove = coda_remove,
2679 .driver = {
2680 .name = CODA_NAME,
2681 .of_match_table = of_match_ptr(coda_dt_ids),
2682 .pm = &coda_pm_ops,
2684 .id_table = coda_platform_ids,
2687 module_platform_driver(coda_driver);
2689 MODULE_LICENSE("GPL");
2690 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2691 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");