Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / platform / coda / coda-common.c
blobfccc771d23a51797f8b6158d6993d6c29ebead4d
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/idr.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/irq.h>
24 #include <linux/kfifo.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/slab.h>
30 #include <linux/videodev2.h>
31 #include <linux/of.h>
32 #include <linux/platform_data/media/coda.h>
33 #include <linux/reset.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-event.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-mem2mem.h>
40 #include <media/videobuf2-v4l2.h>
41 #include <media/videobuf2-dma-contig.h>
42 #include <media/videobuf2-vmalloc.h>
44 #include "coda.h"
45 #include "imx-vdoa.h"
47 #define CODA_NAME "coda"
49 #define CODADX6_MAX_INSTANCES 4
50 #define CODA_MAX_FORMATS 4
52 #define CODA_ISRAM_SIZE (2048 * 2)
54 #define MIN_W 176
55 #define MIN_H 144
57 #define S_ALIGN 1 /* multiple of 2 */
58 #define W_ALIGN 1 /* multiple of 2 */
59 #define H_ALIGN 1 /* multiple of 2 */
61 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
63 int coda_debug;
64 module_param(coda_debug, int, 0644);
65 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
67 static int disable_tiling;
68 module_param(disable_tiling, int, 0644);
69 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
71 static int disable_vdoa;
72 module_param(disable_vdoa, int, 0644);
73 MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
75 static int enable_bwb = 0;
76 module_param(enable_bwb, int, 0644);
77 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
79 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
81 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
82 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
83 writel(data, dev->regs_base + reg);
86 unsigned int coda_read(struct coda_dev *dev, u32 reg)
88 u32 data;
90 data = readl(dev->regs_base + reg);
91 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
92 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
93 return data;
96 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
97 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
99 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
100 u32 base_cb, base_cr;
102 switch (q_data->fourcc) {
103 case V4L2_PIX_FMT_YUYV:
104 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
105 case V4L2_PIX_FMT_NV12:
106 case V4L2_PIX_FMT_YUV420:
107 default:
108 base_cb = base_y + q_data->bytesperline * q_data->height;
109 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
110 break;
111 case V4L2_PIX_FMT_YVU420:
112 /* Switch Cb and Cr for YVU420 format */
113 base_cr = base_y + q_data->bytesperline * q_data->height;
114 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
115 break;
116 case V4L2_PIX_FMT_YUV422P:
117 base_cb = base_y + q_data->bytesperline * q_data->height;
118 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
121 coda_write(ctx->dev, base_y, reg_y);
122 coda_write(ctx->dev, base_cb, reg_y + 4);
123 coda_write(ctx->dev, base_cr, reg_y + 8);
126 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
127 { mode, src_fourcc, dst_fourcc, max_w, max_h }
130 * Arrays of codecs supported by each given version of Coda:
131 * i.MX27 -> codadx6
132 * i.MX51 -> codahx4
133 * i.MX53 -> coda7
134 * i.MX6 -> coda960
135 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
137 static const struct coda_codec codadx6_codecs[] = {
138 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
139 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
142 static const struct coda_codec codahx4_codecs[] = {
143 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
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, 1280, 720),
149 static const struct coda_codec coda7_codecs[] = {
150 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
151 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
152 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
153 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
154 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
155 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
156 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
159 static const struct coda_codec coda9_codecs[] = {
160 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
161 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
162 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
163 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
164 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
167 struct coda_video_device {
168 const char *name;
169 enum coda_inst_type type;
170 const struct coda_context_ops *ops;
171 bool direct;
172 u32 src_formats[CODA_MAX_FORMATS];
173 u32 dst_formats[CODA_MAX_FORMATS];
176 static const struct coda_video_device coda_bit_encoder = {
177 .name = "coda-encoder",
178 .type = CODA_INST_ENCODER,
179 .ops = &coda_bit_encode_ops,
180 .src_formats = {
181 V4L2_PIX_FMT_NV12,
182 V4L2_PIX_FMT_YUV420,
183 V4L2_PIX_FMT_YVU420,
185 .dst_formats = {
186 V4L2_PIX_FMT_H264,
187 V4L2_PIX_FMT_MPEG4,
191 static const struct coda_video_device coda_bit_jpeg_encoder = {
192 .name = "coda-jpeg-encoder",
193 .type = CODA_INST_ENCODER,
194 .ops = &coda_bit_encode_ops,
195 .src_formats = {
196 V4L2_PIX_FMT_NV12,
197 V4L2_PIX_FMT_YUV420,
198 V4L2_PIX_FMT_YVU420,
199 V4L2_PIX_FMT_YUV422P,
201 .dst_formats = {
202 V4L2_PIX_FMT_JPEG,
206 static const struct coda_video_device coda_bit_decoder = {
207 .name = "coda-decoder",
208 .type = CODA_INST_DECODER,
209 .ops = &coda_bit_decode_ops,
210 .src_formats = {
211 V4L2_PIX_FMT_H264,
212 V4L2_PIX_FMT_MPEG2,
213 V4L2_PIX_FMT_MPEG4,
215 .dst_formats = {
216 V4L2_PIX_FMT_NV12,
217 V4L2_PIX_FMT_YUV420,
218 V4L2_PIX_FMT_YVU420,
220 * If V4L2_PIX_FMT_YUYV should be default,
221 * set_default_params() must be adjusted.
223 V4L2_PIX_FMT_YUYV,
227 static const struct coda_video_device coda_bit_jpeg_decoder = {
228 .name = "coda-jpeg-decoder",
229 .type = CODA_INST_DECODER,
230 .ops = &coda_bit_decode_ops,
231 .src_formats = {
232 V4L2_PIX_FMT_JPEG,
234 .dst_formats = {
235 V4L2_PIX_FMT_NV12,
236 V4L2_PIX_FMT_YUV420,
237 V4L2_PIX_FMT_YVU420,
238 V4L2_PIX_FMT_YUV422P,
242 static const struct coda_video_device *codadx6_video_devices[] = {
243 &coda_bit_encoder,
246 static const struct coda_video_device *codahx4_video_devices[] = {
247 &coda_bit_encoder,
248 &coda_bit_decoder,
251 static const struct coda_video_device *coda7_video_devices[] = {
252 &coda_bit_jpeg_encoder,
253 &coda_bit_jpeg_decoder,
254 &coda_bit_encoder,
255 &coda_bit_decoder,
258 static const struct coda_video_device *coda9_video_devices[] = {
259 &coda_bit_encoder,
260 &coda_bit_decoder,
264 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
265 * tables.
267 static u32 coda_format_normalize_yuv(u32 fourcc)
269 switch (fourcc) {
270 case V4L2_PIX_FMT_NV12:
271 case V4L2_PIX_FMT_YUV420:
272 case V4L2_PIX_FMT_YVU420:
273 case V4L2_PIX_FMT_YUV422P:
274 case V4L2_PIX_FMT_YUYV:
275 return V4L2_PIX_FMT_YUV420;
276 default:
277 return fourcc;
281 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
282 int src_fourcc, int dst_fourcc)
284 const struct coda_codec *codecs = dev->devtype->codecs;
285 int num_codecs = dev->devtype->num_codecs;
286 int k;
288 src_fourcc = coda_format_normalize_yuv(src_fourcc);
289 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
290 if (src_fourcc == dst_fourcc)
291 return NULL;
293 for (k = 0; k < num_codecs; k++) {
294 if (codecs[k].src_fourcc == src_fourcc &&
295 codecs[k].dst_fourcc == dst_fourcc)
296 break;
299 if (k == num_codecs)
300 return NULL;
302 return &codecs[k];
305 static void coda_get_max_dimensions(struct coda_dev *dev,
306 const struct coda_codec *codec,
307 int *max_w, int *max_h)
309 const struct coda_codec *codecs = dev->devtype->codecs;
310 int num_codecs = dev->devtype->num_codecs;
311 unsigned int w, h;
312 int k;
314 if (codec) {
315 w = codec->max_w;
316 h = codec->max_h;
317 } else {
318 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
319 w = max(w, codecs[k].max_w);
320 h = max(h, codecs[k].max_h);
324 if (max_w)
325 *max_w = w;
326 if (max_h)
327 *max_h = h;
330 static const struct coda_video_device *to_coda_video_device(struct video_device
331 *vdev)
333 struct coda_dev *dev = video_get_drvdata(vdev);
334 unsigned int i = vdev - dev->vfd;
336 if (i >= dev->devtype->num_vdevs)
337 return NULL;
339 return dev->devtype->vdevs[i];
342 const char *coda_product_name(int product)
344 static char buf[9];
346 switch (product) {
347 case CODA_DX6:
348 return "CodaDx6";
349 case CODA_HX4:
350 return "CodaHx4";
351 case CODA_7541:
352 return "CODA7541";
353 case CODA_960:
354 return "CODA960";
355 default:
356 snprintf(buf, sizeof(buf), "(0x%04x)", product);
357 return buf;
361 static struct vdoa_data *coda_get_vdoa_data(void)
363 struct device_node *vdoa_node;
364 struct platform_device *vdoa_pdev;
365 struct vdoa_data *vdoa_data = NULL;
367 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
368 if (!vdoa_node)
369 return NULL;
371 vdoa_pdev = of_find_device_by_node(vdoa_node);
372 if (!vdoa_pdev)
373 goto out;
375 vdoa_data = platform_get_drvdata(vdoa_pdev);
376 if (!vdoa_data)
377 vdoa_data = ERR_PTR(-EPROBE_DEFER);
379 out:
380 if (vdoa_node)
381 of_node_put(vdoa_node);
383 return vdoa_data;
387 * V4L2 ioctl() operations.
389 static int coda_querycap(struct file *file, void *priv,
390 struct v4l2_capability *cap)
392 struct coda_ctx *ctx = fh_to_ctx(priv);
394 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
395 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
396 sizeof(cap->card));
397 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
398 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
399 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
401 return 0;
404 static int coda_enum_fmt(struct file *file, void *priv,
405 struct v4l2_fmtdesc *f)
407 struct video_device *vdev = video_devdata(file);
408 const struct coda_video_device *cvd = to_coda_video_device(vdev);
409 struct coda_ctx *ctx = fh_to_ctx(priv);
410 const u32 *formats;
412 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
413 formats = cvd->src_formats;
414 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
415 formats = cvd->dst_formats;
416 else
417 return -EINVAL;
419 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
420 return -EINVAL;
422 /* Skip YUYV if the vdoa is not available */
423 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
424 formats[f->index] == V4L2_PIX_FMT_YUYV)
425 return -EINVAL;
427 f->pixelformat = formats[f->index];
429 return 0;
432 static int coda_g_fmt(struct file *file, void *priv,
433 struct v4l2_format *f)
435 struct coda_q_data *q_data;
436 struct coda_ctx *ctx = fh_to_ctx(priv);
438 q_data = get_q_data(ctx, f->type);
439 if (!q_data)
440 return -EINVAL;
442 f->fmt.pix.field = V4L2_FIELD_NONE;
443 f->fmt.pix.pixelformat = q_data->fourcc;
444 f->fmt.pix.width = q_data->width;
445 f->fmt.pix.height = q_data->height;
446 f->fmt.pix.bytesperline = q_data->bytesperline;
448 f->fmt.pix.sizeimage = q_data->sizeimage;
449 f->fmt.pix.colorspace = ctx->colorspace;
450 f->fmt.pix.xfer_func = ctx->xfer_func;
451 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
452 f->fmt.pix.quantization = ctx->quantization;
454 return 0;
457 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
459 struct coda_q_data *q_data;
460 const u32 *formats;
461 int i;
463 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
464 formats = ctx->cvd->src_formats;
465 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
466 formats = ctx->cvd->dst_formats;
467 else
468 return -EINVAL;
470 for (i = 0; i < CODA_MAX_FORMATS; i++) {
471 /* Skip YUYV if the vdoa is not available */
472 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
473 formats[i] == V4L2_PIX_FMT_YUYV)
474 continue;
476 if (formats[i] == f->fmt.pix.pixelformat) {
477 f->fmt.pix.pixelformat = formats[i];
478 return 0;
482 /* Fall back to currently set pixelformat */
483 q_data = get_q_data(ctx, f->type);
484 f->fmt.pix.pixelformat = q_data->fourcc;
486 return 0;
489 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
490 bool *use_vdoa)
492 int err;
494 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
495 return -EINVAL;
497 if (!use_vdoa)
498 return -EINVAL;
500 if (!ctx->vdoa) {
501 *use_vdoa = false;
502 return 0;
505 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
506 f->fmt.pix.height, f->fmt.pix.pixelformat);
507 if (err) {
508 *use_vdoa = false;
509 return 0;
512 *use_vdoa = true;
513 return 0;
516 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
517 u32 width, u32 height)
520 * This is a rough estimate for sensible compressed buffer
521 * sizes (between 1 and 16 bits per pixel). This could be
522 * improved by better format specific worst case estimates.
524 return round_up(clamp(sizeimage, width * height / 8,
525 width * height * 2), PAGE_SIZE);
528 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
529 struct v4l2_format *f)
531 struct coda_dev *dev = ctx->dev;
532 unsigned int max_w, max_h;
533 enum v4l2_field field;
535 field = f->fmt.pix.field;
536 if (field == V4L2_FIELD_ANY)
537 field = V4L2_FIELD_NONE;
538 else if (V4L2_FIELD_NONE != field)
539 return -EINVAL;
541 /* V4L2 specification suggests the driver corrects the format struct
542 * if any of the dimensions is unsupported */
543 f->fmt.pix.field = field;
545 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
546 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
547 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
548 S_ALIGN);
550 switch (f->fmt.pix.pixelformat) {
551 case V4L2_PIX_FMT_NV12:
552 case V4L2_PIX_FMT_YUV420:
553 case V4L2_PIX_FMT_YVU420:
555 * Frame stride must be at least multiple of 8,
556 * but multiple of 16 for h.264 or JPEG 4:2:x
558 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
559 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
560 f->fmt.pix.height * 3 / 2;
561 break;
562 case V4L2_PIX_FMT_YUYV:
563 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
564 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
565 f->fmt.pix.height;
566 break;
567 case V4L2_PIX_FMT_YUV422P:
568 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
569 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
570 f->fmt.pix.height * 2;
571 break;
572 case V4L2_PIX_FMT_JPEG:
573 case V4L2_PIX_FMT_H264:
574 case V4L2_PIX_FMT_MPEG4:
575 case V4L2_PIX_FMT_MPEG2:
576 f->fmt.pix.bytesperline = 0;
577 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
578 f->fmt.pix.sizeimage,
579 f->fmt.pix.width,
580 f->fmt.pix.height);
581 break;
582 default:
583 BUG();
586 return 0;
589 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
590 struct v4l2_format *f)
592 struct coda_ctx *ctx = fh_to_ctx(priv);
593 const struct coda_q_data *q_data_src;
594 const struct coda_codec *codec;
595 struct vb2_queue *src_vq;
596 int ret;
597 bool use_vdoa;
599 ret = coda_try_pixelformat(ctx, f);
600 if (ret < 0)
601 return ret;
603 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
606 * If the source format is already fixed, only allow the same output
607 * resolution
609 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
610 if (vb2_is_streaming(src_vq)) {
611 f->fmt.pix.width = q_data_src->width;
612 f->fmt.pix.height = q_data_src->height;
615 f->fmt.pix.colorspace = ctx->colorspace;
616 f->fmt.pix.xfer_func = ctx->xfer_func;
617 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
618 f->fmt.pix.quantization = ctx->quantization;
620 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
621 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
622 f->fmt.pix.pixelformat);
623 if (!codec)
624 return -EINVAL;
626 ret = coda_try_fmt(ctx, codec, f);
627 if (ret < 0)
628 return ret;
630 /* The h.264 decoder only returns complete 16x16 macroblocks */
631 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
632 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
633 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
634 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
635 f->fmt.pix.height * 3 / 2;
637 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
638 if (ret < 0)
639 return ret;
641 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
642 if (!use_vdoa)
643 return -EINVAL;
645 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
646 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
647 f->fmt.pix.height;
651 return 0;
654 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
656 enum v4l2_colorspace colorspace;
658 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
659 colorspace = V4L2_COLORSPACE_JPEG;
660 else if (fmt->width <= 720 && fmt->height <= 576)
661 colorspace = V4L2_COLORSPACE_SMPTE170M;
662 else
663 colorspace = V4L2_COLORSPACE_REC709;
665 fmt->colorspace = colorspace;
666 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
667 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
668 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
671 static int coda_try_fmt_vid_out(struct file *file, void *priv,
672 struct v4l2_format *f)
674 struct coda_ctx *ctx = fh_to_ctx(priv);
675 struct coda_dev *dev = ctx->dev;
676 const struct coda_q_data *q_data_dst;
677 const struct coda_codec *codec;
678 int ret;
680 ret = coda_try_pixelformat(ctx, f);
681 if (ret < 0)
682 return ret;
684 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
685 coda_set_default_colorspace(&f->fmt.pix);
687 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
688 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
690 return coda_try_fmt(ctx, codec, f);
693 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
694 struct v4l2_rect *r)
696 struct coda_q_data *q_data;
697 struct vb2_queue *vq;
699 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
700 if (!vq)
701 return -EINVAL;
703 q_data = get_q_data(ctx, f->type);
704 if (!q_data)
705 return -EINVAL;
707 if (vb2_is_busy(vq)) {
708 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
709 return -EBUSY;
712 q_data->fourcc = f->fmt.pix.pixelformat;
713 q_data->width = f->fmt.pix.width;
714 q_data->height = f->fmt.pix.height;
715 q_data->bytesperline = f->fmt.pix.bytesperline;
716 q_data->sizeimage = f->fmt.pix.sizeimage;
717 if (r) {
718 q_data->rect = *r;
719 } else {
720 q_data->rect.left = 0;
721 q_data->rect.top = 0;
722 q_data->rect.width = f->fmt.pix.width;
723 q_data->rect.height = f->fmt.pix.height;
726 switch (f->fmt.pix.pixelformat) {
727 case V4L2_PIX_FMT_YUYV:
728 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
729 break;
730 case V4L2_PIX_FMT_NV12:
731 if (!disable_tiling) {
732 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
733 break;
735 /* else fall through */
736 case V4L2_PIX_FMT_YUV420:
737 case V4L2_PIX_FMT_YVU420:
738 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
739 break;
740 default:
741 break;
744 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
745 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
746 ctx->use_vdoa)
747 vdoa_context_configure(ctx->vdoa,
748 round_up(f->fmt.pix.width, 16),
749 f->fmt.pix.height,
750 f->fmt.pix.pixelformat);
751 else
752 ctx->use_vdoa = false;
754 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
755 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n",
756 f->type, q_data->width, q_data->height,
757 (char *)&q_data->fourcc,
758 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
760 return 0;
763 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
764 struct v4l2_format *f)
766 struct coda_ctx *ctx = fh_to_ctx(priv);
767 struct coda_q_data *q_data_src;
768 struct v4l2_rect r;
769 int ret;
771 ret = coda_try_fmt_vid_cap(file, priv, f);
772 if (ret)
773 return ret;
775 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
776 r.left = 0;
777 r.top = 0;
778 r.width = q_data_src->width;
779 r.height = q_data_src->height;
781 ret = coda_s_fmt(ctx, f, &r);
782 if (ret)
783 return ret;
785 if (ctx->inst_type != CODA_INST_ENCODER)
786 return 0;
788 ctx->colorspace = f->fmt.pix.colorspace;
789 ctx->xfer_func = f->fmt.pix.xfer_func;
790 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
791 ctx->quantization = f->fmt.pix.quantization;
793 return 0;
796 static int coda_s_fmt_vid_out(struct file *file, void *priv,
797 struct v4l2_format *f)
799 struct coda_ctx *ctx = fh_to_ctx(priv);
800 struct v4l2_format f_cap;
801 struct vb2_queue *dst_vq;
802 int ret;
804 ret = coda_try_fmt_vid_out(file, priv, f);
805 if (ret)
806 return ret;
808 ret = coda_s_fmt(ctx, f, NULL);
809 if (ret)
810 return ret;
812 if (ctx->inst_type != CODA_INST_DECODER)
813 return 0;
815 ctx->colorspace = f->fmt.pix.colorspace;
816 ctx->xfer_func = f->fmt.pix.xfer_func;
817 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
818 ctx->quantization = f->fmt.pix.quantization;
820 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
821 if (!dst_vq)
822 return -EINVAL;
825 * Setting the capture queue format is not possible while the capture
826 * queue is still busy. This is not an error, but the user will have to
827 * make sure themselves that the capture format is set correctly before
828 * starting the output queue again.
830 if (vb2_is_busy(dst_vq))
831 return 0;
833 memset(&f_cap, 0, sizeof(f_cap));
834 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
835 coda_g_fmt(file, priv, &f_cap);
836 f_cap.fmt.pix.width = f->fmt.pix.width;
837 f_cap.fmt.pix.height = f->fmt.pix.height;
839 return coda_s_fmt_vid_cap(file, priv, &f_cap);
842 static int coda_reqbufs(struct file *file, void *priv,
843 struct v4l2_requestbuffers *rb)
845 struct coda_ctx *ctx = fh_to_ctx(priv);
846 int ret;
848 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
849 if (ret)
850 return ret;
853 * Allow to allocate instance specific per-context buffers, such as
854 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
856 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
857 return ctx->ops->reqbufs(ctx, rb);
859 return 0;
862 static int coda_qbuf(struct file *file, void *priv,
863 struct v4l2_buffer *buf)
865 struct coda_ctx *ctx = fh_to_ctx(priv);
867 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
870 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
871 struct vb2_v4l2_buffer *buf)
873 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
874 (buf->sequence == (ctx->qsequence - 1)));
877 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
878 enum vb2_buffer_state state)
880 const struct v4l2_event eos_event = {
881 .type = V4L2_EVENT_EOS
884 if (coda_buf_is_end_of_stream(ctx, buf)) {
885 buf->flags |= V4L2_BUF_FLAG_LAST;
887 v4l2_event_queue_fh(&ctx->fh, &eos_event);
890 v4l2_m2m_buf_done(buf, state);
893 static int coda_g_selection(struct file *file, void *fh,
894 struct v4l2_selection *s)
896 struct coda_ctx *ctx = fh_to_ctx(fh);
897 struct coda_q_data *q_data;
898 struct v4l2_rect r, *rsel;
900 q_data = get_q_data(ctx, s->type);
901 if (!q_data)
902 return -EINVAL;
904 r.left = 0;
905 r.top = 0;
906 r.width = q_data->width;
907 r.height = q_data->height;
908 rsel = &q_data->rect;
910 switch (s->target) {
911 case V4L2_SEL_TGT_CROP_DEFAULT:
912 case V4L2_SEL_TGT_CROP_BOUNDS:
913 rsel = &r;
914 /* fallthrough */
915 case V4L2_SEL_TGT_CROP:
916 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
917 return -EINVAL;
918 break;
919 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
920 case V4L2_SEL_TGT_COMPOSE_PADDED:
921 rsel = &r;
922 /* fallthrough */
923 case V4L2_SEL_TGT_COMPOSE:
924 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
925 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
926 return -EINVAL;
927 break;
928 default:
929 return -EINVAL;
932 s->r = *rsel;
934 return 0;
937 static int coda_s_selection(struct file *file, void *fh,
938 struct v4l2_selection *s)
940 struct coda_ctx *ctx = fh_to_ctx(fh);
941 struct coda_q_data *q_data;
943 if (ctx->inst_type == CODA_INST_ENCODER &&
944 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
945 s->target == V4L2_SEL_TGT_CROP) {
946 q_data = get_q_data(ctx, s->type);
947 if (!q_data)
948 return -EINVAL;
950 s->r.left = 0;
951 s->r.top = 0;
952 s->r.width = clamp(s->r.width, 2U, q_data->width);
953 s->r.height = clamp(s->r.height, 2U, q_data->height);
955 if (s->flags & V4L2_SEL_FLAG_LE) {
956 s->r.width = round_up(s->r.width, 2);
957 s->r.height = round_up(s->r.height, 2);
958 } else {
959 s->r.width = round_down(s->r.width, 2);
960 s->r.height = round_down(s->r.height, 2);
963 q_data->rect = s->r;
965 return 0;
968 return coda_g_selection(file, fh, s);
971 static int coda_try_encoder_cmd(struct file *file, void *fh,
972 struct v4l2_encoder_cmd *ec)
974 if (ec->cmd != V4L2_ENC_CMD_STOP)
975 return -EINVAL;
977 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
978 return -EINVAL;
980 return 0;
983 static int coda_encoder_cmd(struct file *file, void *fh,
984 struct v4l2_encoder_cmd *ec)
986 struct coda_ctx *ctx = fh_to_ctx(fh);
987 struct vb2_queue *dst_vq;
988 int ret;
990 ret = coda_try_encoder_cmd(file, fh, ec);
991 if (ret < 0)
992 return ret;
994 /* Ignore encoder stop command silently in decoder context */
995 if (ctx->inst_type != CODA_INST_ENCODER)
996 return 0;
998 /* Set the stream-end flag on this context */
999 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1001 flush_work(&ctx->pic_run_work);
1003 /* If there is no buffer in flight, wake up */
1004 if (!ctx->streamon_out || ctx->qsequence == ctx->osequence) {
1005 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1006 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1007 dst_vq->last_buffer_dequeued = true;
1008 wake_up(&dst_vq->done_wq);
1011 return 0;
1014 static int coda_try_decoder_cmd(struct file *file, void *fh,
1015 struct v4l2_decoder_cmd *dc)
1017 if (dc->cmd != V4L2_DEC_CMD_STOP)
1018 return -EINVAL;
1020 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
1021 return -EINVAL;
1023 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
1024 return -EINVAL;
1026 return 0;
1029 static int coda_decoder_cmd(struct file *file, void *fh,
1030 struct v4l2_decoder_cmd *dc)
1032 struct coda_ctx *ctx = fh_to_ctx(fh);
1033 int ret;
1035 ret = coda_try_decoder_cmd(file, fh, dc);
1036 if (ret < 0)
1037 return ret;
1039 /* Ignore decoder stop command silently in encoder context */
1040 if (ctx->inst_type != CODA_INST_DECODER)
1041 return 0;
1043 /* Set the stream-end flag on this context */
1044 coda_bit_stream_end_flag(ctx);
1045 ctx->hold = false;
1046 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1048 return 0;
1051 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1053 struct coda_ctx *ctx = fh_to_ctx(fh);
1054 struct v4l2_fract *tpf;
1056 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1057 return -EINVAL;
1059 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1060 tpf = &a->parm.output.timeperframe;
1061 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1062 tpf->numerator = 1 + (ctx->params.framerate >>
1063 CODA_FRATE_DIV_OFFSET);
1065 return 0;
1069 * Approximate timeperframe v4l2_fract with values that can be written
1070 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1072 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1074 struct v4l2_fract s = *timeperframe;
1075 struct v4l2_fract f0;
1076 struct v4l2_fract f1 = { 1, 0 };
1077 struct v4l2_fract f2 = { 0, 1 };
1078 unsigned int i, div, s_denominator;
1080 /* Lower bound is 1/65535 */
1081 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1082 timeperframe->numerator = 1;
1083 timeperframe->denominator = 65535;
1084 return;
1087 /* Upper bound is 65536/1, map everything above to infinity */
1088 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1089 timeperframe->numerator = 1;
1090 timeperframe->denominator = 0;
1091 return;
1094 /* Reduce fraction to lowest terms */
1095 div = gcd(s.numerator, s.denominator);
1096 if (div > 1) {
1097 s.numerator /= div;
1098 s.denominator /= div;
1101 if (s.numerator <= 65536 && s.denominator < 65536) {
1102 *timeperframe = s;
1103 return;
1106 /* Find successive convergents from continued fraction expansion */
1107 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1108 f0 = f1;
1109 f1 = f2;
1111 /* Stop when f2 exactly equals timeperframe */
1112 if (s.numerator == 0)
1113 break;
1115 i = s.denominator / s.numerator;
1117 f2.numerator = f0.numerator + i * f1.numerator;
1118 f2.denominator = f0.denominator + i * f2.denominator;
1120 s_denominator = s.numerator;
1121 s.numerator = s.denominator % s.numerator;
1122 s.denominator = s_denominator;
1125 *timeperframe = f1;
1128 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1130 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1131 timeperframe->denominator;
1134 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1136 struct coda_ctx *ctx = fh_to_ctx(fh);
1137 struct v4l2_fract *tpf;
1139 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1140 return -EINVAL;
1142 tpf = &a->parm.output.timeperframe;
1143 coda_approximate_timeperframe(tpf);
1144 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1146 return 0;
1149 static int coda_subscribe_event(struct v4l2_fh *fh,
1150 const struct v4l2_event_subscription *sub)
1152 switch (sub->type) {
1153 case V4L2_EVENT_EOS:
1154 return v4l2_event_subscribe(fh, sub, 0, NULL);
1155 default:
1156 return v4l2_ctrl_subscribe_event(fh, sub);
1160 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1161 .vidioc_querycap = coda_querycap,
1163 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1164 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1165 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1166 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1168 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1169 .vidioc_g_fmt_vid_out = coda_g_fmt,
1170 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1171 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1173 .vidioc_reqbufs = coda_reqbufs,
1174 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1176 .vidioc_qbuf = coda_qbuf,
1177 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1178 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
1179 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1180 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1182 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1183 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1185 .vidioc_g_selection = coda_g_selection,
1186 .vidioc_s_selection = coda_s_selection,
1188 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1189 .vidioc_encoder_cmd = coda_encoder_cmd,
1190 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
1191 .vidioc_decoder_cmd = coda_decoder_cmd,
1193 .vidioc_g_parm = coda_g_parm,
1194 .vidioc_s_parm = coda_s_parm,
1196 .vidioc_subscribe_event = coda_subscribe_event,
1197 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1201 * Mem-to-mem operations.
1204 static void coda_device_run(void *m2m_priv)
1206 struct coda_ctx *ctx = m2m_priv;
1207 struct coda_dev *dev = ctx->dev;
1209 queue_work(dev->workqueue, &ctx->pic_run_work);
1212 static void coda_pic_run_work(struct work_struct *work)
1214 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1215 struct coda_dev *dev = ctx->dev;
1216 int ret;
1218 mutex_lock(&ctx->buffer_mutex);
1219 mutex_lock(&dev->coda_mutex);
1221 ret = ctx->ops->prepare_run(ctx);
1222 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1223 mutex_unlock(&dev->coda_mutex);
1224 mutex_unlock(&ctx->buffer_mutex);
1225 /* job_finish scheduled by prepare_decode */
1226 return;
1229 if (!wait_for_completion_timeout(&ctx->completion,
1230 msecs_to_jiffies(1000))) {
1231 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1233 ctx->hold = true;
1235 coda_hw_reset(ctx);
1237 if (ctx->ops->run_timeout)
1238 ctx->ops->run_timeout(ctx);
1239 } else if (!ctx->aborting) {
1240 ctx->ops->finish_run(ctx);
1243 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1244 ctx->ops->seq_end_work)
1245 queue_work(dev->workqueue, &ctx->seq_end_work);
1247 mutex_unlock(&dev->coda_mutex);
1248 mutex_unlock(&ctx->buffer_mutex);
1250 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1253 static int coda_job_ready(void *m2m_priv)
1255 struct coda_ctx *ctx = m2m_priv;
1256 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1259 * For both 'P' and 'key' frame cases 1 picture
1260 * and 1 frame are needed. In the decoder case,
1261 * the compressed frame can be in the bitstream.
1263 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1264 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1265 "not ready: not enough video buffers.\n");
1266 return 0;
1269 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1270 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1271 "not ready: not enough video capture buffers.\n");
1272 return 0;
1275 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1276 bool stream_end = ctx->bit_stream_param &
1277 CODA_BIT_STREAM_END_FLAG;
1278 int num_metas = ctx->num_metas;
1279 unsigned int count;
1281 count = hweight32(ctx->frm_dis_flg);
1282 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1283 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1284 "%d: not ready: all internal buffers in use: %d/%d (0x%x)",
1285 ctx->idx, count, ctx->num_internal_frames,
1286 ctx->frm_dis_flg);
1287 return 0;
1290 if (ctx->hold && !src_bufs) {
1291 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1292 "%d: not ready: on hold for more buffers.\n",
1293 ctx->idx);
1294 return 0;
1297 if (!stream_end && (num_metas + src_bufs) < 2) {
1298 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1299 "%d: not ready: need 2 buffers available (%d, %d)\n",
1300 ctx->idx, num_metas, src_bufs);
1301 return 0;
1305 if (!src_bufs && !stream_end &&
1306 (coda_get_bitstream_payload(ctx) < 512)) {
1307 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1308 "%d: not ready: not enough bitstream data (%d).\n",
1309 ctx->idx, coda_get_bitstream_payload(ctx));
1310 return 0;
1314 if (ctx->aborting) {
1315 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1316 "not ready: aborting\n");
1317 return 0;
1320 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1321 "job ready\n");
1323 return 1;
1326 static void coda_job_abort(void *priv)
1328 struct coda_ctx *ctx = priv;
1330 ctx->aborting = 1;
1332 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1333 "Aborting task\n");
1336 static const struct v4l2_m2m_ops coda_m2m_ops = {
1337 .device_run = coda_device_run,
1338 .job_ready = coda_job_ready,
1339 .job_abort = coda_job_abort,
1342 static void set_default_params(struct coda_ctx *ctx)
1344 unsigned int max_w, max_h, usize, csize;
1346 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1347 ctx->cvd->dst_formats[0]);
1348 max_w = min(ctx->codec->max_w, 1920U);
1349 max_h = min(ctx->codec->max_h, 1088U);
1350 usize = max_w * max_h * 3 / 2;
1351 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1353 ctx->params.codec_mode = ctx->codec->mode;
1354 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1355 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1356 else
1357 ctx->colorspace = V4L2_COLORSPACE_REC709;
1358 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1359 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1360 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1361 ctx->params.framerate = 30;
1363 /* Default formats for output and input queues */
1364 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1365 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1366 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1367 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1368 ctx->q_data[V4L2_M2M_DST].width = max_w;
1369 ctx->q_data[V4L2_M2M_DST].height = max_h;
1370 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1371 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1372 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1373 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1374 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1375 } else {
1376 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1377 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1378 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1379 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1381 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1382 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1383 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1384 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1387 * Since the RBC2AXI logic only supports a single chroma plane,
1388 * macroblock tiling only works for to NV12 pixel format.
1390 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1394 * Queue operations
1396 static int coda_queue_setup(struct vb2_queue *vq,
1397 unsigned int *nbuffers, unsigned int *nplanes,
1398 unsigned int sizes[], struct device *alloc_devs[])
1400 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1401 struct coda_q_data *q_data;
1402 unsigned int size;
1404 q_data = get_q_data(ctx, vq->type);
1405 size = q_data->sizeimage;
1407 *nplanes = 1;
1408 sizes[0] = size;
1410 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1411 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1413 return 0;
1416 static int coda_buf_prepare(struct vb2_buffer *vb)
1418 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1419 struct coda_q_data *q_data;
1421 q_data = get_q_data(ctx, vb->vb2_queue->type);
1423 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1424 v4l2_warn(&ctx->dev->v4l2_dev,
1425 "%s data will not fit into plane (%lu < %lu)\n",
1426 __func__, vb2_plane_size(vb, 0),
1427 (long)q_data->sizeimage);
1428 return -EINVAL;
1431 return 0;
1434 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1436 if (!ctrl)
1437 return;
1439 v4l2_ctrl_lock(ctrl);
1442 * Extend the control range if the parsed stream contains a known but
1443 * unsupported value or level.
1445 if (value > ctrl->maximum) {
1446 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1447 ctrl->menu_skip_mask & ~(1 << value),
1448 ctrl->default_value);
1449 } else if (value < ctrl->minimum) {
1450 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1451 ctrl->menu_skip_mask & ~(1 << value),
1452 ctrl->default_value);
1455 __v4l2_ctrl_s_ctrl(ctrl, value);
1457 v4l2_ctrl_unlock(ctrl);
1460 static void coda_update_h264_profile_ctrl(struct coda_ctx *ctx)
1462 const char * const *profile_names;
1463 int profile;
1465 profile = coda_h264_profile(ctx->params.h264_profile_idc);
1466 if (profile < 0) {
1467 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Profile: %u\n",
1468 ctx->params.h264_profile_idc);
1469 return;
1472 coda_update_menu_ctrl(ctx->h264_profile_ctrl, profile);
1474 profile_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_PROFILE);
1476 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "Parsed H264 Profile: %s\n",
1477 profile_names[profile]);
1480 static void coda_update_h264_level_ctrl(struct coda_ctx *ctx)
1482 const char * const *level_names;
1483 int level;
1485 level = coda_h264_level(ctx->params.h264_level_idc);
1486 if (level < 0) {
1487 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Level: %u\n",
1488 ctx->params.h264_level_idc);
1489 return;
1492 coda_update_menu_ctrl(ctx->h264_level_ctrl, level);
1494 level_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL);
1496 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "Parsed H264 Level: %s\n",
1497 level_names[level]);
1500 static void coda_buf_queue(struct vb2_buffer *vb)
1502 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1503 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1504 struct vb2_queue *vq = vb->vb2_queue;
1505 struct coda_q_data *q_data;
1507 q_data = get_q_data(ctx, vb->vb2_queue->type);
1510 * In the decoder case, immediately try to copy the buffer into the
1511 * bitstream ringbuffer and mark it as ready to be dequeued.
1513 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1515 * For backwards compatibility, queuing an empty buffer marks
1516 * the stream end
1518 if (vb2_get_plane_payload(vb, 0) == 0)
1519 coda_bit_stream_end_flag(ctx);
1521 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1523 * Unless already done, try to obtain profile_idc and
1524 * level_idc from the SPS header. This allows to decide
1525 * whether to enable reordering during sequence
1526 * initialization.
1528 if (!ctx->params.h264_profile_idc) {
1529 coda_sps_parse_profile(ctx, vb);
1530 coda_update_h264_profile_ctrl(ctx);
1531 coda_update_h264_level_ctrl(ctx);
1535 mutex_lock(&ctx->bitstream_mutex);
1536 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1537 if (vb2_is_streaming(vb->vb2_queue))
1538 /* This set buf->sequence = ctx->qsequence++ */
1539 coda_fill_bitstream(ctx, NULL);
1540 mutex_unlock(&ctx->bitstream_mutex);
1541 } else {
1542 if (ctx->inst_type == CODA_INST_ENCODER &&
1543 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1544 vbuf->sequence = ctx->qsequence++;
1545 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1549 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1550 size_t size, const char *name, struct dentry *parent)
1552 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1553 GFP_KERNEL);
1554 if (!buf->vaddr) {
1555 v4l2_err(&dev->v4l2_dev,
1556 "Failed to allocate %s buffer of size %zu\n",
1557 name, size);
1558 return -ENOMEM;
1561 buf->size = size;
1563 if (name && parent) {
1564 buf->blob.data = buf->vaddr;
1565 buf->blob.size = size;
1566 buf->dentry = debugfs_create_blob(name, 0644, parent,
1567 &buf->blob);
1568 if (!buf->dentry)
1569 dev_warn(&dev->plat_dev->dev,
1570 "failed to create debugfs entry %s\n", name);
1573 return 0;
1576 void coda_free_aux_buf(struct coda_dev *dev,
1577 struct coda_aux_buf *buf)
1579 if (buf->vaddr) {
1580 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1581 buf->vaddr, buf->paddr);
1582 buf->vaddr = NULL;
1583 buf->size = 0;
1584 debugfs_remove(buf->dentry);
1585 buf->dentry = NULL;
1589 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1591 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1592 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1593 struct coda_q_data *q_data_src, *q_data_dst;
1594 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1595 struct vb2_v4l2_buffer *buf;
1596 struct list_head list;
1597 int ret = 0;
1599 if (count < 1)
1600 return -EINVAL;
1602 INIT_LIST_HEAD(&list);
1604 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1605 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1606 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1607 /* copy the buffers that were queued before streamon */
1608 mutex_lock(&ctx->bitstream_mutex);
1609 coda_fill_bitstream(ctx, &list);
1610 mutex_unlock(&ctx->bitstream_mutex);
1612 if (coda_get_bitstream_payload(ctx) < 512) {
1613 ret = -EINVAL;
1614 goto err;
1618 ctx->streamon_out = 1;
1619 } else {
1620 ctx->streamon_cap = 1;
1623 /* Don't start the coda unless both queues are on */
1624 if (!(ctx->streamon_out && ctx->streamon_cap))
1625 goto out;
1627 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1628 if ((q_data_src->rect.width != q_data_dst->width &&
1629 round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
1630 (q_data_src->rect.height != q_data_dst->height &&
1631 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
1632 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1633 q_data_src->rect.width, q_data_src->rect.height,
1634 q_data_dst->width, q_data_dst->height);
1635 ret = -EINVAL;
1636 goto err;
1639 /* Allow BIT decoder device_run with no new buffers queued */
1640 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1641 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1643 ctx->gopcounter = ctx->params.gop_size - 1;
1645 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1646 q_data_dst->fourcc);
1647 if (!ctx->codec) {
1648 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1649 ret = -EINVAL;
1650 goto err;
1653 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1654 ctx->params.gop_size = 1;
1655 ctx->gopcounter = ctx->params.gop_size - 1;
1657 ret = ctx->ops->start_streaming(ctx);
1658 if (ctx->inst_type == CODA_INST_DECODER) {
1659 if (ret == -EAGAIN)
1660 goto out;
1662 if (ret < 0)
1663 goto err;
1665 out:
1666 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1667 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1668 list_del(&m2m_buf->list);
1669 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
1672 return 0;
1674 err:
1675 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1676 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1677 list_del(&m2m_buf->list);
1678 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
1680 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1681 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1682 } else {
1683 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1684 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1686 return ret;
1689 static void coda_stop_streaming(struct vb2_queue *q)
1691 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1692 struct coda_dev *dev = ctx->dev;
1693 struct vb2_v4l2_buffer *buf;
1694 unsigned long flags;
1695 bool stop;
1697 stop = ctx->streamon_out && ctx->streamon_cap;
1699 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1700 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1701 "%s: output\n", __func__);
1702 ctx->streamon_out = 0;
1704 coda_bit_stream_end_flag(ctx);
1706 ctx->qsequence = 0;
1708 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1709 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1710 } else {
1711 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1712 "%s: capture\n", __func__);
1713 ctx->streamon_cap = 0;
1715 ctx->osequence = 0;
1716 ctx->sequence_offset = 0;
1718 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1719 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1722 if (stop) {
1723 struct coda_buffer_meta *meta;
1725 if (ctx->ops->seq_end_work) {
1726 queue_work(dev->workqueue, &ctx->seq_end_work);
1727 flush_work(&ctx->seq_end_work);
1729 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1730 while (!list_empty(&ctx->buffer_meta_list)) {
1731 meta = list_first_entry(&ctx->buffer_meta_list,
1732 struct coda_buffer_meta, list);
1733 list_del(&meta->list);
1734 kfree(meta);
1736 ctx->num_metas = 0;
1737 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1738 kfifo_init(&ctx->bitstream_fifo,
1739 ctx->bitstream.vaddr, ctx->bitstream.size);
1740 ctx->runcounter = 0;
1741 ctx->aborting = 0;
1742 ctx->hold = false;
1745 if (!ctx->streamon_out && !ctx->streamon_cap)
1746 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1749 static const struct vb2_ops coda_qops = {
1750 .queue_setup = coda_queue_setup,
1751 .buf_prepare = coda_buf_prepare,
1752 .buf_queue = coda_buf_queue,
1753 .start_streaming = coda_start_streaming,
1754 .stop_streaming = coda_stop_streaming,
1755 .wait_prepare = vb2_ops_wait_prepare,
1756 .wait_finish = vb2_ops_wait_finish,
1759 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1761 struct coda_ctx *ctx =
1762 container_of(ctrl->handler, struct coda_ctx, ctrls);
1764 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1765 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1767 switch (ctrl->id) {
1768 case V4L2_CID_HFLIP:
1769 if (ctrl->val)
1770 ctx->params.rot_mode |= CODA_MIR_HOR;
1771 else
1772 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1773 break;
1774 case V4L2_CID_VFLIP:
1775 if (ctrl->val)
1776 ctx->params.rot_mode |= CODA_MIR_VER;
1777 else
1778 ctx->params.rot_mode &= ~CODA_MIR_VER;
1779 break;
1780 case V4L2_CID_MPEG_VIDEO_BITRATE:
1781 ctx->params.bitrate = ctrl->val / 1000;
1782 break;
1783 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1784 ctx->params.gop_size = ctrl->val;
1785 break;
1786 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1787 ctx->params.h264_intra_qp = ctrl->val;
1788 break;
1789 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1790 ctx->params.h264_inter_qp = ctrl->val;
1791 break;
1792 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1793 ctx->params.h264_min_qp = ctrl->val;
1794 break;
1795 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1796 ctx->params.h264_max_qp = ctrl->val;
1797 break;
1798 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1799 ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
1800 break;
1801 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1802 ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
1803 break;
1804 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1805 ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
1806 break;
1807 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
1808 /* TODO: switch between baseline and constrained baseline */
1809 if (ctx->inst_type == CODA_INST_ENCODER)
1810 ctx->params.h264_profile_idc = 66;
1811 break;
1812 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
1813 /* nothing to do, this is set by the encoder */
1814 break;
1815 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1816 ctx->params.mpeg4_intra_qp = ctrl->val;
1817 break;
1818 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1819 ctx->params.mpeg4_inter_qp = ctrl->val;
1820 break;
1821 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
1822 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
1823 /* nothing to do, these are fixed */
1824 break;
1825 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1826 ctx->params.slice_mode = ctrl->val;
1827 break;
1828 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1829 ctx->params.slice_max_mb = ctrl->val;
1830 break;
1831 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1832 ctx->params.slice_max_bits = ctrl->val * 8;
1833 break;
1834 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1835 break;
1836 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1837 ctx->params.intra_refresh = ctrl->val;
1838 break;
1839 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
1840 ctx->params.force_ipicture = true;
1841 break;
1842 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1843 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1844 break;
1845 case V4L2_CID_JPEG_RESTART_INTERVAL:
1846 ctx->params.jpeg_restart_interval = ctrl->val;
1847 break;
1848 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1849 ctx->params.vbv_delay = ctrl->val;
1850 break;
1851 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1852 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1853 break;
1854 default:
1855 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1856 "Invalid control, id=%d, val=%d\n",
1857 ctrl->id, ctrl->val);
1858 return -EINVAL;
1861 return 0;
1864 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1865 .s_ctrl = coda_s_ctrl,
1868 static void coda_encode_ctrls(struct coda_ctx *ctx)
1870 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
1872 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1873 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1874 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1875 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
1876 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1877 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1878 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1879 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1880 if (ctx->dev->devtype->product != CODA_960) {
1881 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1882 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1884 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1885 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1886 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1887 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
1888 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1889 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
1890 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1891 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1892 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
1893 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1894 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1895 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1896 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
1897 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
1898 if (ctx->dev->devtype->product == CODA_HX4 ||
1899 ctx->dev->devtype->product == CODA_7541) {
1900 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1901 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1902 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
1903 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1904 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1905 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
1906 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
1908 if (ctx->dev->devtype->product == CODA_960) {
1909 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1910 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1911 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
1912 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1913 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1914 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
1915 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
1916 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
1917 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1919 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1920 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1921 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1922 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1923 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1924 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
1925 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
1926 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
1927 if (ctx->dev->devtype->product == CODA_HX4 ||
1928 ctx->dev->devtype->product == CODA_7541 ||
1929 ctx->dev->devtype->product == CODA_960) {
1930 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1931 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
1932 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
1933 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
1934 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
1936 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1937 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1938 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1939 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1940 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1941 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1942 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1943 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1944 500);
1945 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1946 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1947 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1948 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1949 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1950 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1951 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1952 1920 * 1088 / 256, 1, 0);
1953 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1954 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1956 * The maximum VBV size value is 0x7fffffff bits,
1957 * one bit less than 262144 KiB
1959 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1960 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
1963 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1965 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1966 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1967 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1968 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1971 static void coda_decode_ctrls(struct coda_ctx *ctx)
1973 u64 mask;
1974 u8 max;
1976 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
1977 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1978 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
1979 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
1980 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
1981 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
1982 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
1983 if (ctx->h264_profile_ctrl)
1984 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1986 if (ctx->dev->devtype->product == CODA_HX4 ||
1987 ctx->dev->devtype->product == CODA_7541) {
1988 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
1989 mask = ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1990 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1991 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
1992 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
1993 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0));
1994 } else if (ctx->dev->devtype->product == CODA_960) {
1995 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
1996 mask = ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
1997 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
1998 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
1999 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2000 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0) |
2001 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_1));
2002 } else {
2003 return;
2005 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2006 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, mask,
2007 max);
2008 if (ctx->h264_level_ctrl)
2009 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2012 static int coda_ctrls_setup(struct coda_ctx *ctx)
2014 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2016 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2017 V4L2_CID_HFLIP, 0, 1, 1, 0);
2018 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2019 V4L2_CID_VFLIP, 0, 1, 1, 0);
2020 if (ctx->inst_type == CODA_INST_ENCODER) {
2021 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2022 coda_jpeg_encode_ctrls(ctx);
2023 else
2024 coda_encode_ctrls(ctx);
2025 } else {
2026 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2027 coda_decode_ctrls(ctx);
2030 if (ctx->ctrls.error) {
2031 v4l2_err(&ctx->dev->v4l2_dev,
2032 "control initialization error (%d)",
2033 ctx->ctrls.error);
2034 return -EINVAL;
2037 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2040 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2042 vq->drv_priv = ctx;
2043 vq->ops = &coda_qops;
2044 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2045 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2046 vq->lock = &ctx->dev->dev_mutex;
2047 /* One way to indicate end-of-stream for coda is to set the
2048 * bytesused == 0. However by default videobuf2 handles bytesused
2049 * equal to 0 as a special case and changes its value to the size
2050 * of the buffer. Set the allow_zero_bytesused flag, so
2051 * that videobuf2 will keep the value of bytesused intact.
2053 vq->allow_zero_bytesused = 1;
2055 * We might be fine with no buffers on some of the queues, but that
2056 * would need to be reflected in job_ready(). Currently we expect all
2057 * queues to have at least one buffer queued.
2059 vq->min_buffers_needed = 1;
2060 vq->dev = &ctx->dev->plat_dev->dev;
2062 return vb2_queue_init(vq);
2065 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2066 struct vb2_queue *dst_vq)
2068 int ret;
2070 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2071 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2072 src_vq->mem_ops = &vb2_dma_contig_memops;
2074 ret = coda_queue_init(priv, src_vq);
2075 if (ret)
2076 return ret;
2078 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2079 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2080 dst_vq->mem_ops = &vb2_dma_contig_memops;
2082 return coda_queue_init(priv, dst_vq);
2085 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2086 struct vb2_queue *dst_vq)
2088 int ret;
2090 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2091 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2092 src_vq->mem_ops = &vb2_vmalloc_memops;
2094 ret = coda_queue_init(priv, src_vq);
2095 if (ret)
2096 return ret;
2098 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2099 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2100 dst_vq->mem_ops = &vb2_dma_contig_memops;
2102 return coda_queue_init(priv, dst_vq);
2106 * File operations
2109 static int coda_open(struct file *file)
2111 struct video_device *vdev = video_devdata(file);
2112 struct coda_dev *dev = video_get_drvdata(vdev);
2113 struct coda_ctx *ctx;
2114 unsigned int max = ~0;
2115 char *name;
2116 int ret;
2117 int idx;
2119 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2120 if (!ctx)
2121 return -ENOMEM;
2123 if (dev->devtype->product == CODA_DX6)
2124 max = CODADX6_MAX_INSTANCES - 1;
2125 idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2126 if (idx < 0) {
2127 ret = idx;
2128 goto err_coda_max;
2131 name = kasprintf(GFP_KERNEL, "context%d", idx);
2132 if (!name) {
2133 ret = -ENOMEM;
2134 goto err_coda_name_init;
2137 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2138 kfree(name);
2140 ctx->cvd = to_coda_video_device(vdev);
2141 ctx->inst_type = ctx->cvd->type;
2142 ctx->ops = ctx->cvd->ops;
2143 ctx->use_bit = !ctx->cvd->direct;
2144 init_completion(&ctx->completion);
2145 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2146 if (ctx->ops->seq_end_work)
2147 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2148 v4l2_fh_init(&ctx->fh, video_devdata(file));
2149 file->private_data = &ctx->fh;
2150 v4l2_fh_add(&ctx->fh);
2151 ctx->dev = dev;
2152 ctx->idx = idx;
2153 switch (dev->devtype->product) {
2154 case CODA_960:
2156 * Enabling the BWB when decoding can hang the firmware with
2157 * certain streams. The issue was tracked as ENGR00293425 by
2158 * Freescale. As a workaround, disable BWB for all decoders.
2159 * The enable_bwb module parameter allows to override this.
2161 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2162 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2163 /* fallthrough */
2164 case CODA_HX4:
2165 case CODA_7541:
2166 ctx->reg_idx = 0;
2167 break;
2168 default:
2169 ctx->reg_idx = idx;
2171 if (ctx->dev->vdoa && !disable_vdoa) {
2172 ctx->vdoa = vdoa_context_create(dev->vdoa);
2173 if (!ctx->vdoa)
2174 v4l2_warn(&dev->v4l2_dev,
2175 "Failed to create vdoa context: not using vdoa");
2177 ctx->use_vdoa = false;
2179 /* Power up and upload firmware if necessary */
2180 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2181 if (ret < 0) {
2182 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2183 goto err_pm_get;
2186 ret = clk_prepare_enable(dev->clk_per);
2187 if (ret)
2188 goto err_clk_per;
2190 ret = clk_prepare_enable(dev->clk_ahb);
2191 if (ret)
2192 goto err_clk_ahb;
2194 set_default_params(ctx);
2195 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2196 ctx->ops->queue_init);
2197 if (IS_ERR(ctx->fh.m2m_ctx)) {
2198 ret = PTR_ERR(ctx->fh.m2m_ctx);
2200 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2201 __func__, ret);
2202 goto err_ctx_init;
2205 ret = coda_ctrls_setup(ctx);
2206 if (ret) {
2207 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2208 goto err_ctrls_setup;
2211 ctx->fh.ctrl_handler = &ctx->ctrls;
2213 mutex_init(&ctx->bitstream_mutex);
2214 mutex_init(&ctx->buffer_mutex);
2215 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2216 spin_lock_init(&ctx->buffer_meta_lock);
2218 mutex_lock(&dev->dev_mutex);
2219 list_add(&ctx->list, &dev->instances);
2220 mutex_unlock(&dev->dev_mutex);
2222 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2223 ctx->idx, ctx);
2225 return 0;
2227 err_ctrls_setup:
2228 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2229 err_ctx_init:
2230 clk_disable_unprepare(dev->clk_ahb);
2231 err_clk_ahb:
2232 clk_disable_unprepare(dev->clk_per);
2233 err_clk_per:
2234 pm_runtime_put_sync(&dev->plat_dev->dev);
2235 err_pm_get:
2236 v4l2_fh_del(&ctx->fh);
2237 v4l2_fh_exit(&ctx->fh);
2238 err_coda_name_init:
2239 ida_free(&dev->ida, ctx->idx);
2240 err_coda_max:
2241 kfree(ctx);
2242 return ret;
2245 static int coda_release(struct file *file)
2247 struct coda_dev *dev = video_drvdata(file);
2248 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2250 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2251 ctx);
2253 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2254 coda_bit_stream_end_flag(ctx);
2256 /* If this instance is running, call .job_abort and wait for it to end */
2257 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2259 if (ctx->vdoa)
2260 vdoa_context_destroy(ctx->vdoa);
2262 /* In case the instance was not running, we still need to call SEQ_END */
2263 if (ctx->ops->seq_end_work) {
2264 queue_work(dev->workqueue, &ctx->seq_end_work);
2265 flush_work(&ctx->seq_end_work);
2268 mutex_lock(&dev->dev_mutex);
2269 list_del(&ctx->list);
2270 mutex_unlock(&dev->dev_mutex);
2272 if (ctx->dev->devtype->product == CODA_DX6)
2273 coda_free_aux_buf(dev, &ctx->workbuf);
2275 v4l2_ctrl_handler_free(&ctx->ctrls);
2276 clk_disable_unprepare(dev->clk_ahb);
2277 clk_disable_unprepare(dev->clk_per);
2278 pm_runtime_put_sync(&dev->plat_dev->dev);
2279 v4l2_fh_del(&ctx->fh);
2280 v4l2_fh_exit(&ctx->fh);
2281 ida_free(&dev->ida, ctx->idx);
2282 if (ctx->ops->release)
2283 ctx->ops->release(ctx);
2284 debugfs_remove_recursive(ctx->debugfs_entry);
2285 kfree(ctx);
2287 return 0;
2290 static const struct v4l2_file_operations coda_fops = {
2291 .owner = THIS_MODULE,
2292 .open = coda_open,
2293 .release = coda_release,
2294 .poll = v4l2_m2m_fop_poll,
2295 .unlocked_ioctl = video_ioctl2,
2296 .mmap = v4l2_m2m_fop_mmap,
2299 static int coda_hw_init(struct coda_dev *dev)
2301 u32 data;
2302 u16 *p;
2303 int i, ret;
2305 ret = clk_prepare_enable(dev->clk_per);
2306 if (ret)
2307 goto err_clk_per;
2309 ret = clk_prepare_enable(dev->clk_ahb);
2310 if (ret)
2311 goto err_clk_ahb;
2313 reset_control_reset(dev->rstc);
2316 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2317 * The 16-bit chars in the code buffer are in memory access
2318 * order, re-sort them to CODA order for register download.
2319 * Data in this SRAM survives a reboot.
2321 p = (u16 *)dev->codebuf.vaddr;
2322 if (dev->devtype->product == CODA_DX6) {
2323 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2324 data = CODA_DOWN_ADDRESS_SET(i) |
2325 CODA_DOWN_DATA_SET(p[i ^ 1]);
2326 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2328 } else {
2329 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2330 data = CODA_DOWN_ADDRESS_SET(i) |
2331 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2332 3 - (i % 4)]);
2333 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2337 /* Clear registers */
2338 for (i = 0; i < 64; i++)
2339 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2341 /* Tell the BIT where to find everything it needs */
2342 if (dev->devtype->product == CODA_960 ||
2343 dev->devtype->product == CODA_7541 ||
2344 dev->devtype->product == CODA_HX4) {
2345 coda_write(dev, dev->tempbuf.paddr,
2346 CODA_REG_BIT_TEMP_BUF_ADDR);
2347 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2348 } else {
2349 coda_write(dev, dev->workbuf.paddr,
2350 CODA_REG_BIT_WORK_BUF_ADDR);
2352 coda_write(dev, dev->codebuf.paddr,
2353 CODA_REG_BIT_CODE_BUF_ADDR);
2354 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2356 /* Set default values */
2357 switch (dev->devtype->product) {
2358 case CODA_DX6:
2359 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2360 CODA_REG_BIT_STREAM_CTRL);
2361 break;
2362 default:
2363 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2364 CODA_REG_BIT_STREAM_CTRL);
2366 if (dev->devtype->product == CODA_960)
2367 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2368 CODA_REG_BIT_FRAME_MEM_CTRL);
2369 else
2370 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2372 if (dev->devtype->product != CODA_DX6)
2373 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2375 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2376 CODA_REG_BIT_INT_ENABLE);
2378 /* Reset VPU and start processor */
2379 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2380 data |= CODA_REG_RESET_ENABLE;
2381 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2382 udelay(10);
2383 data &= ~CODA_REG_RESET_ENABLE;
2384 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2385 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2387 clk_disable_unprepare(dev->clk_ahb);
2388 clk_disable_unprepare(dev->clk_per);
2390 return 0;
2392 err_clk_ahb:
2393 clk_disable_unprepare(dev->clk_per);
2394 err_clk_per:
2395 return ret;
2398 static int coda_register_device(struct coda_dev *dev, int i)
2400 struct video_device *vfd = &dev->vfd[i];
2402 if (i >= dev->devtype->num_vdevs)
2403 return -EINVAL;
2405 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2406 vfd->fops = &coda_fops;
2407 vfd->ioctl_ops = &coda_ioctl_ops;
2408 vfd->release = video_device_release_empty,
2409 vfd->lock = &dev->dev_mutex;
2410 vfd->v4l2_dev = &dev->v4l2_dev;
2411 vfd->vfl_dir = VFL_DIR_M2M;
2412 video_set_drvdata(vfd, dev);
2414 /* Not applicable, use the selection API instead */
2415 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2416 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2417 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2419 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2422 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2423 size_t size)
2425 u32 *src = (u32 *)buf;
2427 /* Check if the firmware has a 16-byte Freescale header, skip it */
2428 if (buf[0] == 'M' && buf[1] == 'X')
2429 src += 4;
2431 * Check whether the firmware is in native order or pre-reordered for
2432 * memory access. The first instruction opcode always is 0xe40e.
2434 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2435 u32 *dst = dev->codebuf.vaddr;
2436 int i;
2438 /* Firmware in native order, reorder while copying */
2439 if (dev->devtype->product == CODA_DX6) {
2440 for (i = 0; i < (size - 16) / 4; i++)
2441 dst[i] = (src[i] << 16) | (src[i] >> 16);
2442 } else {
2443 for (i = 0; i < (size - 16) / 4; i += 2) {
2444 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2445 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2448 } else {
2449 /* Copy the already reordered firmware image */
2450 memcpy(dev->codebuf.vaddr, src, size);
2454 static void coda_fw_callback(const struct firmware *fw, void *context);
2456 static int coda_firmware_request(struct coda_dev *dev)
2458 char *fw;
2460 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2461 return -EINVAL;
2463 fw = dev->devtype->firmware[dev->firmware];
2465 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2466 coda_product_name(dev->devtype->product));
2468 return request_firmware_nowait(THIS_MODULE, true, fw,
2469 &dev->plat_dev->dev, GFP_KERNEL, dev,
2470 coda_fw_callback);
2473 static void coda_fw_callback(const struct firmware *fw, void *context)
2475 struct coda_dev *dev = context;
2476 struct platform_device *pdev = dev->plat_dev;
2477 int i, ret;
2479 if (!fw) {
2480 dev->firmware++;
2481 ret = coda_firmware_request(dev);
2482 if (ret < 0) {
2483 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2484 goto put_pm;
2486 return;
2488 if (dev->firmware > 0) {
2490 * Since we can't suppress warnings for failed asynchronous
2491 * firmware requests, report that the fallback firmware was
2492 * found.
2494 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2495 dev->devtype->firmware[dev->firmware]);
2498 /* allocate auxiliary per-device code buffer for the BIT processor */
2499 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2500 dev->debugfs_root);
2501 if (ret < 0)
2502 goto put_pm;
2504 coda_copy_firmware(dev, fw->data, fw->size);
2505 release_firmware(fw);
2507 ret = coda_hw_init(dev);
2508 if (ret < 0) {
2509 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2510 goto put_pm;
2513 ret = coda_check_firmware(dev);
2514 if (ret < 0)
2515 goto put_pm;
2517 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2518 if (IS_ERR(dev->m2m_dev)) {
2519 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2520 goto put_pm;
2523 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2524 ret = coda_register_device(dev, i);
2525 if (ret) {
2526 v4l2_err(&dev->v4l2_dev,
2527 "Failed to register %s video device: %d\n",
2528 dev->devtype->vdevs[i]->name, ret);
2529 goto rel_vfd;
2533 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
2534 dev->vfd[0].num, dev->vfd[i - 1].num);
2536 pm_runtime_put_sync(&pdev->dev);
2537 return;
2539 rel_vfd:
2540 while (--i >= 0)
2541 video_unregister_device(&dev->vfd[i]);
2542 v4l2_m2m_release(dev->m2m_dev);
2543 put_pm:
2544 pm_runtime_put_sync(&pdev->dev);
2547 enum coda_platform {
2548 CODA_IMX27,
2549 CODA_IMX51,
2550 CODA_IMX53,
2551 CODA_IMX6Q,
2552 CODA_IMX6DL,
2555 static const struct coda_devtype coda_devdata[] = {
2556 [CODA_IMX27] = {
2557 .firmware = {
2558 "vpu_fw_imx27_TO2.bin",
2559 "vpu/vpu_fw_imx27_TO2.bin",
2560 "v4l-codadx6-imx27.bin"
2562 .product = CODA_DX6,
2563 .codecs = codadx6_codecs,
2564 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2565 .vdevs = codadx6_video_devices,
2566 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2567 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2568 .iram_size = 0xb000,
2570 [CODA_IMX51] = {
2571 .firmware = {
2572 "vpu_fw_imx51.bin",
2573 "vpu/vpu_fw_imx51.bin",
2574 "v4l-codahx4-imx51.bin"
2576 .product = CODA_HX4,
2577 .codecs = codahx4_codecs,
2578 .num_codecs = ARRAY_SIZE(codahx4_codecs),
2579 .vdevs = codahx4_video_devices,
2580 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
2581 .workbuf_size = 128 * 1024,
2582 .tempbuf_size = 304 * 1024,
2583 .iram_size = 0x14000,
2585 [CODA_IMX53] = {
2586 .firmware = {
2587 "vpu_fw_imx53.bin",
2588 "vpu/vpu_fw_imx53.bin",
2589 "v4l-coda7541-imx53.bin"
2591 .product = CODA_7541,
2592 .codecs = coda7_codecs,
2593 .num_codecs = ARRAY_SIZE(coda7_codecs),
2594 .vdevs = coda7_video_devices,
2595 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2596 .workbuf_size = 128 * 1024,
2597 .tempbuf_size = 304 * 1024,
2598 .iram_size = 0x14000,
2600 [CODA_IMX6Q] = {
2601 .firmware = {
2602 "vpu_fw_imx6q.bin",
2603 "vpu/vpu_fw_imx6q.bin",
2604 "v4l-coda960-imx6q.bin"
2606 .product = CODA_960,
2607 .codecs = coda9_codecs,
2608 .num_codecs = ARRAY_SIZE(coda9_codecs),
2609 .vdevs = coda9_video_devices,
2610 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2611 .workbuf_size = 80 * 1024,
2612 .tempbuf_size = 204 * 1024,
2613 .iram_size = 0x21000,
2615 [CODA_IMX6DL] = {
2616 .firmware = {
2617 "vpu_fw_imx6d.bin",
2618 "vpu/vpu_fw_imx6d.bin",
2619 "v4l-coda960-imx6dl.bin"
2621 .product = CODA_960,
2622 .codecs = coda9_codecs,
2623 .num_codecs = ARRAY_SIZE(coda9_codecs),
2624 .vdevs = coda9_video_devices,
2625 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2626 .workbuf_size = 80 * 1024,
2627 .tempbuf_size = 204 * 1024,
2628 .iram_size = 0x1f000, /* leave 4k for suspend code */
2632 static const struct platform_device_id coda_platform_ids[] = {
2633 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2634 { /* sentinel */ }
2636 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2638 #ifdef CONFIG_OF
2639 static const struct of_device_id coda_dt_ids[] = {
2640 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2641 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
2642 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2643 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2644 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2645 { /* sentinel */ }
2647 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2648 #endif
2650 static int coda_probe(struct platform_device *pdev)
2652 const struct of_device_id *of_id =
2653 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2654 const struct platform_device_id *pdev_id;
2655 struct coda_platform_data *pdata = pdev->dev.platform_data;
2656 struct device_node *np = pdev->dev.of_node;
2657 struct gen_pool *pool;
2658 struct coda_dev *dev;
2659 struct resource *res;
2660 int ret, irq;
2662 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2663 if (!dev)
2664 return -ENOMEM;
2666 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2668 if (of_id)
2669 dev->devtype = of_id->data;
2670 else if (pdev_id)
2671 dev->devtype = &coda_devdata[pdev_id->driver_data];
2672 else
2673 return -EINVAL;
2675 spin_lock_init(&dev->irqlock);
2676 INIT_LIST_HEAD(&dev->instances);
2678 dev->plat_dev = pdev;
2679 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2680 if (IS_ERR(dev->clk_per)) {
2681 dev_err(&pdev->dev, "Could not get per clock\n");
2682 return PTR_ERR(dev->clk_per);
2685 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2686 if (IS_ERR(dev->clk_ahb)) {
2687 dev_err(&pdev->dev, "Could not get ahb clock\n");
2688 return PTR_ERR(dev->clk_ahb);
2691 /* Get memory for physical registers */
2692 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2693 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2694 if (IS_ERR(dev->regs_base))
2695 return PTR_ERR(dev->regs_base);
2697 /* IRQ */
2698 irq = platform_get_irq_byname(pdev, "bit");
2699 if (irq < 0)
2700 irq = platform_get_irq(pdev, 0);
2701 if (irq < 0) {
2702 dev_err(&pdev->dev, "failed to get irq resource\n");
2703 return irq;
2706 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2707 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2708 if (ret < 0) {
2709 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2710 return ret;
2713 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
2714 NULL);
2715 if (IS_ERR(dev->rstc)) {
2716 ret = PTR_ERR(dev->rstc);
2717 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
2718 return ret;
2721 /* Get IRAM pool from device tree or platform data */
2722 pool = of_gen_pool_get(np, "iram", 0);
2723 if (!pool && pdata)
2724 pool = gen_pool_get(pdata->iram_dev, NULL);
2725 if (!pool) {
2726 dev_err(&pdev->dev, "iram pool not available\n");
2727 return -ENOMEM;
2729 dev->iram_pool = pool;
2731 /* Get vdoa_data if supported by the platform */
2732 dev->vdoa = coda_get_vdoa_data();
2733 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
2734 return -EPROBE_DEFER;
2736 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2737 if (ret)
2738 return ret;
2740 mutex_init(&dev->dev_mutex);
2741 mutex_init(&dev->coda_mutex);
2742 ida_init(&dev->ida);
2744 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2745 if (!dev->debugfs_root)
2746 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2748 /* allocate auxiliary per-device buffers for the BIT processor */
2749 if (dev->devtype->product == CODA_DX6) {
2750 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2751 dev->devtype->workbuf_size, "workbuf",
2752 dev->debugfs_root);
2753 if (ret < 0)
2754 goto err_v4l2_register;
2757 if (dev->devtype->tempbuf_size) {
2758 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2759 dev->devtype->tempbuf_size, "tempbuf",
2760 dev->debugfs_root);
2761 if (ret < 0)
2762 goto err_v4l2_register;
2765 dev->iram.size = dev->devtype->iram_size;
2766 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2767 &dev->iram.paddr);
2768 if (!dev->iram.vaddr) {
2769 dev_warn(&pdev->dev, "unable to alloc iram\n");
2770 } else {
2771 memset(dev->iram.vaddr, 0, dev->iram.size);
2772 dev->iram.blob.data = dev->iram.vaddr;
2773 dev->iram.blob.size = dev->iram.size;
2774 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2775 dev->debugfs_root,
2776 &dev->iram.blob);
2779 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2780 if (!dev->workqueue) {
2781 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2782 ret = -ENOMEM;
2783 goto err_v4l2_register;
2786 platform_set_drvdata(pdev, dev);
2789 * Start activated so we can directly call coda_hw_init in
2790 * coda_fw_callback regardless of whether CONFIG_PM is
2791 * enabled or whether the device is associated with a PM domain.
2793 pm_runtime_get_noresume(&pdev->dev);
2794 pm_runtime_set_active(&pdev->dev);
2795 pm_runtime_enable(&pdev->dev);
2797 ret = coda_firmware_request(dev);
2798 if (ret)
2799 goto err_alloc_workqueue;
2800 return 0;
2802 err_alloc_workqueue:
2803 destroy_workqueue(dev->workqueue);
2804 err_v4l2_register:
2805 v4l2_device_unregister(&dev->v4l2_dev);
2806 return ret;
2809 static int coda_remove(struct platform_device *pdev)
2811 struct coda_dev *dev = platform_get_drvdata(pdev);
2812 int i;
2814 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2815 if (video_get_drvdata(&dev->vfd[i]))
2816 video_unregister_device(&dev->vfd[i]);
2818 if (dev->m2m_dev)
2819 v4l2_m2m_release(dev->m2m_dev);
2820 pm_runtime_disable(&pdev->dev);
2821 v4l2_device_unregister(&dev->v4l2_dev);
2822 destroy_workqueue(dev->workqueue);
2823 if (dev->iram.vaddr)
2824 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2825 dev->iram.size);
2826 coda_free_aux_buf(dev, &dev->codebuf);
2827 coda_free_aux_buf(dev, &dev->tempbuf);
2828 coda_free_aux_buf(dev, &dev->workbuf);
2829 debugfs_remove_recursive(dev->debugfs_root);
2830 ida_destroy(&dev->ida);
2831 return 0;
2834 #ifdef CONFIG_PM
2835 static int coda_runtime_resume(struct device *dev)
2837 struct coda_dev *cdev = dev_get_drvdata(dev);
2838 int ret = 0;
2840 if (dev->pm_domain && cdev->codebuf.vaddr) {
2841 ret = coda_hw_init(cdev);
2842 if (ret)
2843 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2846 return ret;
2848 #endif
2850 static const struct dev_pm_ops coda_pm_ops = {
2851 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2854 static struct platform_driver coda_driver = {
2855 .probe = coda_probe,
2856 .remove = coda_remove,
2857 .driver = {
2858 .name = CODA_NAME,
2859 .of_match_table = of_match_ptr(coda_dt_ids),
2860 .pm = &coda_pm_ops,
2862 .id_table = coda_platform_ids,
2865 module_platform_driver(coda_driver);
2867 MODULE_LICENSE("GPL");
2868 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2869 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");