blob: 6ddcf8ba5f760f8bdfd13f3abaa46091582d5fd3 [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>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030021#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030022#include <linux/module.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26#include <linux/videodev2.h>
27#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070028#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030029
30#include <media/v4l2-ctrls.h>
31#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030032#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030033#include <media/v4l2-ioctl.h>
34#include <media/v4l2-mem2mem.h>
35#include <media/videobuf2-core.h>
36#include <media/videobuf2-dma-contig.h>
37
38#include "coda.h"
39
40#define CODA_NAME "coda"
41
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030042#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030043
44#define CODA_FMO_BUF_SIZE 32
45#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030046#define CODA7_WORK_BUF_SIZE (128 * 1024)
47#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030048#define CODA_PARA_BUF_SIZE (10 * 1024)
49#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070050#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030051#define CODA7_IRAM_SIZE 0x14000
Javier Martin186b2502012-07-26 05:53:35 -030052
Philipp Zabel918c66f2013-06-27 06:59:01 -030053#define CODA7_PS_BUF_SIZE 0x28000
54
55#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030056
Philipp Zabelb96904e2013-05-23 10:42:58 -030057#define MAX_W 8192
58#define MAX_H 8192
59#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030060#define FMO_SLICE_SAVE_BUF_SIZE (32)
61#define CODA_DEFAULT_GAMMA 4096
62
63#define MIN_W 176
64#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030065
66#define S_ALIGN 1 /* multiple of 2 */
67#define W_ALIGN 1 /* multiple of 2 */
68#define H_ALIGN 1 /* multiple of 2 */
69
70#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
71
72static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030073module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030074MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
75
76enum {
77 V4L2_M2M_SRC = 0,
78 V4L2_M2M_DST = 1,
79};
80
Javier Martin186b2502012-07-26 05:53:35 -030081enum coda_inst_type {
82 CODA_INST_ENCODER,
83 CODA_INST_DECODER,
84};
85
86enum coda_product {
87 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030088 CODA_7541 = 0xf012,
Javier Martin186b2502012-07-26 05:53:35 -030089};
90
91struct coda_fmt {
92 char *name;
93 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030094};
95
96struct coda_codec {
97 u32 mode;
98 u32 src_fourcc;
99 u32 dst_fourcc;
100 u32 max_w;
101 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300102};
103
104struct coda_devtype {
105 char *firmware;
106 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300107 struct coda_codec *codecs;
108 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300109 size_t workbuf_size;
110};
111
112/* Per-queue, driver-specific private data */
113struct coda_q_data {
114 unsigned int width;
115 unsigned int height;
116 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300117 unsigned int fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300118};
119
120struct coda_aux_buf {
121 void *vaddr;
122 dma_addr_t paddr;
123 u32 size;
124};
125
126struct coda_dev {
127 struct v4l2_device v4l2_dev;
128 struct video_device vfd;
129 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300130 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300131
132 void __iomem *regs_base;
133 struct clk *clk_per;
134 struct clk *clk_ahb;
135
136 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300137 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300138 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700139 struct gen_pool *iram_pool;
140 long unsigned int iram_vaddr;
Philipp Zabel10436672012-07-02 09:03:55 -0300141 long unsigned int iram_paddr;
Philipp Zabel657eee72013-04-29 16:17:14 -0700142 unsigned long iram_size;
Javier Martin186b2502012-07-26 05:53:35 -0300143
144 spinlock_t irqlock;
145 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300146 struct mutex coda_mutex;
Javier Martin186b2502012-07-26 05:53:35 -0300147 struct v4l2_m2m_dev *m2m_dev;
148 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300149 struct list_head instances;
150 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300151 struct delayed_work timeout;
Javier Martin186b2502012-07-26 05:53:35 -0300152};
153
154struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300155 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300156 u8 h264_intra_qp;
157 u8 h264_inter_qp;
158 u8 mpeg4_intra_qp;
159 u8 mpeg4_inter_qp;
160 u8 gop_size;
161 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300162 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300163 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
164 u32 framerate;
165 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300166 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300167 u32 slice_max_mb;
168};
169
Philipp Zabelc2d22512013-06-21 03:55:28 -0300170struct coda_iram_info {
171 u32 axi_sram_use;
172 phys_addr_t buf_bit_use;
173 phys_addr_t buf_ip_ac_dc_use;
174 phys_addr_t buf_dbk_y_use;
175 phys_addr_t buf_dbk_c_use;
176 phys_addr_t buf_ovl_use;
177 phys_addr_t buf_btp_use;
178 phys_addr_t search_ram_paddr;
179 int search_ram_size;
180};
181
Javier Martin186b2502012-07-26 05:53:35 -0300182struct coda_ctx {
183 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300184 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300185 struct list_head list;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300186 struct work_struct skip_run;
Javier Martin186b2502012-07-26 05:53:35 -0300187 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300188 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300189 int streamon_out;
190 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300191 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300192 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300193 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300194 struct coda_q_data q_data[2];
195 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300196 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300197 enum v4l2_colorspace colorspace;
198 struct coda_params params;
199 struct v4l2_m2m_ctx *m2m_ctx;
200 struct v4l2_ctrl_handler ctrls;
201 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300202 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300203 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300204 char vpu_header[3][64];
205 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300206 struct kfifo bitstream_fifo;
207 struct mutex bitstream_mutex;
208 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300209 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300210 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300211 struct coda_aux_buf psbuf;
212 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300213 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300214 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300215 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300216 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300217 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300218 struct coda_iram_info iram_info;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300219 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300220 u32 frm_dis_flg;
221 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300222};
223
Javier Martin832fbb52012-10-29 08:34:59 -0300224static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
225 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
226static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300227
Javier Martin186b2502012-07-26 05:53:35 -0300228static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
229{
230 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
231 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
232 writel(data, dev->regs_base + reg);
233}
234
235static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
236{
237 u32 data;
238 data = readl(dev->regs_base + reg);
239 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
240 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
241 return data;
242}
243
244static inline unsigned long coda_isbusy(struct coda_dev *dev)
245{
246 return coda_read(dev, CODA_REG_BIT_BUSY);
247}
248
249static inline int coda_is_initialized(struct coda_dev *dev)
250{
251 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
252}
253
254static int coda_wait_timeout(struct coda_dev *dev)
255{
256 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
257
258 while (coda_isbusy(dev)) {
259 if (time_after(jiffies, timeout))
260 return -ETIMEDOUT;
261 }
262 return 0;
263}
264
265static void coda_command_async(struct coda_ctx *ctx, int cmd)
266{
267 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300268
269 if (dev->devtype->product == CODA_7541) {
270 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300271 coda_write(dev, ctx->bit_stream_param,
272 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300273 coda_write(dev, ctx->frm_dis_flg,
274 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300275 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
276 }
277
Javier Martin186b2502012-07-26 05:53:35 -0300278 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
279
280 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
281 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300282 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
283
Javier Martin186b2502012-07-26 05:53:35 -0300284 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
285}
286
287static int coda_command_sync(struct coda_ctx *ctx, int cmd)
288{
289 struct coda_dev *dev = ctx->dev;
290
291 coda_command_async(ctx, cmd);
292 return coda_wait_timeout(dev);
293}
294
295static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
296 enum v4l2_buf_type type)
297{
298 switch (type) {
299 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
300 return &(ctx->q_data[V4L2_M2M_SRC]);
301 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
302 return &(ctx->q_data[V4L2_M2M_DST]);
303 default:
304 BUG();
305 }
306 return NULL;
307}
308
309/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300310 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300311 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300312static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300313 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300314 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300315 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300316 },
317 {
318 .name = "YUV 4:2:0 Planar, YCrCb",
319 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300320 },
321 {
322 .name = "H264 Encoded Stream",
323 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300324 },
325 {
326 .name = "MPEG4 Encoded Stream",
327 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300328 },
329};
330
Philipp Zabelb96904e2013-05-23 10:42:58 -0300331#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
332 { mode, src_fourcc, dst_fourcc, max_w, max_h }
333
334/*
335 * Arrays of codecs supported by each given version of Coda:
336 * i.MX27 -> codadx6
337 * i.MX5x -> coda7
338 * i.MX6 -> coda960
339 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
340 */
341static struct coda_codec codadx6_codecs[] = {
342 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
343 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300344};
345
Philipp Zabelb96904e2013-05-23 10:42:58 -0300346static struct coda_codec coda7_codecs[] = {
347 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
348 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300349 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
350 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300351};
352
353static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300354{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300355 switch (fourcc) {
356 case V4L2_PIX_FMT_YUV420:
357 case V4L2_PIX_FMT_YVU420:
358 return true;
359 default:
360 return false;
361 }
362}
Javier Martin186b2502012-07-26 05:53:35 -0300363
Philipp Zabelb96904e2013-05-23 10:42:58 -0300364/*
365 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
366 * tables.
367 */
368static u32 coda_format_normalize_yuv(u32 fourcc)
369{
370 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
371}
372
373static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
374 int dst_fourcc)
375{
376 struct coda_codec *codecs = dev->devtype->codecs;
377 int num_codecs = dev->devtype->num_codecs;
378 int k;
379
380 src_fourcc = coda_format_normalize_yuv(src_fourcc);
381 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
382 if (src_fourcc == dst_fourcc)
383 return NULL;
384
385 for (k = 0; k < num_codecs; k++) {
386 if (codecs[k].src_fourcc == src_fourcc &&
387 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300388 break;
389 }
390
Philipp Zabelb96904e2013-05-23 10:42:58 -0300391 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300392 return NULL;
393
Philipp Zabelb96904e2013-05-23 10:42:58 -0300394 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300395}
396
Philipp Zabel927933f2013-09-30 10:34:48 -0300397static char *coda_product_name(int product)
398{
399 static char buf[9];
400
401 switch (product) {
402 case CODA_DX6:
403 return "CodaDx6";
404 case CODA_7541:
405 return "CODA7541";
406 default:
407 snprintf(buf, sizeof(buf), "(0x%04x)", product);
408 return buf;
409 }
410}
411
Javier Martin186b2502012-07-26 05:53:35 -0300412/*
413 * V4L2 ioctl() operations.
414 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300415static int coda_querycap(struct file *file, void *priv,
416 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300417{
Philipp Zabel927933f2013-09-30 10:34:48 -0300418 struct coda_ctx *ctx = fh_to_ctx(priv);
419
Javier Martin186b2502012-07-26 05:53:35 -0300420 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300421 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
422 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300423 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300424 /*
425 * This is only a mem-to-mem video device. The capture and output
426 * device capability flags are left only for backward compatibility
427 * and are scheduled for removal.
428 */
429 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
430 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300431 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
432
433 return 0;
434}
435
436static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300437 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300438{
439 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300440 struct coda_codec *codecs = ctx->dev->devtype->codecs;
441 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300442 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300443 int num_codecs = ctx->dev->devtype->num_codecs;
444 int num_formats = ARRAY_SIZE(coda_formats);
445 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300446
447 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300448 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300449 if (coda_format_is_yuv(formats[i].fourcc) &&
450 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300451 if (num == f->index)
452 break;
453 ++num;
454 continue;
455 }
456 /* Compressed formats may be supported, check the codec list */
457 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300458 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300459 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300460 formats[i].fourcc == codecs[k].dst_fourcc &&
461 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300462 break;
463 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
464 formats[i].fourcc == codecs[k].src_fourcc)
465 break;
466 }
467 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300468 if (num == f->index)
469 break;
470 ++num;
471 }
472 }
473
474 if (i < num_formats) {
475 fmt = &formats[i];
476 strlcpy(f->description, fmt->name, sizeof(f->description));
477 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300478 if (!coda_format_is_yuv(fmt->fourcc))
479 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300480 return 0;
481 }
482
483 /* Format not found */
484 return -EINVAL;
485}
486
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300487static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
488 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300489{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300490 struct coda_ctx *ctx = fh_to_ctx(priv);
491 struct vb2_queue *src_vq;
492 struct coda_q_data *q_data_src;
493
494 /* If the source format is already fixed, only list matching formats */
495 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
496 if (vb2_is_streaming(src_vq)) {
497 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
498
499 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
500 q_data_src->fourcc);
501 }
502
503 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300504}
505
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300506static int coda_enum_fmt_vid_out(struct file *file, void *priv,
507 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300508{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300509 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300510}
511
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300512static int coda_g_fmt(struct file *file, void *priv,
513 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300514{
515 struct vb2_queue *vq;
516 struct coda_q_data *q_data;
517 struct coda_ctx *ctx = fh_to_ctx(priv);
518
519 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
520 if (!vq)
521 return -EINVAL;
522
523 q_data = get_q_data(ctx, f->type);
524
525 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300526 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300527 f->fmt.pix.width = q_data->width;
528 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300529 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300530 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
531 else /* encoded formats h.264/mpeg4 */
532 f->fmt.pix.bytesperline = 0;
533
534 f->fmt.pix.sizeimage = q_data->sizeimage;
535 f->fmt.pix.colorspace = ctx->colorspace;
536
537 return 0;
538}
539
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300540static int coda_try_fmt(struct coda_codec *codec, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300541{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300542 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300543 enum v4l2_field field;
544
545 field = f->fmt.pix.field;
546 if (field == V4L2_FIELD_ANY)
547 field = V4L2_FIELD_NONE;
548 else if (V4L2_FIELD_NONE != field)
549 return -EINVAL;
550
551 /* V4L2 specification suggests the driver corrects the format struct
552 * if any of the dimensions is unsupported */
553 f->fmt.pix.field = field;
554
Philipp Zabelb96904e2013-05-23 10:42:58 -0300555 if (codec) {
556 max_w = codec->max_w;
557 max_h = codec->max_h;
558 } else {
559 max_w = MAX_W;
560 max_h = MAX_H;
561 }
562 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w,
563 W_ALIGN, &f->fmt.pix.height,
564 MIN_H, max_h, H_ALIGN, S_ALIGN);
565
566 if (coda_format_is_yuv(f->fmt.pix.pixelformat)) {
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300567 /* Frame stride must be multiple of 8 */
568 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
569 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300570 f->fmt.pix.height * 3 / 2;
Javier Martin186b2502012-07-26 05:53:35 -0300571 } else { /*encoded formats h.264/mpeg4 */
572 f->fmt.pix.bytesperline = 0;
573 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
574 }
575
576 return 0;
577}
578
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300579static int coda_try_fmt_vid_cap(struct file *file, void *priv,
580 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300581{
Javier Martin186b2502012-07-26 05:53:35 -0300582 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300583 struct coda_codec *codec;
584 struct vb2_queue *src_vq;
585 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300586
Philipp Zabel918c66f2013-06-27 06:59:01 -0300587 /*
588 * If the source format is already fixed, try to find a codec that
589 * converts to the given destination format
590 */
591 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
592 if (vb2_is_streaming(src_vq)) {
593 struct coda_q_data *q_data_src;
594
595 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
596 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
597 f->fmt.pix.pixelformat);
598 if (!codec)
599 return -EINVAL;
600 } else {
601 /* Otherwise determine codec by encoded format, if possible */
602 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
603 f->fmt.pix.pixelformat);
604 }
Javier Martin186b2502012-07-26 05:53:35 -0300605
606 f->fmt.pix.colorspace = ctx->colorspace;
607
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300608 ret = coda_try_fmt(codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300609 if (ret < 0)
610 return ret;
611
612 /* The h.264 decoder only returns complete 16x16 macroblocks */
613 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
614 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
615 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
616 f->fmt.pix.bytesperline = f->fmt.pix.width;
617 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
618 f->fmt.pix.height * 3 / 2;
619 }
620
621 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300622}
623
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300624static int coda_try_fmt_vid_out(struct file *file, void *priv,
625 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300626{
627 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300628 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300629
Philipp Zabelb96904e2013-05-23 10:42:58 -0300630 /* Determine codec by encoded format, returns NULL if raw or invalid */
631 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
632 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300633
634 if (!f->fmt.pix.colorspace)
635 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
636
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300637 return coda_try_fmt(codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300638}
639
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300640static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300641{
642 struct coda_q_data *q_data;
643 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300644
645 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
646 if (!vq)
647 return -EINVAL;
648
649 q_data = get_q_data(ctx, f->type);
650 if (!q_data)
651 return -EINVAL;
652
653 if (vb2_is_busy(vq)) {
654 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
655 return -EBUSY;
656 }
657
Philipp Zabelb96904e2013-05-23 10:42:58 -0300658 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300659 q_data->width = f->fmt.pix.width;
660 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300661 q_data->sizeimage = f->fmt.pix.sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300662
663 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
664 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300665 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300666
667 return 0;
668}
669
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300670static int coda_s_fmt_vid_cap(struct file *file, void *priv,
671 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300672{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300673 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300674 int ret;
675
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300676 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300677 if (ret)
678 return ret;
679
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300680 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300681}
682
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300683static int coda_s_fmt_vid_out(struct file *file, void *priv,
684 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300685{
686 struct coda_ctx *ctx = fh_to_ctx(priv);
687 int ret;
688
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300689 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300690 if (ret)
691 return ret;
692
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300693 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300694 if (ret)
695 ctx->colorspace = f->fmt.pix.colorspace;
696
697 return ret;
698}
699
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300700static int coda_reqbufs(struct file *file, void *priv,
701 struct v4l2_requestbuffers *reqbufs)
Javier Martin186b2502012-07-26 05:53:35 -0300702{
703 struct coda_ctx *ctx = fh_to_ctx(priv);
704
705 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
706}
707
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300708static int coda_querybuf(struct file *file, void *priv,
709 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300710{
711 struct coda_ctx *ctx = fh_to_ctx(priv);
712
713 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
714}
715
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300716static int coda_qbuf(struct file *file, void *priv,
717 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300718{
719 struct coda_ctx *ctx = fh_to_ctx(priv);
720
721 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
722}
723
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300724static int coda_expbuf(struct file *file, void *priv,
725 struct v4l2_exportbuffer *eb)
Philipp Zabel419869c2013-05-23 07:06:30 -0300726{
727 struct coda_ctx *ctx = fh_to_ctx(priv);
728
729 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
730}
731
Philipp Zabel918c66f2013-06-27 06:59:01 -0300732static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
733 struct v4l2_buffer *buf)
734{
735 struct vb2_queue *src_vq;
736
737 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
738
739 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
740 (buf->sequence == (ctx->qsequence - 1)));
741}
742
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300743static int coda_dqbuf(struct file *file, void *priv,
744 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300745{
746 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300747 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300748
Philipp Zabel918c66f2013-06-27 06:59:01 -0300749 ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
750
751 /* If this is the last capture buffer, emit an end-of-stream event */
752 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
753 coda_buf_is_end_of_stream(ctx, buf)) {
754 const struct v4l2_event eos_event = {
755 .type = V4L2_EVENT_EOS
756 };
757
758 v4l2_event_queue_fh(&ctx->fh, &eos_event);
759 }
760
761 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300762}
763
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300764static int coda_create_bufs(struct file *file, void *priv,
765 struct v4l2_create_buffers *create)
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300766{
767 struct coda_ctx *ctx = fh_to_ctx(priv);
768
769 return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
770}
771
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300772static int coda_streamon(struct file *file, void *priv,
773 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300774{
775 struct coda_ctx *ctx = fh_to_ctx(priv);
776
777 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
778}
779
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300780static int coda_streamoff(struct file *file, void *priv,
781 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300782{
783 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300784 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300785
Philipp Zabel918c66f2013-06-27 06:59:01 -0300786 /*
787 * This indirectly calls __vb2_queue_cancel, which dequeues all buffers.
788 * We therefore have to lock it against running hardware in this context,
789 * which still needs the buffers.
790 */
791 mutex_lock(&ctx->buffer_mutex);
792 ret = v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
793 mutex_unlock(&ctx->buffer_mutex);
794
795 return ret;
796}
797
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300798static int coda_decoder_cmd(struct file *file, void *fh,
799 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300800{
801 struct coda_ctx *ctx = fh_to_ctx(fh);
802
803 if (dc->cmd != V4L2_DEC_CMD_STOP)
804 return -EINVAL;
805
806 if ((dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK) ||
807 (dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY))
808 return -EINVAL;
809
810 if (dc->stop.pts != 0)
811 return -EINVAL;
812
813 if (ctx->inst_type != CODA_INST_DECODER)
814 return -EINVAL;
815
816 /* Set the strem-end flag on this context */
817 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
818
819 return 0;
820}
821
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300822static int coda_subscribe_event(struct v4l2_fh *fh,
823 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300824{
825 switch (sub->type) {
826 case V4L2_EVENT_EOS:
827 return v4l2_event_subscribe(fh, sub, 0, NULL);
828 default:
829 return v4l2_ctrl_subscribe_event(fh, sub);
830 }
Javier Martin186b2502012-07-26 05:53:35 -0300831}
832
833static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300834 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300835
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300836 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
837 .vidioc_g_fmt_vid_cap = coda_g_fmt,
838 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
839 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300840
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300841 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
842 .vidioc_g_fmt_vid_out = coda_g_fmt,
843 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
844 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300845
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300846 .vidioc_reqbufs = coda_reqbufs,
847 .vidioc_querybuf = coda_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300848
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300849 .vidioc_qbuf = coda_qbuf,
850 .vidioc_expbuf = coda_expbuf,
851 .vidioc_dqbuf = coda_dqbuf,
852 .vidioc_create_bufs = coda_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300853
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300854 .vidioc_streamon = coda_streamon,
855 .vidioc_streamoff = coda_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300856
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300857 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300858
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300859 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300860 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300861};
862
Philipp Zabel918c66f2013-06-27 06:59:01 -0300863static int coda_start_decoding(struct coda_ctx *ctx);
864
865static void coda_skip_run(struct work_struct *work)
866{
867 struct coda_ctx *ctx = container_of(work, struct coda_ctx, skip_run);
868
869 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
870}
871
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300872static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
873{
874 return kfifo_len(&ctx->bitstream_fifo);
875}
876
877static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
878{
879 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
880 struct coda_dev *dev = ctx->dev;
881 u32 rd_ptr;
882
883 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
884 kfifo->out = (kfifo->in & ~kfifo->mask) |
885 (rd_ptr - ctx->bitstream.paddr);
886 if (kfifo->out > kfifo->in)
887 kfifo->out -= kfifo->mask + 1;
888}
889
890static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
891{
892 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
893 struct coda_dev *dev = ctx->dev;
894 u32 rd_ptr, wr_ptr;
895
896 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
897 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
898 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
899 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
900}
901
902static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
903{
904 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
905 struct coda_dev *dev = ctx->dev;
906 u32 wr_ptr;
907
908 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
909 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
910}
911
912static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
913{
914 u32 src_size = vb2_get_plane_payload(src_buf, 0);
915 u32 n;
916
917 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
918 if (n < src_size)
919 return -ENOSPC;
920
921 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
922 ctx->bitstream.size, DMA_TO_DEVICE);
923
924 ctx->qsequence++;
925
926 return 0;
927}
928
929static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
930 struct vb2_buffer *src_buf)
931{
932 int ret;
933
934 if (coda_get_bitstream_payload(ctx) +
935 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
936 return false;
937
938 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
939 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
940 return true;
941 }
942
943 ret = coda_bitstream_queue(ctx, src_buf);
944 if (ret < 0) {
945 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
946 return false;
947 }
948 /* Sync read pointer to device */
949 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
950 coda_kfifo_sync_to_device_write(ctx);
951
Philipp Zabel918c66f2013-06-27 06:59:01 -0300952 ctx->prescan_failed = false;
953
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300954 return true;
955}
956
957static void coda_fill_bitstream(struct coda_ctx *ctx)
958{
959 struct vb2_buffer *src_buf;
960
961 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
962 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
963
964 if (coda_bitstream_try_queue(ctx, src_buf)) {
965 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
966 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
967 } else {
968 break;
969 }
970 }
971}
972
Javier Martin186b2502012-07-26 05:53:35 -0300973/*
974 * Mem-to-mem operations.
975 */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300976static int coda_prepare_decode(struct coda_ctx *ctx)
977{
978 struct vb2_buffer *dst_buf;
979 struct coda_dev *dev = ctx->dev;
980 struct coda_q_data *q_data_dst;
981 u32 stridey, height;
982 u32 picture_y, picture_cb, picture_cr;
983
984 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
985 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
986
987 if (ctx->params.rot_mode & CODA_ROT_90) {
988 stridey = q_data_dst->height;
989 height = q_data_dst->width;
990 } else {
991 stridey = q_data_dst->width;
992 height = q_data_dst->height;
993 }
994
995 /* Try to copy source buffer contents into the bitstream ringbuffer */
996 mutex_lock(&ctx->bitstream_mutex);
997 coda_fill_bitstream(ctx);
998 mutex_unlock(&ctx->bitstream_mutex);
999
1000 if (coda_get_bitstream_payload(ctx) < 512 &&
1001 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1002 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1003 "bitstream payload: %d, skipping\n",
1004 coda_get_bitstream_payload(ctx));
1005 schedule_work(&ctx->skip_run);
1006 return -EAGAIN;
1007 }
1008
1009 /* Run coda_start_decoding (again) if not yet initialized */
1010 if (!ctx->initialized) {
1011 int ret = coda_start_decoding(ctx);
1012 if (ret < 0) {
1013 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1014 schedule_work(&ctx->skip_run);
1015 return -EAGAIN;
1016 } else {
1017 ctx->initialized = 1;
1018 }
1019 }
1020
1021 /* Set rotator output */
1022 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1023 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1024 /* Switch Cr and Cb for YVU420 format */
1025 picture_cr = picture_y + stridey * height;
1026 picture_cb = picture_cr + stridey / 2 * height / 2;
1027 } else {
1028 picture_cb = picture_y + stridey * height;
1029 picture_cr = picture_cb + stridey / 2 * height / 2;
1030 }
1031 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1032 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1033 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1034 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1035 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1036 CODA_CMD_DEC_PIC_ROT_MODE);
1037
1038 switch (dev->devtype->product) {
1039 case CODA_DX6:
1040 /* TBD */
1041 case CODA_7541:
1042 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1043 break;
1044 }
1045
1046 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1047
1048 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1049 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1050
1051 return 0;
1052}
1053
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001054static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001055{
Javier Martin186b2502012-07-26 05:53:35 -03001056 struct coda_q_data *q_data_src, *q_data_dst;
1057 struct vb2_buffer *src_buf, *dst_buf;
1058 struct coda_dev *dev = ctx->dev;
1059 int force_ipicture;
1060 int quant_param = 0;
1061 u32 picture_y, picture_cb, picture_cr;
1062 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1063 u32 dst_fourcc;
1064
1065 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1066 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1067 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1068 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001069 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001070
Philipp Zabel918c66f2013-06-27 06:59:01 -03001071 src_buf->v4l2_buf.sequence = ctx->osequence;
1072 dst_buf->v4l2_buf.sequence = ctx->osequence;
1073 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001074
1075 /*
1076 * Workaround coda firmware BUG that only marks the first
1077 * frame as IDR. This is a problem for some decoders that can't
1078 * recover when a frame is lost.
1079 */
1080 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1081 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1082 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1083 } else {
1084 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1085 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1086 }
1087
1088 /*
1089 * Copy headers at the beginning of the first frame for H.264 only.
1090 * In MPEG4 they are already copied by the coda.
1091 */
1092 if (src_buf->v4l2_buf.sequence == 0) {
1093 pic_stream_buffer_addr =
1094 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1095 ctx->vpu_header_size[0] +
1096 ctx->vpu_header_size[1] +
1097 ctx->vpu_header_size[2];
1098 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1099 ctx->vpu_header_size[0] -
1100 ctx->vpu_header_size[1] -
1101 ctx->vpu_header_size[2];
1102 memcpy(vb2_plane_vaddr(dst_buf, 0),
1103 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1104 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1105 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1106 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1107 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1108 ctx->vpu_header_size[2]);
1109 } else {
1110 pic_stream_buffer_addr =
1111 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1112 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1113 }
1114
1115 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1116 force_ipicture = 1;
1117 switch (dst_fourcc) {
1118 case V4L2_PIX_FMT_H264:
1119 quant_param = ctx->params.h264_intra_qp;
1120 break;
1121 case V4L2_PIX_FMT_MPEG4:
1122 quant_param = ctx->params.mpeg4_intra_qp;
1123 break;
1124 default:
1125 v4l2_warn(&ctx->dev->v4l2_dev,
1126 "cannot set intra qp, fmt not supported\n");
1127 break;
1128 }
1129 } else {
1130 force_ipicture = 0;
1131 switch (dst_fourcc) {
1132 case V4L2_PIX_FMT_H264:
1133 quant_param = ctx->params.h264_inter_qp;
1134 break;
1135 case V4L2_PIX_FMT_MPEG4:
1136 quant_param = ctx->params.mpeg4_inter_qp;
1137 break;
1138 default:
1139 v4l2_warn(&ctx->dev->v4l2_dev,
1140 "cannot set inter qp, fmt not supported\n");
1141 break;
1142 }
1143 }
1144
1145 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001146 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001147 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1148
1149
1150 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001151 switch (q_data_src->fourcc) {
1152 case V4L2_PIX_FMT_YVU420:
1153 /* Switch Cb and Cr for YVU420 format */
1154 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1155 picture_cb = picture_cr + q_data_src->width / 2 *
1156 q_data_src->height / 2;
1157 break;
1158 case V4L2_PIX_FMT_YUV420:
1159 default:
1160 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1161 picture_cr = picture_cb + q_data_src->width / 2 *
1162 q_data_src->height / 2;
1163 break;
1164 }
Javier Martin186b2502012-07-26 05:53:35 -03001165
1166 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1167 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1168 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1169 coda_write(dev, force_ipicture << 1 & 0x2,
1170 CODA_CMD_ENC_PIC_OPTION);
1171
1172 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1173 coda_write(dev, pic_stream_buffer_size / 1024,
1174 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001175}
1176
1177static void coda_device_run(void *m2m_priv)
1178{
1179 struct coda_ctx *ctx = m2m_priv;
1180 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001181 int ret;
1182
1183 mutex_lock(&ctx->buffer_mutex);
1184
1185 /*
1186 * If streamoff dequeued all buffers before we could get the lock,
1187 * just bail out immediately.
1188 */
1189 if ((!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1190 ctx->inst_type != CODA_INST_DECODER) ||
1191 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1192 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1193 "%d: device_run without buffers\n", ctx->idx);
1194 mutex_unlock(&ctx->buffer_mutex);
1195 schedule_work(&ctx->skip_run);
1196 return;
1197 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001198
1199 mutex_lock(&dev->coda_mutex);
1200
Philipp Zabel918c66f2013-06-27 06:59:01 -03001201 if (ctx->inst_type == CODA_INST_DECODER) {
1202 ret = coda_prepare_decode(ctx);
1203 if (ret < 0) {
1204 mutex_unlock(&dev->coda_mutex);
1205 mutex_unlock(&ctx->buffer_mutex);
1206 /* job_finish scheduled by prepare_decode */
1207 return;
1208 }
1209 } else {
1210 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001211 }
1212
Philipp Zabelc2d22512013-06-21 03:55:28 -03001213 if (dev->devtype->product != CODA_DX6)
1214 coda_write(dev, ctx->iram_info.axi_sram_use,
1215 CODA7_REG_BIT_AXI_SRAM_USE);
1216
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001217 /* 1 second timeout in case CODA locks up */
1218 schedule_delayed_work(&dev->timeout, HZ);
1219
Philipp Zabel918c66f2013-06-27 06:59:01 -03001220 if (ctx->inst_type == CODA_INST_DECODER)
1221 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001222 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1223}
1224
1225static int coda_job_ready(void *m2m_priv)
1226{
1227 struct coda_ctx *ctx = m2m_priv;
1228
1229 /*
1230 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001231 * and 1 frame are needed. In the decoder case,
1232 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001233 */
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001234 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1235 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001236 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1237 "not ready: not enough video buffers.\n");
1238 return 0;
1239 }
1240
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001241 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1242 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1243 "not ready: not enough video capture buffers.\n");
1244 return 0;
1245 }
1246
Philipp Zabel918c66f2013-06-27 06:59:01 -03001247 if (ctx->prescan_failed ||
1248 ((ctx->inst_type == CODA_INST_DECODER) &&
1249 (coda_get_bitstream_payload(ctx) < 512) &&
1250 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1251 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1252 "%d: not ready: not enough bitstream data.\n",
1253 ctx->idx);
1254 return 0;
1255 }
1256
Philipp Zabel3e748262013-05-23 10:43:01 -03001257 if (ctx->aborting) {
1258 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1259 "not ready: aborting\n");
1260 return 0;
1261 }
1262
Javier Martin186b2502012-07-26 05:53:35 -03001263 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1264 "job ready\n");
1265 return 1;
1266}
1267
1268static void coda_job_abort(void *priv)
1269{
1270 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001271
1272 ctx->aborting = 1;
1273
1274 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1275 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001276}
1277
1278static void coda_lock(void *m2m_priv)
1279{
1280 struct coda_ctx *ctx = m2m_priv;
1281 struct coda_dev *pcdev = ctx->dev;
1282 mutex_lock(&pcdev->dev_mutex);
1283}
1284
1285static void coda_unlock(void *m2m_priv)
1286{
1287 struct coda_ctx *ctx = m2m_priv;
1288 struct coda_dev *pcdev = ctx->dev;
1289 mutex_unlock(&pcdev->dev_mutex);
1290}
1291
1292static struct v4l2_m2m_ops coda_m2m_ops = {
1293 .device_run = coda_device_run,
1294 .job_ready = coda_job_ready,
1295 .job_abort = coda_job_abort,
1296 .lock = coda_lock,
1297 .unlock = coda_unlock,
1298};
1299
1300static void set_default_params(struct coda_ctx *ctx)
1301{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001302 int max_w;
1303 int max_h;
1304
1305 ctx->codec = &ctx->dev->devtype->codecs[0];
1306 max_w = ctx->codec->max_w;
1307 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001308
1309 ctx->params.codec_mode = CODA_MODE_INVALID;
1310 ctx->colorspace = V4L2_COLORSPACE_REC709;
1311 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001312 ctx->aborting = 0;
1313
1314 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001315 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1316 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1317 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1318 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1319 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1320 ctx->q_data[V4L2_M2M_DST].width = max_w;
1321 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001322 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1323}
1324
1325/*
1326 * Queue operations
1327 */
1328static int coda_queue_setup(struct vb2_queue *vq,
1329 const struct v4l2_format *fmt,
1330 unsigned int *nbuffers, unsigned int *nplanes,
1331 unsigned int sizes[], void *alloc_ctxs[])
1332{
1333 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001334 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001335 unsigned int size;
1336
Philipp Zabele34db062012-08-29 08:22:00 -03001337 q_data = get_q_data(ctx, vq->type);
1338 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001339
1340 *nplanes = 1;
1341 sizes[0] = size;
1342
1343 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1344
1345 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1346 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1347
1348 return 0;
1349}
1350
1351static int coda_buf_prepare(struct vb2_buffer *vb)
1352{
1353 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1354 struct coda_q_data *q_data;
1355
1356 q_data = get_q_data(ctx, vb->vb2_queue->type);
1357
1358 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1359 v4l2_warn(&ctx->dev->v4l2_dev,
1360 "%s data will not fit into plane (%lu < %lu)\n",
1361 __func__, vb2_plane_size(vb, 0),
1362 (long)q_data->sizeimage);
1363 return -EINVAL;
1364 }
1365
Javier Martin186b2502012-07-26 05:53:35 -03001366 return 0;
1367}
1368
1369static void coda_buf_queue(struct vb2_buffer *vb)
1370{
1371 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001372 struct coda_q_data *q_data;
1373
1374 q_data = get_q_data(ctx, vb->vb2_queue->type);
1375
1376 /*
1377 * In the decoder case, immediately try to copy the buffer into the
1378 * bitstream ringbuffer and mark it as ready to be dequeued.
1379 */
1380 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1381 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1382 /*
1383 * For backwards compatiblity, queuing an empty buffer marks
1384 * the stream end
1385 */
1386 if (vb2_get_plane_payload(vb, 0) == 0)
1387 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1388 mutex_lock(&ctx->bitstream_mutex);
1389 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1390 coda_fill_bitstream(ctx);
1391 mutex_unlock(&ctx->bitstream_mutex);
1392 } else {
1393 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1394 }
Javier Martin186b2502012-07-26 05:53:35 -03001395}
1396
1397static void coda_wait_prepare(struct vb2_queue *q)
1398{
1399 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1400 coda_unlock(ctx);
1401}
1402
1403static void coda_wait_finish(struct vb2_queue *q)
1404{
1405 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1406 coda_lock(ctx);
1407}
1408
Philipp Zabel86eda902013-05-23 10:42:57 -03001409static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1410{
1411 struct coda_dev *dev = ctx->dev;
1412 u32 *p = ctx->parabuf.vaddr;
1413
1414 if (dev->devtype->product == CODA_DX6)
1415 p[index] = value;
1416 else
1417 p[index ^ 1] = value;
1418}
1419
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001420static int coda_alloc_aux_buf(struct coda_dev *dev,
1421 struct coda_aux_buf *buf, size_t size)
1422{
1423 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1424 GFP_KERNEL);
1425 if (!buf->vaddr)
1426 return -ENOMEM;
1427
1428 buf->size = size;
1429
1430 return 0;
1431}
1432
1433static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1434 struct coda_aux_buf *buf, size_t size)
1435{
1436 return coda_alloc_aux_buf(ctx->dev, buf, size);
1437}
1438
1439static void coda_free_aux_buf(struct coda_dev *dev,
1440 struct coda_aux_buf *buf)
1441{
1442 if (buf->vaddr) {
1443 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1444 buf->vaddr, buf->paddr);
1445 buf->vaddr = NULL;
1446 buf->size = 0;
1447 }
1448}
1449
1450static void coda_free_framebuffers(struct coda_ctx *ctx)
1451{
1452 int i;
1453
1454 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1455 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1456}
1457
Philipp Zabelec25f682012-07-20 08:54:29 -03001458static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1459{
1460 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001461 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001462 dma_addr_t paddr;
1463 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001464 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001465 int i;
1466
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001467 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1468 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001469 ysize = round_up(q_data->width, 8) * height;
1470
Philipp Zabelec25f682012-07-20 08:54:29 -03001471 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001472 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001473 size_t size;
1474
1475 size = q_data->sizeimage;
1476 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1477 dev->devtype->product != CODA_DX6)
Philipp Zabel86eda902013-05-23 10:42:57 -03001478 ctx->internal_frames[i].size += ysize/4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001479 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1480 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001481 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001482 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001483 }
1484 }
1485
1486 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001487 for (i = 0; i < ctx->num_internal_frames; i++) {
1488 paddr = ctx->internal_frames[i].paddr;
1489 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1490 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1491 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001492
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001493 /* mvcol buffer for h.264 */
1494 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1495 dev->devtype->product != CODA_DX6)
1496 coda_parabuf_write(ctx, 96 + i,
1497 ctx->internal_frames[i].paddr +
1498 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001499 }
1500
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001501 /* mvcol buffer for mpeg4 */
1502 if ((dev->devtype->product != CODA_DX6) &&
1503 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1504 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1505 ysize + ysize/4 + ysize/4);
1506
Philipp Zabelec25f682012-07-20 08:54:29 -03001507 return 0;
1508}
1509
Javier Martin3f3f5c72012-10-29 05:20:29 -03001510static int coda_h264_padding(int size, char *p)
1511{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001512 int nal_size;
1513 int diff;
1514
Javier Martin832fbb52012-10-29 08:34:59 -03001515 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001516 if (diff == 0)
1517 return 0;
1518
Javier Martin832fbb52012-10-29 08:34:59 -03001519 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001520 memcpy(p, coda_filler_nal, nal_size);
1521
1522 /* Add rbsp stop bit and trailing at the end */
1523 *(p + nal_size - 1) = 0x80;
1524
1525 return nal_size;
1526}
1527
Philipp Zabelc2d22512013-06-21 03:55:28 -03001528static void coda_setup_iram(struct coda_ctx *ctx)
1529{
1530 struct coda_iram_info *iram_info = &ctx->iram_info;
1531 struct coda_dev *dev = ctx->dev;
1532 int ipacdc_size;
1533 int bitram_size;
1534 int dbk_size;
Philipp Zabel8358e762013-06-21 03:55:32 -03001535 int ovl_size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001536 int mb_width;
1537 int me_size;
1538 int size;
1539
1540 memset(iram_info, 0, sizeof(*iram_info));
1541 size = dev->iram_size;
1542
1543 if (dev->devtype->product == CODA_DX6)
1544 return;
1545
1546 if (ctx->inst_type == CODA_INST_ENCODER) {
1547 struct coda_q_data *q_data_src;
1548
1549 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1550 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1551
1552 /* Prioritize in case IRAM is too small for everything */
1553 me_size = round_up(round_up(q_data_src->width, 16) * 36 + 2048,
1554 1024);
1555 iram_info->search_ram_size = me_size;
1556 if (size >= iram_info->search_ram_size) {
1557 if (dev->devtype->product == CODA_7541)
1558 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE;
1559 iram_info->search_ram_paddr = dev->iram_paddr;
1560 size -= iram_info->search_ram_size;
1561 } else {
1562 pr_err("IRAM is smaller than the search ram size\n");
1563 goto out;
1564 }
1565
1566 /* Only H.264BP and H.263P3 are considered */
1567 dbk_size = round_up(128 * mb_width, 1024);
1568 if (size >= dbk_size) {
1569 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1570 iram_info->buf_dbk_y_use = dev->iram_paddr +
1571 iram_info->search_ram_size;
1572 iram_info->buf_dbk_c_use = iram_info->buf_dbk_y_use +
1573 dbk_size / 2;
1574 size -= dbk_size;
1575 } else {
1576 goto out;
1577 }
1578
1579 bitram_size = round_up(128 * mb_width, 1024);
1580 if (size >= bitram_size) {
1581 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1582 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1583 dbk_size / 2;
1584 size -= bitram_size;
1585 } else {
1586 goto out;
1587 }
1588
1589 ipacdc_size = round_up(128 * mb_width, 1024);
1590 if (size >= ipacdc_size) {
1591 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1592 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1593 bitram_size;
1594 size -= ipacdc_size;
1595 }
1596
Philipp Zabel8358e762013-06-21 03:55:32 -03001597 /* OVL and BTP disabled for encoder */
1598 } else if (ctx->inst_type == CODA_INST_DECODER) {
1599 struct coda_q_data *q_data_dst;
1600 int mb_height;
1601
1602 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1603 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
1604 mb_height = DIV_ROUND_UP(q_data_dst->height, 16);
1605
1606 dbk_size = round_up(256 * mb_width, 1024);
1607 if (size >= dbk_size) {
1608 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1609 iram_info->buf_dbk_y_use = dev->iram_paddr;
1610 iram_info->buf_dbk_c_use = dev->iram_paddr +
1611 dbk_size / 2;
1612 size -= dbk_size;
1613 } else {
1614 goto out;
1615 }
1616
1617 bitram_size = round_up(128 * mb_width, 1024);
1618 if (size >= bitram_size) {
1619 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1620 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1621 dbk_size / 2;
1622 size -= bitram_size;
1623 } else {
1624 goto out;
1625 }
1626
1627 ipacdc_size = round_up(128 * mb_width, 1024);
1628 if (size >= ipacdc_size) {
1629 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1630 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1631 bitram_size;
1632 size -= ipacdc_size;
1633 } else {
1634 goto out;
1635 }
1636
1637 ovl_size = round_up(80 * mb_width, 1024);
Philipp Zabelc2d22512013-06-21 03:55:28 -03001638 }
1639
1640out:
1641 switch (dev->devtype->product) {
1642 case CODA_DX6:
1643 break;
1644 case CODA_7541:
1645 /* i.MX53 uses secondary AXI for IRAM access */
1646 if (iram_info->axi_sram_use & CODA7_USE_HOST_BIT_ENABLE)
1647 iram_info->axi_sram_use |= CODA7_USE_BIT_ENABLE;
1648 if (iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)
1649 iram_info->axi_sram_use |= CODA7_USE_IP_ENABLE;
1650 if (iram_info->axi_sram_use & CODA7_USE_HOST_DBK_ENABLE)
1651 iram_info->axi_sram_use |= CODA7_USE_DBK_ENABLE;
1652 if (iram_info->axi_sram_use & CODA7_USE_HOST_OVL_ENABLE)
1653 iram_info->axi_sram_use |= CODA7_USE_OVL_ENABLE;
1654 if (iram_info->axi_sram_use & CODA7_USE_HOST_ME_ENABLE)
1655 iram_info->axi_sram_use |= CODA7_USE_ME_ENABLE;
1656 }
1657
1658 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1659 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1660 "IRAM smaller than needed\n");
1661
1662 if (dev->devtype->product == CODA_7541) {
1663 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001664 if (ctx->inst_type == CODA_INST_DECODER) {
1665 /* fw 1.4.50 */
1666 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1667 CODA7_USE_IP_ENABLE);
1668 } else {
1669 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001670 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1671 CODA7_USE_HOST_DBK_ENABLE |
1672 CODA7_USE_IP_ENABLE |
1673 CODA7_USE_DBK_ENABLE);
1674 }
1675 }
1676}
1677
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001678static void coda_free_context_buffers(struct coda_ctx *ctx)
1679{
1680 struct coda_dev *dev = ctx->dev;
1681
Philipp Zabel918c66f2013-06-27 06:59:01 -03001682 coda_free_aux_buf(dev, &ctx->slicebuf);
1683 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001684 if (dev->devtype->product != CODA_DX6)
1685 coda_free_aux_buf(dev, &ctx->workbuf);
1686}
1687
1688static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1689 struct coda_q_data *q_data)
1690{
1691 struct coda_dev *dev = ctx->dev;
1692 size_t size;
1693 int ret;
1694
1695 switch (dev->devtype->product) {
1696 case CODA_7541:
1697 size = CODA7_WORK_BUF_SIZE;
1698 break;
1699 default:
1700 return 0;
1701 }
1702
Philipp Zabel918c66f2013-06-27 06:59:01 -03001703 if (ctx->psbuf.vaddr) {
1704 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1705 return -EBUSY;
1706 }
1707 if (ctx->slicebuf.vaddr) {
1708 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1709 return -EBUSY;
1710 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001711 if (ctx->workbuf.vaddr) {
1712 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1713 ret = -EBUSY;
1714 return -ENOMEM;
1715 }
1716
Philipp Zabel918c66f2013-06-27 06:59:01 -03001717 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1718 /* worst case slice size */
1719 size = (DIV_ROUND_UP(q_data->width, 16) *
1720 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1721 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1722 if (ret < 0) {
1723 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1724 ctx->slicebuf.size);
1725 return ret;
1726 }
1727 }
1728
1729 if (dev->devtype->product == CODA_7541) {
1730 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1731 if (ret < 0) {
1732 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1733 goto err;
1734 }
1735 }
1736
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001737 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1738 if (ret < 0) {
1739 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1740 ctx->workbuf.size);
1741 goto err;
1742 }
1743
1744 return 0;
1745
1746err:
1747 coda_free_context_buffers(ctx);
1748 return ret;
1749}
1750
Philipp Zabel918c66f2013-06-27 06:59:01 -03001751static int coda_start_decoding(struct coda_ctx *ctx)
1752{
1753 struct coda_q_data *q_data_src, *q_data_dst;
1754 u32 bitstream_buf, bitstream_size;
1755 struct coda_dev *dev = ctx->dev;
1756 int width, height;
1757 u32 src_fourcc;
1758 u32 val;
1759 int ret;
1760
1761 /* Start decoding */
1762 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1763 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1764 bitstream_buf = ctx->bitstream.paddr;
1765 bitstream_size = ctx->bitstream.size;
1766 src_fourcc = q_data_src->fourcc;
1767
1768 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1769
1770 /* Update coda bitstream read and write pointers from kfifo */
1771 coda_kfifo_sync_to_device_full(ctx);
1772
1773 ctx->display_idx = -1;
1774 ctx->frm_dis_flg = 0;
1775 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1776
1777 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1778 CODA_REG_BIT_BIT_STREAM_PARAM);
1779
1780 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1781 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1782 val = 0;
1783 if (dev->devtype->product == CODA_7541)
1784 val |= CODA_REORDER_ENABLE;
1785 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1786
1787 ctx->params.codec_mode = ctx->codec->mode;
1788 ctx->params.codec_mode_aux = 0;
1789 if (src_fourcc == V4L2_PIX_FMT_H264) {
1790 if (dev->devtype->product == CODA_7541) {
1791 coda_write(dev, ctx->psbuf.paddr,
1792 CODA_CMD_DEC_SEQ_PS_BB_START);
1793 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1794 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1795 }
1796 }
1797
1798 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1799 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1800 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1801 return -ETIMEDOUT;
1802 }
1803
1804 /* Update kfifo out pointer from coda bitstream read pointer */
1805 coda_kfifo_sync_from_device(ctx);
1806
1807 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1808
1809 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1810 v4l2_err(&dev->v4l2_dev,
1811 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1812 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1813 return -EAGAIN;
1814 }
1815
1816 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1817 if (dev->devtype->product == CODA_DX6) {
1818 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1819 height = val & CODADX6_PICHEIGHT_MASK;
1820 } else {
1821 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1822 height = val & CODA7_PICHEIGHT_MASK;
1823 }
1824
1825 if (width > q_data_dst->width || height > q_data_dst->height) {
1826 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1827 width, height, q_data_dst->width, q_data_dst->height);
1828 return -EINVAL;
1829 }
1830
1831 width = round_up(width, 16);
1832 height = round_up(height, 16);
1833
1834 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1835 __func__, ctx->idx, width, height);
1836
1837 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED) + 1;
1838 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1839 v4l2_err(&dev->v4l2_dev,
1840 "not enough framebuffers to decode (%d < %d)\n",
1841 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1842 return -EINVAL;
1843 }
1844
1845 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
1846 if (ret < 0)
1847 return ret;
1848
1849 /* Tell the decoder how many frame buffers we allocated. */
1850 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1851 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1852
1853 if (dev->devtype->product != CODA_DX6) {
1854 /* Set secondary AXI IRAM */
1855 coda_setup_iram(ctx);
1856
1857 coda_write(dev, ctx->iram_info.buf_bit_use,
1858 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1859 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1860 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1861 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1862 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1863 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1864 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1865 coda_write(dev, ctx->iram_info.buf_ovl_use,
1866 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1867 }
1868
1869 if (src_fourcc == V4L2_PIX_FMT_H264) {
1870 coda_write(dev, ctx->slicebuf.paddr,
1871 CODA_CMD_SET_FRAME_SLICE_BB_START);
1872 coda_write(dev, ctx->slicebuf.size / 1024,
1873 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
1874 }
1875
1876 if (dev->devtype->product == CODA_7541) {
1877 int max_mb_x = 1920 / 16;
1878 int max_mb_y = 1088 / 16;
1879 int max_mb_num = max_mb_x * max_mb_y;
1880 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1881 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
1882 }
1883
1884 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1885 v4l2_err(&ctx->dev->v4l2_dev,
1886 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1887 return -ETIMEDOUT;
1888 }
1889
1890 return 0;
1891}
1892
Philipp Zabeld35167a2013-05-23 10:42:59 -03001893static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1894 int header_code, u8 *header, int *size)
1895{
1896 struct coda_dev *dev = ctx->dev;
1897 int ret;
1898
1899 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1900 CODA_CMD_ENC_HEADER_BB_START);
1901 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1902 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1903 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1904 if (ret < 0) {
1905 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1906 return ret;
1907 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001908 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
Philipp Zabeld35167a2013-05-23 10:42:59 -03001909 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1910 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1911
1912 return 0;
1913}
1914
Javier Martin186b2502012-07-26 05:53:35 -03001915static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1916{
1917 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1918 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1919 u32 bitstream_buf, bitstream_size;
1920 struct coda_dev *dev = ctx->dev;
1921 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -03001922 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001923 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001924 u32 value;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001925 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001926
Philipp Zabelb96904e2013-05-23 10:42:58 -03001927 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001928 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1929 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
1930 if (coda_get_bitstream_payload(ctx) < 512)
1931 return -EINVAL;
1932 } else {
1933 if (count < 1)
1934 return -EINVAL;
1935 }
1936
1937 ctx->streamon_out = 1;
1938
Philipp Zabelb96904e2013-05-23 10:42:58 -03001939 if (coda_format_is_yuv(q_data_src->fourcc))
1940 ctx->inst_type = CODA_INST_ENCODER;
1941 else
1942 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001943 } else {
1944 if (count < 1)
1945 return -EINVAL;
1946
1947 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001948 }
Javier Martin186b2502012-07-26 05:53:35 -03001949
Philipp Zabelb96904e2013-05-23 10:42:58 -03001950 /* Don't start the coda unless both queues are on */
1951 if (!(ctx->streamon_out & ctx->streamon_cap))
1952 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001953
Philipp Zabeleb107512013-09-30 10:34:45 -03001954 /* Allow decoder device_run with no new buffers queued */
1955 if (ctx->inst_type == CODA_INST_DECODER)
1956 v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001957
Philipp Zabelb96904e2013-05-23 10:42:58 -03001958 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001959 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1960 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1961 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1962 bitstream_size = q_data_dst->sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001963 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001964
Philipp Zabelb96904e2013-05-23 10:42:58 -03001965 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1966 q_data_dst->fourcc);
1967 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001968 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1969 return -EINVAL;
1970 }
1971
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001972 /* Allocate per-instance buffers */
1973 ret = coda_alloc_context_buffers(ctx, q_data_src);
1974 if (ret < 0)
1975 return ret;
1976
Philipp Zabel918c66f2013-06-27 06:59:01 -03001977 if (ctx->inst_type == CODA_INST_DECODER) {
1978 mutex_lock(&dev->coda_mutex);
1979 ret = coda_start_decoding(ctx);
1980 mutex_unlock(&dev->coda_mutex);
1981 if (ret == -EAGAIN) {
1982 return 0;
1983 } else if (ret < 0) {
1984 return ret;
1985 } else {
1986 ctx->initialized = 1;
1987 return 0;
1988 }
1989 }
1990
Javier Martin186b2502012-07-26 05:53:35 -03001991 if (!coda_is_initialized(dev)) {
1992 v4l2_err(v4l2_dev, "coda is not initialized.\n");
1993 return -EFAULT;
1994 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03001995
1996 mutex_lock(&dev->coda_mutex);
1997
Javier Martin186b2502012-07-26 05:53:35 -03001998 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001999 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2000 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002001 switch (dev->devtype->product) {
2002 case CODA_DX6:
2003 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2004 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2005 break;
2006 default:
2007 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2008 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2009 }
2010
Philipp Zabel10436672012-07-02 09:03:55 -03002011 if (dev->devtype->product == CODA_DX6) {
2012 /* Configure the coda */
2013 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
2014 }
Javier Martin186b2502012-07-26 05:53:35 -03002015
2016 /* Could set rotation here if needed */
2017 switch (dev->devtype->product) {
2018 case CODA_DX6:
2019 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002020 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002021 break;
2022 default:
2023 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002024 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002025 }
Javier Martin186b2502012-07-26 05:53:35 -03002026 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2027 coda_write(dev, ctx->params.framerate,
2028 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2029
Philipp Zabelb96904e2013-05-23 10:42:58 -03002030 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002031 switch (dst_fourcc) {
2032 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -03002033 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2034 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2035 break;
2036 case V4L2_PIX_FMT_H264:
Javier Martin186b2502012-07-26 05:53:35 -03002037 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2038 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2039 break;
2040 default:
2041 v4l2_err(v4l2_dev,
2042 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002043 ret = -EINVAL;
2044 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002045 }
2046
Philipp Zabelc566c782012-08-08 11:59:38 -03002047 switch (ctx->params.slice_mode) {
2048 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2049 value = 0;
2050 break;
2051 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2052 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2053 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002054 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002055 break;
2056 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2057 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2058 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2059 value |= 1 & CODA_SLICING_MODE_MASK;
2060 break;
2061 }
Javier Martin186b2502012-07-26 05:53:35 -03002062 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002063 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002064 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2065
2066 if (ctx->params.bitrate) {
2067 /* Rate control enabled */
2068 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2069 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
2070 } else {
2071 value = 0;
2072 }
2073 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2074
2075 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2076 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2077
2078 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2079 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2080
2081 /* set default gamma */
2082 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
2083 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
2084
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002085 if (CODA_DEFAULT_GAMMA > 0) {
2086 if (dev->devtype->product == CODA_DX6)
2087 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
2088 else
2089 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
2090 } else {
2091 value = 0;
2092 }
Javier Martin186b2502012-07-26 05:53:35 -03002093 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2094
Philipp Zabelc2d22512013-06-21 03:55:28 -03002095 coda_setup_iram(ctx);
2096
Javier Martin186b2502012-07-26 05:53:35 -03002097 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel10436672012-07-02 09:03:55 -03002098 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002099 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002100 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
2101 } else {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002102 coda_write(dev, ctx->iram_info.search_ram_paddr,
2103 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2104 coda_write(dev, ctx->iram_info.search_ram_size,
2105 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -03002106 }
Javier Martin186b2502012-07-26 05:53:35 -03002107 }
2108
Philipp Zabelfcb62822013-05-23 10:43:00 -03002109 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2110 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002111 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002112 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002113 }
2114
Philipp Zabelfcb62822013-05-23 10:43:00 -03002115 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2116 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2117 ret = -EFAULT;
2118 goto out;
2119 }
Javier Martin186b2502012-07-26 05:53:35 -03002120
Philipp Zabel20397492013-06-21 03:55:29 -03002121 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002122 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002123 if (ret < 0) {
2124 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2125 goto out;
2126 }
Javier Martin186b2502012-07-26 05:53:35 -03002127
Philipp Zabelec25f682012-07-20 08:54:29 -03002128 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002129 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002130 if (dev->devtype->product == CODA_7541)
2131 coda_write(dev, round_up(q_data_src->width, 8),
2132 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002133 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002134 coda_write(dev, ctx->iram_info.buf_bit_use,
2135 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2136 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2137 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2138 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2139 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2140 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2141 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2142 coda_write(dev, ctx->iram_info.buf_ovl_use,
2143 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002144 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002145 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2146 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002147 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002148 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002149 }
2150
2151 /* Save stream headers */
2152 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2153 switch (dst_fourcc) {
2154 case V4L2_PIX_FMT_H264:
2155 /*
2156 * Get SPS in the first frame and copy it to an
2157 * intermediate buffer.
2158 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002159 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2160 &ctx->vpu_header[0][0],
2161 &ctx->vpu_header_size[0]);
2162 if (ret < 0)
2163 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002164
2165 /*
2166 * Get PPS in the first frame and copy it to an
2167 * intermediate buffer.
2168 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002169 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2170 &ctx->vpu_header[1][0],
2171 &ctx->vpu_header_size[1]);
2172 if (ret < 0)
2173 goto out;
2174
Javier Martin3f3f5c72012-10-29 05:20:29 -03002175 /*
2176 * Length of H.264 headers is variable and thus it might not be
2177 * aligned for the coda to append the encoded frame. In that is
2178 * the case a filler NAL must be added to header 2.
2179 */
2180 ctx->vpu_header_size[2] = coda_h264_padding(
2181 (ctx->vpu_header_size[0] +
2182 ctx->vpu_header_size[1]),
2183 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002184 break;
2185 case V4L2_PIX_FMT_MPEG4:
2186 /*
2187 * Get VOS in the first frame and copy it to an
2188 * intermediate buffer
2189 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002190 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2191 &ctx->vpu_header[0][0],
2192 &ctx->vpu_header_size[0]);
2193 if (ret < 0)
2194 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002195
Philipp Zabeld35167a2013-05-23 10:42:59 -03002196 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2197 &ctx->vpu_header[1][0],
2198 &ctx->vpu_header_size[1]);
2199 if (ret < 0)
2200 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002201
Philipp Zabeld35167a2013-05-23 10:42:59 -03002202 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2203 &ctx->vpu_header[2][0],
2204 &ctx->vpu_header_size[2]);
2205 if (ret < 0)
2206 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002207 break;
2208 default:
2209 /* No more formats need to save headers at the moment */
2210 break;
2211 }
2212
Philipp Zabeld35167a2013-05-23 10:42:59 -03002213out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002214 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002215 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002216}
2217
2218static int coda_stop_streaming(struct vb2_queue *q)
2219{
2220 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002221 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002222
2223 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002224 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002225 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002226 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002227
2228 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2229
2230 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002231 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002232 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002233 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002234 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002235
2236 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002237 }
2238
Philipp Zabel918c66f2013-06-27 06:59:01 -03002239 if (!ctx->streamon_out && !ctx->streamon_cap) {
2240 kfifo_init(&ctx->bitstream_fifo,
2241 ctx->bitstream.vaddr, ctx->bitstream.size);
2242 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002243 }
Philipp Zabel62bed142012-07-25 10:40:39 -03002244
Javier Martin186b2502012-07-26 05:53:35 -03002245 return 0;
2246}
2247
2248static struct vb2_ops coda_qops = {
2249 .queue_setup = coda_queue_setup,
2250 .buf_prepare = coda_buf_prepare,
2251 .buf_queue = coda_buf_queue,
2252 .wait_prepare = coda_wait_prepare,
2253 .wait_finish = coda_wait_finish,
2254 .start_streaming = coda_start_streaming,
2255 .stop_streaming = coda_stop_streaming,
2256};
2257
2258static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2259{
2260 struct coda_ctx *ctx =
2261 container_of(ctrl->handler, struct coda_ctx, ctrls);
2262
2263 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2264 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2265
2266 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002267 case V4L2_CID_HFLIP:
2268 if (ctrl->val)
2269 ctx->params.rot_mode |= CODA_MIR_HOR;
2270 else
2271 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2272 break;
2273 case V4L2_CID_VFLIP:
2274 if (ctrl->val)
2275 ctx->params.rot_mode |= CODA_MIR_VER;
2276 else
2277 ctx->params.rot_mode &= ~CODA_MIR_VER;
2278 break;
Javier Martin186b2502012-07-26 05:53:35 -03002279 case V4L2_CID_MPEG_VIDEO_BITRATE:
2280 ctx->params.bitrate = ctrl->val / 1000;
2281 break;
2282 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2283 ctx->params.gop_size = ctrl->val;
2284 break;
2285 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2286 ctx->params.h264_intra_qp = ctrl->val;
2287 break;
2288 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2289 ctx->params.h264_inter_qp = ctrl->val;
2290 break;
2291 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2292 ctx->params.mpeg4_intra_qp = ctrl->val;
2293 break;
2294 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2295 ctx->params.mpeg4_inter_qp = ctrl->val;
2296 break;
2297 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2298 ctx->params.slice_mode = ctrl->val;
2299 break;
2300 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2301 ctx->params.slice_max_mb = ctrl->val;
2302 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002303 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2304 ctx->params.slice_max_bits = ctrl->val * 8;
2305 break;
Javier Martin186b2502012-07-26 05:53:35 -03002306 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2307 break;
2308 default:
2309 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2310 "Invalid control, id=%d, val=%d\n",
2311 ctrl->id, ctrl->val);
2312 return -EINVAL;
2313 }
2314
2315 return 0;
2316}
2317
2318static struct v4l2_ctrl_ops coda_ctrl_ops = {
2319 .s_ctrl = coda_s_ctrl,
2320};
2321
2322static int coda_ctrls_setup(struct coda_ctx *ctx)
2323{
2324 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2325
2326 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002327 V4L2_CID_HFLIP, 0, 1, 1, 0);
2328 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2329 V4L2_CID_VFLIP, 0, 1, 1, 0);
2330 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002331 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2332 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2333 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2334 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2335 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 25);
2336 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2337 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 25);
2338 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2339 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2340 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2341 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2342 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2343 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002344 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2345 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002346 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2347 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002348 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2349 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002350 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2351 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2352 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2353 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2354 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2355
2356 if (ctx->ctrls.error) {
2357 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2358 ctx->ctrls.error);
2359 return -EINVAL;
2360 }
2361
2362 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2363}
2364
2365static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2366 struct vb2_queue *dst_vq)
2367{
2368 struct coda_ctx *ctx = priv;
2369 int ret;
2370
Javier Martin186b2502012-07-26 05:53:35 -03002371 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002372 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002373 src_vq->drv_priv = ctx;
2374 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2375 src_vq->ops = &coda_qops;
2376 src_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03002377 src_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002378
2379 ret = vb2_queue_init(src_vq);
2380 if (ret)
2381 return ret;
2382
Javier Martin186b2502012-07-26 05:53:35 -03002383 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002384 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002385 dst_vq->drv_priv = ctx;
2386 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2387 dst_vq->ops = &coda_qops;
2388 dst_vq->mem_ops = &vb2_dma_contig_memops;
Kamil Debskiccd571c2013-04-24 10:38:24 -03002389 dst_vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002390
2391 return vb2_queue_init(dst_vq);
2392}
2393
Philipp Zabele11f3e62012-07-25 09:16:58 -03002394static int coda_next_free_instance(struct coda_dev *dev)
2395{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002396 int idx = ffz(dev->instance_mask);
2397
2398 if ((idx < 0) ||
2399 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2400 return -EBUSY;
2401
2402 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002403}
2404
Javier Martin186b2502012-07-26 05:53:35 -03002405static int coda_open(struct file *file)
2406{
2407 struct coda_dev *dev = video_drvdata(file);
2408 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002409 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002410 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002411
Javier Martin186b2502012-07-26 05:53:35 -03002412 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2413 if (!ctx)
2414 return -ENOMEM;
2415
Fabio Estevamf82bc202013-08-21 11:14:16 -03002416 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002417 if (idx < 0) {
2418 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002419 goto err_coda_max;
2420 }
2421 set_bit(idx, &dev->instance_mask);
2422
Philipp Zabel918c66f2013-06-27 06:59:01 -03002423 INIT_WORK(&ctx->skip_run, coda_skip_run);
Javier Martin186b2502012-07-26 05:53:35 -03002424 v4l2_fh_init(&ctx->fh, video_devdata(file));
2425 file->private_data = &ctx->fh;
2426 v4l2_fh_add(&ctx->fh);
2427 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002428 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002429 switch (dev->devtype->product) {
2430 case CODA_7541:
2431 ctx->reg_idx = 0;
2432 break;
2433 default:
2434 ctx->reg_idx = idx;
2435 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002436
Fabio Estevam79830db2013-08-21 11:14:17 -03002437 ret = clk_prepare_enable(dev->clk_per);
2438 if (ret)
2439 goto err_clk_per;
2440
2441 ret = clk_prepare_enable(dev->clk_ahb);
2442 if (ret)
2443 goto err_clk_ahb;
2444
Javier Martin186b2502012-07-26 05:53:35 -03002445 set_default_params(ctx);
2446 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2447 &coda_queue_init);
2448 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03002449 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002450
2451 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2452 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002453 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002454 }
2455 ret = coda_ctrls_setup(ctx);
2456 if (ret) {
2457 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002458 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002459 }
2460
2461 ctx->fh.ctrl_handler = &ctx->ctrls;
2462
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002463 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2464 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002465 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002466 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002467 }
2468
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002469 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2470 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2471 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2472 if (!ctx->bitstream.vaddr) {
2473 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2474 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002475 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002476 }
2477 kfifo_init(&ctx->bitstream_fifo,
2478 ctx->bitstream.vaddr, ctx->bitstream.size);
2479 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002480 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002481
Javier Martin186b2502012-07-26 05:53:35 -03002482 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002483 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002484 coda_unlock(ctx);
2485
Javier Martin186b2502012-07-26 05:53:35 -03002486 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2487 ctx->idx, ctx);
2488
2489 return 0;
2490
Fabio Estevamf82bc202013-08-21 11:14:16 -03002491err_dma_writecombine:
2492 coda_free_context_buffers(ctx);
2493 if (ctx->dev->devtype->product == CODA_DX6)
2494 coda_free_aux_buf(dev, &ctx->workbuf);
2495 coda_free_aux_buf(dev, &ctx->parabuf);
2496err_dma_alloc:
2497 v4l2_ctrl_handler_free(&ctx->ctrls);
2498err_ctrls_setup:
2499 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2500err_ctx_init:
2501 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002502err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002503 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002504err_clk_per:
Javier Martin186b2502012-07-26 05:53:35 -03002505 v4l2_fh_del(&ctx->fh);
2506 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002507 clear_bit(ctx->idx, &dev->instance_mask);
2508err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002509 kfree(ctx);
2510 return ret;
2511}
2512
2513static int coda_release(struct file *file)
2514{
2515 struct coda_dev *dev = video_drvdata(file);
2516 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2517
2518 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2519 ctx);
2520
Philipp Zabel918c66f2013-06-27 06:59:01 -03002521 /* If this instance is running, call .job_abort and wait for it to end */
2522 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2523
2524 /* In case the instance was not running, we still need to call SEQ_END */
2525 mutex_lock(&dev->coda_mutex);
2526 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2527 "%s: sent command 'SEQ_END' to coda\n", __func__);
2528 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
2529 v4l2_err(&dev->v4l2_dev,
2530 "CODA_COMMAND_SEQ_END failed\n");
2531 mutex_unlock(&dev->coda_mutex);
2532 return -ETIMEDOUT;
2533 }
2534 mutex_unlock(&dev->coda_mutex);
2535
2536 coda_free_framebuffers(ctx);
2537
Javier Martin186b2502012-07-26 05:53:35 -03002538 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002539 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002540 coda_unlock(ctx);
2541
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002542 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2543 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002544 coda_free_context_buffers(ctx);
2545 if (ctx->dev->devtype->product == CODA_DX6)
2546 coda_free_aux_buf(dev, &ctx->workbuf);
2547
2548 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002549 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002550 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002551 clk_disable_unprepare(dev->clk_per);
Javier Martin186b2502012-07-26 05:53:35 -03002552 v4l2_fh_del(&ctx->fh);
2553 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002554 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002555 kfree(ctx);
2556
2557 return 0;
2558}
2559
2560static unsigned int coda_poll(struct file *file,
2561 struct poll_table_struct *wait)
2562{
2563 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2564 int ret;
2565
2566 coda_lock(ctx);
2567 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
2568 coda_unlock(ctx);
2569 return ret;
2570}
2571
2572static int coda_mmap(struct file *file, struct vm_area_struct *vma)
2573{
2574 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2575
2576 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2577}
2578
2579static const struct v4l2_file_operations coda_fops = {
2580 .owner = THIS_MODULE,
2581 .open = coda_open,
2582 .release = coda_release,
2583 .poll = coda_poll,
2584 .unlocked_ioctl = video_ioctl2,
2585 .mmap = coda_mmap,
2586};
2587
Philipp Zabel918c66f2013-06-27 06:59:01 -03002588static void coda_finish_decode(struct coda_ctx *ctx)
2589{
2590 struct coda_dev *dev = ctx->dev;
2591 struct coda_q_data *q_data_src;
2592 struct coda_q_data *q_data_dst;
2593 struct vb2_buffer *dst_buf;
2594 int width, height;
2595 int decoded_idx;
2596 int display_idx;
2597 u32 src_fourcc;
2598 int success;
2599 u32 val;
2600
2601 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2602
2603 /* Update kfifo out pointer from coda bitstream read pointer */
2604 coda_kfifo_sync_from_device(ctx);
2605
2606 /*
2607 * in stream-end mode, the read pointer can overshoot the write pointer
2608 * by up to 512 bytes
2609 */
2610 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2611 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2612 kfifo_init(&ctx->bitstream_fifo,
2613 ctx->bitstream.vaddr, ctx->bitstream.size);
2614 }
2615
2616 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2617 src_fourcc = q_data_src->fourcc;
2618
2619 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2620 if (val != 1)
2621 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2622
2623 success = val & 0x1;
2624 if (!success)
2625 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2626
2627 if (src_fourcc == V4L2_PIX_FMT_H264) {
2628 if (val & (1 << 3))
2629 v4l2_err(&dev->v4l2_dev,
2630 "insufficient PS buffer space (%d bytes)\n",
2631 ctx->psbuf.size);
2632 if (val & (1 << 2))
2633 v4l2_err(&dev->v4l2_dev,
2634 "insufficient slice buffer space (%d bytes)\n",
2635 ctx->slicebuf.size);
2636 }
2637
2638 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2639 width = (val >> 16) & 0xffff;
2640 height = val & 0xffff;
2641
2642 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2643
2644 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE);
2645 if ((val & 0x7) == 0) {
2646 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
2647 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
2648 } else {
2649 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
2650 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
2651 }
2652
2653 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2654 if (val > 0)
2655 v4l2_err(&dev->v4l2_dev,
2656 "errors in %d macroblocks\n", val);
2657
2658 if (dev->devtype->product == CODA_7541) {
2659 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2660 if (val == 0) {
2661 /* not enough bitstream data */
2662 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2663 "prescan failed: %d\n", val);
2664 ctx->prescan_failed = true;
2665 return;
2666 }
2667 }
2668
2669 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2670
2671 /*
2672 * The previous display frame was copied out by the rotator,
2673 * now it can be overwritten again
2674 */
2675 if (ctx->display_idx >= 0 &&
2676 ctx->display_idx < ctx->num_internal_frames) {
2677 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
2678 coda_write(dev, ctx->frm_dis_flg,
2679 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2680 }
2681
2682 /*
2683 * The index of the last decoded frame, not necessarily in
2684 * display order, and the index of the next display frame.
2685 * The latter could have been decoded in a previous run.
2686 */
2687 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
2688 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
2689
2690 if (decoded_idx == -1) {
2691 /* no frame was decoded, but we might have a display frame */
2692 if (display_idx < 0 && ctx->display_idx < 0)
2693 ctx->prescan_failed = true;
2694 } else if (decoded_idx == -2) {
2695 /* no frame was decoded, we still return the remaining buffers */
2696 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
2697 v4l2_err(&dev->v4l2_dev,
2698 "decoded frame index out of range: %d\n", decoded_idx);
2699 }
2700
2701 if (display_idx == -1) {
2702 /*
2703 * no more frames to be decoded, but there could still
2704 * be rotator output to dequeue
2705 */
2706 ctx->prescan_failed = true;
2707 } else if (display_idx == -3) {
2708 /* possibly prescan failure */
2709 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
2710 v4l2_err(&dev->v4l2_dev,
2711 "presentation frame index out of range: %d\n",
2712 display_idx);
2713 }
2714
2715 /* If a frame was copied out, return it */
2716 if (ctx->display_idx >= 0 &&
2717 ctx->display_idx < ctx->num_internal_frames) {
2718 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
2719 dst_buf->v4l2_buf.sequence = ctx->osequence++;
2720
2721 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
2722
2723 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
2724 VB2_BUF_STATE_ERROR);
2725
2726 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2727 "job finished: decoding frame (%d) (%s)\n",
2728 dst_buf->v4l2_buf.sequence,
2729 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2730 "KEYFRAME" : "PFRAME");
2731 } else {
2732 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2733 "job finished: no frame decoded\n");
2734 }
2735
2736 /* The rotator will copy the current display frame next time */
2737 ctx->display_idx = display_idx;
2738}
2739
2740static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03002741{
Philipp Zabelec25f682012-07-20 08:54:29 -03002742 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002743 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002744 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03002745
Philipp Zabelec25f682012-07-20 08:54:29 -03002746 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2747 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002748
2749 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03002750 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002751 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
2752
Javier Martin186b2502012-07-26 05:53:35 -03002753 /* Calculate bytesused field */
2754 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03002755 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
2756 ctx->vpu_header_size[0] +
2757 ctx->vpu_header_size[1] +
2758 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002759 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03002760 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03002761 }
2762
2763 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
2764 wr_ptr - start_ptr);
2765
2766 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
2767 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
2768
Philipp Zabel51660282013-09-30 10:34:49 -03002769 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002770 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
2771 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
2772 } else {
2773 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
2774 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
2775 }
2776
Kamil Debskiccd571c2013-04-24 10:38:24 -03002777 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
2778 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
2779
Philipp Zabelec25f682012-07-20 08:54:29 -03002780 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03002781 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
2782
2783 ctx->gopcounter--;
2784 if (ctx->gopcounter < 0)
2785 ctx->gopcounter = ctx->params.gop_size - 1;
2786
2787 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2788 "job finished: encoding frame (%d) (%s)\n",
2789 dst_buf->v4l2_buf.sequence,
2790 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2791 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002792}
2793
2794static irqreturn_t coda_irq_handler(int irq, void *data)
2795{
2796 struct coda_dev *dev = data;
2797 struct coda_ctx *ctx;
2798
2799 cancel_delayed_work(&dev->timeout);
2800
2801 /* read status register to attend the IRQ */
2802 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2803 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2804 CODA_REG_BIT_INT_CLEAR);
2805
2806 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2807 if (ctx == NULL) {
2808 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
2809 mutex_unlock(&dev->coda_mutex);
2810 return IRQ_HANDLED;
2811 }
2812
2813 if (ctx->aborting) {
2814 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2815 "task has been aborted\n");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002816 goto out;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002817 }
2818
2819 if (coda_isbusy(ctx->dev)) {
2820 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2821 "coda is still busy!!!!\n");
2822 return IRQ_NONE;
2823 }
2824
Philipp Zabel918c66f2013-06-27 06:59:01 -03002825 if (ctx->inst_type == CODA_INST_DECODER)
2826 coda_finish_decode(ctx);
2827 else
2828 coda_finish_encode(ctx);
2829
2830out:
2831 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) {
2832 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2833 "%s: sent command 'SEQ_END' to coda\n", __func__);
2834 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
2835 v4l2_err(&dev->v4l2_dev,
2836 "CODA_COMMAND_SEQ_END failed\n");
2837 }
2838
2839 kfifo_init(&ctx->bitstream_fifo,
2840 ctx->bitstream.vaddr, ctx->bitstream.size);
2841
2842 coda_free_framebuffers(ctx);
2843 coda_free_context_buffers(ctx);
2844 }
Javier Martin186b2502012-07-26 05:53:35 -03002845
Philipp Zabelfcb62822013-05-23 10:43:00 -03002846 mutex_unlock(&dev->coda_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002847 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002848
Javier Martin186b2502012-07-26 05:53:35 -03002849 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
2850
2851 return IRQ_HANDLED;
2852}
2853
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002854static void coda_timeout(struct work_struct *work)
2855{
2856 struct coda_ctx *ctx;
2857 struct coda_dev *dev = container_of(to_delayed_work(work),
2858 struct coda_dev, timeout);
2859
Philipp Zabel39cc0292013-05-21 10:32:42 -03002860 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002861
2862 mutex_lock(&dev->dev_mutex);
2863 list_for_each_entry(ctx, &dev->instances, list) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002864 if (mutex_is_locked(&ctx->buffer_mutex))
2865 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002866 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2867 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2868 }
2869 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002870
2871 mutex_unlock(&dev->coda_mutex);
2872 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2873 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002874}
2875
Javier Martin186b2502012-07-26 05:53:35 -03002876static u32 coda_supported_firmwares[] = {
2877 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002878 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Javier Martin186b2502012-07-26 05:53:35 -03002879};
2880
2881static bool coda_firmware_supported(u32 vernum)
2882{
2883 int i;
2884
2885 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
2886 if (vernum == coda_supported_firmwares[i])
2887 return true;
2888 return false;
2889}
2890
Philipp Zabel87048bb2012-07-02 07:03:43 -03002891static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03002892{
2893 u16 product, major, minor, release;
2894 u32 data;
2895 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03002896 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03002897
Fabio Estevam79830db2013-08-21 11:14:17 -03002898 ret = clk_prepare_enable(dev->clk_per);
2899 if (ret)
2900 return ret;
2901
2902 ret = clk_prepare_enable(dev->clk_ahb);
2903 if (ret)
2904 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03002905
Javier Martin186b2502012-07-26 05:53:35 -03002906 /*
2907 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03002908 * The 16-bit chars in the code buffer are in memory access
2909 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03002910 * Data in this SRAM survives a reboot.
2911 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03002912 p = (u16 *)dev->codebuf.vaddr;
2913 if (dev->devtype->product == CODA_DX6) {
2914 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2915 data = CODA_DOWN_ADDRESS_SET(i) |
2916 CODA_DOWN_DATA_SET(p[i ^ 1]);
2917 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2918 }
2919 } else {
2920 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2921 data = CODA_DOWN_ADDRESS_SET(i) |
2922 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2923 3 - (i % 4)]);
2924 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2925 }
Javier Martin186b2502012-07-26 05:53:35 -03002926 }
Javier Martin186b2502012-07-26 05:53:35 -03002927
Philipp Zabel9acf7692013-05-23 10:42:56 -03002928 /* Clear registers */
2929 for (i = 0; i < 64; i++)
2930 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2931
Javier Martin186b2502012-07-26 05:53:35 -03002932 /* Tell the BIT where to find everything it needs */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002933 if (dev->devtype->product == CODA_7541) {
2934 coda_write(dev, dev->tempbuf.paddr,
2935 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002936 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002937 } else {
2938 coda_write(dev, dev->workbuf.paddr,
2939 CODA_REG_BIT_WORK_BUF_ADDR);
2940 }
Javier Martin186b2502012-07-26 05:53:35 -03002941 coda_write(dev, dev->codebuf.paddr,
2942 CODA_REG_BIT_CODE_BUF_ADDR);
2943 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2944
2945 /* Set default values */
2946 switch (dev->devtype->product) {
2947 case CODA_DX6:
2948 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2949 break;
2950 default:
2951 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2952 }
2953 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03002954
2955 if (dev->devtype->product != CODA_DX6)
2956 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2957
Javier Martin186b2502012-07-26 05:53:35 -03002958 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2959 CODA_REG_BIT_INT_ENABLE);
2960
2961 /* Reset VPU and start processor */
2962 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2963 data |= CODA_REG_RESET_ENABLE;
2964 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2965 udelay(10);
2966 data &= ~CODA_REG_RESET_ENABLE;
2967 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2968 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2969
2970 /* Load firmware */
2971 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
2972 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
2973 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
2974 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
2975 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
2976 if (coda_wait_timeout(dev)) {
2977 clk_disable_unprepare(dev->clk_per);
2978 clk_disable_unprepare(dev->clk_ahb);
2979 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
2980 return -EIO;
2981 }
2982
2983 /* Check we are compatible with the loaded firmware */
2984 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
2985 product = CODA_FIRMWARE_PRODUCT(data);
2986 major = CODA_FIRMWARE_MAJOR(data);
2987 minor = CODA_FIRMWARE_MINOR(data);
2988 release = CODA_FIRMWARE_RELEASE(data);
2989
2990 clk_disable_unprepare(dev->clk_per);
2991 clk_disable_unprepare(dev->clk_ahb);
2992
2993 if (product != dev->devtype->product) {
2994 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
2995 " Version: %u.%u.%u\n",
2996 coda_product_name(dev->devtype->product),
2997 coda_product_name(product), major, minor, release);
2998 return -EINVAL;
2999 }
3000
3001 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3002 coda_product_name(product));
3003
3004 if (coda_firmware_supported(data)) {
3005 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3006 major, minor, release);
3007 } else {
3008 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3009 "%u.%u.%u\n", major, minor, release);
3010 }
3011
3012 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003013
3014err_clk_ahb:
3015 clk_disable_unprepare(dev->clk_per);
3016 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003017}
3018
3019static void coda_fw_callback(const struct firmware *fw, void *context)
3020{
3021 struct coda_dev *dev = context;
3022 struct platform_device *pdev = dev->plat_dev;
3023 int ret;
3024
3025 if (!fw) {
3026 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3027 return;
3028 }
3029
3030 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003031 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3032 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003033 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3034 return;
3035 }
3036
Philipp Zabel87048bb2012-07-02 07:03:43 -03003037 /* Copy the whole firmware image to the code buffer */
3038 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3039 release_firmware(fw);
3040
3041 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03003042 if (ret) {
3043 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3044 return;
3045 }
3046
3047 dev->vfd.fops = &coda_fops,
3048 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3049 dev->vfd.release = video_device_release_empty,
3050 dev->vfd.lock = &dev->dev_mutex;
3051 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003052 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003053 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3054 video_set_drvdata(&dev->vfd, dev);
3055
3056 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3057 if (IS_ERR(dev->alloc_ctx)) {
3058 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3059 return;
3060 }
3061
3062 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3063 if (IS_ERR(dev->m2m_dev)) {
3064 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3065 goto rel_ctx;
3066 }
3067
3068 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3069 if (ret) {
3070 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3071 goto rel_m2m;
3072 }
3073 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3074 dev->vfd.num);
3075
3076 return;
3077
3078rel_m2m:
3079 v4l2_m2m_release(dev->m2m_dev);
3080rel_ctx:
3081 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3082}
3083
3084static int coda_firmware_request(struct coda_dev *dev)
3085{
3086 char *fw = dev->devtype->firmware;
3087
3088 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3089 coda_product_name(dev->devtype->product));
3090
3091 return request_firmware_nowait(THIS_MODULE, true,
3092 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3093}
3094
3095enum coda_platform {
3096 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003097 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03003098};
3099
Emil Goodec06d8752012-08-14 17:44:42 -03003100static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003101 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003102 .firmware = "v4l-codadx6-imx27.bin",
3103 .product = CODA_DX6,
3104 .codecs = codadx6_codecs,
3105 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003106 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003107 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003108 .firmware = "v4l-coda7541-imx53.bin",
3109 .product = CODA_7541,
3110 .codecs = coda7_codecs,
3111 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003112 },
Javier Martin186b2502012-07-26 05:53:35 -03003113};
3114
3115static struct platform_device_id coda_platform_ids[] = {
3116 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003117 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003118 { /* sentinel */ }
3119};
3120MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3121
3122#ifdef CONFIG_OF
3123static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003124 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003125 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03003126 { /* sentinel */ }
3127};
3128MODULE_DEVICE_TABLE(of, coda_dt_ids);
3129#endif
3130
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003131static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003132{
3133 const struct of_device_id *of_id =
3134 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3135 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003136 struct coda_platform_data *pdata = pdev->dev.platform_data;
3137 struct device_node *np = pdev->dev.of_node;
3138 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003139 struct coda_dev *dev;
3140 struct resource *res;
3141 int ret, irq;
3142
3143 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3144 if (!dev) {
3145 dev_err(&pdev->dev, "Not enough memory for %s\n",
3146 CODA_NAME);
3147 return -ENOMEM;
3148 }
3149
3150 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003151 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003152 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03003153
3154 dev->plat_dev = pdev;
3155 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3156 if (IS_ERR(dev->clk_per)) {
3157 dev_err(&pdev->dev, "Could not get per clock\n");
3158 return PTR_ERR(dev->clk_per);
3159 }
3160
3161 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3162 if (IS_ERR(dev->clk_ahb)) {
3163 dev_err(&pdev->dev, "Could not get ahb clock\n");
3164 return PTR_ERR(dev->clk_ahb);
3165 }
3166
3167 /* Get memory for physical registers */
3168 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003169 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3170 if (IS_ERR(dev->regs_base))
3171 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003172
3173 /* IRQ */
3174 irq = platform_get_irq(pdev, 0);
3175 if (irq < 0) {
3176 dev_err(&pdev->dev, "failed to get irq resource\n");
3177 return -ENOENT;
3178 }
3179
Philipp Zabel918c66f2013-06-27 06:59:01 -03003180 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
3181 IRQF_ONESHOT, CODA_NAME, dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003182 dev_err(&pdev->dev, "failed to request irq\n");
3183 return -ENOENT;
3184 }
3185
Philipp Zabel657eee72013-04-29 16:17:14 -07003186 /* Get IRAM pool from device tree or platform data */
3187 pool = of_get_named_gen_pool(np, "iram", 0);
3188 if (!pool && pdata)
3189 pool = dev_get_gen_pool(pdata->iram_dev);
3190 if (!pool) {
3191 dev_err(&pdev->dev, "iram pool not available\n");
3192 return -ENOMEM;
3193 }
3194 dev->iram_pool = pool;
3195
Javier Martin186b2502012-07-26 05:53:35 -03003196 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3197 if (ret)
3198 return ret;
3199
3200 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003201 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003202
3203 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3204
3205 if (of_id) {
3206 dev->devtype = of_id->data;
3207 } else if (pdev_id) {
3208 dev->devtype = &coda_devdata[pdev_id->driver_data];
3209 } else {
3210 v4l2_device_unregister(&dev->v4l2_dev);
3211 return -EINVAL;
3212 }
3213
3214 /* allocate auxiliary per-device buffers for the BIT processor */
3215 switch (dev->devtype->product) {
3216 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003217 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3218 CODADX6_WORK_BUF_SIZE);
3219 if (ret < 0) {
3220 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3221 v4l2_device_unregister(&dev->v4l2_dev);
3222 return ret;
3223 }
Javier Martin186b2502012-07-26 05:53:35 -03003224 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003225 case CODA_7541:
3226 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3227 break;
Javier Martin186b2502012-07-26 05:53:35 -03003228 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003229 if (dev->tempbuf.size) {
3230 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3231 dev->tempbuf.size);
3232 if (ret < 0) {
3233 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3234 v4l2_device_unregister(&dev->v4l2_dev);
3235 return ret;
3236 }
Javier Martin186b2502012-07-26 05:53:35 -03003237 }
3238
Philipp Zabel918c66f2013-06-27 06:59:01 -03003239 switch (dev->devtype->product) {
3240 case CODA_DX6:
Philipp Zabel657eee72013-04-29 16:17:14 -07003241 dev->iram_size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003242 break;
3243 case CODA_7541:
Philipp Zabel657eee72013-04-29 16:17:14 -07003244 dev->iram_size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003245 break;
3246 }
Philipp Zabel657eee72013-04-29 16:17:14 -07003247 dev->iram_vaddr = gen_pool_alloc(dev->iram_pool, dev->iram_size);
3248 if (!dev->iram_vaddr) {
3249 dev_err(&pdev->dev, "unable to alloc iram\n");
3250 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003251 }
Philipp Zabel657eee72013-04-29 16:17:14 -07003252 dev->iram_paddr = gen_pool_virt_to_phys(dev->iram_pool,
3253 dev->iram_vaddr);
Philipp Zabel10436672012-07-02 09:03:55 -03003254
Javier Martin186b2502012-07-26 05:53:35 -03003255 platform_set_drvdata(pdev, dev);
3256
3257 return coda_firmware_request(dev);
3258}
3259
3260static int coda_remove(struct platform_device *pdev)
3261{
3262 struct coda_dev *dev = platform_get_drvdata(pdev);
3263
3264 video_unregister_device(&dev->vfd);
3265 if (dev->m2m_dev)
3266 v4l2_m2m_release(dev->m2m_dev);
3267 if (dev->alloc_ctx)
3268 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3269 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07003270 if (dev->iram_vaddr)
3271 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003272 coda_free_aux_buf(dev, &dev->codebuf);
3273 coda_free_aux_buf(dev, &dev->tempbuf);
3274 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003275 return 0;
3276}
3277
3278static struct platform_driver coda_driver = {
3279 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003280 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003281 .driver = {
3282 .name = CODA_NAME,
3283 .owner = THIS_MODULE,
3284 .of_match_table = of_match_ptr(coda_dt_ids),
3285 },
3286 .id_table = coda_platform_ids,
3287};
3288
3289module_platform_driver(coda_driver);
3290
3291MODULE_LICENSE("GPL");
3292MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3293MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");