blob: 366318d87887ac6762979c3029193cea533f74aa [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
Philipp Zabel86eda902013-05-23 10:42:57 -0300908static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
909{
910 struct coda_dev *dev = ctx->dev;
911 u32 *p = ctx->parabuf.vaddr;
912
913 if (dev->devtype->product == CODA_DX6)
914 p[index] = value;
915 else
916 p[index ^ 1] = value;
917}
918
Philipp Zabelec25f682012-07-20 08:54:29 -0300919static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
920{
921 struct coda_dev *dev = ctx->dev;
922
923 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -0300924 dma_addr_t paddr;
925 int ysize;
Philipp Zabelec25f682012-07-20 08:54:29 -0300926 int i;
927
Philipp Zabel86eda902013-05-23 10:42:57 -0300928 ysize = round_up(q_data->width, 8) * height;
929
Philipp Zabelec25f682012-07-20 08:54:29 -0300930 /* Allocate frame buffers */
931 ctx->num_internal_frames = CODA_MAX_FRAMEBUFFERS;
932 for (i = 0; i < ctx->num_internal_frames; i++) {
933 ctx->internal_frames[i].size = q_data->sizeimage;
934 if (fourcc == V4L2_PIX_FMT_H264 && dev->devtype->product != CODA_DX6)
Philipp Zabel86eda902013-05-23 10:42:57 -0300935 ctx->internal_frames[i].size += ysize/4;
Philipp Zabelec25f682012-07-20 08:54:29 -0300936 ctx->internal_frames[i].vaddr = dma_alloc_coherent(
937 &dev->plat_dev->dev, ctx->internal_frames[i].size,
938 &ctx->internal_frames[i].paddr, GFP_KERNEL);
939 if (!ctx->internal_frames[i].vaddr) {
940 coda_free_framebuffers(ctx);
941 return -ENOMEM;
942 }
943 }
944
945 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -0300946 for (i = 0; i < ctx->num_internal_frames; i++) {
947 paddr = ctx->internal_frames[i].paddr;
948 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
949 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
950 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -0300951
Philipp Zabel86eda902013-05-23 10:42:57 -0300952 if (dev->devtype->product != CODA_DX6 && fourcc == V4L2_PIX_FMT_H264)
953 coda_parabuf_write(ctx, 96 + i, ctx->internal_frames[i].paddr + ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -0300954 }
955
956 return 0;
957}
958
Javier Martin3f3f5c72012-10-29 05:20:29 -0300959static int coda_h264_padding(int size, char *p)
960{
Javier Martin3f3f5c72012-10-29 05:20:29 -0300961 int nal_size;
962 int diff;
963
Javier Martin832fbb52012-10-29 08:34:59 -0300964 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -0300965 if (diff == 0)
966 return 0;
967
Javier Martin832fbb52012-10-29 08:34:59 -0300968 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -0300969 memcpy(p, coda_filler_nal, nal_size);
970
971 /* Add rbsp stop bit and trailing at the end */
972 *(p + nal_size - 1) = 0x80;
973
974 return nal_size;
975}
976
Javier Martin186b2502012-07-26 05:53:35 -0300977static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
978{
979 struct coda_ctx *ctx = vb2_get_drv_priv(q);
980 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
981 u32 bitstream_buf, bitstream_size;
982 struct coda_dev *dev = ctx->dev;
983 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -0300984 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300985 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300986 u32 value;
Philipp Zabelec25f682012-07-20 08:54:29 -0300987 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300988
989 if (count < 1)
990 return -EINVAL;
991
992 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
993 ctx->rawstreamon = 1;
994 else
995 ctx->compstreamon = 1;
996
997 /* Don't start the coda unless both queues are on */
998 if (!(ctx->rawstreamon & ctx->compstreamon))
999 return 0;
1000
Philipp Zabel62bed142012-07-25 10:40:39 -03001001 if (coda_isbusy(dev))
1002 if (wait_for_completion_interruptible_timeout(&dev->done, HZ) <= 0)
1003 return -EBUSY;
1004
Javier Martin186b2502012-07-26 05:53:35 -03001005 ctx->gopcounter = ctx->params.gop_size - 1;
1006
1007 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1008 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1009 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1010 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1011 bitstream_size = q_data_dst->sizeimage;
1012 dst_fourcc = q_data_dst->fmt->fourcc;
1013
1014 /* Find out whether coda must encode or decode */
1015 if (q_data_src->fmt->type == CODA_FMT_RAW &&
1016 q_data_dst->fmt->type == CODA_FMT_ENC) {
1017 ctx->inst_type = CODA_INST_ENCODER;
1018 } else if (q_data_src->fmt->type == CODA_FMT_ENC &&
1019 q_data_dst->fmt->type == CODA_FMT_RAW) {
1020 ctx->inst_type = CODA_INST_DECODER;
1021 v4l2_err(v4l2_dev, "decoding not supported.\n");
1022 return -EINVAL;
1023 } else {
1024 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1025 return -EINVAL;
1026 }
1027
1028 if (!coda_is_initialized(dev)) {
1029 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1030 return -EFAULT;
1031 }
1032 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1033 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->idx));
1034 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->idx));
1035 switch (dev->devtype->product) {
1036 case CODA_DX6:
1037 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
1038 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1039 break;
1040 default:
1041 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
1042 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1043 }
1044
Philipp Zabel10436672012-07-02 09:03:55 -03001045 if (dev->devtype->product == CODA_DX6) {
1046 /* Configure the coda */
1047 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
1048 }
Javier Martin186b2502012-07-26 05:53:35 -03001049
1050 /* Could set rotation here if needed */
1051 switch (dev->devtype->product) {
1052 case CODA_DX6:
1053 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
1054 break;
1055 default:
1056 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
1057 }
1058 value |= (q_data_src->height & CODA_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
1059 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
1060 coda_write(dev, ctx->params.framerate,
1061 CODA_CMD_ENC_SEQ_SRC_F_RATE);
1062
1063 switch (dst_fourcc) {
1064 case V4L2_PIX_FMT_MPEG4:
1065 if (dev->devtype->product == CODA_DX6)
1066 ctx->params.codec_mode = CODADX6_MODE_ENCODE_MP4;
1067 else
1068 ctx->params.codec_mode = CODA7_MODE_ENCODE_MP4;
1069
1070 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
1071 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
1072 break;
1073 case V4L2_PIX_FMT_H264:
1074 if (dev->devtype->product == CODA_DX6)
1075 ctx->params.codec_mode = CODADX6_MODE_ENCODE_H264;
1076 else
1077 ctx->params.codec_mode = CODA7_MODE_ENCODE_H264;
1078
1079 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
1080 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
1081 break;
1082 default:
1083 v4l2_err(v4l2_dev,
1084 "dst format (0x%08x) invalid.\n", dst_fourcc);
1085 return -EINVAL;
1086 }
1087
Philipp Zabelc566c782012-08-08 11:59:38 -03001088 switch (ctx->params.slice_mode) {
1089 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
1090 value = 0;
1091 break;
1092 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
1093 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1094 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001095 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03001096 break;
1097 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1098 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1099 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1100 value |= 1 & CODA_SLICING_MODE_MASK;
1101 break;
1102 }
Javier Martin186b2502012-07-26 05:53:35 -03001103 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03001104 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03001105 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1106
1107 if (ctx->params.bitrate) {
1108 /* Rate control enabled */
1109 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1110 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1111 } else {
1112 value = 0;
1113 }
1114 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1115
1116 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1117 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1118
1119 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1120 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1121
1122 /* set default gamma */
1123 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1124 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1125
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03001126 if (CODA_DEFAULT_GAMMA > 0) {
1127 if (dev->devtype->product == CODA_DX6)
1128 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
1129 else
1130 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
1131 } else {
1132 value = 0;
1133 }
Javier Martin186b2502012-07-26 05:53:35 -03001134 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1135
1136 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1137 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1138 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1139 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
Philipp Zabel10436672012-07-02 09:03:55 -03001140 if (dev->devtype->product == CODA_DX6) {
1141 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1142 } else {
1143 coda_write(dev, dev->iram_paddr, CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1144 coda_write(dev, 48 * 1024, CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
1145 }
Javier Martin186b2502012-07-26 05:53:35 -03001146 }
1147
1148 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1149 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1150 return -ETIMEDOUT;
1151 }
1152
1153 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0)
1154 return -EFAULT;
1155
Philipp Zabelec25f682012-07-20 08:54:29 -03001156 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
1157 if (ret < 0)
1158 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001159
Philipp Zabelec25f682012-07-20 08:54:29 -03001160 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03001161 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
1162 if (dev->devtype->product != CODA_DX6) {
1163 coda_write(dev, round_up(q_data_src->width, 8), CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1164 coda_write(dev, dev->iram_paddr + 48 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1165 coda_write(dev, dev->iram_paddr + 53 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1166 coda_write(dev, dev->iram_paddr + 58 * 1024, CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1167 coda_write(dev, dev->iram_paddr + 68 * 1024, CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1168 coda_write(dev, 0x0, CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1169 }
Javier Martin186b2502012-07-26 05:53:35 -03001170 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1171 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1172 return -ETIMEDOUT;
1173 }
1174
1175 /* Save stream headers */
1176 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1177 switch (dst_fourcc) {
1178 case V4L2_PIX_FMT_H264:
1179 /*
1180 * Get SPS in the first frame and copy it to an
1181 * intermediate buffer.
1182 */
1183 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1184 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1185 coda_write(dev, CODA_HEADER_H264_SPS, CODA_CMD_ENC_HEADER_CODE);
1186 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1187 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1188 return -ETIMEDOUT;
1189 }
1190 ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1191 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1192 memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0),
1193 ctx->vpu_header_size[0]);
1194
1195 /*
1196 * Get PPS in the first frame and copy it to an
1197 * intermediate buffer.
1198 */
1199 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1200 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1201 coda_write(dev, CODA_HEADER_H264_PPS, CODA_CMD_ENC_HEADER_CODE);
1202 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1203 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1204 return -ETIMEDOUT;
1205 }
1206 ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1207 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1208 memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0),
1209 ctx->vpu_header_size[1]);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001210 /*
1211 * Length of H.264 headers is variable and thus it might not be
1212 * aligned for the coda to append the encoded frame. In that is
1213 * the case a filler NAL must be added to header 2.
1214 */
1215 ctx->vpu_header_size[2] = coda_h264_padding(
1216 (ctx->vpu_header_size[0] +
1217 ctx->vpu_header_size[1]),
1218 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03001219 break;
1220 case V4L2_PIX_FMT_MPEG4:
1221 /*
1222 * Get VOS in the first frame and copy it to an
1223 * intermediate buffer
1224 */
1225 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1226 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1227 coda_write(dev, CODA_HEADER_MP4V_VOS, CODA_CMD_ENC_HEADER_CODE);
1228 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1229 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1230 return -ETIMEDOUT;
1231 }
1232 ctx->vpu_header_size[0] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1233 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1234 memcpy(&ctx->vpu_header[0][0], vb2_plane_vaddr(buf, 0),
1235 ctx->vpu_header_size[0]);
1236
1237 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1238 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1239 coda_write(dev, CODA_HEADER_MP4V_VIS, CODA_CMD_ENC_HEADER_CODE);
1240 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1241 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n");
1242 return -ETIMEDOUT;
1243 }
1244 ctx->vpu_header_size[1] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1245 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1246 memcpy(&ctx->vpu_header[1][0], vb2_plane_vaddr(buf, 0),
1247 ctx->vpu_header_size[1]);
1248
1249 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0), CODA_CMD_ENC_HEADER_BB_START);
1250 coda_write(dev, bitstream_size, CODA_CMD_ENC_HEADER_BB_SIZE);
1251 coda_write(dev, CODA_HEADER_MP4V_VOL, CODA_CMD_ENC_HEADER_CODE);
1252 if (coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER)) {
1253 v4l2_err(v4l2_dev, "CODA_COMMAND_ENCODE_HEADER failed\n");
1254 return -ETIMEDOUT;
1255 }
1256 ctx->vpu_header_size[2] = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1257 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1258 memcpy(&ctx->vpu_header[2][0], vb2_plane_vaddr(buf, 0),
1259 ctx->vpu_header_size[2]);
1260 break;
1261 default:
1262 /* No more formats need to save headers at the moment */
1263 break;
1264 }
1265
1266 return 0;
1267}
1268
1269static int coda_stop_streaming(struct vb2_queue *q)
1270{
1271 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001272 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03001273
1274 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1275 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1276 "%s: output\n", __func__);
1277 ctx->rawstreamon = 0;
1278 } else {
1279 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1280 "%s: capture\n", __func__);
1281 ctx->compstreamon = 0;
1282 }
1283
Philipp Zabel62bed142012-07-25 10:40:39 -03001284 /* Don't stop the coda unless both queues are off */
1285 if (ctx->rawstreamon || ctx->compstreamon)
1286 return 0;
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001287
Philipp Zabel62bed142012-07-25 10:40:39 -03001288 if (coda_isbusy(dev)) {
1289 if (wait_for_completion_interruptible_timeout(&dev->done, HZ) <= 0) {
1290 v4l2_warn(&dev->v4l2_dev,
1291 "%s: timeout, sending SEQ_END anyway\n", __func__);
Javier Martin186b2502012-07-26 05:53:35 -03001292 }
1293 }
1294
Philipp Zabel62bed142012-07-25 10:40:39 -03001295 cancel_delayed_work(&dev->timeout);
1296
1297 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1298 "%s: sent command 'SEQ_END' to coda\n", __func__);
1299 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1300 v4l2_err(&dev->v4l2_dev,
1301 "CODA_COMMAND_SEQ_END failed\n");
1302 return -ETIMEDOUT;
1303 }
1304
1305 coda_free_framebuffers(ctx);
1306
Javier Martin186b2502012-07-26 05:53:35 -03001307 return 0;
1308}
1309
1310static struct vb2_ops coda_qops = {
1311 .queue_setup = coda_queue_setup,
1312 .buf_prepare = coda_buf_prepare,
1313 .buf_queue = coda_buf_queue,
1314 .wait_prepare = coda_wait_prepare,
1315 .wait_finish = coda_wait_finish,
1316 .start_streaming = coda_start_streaming,
1317 .stop_streaming = coda_stop_streaming,
1318};
1319
1320static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1321{
1322 struct coda_ctx *ctx =
1323 container_of(ctrl->handler, struct coda_ctx, ctrls);
1324
1325 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1326 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1327
1328 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001329 case V4L2_CID_HFLIP:
1330 if (ctrl->val)
1331 ctx->params.rot_mode |= CODA_MIR_HOR;
1332 else
1333 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1334 break;
1335 case V4L2_CID_VFLIP:
1336 if (ctrl->val)
1337 ctx->params.rot_mode |= CODA_MIR_VER;
1338 else
1339 ctx->params.rot_mode &= ~CODA_MIR_VER;
1340 break;
Javier Martin186b2502012-07-26 05:53:35 -03001341 case V4L2_CID_MPEG_VIDEO_BITRATE:
1342 ctx->params.bitrate = ctrl->val / 1000;
1343 break;
1344 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1345 ctx->params.gop_size = ctrl->val;
1346 break;
1347 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1348 ctx->params.h264_intra_qp = ctrl->val;
1349 break;
1350 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1351 ctx->params.h264_inter_qp = ctrl->val;
1352 break;
1353 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1354 ctx->params.mpeg4_intra_qp = ctrl->val;
1355 break;
1356 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1357 ctx->params.mpeg4_inter_qp = ctrl->val;
1358 break;
1359 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1360 ctx->params.slice_mode = ctrl->val;
1361 break;
1362 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1363 ctx->params.slice_max_mb = ctrl->val;
1364 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001365 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1366 ctx->params.slice_max_bits = ctrl->val * 8;
1367 break;
Javier Martin186b2502012-07-26 05:53:35 -03001368 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1369 break;
1370 default:
1371 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1372 "Invalid control, id=%d, val=%d\n",
1373 ctrl->id, ctrl->val);
1374 return -EINVAL;
1375 }
1376
1377 return 0;
1378}
1379
1380static struct v4l2_ctrl_ops coda_ctrl_ops = {
1381 .s_ctrl = coda_s_ctrl,
1382};
1383
1384static int coda_ctrls_setup(struct coda_ctx *ctx)
1385{
1386 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1387
1388 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001389 V4L2_CID_HFLIP, 0, 1, 1, 0);
1390 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1391 V4L2_CID_VFLIP, 0, 1, 1, 0);
1392 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001393 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1394 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1395 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1396 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1397 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1398 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1399 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1400 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1401 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1402 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1403 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1404 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1405 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001406 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1407 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001408 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1409 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001410 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1411 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001412 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1413 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1414 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1415 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1416 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1417
1418 if (ctx->ctrls.error) {
1419 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1420 ctx->ctrls.error);
1421 return -EINVAL;
1422 }
1423
1424 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1425}
1426
1427static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1428 struct vb2_queue *dst_vq)
1429{
1430 struct coda_ctx *ctx = priv;
1431 int ret;
1432
Javier Martin186b2502012-07-26 05:53:35 -03001433 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03001434 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001435 src_vq->drv_priv = ctx;
1436 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1437 src_vq->ops = &coda_qops;
1438 src_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001439 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001440
1441 ret = vb2_queue_init(src_vq);
1442 if (ret)
1443 return ret;
1444
Javier Martin186b2502012-07-26 05:53:35 -03001445 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03001446 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001447 dst_vq->drv_priv = ctx;
1448 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1449 dst_vq->ops = &coda_qops;
1450 dst_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001451 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001452
1453 return vb2_queue_init(dst_vq);
1454}
1455
Philipp Zabele11f3e62012-07-25 09:16:58 -03001456static int coda_next_free_instance(struct coda_dev *dev)
1457{
1458 return ffz(dev->instance_mask);
1459}
1460
Javier Martin186b2502012-07-26 05:53:35 -03001461static int coda_open(struct file *file)
1462{
1463 struct coda_dev *dev = video_drvdata(file);
1464 struct coda_ctx *ctx = NULL;
1465 int ret = 0;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001466 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001467
Philipp Zabele11f3e62012-07-25 09:16:58 -03001468 idx = coda_next_free_instance(dev);
1469 if (idx >= CODA_MAX_INSTANCES)
Javier Martin186b2502012-07-26 05:53:35 -03001470 return -EBUSY;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001471 set_bit(idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001472
1473 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1474 if (!ctx)
1475 return -ENOMEM;
1476
1477 v4l2_fh_init(&ctx->fh, video_devdata(file));
1478 file->private_data = &ctx->fh;
1479 v4l2_fh_add(&ctx->fh);
1480 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001481 ctx->idx = idx;
Javier Martin186b2502012-07-26 05:53:35 -03001482
1483 set_default_params(ctx);
1484 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1485 &coda_queue_init);
1486 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03001487 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001488
1489 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1490 __func__, ret);
1491 goto err;
1492 }
1493 ret = coda_ctrls_setup(ctx);
1494 if (ret) {
1495 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1496 goto err;
1497 }
1498
1499 ctx->fh.ctrl_handler = &ctx->ctrls;
1500
1501 ctx->parabuf.vaddr = dma_alloc_coherent(&dev->plat_dev->dev,
1502 CODA_PARA_BUF_SIZE, &ctx->parabuf.paddr, GFP_KERNEL);
1503 if (!ctx->parabuf.vaddr) {
1504 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1505 ret = -ENOMEM;
1506 goto err;
1507 }
1508
1509 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001510 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001511 coda_unlock(ctx);
1512
1513 clk_prepare_enable(dev->clk_per);
1514 clk_prepare_enable(dev->clk_ahb);
1515
1516 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1517 ctx->idx, ctx);
1518
1519 return 0;
1520
1521err:
1522 v4l2_fh_del(&ctx->fh);
1523 v4l2_fh_exit(&ctx->fh);
1524 kfree(ctx);
1525 return ret;
1526}
1527
1528static int coda_release(struct file *file)
1529{
1530 struct coda_dev *dev = video_drvdata(file);
1531 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1532
1533 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1534 ctx);
1535
1536 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001537 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001538 coda_unlock(ctx);
1539
1540 dma_free_coherent(&dev->plat_dev->dev, CODA_PARA_BUF_SIZE,
1541 ctx->parabuf.vaddr, ctx->parabuf.paddr);
1542 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1543 v4l2_ctrl_handler_free(&ctx->ctrls);
1544 clk_disable_unprepare(dev->clk_per);
1545 clk_disable_unprepare(dev->clk_ahb);
1546 v4l2_fh_del(&ctx->fh);
1547 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001548 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001549 kfree(ctx);
1550
1551 return 0;
1552}
1553
1554static unsigned int coda_poll(struct file *file,
1555 struct poll_table_struct *wait)
1556{
1557 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1558 int ret;
1559
1560 coda_lock(ctx);
1561 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1562 coda_unlock(ctx);
1563 return ret;
1564}
1565
1566static int coda_mmap(struct file *file, struct vm_area_struct *vma)
1567{
1568 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1569
1570 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1571}
1572
1573static const struct v4l2_file_operations coda_fops = {
1574 .owner = THIS_MODULE,
1575 .open = coda_open,
1576 .release = coda_release,
1577 .poll = coda_poll,
1578 .unlocked_ioctl = video_ioctl2,
1579 .mmap = coda_mmap,
1580};
1581
1582static irqreturn_t coda_irq_handler(int irq, void *data)
1583{
Philipp Zabelec25f682012-07-20 08:54:29 -03001584 struct vb2_buffer *src_buf, *dst_buf;
Javier Martin186b2502012-07-26 05:53:35 -03001585 struct coda_dev *dev = data;
1586 u32 wr_ptr, start_ptr;
1587 struct coda_ctx *ctx;
1588
Fabio Estevam2249b452012-10-09 23:33:29 -03001589 cancel_delayed_work(&dev->timeout);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001590
Javier Martin186b2502012-07-26 05:53:35 -03001591 /* read status register to attend the IRQ */
1592 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1593 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1594 CODA_REG_BIT_INT_CLEAR);
1595
1596 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1597 if (ctx == NULL) {
1598 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
1599 return IRQ_HANDLED;
1600 }
1601
1602 if (ctx->aborting) {
1603 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1604 "task has been aborted\n");
1605 return IRQ_HANDLED;
1606 }
1607
1608 if (coda_isbusy(ctx->dev)) {
1609 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1610 "coda is still busy!!!!\n");
1611 return IRQ_NONE;
1612 }
1613
Philipp Zabel62bed142012-07-25 10:40:39 -03001614 complete(&dev->done);
1615
Philipp Zabelec25f682012-07-20 08:54:29 -03001616 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1617 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001618
1619 /* Get results from the coda */
1620 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
1621 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1622 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx));
1623 /* Calculate bytesused field */
1624 if (dst_buf->v4l2_buf.sequence == 0) {
1625 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr) +
1626 ctx->vpu_header_size[0] +
1627 ctx->vpu_header_size[1] +
1628 ctx->vpu_header_size[2];
1629 } else {
1630 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr);
1631 }
1632
1633 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1634 wr_ptr - start_ptr);
1635
1636 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1637 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1638
1639 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1640 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1641 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1642 } else {
1643 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1644 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1645 }
1646
Kamil Debskiccd571c2013-04-24 10:38:24 -03001647 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1648 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1649
Philipp Zabelec25f682012-07-20 08:54:29 -03001650 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03001651 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1652
1653 ctx->gopcounter--;
1654 if (ctx->gopcounter < 0)
1655 ctx->gopcounter = ctx->params.gop_size - 1;
1656
1657 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1658 "job finished: encoding frame (%d) (%s)\n",
1659 dst_buf->v4l2_buf.sequence,
1660 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1661 "KEYFRAME" : "PFRAME");
1662
1663 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
1664
1665 return IRQ_HANDLED;
1666}
1667
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001668static void coda_timeout(struct work_struct *work)
1669{
1670 struct coda_ctx *ctx;
1671 struct coda_dev *dev = container_of(to_delayed_work(work),
1672 struct coda_dev, timeout);
1673
Philipp Zabel62bed142012-07-25 10:40:39 -03001674 if (completion_done(&dev->done))
1675 return;
1676
1677 complete(&dev->done);
1678
Philipp Zabel39cc0292013-05-21 10:32:42 -03001679 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001680
1681 mutex_lock(&dev->dev_mutex);
1682 list_for_each_entry(ctx, &dev->instances, list) {
1683 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1684 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1685 }
1686 mutex_unlock(&dev->dev_mutex);
1687}
1688
Javier Martin186b2502012-07-26 05:53:35 -03001689static u32 coda_supported_firmwares[] = {
1690 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001691 CODA_FIRMWARE_VERNUM(CODA_7541, 13, 4, 29),
Javier Martin186b2502012-07-26 05:53:35 -03001692};
1693
1694static bool coda_firmware_supported(u32 vernum)
1695{
1696 int i;
1697
1698 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
1699 if (vernum == coda_supported_firmwares[i])
1700 return true;
1701 return false;
1702}
1703
1704static char *coda_product_name(int product)
1705{
1706 static char buf[9];
1707
1708 switch (product) {
1709 case CODA_DX6:
1710 return "CodaDx6";
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001711 case CODA_7541:
1712 return "CODA7541";
Javier Martin186b2502012-07-26 05:53:35 -03001713 default:
1714 snprintf(buf, sizeof(buf), "(0x%04x)", product);
1715 return buf;
1716 }
1717}
1718
Philipp Zabel87048bb2012-07-02 07:03:43 -03001719static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001720{
1721 u16 product, major, minor, release;
1722 u32 data;
1723 u16 *p;
1724 int i;
1725
1726 clk_prepare_enable(dev->clk_per);
1727 clk_prepare_enable(dev->clk_ahb);
1728
Javier Martin186b2502012-07-26 05:53:35 -03001729 /*
1730 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001731 * The 16-bit chars in the code buffer are in memory access
1732 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001733 * Data in this SRAM survives a reboot.
1734 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001735 p = (u16 *)dev->codebuf.vaddr;
1736 if (dev->devtype->product == CODA_DX6) {
1737 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1738 data = CODA_DOWN_ADDRESS_SET(i) |
1739 CODA_DOWN_DATA_SET(p[i ^ 1]);
1740 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1741 }
1742 } else {
1743 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1744 data = CODA_DOWN_ADDRESS_SET(i) |
1745 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1746 3 - (i % 4)]);
1747 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1748 }
Javier Martin186b2502012-07-26 05:53:35 -03001749 }
Javier Martin186b2502012-07-26 05:53:35 -03001750
Philipp Zabel9acf7692013-05-23 10:42:56 -03001751 /* Clear registers */
1752 for (i = 0; i < 64; i++)
1753 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1754
Javier Martin186b2502012-07-26 05:53:35 -03001755 /* Tell the BIT where to find everything it needs */
1756 coda_write(dev, dev->workbuf.paddr,
1757 CODA_REG_BIT_WORK_BUF_ADDR);
1758 coda_write(dev, dev->codebuf.paddr,
1759 CODA_REG_BIT_CODE_BUF_ADDR);
1760 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1761
1762 /* Set default values */
1763 switch (dev->devtype->product) {
1764 case CODA_DX6:
1765 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1766 break;
1767 default:
1768 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1769 }
1770 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001771
1772 if (dev->devtype->product != CODA_DX6)
1773 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1774
Javier Martin186b2502012-07-26 05:53:35 -03001775 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1776 CODA_REG_BIT_INT_ENABLE);
1777
1778 /* Reset VPU and start processor */
1779 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1780 data |= CODA_REG_RESET_ENABLE;
1781 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1782 udelay(10);
1783 data &= ~CODA_REG_RESET_ENABLE;
1784 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1785 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1786
1787 /* Load firmware */
1788 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
1789 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
1790 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
1791 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
1792 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
1793 if (coda_wait_timeout(dev)) {
1794 clk_disable_unprepare(dev->clk_per);
1795 clk_disable_unprepare(dev->clk_ahb);
1796 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
1797 return -EIO;
1798 }
1799
1800 /* Check we are compatible with the loaded firmware */
1801 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
1802 product = CODA_FIRMWARE_PRODUCT(data);
1803 major = CODA_FIRMWARE_MAJOR(data);
1804 minor = CODA_FIRMWARE_MINOR(data);
1805 release = CODA_FIRMWARE_RELEASE(data);
1806
1807 clk_disable_unprepare(dev->clk_per);
1808 clk_disable_unprepare(dev->clk_ahb);
1809
1810 if (product != dev->devtype->product) {
1811 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
1812 " Version: %u.%u.%u\n",
1813 coda_product_name(dev->devtype->product),
1814 coda_product_name(product), major, minor, release);
1815 return -EINVAL;
1816 }
1817
1818 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
1819 coda_product_name(product));
1820
1821 if (coda_firmware_supported(data)) {
1822 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
1823 major, minor, release);
1824 } else {
1825 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
1826 "%u.%u.%u\n", major, minor, release);
1827 }
1828
1829 return 0;
1830}
1831
1832static void coda_fw_callback(const struct firmware *fw, void *context)
1833{
1834 struct coda_dev *dev = context;
1835 struct platform_device *pdev = dev->plat_dev;
1836 int ret;
1837
1838 if (!fw) {
1839 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1840 return;
1841 }
1842
1843 /* allocate auxiliary per-device code buffer for the BIT processor */
1844 dev->codebuf.size = fw->size;
1845 dev->codebuf.vaddr = dma_alloc_coherent(&pdev->dev, fw->size,
1846 &dev->codebuf.paddr,
1847 GFP_KERNEL);
1848 if (!dev->codebuf.vaddr) {
1849 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1850 return;
1851 }
1852
Philipp Zabel87048bb2012-07-02 07:03:43 -03001853 /* Copy the whole firmware image to the code buffer */
1854 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1855 release_firmware(fw);
1856
1857 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03001858 if (ret) {
1859 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1860 return;
1861 }
1862
1863 dev->vfd.fops = &coda_fops,
1864 dev->vfd.ioctl_ops = &coda_ioctl_ops;
1865 dev->vfd.release = video_device_release_empty,
1866 dev->vfd.lock = &dev->dev_mutex;
1867 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03001868 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03001869 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
1870 video_set_drvdata(&dev->vfd, dev);
1871
1872 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1873 if (IS_ERR(dev->alloc_ctx)) {
1874 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1875 return;
1876 }
1877
1878 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1879 if (IS_ERR(dev->m2m_dev)) {
1880 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1881 goto rel_ctx;
1882 }
1883
1884 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
1885 if (ret) {
1886 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1887 goto rel_m2m;
1888 }
1889 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
1890 dev->vfd.num);
1891
1892 return;
1893
1894rel_m2m:
1895 v4l2_m2m_release(dev->m2m_dev);
1896rel_ctx:
1897 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1898}
1899
1900static int coda_firmware_request(struct coda_dev *dev)
1901{
1902 char *fw = dev->devtype->firmware;
1903
1904 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1905 coda_product_name(dev->devtype->product));
1906
1907 return request_firmware_nowait(THIS_MODULE, true,
1908 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1909}
1910
1911enum coda_platform {
1912 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001913 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03001914};
1915
Emil Goodec06d8752012-08-14 17:44:42 -03001916static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001917 [CODA_IMX27] = {
1918 .firmware = "v4l-codadx6-imx27.bin",
1919 .product = CODA_DX6,
1920 .formats = codadx6_formats,
1921 .num_formats = ARRAY_SIZE(codadx6_formats),
1922 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001923 [CODA_IMX53] = {
1924 .firmware = "v4l-coda7541-imx53.bin",
1925 .product = CODA_7541,
1926 .formats = coda7_formats,
1927 .num_formats = ARRAY_SIZE(coda7_formats),
1928 },
Javier Martin186b2502012-07-26 05:53:35 -03001929};
1930
1931static struct platform_device_id coda_platform_ids[] = {
1932 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001933 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001934 { /* sentinel */ }
1935};
1936MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1937
1938#ifdef CONFIG_OF
1939static const struct of_device_id coda_dt_ids[] = {
1940 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001941 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03001942 { /* sentinel */ }
1943};
1944MODULE_DEVICE_TABLE(of, coda_dt_ids);
1945#endif
1946
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001947static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001948{
1949 const struct of_device_id *of_id =
1950 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1951 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001952 struct coda_platform_data *pdata = pdev->dev.platform_data;
1953 struct device_node *np = pdev->dev.of_node;
1954 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001955 struct coda_dev *dev;
1956 struct resource *res;
1957 int ret, irq;
1958
1959 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1960 if (!dev) {
1961 dev_err(&pdev->dev, "Not enough memory for %s\n",
1962 CODA_NAME);
1963 return -ENOMEM;
1964 }
1965
1966 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001967 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001968 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Philipp Zabel62bed142012-07-25 10:40:39 -03001969 init_completion(&dev->done);
1970 complete(&dev->done);
Javier Martin186b2502012-07-26 05:53:35 -03001971
1972 dev->plat_dev = pdev;
1973 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1974 if (IS_ERR(dev->clk_per)) {
1975 dev_err(&pdev->dev, "Could not get per clock\n");
1976 return PTR_ERR(dev->clk_per);
1977 }
1978
1979 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1980 if (IS_ERR(dev->clk_ahb)) {
1981 dev_err(&pdev->dev, "Could not get ahb clock\n");
1982 return PTR_ERR(dev->clk_ahb);
1983 }
1984
1985 /* Get memory for physical registers */
1986 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1987 if (res == NULL) {
1988 dev_err(&pdev->dev, "failed to get memory region resource\n");
1989 return -ENOENT;
1990 }
1991
Philipp Zabel611cbbf2013-05-21 04:19:27 -03001992 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1993 if (IS_ERR(dev->regs_base))
1994 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03001995
1996 /* IRQ */
1997 irq = platform_get_irq(pdev, 0);
1998 if (irq < 0) {
1999 dev_err(&pdev->dev, "failed to get irq resource\n");
2000 return -ENOENT;
2001 }
2002
2003 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
2004 0, CODA_NAME, dev) < 0) {
2005 dev_err(&pdev->dev, "failed to request irq\n");
2006 return -ENOENT;
2007 }
2008
Philipp Zabel657eee72013-04-29 16:17:14 -07002009 /* Get IRAM pool from device tree or platform data */
2010 pool = of_get_named_gen_pool(np, "iram", 0);
2011 if (!pool && pdata)
2012 pool = dev_get_gen_pool(pdata->iram_dev);
2013 if (!pool) {
2014 dev_err(&pdev->dev, "iram pool not available\n");
2015 return -ENOMEM;
2016 }
2017 dev->iram_pool = pool;
2018
Javier Martin186b2502012-07-26 05:53:35 -03002019 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2020 if (ret)
2021 return ret;
2022
2023 mutex_init(&dev->dev_mutex);
2024
2025 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2026
2027 if (of_id) {
2028 dev->devtype = of_id->data;
2029 } else if (pdev_id) {
2030 dev->devtype = &coda_devdata[pdev_id->driver_data];
2031 } else {
2032 v4l2_device_unregister(&dev->v4l2_dev);
2033 return -EINVAL;
2034 }
2035
2036 /* allocate auxiliary per-device buffers for the BIT processor */
2037 switch (dev->devtype->product) {
2038 case CODA_DX6:
2039 dev->workbuf.size = CODADX6_WORK_BUF_SIZE;
2040 break;
2041 default:
2042 dev->workbuf.size = CODA7_WORK_BUF_SIZE;
2043 }
2044 dev->workbuf.vaddr = dma_alloc_coherent(&pdev->dev, dev->workbuf.size,
2045 &dev->workbuf.paddr,
2046 GFP_KERNEL);
2047 if (!dev->workbuf.vaddr) {
2048 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2049 v4l2_device_unregister(&dev->v4l2_dev);
2050 return -ENOMEM;
2051 }
2052
Philipp Zabel657eee72013-04-29 16:17:14 -07002053 if (dev->devtype->product == CODA_DX6)
2054 dev->iram_size = CODADX6_IRAM_SIZE;
2055 else
2056 dev->iram_size = CODA7_IRAM_SIZE;
2057 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
2058 if (!dev->iram_vaddr) {
2059 dev_err(&pdev->dev, "unable to alloc iram\n");
2060 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03002061 }
Philipp Zabel657eee72013-04-29 16:17:14 -07002062 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
2063 dev->iram_vaddr);
Philipp Zabel10436672012-07-02 09:03:55 -03002064
Javier Martin186b2502012-07-26 05:53:35 -03002065 platform_set_drvdata(pdev, dev);
2066
2067 return coda_firmware_request(dev);
2068}
2069
2070static int coda_remove(struct platform_device *pdev)
2071{
2072 struct coda_dev *dev = platform_get_drvdata(pdev);
2073
2074 video_unregister_device(&dev->vfd);
2075 if (dev->m2m_dev)
2076 v4l2_m2m_release(dev->m2m_dev);
2077 if (dev->alloc_ctx)
2078 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2079 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002080 if (dev->iram_vaddr)
2081 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Javier Martin186b2502012-07-26 05:53:35 -03002082 if (dev->codebuf.vaddr)
2083 dma_free_coherent(&pdev->dev, dev->codebuf.size,
2084 &dev->codebuf.vaddr, dev->codebuf.paddr);
2085 if (dev->workbuf.vaddr)
2086 dma_free_coherent(&pdev->dev, dev->workbuf.size, &dev->workbuf.vaddr,
2087 dev->workbuf.paddr);
2088 return 0;
2089}
2090
2091static struct platform_driver coda_driver = {
2092 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002093 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002094 .driver = {
2095 .name = CODA_NAME,
2096 .owner = THIS_MODULE,
2097 .of_match_table = of_match_ptr(coda_dt_ids),
2098 },
2099 .id_table = coda_platform_ids,
2100};
2101
2102module_platform_driver(coda_driver);
2103
2104MODULE_LICENSE("GPL");
2105MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2106MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");