blob: f9c4014ee40c3a215a1e11eef499a1d3297ace1c [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;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300140 struct mutex coda_mutex;
Javier Martin186b2502012-07-26 05:53:35 -0300141 struct v4l2_m2m_dev *m2m_dev;
142 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300143 struct list_head instances;
144 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300145 struct delayed_work timeout;
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
Philipp Zabelfcb62822013-05-23 10:43:00 -0300675 mutex_lock(&dev->coda_mutex);
676
Javier Martin186b2502012-07-26 05:53:35 -0300677 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
678 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
679 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
680 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300681 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300682
683 src_buf->v4l2_buf.sequence = ctx->isequence;
684 dst_buf->v4l2_buf.sequence = ctx->isequence;
685 ctx->isequence++;
686
687 /*
688 * Workaround coda firmware BUG that only marks the first
689 * frame as IDR. This is a problem for some decoders that can't
690 * recover when a frame is lost.
691 */
692 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
693 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
694 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
695 } else {
696 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
697 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
698 }
699
700 /*
701 * Copy headers at the beginning of the first frame for H.264 only.
702 * In MPEG4 they are already copied by the coda.
703 */
704 if (src_buf->v4l2_buf.sequence == 0) {
705 pic_stream_buffer_addr =
706 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
707 ctx->vpu_header_size[0] +
708 ctx->vpu_header_size[1] +
709 ctx->vpu_header_size[2];
710 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
711 ctx->vpu_header_size[0] -
712 ctx->vpu_header_size[1] -
713 ctx->vpu_header_size[2];
714 memcpy(vb2_plane_vaddr(dst_buf, 0),
715 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
716 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
717 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
718 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
719 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
720 ctx->vpu_header_size[2]);
721 } else {
722 pic_stream_buffer_addr =
723 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
724 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
725 }
726
727 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
728 force_ipicture = 1;
729 switch (dst_fourcc) {
730 case V4L2_PIX_FMT_H264:
731 quant_param = ctx->params.h264_intra_qp;
732 break;
733 case V4L2_PIX_FMT_MPEG4:
734 quant_param = ctx->params.mpeg4_intra_qp;
735 break;
736 default:
737 v4l2_warn(&ctx->dev->v4l2_dev,
738 "cannot set intra qp, fmt not supported\n");
739 break;
740 }
741 } else {
742 force_ipicture = 0;
743 switch (dst_fourcc) {
744 case V4L2_PIX_FMT_H264:
745 quant_param = ctx->params.h264_inter_qp;
746 break;
747 case V4L2_PIX_FMT_MPEG4:
748 quant_param = ctx->params.mpeg4_inter_qp;
749 break;
750 default:
751 v4l2_warn(&ctx->dev->v4l2_dev,
752 "cannot set inter qp, fmt not supported\n");
753 break;
754 }
755 }
756
757 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300758 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -0300759 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
760
761
762 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300763 switch (q_data_src->fourcc) {
764 case V4L2_PIX_FMT_YVU420:
765 /* Switch Cb and Cr for YVU420 format */
766 picture_cr = picture_y + q_data_src->width * q_data_src->height;
767 picture_cb = picture_cr + q_data_src->width / 2 *
768 q_data_src->height / 2;
769 break;
770 case V4L2_PIX_FMT_YUV420:
771 default:
772 picture_cb = picture_y + q_data_src->width * q_data_src->height;
773 picture_cr = picture_cb + q_data_src->width / 2 *
774 q_data_src->height / 2;
775 break;
776 }
Javier Martin186b2502012-07-26 05:53:35 -0300777
778 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
779 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
780 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
781 coda_write(dev, force_ipicture << 1 & 0x2,
782 CODA_CMD_ENC_PIC_OPTION);
783
784 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
785 coda_write(dev, pic_stream_buffer_size / 1024,
786 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -0300787
788 if (dev->devtype->product == CODA_7541) {
789 coda_write(dev, CODA7_USE_BIT_ENABLE | CODA7_USE_HOST_BIT_ENABLE |
790 CODA7_USE_ME_ENABLE | CODA7_USE_HOST_ME_ENABLE,
791 CODA7_REG_BIT_AXI_SRAM_USE);
792 }
793
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300794 /* 1 second timeout in case CODA locks up */
795 schedule_delayed_work(&dev->timeout, HZ);
796
Javier Martin186b2502012-07-26 05:53:35 -0300797 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
798}
799
800static int coda_job_ready(void *m2m_priv)
801{
802 struct coda_ctx *ctx = m2m_priv;
803
804 /*
805 * For both 'P' and 'key' frame cases 1 picture
806 * and 1 frame are needed.
807 */
808 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) ||
809 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
810 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
811 "not ready: not enough video buffers.\n");
812 return 0;
813 }
814
Javier Martin186b2502012-07-26 05:53:35 -0300815 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
816 "job ready\n");
817 return 1;
818}
819
820static void coda_job_abort(void *priv)
821{
822 struct coda_ctx *ctx = priv;
823 struct coda_dev *dev = ctx->dev;
824
825 ctx->aborting = 1;
826
827 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
828 "Aborting task\n");
829
830 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
831}
832
833static void coda_lock(void *m2m_priv)
834{
835 struct coda_ctx *ctx = m2m_priv;
836 struct coda_dev *pcdev = ctx->dev;
837 mutex_lock(&pcdev->dev_mutex);
838}
839
840static void coda_unlock(void *m2m_priv)
841{
842 struct coda_ctx *ctx = m2m_priv;
843 struct coda_dev *pcdev = ctx->dev;
844 mutex_unlock(&pcdev->dev_mutex);
845}
846
847static struct v4l2_m2m_ops coda_m2m_ops = {
848 .device_run = coda_device_run,
849 .job_ready = coda_job_ready,
850 .job_abort = coda_job_abort,
851 .lock = coda_lock,
852 .unlock = coda_unlock,
853};
854
855static void set_default_params(struct coda_ctx *ctx)
856{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300857 int max_w;
858 int max_h;
859
860 ctx->codec = &ctx->dev->devtype->codecs[0];
861 max_w = ctx->codec->max_w;
862 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300863
864 ctx->params.codec_mode = CODA_MODE_INVALID;
865 ctx->colorspace = V4L2_COLORSPACE_REC709;
866 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300867 ctx->aborting = 0;
868
869 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300870 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
871 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
872 ctx->q_data[V4L2_M2M_SRC].width = max_w;
873 ctx->q_data[V4L2_M2M_SRC].height = max_h;
874 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
875 ctx->q_data[V4L2_M2M_DST].width = max_w;
876 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300877 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
878}
879
880/*
881 * Queue operations
882 */
883static int coda_queue_setup(struct vb2_queue *vq,
884 const struct v4l2_format *fmt,
885 unsigned int *nbuffers, unsigned int *nplanes,
886 unsigned int sizes[], void *alloc_ctxs[])
887{
888 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300889 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300890 unsigned int size;
891
Philipp Zabele34db062012-08-29 08:22:00 -0300892 q_data = get_q_data(ctx, vq->type);
893 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300894
895 *nplanes = 1;
896 sizes[0] = size;
897
898 alloc_ctxs[0] = ctx->dev->alloc_ctx;
899
900 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
901 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
902
903 return 0;
904}
905
906static int coda_buf_prepare(struct vb2_buffer *vb)
907{
908 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
909 struct coda_q_data *q_data;
910
911 q_data = get_q_data(ctx, vb->vb2_queue->type);
912
913 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
914 v4l2_warn(&ctx->dev->v4l2_dev,
915 "%s data will not fit into plane (%lu < %lu)\n",
916 __func__, vb2_plane_size(vb, 0),
917 (long)q_data->sizeimage);
918 return -EINVAL;
919 }
920
Javier Martin186b2502012-07-26 05:53:35 -0300921 return 0;
922}
923
924static void coda_buf_queue(struct vb2_buffer *vb)
925{
926 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
927 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
928}
929
930static void coda_wait_prepare(struct vb2_queue *q)
931{
932 struct coda_ctx *ctx = vb2_get_drv_priv(q);
933 coda_unlock(ctx);
934}
935
936static void coda_wait_finish(struct vb2_queue *q)
937{
938 struct coda_ctx *ctx = vb2_get_drv_priv(q);
939 coda_lock(ctx);
940}
941
Philipp Zabelec25f682012-07-20 08:54:29 -0300942static void coda_free_framebuffers(struct coda_ctx *ctx)
943{
944 int i;
945
946 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++) {
947 if (ctx->internal_frames[i].vaddr) {
948 dma_free_coherent(&ctx->dev->plat_dev->dev,
949 ctx->internal_frames[i].size,
950 ctx->internal_frames[i].vaddr,
951 ctx->internal_frames[i].paddr);
952 ctx->internal_frames[i].vaddr = NULL;
953 }
954 }
955}
956
Philipp Zabel86eda902013-05-23 10:42:57 -0300957static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
958{
959 struct coda_dev *dev = ctx->dev;
960 u32 *p = ctx->parabuf.vaddr;
961
962 if (dev->devtype->product == CODA_DX6)
963 p[index] = value;
964 else
965 p[index ^ 1] = value;
966}
967
Philipp Zabelec25f682012-07-20 08:54:29 -0300968static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
969{
970 struct coda_dev *dev = ctx->dev;
971
972 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -0300973 dma_addr_t paddr;
974 int ysize;
Philipp Zabelec25f682012-07-20 08:54:29 -0300975 int i;
976
Philipp Zabel86eda902013-05-23 10:42:57 -0300977 ysize = round_up(q_data->width, 8) * height;
978
Philipp Zabelec25f682012-07-20 08:54:29 -0300979 /* Allocate frame buffers */
980 ctx->num_internal_frames = CODA_MAX_FRAMEBUFFERS;
981 for (i = 0; i < ctx->num_internal_frames; i++) {
982 ctx->internal_frames[i].size = q_data->sizeimage;
983 if (fourcc == V4L2_PIX_FMT_H264 && dev->devtype->product != CODA_DX6)
Philipp Zabel86eda902013-05-23 10:42:57 -0300984 ctx->internal_frames[i].size += ysize/4;
Philipp Zabelec25f682012-07-20 08:54:29 -0300985 ctx->internal_frames[i].vaddr = dma_alloc_coherent(
986 &dev->plat_dev->dev, ctx->internal_frames[i].size,
987 &ctx->internal_frames[i].paddr, GFP_KERNEL);
988 if (!ctx->internal_frames[i].vaddr) {
989 coda_free_framebuffers(ctx);
990 return -ENOMEM;
991 }
992 }
993
994 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -0300995 for (i = 0; i < ctx->num_internal_frames; i++) {
996 paddr = ctx->internal_frames[i].paddr;
997 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
998 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
999 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001000
Philipp Zabel86eda902013-05-23 10:42:57 -03001001 if (dev->devtype->product != CODA_DX6 && fourcc == V4L2_PIX_FMT_H264)
1002 coda_parabuf_write(ctx, 96 + i, ctx->internal_frames[i].paddr + ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001003 }
1004
1005 return 0;
1006}
1007
Javier Martin3f3f5c72012-10-29 05:20:29 -03001008static int coda_h264_padding(int size, char *p)
1009{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001010 int nal_size;
1011 int diff;
1012
Javier Martin832fbb52012-10-29 08:34:59 -03001013 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001014 if (diff == 0)
1015 return 0;
1016
Javier Martin832fbb52012-10-29 08:34:59 -03001017 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001018 memcpy(p, coda_filler_nal, nal_size);
1019
1020 /* Add rbsp stop bit and trailing at the end */
1021 *(p + nal_size - 1) = 0x80;
1022
1023 return nal_size;
1024}
1025
Philipp Zabeld35167a2013-05-23 10:42:59 -03001026static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1027 int header_code, u8 *header, int *size)
1028{
1029 struct coda_dev *dev = ctx->dev;
1030 int ret;
1031
1032 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1033 CODA_CMD_ENC_HEADER_BB_START);
1034 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1035 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1036 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1037 if (ret < 0) {
1038 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1039 return ret;
1040 }
1041 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx)) -
1042 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1043 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1044
1045 return 0;
1046}
1047
Javier Martin186b2502012-07-26 05:53:35 -03001048static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1049{
1050 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1051 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1052 u32 bitstream_buf, bitstream_size;
1053 struct coda_dev *dev = ctx->dev;
1054 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -03001055 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001056 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001057 u32 value;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001058 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001059
1060 if (count < 1)
1061 return -EINVAL;
1062
1063 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
Philipp Zabelb96904e2013-05-23 10:42:58 -03001064 ctx->streamon_out = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001065 else
Philipp Zabelb96904e2013-05-23 10:42:58 -03001066 ctx->streamon_cap = 1;
Javier Martin186b2502012-07-26 05:53:35 -03001067
Philipp Zabelb96904e2013-05-23 10:42:58 -03001068 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1069 if (ctx->streamon_out) {
1070 if (coda_format_is_yuv(q_data_src->fourcc))
1071 ctx->inst_type = CODA_INST_ENCODER;
1072 else
1073 ctx->inst_type = CODA_INST_DECODER;
1074 }
Javier Martin186b2502012-07-26 05:53:35 -03001075
Philipp Zabelb96904e2013-05-23 10:42:58 -03001076 /* Don't start the coda unless both queues are on */
1077 if (!(ctx->streamon_out & ctx->streamon_cap))
1078 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001079
Philipp Zabelb96904e2013-05-23 10:42:58 -03001080 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001081 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1082 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1083 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1084 bitstream_size = q_data_dst->sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001085 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001086
Philipp Zabelb96904e2013-05-23 10:42:58 -03001087 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1088 q_data_dst->fourcc);
1089 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001090 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1091 return -EINVAL;
1092 }
1093
1094 if (!coda_is_initialized(dev)) {
1095 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1096 return -EFAULT;
1097 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001098
1099 mutex_lock(&dev->coda_mutex);
1100
Javier Martin186b2502012-07-26 05:53:35 -03001101 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);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001146 ret = -EINVAL;
1147 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001148 }
1149
Philipp Zabelc566c782012-08-08 11:59:38 -03001150 switch (ctx->params.slice_mode) {
1151 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
1152 value = 0;
1153 break;
1154 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
1155 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1156 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03001157 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03001158 break;
1159 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
1160 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
1161 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
1162 value |= 1 & CODA_SLICING_MODE_MASK;
1163 break;
1164 }
Javier Martin186b2502012-07-26 05:53:35 -03001165 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03001166 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03001167 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
1168
1169 if (ctx->params.bitrate) {
1170 /* Rate control enabled */
1171 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
1172 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
1173 } else {
1174 value = 0;
1175 }
1176 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
1177
1178 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
1179 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
1180
1181 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
1182 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
1183
1184 /* set default gamma */
1185 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
1186 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
1187
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03001188 if (CODA_DEFAULT_GAMMA > 0) {
1189 if (dev->devtype->product == CODA_DX6)
1190 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
1191 else
1192 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
1193 } else {
1194 value = 0;
1195 }
Javier Martin186b2502012-07-26 05:53:35 -03001196 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
1197
1198 if (dst_fourcc == V4L2_PIX_FMT_H264) {
1199 value = (FMO_SLICE_SAVE_BUF_SIZE << 7);
1200 value |= (0 & CODA_FMOPARAM_TYPE_MASK) << CODA_FMOPARAM_TYPE_OFFSET;
1201 value |= 0 & CODA_FMOPARAM_SLICENUM_MASK;
Philipp Zabel10436672012-07-02 09:03:55 -03001202 if (dev->devtype->product == CODA_DX6) {
1203 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
1204 } else {
1205 coda_write(dev, dev->iram_paddr, CODA7_CMD_ENC_SEQ_SEARCH_BASE);
1206 coda_write(dev, 48 * 1024, CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
1207 }
Javier Martin186b2502012-07-26 05:53:35 -03001208 }
1209
Philipp Zabelfcb62822013-05-23 10:43:00 -03001210 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
1211 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001212 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001213 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001214 }
1215
Philipp Zabelfcb62822013-05-23 10:43:00 -03001216 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
1217 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
1218 ret = -EFAULT;
1219 goto out;
1220 }
Javier Martin186b2502012-07-26 05:53:35 -03001221
Philipp Zabelec25f682012-07-20 08:54:29 -03001222 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001223 if (ret < 0) {
1224 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
1225 goto out;
1226 }
Javier Martin186b2502012-07-26 05:53:35 -03001227
Philipp Zabelec25f682012-07-20 08:54:29 -03001228 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03001229 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
1230 if (dev->devtype->product != CODA_DX6) {
1231 coda_write(dev, round_up(q_data_src->width, 8), CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
1232 coda_write(dev, dev->iram_paddr + 48 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1233 coda_write(dev, dev->iram_paddr + 53 * 1024, CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1234 coda_write(dev, dev->iram_paddr + 58 * 1024, CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1235 coda_write(dev, dev->iram_paddr + 68 * 1024, CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1236 coda_write(dev, 0x0, CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1237 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001238 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
1239 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001240 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001241 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001242 }
1243
1244 /* Save stream headers */
1245 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1246 switch (dst_fourcc) {
1247 case V4L2_PIX_FMT_H264:
1248 /*
1249 * Get SPS in the first frame and copy it to an
1250 * intermediate buffer.
1251 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001252 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
1253 &ctx->vpu_header[0][0],
1254 &ctx->vpu_header_size[0]);
1255 if (ret < 0)
1256 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001257
1258 /*
1259 * Get PPS in the first frame and copy it to an
1260 * intermediate buffer.
1261 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001262 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
1263 &ctx->vpu_header[1][0],
1264 &ctx->vpu_header_size[1]);
1265 if (ret < 0)
1266 goto out;
1267
Javier Martin3f3f5c72012-10-29 05:20:29 -03001268 /*
1269 * Length of H.264 headers is variable and thus it might not be
1270 * aligned for the coda to append the encoded frame. In that is
1271 * the case a filler NAL must be added to header 2.
1272 */
1273 ctx->vpu_header_size[2] = coda_h264_padding(
1274 (ctx->vpu_header_size[0] +
1275 ctx->vpu_header_size[1]),
1276 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03001277 break;
1278 case V4L2_PIX_FMT_MPEG4:
1279 /*
1280 * Get VOS in the first frame and copy it to an
1281 * intermediate buffer
1282 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03001283 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
1284 &ctx->vpu_header[0][0],
1285 &ctx->vpu_header_size[0]);
1286 if (ret < 0)
1287 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001288
Philipp Zabeld35167a2013-05-23 10:42:59 -03001289 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
1290 &ctx->vpu_header[1][0],
1291 &ctx->vpu_header_size[1]);
1292 if (ret < 0)
1293 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001294
Philipp Zabeld35167a2013-05-23 10:42:59 -03001295 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
1296 &ctx->vpu_header[2][0],
1297 &ctx->vpu_header_size[2]);
1298 if (ret < 0)
1299 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03001300 break;
1301 default:
1302 /* No more formats need to save headers at the moment */
1303 break;
1304 }
1305
Philipp Zabeld35167a2013-05-23 10:42:59 -03001306out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03001307 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03001308 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001309}
1310
1311static int coda_stop_streaming(struct vb2_queue *q)
1312{
1313 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001314 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03001315
1316 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1317 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1318 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001319 ctx->streamon_out = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001320 } else {
1321 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1322 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001323 ctx->streamon_cap = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001324 }
1325
Philipp Zabel62bed142012-07-25 10:40:39 -03001326 /* Don't stop the coda unless both queues are off */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001327 if (ctx->streamon_out || ctx->streamon_cap)
Philipp Zabel62bed142012-07-25 10:40:39 -03001328 return 0;
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001329
Philipp Zabel62bed142012-07-25 10:40:39 -03001330 cancel_delayed_work(&dev->timeout);
1331
Philipp Zabelfcb62822013-05-23 10:43:00 -03001332 mutex_lock(&dev->coda_mutex);
Philipp Zabel62bed142012-07-25 10:40:39 -03001333 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1334 "%s: sent command 'SEQ_END' to coda\n", __func__);
1335 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1336 v4l2_err(&dev->v4l2_dev,
1337 "CODA_COMMAND_SEQ_END failed\n");
1338 return -ETIMEDOUT;
1339 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001340 mutex_unlock(&dev->coda_mutex);
Philipp Zabel62bed142012-07-25 10:40:39 -03001341
1342 coda_free_framebuffers(ctx);
1343
Javier Martin186b2502012-07-26 05:53:35 -03001344 return 0;
1345}
1346
1347static struct vb2_ops coda_qops = {
1348 .queue_setup = coda_queue_setup,
1349 .buf_prepare = coda_buf_prepare,
1350 .buf_queue = coda_buf_queue,
1351 .wait_prepare = coda_wait_prepare,
1352 .wait_finish = coda_wait_finish,
1353 .start_streaming = coda_start_streaming,
1354 .stop_streaming = coda_stop_streaming,
1355};
1356
1357static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1358{
1359 struct coda_ctx *ctx =
1360 container_of(ctrl->handler, struct coda_ctx, ctrls);
1361
1362 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1363 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1364
1365 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001366 case V4L2_CID_HFLIP:
1367 if (ctrl->val)
1368 ctx->params.rot_mode |= CODA_MIR_HOR;
1369 else
1370 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1371 break;
1372 case V4L2_CID_VFLIP:
1373 if (ctrl->val)
1374 ctx->params.rot_mode |= CODA_MIR_VER;
1375 else
1376 ctx->params.rot_mode &= ~CODA_MIR_VER;
1377 break;
Javier Martin186b2502012-07-26 05:53:35 -03001378 case V4L2_CID_MPEG_VIDEO_BITRATE:
1379 ctx->params.bitrate = ctrl->val / 1000;
1380 break;
1381 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1382 ctx->params.gop_size = ctrl->val;
1383 break;
1384 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1385 ctx->params.h264_intra_qp = ctrl->val;
1386 break;
1387 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1388 ctx->params.h264_inter_qp = ctrl->val;
1389 break;
1390 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1391 ctx->params.mpeg4_intra_qp = ctrl->val;
1392 break;
1393 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1394 ctx->params.mpeg4_inter_qp = ctrl->val;
1395 break;
1396 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1397 ctx->params.slice_mode = ctrl->val;
1398 break;
1399 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1400 ctx->params.slice_max_mb = ctrl->val;
1401 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001402 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1403 ctx->params.slice_max_bits = ctrl->val * 8;
1404 break;
Javier Martin186b2502012-07-26 05:53:35 -03001405 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1406 break;
1407 default:
1408 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1409 "Invalid control, id=%d, val=%d\n",
1410 ctrl->id, ctrl->val);
1411 return -EINVAL;
1412 }
1413
1414 return 0;
1415}
1416
1417static struct v4l2_ctrl_ops coda_ctrl_ops = {
1418 .s_ctrl = coda_s_ctrl,
1419};
1420
1421static int coda_ctrls_setup(struct coda_ctx *ctx)
1422{
1423 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1424
1425 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001426 V4L2_CID_HFLIP, 0, 1, 1, 0);
1427 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1428 V4L2_CID_VFLIP, 0, 1, 1, 0);
1429 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001430 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1431 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1432 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1433 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1434 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
1435 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1436 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
1437 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1438 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1439 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1440 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1441 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1442 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001443 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1444 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001445 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1446 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001447 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1448 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001449 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1450 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1451 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1452 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1453 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
1454
1455 if (ctx->ctrls.error) {
1456 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1457 ctx->ctrls.error);
1458 return -EINVAL;
1459 }
1460
1461 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1462}
1463
1464static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
1465 struct vb2_queue *dst_vq)
1466{
1467 struct coda_ctx *ctx = priv;
1468 int ret;
1469
Javier Martin186b2502012-07-26 05:53:35 -03001470 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03001471 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001472 src_vq->drv_priv = ctx;
1473 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1474 src_vq->ops = &coda_qops;
1475 src_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001476 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001477
1478 ret = vb2_queue_init(src_vq);
1479 if (ret)
1480 return ret;
1481
Javier Martin186b2502012-07-26 05:53:35 -03001482 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03001483 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03001484 dst_vq->drv_priv = ctx;
1485 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1486 dst_vq->ops = &coda_qops;
1487 dst_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03001488 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03001489
1490 return vb2_queue_init(dst_vq);
1491}
1492
Philipp Zabele11f3e62012-07-25 09:16:58 -03001493static int coda_next_free_instance(struct coda_dev *dev)
1494{
1495 return ffz(dev->instance_mask);
1496}
1497
Javier Martin186b2502012-07-26 05:53:35 -03001498static int coda_open(struct file *file)
1499{
1500 struct coda_dev *dev = video_drvdata(file);
1501 struct coda_ctx *ctx = NULL;
1502 int ret = 0;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001503 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001504
Philipp Zabele11f3e62012-07-25 09:16:58 -03001505 idx = coda_next_free_instance(dev);
1506 if (idx >= CODA_MAX_INSTANCES)
Javier Martin186b2502012-07-26 05:53:35 -03001507 return -EBUSY;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001508 set_bit(idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001509
1510 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1511 if (!ctx)
1512 return -ENOMEM;
1513
1514 v4l2_fh_init(&ctx->fh, video_devdata(file));
1515 file->private_data = &ctx->fh;
1516 v4l2_fh_add(&ctx->fh);
1517 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001518 ctx->idx = idx;
Javier Martin186b2502012-07-26 05:53:35 -03001519
1520 set_default_params(ctx);
1521 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1522 &coda_queue_init);
1523 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03001524 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001525
1526 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1527 __func__, ret);
1528 goto err;
1529 }
1530 ret = coda_ctrls_setup(ctx);
1531 if (ret) {
1532 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
1533 goto err;
1534 }
1535
1536 ctx->fh.ctrl_handler = &ctx->ctrls;
1537
1538 ctx->parabuf.vaddr = dma_alloc_coherent(&dev->plat_dev->dev,
1539 CODA_PARA_BUF_SIZE, &ctx->parabuf.paddr, GFP_KERNEL);
1540 if (!ctx->parabuf.vaddr) {
1541 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1542 ret = -ENOMEM;
1543 goto err;
1544 }
1545
1546 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001547 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001548 coda_unlock(ctx);
1549
1550 clk_prepare_enable(dev->clk_per);
1551 clk_prepare_enable(dev->clk_ahb);
1552
1553 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1554 ctx->idx, ctx);
1555
1556 return 0;
1557
1558err:
1559 v4l2_fh_del(&ctx->fh);
1560 v4l2_fh_exit(&ctx->fh);
1561 kfree(ctx);
1562 return ret;
1563}
1564
1565static int coda_release(struct file *file)
1566{
1567 struct coda_dev *dev = video_drvdata(file);
1568 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1569
1570 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1571 ctx);
1572
1573 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001574 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001575 coda_unlock(ctx);
1576
1577 dma_free_coherent(&dev->plat_dev->dev, CODA_PARA_BUF_SIZE,
1578 ctx->parabuf.vaddr, ctx->parabuf.paddr);
1579 v4l2_m2m_ctx_release(ctx->m2m_ctx);
1580 v4l2_ctrl_handler_free(&ctx->ctrls);
1581 clk_disable_unprepare(dev->clk_per);
1582 clk_disable_unprepare(dev->clk_ahb);
1583 v4l2_fh_del(&ctx->fh);
1584 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001585 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03001586 kfree(ctx);
1587
1588 return 0;
1589}
1590
1591static unsigned int coda_poll(struct file *file,
1592 struct poll_table_struct *wait)
1593{
1594 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1595 int ret;
1596
1597 coda_lock(ctx);
1598 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
1599 coda_unlock(ctx);
1600 return ret;
1601}
1602
1603static int coda_mmap(struct file *file, struct vm_area_struct *vma)
1604{
1605 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1606
1607 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
1608}
1609
1610static const struct v4l2_file_operations coda_fops = {
1611 .owner = THIS_MODULE,
1612 .open = coda_open,
1613 .release = coda_release,
1614 .poll = coda_poll,
1615 .unlocked_ioctl = video_ioctl2,
1616 .mmap = coda_mmap,
1617};
1618
1619static irqreturn_t coda_irq_handler(int irq, void *data)
1620{
Philipp Zabelec25f682012-07-20 08:54:29 -03001621 struct vb2_buffer *src_buf, *dst_buf;
Javier Martin186b2502012-07-26 05:53:35 -03001622 struct coda_dev *dev = data;
1623 u32 wr_ptr, start_ptr;
1624 struct coda_ctx *ctx;
1625
Fabio Estevam2249b452012-10-09 23:33:29 -03001626 cancel_delayed_work(&dev->timeout);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001627
Javier Martin186b2502012-07-26 05:53:35 -03001628 /* read status register to attend the IRQ */
1629 coda_read(dev, CODA_REG_BIT_INT_STATUS);
1630 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
1631 CODA_REG_BIT_INT_CLEAR);
1632
1633 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1634 if (ctx == NULL) {
1635 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001636 mutex_unlock(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001637 return IRQ_HANDLED;
1638 }
1639
1640 if (ctx->aborting) {
1641 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1642 "task has been aborted\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03001643 mutex_unlock(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001644 return IRQ_HANDLED;
1645 }
1646
1647 if (coda_isbusy(ctx->dev)) {
1648 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1649 "coda is still busy!!!!\n");
1650 return IRQ_NONE;
1651 }
1652
Philipp Zabelec25f682012-07-20 08:54:29 -03001653 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1654 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001655
1656 /* Get results from the coda */
1657 coda_read(dev, CODA_RET_ENC_PIC_TYPE);
1658 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
1659 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->idx));
1660 /* Calculate bytesused field */
1661 if (dst_buf->v4l2_buf.sequence == 0) {
1662 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr) +
1663 ctx->vpu_header_size[0] +
1664 ctx->vpu_header_size[1] +
1665 ctx->vpu_header_size[2];
1666 } else {
1667 dst_buf->v4l2_planes[0].bytesused = (wr_ptr - start_ptr);
1668 }
1669
1670 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
1671 wr_ptr - start_ptr);
1672
1673 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
1674 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
1675
1676 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1677 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1678 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1679 } else {
1680 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1681 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1682 }
1683
Kamil Debskiccd571c2013-04-24 10:38:24 -03001684 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
1685 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
1686
Philipp Zabelec25f682012-07-20 08:54:29 -03001687 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03001688 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1689
1690 ctx->gopcounter--;
1691 if (ctx->gopcounter < 0)
1692 ctx->gopcounter = ctx->params.gop_size - 1;
1693
1694 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1695 "job finished: encoding frame (%d) (%s)\n",
1696 dst_buf->v4l2_buf.sequence,
1697 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
1698 "KEYFRAME" : "PFRAME");
1699
Philipp Zabelfcb62822013-05-23 10:43:00 -03001700 mutex_unlock(&dev->coda_mutex);
1701
Javier Martin186b2502012-07-26 05:53:35 -03001702 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
1703
1704 return IRQ_HANDLED;
1705}
1706
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001707static void coda_timeout(struct work_struct *work)
1708{
1709 struct coda_ctx *ctx;
1710 struct coda_dev *dev = container_of(to_delayed_work(work),
1711 struct coda_dev, timeout);
1712
Philipp Zabel39cc0292013-05-21 10:32:42 -03001713 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001714
1715 mutex_lock(&dev->dev_mutex);
1716 list_for_each_entry(ctx, &dev->instances, list) {
1717 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1718 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1719 }
1720 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001721
1722 mutex_unlock(&dev->coda_mutex);
1723 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
1724 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001725}
1726
Javier Martin186b2502012-07-26 05:53:35 -03001727static u32 coda_supported_firmwares[] = {
1728 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001729 CODA_FIRMWARE_VERNUM(CODA_7541, 13, 4, 29),
Javier Martin186b2502012-07-26 05:53:35 -03001730};
1731
1732static bool coda_firmware_supported(u32 vernum)
1733{
1734 int i;
1735
1736 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
1737 if (vernum == coda_supported_firmwares[i])
1738 return true;
1739 return false;
1740}
1741
1742static char *coda_product_name(int product)
1743{
1744 static char buf[9];
1745
1746 switch (product) {
1747 case CODA_DX6:
1748 return "CodaDx6";
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001749 case CODA_7541:
1750 return "CODA7541";
Javier Martin186b2502012-07-26 05:53:35 -03001751 default:
1752 snprintf(buf, sizeof(buf), "(0x%04x)", product);
1753 return buf;
1754 }
1755}
1756
Philipp Zabel87048bb2012-07-02 07:03:43 -03001757static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001758{
1759 u16 product, major, minor, release;
1760 u32 data;
1761 u16 *p;
1762 int i;
1763
1764 clk_prepare_enable(dev->clk_per);
1765 clk_prepare_enable(dev->clk_ahb);
1766
Javier Martin186b2502012-07-26 05:53:35 -03001767 /*
1768 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001769 * The 16-bit chars in the code buffer are in memory access
1770 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001771 * Data in this SRAM survives a reboot.
1772 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001773 p = (u16 *)dev->codebuf.vaddr;
1774 if (dev->devtype->product == CODA_DX6) {
1775 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1776 data = CODA_DOWN_ADDRESS_SET(i) |
1777 CODA_DOWN_DATA_SET(p[i ^ 1]);
1778 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1779 }
1780 } else {
1781 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1782 data = CODA_DOWN_ADDRESS_SET(i) |
1783 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1784 3 - (i % 4)]);
1785 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1786 }
Javier Martin186b2502012-07-26 05:53:35 -03001787 }
Javier Martin186b2502012-07-26 05:53:35 -03001788
Philipp Zabel9acf7692013-05-23 10:42:56 -03001789 /* Clear registers */
1790 for (i = 0; i < 64; i++)
1791 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1792
Javier Martin186b2502012-07-26 05:53:35 -03001793 /* Tell the BIT where to find everything it needs */
1794 coda_write(dev, dev->workbuf.paddr,
1795 CODA_REG_BIT_WORK_BUF_ADDR);
1796 coda_write(dev, dev->codebuf.paddr,
1797 CODA_REG_BIT_CODE_BUF_ADDR);
1798 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1799
1800 /* Set default values */
1801 switch (dev->devtype->product) {
1802 case CODA_DX6:
1803 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1804 break;
1805 default:
1806 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1807 }
1808 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001809
1810 if (dev->devtype->product != CODA_DX6)
1811 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1812
Javier Martin186b2502012-07-26 05:53:35 -03001813 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1814 CODA_REG_BIT_INT_ENABLE);
1815
1816 /* Reset VPU and start processor */
1817 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1818 data |= CODA_REG_RESET_ENABLE;
1819 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1820 udelay(10);
1821 data &= ~CODA_REG_RESET_ENABLE;
1822 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1823 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1824
1825 /* Load firmware */
1826 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
1827 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
1828 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
1829 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
1830 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
1831 if (coda_wait_timeout(dev)) {
1832 clk_disable_unprepare(dev->clk_per);
1833 clk_disable_unprepare(dev->clk_ahb);
1834 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
1835 return -EIO;
1836 }
1837
1838 /* Check we are compatible with the loaded firmware */
1839 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
1840 product = CODA_FIRMWARE_PRODUCT(data);
1841 major = CODA_FIRMWARE_MAJOR(data);
1842 minor = CODA_FIRMWARE_MINOR(data);
1843 release = CODA_FIRMWARE_RELEASE(data);
1844
1845 clk_disable_unprepare(dev->clk_per);
1846 clk_disable_unprepare(dev->clk_ahb);
1847
1848 if (product != dev->devtype->product) {
1849 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
1850 " Version: %u.%u.%u\n",
1851 coda_product_name(dev->devtype->product),
1852 coda_product_name(product), major, minor, release);
1853 return -EINVAL;
1854 }
1855
1856 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
1857 coda_product_name(product));
1858
1859 if (coda_firmware_supported(data)) {
1860 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
1861 major, minor, release);
1862 } else {
1863 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
1864 "%u.%u.%u\n", major, minor, release);
1865 }
1866
1867 return 0;
1868}
1869
1870static void coda_fw_callback(const struct firmware *fw, void *context)
1871{
1872 struct coda_dev *dev = context;
1873 struct platform_device *pdev = dev->plat_dev;
1874 int ret;
1875
1876 if (!fw) {
1877 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1878 return;
1879 }
1880
1881 /* allocate auxiliary per-device code buffer for the BIT processor */
1882 dev->codebuf.size = fw->size;
1883 dev->codebuf.vaddr = dma_alloc_coherent(&pdev->dev, fw->size,
1884 &dev->codebuf.paddr,
1885 GFP_KERNEL);
1886 if (!dev->codebuf.vaddr) {
1887 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1888 return;
1889 }
1890
Philipp Zabel87048bb2012-07-02 07:03:43 -03001891 /* Copy the whole firmware image to the code buffer */
1892 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1893 release_firmware(fw);
1894
1895 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03001896 if (ret) {
1897 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1898 return;
1899 }
1900
1901 dev->vfd.fops = &coda_fops,
1902 dev->vfd.ioctl_ops = &coda_ioctl_ops;
1903 dev->vfd.release = video_device_release_empty,
1904 dev->vfd.lock = &dev->dev_mutex;
1905 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03001906 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03001907 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
1908 video_set_drvdata(&dev->vfd, dev);
1909
1910 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1911 if (IS_ERR(dev->alloc_ctx)) {
1912 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1913 return;
1914 }
1915
1916 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1917 if (IS_ERR(dev->m2m_dev)) {
1918 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1919 goto rel_ctx;
1920 }
1921
1922 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
1923 if (ret) {
1924 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1925 goto rel_m2m;
1926 }
1927 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
1928 dev->vfd.num);
1929
1930 return;
1931
1932rel_m2m:
1933 v4l2_m2m_release(dev->m2m_dev);
1934rel_ctx:
1935 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1936}
1937
1938static int coda_firmware_request(struct coda_dev *dev)
1939{
1940 char *fw = dev->devtype->firmware;
1941
1942 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1943 coda_product_name(dev->devtype->product));
1944
1945 return request_firmware_nowait(THIS_MODULE, true,
1946 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1947}
1948
1949enum coda_platform {
1950 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001951 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03001952};
1953
Emil Goodec06d8752012-08-14 17:44:42 -03001954static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001955 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03001956 .firmware = "v4l-codadx6-imx27.bin",
1957 .product = CODA_DX6,
1958 .codecs = codadx6_codecs,
1959 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03001960 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001961 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03001962 .firmware = "v4l-coda7541-imx53.bin",
1963 .product = CODA_7541,
1964 .codecs = coda7_codecs,
1965 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001966 },
Javier Martin186b2502012-07-26 05:53:35 -03001967};
1968
1969static struct platform_device_id coda_platform_ids[] = {
1970 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001971 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001972 { /* sentinel */ }
1973};
1974MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1975
1976#ifdef CONFIG_OF
1977static const struct of_device_id coda_dt_ids[] = {
1978 { .compatible = "fsl,imx27-vpu", .data = &coda_platform_ids[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001979 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03001980 { /* sentinel */ }
1981};
1982MODULE_DEVICE_TABLE(of, coda_dt_ids);
1983#endif
1984
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001985static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001986{
1987 const struct of_device_id *of_id =
1988 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1989 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001990 struct coda_platform_data *pdata = pdev->dev.platform_data;
1991 struct device_node *np = pdev->dev.of_node;
1992 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001993 struct coda_dev *dev;
1994 struct resource *res;
1995 int ret, irq;
1996
1997 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1998 if (!dev) {
1999 dev_err(&pdev->dev, "Not enough memory for %s\n",
2000 CODA_NAME);
2001 return -ENOMEM;
2002 }
2003
2004 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002005 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002006 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03002007
2008 dev->plat_dev = pdev;
2009 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2010 if (IS_ERR(dev->clk_per)) {
2011 dev_err(&pdev->dev, "Could not get per clock\n");
2012 return PTR_ERR(dev->clk_per);
2013 }
2014
2015 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2016 if (IS_ERR(dev->clk_ahb)) {
2017 dev_err(&pdev->dev, "Could not get ahb clock\n");
2018 return PTR_ERR(dev->clk_ahb);
2019 }
2020
2021 /* Get memory for physical registers */
2022 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2023 if (res == NULL) {
2024 dev_err(&pdev->dev, "failed to get memory region resource\n");
2025 return -ENOENT;
2026 }
2027
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002028 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2029 if (IS_ERR(dev->regs_base))
2030 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002031
2032 /* IRQ */
2033 irq = platform_get_irq(pdev, 0);
2034 if (irq < 0) {
2035 dev_err(&pdev->dev, "failed to get irq resource\n");
2036 return -ENOENT;
2037 }
2038
2039 if (devm_request_irq(&pdev->dev, irq, coda_irq_handler,
2040 0, CODA_NAME, dev) < 0) {
2041 dev_err(&pdev->dev, "failed to request irq\n");
2042 return -ENOENT;
2043 }
2044
Philipp Zabel657eee72013-04-29 16:17:14 -07002045 /* Get IRAM pool from device tree or platform data */
2046 pool = of_get_named_gen_pool(np, "iram", 0);
2047 if (!pool && pdata)
2048 pool = dev_get_gen_pool(pdata->iram_dev);
2049 if (!pool) {
2050 dev_err(&pdev->dev, "iram pool not available\n");
2051 return -ENOMEM;
2052 }
2053 dev->iram_pool = pool;
2054
Javier Martin186b2502012-07-26 05:53:35 -03002055 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2056 if (ret)
2057 return ret;
2058
2059 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002060 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002061
2062 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2063
2064 if (of_id) {
2065 dev->devtype = of_id->data;
2066 } else if (pdev_id) {
2067 dev->devtype = &coda_devdata[pdev_id->driver_data];
2068 } else {
2069 v4l2_device_unregister(&dev->v4l2_dev);
2070 return -EINVAL;
2071 }
2072
2073 /* allocate auxiliary per-device buffers for the BIT processor */
2074 switch (dev->devtype->product) {
2075 case CODA_DX6:
2076 dev->workbuf.size = CODADX6_WORK_BUF_SIZE;
2077 break;
2078 default:
2079 dev->workbuf.size = CODA7_WORK_BUF_SIZE;
2080 }
2081 dev->workbuf.vaddr = dma_alloc_coherent(&pdev->dev, dev->workbuf.size,
2082 &dev->workbuf.paddr,
2083 GFP_KERNEL);
2084 if (!dev->workbuf.vaddr) {
2085 dev_err(&pdev->dev, "failed to allocate work buffer\n");
2086 v4l2_device_unregister(&dev->v4l2_dev);
2087 return -ENOMEM;
2088 }
2089
Philipp Zabel657eee72013-04-29 16:17:14 -07002090 if (dev->devtype->product == CODA_DX6)
2091 dev->iram_size = CODADX6_IRAM_SIZE;
2092 else
2093 dev->iram_size = CODA7_IRAM_SIZE;
2094 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
2095 if (!dev->iram_vaddr) {
2096 dev_err(&pdev->dev, "unable to alloc iram\n");
2097 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03002098 }
Philipp Zabel657eee72013-04-29 16:17:14 -07002099 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
2100 dev->iram_vaddr);
Philipp Zabel10436672012-07-02 09:03:55 -03002101
Javier Martin186b2502012-07-26 05:53:35 -03002102 platform_set_drvdata(pdev, dev);
2103
2104 return coda_firmware_request(dev);
2105}
2106
2107static int coda_remove(struct platform_device *pdev)
2108{
2109 struct coda_dev *dev = platform_get_drvdata(pdev);
2110
2111 video_unregister_device(&dev->vfd);
2112 if (dev->m2m_dev)
2113 v4l2_m2m_release(dev->m2m_dev);
2114 if (dev->alloc_ctx)
2115 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2116 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002117 if (dev->iram_vaddr)
2118 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Javier Martin186b2502012-07-26 05:53:35 -03002119 if (dev->codebuf.vaddr)
2120 dma_free_coherent(&pdev->dev, dev->codebuf.size,
2121 &dev->codebuf.vaddr, dev->codebuf.paddr);
2122 if (dev->workbuf.vaddr)
2123 dma_free_coherent(&pdev->dev, dev->workbuf.size, &dev->workbuf.vaddr,
2124 dev->workbuf.paddr);
2125 return 0;
2126}
2127
2128static struct platform_driver coda_driver = {
2129 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002130 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002131 .driver = {
2132 .name = CODA_NAME,
2133 .owner = THIS_MODULE,
2134 .of_match_table = of_match_ptr(coda_dt_ids),
2135 },
2136 .id_table = coda_platform_ids,
2137};
2138
2139module_platform_driver(coda_driver);
2140
2141MODULE_LICENSE("GPL");
2142MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2143MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");