2 * Coda multi-standard codec IP
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
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>
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>
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>
42 #include "coda_regs.h"
44 #define CODA_NAME "coda"
46 #define CODADX6_MAX_INSTANCES 4
48 #define CODA_PARA_BUF_SIZE (10 * 1024)
49 #define CODA_ISRAM_SIZE (2048 * 2)
54 #define S_ALIGN 1 /* multiple of 2 */
55 #define W_ALIGN 1 /* multiple of 2 */
56 #define H_ALIGN 1 /* multiple of 2 */
58 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
61 module_param(coda_debug, int, 0644);
62 MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
69 void coda_write(struct coda_dev *dev, u32 data, u32 reg)
71 v4l2_dbg(1, 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)
79 data = readl(dev->regs_base + reg);
80 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
81 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
86 * Array of all formats supported by any version of Coda:
88 static const struct coda_fmt coda_formats[] = {
90 .name = "YUV 4:2:0 Planar, YCbCr",
91 .fourcc = V4L2_PIX_FMT_YUV420,
94 .name = "YUV 4:2:0 Planar, YCrCb",
95 .fourcc = V4L2_PIX_FMT_YVU420,
98 .name = "H264 Encoded Stream",
99 .fourcc = V4L2_PIX_FMT_H264,
102 .name = "MPEG4 Encoded Stream",
103 .fourcc = V4L2_PIX_FMT_MPEG4,
107 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
108 { mode, src_fourcc, dst_fourcc, max_w, max_h }
111 * Arrays of codecs supported by each given version of Coda:
115 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
117 static const struct coda_codec codadx6_codecs[] = {
118 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
119 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
122 static const struct coda_codec coda7_codecs[] = {
123 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
124 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
125 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
126 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
129 static const struct coda_codec coda9_codecs[] = {
130 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
131 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
132 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
133 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
136 static bool coda_format_is_yuv(u32 fourcc)
139 case V4L2_PIX_FMT_YUV420:
140 case V4L2_PIX_FMT_YVU420:
148 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
151 static u32 coda_format_normalize_yuv(u32 fourcc)
153 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
156 static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
157 int src_fourcc, int dst_fourcc)
159 const struct coda_codec *codecs = dev->devtype->codecs;
160 int num_codecs = dev->devtype->num_codecs;
163 src_fourcc = coda_format_normalize_yuv(src_fourcc);
164 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
165 if (src_fourcc == dst_fourcc)
168 for (k = 0; k < num_codecs; k++) {
169 if (codecs[k].src_fourcc == src_fourcc &&
170 codecs[k].dst_fourcc == dst_fourcc)
180 static void coda_get_max_dimensions(struct coda_dev *dev,
181 const struct coda_codec *codec,
182 int *max_w, int *max_h)
184 const struct coda_codec *codecs = dev->devtype->codecs;
185 int num_codecs = dev->devtype->num_codecs;
193 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
194 w = max(w, codecs[k].max_w);
195 h = max(h, codecs[k].max_h);
205 const char *coda_product_name(int product)
217 snprintf(buf, sizeof(buf), "(0x%04x)", product);
223 * V4L2 ioctl() operations.
225 static int coda_querycap(struct file *file, void *priv,
226 struct v4l2_capability *cap)
228 struct coda_ctx *ctx = fh_to_ctx(priv);
230 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
231 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
233 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
234 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
235 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
240 static int coda_enum_fmt(struct file *file, void *priv,
241 struct v4l2_fmtdesc *f)
243 struct coda_ctx *ctx = fh_to_ctx(priv);
244 const struct coda_codec *codecs = ctx->dev->devtype->codecs;
245 const struct coda_fmt *formats = coda_formats;
246 const struct coda_fmt *fmt;
247 int num_codecs = ctx->dev->devtype->num_codecs;
248 int num_formats = ARRAY_SIZE(coda_formats);
252 if (ctx->inst_type == CODA_INST_ENCODER)
253 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
255 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
257 for (i = 0; i < num_formats; i++) {
258 /* Skip either raw or compressed formats */
259 if (yuv != coda_format_is_yuv(formats[i].fourcc))
261 /* All uncompressed formats are always supported */
268 /* Compressed formats may be supported, check the codec list */
269 for (k = 0; k < num_codecs; k++) {
270 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
271 formats[i].fourcc == codecs[k].dst_fourcc)
273 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
274 formats[i].fourcc == codecs[k].src_fourcc)
277 if (k < num_codecs) {
284 if (i < num_formats) {
286 strlcpy(f->description, fmt->name, sizeof(f->description));
287 f->pixelformat = fmt->fourcc;
289 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
293 /* Format not found */
297 static int coda_g_fmt(struct file *file, void *priv,
298 struct v4l2_format *f)
300 struct coda_q_data *q_data;
301 struct coda_ctx *ctx = fh_to_ctx(priv);
303 q_data = get_q_data(ctx, f->type);
307 f->fmt.pix.field = V4L2_FIELD_NONE;
308 f->fmt.pix.pixelformat = q_data->fourcc;
309 f->fmt.pix.width = q_data->width;
310 f->fmt.pix.height = q_data->height;
311 f->fmt.pix.bytesperline = q_data->bytesperline;
313 f->fmt.pix.sizeimage = q_data->sizeimage;
314 f->fmt.pix.colorspace = ctx->colorspace;
319 static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
320 struct v4l2_format *f)
322 struct coda_dev *dev = ctx->dev;
323 struct coda_q_data *q_data;
324 unsigned int max_w, max_h;
325 enum v4l2_field field;
327 field = f->fmt.pix.field;
328 if (field == V4L2_FIELD_ANY)
329 field = V4L2_FIELD_NONE;
330 else if (V4L2_FIELD_NONE != field)
333 /* V4L2 specification suggests the driver corrects the format struct
334 * if any of the dimensions is unsupported */
335 f->fmt.pix.field = field;
337 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
338 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
339 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
342 switch (f->fmt.pix.pixelformat) {
343 case V4L2_PIX_FMT_YUV420:
344 case V4L2_PIX_FMT_YVU420:
345 case V4L2_PIX_FMT_H264:
346 case V4L2_PIX_FMT_MPEG4:
347 case V4L2_PIX_FMT_JPEG:
350 q_data = get_q_data(ctx, f->type);
353 f->fmt.pix.pixelformat = q_data->fourcc;
356 switch (f->fmt.pix.pixelformat) {
357 case V4L2_PIX_FMT_YUV420:
358 case V4L2_PIX_FMT_YVU420:
359 /* Frame stride must be multiple of 8, but 16 for h.264 */
360 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
361 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
362 f->fmt.pix.height * 3 / 2;
364 case V4L2_PIX_FMT_H264:
365 case V4L2_PIX_FMT_MPEG4:
366 case V4L2_PIX_FMT_JPEG:
367 f->fmt.pix.bytesperline = 0;
368 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
377 static int coda_try_fmt_vid_cap(struct file *file, void *priv,
378 struct v4l2_format *f)
380 struct coda_ctx *ctx = fh_to_ctx(priv);
381 const struct coda_codec *codec = NULL;
382 struct vb2_queue *src_vq;
386 * If the source format is already fixed, try to find a codec that
387 * converts to the given destination format
389 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
390 if (vb2_is_streaming(src_vq)) {
391 struct coda_q_data *q_data_src;
393 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
394 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
395 f->fmt.pix.pixelformat);
399 f->fmt.pix.width = q_data_src->width;
400 f->fmt.pix.height = q_data_src->height;
402 /* Otherwise determine codec by encoded format, if possible */
403 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
404 f->fmt.pix.pixelformat);
407 f->fmt.pix.colorspace = ctx->colorspace;
409 ret = coda_try_fmt(ctx, codec, f);
413 /* The h.264 decoder only returns complete 16x16 macroblocks */
414 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
415 f->fmt.pix.width = f->fmt.pix.width;
416 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
417 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
418 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
419 f->fmt.pix.height * 3 / 2;
425 static int coda_try_fmt_vid_out(struct file *file, void *priv,
426 struct v4l2_format *f)
428 struct coda_ctx *ctx = fh_to_ctx(priv);
429 const struct coda_codec *codec;
431 /* Determine codec by encoded format, returns NULL if raw or invalid */
432 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
433 V4L2_PIX_FMT_YUV420);
434 if (!codec && ctx->inst_type == CODA_INST_DECODER) {
435 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_H264,
436 V4L2_PIX_FMT_YUV420);
441 if (!f->fmt.pix.colorspace)
442 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
444 return coda_try_fmt(ctx, codec, f);
447 static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
449 struct coda_q_data *q_data;
450 struct vb2_queue *vq;
452 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
456 q_data = get_q_data(ctx, f->type);
460 if (vb2_is_busy(vq)) {
461 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
465 q_data->fourcc = f->fmt.pix.pixelformat;
466 q_data->width = f->fmt.pix.width;
467 q_data->height = f->fmt.pix.height;
468 q_data->bytesperline = f->fmt.pix.bytesperline;
469 q_data->sizeimage = f->fmt.pix.sizeimage;
470 q_data->rect.left = 0;
471 q_data->rect.top = 0;
472 q_data->rect.width = f->fmt.pix.width;
473 q_data->rect.height = f->fmt.pix.height;
475 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
476 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
477 f->type, q_data->width, q_data->height, q_data->fourcc);
482 static int coda_s_fmt_vid_cap(struct file *file, void *priv,
483 struct v4l2_format *f)
485 struct coda_ctx *ctx = fh_to_ctx(priv);
488 ret = coda_try_fmt_vid_cap(file, priv, f);
492 return coda_s_fmt(ctx, f);
495 static int coda_s_fmt_vid_out(struct file *file, void *priv,
496 struct v4l2_format *f)
498 struct coda_ctx *ctx = fh_to_ctx(priv);
501 ret = coda_try_fmt_vid_out(file, priv, f);
505 ret = coda_s_fmt(ctx, f);
507 ctx->colorspace = f->fmt.pix.colorspace;
512 static int coda_qbuf(struct file *file, void *priv,
513 struct v4l2_buffer *buf)
515 struct coda_ctx *ctx = fh_to_ctx(priv);
517 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
520 static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
521 struct v4l2_buffer *buf)
523 struct vb2_queue *src_vq;
525 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
527 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
528 (buf->sequence == (ctx->qsequence - 1)));
531 static int coda_dqbuf(struct file *file, void *priv,
532 struct v4l2_buffer *buf)
534 struct coda_ctx *ctx = fh_to_ctx(priv);
537 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
539 /* If this is the last capture buffer, emit an end-of-stream event */
540 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
541 coda_buf_is_end_of_stream(ctx, buf)) {
542 const struct v4l2_event eos_event = {
543 .type = V4L2_EVENT_EOS
546 v4l2_event_queue_fh(&ctx->fh, &eos_event);
552 static int coda_g_selection(struct file *file, void *fh,
553 struct v4l2_selection *s)
555 struct coda_ctx *ctx = fh_to_ctx(fh);
556 struct coda_q_data *q_data;
557 struct v4l2_rect r, *rsel;
559 q_data = get_q_data(ctx, s->type);
565 r.width = q_data->width;
566 r.height = q_data->height;
567 rsel = &q_data->rect;
570 case V4L2_SEL_TGT_CROP_DEFAULT:
571 case V4L2_SEL_TGT_CROP_BOUNDS:
574 case V4L2_SEL_TGT_CROP:
575 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
578 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
579 case V4L2_SEL_TGT_COMPOSE_PADDED:
582 case V4L2_SEL_TGT_COMPOSE:
583 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
584 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
596 static int coda_try_decoder_cmd(struct file *file, void *fh,
597 struct v4l2_decoder_cmd *dc)
599 if (dc->cmd != V4L2_DEC_CMD_STOP)
602 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
605 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
611 static int coda_decoder_cmd(struct file *file, void *fh,
612 struct v4l2_decoder_cmd *dc)
614 struct coda_ctx *ctx = fh_to_ctx(fh);
617 ret = coda_try_decoder_cmd(file, fh, dc);
621 /* Ignore decoder stop command silently in encoder context */
622 if (ctx->inst_type != CODA_INST_DECODER)
625 /* Set the stream-end flag on this context */
626 coda_bit_stream_end_flag(ctx);
628 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
633 static int coda_subscribe_event(struct v4l2_fh *fh,
634 const struct v4l2_event_subscription *sub)
638 return v4l2_event_subscribe(fh, sub, 0, NULL);
640 return v4l2_ctrl_subscribe_event(fh, sub);
644 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
645 .vidioc_querycap = coda_querycap,
647 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
648 .vidioc_g_fmt_vid_cap = coda_g_fmt,
649 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
650 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
652 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
653 .vidioc_g_fmt_vid_out = coda_g_fmt,
654 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
655 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
657 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
658 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
660 .vidioc_qbuf = coda_qbuf,
661 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
662 .vidioc_dqbuf = coda_dqbuf,
663 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
665 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
666 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
668 .vidioc_g_selection = coda_g_selection,
670 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
671 .vidioc_decoder_cmd = coda_decoder_cmd,
673 .vidioc_subscribe_event = coda_subscribe_event,
674 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
677 void coda_set_gdi_regs(struct coda_ctx *ctx)
679 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
680 struct coda_dev *dev = ctx->dev;
683 for (i = 0; i < 16; i++)
684 coda_write(dev, tiled_map->xy2ca_map[i],
685 CODA9_GDI_XY2_CAS_0 + 4 * i);
686 for (i = 0; i < 4; i++)
687 coda_write(dev, tiled_map->xy2ba_map[i],
688 CODA9_GDI_XY2_BA_0 + 4 * i);
689 for (i = 0; i < 16; i++)
690 coda_write(dev, tiled_map->xy2ra_map[i],
691 CODA9_GDI_XY2_RAS_0 + 4 * i);
692 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
693 for (i = 0; i < 32; i++)
694 coda_write(dev, tiled_map->rbc2axi_map[i],
695 CODA9_GDI_RBC2_AXI_0 + 4 * i);
699 * Mem-to-mem operations.
702 static void coda_device_run(void *m2m_priv)
704 struct coda_ctx *ctx = m2m_priv;
705 struct coda_dev *dev = ctx->dev;
707 queue_work(dev->workqueue, &ctx->pic_run_work);
710 static void coda_pic_run_work(struct work_struct *work)
712 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
713 struct coda_dev *dev = ctx->dev;
716 mutex_lock(&ctx->buffer_mutex);
717 mutex_lock(&dev->coda_mutex);
719 ret = ctx->ops->prepare_run(ctx);
720 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
721 mutex_unlock(&dev->coda_mutex);
722 mutex_unlock(&ctx->buffer_mutex);
723 /* job_finish scheduled by prepare_decode */
727 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
728 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
733 } else if (!ctx->aborting) {
734 ctx->ops->finish_run(ctx);
737 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
738 queue_work(dev->workqueue, &ctx->seq_end_work);
740 mutex_unlock(&dev->coda_mutex);
741 mutex_unlock(&ctx->buffer_mutex);
743 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
746 static int coda_job_ready(void *m2m_priv)
748 struct coda_ctx *ctx = m2m_priv;
751 * For both 'P' and 'key' frame cases 1 picture
752 * and 1 frame are needed. In the decoder case,
753 * the compressed frame can be in the bitstream.
755 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
756 ctx->inst_type != CODA_INST_DECODER) {
757 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
758 "not ready: not enough video buffers.\n");
762 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
763 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
764 "not ready: not enough video capture buffers.\n");
769 ((ctx->inst_type == CODA_INST_DECODER) &&
770 (coda_get_bitstream_payload(ctx) < 512) &&
771 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
772 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
773 "%d: not ready: not enough bitstream data.\n",
779 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
780 "not ready: aborting\n");
784 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
789 static void coda_job_abort(void *priv)
791 struct coda_ctx *ctx = priv;
795 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
799 static void coda_lock(void *m2m_priv)
801 struct coda_ctx *ctx = m2m_priv;
802 struct coda_dev *pcdev = ctx->dev;
803 mutex_lock(&pcdev->dev_mutex);
806 static void coda_unlock(void *m2m_priv)
808 struct coda_ctx *ctx = m2m_priv;
809 struct coda_dev *pcdev = ctx->dev;
810 mutex_unlock(&pcdev->dev_mutex);
813 static const struct v4l2_m2m_ops coda_m2m_ops = {
814 .device_run = coda_device_run,
815 .job_ready = coda_job_ready,
816 .job_abort = coda_job_abort,
818 .unlock = coda_unlock,
821 static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
823 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
824 int luma_map, chro_map, i;
826 memset(tiled_map, 0, sizeof(*tiled_map));
830 tiled_map->map_type = tiled_map_type;
831 for (i = 0; i < 16; i++)
832 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
833 for (i = 0; i < 4; i++)
834 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
835 for (i = 0; i < 16; i++)
836 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
838 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
839 tiled_map->xy2rbc_config = 0;
841 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
847 static void set_default_params(struct coda_ctx *ctx)
849 u32 src_fourcc, dst_fourcc;
853 if (ctx->inst_type == CODA_INST_ENCODER) {
854 src_fourcc = V4L2_PIX_FMT_YUV420;
855 dst_fourcc = V4L2_PIX_FMT_H264;
857 src_fourcc = V4L2_PIX_FMT_H264;
858 dst_fourcc = V4L2_PIX_FMT_YUV420;
860 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
861 max_w = ctx->codec->max_w;
862 max_h = ctx->codec->max_h;
864 ctx->params.codec_mode = ctx->codec->mode;
865 ctx->colorspace = V4L2_COLORSPACE_REC709;
866 ctx->params.framerate = 30;
869 /* Default formats for output and input queues */
870 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
871 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
872 ctx->q_data[V4L2_M2M_SRC].width = max_w;
873 ctx->q_data[V4L2_M2M_SRC].height = max_h;
874 ctx->q_data[V4L2_M2M_DST].width = max_w;
875 ctx->q_data[V4L2_M2M_DST].height = max_h;
876 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
877 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
878 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
879 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
880 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
882 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
883 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
884 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
885 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
887 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
888 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
889 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
890 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
892 if (ctx->dev->devtype->product == CODA_960)
893 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
899 static int coda_queue_setup(struct vb2_queue *vq,
900 const struct v4l2_format *fmt,
901 unsigned int *nbuffers, unsigned int *nplanes,
902 unsigned int sizes[], void *alloc_ctxs[])
904 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
905 struct coda_q_data *q_data;
908 q_data = get_q_data(ctx, vq->type);
909 size = q_data->sizeimage;
914 alloc_ctxs[0] = ctx->dev->alloc_ctx;
916 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
917 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
922 static int coda_buf_prepare(struct vb2_buffer *vb)
924 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
925 struct coda_q_data *q_data;
927 q_data = get_q_data(ctx, vb->vb2_queue->type);
929 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
930 v4l2_warn(&ctx->dev->v4l2_dev,
931 "%s data will not fit into plane (%lu < %lu)\n",
932 __func__, vb2_plane_size(vb, 0),
933 (long)q_data->sizeimage);
940 static void coda_buf_queue(struct vb2_buffer *vb)
942 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
943 struct coda_q_data *q_data;
945 q_data = get_q_data(ctx, vb->vb2_queue->type);
948 * In the decoder case, immediately try to copy the buffer into the
949 * bitstream ringbuffer and mark it as ready to be dequeued.
951 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
952 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
954 * For backwards compatibility, queuing an empty buffer marks
957 if (vb2_get_plane_payload(vb, 0) == 0)
958 coda_bit_stream_end_flag(ctx);
959 mutex_lock(&ctx->bitstream_mutex);
960 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
961 if (vb2_is_streaming(vb->vb2_queue))
962 coda_fill_bitstream(ctx);
963 mutex_unlock(&ctx->bitstream_mutex);
965 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
969 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
970 size_t size, const char *name, struct dentry *parent)
972 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
979 if (name && parent) {
980 buf->blob.data = buf->vaddr;
981 buf->blob.size = size;
982 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
984 dev_warn(&dev->plat_dev->dev,
985 "failed to create debugfs entry %s\n", name);
991 void coda_free_aux_buf(struct coda_dev *dev,
992 struct coda_aux_buf *buf)
995 dma_free_coherent(&dev->plat_dev->dev, buf->size,
996 buf->vaddr, buf->paddr);
1000 debugfs_remove(buf->dentry);
1003 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1005 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1006 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1007 struct coda_q_data *q_data_src, *q_data_dst;
1008 struct vb2_buffer *buf;
1012 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1013 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1014 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
1015 /* copy the buffers that where queued before streamon */
1016 mutex_lock(&ctx->bitstream_mutex);
1017 coda_fill_bitstream(ctx);
1018 mutex_unlock(&ctx->bitstream_mutex);
1020 if (coda_get_bitstream_payload(ctx) < 512) {
1031 ctx->streamon_out = 1;
1038 ctx->streamon_cap = 1;
1041 /* Don't start the coda unless both queues are on */
1042 if (!(ctx->streamon_out & ctx->streamon_cap))
1045 /* Allow decoder device_run with no new buffers queued */
1046 if (ctx->inst_type == CODA_INST_DECODER)
1047 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
1049 ctx->gopcounter = ctx->params.gop_size - 1;
1050 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1051 dst_fourcc = q_data_dst->fourcc;
1053 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1054 q_data_dst->fourcc);
1056 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1061 ret = ctx->ops->start_streaming(ctx);
1062 if (ctx->inst_type == CODA_INST_DECODER) {
1069 ctx->initialized = 1;
1073 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1074 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1075 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1077 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1078 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1083 static void coda_stop_streaming(struct vb2_queue *q)
1085 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1086 struct coda_dev *dev = ctx->dev;
1088 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1089 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1090 "%s: output\n", __func__);
1091 ctx->streamon_out = 0;
1093 coda_bit_stream_end_flag(ctx);
1096 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1097 "%s: capture\n", __func__);
1098 ctx->streamon_cap = 0;
1101 ctx->sequence_offset = 0;
1104 if (!ctx->streamon_out && !ctx->streamon_cap) {
1105 struct coda_timestamp *ts;
1107 while (!list_empty(&ctx->timestamp_list)) {
1108 ts = list_first_entry(&ctx->timestamp_list,
1109 struct coda_timestamp, list);
1110 list_del(&ts->list);
1113 kfifo_init(&ctx->bitstream_fifo,
1114 ctx->bitstream.vaddr, ctx->bitstream.size);
1115 ctx->runcounter = 0;
1119 static const struct vb2_ops coda_qops = {
1120 .queue_setup = coda_queue_setup,
1121 .buf_prepare = coda_buf_prepare,
1122 .buf_queue = coda_buf_queue,
1123 .start_streaming = coda_start_streaming,
1124 .stop_streaming = coda_stop_streaming,
1125 .wait_prepare = vb2_ops_wait_prepare,
1126 .wait_finish = vb2_ops_wait_finish,
1129 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1131 struct coda_ctx *ctx =
1132 container_of(ctrl->handler, struct coda_ctx, ctrls);
1134 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1135 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1138 case V4L2_CID_HFLIP:
1140 ctx->params.rot_mode |= CODA_MIR_HOR;
1142 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1144 case V4L2_CID_VFLIP:
1146 ctx->params.rot_mode |= CODA_MIR_VER;
1148 ctx->params.rot_mode &= ~CODA_MIR_VER;
1150 case V4L2_CID_MPEG_VIDEO_BITRATE:
1151 ctx->params.bitrate = ctrl->val / 1000;
1153 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1154 ctx->params.gop_size = ctrl->val;
1156 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1157 ctx->params.h264_intra_qp = ctrl->val;
1159 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1160 ctx->params.h264_inter_qp = ctrl->val;
1162 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1163 ctx->params.h264_min_qp = ctrl->val;
1165 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1166 ctx->params.h264_max_qp = ctrl->val;
1168 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1169 ctx->params.h264_deblk_alpha = ctrl->val;
1171 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1172 ctx->params.h264_deblk_beta = ctrl->val;
1174 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1175 ctx->params.h264_deblk_enabled = (ctrl->val ==
1176 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1178 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1179 ctx->params.mpeg4_intra_qp = ctrl->val;
1181 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1182 ctx->params.mpeg4_inter_qp = ctrl->val;
1184 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1185 ctx->params.slice_mode = ctrl->val;
1187 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1188 ctx->params.slice_max_mb = ctrl->val;
1190 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1191 ctx->params.slice_max_bits = ctrl->val * 8;
1193 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1195 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1196 ctx->params.intra_refresh = ctrl->val;
1199 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1200 "Invalid control, id=%d, val=%d\n",
1201 ctrl->id, ctrl->val);
1208 static const struct v4l2_ctrl_ops coda_ctrl_ops = {
1209 .s_ctrl = coda_s_ctrl,
1212 static int coda_ctrls_setup(struct coda_ctx *ctx)
1214 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1216 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1217 V4L2_CID_HFLIP, 0, 1, 1, 0);
1218 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1219 V4L2_CID_VFLIP, 0, 1, 1, 0);
1220 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1221 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1222 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1223 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1224 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1225 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
1226 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1227 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
1228 if (ctx->dev->devtype->product != CODA_960) {
1229 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1230 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1232 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1233 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
1234 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1235 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1236 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1237 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1238 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1239 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1240 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1241 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1242 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1243 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1244 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1245 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1246 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1247 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1248 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1249 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1250 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1251 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1252 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1253 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
1254 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1255 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1256 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1257 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1258 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1259 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1260 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
1262 if (ctx->ctrls.error) {
1263 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1268 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1271 static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
1274 vq->ops = &coda_qops;
1275 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1276 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1277 vq->lock = &ctx->dev->dev_mutex;
1279 return vb2_queue_init(vq);
1282 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1283 struct vb2_queue *dst_vq)
1287 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1288 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1289 src_vq->mem_ops = &vb2_dma_contig_memops;
1291 ret = coda_queue_init(priv, src_vq);
1295 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1296 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1297 dst_vq->mem_ops = &vb2_dma_contig_memops;
1299 return coda_queue_init(priv, dst_vq);
1302 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1303 struct vb2_queue *dst_vq)
1307 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1308 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1309 src_vq->mem_ops = &vb2_dma_contig_memops;
1311 ret = coda_queue_init(priv, src_vq);
1315 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1316 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1317 dst_vq->mem_ops = &vb2_dma_contig_memops;
1319 return coda_queue_init(priv, dst_vq);
1322 static int coda_next_free_instance(struct coda_dev *dev)
1324 int idx = ffz(dev->instance_mask);
1327 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1333 static int coda_open(struct file *file, enum coda_inst_type inst_type,
1334 const struct coda_context_ops *ctx_ops)
1336 struct coda_dev *dev = video_drvdata(file);
1337 struct coda_ctx *ctx = NULL;
1342 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1346 idx = coda_next_free_instance(dev);
1351 set_bit(idx, &dev->instance_mask);
1353 name = kasprintf(GFP_KERNEL, "context%d", idx);
1354 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1357 ctx->inst_type = inst_type;
1359 init_completion(&ctx->completion);
1360 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
1361 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
1362 v4l2_fh_init(&ctx->fh, video_devdata(file));
1363 file->private_data = &ctx->fh;
1364 v4l2_fh_add(&ctx->fh);
1367 switch (dev->devtype->product) {
1376 /* Power up and upload firmware if necessary */
1377 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1379 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1383 ret = clk_prepare_enable(dev->clk_per);
1387 ret = clk_prepare_enable(dev->clk_ahb);
1391 set_default_params(ctx);
1392 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1393 ctx->ops->queue_init);
1394 if (IS_ERR(ctx->fh.m2m_ctx)) {
1395 ret = PTR_ERR(ctx->fh.m2m_ctx);
1397 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1402 ret = coda_ctrls_setup(ctx);
1404 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1405 goto err_ctrls_setup;
1408 ctx->fh.ctrl_handler = &ctx->ctrls;
1410 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
1413 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1417 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1418 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1419 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1420 if (!ctx->bitstream.vaddr) {
1421 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
1423 goto err_dma_writecombine;
1425 kfifo_init(&ctx->bitstream_fifo,
1426 ctx->bitstream.vaddr, ctx->bitstream.size);
1427 mutex_init(&ctx->bitstream_mutex);
1428 mutex_init(&ctx->buffer_mutex);
1429 INIT_LIST_HEAD(&ctx->timestamp_list);
1432 list_add(&ctx->list, &dev->instances);
1435 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1440 err_dma_writecombine:
1441 if (ctx->dev->devtype->product == CODA_DX6)
1442 coda_free_aux_buf(dev, &ctx->workbuf);
1443 coda_free_aux_buf(dev, &ctx->parabuf);
1445 v4l2_ctrl_handler_free(&ctx->ctrls);
1447 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1449 clk_disable_unprepare(dev->clk_ahb);
1451 clk_disable_unprepare(dev->clk_per);
1453 pm_runtime_put_sync(&dev->plat_dev->dev);
1455 v4l2_fh_del(&ctx->fh);
1456 v4l2_fh_exit(&ctx->fh);
1457 clear_bit(ctx->idx, &dev->instance_mask);
1463 static int coda_encoder_open(struct file *file)
1465 return coda_open(file, CODA_INST_ENCODER, &coda_bit_encode_ops);
1468 static int coda_decoder_open(struct file *file)
1470 return coda_open(file, CODA_INST_DECODER, &coda_bit_decode_ops);
1473 static int coda_release(struct file *file)
1475 struct coda_dev *dev = video_drvdata(file);
1476 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1478 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1481 debugfs_remove_recursive(ctx->debugfs_entry);
1483 /* If this instance is running, call .job_abort and wait for it to end */
1484 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1486 /* In case the instance was not running, we still need to call SEQ_END */
1487 if (ctx->initialized) {
1488 queue_work(dev->workqueue, &ctx->seq_end_work);
1489 flush_work(&ctx->seq_end_work);
1493 list_del(&ctx->list);
1496 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1497 ctx->bitstream.vaddr, ctx->bitstream.paddr);
1498 if (ctx->dev->devtype->product == CODA_DX6)
1499 coda_free_aux_buf(dev, &ctx->workbuf);
1501 coda_free_aux_buf(dev, &ctx->parabuf);
1502 v4l2_ctrl_handler_free(&ctx->ctrls);
1503 clk_disable_unprepare(dev->clk_ahb);
1504 clk_disable_unprepare(dev->clk_per);
1505 pm_runtime_put_sync(&dev->plat_dev->dev);
1506 v4l2_fh_del(&ctx->fh);
1507 v4l2_fh_exit(&ctx->fh);
1508 clear_bit(ctx->idx, &dev->instance_mask);
1509 if (ctx->ops->release)
1510 ctx->ops->release(ctx);
1516 static const struct v4l2_file_operations coda_encoder_fops = {
1517 .owner = THIS_MODULE,
1518 .open = coda_encoder_open,
1519 .release = coda_release,
1520 .poll = v4l2_m2m_fop_poll,
1521 .unlocked_ioctl = video_ioctl2,
1522 .mmap = v4l2_m2m_fop_mmap,
1525 static const struct v4l2_file_operations coda_decoder_fops = {
1526 .owner = THIS_MODULE,
1527 .open = coda_decoder_open,
1528 .release = coda_release,
1529 .poll = v4l2_m2m_fop_poll,
1530 .unlocked_ioctl = video_ioctl2,
1531 .mmap = v4l2_m2m_fop_mmap,
1534 static int coda_hw_init(struct coda_dev *dev)
1540 ret = clk_prepare_enable(dev->clk_per);
1544 ret = clk_prepare_enable(dev->clk_ahb);
1549 reset_control_reset(dev->rstc);
1552 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1553 * The 16-bit chars in the code buffer are in memory access
1554 * order, re-sort them to CODA order for register download.
1555 * Data in this SRAM survives a reboot.
1557 p = (u16 *)dev->codebuf.vaddr;
1558 if (dev->devtype->product == CODA_DX6) {
1559 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1560 data = CODA_DOWN_ADDRESS_SET(i) |
1561 CODA_DOWN_DATA_SET(p[i ^ 1]);
1562 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1565 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1566 data = CODA_DOWN_ADDRESS_SET(i) |
1567 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1569 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1573 /* Clear registers */
1574 for (i = 0; i < 64; i++)
1575 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1577 /* Tell the BIT where to find everything it needs */
1578 if (dev->devtype->product == CODA_960 ||
1579 dev->devtype->product == CODA_7541) {
1580 coda_write(dev, dev->tempbuf.paddr,
1581 CODA_REG_BIT_TEMP_BUF_ADDR);
1582 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1584 coda_write(dev, dev->workbuf.paddr,
1585 CODA_REG_BIT_WORK_BUF_ADDR);
1587 coda_write(dev, dev->codebuf.paddr,
1588 CODA_REG_BIT_CODE_BUF_ADDR);
1589 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1591 /* Set default values */
1592 switch (dev->devtype->product) {
1594 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1597 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1599 if (dev->devtype->product == CODA_960)
1600 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1602 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1604 if (dev->devtype->product != CODA_DX6)
1605 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1607 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1608 CODA_REG_BIT_INT_ENABLE);
1610 /* Reset VPU and start processor */
1611 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1612 data |= CODA_REG_RESET_ENABLE;
1613 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1615 data &= ~CODA_REG_RESET_ENABLE;
1616 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1617 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1619 clk_disable_unprepare(dev->clk_ahb);
1620 clk_disable_unprepare(dev->clk_per);
1625 clk_disable_unprepare(dev->clk_per);
1630 static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
1632 vfd->release = video_device_release_empty,
1633 vfd->lock = &dev->dev_mutex;
1634 vfd->v4l2_dev = &dev->v4l2_dev;
1635 vfd->vfl_dir = VFL_DIR_M2M;
1636 video_set_drvdata(vfd, dev);
1638 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1641 static void coda_fw_callback(const struct firmware *fw, void *context)
1643 struct coda_dev *dev = context;
1644 struct platform_device *pdev = dev->plat_dev;
1648 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1652 /* allocate auxiliary per-device code buffer for the BIT processor */
1653 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1656 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1660 /* Copy the whole firmware image to the code buffer */
1661 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1662 release_firmware(fw);
1664 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
1666 * Enabling power temporarily will cause coda_hw_init to be
1667 * called via coda_runtime_resume by the pm domain.
1669 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1671 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
1676 ret = coda_check_firmware(dev);
1680 pm_runtime_put_sync(&dev->plat_dev->dev);
1683 * If runtime pm is disabled or pm_domain is not set,
1684 * initialize once manually.
1686 ret = coda_hw_init(dev);
1688 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1692 ret = coda_check_firmware(dev);
1697 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1698 if (IS_ERR(dev->alloc_ctx)) {
1699 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1703 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1704 if (IS_ERR(dev->m2m_dev)) {
1705 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1709 dev->vfd[0].fops = &coda_encoder_fops,
1710 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
1711 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
1712 ret = coda_register_device(dev, &dev->vfd[0]);
1714 v4l2_err(&dev->v4l2_dev,
1715 "Failed to register encoder video device\n");
1719 dev->vfd[1].fops = &coda_decoder_fops,
1720 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
1721 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
1722 ret = coda_register_device(dev, &dev->vfd[1]);
1724 v4l2_err(&dev->v4l2_dev,
1725 "Failed to register decoder video device\n");
1729 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1730 dev->vfd[0].num, dev->vfd[1].num);
1735 v4l2_m2m_release(dev->m2m_dev);
1737 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1740 static int coda_firmware_request(struct coda_dev *dev)
1742 char *fw = dev->devtype->firmware;
1744 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1745 coda_product_name(dev->devtype->product));
1747 return request_firmware_nowait(THIS_MODULE, true,
1748 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1751 enum coda_platform {
1758 static const struct coda_devtype coda_devdata[] = {
1760 .firmware = "v4l-codadx6-imx27.bin",
1761 .product = CODA_DX6,
1762 .codecs = codadx6_codecs,
1763 .num_codecs = ARRAY_SIZE(codadx6_codecs),
1764 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
1765 .iram_size = 0xb000,
1768 .firmware = "v4l-coda7541-imx53.bin",
1769 .product = CODA_7541,
1770 .codecs = coda7_codecs,
1771 .num_codecs = ARRAY_SIZE(coda7_codecs),
1772 .workbuf_size = 128 * 1024,
1773 .tempbuf_size = 304 * 1024,
1774 .iram_size = 0x14000,
1777 .firmware = "v4l-coda960-imx6q.bin",
1778 .product = CODA_960,
1779 .codecs = coda9_codecs,
1780 .num_codecs = ARRAY_SIZE(coda9_codecs),
1781 .workbuf_size = 80 * 1024,
1782 .tempbuf_size = 204 * 1024,
1783 .iram_size = 0x21000,
1786 .firmware = "v4l-coda960-imx6dl.bin",
1787 .product = CODA_960,
1788 .codecs = coda9_codecs,
1789 .num_codecs = ARRAY_SIZE(coda9_codecs),
1790 .workbuf_size = 80 * 1024,
1791 .tempbuf_size = 204 * 1024,
1792 .iram_size = 0x20000,
1796 static struct platform_device_id coda_platform_ids[] = {
1797 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
1798 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
1801 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1804 static const struct of_device_id coda_dt_ids[] = {
1805 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
1806 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
1807 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
1808 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
1811 MODULE_DEVICE_TABLE(of, coda_dt_ids);
1814 static int coda_probe(struct platform_device *pdev)
1816 const struct of_device_id *of_id =
1817 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1818 const struct platform_device_id *pdev_id;
1819 struct coda_platform_data *pdata = pdev->dev.platform_data;
1820 struct device_node *np = pdev->dev.of_node;
1821 struct gen_pool *pool;
1822 struct coda_dev *dev;
1823 struct resource *res;
1826 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1828 dev_err(&pdev->dev, "Not enough memory for %s\n",
1833 spin_lock_init(&dev->irqlock);
1834 INIT_LIST_HEAD(&dev->instances);
1836 dev->plat_dev = pdev;
1837 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1838 if (IS_ERR(dev->clk_per)) {
1839 dev_err(&pdev->dev, "Could not get per clock\n");
1840 return PTR_ERR(dev->clk_per);
1843 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1844 if (IS_ERR(dev->clk_ahb)) {
1845 dev_err(&pdev->dev, "Could not get ahb clock\n");
1846 return PTR_ERR(dev->clk_ahb);
1849 /* Get memory for physical registers */
1850 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1851 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1852 if (IS_ERR(dev->regs_base))
1853 return PTR_ERR(dev->regs_base);
1856 irq = platform_get_irq_byname(pdev, "bit");
1858 irq = platform_get_irq(pdev, 0);
1860 dev_err(&pdev->dev, "failed to get irq resource\n");
1864 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
1865 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
1867 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
1871 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
1872 if (IS_ERR(dev->rstc)) {
1873 ret = PTR_ERR(dev->rstc);
1874 if (ret == -ENOENT || ret == -ENOSYS) {
1877 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
1882 /* Get IRAM pool from device tree or platform data */
1883 pool = of_get_named_gen_pool(np, "iram", 0);
1885 pool = dev_get_gen_pool(pdata->iram_dev);
1887 dev_err(&pdev->dev, "iram pool not available\n");
1890 dev->iram_pool = pool;
1892 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1896 mutex_init(&dev->dev_mutex);
1897 mutex_init(&dev->coda_mutex);
1899 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
1902 dev->devtype = of_id->data;
1903 } else if (pdev_id) {
1904 dev->devtype = &coda_devdata[pdev_id->driver_data];
1906 v4l2_device_unregister(&dev->v4l2_dev);
1910 dev->debugfs_root = debugfs_create_dir("coda", NULL);
1911 if (!dev->debugfs_root)
1912 dev_warn(&pdev->dev, "failed to create debugfs root\n");
1914 /* allocate auxiliary per-device buffers for the BIT processor */
1915 if (dev->devtype->product == CODA_DX6) {
1916 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
1917 dev->devtype->workbuf_size, "workbuf",
1920 dev_err(&pdev->dev, "failed to allocate work buffer\n");
1921 v4l2_device_unregister(&dev->v4l2_dev);
1926 if (dev->devtype->tempbuf_size) {
1927 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
1928 dev->devtype->tempbuf_size, "tempbuf",
1931 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
1932 v4l2_device_unregister(&dev->v4l2_dev);
1937 dev->iram.size = dev->devtype->iram_size;
1938 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
1940 if (!dev->iram.vaddr) {
1941 dev_err(&pdev->dev, "unable to alloc iram\n");
1945 dev->iram.blob.data = dev->iram.vaddr;
1946 dev->iram.blob.size = dev->iram.size;
1947 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
1950 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1951 if (!dev->workqueue) {
1952 dev_err(&pdev->dev, "unable to alloc workqueue\n");
1956 platform_set_drvdata(pdev, dev);
1958 pm_runtime_enable(&pdev->dev);
1960 return coda_firmware_request(dev);
1963 static int coda_remove(struct platform_device *pdev)
1965 struct coda_dev *dev = platform_get_drvdata(pdev);
1967 video_unregister_device(&dev->vfd[0]);
1968 video_unregister_device(&dev->vfd[1]);
1970 v4l2_m2m_release(dev->m2m_dev);
1971 pm_runtime_disable(&pdev->dev);
1973 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1974 v4l2_device_unregister(&dev->v4l2_dev);
1975 destroy_workqueue(dev->workqueue);
1976 if (dev->iram.vaddr)
1977 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
1979 coda_free_aux_buf(dev, &dev->codebuf);
1980 coda_free_aux_buf(dev, &dev->tempbuf);
1981 coda_free_aux_buf(dev, &dev->workbuf);
1982 debugfs_remove_recursive(dev->debugfs_root);
1986 #ifdef CONFIG_PM_RUNTIME
1987 static int coda_runtime_resume(struct device *dev)
1989 struct coda_dev *cdev = dev_get_drvdata(dev);
1992 if (dev->pm_domain && cdev->codebuf.vaddr) {
1993 ret = coda_hw_init(cdev);
1995 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2002 static const struct dev_pm_ops coda_pm_ops = {
2003 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2006 static struct platform_driver coda_driver = {
2007 .probe = coda_probe,
2008 .remove = coda_remove,
2011 .owner = THIS_MODULE,
2012 .of_match_table = of_match_ptr(coda_dt_ids),
2015 .id_table = coda_platform_ids,
2018 module_platform_driver(coda_driver);
2020 MODULE_LICENSE("GPL");
2021 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2022 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");