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/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-mem2mem.h>
31 #include <media/videobuf2-core.h>
32 #include <media/videobuf2-dma-contig.h>
36 #define CODA_NAME "coda"
38 #define CODA_MAX_INSTANCES 4
40 #define CODA_FMO_BUF_SIZE 32
41 #define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
42 #define CODA7_WORK_BUF_SIZE (512 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
43 #define CODA_PARA_BUF_SIZE (10 * 1024)
44 #define CODA_ISRAM_SIZE (2048 * 2)
46 #define CODA_OUTPUT_BUFS 4
47 #define CODA_CAPTURE_BUFS 2
51 #define CODA_MAX_FRAME_SIZE 0x90000
52 #define FMO_SLICE_SAVE_BUF_SIZE (32)
53 #define CODA_DEFAULT_GAMMA 4096
60 #define S_ALIGN 1 /* multiple of 2 */
61 #define W_ALIGN 1 /* multiple of 2 */
62 #define H_ALIGN 1 /* multiple of 2 */
64 #define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
66 static int coda_debug;
67 module_param(coda_debug, int, 0);
68 MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
92 enum coda_fmt_type type;
97 enum coda_product product;
98 struct coda_fmt *formats;
99 unsigned int num_formats;
103 /* Per-queue, driver-specific private data */
107 unsigned int sizeimage;
108 struct coda_fmt *fmt;
111 struct coda_aux_buf {
118 struct v4l2_device v4l2_dev;
119 struct video_device vfd;
120 struct platform_device *plat_dev;
121 const struct coda_devtype *devtype;
123 void __iomem *regs_base;
127 struct coda_aux_buf codebuf;
128 struct coda_aux_buf workbuf;
131 struct mutex dev_mutex;
132 struct v4l2_m2m_dev *m2m_dev;
133 struct vb2_alloc_ctx *alloc_ctx;
144 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
151 struct coda_dev *dev;
156 struct coda_q_data q_data[2];
157 enum coda_inst_type inst_type;
158 enum v4l2_colorspace colorspace;
159 struct coda_params params;
160 struct v4l2_m2m_ctx *m2m_ctx;
161 struct v4l2_ctrl_handler ctrls;
163 struct vb2_buffer *reference;
165 char vpu_header[3][64];
166 int vpu_header_size[3];
167 struct coda_aux_buf parabuf;
171 static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
173 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
174 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
175 writel(data, dev->regs_base + reg);
178 static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
181 data = readl(dev->regs_base + reg);
182 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
183 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
187 static inline unsigned long coda_isbusy(struct coda_dev *dev)
189 return coda_read(dev, CODA_REG_BIT_BUSY);
192 static inline int coda_is_initialized(struct coda_dev *dev)
194 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
197 static int coda_wait_timeout(struct coda_dev *dev)
199 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
201 while (coda_isbusy(dev)) {
202 if (time_after(jiffies, timeout))
208 static void coda_command_async(struct coda_ctx *ctx, int cmd)
210 struct coda_dev *dev = ctx->dev;
211 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
213 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
214 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
215 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
218 static int coda_command_sync(struct coda_ctx *ctx, int cmd)
220 struct coda_dev *dev = ctx->dev;
222 coda_command_async(ctx, cmd);
223 return coda_wait_timeout(dev);
226 static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
227 enum v4l2_buf_type type)
230 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
231 return &(ctx->q_data[V4L2_M2M_SRC]);
232 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
233 return &(ctx->q_data[V4L2_M2M_DST]);
241 * Add one array of supported formats for each version of Coda:
246 static struct coda_fmt codadx6_formats[] = {
248 .name = "YUV 4:2:0 Planar",
249 .fourcc = V4L2_PIX_FMT_YUV420,
250 .type = CODA_FMT_RAW,
253 .name = "H264 Encoded Stream",
254 .fourcc = V4L2_PIX_FMT_H264,
255 .type = CODA_FMT_ENC,
258 .name = "MPEG4 Encoded Stream",
259 .fourcc = V4L2_PIX_FMT_MPEG4,
260 .type = CODA_FMT_ENC,
264 static struct coda_fmt *find_format(struct coda_dev *dev, struct v4l2_format *f)
266 struct coda_fmt *formats = dev->devtype->formats;
267 int num_formats = dev->devtype->num_formats;
270 for (k = 0; k < num_formats; k++) {
271 if (formats[k].fourcc == f->fmt.pix.pixelformat)
275 if (k == num_formats)
282 * V4L2 ioctl() operations.
284 static int vidioc_querycap(struct file *file, void *priv,
285 struct v4l2_capability *cap)
287 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
288 strlcpy(cap->card, CODA_NAME, sizeof(cap->card));
289 strlcpy(cap->bus_info, CODA_NAME, sizeof(cap->bus_info));
291 * This is only a mem-to-mem video device. The capture and output
292 * device capability flags are left only for backward compatibility
293 * and are scheduled for removal.
295 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
296 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
297 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
302 static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
303 enum coda_fmt_type type)
305 struct coda_ctx *ctx = fh_to_ctx(priv);
306 struct coda_dev *dev = ctx->dev;
307 struct coda_fmt *formats = dev->devtype->formats;
308 struct coda_fmt *fmt;
309 int num_formats = dev->devtype->num_formats;
312 for (i = 0; i < num_formats; i++) {
313 if (formats[i].type == type) {
320 if (i < num_formats) {
322 strlcpy(f->description, fmt->name, sizeof(f->description));
323 f->pixelformat = fmt->fourcc;
327 /* Format not found */
331 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
332 struct v4l2_fmtdesc *f)
334 return enum_fmt(priv, f, CODA_FMT_ENC);
337 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
338 struct v4l2_fmtdesc *f)
340 return enum_fmt(priv, f, CODA_FMT_RAW);
343 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
345 struct vb2_queue *vq;
346 struct coda_q_data *q_data;
347 struct coda_ctx *ctx = fh_to_ctx(priv);
349 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
353 q_data = get_q_data(ctx, f->type);
355 f->fmt.pix.field = V4L2_FIELD_NONE;
356 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
357 f->fmt.pix.width = q_data->width;
358 f->fmt.pix.height = q_data->height;
359 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420)
360 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
361 else /* encoded formats h.264/mpeg4 */
362 f->fmt.pix.bytesperline = 0;
364 f->fmt.pix.sizeimage = q_data->sizeimage;
365 f->fmt.pix.colorspace = ctx->colorspace;
370 static int vidioc_try_fmt(struct coda_dev *dev, struct v4l2_format *f)
372 enum v4l2_field field;
374 field = f->fmt.pix.field;
375 if (field == V4L2_FIELD_ANY)
376 field = V4L2_FIELD_NONE;
377 else if (V4L2_FIELD_NONE != field)
380 /* V4L2 specification suggests the driver corrects the format struct
381 * if any of the dimensions is unsupported */
382 f->fmt.pix.field = field;
384 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420) {
385 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
386 W_ALIGN, &f->fmt.pix.height,
387 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
388 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
389 f->fmt.pix.sizeimage = f->fmt.pix.height *
390 f->fmt.pix.bytesperline;
391 } else { /*encoded formats h.264/mpeg4 */
392 f->fmt.pix.bytesperline = 0;
393 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
399 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
400 struct v4l2_format *f)
403 struct coda_fmt *fmt;
404 struct coda_ctx *ctx = fh_to_ctx(priv);
406 fmt = find_format(ctx->dev, f);
408 * Since decoding support is not implemented yet do not allow
409 * CODA_FMT_RAW formats in the capture interface.
411 if (!fmt || !(fmt->type == CODA_FMT_ENC))
412 f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
414 f->fmt.pix.colorspace = ctx->colorspace;
416 ret = vidioc_try_fmt(ctx->dev, f);
423 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
424 struct v4l2_format *f)
426 struct coda_ctx *ctx = fh_to_ctx(priv);
427 struct coda_fmt *fmt;
430 fmt = find_format(ctx->dev, f);
432 * Since decoding support is not implemented yet do not allow
433 * CODA_FMT formats in the capture interface.
435 if (!fmt || !(fmt->type == CODA_FMT_RAW))
436 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
438 if (!f->fmt.pix.colorspace)
439 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
441 ret = vidioc_try_fmt(ctx->dev, f);
448 static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
450 struct coda_q_data *q_data;
451 struct vb2_queue *vq;
454 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
458 q_data = get_q_data(ctx, f->type);
462 if (vb2_is_busy(vq)) {
463 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
467 ret = vidioc_try_fmt(ctx->dev, f);
471 q_data->fmt = find_format(ctx->dev, f);
472 q_data->width = f->fmt.pix.width;
473 q_data->height = f->fmt.pix.height;
474 if (q_data->fmt->fourcc == V4L2_PIX_FMT_YUV420) {
475 q_data->sizeimage = q_data->width * q_data->height * 3 / 2;
476 } else { /* encoded format h.264/mpeg-4 */
477 q_data->sizeimage = CODA_MAX_FRAME_SIZE;
480 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
481 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
482 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
487 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
488 struct v4l2_format *f)
492 ret = vidioc_try_fmt_vid_cap(file, priv, f);
496 return vidioc_s_fmt(fh_to_ctx(priv), f);
499 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
500 struct v4l2_format *f)
502 struct coda_ctx *ctx = fh_to_ctx(priv);
505 ret = vidioc_try_fmt_vid_out(file, priv, f);
509 ret = vidioc_s_fmt(ctx, f);
511 ctx->colorspace = f->fmt.pix.colorspace;
516 static int vidioc_reqbufs(struct file *file, void *priv,
517 struct v4l2_requestbuffers *reqbufs)
519 struct coda_ctx *ctx = fh_to_ctx(priv);
521 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
524 static int vidioc_querybuf(struct file *file, void *priv,
525 struct v4l2_buffer *buf)
527 struct coda_ctx *ctx = fh_to_ctx(priv);
529 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
532 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
534 struct coda_ctx *ctx = fh_to_ctx(priv);
536 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
539 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
541 struct coda_ctx *ctx = fh_to_ctx(priv);
543 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
546 static int vidioc_streamon(struct file *file, void *priv,
547 enum v4l2_buf_type type)
549 struct coda_ctx *ctx = fh_to_ctx(priv);
551 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
554 static int vidioc_streamoff(struct file *file, void *priv,
555 enum v4l2_buf_type type)
557 struct coda_ctx *ctx = fh_to_ctx(priv);
559 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
562 static const struct v4l2_ioctl_ops coda_ioctl_ops = {
563 .vidioc_querycap = vidioc_querycap,
565 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
566 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
567 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
568 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
570 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
571 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
572 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
573 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
575 .vidioc_reqbufs = vidioc_reqbufs,
576 .vidioc_querybuf = vidioc_querybuf,
578 .vidioc_qbuf = vidioc_qbuf,
579 .vidioc_dqbuf = vidioc_dqbuf,
581 .vidioc_streamon = vidioc_streamon,
582 .vidioc_streamoff = vidioc_streamoff,
586 * Mem-to-mem operations.
588 static void coda_device_run(void *m2m_priv)
590 struct coda_ctx *ctx = m2m_priv;
591 struct coda_q_data *q_data_src, *q_data_dst;
592 struct vb2_buffer *src_buf, *dst_buf;
593 struct coda_dev *dev = ctx->dev;
596 u32 picture_y, picture_cb, picture_cr;
597 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
600 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
601 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
602 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
603 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
604 dst_fourcc = q_data_dst->fmt->fourcc;
606 src_buf->v4l2_buf.sequence = ctx->isequence;
607 dst_buf->v4l2_buf.sequence = ctx->isequence;
611 * Workaround coda firmware BUG that only marks the first
612 * frame as IDR. This is a problem for some decoders that can't
613 * recover when a frame is lost.
615 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
616 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
617 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
619 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
620 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
624 * Copy headers at the beginning of the first frame for H.264 only.
625 * In MPEG4 they are already copied by the coda.
627 if (src_buf->v4l2_buf.sequence == 0) {
628 pic_stream_buffer_addr =
629 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
630 ctx->vpu_header_size[0] +
631 ctx->vpu_header_size[1] +
632 ctx->vpu_header_size[2];
633 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
634 ctx->vpu_header_size[0] -
635 ctx->vpu_header_size[1] -
636 ctx->vpu_header_size[2];
637 memcpy(vb2_plane_vaddr(dst_buf, 0),
638 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
639 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
640 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
641 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
642 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
643 ctx->vpu_header_size[2]);
645 pic_stream_buffer_addr =
646 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
647 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
650 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
652 switch (dst_fourcc) {
653 case V4L2_PIX_FMT_H264:
654 quant_param = ctx->params.h264_intra_qp;
656 case V4L2_PIX_FMT_MPEG4:
657 quant_param = ctx->params.mpeg4_intra_qp;
660 v4l2_warn(&ctx->dev->v4l2_dev,
661 "cannot set intra qp, fmt not supported\n");
666 switch (dst_fourcc) {
667 case V4L2_PIX_FMT_H264:
668 quant_param = ctx->params.h264_inter_qp;
670 case V4L2_PIX_FMT_MPEG4:
671 quant_param = ctx->params.mpeg4_inter_qp;
674 v4l2_warn(&ctx->dev->v4l2_dev,
675 "cannot set inter qp, fmt not supported\n");
681 coda_write(dev, 0, CODA_CMD_ENC_PIC_ROT_MODE);
682 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
685 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
686 picture_cb = picture_y + q_data_src->width * q_data_src->height;
687 picture_cr = picture_cb + q_data_src->width / 2 *
688 q_data_src->height / 2;
690 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
691 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
692 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
693 coda_write(dev, force_ipicture << 1 & 0x2,
694 CODA_CMD_ENC_PIC_OPTION);
696 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
697 coda_write(dev, pic_stream_buffer_size / 1024,
698 CODA_CMD_ENC_PIC_BB_SIZE);
699 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
702 static int coda_job_ready(void *m2m_priv)
704 struct coda_ctx *ctx = m2m_priv;
707 * For both 'P' and 'key' frame cases 1 picture
708 * and 1 frame are needed.
710 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) ||
711 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
712 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
713 "not ready: not enough video buffers.\n");
717 /* For P frames a reference picture is needed too */
718 if ((ctx->gopcounter != (ctx->params.gop_size - 1)) &&
720 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
721 "not ready: reference picture not available.\n");
725 if (coda_isbusy(ctx->dev)) {
726 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
727 "not ready: coda is still busy.\n");
731 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
736 static void coda_job_abort(void *priv)
738 struct coda_ctx *ctx = priv;
739 struct coda_dev *dev = ctx->dev;
743 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
746 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
749 static void coda_lock(void *m2m_priv)
751 struct coda_ctx *ctx = m2m_priv;
752 struct coda_dev *pcdev = ctx->dev;
753 mutex_lock(&pcdev->dev_mutex);
756 static void coda_unlock(void *m2m_priv)
758 struct coda_ctx *ctx = m2m_priv;
759 struct coda_dev *pcdev = ctx->dev;
760 mutex_unlock(&pcdev->dev_mutex);
763 static struct v4l2_m2m_ops coda_m2m_ops = {
764 .device_run = coda_device_run,
765 .job_ready = coda_job_ready,
766 .job_abort = coda_job_abort,
768 .unlock = coda_unlock,
771 static void set_default_params(struct coda_ctx *ctx)
773 struct coda_dev *dev = ctx->dev;
775 ctx->params.codec_mode = CODA_MODE_INVALID;
776 ctx->colorspace = V4L2_COLORSPACE_REC709;
777 ctx->params.framerate = 30;
778 ctx->reference = NULL;
781 /* Default formats for output and input queues */
782 ctx->q_data[V4L2_M2M_SRC].fmt = &dev->devtype->formats[0];
783 ctx->q_data[V4L2_M2M_DST].fmt = &dev->devtype->formats[1];
784 ctx->q_data[V4L2_M2M_SRC].width = MAX_W;
785 ctx->q_data[V4L2_M2M_SRC].height = MAX_H;
786 ctx->q_data[V4L2_M2M_SRC].sizeimage = (MAX_W * MAX_H * 3) / 2;
787 ctx->q_data[V4L2_M2M_DST].width = MAX_W;
788 ctx->q_data[V4L2_M2M_DST].height = MAX_H;
789 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
795 static int coda_queue_setup(struct vb2_queue *vq,
796 const struct v4l2_format *fmt,
797 unsigned int *nbuffers, unsigned int *nplanes,
798 unsigned int sizes[], void *alloc_ctxs[])
800 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
803 if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
804 *nbuffers = CODA_OUTPUT_BUFS;
806 size = fmt->fmt.pix.width *
807 fmt->fmt.pix.height * 3 / 2;
812 *nbuffers = CODA_CAPTURE_BUFS;
813 size = CODA_MAX_FRAME_SIZE;
819 alloc_ctxs[0] = ctx->dev->alloc_ctx;
821 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
822 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
827 static int coda_buf_prepare(struct vb2_buffer *vb)
829 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
830 struct coda_q_data *q_data;
832 q_data = get_q_data(ctx, vb->vb2_queue->type);
834 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
835 v4l2_warn(&ctx->dev->v4l2_dev,
836 "%s data will not fit into plane (%lu < %lu)\n",
837 __func__, vb2_plane_size(vb, 0),
838 (long)q_data->sizeimage);
842 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
847 static void coda_buf_queue(struct vb2_buffer *vb)
849 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
850 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
853 static void coda_wait_prepare(struct vb2_queue *q)
855 struct coda_ctx *ctx = vb2_get_drv_priv(q);
859 static void coda_wait_finish(struct vb2_queue *q)
861 struct coda_ctx *ctx = vb2_get_drv_priv(q);
865 static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
867 struct coda_ctx *ctx = vb2_get_drv_priv(q);
868 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
869 u32 bitstream_buf, bitstream_size;
870 struct coda_dev *dev = ctx->dev;
871 struct coda_q_data *q_data_src, *q_data_dst;
873 struct vb2_buffer *buf;
874 struct vb2_queue *src_vq;
881 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
882 ctx->rawstreamon = 1;
884 ctx->compstreamon = 1;
886 /* Don't start the coda unless both queues are on */
887 if (!(ctx->rawstreamon & ctx->compstreamon))
890 ctx->gopcounter = ctx->params.gop_size - 1;
892 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
893 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
894 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
895 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
896 bitstream_size = q_data_dst->sizeimage;
897 dst_fourcc = q_data_dst->fmt->fourcc;
899 /* Find out whether coda must encode or decode */
900 if (q_data_src->fmt->type == CODA_FMT_RAW &&
901 q_data_dst->fmt->type == CODA_FMT_ENC) {
902 ctx->inst_type = CODA_INST_ENCODER;
903 } else if (q_data_src->fmt->type == CODA_FMT_ENC &&
904 q_data_dst->fmt->type == CODA_FMT_RAW) {
905 ctx->inst_type = CODA_INST_DECODER;
906 v4l2_err(v4l2_dev, "decoding not supported.\n");
909 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
913 if (!coda_is_initialized(dev)) {
914 v4l2_err(v4l2_dev, "coda is not initialized.\n");
917 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
918 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->idx));
919 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->idx));
920 switch (dev->devtype->product) {
922 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
923 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
926 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
927 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
930 /* Configure the coda */
931 coda_write(dev, 0xffff4c00, CODA_REG_BIT_SEARCH_RAM_BASE_ADDR);
933 /* Could set rotation here if needed */
934 switch (dev->devtype->product) {
936 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
939 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
941 value |= (q_data_src->height & CODA_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
942 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
943 coda_write(dev, ctx->params.framerate,
944 CODA_CMD_ENC_SEQ_SRC_F_RATE);
946 switch (dst_fourcc) {
947 case V4L2_PIX_FMT_MPEG4:
948 if (dev->devtype->product == CODA_DX6)
949 ctx->params.codec_mode = CODADX6_MODE_ENCODE_MP4;
951 ctx->params.codec_mode = CODA7_MODE_ENCODE_MP4;
953 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
954 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
956 case V4L2_PIX_FMT_H264:
957 if (dev->devtype->product == CODA_DX6)
958 ctx->params.codec_mode = CODADX6_MODE_ENCODE_H264;
960 ctx->params.codec_mode = CODA7_MODE_ENCODE_H264;
962 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
963 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
967 "dst format (0x%08x) invalid.\n", dst_fourcc);
971 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
972 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
973 if (ctx->params.slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB)
974 value |= 1 & CODA_SLICING_MODE_MASK;
975 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
976 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
977 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
979 if (ctx->params.bitrate) {
980 /* Rate control enabled */
981 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
982 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
986 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
988 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
989 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
991 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
992 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
994 /* set default gamma */
995 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
996 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
998 value = (CODA_DEFAULT_GAMMA > 0) << CODA_OPTION_GAMMA_OFFSET;
999 value |= (0 & CODA_OPTION_SLICEREPORT_MASK) << CODA_OPTION_SLICEREPORT_OFFSET;
1000 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1002 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1003 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1004 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1005 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
1006 coda_write(dev, value, CODA_CMD_ENC_SEQ_FMO);
1009 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1010 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1014 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0)
1018 * Walk the src buffer list and let the codec know the
1019 * addresses of the pictures.
1021 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1022 for (i = 0; i < src_vq->num_buffers; i++) {
1025 buf = src_vq->bufs[i];
1026 p = ctx->parabuf.vaddr;
1028 p[i * 3] = vb2_dma_contig_plane_dma_addr(buf, 0);
1029 p[i * 3 + 1] = p[i * 3] + q_data_src->width *
1031 p[i * 3 + 2] = p[i * 3 + 1] + q_data_src->width / 2 *
1032 q_data_src->height / 2;
1035 coda_write(dev, src_vq->num_buffers, CODA_CMD_SET_FRAME_BUF_NUM);
1036 coda_write(dev, q_data_src->width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1037 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1038 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1042 /* Save stream headers */
1043 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1044 switch (dst_fourcc) {
1045 case V4L2_PIX_FMT_H264:
1047 * Get SPS in the first frame and copy it to an
1048 * intermediate buffer.
1050 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1051 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1052 coda_write(dev, CODA_HEADER_H264_SPS, CODA_CMD_ENC_HEADER_CODE);
1053 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1054 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1057 ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1058 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1059 memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0),
1060 ctx->vpu_header_size[0]);
1063 * Get PPS in the first frame and copy it to an
1064 * intermediate buffer.
1066 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1067 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1068 coda_write(dev, CODA_HEADER_H264_PPS, CODA_CMD_ENC_HEADER_CODE);
1069 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1070 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1073 ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1074 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1075 memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0),
1076 ctx->vpu_header_size[1]);
1077 ctx->vpu_header_size[2] = 0;
1079 case V4L2_PIX_FMT_MPEG4:
1081 * Get VOS in the first frame and copy it to an
1082 * intermediate buffer
1084 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1085 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1086 coda_write(dev, CODA_HEADER_MP4V_VOS, CODA_CMD_ENC_HEADER_CODE);
1087 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1088 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1091 ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1092 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1093 memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0),
1094 ctx->vpu_header_size[0]);
1096 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1097 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1098 coda_write(dev, CODA_HEADER_MP4V_VIS, CODA_CMD_ENC_HEADER_CODE);
1099 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1100 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n");
1103 ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1104 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1105 memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0),
1106 ctx->vpu_header_size[1]);
1108 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1109 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1110 coda_write(dev, CODA_HEADER_MP4V_VOL, CODA_CMD_ENC_HEADER_CODE);
1111 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1112 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n");
1115 ctx->vpu_header_size[2] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1116 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1117 memcpy(&ctx->vpu_header[2][0], vb2_plane_vaddr(buf, 0),
1118 ctx->vpu_header_size[2]);
1121 /* No more formats need to save headers at the moment */
1128 static int coda_stop_streaming(struct vb2_queue *q)
1130 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1132 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1133 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1134 "%s: output\n", __func__);
1135 ctx->rawstreamon = 0;
1137 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1138 "%s: capture\n", __func__);
1139 ctx->compstreamon = 0;
1142 if (!ctx->rawstreamon && !ctx->compstreamon) {
1143 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1144 "%s: sent command 'SEQ_END' to coda\n", __func__);
1145 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1146 v4l2_err(&ctx->dev->v4l2_dev,
1147 "CODA_COMMAND_SEQ_END failed\n");
1155 static struct vb2_ops coda_qops = {
1156 .queue_setup = coda_queue_setup,
1157 .buf_prepare = coda_buf_prepare,
1158 .buf_queue = coda_buf_queue,
1159 .wait_prepare = coda_wait_prepare,
1160 .wait_finish = coda_wait_finish,
1161 .start_streaming = coda_start_streaming,
1162 .stop_streaming = coda_stop_streaming,
1165 static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1167 struct coda_ctx *ctx =
1168 container_of(ctrl->handler, struct coda_ctx, ctrls);
1170 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1171 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1174 case V4L2_CID_MPEG_VIDEO_BITRATE:
1175 ctx->params.bitrate = ctrl->val / 1000;
1177 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1178 ctx->params.gop_size = ctrl->val;
1180 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1181 ctx->params.h264_intra_qp = ctrl->val;
1183 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1184 ctx->params.h264_inter_qp = ctrl->val;
1186 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1187 ctx->params.mpeg4_intra_qp = ctrl->val;
1189 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1190 ctx->params.mpeg4_inter_qp = ctrl->val;
1192 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1193 ctx->params.slice_mode = ctrl->val;
1195 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1196 ctx->params.slice_max_mb = ctrl->val;
1198 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1201 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1202 "Invalid control, id=%d, val=%d\n",
1203 ctrl->id, ctrl->val);
1210 static struct v4l2_ctrl_ops coda_ctrl_ops = {
1211 .s_ctrl = coda_s_ctrl,
1214 static int coda_ctrls_setup(struct coda_ctx *ctx)
1216 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1218 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1219 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1220 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1221 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1222 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1223 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1224 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1225 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1226 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1227 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1228 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1229 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1230 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1231 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1232 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB, 0,
1233 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB);
1234 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1235 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1236 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1237 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1238 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1239 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1240 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1242 if (ctx->ctrls.error) {
1243 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1248 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1251 static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1252 struct vb2_queue *dst_vq)
1254 struct coda_ctx *ctx = priv;
1257 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1258 src_vq->io_modes = VB2_MMAP;
1259 src_vq->drv_priv = ctx;
1260 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1261 src_vq->ops = &coda_qops;
1262 src_vq->mem_ops = &vb2_dma_contig_memops;
1264 ret = vb2_queue_init(src_vq);
1268 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1269 dst_vq->io_modes = VB2_MMAP;
1270 dst_vq->drv_priv = ctx;
1271 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1272 dst_vq->ops = &coda_qops;
1273 dst_vq->mem_ops = &vb2_dma_contig_memops;
1275 return vb2_queue_init(dst_vq);
1278 static int coda_open(struct file *file)
1280 struct coda_dev *dev = video_drvdata(file);
1281 struct coda_ctx *ctx = NULL;
1284 if (dev->instances >= CODA_MAX_INSTANCES)
1287 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1291 v4l2_fh_init(&ctx->fh, video_devdata(file));
1292 file->private_data = &ctx->fh;
1293 v4l2_fh_add(&ctx->fh);
1296 set_default_params(ctx);
1297 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1299 if (IS_ERR(ctx->m2m_ctx)) {
1300 int ret = PTR_ERR(ctx->m2m_ctx);
1302 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1306 ret = coda_ctrls_setup(ctx);
1308 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1312 ctx->fh.ctrl_handler = &ctx->ctrls;
1314 ctx->parabuf.vaddr = dma_alloc_coherent(&dev->plat_dev->dev,
1315 CODA_PARA_BUF_SIZE, &ctx->parabuf.paddr, GFP_KERNEL);
1316 if (!ctx->parabuf.vaddr) {
1317 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1323 ctx->idx = dev->instances++;
1326 clk_prepare_enable(dev->clk_per);
1327 clk_prepare_enable(dev->clk_ahb);
1329 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1335 v4l2_fh_del(&ctx->fh);
1336 v4l2_fh_exit(&ctx->fh);
1341 static int coda_release(struct file *file)
1343 struct coda_dev *dev = video_drvdata(file);
1344 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1346 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1353 dma_free_coherent(&dev->plat_dev->dev, CODA_PARA_BUF_SIZE,
1354 ctx->parabuf.vaddr, ctx->parabuf.paddr);
1355 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1356 v4l2_ctrl_handler_free(&ctx->ctrls);
1357 clk_disable_unprepare(dev->clk_per);
1358 clk_disable_unprepare(dev->clk_ahb);
1359 v4l2_fh_del(&ctx->fh);
1360 v4l2_fh_exit(&ctx->fh);
1366 static unsigned int coda_poll(struct file *file,
1367 struct poll_table_struct *wait)
1369 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1373 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1378 static int coda_mmap(struct file *file, struct vm_area_struct *vma)
1380 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1382 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1385 static const struct v4l2_file_operations coda_fops = {
1386 .owner = THIS_MODULE,
1388 .release = coda_release,
1390 .unlocked_ioctl = video_ioctl2,
1394 static irqreturn_t coda_irq_handler(int irq, void *data)
1396 struct vb2_buffer *src_buf, *dst_buf, *tmp_buf;
1397 struct coda_dev *dev = data;
1398 u32 wr_ptr, start_ptr;
1399 struct coda_ctx *ctx;
1401 /* read status register to attend the IRQ */
1402 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1403 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1404 CODA_REG_BIT_INT_CLEAR);
1406 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1408 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
1412 if (ctx->aborting) {
1413 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1414 "task has been aborted\n");
1418 if (coda_isbusy(ctx->dev)) {
1419 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1420 "coda is still busy!!!!\n");
1424 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1425 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1427 /* Get results from the coda */
1428 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
1429 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1430 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx));
1431 /* Calculate bytesused field */
1432 if (dst_buf->v4l2_buf.sequence == 0) {
1433 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr) +
1434 ctx->vpu_header_size[0] +
1435 ctx->vpu_header_size[1] +
1436 ctx->vpu_header_size[2];
1438 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr);
1441 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1442 wr_ptr - start_ptr);
1444 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1445 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1447 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1448 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1449 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1451 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1452 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1455 /* Free previous reference picture if available */
1456 if (ctx->reference) {
1457 v4l2_m2m_buf_done(ctx->reference, VB2_BUF_STATE_DONE);
1458 ctx->reference = NULL;
1462 * For the last frame of the gop we don't need to save
1463 * a reference picture.
1465 v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1466 tmp_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1467 if (ctx->gopcounter == 0)
1468 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1470 ctx->reference = tmp_buf;
1472 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1475 if (ctx->gopcounter < 0)
1476 ctx->gopcounter = ctx->params.gop_size - 1;
1478 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1479 "job finished: encoding frame (%d) (%s)\n",
1480 dst_buf->v4l2_buf.sequence,
1481 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1482 "KEYFRAME" : "PFRAME");
1484 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
1489 static u32 coda_supported_firmwares[] = {
1490 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
1493 static bool coda_firmware_supported(u32 vernum)
1497 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
1498 if (vernum == coda_supported_firmwares[i])
1503 static char *coda_product_name(int product)
1511 snprintf(buf, sizeof(buf), "(0x%04x)", product);
1516 static int coda_hw_init(struct coda_dev *dev)
1518 u16 product, major, minor, release;
1523 clk_prepare_enable(dev->clk_per);
1524 clk_prepare_enable(dev->clk_ahb);
1527 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
1528 * The 16-bit chars in the code buffer are in memory access
1529 * order, re-sort them to CODA order for register download.
1530 * Data in this SRAM survives a reboot.
1532 p = (u16 *)dev->codebuf.vaddr;
1533 if (dev->devtype->product == CODA_DX6) {
1534 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1535 data = CODA_DOWN_ADDRESS_SET(i) |
1536 CODA_DOWN_DATA_SET(p[i ^ 1]);
1537 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1540 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1541 data = CODA_DOWN_ADDRESS_SET(i) |
1542 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1544 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1548 /* Tell the BIT where to find everything it needs */
1549 coda_write(dev, dev->workbuf.paddr,
1550 CODA_REG_BIT_WORK_BUF_ADDR);
1551 coda_write(dev, dev->codebuf.paddr,
1552 CODA_REG_BIT_CODE_BUF_ADDR);
1553 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1555 /* Set default values */
1556 switch (dev->devtype->product) {
1558 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1561 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1563 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1564 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1565 CODA_REG_BIT_INT_ENABLE);
1567 /* Reset VPU and start processor */
1568 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1569 data |= CODA_REG_RESET_ENABLE;
1570 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1572 data &= ~CODA_REG_RESET_ENABLE;
1573 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1574 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1577 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
1578 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
1579 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
1580 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
1581 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
1582 if (coda_wait_timeout(dev)) {
1583 clk_disable_unprepare(dev->clk_per);
1584 clk_disable_unprepare(dev->clk_ahb);
1585 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
1589 /* Check we are compatible with the loaded firmware */
1590 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
1591 product = CODA_FIRMWARE_PRODUCT(data);
1592 major = CODA_FIRMWARE_MAJOR(data);
1593 minor = CODA_FIRMWARE_MINOR(data);
1594 release = CODA_FIRMWARE_RELEASE(data);
1596 clk_disable_unprepare(dev->clk_per);
1597 clk_disable_unprepare(dev->clk_ahb);
1599 if (product != dev->devtype->product) {
1600 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
1601 " Version: %u.%u.%u\n",
1602 coda_product_name(dev->devtype->product),
1603 coda_product_name(product), major, minor, release);
1607 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
1608 coda_product_name(product));
1610 if (coda_firmware_supported(data)) {
1611 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
1612 major, minor, release);
1614 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
1615 "%u.%u.%u\n", major, minor, release);
1621 static void coda_fw_callback(const struct firmware *fw, void *context)
1623 struct coda_dev *dev = context;
1624 struct platform_device *pdev = dev->plat_dev;
1628 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1632 /* allocate auxiliary per-device code buffer for the BIT processor */
1633 dev->codebuf.size = fw->size;
1634 dev->codebuf.vaddr = dma_alloc_coherent(&pdev->dev, fw->size,
1635 &dev->codebuf.paddr,
1637 if (!dev->codebuf.vaddr) {
1638 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1642 /* Copy the whole firmware image to the code buffer */
1643 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1644 release_firmware(fw);
1646 ret = coda_hw_init(dev);
1648 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1652 dev->vfd.fops = &coda_fops,
1653 dev->vfd.ioctl_ops = &coda_ioctl_ops;
1654 dev->vfd.release = video_device_release_empty,
1655 dev->vfd.lock = &dev->dev_mutex;
1656 dev->vfd.v4l2_dev = &dev->v4l2_dev;
1657 dev->vfd.vfl_dir = VFL_DIR_M2M;
1658 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
1659 video_set_drvdata(&dev->vfd, dev);
1661 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1662 if (IS_ERR(dev->alloc_ctx)) {
1663 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1667 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1668 if (IS_ERR(dev->m2m_dev)) {
1669 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1673 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
1675 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1678 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
1684 v4l2_m2m_release(dev->m2m_dev);
1686 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1689 static int coda_firmware_request(struct coda_dev *dev)
1691 char *fw = dev->devtype->firmware;
1693 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1694 coda_product_name(dev->devtype->product));
1696 return request_firmware_nowait(THIS_MODULE, true,
1697 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1700 enum coda_platform {
1704 static const struct coda_devtype coda_devdata[] = {
1706 .firmware = "v4l-codadx6-imx27.bin",
1707 .product = CODA_DX6,
1708 .formats = codadx6_formats,
1709 .num_formats = ARRAY_SIZE(codadx6_formats),
1713 static struct platform_device_id coda_platform_ids[] = {
1714 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
1717 MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1720 static const struct of_device_id coda_dt_ids[] = {
1721 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
1724 MODULE_DEVICE_TABLE(of, coda_dt_ids);
1727 static int __devinit coda_probe(struct platform_device *pdev)
1729 const struct of_device_id *of_id =
1730 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1731 const struct platform_device_id *pdev_id;
1732 struct coda_dev *dev;
1733 struct resource *res;
1736 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1738 dev_err(&pdev->dev, "Not enough memory for %s\n",
1743 spin_lock_init(&dev->irqlock);
1745 dev->plat_dev = pdev;
1746 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1747 if (IS_ERR(dev->clk_per)) {
1748 dev_err(&pdev->dev, "Could not get per clock\n");
1749 return PTR_ERR(dev->clk_per);
1752 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1753 if (IS_ERR(dev->clk_ahb)) {
1754 dev_err(&pdev->dev, "Could not get ahb clock\n");
1755 return PTR_ERR(dev->clk_ahb);
1758 /* Get memory for physical registers */
1759 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1761 dev_err(&pdev->dev, "failed to get memory region resource\n");
1765 if (devm_request_mem_region(&pdev->dev, res->start,
1766 resource_size(res), CODA_NAME) == NULL) {
1767 dev_err(&pdev->dev, "failed to request memory region\n");
1770 dev->regs_base = devm_ioremap(&pdev->dev, res->start,
1771 resource_size(res));
1772 if (!dev->regs_base) {
1773 dev_err(&pdev->dev, "failed to ioremap address region\n");
1778 irq = platform_get_irq(pdev, 0);
1780 dev_err(&pdev->dev, "failed to get irq resource\n");
1784 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
1785 0, CODA_NAME, dev) < 0) {
1786 dev_err(&pdev->dev, "failed to request irq\n");
1790 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1794 mutex_init(&dev->dev_mutex);
1796 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
1799 dev->devtype = of_id->data;
1800 } else if (pdev_id) {
1801 dev->devtype = &coda_devdata[pdev_id->driver_data];
1803 v4l2_device_unregister(&dev->v4l2_dev);
1807 /* allocate auxiliary per-device buffers for the BIT processor */
1808 switch (dev->devtype->product) {
1810 dev->workbuf.size = CODADX6_WORK_BUF_SIZE;
1813 dev->workbuf.size = CODA7_WORK_BUF_SIZE;
1815 dev->workbuf.vaddr = dma_alloc_coherent(&pdev->dev, dev->workbuf.size,
1816 &dev->workbuf.paddr,
1818 if (!dev->workbuf.vaddr) {
1819 dev_err(&pdev->dev, "failed to allocate work buffer\n");
1820 v4l2_device_unregister(&dev->v4l2_dev);
1824 platform_set_drvdata(pdev, dev);
1826 return coda_firmware_request(dev);
1829 static int coda_remove(struct platform_device *pdev)
1831 struct coda_dev *dev = platform_get_drvdata(pdev);
1833 video_unregister_device(&dev->vfd);
1835 v4l2_m2m_release(dev->m2m_dev);
1837 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1838 v4l2_device_unregister(&dev->v4l2_dev);
1839 if (dev->codebuf.vaddr)
1840 dma_free_coherent(&pdev->dev, dev->codebuf.size,
1841 &dev->codebuf.vaddr, dev->codebuf.paddr);
1842 if (dev->workbuf.vaddr)
1843 dma_free_coherent(&pdev->dev, dev->workbuf.size, &dev->workbuf.vaddr,
1844 dev->workbuf.paddr);
1848 static struct platform_driver coda_driver = {
1849 .probe = coda_probe,
1850 .remove = __devexit_p(coda_remove),
1853 .owner = THIS_MODULE,
1854 .of_match_table = of_match_ptr(coda_dt_ids),
1856 .id_table = coda_platform_ids,
1859 module_platform_driver(coda_driver);
1861 MODULE_LICENSE("GPL");
1862 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
1863 MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");