blob: ba99e2fb0f85ff7df1b059f804b020cb7d3d4c95 [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>
Philipp Zabel657eee72013-04-29 16:17:14 -070017#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030018#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
21#include <linux/module.h>
22#include <linux/of_device.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/videodev2.h>
26#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070027#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030028
29#include <media/v4l2-ctrls.h>
30#include <media/v4l2-device.h>
31#include <media/v4l2-ioctl.h>
32#include <media/v4l2-mem2mem.h>
33#include <media/videobuf2-core.h>
34#include <media/videobuf2-dma-contig.h>
35
36#include "coda.h"
37
38#define CODA_NAME "coda"
39
40#define CODA_MAX_INSTANCES 4
41
42#define CODA_FMO_BUF_SIZE 32
43#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
44#define CODA7_WORK_BUF_SIZE (512 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
45#define CODA_PARA_BUF_SIZE (10 * 1024)
46#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070047#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel10436672012-07-02 09:03:55 -030048#define CODA7_IRAM_SIZE 0x14000 /* 81920 bytes */
Javier Martin186b2502012-07-26 05:53:35 -030049
Philipp Zabelec25f682012-07-20 08:54:29 -030050#define CODA_MAX_FRAMEBUFFERS 2
Javier Martin186b2502012-07-26 05:53:35 -030051
52#define MAX_W 720
53#define MAX_H 576
54#define CODA_MAX_FRAME_SIZE 0x90000
55#define FMO_SLICE_SAVE_BUF_SIZE (32)
56#define CODA_DEFAULT_GAMMA 4096
57
58#define MIN_W 176
59#define MIN_H 144
60#define MAX_W 720
61#define MAX_H 576
62
63#define S_ALIGN 1 /* multiple of 2 */
64#define W_ALIGN 1 /* multiple of 2 */
65#define H_ALIGN 1 /* multiple of 2 */
66
67#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
68
69static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030070module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030071MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
72
73enum {
74 V4L2_M2M_SRC = 0,
75 V4L2_M2M_DST = 1,
76};
77
78enum coda_fmt_type {
79 CODA_FMT_ENC,
80 CODA_FMT_RAW,
81};
82
83enum coda_inst_type {
84 CODA_INST_ENCODER,
85 CODA_INST_DECODER,
86};
87
88enum coda_product {
89 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030090 CODA_7541 = 0xf012,
Javier Martin186b2502012-07-26 05:53:35 -030091};
92
93struct coda_fmt {
94 char *name;
95 u32 fourcc;
96 enum coda_fmt_type type;
97};
98
99struct coda_devtype {
100 char *firmware;
101 enum coda_product product;
102 struct coda_fmt *formats;
103 unsigned int num_formats;
104 size_t workbuf_size;
105};
106
107/* Per-queue, driver-specific private data */
108struct coda_q_data {
109 unsigned int width;
110 unsigned int height;
111 unsigned int sizeimage;
112 struct coda_fmt *fmt;
113};
114
115struct coda_aux_buf {
116 void *vaddr;
117 dma_addr_t paddr;
118 u32 size;
119};
120
121struct coda_dev {
122 struct v4l2_device v4l2_dev;
123 struct video_device vfd;
124 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300125 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300126
127 void __iomem *regs_base;
128 struct clk *clk_per;
129 struct clk *clk_ahb;
130
131 struct coda_aux_buf codebuf;
132 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700133 struct gen_pool *iram_pool;
134 long unsigned int iram_vaddr;
Philipp Zabel10436672012-07-02 09:03:55 -0300135 long unsigned int iram_paddr;
Philipp Zabel657eee72013-04-29 16:17:14 -0700136 unsigned long iram_size;
Javier Martin186b2502012-07-26 05:53:35 -0300137
138 spinlock_t irqlock;
139 struct mutex dev_mutex;
140 struct v4l2_m2m_dev *m2m_dev;
141 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300142 struct list_head instances;
143 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300144 struct delayed_work timeout;
Philipp Zabel62bed142012-07-25 10:40:39 -0300145 struct completion done;
Javier Martin186b2502012-07-26 05:53:35 -0300146};
147
148struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300149 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300150 u8 h264_intra_qp;
151 u8 h264_inter_qp;
152 u8 mpeg4_intra_qp;
153 u8 mpeg4_inter_qp;
154 u8 gop_size;
155 int codec_mode;
156 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
157 u32 framerate;
158 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300159 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300160 u32 slice_max_mb;
161};
162
163struct coda_ctx {
164 struct coda_dev *dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300165 struct list_head list;
Javier Martin186b2502012-07-26 05:53:35 -0300166 int aborting;
167 int rawstreamon;
168 int compstreamon;
169 u32 isequence;
170 struct coda_q_data q_data[2];
171 enum coda_inst_type inst_type;
172 enum v4l2_colorspace colorspace;
173 struct coda_params params;
174 struct v4l2_m2m_ctx *m2m_ctx;
175 struct v4l2_ctrl_handler ctrls;
176 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300177 int gopcounter;
178 char vpu_header[3][64];
179 int vpu_header_size[3];
180 struct coda_aux_buf parabuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300181 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
182 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300183 int idx;
184};
185
Javier Martin832fbb52012-10-29 08:34:59 -0300186static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
188static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300189
Javier Martin186b2502012-07-26 05:53:35 -0300190static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
191{
192 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
193 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
194 writel(data, dev->regs_base + reg);
195}
196
197static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
198{
199 u32 data;
200 data = readl(dev->regs_base + reg);
201 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
202 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
203 return data;
204}
205
206static inline unsigned long coda_isbusy(struct coda_dev *dev)
207{
208 return coda_read(dev, CODA_REG_BIT_BUSY);
209}
210
211static inline int coda_is_initialized(struct coda_dev *dev)
212{
213 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
214}
215
216static int coda_wait_timeout(struct coda_dev *dev)
217{
218 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
219
220 while (coda_isbusy(dev)) {
221 if (time_after(jiffies, timeout))
222 return -ETIMEDOUT;
223 }
224 return 0;
225}
226
227static void coda_command_async(struct coda_ctx *ctx, int cmd)
228{
229 struct coda_dev *dev = ctx->dev;
230 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
231
232 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
233 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
234 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
235}
236
237static int coda_command_sync(struct coda_ctx *ctx, int cmd)
238{
239 struct coda_dev *dev = ctx->dev;
240
241 coda_command_async(ctx, cmd);
242 return coda_wait_timeout(dev);
243}
244
245static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
246 enum v4l2_buf_type type)
247{
248 switch (type) {
249 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
250 return &(ctx->q_data[V4L2_M2M_SRC]);
251 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
252 return &(ctx->q_data[V4L2_M2M_DST]);
253 default:
254 BUG();
255 }
256 return NULL;
257}
258
259/*
260 * Add one array of supported formats for each version of Coda:
261 * i.MX27 -> codadx6
262 * i.MX51 -> coda7
263 * i.MX6 -> coda960
264 */
265static struct coda_fmt codadx6_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
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300283static struct coda_fmt coda7_formats[] = {
284 {
285 .name = "YUV 4:2:0 Planar",
286 .fourcc = V4L2_PIX_FMT_YUV420,
287 .type = CODA_FMT_RAW,
288 },
289 {
290 .name = "H264 Encoded Stream",
291 .fourcc = V4L2_PIX_FMT_H264,
292 .type = CODA_FMT_ENC,
293 },
294 {
295 .name = "MPEG4 Encoded Stream",
296 .fourcc = V4L2_PIX_FMT_MPEG4,
297 .type = CODA_FMT_ENC,
298 },
299};
300
Javier Martin186b2502012-07-26 05:53:35 -0300301static struct coda_fmt *find_format(struct coda_dev *dev, struct v4l2_format *f)
302{
303 struct coda_fmt *formats = dev->devtype->formats;
304 int num_formats = dev->devtype->num_formats;
305 unsigned int k;
306
307 for (k = 0; k < num_formats; k++) {
308 if (formats[k].fourcc == f->fmt.pix.pixelformat)
309 break;
310 }
311
312 if (k == num_formats)
313 return NULL;
314
315 return &formats[k];
316}
317
318/*
319 * V4L2 ioctl() operations.
320 */
321static int vidioc_querycap(struct file *file, void *priv,
322 struct v4l2_capability *cap)
323{
324 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
325 strlcpy(cap->card, CODA_NAME, sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300326 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300327 /*
328 * This is only a mem-to-mem video device. The capture and output
329 * device capability flags are left only for backward compatibility
330 * and are scheduled for removal.
331 */
332 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
333 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300334 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
335
336 return 0;
337}
338
339static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
340 enum coda_fmt_type type)
341{
342 struct coda_ctx *ctx = fh_to_ctx(priv);
343 struct coda_dev *dev = ctx->dev;
344 struct coda_fmt *formats = dev->devtype->formats;
345 struct coda_fmt *fmt;
346 int num_formats = dev->devtype->num_formats;
347 int i, num = 0;
348
349 for (i = 0; i < num_formats; i++) {
350 if (formats[i].type == type) {
351 if (num == f->index)
352 break;
353 ++num;
354 }
355 }
356
357 if (i < num_formats) {
358 fmt = &formats[i];
359 strlcpy(f->description, fmt->name, sizeof(f->description));
360 f->pixelformat = fmt->fourcc;
361 return 0;
362 }
363
364 /* Format not found */
365 return -EINVAL;
366}
367
368static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
369 struct v4l2_fmtdesc *f)
370{
371 return enum_fmt(priv, f, CODA_FMT_ENC);
372}
373
374static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
375 struct v4l2_fmtdesc *f)
376{
377 return enum_fmt(priv, f, CODA_FMT_RAW);
378}
379
380static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
381{
382 struct vb2_queue *vq;
383 struct coda_q_data *q_data;
384 struct coda_ctx *ctx = fh_to_ctx(priv);
385
386 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
387 if (!vq)
388 return -EINVAL;
389
390 q_data = get_q_data(ctx, f->type);
391
392 f->fmt.pix.field = V4L2_FIELD_NONE;
393 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
394 f->fmt.pix.width = q_data->width;
395 f->fmt.pix.height = q_data->height;
396 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420)
397 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
398 else /* encoded formats h.264/mpeg4 */
399 f->fmt.pix.bytesperline = 0;
400
401 f->fmt.pix.sizeimage = q_data->sizeimage;
402 f->fmt.pix.colorspace = ctx->colorspace;
403
404 return 0;
405}
406
407static int vidioc_try_fmt(struct coda_dev *dev, struct v4l2_format *f)
408{
409 enum v4l2_field field;
410
411 field = f->fmt.pix.field;
412 if (field == V4L2_FIELD_ANY)
413 field = V4L2_FIELD_NONE;
414 else if (V4L2_FIELD_NONE != field)
415 return -EINVAL;
416
417 /* V4L2 specification suggests the driver corrects the format struct
418 * if any of the dimensions is unsupported */
419 f->fmt.pix.field = field;
420
421 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420) {
422 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W,
423 W_ALIGN, &f->fmt.pix.height,
424 MIN_H, MAX_H, H_ALIGN, S_ALIGN);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300425 /* Frame stride must be multiple of 8 */
426 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
427 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300428 f->fmt.pix.height * 3 / 2;
Javier Martin186b2502012-07-26 05:53:35 -0300429 } else { /*encoded formats h.264/mpeg4 */
430 f->fmt.pix.bytesperline = 0;
431 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
432 }
433
434 return 0;
435}
436
437static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
438 struct v4l2_format *f)
439{
440 int ret;
441 struct coda_fmt *fmt;
442 struct coda_ctx *ctx = fh_to_ctx(priv);
443
444 fmt = find_format(ctx->dev, f);
445 /*
446 * Since decoding support is not implemented yet do not allow
447 * CODA_FMT_RAW formats in the capture interface.
448 */
449 if (!fmt || !(fmt->type == CODA_FMT_ENC))
450 f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
451
452 f->fmt.pix.colorspace = ctx->colorspace;
453
454 ret = vidioc_try_fmt(ctx->dev, f);
455 if (ret < 0)
456 return ret;
457
458 return 0;
459}
460
461static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
462 struct v4l2_format *f)
463{
464 struct coda_ctx *ctx = fh_to_ctx(priv);
465 struct coda_fmt *fmt;
466 int ret;
467
468 fmt = find_format(ctx->dev, f);
469 /*
470 * Since decoding support is not implemented yet do not allow
471 * CODA_FMT formats in the capture interface.
472 */
473 if (!fmt || !(fmt->type == CODA_FMT_RAW))
474 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
475
476 if (!f->fmt.pix.colorspace)
477 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
478
479 ret = vidioc_try_fmt(ctx->dev, f);
480 if (ret < 0)
481 return ret;
482
483 return 0;
484}
485
486static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
487{
488 struct coda_q_data *q_data;
489 struct vb2_queue *vq;
490 int ret;
491
492 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
493 if (!vq)
494 return -EINVAL;
495
496 q_data = get_q_data(ctx, f->type);
497 if (!q_data)
498 return -EINVAL;
499
500 if (vb2_is_busy(vq)) {
501 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
502 return -EBUSY;
503 }
504
505 ret = vidioc_try_fmt(ctx->dev, f);
506 if (ret)
507 return ret;
508
509 q_data->fmt = find_format(ctx->dev, f);
510 q_data->width = f->fmt.pix.width;
511 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300512 q_data->sizeimage = f->fmt.pix.sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300513
514 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
515 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
516 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
517
518 return 0;
519}
520
521static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
522 struct v4l2_format *f)
523{
524 int ret;
525
526 ret = vidioc_try_fmt_vid_cap(file, priv, f);
527 if (ret)
528 return ret;
529
530 return vidioc_s_fmt(fh_to_ctx(priv), f);
531}
532
533static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
534 struct v4l2_format *f)
535{
536 struct coda_ctx *ctx = fh_to_ctx(priv);
537 int ret;
538
539 ret = vidioc_try_fmt_vid_out(file, priv, f);
540 if (ret)
541 return ret;
542
Richard Zhao8d621472012-08-21 02:47:42 -0300543 ret = vidioc_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300544 if (ret)
545 ctx->colorspace = f->fmt.pix.colorspace;
546
547 return ret;
548}
549
550static int vidioc_reqbufs(struct file *file, void *priv,
551 struct v4l2_requestbuffers *reqbufs)
552{
553 struct coda_ctx *ctx = fh_to_ctx(priv);
554
555 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
556}
557
558static int vidioc_querybuf(struct file *file, void *priv,
559 struct v4l2_buffer *buf)
560{
561 struct coda_ctx *ctx = fh_to_ctx(priv);
562
563 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
564}
565
566static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
567{
568 struct coda_ctx *ctx = fh_to_ctx(priv);
569
570 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
571}
572
Philipp Zabel419869c2013-05-23 07:06:30 -0300573static int vidioc_expbuf(struct file *file, void *priv,
574 struct v4l2_exportbuffer *eb)
575{
576 struct coda_ctx *ctx = fh_to_ctx(priv);
577
578 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
579}
580
Javier Martin186b2502012-07-26 05:53:35 -0300581static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
582{
583 struct coda_ctx *ctx = fh_to_ctx(priv);
584
585 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
586}
587
588static int vidioc_streamon(struct file *file, void *priv,
589 enum v4l2_buf_type type)
590{
591 struct coda_ctx *ctx = fh_to_ctx(priv);
592
593 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
594}
595
596static int vidioc_streamoff(struct file *file, void *priv,
597 enum v4l2_buf_type type)
598{
599 struct coda_ctx *ctx = fh_to_ctx(priv);
600
601 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
602}
603
604static const struct v4l2_ioctl_ops coda_ioctl_ops = {
605 .vidioc_querycap = vidioc_querycap,
606
607 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
608 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
609 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
610 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
611
612 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
613 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
614 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
615 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
616
617 .vidioc_reqbufs = vidioc_reqbufs,
618 .vidioc_querybuf = vidioc_querybuf,
619
620 .vidioc_qbuf = vidioc_qbuf,
Philipp Zabel419869c2013-05-23 07:06:30 -0300621 .vidioc_expbuf = vidioc_expbuf,
Javier Martin186b2502012-07-26 05:53:35 -0300622 .vidioc_dqbuf = vidioc_dqbuf,
623
624 .vidioc_streamon = vidioc_streamon,
625 .vidioc_streamoff = vidioc_streamoff,
626};
627
628/*
629 * Mem-to-mem operations.
630 */
631static void coda_device_run(void *m2m_priv)
632{
633 struct coda_ctx *ctx = m2m_priv;
634 struct coda_q_data *q_data_src, *q_data_dst;
635 struct vb2_buffer *src_buf, *dst_buf;
636 struct coda_dev *dev = ctx->dev;
637 int force_ipicture;
638 int quant_param = 0;
639 u32 picture_y, picture_cb, picture_cr;
640 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
641 u32 dst_fourcc;
642
643 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
644 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
645 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
646 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
647 dst_fourcc = q_data_dst->fmt->fourcc;
648
649 src_buf->v4l2_buf.sequence = ctx->isequence;
650 dst_buf->v4l2_buf.sequence = ctx->isequence;
651 ctx->isequence++;
652
653 /*
654 * Workaround coda firmware BUG that only marks the first
655 * frame as IDR. This is a problem for some decoders that can't
656 * recover when a frame is lost.
657 */
658 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
659 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
660 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
661 } else {
662 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
663 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
664 }
665
666 /*
667 * Copy headers at the beginning of the first frame for H.264 only.
668 * In MPEG4 they are already copied by the coda.
669 */
670 if (src_buf->v4l2_buf.sequence == 0) {
671 pic_stream_buffer_addr =
672 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
673 ctx->vpu_header_size[0] +
674 ctx->vpu_header_size[1] +
675 ctx->vpu_header_size[2];
676 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
677 ctx->vpu_header_size[0] -
678 ctx->vpu_header_size[1] -
679 ctx->vpu_header_size[2];
680 memcpy(vb2_plane_vaddr(dst_buf, 0),
681 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
682 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
683 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
684 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
685 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
686 ctx->vpu_header_size[2]);
687 } else {
688 pic_stream_buffer_addr =
689 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
690 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
691 }
692
693 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
694 force_ipicture = 1;
695 switch (dst_fourcc) {
696 case V4L2_PIX_FMT_H264:
697 quant_param = ctx->params.h264_intra_qp;
698 break;
699 case V4L2_PIX_FMT_MPEG4:
700 quant_param = ctx->params.mpeg4_intra_qp;
701 break;
702 default:
703 v4l2_warn(&ctx->dev->v4l2_dev,
704 "cannot set intra qp, fmt not supported\n");
705 break;
706 }
707 } else {
708 force_ipicture = 0;
709 switch (dst_fourcc) {
710 case V4L2_PIX_FMT_H264:
711 quant_param = ctx->params.h264_inter_qp;
712 break;
713 case V4L2_PIX_FMT_MPEG4:
714 quant_param = ctx->params.mpeg4_inter_qp;
715 break;
716 default:
717 v4l2_warn(&ctx->dev->v4l2_dev,
718 "cannot set inter qp, fmt not supported\n");
719 break;
720 }
721 }
722
723 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300724 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -0300725 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
726
727
728 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
729 picture_cb = picture_y + q_data_src->width * q_data_src->height;
730 picture_cr = picture_cb + q_data_src->width / 2 *
731 q_data_src->height / 2;
732
733 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
734 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
735 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
736 coda_write(dev, force_ipicture << 1 & 0x2,
737 CODA_CMD_ENC_PIC_OPTION);
738
739 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
740 coda_write(dev, pic_stream_buffer_size / 1024,
741 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -0300742
743 if (dev->devtype->product == CODA_7541) {
744 coda_write(dev, CODA7_USE_BIT_ENABLE | CODA7_USE_HOST_BIT_ENABLE |
745 CODA7_USE_ME_ENABLE | CODA7_USE_HOST_ME_ENABLE,
746 CODA7_REG_BIT_AXI_SRAM_USE);
747 }
748
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300749 /* 1 second timeout in case CODA locks up */
750 schedule_delayed_work(&dev->timeout, HZ);
751
Philipp Zabel62bed142012-07-25 10:40:39 -0300752 INIT_COMPLETION(dev->done);
Javier Martin186b2502012-07-26 05:53:35 -0300753 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
754}
755
756static int coda_job_ready(void *m2m_priv)
757{
758 struct coda_ctx *ctx = m2m_priv;
759
760 /*
761 * For both 'P' and 'key' frame cases 1 picture
762 * and 1 frame are needed.
763 */
764 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) ||
765 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
766 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
767 "not ready: not enough video buffers.\n");
768 return 0;
769 }
770
Javier Martin186b2502012-07-26 05:53:35 -0300771 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
772 "job ready\n");
773 return 1;
774}
775
776static void coda_job_abort(void *priv)
777{
778 struct coda_ctx *ctx = priv;
779 struct coda_dev *dev = ctx->dev;
780
781 ctx->aborting = 1;
782
783 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
784 "Aborting task\n");
785
786 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
787}
788
789static void coda_lock(void *m2m_priv)
790{
791 struct coda_ctx *ctx = m2m_priv;
792 struct coda_dev *pcdev = ctx->dev;
793 mutex_lock(&pcdev->dev_mutex);
794}
795
796static void coda_unlock(void *m2m_priv)
797{
798 struct coda_ctx *ctx = m2m_priv;
799 struct coda_dev *pcdev = ctx->dev;
800 mutex_unlock(&pcdev->dev_mutex);
801}
802
803static struct v4l2_m2m_ops coda_m2m_ops = {
804 .device_run = coda_device_run,
805 .job_ready = coda_job_ready,
806 .job_abort = coda_job_abort,
807 .lock = coda_lock,
808 .unlock = coda_unlock,
809};
810
811static void set_default_params(struct coda_ctx *ctx)
812{
813 struct coda_dev *dev = ctx->dev;
814
815 ctx->params.codec_mode = CODA_MODE_INVALID;
816 ctx->colorspace = V4L2_COLORSPACE_REC709;
817 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300818 ctx->aborting = 0;
819
820 /* Default formats for output and input queues */
821 ctx->q_data[V4L2_M2M_SRC].fmt = &dev->devtype->formats[0];
822 ctx->q_data[V4L2_M2M_DST].fmt = &dev->devtype->formats[1];
823 ctx->q_data[V4L2_M2M_SRC].width = MAX_W;
824 ctx->q_data[V4L2_M2M_SRC].height = MAX_H;
825 ctx->q_data[V4L2_M2M_SRC].sizeimage = (MAX_W * MAX_H * 3) / 2;
826 ctx->q_data[V4L2_M2M_DST].width = MAX_W;
827 ctx->q_data[V4L2_M2M_DST].height = MAX_H;
828 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
829}
830
831/*
832 * Queue operations
833 */
834static int coda_queue_setup(struct vb2_queue *vq,
835 const struct v4l2_format *fmt,
836 unsigned int *nbuffers, unsigned int *nplanes,
837 unsigned int sizes[], void *alloc_ctxs[])
838{
839 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300840 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300841 unsigned int size;
842
Philipp Zabele34db062012-08-29 08:22:00 -0300843 q_data = get_q_data(ctx, vq->type);
844 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300845
846 *nplanes = 1;
847 sizes[0] = size;
848
849 alloc_ctxs[0] = ctx->dev->alloc_ctx;
850
851 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
852 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
853
854 return 0;
855}
856
857static int coda_buf_prepare(struct vb2_buffer *vb)
858{
859 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
860 struct coda_q_data *q_data;
861
862 q_data = get_q_data(ctx, vb->vb2_queue->type);
863
864 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
865 v4l2_warn(&ctx->dev->v4l2_dev,
866 "%s data will not fit into plane (%lu < %lu)\n",
867 __func__, vb2_plane_size(vb, 0),
868 (long)q_data->sizeimage);
869 return -EINVAL;
870 }
871
Javier Martin186b2502012-07-26 05:53:35 -0300872 return 0;
873}
874
875static void coda_buf_queue(struct vb2_buffer *vb)
876{
877 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
878 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
879}
880
881static void coda_wait_prepare(struct vb2_queue *q)
882{
883 struct coda_ctx *ctx = vb2_get_drv_priv(q);
884 coda_unlock(ctx);
885}
886
887static void coda_wait_finish(struct vb2_queue *q)
888{
889 struct coda_ctx *ctx = vb2_get_drv_priv(q);
890 coda_lock(ctx);
891}
892
Philipp Zabelec25f682012-07-20 08:54:29 -0300893static void coda_free_framebuffers(struct coda_ctx *ctx)
894{
895 int i;
896
897 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++) {
898 if (ctx->internal_frames[i].vaddr) {
899 dma_free_coherent(&ctx->dev->plat_dev->dev,
900 ctx->internal_frames[i].size,
901 ctx->internal_frames[i].vaddr,
902 ctx->internal_frames[i].paddr);
903 ctx->internal_frames[i].vaddr = NULL;
904 }
905 }
906}
907
908static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
909{
910 struct coda_dev *dev = ctx->dev;
911
912 int height = q_data->height;
913 int width = q_data->width;
914 u32 *p;
915 int i;
916
917 /* Allocate frame buffers */
918 ctx->num_internal_frames = CODA_MAX_FRAMEBUFFERS;
919 for (i = 0; i < ctx->num_internal_frames; i++) {
920 ctx->internal_frames[i].size = q_data->sizeimage;
921 if (fourcc == V4L2_PIX_FMT_H264 && dev->devtype->product != CODA_DX6)
922 ctx->internal_frames[i].size += width / 2 * height / 2;
923 ctx->internal_frames[i].vaddr = dma_alloc_coherent(
924 &dev->plat_dev->dev, ctx->internal_frames[i].size,
925 &ctx->internal_frames[i].paddr, GFP_KERNEL);
926 if (!ctx->internal_frames[i].vaddr) {
927 coda_free_framebuffers(ctx);
928 return -ENOMEM;
929 }
930 }
931
932 /* Register frame buffers in the parameter buffer */
933 p = ctx->parabuf.vaddr;
934
935 if (dev->devtype->product == CODA_DX6) {
936 for (i = 0; i < ctx->num_internal_frames; i++) {
937 p[i * 3] = ctx->internal_frames[i].paddr; /* Y */
938 p[i * 3 + 1] = p[i * 3] + width * height; /* Cb */
939 p[i * 3 + 2] = p[i * 3 + 1] + width / 2 * height / 2; /* Cr */
940 }
941 } else {
942 for (i = 0; i < ctx->num_internal_frames; i += 2) {
943 p[i * 3 + 1] = ctx->internal_frames[i].paddr; /* Y */
944 p[i * 3] = p[i * 3 + 1] + width * height; /* Cb */
945 p[i * 3 + 3] = p[i * 3] + (width / 2) * (height / 2); /* Cr */
946
947 if (fourcc == V4L2_PIX_FMT_H264)
948 p[96 + i + 1] = p[i * 3 + 3] + (width / 2) * (height / 2);
949
950 if (i + 1 < ctx->num_internal_frames) {
951 p[i * 3 + 2] = ctx->internal_frames[i+1].paddr; /* Y */
952 p[i * 3 + 5] = p[i * 3 + 2] + width * height ; /* Cb */
953 p[i * 3 + 4] = p[i * 3 + 5] + (width / 2) * (height / 2); /* Cr */
954
955 if (fourcc == V4L2_PIX_FMT_H264)
956 p[96 + i] = p[i * 3 + 4] + (width / 2) * (height / 2);
957 }
958 }
959 }
960
961 return 0;
962}
963
Javier Martin3f3f5c72012-10-29 05:20:29 -0300964static int coda_h264_padding(int size, char *p)
965{
Javier Martin3f3f5c72012-10-29 05:20:29 -0300966 int nal_size;
967 int diff;
968
Javier Martin832fbb52012-10-29 08:34:59 -0300969 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -0300970 if (diff == 0)
971 return 0;
972
Javier Martin832fbb52012-10-29 08:34:59 -0300973 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -0300974 memcpy(p, coda_filler_nal, nal_size);
975
976 /* Add rbsp stop bit and trailing at the end */
977 *(p + nal_size - 1) = 0x80;
978
979 return nal_size;
980}
981
Javier Martin186b2502012-07-26 05:53:35 -0300982static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
983{
984 struct coda_ctx *ctx = vb2_get_drv_priv(q);
985 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
986 u32 bitstream_buf, bitstream_size;
987 struct coda_dev *dev = ctx->dev;
988 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -0300989 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300990 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300991 u32 value;
Philipp Zabelec25f682012-07-20 08:54:29 -0300992 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300993
994 if (count < 1)
995 return -EINVAL;
996
997 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
998 ctx->rawstreamon = 1;
999 else
1000 ctx->compstreamon = 1;
1001
1002 /* Don't start the coda unless both queues are on */
1003 if (!(ctx->rawstreamon & ctx->compstreamon))
1004 return 0;
1005
Philipp Zabel62bed142012-07-25 10:40:39 -03001006 if (coda_isbusy(dev))
1007 if (wait_for_completion_interruptible_timeout(&dev->done, HZ) <= 0)
1008 return -EBUSY;
1009
Javier Martin186b2502012-07-26 05:53:35 -03001010 ctx->gopcounter = ctx->params.gop_size - 1;
1011
1012 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1013 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1014 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1015 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1016 bitstream_size = q_data_dst->sizeimage;
1017 dst_fourcc = q_data_dst->fmt->fourcc;
1018
1019 /* Find out whether coda must encode or decode */
1020 if (q_data_src->fmt->type == CODA_FMT_RAW &&
1021 q_data_dst->fmt->type == CODA_FMT_ENC) {
1022 ctx->inst_type = CODA_INST_ENCODER;
1023 } else if (q_data_src->fmt->type == CODA_FMT_ENC &&
1024 q_data_dst->fmt->type == CODA_FMT_RAW) {
1025 ctx->inst_type = CODA_INST_DECODER;
1026 v4l2_err(v4l2_dev, "decoding not supported.\n");
1027 return -EINVAL;
1028 } else {
1029 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1030 return -EINVAL;
1031 }
1032
1033 if (!coda_is_initialized(dev)) {
1034 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1035 return -EFAULT;
1036 }
1037 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1038 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->idx));
1039 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->idx));
1040 switch (dev->devtype->product) {
1041 case CODA_DX6:
1042 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
1043 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1044 break;
1045 default:
1046 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
1047 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1048 }
1049
Philipp Zabel10436672012-07-02 09:03:55 -03001050 if (dev->devtype->product == CODA_DX6) {
1051 /* Configure the coda */
1052 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
1053 }
Javier Martin186b2502012-07-26 05:53:35 -03001054
1055 /* Could set rotation here if needed */
1056 switch (dev->devtype->product) {
1057 case CODA_DX6:
1058 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
1059 break;
1060 default:
1061 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
1062 }
1063 value |= (q_data_src->height & CODA_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
1064 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
1065 coda_write(dev, ctx->params.framerate,
1066 CODA_CMD_ENC_SEQ_SRC_F_RATE);
1067
1068 switch (dst_fourcc) {
1069 case V4L2_PIX_FMT_MPEG4:
1070 if (dev->devtype->product == CODA_DX6)
1071 ctx->params.codec_mode = CODADX6_MODE_ENCODE_MP4;
1072 else
1073 ctx->params.codec_mode = CODA7_MODE_ENCODE_MP4;
1074
1075 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
1076 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
1077 break;
1078 case V4L2_PIX_FMT_H264:
1079 if (dev->devtype->product == CODA_DX6)
1080 ctx->params.codec_mode = CODADX6_MODE_ENCODE_H264;
1081 else
1082 ctx->params.codec_mode = CODA7_MODE_ENCODE_H264;
1083
1084 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
1085 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
1086 break;
1087 default:
1088 v4l2_err(v4l2_dev,
1089 "dst format (0x%08x) invalid.\n", dst_fourcc);
1090 return -EINVAL;
1091 }
1092
Philipp Zabelc566c782012-08-08 11:59:38 -03001093 switch (ctx->params.slice_mode) {
1094 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
1095 value = 0;
1096 break;
1097 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
1098 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1099 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001100 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03001101 break;
1102 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1103 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1104 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1105 value |= 1 & CODA_SLICING_MODE_MASK;
1106 break;
1107 }
Javier Martin186b2502012-07-26 05:53:35 -03001108 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03001109 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03001110 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1111
1112 if (ctx->params.bitrate) {
1113 /* Rate control enabled */
1114 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1115 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1116 } else {
1117 value = 0;
1118 }
1119 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1120
1121 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1122 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1123
1124 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1125 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1126
1127 /* set default gamma */
1128 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1129 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1130
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03001131 if (CODA_DEFAULT_GAMMA > 0) {
1132 if (dev->devtype->product == CODA_DX6)
1133 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
1134 else
1135 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
1136 } else {
1137 value = 0;
1138 }
Javier Martin186b2502012-07-26 05:53:35 -03001139 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1140
1141 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1142 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1143 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1144 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
Philipp Zabel10436672012-07-02 09:03:55 -03001145 if (dev->devtype->product == CODA_DX6) {
1146 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1147 } else {
1148 coda_write(dev, dev->iram_paddr, CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1149 coda_write(dev, 48 * 1024, CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
1150 }
Javier Martin186b2502012-07-26 05:53:35 -03001151 }
1152
1153 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1154 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1155 return -ETIMEDOUT;
1156 }
1157
1158 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0)
1159 return -EFAULT;
1160
Philipp Zabelec25f682012-07-20 08:54:29 -03001161 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
1162 if (ret < 0)
1163 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001164
Philipp Zabelec25f682012-07-20 08:54:29 -03001165 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03001166 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
1167 if (dev->devtype->product != CODA_DX6) {
1168 coda_write(dev, round_up(q_data_src->width, 8), CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1169 coda_write(dev, dev->iram_paddr + 48 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1170 coda_write(dev, dev->iram_paddr + 53 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1171 coda_write(dev, dev->iram_paddr + 58 * 1024, CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1172 coda_write(dev, dev->iram_paddr + 68 * 1024, CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1173 coda_write(dev, 0x0, CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1174 }
Javier Martin186b2502012-07-26 05:53:35 -03001175 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1176 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1177 return -ETIMEDOUT;
1178 }
1179
1180 /* Save stream headers */
1181 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1182 switch (dst_fourcc) {
1183 case V4L2_PIX_FMT_H264:
1184 /*
1185 * Get SPS in the first frame and copy it to an
1186 * intermediate buffer.
1187 */
1188 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1189 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1190 coda_write(dev, CODA_HEADER_H264_SPS, CODA_CMD_ENC_HEADER_CODE);
1191 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1192 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1193 return -ETIMEDOUT;
1194 }
1195 ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1196 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1197 memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0),
1198 ctx->vpu_header_size[0]);
1199
1200 /*
1201 * Get PPS in the first frame and copy it to an
1202 * intermediate buffer.
1203 */
1204 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1205 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1206 coda_write(dev, CODA_HEADER_H264_PPS, CODA_CMD_ENC_HEADER_CODE);
1207 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1208 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1209 return -ETIMEDOUT;
1210 }
1211 ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1212 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1213 memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0),
1214 ctx->vpu_header_size[1]);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001215 /*
1216 * Length of H.264 headers is variable and thus it might not be
1217 * aligned for the coda to append the encoded frame. In that is
1218 * the case a filler NAL must be added to header 2.
1219 */
1220 ctx->vpu_header_size[2] = coda_h264_padding(
1221 (ctx->vpu_header_size[0] +
1222 ctx->vpu_header_size[1]),
1223 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03001224 break;
1225 case V4L2_PIX_FMT_MPEG4:
1226 /*
1227 * Get VOS in the first frame and copy it to an
1228 * intermediate buffer
1229 */
1230 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1231 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1232 coda_write(dev, CODA_HEADER_MP4V_VOS, CODA_CMD_ENC_HEADER_CODE);
1233 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1234 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1235 return -ETIMEDOUT;
1236 }
1237 ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1238 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1239 memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0),
1240 ctx->vpu_header_size[0]);
1241
1242 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1243 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1244 coda_write(dev, CODA_HEADER_MP4V_VIS, CODA_CMD_ENC_HEADER_CODE);
1245 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1246 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n");
1247 return -ETIMEDOUT;
1248 }
1249 ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1250 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1251 memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0),
1252 ctx->vpu_header_size[1]);
1253
1254 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1255 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1256 coda_write(dev, CODA_HEADER_MP4V_VOL, CODA_CMD_ENC_HEADER_CODE);
1257 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1258 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n");
1259 return -ETIMEDOUT;
1260 }
1261 ctx->vpu_header_size[2] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1262 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1263 memcpy(&ctx->vpu_header[2][0], vb2_plane_vaddr(buf, 0),
1264 ctx->vpu_header_size[2]);
1265 break;
1266 default:
1267 /* No more formats need to save headers at the moment */
1268 break;
1269 }
1270
1271 return 0;
1272}
1273
1274static int coda_stop_streaming(struct vb2_queue *q)
1275{
1276 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001277 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03001278
1279 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1280 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1281 "%s: output\n", __func__);
1282 ctx->rawstreamon = 0;
1283 } else {
1284 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1285 "%s: capture\n", __func__);
1286 ctx->compstreamon = 0;
1287 }
1288
Philipp Zabel62bed142012-07-25 10:40:39 -03001289 /* Don't stop the coda unless both queues are off */
1290 if (ctx->rawstreamon || ctx->compstreamon)
1291 return 0;
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001292
Philipp Zabel62bed142012-07-25 10:40:39 -03001293 if (coda_isbusy(dev)) {
1294 if (wait_for_completion_interruptible_timeout(&dev->done, HZ) <= 0) {
1295 v4l2_warn(&dev->v4l2_dev,
1296 "%s: timeout, sending SEQ_END anyway\n", __func__);
Javier Martin186b2502012-07-26 05:53:35 -03001297 }
1298 }
1299
Philipp Zabel62bed142012-07-25 10:40:39 -03001300 cancel_delayed_work(&dev->timeout);
1301
1302 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1303 "%s: sent command 'SEQ_END' to coda\n", __func__);
1304 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1305 v4l2_err(&dev->v4l2_dev,
1306 "CODA_COMMAND_SEQ_END failed\n");
1307 return -ETIMEDOUT;
1308 }
1309
1310 coda_free_framebuffers(ctx);
1311
Javier Martin186b2502012-07-26 05:53:35 -03001312 return 0;
1313}
1314
1315static struct vb2_ops coda_qops = {
1316 .queue_setup = coda_queue_setup,
1317 .buf_prepare = coda_buf_prepare,
1318 .buf_queue = coda_buf_queue,
1319 .wait_prepare = coda_wait_prepare,
1320 .wait_finish = coda_wait_finish,
1321 .start_streaming = coda_start_streaming,
1322 .stop_streaming = coda_stop_streaming,
1323};
1324
1325static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1326{
1327 struct coda_ctx *ctx =
1328 container_of(ctrl->handler, struct coda_ctx, ctrls);
1329
1330 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1331 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1332
1333 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001334 case V4L2_CID_HFLIP:
1335 if (ctrl->val)
1336 ctx->params.rot_mode |= CODA_MIR_HOR;
1337 else
1338 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1339 break;
1340 case V4L2_CID_VFLIP:
1341 if (ctrl->val)
1342 ctx->params.rot_mode |= CODA_MIR_VER;
1343 else
1344 ctx->params.rot_mode &= ~CODA_MIR_VER;
1345 break;
Javier Martin186b2502012-07-26 05:53:35 -03001346 case V4L2_CID_MPEG_VIDEO_BITRATE:
1347 ctx->params.bitrate = ctrl->val / 1000;
1348 break;
1349 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1350 ctx->params.gop_size = ctrl->val;
1351 break;
1352 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1353 ctx->params.h264_intra_qp = ctrl->val;
1354 break;
1355 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1356 ctx->params.h264_inter_qp = ctrl->val;
1357 break;
1358 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1359 ctx->params.mpeg4_intra_qp = ctrl->val;
1360 break;
1361 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1362 ctx->params.mpeg4_inter_qp = ctrl->val;
1363 break;
1364 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1365 ctx->params.slice_mode = ctrl->val;
1366 break;
1367 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1368 ctx->params.slice_max_mb = ctrl->val;
1369 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001370 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1371 ctx->params.slice_max_bits = ctrl->val * 8;
1372 break;
Javier Martin186b2502012-07-26 05:53:35 -03001373 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1374 break;
1375 default:
1376 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1377 "Invalid control, id=%d, val=%d\n",
1378 ctrl->id, ctrl->val);
1379 return -EINVAL;
1380 }
1381
1382 return 0;
1383}
1384
1385static struct v4l2_ctrl_ops coda_ctrl_ops = {
1386 .s_ctrl = coda_s_ctrl,
1387};
1388
1389static int coda_ctrls_setup(struct coda_ctx *ctx)
1390{
1391 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1392
1393 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001394 V4L2_CID_HFLIP, 0, 1, 1, 0);
1395 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1396 V4L2_CID_VFLIP, 0, 1, 1, 0);
1397 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001398 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1399 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1400 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1401 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1402 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1403 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1404 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1405 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1406 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1407 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1408 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1409 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1410 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001411 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1412 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001413 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1414 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001415 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1416 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001417 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1418 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1419 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1420 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1421 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1422
1423 if (ctx->ctrls.error) {
1424 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1425 ctx->ctrls.error);
1426 return -EINVAL;
1427 }
1428
1429 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1430}
1431
1432static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1433 struct vb2_queue *dst_vq)
1434{
1435 struct coda_ctx *ctx = priv;
1436 int ret;
1437
Javier Martin186b2502012-07-26 05:53:35 -03001438 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03001439 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001440 src_vq->drv_priv = ctx;
1441 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1442 src_vq->ops = &coda_qops;
1443 src_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001444 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001445
1446 ret = vb2_queue_init(src_vq);
1447 if (ret)
1448 return ret;
1449
Javier Martin186b2502012-07-26 05:53:35 -03001450 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03001451 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001452 dst_vq->drv_priv = ctx;
1453 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1454 dst_vq->ops = &coda_qops;
1455 dst_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001456 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001457
1458 return vb2_queue_init(dst_vq);
1459}
1460
Philipp Zabele11f3e62012-07-25 09:16:58 -03001461static int coda_next_free_instance(struct coda_dev *dev)
1462{
1463 return ffz(dev->instance_mask);
1464}
1465
Javier Martin186b2502012-07-26 05:53:35 -03001466static int coda_open(struct file *file)
1467{
1468 struct coda_dev *dev = video_drvdata(file);
1469 struct coda_ctx *ctx = NULL;
1470 int ret = 0;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001471 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001472
Philipp Zabele11f3e62012-07-25 09:16:58 -03001473 idx = coda_next_free_instance(dev);
1474 if (idx >= CODA_MAX_INSTANCES)
Javier Martin186b2502012-07-26 05:53:35 -03001475 return -EBUSY;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001476 set_bit(idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001477
1478 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1479 if (!ctx)
1480 return -ENOMEM;
1481
1482 v4l2_fh_init(&ctx->fh, video_devdata(file));
1483 file->private_data = &ctx->fh;
1484 v4l2_fh_add(&ctx->fh);
1485 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001486 ctx->idx = idx;
Javier Martin186b2502012-07-26 05:53:35 -03001487
1488 set_default_params(ctx);
1489 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1490 &coda_queue_init);
1491 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03001492 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001493
1494 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1495 __func__, ret);
1496 goto err;
1497 }
1498 ret = coda_ctrls_setup(ctx);
1499 if (ret) {
1500 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1501 goto err;
1502 }
1503
1504 ctx->fh.ctrl_handler = &ctx->ctrls;
1505
1506 ctx->parabuf.vaddr = dma_alloc_coherent(&dev->plat_dev->dev,
1507 CODA_PARA_BUF_SIZE, &ctx->parabuf.paddr, GFP_KERNEL);
1508 if (!ctx->parabuf.vaddr) {
1509 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1510 ret = -ENOMEM;
1511 goto err;
1512 }
1513
1514 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001515 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001516 coda_unlock(ctx);
1517
1518 clk_prepare_enable(dev->clk_per);
1519 clk_prepare_enable(dev->clk_ahb);
1520
1521 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1522 ctx->idx, ctx);
1523
1524 return 0;
1525
1526err:
1527 v4l2_fh_del(&ctx->fh);
1528 v4l2_fh_exit(&ctx->fh);
1529 kfree(ctx);
1530 return ret;
1531}
1532
1533static int coda_release(struct file *file)
1534{
1535 struct coda_dev *dev = video_drvdata(file);
1536 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1537
1538 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1539 ctx);
1540
1541 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001542 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001543 coda_unlock(ctx);
1544
1545 dma_free_coherent(&dev->plat_dev->dev, CODA_PARA_BUF_SIZE,
1546 ctx->parabuf.vaddr, ctx->parabuf.paddr);
1547 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1548 v4l2_ctrl_handler_free(&ctx->ctrls);
1549 clk_disable_unprepare(dev->clk_per);
1550 clk_disable_unprepare(dev->clk_ahb);
1551 v4l2_fh_del(&ctx->fh);
1552 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001553 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001554 kfree(ctx);
1555
1556 return 0;
1557}
1558
1559static unsigned int coda_poll(struct file *file,
1560 struct poll_table_struct *wait)
1561{
1562 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1563 int ret;
1564
1565 coda_lock(ctx);
1566 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1567 coda_unlock(ctx);
1568 return ret;
1569}
1570
1571static int coda_mmap(struct file *file, struct vm_area_struct *vma)
1572{
1573 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1574
1575 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1576}
1577
1578static const struct v4l2_file_operations coda_fops = {
1579 .owner = THIS_MODULE,
1580 .open = coda_open,
1581 .release = coda_release,
1582 .poll = coda_poll,
1583 .unlocked_ioctl = video_ioctl2,
1584 .mmap = coda_mmap,
1585};
1586
1587static irqreturn_t coda_irq_handler(int irq, void *data)
1588{
Philipp Zabelec25f682012-07-20 08:54:29 -03001589 struct vb2_buffer *src_buf, *dst_buf;
Javier Martin186b2502012-07-26 05:53:35 -03001590 struct coda_dev *dev = data;
1591 u32 wr_ptr, start_ptr;
1592 struct coda_ctx *ctx;
1593
Fabio Estevam2249b452012-10-09 23:33:29 -03001594 cancel_delayed_work(&dev->timeout);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001595
Javier Martin186b2502012-07-26 05:53:35 -03001596 /* read status register to attend the IRQ */
1597 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1598 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1599 CODA_REG_BIT_INT_CLEAR);
1600
1601 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1602 if (ctx == NULL) {
1603 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
1604 return IRQ_HANDLED;
1605 }
1606
1607 if (ctx->aborting) {
1608 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1609 "task has been aborted\n");
1610 return IRQ_HANDLED;
1611 }
1612
1613 if (coda_isbusy(ctx->dev)) {
1614 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1615 "coda is still busy!!!!\n");
1616 return IRQ_NONE;
1617 }
1618
Philipp Zabel62bed142012-07-25 10:40:39 -03001619 complete(&dev->done);
1620
Philipp Zabelec25f682012-07-20 08:54:29 -03001621 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1622 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001623
1624 /* Get results from the coda */
1625 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
1626 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1627 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx));
1628 /* Calculate bytesused field */
1629 if (dst_buf->v4l2_buf.sequence == 0) {
1630 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr) +
1631 ctx->vpu_header_size[0] +
1632 ctx->vpu_header_size[1] +
1633 ctx->vpu_header_size[2];
1634 } else {
1635 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr);
1636 }
1637
1638 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1639 wr_ptr - start_ptr);
1640
1641 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1642 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1643
1644 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1645 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1646 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1647 } else {
1648 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1649 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1650 }
1651
Kamil Debskiccd571c2013-04-24 10:38:24 -03001652 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1653 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1654
Philipp Zabelec25f682012-07-20 08:54:29 -03001655 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03001656 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1657
1658 ctx->gopcounter--;
1659 if (ctx->gopcounter < 0)
1660 ctx->gopcounter = ctx->params.gop_size - 1;
1661
1662 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1663 "job finished: encoding frame (%d) (%s)\n",
1664 dst_buf->v4l2_buf.sequence,
1665 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1666 "KEYFRAME" : "PFRAME");
1667
1668 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
1669
1670 return IRQ_HANDLED;
1671}
1672
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001673static void coda_timeout(struct work_struct *work)
1674{
1675 struct coda_ctx *ctx;
1676 struct coda_dev *dev = container_of(to_delayed_work(work),
1677 struct coda_dev, timeout);
1678
Philipp Zabel62bed142012-07-25 10:40:39 -03001679 if (completion_done(&dev->done))
1680 return;
1681
1682 complete(&dev->done);
1683
Philipp Zabel39cc0292013-05-21 10:32:42 -03001684 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001685
1686 mutex_lock(&dev->dev_mutex);
1687 list_for_each_entry(ctx, &dev->instances, list) {
1688 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1689 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1690 }
1691 mutex_unlock(&dev->dev_mutex);
1692}
1693
Javier Martin186b2502012-07-26 05:53:35 -03001694static u32 coda_supported_firmwares[] = {
1695 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001696 CODA_FIRMWARE_VERNUM(CODA_7541, 13, 4, 29),
Javier Martin186b2502012-07-26 05:53:35 -03001697};
1698
1699static bool coda_firmware_supported(u32 vernum)
1700{
1701 int i;
1702
1703 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
1704 if (vernum == coda_supported_firmwares[i])
1705 return true;
1706 return false;
1707}
1708
1709static char *coda_product_name(int product)
1710{
1711 static char buf[9];
1712
1713 switch (product) {
1714 case CODA_DX6:
1715 return "CodaDx6";
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001716 case CODA_7541:
1717 return "CODA7541";
Javier Martin186b2502012-07-26 05:53:35 -03001718 default:
1719 snprintf(buf, sizeof(buf), "(0x%04x)", product);
1720 return buf;
1721 }
1722}
1723
Philipp Zabel87048bb2012-07-02 07:03:43 -03001724static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001725{
1726 u16 product, major, minor, release;
1727 u32 data;
1728 u16 *p;
1729 int i;
1730
1731 clk_prepare_enable(dev->clk_per);
1732 clk_prepare_enable(dev->clk_ahb);
1733
Javier Martin186b2502012-07-26 05:53:35 -03001734 /*
1735 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001736 * The 16-bit chars in the code buffer are in memory access
1737 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001738 * Data in this SRAM survives a reboot.
1739 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001740 p = (u16 *)dev->codebuf.vaddr;
1741 if (dev->devtype->product == CODA_DX6) {
1742 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1743 data = CODA_DOWN_ADDRESS_SET(i) |
1744 CODA_DOWN_DATA_SET(p[i ^ 1]);
1745 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1746 }
1747 } else {
1748 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1749 data = CODA_DOWN_ADDRESS_SET(i) |
1750 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1751 3 - (i % 4)]);
1752 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1753 }
Javier Martin186b2502012-07-26 05:53:35 -03001754 }
Javier Martin186b2502012-07-26 05:53:35 -03001755
Philipp Zabel9acf7692013-05-23 10:42:56 -03001756 /* Clear registers */
1757 for (i = 0; i < 64; i++)
1758 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1759
Javier Martin186b2502012-07-26 05:53:35 -03001760 /* Tell the BIT where to find everything it needs */
1761 coda_write(dev, dev->workbuf.paddr,
1762 CODA_REG_BIT_WORK_BUF_ADDR);
1763 coda_write(dev, dev->codebuf.paddr,
1764 CODA_REG_BIT_CODE_BUF_ADDR);
1765 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1766
1767 /* Set default values */
1768 switch (dev->devtype->product) {
1769 case CODA_DX6:
1770 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1771 break;
1772 default:
1773 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1774 }
1775 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001776
1777 if (dev->devtype->product != CODA_DX6)
1778 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1779
Javier Martin186b2502012-07-26 05:53:35 -03001780 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1781 CODA_REG_BIT_INT_ENABLE);
1782
1783 /* Reset VPU and start processor */
1784 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1785 data |= CODA_REG_RESET_ENABLE;
1786 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1787 udelay(10);
1788 data &= ~CODA_REG_RESET_ENABLE;
1789 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1790 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1791
1792 /* Load firmware */
1793 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
1794 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
1795 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
1796 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
1797 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
1798 if (coda_wait_timeout(dev)) {
1799 clk_disable_unprepare(dev->clk_per);
1800 clk_disable_unprepare(dev->clk_ahb);
1801 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
1802 return -EIO;
1803 }
1804
1805 /* Check we are compatible with the loaded firmware */
1806 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
1807 product = CODA_FIRMWARE_PRODUCT(data);
1808 major = CODA_FIRMWARE_MAJOR(data);
1809 minor = CODA_FIRMWARE_MINOR(data);
1810 release = CODA_FIRMWARE_RELEASE(data);
1811
1812 clk_disable_unprepare(dev->clk_per);
1813 clk_disable_unprepare(dev->clk_ahb);
1814
1815 if (product != dev->devtype->product) {
1816 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
1817 " Version: %u.%u.%u\n",
1818 coda_product_name(dev->devtype->product),
1819 coda_product_name(product), major, minor, release);
1820 return -EINVAL;
1821 }
1822
1823 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
1824 coda_product_name(product));
1825
1826 if (coda_firmware_supported(data)) {
1827 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
1828 major, minor, release);
1829 } else {
1830 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
1831 "%u.%u.%u\n", major, minor, release);
1832 }
1833
1834 return 0;
1835}
1836
1837static void coda_fw_callback(const struct firmware *fw, void *context)
1838{
1839 struct coda_dev *dev = context;
1840 struct platform_device *pdev = dev->plat_dev;
1841 int ret;
1842
1843 if (!fw) {
1844 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1845 return;
1846 }
1847
1848 /* allocate auxiliary per-device code buffer for the BIT processor */
1849 dev->codebuf.size = fw->size;
1850 dev->codebuf.vaddr = dma_alloc_coherent(&pdev->dev, fw->size,
1851 &dev->codebuf.paddr,
1852 GFP_KERNEL);
1853 if (!dev->codebuf.vaddr) {
1854 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1855 return;
1856 }
1857
Philipp Zabel87048bb2012-07-02 07:03:43 -03001858 /* Copy the whole firmware image to the code buffer */
1859 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1860 release_firmware(fw);
1861
1862 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03001863 if (ret) {
1864 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1865 return;
1866 }
1867
1868 dev->vfd.fops = &coda_fops,
1869 dev->vfd.ioctl_ops = &coda_ioctl_ops;
1870 dev->vfd.release = video_device_release_empty,
1871 dev->vfd.lock = &dev->dev_mutex;
1872 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03001873 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03001874 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
1875 video_set_drvdata(&dev->vfd, dev);
1876
1877 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1878 if (IS_ERR(dev->alloc_ctx)) {
1879 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1880 return;
1881 }
1882
1883 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1884 if (IS_ERR(dev->m2m_dev)) {
1885 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1886 goto rel_ctx;
1887 }
1888
1889 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
1890 if (ret) {
1891 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1892 goto rel_m2m;
1893 }
1894 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
1895 dev->vfd.num);
1896
1897 return;
1898
1899rel_m2m:
1900 v4l2_m2m_release(dev->m2m_dev);
1901rel_ctx:
1902 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1903}
1904
1905static int coda_firmware_request(struct coda_dev *dev)
1906{
1907 char *fw = dev->devtype->firmware;
1908
1909 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1910 coda_product_name(dev->devtype->product));
1911
1912 return request_firmware_nowait(THIS_MODULE, true,
1913 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1914}
1915
1916enum coda_platform {
1917 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001918 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03001919};
1920
Emil Goodec06d8752012-08-14 17:44:42 -03001921static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001922 [CODA_IMX27] = {
1923 .firmware = "v4l-codadx6-imx27.bin",
1924 .product = CODA_DX6,
1925 .formats = codadx6_formats,
1926 .num_formats = ARRAY_SIZE(codadx6_formats),
1927 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001928 [CODA_IMX53] = {
1929 .firmware = "v4l-coda7541-imx53.bin",
1930 .product = CODA_7541,
1931 .formats = coda7_formats,
1932 .num_formats = ARRAY_SIZE(coda7_formats),
1933 },
Javier Martin186b2502012-07-26 05:53:35 -03001934};
1935
1936static struct platform_device_id coda_platform_ids[] = {
1937 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001938 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001939 { /* sentinel */ }
1940};
1941MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1942
1943#ifdef CONFIG_OF
1944static const struct of_device_id coda_dt_ids[] = {
1945 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001946 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03001947 { /* sentinel */ }
1948};
1949MODULE_DEVICE_TABLE(of, coda_dt_ids);
1950#endif
1951
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001952static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001953{
1954 const struct of_device_id *of_id =
1955 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1956 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001957 struct coda_platform_data *pdata = pdev->dev.platform_data;
1958 struct device_node *np = pdev->dev.of_node;
1959 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001960 struct coda_dev *dev;
1961 struct resource *res;
1962 int ret, irq;
1963
1964 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1965 if (!dev) {
1966 dev_err(&pdev->dev, "Not enough memory for %s\n",
1967 CODA_NAME);
1968 return -ENOMEM;
1969 }
1970
1971 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001972 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001973 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Philipp Zabel62bed142012-07-25 10:40:39 -03001974 init_completion(&dev->done);
1975 complete(&dev->done);
Javier Martin186b2502012-07-26 05:53:35 -03001976
1977 dev->plat_dev = pdev;
1978 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1979 if (IS_ERR(dev->clk_per)) {
1980 dev_err(&pdev->dev, "Could not get per clock\n");
1981 return PTR_ERR(dev->clk_per);
1982 }
1983
1984 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1985 if (IS_ERR(dev->clk_ahb)) {
1986 dev_err(&pdev->dev, "Could not get ahb clock\n");
1987 return PTR_ERR(dev->clk_ahb);
1988 }
1989
1990 /* Get memory for physical registers */
1991 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1992 if (res == NULL) {
1993 dev_err(&pdev->dev, "failed to get memory region resource\n");
1994 return -ENOENT;
1995 }
1996
Philipp Zabel611cbbf2013-05-21 04:19:27 -03001997 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1998 if (IS_ERR(dev->regs_base))
1999 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002000
2001 /* IRQ */
2002 irq = platform_get_irq(pdev, 0);
2003 if (irq < 0) {
2004 dev_err(&pdev->dev, "failed to get irq resource\n");
2005 return -ENOENT;
2006 }
2007
2008 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
2009 0, CODA_NAME, dev) < 0) {
2010 dev_err(&pdev->dev, "failed to request irq\n");
2011 return -ENOENT;
2012 }
2013
Philipp Zabel657eee72013-04-29 16:17:14 -07002014 /* Get IRAM pool from device tree or platform data */
2015 pool = of_get_named_gen_pool(np, "iram", 0);
2016 if (!pool && pdata)
2017 pool = dev_get_gen_pool(pdata->iram_dev);
2018 if (!pool) {
2019 dev_err(&pdev->dev, "iram pool not available\n");
2020 return -ENOMEM;
2021 }
2022 dev->iram_pool = pool;
2023
Javier Martin186b2502012-07-26 05:53:35 -03002024 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2025 if (ret)
2026 return ret;
2027
2028 mutex_init(&dev->dev_mutex);
2029
2030 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2031
2032 if (of_id) {
2033 dev->devtype = of_id->data;
2034 } else if (pdev_id) {
2035 dev->devtype = &coda_devdata[pdev_id->driver_data];
2036 } else {
2037 v4l2_device_unregister(&dev->v4l2_dev);
2038 return -EINVAL;
2039 }
2040
2041 /* allocate auxiliary per-device buffers for the BIT processor */
2042 switch (dev->devtype->product) {
2043 case CODA_DX6:
2044 dev->workbuf.size = CODADX6_WORK_BUF_SIZE;
2045 break;
2046 default:
2047 dev->workbuf.size = CODA7_WORK_BUF_SIZE;
2048 }
2049 dev->workbuf.vaddr = dma_alloc_coherent(&pdev->dev, dev->workbuf.size,
2050 &dev->workbuf.paddr,
2051 GFP_KERNEL);
2052 if (!dev->workbuf.vaddr) {
2053 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2054 v4l2_device_unregister(&dev->v4l2_dev);
2055 return -ENOMEM;
2056 }
2057
Philipp Zabel657eee72013-04-29 16:17:14 -07002058 if (dev->devtype->product == CODA_DX6)
2059 dev->iram_size = CODADX6_IRAM_SIZE;
2060 else
2061 dev->iram_size = CODA7_IRAM_SIZE;
2062 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
2063 if (!dev->iram_vaddr) {
2064 dev_err(&pdev->dev, "unable to alloc iram\n");
2065 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03002066 }
Philipp Zabel657eee72013-04-29 16:17:14 -07002067 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
2068 dev->iram_vaddr);
Philipp Zabel10436672012-07-02 09:03:55 -03002069
Javier Martin186b2502012-07-26 05:53:35 -03002070 platform_set_drvdata(pdev, dev);
2071
2072 return coda_firmware_request(dev);
2073}
2074
2075static int coda_remove(struct platform_device *pdev)
2076{
2077 struct coda_dev *dev = platform_get_drvdata(pdev);
2078
2079 video_unregister_device(&dev->vfd);
2080 if (dev->m2m_dev)
2081 v4l2_m2m_release(dev->m2m_dev);
2082 if (dev->alloc_ctx)
2083 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2084 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002085 if (dev->iram_vaddr)
2086 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Javier Martin186b2502012-07-26 05:53:35 -03002087 if (dev->codebuf.vaddr)
2088 dma_free_coherent(&pdev->dev, dev->codebuf.size,
2089 &dev->codebuf.vaddr, dev->codebuf.paddr);
2090 if (dev->workbuf.vaddr)
2091 dma_free_coherent(&pdev->dev, dev->workbuf.size, &dev->workbuf.vaddr,
2092 dev->workbuf.paddr);
2093 return 0;
2094}
2095
2096static struct platform_driver coda_driver = {
2097 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002098 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002099 .driver = {
2100 .name = CODA_NAME,
2101 .owner = THIS_MODULE,
2102 .of_match_table = of_match_ptr(coda_dt_ids),
2103 },
2104 .id_table = coda_platform_ids,
2105};
2106
2107module_platform_driver(coda_driver);
2108
2109MODULE_LICENSE("GPL");
2110MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2111MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");