blob: a560c0da68ae89742f351a06b6b27b553e2363ae [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
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.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/firmware.h>
17#include <linux/interrupt.h>
18#include <linux/io.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>
25#include <linux/of.h>
26
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>
33
34#include "coda.h"
35
36#define CODA_NAME "coda"
37
38#define CODA_MAX_INSTANCES 4
39
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)
45
46#define CODA_OUTPUT_BUFS 4
47#define CODA_CAPTURE_BUFS 2
48
49#define MAX_W 720
50#define MAX_H 576
51#define CODA_MAX_FRAME_SIZE 0x90000
52#define FMO_SLICE_SAVE_BUF_SIZE (32)
53#define CODA_DEFAULT_GAMMA 4096
54
55#define MIN_W 176
56#define MIN_H 144
57#define MAX_W 720
58#define MAX_H 576
59
60#define S_ALIGN 1 /* multiple of 2 */
61#define W_ALIGN 1 /* multiple of 2 */
62#define H_ALIGN 1 /* multiple of 2 */
63
64#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
65
66static int coda_debug;
67module_param(coda_debug, int, 0);
68MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
69
70enum {
71 V4L2_M2M_SRC = 0,
72 V4L2_M2M_DST = 1,
73};
74
75enum coda_fmt_type {
76 CODA_FMT_ENC,
77 CODA_FMT_RAW,
78};
79
80enum coda_inst_type {
81 CODA_INST_ENCODER,
82 CODA_INST_DECODER,
83};
84
85enum coda_product {
86 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030087 CODA_7541 = 0xf012,
Javier Martin186b2502012-07-26 05:53:35 -030088};
89
90struct coda_fmt {
91 char *name;
92 u32 fourcc;
93 enum coda_fmt_type type;
94};
95
96struct coda_devtype {
97 char *firmware;
98 enum coda_product product;
99 struct coda_fmt *formats;
100 unsigned int num_formats;
101 size_t workbuf_size;
102};
103
104/* Per-queue, driver-specific private data */
105struct coda_q_data {
106 unsigned int width;
107 unsigned int height;
108 unsigned int sizeimage;
109 struct coda_fmt *fmt;
110};
111
112struct coda_aux_buf {
113 void *vaddr;
114 dma_addr_t paddr;
115 u32 size;
116};
117
118struct coda_dev {
119 struct v4l2_device v4l2_dev;
120 struct video_device vfd;
121 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300122 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300123
124 void __iomem *regs_base;
125 struct clk *clk_per;
126 struct clk *clk_ahb;
127
128 struct coda_aux_buf codebuf;
129 struct coda_aux_buf workbuf;
130
131 spinlock_t irqlock;
132 struct mutex dev_mutex;
133 struct v4l2_m2m_dev *m2m_dev;
134 struct vb2_alloc_ctx *alloc_ctx;
135 int instances;
136};
137
138struct coda_params {
139 u8 h264_intra_qp;
140 u8 h264_inter_qp;
141 u8 mpeg4_intra_qp;
142 u8 mpeg4_inter_qp;
143 u8 gop_size;
144 int codec_mode;
145 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
146 u32 framerate;
147 u16 bitrate;
148 u32 slice_max_mb;
149};
150
151struct coda_ctx {
152 struct coda_dev *dev;
153 int aborting;
154 int rawstreamon;
155 int compstreamon;
156 u32 isequence;
157 struct coda_q_data q_data[2];
158 enum coda_inst_type inst_type;
159 enum v4l2_colorspace colorspace;
160 struct coda_params params;
161 struct v4l2_m2m_ctx *m2m_ctx;
162 struct v4l2_ctrl_handler ctrls;
163 struct v4l2_fh fh;
164 struct vb2_buffer *reference;
165 int gopcounter;
166 char vpu_header[3][64];
167 int vpu_header_size[3];
168 struct coda_aux_buf parabuf;
169 int idx;
170};
171
172static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
173{
174 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
175 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
176 writel(data, dev->regs_base + reg);
177}
178
179static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
180{
181 u32 data;
182 data = readl(dev->regs_base + reg);
183 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
184 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
185 return data;
186}
187
188static inline unsigned long coda_isbusy(struct coda_dev *dev)
189{
190 return coda_read(dev, CODA_REG_BIT_BUSY);
191}
192
193static inline int coda_is_initialized(struct coda_dev *dev)
194{
195 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
196}
197
198static int coda_wait_timeout(struct coda_dev *dev)
199{
200 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
201
202 while (coda_isbusy(dev)) {
203 if (time_after(jiffies, timeout))
204 return -ETIMEDOUT;
205 }
206 return 0;
207}
208
209static void coda_command_async(struct coda_ctx *ctx, int cmd)
210{
211 struct coda_dev *dev = ctx->dev;
212 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
213
214 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
215 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
216 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
217}
218
219static int coda_command_sync(struct coda_ctx *ctx, int cmd)
220{
221 struct coda_dev *dev = ctx->dev;
222
223 coda_command_async(ctx, cmd);
224 return coda_wait_timeout(dev);
225}
226
227static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
228 enum v4l2_buf_type type)
229{
230 switch (type) {
231 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
232 return &(ctx->q_data[V4L2_M2M_SRC]);
233 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
234 return &(ctx->q_data[V4L2_M2M_DST]);
235 default:
236 BUG();
237 }
238 return NULL;
239}
240
241/*
242 * Add one array of supported formats for each version of Coda:
243 * i.MX27 -> codadx6
244 * i.MX51 -> coda7
245 * i.MX6 -> coda960
246 */
247static struct coda_fmt codadx6_formats[] = {
248 {
249 .name = "YUV 4:2:0 Planar",
250 .fourcc = V4L2_PIX_FMT_YUV420,
251 .type = CODA_FMT_RAW,
252 },
253 {
254 .name = "H264 Encoded Stream",
255 .fourcc = V4L2_PIX_FMT_H264,
256 .type = CODA_FMT_ENC,
257 },
258 {
259 .name = "MPEG4 Encoded Stream",
260 .fourcc = V4L2_PIX_FMT_MPEG4,
261 .type = CODA_FMT_ENC,
262 },
263};
264
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300265static struct coda_fmt coda7_formats[] = {
266 {
267 .name = "YUV 4:2:0 Planar",
268 .fourcc = V4L2_PIX_FMT_YUV420,
269 .type = CODA_FMT_RAW,
270 },
271 {
272 .name = "H264 Encoded Stream",
273 .fourcc = V4L2_PIX_FMT_H264,
274 .type = CODA_FMT_ENC,
275 },
276 {
277 .name = "MPEG4 Encoded Stream",
278 .fourcc = V4L2_PIX_FMT_MPEG4,
279 .type = CODA_FMT_ENC,
280 },
281};
282
Javier Martin186b2502012-07-26 05:53:35 -0300283static struct coda_fmt *find_format(struct coda_dev *dev, struct v4l2_format *f)
284{
285 struct coda_fmt *formats = dev->devtype->formats;
286 int num_formats = dev->devtype->num_formats;
287 unsigned int k;
288
289 for (k = 0; k < num_formats; k++) {
290 if (formats[k].fourcc == f->fmt.pix.pixelformat)
291 break;
292 }
293
294 if (k == num_formats)
295 return NULL;
296
297 return &formats[k];
298}
299
300/*
301 * V4L2 ioctl() operations.
302 */
303static int vidioc_querycap(struct file *file, void *priv,
304 struct v4l2_capability *cap)
305{
306 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
307 strlcpy(cap->card, CODA_NAME, sizeof(cap->card));
308 strlcpy(cap->bus_info, CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300309 /*
310 * This is only a mem-to-mem video device. The capture and output
311 * device capability flags are left only for backward compatibility
312 * and are scheduled for removal.
313 */
314 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
315 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300316 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
317
318 return 0;
319}
320
321static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
322 enum coda_fmt_type type)
323{
324 struct coda_ctx *ctx = fh_to_ctx(priv);
325 struct coda_dev *dev = ctx->dev;
326 struct coda_fmt *formats = dev->devtype->formats;
327 struct coda_fmt *fmt;
328 int num_formats = dev->devtype->num_formats;
329 int i, num = 0;
330
331 for (i = 0; i < num_formats; i++) {
332 if (formats[i].type == type) {
333 if (num == f->index)
334 break;
335 ++num;
336 }
337 }
338
339 if (i < num_formats) {
340 fmt = &formats[i];
341 strlcpy(f->description, fmt->name, sizeof(f->description));
342 f->pixelformat = fmt->fourcc;
343 return 0;
344 }
345
346 /* Format not found */
347 return -EINVAL;
348}
349
350static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
351 struct v4l2_fmtdesc *f)
352{
353 return enum_fmt(priv, f, CODA_FMT_ENC);
354}
355
356static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
357 struct v4l2_fmtdesc *f)
358{
359 return enum_fmt(priv, f, CODA_FMT_RAW);
360}
361
362static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
363{
364 struct vb2_queue *vq;
365 struct coda_q_data *q_data;
366 struct coda_ctx *ctx = fh_to_ctx(priv);
367
368 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
369 if (!vq)
370 return -EINVAL;
371
372 q_data = get_q_data(ctx, f->type);
373
374 f->fmt.pix.field = V4L2_FIELD_NONE;
375 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
376 f->fmt.pix.width = q_data->width;
377 f->fmt.pix.height = q_data->height;
378 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420)
379 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
380 else /* encoded formats h.264/mpeg4 */
381 f->fmt.pix.bytesperline = 0;
382
383 f->fmt.pix.sizeimage = q_data->sizeimage;
384 f->fmt.pix.colorspace = ctx->colorspace;
385
386 return 0;
387}
388
389static int vidioc_try_fmt(struct coda_dev *dev, struct v4l2_format *f)
390{
391 enum v4l2_field field;
392
393 field = f->fmt.pix.field;
394 if (field == V4L2_FIELD_ANY)
395 field = V4L2_FIELD_NONE;
396 else if (V4L2_FIELD_NONE != field)
397 return -EINVAL;
398
399 /* V4L2 specification suggests the driver corrects the format struct
400 * if any of the dimensions is unsupported */
401 f->fmt.pix.field = field;
402
403 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420) {
404 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
405 W_ALIGN, &f->fmt.pix.height,
406 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
407 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
408 f->fmt.pix.sizeimage = f->fmt.pix.height *
409 f->fmt.pix.bytesperline;
410 } else { /*encoded formats h.264/mpeg4 */
411 f->fmt.pix.bytesperline = 0;
412 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
413 }
414
415 return 0;
416}
417
418static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
419 struct v4l2_format *f)
420{
421 int ret;
422 struct coda_fmt *fmt;
423 struct coda_ctx *ctx = fh_to_ctx(priv);
424
425 fmt = find_format(ctx->dev, f);
426 /*
427 * Since decoding support is not implemented yet do not allow
428 * CODA_FMT_RAW formats in the capture interface.
429 */
430 if (!fmt || !(fmt->type == CODA_FMT_ENC))
431 f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
432
433 f->fmt.pix.colorspace = ctx->colorspace;
434
435 ret = vidioc_try_fmt(ctx->dev, f);
436 if (ret < 0)
437 return ret;
438
439 return 0;
440}
441
442static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
443 struct v4l2_format *f)
444{
445 struct coda_ctx *ctx = fh_to_ctx(priv);
446 struct coda_fmt *fmt;
447 int ret;
448
449 fmt = find_format(ctx->dev, f);
450 /*
451 * Since decoding support is not implemented yet do not allow
452 * CODA_FMT formats in the capture interface.
453 */
454 if (!fmt || !(fmt->type == CODA_FMT_RAW))
455 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
456
457 if (!f->fmt.pix.colorspace)
458 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
459
460 ret = vidioc_try_fmt(ctx->dev, f);
461 if (ret < 0)
462 return ret;
463
464 return 0;
465}
466
467static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
468{
469 struct coda_q_data *q_data;
470 struct vb2_queue *vq;
471 int ret;
472
473 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
474 if (!vq)
475 return -EINVAL;
476
477 q_data = get_q_data(ctx, f->type);
478 if (!q_data)
479 return -EINVAL;
480
481 if (vb2_is_busy(vq)) {
482 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
483 return -EBUSY;
484 }
485
486 ret = vidioc_try_fmt(ctx->dev, f);
487 if (ret)
488 return ret;
489
490 q_data->fmt = find_format(ctx->dev, f);
491 q_data->width = f->fmt.pix.width;
492 q_data->height = f->fmt.pix.height;
493 if (q_data->fmt->fourcc == V4L2_PIX_FMT_YUV420) {
494 q_data->sizeimage = q_data->width * q_data->height * 3 / 2;
495 } else { /* encoded format h.264/mpeg-4 */
496 q_data->sizeimage = CODA_MAX_FRAME_SIZE;
497 }
498
499 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
500 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
501 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
502
503 return 0;
504}
505
506static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
507 struct v4l2_format *f)
508{
509 int ret;
510
511 ret = vidioc_try_fmt_vid_cap(file, priv, f);
512 if (ret)
513 return ret;
514
515 return vidioc_s_fmt(fh_to_ctx(priv), f);
516}
517
518static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
519 struct v4l2_format *f)
520{
521 struct coda_ctx *ctx = fh_to_ctx(priv);
522 int ret;
523
524 ret = vidioc_try_fmt_vid_out(file, priv, f);
525 if (ret)
526 return ret;
527
Richard Zhao8d621472012-08-21 02:47:42 -0300528 ret = vidioc_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300529 if (ret)
530 ctx->colorspace = f->fmt.pix.colorspace;
531
532 return ret;
533}
534
535static int vidioc_reqbufs(struct file *file, void *priv,
536 struct v4l2_requestbuffers *reqbufs)
537{
538 struct coda_ctx *ctx = fh_to_ctx(priv);
539
540 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
541}
542
543static int vidioc_querybuf(struct file *file, void *priv,
544 struct v4l2_buffer *buf)
545{
546 struct coda_ctx *ctx = fh_to_ctx(priv);
547
548 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
549}
550
551static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
552{
553 struct coda_ctx *ctx = fh_to_ctx(priv);
554
555 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
556}
557
558static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
559{
560 struct coda_ctx *ctx = fh_to_ctx(priv);
561
562 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
563}
564
565static int vidioc_streamon(struct file *file, void *priv,
566 enum v4l2_buf_type type)
567{
568 struct coda_ctx *ctx = fh_to_ctx(priv);
569
570 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
571}
572
573static int vidioc_streamoff(struct file *file, void *priv,
574 enum v4l2_buf_type type)
575{
576 struct coda_ctx *ctx = fh_to_ctx(priv);
577
578 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
579}
580
581static const struct v4l2_ioctl_ops coda_ioctl_ops = {
582 .vidioc_querycap = vidioc_querycap,
583
584 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
585 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
586 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
587 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
588
589 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
590 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
591 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
592 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
593
594 .vidioc_reqbufs = vidioc_reqbufs,
595 .vidioc_querybuf = vidioc_querybuf,
596
597 .vidioc_qbuf = vidioc_qbuf,
598 .vidioc_dqbuf = vidioc_dqbuf,
599
600 .vidioc_streamon = vidioc_streamon,
601 .vidioc_streamoff = vidioc_streamoff,
602};
603
604/*
605 * Mem-to-mem operations.
606 */
607static void coda_device_run(void *m2m_priv)
608{
609 struct coda_ctx *ctx = m2m_priv;
610 struct coda_q_data *q_data_src, *q_data_dst;
611 struct vb2_buffer *src_buf, *dst_buf;
612 struct coda_dev *dev = ctx->dev;
613 int force_ipicture;
614 int quant_param = 0;
615 u32 picture_y, picture_cb, picture_cr;
616 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
617 u32 dst_fourcc;
618
619 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
620 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
621 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
622 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
623 dst_fourcc = q_data_dst->fmt->fourcc;
624
625 src_buf->v4l2_buf.sequence = ctx->isequence;
626 dst_buf->v4l2_buf.sequence = ctx->isequence;
627 ctx->isequence++;
628
629 /*
630 * Workaround coda firmware BUG that only marks the first
631 * frame as IDR. This is a problem for some decoders that can't
632 * recover when a frame is lost.
633 */
634 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
635 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
636 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
637 } else {
638 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
639 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
640 }
641
642 /*
643 * Copy headers at the beginning of the first frame for H.264 only.
644 * In MPEG4 they are already copied by the coda.
645 */
646 if (src_buf->v4l2_buf.sequence == 0) {
647 pic_stream_buffer_addr =
648 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
649 ctx->vpu_header_size[0] +
650 ctx->vpu_header_size[1] +
651 ctx->vpu_header_size[2];
652 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
653 ctx->vpu_header_size[0] -
654 ctx->vpu_header_size[1] -
655 ctx->vpu_header_size[2];
656 memcpy(vb2_plane_vaddr(dst_buf, 0),
657 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
658 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
659 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
660 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
661 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
662 ctx->vpu_header_size[2]);
663 } else {
664 pic_stream_buffer_addr =
665 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
666 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
667 }
668
669 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
670 force_ipicture = 1;
671 switch (dst_fourcc) {
672 case V4L2_PIX_FMT_H264:
673 quant_param = ctx->params.h264_intra_qp;
674 break;
675 case V4L2_PIX_FMT_MPEG4:
676 quant_param = ctx->params.mpeg4_intra_qp;
677 break;
678 default:
679 v4l2_warn(&ctx->dev->v4l2_dev,
680 "cannot set intra qp, fmt not supported\n");
681 break;
682 }
683 } else {
684 force_ipicture = 0;
685 switch (dst_fourcc) {
686 case V4L2_PIX_FMT_H264:
687 quant_param = ctx->params.h264_inter_qp;
688 break;
689 case V4L2_PIX_FMT_MPEG4:
690 quant_param = ctx->params.mpeg4_inter_qp;
691 break;
692 default:
693 v4l2_warn(&ctx->dev->v4l2_dev,
694 "cannot set inter qp, fmt not supported\n");
695 break;
696 }
697 }
698
699 /* submit */
700 coda_write(dev, 0, CODA_CMD_ENC_PIC_ROT_MODE);
701 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
702
703
704 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
705 picture_cb = picture_y + q_data_src->width * q_data_src->height;
706 picture_cr = picture_cb + q_data_src->width / 2 *
707 q_data_src->height / 2;
708
709 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
710 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
711 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
712 coda_write(dev, force_ipicture << 1 & 0x2,
713 CODA_CMD_ENC_PIC_OPTION);
714
715 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
716 coda_write(dev, pic_stream_buffer_size / 1024,
717 CODA_CMD_ENC_PIC_BB_SIZE);
718 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
719}
720
721static int coda_job_ready(void *m2m_priv)
722{
723 struct coda_ctx *ctx = m2m_priv;
724
725 /*
726 * For both 'P' and 'key' frame cases 1 picture
727 * and 1 frame are needed.
728 */
729 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) ||
730 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
731 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
732 "not ready: not enough video buffers.\n");
733 return 0;
734 }
735
736 /* For P frames a reference picture is needed too */
737 if ((ctx->gopcounter != (ctx->params.gop_size - 1)) &&
738 !ctx->reference) {
739 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
740 "not ready: reference picture not available.\n");
741 return 0;
742 }
743
744 if (coda_isbusy(ctx->dev)) {
745 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
746 "not ready: coda is still busy.\n");
747 return 0;
748 }
749
750 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
751 "job ready\n");
752 return 1;
753}
754
755static void coda_job_abort(void *priv)
756{
757 struct coda_ctx *ctx = priv;
758 struct coda_dev *dev = ctx->dev;
759
760 ctx->aborting = 1;
761
762 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
763 "Aborting task\n");
764
765 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
766}
767
768static void coda_lock(void *m2m_priv)
769{
770 struct coda_ctx *ctx = m2m_priv;
771 struct coda_dev *pcdev = ctx->dev;
772 mutex_lock(&pcdev->dev_mutex);
773}
774
775static void coda_unlock(void *m2m_priv)
776{
777 struct coda_ctx *ctx = m2m_priv;
778 struct coda_dev *pcdev = ctx->dev;
779 mutex_unlock(&pcdev->dev_mutex);
780}
781
782static struct v4l2_m2m_ops coda_m2m_ops = {
783 .device_run = coda_device_run,
784 .job_ready = coda_job_ready,
785 .job_abort = coda_job_abort,
786 .lock = coda_lock,
787 .unlock = coda_unlock,
788};
789
790static void set_default_params(struct coda_ctx *ctx)
791{
792 struct coda_dev *dev = ctx->dev;
793
794 ctx->params.codec_mode = CODA_MODE_INVALID;
795 ctx->colorspace = V4L2_COLORSPACE_REC709;
796 ctx->params.framerate = 30;
797 ctx->reference = NULL;
798 ctx->aborting = 0;
799
800 /* Default formats for output and input queues */
801 ctx->q_data[V4L2_M2M_SRC].fmt = &dev->devtype->formats[0];
802 ctx->q_data[V4L2_M2M_DST].fmt = &dev->devtype->formats[1];
803 ctx->q_data[V4L2_M2M_SRC].width = MAX_W;
804 ctx->q_data[V4L2_M2M_SRC].height = MAX_H;
805 ctx->q_data[V4L2_M2M_SRC].sizeimage = (MAX_W * MAX_H * 3) / 2;
806 ctx->q_data[V4L2_M2M_DST].width = MAX_W;
807 ctx->q_data[V4L2_M2M_DST].height = MAX_H;
808 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
809}
810
811/*
812 * Queue operations
813 */
814static int coda_queue_setup(struct vb2_queue *vq,
815 const struct v4l2_format *fmt,
816 unsigned int *nbuffers, unsigned int *nplanes,
817 unsigned int sizes[], void *alloc_ctxs[])
818{
819 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
820 unsigned int size;
821
822 if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
823 *nbuffers = CODA_OUTPUT_BUFS;
824 if (fmt)
825 size = fmt->fmt.pix.width *
826 fmt->fmt.pix.height * 3 / 2;
827 else
828 size = MAX_W *
829 MAX_H * 3 / 2;
830 } else {
831 *nbuffers = CODA_CAPTURE_BUFS;
832 size = CODA_MAX_FRAME_SIZE;
833 }
834
835 *nplanes = 1;
836 sizes[0] = size;
837
838 alloc_ctxs[0] = ctx->dev->alloc_ctx;
839
840 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
841 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
842
843 return 0;
844}
845
846static int coda_buf_prepare(struct vb2_buffer *vb)
847{
848 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
849 struct coda_q_data *q_data;
850
851 q_data = get_q_data(ctx, vb->vb2_queue->type);
852
853 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
854 v4l2_warn(&ctx->dev->v4l2_dev,
855 "%s data will not fit into plane (%lu < %lu)\n",
856 __func__, vb2_plane_size(vb, 0),
857 (long)q_data->sizeimage);
858 return -EINVAL;
859 }
860
861 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
862
863 return 0;
864}
865
866static void coda_buf_queue(struct vb2_buffer *vb)
867{
868 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
869 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
870}
871
872static void coda_wait_prepare(struct vb2_queue *q)
873{
874 struct coda_ctx *ctx = vb2_get_drv_priv(q);
875 coda_unlock(ctx);
876}
877
878static void coda_wait_finish(struct vb2_queue *q)
879{
880 struct coda_ctx *ctx = vb2_get_drv_priv(q);
881 coda_lock(ctx);
882}
883
884static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
885{
886 struct coda_ctx *ctx = vb2_get_drv_priv(q);
887 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
888 u32 bitstream_buf, bitstream_size;
889 struct coda_dev *dev = ctx->dev;
890 struct coda_q_data *q_data_src, *q_data_dst;
891 u32 dst_fourcc;
892 struct vb2_buffer *buf;
893 struct vb2_queue *src_vq;
894 u32 value;
895 int i = 0;
896
897 if (count < 1)
898 return -EINVAL;
899
900 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
901 ctx->rawstreamon = 1;
902 else
903 ctx->compstreamon = 1;
904
905 /* Don't start the coda unless both queues are on */
906 if (!(ctx->rawstreamon & ctx->compstreamon))
907 return 0;
908
909 ctx->gopcounter = ctx->params.gop_size - 1;
910
911 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
912 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
913 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
914 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
915 bitstream_size = q_data_dst->sizeimage;
916 dst_fourcc = q_data_dst->fmt->fourcc;
917
918 /* Find out whether coda must encode or decode */
919 if (q_data_src->fmt->type == CODA_FMT_RAW &&
920 q_data_dst->fmt->type == CODA_FMT_ENC) {
921 ctx->inst_type = CODA_INST_ENCODER;
922 } else if (q_data_src->fmt->type == CODA_FMT_ENC &&
923 q_data_dst->fmt->type == CODA_FMT_RAW) {
924 ctx->inst_type = CODA_INST_DECODER;
925 v4l2_err(v4l2_dev, "decoding not supported.\n");
926 return -EINVAL;
927 } else {
928 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
929 return -EINVAL;
930 }
931
932 if (!coda_is_initialized(dev)) {
933 v4l2_err(v4l2_dev, "coda is not initialized.\n");
934 return -EFAULT;
935 }
936 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
937 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->idx));
938 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->idx));
939 switch (dev->devtype->product) {
940 case CODA_DX6:
941 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
942 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
943 break;
944 default:
945 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
946 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
947 }
948
949 /* Configure the coda */
950 coda_write(dev, 0xffff4c00, CODA_REG_BIT_SEARCH_RAM_BASE_ADDR);
951
952 /* Could set rotation here if needed */
953 switch (dev->devtype->product) {
954 case CODA_DX6:
955 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
956 break;
957 default:
958 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
959 }
960 value |= (q_data_src->height & CODA_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
961 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
962 coda_write(dev, ctx->params.framerate,
963 CODA_CMD_ENC_SEQ_SRC_F_RATE);
964
965 switch (dst_fourcc) {
966 case V4L2_PIX_FMT_MPEG4:
967 if (dev->devtype->product == CODA_DX6)
968 ctx->params.codec_mode = CODADX6_MODE_ENCODE_MP4;
969 else
970 ctx->params.codec_mode = CODA7_MODE_ENCODE_MP4;
971
972 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
973 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
974 break;
975 case V4L2_PIX_FMT_H264:
976 if (dev->devtype->product == CODA_DX6)
977 ctx->params.codec_mode = CODADX6_MODE_ENCODE_H264;
978 else
979 ctx->params.codec_mode = CODA7_MODE_ENCODE_H264;
980
981 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
982 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
983 break;
984 default:
985 v4l2_err(v4l2_dev,
986 "dst format (0x%08x) invalid.\n", dst_fourcc);
987 return -EINVAL;
988 }
989
990 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
991 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
992 if (ctx->params.slice_mode == V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB)
993 value |= 1 & CODA_SLICING_MODE_MASK;
994 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
995 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
996 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
997
998 if (ctx->params.bitrate) {
999 /* Rate control enabled */
1000 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1001 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1002 } else {
1003 value = 0;
1004 }
1005 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1006
1007 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1008 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1009
1010 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1011 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1012
1013 /* set default gamma */
1014 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1015 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1016
1017 value = (CODA_DEFAULT_GAMMA > 0) << CODA_OPTION_GAMMA_OFFSET;
1018 value |= (0 & CODA_OPTION_SLICEREPORT_MASK) << CODA_OPTION_SLICEREPORT_OFFSET;
1019 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1020
1021 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1022 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1023 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1024 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
1025 coda_write(dev, value, CODA_CMD_ENC_SEQ_FMO);
1026 }
1027
1028 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1029 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1030 return -ETIMEDOUT;
1031 }
1032
1033 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0)
1034 return -EFAULT;
1035
1036 /*
1037 * Walk the src buffer list and let the codec know the
1038 * addresses of the pictures.
1039 */
1040 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1041 for (i = 0; i < src_vq->num_buffers; i++) {
1042 u32 *p;
1043
1044 buf = src_vq->bufs[i];
1045 p = ctx->parabuf.vaddr;
1046
1047 p[i * 3] = vb2_dma_contig_plane_dma_addr(buf, 0);
1048 p[i * 3 + 1] = p[i * 3] + q_data_src->width *
1049 q_data_src->height;
1050 p[i * 3 + 2] = p[i * 3 + 1] + q_data_src->width / 2 *
1051 q_data_src->height / 2;
1052 }
1053
1054 coda_write(dev, src_vq->num_buffers, CODA_CMD_SET_FRAME_BUF_NUM);
1055 coda_write(dev, q_data_src->width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1056 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1057 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1058 return -ETIMEDOUT;
1059 }
1060
1061 /* Save stream headers */
1062 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1063 switch (dst_fourcc) {
1064 case V4L2_PIX_FMT_H264:
1065 /*
1066 * Get SPS in the first frame and copy it to an
1067 * intermediate buffer.
1068 */
1069 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1070 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1071 coda_write(dev, CODA_HEADER_H264_SPS, CODA_CMD_ENC_HEADER_CODE);
1072 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1073 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1074 return -ETIMEDOUT;
1075 }
1076 ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1077 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1078 memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0),
1079 ctx->vpu_header_size[0]);
1080
1081 /*
1082 * Get PPS in the first frame and copy it to an
1083 * intermediate buffer.
1084 */
1085 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1086 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1087 coda_write(dev, CODA_HEADER_H264_PPS, CODA_CMD_ENC_HEADER_CODE);
1088 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1089 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1090 return -ETIMEDOUT;
1091 }
1092 ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1093 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1094 memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0),
1095 ctx->vpu_header_size[1]);
1096 ctx->vpu_header_size[2] = 0;
1097 break;
1098 case V4L2_PIX_FMT_MPEG4:
1099 /*
1100 * Get VOS in the first frame and copy it to an
1101 * intermediate buffer
1102 */
1103 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1104 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1105 coda_write(dev, CODA_HEADER_MP4V_VOS, CODA_CMD_ENC_HEADER_CODE);
1106 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1107 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1108 return -ETIMEDOUT;
1109 }
1110 ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1111 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1112 memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0),
1113 ctx->vpu_header_size[0]);
1114
1115 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1116 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1117 coda_write(dev, CODA_HEADER_MP4V_VIS, CODA_CMD_ENC_HEADER_CODE);
1118 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1119 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n");
1120 return -ETIMEDOUT;
1121 }
1122 ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1123 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1124 memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0),
1125 ctx->vpu_header_size[1]);
1126
1127 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1128 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1129 coda_write(dev, CODA_HEADER_MP4V_VOL, CODA_CMD_ENC_HEADER_CODE);
1130 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1131 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n");
1132 return -ETIMEDOUT;
1133 }
1134 ctx->vpu_header_size[2] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1135 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1136 memcpy(&ctx->vpu_header[2][0], vb2_plane_vaddr(buf, 0),
1137 ctx->vpu_header_size[2]);
1138 break;
1139 default:
1140 /* No more formats need to save headers at the moment */
1141 break;
1142 }
1143
1144 return 0;
1145}
1146
1147static int coda_stop_streaming(struct vb2_queue *q)
1148{
1149 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1150
1151 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1152 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1153 "%s: output\n", __func__);
1154 ctx->rawstreamon = 0;
1155 } else {
1156 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1157 "%s: capture\n", __func__);
1158 ctx->compstreamon = 0;
1159 }
1160
1161 if (!ctx->rawstreamon && !ctx->compstreamon) {
1162 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1163 "%s: sent command 'SEQ_END' to coda\n", __func__);
1164 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1165 v4l2_err(&ctx->dev->v4l2_dev,
1166 "CODA_COMMAND_SEQ_END failed\n");
1167 return -ETIMEDOUT;
1168 }
1169 }
1170
1171 return 0;
1172}
1173
1174static struct vb2_ops coda_qops = {
1175 .queue_setup = coda_queue_setup,
1176 .buf_prepare = coda_buf_prepare,
1177 .buf_queue = coda_buf_queue,
1178 .wait_prepare = coda_wait_prepare,
1179 .wait_finish = coda_wait_finish,
1180 .start_streaming = coda_start_streaming,
1181 .stop_streaming = coda_stop_streaming,
1182};
1183
1184static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1185{
1186 struct coda_ctx *ctx =
1187 container_of(ctrl->handler, struct coda_ctx, ctrls);
1188
1189 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1190 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1191
1192 switch (ctrl->id) {
1193 case V4L2_CID_MPEG_VIDEO_BITRATE:
1194 ctx->params.bitrate = ctrl->val / 1000;
1195 break;
1196 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1197 ctx->params.gop_size = ctrl->val;
1198 break;
1199 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1200 ctx->params.h264_intra_qp = ctrl->val;
1201 break;
1202 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1203 ctx->params.h264_inter_qp = ctrl->val;
1204 break;
1205 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1206 ctx->params.mpeg4_intra_qp = ctrl->val;
1207 break;
1208 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1209 ctx->params.mpeg4_inter_qp = ctrl->val;
1210 break;
1211 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1212 ctx->params.slice_mode = ctrl->val;
1213 break;
1214 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1215 ctx->params.slice_max_mb = ctrl->val;
1216 break;
1217 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1218 break;
1219 default:
1220 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1221 "Invalid control, id=%d, val=%d\n",
1222 ctrl->id, ctrl->val);
1223 return -EINVAL;
1224 }
1225
1226 return 0;
1227}
1228
1229static struct v4l2_ctrl_ops coda_ctrl_ops = {
1230 .s_ctrl = coda_s_ctrl,
1231};
1232
1233static int coda_ctrls_setup(struct coda_ctx *ctx)
1234{
1235 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1236
1237 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1238 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1239 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1240 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1241 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1242 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1243 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1244 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1245 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1246 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1247 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1248 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1249 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1250 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
1251 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB, 0,
1252 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB);
1253 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1254 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
1255 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1256 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1257 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1258 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1259 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1260
1261 if (ctx->ctrls.error) {
1262 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1263 ctx->ctrls.error);
1264 return -EINVAL;
1265 }
1266
1267 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1268}
1269
1270static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1271 struct vb2_queue *dst_vq)
1272{
1273 struct coda_ctx *ctx = priv;
1274 int ret;
1275
Javier Martin186b2502012-07-26 05:53:35 -03001276 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1277 src_vq->io_modes = VB2_MMAP;
1278 src_vq->drv_priv = ctx;
1279 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1280 src_vq->ops = &coda_qops;
1281 src_vq->mem_ops = &vb2_dma_contig_memops;
1282
1283 ret = vb2_queue_init(src_vq);
1284 if (ret)
1285 return ret;
1286
Javier Martin186b2502012-07-26 05:53:35 -03001287 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1288 dst_vq->io_modes = VB2_MMAP;
1289 dst_vq->drv_priv = ctx;
1290 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1291 dst_vq->ops = &coda_qops;
1292 dst_vq->mem_ops = &vb2_dma_contig_memops;
1293
1294 return vb2_queue_init(dst_vq);
1295}
1296
1297static int coda_open(struct file *file)
1298{
1299 struct coda_dev *dev = video_drvdata(file);
1300 struct coda_ctx *ctx = NULL;
1301 int ret = 0;
1302
1303 if (dev->instances >= CODA_MAX_INSTANCES)
1304 return -EBUSY;
1305
1306 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1307 if (!ctx)
1308 return -ENOMEM;
1309
1310 v4l2_fh_init(&ctx->fh, video_devdata(file));
1311 file->private_data = &ctx->fh;
1312 v4l2_fh_add(&ctx->fh);
1313 ctx->dev = dev;
1314
1315 set_default_params(ctx);
1316 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1317 &coda_queue_init);
1318 if (IS_ERR(ctx->m2m_ctx)) {
1319 int ret = PTR_ERR(ctx->m2m_ctx);
1320
1321 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1322 __func__, ret);
1323 goto err;
1324 }
1325 ret = coda_ctrls_setup(ctx);
1326 if (ret) {
1327 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1328 goto err;
1329 }
1330
1331 ctx->fh.ctrl_handler = &ctx->ctrls;
1332
1333 ctx->parabuf.vaddr = dma_alloc_coherent(&dev->plat_dev->dev,
1334 CODA_PARA_BUF_SIZE, &ctx->parabuf.paddr, GFP_KERNEL);
1335 if (!ctx->parabuf.vaddr) {
1336 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1337 ret = -ENOMEM;
1338 goto err;
1339 }
1340
1341 coda_lock(ctx);
1342 ctx->idx = dev->instances++;
1343 coda_unlock(ctx);
1344
1345 clk_prepare_enable(dev->clk_per);
1346 clk_prepare_enable(dev->clk_ahb);
1347
1348 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1349 ctx->idx, ctx);
1350
1351 return 0;
1352
1353err:
1354 v4l2_fh_del(&ctx->fh);
1355 v4l2_fh_exit(&ctx->fh);
1356 kfree(ctx);
1357 return ret;
1358}
1359
1360static int coda_release(struct file *file)
1361{
1362 struct coda_dev *dev = video_drvdata(file);
1363 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1364
1365 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1366 ctx);
1367
1368 coda_lock(ctx);
1369 dev->instances--;
1370 coda_unlock(ctx);
1371
1372 dma_free_coherent(&dev->plat_dev->dev, CODA_PARA_BUF_SIZE,
1373 ctx->parabuf.vaddr, ctx->parabuf.paddr);
1374 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1375 v4l2_ctrl_handler_free(&ctx->ctrls);
1376 clk_disable_unprepare(dev->clk_per);
1377 clk_disable_unprepare(dev->clk_ahb);
1378 v4l2_fh_del(&ctx->fh);
1379 v4l2_fh_exit(&ctx->fh);
1380 kfree(ctx);
1381
1382 return 0;
1383}
1384
1385static unsigned int coda_poll(struct file *file,
1386 struct poll_table_struct *wait)
1387{
1388 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1389 int ret;
1390
1391 coda_lock(ctx);
1392 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1393 coda_unlock(ctx);
1394 return ret;
1395}
1396
1397static int coda_mmap(struct file *file, struct vm_area_struct *vma)
1398{
1399 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1400
1401 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1402}
1403
1404static const struct v4l2_file_operations coda_fops = {
1405 .owner = THIS_MODULE,
1406 .open = coda_open,
1407 .release = coda_release,
1408 .poll = coda_poll,
1409 .unlocked_ioctl = video_ioctl2,
1410 .mmap = coda_mmap,
1411};
1412
1413static irqreturn_t coda_irq_handler(int irq, void *data)
1414{
1415 struct vb2_buffer *src_buf, *dst_buf, *tmp_buf;
1416 struct coda_dev *dev = data;
1417 u32 wr_ptr, start_ptr;
1418 struct coda_ctx *ctx;
1419
1420 /* read status register to attend the IRQ */
1421 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1422 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1423 CODA_REG_BIT_INT_CLEAR);
1424
1425 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1426 if (ctx == NULL) {
1427 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
1428 return IRQ_HANDLED;
1429 }
1430
1431 if (ctx->aborting) {
1432 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1433 "task has been aborted\n");
1434 return IRQ_HANDLED;
1435 }
1436
1437 if (coda_isbusy(ctx->dev)) {
1438 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1439 "coda is still busy!!!!\n");
1440 return IRQ_NONE;
1441 }
1442
1443 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1444 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1445
1446 /* Get results from the coda */
1447 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
1448 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1449 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx));
1450 /* Calculate bytesused field */
1451 if (dst_buf->v4l2_buf.sequence == 0) {
1452 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr) +
1453 ctx->vpu_header_size[0] +
1454 ctx->vpu_header_size[1] +
1455 ctx->vpu_header_size[2];
1456 } else {
1457 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr);
1458 }
1459
1460 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1461 wr_ptr - start_ptr);
1462
1463 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1464 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1465
1466 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1467 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1468 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1469 } else {
1470 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1471 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1472 }
1473
1474 /* Free previous reference picture if available */
1475 if (ctx->reference) {
1476 v4l2_m2m_buf_done(ctx->reference, VB2_BUF_STATE_DONE);
1477 ctx->reference = NULL;
1478 }
1479
1480 /*
1481 * For the last frame of the gop we don't need to save
1482 * a reference picture.
1483 */
1484 v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1485 tmp_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1486 if (ctx->gopcounter == 0)
1487 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1488 else
1489 ctx->reference = tmp_buf;
1490
1491 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1492
1493 ctx->gopcounter--;
1494 if (ctx->gopcounter < 0)
1495 ctx->gopcounter = ctx->params.gop_size - 1;
1496
1497 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1498 "job finished: encoding frame (%d) (%s)\n",
1499 dst_buf->v4l2_buf.sequence,
1500 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1501 "KEYFRAME" : "PFRAME");
1502
1503 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
1504
1505 return IRQ_HANDLED;
1506}
1507
1508static u32 coda_supported_firmwares[] = {
1509 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001510 CODA_FIRMWARE_VERNUM(CODA_7541, 13, 4, 29),
Javier Martin186b2502012-07-26 05:53:35 -03001511};
1512
1513static bool coda_firmware_supported(u32 vernum)
1514{
1515 int i;
1516
1517 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
1518 if (vernum == coda_supported_firmwares[i])
1519 return true;
1520 return false;
1521}
1522
1523static char *coda_product_name(int product)
1524{
1525 static char buf[9];
1526
1527 switch (product) {
1528 case CODA_DX6:
1529 return "CodaDx6";
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001530 case CODA_7541:
1531 return "CODA7541";
Javier Martin186b2502012-07-26 05:53:35 -03001532 default:
1533 snprintf(buf, sizeof(buf), "(0x%04x)", product);
1534 return buf;
1535 }
1536}
1537
Philipp Zabel87048bb2012-07-02 07:03:43 -03001538static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001539{
1540 u16 product, major, minor, release;
1541 u32 data;
1542 u16 *p;
1543 int i;
1544
1545 clk_prepare_enable(dev->clk_per);
1546 clk_prepare_enable(dev->clk_ahb);
1547
Javier Martin186b2502012-07-26 05:53:35 -03001548 /*
1549 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001550 * The 16-bit chars in the code buffer are in memory access
1551 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001552 * Data in this SRAM survives a reboot.
1553 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001554 p = (u16 *)dev->codebuf.vaddr;
1555 if (dev->devtype->product == CODA_DX6) {
1556 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1557 data = CODA_DOWN_ADDRESS_SET(i) |
1558 CODA_DOWN_DATA_SET(p[i ^ 1]);
1559 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1560 }
1561 } else {
1562 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1563 data = CODA_DOWN_ADDRESS_SET(i) |
1564 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1565 3 - (i % 4)]);
1566 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1567 }
Javier Martin186b2502012-07-26 05:53:35 -03001568 }
Javier Martin186b2502012-07-26 05:53:35 -03001569
1570 /* Tell the BIT where to find everything it needs */
1571 coda_write(dev, dev->workbuf.paddr,
1572 CODA_REG_BIT_WORK_BUF_ADDR);
1573 coda_write(dev, dev->codebuf.paddr,
1574 CODA_REG_BIT_CODE_BUF_ADDR);
1575 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1576
1577 /* Set default values */
1578 switch (dev->devtype->product) {
1579 case CODA_DX6:
1580 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1581 break;
1582 default:
1583 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1584 }
1585 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
1586 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1587 CODA_REG_BIT_INT_ENABLE);
1588
1589 /* Reset VPU and start processor */
1590 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1591 data |= CODA_REG_RESET_ENABLE;
1592 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1593 udelay(10);
1594 data &= ~CODA_REG_RESET_ENABLE;
1595 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1596 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1597
1598 /* Load firmware */
1599 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
1600 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
1601 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
1602 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
1603 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
1604 if (coda_wait_timeout(dev)) {
1605 clk_disable_unprepare(dev->clk_per);
1606 clk_disable_unprepare(dev->clk_ahb);
1607 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
1608 return -EIO;
1609 }
1610
1611 /* Check we are compatible with the loaded firmware */
1612 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
1613 product = CODA_FIRMWARE_PRODUCT(data);
1614 major = CODA_FIRMWARE_MAJOR(data);
1615 minor = CODA_FIRMWARE_MINOR(data);
1616 release = CODA_FIRMWARE_RELEASE(data);
1617
1618 clk_disable_unprepare(dev->clk_per);
1619 clk_disable_unprepare(dev->clk_ahb);
1620
1621 if (product != dev->devtype->product) {
1622 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
1623 " Version: %u.%u.%u\n",
1624 coda_product_name(dev->devtype->product),
1625 coda_product_name(product), major, minor, release);
1626 return -EINVAL;
1627 }
1628
1629 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
1630 coda_product_name(product));
1631
1632 if (coda_firmware_supported(data)) {
1633 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
1634 major, minor, release);
1635 } else {
1636 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
1637 "%u.%u.%u\n", major, minor, release);
1638 }
1639
1640 return 0;
1641}
1642
1643static void coda_fw_callback(const struct firmware *fw, void *context)
1644{
1645 struct coda_dev *dev = context;
1646 struct platform_device *pdev = dev->plat_dev;
1647 int ret;
1648
1649 if (!fw) {
1650 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1651 return;
1652 }
1653
1654 /* allocate auxiliary per-device code buffer for the BIT processor */
1655 dev->codebuf.size = fw->size;
1656 dev->codebuf.vaddr = dma_alloc_coherent(&pdev->dev, fw->size,
1657 &dev->codebuf.paddr,
1658 GFP_KERNEL);
1659 if (!dev->codebuf.vaddr) {
1660 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1661 return;
1662 }
1663
Philipp Zabel87048bb2012-07-02 07:03:43 -03001664 /* Copy the whole firmware image to the code buffer */
1665 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1666 release_firmware(fw);
1667
1668 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03001669 if (ret) {
1670 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1671 return;
1672 }
1673
1674 dev->vfd.fops = &coda_fops,
1675 dev->vfd.ioctl_ops = &coda_ioctl_ops;
1676 dev->vfd.release = video_device_release_empty,
1677 dev->vfd.lock = &dev->dev_mutex;
1678 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03001679 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03001680 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
1681 video_set_drvdata(&dev->vfd, dev);
1682
1683 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1684 if (IS_ERR(dev->alloc_ctx)) {
1685 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1686 return;
1687 }
1688
1689 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1690 if (IS_ERR(dev->m2m_dev)) {
1691 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1692 goto rel_ctx;
1693 }
1694
1695 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
1696 if (ret) {
1697 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1698 goto rel_m2m;
1699 }
1700 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
1701 dev->vfd.num);
1702
1703 return;
1704
1705rel_m2m:
1706 v4l2_m2m_release(dev->m2m_dev);
1707rel_ctx:
1708 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1709}
1710
1711static int coda_firmware_request(struct coda_dev *dev)
1712{
1713 char *fw = dev->devtype->firmware;
1714
1715 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1716 coda_product_name(dev->devtype->product));
1717
1718 return request_firmware_nowait(THIS_MODULE, true,
1719 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1720}
1721
1722enum coda_platform {
1723 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001724 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03001725};
1726
Emil Goodec06d8752012-08-14 17:44:42 -03001727static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001728 [CODA_IMX27] = {
1729 .firmware = "v4l-codadx6-imx27.bin",
1730 .product = CODA_DX6,
1731 .formats = codadx6_formats,
1732 .num_formats = ARRAY_SIZE(codadx6_formats),
1733 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001734 [CODA_IMX53] = {
1735 .firmware = "v4l-coda7541-imx53.bin",
1736 .product = CODA_7541,
1737 .formats = coda7_formats,
1738 .num_formats = ARRAY_SIZE(coda7_formats),
1739 },
Javier Martin186b2502012-07-26 05:53:35 -03001740};
1741
1742static struct platform_device_id coda_platform_ids[] = {
1743 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001744 { .name = "coda-imx53", .driver_data = CODA_7541 },
Javier Martin186b2502012-07-26 05:53:35 -03001745 { /* sentinel */ }
1746};
1747MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1748
1749#ifdef CONFIG_OF
1750static const struct of_device_id coda_dt_ids[] = {
1751 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001752 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03001753 { /* sentinel */ }
1754};
1755MODULE_DEVICE_TABLE(of, coda_dt_ids);
1756#endif
1757
1758static int __devinit coda_probe(struct platform_device *pdev)
1759{
1760 const struct of_device_id *of_id =
1761 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1762 const struct platform_device_id *pdev_id;
1763 struct coda_dev *dev;
1764 struct resource *res;
1765 int ret, irq;
1766
1767 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1768 if (!dev) {
1769 dev_err(&pdev->dev, "Not enough memory for %s\n",
1770 CODA_NAME);
1771 return -ENOMEM;
1772 }
1773
1774 spin_lock_init(&dev->irqlock);
1775
1776 dev->plat_dev = pdev;
1777 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1778 if (IS_ERR(dev->clk_per)) {
1779 dev_err(&pdev->dev, "Could not get per clock\n");
1780 return PTR_ERR(dev->clk_per);
1781 }
1782
1783 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1784 if (IS_ERR(dev->clk_ahb)) {
1785 dev_err(&pdev->dev, "Could not get ahb clock\n");
1786 return PTR_ERR(dev->clk_ahb);
1787 }
1788
1789 /* Get memory for physical registers */
1790 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1791 if (res == NULL) {
1792 dev_err(&pdev->dev, "failed to get memory region resource\n");
1793 return -ENOENT;
1794 }
1795
1796 if (devm_request_mem_region(&pdev->dev, res->start,
1797 resource_size(res), CODA_NAME) == NULL) {
1798 dev_err(&pdev->dev, "failed to request memory region\n");
1799 return -ENOENT;
1800 }
1801 dev->regs_base = devm_ioremap(&pdev->dev, res->start,
1802 resource_size(res));
1803 if (!dev->regs_base) {
1804 dev_err(&pdev->dev, "failed to ioremap address region\n");
1805 return -ENOENT;
1806 }
1807
1808 /* IRQ */
1809 irq = platform_get_irq(pdev, 0);
1810 if (irq < 0) {
1811 dev_err(&pdev->dev, "failed to get irq resource\n");
1812 return -ENOENT;
1813 }
1814
1815 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
1816 0, CODA_NAME, dev) < 0) {
1817 dev_err(&pdev->dev, "failed to request irq\n");
1818 return -ENOENT;
1819 }
1820
1821 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1822 if (ret)
1823 return ret;
1824
1825 mutex_init(&dev->dev_mutex);
1826
1827 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
1828
1829 if (of_id) {
1830 dev->devtype = of_id->data;
1831 } else if (pdev_id) {
1832 dev->devtype = &coda_devdata[pdev_id->driver_data];
1833 } else {
1834 v4l2_device_unregister(&dev->v4l2_dev);
1835 return -EINVAL;
1836 }
1837
1838 /* allocate auxiliary per-device buffers for the BIT processor */
1839 switch (dev->devtype->product) {
1840 case CODA_DX6:
1841 dev->workbuf.size = CODADX6_WORK_BUF_SIZE;
1842 break;
1843 default:
1844 dev->workbuf.size = CODA7_WORK_BUF_SIZE;
1845 }
1846 dev->workbuf.vaddr = dma_alloc_coherent(&pdev->dev, dev->workbuf.size,
1847 &dev->workbuf.paddr,
1848 GFP_KERNEL);
1849 if (!dev->workbuf.vaddr) {
1850 dev_err(&pdev->dev, "failed to allocate work buffer\n");
1851 v4l2_device_unregister(&dev->v4l2_dev);
1852 return -ENOMEM;
1853 }
1854
1855 platform_set_drvdata(pdev, dev);
1856
1857 return coda_firmware_request(dev);
1858}
1859
1860static int coda_remove(struct platform_device *pdev)
1861{
1862 struct coda_dev *dev = platform_get_drvdata(pdev);
1863
1864 video_unregister_device(&dev->vfd);
1865 if (dev->m2m_dev)
1866 v4l2_m2m_release(dev->m2m_dev);
1867 if (dev->alloc_ctx)
1868 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1869 v4l2_device_unregister(&dev->v4l2_dev);
1870 if (dev->codebuf.vaddr)
1871 dma_free_coherent(&pdev->dev, dev->codebuf.size,
1872 &dev->codebuf.vaddr, dev->codebuf.paddr);
1873 if (dev->workbuf.vaddr)
1874 dma_free_coherent(&pdev->dev, dev->workbuf.size, &dev->workbuf.vaddr,
1875 dev->workbuf.paddr);
1876 return 0;
1877}
1878
1879static struct platform_driver coda_driver = {
1880 .probe = coda_probe,
1881 .remove = __devexit_p(coda_remove),
1882 .driver = {
1883 .name = CODA_NAME,
1884 .owner = THIS_MODULE,
1885 .of_match_table = of_match_ptr(coda_dt_ids),
1886 },
1887 .id_table = coda_platform_ids,
1888};
1889
1890module_platform_driver(coda_driver);
1891
1892MODULE_LICENSE("GPL");
1893MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
1894MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");