[media] coda: use strlcpy instead of snprintf
[linux-2.6/btrfs-unstable.git] / drivers / media / platform / coda / coda-common.c
blob443886535230ee034835eaef0967382b0f8b337e
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/genalloc.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/irq.h>
22 #include <linux/kfifo.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/videodev2.h>
29 #include <linux/of.h>
30 #include <linux/platform_data/coda.h>
31 #include <linux/reset.h>
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-event.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-mem2mem.h>
38 #include <media/videobuf2-core.h>
39 #include <media/videobuf2-dma-contig.h>
40 #include <media/videobuf2-vmalloc.h>
42 #include "coda.h"
44 #define CODA_NAME "coda"
46 #define CODADX6_MAX_INSTANCES 4
47 #define CODA_MAX_FORMATS 4
49 #define CODA_PARA_BUF_SIZE (10 * 1024)
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 struct coda_fmt {
66 char *name;
67 u32 fourcc;
70 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
72 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
73 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
74 writel(data, dev->regs_base + reg);
77 unsigned int coda_read(struct coda_dev *dev, u32 reg)
79 u32 data;
81 data = readl(dev->regs_base + reg);
82 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
83 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
84 return data;
87 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
88 struct vb2_buffer *buf, unsigned int reg_y)
90 u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
91 u32 base_cb, base_cr;
93 switch (q_data->fourcc) {
94 case V4L2_PIX_FMT_YVU420:
95 /* Switch Cb and Cr for YVU420 format */
96 base_cr = base_y + q_data->bytesperline * q_data->height;
97 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
98 break;
99 case V4L2_PIX_FMT_YUV420:
100 case V4L2_PIX_FMT_NV12:
101 default:
102 base_cb = base_y + q_data->bytesperline * q_data->height;
103 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
104 break;
105 case V4L2_PIX_FMT_YUV422P:
106 base_cb = base_y + q_data->bytesperline * q_data->height;
107 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
110 coda_write(ctx->dev, base_y, reg_y);
111 coda_write(ctx->dev, base_cb, reg_y + 4);
112 coda_write(ctx->dev, base_cr, reg_y + 8);
116 * Array of all formats supported by any version of Coda:
118 static const struct coda_fmt coda_formats[] = {
120 .name = "YUV 4:2:0 Planar, YCbCr",
121 .fourcc = V4L2_PIX_FMT_YUV420,
124 .name = "YUV 4:2:0 Planar, YCrCb",
125 .fourcc = V4L2_PIX_FMT_YVU420,
128 .name = "YUV 4:2:0 Partial interleaved Y/CbCr",
129 .fourcc = V4L2_PIX_FMT_NV12,
132 .name = "YUV 4:2:2 Planar, YCbCr",
133 .fourcc = V4L2_PIX_FMT_YUV422P,
136 .name = "H264 Encoded Stream",
137 .fourcc = V4L2_PIX_FMT_H264,
140 .name = "MPEG4 Encoded Stream",
141 .fourcc = V4L2_PIX_FMT_MPEG4,
144 .name = "JPEG Encoded Images",
145 .fourcc = V4L2_PIX_FMT_JPEG,
149 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
150 { mode, src_fourcc, dst_fourcc, max_w, max_h }
153 * Arrays of codecs supported by each given version of Coda:
154 * i.MX27 -> codadx6
155 * i.MX5x -> coda7
156 * i.MX6 -> coda960
157 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
159 static const struct coda_codec codadx6_codecs[] = {
160 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
161 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
164 static const struct coda_codec coda7_codecs[] = {
165 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
166 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
167 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
168 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
169 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
170 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
173 static const struct coda_codec coda9_codecs[] = {
174 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
175 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
176 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
177 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
180 struct coda_video_device {
181 const char *name;
182 enum coda_inst_type type;
183 const struct coda_context_ops *ops;
184 bool direct;
185 u32 src_formats[CODA_MAX_FORMATS];
186 u32 dst_formats[CODA_MAX_FORMATS];
189 static const struct coda_video_device coda_bit_encoder = {
190 .name = "coda-encoder",
191 .type = CODA_INST_ENCODER,
192 .ops = &coda_bit_encode_ops,
193 .src_formats = {
194 V4L2_PIX_FMT_YUV420,
195 V4L2_PIX_FMT_YVU420,
196 V4L2_PIX_FMT_NV12,
198 .dst_formats = {
199 V4L2_PIX_FMT_H264,
200 V4L2_PIX_FMT_MPEG4,
204 static const struct coda_video_device coda_bit_jpeg_encoder = {
205 .name = "coda-jpeg-encoder",
206 .type = CODA_INST_ENCODER,
207 .ops = &coda_bit_encode_ops,
208 .src_formats = {
209 V4L2_PIX_FMT_YUV420,
210 V4L2_PIX_FMT_YVU420,
211 V4L2_PIX_FMT_NV12,
212 V4L2_PIX_FMT_YUV422P,
214 .dst_formats = {
215 V4L2_PIX_FMT_JPEG,
219 static const struct coda_video_device coda_bit_decoder = {
220 .name = "coda-decoder",
221 .type = CODA_INST_DECODER,
222 .ops = &coda_bit_decode_ops,
223 .src_formats = {
224 V4L2_PIX_FMT_H264,
225 V4L2_PIX_FMT_MPEG4,
227 .dst_formats = {
228 V4L2_PIX_FMT_YUV420,
229 V4L2_PIX_FMT_YVU420,
230 V4L2_PIX_FMT_NV12,
234 static const struct coda_video_device coda_bit_jpeg_decoder = {
235 .name = "coda-jpeg-decoder",
236 .type = CODA_INST_DECODER,
237 .ops = &coda_bit_decode_ops,
238 .src_formats = {
239 V4L2_PIX_FMT_JPEG,
241 .dst_formats = {
242 V4L2_PIX_FMT_YUV420,
243 V4L2_PIX_FMT_YVU420,
244 V4L2_PIX_FMT_NV12,
245 V4L2_PIX_FMT_YUV422P,
249 static const struct coda_video_device *codadx6_video_devices[] = {
250 &coda_bit_encoder,
253 static const struct coda_video_device *coda7_video_devices[] = {
254 &coda_bit_jpeg_encoder,
255 &coda_bit_jpeg_decoder,
256 &coda_bit_encoder,
257 &coda_bit_decoder,
260 static const struct coda_video_device *coda9_video_devices[] = {
261 &coda_bit_encoder,
262 &coda_bit_decoder,
265 static bool coda_format_is_yuv(u32 fourcc)
267 switch (fourcc) {
268 case V4L2_PIX_FMT_YUV420:
269 case V4L2_PIX_FMT_YVU420:
270 case V4L2_PIX_FMT_NV12:
271 case V4L2_PIX_FMT_YUV422P:
272 return true;
273 default:
274 return false;
278 static const char *coda_format_name(u32 fourcc)
280 int i;
282 for (i = 0; i < ARRAY_SIZE(coda_formats); i++) {
283 if (coda_formats[i].fourcc == fourcc)
284 return coda_formats[i].name;
287 return NULL;
291 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
292 * tables.
294 static u32 coda_format_normalize_yuv(u32 fourcc)
296 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
299 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
300 int src_fourcc, int dst_fourcc)
302 const struct coda_codec *codecs = dev->devtype->codecs;
303 int num_codecs = dev->devtype->num_codecs;
304 int k;
306 src_fourcc = coda_format_normalize_yuv(src_fourcc);
307 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
308 if (src_fourcc == dst_fourcc)
309 return NULL;
311 for (k = 0; k < num_codecs; k++) {
312 if (codecs[k].src_fourcc == src_fourcc &&
313 codecs[k].dst_fourcc == dst_fourcc)
314 break;
317 if (k == num_codecs)
318 return NULL;
320 return &codecs[k];
323 static void coda_get_max_dimensions(struct coda_dev *dev,
324 const struct coda_codec *codec,
325 int *max_w, int *max_h)
327 const struct coda_codec *codecs = dev->devtype->codecs;
328 int num_codecs = dev->devtype->num_codecs;
329 unsigned int w, h;
330 int k;
332 if (codec) {
333 w = codec->max_w;
334 h = codec->max_h;
335 } else {
336 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
337 w = max(w, codecs[k].max_w);
338 h = max(h, codecs[k].max_h);
342 if (max_w)
343 *max_w = w;
344 if (max_h)
345 *max_h = h;
348 const struct coda_video_device *to_coda_video_device(struct video_device *vdev)
350 struct coda_dev *dev = video_get_drvdata(vdev);
351 unsigned int i = vdev - dev->vfd;
353 if (i >= dev->devtype->num_vdevs)
354 return NULL;
356 return dev->devtype->vdevs[i];
359 const char *coda_product_name(int product)
361 static char buf[9];
363 switch (product) {
364 case CODA_DX6:
365 return "CodaDx6";
366 case CODA_7541:
367 return "CODA7541";
368 case CODA_960:
369 return "CODA960";
370 default:
371 snprintf(buf, sizeof(buf), "(0x%04x)", product);
372 return buf;
377 * V4L2 ioctl() operations.
379 static int coda_querycap(struct file *file, void *priv,
380 struct v4l2_capability *cap)
382 struct coda_ctx *ctx = fh_to_ctx(priv);
384 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
385 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
386 sizeof(cap->card));
387 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
388 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
389 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
391 return 0;
394 static int coda_enum_fmt(struct file *file, void *priv,
395 struct v4l2_fmtdesc *f)
397 struct video_device *vdev = video_devdata(file);
398 const struct coda_video_device *cvd = to_coda_video_device(vdev);
399 const u32 *formats;
400 const char *name;
402 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
403 formats = cvd->src_formats;
404 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
405 formats = cvd->dst_formats;
406 else
407 return -EINVAL;
409 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
410 return -EINVAL;
412 name = coda_format_name(formats[f->index]);
413 strlcpy(f->description, name, sizeof(f->description));
414 f->pixelformat = formats[f->index];
415 if (!coda_format_is_yuv(formats[f->index]))
416 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
418 return 0;
421 static int coda_g_fmt(struct file *file, void *priv,
422 struct v4l2_format *f)
424 struct coda_q_data *q_data;
425 struct coda_ctx *ctx = fh_to_ctx(priv);
427 q_data = get_q_data(ctx, f->type);
428 if (!q_data)
429 return -EINVAL;
431 f->fmt.pix.field = V4L2_FIELD_NONE;
432 f->fmt.pix.pixelformat = q_data->fourcc;
433 f->fmt.pix.width = q_data->width;
434 f->fmt.pix.height = q_data->height;
435 f->fmt.pix.bytesperline = q_data->bytesperline;
437 f->fmt.pix.sizeimage = q_data->sizeimage;
438 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
439 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
440 else
441 f->fmt.pix.colorspace = ctx->colorspace;
443 return 0;
446 static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
448 struct coda_q_data *q_data;
449 const u32 *formats;
450 int i;
452 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
453 formats = ctx->cvd->src_formats;
454 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
455 formats = ctx->cvd->dst_formats;
456 else
457 return -EINVAL;
459 for (i = 0; i < CODA_MAX_FORMATS; i++) {
460 if (formats[i] == f->fmt.pix.pixelformat) {
461 f->fmt.pix.pixelformat = formats[i];
462 return 0;
466 /* Fall back to currently set pixelformat */
467 q_data = get_q_data(ctx, f->type);
468 f->fmt.pix.pixelformat = q_data->fourcc;
470 return 0;
473 static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
474 u32 width, u32 height)
477 * This is a rough estimate for sensible compressed buffer
478 * sizes (between 1 and 16 bits per pixel). This could be
479 * improved by better format specific worst case estimates.
481 return round_up(clamp(sizeimage, width * height / 8,
482 width * height * 2), PAGE_SIZE);
485 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
486 struct v4l2_format *f)
488 struct coda_dev *dev = ctx->dev;
489 unsigned int max_w, max_h;
490 enum v4l2_field field;
492 field = f->fmt.pix.field;
493 if (field == V4L2_FIELD_ANY)
494 field = V4L2_FIELD_NONE;
495 else if (V4L2_FIELD_NONE != field)
496 return -EINVAL;
498 /* V4L2 specification suggests the driver corrects the format struct
499 * if any of the dimensions is unsupported */
500 f->fmt.pix.field = field;
502 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
503 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
504 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
505 S_ALIGN);
507 switch (f->fmt.pix.pixelformat) {
508 case V4L2_PIX_FMT_YUV420:
509 case V4L2_PIX_FMT_YVU420:
510 case V4L2_PIX_FMT_NV12:
512 * Frame stride must be at least multiple of 8,
513 * but multiple of 16 for h.264 or JPEG 4:2:x
515 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
516 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
517 f->fmt.pix.height * 3 / 2;
518 break;
519 case V4L2_PIX_FMT_YUV422P:
520 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
521 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
522 f->fmt.pix.height * 2;
523 break;
524 case V4L2_PIX_FMT_JPEG:
525 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
526 /* fallthrough */
527 case V4L2_PIX_FMT_H264:
528 case V4L2_PIX_FMT_MPEG4:
529 f->fmt.pix.bytesperline = 0;
530 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
531 f->fmt.pix.sizeimage,
532 f->fmt.pix.width,
533 f->fmt.pix.height);
534 break;
535 default:
536 BUG();
539 return 0;
542 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
543 struct v4l2_format *f)
545 struct coda_ctx *ctx = fh_to_ctx(priv);
546 const struct coda_q_data *q_data_src;
547 const struct coda_codec *codec;
548 struct vb2_queue *src_vq;
549 int ret;
551 ret = coda_try_pixelformat(ctx, f);
552 if (ret < 0)
553 return ret;
555 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
558 * If the source format is already fixed, only allow the same output
559 * resolution
561 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
562 if (vb2_is_streaming(src_vq)) {
563 f->fmt.pix.width = q_data_src->width;
564 f->fmt.pix.height = q_data_src->height;
567 f->fmt.pix.colorspace = ctx->colorspace;
569 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
570 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
571 f->fmt.pix.pixelformat);
572 if (!codec)
573 return -EINVAL;
575 ret = coda_try_fmt(ctx, codec, f);
576 if (ret < 0)
577 return ret;
579 /* The h.264 decoder only returns complete 16x16 macroblocks */
580 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
581 f->fmt.pix.width = f->fmt.pix.width;
582 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
583 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
584 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
585 f->fmt.pix.height * 3 / 2;
588 return 0;
591 static int coda_try_fmt_vid_out(struct file *file, void *priv,
592 struct v4l2_format *f)
594 struct coda_ctx *ctx = fh_to_ctx(priv);
595 struct coda_dev *dev = ctx->dev;
596 const struct coda_q_data *q_data_dst;
597 const struct coda_codec *codec;
598 int ret;
600 ret = coda_try_pixelformat(ctx, f);
601 if (ret < 0)
602 return ret;
604 switch (f->fmt.pix.colorspace) {
605 case V4L2_COLORSPACE_REC709:
606 case V4L2_COLORSPACE_JPEG:
607 break;
608 default:
609 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
610 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
611 else
612 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
615 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
616 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
618 return coda_try_fmt(ctx, codec, f);
621 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
623 struct coda_q_data *q_data;
624 struct vb2_queue *vq;
626 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
627 if (!vq)
628 return -EINVAL;
630 q_data = get_q_data(ctx, f->type);
631 if (!q_data)
632 return -EINVAL;
634 if (vb2_is_busy(vq)) {
635 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
636 return -EBUSY;
639 q_data->fourcc = f->fmt.pix.pixelformat;
640 q_data->width = f->fmt.pix.width;
641 q_data->height = f->fmt.pix.height;
642 q_data->bytesperline = f->fmt.pix.bytesperline;
643 q_data->sizeimage = f->fmt.pix.sizeimage;
644 q_data->rect.left = 0;
645 q_data->rect.top = 0;
646 q_data->rect.width = f->fmt.pix.width;
647 q_data->rect.height = f->fmt.pix.height;
649 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
650 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
651 f->type, q_data->width, q_data->height, q_data->fourcc);
653 return 0;
656 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
657 struct v4l2_format *f)
659 struct coda_ctx *ctx = fh_to_ctx(priv);
660 int ret;
662 ret = coda_try_fmt_vid_cap(file, priv, f);
663 if (ret)
664 return ret;
666 return coda_s_fmt(ctx, f);
669 static int coda_s_fmt_vid_out(struct file *file, void *priv,
670 struct v4l2_format *f)
672 struct coda_ctx *ctx = fh_to_ctx(priv);
673 struct v4l2_format f_cap;
674 int ret;
676 ret = coda_try_fmt_vid_out(file, priv, f);
677 if (ret)
678 return ret;
680 ret = coda_s_fmt(ctx, f);
681 if (ret)
682 return ret;
684 ctx->colorspace = f->fmt.pix.colorspace;
686 memset(&f_cap, 0, sizeof(f_cap));
687 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
688 coda_g_fmt(file, priv, &f_cap);
689 f_cap.fmt.pix.width = f->fmt.pix.width;
690 f_cap.fmt.pix.height = f->fmt.pix.height;
692 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
693 if (ret)
694 return ret;
696 return coda_s_fmt(ctx, &f_cap);
699 static int coda_qbuf(struct file *file, void *priv,
700 struct v4l2_buffer *buf)
702 struct coda_ctx *ctx = fh_to_ctx(priv);
704 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
707 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
708 struct v4l2_buffer *buf)
710 struct vb2_queue *src_vq;
712 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
714 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
715 (buf->sequence == (ctx->qsequence - 1)));
718 static int coda_dqbuf(struct file *file, void *priv,
719 struct v4l2_buffer *buf)
721 struct coda_ctx *ctx = fh_to_ctx(priv);
722 int ret;
724 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
726 /* If this is the last capture buffer, emit an end-of-stream event */
727 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
728 coda_buf_is_end_of_stream(ctx, buf)) {
729 const struct v4l2_event eos_event = {
730 .type = V4L2_EVENT_EOS
733 v4l2_event_queue_fh(&ctx->fh, &eos_event);
736 return ret;
739 static int coda_g_selection(struct file *file, void *fh,
740 struct v4l2_selection *s)
742 struct coda_ctx *ctx = fh_to_ctx(fh);
743 struct coda_q_data *q_data;
744 struct v4l2_rect r, *rsel;
746 q_data = get_q_data(ctx, s->type);
747 if (!q_data)
748 return -EINVAL;
750 r.left = 0;
751 r.top = 0;
752 r.width = q_data->width;
753 r.height = q_data->height;
754 rsel = &q_data->rect;
756 switch (s->target) {
757 case V4L2_SEL_TGT_CROP_DEFAULT:
758 case V4L2_SEL_TGT_CROP_BOUNDS:
759 rsel = &r;
760 /* fallthrough */
761 case V4L2_SEL_TGT_CROP:
762 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
763 return -EINVAL;
764 break;
765 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
766 case V4L2_SEL_TGT_COMPOSE_PADDED:
767 rsel = &r;
768 /* fallthrough */
769 case V4L2_SEL_TGT_COMPOSE:
770 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
771 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
772 return -EINVAL;
773 break;
774 default:
775 return -EINVAL;
778 s->r = *rsel;
780 return 0;
783 static int coda_try_decoder_cmd(struct file *file, void *fh,
784 struct v4l2_decoder_cmd *dc)
786 if (dc->cmd != V4L2_DEC_CMD_STOP)
787 return -EINVAL;
789 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
790 return -EINVAL;
792 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
793 return -EINVAL;
795 return 0;
798 static int coda_decoder_cmd(struct file *file, void *fh,
799 struct v4l2_decoder_cmd *dc)
801 struct coda_ctx *ctx = fh_to_ctx(fh);
802 int ret;
804 ret = coda_try_decoder_cmd(file, fh, dc);
805 if (ret < 0)
806 return ret;
808 /* Ignore decoder stop command silently in encoder context */
809 if (ctx->inst_type != CODA_INST_DECODER)
810 return 0;
812 /* Set the stream-end flag on this context */
813 coda_bit_stream_end_flag(ctx);
814 ctx->hold = false;
815 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
817 return 0;
820 static int coda_subscribe_event(struct v4l2_fh *fh,
821 const struct v4l2_event_subscription *sub)
823 switch (sub->type) {
824 case V4L2_EVENT_EOS:
825 return v4l2_event_subscribe(fh, sub, 0, NULL);
826 default:
827 return v4l2_ctrl_subscribe_event(fh, sub);
831 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
832 .vidioc_querycap = coda_querycap,
834 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
835 .vidioc_g_fmt_vid_cap = coda_g_fmt,
836 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
837 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
839 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
840 .vidioc_g_fmt_vid_out = coda_g_fmt,
841 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
842 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
844 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
845 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
847 .vidioc_qbuf = coda_qbuf,
848 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
849 .vidioc_dqbuf = coda_dqbuf,
850 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
852 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
853 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
855 .vidioc_g_selection = coda_g_selection,
857 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
858 .vidioc_decoder_cmd = coda_decoder_cmd,
860 .vidioc_subscribe_event = coda_subscribe_event,
861 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
864 void coda_set_gdi_regs(struct coda_ctx *ctx)
866 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
867 struct coda_dev *dev = ctx->dev;
868 int i;
870 for (i = 0; i < 16; i++)
871 coda_write(dev, tiled_map->xy2ca_map[i],
872 CODA9_GDI_XY2_CAS_0 + 4 * i);
873 for (i = 0; i < 4; i++)
874 coda_write(dev, tiled_map->xy2ba_map[i],
875 CODA9_GDI_XY2_BA_0 + 4 * i);
876 for (i = 0; i < 16; i++)
877 coda_write(dev, tiled_map->xy2ra_map[i],
878 CODA9_GDI_XY2_RAS_0 + 4 * i);
879 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
880 for (i = 0; i < 32; i++)
881 coda_write(dev, tiled_map->rbc2axi_map[i],
882 CODA9_GDI_RBC2_AXI_0 + 4 * i);
886 * Mem-to-mem operations.
889 static void coda_device_run(void *m2m_priv)
891 struct coda_ctx *ctx = m2m_priv;
892 struct coda_dev *dev = ctx->dev;
894 queue_work(dev->workqueue, &ctx->pic_run_work);
897 static void coda_pic_run_work(struct work_struct *work)
899 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
900 struct coda_dev *dev = ctx->dev;
901 int ret;
903 mutex_lock(&ctx->buffer_mutex);
904 mutex_lock(&dev->coda_mutex);
906 ret = ctx->ops->prepare_run(ctx);
907 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
908 mutex_unlock(&dev->coda_mutex);
909 mutex_unlock(&ctx->buffer_mutex);
910 /* job_finish scheduled by prepare_decode */
911 return;
914 if (!wait_for_completion_timeout(&ctx->completion,
915 msecs_to_jiffies(1000))) {
916 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
918 ctx->hold = true;
920 coda_hw_reset(ctx);
921 } else if (!ctx->aborting) {
922 ctx->ops->finish_run(ctx);
925 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
926 ctx->ops->seq_end_work)
927 queue_work(dev->workqueue, &ctx->seq_end_work);
929 mutex_unlock(&dev->coda_mutex);
930 mutex_unlock(&ctx->buffer_mutex);
932 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
935 static int coda_job_ready(void *m2m_priv)
937 struct coda_ctx *ctx = m2m_priv;
940 * For both 'P' and 'key' frame cases 1 picture
941 * and 1 frame are needed. In the decoder case,
942 * the compressed frame can be in the bitstream.
944 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
945 ctx->inst_type != CODA_INST_DECODER) {
946 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
947 "not ready: not enough video buffers.\n");
948 return 0;
951 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
952 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
953 "not ready: not enough video capture buffers.\n");
954 return 0;
957 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
958 struct list_head *meta;
959 bool stream_end;
960 int num_metas;
961 int src_bufs;
963 if (ctx->hold && !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
964 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
965 "%d: not ready: on hold for more buffers.\n",
966 ctx->idx);
967 return 0;
970 stream_end = ctx->bit_stream_param &
971 CODA_BIT_STREAM_END_FLAG;
973 num_metas = 0;
974 list_for_each(meta, &ctx->buffer_meta_list)
975 num_metas++;
977 src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
979 if (!stream_end && (num_metas + src_bufs) < 2) {
980 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
981 "%d: not ready: need 2 buffers available (%d, %d)\n",
982 ctx->idx, num_metas, src_bufs);
983 return 0;
987 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
988 !stream_end && (coda_get_bitstream_payload(ctx) < 512)) {
989 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
990 "%d: not ready: not enough bitstream data (%d).\n",
991 ctx->idx, coda_get_bitstream_payload(ctx));
992 return 0;
996 if (ctx->aborting) {
997 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
998 "not ready: aborting\n");
999 return 0;
1002 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1003 "job ready\n");
1004 return 1;
1007 static void coda_job_abort(void *priv)
1009 struct coda_ctx *ctx = priv;
1011 ctx->aborting = 1;
1013 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1014 "Aborting task\n");
1017 static void coda_lock(void *m2m_priv)
1019 struct coda_ctx *ctx = m2m_priv;
1020 struct coda_dev *pcdev = ctx->dev;
1022 mutex_lock(&pcdev->dev_mutex);
1025 static void coda_unlock(void *m2m_priv)
1027 struct coda_ctx *ctx = m2m_priv;
1028 struct coda_dev *pcdev = ctx->dev;
1030 mutex_unlock(&pcdev->dev_mutex);
1033 static const struct v4l2_m2m_ops coda_m2m_ops = {
1034 .device_run = coda_device_run,
1035 .job_ready = coda_job_ready,
1036 .job_abort = coda_job_abort,
1037 .lock = coda_lock,
1038 .unlock = coda_unlock,
1041 static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1043 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1044 int luma_map, chro_map, i;
1046 memset(tiled_map, 0, sizeof(*tiled_map));
1048 luma_map = 64;
1049 chro_map = 64;
1050 tiled_map->map_type = tiled_map_type;
1051 for (i = 0; i < 16; i++)
1052 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1053 for (i = 0; i < 4; i++)
1054 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1055 for (i = 0; i < 16; i++)
1056 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1058 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1059 tiled_map->xy2rbc_config = 0;
1060 } else {
1061 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1062 tiled_map_type);
1063 return;
1067 static void set_default_params(struct coda_ctx *ctx)
1069 unsigned int max_w, max_h, usize, csize;
1071 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1072 ctx->cvd->dst_formats[0]);
1073 max_w = min(ctx->codec->max_w, 1920U);
1074 max_h = min(ctx->codec->max_h, 1088U);
1075 usize = max_w * max_h * 3 / 2;
1076 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
1078 ctx->params.codec_mode = ctx->codec->mode;
1079 ctx->colorspace = V4L2_COLORSPACE_REC709;
1080 ctx->params.framerate = 30;
1082 /* Default formats for output and input queues */
1083 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1084 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1085 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1086 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1087 ctx->q_data[V4L2_M2M_DST].width = max_w;
1088 ctx->q_data[V4L2_M2M_DST].height = max_h;
1089 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1090 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1091 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
1092 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1093 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
1094 } else {
1095 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1096 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
1097 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1098 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
1100 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1101 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1102 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1103 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
1105 if (ctx->dev->devtype->product == CODA_960)
1106 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
1110 * Queue operations
1112 static int coda_queue_setup(struct vb2_queue *vq,
1113 const struct v4l2_format *fmt,
1114 unsigned int *nbuffers, unsigned int *nplanes,
1115 unsigned int sizes[], void *alloc_ctxs[])
1117 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1118 struct coda_q_data *q_data;
1119 unsigned int size;
1121 q_data = get_q_data(ctx, vq->type);
1122 size = q_data->sizeimage;
1124 *nplanes = 1;
1125 sizes[0] = size;
1127 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
1128 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1130 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1131 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1133 return 0;
1136 static int coda_buf_prepare(struct vb2_buffer *vb)
1138 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1139 struct coda_q_data *q_data;
1141 q_data = get_q_data(ctx, vb->vb2_queue->type);
1143 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1144 v4l2_warn(&ctx->dev->v4l2_dev,
1145 "%s data will not fit into plane (%lu < %lu)\n",
1146 __func__, vb2_plane_size(vb, 0),
1147 (long)q_data->sizeimage);
1148 return -EINVAL;
1151 return 0;
1154 static void coda_buf_queue(struct vb2_buffer *vb)
1156 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1157 struct vb2_queue *vq = vb->vb2_queue;
1158 struct coda_q_data *q_data;
1160 q_data = get_q_data(ctx, vb->vb2_queue->type);
1163 * In the decoder case, immediately try to copy the buffer into the
1164 * bitstream ringbuffer and mark it as ready to be dequeued.
1166 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1168 * For backwards compatibility, queuing an empty buffer marks
1169 * the stream end
1171 if (vb2_get_plane_payload(vb, 0) == 0)
1172 coda_bit_stream_end_flag(ctx);
1173 mutex_lock(&ctx->bitstream_mutex);
1174 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1175 if (vb2_is_streaming(vb->vb2_queue))
1176 coda_fill_bitstream(ctx);
1177 mutex_unlock(&ctx->bitstream_mutex);
1178 } else {
1179 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
1183 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1184 size_t size, const char *name, struct dentry *parent)
1186 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1187 GFP_KERNEL);
1188 if (!buf->vaddr) {
1189 v4l2_err(&dev->v4l2_dev,
1190 "Failed to allocate %s buffer of size %u\n",
1191 name, size);
1192 return -ENOMEM;
1195 buf->size = size;
1197 if (name && parent) {
1198 buf->blob.data = buf->vaddr;
1199 buf->blob.size = size;
1200 buf->dentry = debugfs_create_blob(name, 0644, parent,
1201 &buf->blob);
1202 if (!buf->dentry)
1203 dev_warn(&dev->plat_dev->dev,
1204 "failed to create debugfs entry %s\n", name);
1207 return 0;
1210 void coda_free_aux_buf(struct coda_dev *dev,
1211 struct coda_aux_buf *buf)
1213 if (buf->vaddr) {
1214 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1215 buf->vaddr, buf->paddr);
1216 buf->vaddr = NULL;
1217 buf->size = 0;
1218 debugfs_remove(buf->dentry);
1219 buf->dentry = NULL;
1223 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1225 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1226 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1227 struct coda_q_data *q_data_src, *q_data_dst;
1228 struct vb2_buffer *buf;
1229 int ret = 0;
1231 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1232 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1233 if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
1234 (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
1235 ctx->dev->devtype->product == CODA_7541)) {
1236 /* copy the buffers that where queued before streamon */
1237 mutex_lock(&ctx->bitstream_mutex);
1238 coda_fill_bitstream(ctx);
1239 mutex_unlock(&ctx->bitstream_mutex);
1241 if (coda_get_bitstream_payload(ctx) < 512) {
1242 ret = -EINVAL;
1243 goto err;
1245 } else {
1246 if (count < 1) {
1247 ret = -EINVAL;
1248 goto err;
1252 ctx->streamon_out = 1;
1253 } else {
1254 if (count < 1) {
1255 ret = -EINVAL;
1256 goto err;
1259 ctx->streamon_cap = 1;
1262 /* Don't start the coda unless both queues are on */
1263 if (!(ctx->streamon_out & ctx->streamon_cap))
1264 return 0;
1266 /* Allow BIT decoder device_run with no new buffers queued */
1267 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1268 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1270 ctx->gopcounter = ctx->params.gop_size - 1;
1271 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1273 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1274 q_data_dst->fourcc);
1275 if (!ctx->codec) {
1276 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1277 ret = -EINVAL;
1278 goto err;
1281 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1282 ctx->params.gop_size = 1;
1283 ctx->gopcounter = ctx->params.gop_size - 1;
1285 ret = ctx->ops->start_streaming(ctx);
1286 if (ctx->inst_type == CODA_INST_DECODER) {
1287 if (ret == -EAGAIN)
1288 return 0;
1289 else if (ret < 0)
1290 goto err;
1293 ctx->initialized = 1;
1294 return ret;
1296 err:
1297 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1298 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1299 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1300 } else {
1301 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1302 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
1304 return ret;
1307 static void coda_stop_streaming(struct vb2_queue *q)
1309 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1310 struct coda_dev *dev = ctx->dev;
1311 struct vb2_buffer *buf;
1313 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1314 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1315 "%s: output\n", __func__);
1316 ctx->streamon_out = 0;
1318 coda_bit_stream_end_flag(ctx);
1320 ctx->qsequence = 0;
1322 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1323 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1324 } else {
1325 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1326 "%s: capture\n", __func__);
1327 ctx->streamon_cap = 0;
1329 ctx->osequence = 0;
1330 ctx->sequence_offset = 0;
1332 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1333 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
1336 if (!ctx->streamon_out && !ctx->streamon_cap) {
1337 struct coda_buffer_meta *meta;
1339 if (ctx->ops->seq_end_work) {
1340 queue_work(dev->workqueue, &ctx->seq_end_work);
1341 flush_work(&ctx->seq_end_work);
1343 mutex_lock(&ctx->bitstream_mutex);
1344 while (!list_empty(&ctx->buffer_meta_list)) {
1345 meta = list_first_entry(&ctx->buffer_meta_list,
1346 struct coda_buffer_meta, list);
1347 list_del(&meta->list);
1348 kfree(meta);
1350 mutex_unlock(&ctx->bitstream_mutex);
1351 kfifo_init(&ctx->bitstream_fifo,
1352 ctx->bitstream.vaddr, ctx->bitstream.size);
1353 ctx->initialized = 0;
1354 ctx->runcounter = 0;
1355 ctx->aborting = 0;
1359 static const struct vb2_ops coda_qops = {
1360 .queue_setup = coda_queue_setup,
1361 .buf_prepare = coda_buf_prepare,
1362 .buf_queue = coda_buf_queue,
1363 .start_streaming = coda_start_streaming,
1364 .stop_streaming = coda_stop_streaming,
1365 .wait_prepare = vb2_ops_wait_prepare,
1366 .wait_finish = vb2_ops_wait_finish,
1369 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1371 struct coda_ctx *ctx =
1372 container_of(ctrl->handler, struct coda_ctx, ctrls);
1374 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1375 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1377 switch (ctrl->id) {
1378 case V4L2_CID_HFLIP:
1379 if (ctrl->val)
1380 ctx->params.rot_mode |= CODA_MIR_HOR;
1381 else
1382 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1383 break;
1384 case V4L2_CID_VFLIP:
1385 if (ctrl->val)
1386 ctx->params.rot_mode |= CODA_MIR_VER;
1387 else
1388 ctx->params.rot_mode &= ~CODA_MIR_VER;
1389 break;
1390 case V4L2_CID_MPEG_VIDEO_BITRATE:
1391 ctx->params.bitrate = ctrl->val / 1000;
1392 break;
1393 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1394 ctx->params.gop_size = ctrl->val;
1395 break;
1396 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1397 ctx->params.h264_intra_qp = ctrl->val;
1398 break;
1399 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1400 ctx->params.h264_inter_qp = ctrl->val;
1401 break;
1402 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1403 ctx->params.h264_min_qp = ctrl->val;
1404 break;
1405 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1406 ctx->params.h264_max_qp = ctrl->val;
1407 break;
1408 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1409 ctx->params.h264_deblk_alpha = ctrl->val;
1410 break;
1411 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1412 ctx->params.h264_deblk_beta = ctrl->val;
1413 break;
1414 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1415 ctx->params.h264_deblk_enabled = (ctrl->val ==
1416 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1417 break;
1418 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1419 ctx->params.mpeg4_intra_qp = ctrl->val;
1420 break;
1421 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1422 ctx->params.mpeg4_inter_qp = ctrl->val;
1423 break;
1424 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1425 ctx->params.slice_mode = ctrl->val;
1426 break;
1427 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1428 ctx->params.slice_max_mb = ctrl->val;
1429 break;
1430 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1431 ctx->params.slice_max_bits = ctrl->val * 8;
1432 break;
1433 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1434 break;
1435 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1436 ctx->params.intra_refresh = ctrl->val;
1437 break;
1438 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1439 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1440 break;
1441 case V4L2_CID_JPEG_RESTART_INTERVAL:
1442 ctx->params.jpeg_restart_interval = ctrl->val;
1443 break;
1444 default:
1445 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1446 "Invalid control, id=%d, val=%d\n",
1447 ctrl->id, ctrl->val);
1448 return -EINVAL;
1451 return 0;
1454 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1455 .s_ctrl = coda_s_ctrl,
1458 static void coda_encode_ctrls(struct coda_ctx *ctx)
1460 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1461 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
1462 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1463 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1464 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1465 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1466 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1467 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1468 if (ctx->dev->devtype->product != CODA_960) {
1469 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1470 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1472 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1473 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1474 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1475 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1476 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1477 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1478 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1479 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1480 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1481 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1482 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1483 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1484 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1485 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1486 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1487 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1488 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1489 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1490 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1491 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1492 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1493 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1494 500);
1495 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1496 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1497 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1498 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1499 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1500 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1501 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1502 1920 * 1088 / 256, 1, 0);
1505 static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1507 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1508 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1509 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1510 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1513 static int coda_ctrls_setup(struct coda_ctx *ctx)
1515 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1517 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1518 V4L2_CID_HFLIP, 0, 1, 1, 0);
1519 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1520 V4L2_CID_VFLIP, 0, 1, 1, 0);
1521 if (ctx->inst_type == CODA_INST_ENCODER) {
1522 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1523 coda_jpeg_encode_ctrls(ctx);
1524 else
1525 coda_encode_ctrls(ctx);
1528 if (ctx->ctrls.error) {
1529 v4l2_err(&ctx->dev->v4l2_dev,
1530 "control initialization error (%d)",
1531 ctx->ctrls.error);
1532 return -EINVAL;
1535 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1538 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1540 vq->drv_priv = ctx;
1541 vq->ops = &coda_qops;
1542 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1543 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1544 vq->lock = &ctx->dev->dev_mutex;
1545 /* One way to indicate end-of-stream for coda is to set the
1546 * bytesused == 0. However by default videobuf2 handles bytesused
1547 * equal to 0 as a special case and changes its value to the size
1548 * of the buffer. Set the allow_zero_bytesused flag, so
1549 * that videobuf2 will keep the value of bytesused intact.
1551 vq->allow_zero_bytesused = 1;
1553 return vb2_queue_init(vq);
1556 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1557 struct vb2_queue *dst_vq)
1559 int ret;
1561 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1562 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1563 src_vq->mem_ops = &vb2_dma_contig_memops;
1565 ret = coda_queue_init(priv, src_vq);
1566 if (ret)
1567 return ret;
1569 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1570 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1571 dst_vq->mem_ops = &vb2_dma_contig_memops;
1573 return coda_queue_init(priv, dst_vq);
1576 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1577 struct vb2_queue *dst_vq)
1579 int ret;
1581 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1582 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1583 src_vq->mem_ops = &vb2_vmalloc_memops;
1585 ret = coda_queue_init(priv, src_vq);
1586 if (ret)
1587 return ret;
1589 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1590 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1591 dst_vq->mem_ops = &vb2_dma_contig_memops;
1593 return coda_queue_init(priv, dst_vq);
1596 static int coda_next_free_instance(struct coda_dev *dev)
1598 int idx = ffz(dev->instance_mask);
1600 if ((idx < 0) ||
1601 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1602 return -EBUSY;
1604 return idx;
1608 * File operations
1611 static int coda_open(struct file *file)
1613 struct video_device *vdev = video_devdata(file);
1614 struct coda_dev *dev = video_get_drvdata(vdev);
1615 struct coda_ctx *ctx = NULL;
1616 char *name;
1617 int ret;
1618 int idx;
1620 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1621 if (!ctx)
1622 return -ENOMEM;
1624 idx = coda_next_free_instance(dev);
1625 if (idx < 0) {
1626 ret = idx;
1627 goto err_coda_max;
1629 set_bit(idx, &dev->instance_mask);
1631 name = kasprintf(GFP_KERNEL, "context%d", idx);
1632 if (!name) {
1633 ret = -ENOMEM;
1634 goto err_coda_name_init;
1637 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1638 kfree(name);
1640 ctx->cvd = to_coda_video_device(vdev);
1641 ctx->inst_type = ctx->cvd->type;
1642 ctx->ops = ctx->cvd->ops;
1643 ctx->use_bit = !ctx->cvd->direct;
1644 init_completion(&ctx->completion);
1645 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
1646 if (ctx->ops->seq_end_work)
1647 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1648 v4l2_fh_init(&ctx->fh, video_devdata(file));
1649 file->private_data = &ctx->fh;
1650 v4l2_fh_add(&ctx->fh);
1651 ctx->dev = dev;
1652 ctx->idx = idx;
1653 switch (dev->devtype->product) {
1654 case CODA_960:
1655 ctx->frame_mem_ctrl = 1 << 12;
1656 /* fallthrough */
1657 case CODA_7541:
1658 ctx->reg_idx = 0;
1659 break;
1660 default:
1661 ctx->reg_idx = idx;
1664 /* Power up and upload firmware if necessary */
1665 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1666 if (ret < 0) {
1667 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1668 goto err_pm_get;
1671 ret = clk_prepare_enable(dev->clk_per);
1672 if (ret)
1673 goto err_clk_per;
1675 ret = clk_prepare_enable(dev->clk_ahb);
1676 if (ret)
1677 goto err_clk_ahb;
1679 set_default_params(ctx);
1680 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1681 ctx->ops->queue_init);
1682 if (IS_ERR(ctx->fh.m2m_ctx)) {
1683 ret = PTR_ERR(ctx->fh.m2m_ctx);
1685 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1686 __func__, ret);
1687 goto err_ctx_init;
1690 ret = coda_ctrls_setup(ctx);
1691 if (ret) {
1692 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1693 goto err_ctrls_setup;
1696 ctx->fh.ctrl_handler = &ctx->ctrls;
1698 if (ctx->use_bit) {
1699 ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
1700 CODA_PARA_BUF_SIZE, "parabuf");
1701 if (ret < 0) {
1702 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1703 goto err_dma_alloc;
1706 if (ctx->use_bit && ctx->inst_type == CODA_INST_DECODER) {
1707 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1708 ctx->bitstream.vaddr = dma_alloc_writecombine(
1709 &dev->plat_dev->dev, ctx->bitstream.size,
1710 &ctx->bitstream.paddr, GFP_KERNEL);
1711 if (!ctx->bitstream.vaddr) {
1712 v4l2_err(&dev->v4l2_dev,
1713 "failed to allocate bitstream ringbuffer");
1714 ret = -ENOMEM;
1715 goto err_dma_writecombine;
1718 kfifo_init(&ctx->bitstream_fifo,
1719 ctx->bitstream.vaddr, ctx->bitstream.size);
1720 mutex_init(&ctx->bitstream_mutex);
1721 mutex_init(&ctx->buffer_mutex);
1722 INIT_LIST_HEAD(&ctx->buffer_meta_list);
1724 coda_lock(ctx);
1725 list_add(&ctx->list, &dev->instances);
1726 coda_unlock(ctx);
1728 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1729 ctx->idx, ctx);
1731 return 0;
1733 err_dma_writecombine:
1734 if (ctx->dev->devtype->product == CODA_DX6)
1735 coda_free_aux_buf(dev, &ctx->workbuf);
1736 coda_free_aux_buf(dev, &ctx->parabuf);
1737 err_dma_alloc:
1738 v4l2_ctrl_handler_free(&ctx->ctrls);
1739 err_ctrls_setup:
1740 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1741 err_ctx_init:
1742 clk_disable_unprepare(dev->clk_ahb);
1743 err_clk_ahb:
1744 clk_disable_unprepare(dev->clk_per);
1745 err_clk_per:
1746 pm_runtime_put_sync(&dev->plat_dev->dev);
1747 err_pm_get:
1748 v4l2_fh_del(&ctx->fh);
1749 v4l2_fh_exit(&ctx->fh);
1750 clear_bit(ctx->idx, &dev->instance_mask);
1751 err_coda_name_init:
1752 err_coda_max:
1753 kfree(ctx);
1754 return ret;
1757 static int coda_release(struct file *file)
1759 struct coda_dev *dev = video_drvdata(file);
1760 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1762 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1763 ctx);
1765 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
1766 coda_bit_stream_end_flag(ctx);
1768 /* If this instance is running, call .job_abort and wait for it to end */
1769 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1771 /* In case the instance was not running, we still need to call SEQ_END */
1772 if (ctx->initialized && ctx->ops->seq_end_work) {
1773 queue_work(dev->workqueue, &ctx->seq_end_work);
1774 flush_work(&ctx->seq_end_work);
1777 coda_lock(ctx);
1778 list_del(&ctx->list);
1779 coda_unlock(ctx);
1781 if (ctx->bitstream.vaddr) {
1782 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1783 ctx->bitstream.vaddr, ctx->bitstream.paddr);
1785 if (ctx->dev->devtype->product == CODA_DX6)
1786 coda_free_aux_buf(dev, &ctx->workbuf);
1788 coda_free_aux_buf(dev, &ctx->parabuf);
1789 v4l2_ctrl_handler_free(&ctx->ctrls);
1790 clk_disable_unprepare(dev->clk_ahb);
1791 clk_disable_unprepare(dev->clk_per);
1792 pm_runtime_put_sync(&dev->plat_dev->dev);
1793 v4l2_fh_del(&ctx->fh);
1794 v4l2_fh_exit(&ctx->fh);
1795 clear_bit(ctx->idx, &dev->instance_mask);
1796 if (ctx->ops->release)
1797 ctx->ops->release(ctx);
1798 debugfs_remove_recursive(ctx->debugfs_entry);
1799 kfree(ctx);
1801 return 0;
1804 static const struct v4l2_file_operations coda_fops = {
1805 .owner = THIS_MODULE,
1806 .open = coda_open,
1807 .release = coda_release,
1808 .poll = v4l2_m2m_fop_poll,
1809 .unlocked_ioctl = video_ioctl2,
1810 .mmap = v4l2_m2m_fop_mmap,
1813 static int coda_hw_init(struct coda_dev *dev)
1815 u32 data;
1816 u16 *p;
1817 int i, ret;
1819 ret = clk_prepare_enable(dev->clk_per);
1820 if (ret)
1821 goto err_clk_per;
1823 ret = clk_prepare_enable(dev->clk_ahb);
1824 if (ret)
1825 goto err_clk_ahb;
1827 if (dev->rstc)
1828 reset_control_reset(dev->rstc);
1831 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1832 * The 16-bit chars in the code buffer are in memory access
1833 * order, re-sort them to CODA order for register download.
1834 * Data in this SRAM survives a reboot.
1836 p = (u16 *)dev->codebuf.vaddr;
1837 if (dev->devtype->product == CODA_DX6) {
1838 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1839 data = CODA_DOWN_ADDRESS_SET(i) |
1840 CODA_DOWN_DATA_SET(p[i ^ 1]);
1841 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1843 } else {
1844 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1845 data = CODA_DOWN_ADDRESS_SET(i) |
1846 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1847 3 - (i % 4)]);
1848 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1852 /* Clear registers */
1853 for (i = 0; i < 64; i++)
1854 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1856 /* Tell the BIT where to find everything it needs */
1857 if (dev->devtype->product == CODA_960 ||
1858 dev->devtype->product == CODA_7541) {
1859 coda_write(dev, dev->tempbuf.paddr,
1860 CODA_REG_BIT_TEMP_BUF_ADDR);
1861 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1862 } else {
1863 coda_write(dev, dev->workbuf.paddr,
1864 CODA_REG_BIT_WORK_BUF_ADDR);
1866 coda_write(dev, dev->codebuf.paddr,
1867 CODA_REG_BIT_CODE_BUF_ADDR);
1868 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1870 /* Set default values */
1871 switch (dev->devtype->product) {
1872 case CODA_DX6:
1873 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1874 CODA_REG_BIT_STREAM_CTRL);
1875 break;
1876 default:
1877 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1878 CODA_REG_BIT_STREAM_CTRL);
1880 if (dev->devtype->product == CODA_960)
1881 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1882 else
1883 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1885 if (dev->devtype->product != CODA_DX6)
1886 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1888 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1889 CODA_REG_BIT_INT_ENABLE);
1891 /* Reset VPU and start processor */
1892 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1893 data |= CODA_REG_RESET_ENABLE;
1894 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1895 udelay(10);
1896 data &= ~CODA_REG_RESET_ENABLE;
1897 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1898 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1900 clk_disable_unprepare(dev->clk_ahb);
1901 clk_disable_unprepare(dev->clk_per);
1903 return 0;
1905 err_clk_ahb:
1906 clk_disable_unprepare(dev->clk_per);
1907 err_clk_per:
1908 return ret;
1911 static int coda_register_device(struct coda_dev *dev, int i)
1913 struct video_device *vfd = &dev->vfd[i];
1915 if (i >= dev->devtype->num_vdevs)
1916 return -EINVAL;
1918 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
1919 vfd->fops = &coda_fops;
1920 vfd->ioctl_ops = &coda_ioctl_ops;
1921 vfd->release = video_device_release_empty,
1922 vfd->lock = &dev->dev_mutex;
1923 vfd->v4l2_dev = &dev->v4l2_dev;
1924 vfd->vfl_dir = VFL_DIR_M2M;
1925 video_set_drvdata(vfd, dev);
1927 /* Not applicable, use the selection API instead */
1928 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1929 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1930 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1932 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1935 static void coda_fw_callback(const struct firmware *fw, void *context)
1937 struct coda_dev *dev = context;
1938 struct platform_device *pdev = dev->plat_dev;
1939 int i, ret;
1941 if (!fw) {
1942 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1943 goto put_pm;
1946 /* allocate auxiliary per-device code buffer for the BIT processor */
1947 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1948 dev->debugfs_root);
1949 if (ret < 0) {
1950 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1951 goto put_pm;
1954 /* Copy the whole firmware image to the code buffer */
1955 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1956 release_firmware(fw);
1958 ret = coda_hw_init(dev);
1959 if (ret < 0) {
1960 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1961 goto put_pm;
1964 ret = coda_check_firmware(dev);
1965 if (ret < 0)
1966 goto put_pm;
1968 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1969 if (IS_ERR(dev->alloc_ctx)) {
1970 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1971 goto put_pm;
1974 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1975 if (IS_ERR(dev->m2m_dev)) {
1976 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1977 goto rel_ctx;
1980 for (i = 0; i < dev->devtype->num_vdevs; i++) {
1981 ret = coda_register_device(dev, i);
1982 if (ret) {
1983 v4l2_err(&dev->v4l2_dev,
1984 "Failed to register %s video device: %d\n",
1985 dev->devtype->vdevs[i]->name, ret);
1986 goto rel_vfd;
1990 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1991 dev->vfd[0].num, dev->vfd[i - 1].num);
1993 pm_runtime_put_sync(&pdev->dev);
1994 return;
1996 rel_vfd:
1997 while (--i >= 0)
1998 video_unregister_device(&dev->vfd[i]);
1999 v4l2_m2m_release(dev->m2m_dev);
2000 rel_ctx:
2001 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2002 put_pm:
2003 pm_runtime_put_sync(&pdev->dev);
2006 static int coda_firmware_request(struct coda_dev *dev)
2008 char *fw = dev->devtype->firmware;
2010 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2011 coda_product_name(dev->devtype->product));
2013 return request_firmware_nowait(THIS_MODULE, true,
2014 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2017 enum coda_platform {
2018 CODA_IMX27,
2019 CODA_IMX53,
2020 CODA_IMX6Q,
2021 CODA_IMX6DL,
2024 static const struct coda_devtype coda_devdata[] = {
2025 [CODA_IMX27] = {
2026 .firmware = "v4l-codadx6-imx27.bin",
2027 .product = CODA_DX6,
2028 .codecs = codadx6_codecs,
2029 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2030 .vdevs = codadx6_video_devices,
2031 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
2032 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
2033 .iram_size = 0xb000,
2035 [CODA_IMX53] = {
2036 .firmware = "v4l-coda7541-imx53.bin",
2037 .product = CODA_7541,
2038 .codecs = coda7_codecs,
2039 .num_codecs = ARRAY_SIZE(coda7_codecs),
2040 .vdevs = coda7_video_devices,
2041 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
2042 .workbuf_size = 128 * 1024,
2043 .tempbuf_size = 304 * 1024,
2044 .iram_size = 0x14000,
2046 [CODA_IMX6Q] = {
2047 .firmware = "v4l-coda960-imx6q.bin",
2048 .product = CODA_960,
2049 .codecs = coda9_codecs,
2050 .num_codecs = ARRAY_SIZE(coda9_codecs),
2051 .vdevs = coda9_video_devices,
2052 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2053 .workbuf_size = 80 * 1024,
2054 .tempbuf_size = 204 * 1024,
2055 .iram_size = 0x21000,
2057 [CODA_IMX6DL] = {
2058 .firmware = "v4l-coda960-imx6dl.bin",
2059 .product = CODA_960,
2060 .codecs = coda9_codecs,
2061 .num_codecs = ARRAY_SIZE(coda9_codecs),
2062 .vdevs = coda9_video_devices,
2063 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
2064 .workbuf_size = 80 * 1024,
2065 .tempbuf_size = 204 * 1024,
2066 .iram_size = 0x20000,
2070 static struct platform_device_id coda_platform_ids[] = {
2071 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2072 { /* sentinel */ }
2074 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2076 #ifdef CONFIG_OF
2077 static const struct of_device_id coda_dt_ids[] = {
2078 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
2079 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2080 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2081 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
2082 { /* sentinel */ }
2084 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2085 #endif
2087 static int coda_probe(struct platform_device *pdev)
2089 const struct of_device_id *of_id =
2090 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2091 const struct platform_device_id *pdev_id;
2092 struct coda_platform_data *pdata = pdev->dev.platform_data;
2093 struct device_node *np = pdev->dev.of_node;
2094 struct gen_pool *pool;
2095 struct coda_dev *dev;
2096 struct resource *res;
2097 int ret, irq;
2099 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
2100 if (!dev)
2101 return -ENOMEM;
2103 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2105 if (of_id) {
2106 dev->devtype = of_id->data;
2107 } else if (pdev_id) {
2108 dev->devtype = &coda_devdata[pdev_id->driver_data];
2109 } else {
2110 ret = -EINVAL;
2111 goto err_v4l2_register;
2114 spin_lock_init(&dev->irqlock);
2115 INIT_LIST_HEAD(&dev->instances);
2117 dev->plat_dev = pdev;
2118 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2119 if (IS_ERR(dev->clk_per)) {
2120 dev_err(&pdev->dev, "Could not get per clock\n");
2121 return PTR_ERR(dev->clk_per);
2124 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2125 if (IS_ERR(dev->clk_ahb)) {
2126 dev_err(&pdev->dev, "Could not get ahb clock\n");
2127 return PTR_ERR(dev->clk_ahb);
2130 /* Get memory for physical registers */
2131 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2132 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2133 if (IS_ERR(dev->regs_base))
2134 return PTR_ERR(dev->regs_base);
2136 /* IRQ */
2137 irq = platform_get_irq_byname(pdev, "bit");
2138 if (irq < 0)
2139 irq = platform_get_irq(pdev, 0);
2140 if (irq < 0) {
2141 dev_err(&pdev->dev, "failed to get irq resource\n");
2142 return irq;
2145 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2146 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2147 if (ret < 0) {
2148 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2149 return ret;
2152 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
2153 if (IS_ERR(dev->rstc)) {
2154 ret = PTR_ERR(dev->rstc);
2155 if (ret == -ENOENT || ret == -ENOSYS) {
2156 dev->rstc = NULL;
2157 } else {
2158 dev_err(&pdev->dev, "failed get reset control: %d\n",
2159 ret);
2160 return ret;
2164 /* Get IRAM pool from device tree or platform data */
2165 pool = of_get_named_gen_pool(np, "iram", 0);
2166 if (!pool && pdata)
2167 pool = dev_get_gen_pool(pdata->iram_dev);
2168 if (!pool) {
2169 dev_err(&pdev->dev, "iram pool not available\n");
2170 return -ENOMEM;
2172 dev->iram_pool = pool;
2174 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2175 if (ret)
2176 return ret;
2178 mutex_init(&dev->dev_mutex);
2179 mutex_init(&dev->coda_mutex);
2181 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2182 if (!dev->debugfs_root)
2183 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2185 /* allocate auxiliary per-device buffers for the BIT processor */
2186 if (dev->devtype->product == CODA_DX6) {
2187 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2188 dev->devtype->workbuf_size, "workbuf",
2189 dev->debugfs_root);
2190 if (ret < 0) {
2191 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2192 goto err_v4l2_register;
2196 if (dev->devtype->tempbuf_size) {
2197 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2198 dev->devtype->tempbuf_size, "tempbuf",
2199 dev->debugfs_root);
2200 if (ret < 0) {
2201 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
2202 goto err_v4l2_register;
2206 dev->iram.size = dev->devtype->iram_size;
2207 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2208 &dev->iram.paddr);
2209 if (!dev->iram.vaddr) {
2210 dev_warn(&pdev->dev, "unable to alloc iram\n");
2211 } else {
2212 memset(dev->iram.vaddr, 0, dev->iram.size);
2213 dev->iram.blob.data = dev->iram.vaddr;
2214 dev->iram.blob.size = dev->iram.size;
2215 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2216 dev->debugfs_root,
2217 &dev->iram.blob);
2220 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2221 if (!dev->workqueue) {
2222 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2223 ret = -ENOMEM;
2224 goto err_v4l2_register;
2227 platform_set_drvdata(pdev, dev);
2230 * Start activated so we can directly call coda_hw_init in
2231 * coda_fw_callback regardless of whether CONFIG_PM is
2232 * enabled or whether the device is associated with a PM domain.
2234 pm_runtime_get_noresume(&pdev->dev);
2235 pm_runtime_set_active(&pdev->dev);
2236 pm_runtime_enable(&pdev->dev);
2238 return coda_firmware_request(dev);
2240 err_v4l2_register:
2241 v4l2_device_unregister(&dev->v4l2_dev);
2242 return ret;
2245 static int coda_remove(struct platform_device *pdev)
2247 struct coda_dev *dev = platform_get_drvdata(pdev);
2248 int i;
2250 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2251 if (video_get_drvdata(&dev->vfd[i]))
2252 video_unregister_device(&dev->vfd[i]);
2254 if (dev->m2m_dev)
2255 v4l2_m2m_release(dev->m2m_dev);
2256 pm_runtime_disable(&pdev->dev);
2257 if (dev->alloc_ctx)
2258 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2259 v4l2_device_unregister(&dev->v4l2_dev);
2260 destroy_workqueue(dev->workqueue);
2261 if (dev->iram.vaddr)
2262 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2263 dev->iram.size);
2264 coda_free_aux_buf(dev, &dev->codebuf);
2265 coda_free_aux_buf(dev, &dev->tempbuf);
2266 coda_free_aux_buf(dev, &dev->workbuf);
2267 debugfs_remove_recursive(dev->debugfs_root);
2268 return 0;
2271 #ifdef CONFIG_PM
2272 static int coda_runtime_resume(struct device *dev)
2274 struct coda_dev *cdev = dev_get_drvdata(dev);
2275 int ret = 0;
2277 if (dev->pm_domain && cdev->codebuf.vaddr) {
2278 ret = coda_hw_init(cdev);
2279 if (ret)
2280 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2283 return ret;
2285 #endif
2287 static const struct dev_pm_ops coda_pm_ops = {
2288 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2291 static struct platform_driver coda_driver = {
2292 .probe = coda_probe,
2293 .remove = coda_remove,
2294 .driver = {
2295 .name = CODA_NAME,
2296 .of_match_table = of_match_ptr(coda_dt_ids),
2297 .pm = &coda_pm_ops,
2299 .id_table = coda_platform_ids,
2302 module_platform_driver(coda_driver);
2304 MODULE_LICENSE("GPL");
2305 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2306 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");