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/delay.h>
16 #include <linux/firmware.h>
17 #include <linux/genalloc.h>
18 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/kfifo.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
28 #include <linux/platform_data/coda.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-mem2mem.h>
34 #include <media/videobuf2-core.h>
35 #include <media/videobuf2-dma-contig.h>
39 #define CODA_NAME "coda"
41 #define CODA_MAX_INSTANCES 4
43 #define CODA_FMO_BUF_SIZE 32
44 #define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
45 #define CODA7_WORK_BUF_SIZE (128 * 1024)
46 #define CODA7_TEMP_BUF_SIZE (304 * 1024)
47 #define CODA_PARA_BUF_SIZE (10 * 1024)
48 #define CODA_ISRAM_SIZE (2048 * 2)
49 #define CODADX6_IRAM_SIZE 0xb000
50 #define CODA7_IRAM_SIZE 0x14000 /* 81920 bytes */
52 #define CODA_MAX_FRAMEBUFFERS 2
56 #define CODA_MAX_FRAME_SIZE 0x100000
57 #define FMO_SLICE_SAVE_BUF_SIZE (32)
58 #define CODA_DEFAULT_GAMMA 4096
63 #define S_ALIGN 1 /* multiple of 2 */
64 #define W_ALIGN 1 /* multiple of 2 */
65 #define H_ALIGN 1 /* multiple of 2 */
67 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
69 static int coda_debug;
70 module_param(coda_debug, int, 0644);
71 MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
101 struct coda_devtype {
103 enum coda_product product;
104 struct coda_codec *codecs;
105 unsigned int num_codecs;
109 /* Per-queue, driver-specific private data */
113 unsigned int sizeimage;
117 struct coda_aux_buf {
124 struct v4l2_device v4l2_dev;
125 struct video_device vfd;
126 struct platform_device *plat_dev;
127 const struct coda_devtype *devtype;
129 void __iomem *regs_base;
133 struct coda_aux_buf codebuf;
134 struct coda_aux_buf tempbuf;
135 struct coda_aux_buf workbuf;
136 struct gen_pool *iram_pool;
137 long unsigned int iram_vaddr;
138 long unsigned int iram_paddr;
139 unsigned long iram_size;
142 struct mutex dev_mutex;
143 struct mutex coda_mutex;
144 struct v4l2_m2m_dev *m2m_dev;
145 struct vb2_alloc_ctx *alloc_ctx;
146 struct list_head instances;
147 unsigned long instance_mask;
148 struct delayed_work timeout;
160 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
167 struct coda_iram_info {
169 phys_addr_t buf_bit_use;
170 phys_addr_t buf_ip_ac_dc_use;
171 phys_addr_t buf_dbk_y_use;
172 phys_addr_t buf_dbk_c_use;
173 phys_addr_t buf_ovl_use;
174 phys_addr_t buf_btp_use;
175 phys_addr_t search_ram_paddr;
180 struct coda_dev *dev;
181 struct list_head list;
187 struct coda_q_data q_data[2];
188 enum coda_inst_type inst_type;
189 struct coda_codec *codec;
190 enum v4l2_colorspace colorspace;
191 struct coda_params params;
192 struct v4l2_m2m_ctx *m2m_ctx;
193 struct v4l2_ctrl_handler ctrls;
196 char vpu_header[3][64];
197 int vpu_header_size[3];
198 struct kfifo bitstream_fifo;
199 struct mutex bitstream_mutex;
200 struct coda_aux_buf bitstream;
201 struct coda_aux_buf parabuf;
202 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
203 struct coda_aux_buf workbuf;
204 int num_internal_frames;
207 struct coda_iram_info iram_info;
208 u32 bit_stream_param;
211 static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
212 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
213 static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
215 static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
217 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
218 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
219 writel(data, dev->regs_base + reg);
222 static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
225 data = readl(dev->regs_base + reg);
226 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
227 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
231 static inline unsigned long coda_isbusy(struct coda_dev *dev)
233 return coda_read(dev, CODA_REG_BIT_BUSY);
236 static inline int coda_is_initialized(struct coda_dev *dev)
238 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
241 static int coda_wait_timeout(struct coda_dev *dev)
243 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
245 while (coda_isbusy(dev)) {
246 if (time_after(jiffies, timeout))
252 static void coda_command_async(struct coda_ctx *ctx, int cmd)
254 struct coda_dev *dev = ctx->dev;
256 if (dev->devtype->product == CODA_7541) {
257 /* Restore context related registers to CODA */
258 coda_write(dev, ctx->bit_stream_param,
259 CODA_REG_BIT_BIT_STREAM_PARAM);
260 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
263 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
265 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
266 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
267 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
269 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
272 static int coda_command_sync(struct coda_ctx *ctx, int cmd)
274 struct coda_dev *dev = ctx->dev;
276 coda_command_async(ctx, cmd);
277 return coda_wait_timeout(dev);
280 static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
281 enum v4l2_buf_type type)
284 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
285 return &(ctx->q_data[V4L2_M2M_SRC]);
286 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
287 return &(ctx->q_data[V4L2_M2M_DST]);
295 * Array of all formats supported by any version of Coda:
297 static struct coda_fmt coda_formats[] = {
299 .name = "YUV 4:2:0 Planar, YCbCr",
300 .fourcc = V4L2_PIX_FMT_YUV420,
303 .name = "YUV 4:2:0 Planar, YCrCb",
304 .fourcc = V4L2_PIX_FMT_YVU420,
307 .name = "H264 Encoded Stream",
308 .fourcc = V4L2_PIX_FMT_H264,
311 .name = "MPEG4 Encoded Stream",
312 .fourcc = V4L2_PIX_FMT_MPEG4,
316 #define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
317 { mode, src_fourcc, dst_fourcc, max_w, max_h }
320 * Arrays of codecs supported by each given version of Coda:
324 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
326 static struct coda_codec codadx6_codecs[] = {
327 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
328 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
331 static struct coda_codec coda7_codecs[] = {
332 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
333 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
336 static bool coda_format_is_yuv(u32 fourcc)
339 case V4L2_PIX_FMT_YUV420:
340 case V4L2_PIX_FMT_YVU420:
348 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
351 static u32 coda_format_normalize_yuv(u32 fourcc)
353 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
356 static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
359 struct coda_codec *codecs = dev->devtype->codecs;
360 int num_codecs = dev->devtype->num_codecs;
363 src_fourcc = coda_format_normalize_yuv(src_fourcc);
364 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
365 if (src_fourcc == dst_fourcc)
368 for (k = 0; k < num_codecs; k++) {
369 if (codecs[k].src_fourcc == src_fourcc &&
370 codecs[k].dst_fourcc == dst_fourcc)
381 * V4L2 ioctl() operations.
383 static int vidioc_querycap(struct file *file, void *priv,
384 struct v4l2_capability *cap)
386 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
387 strlcpy(cap->card, CODA_NAME, sizeof(cap->card));
388 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
390 * This is only a mem-to-mem video device. The capture and output
391 * device capability flags are left only for backward compatibility
392 * and are scheduled for removal.
394 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
395 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
396 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
401 static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
402 enum v4l2_buf_type type)
404 struct coda_ctx *ctx = fh_to_ctx(priv);
405 struct coda_codec *codecs = ctx->dev->devtype->codecs;
406 struct coda_fmt *formats = coda_formats;
407 struct coda_fmt *fmt;
408 int num_codecs = ctx->dev->devtype->num_codecs;
409 int num_formats = ARRAY_SIZE(coda_formats);
412 for (i = 0; i < num_formats; i++) {
413 /* Both uncompressed formats are always supported */
414 if (coda_format_is_yuv(formats[i].fourcc)) {
420 /* Compressed formats may be supported, check the codec list */
421 for (k = 0; k < num_codecs; k++) {
422 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
423 formats[i].fourcc == codecs[k].dst_fourcc)
425 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
426 formats[i].fourcc == codecs[k].src_fourcc)
429 if (k < num_codecs) {
436 if (i < num_formats) {
438 strlcpy(f->description, fmt->name, sizeof(f->description));
439 f->pixelformat = fmt->fourcc;
443 /* Format not found */
447 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
448 struct v4l2_fmtdesc *f)
450 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE);
453 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
454 struct v4l2_fmtdesc *f)
456 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT);
459 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
461 struct vb2_queue *vq;
462 struct coda_q_data *q_data;
463 struct coda_ctx *ctx = fh_to_ctx(priv);
465 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
469 q_data = get_q_data(ctx, f->type);
471 f->fmt.pix.field = V4L2_FIELD_NONE;
472 f->fmt.pix.pixelformat = q_data->fourcc;
473 f->fmt.pix.width = q_data->width;
474 f->fmt.pix.height = q_data->height;
475 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
476 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
477 else /* encoded formats h.264/mpeg4 */
478 f->fmt.pix.bytesperline = 0;
480 f->fmt.pix.sizeimage = q_data->sizeimage;
481 f->fmt.pix.colorspace = ctx->colorspace;
486 static int vidioc_try_fmt(struct coda_codec *codec, struct v4l2_format *f)
488 unsigned int max_w, max_h;
489 enum v4l2_field field;
491 field = f->fmt.pix.field;
492 if (field == V4L2_FIELD_ANY)
493 field = V4L2_FIELD_NONE;
494 else if (V4L2_FIELD_NONE != field)
497 /* V4L2 specification suggests the driver corrects the format struct
498 * if any of the dimensions is unsupported */
499 f->fmt.pix.field = field;
502 max_w = codec->max_w;
503 max_h = codec->max_h;
508 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w,
509 W_ALIGN, &f->fmt.pix.height,
510 MIN_H, max_h, H_ALIGN, S_ALIGN);
512 if (coda_format_is_yuv(f->fmt.pix.pixelformat)) {
513 /* Frame stride must be multiple of 8 */
514 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
515 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
516 f->fmt.pix.height * 3 / 2;
517 } else { /*encoded formats h.264/mpeg4 */
518 f->fmt.pix.bytesperline = 0;
519 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
525 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
526 struct v4l2_format *f)
528 struct coda_ctx *ctx = fh_to_ctx(priv);
529 struct coda_codec *codec = NULL;
531 /* Determine codec by the encoded format */
532 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
533 f->fmt.pix.pixelformat);
535 f->fmt.pix.colorspace = ctx->colorspace;
537 return vidioc_try_fmt(codec, f);
540 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
541 struct v4l2_format *f)
543 struct coda_ctx *ctx = fh_to_ctx(priv);
544 struct coda_codec *codec;
546 /* Determine codec by encoded format, returns NULL if raw or invalid */
547 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
548 V4L2_PIX_FMT_YUV420);
550 if (!f->fmt.pix.colorspace)
551 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
553 return vidioc_try_fmt(codec, f);
556 static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
558 struct coda_q_data *q_data;
559 struct vb2_queue *vq;
561 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
565 q_data = get_q_data(ctx, f->type);
569 if (vb2_is_busy(vq)) {
570 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
574 q_data->fourcc = f->fmt.pix.pixelformat;
575 q_data->width = f->fmt.pix.width;
576 q_data->height = f->fmt.pix.height;
577 q_data->sizeimage = f->fmt.pix.sizeimage;
579 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
580 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
581 f->type, q_data->width, q_data->height, q_data->fourcc);
586 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
587 struct v4l2_format *f)
589 struct coda_ctx *ctx = fh_to_ctx(priv);
592 ret = vidioc_try_fmt_vid_cap(file, priv, f);
596 return vidioc_s_fmt(ctx, f);
599 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
600 struct v4l2_format *f)
602 struct coda_ctx *ctx = fh_to_ctx(priv);
605 ret = vidioc_try_fmt_vid_out(file, priv, f);
609 ret = vidioc_s_fmt(ctx, f);
611 ctx->colorspace = f->fmt.pix.colorspace;
616 static int vidioc_reqbufs(struct file *file, void *priv,
617 struct v4l2_requestbuffers *reqbufs)
619 struct coda_ctx *ctx = fh_to_ctx(priv);
621 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
624 static int vidioc_querybuf(struct file *file, void *priv,
625 struct v4l2_buffer *buf)
627 struct coda_ctx *ctx = fh_to_ctx(priv);
629 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
632 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
634 struct coda_ctx *ctx = fh_to_ctx(priv);
636 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
639 static int vidioc_expbuf(struct file *file, void *priv,
640 struct v4l2_exportbuffer *eb)
642 struct coda_ctx *ctx = fh_to_ctx(priv);
644 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
647 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
649 struct coda_ctx *ctx = fh_to_ctx(priv);
651 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
654 static int vidioc_create_bufs(struct file *file, void *priv,
655 struct v4l2_create_buffers *create)
657 struct coda_ctx *ctx = fh_to_ctx(priv);
659 return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
662 static int vidioc_streamon(struct file *file, void *priv,
663 enum v4l2_buf_type type)
665 struct coda_ctx *ctx = fh_to_ctx(priv);
667 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
670 static int vidioc_streamoff(struct file *file, void *priv,
671 enum v4l2_buf_type type)
673 struct coda_ctx *ctx = fh_to_ctx(priv);
675 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
678 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
679 .vidioc_querycap = vidioc_querycap,
681 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
682 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
683 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
684 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
686 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
687 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
688 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
689 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
691 .vidioc_reqbufs = vidioc_reqbufs,
692 .vidioc_querybuf = vidioc_querybuf,
694 .vidioc_qbuf = vidioc_qbuf,
695 .vidioc_expbuf = vidioc_expbuf,
696 .vidioc_dqbuf = vidioc_dqbuf,
697 .vidioc_create_bufs = vidioc_create_bufs,
699 .vidioc_streamon = vidioc_streamon,
700 .vidioc_streamoff = vidioc_streamoff,
703 static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
705 return kfifo_len(&ctx->bitstream_fifo);
708 static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
710 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
711 struct coda_dev *dev = ctx->dev;
714 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
715 kfifo->out = (kfifo->in & ~kfifo->mask) |
716 (rd_ptr - ctx->bitstream.paddr);
717 if (kfifo->out > kfifo->in)
718 kfifo->out -= kfifo->mask + 1;
721 static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
723 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
724 struct coda_dev *dev = ctx->dev;
727 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
728 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
729 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
730 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
733 static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
735 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
736 struct coda_dev *dev = ctx->dev;
739 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
740 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
743 static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
745 u32 src_size = vb2_get_plane_payload(src_buf, 0);
748 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
752 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
753 ctx->bitstream.size, DMA_TO_DEVICE);
760 static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
761 struct vb2_buffer *src_buf)
765 if (coda_get_bitstream_payload(ctx) +
766 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
769 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
770 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
774 ret = coda_bitstream_queue(ctx, src_buf);
776 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
779 /* Sync read pointer to device */
780 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
781 coda_kfifo_sync_to_device_write(ctx);
786 static void coda_fill_bitstream(struct coda_ctx *ctx)
788 struct vb2_buffer *src_buf;
790 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
791 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
793 if (coda_bitstream_try_queue(ctx, src_buf)) {
794 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
795 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
803 * Mem-to-mem operations.
805 static void coda_device_run(void *m2m_priv)
807 struct coda_ctx *ctx = m2m_priv;
808 struct coda_q_data *q_data_src, *q_data_dst;
809 struct vb2_buffer *src_buf, *dst_buf;
810 struct coda_dev *dev = ctx->dev;
813 u32 picture_y, picture_cb, picture_cr;
814 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
817 mutex_lock(&dev->coda_mutex);
819 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
820 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
821 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
822 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
823 dst_fourcc = q_data_dst->fourcc;
825 src_buf->v4l2_buf.sequence = ctx->isequence;
826 dst_buf->v4l2_buf.sequence = ctx->isequence;
830 * Workaround coda firmware BUG that only marks the first
831 * frame as IDR. This is a problem for some decoders that can't
832 * recover when a frame is lost.
834 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
835 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
836 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
838 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
839 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
843 * Copy headers at the beginning of the first frame for H.264 only.
844 * In MPEG4 they are already copied by the coda.
846 if (src_buf->v4l2_buf.sequence == 0) {
847 pic_stream_buffer_addr =
848 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
849 ctx->vpu_header_size[0] +
850 ctx->vpu_header_size[1] +
851 ctx->vpu_header_size[2];
852 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
853 ctx->vpu_header_size[0] -
854 ctx->vpu_header_size[1] -
855 ctx->vpu_header_size[2];
856 memcpy(vb2_plane_vaddr(dst_buf, 0),
857 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
858 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
859 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
860 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
861 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
862 ctx->vpu_header_size[2]);
864 pic_stream_buffer_addr =
865 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
866 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
869 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
871 switch (dst_fourcc) {
872 case V4L2_PIX_FMT_H264:
873 quant_param = ctx->params.h264_intra_qp;
875 case V4L2_PIX_FMT_MPEG4:
876 quant_param = ctx->params.mpeg4_intra_qp;
879 v4l2_warn(&ctx->dev->v4l2_dev,
880 "cannot set intra qp, fmt not supported\n");
885 switch (dst_fourcc) {
886 case V4L2_PIX_FMT_H264:
887 quant_param = ctx->params.h264_inter_qp;
889 case V4L2_PIX_FMT_MPEG4:
890 quant_param = ctx->params.mpeg4_inter_qp;
893 v4l2_warn(&ctx->dev->v4l2_dev,
894 "cannot set inter qp, fmt not supported\n");
900 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
901 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
904 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
905 switch (q_data_src->fourcc) {
906 case V4L2_PIX_FMT_YVU420:
907 /* Switch Cb and Cr for YVU420 format */
908 picture_cr = picture_y + q_data_src->width * q_data_src->height;
909 picture_cb = picture_cr + q_data_src->width / 2 *
910 q_data_src->height / 2;
912 case V4L2_PIX_FMT_YUV420:
914 picture_cb = picture_y + q_data_src->width * q_data_src->height;
915 picture_cr = picture_cb + q_data_src->width / 2 *
916 q_data_src->height / 2;
920 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
921 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
922 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
923 coda_write(dev, force_ipicture << 1 & 0x2,
924 CODA_CMD_ENC_PIC_OPTION);
926 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
927 coda_write(dev, pic_stream_buffer_size / 1024,
928 CODA_CMD_ENC_PIC_BB_SIZE);
930 if (dev->devtype->product == CODA_7541) {
931 coda_write(dev, CODA7_USE_BIT_ENABLE | CODA7_USE_HOST_BIT_ENABLE |
932 CODA7_USE_ME_ENABLE | CODA7_USE_HOST_ME_ENABLE,
933 CODA7_REG_BIT_AXI_SRAM_USE);
936 if (dev->devtype->product != CODA_DX6)
937 coda_write(dev, ctx->iram_info.axi_sram_use,
938 CODA7_REG_BIT_AXI_SRAM_USE);
940 /* 1 second timeout in case CODA locks up */
941 schedule_delayed_work(&dev->timeout, HZ);
943 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
946 static int coda_job_ready(void *m2m_priv)
948 struct coda_ctx *ctx = m2m_priv;
951 * For both 'P' and 'key' frame cases 1 picture
952 * and 1 frame are needed. In the decoder case,
953 * the compressed frame can be in the bitstream.
955 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
956 ctx->inst_type != CODA_INST_DECODER) {
957 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
958 "not ready: not enough video buffers.\n");
962 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
963 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
964 "not ready: not enough video capture buffers.\n");
969 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
970 "not ready: aborting\n");
974 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
979 static void coda_job_abort(void *priv)
981 struct coda_ctx *ctx = priv;
985 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
989 static void coda_lock(void *m2m_priv)
991 struct coda_ctx *ctx = m2m_priv;
992 struct coda_dev *pcdev = ctx->dev;
993 mutex_lock(&pcdev->dev_mutex);
996 static void coda_unlock(void *m2m_priv)
998 struct coda_ctx *ctx = m2m_priv;
999 struct coda_dev *pcdev = ctx->dev;
1000 mutex_unlock(&pcdev->dev_mutex);
1003 static struct v4l2_m2m_ops coda_m2m_ops = {
1004 .device_run = coda_device_run,
1005 .job_ready = coda_job_ready,
1006 .job_abort = coda_job_abort,
1008 .unlock = coda_unlock,
1011 static void set_default_params(struct coda_ctx *ctx)
1016 ctx->codec = &ctx->dev->devtype->codecs[0];
1017 max_w = ctx->codec->max_w;
1018 max_h = ctx->codec->max_h;
1020 ctx->params.codec_mode = CODA_MODE_INVALID;
1021 ctx->colorspace = V4L2_COLORSPACE_REC709;
1022 ctx->params.framerate = 30;
1025 /* Default formats for output and input queues */
1026 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1027 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1028 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1029 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1030 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1031 ctx->q_data[V4L2_M2M_DST].width = max_w;
1032 ctx->q_data[V4L2_M2M_DST].height = max_h;
1033 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1039 static int coda_queue_setup(struct vb2_queue *vq,
1040 const struct v4l2_format *fmt,
1041 unsigned int *nbuffers, unsigned int *nplanes,
1042 unsigned int sizes[], void *alloc_ctxs[])
1044 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
1045 struct coda_q_data *q_data;
1048 q_data = get_q_data(ctx, vq->type);
1049 size = q_data->sizeimage;
1054 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1056 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1057 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1062 static int coda_buf_prepare(struct vb2_buffer *vb)
1064 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1065 struct coda_q_data *q_data;
1067 q_data = get_q_data(ctx, vb->vb2_queue->type);
1069 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1070 v4l2_warn(&ctx->dev->v4l2_dev,
1071 "%s data will not fit into plane (%lu < %lu)\n",
1072 __func__, vb2_plane_size(vb, 0),
1073 (long)q_data->sizeimage);
1080 static void coda_buf_queue(struct vb2_buffer *vb)
1082 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1083 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1086 static void coda_wait_prepare(struct vb2_queue *q)
1088 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1092 static void coda_wait_finish(struct vb2_queue *q)
1094 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1098 static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1100 struct coda_dev *dev = ctx->dev;
1101 u32 *p = ctx->parabuf.vaddr;
1103 if (dev->devtype->product == CODA_DX6)
1106 p[index ^ 1] = value;
1109 static int coda_alloc_aux_buf(struct coda_dev *dev,
1110 struct coda_aux_buf *buf, size_t size)
1112 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1122 static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1123 struct coda_aux_buf *buf, size_t size)
1125 return coda_alloc_aux_buf(ctx->dev, buf, size);
1128 static void coda_free_aux_buf(struct coda_dev *dev,
1129 struct coda_aux_buf *buf)
1132 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1133 buf->vaddr, buf->paddr);
1139 static void coda_free_framebuffers(struct coda_ctx *ctx)
1143 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1144 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1147 static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1149 struct coda_dev *dev = ctx->dev;
1150 int height = q_data->height;
1156 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1157 height = round_up(height, 16);
1158 ysize = round_up(q_data->width, 8) * height;
1160 /* Allocate frame buffers */
1161 for (i = 0; i < ctx->num_internal_frames; i++) {
1164 size = q_data->sizeimage;
1165 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1166 dev->devtype->product != CODA_DX6)
1167 ctx->internal_frames[i].size += ysize/4;
1168 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1170 coda_free_framebuffers(ctx);
1175 /* Register frame buffers in the parameter buffer */
1176 for (i = 0; i < ctx->num_internal_frames; i++) {
1177 paddr = ctx->internal_frames[i].paddr;
1178 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1179 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1180 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
1182 /* mvcol buffer for h.264 */
1183 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1184 dev->devtype->product != CODA_DX6)
1185 coda_parabuf_write(ctx, 96 + i,
1186 ctx->internal_frames[i].paddr +
1187 ysize + ysize/4 + ysize/4);
1190 /* mvcol buffer for mpeg4 */
1191 if ((dev->devtype->product != CODA_DX6) &&
1192 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1193 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1194 ysize + ysize/4 + ysize/4);
1199 static int coda_h264_padding(int size, char *p)
1204 diff = size - (size & ~0x7);
1208 nal_size = coda_filler_size[diff];
1209 memcpy(p, coda_filler_nal, nal_size);
1211 /* Add rbsp stop bit and trailing at the end */
1212 *(p + nal_size - 1) = 0x80;
1217 static void coda_setup_iram(struct coda_ctx *ctx)
1219 struct coda_iram_info *iram_info = &ctx->iram_info;
1220 struct coda_dev *dev = ctx->dev;
1228 memset(iram_info, 0, sizeof(*iram_info));
1229 size = dev->iram_size;
1231 if (dev->devtype->product == CODA_DX6)
1234 if (ctx->inst_type == CODA_INST_ENCODER) {
1235 struct coda_q_data *q_data_src;
1237 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1238 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1240 /* Prioritize in case IRAM is too small for everything */
1241 me_size = round_up(round_up(q_data_src->width, 16) * 36 + 2048,
1243 iram_info->search_ram_size = me_size;
1244 if (size >= iram_info->search_ram_size) {
1245 if (dev->devtype->product == CODA_7541)
1246 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE;
1247 iram_info->search_ram_paddr = dev->iram_paddr;
1248 size -= iram_info->search_ram_size;
1250 pr_err("IRAM is smaller than the search ram size\n");
1254 /* Only H.264BP and H.263P3 are considered */
1255 dbk_size = round_up(128 * mb_width, 1024);
1256 if (size >= dbk_size) {
1257 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1258 iram_info->buf_dbk_y_use = dev->iram_paddr +
1259 iram_info->search_ram_size;
1260 iram_info->buf_dbk_c_use = iram_info->buf_dbk_y_use +
1267 bitram_size = round_up(128 * mb_width, 1024);
1268 if (size >= bitram_size) {
1269 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1270 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1272 size -= bitram_size;
1277 ipacdc_size = round_up(128 * mb_width, 1024);
1278 if (size >= ipacdc_size) {
1279 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1280 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1282 size -= ipacdc_size;
1285 /* OVL disabled for encoder */
1289 switch (dev->devtype->product) {
1293 /* i.MX53 uses secondary AXI for IRAM access */
1294 if (iram_info->axi_sram_use & CODA7_USE_HOST_BIT_ENABLE)
1295 iram_info->axi_sram_use |= CODA7_USE_BIT_ENABLE;
1296 if (iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)
1297 iram_info->axi_sram_use |= CODA7_USE_IP_ENABLE;
1298 if (iram_info->axi_sram_use & CODA7_USE_HOST_DBK_ENABLE)
1299 iram_info->axi_sram_use |= CODA7_USE_DBK_ENABLE;
1300 if (iram_info->axi_sram_use & CODA7_USE_HOST_OVL_ENABLE)
1301 iram_info->axi_sram_use |= CODA7_USE_OVL_ENABLE;
1302 if (iram_info->axi_sram_use & CODA7_USE_HOST_ME_ENABLE)
1303 iram_info->axi_sram_use |= CODA7_USE_ME_ENABLE;
1306 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1307 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1308 "IRAM smaller than needed\n");
1310 if (dev->devtype->product == CODA_7541) {
1311 /* TODO - Enabling these causes picture errors on CODA7541 */
1312 if (ctx->inst_type == CODA_INST_ENCODER) {
1313 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1314 CODA7_USE_HOST_DBK_ENABLE |
1315 CODA7_USE_IP_ENABLE |
1316 CODA7_USE_DBK_ENABLE);
1321 static void coda_free_context_buffers(struct coda_ctx *ctx)
1323 struct coda_dev *dev = ctx->dev;
1325 if (dev->devtype->product != CODA_DX6)
1326 coda_free_aux_buf(dev, &ctx->workbuf);
1329 static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1330 struct coda_q_data *q_data)
1332 struct coda_dev *dev = ctx->dev;
1336 switch (dev->devtype->product) {
1338 size = CODA7_WORK_BUF_SIZE;
1344 if (ctx->workbuf.vaddr) {
1345 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1350 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1352 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1360 coda_free_context_buffers(ctx);
1364 static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1365 int header_code, u8 *header, int *size)
1367 struct coda_dev *dev = ctx->dev;
1370 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1371 CODA_CMD_ENC_HEADER_BB_START);
1372 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1373 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1374 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1376 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1379 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
1380 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1381 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1386 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1388 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1389 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1390 u32 bitstream_buf, bitstream_size;
1391 struct coda_dev *dev = ctx->dev;
1392 struct coda_q_data *q_data_src, *q_data_dst;
1393 struct vb2_buffer *buf;
1401 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1402 ctx->streamon_out = 1;
1404 ctx->streamon_cap = 1;
1406 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1407 if (ctx->streamon_out) {
1408 if (coda_format_is_yuv(q_data_src->fourcc))
1409 ctx->inst_type = CODA_INST_ENCODER;
1411 ctx->inst_type = CODA_INST_DECODER;
1414 /* Don't start the coda unless both queues are on */
1415 if (!(ctx->streamon_out & ctx->streamon_cap))
1418 ctx->gopcounter = ctx->params.gop_size - 1;
1419 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1420 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1421 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1422 bitstream_size = q_data_dst->sizeimage;
1423 dst_fourcc = q_data_dst->fourcc;
1425 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1426 q_data_dst->fourcc);
1428 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1432 /* Allocate per-instance buffers */
1433 ret = coda_alloc_context_buffers(ctx, q_data_src);
1437 if (!coda_is_initialized(dev)) {
1438 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1442 mutex_lock(&dev->coda_mutex);
1444 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1445 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1446 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1447 switch (dev->devtype->product) {
1449 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
1450 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1453 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
1454 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1457 if (dev->devtype->product == CODA_DX6) {
1458 /* Configure the coda */
1459 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
1462 /* Could set rotation here if needed */
1463 switch (dev->devtype->product) {
1465 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
1466 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
1469 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
1470 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
1472 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
1473 coda_write(dev, ctx->params.framerate,
1474 CODA_CMD_ENC_SEQ_SRC_F_RATE);
1476 ctx->params.codec_mode = ctx->codec->mode;
1477 switch (dst_fourcc) {
1478 case V4L2_PIX_FMT_MPEG4:
1479 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
1480 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
1482 case V4L2_PIX_FMT_H264:
1483 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
1484 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
1488 "dst format (0x%08x) invalid.\n", dst_fourcc);
1493 switch (ctx->params.slice_mode) {
1494 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
1497 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
1498 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1499 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1500 value |= 1 & CODA_SLICING_MODE_MASK;
1502 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1503 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1504 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1505 value |= 1 & CODA_SLICING_MODE_MASK;
1508 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
1509 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
1510 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1512 if (ctx->params.bitrate) {
1513 /* Rate control enabled */
1514 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1515 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1519 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1521 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1522 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1524 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1525 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1527 /* set default gamma */
1528 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1529 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1531 if (CODA_DEFAULT_GAMMA > 0) {
1532 if (dev->devtype->product == CODA_DX6)
1533 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
1535 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
1539 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1541 coda_setup_iram(ctx);
1543 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1544 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1545 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1546 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
1547 if (dev->devtype->product == CODA_DX6) {
1548 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1550 coda_write(dev, ctx->iram_info.search_ram_paddr,
1551 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1552 coda_write(dev, ctx->iram_info.search_ram_size,
1553 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
1557 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1559 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1563 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
1564 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
1569 ctx->num_internal_frames = 2;
1570 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
1572 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1576 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1577 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
1578 if (dev->devtype->product != CODA_DX6) {
1579 coda_write(dev, ctx->iram_info.buf_bit_use,
1580 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1581 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1582 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1583 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1584 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1585 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1586 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1587 coda_write(dev, ctx->iram_info.buf_ovl_use,
1588 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1590 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1592 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1596 /* Save stream headers */
1597 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1598 switch (dst_fourcc) {
1599 case V4L2_PIX_FMT_H264:
1601 * Get SPS in the first frame and copy it to an
1602 * intermediate buffer.
1604 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1605 &ctx->vpu_header[0][0],
1606 &ctx->vpu_header_size[0]);
1611 * Get PPS in the first frame and copy it to an
1612 * intermediate buffer.
1614 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1615 &ctx->vpu_header[1][0],
1616 &ctx->vpu_header_size[1]);
1621 * Length of H.264 headers is variable and thus it might not be
1622 * aligned for the coda to append the encoded frame. In that is
1623 * the case a filler NAL must be added to header 2.
1625 ctx->vpu_header_size[2] = coda_h264_padding(
1626 (ctx->vpu_header_size[0] +
1627 ctx->vpu_header_size[1]),
1628 ctx->vpu_header[2]);
1630 case V4L2_PIX_FMT_MPEG4:
1632 * Get VOS in the first frame and copy it to an
1633 * intermediate buffer
1635 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1636 &ctx->vpu_header[0][0],
1637 &ctx->vpu_header_size[0]);
1641 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1642 &ctx->vpu_header[1][0],
1643 &ctx->vpu_header_size[1]);
1647 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1648 &ctx->vpu_header[2][0],
1649 &ctx->vpu_header_size[2]);
1654 /* No more formats need to save headers at the moment */
1659 mutex_unlock(&dev->coda_mutex);
1663 static int coda_stop_streaming(struct vb2_queue *q)
1665 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1666 struct coda_dev *dev = ctx->dev;
1668 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1669 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1670 "%s: output\n", __func__);
1671 ctx->streamon_out = 0;
1673 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1674 "%s: capture\n", __func__);
1675 ctx->streamon_cap = 0;
1678 /* Don't stop the coda unless both queues are off */
1679 if (ctx->streamon_out || ctx->streamon_cap)
1682 cancel_delayed_work(&dev->timeout);
1684 mutex_lock(&dev->coda_mutex);
1685 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1686 "%s: sent command 'SEQ_END' to coda\n", __func__);
1687 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1688 v4l2_err(&dev->v4l2_dev,
1689 "CODA_COMMAND_SEQ_END failed\n");
1692 mutex_unlock(&dev->coda_mutex);
1694 coda_free_framebuffers(ctx);
1699 static struct vb2_ops coda_qops = {
1700 .queue_setup = coda_queue_setup,
1701 .buf_prepare = coda_buf_prepare,
1702 .buf_queue = coda_buf_queue,
1703 .wait_prepare = coda_wait_prepare,
1704 .wait_finish = coda_wait_finish,
1705 .start_streaming = coda_start_streaming,
1706 .stop_streaming = coda_stop_streaming,
1709 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1711 struct coda_ctx *ctx =
1712 container_of(ctrl->handler, struct coda_ctx, ctrls);
1714 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1715 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1718 case V4L2_CID_HFLIP:
1720 ctx->params.rot_mode |= CODA_MIR_HOR;
1722 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1724 case V4L2_CID_VFLIP:
1726 ctx->params.rot_mode |= CODA_MIR_VER;
1728 ctx->params.rot_mode &= ~CODA_MIR_VER;
1730 case V4L2_CID_MPEG_VIDEO_BITRATE:
1731 ctx->params.bitrate = ctrl->val / 1000;
1733 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1734 ctx->params.gop_size = ctrl->val;
1736 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1737 ctx->params.h264_intra_qp = ctrl->val;
1739 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1740 ctx->params.h264_inter_qp = ctrl->val;
1742 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1743 ctx->params.mpeg4_intra_qp = ctrl->val;
1745 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1746 ctx->params.mpeg4_inter_qp = ctrl->val;
1748 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1749 ctx->params.slice_mode = ctrl->val;
1751 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1752 ctx->params.slice_max_mb = ctrl->val;
1754 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1755 ctx->params.slice_max_bits = ctrl->val * 8;
1757 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1760 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1761 "Invalid control, id=%d, val=%d\n",
1762 ctrl->id, ctrl->val);
1769 static struct v4l2_ctrl_ops coda_ctrl_ops = {
1770 .s_ctrl = coda_s_ctrl,
1773 static int coda_ctrls_setup(struct coda_ctx *ctx)
1775 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1777 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1778 V4L2_CID_HFLIP, 0, 1, 1, 0);
1779 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1780 V4L2_CID_VFLIP, 0, 1, 1, 0);
1781 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1782 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1783 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1784 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1785 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1786 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1787 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1788 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1789 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1790 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1791 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1792 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1793 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1794 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1795 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1796 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
1797 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1798 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1799 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1800 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
1801 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1802 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1803 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1804 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1805 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1807 if (ctx->ctrls.error) {
1808 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1813 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1816 static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1817 struct vb2_queue *dst_vq)
1819 struct coda_ctx *ctx = priv;
1822 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1823 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1824 src_vq->drv_priv = ctx;
1825 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1826 src_vq->ops = &coda_qops;
1827 src_vq->mem_ops = &vb2_dma_contig_memops;
1828 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1830 ret = vb2_queue_init(src_vq);
1834 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1835 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1836 dst_vq->drv_priv = ctx;
1837 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1838 dst_vq->ops = &coda_qops;
1839 dst_vq->mem_ops = &vb2_dma_contig_memops;
1840 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1842 return vb2_queue_init(dst_vq);
1845 static int coda_next_free_instance(struct coda_dev *dev)
1847 return ffz(dev->instance_mask);
1850 static int coda_open(struct file *file)
1852 struct coda_dev *dev = video_drvdata(file);
1853 struct coda_ctx *ctx = NULL;
1857 idx = coda_next_free_instance(dev);
1858 if (idx >= CODA_MAX_INSTANCES)
1860 set_bit(idx, &dev->instance_mask);
1862 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1866 v4l2_fh_init(&ctx->fh, video_devdata(file));
1867 file->private_data = &ctx->fh;
1868 v4l2_fh_add(&ctx->fh);
1871 switch (dev->devtype->product) {
1878 set_default_params(ctx);
1879 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1881 if (IS_ERR(ctx->m2m_ctx)) {
1882 ret = PTR_ERR(ctx->m2m_ctx);
1884 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1888 ret = coda_ctrls_setup(ctx);
1890 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1894 ctx->fh.ctrl_handler = &ctx->ctrls;
1896 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
1898 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1902 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1903 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1904 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1905 if (!ctx->bitstream.vaddr) {
1906 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
1910 kfifo_init(&ctx->bitstream_fifo,
1911 ctx->bitstream.vaddr, ctx->bitstream.size);
1912 mutex_init(&ctx->bitstream_mutex);
1915 list_add(&ctx->list, &dev->instances);
1918 clk_prepare_enable(dev->clk_per);
1919 clk_prepare_enable(dev->clk_ahb);
1921 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1927 v4l2_fh_del(&ctx->fh);
1928 v4l2_fh_exit(&ctx->fh);
1933 static int coda_release(struct file *file)
1935 struct coda_dev *dev = video_drvdata(file);
1936 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1938 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1942 list_del(&ctx->list);
1945 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1946 ctx->bitstream.vaddr, ctx->bitstream.paddr);
1947 coda_free_context_buffers(ctx);
1948 if (ctx->dev->devtype->product == CODA_DX6)
1949 coda_free_aux_buf(dev, &ctx->workbuf);
1951 coda_free_aux_buf(dev, &ctx->parabuf);
1952 v4l2_ctrl_handler_free(&ctx->ctrls);
1953 clk_disable_unprepare(dev->clk_per);
1954 clk_disable_unprepare(dev->clk_ahb);
1955 v4l2_fh_del(&ctx->fh);
1956 v4l2_fh_exit(&ctx->fh);
1957 clear_bit(ctx->idx, &dev->instance_mask);
1963 static unsigned int coda_poll(struct file *file,
1964 struct poll_table_struct *wait)
1966 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1970 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1975 static int coda_mmap(struct file *file, struct vm_area_struct *vma)
1977 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1979 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1982 static const struct v4l2_file_operations coda_fops = {
1983 .owner = THIS_MODULE,
1985 .release = coda_release,
1987 .unlocked_ioctl = video_ioctl2,
1991 static irqreturn_t coda_irq_handler(int irq, void *data)
1993 struct vb2_buffer *src_buf, *dst_buf;
1994 struct coda_dev *dev = data;
1995 u32 wr_ptr, start_ptr;
1996 struct coda_ctx *ctx;
1998 cancel_delayed_work(&dev->timeout);
2000 /* read status register to attend the IRQ */
2001 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2002 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2003 CODA_REG_BIT_INT_CLEAR);
2005 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2007 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
2008 mutex_unlock(&dev->coda_mutex);
2012 if (ctx->aborting) {
2013 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2014 "task has been aborted\n");
2015 mutex_unlock(&dev->coda_mutex);
2019 if (coda_isbusy(ctx->dev)) {
2020 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2021 "coda is still busy!!!!\n");
2025 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2026 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
2028 /* Get results from the coda */
2029 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
2030 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
2031 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
2033 /* Calculate bytesused field */
2034 if (dst_buf->v4l2_buf.sequence == 0) {
2035 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
2036 ctx->vpu_header_size[0] +
2037 ctx->vpu_header_size[1] +
2038 ctx->vpu_header_size[2]);
2040 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
2043 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
2044 wr_ptr - start_ptr);
2046 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
2047 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
2049 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
2050 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
2051 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
2053 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
2054 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
2057 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
2058 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
2060 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
2061 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
2064 if (ctx->gopcounter < 0)
2065 ctx->gopcounter = ctx->params.gop_size - 1;
2067 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2068 "job finished: encoding frame (%d) (%s)\n",
2069 dst_buf->v4l2_buf.sequence,
2070 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2071 "KEYFRAME" : "PFRAME");
2073 mutex_unlock(&dev->coda_mutex);
2075 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
2080 static void coda_timeout(struct work_struct *work)
2082 struct coda_ctx *ctx;
2083 struct coda_dev *dev = container_of(to_delayed_work(work),
2084 struct coda_dev, timeout);
2086 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
2088 mutex_lock(&dev->dev_mutex);
2089 list_for_each_entry(ctx, &dev->instances, list) {
2090 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2091 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2093 mutex_unlock(&dev->dev_mutex);
2095 mutex_unlock(&dev->coda_mutex);
2096 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2097 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
2100 static u32 coda_supported_firmwares[] = {
2101 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
2102 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
2105 static bool coda_firmware_supported(u32 vernum)
2109 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
2110 if (vernum == coda_supported_firmwares[i])
2115 static char *coda_product_name(int product)
2125 snprintf(buf, sizeof(buf), "(0x%04x)", product);
2130 static int coda_hw_init(struct coda_dev *dev)
2132 u16 product, major, minor, release;
2137 clk_prepare_enable(dev->clk_per);
2138 clk_prepare_enable(dev->clk_ahb);
2141 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
2142 * The 16-bit chars in the code buffer are in memory access
2143 * order, re-sort them to CODA order for register download.
2144 * Data in this SRAM survives a reboot.
2146 p = (u16 *)dev->codebuf.vaddr;
2147 if (dev->devtype->product == CODA_DX6) {
2148 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2149 data = CODA_DOWN_ADDRESS_SET(i) |
2150 CODA_DOWN_DATA_SET(p[i ^ 1]);
2151 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2154 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2155 data = CODA_DOWN_ADDRESS_SET(i) |
2156 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2158 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2162 /* Clear registers */
2163 for (i = 0; i < 64; i++)
2164 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2166 /* Tell the BIT where to find everything it needs */
2167 if (dev->devtype->product == CODA_7541) {
2168 coda_write(dev, dev->tempbuf.paddr,
2169 CODA_REG_BIT_TEMP_BUF_ADDR);
2171 coda_write(dev, dev->workbuf.paddr,
2172 CODA_REG_BIT_WORK_BUF_ADDR);
2174 coda_write(dev, dev->codebuf.paddr,
2175 CODA_REG_BIT_CODE_BUF_ADDR);
2176 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2178 /* Set default values */
2179 switch (dev->devtype->product) {
2181 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2184 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2186 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
2188 if (dev->devtype->product != CODA_DX6)
2189 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2191 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2192 CODA_REG_BIT_INT_ENABLE);
2194 /* Reset VPU and start processor */
2195 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2196 data |= CODA_REG_RESET_ENABLE;
2197 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2199 data &= ~CODA_REG_RESET_ENABLE;
2200 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2201 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2204 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
2205 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
2206 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
2207 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
2208 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
2209 if (coda_wait_timeout(dev)) {
2210 clk_disable_unprepare(dev->clk_per);
2211 clk_disable_unprepare(dev->clk_ahb);
2212 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
2216 /* Check we are compatible with the loaded firmware */
2217 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
2218 product = CODA_FIRMWARE_PRODUCT(data);
2219 major = CODA_FIRMWARE_MAJOR(data);
2220 minor = CODA_FIRMWARE_MINOR(data);
2221 release = CODA_FIRMWARE_RELEASE(data);
2223 clk_disable_unprepare(dev->clk_per);
2224 clk_disable_unprepare(dev->clk_ahb);
2226 if (product != dev->devtype->product) {
2227 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
2228 " Version: %u.%u.%u\n",
2229 coda_product_name(dev->devtype->product),
2230 coda_product_name(product), major, minor, release);
2234 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
2235 coda_product_name(product));
2237 if (coda_firmware_supported(data)) {
2238 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
2239 major, minor, release);
2241 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
2242 "%u.%u.%u\n", major, minor, release);
2248 static void coda_fw_callback(const struct firmware *fw, void *context)
2250 struct coda_dev *dev = context;
2251 struct platform_device *pdev = dev->plat_dev;
2255 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
2259 /* allocate auxiliary per-device code buffer for the BIT processor */
2260 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
2262 dev_err(&pdev->dev, "failed to allocate code buffer\n");
2266 /* Copy the whole firmware image to the code buffer */
2267 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
2268 release_firmware(fw);
2270 ret = coda_hw_init(dev);
2272 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2276 dev->vfd.fops = &coda_fops,
2277 dev->vfd.ioctl_ops = &coda_ioctl_ops;
2278 dev->vfd.release = video_device_release_empty,
2279 dev->vfd.lock = &dev->dev_mutex;
2280 dev->vfd.v4l2_dev = &dev->v4l2_dev;
2281 dev->vfd.vfl_dir = VFL_DIR_M2M;
2282 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
2283 video_set_drvdata(&dev->vfd, dev);
2285 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2286 if (IS_ERR(dev->alloc_ctx)) {
2287 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
2291 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2292 if (IS_ERR(dev->m2m_dev)) {
2293 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2297 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
2299 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
2302 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
2308 v4l2_m2m_release(dev->m2m_dev);
2310 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2313 static int coda_firmware_request(struct coda_dev *dev)
2315 char *fw = dev->devtype->firmware;
2317 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2318 coda_product_name(dev->devtype->product));
2320 return request_firmware_nowait(THIS_MODULE, true,
2321 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2324 enum coda_platform {
2329 static const struct coda_devtype coda_devdata[] = {
2331 .firmware = "v4l-codadx6-imx27.bin",
2332 .product = CODA_DX6,
2333 .codecs = codadx6_codecs,
2334 .num_codecs = ARRAY_SIZE(codadx6_codecs),
2337 .firmware = "v4l-coda7541-imx53.bin",
2338 .product = CODA_7541,
2339 .codecs = coda7_codecs,
2340 .num_codecs = ARRAY_SIZE(coda7_codecs),
2344 static struct platform_device_id coda_platform_ids[] = {
2345 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2346 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
2349 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2352 static const struct of_device_id coda_dt_ids[] = {
2353 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
2354 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
2357 MODULE_DEVICE_TABLE(of, coda_dt_ids);
2360 static int coda_probe(struct platform_device *pdev)
2362 const struct of_device_id *of_id =
2363 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2364 const struct platform_device_id *pdev_id;
2365 struct coda_platform_data *pdata = pdev->dev.platform_data;
2366 struct device_node *np = pdev->dev.of_node;
2367 struct gen_pool *pool;
2368 struct coda_dev *dev;
2369 struct resource *res;
2372 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
2374 dev_err(&pdev->dev, "Not enough memory for %s\n",
2379 spin_lock_init(&dev->irqlock);
2380 INIT_LIST_HEAD(&dev->instances);
2381 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
2383 dev->plat_dev = pdev;
2384 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2385 if (IS_ERR(dev->clk_per)) {
2386 dev_err(&pdev->dev, "Could not get per clock\n");
2387 return PTR_ERR(dev->clk_per);
2390 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2391 if (IS_ERR(dev->clk_ahb)) {
2392 dev_err(&pdev->dev, "Could not get ahb clock\n");
2393 return PTR_ERR(dev->clk_ahb);
2396 /* Get memory for physical registers */
2397 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2399 dev_err(&pdev->dev, "failed to get memory region resource\n");
2403 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2404 if (IS_ERR(dev->regs_base))
2405 return PTR_ERR(dev->regs_base);
2408 irq = platform_get_irq(pdev, 0);
2410 dev_err(&pdev->dev, "failed to get irq resource\n");
2414 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
2415 0, CODA_NAME, dev) < 0) {
2416 dev_err(&pdev->dev, "failed to request irq\n");
2420 /* Get IRAM pool from device tree or platform data */
2421 pool = of_get_named_gen_pool(np, "iram", 0);
2423 pool = dev_get_gen_pool(pdata->iram_dev);
2425 dev_err(&pdev->dev, "iram pool not available\n");
2428 dev->iram_pool = pool;
2430 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2434 mutex_init(&dev->dev_mutex);
2435 mutex_init(&dev->coda_mutex);
2437 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2440 dev->devtype = of_id->data;
2441 } else if (pdev_id) {
2442 dev->devtype = &coda_devdata[pdev_id->driver_data];
2444 v4l2_device_unregister(&dev->v4l2_dev);
2448 /* allocate auxiliary per-device buffers for the BIT processor */
2449 switch (dev->devtype->product) {
2451 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
2452 CODADX6_WORK_BUF_SIZE);
2454 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2455 v4l2_device_unregister(&dev->v4l2_dev);
2460 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
2463 if (dev->tempbuf.size) {
2464 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
2467 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
2468 v4l2_device_unregister(&dev->v4l2_dev);
2473 if (dev->devtype->product == CODA_DX6)
2474 dev->iram_size = CODADX6_IRAM_SIZE;
2476 dev->iram_size = CODA7_IRAM_SIZE;
2477 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
2478 if (!dev->iram_vaddr) {
2479 dev_err(&pdev->dev, "unable to alloc iram\n");
2482 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
2485 platform_set_drvdata(pdev, dev);
2487 return coda_firmware_request(dev);
2490 static int coda_remove(struct platform_device *pdev)
2492 struct coda_dev *dev = platform_get_drvdata(pdev);
2494 video_unregister_device(&dev->vfd);
2496 v4l2_m2m_release(dev->m2m_dev);
2498 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2499 v4l2_device_unregister(&dev->v4l2_dev);
2500 if (dev->iram_vaddr)
2501 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
2502 coda_free_aux_buf(dev, &dev->codebuf);
2503 coda_free_aux_buf(dev, &dev->tempbuf);
2504 coda_free_aux_buf(dev, &dev->workbuf);
2508 static struct platform_driver coda_driver = {
2509 .probe = coda_probe,
2510 .remove = coda_remove,
2513 .owner = THIS_MODULE,
2514 .of_match_table = of_match_ptr(coda_dt_ids),
2516 .id_table = coda_platform_ids,
2519 module_platform_driver(coda_driver);
2521 MODULE_LICENSE("GPL");
2522 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2523 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");