treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / platform / coda / coda-common.c
blobacff10ad257a9707a9ed93b7c2d1c1cda678a847
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Coda multi-standard codec IP
5 * Copyright (C) 2012 Vista Silicon S.L.
6 * Javier Martin, <javier.martin@vista-silicon.com>
7 * Xavier Duret
8 */
10 #include <linux/clk.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/gcd.h>
15 #include <linux/genalloc.h>
16 #include <linux/idr.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/irq.h>
20 #include <linux/kfifo.h>
21 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
27 #include <linux/of.h>
28 #include <linux/platform_data/media/coda.h>
29 #include <linux/reset.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-device.h>
33 #include <media/v4l2-event.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-mem2mem.h>
36 #include <media/videobuf2-v4l2.h>
37 #include <media/videobuf2-dma-contig.h>
38 #include <media/videobuf2-vmalloc.h>
40 #include "coda.h"
41 #include "imx-vdoa.h"
43 #define CODA_NAME "coda"
45 #define CODADX6_MAX_INSTANCES 4
46 #define CODA_MAX_FORMATS 4
48 #define CODA_ISRAM_SIZE (2048 * 2)
50 #define MIN_W 48
51 #define MIN_H 16
53 #define S_ALIGN 1 /* multiple of 2 */
54 #define W_ALIGN 1 /* multiple of 2 */
55 #define H_ALIGN 1 /* multiple of 2 */
57 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
59 int coda_debug;
60 module_param(coda_debug, int, 0644);
61 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
63 static int disable_tiling;
64 module_param(disable_tiling, int, 0644);
65 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
67 static int disable_vdoa;
68 module_param(disable_vdoa, int, 0644);
69 MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion");
71 static int enable_bwb = 0;
72 module_param(enable_bwb, int, 0644);
73 MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams");
75 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
77 v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
78 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
79 writel(data, dev->regs_base + reg);
82 unsigned int coda_read(struct coda_dev *dev, u32 reg)
84 u32 data;
86 data = readl(dev->regs_base + reg);
87 v4l2_dbg(3, coda_debug, &dev->v4l2_dev,
88 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
89 return data;
92 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
93 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
95 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
96 u32 base_cb, base_cr;
98 switch (q_data->fourcc) {
99 case V4L2_PIX_FMT_YUYV:
100 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */
101 case V4L2_PIX_FMT_NV12:
102 case V4L2_PIX_FMT_YUV420:
103 default:
104 base_cb = base_y + q_data->bytesperline * q_data->height;
105 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
106 break;
107 case V4L2_PIX_FMT_YVU420:
108 /* Switch Cb and Cr for YVU420 format */
109 base_cr = base_y + q_data->bytesperline * q_data->height;
110 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
111 break;
112 case V4L2_PIX_FMT_YUV422P:
113 base_cb = base_y + q_data->bytesperline * q_data->height;
114 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
117 coda_write(ctx->dev, base_y, reg_y);
118 coda_write(ctx->dev, base_cb, reg_y + 4);
119 coda_write(ctx->dev, base_cr, reg_y + 8);
122 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
123 { mode, src_fourcc, dst_fourcc, max_w, max_h }
126 * Arrays of codecs supported by each given version of Coda:
127 * i.MX27 -> codadx6
128 * i.MX51 -> codahx4
129 * i.MX53 -> coda7
130 * i.MX6 -> coda960
131 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
133 static const struct coda_codec codadx6_codecs[] = {
134 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
135 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
138 static const struct coda_codec codahx4_codecs[] = {
139 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
140 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
141 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
142 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720),
145 static const struct coda_codec coda7_codecs[] = {
146 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
147 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
148 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
149 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
150 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
151 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
152 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
155 static const struct coda_codec coda9_codecs[] = {
156 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
157 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
158 CODA_CODEC(CODA9_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
159 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
160 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
161 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
164 struct coda_video_device {
165 const char *name;
166 enum coda_inst_type type;
167 const struct coda_context_ops *ops;
168 bool direct;
169 u32 src_formats[CODA_MAX_FORMATS];
170 u32 dst_formats[CODA_MAX_FORMATS];
173 static const struct coda_video_device coda_bit_encoder = {
174 .name = "coda-encoder",
175 .type = CODA_INST_ENCODER,
176 .ops = &coda_bit_encode_ops,
177 .src_formats = {
178 V4L2_PIX_FMT_NV12,
179 V4L2_PIX_FMT_YUV420,
180 V4L2_PIX_FMT_YVU420,
182 .dst_formats = {
183 V4L2_PIX_FMT_H264,
184 V4L2_PIX_FMT_MPEG4,
188 static const struct coda_video_device coda_bit_jpeg_encoder = {
189 .name = "coda-jpeg-encoder",
190 .type = CODA_INST_ENCODER,
191 .ops = &coda_bit_encode_ops,
192 .src_formats = {
193 V4L2_PIX_FMT_NV12,
194 V4L2_PIX_FMT_YUV420,
195 V4L2_PIX_FMT_YVU420,
196 V4L2_PIX_FMT_YUV422P,
198 .dst_formats = {
199 V4L2_PIX_FMT_JPEG,
203 static const struct coda_video_device coda_bit_decoder = {
204 .name = "coda-decoder",
205 .type = CODA_INST_DECODER,
206 .ops = &coda_bit_decode_ops,
207 .src_formats = {
208 V4L2_PIX_FMT_H264,
209 V4L2_PIX_FMT_MPEG2,
210 V4L2_PIX_FMT_MPEG4,
212 .dst_formats = {
213 V4L2_PIX_FMT_NV12,
214 V4L2_PIX_FMT_YUV420,
215 V4L2_PIX_FMT_YVU420,
217 * If V4L2_PIX_FMT_YUYV should be default,
218 * set_default_params() must be adjusted.
220 V4L2_PIX_FMT_YUYV,
224 static const struct coda_video_device coda_bit_jpeg_decoder = {
225 .name = "coda-jpeg-decoder",
226 .type = CODA_INST_DECODER,
227 .ops = &coda_bit_decode_ops,
228 .src_formats = {
229 V4L2_PIX_FMT_JPEG,
231 .dst_formats = {
232 V4L2_PIX_FMT_NV12,
233 V4L2_PIX_FMT_YUV420,
234 V4L2_PIX_FMT_YVU420,
235 V4L2_PIX_FMT_YUV422P,
239 static const struct coda_video_device coda9_jpeg_encoder = {
240 .name = "coda-jpeg-encoder",
241 .type = CODA_INST_ENCODER,
242 .ops = &coda9_jpeg_encode_ops,
243 .direct = true,
244 .src_formats = {
245 V4L2_PIX_FMT_NV12,
246 V4L2_PIX_FMT_YUV420,
247 V4L2_PIX_FMT_YVU420,
248 V4L2_PIX_FMT_YUV422P,
250 .dst_formats = {
251 V4L2_PIX_FMT_JPEG,
255 static const struct coda_video_device *codadx6_video_devices[] = {
256 &coda_bit_encoder,
259 static const struct coda_video_device *codahx4_video_devices[] = {
260 &coda_bit_encoder,
261 &coda_bit_decoder,
264 static const struct coda_video_device *coda7_video_devices[] = {
265 &coda_bit_jpeg_encoder,
266 &coda_bit_jpeg_decoder,
267 &coda_bit_encoder,
268 &coda_bit_decoder,
271 static const struct coda_video_device *coda9_video_devices[] = {
272 &coda9_jpeg_encoder,
273 &coda_bit_encoder,
274 &coda_bit_decoder,
278 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
279 * tables.
281 static u32 coda_format_normalize_yuv(u32 fourcc)
283 switch (fourcc) {
284 case V4L2_PIX_FMT_NV12:
285 case V4L2_PIX_FMT_YUV420:
286 case V4L2_PIX_FMT_YVU420:
287 case V4L2_PIX_FMT_YUV422P:
288 case V4L2_PIX_FMT_YUYV:
289 return V4L2_PIX_FMT_YUV420;
290 default:
291 return fourcc;
295 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
296 int src_fourcc, int dst_fourcc)
298 const struct coda_codec *codecs = dev->devtype->codecs;
299 int num_codecs = dev->devtype->num_codecs;
300 int k;
302 src_fourcc = coda_format_normalize_yuv(src_fourcc);
303 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
304 if (src_fourcc == dst_fourcc)
305 return NULL;
307 for (k = 0; k < num_codecs; k++) {
308 if (codecs[k].src_fourcc == src_fourcc &&
309 codecs[k].dst_fourcc == dst_fourcc)
310 break;
313 if (k == num_codecs)
314 return NULL;
316 return &codecs[k];
319 static void coda_get_max_dimensions(struct coda_dev *dev,
320 const struct coda_codec *codec,
321 int *max_w, int *max_h)
323 const struct coda_codec *codecs = dev->devtype->codecs;
324 int num_codecs = dev->devtype->num_codecs;
325 unsigned int w, h;
326 int k;
328 if (codec) {
329 w = codec->max_w;
330 h = codec->max_h;
331 } else {
332 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
333 w = max(w, codecs[k].max_w);
334 h = max(h, codecs[k].max_h);
338 if (max_w)
339 *max_w = w;
340 if (max_h)
341 *max_h = h;
344 static const struct coda_video_device *to_coda_video_device(struct video_device
345 *vdev)
347 struct coda_dev *dev = video_get_drvdata(vdev);
348 unsigned int i = vdev - dev->vfd;
350 if (i >= dev->devtype->num_vdevs)
351 return NULL;
353 return dev->devtype->vdevs[i];
356 const char *coda_product_name(int product)
358 static char buf[9];
360 switch (product) {
361 case CODA_DX6:
362 return "CodaDx6";
363 case CODA_HX4:
364 return "CodaHx4";
365 case CODA_7541:
366 return "CODA7541";
367 case CODA_960:
368 return "CODA960";
369 default:
370 snprintf(buf, sizeof(buf), "(0x%04x)", product);
371 return buf;
375 static struct vdoa_data *coda_get_vdoa_data(void)
377 struct device_node *vdoa_node;
378 struct platform_device *vdoa_pdev;
379 struct vdoa_data *vdoa_data = NULL;
381 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa");
382 if (!vdoa_node)
383 return NULL;
385 vdoa_pdev = of_find_device_by_node(vdoa_node);
386 if (!vdoa_pdev)
387 goto out;
389 vdoa_data = platform_get_drvdata(vdoa_pdev);
390 if (!vdoa_data)
391 vdoa_data = ERR_PTR(-EPROBE_DEFER);
393 out:
394 of_node_put(vdoa_node);
396 return vdoa_data;
400 * V4L2 ioctl() operations.
402 static int coda_querycap(struct file *file, void *priv,
403 struct v4l2_capability *cap)
405 struct coda_ctx *ctx = fh_to_ctx(priv);
407 strscpy(cap->driver, CODA_NAME, sizeof(cap->driver));
408 strscpy(cap->card, coda_product_name(ctx->dev->devtype->product),
409 sizeof(cap->card));
410 strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
411 return 0;
414 static int coda_enum_fmt(struct file *file, void *priv,
415 struct v4l2_fmtdesc *f)
417 struct video_device *vdev = video_devdata(file);
418 const struct coda_video_device *cvd = to_coda_video_device(vdev);
419 struct coda_ctx *ctx = fh_to_ctx(priv);
420 const u32 *formats;
422 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
423 formats = cvd->src_formats;
424 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
425 formats = cvd->dst_formats;
426 else
427 return -EINVAL;
429 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
430 return -EINVAL;
432 /* Skip YUYV if the vdoa is not available */
433 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
434 formats[f->index] == V4L2_PIX_FMT_YUYV)
435 return -EINVAL;
437 f->pixelformat = formats[f->index];
439 return 0;
442 static int coda_g_fmt(struct file *file, void *priv,
443 struct v4l2_format *f)
445 struct coda_q_data *q_data;
446 struct coda_ctx *ctx = fh_to_ctx(priv);
448 q_data = get_q_data(ctx, f->type);
449 if (!q_data)
450 return -EINVAL;
452 f->fmt.pix.field = V4L2_FIELD_NONE;
453 f->fmt.pix.pixelformat = q_data->fourcc;
454 f->fmt.pix.width = q_data->width;
455 f->fmt.pix.height = q_data->height;
456 f->fmt.pix.bytesperline = q_data->bytesperline;
458 f->fmt.pix.sizeimage = q_data->sizeimage;
459 f->fmt.pix.colorspace = ctx->colorspace;
460 f->fmt.pix.xfer_func = ctx->xfer_func;
461 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
462 f->fmt.pix.quantization = ctx->quantization;
464 return 0;
467 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
469 struct coda_q_data *q_data;
470 const u32 *formats;
471 int i;
473 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
474 formats = ctx->cvd->src_formats;
475 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
476 formats = ctx->cvd->dst_formats;
477 else
478 return -EINVAL;
480 for (i = 0; i < CODA_MAX_FORMATS; i++) {
481 /* Skip YUYV if the vdoa is not available */
482 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
483 formats[i] == V4L2_PIX_FMT_YUYV)
484 continue;
486 if (formats[i] == f->fmt.pix.pixelformat) {
487 f->fmt.pix.pixelformat = formats[i];
488 return 0;
492 /* Fall back to currently set pixelformat */
493 q_data = get_q_data(ctx, f->type);
494 f->fmt.pix.pixelformat = q_data->fourcc;
496 return 0;
499 static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f,
500 bool *use_vdoa)
502 int err;
504 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
505 return -EINVAL;
507 if (!use_vdoa)
508 return -EINVAL;
510 if (!ctx->vdoa) {
511 *use_vdoa = false;
512 return 0;
515 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16),
516 f->fmt.pix.height, f->fmt.pix.pixelformat);
517 if (err) {
518 *use_vdoa = false;
519 return 0;
522 *use_vdoa = true;
523 return 0;
526 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
527 u32 width, u32 height)
530 * This is a rough estimate for sensible compressed buffer
531 * sizes (between 1 and 16 bits per pixel). This could be
532 * improved by better format specific worst case estimates.
534 return round_up(clamp(sizeimage, width * height / 8,
535 width * height * 2), PAGE_SIZE);
538 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
539 struct v4l2_format *f)
541 struct coda_dev *dev = ctx->dev;
542 unsigned int max_w, max_h;
543 enum v4l2_field field;
545 field = f->fmt.pix.field;
546 if (field == V4L2_FIELD_ANY)
547 field = V4L2_FIELD_NONE;
548 else if (V4L2_FIELD_NONE != field)
549 return -EINVAL;
551 /* V4L2 specification suggests the driver corrects the format struct
552 * if any of the dimensions is unsupported */
553 f->fmt.pix.field = field;
555 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
556 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
557 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
558 S_ALIGN);
560 switch (f->fmt.pix.pixelformat) {
561 case V4L2_PIX_FMT_NV12:
562 case V4L2_PIX_FMT_YUV420:
563 case V4L2_PIX_FMT_YVU420:
565 * Frame stride must be at least multiple of 8,
566 * but multiple of 16 for h.264 or JPEG 4:2:x
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 * 3 / 2;
571 break;
572 case V4L2_PIX_FMT_YUYV:
573 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
574 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
575 f->fmt.pix.height;
576 break;
577 case V4L2_PIX_FMT_YUV422P:
578 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
579 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
580 f->fmt.pix.height * 2;
581 break;
582 case V4L2_PIX_FMT_JPEG:
583 case V4L2_PIX_FMT_H264:
584 case V4L2_PIX_FMT_MPEG4:
585 case V4L2_PIX_FMT_MPEG2:
586 f->fmt.pix.bytesperline = 0;
587 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
588 f->fmt.pix.sizeimage,
589 f->fmt.pix.width,
590 f->fmt.pix.height);
591 break;
592 default:
593 BUG();
596 return 0;
599 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
600 struct v4l2_format *f)
602 struct coda_ctx *ctx = fh_to_ctx(priv);
603 const struct coda_q_data *q_data_src;
604 const struct coda_codec *codec;
605 struct vb2_queue *src_vq;
606 int ret;
607 bool use_vdoa;
609 ret = coda_try_pixelformat(ctx, f);
610 if (ret < 0)
611 return ret;
613 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
616 * If the source format is already fixed, only allow the same output
617 * resolution
619 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
620 if (vb2_is_streaming(src_vq)) {
621 f->fmt.pix.width = q_data_src->width;
622 f->fmt.pix.height = q_data_src->height;
625 f->fmt.pix.colorspace = ctx->colorspace;
626 f->fmt.pix.xfer_func = ctx->xfer_func;
627 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
628 f->fmt.pix.quantization = ctx->quantization;
630 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
631 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
632 f->fmt.pix.pixelformat);
633 if (!codec)
634 return -EINVAL;
636 ret = coda_try_fmt(ctx, codec, f);
637 if (ret < 0)
638 return ret;
640 /* The h.264 decoder only returns complete 16x16 macroblocks */
641 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
642 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
643 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
644 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
645 f->fmt.pix.height * 3 / 2;
647 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa);
648 if (ret < 0)
649 return ret;
651 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
652 if (!use_vdoa)
653 return -EINVAL;
655 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2;
656 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
657 f->fmt.pix.height;
661 return 0;
664 static void coda_set_default_colorspace(struct v4l2_pix_format *fmt)
666 enum v4l2_colorspace colorspace;
668 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG)
669 colorspace = V4L2_COLORSPACE_JPEG;
670 else if (fmt->width <= 720 && fmt->height <= 576)
671 colorspace = V4L2_COLORSPACE_SMPTE170M;
672 else
673 colorspace = V4L2_COLORSPACE_REC709;
675 fmt->colorspace = colorspace;
676 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
677 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
678 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
681 static int coda_try_fmt_vid_out(struct file *file, void *priv,
682 struct v4l2_format *f)
684 struct coda_ctx *ctx = fh_to_ctx(priv);
685 struct coda_dev *dev = ctx->dev;
686 const struct coda_q_data *q_data_dst;
687 const struct coda_codec *codec;
688 int ret;
690 ret = coda_try_pixelformat(ctx, f);
691 if (ret < 0)
692 return ret;
694 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
695 coda_set_default_colorspace(&f->fmt.pix);
697 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
698 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
700 return coda_try_fmt(ctx, codec, f);
703 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f,
704 struct v4l2_rect *r)
706 struct coda_q_data *q_data;
707 struct vb2_queue *vq;
709 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
710 if (!vq)
711 return -EINVAL;
713 q_data = get_q_data(ctx, f->type);
714 if (!q_data)
715 return -EINVAL;
717 if (vb2_is_busy(vq)) {
718 v4l2_err(&ctx->dev->v4l2_dev, "%s: %s queue busy: %d\n",
719 __func__, v4l2_type_names[f->type], vq->num_buffers);
720 return -EBUSY;
723 q_data->fourcc = f->fmt.pix.pixelformat;
724 q_data->width = f->fmt.pix.width;
725 q_data->height = f->fmt.pix.height;
726 q_data->bytesperline = f->fmt.pix.bytesperline;
727 q_data->sizeimage = f->fmt.pix.sizeimage;
728 if (r) {
729 q_data->rect = *r;
730 } else {
731 q_data->rect.left = 0;
732 q_data->rect.top = 0;
733 q_data->rect.width = f->fmt.pix.width;
734 q_data->rect.height = f->fmt.pix.height;
737 switch (f->fmt.pix.pixelformat) {
738 case V4L2_PIX_FMT_YUYV:
739 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
740 break;
741 case V4L2_PIX_FMT_NV12:
742 if (!disable_tiling && ctx->use_bit &&
743 ctx->dev->devtype->product == CODA_960) {
744 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
745 break;
747 /* else fall through */
748 case V4L2_PIX_FMT_YUV420:
749 case V4L2_PIX_FMT_YVU420:
750 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
751 break;
752 default:
753 break;
756 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP &&
757 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) &&
758 ctx->use_vdoa)
759 vdoa_context_configure(ctx->vdoa,
760 round_up(f->fmt.pix.width, 16),
761 f->fmt.pix.height,
762 f->fmt.pix.pixelformat);
763 else
764 ctx->use_vdoa = false;
766 coda_dbg(1, ctx, "Setting %s format, wxh: %dx%d, fmt: %4.4s %c\n",
767 v4l2_type_names[f->type], q_data->width, q_data->height,
768 (char *)&q_data->fourcc,
769 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T');
771 return 0;
774 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
775 struct v4l2_format *f)
777 struct coda_ctx *ctx = fh_to_ctx(priv);
778 struct coda_q_data *q_data_src;
779 const struct coda_codec *codec;
780 struct v4l2_rect r;
781 int ret;
783 ret = coda_try_fmt_vid_cap(file, priv, f);
784 if (ret)
785 return ret;
787 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
788 r.left = 0;
789 r.top = 0;
790 r.width = q_data_src->width;
791 r.height = q_data_src->height;
793 ret = coda_s_fmt(ctx, f, &r);
794 if (ret)
795 return ret;
797 if (ctx->inst_type != CODA_INST_ENCODER)
798 return 0;
800 /* Setting the coded format determines the selected codec */
801 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
802 f->fmt.pix.pixelformat);
803 if (!codec) {
804 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
805 return -EINVAL;
807 ctx->codec = codec;
809 ctx->colorspace = f->fmt.pix.colorspace;
810 ctx->xfer_func = f->fmt.pix.xfer_func;
811 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
812 ctx->quantization = f->fmt.pix.quantization;
814 return 0;
817 static int coda_s_fmt_vid_out(struct file *file, void *priv,
818 struct v4l2_format *f)
820 struct coda_ctx *ctx = fh_to_ctx(priv);
821 const struct coda_codec *codec;
822 struct v4l2_format f_cap;
823 struct vb2_queue *dst_vq;
824 int ret;
826 ret = coda_try_fmt_vid_out(file, priv, f);
827 if (ret)
828 return ret;
830 ret = coda_s_fmt(ctx, f, NULL);
831 if (ret)
832 return ret;
834 ctx->colorspace = f->fmt.pix.colorspace;
835 ctx->xfer_func = f->fmt.pix.xfer_func;
836 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
837 ctx->quantization = f->fmt.pix.quantization;
839 if (ctx->inst_type != CODA_INST_DECODER)
840 return 0;
842 /* Setting the coded format determines the selected codec */
843 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
844 V4L2_PIX_FMT_YUV420);
845 if (!codec) {
846 v4l2_err(&ctx->dev->v4l2_dev, "failed to determine codec\n");
847 return -EINVAL;
849 ctx->codec = codec;
851 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
852 if (!dst_vq)
853 return -EINVAL;
856 * Setting the capture queue format is not possible while the capture
857 * queue is still busy. This is not an error, but the user will have to
858 * make sure themselves that the capture format is set correctly before
859 * starting the output queue again.
861 if (vb2_is_busy(dst_vq))
862 return 0;
864 memset(&f_cap, 0, sizeof(f_cap));
865 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
866 coda_g_fmt(file, priv, &f_cap);
867 f_cap.fmt.pix.width = f->fmt.pix.width;
868 f_cap.fmt.pix.height = f->fmt.pix.height;
870 return coda_s_fmt_vid_cap(file, priv, &f_cap);
873 static int coda_reqbufs(struct file *file, void *priv,
874 struct v4l2_requestbuffers *rb)
876 struct coda_ctx *ctx = fh_to_ctx(priv);
877 int ret;
879 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
880 if (ret)
881 return ret;
884 * Allow to allocate instance specific per-context buffers, such as
885 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
887 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
888 return ctx->ops->reqbufs(ctx, rb);
890 return 0;
893 static int coda_qbuf(struct file *file, void *priv,
894 struct v4l2_buffer *buf)
896 struct coda_ctx *ctx = fh_to_ctx(priv);
898 if (ctx->inst_type == CODA_INST_DECODER &&
899 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
900 buf->flags &= ~V4L2_BUF_FLAG_LAST;
902 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
905 static int coda_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
907 struct coda_ctx *ctx = fh_to_ctx(priv);
908 int ret;
910 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
912 if (ctx->inst_type == CODA_INST_DECODER &&
913 buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
914 buf->flags &= ~V4L2_BUF_FLAG_LAST;
916 return ret;
919 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
920 enum vb2_buffer_state state)
922 const struct v4l2_event eos_event = {
923 .type = V4L2_EVENT_EOS
926 if (buf->flags & V4L2_BUF_FLAG_LAST)
927 v4l2_event_queue_fh(&ctx->fh, &eos_event);
929 v4l2_m2m_buf_done(buf, state);
932 static int coda_g_selection(struct file *file, void *fh,
933 struct v4l2_selection *s)
935 struct coda_ctx *ctx = fh_to_ctx(fh);
936 struct coda_q_data *q_data;
937 struct v4l2_rect r, *rsel;
939 q_data = get_q_data(ctx, s->type);
940 if (!q_data)
941 return -EINVAL;
943 r.left = 0;
944 r.top = 0;
945 r.width = q_data->width;
946 r.height = q_data->height;
947 rsel = &q_data->rect;
949 switch (s->target) {
950 case V4L2_SEL_TGT_CROP_DEFAULT:
951 case V4L2_SEL_TGT_CROP_BOUNDS:
952 rsel = &r;
953 /* fallthrough */
954 case V4L2_SEL_TGT_CROP:
955 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
956 ctx->inst_type == CODA_INST_DECODER)
957 return -EINVAL;
958 break;
959 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
960 case V4L2_SEL_TGT_COMPOSE_PADDED:
961 rsel = &r;
962 /* fallthrough */
963 case V4L2_SEL_TGT_COMPOSE:
964 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
965 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
966 ctx->inst_type == CODA_INST_ENCODER)
967 return -EINVAL;
968 break;
969 default:
970 return -EINVAL;
973 s->r = *rsel;
975 return 0;
978 static int coda_s_selection(struct file *file, void *fh,
979 struct v4l2_selection *s)
981 struct coda_ctx *ctx = fh_to_ctx(fh);
982 struct coda_q_data *q_data;
984 switch (s->target) {
985 case V4L2_SEL_TGT_CROP:
986 if (ctx->inst_type == CODA_INST_ENCODER &&
987 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
988 q_data = get_q_data(ctx, s->type);
989 if (!q_data)
990 return -EINVAL;
992 s->r.left = 0;
993 s->r.top = 0;
994 s->r.width = clamp(s->r.width, 2U, q_data->width);
995 s->r.height = clamp(s->r.height, 2U, q_data->height);
997 if (s->flags & V4L2_SEL_FLAG_LE) {
998 s->r.width = round_up(s->r.width, 2);
999 s->r.height = round_up(s->r.height, 2);
1000 } else {
1001 s->r.width = round_down(s->r.width, 2);
1002 s->r.height = round_down(s->r.height, 2);
1005 q_data->rect = s->r;
1007 coda_dbg(1, ctx, "Setting crop rectangle: %dx%d\n",
1008 s->r.width, s->r.height);
1010 return 0;
1012 /* else fall through */
1013 case V4L2_SEL_TGT_NATIVE_SIZE:
1014 case V4L2_SEL_TGT_COMPOSE:
1015 return coda_g_selection(file, fh, s);
1016 default:
1017 /* v4l2-compliance expects this to fail for read-only targets */
1018 return -EINVAL;
1022 static int coda_try_encoder_cmd(struct file *file, void *fh,
1023 struct v4l2_encoder_cmd *ec)
1025 struct coda_ctx *ctx = fh_to_ctx(fh);
1027 if (ctx->inst_type != CODA_INST_ENCODER)
1028 return -ENOTTY;
1030 return v4l2_m2m_ioctl_try_encoder_cmd(file, fh, ec);
1033 static void coda_wake_up_capture_queue(struct coda_ctx *ctx)
1035 struct vb2_queue *dst_vq;
1037 coda_dbg(1, ctx, "waking up capture queue\n");
1039 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1040 dst_vq->last_buffer_dequeued = true;
1041 wake_up(&dst_vq->done_wq);
1044 static int coda_encoder_cmd(struct file *file, void *fh,
1045 struct v4l2_encoder_cmd *ec)
1047 struct coda_ctx *ctx = fh_to_ctx(fh);
1048 struct vb2_v4l2_buffer *buf;
1049 int ret;
1051 ret = coda_try_encoder_cmd(file, fh, ec);
1052 if (ret < 0)
1053 return ret;
1055 mutex_lock(&ctx->wakeup_mutex);
1056 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1057 if (buf) {
1059 * If the last output buffer is still on the queue, make sure
1060 * that decoder finish_run will see the last flag and report it
1061 * to userspace.
1063 buf->flags |= V4L2_BUF_FLAG_LAST;
1064 } else {
1065 /* Set the stream-end flag on this context */
1066 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1069 * If the last output buffer has already been taken from the
1070 * queue, wake up the capture queue and signal end of stream
1071 * via the -EPIPE mechanism.
1073 coda_wake_up_capture_queue(ctx);
1075 mutex_unlock(&ctx->wakeup_mutex);
1077 return 0;
1080 static int coda_try_decoder_cmd(struct file *file, void *fh,
1081 struct v4l2_decoder_cmd *dc)
1083 struct coda_ctx *ctx = fh_to_ctx(fh);
1085 if (ctx->inst_type != CODA_INST_DECODER)
1086 return -ENOTTY;
1088 return v4l2_m2m_ioctl_try_decoder_cmd(file, fh, dc);
1091 static int coda_decoder_cmd(struct file *file, void *fh,
1092 struct v4l2_decoder_cmd *dc)
1094 struct coda_ctx *ctx = fh_to_ctx(fh);
1095 struct coda_dev *dev = ctx->dev;
1096 struct vb2_v4l2_buffer *buf;
1097 struct vb2_queue *dst_vq;
1098 bool stream_end;
1099 bool wakeup;
1100 int ret;
1102 ret = coda_try_decoder_cmd(file, fh, dc);
1103 if (ret < 0)
1104 return ret;
1106 switch (dc->cmd) {
1107 case V4L2_DEC_CMD_START:
1108 mutex_lock(&dev->coda_mutex);
1109 mutex_lock(&ctx->bitstream_mutex);
1110 coda_bitstream_flush(ctx);
1111 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1112 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1113 vb2_clear_last_buffer_dequeued(dst_vq);
1114 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1115 coda_fill_bitstream(ctx, NULL);
1116 mutex_unlock(&ctx->bitstream_mutex);
1117 mutex_unlock(&dev->coda_mutex);
1118 break;
1119 case V4L2_DEC_CMD_STOP:
1120 stream_end = false;
1121 wakeup = false;
1123 buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
1124 if (buf) {
1125 coda_dbg(1, ctx, "marking last pending buffer\n");
1127 /* Mark last buffer */
1128 buf->flags |= V4L2_BUF_FLAG_LAST;
1130 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) == 0) {
1131 coda_dbg(1, ctx, "all remaining buffers queued\n");
1132 stream_end = true;
1134 } else {
1135 coda_dbg(1, ctx, "marking last meta\n");
1137 /* Mark last meta */
1138 spin_lock(&ctx->buffer_meta_lock);
1139 if (!list_empty(&ctx->buffer_meta_list)) {
1140 struct coda_buffer_meta *meta;
1142 meta = list_last_entry(&ctx->buffer_meta_list,
1143 struct coda_buffer_meta,
1144 list);
1145 meta->last = true;
1146 stream_end = true;
1147 } else {
1148 wakeup = true;
1150 spin_unlock(&ctx->buffer_meta_lock);
1153 if (stream_end) {
1154 coda_dbg(1, ctx, "all remaining buffers queued\n");
1156 /* Set the stream-end flag on this context */
1157 coda_bit_stream_end_flag(ctx);
1158 ctx->hold = false;
1159 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
1162 if (wakeup) {
1163 /* If there is no buffer in flight, wake up */
1164 coda_wake_up_capture_queue(ctx);
1167 break;
1168 default:
1169 return -EINVAL;
1172 return 0;
1175 static int coda_enum_framesizes(struct file *file, void *fh,
1176 struct v4l2_frmsizeenum *fsize)
1178 struct coda_ctx *ctx = fh_to_ctx(fh);
1179 struct coda_q_data *q_data_dst;
1180 const struct coda_codec *codec;
1182 if (ctx->inst_type != CODA_INST_ENCODER)
1183 return -ENOTTY;
1185 if (fsize->index)
1186 return -EINVAL;
1188 if (coda_format_normalize_yuv(fsize->pixel_format) ==
1189 V4L2_PIX_FMT_YUV420) {
1190 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1191 codec = coda_find_codec(ctx->dev, fsize->pixel_format,
1192 q_data_dst->fourcc);
1193 } else {
1194 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
1195 fsize->pixel_format);
1197 if (!codec)
1198 return -EINVAL;
1200 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1201 fsize->stepwise.min_width = MIN_W;
1202 fsize->stepwise.max_width = codec->max_w;
1203 fsize->stepwise.step_width = 1;
1204 fsize->stepwise.min_height = MIN_H;
1205 fsize->stepwise.max_height = codec->max_h;
1206 fsize->stepwise.step_height = 1;
1208 return 0;
1211 static int coda_enum_frameintervals(struct file *file, void *fh,
1212 struct v4l2_frmivalenum *f)
1214 struct coda_ctx *ctx = fh_to_ctx(fh);
1215 int i;
1217 if (f->index)
1218 return -EINVAL;
1220 /* Disallow YUYV if the vdoa is not available */
1221 if (!ctx->vdoa && f->pixel_format == V4L2_PIX_FMT_YUYV)
1222 return -EINVAL;
1224 for (i = 0; i < CODA_MAX_FORMATS; i++) {
1225 if (f->pixel_format == ctx->cvd->src_formats[i] ||
1226 f->pixel_format == ctx->cvd->dst_formats[i])
1227 break;
1229 if (i == CODA_MAX_FORMATS)
1230 return -EINVAL;
1232 f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
1233 f->stepwise.min.numerator = 1;
1234 f->stepwise.min.denominator = 65535;
1235 f->stepwise.max.numerator = 65536;
1236 f->stepwise.max.denominator = 1;
1237 f->stepwise.step.numerator = 1;
1238 f->stepwise.step.denominator = 1;
1240 return 0;
1243 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1245 struct coda_ctx *ctx = fh_to_ctx(fh);
1246 struct v4l2_fract *tpf;
1248 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1249 return -EINVAL;
1251 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1252 tpf = &a->parm.output.timeperframe;
1253 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
1254 tpf->numerator = 1 + (ctx->params.framerate >>
1255 CODA_FRATE_DIV_OFFSET);
1257 return 0;
1261 * Approximate timeperframe v4l2_fract with values that can be written
1262 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
1264 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
1266 struct v4l2_fract s = *timeperframe;
1267 struct v4l2_fract f0;
1268 struct v4l2_fract f1 = { 1, 0 };
1269 struct v4l2_fract f2 = { 0, 1 };
1270 unsigned int i, div, s_denominator;
1272 /* Lower bound is 1/65535 */
1273 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
1274 timeperframe->numerator = 1;
1275 timeperframe->denominator = 65535;
1276 return;
1279 /* Upper bound is 65536/1 */
1280 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
1281 timeperframe->numerator = 65536;
1282 timeperframe->denominator = 1;
1283 return;
1286 /* Reduce fraction to lowest terms */
1287 div = gcd(s.numerator, s.denominator);
1288 if (div > 1) {
1289 s.numerator /= div;
1290 s.denominator /= div;
1293 if (s.numerator <= 65536 && s.denominator < 65536) {
1294 *timeperframe = s;
1295 return;
1298 /* Find successive convergents from continued fraction expansion */
1299 while (f2.numerator <= 65536 && f2.denominator < 65536) {
1300 f0 = f1;
1301 f1 = f2;
1303 /* Stop when f2 exactly equals timeperframe */
1304 if (s.numerator == 0)
1305 break;
1307 i = s.denominator / s.numerator;
1309 f2.numerator = f0.numerator + i * f1.numerator;
1310 f2.denominator = f0.denominator + i * f2.denominator;
1312 s_denominator = s.numerator;
1313 s.numerator = s.denominator % s.numerator;
1314 s.denominator = s_denominator;
1317 *timeperframe = f1;
1320 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
1322 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
1323 timeperframe->denominator;
1326 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1328 struct coda_ctx *ctx = fh_to_ctx(fh);
1329 struct v4l2_fract *tpf;
1331 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1332 return -EINVAL;
1334 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1335 tpf = &a->parm.output.timeperframe;
1336 coda_approximate_timeperframe(tpf);
1337 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
1338 ctx->params.framerate_changed = true;
1340 return 0;
1343 static int coda_subscribe_event(struct v4l2_fh *fh,
1344 const struct v4l2_event_subscription *sub)
1346 struct coda_ctx *ctx = fh_to_ctx(fh);
1348 switch (sub->type) {
1349 case V4L2_EVENT_EOS:
1350 return v4l2_event_subscribe(fh, sub, 0, NULL);
1351 case V4L2_EVENT_SOURCE_CHANGE:
1352 if (ctx->inst_type == CODA_INST_DECODER)
1353 return v4l2_event_subscribe(fh, sub, 0, NULL);
1354 else
1355 return -EINVAL;
1356 default:
1357 return v4l2_ctrl_subscribe_event(fh, sub);
1361 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
1362 .vidioc_querycap = coda_querycap,
1364 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
1365 .vidioc_g_fmt_vid_cap = coda_g_fmt,
1366 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
1367 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
1369 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
1370 .vidioc_g_fmt_vid_out = coda_g_fmt,
1371 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
1372 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
1374 .vidioc_reqbufs = coda_reqbufs,
1375 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
1377 .vidioc_qbuf = coda_qbuf,
1378 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
1379 .vidioc_dqbuf = coda_dqbuf,
1380 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
1381 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
1383 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
1384 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
1386 .vidioc_g_selection = coda_g_selection,
1387 .vidioc_s_selection = coda_s_selection,
1389 .vidioc_try_encoder_cmd = coda_try_encoder_cmd,
1390 .vidioc_encoder_cmd = coda_encoder_cmd,
1391 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
1392 .vidioc_decoder_cmd = coda_decoder_cmd,
1394 .vidioc_g_parm = coda_g_parm,
1395 .vidioc_s_parm = coda_s_parm,
1397 .vidioc_enum_framesizes = coda_enum_framesizes,
1398 .vidioc_enum_frameintervals = coda_enum_frameintervals,
1400 .vidioc_subscribe_event = coda_subscribe_event,
1401 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1405 * Mem-to-mem operations.
1408 static void coda_device_run(void *m2m_priv)
1410 struct coda_ctx *ctx = m2m_priv;
1411 struct coda_dev *dev = ctx->dev;
1413 queue_work(dev->workqueue, &ctx->pic_run_work);
1416 static void coda_pic_run_work(struct work_struct *work)
1418 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1419 struct coda_dev *dev = ctx->dev;
1420 int ret;
1422 mutex_lock(&ctx->buffer_mutex);
1423 mutex_lock(&dev->coda_mutex);
1425 ret = ctx->ops->prepare_run(ctx);
1426 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1427 mutex_unlock(&dev->coda_mutex);
1428 mutex_unlock(&ctx->buffer_mutex);
1429 /* job_finish scheduled by prepare_decode */
1430 return;
1433 if (!wait_for_completion_timeout(&ctx->completion,
1434 msecs_to_jiffies(1000))) {
1435 dev_err(dev->dev, "CODA PIC_RUN timeout\n");
1437 ctx->hold = true;
1439 coda_hw_reset(ctx);
1441 if (ctx->ops->run_timeout)
1442 ctx->ops->run_timeout(ctx);
1443 } else {
1444 ctx->ops->finish_run(ctx);
1447 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
1448 ctx->ops->seq_end_work)
1449 queue_work(dev->workqueue, &ctx->seq_end_work);
1451 mutex_unlock(&dev->coda_mutex);
1452 mutex_unlock(&ctx->buffer_mutex);
1454 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
1457 static int coda_job_ready(void *m2m_priv)
1459 struct coda_ctx *ctx = m2m_priv;
1460 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1463 * For both 'P' and 'key' frame cases 1 picture
1464 * and 1 frame are needed. In the decoder case,
1465 * the compressed frame can be in the bitstream.
1467 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1468 coda_dbg(1, ctx, "not ready: not enough vid-out buffers.\n");
1469 return 0;
1472 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1473 coda_dbg(1, ctx, "not ready: not enough vid-cap buffers.\n");
1474 return 0;
1477 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1478 bool stream_end = ctx->bit_stream_param &
1479 CODA_BIT_STREAM_END_FLAG;
1480 int num_metas = ctx->num_metas;
1481 struct coda_buffer_meta *meta;
1482 unsigned int count;
1484 count = hweight32(ctx->frm_dis_flg);
1485 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) {
1486 coda_dbg(1, ctx,
1487 "not ready: all internal buffers in use: %d/%d (0x%x)",
1488 count, ctx->num_internal_frames,
1489 ctx->frm_dis_flg);
1490 return 0;
1493 if (ctx->hold && !src_bufs) {
1494 coda_dbg(1, ctx,
1495 "not ready: on hold for more buffers.\n");
1496 return 0;
1499 if (!stream_end && (num_metas + src_bufs) < 2) {
1500 coda_dbg(1, ctx,
1501 "not ready: need 2 buffers available (queue:%d + bitstream:%d)\n",
1502 num_metas, src_bufs);
1503 return 0;
1506 meta = list_first_entry(&ctx->buffer_meta_list,
1507 struct coda_buffer_meta, list);
1508 if (!coda_bitstream_can_fetch_past(ctx, meta->end) &&
1509 !stream_end) {
1510 coda_dbg(1, ctx,
1511 "not ready: not enough bitstream data to read past %u (%u)\n",
1512 meta->end, ctx->bitstream_fifo.kfifo.in);
1513 return 0;
1517 if (ctx->aborting) {
1518 coda_dbg(1, ctx, "not ready: aborting\n");
1519 return 0;
1522 coda_dbg(2, ctx, "job ready\n");
1524 return 1;
1527 static void coda_job_abort(void *priv)
1529 struct coda_ctx *ctx = priv;
1531 ctx->aborting = 1;
1533 coda_dbg(1, ctx, "job abort\n");
1536 static const struct v4l2_m2m_ops coda_m2m_ops = {
1537 .device_run = coda_device_run,
1538 .job_ready = coda_job_ready,
1539 .job_abort = coda_job_abort,
1542 static void set_default_params(struct coda_ctx *ctx)
1544 unsigned int max_w, max_h, usize, csize;
1546 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1547 ctx->cvd->dst_formats[0]);
1548 max_w = min(ctx->codec->max_w, 1920U);
1549 max_h = min(ctx->codec->max_h, 1088U);
1550 usize = max_w * max_h * 3 / 2;
1551 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1553 ctx->params.codec_mode = ctx->codec->mode;
1554 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG)
1555 ctx->colorspace = V4L2_COLORSPACE_JPEG;
1556 else
1557 ctx->colorspace = V4L2_COLORSPACE_REC709;
1558 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1559 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1560 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1561 ctx->params.framerate = 30;
1563 /* Default formats for output and input queues */
1564 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1565 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1566 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1567 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1568 ctx->q_data[V4L2_M2M_DST].width = max_w;
1569 ctx->q_data[V4L2_M2M_DST].height = max_h;
1570 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1571 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1572 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1573 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1574 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1575 } else {
1576 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1577 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1578 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1579 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1581 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1582 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1583 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1584 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1587 * Since the RBC2AXI logic only supports a single chroma plane,
1588 * macroblock tiling only works for to NV12 pixel format.
1590 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1594 * Queue operations
1596 static int coda_queue_setup(struct vb2_queue *vq,
1597 unsigned int *nbuffers, unsigned int *nplanes,
1598 unsigned int sizes[], struct device *alloc_devs[])
1600 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1601 struct coda_q_data *q_data;
1602 unsigned int size;
1604 q_data = get_q_data(ctx, vq->type);
1605 size = q_data->sizeimage;
1607 if (*nplanes)
1608 return sizes[0] < size ? -EINVAL : 0;
1610 *nplanes = 1;
1611 sizes[0] = size;
1613 coda_dbg(1, ctx, "get %d buffer(s) of size %d each.\n", *nbuffers,
1614 size);
1616 return 0;
1619 static int coda_buf_prepare(struct vb2_buffer *vb)
1621 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1622 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1623 struct coda_q_data *q_data;
1625 q_data = get_q_data(ctx, vb->vb2_queue->type);
1626 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1627 if (vbuf->field == V4L2_FIELD_ANY)
1628 vbuf->field = V4L2_FIELD_NONE;
1629 if (vbuf->field != V4L2_FIELD_NONE) {
1630 v4l2_warn(&ctx->dev->v4l2_dev,
1631 "%s field isn't supported\n", __func__);
1632 return -EINVAL;
1636 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1637 v4l2_warn(&ctx->dev->v4l2_dev,
1638 "%s data will not fit into plane (%lu < %lu)\n",
1639 __func__, vb2_plane_size(vb, 0),
1640 (long)q_data->sizeimage);
1641 return -EINVAL;
1644 return 0;
1647 static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value)
1649 if (!ctrl)
1650 return;
1652 v4l2_ctrl_lock(ctrl);
1655 * Extend the control range if the parsed stream contains a known but
1656 * unsupported value or level.
1658 if (value > ctrl->maximum) {
1659 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value,
1660 ctrl->menu_skip_mask & ~(1 << value),
1661 ctrl->default_value);
1662 } else if (value < ctrl->minimum) {
1663 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum,
1664 ctrl->menu_skip_mask & ~(1 << value),
1665 ctrl->default_value);
1668 __v4l2_ctrl_s_ctrl(ctrl, value);
1670 v4l2_ctrl_unlock(ctrl);
1673 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
1674 u8 level_idc)
1676 const char * const *profile_names;
1677 const char * const *level_names;
1678 struct v4l2_ctrl *profile_ctrl;
1679 struct v4l2_ctrl *level_ctrl;
1680 const char *codec_name;
1681 u32 profile_cid;
1682 u32 level_cid;
1683 int profile;
1684 int level;
1686 switch (ctx->codec->src_fourcc) {
1687 case V4L2_PIX_FMT_H264:
1688 codec_name = "H264";
1689 profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
1690 level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
1691 profile_ctrl = ctx->h264_profile_ctrl;
1692 level_ctrl = ctx->h264_level_ctrl;
1693 profile = coda_h264_profile(profile_idc);
1694 level = coda_h264_level(level_idc);
1695 break;
1696 case V4L2_PIX_FMT_MPEG2:
1697 codec_name = "MPEG-2";
1698 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE;
1699 level_cid = V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL;
1700 profile_ctrl = ctx->mpeg2_profile_ctrl;
1701 level_ctrl = ctx->mpeg2_level_ctrl;
1702 profile = coda_mpeg2_profile(profile_idc);
1703 level = coda_mpeg2_level(level_idc);
1704 break;
1705 case V4L2_PIX_FMT_MPEG4:
1706 codec_name = "MPEG-4";
1707 profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
1708 level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
1709 profile_ctrl = ctx->mpeg4_profile_ctrl;
1710 level_ctrl = ctx->mpeg4_level_ctrl;
1711 profile = coda_mpeg4_profile(profile_idc);
1712 level = coda_mpeg4_level(level_idc);
1713 break;
1714 default:
1715 return;
1718 profile_names = v4l2_ctrl_get_menu(profile_cid);
1719 level_names = v4l2_ctrl_get_menu(level_cid);
1721 if (profile < 0) {
1722 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s profile: %u\n",
1723 codec_name, profile_idc);
1724 } else {
1725 coda_dbg(1, ctx, "Parsed %s profile: %s\n", codec_name,
1726 profile_names[profile]);
1727 coda_update_menu_ctrl(profile_ctrl, profile);
1730 if (level < 0) {
1731 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid %s level: %u\n",
1732 codec_name, level_idc);
1733 } else {
1734 coda_dbg(1, ctx, "Parsed %s level: %s\n", codec_name,
1735 level_names[level]);
1736 coda_update_menu_ctrl(level_ctrl, level);
1740 static void coda_queue_source_change_event(struct coda_ctx *ctx)
1742 static const struct v4l2_event source_change_event = {
1743 .type = V4L2_EVENT_SOURCE_CHANGE,
1744 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1747 v4l2_event_queue_fh(&ctx->fh, &source_change_event);
1750 static void coda_buf_queue(struct vb2_buffer *vb)
1752 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1753 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1754 struct vb2_queue *vq = vb->vb2_queue;
1755 struct coda_q_data *q_data;
1757 q_data = get_q_data(ctx, vb->vb2_queue->type);
1760 * In the decoder case, immediately try to copy the buffer into the
1761 * bitstream ringbuffer and mark it as ready to be dequeued.
1763 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1765 * For backwards compatibility, queuing an empty buffer marks
1766 * the stream end
1768 if (vb2_get_plane_payload(vb, 0) == 0)
1769 coda_bit_stream_end_flag(ctx);
1771 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1773 * Unless already done, try to obtain profile_idc and
1774 * level_idc from the SPS header. This allows to decide
1775 * whether to enable reordering during sequence
1776 * initialization.
1778 if (!ctx->params.h264_profile_idc) {
1779 coda_sps_parse_profile(ctx, vb);
1780 coda_update_profile_level_ctrls(ctx,
1781 ctx->params.h264_profile_idc,
1782 ctx->params.h264_level_idc);
1786 mutex_lock(&ctx->bitstream_mutex);
1787 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1788 if (vb2_is_streaming(vb->vb2_queue))
1789 /* This set buf->sequence = ctx->qsequence++ */
1790 coda_fill_bitstream(ctx, NULL);
1791 mutex_unlock(&ctx->bitstream_mutex);
1793 if (!ctx->initialized) {
1795 * Run sequence initialization in case the queued
1796 * buffer contained headers.
1798 if (vb2_is_streaming(vb->vb2_queue) &&
1799 ctx->ops->seq_init_work) {
1800 queue_work(ctx->dev->workqueue,
1801 &ctx->seq_init_work);
1802 flush_work(&ctx->seq_init_work);
1805 if (ctx->initialized)
1806 coda_queue_source_change_event(ctx);
1808 } else {
1809 if ((ctx->inst_type == CODA_INST_ENCODER || !ctx->use_bit) &&
1810 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1811 vbuf->sequence = ctx->qsequence++;
1812 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1816 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1817 size_t size, const char *name, struct dentry *parent)
1819 buf->vaddr = dma_alloc_coherent(dev->dev, size, &buf->paddr,
1820 GFP_KERNEL);
1821 if (!buf->vaddr) {
1822 v4l2_err(&dev->v4l2_dev,
1823 "Failed to allocate %s buffer of size %zu\n",
1824 name, size);
1825 return -ENOMEM;
1828 buf->size = size;
1830 if (name && parent) {
1831 buf->blob.data = buf->vaddr;
1832 buf->blob.size = size;
1833 buf->dentry = debugfs_create_blob(name, 0644, parent,
1834 &buf->blob);
1835 if (!buf->dentry)
1836 dev_warn(dev->dev,
1837 "failed to create debugfs entry %s\n", name);
1840 return 0;
1843 void coda_free_aux_buf(struct coda_dev *dev,
1844 struct coda_aux_buf *buf)
1846 if (buf->vaddr) {
1847 dma_free_coherent(dev->dev, buf->size, buf->vaddr, buf->paddr);
1848 buf->vaddr = NULL;
1849 buf->size = 0;
1850 debugfs_remove(buf->dentry);
1851 buf->dentry = NULL;
1855 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1857 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1858 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1859 struct coda_q_data *q_data_src, *q_data_dst;
1860 struct v4l2_m2m_buffer *m2m_buf, *tmp;
1861 struct vb2_v4l2_buffer *buf;
1862 struct list_head list;
1863 int ret = 0;
1865 if (count < 1)
1866 return -EINVAL;
1868 coda_dbg(1, ctx, "start streaming %s\n", v4l2_type_names[q->type]);
1870 INIT_LIST_HEAD(&list);
1872 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1873 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1874 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1875 /* copy the buffers that were queued before streamon */
1876 mutex_lock(&ctx->bitstream_mutex);
1877 coda_fill_bitstream(ctx, &list);
1878 mutex_unlock(&ctx->bitstream_mutex);
1880 if (ctx->dev->devtype->product != CODA_960 &&
1881 coda_get_bitstream_payload(ctx) < 512) {
1882 v4l2_err(v4l2_dev, "start payload < 512\n");
1883 ret = -EINVAL;
1884 goto err;
1887 if (!ctx->initialized) {
1888 /* Run sequence initialization */
1889 if (ctx->ops->seq_init_work) {
1890 queue_work(ctx->dev->workqueue,
1891 &ctx->seq_init_work);
1892 flush_work(&ctx->seq_init_work);
1897 ctx->streamon_out = 1;
1898 } else {
1899 ctx->streamon_cap = 1;
1902 /* Don't start the coda unless both queues are on */
1903 if (!(ctx->streamon_out && ctx->streamon_cap))
1904 goto out;
1906 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1907 if ((q_data_src->rect.width != q_data_dst->width &&
1908 round_up(q_data_src->rect.width, 16) != q_data_dst->width) ||
1909 (q_data_src->rect.height != q_data_dst->height &&
1910 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) {
1911 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1912 q_data_src->rect.width, q_data_src->rect.height,
1913 q_data_dst->width, q_data_dst->height);
1914 ret = -EINVAL;
1915 goto err;
1918 /* Allow BIT decoder device_run with no new buffers queued */
1919 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1920 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1922 ctx->gopcounter = ctx->params.gop_size - 1;
1924 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1925 ctx->params.gop_size = 1;
1926 ctx->gopcounter = ctx->params.gop_size - 1;
1928 ret = ctx->ops->start_streaming(ctx);
1929 if (ctx->inst_type == CODA_INST_DECODER) {
1930 if (ret == -EAGAIN)
1931 goto out;
1933 if (ret < 0)
1934 goto err;
1936 out:
1937 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1938 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1939 list_del(&m2m_buf->list);
1940 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE);
1943 return 0;
1945 err:
1946 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1947 list_for_each_entry_safe(m2m_buf, tmp, &list, list) {
1948 list_del(&m2m_buf->list);
1949 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED);
1951 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1952 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1953 } else {
1954 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1955 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1957 return ret;
1960 static void coda_stop_streaming(struct vb2_queue *q)
1962 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1963 struct coda_dev *dev = ctx->dev;
1964 struct vb2_v4l2_buffer *buf;
1965 bool stop;
1967 stop = ctx->streamon_out && ctx->streamon_cap;
1969 coda_dbg(1, ctx, "stop streaming %s\n", v4l2_type_names[q->type]);
1971 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1972 ctx->streamon_out = 0;
1974 coda_bit_stream_end_flag(ctx);
1976 ctx->qsequence = 0;
1978 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1979 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1980 } else {
1981 ctx->streamon_cap = 0;
1983 ctx->osequence = 0;
1984 ctx->sequence_offset = 0;
1986 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1987 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1990 if (stop) {
1991 struct coda_buffer_meta *meta;
1993 if (ctx->ops->seq_end_work) {
1994 queue_work(dev->workqueue, &ctx->seq_end_work);
1995 flush_work(&ctx->seq_end_work);
1997 spin_lock(&ctx->buffer_meta_lock);
1998 while (!list_empty(&ctx->buffer_meta_list)) {
1999 meta = list_first_entry(&ctx->buffer_meta_list,
2000 struct coda_buffer_meta, list);
2001 list_del(&meta->list);
2002 kfree(meta);
2004 ctx->num_metas = 0;
2005 spin_unlock(&ctx->buffer_meta_lock);
2006 kfifo_init(&ctx->bitstream_fifo,
2007 ctx->bitstream.vaddr, ctx->bitstream.size);
2008 ctx->runcounter = 0;
2009 ctx->aborting = 0;
2010 ctx->hold = false;
2013 if (!ctx->streamon_out && !ctx->streamon_cap)
2014 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
2017 static const struct vb2_ops coda_qops = {
2018 .queue_setup = coda_queue_setup,
2019 .buf_prepare = coda_buf_prepare,
2020 .buf_queue = coda_buf_queue,
2021 .start_streaming = coda_start_streaming,
2022 .stop_streaming = coda_stop_streaming,
2023 .wait_prepare = vb2_ops_wait_prepare,
2024 .wait_finish = vb2_ops_wait_finish,
2027 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2029 const char * const *val_names = v4l2_ctrl_get_menu(ctrl->id);
2030 struct coda_ctx *ctx =
2031 container_of(ctrl->handler, struct coda_ctx, ctrls);
2033 if (val_names)
2034 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d (\"%s\")\n",
2035 ctrl->id, ctrl->name, ctrl->val, val_names[ctrl->val]);
2036 else
2037 coda_dbg(2, ctx, "s_ctrl: id = 0x%x, name = \"%s\", val = %d\n",
2038 ctrl->id, ctrl->name, ctrl->val);
2040 switch (ctrl->id) {
2041 case V4L2_CID_HFLIP:
2042 if (ctrl->val)
2043 ctx->params.rot_mode |= CODA_MIR_HOR;
2044 else
2045 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2046 break;
2047 case V4L2_CID_VFLIP:
2048 if (ctrl->val)
2049 ctx->params.rot_mode |= CODA_MIR_VER;
2050 else
2051 ctx->params.rot_mode &= ~CODA_MIR_VER;
2052 break;
2053 case V4L2_CID_MPEG_VIDEO_BITRATE:
2054 ctx->params.bitrate = ctrl->val / 1000;
2055 ctx->params.bitrate_changed = true;
2056 break;
2057 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2058 ctx->params.gop_size = ctrl->val;
2059 break;
2060 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2061 ctx->params.h264_intra_qp = ctrl->val;
2062 ctx->params.h264_intra_qp_changed = true;
2063 break;
2064 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2065 ctx->params.h264_inter_qp = ctrl->val;
2066 break;
2067 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2068 ctx->params.h264_min_qp = ctrl->val;
2069 break;
2070 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2071 ctx->params.h264_max_qp = ctrl->val;
2072 break;
2073 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2074 ctx->params.h264_slice_alpha_c0_offset_div2 = ctrl->val;
2075 break;
2076 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2077 ctx->params.h264_slice_beta_offset_div2 = ctrl->val;
2078 break;
2079 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2080 ctx->params.h264_disable_deblocking_filter_idc = ctrl->val;
2081 break;
2082 case V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION:
2083 ctx->params.h264_constrained_intra_pred_flag = ctrl->val;
2084 break;
2085 case V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET:
2086 ctx->params.h264_chroma_qp_index_offset = ctrl->val;
2087 break;
2088 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
2089 /* TODO: switch between baseline and constrained baseline */
2090 if (ctx->inst_type == CODA_INST_ENCODER)
2091 ctx->params.h264_profile_idc = 66;
2092 break;
2093 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
2094 /* nothing to do, this is set by the encoder */
2095 break;
2096 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2097 ctx->params.mpeg4_intra_qp = ctrl->val;
2098 break;
2099 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2100 ctx->params.mpeg4_inter_qp = ctrl->val;
2101 break;
2102 case V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE:
2103 case V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL:
2104 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
2105 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
2106 /* nothing to do, these are fixed */
2107 break;
2108 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2109 ctx->params.slice_mode = ctrl->val;
2110 ctx->params.slice_mode_changed = true;
2111 break;
2112 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2113 ctx->params.slice_max_mb = ctrl->val;
2114 ctx->params.slice_mode_changed = true;
2115 break;
2116 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2117 ctx->params.slice_max_bits = ctrl->val * 8;
2118 ctx->params.slice_mode_changed = true;
2119 break;
2120 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2121 break;
2122 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2123 ctx->params.intra_refresh = ctrl->val;
2124 ctx->params.intra_refresh_changed = true;
2125 break;
2126 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
2127 ctx->params.force_ipicture = true;
2128 break;
2129 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
2130 coda_set_jpeg_compression_quality(ctx, ctrl->val);
2131 break;
2132 case V4L2_CID_JPEG_RESTART_INTERVAL:
2133 ctx->params.jpeg_restart_interval = ctrl->val;
2134 break;
2135 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
2136 ctx->params.vbv_delay = ctrl->val;
2137 break;
2138 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
2139 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
2140 break;
2141 default:
2142 coda_dbg(1, ctx, "Invalid control, id=%d, val=%d\n",
2143 ctrl->id, ctrl->val);
2144 return -EINVAL;
2147 return 0;
2150 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
2151 .s_ctrl = coda_s_ctrl,
2154 static void coda_encode_ctrls(struct coda_ctx *ctx)
2156 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99;
2158 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2159 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
2160 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2161 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16);
2162 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2163 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
2164 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2165 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
2166 if (ctx->dev->devtype->product != CODA_960) {
2167 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2168 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2170 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2171 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
2172 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2173 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
2174 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2175 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
2176 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2177 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2178 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY,
2179 0x0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2180 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2181 V4L2_CID_MPEG_VIDEO_H264_CONSTRAINED_INTRA_PREDICTION, 0, 1, 1,
2183 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2184 V4L2_CID_MPEG_VIDEO_H264_CHROMA_QP_INDEX_OFFSET, -12, 12, 1, 0);
2185 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2186 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2187 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
2188 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
2189 if (ctx->dev->devtype->product == CODA_HX4 ||
2190 ctx->dev->devtype->product == CODA_7541) {
2191 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2192 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2193 V4L2_MPEG_VIDEO_H264_LEVEL_3_1,
2194 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2195 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2196 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)),
2197 V4L2_MPEG_VIDEO_H264_LEVEL_3_1);
2199 if (ctx->dev->devtype->product == CODA_960) {
2200 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2201 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
2202 V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
2203 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
2204 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
2205 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
2206 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
2207 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
2208 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
2210 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2211 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2212 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2213 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2214 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2215 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2216 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0,
2217 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
2218 if (ctx->dev->devtype->product == CODA_HX4 ||
2219 ctx->dev->devtype->product == CODA_7541 ||
2220 ctx->dev->devtype->product == CODA_960) {
2221 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2222 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2223 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
2224 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5),
2225 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2227 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2228 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
2229 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES, 0x0,
2230 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
2231 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2232 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
2233 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2234 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
2235 500);
2236 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2237 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2238 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2239 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2240 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2241 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2242 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
2243 1920 * 1088 / 256, 1, 0);
2244 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2245 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
2247 * The maximum VBV size value is 0x7fffffff bits,
2248 * one bit less than 262144 KiB
2250 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2251 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
2254 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
2256 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2257 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
2258 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2259 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
2262 static void coda_decode_ctrls(struct coda_ctx *ctx)
2264 u8 max;
2266 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2267 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
2268 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
2269 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
2270 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
2271 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
2272 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
2273 if (ctx->h264_profile_ctrl)
2274 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2276 if (ctx->dev->devtype->product == CODA_HX4 ||
2277 ctx->dev->devtype->product == CODA_7541)
2278 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
2279 else if (ctx->dev->devtype->product == CODA_960)
2280 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
2281 else
2282 return;
2283 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2284 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, 0, max);
2285 if (ctx->h264_level_ctrl)
2286 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2288 ctx->mpeg2_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2289 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_PROFILE,
2290 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH, 0,
2291 V4L2_MPEG_VIDEO_MPEG2_PROFILE_HIGH);
2292 if (ctx->mpeg2_profile_ctrl)
2293 ctx->mpeg2_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2295 ctx->mpeg2_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2296 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG2_LEVEL,
2297 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH, 0,
2298 V4L2_MPEG_VIDEO_MPEG2_LEVEL_HIGH);
2299 if (ctx->mpeg2_level_ctrl)
2300 ctx->mpeg2_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2302 ctx->mpeg4_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2303 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
2304 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY, 0,
2305 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY);
2306 if (ctx->mpeg4_profile_ctrl)
2307 ctx->mpeg4_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2309 ctx->mpeg4_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls,
2310 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
2311 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 0,
2312 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5);
2313 if (ctx->mpeg4_level_ctrl)
2314 ctx->mpeg4_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2317 static int coda_ctrls_setup(struct coda_ctx *ctx)
2319 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
2321 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2322 V4L2_CID_HFLIP, 0, 1, 1, 0);
2323 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2324 V4L2_CID_VFLIP, 0, 1, 1, 0);
2325 if (ctx->inst_type == CODA_INST_ENCODER) {
2326 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2327 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2328 1, 1, 1, 1);
2329 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
2330 coda_jpeg_encode_ctrls(ctx);
2331 else
2332 coda_encode_ctrls(ctx);
2333 } else {
2334 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2335 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2336 1, 1, 1, 1);
2337 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264)
2338 coda_decode_ctrls(ctx);
2341 if (ctx->ctrls.error) {
2342 v4l2_err(&ctx->dev->v4l2_dev,
2343 "control initialization error (%d)",
2344 ctx->ctrls.error);
2345 return -EINVAL;
2348 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2351 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
2353 vq->drv_priv = ctx;
2354 vq->ops = &coda_qops;
2355 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2356 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2357 vq->lock = &ctx->dev->dev_mutex;
2358 /* One way to indicate end-of-stream for coda is to set the
2359 * bytesused == 0. However by default videobuf2 handles bytesused
2360 * equal to 0 as a special case and changes its value to the size
2361 * of the buffer. Set the allow_zero_bytesused flag, so
2362 * that videobuf2 will keep the value of bytesused intact.
2364 vq->allow_zero_bytesused = 1;
2366 * We might be fine with no buffers on some of the queues, but that
2367 * would need to be reflected in job_ready(). Currently we expect all
2368 * queues to have at least one buffer queued.
2370 vq->min_buffers_needed = 1;
2371 vq->dev = ctx->dev->dev;
2373 return vb2_queue_init(vq);
2376 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2377 struct vb2_queue *dst_vq)
2379 int ret;
2381 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2382 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2383 src_vq->mem_ops = &vb2_dma_contig_memops;
2385 ret = coda_queue_init(priv, src_vq);
2386 if (ret)
2387 return ret;
2389 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2390 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2391 dst_vq->mem_ops = &vb2_dma_contig_memops;
2393 return coda_queue_init(priv, dst_vq);
2396 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2397 struct vb2_queue *dst_vq)
2399 int ret;
2401 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2402 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2403 src_vq->mem_ops = &vb2_vmalloc_memops;
2405 ret = coda_queue_init(priv, src_vq);
2406 if (ret)
2407 return ret;
2409 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2410 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2411 dst_vq->dma_attrs = DMA_ATTR_NO_KERNEL_MAPPING;
2412 dst_vq->mem_ops = &vb2_dma_contig_memops;
2414 return coda_queue_init(priv, dst_vq);
2418 * File operations
2421 static int coda_open(struct file *file)
2423 struct video_device *vdev = video_devdata(file);
2424 struct coda_dev *dev = video_get_drvdata(vdev);
2425 struct coda_ctx *ctx;
2426 unsigned int max = ~0;
2427 char *name;
2428 int ret;
2429 int idx;
2431 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2432 if (!ctx)
2433 return -ENOMEM;
2435 if (dev->devtype->product == CODA_DX6)
2436 max = CODADX6_MAX_INSTANCES - 1;
2437 idx = ida_alloc_max(&dev->ida, max, GFP_KERNEL);
2438 if (idx < 0) {
2439 ret = idx;
2440 goto err_coda_max;
2443 name = kasprintf(GFP_KERNEL, "context%d", idx);
2444 if (!name) {
2445 ret = -ENOMEM;
2446 goto err_coda_name_init;
2449 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2450 kfree(name);
2452 ctx->cvd = to_coda_video_device(vdev);
2453 ctx->inst_type = ctx->cvd->type;
2454 ctx->ops = ctx->cvd->ops;
2455 ctx->use_bit = !ctx->cvd->direct;
2456 init_completion(&ctx->completion);
2457 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2458 if (ctx->ops->seq_init_work)
2459 INIT_WORK(&ctx->seq_init_work, ctx->ops->seq_init_work);
2460 if (ctx->ops->seq_end_work)
2461 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
2462 v4l2_fh_init(&ctx->fh, video_devdata(file));
2463 file->private_data = &ctx->fh;
2464 v4l2_fh_add(&ctx->fh);
2465 ctx->dev = dev;
2466 ctx->idx = idx;
2468 coda_dbg(1, ctx, "open instance (%p)\n", ctx);
2470 switch (dev->devtype->product) {
2471 case CODA_960:
2473 * Enabling the BWB when decoding can hang the firmware with
2474 * certain streams. The issue was tracked as ENGR00293425 by
2475 * Freescale. As a workaround, disable BWB for all decoders.
2476 * The enable_bwb module parameter allows to override this.
2478 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER)
2479 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB;
2480 /* fallthrough */
2481 case CODA_HX4:
2482 case CODA_7541:
2483 ctx->reg_idx = 0;
2484 break;
2485 default:
2486 ctx->reg_idx = idx;
2488 if (ctx->dev->vdoa && !disable_vdoa) {
2489 ctx->vdoa = vdoa_context_create(dev->vdoa);
2490 if (!ctx->vdoa)
2491 v4l2_warn(&dev->v4l2_dev,
2492 "Failed to create vdoa context: not using vdoa");
2494 ctx->use_vdoa = false;
2496 /* Power up and upload firmware if necessary */
2497 ret = pm_runtime_get_sync(dev->dev);
2498 if (ret < 0) {
2499 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2500 goto err_pm_get;
2503 ret = clk_prepare_enable(dev->clk_per);
2504 if (ret)
2505 goto err_clk_per;
2507 ret = clk_prepare_enable(dev->clk_ahb);
2508 if (ret)
2509 goto err_clk_ahb;
2511 set_default_params(ctx);
2512 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2513 ctx->ops->queue_init);
2514 if (IS_ERR(ctx->fh.m2m_ctx)) {
2515 ret = PTR_ERR(ctx->fh.m2m_ctx);
2517 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2518 __func__, ret);
2519 goto err_ctx_init;
2522 ret = coda_ctrls_setup(ctx);
2523 if (ret) {
2524 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
2525 goto err_ctrls_setup;
2528 ctx->fh.ctrl_handler = &ctx->ctrls;
2530 mutex_init(&ctx->bitstream_mutex);
2531 mutex_init(&ctx->buffer_mutex);
2532 mutex_init(&ctx->wakeup_mutex);
2533 INIT_LIST_HEAD(&ctx->buffer_meta_list);
2534 spin_lock_init(&ctx->buffer_meta_lock);
2536 return 0;
2538 err_ctrls_setup:
2539 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2540 err_ctx_init:
2541 clk_disable_unprepare(dev->clk_ahb);
2542 err_clk_ahb:
2543 clk_disable_unprepare(dev->clk_per);
2544 err_clk_per:
2545 pm_runtime_put_sync(dev->dev);
2546 err_pm_get:
2547 v4l2_fh_del(&ctx->fh);
2548 v4l2_fh_exit(&ctx->fh);
2549 err_coda_name_init:
2550 ida_free(&dev->ida, ctx->idx);
2551 err_coda_max:
2552 kfree(ctx);
2553 return ret;
2556 static int coda_release(struct file *file)
2558 struct coda_dev *dev = video_drvdata(file);
2559 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2561 coda_dbg(1, ctx, "release instance (%p)\n", ctx);
2563 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
2564 coda_bit_stream_end_flag(ctx);
2566 /* If this instance is running, call .job_abort and wait for it to end */
2567 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
2569 if (ctx->vdoa)
2570 vdoa_context_destroy(ctx->vdoa);
2572 /* In case the instance was not running, we still need to call SEQ_END */
2573 if (ctx->ops->seq_end_work) {
2574 queue_work(dev->workqueue, &ctx->seq_end_work);
2575 flush_work(&ctx->seq_end_work);
2578 if (ctx->dev->devtype->product == CODA_DX6)
2579 coda_free_aux_buf(dev, &ctx->workbuf);
2581 v4l2_ctrl_handler_free(&ctx->ctrls);
2582 clk_disable_unprepare(dev->clk_ahb);
2583 clk_disable_unprepare(dev->clk_per);
2584 pm_runtime_put_sync(dev->dev);
2585 v4l2_fh_del(&ctx->fh);
2586 v4l2_fh_exit(&ctx->fh);
2587 ida_free(&dev->ida, ctx->idx);
2588 if (ctx->ops->release)
2589 ctx->ops->release(ctx);
2590 debugfs_remove_recursive(ctx->debugfs_entry);
2591 kfree(ctx);
2593 return 0;
2596 static const struct v4l2_file_operations coda_fops = {
2597 .owner = THIS_MODULE,
2598 .open = coda_open,
2599 .release = coda_release,
2600 .poll = v4l2_m2m_fop_poll,
2601 .unlocked_ioctl = video_ioctl2,
2602 .mmap = v4l2_m2m_fop_mmap,
2605 static int coda_hw_init(struct coda_dev *dev)
2607 u32 data;
2608 u16 *p;
2609 int i, ret;
2611 ret = clk_prepare_enable(dev->clk_per);
2612 if (ret)
2613 goto err_clk_per;
2615 ret = clk_prepare_enable(dev->clk_ahb);
2616 if (ret)
2617 goto err_clk_ahb;
2619 reset_control_reset(dev->rstc);
2622 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2623 * The 16-bit chars in the code buffer are in memory access
2624 * order, re-sort them to CODA order for register download.
2625 * Data in this SRAM survives a reboot.
2627 p = (u16 *)dev->codebuf.vaddr;
2628 if (dev->devtype->product == CODA_DX6) {
2629 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2630 data = CODA_DOWN_ADDRESS_SET(i) |
2631 CODA_DOWN_DATA_SET(p[i ^ 1]);
2632 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2634 } else {
2635 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2636 data = CODA_DOWN_ADDRESS_SET(i) |
2637 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2638 3 - (i % 4)]);
2639 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2643 /* Clear registers */
2644 for (i = 0; i < 64; i++)
2645 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2647 /* Tell the BIT where to find everything it needs */
2648 if (dev->devtype->product == CODA_960 ||
2649 dev->devtype->product == CODA_7541 ||
2650 dev->devtype->product == CODA_HX4) {
2651 coda_write(dev, dev->tempbuf.paddr,
2652 CODA_REG_BIT_TEMP_BUF_ADDR);
2653 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2654 } else {
2655 coda_write(dev, dev->workbuf.paddr,
2656 CODA_REG_BIT_WORK_BUF_ADDR);
2658 coda_write(dev, dev->codebuf.paddr,
2659 CODA_REG_BIT_CODE_BUF_ADDR);
2660 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2662 /* Set default values */
2663 switch (dev->devtype->product) {
2664 case CODA_DX6:
2665 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
2666 CODA_REG_BIT_STREAM_CTRL);
2667 break;
2668 default:
2669 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
2670 CODA_REG_BIT_STREAM_CTRL);
2672 if (dev->devtype->product == CODA_960)
2673 coda_write(dev, CODA9_FRAME_ENABLE_BWB,
2674 CODA_REG_BIT_FRAME_MEM_CTRL);
2675 else
2676 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2678 if (dev->devtype->product != CODA_DX6)
2679 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2681 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2682 CODA_REG_BIT_INT_ENABLE);
2684 /* Reset VPU and start processor */
2685 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2686 data |= CODA_REG_RESET_ENABLE;
2687 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2688 udelay(10);
2689 data &= ~CODA_REG_RESET_ENABLE;
2690 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2691 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2693 clk_disable_unprepare(dev->clk_ahb);
2694 clk_disable_unprepare(dev->clk_per);
2696 return 0;
2698 err_clk_ahb:
2699 clk_disable_unprepare(dev->clk_per);
2700 err_clk_per:
2701 return ret;
2704 static int coda_register_device(struct coda_dev *dev, int i)
2706 struct video_device *vfd = &dev->vfd[i];
2707 enum coda_inst_type type;
2708 int ret;
2710 if (i >= dev->devtype->num_vdevs)
2711 return -EINVAL;
2712 type = dev->devtype->vdevs[i]->type;
2714 strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
2715 vfd->fops = &coda_fops;
2716 vfd->ioctl_ops = &coda_ioctl_ops;
2717 vfd->release = video_device_release_empty,
2718 vfd->lock = &dev->dev_mutex;
2719 vfd->v4l2_dev = &dev->v4l2_dev;
2720 vfd->vfl_dir = VFL_DIR_M2M;
2721 vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
2722 video_set_drvdata(vfd, dev);
2724 /* Not applicable, use the selection API instead */
2725 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
2726 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
2727 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
2729 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
2730 if (!ret)
2731 v4l2_info(&dev->v4l2_dev, "%s registered as %s\n",
2732 type == CODA_INST_ENCODER ? "encoder" : "decoder",
2733 video_device_node_name(vfd));
2734 return ret;
2737 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
2738 size_t size)
2740 u32 *src = (u32 *)buf;
2742 /* Check if the firmware has a 16-byte Freescale header, skip it */
2743 if (buf[0] == 'M' && buf[1] == 'X')
2744 src += 4;
2746 * Check whether the firmware is in native order or pre-reordered for
2747 * memory access. The first instruction opcode always is 0xe40e.
2749 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
2750 u32 *dst = dev->codebuf.vaddr;
2751 int i;
2753 /* Firmware in native order, reorder while copying */
2754 if (dev->devtype->product == CODA_DX6) {
2755 for (i = 0; i < (size - 16) / 4; i++)
2756 dst[i] = (src[i] << 16) | (src[i] >> 16);
2757 } else {
2758 for (i = 0; i < (size - 16) / 4; i += 2) {
2759 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
2760 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
2763 } else {
2764 /* Copy the already reordered firmware image */
2765 memcpy(dev->codebuf.vaddr, src, size);
2769 static void coda_fw_callback(const struct firmware *fw, void *context);
2771 static int coda_firmware_request(struct coda_dev *dev)
2773 char *fw;
2775 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware))
2776 return -EINVAL;
2778 fw = dev->devtype->firmware[dev->firmware];
2780 dev_dbg(dev->dev, "requesting firmware '%s' for %s\n", fw,
2781 coda_product_name(dev->devtype->product));
2783 return request_firmware_nowait(THIS_MODULE, true, fw, dev->dev,
2784 GFP_KERNEL, dev, coda_fw_callback);
2787 static void coda_fw_callback(const struct firmware *fw, void *context)
2789 struct coda_dev *dev = context;
2790 int i, ret;
2792 if (!fw) {
2793 dev->firmware++;
2794 ret = coda_firmware_request(dev);
2795 if (ret < 0) {
2796 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2797 goto put_pm;
2799 return;
2801 if (dev->firmware > 0) {
2803 * Since we can't suppress warnings for failed asynchronous
2804 * firmware requests, report that the fallback firmware was
2805 * found.
2807 dev_info(dev->dev, "Using fallback firmware %s\n",
2808 dev->devtype->firmware[dev->firmware]);
2811 /* allocate auxiliary per-device code buffer for the BIT processor */
2812 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2813 dev->debugfs_root);
2814 if (ret < 0)
2815 goto put_pm;
2817 coda_copy_firmware(dev, fw->data, fw->size);
2818 release_firmware(fw);
2820 ret = coda_hw_init(dev);
2821 if (ret < 0) {
2822 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2823 goto put_pm;
2826 ret = coda_check_firmware(dev);
2827 if (ret < 0)
2828 goto put_pm;
2830 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2831 if (IS_ERR(dev->m2m_dev)) {
2832 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2833 goto put_pm;
2836 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2837 ret = coda_register_device(dev, i);
2838 if (ret) {
2839 v4l2_err(&dev->v4l2_dev,
2840 "Failed to register %s video device: %d\n",
2841 dev->devtype->vdevs[i]->name, ret);
2842 goto rel_vfd;
2846 pm_runtime_put_sync(dev->dev);
2847 return;
2849 rel_vfd:
2850 while (--i >= 0)
2851 video_unregister_device(&dev->vfd[i]);
2852 v4l2_m2m_release(dev->m2m_dev);
2853 put_pm:
2854 pm_runtime_put_sync(dev->dev);
2857 enum coda_platform {
2858 CODA_IMX27,
2859 CODA_IMX51,
2860 CODA_IMX53,
2861 CODA_IMX6Q,
2862 CODA_IMX6DL,
2865 static const struct coda_devtype coda_devdata[] = {
2866 [CODA_IMX27] = {
2867 .firmware = {
2868 "vpu_fw_imx27_TO2.bin",
2869 "vpu/vpu_fw_imx27_TO2.bin",
2870 "v4l-codadx6-imx27.bin"
2872 .product = CODA_DX6,
2873 .codecs = codadx6_codecs,
2874 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2875 .vdevs = codadx6_video_devices,
2876 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2877 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2878 .iram_size = 0xb000,
2880 [CODA_IMX51] = {
2881 .firmware = {
2882 "vpu_fw_imx51.bin",
2883 "vpu/vpu_fw_imx51.bin",
2884 "v4l-codahx4-imx51.bin"
2886 .product = CODA_HX4,
2887 .codecs = codahx4_codecs,
2888 .num_codecs = ARRAY_SIZE(codahx4_codecs),
2889 .vdevs = codahx4_video_devices,
2890 .num_vdevs = ARRAY_SIZE(codahx4_video_devices),
2891 .workbuf_size = 128 * 1024,
2892 .tempbuf_size = 304 * 1024,
2893 .iram_size = 0x14000,
2895 [CODA_IMX53] = {
2896 .firmware = {
2897 "vpu_fw_imx53.bin",
2898 "vpu/vpu_fw_imx53.bin",
2899 "v4l-coda7541-imx53.bin"
2901 .product = CODA_7541,
2902 .codecs = coda7_codecs,
2903 .num_codecs = ARRAY_SIZE(coda7_codecs),
2904 .vdevs = coda7_video_devices,
2905 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2906 .workbuf_size = 128 * 1024,
2907 .tempbuf_size = 304 * 1024,
2908 .iram_size = 0x14000,
2910 [CODA_IMX6Q] = {
2911 .firmware = {
2912 "vpu_fw_imx6q.bin",
2913 "vpu/vpu_fw_imx6q.bin",
2914 "v4l-coda960-imx6q.bin"
2916 .product = CODA_960,
2917 .codecs = coda9_codecs,
2918 .num_codecs = ARRAY_SIZE(coda9_codecs),
2919 .vdevs = coda9_video_devices,
2920 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2921 .workbuf_size = 80 * 1024,
2922 .tempbuf_size = 204 * 1024,
2923 .iram_size = 0x21000,
2925 [CODA_IMX6DL] = {
2926 .firmware = {
2927 "vpu_fw_imx6d.bin",
2928 "vpu/vpu_fw_imx6d.bin",
2929 "v4l-coda960-imx6dl.bin"
2931 .product = CODA_960,
2932 .codecs = coda9_codecs,
2933 .num_codecs = ARRAY_SIZE(coda9_codecs),
2934 .vdevs = coda9_video_devices,
2935 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2936 .workbuf_size = 80 * 1024,
2937 .tempbuf_size = 204 * 1024,
2938 .iram_size = 0x1f000, /* leave 4k for suspend code */
2942 static const struct platform_device_id coda_platform_ids[] = {
2943 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2944 { /* sentinel */ }
2946 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2948 #ifdef CONFIG_OF
2949 static const struct of_device_id coda_dt_ids[] = {
2950 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2951 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] },
2952 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2953 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2954 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2955 { /* sentinel */ }
2957 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2958 #endif
2960 static int coda_probe(struct platform_device *pdev)
2962 const struct of_device_id *of_id =
2963 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2964 const struct platform_device_id *pdev_id;
2965 struct coda_platform_data *pdata = pdev->dev.platform_data;
2966 struct device_node *np = pdev->dev.of_node;
2967 struct gen_pool *pool;
2968 struct coda_dev *dev;
2969 int ret, irq;
2971 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2972 if (!dev)
2973 return -ENOMEM;
2975 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2977 if (of_id)
2978 dev->devtype = of_id->data;
2979 else if (pdev_id)
2980 dev->devtype = &coda_devdata[pdev_id->driver_data];
2981 else
2982 return -EINVAL;
2984 dev->dev = &pdev->dev;
2985 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2986 if (IS_ERR(dev->clk_per)) {
2987 dev_err(&pdev->dev, "Could not get per clock\n");
2988 return PTR_ERR(dev->clk_per);
2991 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2992 if (IS_ERR(dev->clk_ahb)) {
2993 dev_err(&pdev->dev, "Could not get ahb clock\n");
2994 return PTR_ERR(dev->clk_ahb);
2997 /* Get memory for physical registers */
2998 dev->regs_base = devm_platform_ioremap_resource(pdev, 0);
2999 if (IS_ERR(dev->regs_base))
3000 return PTR_ERR(dev->regs_base);
3002 /* IRQ */
3003 irq = platform_get_irq_byname(pdev, "bit");
3004 if (irq < 0)
3005 irq = platform_get_irq(pdev, 0);
3006 if (irq < 0)
3007 return irq;
3009 ret = devm_request_irq(&pdev->dev, irq, coda_irq_handler, 0,
3010 dev_name(&pdev->dev), dev);
3011 if (ret < 0) {
3012 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3013 return ret;
3016 /* JPEG IRQ */
3017 if (dev->devtype->product == CODA_960) {
3018 irq = platform_get_irq_byname(pdev, "jpeg");
3019 if (irq < 0)
3020 return irq;
3022 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
3023 coda9_jpeg_irq_handler,
3024 IRQF_ONESHOT, CODA_NAME " jpeg",
3025 dev);
3026 if (ret < 0) {
3027 dev_err(&pdev->dev, "failed to request jpeg irq\n");
3028 return ret;
3032 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev,
3033 NULL);
3034 if (IS_ERR(dev->rstc)) {
3035 ret = PTR_ERR(dev->rstc);
3036 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3037 return ret;
3040 /* Get IRAM pool from device tree or platform data */
3041 pool = of_gen_pool_get(np, "iram", 0);
3042 if (!pool && pdata)
3043 pool = gen_pool_get(pdata->iram_dev, NULL);
3044 if (!pool) {
3045 dev_err(&pdev->dev, "iram pool not available\n");
3046 return -ENOMEM;
3048 dev->iram_pool = pool;
3050 /* Get vdoa_data if supported by the platform */
3051 dev->vdoa = coda_get_vdoa_data();
3052 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER)
3053 return -EPROBE_DEFER;
3055 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3056 if (ret)
3057 return ret;
3059 mutex_init(&dev->dev_mutex);
3060 mutex_init(&dev->coda_mutex);
3061 ida_init(&dev->ida);
3063 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3064 if (!dev->debugfs_root)
3065 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3067 /* allocate auxiliary per-device buffers for the BIT processor */
3068 if (dev->devtype->product == CODA_DX6) {
3069 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3070 dev->devtype->workbuf_size, "workbuf",
3071 dev->debugfs_root);
3072 if (ret < 0)
3073 goto err_v4l2_register;
3076 if (dev->devtype->tempbuf_size) {
3077 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3078 dev->devtype->tempbuf_size, "tempbuf",
3079 dev->debugfs_root);
3080 if (ret < 0)
3081 goto err_v4l2_register;
3084 dev->iram.size = dev->devtype->iram_size;
3085 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3086 &dev->iram.paddr);
3087 if (!dev->iram.vaddr) {
3088 dev_warn(&pdev->dev, "unable to alloc iram\n");
3089 } else {
3090 memset(dev->iram.vaddr, 0, dev->iram.size);
3091 dev->iram.blob.data = dev->iram.vaddr;
3092 dev->iram.blob.size = dev->iram.size;
3093 dev->iram.dentry = debugfs_create_blob("iram", 0644,
3094 dev->debugfs_root,
3095 &dev->iram.blob);
3098 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3099 if (!dev->workqueue) {
3100 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3101 ret = -ENOMEM;
3102 goto err_v4l2_register;
3105 platform_set_drvdata(pdev, dev);
3108 * Start activated so we can directly call coda_hw_init in
3109 * coda_fw_callback regardless of whether CONFIG_PM is
3110 * enabled or whether the device is associated with a PM domain.
3112 pm_runtime_get_noresume(&pdev->dev);
3113 pm_runtime_set_active(&pdev->dev);
3114 pm_runtime_enable(&pdev->dev);
3116 ret = coda_firmware_request(dev);
3117 if (ret)
3118 goto err_alloc_workqueue;
3119 return 0;
3121 err_alloc_workqueue:
3122 destroy_workqueue(dev->workqueue);
3123 err_v4l2_register:
3124 v4l2_device_unregister(&dev->v4l2_dev);
3125 return ret;
3128 static int coda_remove(struct platform_device *pdev)
3130 struct coda_dev *dev = platform_get_drvdata(pdev);
3131 int i;
3133 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
3134 if (video_get_drvdata(&dev->vfd[i]))
3135 video_unregister_device(&dev->vfd[i]);
3137 if (dev->m2m_dev)
3138 v4l2_m2m_release(dev->m2m_dev);
3139 pm_runtime_disable(&pdev->dev);
3140 v4l2_device_unregister(&dev->v4l2_dev);
3141 destroy_workqueue(dev->workqueue);
3142 if (dev->iram.vaddr)
3143 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3144 dev->iram.size);
3145 coda_free_aux_buf(dev, &dev->codebuf);
3146 coda_free_aux_buf(dev, &dev->tempbuf);
3147 coda_free_aux_buf(dev, &dev->workbuf);
3148 debugfs_remove_recursive(dev->debugfs_root);
3149 ida_destroy(&dev->ida);
3150 return 0;
3153 #ifdef CONFIG_PM
3154 static int coda_runtime_resume(struct device *dev)
3156 struct coda_dev *cdev = dev_get_drvdata(dev);
3157 int ret = 0;
3159 if (dev->pm_domain && cdev->codebuf.vaddr) {
3160 ret = coda_hw_init(cdev);
3161 if (ret)
3162 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3165 return ret;
3167 #endif
3169 static const struct dev_pm_ops coda_pm_ops = {
3170 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3173 static struct platform_driver coda_driver = {
3174 .probe = coda_probe,
3175 .remove = coda_remove,
3176 .driver = {
3177 .name = CODA_NAME,
3178 .of_match_table = of_match_ptr(coda_dt_ids),
3179 .pm = &coda_pm_ops,
3181 .id_table = coda_platform_ids,
3184 module_platform_driver(coda_driver);
3186 MODULE_LICENSE("GPL");
3187 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3188 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");