blob: 34d6fd6c051732fb610ee175ad66b547cb762549 [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
Philipp Zabelb96904e2013-05-23 10:42:58 -030052#define MAX_W 8192
53#define MAX_H 8192
54#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030055#define FMO_SLICE_SAVE_BUF_SIZE (32)
56#define CODA_DEFAULT_GAMMA 4096
57
58#define MIN_W 176
59#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030060
61#define S_ALIGN 1 /* multiple of 2 */
62#define W_ALIGN 1 /* multiple of 2 */
63#define H_ALIGN 1 /* multiple of 2 */
64
65#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
66
67static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030068module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030069MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
70
71enum {
72 V4L2_M2M_SRC = 0,
73 V4L2_M2M_DST = 1,
74};
75
Javier Martin186b2502012-07-26 05:53:35 -030076enum coda_inst_type {
77 CODA_INST_ENCODER,
78 CODA_INST_DECODER,
79};
80
81enum coda_product {
82 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030083 CODA_7541 = 0xf012,
Javier Martin186b2502012-07-26 05:53:35 -030084};
85
86struct coda_fmt {
87 char *name;
88 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030089};
90
91struct coda_codec {
92 u32 mode;
93 u32 src_fourcc;
94 u32 dst_fourcc;
95 u32 max_w;
96 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -030097};
98
99struct coda_devtype {
100 char *firmware;
101 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300102 struct coda_codec *codecs;
103 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300104 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;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300112 unsigned int fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300113};
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;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300167 int streamon_out;
168 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300169 u32 isequence;
170 struct coda_q_data q_data[2];
171 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300172 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300173 enum v4l2_colorspace colorspace;
174 struct coda_params params;
175 struct v4l2_m2m_ctx *m2m_ctx;
176 struct v4l2_ctrl_handler ctrls;
177 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300178 int gopcounter;
179 char vpu_header[3][64];
180 int vpu_header_size[3];
181 struct coda_aux_buf parabuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300182 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
183 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300184 int idx;
185};
186
Javier Martin832fbb52012-10-29 08:34:59 -0300187static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
189static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300190
Javier Martin186b2502012-07-26 05:53:35 -0300191static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
192{
193 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
194 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
195 writel(data, dev->regs_base + reg);
196}
197
198static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
199{
200 u32 data;
201 data = readl(dev->regs_base + reg);
202 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
203 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
204 return data;
205}
206
207static inline unsigned long coda_isbusy(struct coda_dev *dev)
208{
209 return coda_read(dev, CODA_REG_BIT_BUSY);
210}
211
212static inline int coda_is_initialized(struct coda_dev *dev)
213{
214 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
215}
216
217static int coda_wait_timeout(struct coda_dev *dev)
218{
219 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
220
221 while (coda_isbusy(dev)) {
222 if (time_after(jiffies, timeout))
223 return -ETIMEDOUT;
224 }
225 return 0;
226}
227
228static void coda_command_async(struct coda_ctx *ctx, int cmd)
229{
230 struct coda_dev *dev = ctx->dev;
231 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
232
233 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
234 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
235 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
236}
237
238static int coda_command_sync(struct coda_ctx *ctx, int cmd)
239{
240 struct coda_dev *dev = ctx->dev;
241
242 coda_command_async(ctx, cmd);
243 return coda_wait_timeout(dev);
244}
245
246static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
247 enum v4l2_buf_type type)
248{
249 switch (type) {
250 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
251 return &(ctx->q_data[V4L2_M2M_SRC]);
252 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
253 return &(ctx->q_data[V4L2_M2M_DST]);
254 default:
255 BUG();
256 }
257 return NULL;
258}
259
260/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300261 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300262 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300263static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300264 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300265 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300266 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300267 },
268 {
269 .name = "YUV 4:2:0 Planar, YCrCb",
270 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300271 },
272 {
273 .name = "H264 Encoded Stream",
274 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300275 },
276 {
277 .name = "MPEG4 Encoded Stream",
278 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300279 },
280};
281
Philipp Zabelb96904e2013-05-23 10:42:58 -0300282#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
283 { mode, src_fourcc, dst_fourcc, max_w, max_h }
284
285/*
286 * Arrays of codecs supported by each given version of Coda:
287 * i.MX27 -> codadx6
288 * i.MX5x -> coda7
289 * i.MX6 -> coda960
290 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
291 */
292static struct coda_codec codadx6_codecs[] = {
293 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
294 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300295};
296
Philipp Zabelb96904e2013-05-23 10:42:58 -0300297static struct coda_codec coda7_codecs[] = {
298 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
299 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
300};
301
302static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300303{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300304 switch (fourcc) {
305 case V4L2_PIX_FMT_YUV420:
306 case V4L2_PIX_FMT_YVU420:
307 return true;
308 default:
309 return false;
310 }
311}
Javier Martin186b2502012-07-26 05:53:35 -0300312
Philipp Zabelb96904e2013-05-23 10:42:58 -0300313/*
314 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
315 * tables.
316 */
317static u32 coda_format_normalize_yuv(u32 fourcc)
318{
319 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
320}
321
322static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
323 int dst_fourcc)
324{
325 struct coda_codec *codecs = dev->devtype->codecs;
326 int num_codecs = dev->devtype->num_codecs;
327 int k;
328
329 src_fourcc = coda_format_normalize_yuv(src_fourcc);
330 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
331 if (src_fourcc == dst_fourcc)
332 return NULL;
333
334 for (k = 0; k < num_codecs; k++) {
335 if (codecs[k].src_fourcc == src_fourcc &&
336 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300337 break;
338 }
339
Philipp Zabelb96904e2013-05-23 10:42:58 -0300340 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300341 return NULL;
342
Philipp Zabelb96904e2013-05-23 10:42:58 -0300343 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300344}
345
346/*
347 * V4L2 ioctl() operations.
348 */
349static int vidioc_querycap(struct file *file, void *priv,
350 struct v4l2_capability *cap)
351{
352 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
353 strlcpy(cap->card, CODA_NAME, sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300354 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300355 /*
356 * This is only a mem-to-mem video device. The capture and output
357 * device capability flags are left only for backward compatibility
358 * and are scheduled for removal.
359 */
360 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
361 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300362 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
363
364 return 0;
365}
366
367static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300368 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300369{
370 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300371 struct coda_codec *codecs = ctx->dev->devtype->codecs;
372 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300373 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300374 int num_codecs = ctx->dev->devtype->num_codecs;
375 int num_formats = ARRAY_SIZE(coda_formats);
376 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300377
378 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300379 /* Both uncompressed formats are always supported */
380 if (coda_format_is_yuv(formats[i].fourcc)) {
381 if (num == f->index)
382 break;
383 ++num;
384 continue;
385 }
386 /* Compressed formats may be supported, check the codec list */
387 for (k = 0; k < num_codecs; k++) {
388 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
389 formats[i].fourcc == codecs[k].dst_fourcc)
390 break;
391 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
392 formats[i].fourcc == codecs[k].src_fourcc)
393 break;
394 }
395 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300396 if (num == f->index)
397 break;
398 ++num;
399 }
400 }
401
402 if (i < num_formats) {
403 fmt = &formats[i];
404 strlcpy(f->description, fmt->name, sizeof(f->description));
405 f->pixelformat = fmt->fourcc;
406 return 0;
407 }
408
409 /* Format not found */
410 return -EINVAL;
411}
412
413static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
414 struct v4l2_fmtdesc *f)
415{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300416 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300417}
418
419static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
420 struct v4l2_fmtdesc *f)
421{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300422 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Javier Martin186b2502012-07-26 05:53:35 -0300423}
424
425static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
426{
427 struct vb2_queue *vq;
428 struct coda_q_data *q_data;
429 struct coda_ctx *ctx = fh_to_ctx(priv);
430
431 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
432 if (!vq)
433 return -EINVAL;
434
435 q_data = get_q_data(ctx, f->type);
436
437 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300438 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300439 f->fmt.pix.width = q_data->width;
440 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300441 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300442 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
443 else /* encoded formats h.264/mpeg4 */
444 f->fmt.pix.bytesperline = 0;
445
446 f->fmt.pix.sizeimage = q_data->sizeimage;
447 f->fmt.pix.colorspace = ctx->colorspace;
448
449 return 0;
450}
451
Philipp Zabelb96904e2013-05-23 10:42:58 -0300452static int vidioc_try_fmt(struct coda_codec *codec, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300453{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300454 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300455 enum v4l2_field field;
456
457 field = f->fmt.pix.field;
458 if (field == V4L2_FIELD_ANY)
459 field = V4L2_FIELD_NONE;
460 else if (V4L2_FIELD_NONE != field)
461 return -EINVAL;
462
463 /* V4L2 specification suggests the driver corrects the format struct
464 * if any of the dimensions is unsupported */
465 f->fmt.pix.field = field;
466
Philipp Zabelb96904e2013-05-23 10:42:58 -0300467 if (codec) {
468 max_w = codec->max_w;
469 max_h = codec->max_h;
470 } else {
471 max_w = MAX_W;
472 max_h = MAX_H;
473 }
474 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w,
475 W_ALIGN, &f->fmt.pix.height,
476 MIN_H, max_h, H_ALIGN, S_ALIGN);
477
478 if (coda_format_is_yuv(f->fmt.pix.pixelformat)) {
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300479 /* Frame stride must be multiple of 8 */
480 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
481 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300482 f->fmt.pix.height * 3 / 2;
Javier Martin186b2502012-07-26 05:53:35 -0300483 } else { /*encoded formats h.264/mpeg4 */
484 f->fmt.pix.bytesperline = 0;
485 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
486 }
487
488 return 0;
489}
490
491static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
492 struct v4l2_format *f)
493{
Javier Martin186b2502012-07-26 05:53:35 -0300494 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300495 struct coda_codec *codec = NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300496
Philipp Zabelb96904e2013-05-23 10:42:58 -0300497 /* Determine codec by the encoded format */
498 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
499 f->fmt.pix.pixelformat);
Javier Martin186b2502012-07-26 05:53:35 -0300500
501 f->fmt.pix.colorspace = ctx->colorspace;
502
Philipp Zabelb96904e2013-05-23 10:42:58 -0300503 return vidioc_try_fmt(codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300504}
505
506static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
507 struct v4l2_format *f)
508{
509 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300510 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300511
Philipp Zabelb96904e2013-05-23 10:42:58 -0300512 /* Determine codec by encoded format, returns NULL if raw or invalid */
513 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
514 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300515
516 if (!f->fmt.pix.colorspace)
517 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
518
Philipp Zabelb96904e2013-05-23 10:42:58 -0300519 return vidioc_try_fmt(codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300520}
521
522static int vidioc_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
523{
524 struct coda_q_data *q_data;
525 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300526
527 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
528 if (!vq)
529 return -EINVAL;
530
531 q_data = get_q_data(ctx, f->type);
532 if (!q_data)
533 return -EINVAL;
534
535 if (vb2_is_busy(vq)) {
536 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
537 return -EBUSY;
538 }
539
Philipp Zabelb96904e2013-05-23 10:42:58 -0300540 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300541 q_data->width = f->fmt.pix.width;
542 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300543 q_data->sizeimage = f->fmt.pix.sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300544
545 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
546 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300547 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300548
549 return 0;
550}
551
552static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
553 struct v4l2_format *f)
554{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300555 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300556 int ret;
557
558 ret = vidioc_try_fmt_vid_cap(file, priv, f);
559 if (ret)
560 return ret;
561
Philipp Zabelb96904e2013-05-23 10:42:58 -0300562 return vidioc_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300563}
564
565static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
566 struct v4l2_format *f)
567{
568 struct coda_ctx *ctx = fh_to_ctx(priv);
569 int ret;
570
571 ret = vidioc_try_fmt_vid_out(file, priv, f);
572 if (ret)
573 return ret;
574
Richard Zhao8d621472012-08-21 02:47:42 -0300575 ret = vidioc_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300576 if (ret)
577 ctx->colorspace = f->fmt.pix.colorspace;
578
579 return ret;
580}
581
582static int vidioc_reqbufs(struct file *file, void *priv,
583 struct v4l2_requestbuffers *reqbufs)
584{
585 struct coda_ctx *ctx = fh_to_ctx(priv);
586
587 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
588}
589
590static int vidioc_querybuf(struct file *file, void *priv,
591 struct v4l2_buffer *buf)
592{
593 struct coda_ctx *ctx = fh_to_ctx(priv);
594
595 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
596}
597
598static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
599{
600 struct coda_ctx *ctx = fh_to_ctx(priv);
601
602 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
603}
604
Philipp Zabel419869c2013-05-23 07:06:30 -0300605static int vidioc_expbuf(struct file *file, void *priv,
606 struct v4l2_exportbuffer *eb)
607{
608 struct coda_ctx *ctx = fh_to_ctx(priv);
609
610 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
611}
612
Javier Martin186b2502012-07-26 05:53:35 -0300613static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
614{
615 struct coda_ctx *ctx = fh_to_ctx(priv);
616
617 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
618}
619
620static int vidioc_streamon(struct file *file, void *priv,
621 enum v4l2_buf_type type)
622{
623 struct coda_ctx *ctx = fh_to_ctx(priv);
624
625 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
626}
627
628static int vidioc_streamoff(struct file *file, void *priv,
629 enum v4l2_buf_type type)
630{
631 struct coda_ctx *ctx = fh_to_ctx(priv);
632
633 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
634}
635
636static const struct v4l2_ioctl_ops coda_ioctl_ops = {
637 .vidioc_querycap = vidioc_querycap,
638
639 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
640 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
641 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
642 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
643
644 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
645 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
646 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
647 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
648
649 .vidioc_reqbufs = vidioc_reqbufs,
650 .vidioc_querybuf = vidioc_querybuf,
651
652 .vidioc_qbuf = vidioc_qbuf,
Philipp Zabel419869c2013-05-23 07:06:30 -0300653 .vidioc_expbuf = vidioc_expbuf,
Javier Martin186b2502012-07-26 05:53:35 -0300654 .vidioc_dqbuf = vidioc_dqbuf,
655
656 .vidioc_streamon = vidioc_streamon,
657 .vidioc_streamoff = vidioc_streamoff,
658};
659
660/*
661 * Mem-to-mem operations.
662 */
663static void coda_device_run(void *m2m_priv)
664{
665 struct coda_ctx *ctx = m2m_priv;
666 struct coda_q_data *q_data_src, *q_data_dst;
667 struct vb2_buffer *src_buf, *dst_buf;
668 struct coda_dev *dev = ctx->dev;
669 int force_ipicture;
670 int quant_param = 0;
671 u32 picture_y, picture_cb, picture_cr;
672 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
673 u32 dst_fourcc;
674
675 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
676 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
677 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
678 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300679 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300680
681 src_buf->v4l2_buf.sequence = ctx->isequence;
682 dst_buf->v4l2_buf.sequence = ctx->isequence;
683 ctx->isequence++;
684
685 /*
686 * Workaround coda firmware BUG that only marks the first
687 * frame as IDR. This is a problem for some decoders that can't
688 * recover when a frame is lost.
689 */
690 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
691 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
692 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
693 } else {
694 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
695 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
696 }
697
698 /*
699 * Copy headers at the beginning of the first frame for H.264 only.
700 * In MPEG4 they are already copied by the coda.
701 */
702 if (src_buf->v4l2_buf.sequence == 0) {
703 pic_stream_buffer_addr =
704 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
705 ctx->vpu_header_size[0] +
706 ctx->vpu_header_size[1] +
707 ctx->vpu_header_size[2];
708 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
709 ctx->vpu_header_size[0] -
710 ctx->vpu_header_size[1] -
711 ctx->vpu_header_size[2];
712 memcpy(vb2_plane_vaddr(dst_buf, 0),
713 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
714 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
715 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
716 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
717 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
718 ctx->vpu_header_size[2]);
719 } else {
720 pic_stream_buffer_addr =
721 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
722 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
723 }
724
725 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
726 force_ipicture = 1;
727 switch (dst_fourcc) {
728 case V4L2_PIX_FMT_H264:
729 quant_param = ctx->params.h264_intra_qp;
730 break;
731 case V4L2_PIX_FMT_MPEG4:
732 quant_param = ctx->params.mpeg4_intra_qp;
733 break;
734 default:
735 v4l2_warn(&ctx->dev->v4l2_dev,
736 "cannot set intra qp, fmt not supported\n");
737 break;
738 }
739 } else {
740 force_ipicture = 0;
741 switch (dst_fourcc) {
742 case V4L2_PIX_FMT_H264:
743 quant_param = ctx->params.h264_inter_qp;
744 break;
745 case V4L2_PIX_FMT_MPEG4:
746 quant_param = ctx->params.mpeg4_inter_qp;
747 break;
748 default:
749 v4l2_warn(&ctx->dev->v4l2_dev,
750 "cannot set inter qp, fmt not supported\n");
751 break;
752 }
753 }
754
755 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300756 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -0300757 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
758
759
760 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300761 switch (q_data_src->fourcc) {
762 case V4L2_PIX_FMT_YVU420:
763 /* Switch Cb and Cr for YVU420 format */
764 picture_cr = picture_y + q_data_src->width * q_data_src->height;
765 picture_cb = picture_cr + q_data_src->width / 2 *
766 q_data_src->height / 2;
767 break;
768 case V4L2_PIX_FMT_YUV420:
769 default:
770 picture_cb = picture_y + q_data_src->width * q_data_src->height;
771 picture_cr = picture_cb + q_data_src->width / 2 *
772 q_data_src->height / 2;
773 break;
774 }
Javier Martin186b2502012-07-26 05:53:35 -0300775
776 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
777 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
778 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
779 coda_write(dev, force_ipicture << 1 & 0x2,
780 CODA_CMD_ENC_PIC_OPTION);
781
782 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
783 coda_write(dev, pic_stream_buffer_size / 1024,
784 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -0300785
786 if (dev->devtype->product == CODA_7541) {
787 coda_write(dev, CODA7_USE_BIT_ENABLE | CODA7_USE_HOST_BIT_ENABLE |
788 CODA7_USE_ME_ENABLE | CODA7_USE_HOST_ME_ENABLE,
789 CODA7_REG_BIT_AXI_SRAM_USE);
790 }
791
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300792 /* 1 second timeout in case CODA locks up */
793 schedule_delayed_work(&dev->timeout, HZ);
794
Philipp Zabel62bed142012-07-25 10:40:39 -0300795 INIT_COMPLETION(dev->done);
Javier Martin186b2502012-07-26 05:53:35 -0300796 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
797}
798
799static int coda_job_ready(void *m2m_priv)
800{
801 struct coda_ctx *ctx = m2m_priv;
802
803 /*
804 * For both 'P' and 'key' frame cases 1 picture
805 * and 1 frame are needed.
806 */
807 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) ||
808 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
809 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
810 "not ready: not enough video buffers.\n");
811 return 0;
812 }
813
Javier Martin186b2502012-07-26 05:53:35 -0300814 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
815 "job ready\n");
816 return 1;
817}
818
819static void coda_job_abort(void *priv)
820{
821 struct coda_ctx *ctx = priv;
822 struct coda_dev *dev = ctx->dev;
823
824 ctx->aborting = 1;
825
826 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
827 "Aborting task\n");
828
829 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
830}
831
832static void coda_lock(void *m2m_priv)
833{
834 struct coda_ctx *ctx = m2m_priv;
835 struct coda_dev *pcdev = ctx->dev;
836 mutex_lock(&pcdev->dev_mutex);
837}
838
839static void coda_unlock(void *m2m_priv)
840{
841 struct coda_ctx *ctx = m2m_priv;
842 struct coda_dev *pcdev = ctx->dev;
843 mutex_unlock(&pcdev->dev_mutex);
844}
845
846static struct v4l2_m2m_ops coda_m2m_ops = {
847 .device_run = coda_device_run,
848 .job_ready = coda_job_ready,
849 .job_abort = coda_job_abort,
850 .lock = coda_lock,
851 .unlock = coda_unlock,
852};
853
854static void set_default_params(struct coda_ctx *ctx)
855{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300856 int max_w;
857 int max_h;
858
859 ctx->codec = &ctx->dev->devtype->codecs[0];
860 max_w = ctx->codec->max_w;
861 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300862
863 ctx->params.codec_mode = CODA_MODE_INVALID;
864 ctx->colorspace = V4L2_COLORSPACE_REC709;
865 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300866 ctx->aborting = 0;
867
868 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300869 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
870 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
871 ctx->q_data[V4L2_M2M_SRC].width = max_w;
872 ctx->q_data[V4L2_M2M_SRC].height = max_h;
873 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
874 ctx->q_data[V4L2_M2M_DST].width = max_w;
875 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300876 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
877}
878
879/*
880 * Queue operations
881 */
882static int coda_queue_setup(struct vb2_queue *vq,
883 const struct v4l2_format *fmt,
884 unsigned int *nbuffers, unsigned int *nplanes,
885 unsigned int sizes[], void *alloc_ctxs[])
886{
887 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300888 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300889 unsigned int size;
890
Philipp Zabele34db062012-08-29 08:22:00 -0300891 q_data = get_q_data(ctx, vq->type);
892 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300893
894 *nplanes = 1;
895 sizes[0] = size;
896
897 alloc_ctxs[0] = ctx->dev->alloc_ctx;
898
899 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
900 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
901
902 return 0;
903}
904
905static int coda_buf_prepare(struct vb2_buffer *vb)
906{
907 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
908 struct coda_q_data *q_data;
909
910 q_data = get_q_data(ctx, vb->vb2_queue->type);
911
912 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
913 v4l2_warn(&ctx->dev->v4l2_dev,
914 "%s data will not fit into plane (%lu < %lu)\n",
915 __func__, vb2_plane_size(vb, 0),
916 (long)q_data->sizeimage);
917 return -EINVAL;
918 }
919
Javier Martin186b2502012-07-26 05:53:35 -0300920 return 0;
921}
922
923static void coda_buf_queue(struct vb2_buffer *vb)
924{
925 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
926 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
927}
928
929static void coda_wait_prepare(struct vb2_queue *q)
930{
931 struct coda_ctx *ctx = vb2_get_drv_priv(q);
932 coda_unlock(ctx);
933}
934
935static void coda_wait_finish(struct vb2_queue *q)
936{
937 struct coda_ctx *ctx = vb2_get_drv_priv(q);
938 coda_lock(ctx);
939}
940
Philipp Zabelec25f682012-07-20 08:54:29 -0300941static void coda_free_framebuffers(struct coda_ctx *ctx)
942{
943 int i;
944
945 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++) {
946 if (ctx->internal_frames[i].vaddr) {
947 dma_free_coherent(&ctx->dev->plat_dev->dev,
948 ctx->internal_frames[i].size,
949 ctx->internal_frames[i].vaddr,
950 ctx->internal_frames[i].paddr);
951 ctx->internal_frames[i].vaddr = NULL;
952 }
953 }
954}
955
Philipp Zabel86eda902013-05-23 10:42:57 -0300956static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
957{
958 struct coda_dev *dev = ctx->dev;
959 u32 *p = ctx->parabuf.vaddr;
960
961 if (dev->devtype->product == CODA_DX6)
962 p[index] = value;
963 else
964 p[index ^ 1] = value;
965}
966
Philipp Zabelec25f682012-07-20 08:54:29 -0300967static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
968{
969 struct coda_dev *dev = ctx->dev;
970
971 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -0300972 dma_addr_t paddr;
973 int ysize;
Philipp Zabelec25f682012-07-20 08:54:29 -0300974 int i;
975
Philipp Zabel86eda902013-05-23 10:42:57 -0300976 ysize = round_up(q_data->width, 8) * height;
977
Philipp Zabelec25f682012-07-20 08:54:29 -0300978 /* Allocate frame buffers */
979 ctx->num_internal_frames = CODA_MAX_FRAMEBUFFERS;
980 for (i = 0; i < ctx->num_internal_frames; i++) {
981 ctx->internal_frames[i].size = q_data->sizeimage;
982 if (fourcc == V4L2_PIX_FMT_H264 && dev->devtype->product != CODA_DX6)
Philipp Zabel86eda902013-05-23 10:42:57 -0300983 ctx->internal_frames[i].size += ysize/4;
Philipp Zabelec25f682012-07-20 08:54:29 -0300984 ctx->internal_frames[i].vaddr = dma_alloc_coherent(
985 &dev->plat_dev->dev, ctx->internal_frames[i].size,
986 &ctx->internal_frames[i].paddr, GFP_KERNEL);
987 if (!ctx->internal_frames[i].vaddr) {
988 coda_free_framebuffers(ctx);
989 return -ENOMEM;
990 }
991 }
992
993 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -0300994 for (i = 0; i < ctx->num_internal_frames; i++) {
995 paddr = ctx->internal_frames[i].paddr;
996 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
997 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
998 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -0300999
Philipp Zabel86eda902013-05-23 10:42:57 -03001000 if (dev->devtype->product != CODA_DX6 && fourcc == V4L2_PIX_FMT_H264)
1001 coda_parabuf_write(ctx, 96 + i, ctx->internal_frames[i].paddr + ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001002 }
1003
1004 return 0;
1005}
1006
Javier Martin3f3f5c72012-10-29 05:20:29 -03001007static int coda_h264_padding(int size, char *p)
1008{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001009 int nal_size;
1010 int diff;
1011
Javier Martin832fbb52012-10-29 08:34:59 -03001012 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001013 if (diff == 0)
1014 return 0;
1015
Javier Martin832fbb52012-10-29 08:34:59 -03001016 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001017 memcpy(p, coda_filler_nal, nal_size);
1018
1019 /* Add rbsp stop bit and trailing at the end */
1020 *(p + nal_size - 1) = 0x80;
1021
1022 return nal_size;
1023}
1024
Philipp Zabeld35167a2013-05-23 10:42:59 -03001025static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1026 int header_code, u8 *header, int *size)
1027{
1028 struct coda_dev *dev = ctx->dev;
1029 int ret;
1030
1031 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1032 CODA_CMD_ENC_HEADER_BB_START);
1033 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1034 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1035 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1036 if (ret < 0) {
1037 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1038 return ret;
1039 }
1040 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1041 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1042 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1043
1044 return 0;
1045}
1046
Javier Martin186b2502012-07-26 05:53:35 -03001047static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1048{
1049 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1050 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1051 u32 bitstream_buf, bitstream_size;
1052 struct coda_dev *dev = ctx->dev;
1053 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -03001054 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001055 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001056 u32 value;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001057 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001058
1059 if (count < 1)
1060 return -EINVAL;
1061
1062 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Philipp Zabelb96904e2013-05-23 10:42:58 -03001063 ctx->streamon_out = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001064 else
Philipp Zabelb96904e2013-05-23 10:42:58 -03001065 ctx->streamon_cap = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001066
Philipp Zabelb96904e2013-05-23 10:42:58 -03001067 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1068 if (ctx->streamon_out) {
1069 if (coda_format_is_yuv(q_data_src->fourcc))
1070 ctx->inst_type = CODA_INST_ENCODER;
1071 else
1072 ctx->inst_type = CODA_INST_DECODER;
1073 }
Javier Martin186b2502012-07-26 05:53:35 -03001074
Philipp Zabel62bed142012-07-25 10:40:39 -03001075 if (coda_isbusy(dev))
1076 if (wait_for_completion_interruptible_timeout(&dev->done, HZ) <= 0)
1077 return -EBUSY;
1078
Philipp Zabelb96904e2013-05-23 10:42:58 -03001079 /* Don't start the coda unless both queues are on */
1080 if (!(ctx->streamon_out & ctx->streamon_cap))
1081 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001082
Philipp Zabelb96904e2013-05-23 10:42:58 -03001083 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001084 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1085 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1086 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1087 bitstream_size = q_data_dst->sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001088 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001089
Philipp Zabelb96904e2013-05-23 10:42:58 -03001090 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1091 q_data_dst->fourcc);
1092 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001093 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1094 return -EINVAL;
1095 }
1096
1097 if (!coda_is_initialized(dev)) {
1098 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1099 return -EFAULT;
1100 }
1101 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1102 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->idx));
1103 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->idx));
1104 switch (dev->devtype->product) {
1105 case CODA_DX6:
1106 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
1107 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1108 break;
1109 default:
1110 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
1111 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
1112 }
1113
Philipp Zabel10436672012-07-02 09:03:55 -03001114 if (dev->devtype->product == CODA_DX6) {
1115 /* Configure the coda */
1116 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
1117 }
Javier Martin186b2502012-07-26 05:53:35 -03001118
1119 /* Could set rotation here if needed */
1120 switch (dev->devtype->product) {
1121 case CODA_DX6:
1122 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001123 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001124 break;
1125 default:
1126 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001127 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001128 }
Javier Martin186b2502012-07-26 05:53:35 -03001129 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
1130 coda_write(dev, ctx->params.framerate,
1131 CODA_CMD_ENC_SEQ_SRC_F_RATE);
1132
Philipp Zabelb96904e2013-05-23 10:42:58 -03001133 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001134 switch (dst_fourcc) {
1135 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -03001136 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
1137 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
1138 break;
1139 case V4L2_PIX_FMT_H264:
Javier Martin186b2502012-07-26 05:53:35 -03001140 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
1141 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
1142 break;
1143 default:
1144 v4l2_err(v4l2_dev,
1145 "dst format (0x%08x) invalid.\n", dst_fourcc);
1146 return -EINVAL;
1147 }
1148
Philipp Zabelc566c782012-08-08 11:59:38 -03001149 switch (ctx->params.slice_mode) {
1150 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
1151 value = 0;
1152 break;
1153 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
1154 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1155 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001156 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03001157 break;
1158 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1159 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1160 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1161 value |= 1 & CODA_SLICING_MODE_MASK;
1162 break;
1163 }
Javier Martin186b2502012-07-26 05:53:35 -03001164 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03001165 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03001166 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1167
1168 if (ctx->params.bitrate) {
1169 /* Rate control enabled */
1170 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1171 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1172 } else {
1173 value = 0;
1174 }
1175 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1176
1177 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1178 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1179
1180 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1181 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1182
1183 /* set default gamma */
1184 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1185 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1186
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03001187 if (CODA_DEFAULT_GAMMA > 0) {
1188 if (dev->devtype->product == CODA_DX6)
1189 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
1190 else
1191 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
1192 } else {
1193 value = 0;
1194 }
Javier Martin186b2502012-07-26 05:53:35 -03001195 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1196
1197 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1198 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1199 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1200 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
Philipp Zabel10436672012-07-02 09:03:55 -03001201 if (dev->devtype->product == CODA_DX6) {
1202 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1203 } else {
1204 coda_write(dev, dev->iram_paddr, CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1205 coda_write(dev, 48 * 1024, CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
1206 }
Javier Martin186b2502012-07-26 05:53:35 -03001207 }
1208
1209 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1210 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1211 return -ETIMEDOUT;
1212 }
1213
1214 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0)
1215 return -EFAULT;
1216
Philipp Zabelec25f682012-07-20 08:54:29 -03001217 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
1218 if (ret < 0)
1219 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001220
Philipp Zabelec25f682012-07-20 08:54:29 -03001221 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03001222 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
1223 if (dev->devtype->product != CODA_DX6) {
1224 coda_write(dev, round_up(q_data_src->width, 8), CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1225 coda_write(dev, dev->iram_paddr + 48 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1226 coda_write(dev, dev->iram_paddr + 53 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1227 coda_write(dev, dev->iram_paddr + 58 * 1024, CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1228 coda_write(dev, dev->iram_paddr + 68 * 1024, CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1229 coda_write(dev, 0x0, CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1230 }
Javier Martin186b2502012-07-26 05:53:35 -03001231 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1232 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1233 return -ETIMEDOUT;
1234 }
1235
1236 /* Save stream headers */
1237 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1238 switch (dst_fourcc) {
1239 case V4L2_PIX_FMT_H264:
1240 /*
1241 * Get SPS in the first frame and copy it to an
1242 * intermediate buffer.
1243 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001244 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1245 &ctx->vpu_header[0][0],
1246 &ctx->vpu_header_size[0]);
1247 if (ret < 0)
1248 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001249
1250 /*
1251 * Get PPS in the first frame and copy it to an
1252 * intermediate buffer.
1253 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001254 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1255 &ctx->vpu_header[1][0],
1256 &ctx->vpu_header_size[1]);
1257 if (ret < 0)
1258 goto out;
1259
Javier Martin3f3f5c72012-10-29 05:20:29 -03001260 /*
1261 * Length of H.264 headers is variable and thus it might not be
1262 * aligned for the coda to append the encoded frame. In that is
1263 * the case a filler NAL must be added to header 2.
1264 */
1265 ctx->vpu_header_size[2] = coda_h264_padding(
1266 (ctx->vpu_header_size[0] +
1267 ctx->vpu_header_size[1]),
1268 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03001269 break;
1270 case V4L2_PIX_FMT_MPEG4:
1271 /*
1272 * Get VOS in the first frame and copy it to an
1273 * intermediate buffer
1274 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001275 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1276 &ctx->vpu_header[0][0],
1277 &ctx->vpu_header_size[0]);
1278 if (ret < 0)
1279 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001280
Philipp Zabeld35167a2013-05-23 10:42:59 -03001281 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1282 &ctx->vpu_header[1][0],
1283 &ctx->vpu_header_size[1]);
1284 if (ret < 0)
1285 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001286
Philipp Zabeld35167a2013-05-23 10:42:59 -03001287 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1288 &ctx->vpu_header[2][0],
1289 &ctx->vpu_header_size[2]);
1290 if (ret < 0)
1291 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001292 break;
1293 default:
1294 /* No more formats need to save headers at the moment */
1295 break;
1296 }
1297
Philipp Zabeld35167a2013-05-23 10:42:59 -03001298out:
1299 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001300}
1301
1302static int coda_stop_streaming(struct vb2_queue *q)
1303{
1304 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001305 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03001306
1307 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1308 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1309 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001310 ctx->streamon_out = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001311 } else {
1312 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1313 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001314 ctx->streamon_cap = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001315 }
1316
Philipp Zabel62bed142012-07-25 10:40:39 -03001317 /* Don't stop the coda unless both queues are off */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001318 if (ctx->streamon_out || ctx->streamon_cap)
Philipp Zabel62bed142012-07-25 10:40:39 -03001319 return 0;
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001320
Philipp Zabel62bed142012-07-25 10:40:39 -03001321 if (coda_isbusy(dev)) {
1322 if (wait_for_completion_interruptible_timeout(&dev->done, HZ) <= 0) {
1323 v4l2_warn(&dev->v4l2_dev,
1324 "%s: timeout, sending SEQ_END anyway\n", __func__);
Javier Martin186b2502012-07-26 05:53:35 -03001325 }
1326 }
1327
Philipp Zabel62bed142012-07-25 10:40:39 -03001328 cancel_delayed_work(&dev->timeout);
1329
1330 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1331 "%s: sent command 'SEQ_END' to coda\n", __func__);
1332 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1333 v4l2_err(&dev->v4l2_dev,
1334 "CODA_COMMAND_SEQ_END failed\n");
1335 return -ETIMEDOUT;
1336 }
1337
1338 coda_free_framebuffers(ctx);
1339
Javier Martin186b2502012-07-26 05:53:35 -03001340 return 0;
1341}
1342
1343static struct vb2_ops coda_qops = {
1344 .queue_setup = coda_queue_setup,
1345 .buf_prepare = coda_buf_prepare,
1346 .buf_queue = coda_buf_queue,
1347 .wait_prepare = coda_wait_prepare,
1348 .wait_finish = coda_wait_finish,
1349 .start_streaming = coda_start_streaming,
1350 .stop_streaming = coda_stop_streaming,
1351};
1352
1353static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1354{
1355 struct coda_ctx *ctx =
1356 container_of(ctrl->handler, struct coda_ctx, ctrls);
1357
1358 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1359 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1360
1361 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001362 case V4L2_CID_HFLIP:
1363 if (ctrl->val)
1364 ctx->params.rot_mode |= CODA_MIR_HOR;
1365 else
1366 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1367 break;
1368 case V4L2_CID_VFLIP:
1369 if (ctrl->val)
1370 ctx->params.rot_mode |= CODA_MIR_VER;
1371 else
1372 ctx->params.rot_mode &= ~CODA_MIR_VER;
1373 break;
Javier Martin186b2502012-07-26 05:53:35 -03001374 case V4L2_CID_MPEG_VIDEO_BITRATE:
1375 ctx->params.bitrate = ctrl->val / 1000;
1376 break;
1377 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1378 ctx->params.gop_size = ctrl->val;
1379 break;
1380 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1381 ctx->params.h264_intra_qp = ctrl->val;
1382 break;
1383 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1384 ctx->params.h264_inter_qp = ctrl->val;
1385 break;
1386 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1387 ctx->params.mpeg4_intra_qp = ctrl->val;
1388 break;
1389 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1390 ctx->params.mpeg4_inter_qp = ctrl->val;
1391 break;
1392 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1393 ctx->params.slice_mode = ctrl->val;
1394 break;
1395 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1396 ctx->params.slice_max_mb = ctrl->val;
1397 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001398 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1399 ctx->params.slice_max_bits = ctrl->val * 8;
1400 break;
Javier Martin186b2502012-07-26 05:53:35 -03001401 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1402 break;
1403 default:
1404 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1405 "Invalid control, id=%d, val=%d\n",
1406 ctrl->id, ctrl->val);
1407 return -EINVAL;
1408 }
1409
1410 return 0;
1411}
1412
1413static struct v4l2_ctrl_ops coda_ctrl_ops = {
1414 .s_ctrl = coda_s_ctrl,
1415};
1416
1417static int coda_ctrls_setup(struct coda_ctx *ctx)
1418{
1419 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1420
1421 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001422 V4L2_CID_HFLIP, 0, 1, 1, 0);
1423 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1424 V4L2_CID_VFLIP, 0, 1, 1, 0);
1425 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001426 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1427 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1428 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1429 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1430 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1431 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1432 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1433 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1434 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1435 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1436 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1437 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1438 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001439 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1440 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001441 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1442 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001443 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1444 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001445 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1446 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1447 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1448 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1449 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1450
1451 if (ctx->ctrls.error) {
1452 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1453 ctx->ctrls.error);
1454 return -EINVAL;
1455 }
1456
1457 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1458}
1459
1460static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1461 struct vb2_queue *dst_vq)
1462{
1463 struct coda_ctx *ctx = priv;
1464 int ret;
1465
Javier Martin186b2502012-07-26 05:53:35 -03001466 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03001467 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001468 src_vq->drv_priv = ctx;
1469 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1470 src_vq->ops = &coda_qops;
1471 src_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001472 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001473
1474 ret = vb2_queue_init(src_vq);
1475 if (ret)
1476 return ret;
1477
Javier Martin186b2502012-07-26 05:53:35 -03001478 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03001479 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001480 dst_vq->drv_priv = ctx;
1481 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1482 dst_vq->ops = &coda_qops;
1483 dst_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001484 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001485
1486 return vb2_queue_init(dst_vq);
1487}
1488
Philipp Zabele11f3e62012-07-25 09:16:58 -03001489static int coda_next_free_instance(struct coda_dev *dev)
1490{
1491 return ffz(dev->instance_mask);
1492}
1493
Javier Martin186b2502012-07-26 05:53:35 -03001494static int coda_open(struct file *file)
1495{
1496 struct coda_dev *dev = video_drvdata(file);
1497 struct coda_ctx *ctx = NULL;
1498 int ret = 0;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001499 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001500
Philipp Zabele11f3e62012-07-25 09:16:58 -03001501 idx = coda_next_free_instance(dev);
1502 if (idx >= CODA_MAX_INSTANCES)
Javier Martin186b2502012-07-26 05:53:35 -03001503 return -EBUSY;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001504 set_bit(idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001505
1506 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1507 if (!ctx)
1508 return -ENOMEM;
1509
1510 v4l2_fh_init(&ctx->fh, video_devdata(file));
1511 file->private_data = &ctx->fh;
1512 v4l2_fh_add(&ctx->fh);
1513 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001514 ctx->idx = idx;
Javier Martin186b2502012-07-26 05:53:35 -03001515
1516 set_default_params(ctx);
1517 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1518 &coda_queue_init);
1519 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03001520 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001521
1522 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1523 __func__, ret);
1524 goto err;
1525 }
1526 ret = coda_ctrls_setup(ctx);
1527 if (ret) {
1528 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1529 goto err;
1530 }
1531
1532 ctx->fh.ctrl_handler = &ctx->ctrls;
1533
1534 ctx->parabuf.vaddr = dma_alloc_coherent(&dev->plat_dev->dev,
1535 CODA_PARA_BUF_SIZE, &ctx->parabuf.paddr, GFP_KERNEL);
1536 if (!ctx->parabuf.vaddr) {
1537 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1538 ret = -ENOMEM;
1539 goto err;
1540 }
1541
1542 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001543 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001544 coda_unlock(ctx);
1545
1546 clk_prepare_enable(dev->clk_per);
1547 clk_prepare_enable(dev->clk_ahb);
1548
1549 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1550 ctx->idx, ctx);
1551
1552 return 0;
1553
1554err:
1555 v4l2_fh_del(&ctx->fh);
1556 v4l2_fh_exit(&ctx->fh);
1557 kfree(ctx);
1558 return ret;
1559}
1560
1561static int coda_release(struct file *file)
1562{
1563 struct coda_dev *dev = video_drvdata(file);
1564 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1565
1566 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1567 ctx);
1568
1569 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001570 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001571 coda_unlock(ctx);
1572
1573 dma_free_coherent(&dev->plat_dev->dev, CODA_PARA_BUF_SIZE,
1574 ctx->parabuf.vaddr, ctx->parabuf.paddr);
1575 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1576 v4l2_ctrl_handler_free(&ctx->ctrls);
1577 clk_disable_unprepare(dev->clk_per);
1578 clk_disable_unprepare(dev->clk_ahb);
1579 v4l2_fh_del(&ctx->fh);
1580 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001581 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001582 kfree(ctx);
1583
1584 return 0;
1585}
1586
1587static unsigned int coda_poll(struct file *file,
1588 struct poll_table_struct *wait)
1589{
1590 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1591 int ret;
1592
1593 coda_lock(ctx);
1594 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1595 coda_unlock(ctx);
1596 return ret;
1597}
1598
1599static int coda_mmap(struct file *file, struct vm_area_struct *vma)
1600{
1601 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1602
1603 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1604}
1605
1606static const struct v4l2_file_operations coda_fops = {
1607 .owner = THIS_MODULE,
1608 .open = coda_open,
1609 .release = coda_release,
1610 .poll = coda_poll,
1611 .unlocked_ioctl = video_ioctl2,
1612 .mmap = coda_mmap,
1613};
1614
1615static irqreturn_t coda_irq_handler(int irq, void *data)
1616{
Philipp Zabelec25f682012-07-20 08:54:29 -03001617 struct vb2_buffer *src_buf, *dst_buf;
Javier Martin186b2502012-07-26 05:53:35 -03001618 struct coda_dev *dev = data;
1619 u32 wr_ptr, start_ptr;
1620 struct coda_ctx *ctx;
1621
Fabio Estevam2249b452012-10-09 23:33:29 -03001622 cancel_delayed_work(&dev->timeout);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001623
Javier Martin186b2502012-07-26 05:53:35 -03001624 /* read status register to attend the IRQ */
1625 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1626 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1627 CODA_REG_BIT_INT_CLEAR);
1628
1629 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1630 if (ctx == NULL) {
1631 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
1632 return IRQ_HANDLED;
1633 }
1634
1635 if (ctx->aborting) {
1636 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1637 "task has been aborted\n");
1638 return IRQ_HANDLED;
1639 }
1640
1641 if (coda_isbusy(ctx->dev)) {
1642 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1643 "coda is still busy!!!!\n");
1644 return IRQ_NONE;
1645 }
1646
Philipp Zabel62bed142012-07-25 10:40:39 -03001647 complete(&dev->done);
1648
Philipp Zabelec25f682012-07-20 08:54:29 -03001649 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1650 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001651
1652 /* Get results from the coda */
1653 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
1654 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1655 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx));
1656 /* Calculate bytesused field */
1657 if (dst_buf->v4l2_buf.sequence == 0) {
1658 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr) +
1659 ctx->vpu_header_size[0] +
1660 ctx->vpu_header_size[1] +
1661 ctx->vpu_header_size[2];
1662 } else {
1663 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr);
1664 }
1665
1666 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1667 wr_ptr - start_ptr);
1668
1669 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1670 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1671
1672 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1673 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1674 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1675 } else {
1676 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1677 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1678 }
1679
Kamil Debskiccd571c2013-04-24 10:38:24 -03001680 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1681 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1682
Philipp Zabelec25f682012-07-20 08:54:29 -03001683 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03001684 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1685
1686 ctx->gopcounter--;
1687 if (ctx->gopcounter < 0)
1688 ctx->gopcounter = ctx->params.gop_size - 1;
1689
1690 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1691 "job finished: encoding frame (%d) (%s)\n",
1692 dst_buf->v4l2_buf.sequence,
1693 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1694 "KEYFRAME" : "PFRAME");
1695
1696 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
1697
1698 return IRQ_HANDLED;
1699}
1700
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001701static void coda_timeout(struct work_struct *work)
1702{
1703 struct coda_ctx *ctx;
1704 struct coda_dev *dev = container_of(to_delayed_work(work),
1705 struct coda_dev, timeout);
1706
Philipp Zabel62bed142012-07-25 10:40:39 -03001707 if (completion_done(&dev->done))
1708 return;
1709
1710 complete(&dev->done);
1711
Philipp Zabel39cc0292013-05-21 10:32:42 -03001712 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001713
1714 mutex_lock(&dev->dev_mutex);
1715 list_for_each_entry(ctx, &dev->instances, list) {
1716 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1717 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1718 }
1719 mutex_unlock(&dev->dev_mutex);
1720}
1721
Javier Martin186b2502012-07-26 05:53:35 -03001722static u32 coda_supported_firmwares[] = {
1723 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001724 CODA_FIRMWARE_VERNUM(CODA_7541, 13, 4, 29),
Javier Martin186b2502012-07-26 05:53:35 -03001725};
1726
1727static bool coda_firmware_supported(u32 vernum)
1728{
1729 int i;
1730
1731 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
1732 if (vernum == coda_supported_firmwares[i])
1733 return true;
1734 return false;
1735}
1736
1737static char *coda_product_name(int product)
1738{
1739 static char buf[9];
1740
1741 switch (product) {
1742 case CODA_DX6:
1743 return "CodaDx6";
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001744 case CODA_7541:
1745 return "CODA7541";
Javier Martin186b2502012-07-26 05:53:35 -03001746 default:
1747 snprintf(buf, sizeof(buf), "(0x%04x)", product);
1748 return buf;
1749 }
1750}
1751
Philipp Zabel87048bb2012-07-02 07:03:43 -03001752static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001753{
1754 u16 product, major, minor, release;
1755 u32 data;
1756 u16 *p;
1757 int i;
1758
1759 clk_prepare_enable(dev->clk_per);
1760 clk_prepare_enable(dev->clk_ahb);
1761
Javier Martin186b2502012-07-26 05:53:35 -03001762 /*
1763 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001764 * The 16-bit chars in the code buffer are in memory access
1765 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001766 * Data in this SRAM survives a reboot.
1767 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001768 p = (u16 *)dev->codebuf.vaddr;
1769 if (dev->devtype->product == CODA_DX6) {
1770 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1771 data = CODA_DOWN_ADDRESS_SET(i) |
1772 CODA_DOWN_DATA_SET(p[i ^ 1]);
1773 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1774 }
1775 } else {
1776 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1777 data = CODA_DOWN_ADDRESS_SET(i) |
1778 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1779 3 - (i % 4)]);
1780 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1781 }
Javier Martin186b2502012-07-26 05:53:35 -03001782 }
Javier Martin186b2502012-07-26 05:53:35 -03001783
Philipp Zabel9acf7692013-05-23 10:42:56 -03001784 /* Clear registers */
1785 for (i = 0; i < 64; i++)
1786 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1787
Javier Martin186b2502012-07-26 05:53:35 -03001788 /* Tell the BIT where to find everything it needs */
1789 coda_write(dev, dev->workbuf.paddr,
1790 CODA_REG_BIT_WORK_BUF_ADDR);
1791 coda_write(dev, dev->codebuf.paddr,
1792 CODA_REG_BIT_CODE_BUF_ADDR);
1793 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1794
1795 /* Set default values */
1796 switch (dev->devtype->product) {
1797 case CODA_DX6:
1798 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1799 break;
1800 default:
1801 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1802 }
1803 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001804
1805 if (dev->devtype->product != CODA_DX6)
1806 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1807
Javier Martin186b2502012-07-26 05:53:35 -03001808 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1809 CODA_REG_BIT_INT_ENABLE);
1810
1811 /* Reset VPU and start processor */
1812 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1813 data |= CODA_REG_RESET_ENABLE;
1814 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1815 udelay(10);
1816 data &= ~CODA_REG_RESET_ENABLE;
1817 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1818 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1819
1820 /* Load firmware */
1821 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
1822 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
1823 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
1824 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
1825 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
1826 if (coda_wait_timeout(dev)) {
1827 clk_disable_unprepare(dev->clk_per);
1828 clk_disable_unprepare(dev->clk_ahb);
1829 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
1830 return -EIO;
1831 }
1832
1833 /* Check we are compatible with the loaded firmware */
1834 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
1835 product = CODA_FIRMWARE_PRODUCT(data);
1836 major = CODA_FIRMWARE_MAJOR(data);
1837 minor = CODA_FIRMWARE_MINOR(data);
1838 release = CODA_FIRMWARE_RELEASE(data);
1839
1840 clk_disable_unprepare(dev->clk_per);
1841 clk_disable_unprepare(dev->clk_ahb);
1842
1843 if (product != dev->devtype->product) {
1844 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
1845 " Version: %u.%u.%u\n",
1846 coda_product_name(dev->devtype->product),
1847 coda_product_name(product), major, minor, release);
1848 return -EINVAL;
1849 }
1850
1851 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
1852 coda_product_name(product));
1853
1854 if (coda_firmware_supported(data)) {
1855 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
1856 major, minor, release);
1857 } else {
1858 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
1859 "%u.%u.%u\n", major, minor, release);
1860 }
1861
1862 return 0;
1863}
1864
1865static void coda_fw_callback(const struct firmware *fw, void *context)
1866{
1867 struct coda_dev *dev = context;
1868 struct platform_device *pdev = dev->plat_dev;
1869 int ret;
1870
1871 if (!fw) {
1872 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1873 return;
1874 }
1875
1876 /* allocate auxiliary per-device code buffer for the BIT processor */
1877 dev->codebuf.size = fw->size;
1878 dev->codebuf.vaddr = dma_alloc_coherent(&pdev->dev, fw->size,
1879 &dev->codebuf.paddr,
1880 GFP_KERNEL);
1881 if (!dev->codebuf.vaddr) {
1882 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1883 return;
1884 }
1885
Philipp Zabel87048bb2012-07-02 07:03:43 -03001886 /* Copy the whole firmware image to the code buffer */
1887 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1888 release_firmware(fw);
1889
1890 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03001891 if (ret) {
1892 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1893 return;
1894 }
1895
1896 dev->vfd.fops = &coda_fops,
1897 dev->vfd.ioctl_ops = &coda_ioctl_ops;
1898 dev->vfd.release = video_device_release_empty,
1899 dev->vfd.lock = &dev->dev_mutex;
1900 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03001901 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03001902 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
1903 video_set_drvdata(&dev->vfd, dev);
1904
1905 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1906 if (IS_ERR(dev->alloc_ctx)) {
1907 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1908 return;
1909 }
1910
1911 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1912 if (IS_ERR(dev->m2m_dev)) {
1913 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1914 goto rel_ctx;
1915 }
1916
1917 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
1918 if (ret) {
1919 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1920 goto rel_m2m;
1921 }
1922 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
1923 dev->vfd.num);
1924
1925 return;
1926
1927rel_m2m:
1928 v4l2_m2m_release(dev->m2m_dev);
1929rel_ctx:
1930 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1931}
1932
1933static int coda_firmware_request(struct coda_dev *dev)
1934{
1935 char *fw = dev->devtype->firmware;
1936
1937 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1938 coda_product_name(dev->devtype->product));
1939
1940 return request_firmware_nowait(THIS_MODULE, true,
1941 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1942}
1943
1944enum coda_platform {
1945 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001946 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03001947};
1948
Emil Goodec06d8752012-08-14 17:44:42 -03001949static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001950 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03001951 .firmware = "v4l-codadx6-imx27.bin",
1952 .product = CODA_DX6,
1953 .codecs = codadx6_codecs,
1954 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03001955 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001956 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03001957 .firmware = "v4l-coda7541-imx53.bin",
1958 .product = CODA_7541,
1959 .codecs = coda7_codecs,
1960 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001961 },
Javier Martin186b2502012-07-26 05:53:35 -03001962};
1963
1964static struct platform_device_id coda_platform_ids[] = {
1965 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001966 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001967 { /* sentinel */ }
1968};
1969MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1970
1971#ifdef CONFIG_OF
1972static const struct of_device_id coda_dt_ids[] = {
1973 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001974 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03001975 { /* sentinel */ }
1976};
1977MODULE_DEVICE_TABLE(of, coda_dt_ids);
1978#endif
1979
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001980static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001981{
1982 const struct of_device_id *of_id =
1983 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1984 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001985 struct coda_platform_data *pdata = pdev->dev.platform_data;
1986 struct device_node *np = pdev->dev.of_node;
1987 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001988 struct coda_dev *dev;
1989 struct resource *res;
1990 int ret, irq;
1991
1992 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1993 if (!dev) {
1994 dev_err(&pdev->dev, "Not enough memory for %s\n",
1995 CODA_NAME);
1996 return -ENOMEM;
1997 }
1998
1999 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002000 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002001 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Philipp Zabel62bed142012-07-25 10:40:39 -03002002 init_completion(&dev->done);
2003 complete(&dev->done);
Javier Martin186b2502012-07-26 05:53:35 -03002004
2005 dev->plat_dev = pdev;
2006 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2007 if (IS_ERR(dev->clk_per)) {
2008 dev_err(&pdev->dev, "Could not get per clock\n");
2009 return PTR_ERR(dev->clk_per);
2010 }
2011
2012 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2013 if (IS_ERR(dev->clk_ahb)) {
2014 dev_err(&pdev->dev, "Could not get ahb clock\n");
2015 return PTR_ERR(dev->clk_ahb);
2016 }
2017
2018 /* Get memory for physical registers */
2019 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2020 if (res == NULL) {
2021 dev_err(&pdev->dev, "failed to get memory region resource\n");
2022 return -ENOENT;
2023 }
2024
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002025 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2026 if (IS_ERR(dev->regs_base))
2027 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002028
2029 /* IRQ */
2030 irq = platform_get_irq(pdev, 0);
2031 if (irq < 0) {
2032 dev_err(&pdev->dev, "failed to get irq resource\n");
2033 return -ENOENT;
2034 }
2035
2036 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
2037 0, CODA_NAME, dev) < 0) {
2038 dev_err(&pdev->dev, "failed to request irq\n");
2039 return -ENOENT;
2040 }
2041
Philipp Zabel657eee72013-04-29 16:17:14 -07002042 /* Get IRAM pool from device tree or platform data */
2043 pool = of_get_named_gen_pool(np, "iram", 0);
2044 if (!pool && pdata)
2045 pool = dev_get_gen_pool(pdata->iram_dev);
2046 if (!pool) {
2047 dev_err(&pdev->dev, "iram pool not available\n");
2048 return -ENOMEM;
2049 }
2050 dev->iram_pool = pool;
2051
Javier Martin186b2502012-07-26 05:53:35 -03002052 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2053 if (ret)
2054 return ret;
2055
2056 mutex_init(&dev->dev_mutex);
2057
2058 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2059
2060 if (of_id) {
2061 dev->devtype = of_id->data;
2062 } else if (pdev_id) {
2063 dev->devtype = &coda_devdata[pdev_id->driver_data];
2064 } else {
2065 v4l2_device_unregister(&dev->v4l2_dev);
2066 return -EINVAL;
2067 }
2068
2069 /* allocate auxiliary per-device buffers for the BIT processor */
2070 switch (dev->devtype->product) {
2071 case CODA_DX6:
2072 dev->workbuf.size = CODADX6_WORK_BUF_SIZE;
2073 break;
2074 default:
2075 dev->workbuf.size = CODA7_WORK_BUF_SIZE;
2076 }
2077 dev->workbuf.vaddr = dma_alloc_coherent(&pdev->dev, dev->workbuf.size,
2078 &dev->workbuf.paddr,
2079 GFP_KERNEL);
2080 if (!dev->workbuf.vaddr) {
2081 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2082 v4l2_device_unregister(&dev->v4l2_dev);
2083 return -ENOMEM;
2084 }
2085
Philipp Zabel657eee72013-04-29 16:17:14 -07002086 if (dev->devtype->product == CODA_DX6)
2087 dev->iram_size = CODADX6_IRAM_SIZE;
2088 else
2089 dev->iram_size = CODA7_IRAM_SIZE;
2090 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
2091 if (!dev->iram_vaddr) {
2092 dev_err(&pdev->dev, "unable to alloc iram\n");
2093 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03002094 }
Philipp Zabel657eee72013-04-29 16:17:14 -07002095 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
2096 dev->iram_vaddr);
Philipp Zabel10436672012-07-02 09:03:55 -03002097
Javier Martin186b2502012-07-26 05:53:35 -03002098 platform_set_drvdata(pdev, dev);
2099
2100 return coda_firmware_request(dev);
2101}
2102
2103static int coda_remove(struct platform_device *pdev)
2104{
2105 struct coda_dev *dev = platform_get_drvdata(pdev);
2106
2107 video_unregister_device(&dev->vfd);
2108 if (dev->m2m_dev)
2109 v4l2_m2m_release(dev->m2m_dev);
2110 if (dev->alloc_ctx)
2111 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2112 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002113 if (dev->iram_vaddr)
2114 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Javier Martin186b2502012-07-26 05:53:35 -03002115 if (dev->codebuf.vaddr)
2116 dma_free_coherent(&pdev->dev, dev->codebuf.size,
2117 &dev->codebuf.vaddr, dev->codebuf.paddr);
2118 if (dev->workbuf.vaddr)
2119 dma_free_coherent(&pdev->dev, dev->workbuf.size, &dev->workbuf.vaddr,
2120 dev->workbuf.paddr);
2121 return 0;
2122}
2123
2124static struct platform_driver coda_driver = {
2125 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002126 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002127 .driver = {
2128 .name = CODA_NAME,
2129 .owner = THIS_MODULE,
2130 .of_match_table = of_match_ptr(coda_dt_ids),
2131 },
2132 .id_table = coda_platform_ids,
2133};
2134
2135module_platform_driver(coda_driver);
2136
2137MODULE_LICENSE("GPL");
2138MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2139MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");