Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / media / platform / coda / coda-common.c
blob133ab9f70f851c4a71b89bf0e4c19b039018bf78
1 /*
2 * Coda multi-standard codec IP
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/clk.h>
15 #include <linux/debugfs.h>
16 #include <linux/delay.h>
17 #include <linux/firmware.h>
18 #include <linux/gcd.h>
19 #include <linux/genalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
22 #include <linux/irq.h>
23 #include <linux/kfifo.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/slab.h>
29 #include <linux/videodev2.h>
30 #include <linux/of.h>
31 #include <linux/platform_data/media/coda.h>
32 #include <linux/reset.h>
34 #include <media/v4l2-ctrls.h>
35 #include <media/v4l2-device.h>
36 #include <media/v4l2-event.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-mem2mem.h>
39 #include <media/videobuf2-v4l2.h>
40 #include <media/videobuf2-dma-contig.h>
41 #include <media/videobuf2-vmalloc.h>
43 #include "coda.h"
45 #define CODA_NAME "coda"
47 #define CODADX6_MAX_INSTANCES 4
48 #define CODA_MAX_FORMATS 4
50 #define CODA_ISRAM_SIZE (2048 * 2)
52 #define MIN_W 176
53 #define MIN_H 144
55 #define S_ALIGN 1 /* multiple of 2 */
56 #define W_ALIGN 1 /* multiple of 2 */
57 #define H_ALIGN 1 /* multiple of 2 */
59 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
61 int coda_debug;
62 module_param(coda_debug, int, 0644);
63 MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
65 static int disable_tiling;
66 module_param(disable_tiling, int, 0644);
67 MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers");
69 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
71 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
72 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
73 writel(data, dev->regs_base + reg);
76 unsigned int coda_read(struct coda_dev *dev, u32 reg)
78 u32 data;
80 data = readl(dev->regs_base + reg);
81 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
82 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
83 return data;
86 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
87 struct vb2_v4l2_buffer *buf, unsigned int reg_y)
89 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0);
90 u32 base_cb, base_cr;
92 switch (q_data->fourcc) {
93 case V4L2_PIX_FMT_NV12:
94 case V4L2_PIX_FMT_YUV420:
95 default:
96 base_cb = base_y + q_data->bytesperline * q_data->height;
97 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
98 break;
99 case V4L2_PIX_FMT_YVU420:
100 /* Switch Cb and Cr for YVU420 format */
101 base_cr = base_y + q_data->bytesperline * q_data->height;
102 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
103 break;
104 case V4L2_PIX_FMT_YUV422P:
105 base_cb = base_y + q_data->bytesperline * q_data->height;
106 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
109 coda_write(ctx->dev, base_y, reg_y);
110 coda_write(ctx->dev, base_cb, reg_y + 4);
111 coda_write(ctx->dev, base_cr, reg_y + 8);
114 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
115 { mode, src_fourcc, dst_fourcc, max_w, max_h }
118 * Arrays of codecs supported by each given version of Coda:
119 * i.MX27 -> codadx6
120 * i.MX5x -> coda7
121 * i.MX6 -> coda960
122 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
124 static const struct coda_codec codadx6_codecs[] = {
125 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
126 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
129 static const struct coda_codec coda7_codecs[] = {
130 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
131 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
132 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
133 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
134 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
135 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
136 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
139 static const struct coda_codec coda9_codecs[] = {
140 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
141 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
142 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
143 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088),
144 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
147 struct coda_video_device {
148 const char *name;
149 enum coda_inst_type type;
150 const struct coda_context_ops *ops;
151 bool direct;
152 u32 src_formats[CODA_MAX_FORMATS];
153 u32 dst_formats[CODA_MAX_FORMATS];
156 static const struct coda_video_device coda_bit_encoder = {
157 .name = "coda-encoder",
158 .type = CODA_INST_ENCODER,
159 .ops = &coda_bit_encode_ops,
160 .src_formats = {
161 V4L2_PIX_FMT_NV12,
162 V4L2_PIX_FMT_YUV420,
163 V4L2_PIX_FMT_YVU420,
165 .dst_formats = {
166 V4L2_PIX_FMT_H264,
167 V4L2_PIX_FMT_MPEG4,
171 static const struct coda_video_device coda_bit_jpeg_encoder = {
172 .name = "coda-jpeg-encoder",
173 .type = CODA_INST_ENCODER,
174 .ops = &coda_bit_encode_ops,
175 .src_formats = {
176 V4L2_PIX_FMT_NV12,
177 V4L2_PIX_FMT_YUV420,
178 V4L2_PIX_FMT_YVU420,
179 V4L2_PIX_FMT_YUV422P,
181 .dst_formats = {
182 V4L2_PIX_FMT_JPEG,
186 static const struct coda_video_device coda_bit_decoder = {
187 .name = "coda-decoder",
188 .type = CODA_INST_DECODER,
189 .ops = &coda_bit_decode_ops,
190 .src_formats = {
191 V4L2_PIX_FMT_H264,
192 V4L2_PIX_FMT_MPEG2,
193 V4L2_PIX_FMT_MPEG4,
195 .dst_formats = {
196 V4L2_PIX_FMT_NV12,
197 V4L2_PIX_FMT_YUV420,
198 V4L2_PIX_FMT_YVU420,
202 static const struct coda_video_device coda_bit_jpeg_decoder = {
203 .name = "coda-jpeg-decoder",
204 .type = CODA_INST_DECODER,
205 .ops = &coda_bit_decode_ops,
206 .src_formats = {
207 V4L2_PIX_FMT_JPEG,
209 .dst_formats = {
210 V4L2_PIX_FMT_NV12,
211 V4L2_PIX_FMT_YUV420,
212 V4L2_PIX_FMT_YVU420,
213 V4L2_PIX_FMT_YUV422P,
217 static const struct coda_video_device *codadx6_video_devices[] = {
218 &coda_bit_encoder,
221 static const struct coda_video_device *coda7_video_devices[] = {
222 &coda_bit_jpeg_encoder,
223 &coda_bit_jpeg_decoder,
224 &coda_bit_encoder,
225 &coda_bit_decoder,
228 static const struct coda_video_device *coda9_video_devices[] = {
229 &coda_bit_encoder,
230 &coda_bit_decoder,
234 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
235 * tables.
237 static u32 coda_format_normalize_yuv(u32 fourcc)
239 switch (fourcc) {
240 case V4L2_PIX_FMT_NV12:
241 case V4L2_PIX_FMT_YUV420:
242 case V4L2_PIX_FMT_YVU420:
243 case V4L2_PIX_FMT_YUV422P:
244 return V4L2_PIX_FMT_YUV420;
245 default:
246 return fourcc;
250 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
251 int src_fourcc, int dst_fourcc)
253 const struct coda_codec *codecs = dev->devtype->codecs;
254 int num_codecs = dev->devtype->num_codecs;
255 int k;
257 src_fourcc = coda_format_normalize_yuv(src_fourcc);
258 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
259 if (src_fourcc == dst_fourcc)
260 return NULL;
262 for (k = 0; k < num_codecs; k++) {
263 if (codecs[k].src_fourcc == src_fourcc &&
264 codecs[k].dst_fourcc == dst_fourcc)
265 break;
268 if (k == num_codecs)
269 return NULL;
271 return &codecs[k];
274 static void coda_get_max_dimensions(struct coda_dev *dev,
275 const struct coda_codec *codec,
276 int *max_w, int *max_h)
278 const struct coda_codec *codecs = dev->devtype->codecs;
279 int num_codecs = dev->devtype->num_codecs;
280 unsigned int w, h;
281 int k;
283 if (codec) {
284 w = codec->max_w;
285 h = codec->max_h;
286 } else {
287 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
288 w = max(w, codecs[k].max_w);
289 h = max(h, codecs[k].max_h);
293 if (max_w)
294 *max_w = w;
295 if (max_h)
296 *max_h = h;
299 static const struct coda_video_device *to_coda_video_device(struct video_device
300 *vdev)
302 struct coda_dev *dev = video_get_drvdata(vdev);
303 unsigned int i = vdev - dev->vfd;
305 if (i >= dev->devtype->num_vdevs)
306 return NULL;
308 return dev->devtype->vdevs[i];
311 const char *coda_product_name(int product)
313 static char buf[9];
315 switch (product) {
316 case CODA_DX6:
317 return "CodaDx6";
318 case CODA_7541:
319 return "CODA7541";
320 case CODA_960:
321 return "CODA960";
322 default:
323 snprintf(buf, sizeof(buf), "(0x%04x)", product);
324 return buf;
329 * V4L2 ioctl() operations.
331 static int coda_querycap(struct file *file, void *priv,
332 struct v4l2_capability *cap)
334 struct coda_ctx *ctx = fh_to_ctx(priv);
336 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
337 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
338 sizeof(cap->card));
339 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
340 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
341 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
343 return 0;
346 static int coda_enum_fmt(struct file *file, void *priv,
347 struct v4l2_fmtdesc *f)
349 struct video_device *vdev = video_devdata(file);
350 const struct coda_video_device *cvd = to_coda_video_device(vdev);
351 const u32 *formats;
353 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
354 formats = cvd->src_formats;
355 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
356 formats = cvd->dst_formats;
357 else
358 return -EINVAL;
360 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
361 return -EINVAL;
363 f->pixelformat = formats[f->index];
365 return 0;
368 static int coda_g_fmt(struct file *file, void *priv,
369 struct v4l2_format *f)
371 struct coda_q_data *q_data;
372 struct coda_ctx *ctx = fh_to_ctx(priv);
374 q_data = get_q_data(ctx, f->type);
375 if (!q_data)
376 return -EINVAL;
378 f->fmt.pix.field = V4L2_FIELD_NONE;
379 f->fmt.pix.pixelformat = q_data->fourcc;
380 f->fmt.pix.width = q_data->width;
381 f->fmt.pix.height = q_data->height;
382 f->fmt.pix.bytesperline = q_data->bytesperline;
384 f->fmt.pix.sizeimage = q_data->sizeimage;
385 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
386 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
387 else
388 f->fmt.pix.colorspace = ctx->colorspace;
390 return 0;
393 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
395 struct coda_q_data *q_data;
396 const u32 *formats;
397 int i;
399 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
400 formats = ctx->cvd->src_formats;
401 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
402 formats = ctx->cvd->dst_formats;
403 else
404 return -EINVAL;
406 for (i = 0; i < CODA_MAX_FORMATS; i++) {
407 if (formats[i] == f->fmt.pix.pixelformat) {
408 f->fmt.pix.pixelformat = formats[i];
409 return 0;
413 /* Fall back to currently set pixelformat */
414 q_data = get_q_data(ctx, f->type);
415 f->fmt.pix.pixelformat = q_data->fourcc;
417 return 0;
420 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
421 u32 width, u32 height)
424 * This is a rough estimate for sensible compressed buffer
425 * sizes (between 1 and 16 bits per pixel). This could be
426 * improved by better format specific worst case estimates.
428 return round_up(clamp(sizeimage, width * height / 8,
429 width * height * 2), PAGE_SIZE);
432 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
433 struct v4l2_format *f)
435 struct coda_dev *dev = ctx->dev;
436 unsigned int max_w, max_h;
437 enum v4l2_field field;
439 field = f->fmt.pix.field;
440 if (field == V4L2_FIELD_ANY)
441 field = V4L2_FIELD_NONE;
442 else if (V4L2_FIELD_NONE != field)
443 return -EINVAL;
445 /* V4L2 specification suggests the driver corrects the format struct
446 * if any of the dimensions is unsupported */
447 f->fmt.pix.field = field;
449 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
450 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
451 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
452 S_ALIGN);
454 switch (f->fmt.pix.pixelformat) {
455 case V4L2_PIX_FMT_NV12:
456 case V4L2_PIX_FMT_YUV420:
457 case V4L2_PIX_FMT_YVU420:
459 * Frame stride must be at least multiple of 8,
460 * but multiple of 16 for h.264 or JPEG 4:2:x
462 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
463 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
464 f->fmt.pix.height * 3 / 2;
465 break;
466 case V4L2_PIX_FMT_YUV422P:
467 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
468 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
469 f->fmt.pix.height * 2;
470 break;
471 case V4L2_PIX_FMT_JPEG:
472 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
473 /* fallthrough */
474 case V4L2_PIX_FMT_H264:
475 case V4L2_PIX_FMT_MPEG4:
476 case V4L2_PIX_FMT_MPEG2:
477 f->fmt.pix.bytesperline = 0;
478 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
479 f->fmt.pix.sizeimage,
480 f->fmt.pix.width,
481 f->fmt.pix.height);
482 break;
483 default:
484 BUG();
487 return 0;
490 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
491 struct v4l2_format *f)
493 struct coda_ctx *ctx = fh_to_ctx(priv);
494 const struct coda_q_data *q_data_src;
495 const struct coda_codec *codec;
496 struct vb2_queue *src_vq;
497 int ret;
499 ret = coda_try_pixelformat(ctx, f);
500 if (ret < 0)
501 return ret;
503 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
506 * If the source format is already fixed, only allow the same output
507 * resolution
509 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
510 if (vb2_is_streaming(src_vq)) {
511 f->fmt.pix.width = q_data_src->width;
512 f->fmt.pix.height = q_data_src->height;
515 f->fmt.pix.colorspace = ctx->colorspace;
517 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
518 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
519 f->fmt.pix.pixelformat);
520 if (!codec)
521 return -EINVAL;
523 ret = coda_try_fmt(ctx, codec, f);
524 if (ret < 0)
525 return ret;
527 /* The h.264 decoder only returns complete 16x16 macroblocks */
528 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
529 f->fmt.pix.width = f->fmt.pix.width;
530 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
531 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
532 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
533 f->fmt.pix.height * 3 / 2;
536 return 0;
539 static int coda_try_fmt_vid_out(struct file *file, void *priv,
540 struct v4l2_format *f)
542 struct coda_ctx *ctx = fh_to_ctx(priv);
543 struct coda_dev *dev = ctx->dev;
544 const struct coda_q_data *q_data_dst;
545 const struct coda_codec *codec;
546 int ret;
548 ret = coda_try_pixelformat(ctx, f);
549 if (ret < 0)
550 return ret;
552 switch (f->fmt.pix.colorspace) {
553 case V4L2_COLORSPACE_REC709:
554 case V4L2_COLORSPACE_JPEG:
555 break;
556 default:
557 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
558 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
559 else
560 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
563 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
564 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
566 return coda_try_fmt(ctx, codec, f);
569 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
571 struct coda_q_data *q_data;
572 struct vb2_queue *vq;
574 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
575 if (!vq)
576 return -EINVAL;
578 q_data = get_q_data(ctx, f->type);
579 if (!q_data)
580 return -EINVAL;
582 if (vb2_is_busy(vq)) {
583 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
584 return -EBUSY;
587 q_data->fourcc = f->fmt.pix.pixelformat;
588 q_data->width = f->fmt.pix.width;
589 q_data->height = f->fmt.pix.height;
590 q_data->bytesperline = f->fmt.pix.bytesperline;
591 q_data->sizeimage = f->fmt.pix.sizeimage;
592 q_data->rect.left = 0;
593 q_data->rect.top = 0;
594 q_data->rect.width = f->fmt.pix.width;
595 q_data->rect.height = f->fmt.pix.height;
597 switch (f->fmt.pix.pixelformat) {
598 case V4L2_PIX_FMT_NV12:
599 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
600 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP;
601 if (!disable_tiling)
602 break;
604 /* else fall through */
605 case V4L2_PIX_FMT_YUV420:
606 case V4L2_PIX_FMT_YVU420:
607 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
608 break;
609 default:
610 break;
613 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
614 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
615 f->type, q_data->width, q_data->height, q_data->fourcc);
617 return 0;
620 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
621 struct v4l2_format *f)
623 struct coda_ctx *ctx = fh_to_ctx(priv);
624 int ret;
626 ret = coda_try_fmt_vid_cap(file, priv, f);
627 if (ret)
628 return ret;
630 return coda_s_fmt(ctx, f);
633 static int coda_s_fmt_vid_out(struct file *file, void *priv,
634 struct v4l2_format *f)
636 struct coda_ctx *ctx = fh_to_ctx(priv);
637 struct v4l2_format f_cap;
638 int ret;
640 ret = coda_try_fmt_vid_out(file, priv, f);
641 if (ret)
642 return ret;
644 ret = coda_s_fmt(ctx, f);
645 if (ret)
646 return ret;
648 ctx->colorspace = f->fmt.pix.colorspace;
650 memset(&f_cap, 0, sizeof(f_cap));
651 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
652 coda_g_fmt(file, priv, &f_cap);
653 f_cap.fmt.pix.width = f->fmt.pix.width;
654 f_cap.fmt.pix.height = f->fmt.pix.height;
656 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
657 if (ret)
658 return ret;
660 return coda_s_fmt(ctx, &f_cap);
663 static int coda_reqbufs(struct file *file, void *priv,
664 struct v4l2_requestbuffers *rb)
666 struct coda_ctx *ctx = fh_to_ctx(priv);
667 int ret;
669 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
670 if (ret)
671 return ret;
674 * Allow to allocate instance specific per-context buffers, such as
675 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
677 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
678 return ctx->ops->reqbufs(ctx, rb);
680 return 0;
683 static int coda_qbuf(struct file *file, void *priv,
684 struct v4l2_buffer *buf)
686 struct coda_ctx *ctx = fh_to_ctx(priv);
688 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
691 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
692 struct vb2_v4l2_buffer *buf)
694 struct vb2_queue *src_vq;
696 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
698 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
699 (buf->sequence == (ctx->qsequence - 1)));
702 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
703 enum vb2_buffer_state state)
705 const struct v4l2_event eos_event = {
706 .type = V4L2_EVENT_EOS
709 if (coda_buf_is_end_of_stream(ctx, buf)) {
710 buf->flags |= V4L2_BUF_FLAG_LAST;
712 v4l2_event_queue_fh(&ctx->fh, &eos_event);
715 v4l2_m2m_buf_done(buf, state);
718 static int coda_g_selection(struct file *file, void *fh,
719 struct v4l2_selection *s)
721 struct coda_ctx *ctx = fh_to_ctx(fh);
722 struct coda_q_data *q_data;
723 struct v4l2_rect r, *rsel;
725 q_data = get_q_data(ctx, s->type);
726 if (!q_data)
727 return -EINVAL;
729 r.left = 0;
730 r.top = 0;
731 r.width = q_data->width;
732 r.height = q_data->height;
733 rsel = &q_data->rect;
735 switch (s->target) {
736 case V4L2_SEL_TGT_CROP_DEFAULT:
737 case V4L2_SEL_TGT_CROP_BOUNDS:
738 rsel = &r;
739 /* fallthrough */
740 case V4L2_SEL_TGT_CROP:
741 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
742 return -EINVAL;
743 break;
744 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
745 case V4L2_SEL_TGT_COMPOSE_PADDED:
746 rsel = &r;
747 /* fallthrough */
748 case V4L2_SEL_TGT_COMPOSE:
749 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
750 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
751 return -EINVAL;
752 break;
753 default:
754 return -EINVAL;
757 s->r = *rsel;
759 return 0;
762 static int coda_try_decoder_cmd(struct file *file, void *fh,
763 struct v4l2_decoder_cmd *dc)
765 if (dc->cmd != V4L2_DEC_CMD_STOP)
766 return -EINVAL;
768 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
769 return -EINVAL;
771 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
772 return -EINVAL;
774 return 0;
777 static int coda_decoder_cmd(struct file *file, void *fh,
778 struct v4l2_decoder_cmd *dc)
780 struct coda_ctx *ctx = fh_to_ctx(fh);
781 int ret;
783 ret = coda_try_decoder_cmd(file, fh, dc);
784 if (ret < 0)
785 return ret;
787 /* Ignore decoder stop command silently in encoder context */
788 if (ctx->inst_type != CODA_INST_DECODER)
789 return 0;
791 /* Set the stream-end flag on this context */
792 coda_bit_stream_end_flag(ctx);
793 ctx->hold = false;
794 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
796 return 0;
799 static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
801 struct coda_ctx *ctx = fh_to_ctx(fh);
802 struct v4l2_fract *tpf;
804 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
805 return -EINVAL;
807 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
808 tpf = &a->parm.output.timeperframe;
809 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
810 tpf->numerator = 1 + (ctx->params.framerate >>
811 CODA_FRATE_DIV_OFFSET);
813 return 0;
817 * Approximate timeperframe v4l2_fract with values that can be written
818 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
820 static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
822 struct v4l2_fract s = *timeperframe;
823 struct v4l2_fract f0;
824 struct v4l2_fract f1 = { 1, 0 };
825 struct v4l2_fract f2 = { 0, 1 };
826 unsigned int i, div, s_denominator;
828 /* Lower bound is 1/65535 */
829 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
830 timeperframe->numerator = 1;
831 timeperframe->denominator = 65535;
832 return;
835 /* Upper bound is 65536/1, map everything above to infinity */
836 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
837 timeperframe->numerator = 1;
838 timeperframe->denominator = 0;
839 return;
842 /* Reduce fraction to lowest terms */
843 div = gcd(s.numerator, s.denominator);
844 if (div > 1) {
845 s.numerator /= div;
846 s.denominator /= div;
849 if (s.numerator <= 65536 && s.denominator < 65536) {
850 *timeperframe = s;
851 return;
854 /* Find successive convergents from continued fraction expansion */
855 while (f2.numerator <= 65536 && f2.denominator < 65536) {
856 f0 = f1;
857 f1 = f2;
859 /* Stop when f2 exactly equals timeperframe */
860 if (s.numerator == 0)
861 break;
863 i = s.denominator / s.numerator;
865 f2.numerator = f0.numerator + i * f1.numerator;
866 f2.denominator = f0.denominator + i * f2.denominator;
868 s_denominator = s.numerator;
869 s.numerator = s.denominator % s.numerator;
870 s.denominator = s_denominator;
873 *timeperframe = f1;
876 static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
878 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
879 timeperframe->denominator;
882 static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
884 struct coda_ctx *ctx = fh_to_ctx(fh);
885 struct v4l2_fract *tpf;
887 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
888 return -EINVAL;
890 tpf = &a->parm.output.timeperframe;
891 coda_approximate_timeperframe(tpf);
892 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
894 return 0;
897 static int coda_subscribe_event(struct v4l2_fh *fh,
898 const struct v4l2_event_subscription *sub)
900 switch (sub->type) {
901 case V4L2_EVENT_EOS:
902 return v4l2_event_subscribe(fh, sub, 0, NULL);
903 default:
904 return v4l2_ctrl_subscribe_event(fh, sub);
908 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
909 .vidioc_querycap = coda_querycap,
911 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
912 .vidioc_g_fmt_vid_cap = coda_g_fmt,
913 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
914 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
916 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
917 .vidioc_g_fmt_vid_out = coda_g_fmt,
918 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
919 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
921 .vidioc_reqbufs = coda_reqbufs,
922 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
924 .vidioc_qbuf = coda_qbuf,
925 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
926 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
927 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
928 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
930 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
931 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
933 .vidioc_g_selection = coda_g_selection,
935 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
936 .vidioc_decoder_cmd = coda_decoder_cmd,
938 .vidioc_g_parm = coda_g_parm,
939 .vidioc_s_parm = coda_s_parm,
941 .vidioc_subscribe_event = coda_subscribe_event,
942 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
946 * Mem-to-mem operations.
949 static void coda_device_run(void *m2m_priv)
951 struct coda_ctx *ctx = m2m_priv;
952 struct coda_dev *dev = ctx->dev;
954 queue_work(dev->workqueue, &ctx->pic_run_work);
957 static void coda_pic_run_work(struct work_struct *work)
959 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
960 struct coda_dev *dev = ctx->dev;
961 int ret;
963 mutex_lock(&ctx->buffer_mutex);
964 mutex_lock(&dev->coda_mutex);
966 ret = ctx->ops->prepare_run(ctx);
967 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
968 mutex_unlock(&dev->coda_mutex);
969 mutex_unlock(&ctx->buffer_mutex);
970 /* job_finish scheduled by prepare_decode */
971 return;
974 if (!wait_for_completion_timeout(&ctx->completion,
975 msecs_to_jiffies(1000))) {
976 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
978 ctx->hold = true;
980 coda_hw_reset(ctx);
981 } else if (!ctx->aborting) {
982 ctx->ops->finish_run(ctx);
985 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
986 ctx->ops->seq_end_work)
987 queue_work(dev->workqueue, &ctx->seq_end_work);
989 mutex_unlock(&dev->coda_mutex);
990 mutex_unlock(&ctx->buffer_mutex);
992 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
995 static int coda_job_ready(void *m2m_priv)
997 struct coda_ctx *ctx = m2m_priv;
998 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1001 * For both 'P' and 'key' frame cases 1 picture
1002 * and 1 frame are needed. In the decoder case,
1003 * the compressed frame can be in the bitstream.
1005 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
1006 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1007 "not ready: not enough video buffers.\n");
1008 return 0;
1011 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1012 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1013 "not ready: not enough video capture buffers.\n");
1014 return 0;
1017 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1018 bool stream_end = ctx->bit_stream_param &
1019 CODA_BIT_STREAM_END_FLAG;
1020 int num_metas = ctx->num_metas;
1022 if (ctx->hold && !src_bufs) {
1023 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1024 "%d: not ready: on hold for more buffers.\n",
1025 ctx->idx);
1026 return 0;
1029 if (!stream_end && (num_metas + src_bufs) < 2) {
1030 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1031 "%d: not ready: need 2 buffers available (%d, %d)\n",
1032 ctx->idx, num_metas, src_bufs);
1033 return 0;
1037 if (!src_bufs && !stream_end &&
1038 (coda_get_bitstream_payload(ctx) < 512)) {
1039 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1040 "%d: not ready: not enough bitstream data (%d).\n",
1041 ctx->idx, coda_get_bitstream_payload(ctx));
1042 return 0;
1046 if (ctx->aborting) {
1047 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1048 "not ready: aborting\n");
1049 return 0;
1052 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1053 "job ready\n");
1055 return 1;
1058 static void coda_job_abort(void *priv)
1060 struct coda_ctx *ctx = priv;
1062 ctx->aborting = 1;
1064 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1065 "Aborting task\n");
1068 static void coda_lock(void *m2m_priv)
1070 struct coda_ctx *ctx = m2m_priv;
1071 struct coda_dev *pcdev = ctx->dev;
1073 mutex_lock(&pcdev->dev_mutex);
1076 static void coda_unlock(void *m2m_priv)
1078 struct coda_ctx *ctx = m2m_priv;
1079 struct coda_dev *pcdev = ctx->dev;
1081 mutex_unlock(&pcdev->dev_mutex);
1084 static const struct v4l2_m2m_ops coda_m2m_ops = {
1085 .device_run = coda_device_run,
1086 .job_ready = coda_job_ready,
1087 .job_abort = coda_job_abort,
1088 .lock = coda_lock,
1089 .unlock = coda_unlock,
1092 static void set_default_params(struct coda_ctx *ctx)
1094 unsigned int max_w, max_h, usize, csize;
1096 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1097 ctx->cvd->dst_formats[0]);
1098 max_w = min(ctx->codec->max_w, 1920U);
1099 max_h = min(ctx->codec->max_h, 1088U);
1100 usize = max_w * max_h * 3 / 2;
1101 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1103 ctx->params.codec_mode = ctx->codec->mode;
1104 ctx->colorspace = V4L2_COLORSPACE_REC709;
1105 ctx->params.framerate = 30;
1107 /* Default formats for output and input queues */
1108 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0];
1109 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0];
1110 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1111 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1112 ctx->q_data[V4L2_M2M_DST].width = max_w;
1113 ctx->q_data[V4L2_M2M_DST].height = max_h;
1114 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1115 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1116 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1117 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1118 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1119 } else {
1120 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1121 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1122 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1123 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1125 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1126 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1127 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1128 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1131 * Since the RBC2AXI logic only supports a single chroma plane,
1132 * macroblock tiling only works for to NV12 pixel format.
1134 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP;
1138 * Queue operations
1140 static int coda_queue_setup(struct vb2_queue *vq,
1141 unsigned int *nbuffers, unsigned int *nplanes,
1142 unsigned int sizes[], void *alloc_ctxs[])
1144 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1145 struct coda_q_data *q_data;
1146 unsigned int size;
1148 q_data = get_q_data(ctx, vq->type);
1149 size = q_data->sizeimage;
1151 *nplanes = 1;
1152 sizes[0] = size;
1154 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1155 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1157 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1158 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1160 return 0;
1163 static int coda_buf_prepare(struct vb2_buffer *vb)
1165 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1166 struct coda_q_data *q_data;
1168 q_data = get_q_data(ctx, vb->vb2_queue->type);
1170 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1171 v4l2_warn(&ctx->dev->v4l2_dev,
1172 "%s data will not fit into plane (%lu < %lu)\n",
1173 __func__, vb2_plane_size(vb, 0),
1174 (long)q_data->sizeimage);
1175 return -EINVAL;
1178 return 0;
1181 static void coda_buf_queue(struct vb2_buffer *vb)
1183 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1184 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1185 struct vb2_queue *vq = vb->vb2_queue;
1186 struct coda_q_data *q_data;
1188 q_data = get_q_data(ctx, vb->vb2_queue->type);
1191 * In the decoder case, immediately try to copy the buffer into the
1192 * bitstream ringbuffer and mark it as ready to be dequeued.
1194 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1196 * For backwards compatibility, queuing an empty buffer marks
1197 * the stream end
1199 if (vb2_get_plane_payload(vb, 0) == 0)
1200 coda_bit_stream_end_flag(ctx);
1201 mutex_lock(&ctx->bitstream_mutex);
1202 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1203 if (vb2_is_streaming(vb->vb2_queue))
1204 coda_fill_bitstream(ctx, true);
1205 mutex_unlock(&ctx->bitstream_mutex);
1206 } else {
1207 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1211 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1212 size_t size, const char *name, struct dentry *parent)
1214 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1215 GFP_KERNEL);
1216 if (!buf->vaddr) {
1217 v4l2_err(&dev->v4l2_dev,
1218 "Failed to allocate %s buffer of size %u\n",
1219 name, size);
1220 return -ENOMEM;
1223 buf->size = size;
1225 if (name && parent) {
1226 buf->blob.data = buf->vaddr;
1227 buf->blob.size = size;
1228 buf->dentry = debugfs_create_blob(name, 0644, parent,
1229 &buf->blob);
1230 if (!buf->dentry)
1231 dev_warn(&dev->plat_dev->dev,
1232 "failed to create debugfs entry %s\n", name);
1235 return 0;
1238 void coda_free_aux_buf(struct coda_dev *dev,
1239 struct coda_aux_buf *buf)
1241 if (buf->vaddr) {
1242 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1243 buf->vaddr, buf->paddr);
1244 buf->vaddr = NULL;
1245 buf->size = 0;
1246 debugfs_remove(buf->dentry);
1247 buf->dentry = NULL;
1251 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1253 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1254 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1255 struct coda_q_data *q_data_src, *q_data_dst;
1256 struct vb2_v4l2_buffer *buf;
1257 int ret = 0;
1259 if (count < 1)
1260 return -EINVAL;
1262 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1263 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1264 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
1265 /* copy the buffers that were queued before streamon */
1266 mutex_lock(&ctx->bitstream_mutex);
1267 coda_fill_bitstream(ctx, false);
1268 mutex_unlock(&ctx->bitstream_mutex);
1270 if (coda_get_bitstream_payload(ctx) < 512) {
1271 ret = -EINVAL;
1272 goto err;
1276 ctx->streamon_out = 1;
1277 } else {
1278 ctx->streamon_cap = 1;
1281 /* Don't start the coda unless both queues are on */
1282 if (!(ctx->streamon_out & ctx->streamon_cap))
1283 return 0;
1285 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1286 if ((q_data_src->width != q_data_dst->width &&
1287 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1288 (q_data_src->height != q_data_dst->height &&
1289 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1290 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1291 q_data_src->width, q_data_src->height,
1292 q_data_dst->width, q_data_dst->height);
1293 ret = -EINVAL;
1294 goto err;
1297 /* Allow BIT decoder device_run with no new buffers queued */
1298 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1299 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1301 ctx->gopcounter = ctx->params.gop_size - 1;
1303 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1304 q_data_dst->fourcc);
1305 if (!ctx->codec) {
1306 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1307 ret = -EINVAL;
1308 goto err;
1311 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1312 ctx->params.gop_size = 1;
1313 ctx->gopcounter = ctx->params.gop_size - 1;
1315 ret = ctx->ops->start_streaming(ctx);
1316 if (ctx->inst_type == CODA_INST_DECODER) {
1317 if (ret == -EAGAIN)
1318 return 0;
1319 else if (ret < 0)
1320 goto err;
1323 return ret;
1325 err:
1326 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1327 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1328 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1329 } else {
1330 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1331 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1333 return ret;
1336 static void coda_stop_streaming(struct vb2_queue *q)
1338 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1339 struct coda_dev *dev = ctx->dev;
1340 struct vb2_v4l2_buffer *buf;
1341 unsigned long flags;
1342 bool stop;
1344 stop = ctx->streamon_out && ctx->streamon_cap;
1346 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1347 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1348 "%s: output\n", __func__);
1349 ctx->streamon_out = 0;
1351 coda_bit_stream_end_flag(ctx);
1353 ctx->qsequence = 0;
1355 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1356 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1357 } else {
1358 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1359 "%s: capture\n", __func__);
1360 ctx->streamon_cap = 0;
1362 ctx->osequence = 0;
1363 ctx->sequence_offset = 0;
1365 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1366 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1369 if (stop) {
1370 struct coda_buffer_meta *meta;
1372 if (ctx->ops->seq_end_work) {
1373 queue_work(dev->workqueue, &ctx->seq_end_work);
1374 flush_work(&ctx->seq_end_work);
1376 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
1377 while (!list_empty(&ctx->buffer_meta_list)) {
1378 meta = list_first_entry(&ctx->buffer_meta_list,
1379 struct coda_buffer_meta, list);
1380 list_del(&meta->list);
1381 kfree(meta);
1383 ctx->num_metas = 0;
1384 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
1385 kfifo_init(&ctx->bitstream_fifo,
1386 ctx->bitstream.vaddr, ctx->bitstream.size);
1387 ctx->runcounter = 0;
1388 ctx->aborting = 0;
1391 if (!ctx->streamon_out && !ctx->streamon_cap)
1392 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
1395 static const struct vb2_ops coda_qops = {
1396 .queue_setup = coda_queue_setup,
1397 .buf_prepare = coda_buf_prepare,
1398 .buf_queue = coda_buf_queue,
1399 .start_streaming = coda_start_streaming,
1400 .stop_streaming = coda_stop_streaming,
1401 .wait_prepare = vb2_ops_wait_prepare,
1402 .wait_finish = vb2_ops_wait_finish,
1405 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1407 struct coda_ctx *ctx =
1408 container_of(ctrl->handler, struct coda_ctx, ctrls);
1410 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1411 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1413 switch (ctrl->id) {
1414 case V4L2_CID_HFLIP:
1415 if (ctrl->val)
1416 ctx->params.rot_mode |= CODA_MIR_HOR;
1417 else
1418 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1419 break;
1420 case V4L2_CID_VFLIP:
1421 if (ctrl->val)
1422 ctx->params.rot_mode |= CODA_MIR_VER;
1423 else
1424 ctx->params.rot_mode &= ~CODA_MIR_VER;
1425 break;
1426 case V4L2_CID_MPEG_VIDEO_BITRATE:
1427 ctx->params.bitrate = ctrl->val / 1000;
1428 break;
1429 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1430 ctx->params.gop_size = ctrl->val;
1431 break;
1432 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1433 ctx->params.h264_intra_qp = ctrl->val;
1434 break;
1435 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1436 ctx->params.h264_inter_qp = ctrl->val;
1437 break;
1438 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1439 ctx->params.h264_min_qp = ctrl->val;
1440 break;
1441 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1442 ctx->params.h264_max_qp = ctrl->val;
1443 break;
1444 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1445 ctx->params.h264_deblk_alpha = ctrl->val;
1446 break;
1447 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1448 ctx->params.h264_deblk_beta = ctrl->val;
1449 break;
1450 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1451 ctx->params.h264_deblk_enabled = (ctrl->val ==
1452 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1453 break;
1454 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1455 ctx->params.mpeg4_intra_qp = ctrl->val;
1456 break;
1457 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1458 ctx->params.mpeg4_inter_qp = ctrl->val;
1459 break;
1460 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1461 ctx->params.slice_mode = ctrl->val;
1462 break;
1463 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1464 ctx->params.slice_max_mb = ctrl->val;
1465 break;
1466 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1467 ctx->params.slice_max_bits = ctrl->val * 8;
1468 break;
1469 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1470 break;
1471 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1472 ctx->params.intra_refresh = ctrl->val;
1473 break;
1474 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1475 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1476 break;
1477 case V4L2_CID_JPEG_RESTART_INTERVAL:
1478 ctx->params.jpeg_restart_interval = ctrl->val;
1479 break;
1480 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1481 ctx->params.vbv_delay = ctrl->val;
1482 break;
1483 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1484 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1485 break;
1486 default:
1487 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1488 "Invalid control, id=%d, val=%d\n",
1489 ctrl->id, ctrl->val);
1490 return -EINVAL;
1493 return 0;
1496 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1497 .s_ctrl = coda_s_ctrl,
1500 static void coda_encode_ctrls(struct coda_ctx *ctx)
1502 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1503 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1504 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1505 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1506 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1507 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1508 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1509 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1510 if (ctx->dev->devtype->product != CODA_960) {
1511 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1512 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1514 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1515 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1516 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1517 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1518 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1519 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1520 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1521 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1522 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1523 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1524 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1525 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1526 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1527 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1528 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1529 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1530 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1531 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1532 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1533 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1534 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1535 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1536 500);
1537 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1538 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1539 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1540 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1541 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1542 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1543 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1544 1920 * 1088 / 256, 1, 0);
1545 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1546 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1548 * The maximum VBV size value is 0x7fffffff bits,
1549 * one bit less than 262144 KiB
1551 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1552 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
1555 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1557 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1558 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1559 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1560 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1563 static int coda_ctrls_setup(struct coda_ctx *ctx)
1565 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1567 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1568 V4L2_CID_HFLIP, 0, 1, 1, 0);
1569 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1570 V4L2_CID_VFLIP, 0, 1, 1, 0);
1571 if (ctx->inst_type == CODA_INST_ENCODER) {
1572 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1573 coda_jpeg_encode_ctrls(ctx);
1574 else
1575 coda_encode_ctrls(ctx);
1578 if (ctx->ctrls.error) {
1579 v4l2_err(&ctx->dev->v4l2_dev,
1580 "control initialization error (%d)",
1581 ctx->ctrls.error);
1582 return -EINVAL;
1585 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1588 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1590 vq->drv_priv = ctx;
1591 vq->ops = &coda_qops;
1592 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1593 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1594 vq->lock = &ctx->dev->dev_mutex;
1595 /* One way to indicate end-of-stream for coda is to set the
1596 * bytesused == 0. However by default videobuf2 handles bytesused
1597 * equal to 0 as a special case and changes its value to the size
1598 * of the buffer. Set the allow_zero_bytesused flag, so
1599 * that videobuf2 will keep the value of bytesused intact.
1601 vq->allow_zero_bytesused = 1;
1603 return vb2_queue_init(vq);
1606 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1607 struct vb2_queue *dst_vq)
1609 int ret;
1611 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1612 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1613 src_vq->mem_ops = &vb2_dma_contig_memops;
1615 ret = coda_queue_init(priv, src_vq);
1616 if (ret)
1617 return ret;
1619 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1620 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1621 dst_vq->mem_ops = &vb2_dma_contig_memops;
1623 return coda_queue_init(priv, dst_vq);
1626 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1627 struct vb2_queue *dst_vq)
1629 int ret;
1631 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1632 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1633 src_vq->mem_ops = &vb2_vmalloc_memops;
1635 ret = coda_queue_init(priv, src_vq);
1636 if (ret)
1637 return ret;
1639 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1640 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1641 dst_vq->mem_ops = &vb2_dma_contig_memops;
1643 return coda_queue_init(priv, dst_vq);
1646 static int coda_next_free_instance(struct coda_dev *dev)
1648 int idx = ffz(dev->instance_mask);
1650 if ((idx < 0) ||
1651 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1652 return -EBUSY;
1654 return idx;
1658 * File operations
1661 static int coda_open(struct file *file)
1663 struct video_device *vdev = video_devdata(file);
1664 struct coda_dev *dev = video_get_drvdata(vdev);
1665 struct coda_ctx *ctx = NULL;
1666 char *name;
1667 int ret;
1668 int idx;
1670 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1671 if (!ctx)
1672 return -ENOMEM;
1674 idx = coda_next_free_instance(dev);
1675 if (idx < 0) {
1676 ret = idx;
1677 goto err_coda_max;
1679 set_bit(idx, &dev->instance_mask);
1681 name = kasprintf(GFP_KERNEL, "context%d", idx);
1682 if (!name) {
1683 ret = -ENOMEM;
1684 goto err_coda_name_init;
1687 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1688 kfree(name);
1690 ctx->cvd = to_coda_video_device(vdev);
1691 ctx->inst_type = ctx->cvd->type;
1692 ctx->ops = ctx->cvd->ops;
1693 ctx->use_bit = !ctx->cvd->direct;
1694 init_completion(&ctx->completion);
1695 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
1696 if (ctx->ops->seq_end_work)
1697 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1698 v4l2_fh_init(&ctx->fh, video_devdata(file));
1699 file->private_data = &ctx->fh;
1700 v4l2_fh_add(&ctx->fh);
1701 ctx->dev = dev;
1702 ctx->idx = idx;
1703 switch (dev->devtype->product) {
1704 case CODA_960:
1705 ctx->frame_mem_ctrl = 1 << 12;
1706 /* fallthrough */
1707 case CODA_7541:
1708 ctx->reg_idx = 0;
1709 break;
1710 default:
1711 ctx->reg_idx = idx;
1714 /* Power up and upload firmware if necessary */
1715 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1716 if (ret < 0) {
1717 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1718 goto err_pm_get;
1721 ret = clk_prepare_enable(dev->clk_per);
1722 if (ret)
1723 goto err_clk_per;
1725 ret = clk_prepare_enable(dev->clk_ahb);
1726 if (ret)
1727 goto err_clk_ahb;
1729 set_default_params(ctx);
1730 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1731 ctx->ops->queue_init);
1732 if (IS_ERR(ctx->fh.m2m_ctx)) {
1733 ret = PTR_ERR(ctx->fh.m2m_ctx);
1735 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1736 __func__, ret);
1737 goto err_ctx_init;
1740 ret = coda_ctrls_setup(ctx);
1741 if (ret) {
1742 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1743 goto err_ctrls_setup;
1746 ctx->fh.ctrl_handler = &ctx->ctrls;
1748 mutex_init(&ctx->bitstream_mutex);
1749 mutex_init(&ctx->buffer_mutex);
1750 INIT_LIST_HEAD(&ctx->buffer_meta_list);
1751 spin_lock_init(&ctx->buffer_meta_lock);
1753 coda_lock(ctx);
1754 list_add(&ctx->list, &dev->instances);
1755 coda_unlock(ctx);
1757 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1758 ctx->idx, ctx);
1760 return 0;
1762 err_ctrls_setup:
1763 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1764 err_ctx_init:
1765 clk_disable_unprepare(dev->clk_ahb);
1766 err_clk_ahb:
1767 clk_disable_unprepare(dev->clk_per);
1768 err_clk_per:
1769 pm_runtime_put_sync(&dev->plat_dev->dev);
1770 err_pm_get:
1771 v4l2_fh_del(&ctx->fh);
1772 v4l2_fh_exit(&ctx->fh);
1773 clear_bit(ctx->idx, &dev->instance_mask);
1774 err_coda_name_init:
1775 err_coda_max:
1776 kfree(ctx);
1777 return ret;
1780 static int coda_release(struct file *file)
1782 struct coda_dev *dev = video_drvdata(file);
1783 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1785 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1786 ctx);
1788 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1789 coda_bit_stream_end_flag(ctx);
1791 /* If this instance is running, call .job_abort and wait for it to end */
1792 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1794 /* In case the instance was not running, we still need to call SEQ_END */
1795 if (ctx->ops->seq_end_work) {
1796 queue_work(dev->workqueue, &ctx->seq_end_work);
1797 flush_work(&ctx->seq_end_work);
1800 coda_lock(ctx);
1801 list_del(&ctx->list);
1802 coda_unlock(ctx);
1804 if (ctx->dev->devtype->product == CODA_DX6)
1805 coda_free_aux_buf(dev, &ctx->workbuf);
1807 v4l2_ctrl_handler_free(&ctx->ctrls);
1808 clk_disable_unprepare(dev->clk_ahb);
1809 clk_disable_unprepare(dev->clk_per);
1810 pm_runtime_put_sync(&dev->plat_dev->dev);
1811 v4l2_fh_del(&ctx->fh);
1812 v4l2_fh_exit(&ctx->fh);
1813 clear_bit(ctx->idx, &dev->instance_mask);
1814 if (ctx->ops->release)
1815 ctx->ops->release(ctx);
1816 debugfs_remove_recursive(ctx->debugfs_entry);
1817 kfree(ctx);
1819 return 0;
1822 static const struct v4l2_file_operations coda_fops = {
1823 .owner = THIS_MODULE,
1824 .open = coda_open,
1825 .release = coda_release,
1826 .poll = v4l2_m2m_fop_poll,
1827 .unlocked_ioctl = video_ioctl2,
1828 .mmap = v4l2_m2m_fop_mmap,
1831 static int coda_hw_init(struct coda_dev *dev)
1833 u32 data;
1834 u16 *p;
1835 int i, ret;
1837 ret = clk_prepare_enable(dev->clk_per);
1838 if (ret)
1839 goto err_clk_per;
1841 ret = clk_prepare_enable(dev->clk_ahb);
1842 if (ret)
1843 goto err_clk_ahb;
1845 if (dev->rstc)
1846 reset_control_reset(dev->rstc);
1849 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1850 * The 16-bit chars in the code buffer are in memory access
1851 * order, re-sort them to CODA order for register download.
1852 * Data in this SRAM survives a reboot.
1854 p = (u16 *)dev->codebuf.vaddr;
1855 if (dev->devtype->product == CODA_DX6) {
1856 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1857 data = CODA_DOWN_ADDRESS_SET(i) |
1858 CODA_DOWN_DATA_SET(p[i ^ 1]);
1859 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1861 } else {
1862 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1863 data = CODA_DOWN_ADDRESS_SET(i) |
1864 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1865 3 - (i % 4)]);
1866 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1870 /* Clear registers */
1871 for (i = 0; i < 64; i++)
1872 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1874 /* Tell the BIT where to find everything it needs */
1875 if (dev->devtype->product == CODA_960 ||
1876 dev->devtype->product == CODA_7541) {
1877 coda_write(dev, dev->tempbuf.paddr,
1878 CODA_REG_BIT_TEMP_BUF_ADDR);
1879 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1880 } else {
1881 coda_write(dev, dev->workbuf.paddr,
1882 CODA_REG_BIT_WORK_BUF_ADDR);
1884 coda_write(dev, dev->codebuf.paddr,
1885 CODA_REG_BIT_CODE_BUF_ADDR);
1886 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1888 /* Set default values */
1889 switch (dev->devtype->product) {
1890 case CODA_DX6:
1891 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1892 CODA_REG_BIT_STREAM_CTRL);
1893 break;
1894 default:
1895 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1896 CODA_REG_BIT_STREAM_CTRL);
1898 if (dev->devtype->product == CODA_960)
1899 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1900 else
1901 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1903 if (dev->devtype->product != CODA_DX6)
1904 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1906 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1907 CODA_REG_BIT_INT_ENABLE);
1909 /* Reset VPU and start processor */
1910 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1911 data |= CODA_REG_RESET_ENABLE;
1912 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1913 udelay(10);
1914 data &= ~CODA_REG_RESET_ENABLE;
1915 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1916 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1918 clk_disable_unprepare(dev->clk_ahb);
1919 clk_disable_unprepare(dev->clk_per);
1921 return 0;
1923 err_clk_ahb:
1924 clk_disable_unprepare(dev->clk_per);
1925 err_clk_per:
1926 return ret;
1929 static int coda_register_device(struct coda_dev *dev, int i)
1931 struct video_device *vfd = &dev->vfd[i];
1933 if (i >= dev->devtype->num_vdevs)
1934 return -EINVAL;
1936 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
1937 vfd->fops = &coda_fops;
1938 vfd->ioctl_ops = &coda_ioctl_ops;
1939 vfd->release = video_device_release_empty,
1940 vfd->lock = &dev->dev_mutex;
1941 vfd->v4l2_dev = &dev->v4l2_dev;
1942 vfd->vfl_dir = VFL_DIR_M2M;
1943 video_set_drvdata(vfd, dev);
1945 /* Not applicable, use the selection API instead */
1946 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1947 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1948 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1950 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1953 static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf,
1954 size_t size)
1956 u32 *src = (u32 *)buf;
1958 /* Check if the firmware has a 16-byte Freescale header, skip it */
1959 if (buf[0] == 'M' && buf[1] == 'X')
1960 src += 4;
1962 * Check whether the firmware is in native order or pre-reordered for
1963 * memory access. The first instruction opcode always is 0xe40e.
1965 if (__le16_to_cpup((__le16 *)src) == 0xe40e) {
1966 u32 *dst = dev->codebuf.vaddr;
1967 int i;
1969 /* Firmware in native order, reorder while copying */
1970 if (dev->devtype->product == CODA_DX6) {
1971 for (i = 0; i < (size - 16) / 4; i++)
1972 dst[i] = (src[i] << 16) | (src[i] >> 16);
1973 } else {
1974 for (i = 0; i < (size - 16) / 4; i += 2) {
1975 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16);
1976 dst[i + 1] = (src[i] << 16) | (src[i] >> 16);
1979 } else {
1980 /* Copy the already reordered firmware image */
1981 memcpy(dev->codebuf.vaddr, src, size);
1985 static void coda_fw_callback(const struct firmware *fw, void *context);
1987 static int coda_firmware_request(struct coda_dev *dev)
1989 char *fw = dev->devtype->firmware[dev->firmware];
1991 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1992 coda_product_name(dev->devtype->product));
1994 return request_firmware_nowait(THIS_MODULE, true, fw,
1995 &dev->plat_dev->dev, GFP_KERNEL, dev,
1996 coda_fw_callback);
1999 static void coda_fw_callback(const struct firmware *fw, void *context)
2001 struct coda_dev *dev = context;
2002 struct platform_device *pdev = dev->plat_dev;
2003 int i, ret;
2005 if (!fw && dev->firmware == 1) {
2006 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2007 goto put_pm;
2009 if (!fw) {
2010 dev->firmware = 1;
2011 coda_firmware_request(dev);
2012 return;
2014 if (dev->firmware == 1) {
2016 * Since we can't suppress warnings for failed asynchronous
2017 * firmware requests, report that the fallback firmware was
2018 * found.
2020 dev_info(&pdev->dev, "Using fallback firmware %s\n",
2021 dev->devtype->firmware[dev->firmware]);
2024 /* allocate auxiliary per-device code buffer for the BIT processor */
2025 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
2026 dev->debugfs_root);
2027 if (ret < 0)
2028 goto put_pm;
2030 coda_copy_firmware(dev, fw->data, fw->size);
2031 release_firmware(fw);
2033 ret = coda_hw_init(dev);
2034 if (ret < 0) {
2035 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2036 goto put_pm;
2039 ret = coda_check_firmware(dev);
2040 if (ret < 0)
2041 goto put_pm;
2043 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2044 if (IS_ERR(dev->alloc_ctx)) {
2045 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
2046 goto put_pm;
2049 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2050 if (IS_ERR(dev->m2m_dev)) {
2051 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2052 goto rel_ctx;
2055 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2056 ret = coda_register_device(dev, i);
2057 if (ret) {
2058 v4l2_err(&dev->v4l2_dev,
2059 "Failed to register %s video device: %d\n",
2060 dev->devtype->vdevs[i]->name, ret);
2061 goto rel_vfd;
2065 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
2066 dev->vfd[0].num, dev->vfd[i - 1].num);
2068 pm_runtime_put_sync(&pdev->dev);
2069 return;
2071 rel_vfd:
2072 while (--i >= 0)
2073 video_unregister_device(&dev->vfd[i]);
2074 v4l2_m2m_release(dev->m2m_dev);
2075 rel_ctx:
2076 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2077 put_pm:
2078 pm_runtime_put_sync(&pdev->dev);
2081 enum coda_platform {
2082 CODA_IMX27,
2083 CODA_IMX53,
2084 CODA_IMX6Q,
2085 CODA_IMX6DL,
2088 static const struct coda_devtype coda_devdata[] = {
2089 [CODA_IMX27] = {
2090 .firmware = {
2091 "vpu_fw_imx27_TO2.bin",
2092 "v4l-codadx6-imx27.bin"
2094 .product = CODA_DX6,
2095 .codecs = codadx6_codecs,
2096 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2097 .vdevs = codadx6_video_devices,
2098 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2099 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2100 .iram_size = 0xb000,
2102 [CODA_IMX53] = {
2103 .firmware = {
2104 "vpu_fw_imx53.bin",
2105 "v4l-coda7541-imx53.bin"
2107 .product = CODA_7541,
2108 .codecs = coda7_codecs,
2109 .num_codecs = ARRAY_SIZE(coda7_codecs),
2110 .vdevs = coda7_video_devices,
2111 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2112 .workbuf_size = 128 * 1024,
2113 .tempbuf_size = 304 * 1024,
2114 .iram_size = 0x14000,
2116 [CODA_IMX6Q] = {
2117 .firmware = {
2118 "vpu_fw_imx6q.bin",
2119 "v4l-coda960-imx6q.bin"
2121 .product = CODA_960,
2122 .codecs = coda9_codecs,
2123 .num_codecs = ARRAY_SIZE(coda9_codecs),
2124 .vdevs = coda9_video_devices,
2125 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2126 .workbuf_size = 80 * 1024,
2127 .tempbuf_size = 204 * 1024,
2128 .iram_size = 0x21000,
2130 [CODA_IMX6DL] = {
2131 .firmware = {
2132 "vpu_fw_imx6d.bin",
2133 "v4l-coda960-imx6dl.bin"
2135 .product = CODA_960,
2136 .codecs = coda9_codecs,
2137 .num_codecs = ARRAY_SIZE(coda9_codecs),
2138 .vdevs = coda9_video_devices,
2139 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2140 .workbuf_size = 80 * 1024,
2141 .tempbuf_size = 204 * 1024,
2142 .iram_size = 0x20000,
2146 static struct platform_device_id coda_platform_ids[] = {
2147 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2148 { /* sentinel */ }
2150 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2152 #ifdef CONFIG_OF
2153 static const struct of_device_id coda_dt_ids[] = {
2154 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2155 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2156 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2157 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2158 { /* sentinel */ }
2160 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2161 #endif
2163 static int coda_probe(struct platform_device *pdev)
2165 const struct of_device_id *of_id =
2166 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2167 const struct platform_device_id *pdev_id;
2168 struct coda_platform_data *pdata = pdev->dev.platform_data;
2169 struct device_node *np = pdev->dev.of_node;
2170 struct gen_pool *pool;
2171 struct coda_dev *dev;
2172 struct resource *res;
2173 int ret, irq;
2175 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2176 if (!dev)
2177 return -ENOMEM;
2179 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2181 if (of_id)
2182 dev->devtype = of_id->data;
2183 else if (pdev_id)
2184 dev->devtype = &coda_devdata[pdev_id->driver_data];
2185 else
2186 return -EINVAL;
2188 spin_lock_init(&dev->irqlock);
2189 INIT_LIST_HEAD(&dev->instances);
2191 dev->plat_dev = pdev;
2192 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2193 if (IS_ERR(dev->clk_per)) {
2194 dev_err(&pdev->dev, "Could not get per clock\n");
2195 return PTR_ERR(dev->clk_per);
2198 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2199 if (IS_ERR(dev->clk_ahb)) {
2200 dev_err(&pdev->dev, "Could not get ahb clock\n");
2201 return PTR_ERR(dev->clk_ahb);
2204 /* Get memory for physical registers */
2205 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2206 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2207 if (IS_ERR(dev->regs_base))
2208 return PTR_ERR(dev->regs_base);
2210 /* IRQ */
2211 irq = platform_get_irq_byname(pdev, "bit");
2212 if (irq < 0)
2213 irq = platform_get_irq(pdev, 0);
2214 if (irq < 0) {
2215 dev_err(&pdev->dev, "failed to get irq resource\n");
2216 return irq;
2219 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2220 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2221 if (ret < 0) {
2222 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2223 return ret;
2226 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
2227 if (IS_ERR(dev->rstc)) {
2228 ret = PTR_ERR(dev->rstc);
2229 if (ret == -ENOENT || ret == -ENOSYS) {
2230 dev->rstc = NULL;
2231 } else {
2232 dev_err(&pdev->dev, "failed get reset control: %d\n",
2233 ret);
2234 return ret;
2238 /* Get IRAM pool from device tree or platform data */
2239 pool = of_gen_pool_get(np, "iram", 0);
2240 if (!pool && pdata)
2241 pool = gen_pool_get(pdata->iram_dev, NULL);
2242 if (!pool) {
2243 dev_err(&pdev->dev, "iram pool not available\n");
2244 return -ENOMEM;
2246 dev->iram_pool = pool;
2248 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2249 if (ret)
2250 return ret;
2252 mutex_init(&dev->dev_mutex);
2253 mutex_init(&dev->coda_mutex);
2255 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2256 if (!dev->debugfs_root)
2257 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2259 /* allocate auxiliary per-device buffers for the BIT processor */
2260 if (dev->devtype->product == CODA_DX6) {
2261 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2262 dev->devtype->workbuf_size, "workbuf",
2263 dev->debugfs_root);
2264 if (ret < 0)
2265 goto err_v4l2_register;
2268 if (dev->devtype->tempbuf_size) {
2269 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2270 dev->devtype->tempbuf_size, "tempbuf",
2271 dev->debugfs_root);
2272 if (ret < 0)
2273 goto err_v4l2_register;
2276 dev->iram.size = dev->devtype->iram_size;
2277 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2278 &dev->iram.paddr);
2279 if (!dev->iram.vaddr) {
2280 dev_warn(&pdev->dev, "unable to alloc iram\n");
2281 } else {
2282 memset(dev->iram.vaddr, 0, dev->iram.size);
2283 dev->iram.blob.data = dev->iram.vaddr;
2284 dev->iram.blob.size = dev->iram.size;
2285 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2286 dev->debugfs_root,
2287 &dev->iram.blob);
2290 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2291 if (!dev->workqueue) {
2292 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2293 ret = -ENOMEM;
2294 goto err_v4l2_register;
2297 platform_set_drvdata(pdev, dev);
2300 * Start activated so we can directly call coda_hw_init in
2301 * coda_fw_callback regardless of whether CONFIG_PM is
2302 * enabled or whether the device is associated with a PM domain.
2304 pm_runtime_get_noresume(&pdev->dev);
2305 pm_runtime_set_active(&pdev->dev);
2306 pm_runtime_enable(&pdev->dev);
2308 return coda_firmware_request(dev);
2310 err_v4l2_register:
2311 v4l2_device_unregister(&dev->v4l2_dev);
2312 return ret;
2315 static int coda_remove(struct platform_device *pdev)
2317 struct coda_dev *dev = platform_get_drvdata(pdev);
2318 int i;
2320 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2321 if (video_get_drvdata(&dev->vfd[i]))
2322 video_unregister_device(&dev->vfd[i]);
2324 if (dev->m2m_dev)
2325 v4l2_m2m_release(dev->m2m_dev);
2326 pm_runtime_disable(&pdev->dev);
2327 if (dev->alloc_ctx)
2328 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2329 v4l2_device_unregister(&dev->v4l2_dev);
2330 destroy_workqueue(dev->workqueue);
2331 if (dev->iram.vaddr)
2332 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2333 dev->iram.size);
2334 coda_free_aux_buf(dev, &dev->codebuf);
2335 coda_free_aux_buf(dev, &dev->tempbuf);
2336 coda_free_aux_buf(dev, &dev->workbuf);
2337 debugfs_remove_recursive(dev->debugfs_root);
2338 return 0;
2341 #ifdef CONFIG_PM
2342 static int coda_runtime_resume(struct device *dev)
2344 struct coda_dev *cdev = dev_get_drvdata(dev);
2345 int ret = 0;
2347 if (dev->pm_domain && cdev->codebuf.vaddr) {
2348 ret = coda_hw_init(cdev);
2349 if (ret)
2350 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2353 return ret;
2355 #endif
2357 static const struct dev_pm_ops coda_pm_ops = {
2358 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2361 static struct platform_driver coda_driver = {
2362 .probe = coda_probe,
2363 .remove = coda_remove,
2364 .driver = {
2365 .name = CODA_NAME,
2366 .of_match_table = of_match_ptr(coda_dt_ids),
2367 .pm = &coda_pm_ops,
2369 .id_table = coda_platform_ids,
2372 module_platform_driver(coda_driver);
2374 MODULE_LICENSE("GPL");
2375 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2376 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");