blob: d7778015575c2e089ccf8eb6445e356958750a28 [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 CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030058#define FMO_SLICE_SAVE_BUF_SIZE (32)
59#define CODA_DEFAULT_GAMMA 4096
60
61#define MIN_W 176
62#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030063
64#define S_ALIGN 1 /* multiple of 2 */
65#define W_ALIGN 1 /* multiple of 2 */
66#define H_ALIGN 1 /* multiple of 2 */
67
68#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
69
70static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030071module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030072MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
73
74enum {
75 V4L2_M2M_SRC = 0,
76 V4L2_M2M_DST = 1,
77};
78
Javier Martin186b2502012-07-26 05:53:35 -030079enum coda_inst_type {
80 CODA_INST_ENCODER,
81 CODA_INST_DECODER,
82};
83
84enum coda_product {
85 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030086 CODA_7541 = 0xf012,
Javier Martin186b2502012-07-26 05:53:35 -030087};
88
89struct coda_fmt {
90 char *name;
91 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030092};
93
94struct coda_codec {
95 u32 mode;
96 u32 src_fourcc;
97 u32 dst_fourcc;
98 u32 max_w;
99 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300100};
101
102struct coda_devtype {
103 char *firmware;
104 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300105 struct coda_codec *codecs;
106 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300107 size_t workbuf_size;
108};
109
110/* Per-queue, driver-specific private data */
111struct coda_q_data {
112 unsigned int width;
113 unsigned int height;
114 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300115 unsigned int fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300116};
117
118struct coda_aux_buf {
119 void *vaddr;
120 dma_addr_t paddr;
121 u32 size;
122};
123
124struct coda_dev {
125 struct v4l2_device v4l2_dev;
126 struct video_device vfd;
127 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300128 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300129
130 void __iomem *regs_base;
131 struct clk *clk_per;
132 struct clk *clk_ahb;
133
134 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300135 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300136 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700137 struct gen_pool *iram_pool;
138 long unsigned int iram_vaddr;
Philipp Zabel10436672012-07-02 09:03:55 -0300139 long unsigned int iram_paddr;
Philipp Zabel657eee72013-04-29 16:17:14 -0700140 unsigned long iram_size;
Javier Martin186b2502012-07-26 05:53:35 -0300141
142 spinlock_t irqlock;
143 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300144 struct mutex coda_mutex;
Javier Martin186b2502012-07-26 05:53:35 -0300145 struct v4l2_m2m_dev *m2m_dev;
146 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300147 struct list_head instances;
148 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300149 struct delayed_work timeout;
Javier Martin186b2502012-07-26 05:53:35 -0300150};
151
152struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300153 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300154 u8 h264_intra_qp;
155 u8 h264_inter_qp;
156 u8 mpeg4_intra_qp;
157 u8 mpeg4_inter_qp;
158 u8 gop_size;
159 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300160 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300161 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
162 u32 framerate;
163 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300164 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300165 u32 slice_max_mb;
166};
167
Philipp Zabelc2d22512013-06-21 03:55:28 -0300168struct coda_iram_info {
169 u32 axi_sram_use;
170 phys_addr_t buf_bit_use;
171 phys_addr_t buf_ip_ac_dc_use;
172 phys_addr_t buf_dbk_y_use;
173 phys_addr_t buf_dbk_c_use;
174 phys_addr_t buf_ovl_use;
175 phys_addr_t buf_btp_use;
176 phys_addr_t search_ram_paddr;
177 int search_ram_size;
178};
179
Javier Martin186b2502012-07-26 05:53:35 -0300180struct coda_ctx {
181 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300182 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300183 struct list_head list;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300184 struct work_struct skip_run;
Javier Martin186b2502012-07-26 05:53:35 -0300185 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300186 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300187 int streamon_out;
188 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300189 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300190 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300191 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300192 struct coda_q_data q_data[2];
193 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300194 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300195 enum v4l2_colorspace colorspace;
196 struct coda_params params;
197 struct v4l2_m2m_ctx *m2m_ctx;
198 struct v4l2_ctrl_handler ctrls;
199 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300200 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300201 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300202 char vpu_header[3][64];
203 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300204 struct kfifo bitstream_fifo;
205 struct mutex bitstream_mutex;
206 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300207 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300208 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300209 struct coda_aux_buf psbuf;
210 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300211 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300212 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300213 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300214 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300215 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300216 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300217 struct coda_iram_info iram_info;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300218 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300219 u32 frm_dis_flg;
220 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300221};
222
Javier Martin832fbb52012-10-29 08:34:59 -0300223static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
224 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
225static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300226
Javier Martin186b2502012-07-26 05:53:35 -0300227static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
228{
229 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
230 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
231 writel(data, dev->regs_base + reg);
232}
233
234static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
235{
236 u32 data;
237 data = readl(dev->regs_base + reg);
238 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
239 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
240 return data;
241}
242
243static inline unsigned long coda_isbusy(struct coda_dev *dev)
244{
245 return coda_read(dev, CODA_REG_BIT_BUSY);
246}
247
248static inline int coda_is_initialized(struct coda_dev *dev)
249{
250 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
251}
252
253static int coda_wait_timeout(struct coda_dev *dev)
254{
255 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
256
257 while (coda_isbusy(dev)) {
258 if (time_after(jiffies, timeout))
259 return -ETIMEDOUT;
260 }
261 return 0;
262}
263
264static void coda_command_async(struct coda_ctx *ctx, int cmd)
265{
266 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300267
268 if (dev->devtype->product == CODA_7541) {
269 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300270 coda_write(dev, ctx->bit_stream_param,
271 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300272 coda_write(dev, ctx->frm_dis_flg,
273 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300274 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
275 }
276
Javier Martin186b2502012-07-26 05:53:35 -0300277 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
278
279 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
280 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300281 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
282
Javier Martin186b2502012-07-26 05:53:35 -0300283 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
284}
285
286static int coda_command_sync(struct coda_ctx *ctx, int cmd)
287{
288 struct coda_dev *dev = ctx->dev;
289
290 coda_command_async(ctx, cmd);
291 return coda_wait_timeout(dev);
292}
293
294static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
295 enum v4l2_buf_type type)
296{
297 switch (type) {
298 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
299 return &(ctx->q_data[V4L2_M2M_SRC]);
300 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
301 return &(ctx->q_data[V4L2_M2M_DST]);
302 default:
303 BUG();
304 }
305 return NULL;
306}
307
308/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300309 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300310 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300311static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300312 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300313 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300314 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300315 },
316 {
317 .name = "YUV 4:2:0 Planar, YCrCb",
318 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300319 },
320 {
321 .name = "H264 Encoded Stream",
322 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300323 },
324 {
325 .name = "MPEG4 Encoded Stream",
326 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300327 },
328};
329
Philipp Zabelb96904e2013-05-23 10:42:58 -0300330#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
331 { mode, src_fourcc, dst_fourcc, max_w, max_h }
332
333/*
334 * Arrays of codecs supported by each given version of Coda:
335 * i.MX27 -> codadx6
336 * i.MX5x -> coda7
337 * i.MX6 -> coda960
338 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
339 */
340static struct coda_codec codadx6_codecs[] = {
341 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
342 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300343};
344
Philipp Zabelb96904e2013-05-23 10:42:58 -0300345static struct coda_codec coda7_codecs[] = {
346 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
347 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300348 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
349 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300350};
351
352static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300353{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300354 switch (fourcc) {
355 case V4L2_PIX_FMT_YUV420:
356 case V4L2_PIX_FMT_YVU420:
357 return true;
358 default:
359 return false;
360 }
361}
Javier Martin186b2502012-07-26 05:53:35 -0300362
Philipp Zabelb96904e2013-05-23 10:42:58 -0300363/*
364 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
365 * tables.
366 */
367static u32 coda_format_normalize_yuv(u32 fourcc)
368{
369 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
370}
371
372static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
373 int dst_fourcc)
374{
375 struct coda_codec *codecs = dev->devtype->codecs;
376 int num_codecs = dev->devtype->num_codecs;
377 int k;
378
379 src_fourcc = coda_format_normalize_yuv(src_fourcc);
380 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
381 if (src_fourcc == dst_fourcc)
382 return NULL;
383
384 for (k = 0; k < num_codecs; k++) {
385 if (codecs[k].src_fourcc == src_fourcc &&
386 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300387 break;
388 }
389
Philipp Zabelb96904e2013-05-23 10:42:58 -0300390 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300391 return NULL;
392
Philipp Zabelb96904e2013-05-23 10:42:58 -0300393 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300394}
395
Philipp Zabel57625592013-09-30 10:34:51 -0300396static void coda_get_max_dimensions(struct coda_dev *dev,
397 struct coda_codec *codec,
398 int *max_w, int *max_h)
399{
400 struct coda_codec *codecs = dev->devtype->codecs;
401 int num_codecs = dev->devtype->num_codecs;
402 unsigned int w, h;
403 int k;
404
405 if (codec) {
406 w = codec->max_w;
407 h = codec->max_h;
408 } else {
409 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
410 w = max(w, codecs[k].max_w);
411 h = max(h, codecs[k].max_h);
412 }
413 }
414
415 if (max_w)
416 *max_w = w;
417 if (max_h)
418 *max_h = h;
419}
420
Philipp Zabel927933f2013-09-30 10:34:48 -0300421static char *coda_product_name(int product)
422{
423 static char buf[9];
424
425 switch (product) {
426 case CODA_DX6:
427 return "CodaDx6";
428 case CODA_7541:
429 return "CODA7541";
430 default:
431 snprintf(buf, sizeof(buf), "(0x%04x)", product);
432 return buf;
433 }
434}
435
Javier Martin186b2502012-07-26 05:53:35 -0300436/*
437 * V4L2 ioctl() operations.
438 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300439static int coda_querycap(struct file *file, void *priv,
440 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300441{
Philipp Zabel927933f2013-09-30 10:34:48 -0300442 struct coda_ctx *ctx = fh_to_ctx(priv);
443
Javier Martin186b2502012-07-26 05:53:35 -0300444 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300445 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
446 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300447 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300448 /*
449 * This is only a mem-to-mem video device. The capture and output
450 * device capability flags are left only for backward compatibility
451 * and are scheduled for removal.
452 */
453 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
454 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300455 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
456
457 return 0;
458}
459
460static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300461 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300462{
463 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300464 struct coda_codec *codecs = ctx->dev->devtype->codecs;
465 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300466 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300467 int num_codecs = ctx->dev->devtype->num_codecs;
468 int num_formats = ARRAY_SIZE(coda_formats);
469 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300470
471 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300472 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300473 if (coda_format_is_yuv(formats[i].fourcc) &&
474 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300475 if (num == f->index)
476 break;
477 ++num;
478 continue;
479 }
480 /* Compressed formats may be supported, check the codec list */
481 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300482 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300483 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300484 formats[i].fourcc == codecs[k].dst_fourcc &&
485 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300486 break;
487 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
488 formats[i].fourcc == codecs[k].src_fourcc)
489 break;
490 }
491 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300492 if (num == f->index)
493 break;
494 ++num;
495 }
496 }
497
498 if (i < num_formats) {
499 fmt = &formats[i];
500 strlcpy(f->description, fmt->name, sizeof(f->description));
501 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300502 if (!coda_format_is_yuv(fmt->fourcc))
503 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300504 return 0;
505 }
506
507 /* Format not found */
508 return -EINVAL;
509}
510
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300511static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
512 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300513{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300514 struct coda_ctx *ctx = fh_to_ctx(priv);
515 struct vb2_queue *src_vq;
516 struct coda_q_data *q_data_src;
517
518 /* If the source format is already fixed, only list matching formats */
519 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
520 if (vb2_is_streaming(src_vq)) {
521 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
522
523 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
524 q_data_src->fourcc);
525 }
526
527 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300528}
529
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300530static int coda_enum_fmt_vid_out(struct file *file, void *priv,
531 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300532{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300533 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300534}
535
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300536static int coda_g_fmt(struct file *file, void *priv,
537 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300538{
539 struct vb2_queue *vq;
540 struct coda_q_data *q_data;
541 struct coda_ctx *ctx = fh_to_ctx(priv);
542
543 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
544 if (!vq)
545 return -EINVAL;
546
547 q_data = get_q_data(ctx, f->type);
548
549 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300550 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300551 f->fmt.pix.width = q_data->width;
552 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300553 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300554 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
555 else /* encoded formats h.264/mpeg4 */
556 f->fmt.pix.bytesperline = 0;
557
558 f->fmt.pix.sizeimage = q_data->sizeimage;
559 f->fmt.pix.colorspace = ctx->colorspace;
560
561 return 0;
562}
563
Philipp Zabel57625592013-09-30 10:34:51 -0300564static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
565 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300566{
Philipp Zabel57625592013-09-30 10:34:51 -0300567 struct coda_dev *dev = ctx->dev;
568 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300569 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300570 enum v4l2_field field;
571
572 field = f->fmt.pix.field;
573 if (field == V4L2_FIELD_ANY)
574 field = V4L2_FIELD_NONE;
575 else if (V4L2_FIELD_NONE != field)
576 return -EINVAL;
577
578 /* V4L2 specification suggests the driver corrects the format struct
579 * if any of the dimensions is unsupported */
580 f->fmt.pix.field = field;
581
Philipp Zabel57625592013-09-30 10:34:51 -0300582 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
583 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
584 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
585 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300586
Philipp Zabel57625592013-09-30 10:34:51 -0300587 switch (f->fmt.pix.pixelformat) {
588 case V4L2_PIX_FMT_YUV420:
589 case V4L2_PIX_FMT_YVU420:
590 case V4L2_PIX_FMT_H264:
591 case V4L2_PIX_FMT_MPEG4:
592 case V4L2_PIX_FMT_JPEG:
593 break;
594 default:
595 q_data = get_q_data(ctx, f->type);
596 f->fmt.pix.pixelformat = q_data->fourcc;
597 }
598
599 switch (f->fmt.pix.pixelformat) {
600 case V4L2_PIX_FMT_YUV420:
601 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300602 /* Frame stride must be multiple of 8 */
603 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
604 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300605 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300606 break;
607 case V4L2_PIX_FMT_H264:
608 case V4L2_PIX_FMT_MPEG4:
609 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300610 f->fmt.pix.bytesperline = 0;
611 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300612 break;
613 default:
614 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300615 }
616
617 return 0;
618}
619
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300620static int coda_try_fmt_vid_cap(struct file *file, void *priv,
621 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300622{
Javier Martin186b2502012-07-26 05:53:35 -0300623 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300624 struct coda_codec *codec;
625 struct vb2_queue *src_vq;
626 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300627
Philipp Zabel918c66f2013-06-27 06:59:01 -0300628 /*
629 * If the source format is already fixed, try to find a codec that
630 * converts to the given destination format
631 */
632 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
633 if (vb2_is_streaming(src_vq)) {
634 struct coda_q_data *q_data_src;
635
636 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
637 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
638 f->fmt.pix.pixelformat);
639 if (!codec)
640 return -EINVAL;
641 } else {
642 /* Otherwise determine codec by encoded format, if possible */
643 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
644 f->fmt.pix.pixelformat);
645 }
Javier Martin186b2502012-07-26 05:53:35 -0300646
647 f->fmt.pix.colorspace = ctx->colorspace;
648
Philipp Zabel57625592013-09-30 10:34:51 -0300649 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300650 if (ret < 0)
651 return ret;
652
653 /* The h.264 decoder only returns complete 16x16 macroblocks */
654 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
655 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
656 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
657 f->fmt.pix.bytesperline = f->fmt.pix.width;
658 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
659 f->fmt.pix.height * 3 / 2;
660 }
661
662 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300663}
664
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300665static int coda_try_fmt_vid_out(struct file *file, void *priv,
666 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300667{
668 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300669 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300670
Philipp Zabelb96904e2013-05-23 10:42:58 -0300671 /* Determine codec by encoded format, returns NULL if raw or invalid */
672 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
673 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300674
675 if (!f->fmt.pix.colorspace)
676 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
677
Philipp Zabel57625592013-09-30 10:34:51 -0300678 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300679}
680
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300681static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300682{
683 struct coda_q_data *q_data;
684 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300685
686 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
687 if (!vq)
688 return -EINVAL;
689
690 q_data = get_q_data(ctx, f->type);
691 if (!q_data)
692 return -EINVAL;
693
694 if (vb2_is_busy(vq)) {
695 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
696 return -EBUSY;
697 }
698
Philipp Zabelb96904e2013-05-23 10:42:58 -0300699 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300700 q_data->width = f->fmt.pix.width;
701 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300702 q_data->sizeimage = f->fmt.pix.sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300703
704 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
705 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300706 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300707
708 return 0;
709}
710
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300711static int coda_s_fmt_vid_cap(struct file *file, void *priv,
712 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300713{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300714 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300715 int ret;
716
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300717 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300718 if (ret)
719 return ret;
720
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300721 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300722}
723
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300724static int coda_s_fmt_vid_out(struct file *file, void *priv,
725 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300726{
727 struct coda_ctx *ctx = fh_to_ctx(priv);
728 int ret;
729
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300730 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300731 if (ret)
732 return ret;
733
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300734 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300735 if (ret)
736 ctx->colorspace = f->fmt.pix.colorspace;
737
738 return ret;
739}
740
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300741static int coda_reqbufs(struct file *file, void *priv,
742 struct v4l2_requestbuffers *reqbufs)
Javier Martin186b2502012-07-26 05:53:35 -0300743{
744 struct coda_ctx *ctx = fh_to_ctx(priv);
745
746 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
747}
748
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300749static int coda_querybuf(struct file *file, void *priv,
750 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300751{
752 struct coda_ctx *ctx = fh_to_ctx(priv);
753
754 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
755}
756
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300757static int coda_qbuf(struct file *file, void *priv,
758 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300759{
760 struct coda_ctx *ctx = fh_to_ctx(priv);
761
762 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
763}
764
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300765static int coda_expbuf(struct file *file, void *priv,
766 struct v4l2_exportbuffer *eb)
Philipp Zabel419869c2013-05-23 07:06:30 -0300767{
768 struct coda_ctx *ctx = fh_to_ctx(priv);
769
770 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
771}
772
Philipp Zabel918c66f2013-06-27 06:59:01 -0300773static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
774 struct v4l2_buffer *buf)
775{
776 struct vb2_queue *src_vq;
777
778 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
779
780 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
781 (buf->sequence == (ctx->qsequence - 1)));
782}
783
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300784static int coda_dqbuf(struct file *file, void *priv,
785 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300786{
787 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300788 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300789
Philipp Zabel918c66f2013-06-27 06:59:01 -0300790 ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
791
792 /* If this is the last capture buffer, emit an end-of-stream event */
793 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
794 coda_buf_is_end_of_stream(ctx, buf)) {
795 const struct v4l2_event eos_event = {
796 .type = V4L2_EVENT_EOS
797 };
798
799 v4l2_event_queue_fh(&ctx->fh, &eos_event);
800 }
801
802 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300803}
804
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300805static int coda_create_bufs(struct file *file, void *priv,
806 struct v4l2_create_buffers *create)
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300807{
808 struct coda_ctx *ctx = fh_to_ctx(priv);
809
810 return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
811}
812
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300813static int coda_streamon(struct file *file, void *priv,
814 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300815{
816 struct coda_ctx *ctx = fh_to_ctx(priv);
817
818 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
819}
820
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300821static int coda_streamoff(struct file *file, void *priv,
822 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300823{
824 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300825 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300826
Philipp Zabel918c66f2013-06-27 06:59:01 -0300827 /*
828 * This indirectly calls __vb2_queue_cancel, which dequeues all buffers.
829 * We therefore have to lock it against running hardware in this context,
830 * which still needs the buffers.
831 */
832 mutex_lock(&ctx->buffer_mutex);
833 ret = v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
834 mutex_unlock(&ctx->buffer_mutex);
835
836 return ret;
837}
838
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300839static int coda_try_decoder_cmd(struct file *file, void *fh,
840 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300841{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300842 if (dc->cmd != V4L2_DEC_CMD_STOP)
843 return -EINVAL;
844
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300845 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300846 return -EINVAL;
847
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300848 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300849 return -EINVAL;
850
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300851 return 0;
852}
853
854static int coda_decoder_cmd(struct file *file, void *fh,
855 struct v4l2_decoder_cmd *dc)
856{
857 struct coda_ctx *ctx = fh_to_ctx(fh);
858 int ret;
859
860 ret = coda_try_decoder_cmd(file, fh, dc);
861 if (ret < 0)
862 return ret;
863
864 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300865 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300866 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300867
868 /* Set the strem-end flag on this context */
869 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
870
871 return 0;
872}
873
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300874static int coda_subscribe_event(struct v4l2_fh *fh,
875 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300876{
877 switch (sub->type) {
878 case V4L2_EVENT_EOS:
879 return v4l2_event_subscribe(fh, sub, 0, NULL);
880 default:
881 return v4l2_ctrl_subscribe_event(fh, sub);
882 }
Javier Martin186b2502012-07-26 05:53:35 -0300883}
884
885static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300886 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300887
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300888 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
889 .vidioc_g_fmt_vid_cap = coda_g_fmt,
890 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
891 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300892
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300893 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
894 .vidioc_g_fmt_vid_out = coda_g_fmt,
895 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
896 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300897
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300898 .vidioc_reqbufs = coda_reqbufs,
899 .vidioc_querybuf = coda_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300900
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300901 .vidioc_qbuf = coda_qbuf,
902 .vidioc_expbuf = coda_expbuf,
903 .vidioc_dqbuf = coda_dqbuf,
904 .vidioc_create_bufs = coda_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300905
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300906 .vidioc_streamon = coda_streamon,
907 .vidioc_streamoff = coda_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300908
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300909 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300910 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300911
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300912 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300913 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300914};
915
Philipp Zabel918c66f2013-06-27 06:59:01 -0300916static int coda_start_decoding(struct coda_ctx *ctx);
917
918static void coda_skip_run(struct work_struct *work)
919{
920 struct coda_ctx *ctx = container_of(work, struct coda_ctx, skip_run);
921
922 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
923}
924
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300925static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
926{
927 return kfifo_len(&ctx->bitstream_fifo);
928}
929
930static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
931{
932 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
933 struct coda_dev *dev = ctx->dev;
934 u32 rd_ptr;
935
936 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
937 kfifo->out = (kfifo->in & ~kfifo->mask) |
938 (rd_ptr - ctx->bitstream.paddr);
939 if (kfifo->out > kfifo->in)
940 kfifo->out -= kfifo->mask + 1;
941}
942
943static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
944{
945 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
946 struct coda_dev *dev = ctx->dev;
947 u32 rd_ptr, wr_ptr;
948
949 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
950 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
951 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
952 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
953}
954
955static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
956{
957 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
958 struct coda_dev *dev = ctx->dev;
959 u32 wr_ptr;
960
961 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
962 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
963}
964
965static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
966{
967 u32 src_size = vb2_get_plane_payload(src_buf, 0);
968 u32 n;
969
970 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
971 if (n < src_size)
972 return -ENOSPC;
973
974 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
975 ctx->bitstream.size, DMA_TO_DEVICE);
976
977 ctx->qsequence++;
978
979 return 0;
980}
981
982static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
983 struct vb2_buffer *src_buf)
984{
985 int ret;
986
987 if (coda_get_bitstream_payload(ctx) +
988 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
989 return false;
990
991 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
992 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
993 return true;
994 }
995
996 ret = coda_bitstream_queue(ctx, src_buf);
997 if (ret < 0) {
998 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
999 return false;
1000 }
1001 /* Sync read pointer to device */
1002 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1003 coda_kfifo_sync_to_device_write(ctx);
1004
Philipp Zabel918c66f2013-06-27 06:59:01 -03001005 ctx->prescan_failed = false;
1006
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001007 return true;
1008}
1009
1010static void coda_fill_bitstream(struct coda_ctx *ctx)
1011{
1012 struct vb2_buffer *src_buf;
1013
1014 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
1015 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1016
1017 if (coda_bitstream_try_queue(ctx, src_buf)) {
1018 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1019 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1020 } else {
1021 break;
1022 }
1023 }
1024}
1025
Javier Martin186b2502012-07-26 05:53:35 -03001026/*
1027 * Mem-to-mem operations.
1028 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001029static int coda_prepare_decode(struct coda_ctx *ctx)
1030{
1031 struct vb2_buffer *dst_buf;
1032 struct coda_dev *dev = ctx->dev;
1033 struct coda_q_data *q_data_dst;
1034 u32 stridey, height;
1035 u32 picture_y, picture_cb, picture_cr;
1036
1037 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1038 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1039
1040 if (ctx->params.rot_mode & CODA_ROT_90) {
1041 stridey = q_data_dst->height;
1042 height = q_data_dst->width;
1043 } else {
1044 stridey = q_data_dst->width;
1045 height = q_data_dst->height;
1046 }
1047
1048 /* Try to copy source buffer contents into the bitstream ringbuffer */
1049 mutex_lock(&ctx->bitstream_mutex);
1050 coda_fill_bitstream(ctx);
1051 mutex_unlock(&ctx->bitstream_mutex);
1052
1053 if (coda_get_bitstream_payload(ctx) < 512 &&
1054 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1055 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1056 "bitstream payload: %d, skipping\n",
1057 coda_get_bitstream_payload(ctx));
1058 schedule_work(&ctx->skip_run);
1059 return -EAGAIN;
1060 }
1061
1062 /* Run coda_start_decoding (again) if not yet initialized */
1063 if (!ctx->initialized) {
1064 int ret = coda_start_decoding(ctx);
1065 if (ret < 0) {
1066 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1067 schedule_work(&ctx->skip_run);
1068 return -EAGAIN;
1069 } else {
1070 ctx->initialized = 1;
1071 }
1072 }
1073
1074 /* Set rotator output */
1075 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1076 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1077 /* Switch Cr and Cb for YVU420 format */
1078 picture_cr = picture_y + stridey * height;
1079 picture_cb = picture_cr + stridey / 2 * height / 2;
1080 } else {
1081 picture_cb = picture_y + stridey * height;
1082 picture_cr = picture_cb + stridey / 2 * height / 2;
1083 }
1084 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1085 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1086 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1087 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1088 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1089 CODA_CMD_DEC_PIC_ROT_MODE);
1090
1091 switch (dev->devtype->product) {
1092 case CODA_DX6:
1093 /* TBD */
1094 case CODA_7541:
1095 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1096 break;
1097 }
1098
1099 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1100
1101 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1102 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1103
1104 return 0;
1105}
1106
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001107static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001108{
Javier Martin186b2502012-07-26 05:53:35 -03001109 struct coda_q_data *q_data_src, *q_data_dst;
1110 struct vb2_buffer *src_buf, *dst_buf;
1111 struct coda_dev *dev = ctx->dev;
1112 int force_ipicture;
1113 int quant_param = 0;
1114 u32 picture_y, picture_cb, picture_cr;
1115 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1116 u32 dst_fourcc;
1117
1118 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1119 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1120 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1121 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001122 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001123
Philipp Zabel918c66f2013-06-27 06:59:01 -03001124 src_buf->v4l2_buf.sequence = ctx->osequence;
1125 dst_buf->v4l2_buf.sequence = ctx->osequence;
1126 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001127
1128 /*
1129 * Workaround coda firmware BUG that only marks the first
1130 * frame as IDR. This is a problem for some decoders that can't
1131 * recover when a frame is lost.
1132 */
1133 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1134 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1135 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1136 } else {
1137 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1138 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1139 }
1140
1141 /*
1142 * Copy headers at the beginning of the first frame for H.264 only.
1143 * In MPEG4 they are already copied by the coda.
1144 */
1145 if (src_buf->v4l2_buf.sequence == 0) {
1146 pic_stream_buffer_addr =
1147 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1148 ctx->vpu_header_size[0] +
1149 ctx->vpu_header_size[1] +
1150 ctx->vpu_header_size[2];
1151 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1152 ctx->vpu_header_size[0] -
1153 ctx->vpu_header_size[1] -
1154 ctx->vpu_header_size[2];
1155 memcpy(vb2_plane_vaddr(dst_buf, 0),
1156 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1157 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1158 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1159 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1160 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1161 ctx->vpu_header_size[2]);
1162 } else {
1163 pic_stream_buffer_addr =
1164 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1165 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1166 }
1167
1168 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1169 force_ipicture = 1;
1170 switch (dst_fourcc) {
1171 case V4L2_PIX_FMT_H264:
1172 quant_param = ctx->params.h264_intra_qp;
1173 break;
1174 case V4L2_PIX_FMT_MPEG4:
1175 quant_param = ctx->params.mpeg4_intra_qp;
1176 break;
1177 default:
1178 v4l2_warn(&ctx->dev->v4l2_dev,
1179 "cannot set intra qp, fmt not supported\n");
1180 break;
1181 }
1182 } else {
1183 force_ipicture = 0;
1184 switch (dst_fourcc) {
1185 case V4L2_PIX_FMT_H264:
1186 quant_param = ctx->params.h264_inter_qp;
1187 break;
1188 case V4L2_PIX_FMT_MPEG4:
1189 quant_param = ctx->params.mpeg4_inter_qp;
1190 break;
1191 default:
1192 v4l2_warn(&ctx->dev->v4l2_dev,
1193 "cannot set inter qp, fmt not supported\n");
1194 break;
1195 }
1196 }
1197
1198 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001199 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001200 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1201
1202
1203 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001204 switch (q_data_src->fourcc) {
1205 case V4L2_PIX_FMT_YVU420:
1206 /* Switch Cb and Cr for YVU420 format */
1207 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1208 picture_cb = picture_cr + q_data_src->width / 2 *
1209 q_data_src->height / 2;
1210 break;
1211 case V4L2_PIX_FMT_YUV420:
1212 default:
1213 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1214 picture_cr = picture_cb + q_data_src->width / 2 *
1215 q_data_src->height / 2;
1216 break;
1217 }
Javier Martin186b2502012-07-26 05:53:35 -03001218
1219 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1220 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1221 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1222 coda_write(dev, force_ipicture << 1 & 0x2,
1223 CODA_CMD_ENC_PIC_OPTION);
1224
1225 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1226 coda_write(dev, pic_stream_buffer_size / 1024,
1227 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001228}
1229
1230static void coda_device_run(void *m2m_priv)
1231{
1232 struct coda_ctx *ctx = m2m_priv;
1233 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001234 int ret;
1235
1236 mutex_lock(&ctx->buffer_mutex);
1237
1238 /*
1239 * If streamoff dequeued all buffers before we could get the lock,
1240 * just bail out immediately.
1241 */
1242 if ((!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1243 ctx->inst_type != CODA_INST_DECODER) ||
1244 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1245 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1246 "%d: device_run without buffers\n", ctx->idx);
1247 mutex_unlock(&ctx->buffer_mutex);
1248 schedule_work(&ctx->skip_run);
1249 return;
1250 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001251
1252 mutex_lock(&dev->coda_mutex);
1253
Philipp Zabel918c66f2013-06-27 06:59:01 -03001254 if (ctx->inst_type == CODA_INST_DECODER) {
1255 ret = coda_prepare_decode(ctx);
1256 if (ret < 0) {
1257 mutex_unlock(&dev->coda_mutex);
1258 mutex_unlock(&ctx->buffer_mutex);
1259 /* job_finish scheduled by prepare_decode */
1260 return;
1261 }
1262 } else {
1263 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001264 }
1265
Philipp Zabelc2d22512013-06-21 03:55:28 -03001266 if (dev->devtype->product != CODA_DX6)
1267 coda_write(dev, ctx->iram_info.axi_sram_use,
1268 CODA7_REG_BIT_AXI_SRAM_USE);
1269
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001270 /* 1 second timeout in case CODA locks up */
1271 schedule_delayed_work(&dev->timeout, HZ);
1272
Philipp Zabel918c66f2013-06-27 06:59:01 -03001273 if (ctx->inst_type == CODA_INST_DECODER)
1274 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001275 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1276}
1277
1278static int coda_job_ready(void *m2m_priv)
1279{
1280 struct coda_ctx *ctx = m2m_priv;
1281
1282 /*
1283 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001284 * and 1 frame are needed. In the decoder case,
1285 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001286 */
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001287 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1288 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001289 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1290 "not ready: not enough video buffers.\n");
1291 return 0;
1292 }
1293
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001294 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1295 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1296 "not ready: not enough video capture buffers.\n");
1297 return 0;
1298 }
1299
Philipp Zabel918c66f2013-06-27 06:59:01 -03001300 if (ctx->prescan_failed ||
1301 ((ctx->inst_type == CODA_INST_DECODER) &&
1302 (coda_get_bitstream_payload(ctx) < 512) &&
1303 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1304 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1305 "%d: not ready: not enough bitstream data.\n",
1306 ctx->idx);
1307 return 0;
1308 }
1309
Philipp Zabel3e748262013-05-23 10:43:01 -03001310 if (ctx->aborting) {
1311 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1312 "not ready: aborting\n");
1313 return 0;
1314 }
1315
Javier Martin186b2502012-07-26 05:53:35 -03001316 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1317 "job ready\n");
1318 return 1;
1319}
1320
1321static void coda_job_abort(void *priv)
1322{
1323 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001324
1325 ctx->aborting = 1;
1326
1327 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1328 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001329}
1330
1331static void coda_lock(void *m2m_priv)
1332{
1333 struct coda_ctx *ctx = m2m_priv;
1334 struct coda_dev *pcdev = ctx->dev;
1335 mutex_lock(&pcdev->dev_mutex);
1336}
1337
1338static void coda_unlock(void *m2m_priv)
1339{
1340 struct coda_ctx *ctx = m2m_priv;
1341 struct coda_dev *pcdev = ctx->dev;
1342 mutex_unlock(&pcdev->dev_mutex);
1343}
1344
1345static struct v4l2_m2m_ops coda_m2m_ops = {
1346 .device_run = coda_device_run,
1347 .job_ready = coda_job_ready,
1348 .job_abort = coda_job_abort,
1349 .lock = coda_lock,
1350 .unlock = coda_unlock,
1351};
1352
1353static void set_default_params(struct coda_ctx *ctx)
1354{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001355 int max_w;
1356 int max_h;
1357
1358 ctx->codec = &ctx->dev->devtype->codecs[0];
1359 max_w = ctx->codec->max_w;
1360 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001361
1362 ctx->params.codec_mode = CODA_MODE_INVALID;
1363 ctx->colorspace = V4L2_COLORSPACE_REC709;
1364 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001365 ctx->aborting = 0;
1366
1367 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001368 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1369 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1370 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1371 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1372 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1373 ctx->q_data[V4L2_M2M_DST].width = max_w;
1374 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001375 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1376}
1377
1378/*
1379 * Queue operations
1380 */
1381static int coda_queue_setup(struct vb2_queue *vq,
1382 const struct v4l2_format *fmt,
1383 unsigned int *nbuffers, unsigned int *nplanes,
1384 unsigned int sizes[], void *alloc_ctxs[])
1385{
1386 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001387 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001388 unsigned int size;
1389
Philipp Zabele34db062012-08-29 08:22:00 -03001390 q_data = get_q_data(ctx, vq->type);
1391 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001392
1393 *nplanes = 1;
1394 sizes[0] = size;
1395
1396 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1397
1398 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1399 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1400
1401 return 0;
1402}
1403
1404static int coda_buf_prepare(struct vb2_buffer *vb)
1405{
1406 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1407 struct coda_q_data *q_data;
1408
1409 q_data = get_q_data(ctx, vb->vb2_queue->type);
1410
1411 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1412 v4l2_warn(&ctx->dev->v4l2_dev,
1413 "%s data will not fit into plane (%lu < %lu)\n",
1414 __func__, vb2_plane_size(vb, 0),
1415 (long)q_data->sizeimage);
1416 return -EINVAL;
1417 }
1418
Javier Martin186b2502012-07-26 05:53:35 -03001419 return 0;
1420}
1421
1422static void coda_buf_queue(struct vb2_buffer *vb)
1423{
1424 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001425 struct coda_q_data *q_data;
1426
1427 q_data = get_q_data(ctx, vb->vb2_queue->type);
1428
1429 /*
1430 * In the decoder case, immediately try to copy the buffer into the
1431 * bitstream ringbuffer and mark it as ready to be dequeued.
1432 */
1433 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1434 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1435 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001436 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001437 * the stream end
1438 */
1439 if (vb2_get_plane_payload(vb, 0) == 0)
1440 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1441 mutex_lock(&ctx->bitstream_mutex);
1442 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1443 coda_fill_bitstream(ctx);
1444 mutex_unlock(&ctx->bitstream_mutex);
1445 } else {
1446 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1447 }
Javier Martin186b2502012-07-26 05:53:35 -03001448}
1449
1450static void coda_wait_prepare(struct vb2_queue *q)
1451{
1452 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1453 coda_unlock(ctx);
1454}
1455
1456static void coda_wait_finish(struct vb2_queue *q)
1457{
1458 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1459 coda_lock(ctx);
1460}
1461
Philipp Zabel86eda902013-05-23 10:42:57 -03001462static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1463{
1464 struct coda_dev *dev = ctx->dev;
1465 u32 *p = ctx->parabuf.vaddr;
1466
1467 if (dev->devtype->product == CODA_DX6)
1468 p[index] = value;
1469 else
1470 p[index ^ 1] = value;
1471}
1472
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001473static int coda_alloc_aux_buf(struct coda_dev *dev,
1474 struct coda_aux_buf *buf, size_t size)
1475{
1476 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1477 GFP_KERNEL);
1478 if (!buf->vaddr)
1479 return -ENOMEM;
1480
1481 buf->size = size;
1482
1483 return 0;
1484}
1485
1486static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1487 struct coda_aux_buf *buf, size_t size)
1488{
1489 return coda_alloc_aux_buf(ctx->dev, buf, size);
1490}
1491
1492static void coda_free_aux_buf(struct coda_dev *dev,
1493 struct coda_aux_buf *buf)
1494{
1495 if (buf->vaddr) {
1496 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1497 buf->vaddr, buf->paddr);
1498 buf->vaddr = NULL;
1499 buf->size = 0;
1500 }
1501}
1502
1503static void coda_free_framebuffers(struct coda_ctx *ctx)
1504{
1505 int i;
1506
1507 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1508 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1509}
1510
Philipp Zabelec25f682012-07-20 08:54:29 -03001511static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1512{
1513 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001514 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001515 dma_addr_t paddr;
1516 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001517 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001518 int i;
1519
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001520 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1521 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001522 ysize = round_up(q_data->width, 8) * height;
1523
Philipp Zabelec25f682012-07-20 08:54:29 -03001524 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001525 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001526 size_t size;
1527
1528 size = q_data->sizeimage;
1529 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1530 dev->devtype->product != CODA_DX6)
Philipp Zabel86eda902013-05-23 10:42:57 -03001531 ctx->internal_frames[i].size += ysize/4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001532 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1533 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001534 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001535 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001536 }
1537 }
1538
1539 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001540 for (i = 0; i < ctx->num_internal_frames; i++) {
1541 paddr = ctx->internal_frames[i].paddr;
1542 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1543 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1544 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001545
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001546 /* mvcol buffer for h.264 */
1547 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1548 dev->devtype->product != CODA_DX6)
1549 coda_parabuf_write(ctx, 96 + i,
1550 ctx->internal_frames[i].paddr +
1551 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001552 }
1553
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001554 /* mvcol buffer for mpeg4 */
1555 if ((dev->devtype->product != CODA_DX6) &&
1556 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1557 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1558 ysize + ysize/4 + ysize/4);
1559
Philipp Zabelec25f682012-07-20 08:54:29 -03001560 return 0;
1561}
1562
Javier Martin3f3f5c72012-10-29 05:20:29 -03001563static int coda_h264_padding(int size, char *p)
1564{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001565 int nal_size;
1566 int diff;
1567
Javier Martin832fbb52012-10-29 08:34:59 -03001568 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001569 if (diff == 0)
1570 return 0;
1571
Javier Martin832fbb52012-10-29 08:34:59 -03001572 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001573 memcpy(p, coda_filler_nal, nal_size);
1574
1575 /* Add rbsp stop bit and trailing at the end */
1576 *(p + nal_size - 1) = 0x80;
1577
1578 return nal_size;
1579}
1580
Philipp Zabelc2d22512013-06-21 03:55:28 -03001581static void coda_setup_iram(struct coda_ctx *ctx)
1582{
1583 struct coda_iram_info *iram_info = &ctx->iram_info;
1584 struct coda_dev *dev = ctx->dev;
1585 int ipacdc_size;
1586 int bitram_size;
1587 int dbk_size;
Philipp Zabel8358e762013-06-21 03:55:32 -03001588 int ovl_size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001589 int mb_width;
1590 int me_size;
1591 int size;
1592
1593 memset(iram_info, 0, sizeof(*iram_info));
1594 size = dev->iram_size;
1595
1596 if (dev->devtype->product == CODA_DX6)
1597 return;
1598
1599 if (ctx->inst_type == CODA_INST_ENCODER) {
1600 struct coda_q_data *q_data_src;
1601
1602 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1603 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1604
1605 /* Prioritize in case IRAM is too small for everything */
1606 me_size = round_up(round_up(q_data_src->width, 16) * 36 + 2048,
1607 1024);
1608 iram_info->search_ram_size = me_size;
1609 if (size >= iram_info->search_ram_size) {
1610 if (dev->devtype->product == CODA_7541)
1611 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE;
1612 iram_info->search_ram_paddr = dev->iram_paddr;
1613 size -= iram_info->search_ram_size;
1614 } else {
1615 pr_err("IRAM is smaller than the search ram size\n");
1616 goto out;
1617 }
1618
1619 /* Only H.264BP and H.263P3 are considered */
1620 dbk_size = round_up(128 * mb_width, 1024);
1621 if (size >= dbk_size) {
1622 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1623 iram_info->buf_dbk_y_use = dev->iram_paddr +
1624 iram_info->search_ram_size;
1625 iram_info->buf_dbk_c_use = iram_info->buf_dbk_y_use +
1626 dbk_size / 2;
1627 size -= dbk_size;
1628 } else {
1629 goto out;
1630 }
1631
1632 bitram_size = round_up(128 * mb_width, 1024);
1633 if (size >= bitram_size) {
1634 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1635 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1636 dbk_size / 2;
1637 size -= bitram_size;
1638 } else {
1639 goto out;
1640 }
1641
1642 ipacdc_size = round_up(128 * mb_width, 1024);
1643 if (size >= ipacdc_size) {
1644 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1645 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1646 bitram_size;
1647 size -= ipacdc_size;
1648 }
1649
Philipp Zabel8358e762013-06-21 03:55:32 -03001650 /* OVL and BTP disabled for encoder */
1651 } else if (ctx->inst_type == CODA_INST_DECODER) {
1652 struct coda_q_data *q_data_dst;
1653 int mb_height;
1654
1655 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1656 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
1657 mb_height = DIV_ROUND_UP(q_data_dst->height, 16);
1658
1659 dbk_size = round_up(256 * mb_width, 1024);
1660 if (size >= dbk_size) {
1661 iram_info->axi_sram_use |= CODA7_USE_HOST_DBK_ENABLE;
1662 iram_info->buf_dbk_y_use = dev->iram_paddr;
1663 iram_info->buf_dbk_c_use = dev->iram_paddr +
1664 dbk_size / 2;
1665 size -= dbk_size;
1666 } else {
1667 goto out;
1668 }
1669
1670 bitram_size = round_up(128 * mb_width, 1024);
1671 if (size >= bitram_size) {
1672 iram_info->axi_sram_use |= CODA7_USE_HOST_BIT_ENABLE;
1673 iram_info->buf_bit_use = iram_info->buf_dbk_c_use +
1674 dbk_size / 2;
1675 size -= bitram_size;
1676 } else {
1677 goto out;
1678 }
1679
1680 ipacdc_size = round_up(128 * mb_width, 1024);
1681 if (size >= ipacdc_size) {
1682 iram_info->axi_sram_use |= CODA7_USE_HOST_IP_ENABLE;
1683 iram_info->buf_ip_ac_dc_use = iram_info->buf_bit_use +
1684 bitram_size;
1685 size -= ipacdc_size;
1686 } else {
1687 goto out;
1688 }
1689
1690 ovl_size = round_up(80 * mb_width, 1024);
Philipp Zabelc2d22512013-06-21 03:55:28 -03001691 }
1692
1693out:
1694 switch (dev->devtype->product) {
1695 case CODA_DX6:
1696 break;
1697 case CODA_7541:
1698 /* i.MX53 uses secondary AXI for IRAM access */
1699 if (iram_info->axi_sram_use & CODA7_USE_HOST_BIT_ENABLE)
1700 iram_info->axi_sram_use |= CODA7_USE_BIT_ENABLE;
1701 if (iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE)
1702 iram_info->axi_sram_use |= CODA7_USE_IP_ENABLE;
1703 if (iram_info->axi_sram_use & CODA7_USE_HOST_DBK_ENABLE)
1704 iram_info->axi_sram_use |= CODA7_USE_DBK_ENABLE;
1705 if (iram_info->axi_sram_use & CODA7_USE_HOST_OVL_ENABLE)
1706 iram_info->axi_sram_use |= CODA7_USE_OVL_ENABLE;
1707 if (iram_info->axi_sram_use & CODA7_USE_HOST_ME_ENABLE)
1708 iram_info->axi_sram_use |= CODA7_USE_ME_ENABLE;
1709 }
1710
1711 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1712 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1713 "IRAM smaller than needed\n");
1714
1715 if (dev->devtype->product == CODA_7541) {
1716 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001717 if (ctx->inst_type == CODA_INST_DECODER) {
1718 /* fw 1.4.50 */
1719 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1720 CODA7_USE_IP_ENABLE);
1721 } else {
1722 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001723 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1724 CODA7_USE_HOST_DBK_ENABLE |
1725 CODA7_USE_IP_ENABLE |
1726 CODA7_USE_DBK_ENABLE);
1727 }
1728 }
1729}
1730
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001731static void coda_free_context_buffers(struct coda_ctx *ctx)
1732{
1733 struct coda_dev *dev = ctx->dev;
1734
Philipp Zabel918c66f2013-06-27 06:59:01 -03001735 coda_free_aux_buf(dev, &ctx->slicebuf);
1736 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001737 if (dev->devtype->product != CODA_DX6)
1738 coda_free_aux_buf(dev, &ctx->workbuf);
1739}
1740
1741static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1742 struct coda_q_data *q_data)
1743{
1744 struct coda_dev *dev = ctx->dev;
1745 size_t size;
1746 int ret;
1747
1748 switch (dev->devtype->product) {
1749 case CODA_7541:
1750 size = CODA7_WORK_BUF_SIZE;
1751 break;
1752 default:
1753 return 0;
1754 }
1755
Philipp Zabel918c66f2013-06-27 06:59:01 -03001756 if (ctx->psbuf.vaddr) {
1757 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1758 return -EBUSY;
1759 }
1760 if (ctx->slicebuf.vaddr) {
1761 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1762 return -EBUSY;
1763 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001764 if (ctx->workbuf.vaddr) {
1765 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1766 ret = -EBUSY;
1767 return -ENOMEM;
1768 }
1769
Philipp Zabel918c66f2013-06-27 06:59:01 -03001770 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1771 /* worst case slice size */
1772 size = (DIV_ROUND_UP(q_data->width, 16) *
1773 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1774 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1775 if (ret < 0) {
1776 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1777 ctx->slicebuf.size);
1778 return ret;
1779 }
1780 }
1781
1782 if (dev->devtype->product == CODA_7541) {
1783 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1784 if (ret < 0) {
1785 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1786 goto err;
1787 }
1788 }
1789
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001790 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1791 if (ret < 0) {
1792 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1793 ctx->workbuf.size);
1794 goto err;
1795 }
1796
1797 return 0;
1798
1799err:
1800 coda_free_context_buffers(ctx);
1801 return ret;
1802}
1803
Philipp Zabel918c66f2013-06-27 06:59:01 -03001804static int coda_start_decoding(struct coda_ctx *ctx)
1805{
1806 struct coda_q_data *q_data_src, *q_data_dst;
1807 u32 bitstream_buf, bitstream_size;
1808 struct coda_dev *dev = ctx->dev;
1809 int width, height;
1810 u32 src_fourcc;
1811 u32 val;
1812 int ret;
1813
1814 /* Start decoding */
1815 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1816 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1817 bitstream_buf = ctx->bitstream.paddr;
1818 bitstream_size = ctx->bitstream.size;
1819 src_fourcc = q_data_src->fourcc;
1820
1821 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1822
1823 /* Update coda bitstream read and write pointers from kfifo */
1824 coda_kfifo_sync_to_device_full(ctx);
1825
1826 ctx->display_idx = -1;
1827 ctx->frm_dis_flg = 0;
1828 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1829
1830 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1831 CODA_REG_BIT_BIT_STREAM_PARAM);
1832
1833 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1834 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1835 val = 0;
1836 if (dev->devtype->product == CODA_7541)
1837 val |= CODA_REORDER_ENABLE;
1838 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1839
1840 ctx->params.codec_mode = ctx->codec->mode;
1841 ctx->params.codec_mode_aux = 0;
1842 if (src_fourcc == V4L2_PIX_FMT_H264) {
1843 if (dev->devtype->product == CODA_7541) {
1844 coda_write(dev, ctx->psbuf.paddr,
1845 CODA_CMD_DEC_SEQ_PS_BB_START);
1846 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1847 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1848 }
1849 }
1850
1851 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1852 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1853 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1854 return -ETIMEDOUT;
1855 }
1856
1857 /* Update kfifo out pointer from coda bitstream read pointer */
1858 coda_kfifo_sync_from_device(ctx);
1859
1860 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1861
1862 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1863 v4l2_err(&dev->v4l2_dev,
1864 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1865 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1866 return -EAGAIN;
1867 }
1868
1869 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1870 if (dev->devtype->product == CODA_DX6) {
1871 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1872 height = val & CODADX6_PICHEIGHT_MASK;
1873 } else {
1874 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1875 height = val & CODA7_PICHEIGHT_MASK;
1876 }
1877
1878 if (width > q_data_dst->width || height > q_data_dst->height) {
1879 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1880 width, height, q_data_dst->width, q_data_dst->height);
1881 return -EINVAL;
1882 }
1883
1884 width = round_up(width, 16);
1885 height = round_up(height, 16);
1886
1887 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1888 __func__, ctx->idx, width, height);
1889
Philipp Zabela500a932014-07-11 06:36:13 -03001890 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001891 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1892 v4l2_err(&dev->v4l2_dev,
1893 "not enough framebuffers to decode (%d < %d)\n",
1894 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1895 return -EINVAL;
1896 }
1897
1898 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
1899 if (ret < 0)
1900 return ret;
1901
1902 /* Tell the decoder how many frame buffers we allocated. */
1903 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1904 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1905
1906 if (dev->devtype->product != CODA_DX6) {
1907 /* Set secondary AXI IRAM */
1908 coda_setup_iram(ctx);
1909
1910 coda_write(dev, ctx->iram_info.buf_bit_use,
1911 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1912 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1913 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1914 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1915 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1916 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1917 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1918 coda_write(dev, ctx->iram_info.buf_ovl_use,
1919 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1920 }
1921
1922 if (src_fourcc == V4L2_PIX_FMT_H264) {
1923 coda_write(dev, ctx->slicebuf.paddr,
1924 CODA_CMD_SET_FRAME_SLICE_BB_START);
1925 coda_write(dev, ctx->slicebuf.size / 1024,
1926 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
1927 }
1928
1929 if (dev->devtype->product == CODA_7541) {
1930 int max_mb_x = 1920 / 16;
1931 int max_mb_y = 1088 / 16;
1932 int max_mb_num = max_mb_x * max_mb_y;
1933 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1934 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
1935 }
1936
1937 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1938 v4l2_err(&ctx->dev->v4l2_dev,
1939 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1940 return -ETIMEDOUT;
1941 }
1942
1943 return 0;
1944}
1945
Philipp Zabeld35167a2013-05-23 10:42:59 -03001946static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1947 int header_code, u8 *header, int *size)
1948{
1949 struct coda_dev *dev = ctx->dev;
1950 int ret;
1951
1952 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1953 CODA_CMD_ENC_HEADER_BB_START);
1954 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1955 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1956 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1957 if (ret < 0) {
1958 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1959 return ret;
1960 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001961 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
Philipp Zabeld35167a2013-05-23 10:42:59 -03001962 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1963 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1964
1965 return 0;
1966}
1967
Javier Martin186b2502012-07-26 05:53:35 -03001968static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1969{
1970 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1971 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1972 u32 bitstream_buf, bitstream_size;
1973 struct coda_dev *dev = ctx->dev;
1974 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -03001975 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001976 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001977 u32 value;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001978 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001979
Philipp Zabelb96904e2013-05-23 10:42:58 -03001980 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001981 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1982 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
1983 if (coda_get_bitstream_payload(ctx) < 512)
1984 return -EINVAL;
1985 } else {
1986 if (count < 1)
1987 return -EINVAL;
1988 }
1989
1990 ctx->streamon_out = 1;
1991
Philipp Zabelb96904e2013-05-23 10:42:58 -03001992 if (coda_format_is_yuv(q_data_src->fourcc))
1993 ctx->inst_type = CODA_INST_ENCODER;
1994 else
1995 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001996 } else {
1997 if (count < 1)
1998 return -EINVAL;
1999
2000 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002001 }
Javier Martin186b2502012-07-26 05:53:35 -03002002
Philipp Zabelb96904e2013-05-23 10:42:58 -03002003 /* Don't start the coda unless both queues are on */
2004 if (!(ctx->streamon_out & ctx->streamon_cap))
2005 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002006
Philipp Zabeleb107512013-09-30 10:34:45 -03002007 /* Allow decoder device_run with no new buffers queued */
2008 if (ctx->inst_type == CODA_INST_DECODER)
2009 v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002010
Philipp Zabelb96904e2013-05-23 10:42:58 -03002011 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002012 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2013 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2014 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2015 bitstream_size = q_data_dst->sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002016 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002017
Philipp Zabelb96904e2013-05-23 10:42:58 -03002018 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2019 q_data_dst->fourcc);
2020 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002021 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2022 return -EINVAL;
2023 }
2024
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002025 /* Allocate per-instance buffers */
2026 ret = coda_alloc_context_buffers(ctx, q_data_src);
2027 if (ret < 0)
2028 return ret;
2029
Philipp Zabel918c66f2013-06-27 06:59:01 -03002030 if (ctx->inst_type == CODA_INST_DECODER) {
2031 mutex_lock(&dev->coda_mutex);
2032 ret = coda_start_decoding(ctx);
2033 mutex_unlock(&dev->coda_mutex);
2034 if (ret == -EAGAIN) {
2035 return 0;
2036 } else if (ret < 0) {
2037 return ret;
2038 } else {
2039 ctx->initialized = 1;
2040 return 0;
2041 }
2042 }
2043
Javier Martin186b2502012-07-26 05:53:35 -03002044 if (!coda_is_initialized(dev)) {
2045 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2046 return -EFAULT;
2047 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002048
2049 mutex_lock(&dev->coda_mutex);
2050
Javier Martin186b2502012-07-26 05:53:35 -03002051 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002052 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2053 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002054 switch (dev->devtype->product) {
2055 case CODA_DX6:
2056 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2057 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2058 break;
2059 default:
2060 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2061 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2062 }
2063
Philipp Zabel10436672012-07-02 09:03:55 -03002064 if (dev->devtype->product == CODA_DX6) {
2065 /* Configure the coda */
2066 coda_write(dev, dev->iram_paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
2067 }
Javier Martin186b2502012-07-26 05:53:35 -03002068
2069 /* Could set rotation here if needed */
2070 switch (dev->devtype->product) {
2071 case CODA_DX6:
2072 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002073 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002074 break;
2075 default:
2076 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002077 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002078 }
Javier Martin186b2502012-07-26 05:53:35 -03002079 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2080 coda_write(dev, ctx->params.framerate,
2081 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2082
Philipp Zabelb96904e2013-05-23 10:42:58 -03002083 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002084 switch (dst_fourcc) {
2085 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -03002086 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2087 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2088 break;
2089 case V4L2_PIX_FMT_H264:
Javier Martin186b2502012-07-26 05:53:35 -03002090 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2091 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2092 break;
2093 default:
2094 v4l2_err(v4l2_dev,
2095 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002096 ret = -EINVAL;
2097 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002098 }
2099
Philipp Zabelc566c782012-08-08 11:59:38 -03002100 switch (ctx->params.slice_mode) {
2101 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2102 value = 0;
2103 break;
2104 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2105 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2106 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002107 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002108 break;
2109 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2110 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2111 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2112 value |= 1 & CODA_SLICING_MODE_MASK;
2113 break;
2114 }
Javier Martin186b2502012-07-26 05:53:35 -03002115 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002116 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002117 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2118
2119 if (ctx->params.bitrate) {
2120 /* Rate control enabled */
2121 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2122 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
2123 } else {
2124 value = 0;
2125 }
2126 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2127
2128 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2129 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2130
2131 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2132 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2133
2134 /* set default gamma */
2135 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
2136 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
2137
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002138 if (CODA_DEFAULT_GAMMA > 0) {
2139 if (dev->devtype->product == CODA_DX6)
2140 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
2141 else
2142 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
2143 } else {
2144 value = 0;
2145 }
Javier Martin186b2502012-07-26 05:53:35 -03002146 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2147
Philipp Zabelc2d22512013-06-21 03:55:28 -03002148 coda_setup_iram(ctx);
2149
Javier Martin186b2502012-07-26 05:53:35 -03002150 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel10436672012-07-02 09:03:55 -03002151 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002152 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002153 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
2154 } else {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002155 coda_write(dev, ctx->iram_info.search_ram_paddr,
2156 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2157 coda_write(dev, ctx->iram_info.search_ram_size,
2158 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -03002159 }
Javier Martin186b2502012-07-26 05:53:35 -03002160 }
2161
Philipp Zabelfcb62822013-05-23 10:43:00 -03002162 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2163 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002164 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002165 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002166 }
2167
Philipp Zabelfcb62822013-05-23 10:43:00 -03002168 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2169 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2170 ret = -EFAULT;
2171 goto out;
2172 }
Javier Martin186b2502012-07-26 05:53:35 -03002173
Philipp Zabel20397492013-06-21 03:55:29 -03002174 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002175 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002176 if (ret < 0) {
2177 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2178 goto out;
2179 }
Javier Martin186b2502012-07-26 05:53:35 -03002180
Philipp Zabelec25f682012-07-20 08:54:29 -03002181 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002182 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002183 if (dev->devtype->product == CODA_7541)
2184 coda_write(dev, round_up(q_data_src->width, 8),
2185 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002186 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002187 coda_write(dev, ctx->iram_info.buf_bit_use,
2188 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2189 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2190 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2191 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2192 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2193 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2194 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2195 coda_write(dev, ctx->iram_info.buf_ovl_use,
2196 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002197 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002198 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2199 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002200 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002201 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002202 }
2203
2204 /* Save stream headers */
2205 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2206 switch (dst_fourcc) {
2207 case V4L2_PIX_FMT_H264:
2208 /*
2209 * Get SPS in the first frame and copy it to an
2210 * intermediate buffer.
2211 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002212 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2213 &ctx->vpu_header[0][0],
2214 &ctx->vpu_header_size[0]);
2215 if (ret < 0)
2216 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002217
2218 /*
2219 * Get PPS in the first frame and copy it to an
2220 * intermediate buffer.
2221 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002222 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2223 &ctx->vpu_header[1][0],
2224 &ctx->vpu_header_size[1]);
2225 if (ret < 0)
2226 goto out;
2227
Javier Martin3f3f5c72012-10-29 05:20:29 -03002228 /*
2229 * Length of H.264 headers is variable and thus it might not be
2230 * aligned for the coda to append the encoded frame. In that is
2231 * the case a filler NAL must be added to header 2.
2232 */
2233 ctx->vpu_header_size[2] = coda_h264_padding(
2234 (ctx->vpu_header_size[0] +
2235 ctx->vpu_header_size[1]),
2236 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002237 break;
2238 case V4L2_PIX_FMT_MPEG4:
2239 /*
2240 * Get VOS in the first frame and copy it to an
2241 * intermediate buffer
2242 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002243 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2244 &ctx->vpu_header[0][0],
2245 &ctx->vpu_header_size[0]);
2246 if (ret < 0)
2247 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002248
Philipp Zabeld35167a2013-05-23 10:42:59 -03002249 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2250 &ctx->vpu_header[1][0],
2251 &ctx->vpu_header_size[1]);
2252 if (ret < 0)
2253 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002254
Philipp Zabeld35167a2013-05-23 10:42:59 -03002255 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2256 &ctx->vpu_header[2][0],
2257 &ctx->vpu_header_size[2]);
2258 if (ret < 0)
2259 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002260 break;
2261 default:
2262 /* No more formats need to save headers at the moment */
2263 break;
2264 }
2265
Philipp Zabeld35167a2013-05-23 10:42:59 -03002266out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002267 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002268 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002269}
2270
Hans Verkuile37559b2014-04-17 02:47:21 -03002271static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002272{
2273 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002274 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002275
2276 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002277 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002278 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002279 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002280
2281 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2282
2283 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002284 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002285 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002286 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002287 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002288
2289 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002290 }
2291
Philipp Zabel918c66f2013-06-27 06:59:01 -03002292 if (!ctx->streamon_out && !ctx->streamon_cap) {
2293 kfifo_init(&ctx->bitstream_fifo,
2294 ctx->bitstream.vaddr, ctx->bitstream.size);
2295 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002296 }
Javier Martin186b2502012-07-26 05:53:35 -03002297}
2298
2299static struct vb2_ops coda_qops = {
2300 .queue_setup = coda_queue_setup,
2301 .buf_prepare = coda_buf_prepare,
2302 .buf_queue = coda_buf_queue,
2303 .wait_prepare = coda_wait_prepare,
2304 .wait_finish = coda_wait_finish,
2305 .start_streaming = coda_start_streaming,
2306 .stop_streaming = coda_stop_streaming,
2307};
2308
2309static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2310{
2311 struct coda_ctx *ctx =
2312 container_of(ctrl->handler, struct coda_ctx, ctrls);
2313
2314 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2315 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2316
2317 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002318 case V4L2_CID_HFLIP:
2319 if (ctrl->val)
2320 ctx->params.rot_mode |= CODA_MIR_HOR;
2321 else
2322 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2323 break;
2324 case V4L2_CID_VFLIP:
2325 if (ctrl->val)
2326 ctx->params.rot_mode |= CODA_MIR_VER;
2327 else
2328 ctx->params.rot_mode &= ~CODA_MIR_VER;
2329 break;
Javier Martin186b2502012-07-26 05:53:35 -03002330 case V4L2_CID_MPEG_VIDEO_BITRATE:
2331 ctx->params.bitrate = ctrl->val / 1000;
2332 break;
2333 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2334 ctx->params.gop_size = ctrl->val;
2335 break;
2336 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2337 ctx->params.h264_intra_qp = ctrl->val;
2338 break;
2339 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2340 ctx->params.h264_inter_qp = ctrl->val;
2341 break;
2342 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2343 ctx->params.mpeg4_intra_qp = ctrl->val;
2344 break;
2345 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2346 ctx->params.mpeg4_inter_qp = ctrl->val;
2347 break;
2348 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2349 ctx->params.slice_mode = ctrl->val;
2350 break;
2351 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2352 ctx->params.slice_max_mb = ctrl->val;
2353 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002354 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2355 ctx->params.slice_max_bits = ctrl->val * 8;
2356 break;
Javier Martin186b2502012-07-26 05:53:35 -03002357 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2358 break;
2359 default:
2360 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2361 "Invalid control, id=%d, val=%d\n",
2362 ctrl->id, ctrl->val);
2363 return -EINVAL;
2364 }
2365
2366 return 0;
2367}
2368
2369static struct v4l2_ctrl_ops coda_ctrl_ops = {
2370 .s_ctrl = coda_s_ctrl,
2371};
2372
2373static int coda_ctrls_setup(struct coda_ctx *ctx)
2374{
2375 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2376
2377 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002378 V4L2_CID_HFLIP, 0, 1, 1, 0);
2379 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2380 V4L2_CID_VFLIP, 0, 1, 1, 0);
2381 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002382 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2383 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2384 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2385 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002386 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002387 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002388 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002389 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2390 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2391 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2392 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2393 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2394 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002395 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2396 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002397 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2398 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002399 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2400 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002401 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2402 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2403 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2404 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2405 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2406
2407 if (ctx->ctrls.error) {
2408 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2409 ctx->ctrls.error);
2410 return -EINVAL;
2411 }
2412
2413 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2414}
2415
2416static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2417 struct vb2_queue *dst_vq)
2418{
2419 struct coda_ctx *ctx = priv;
2420 int ret;
2421
Javier Martin186b2502012-07-26 05:53:35 -03002422 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002423 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002424 src_vq->drv_priv = ctx;
2425 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2426 src_vq->ops = &coda_qops;
2427 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002428 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002429
2430 ret = vb2_queue_init(src_vq);
2431 if (ret)
2432 return ret;
2433
Javier Martin186b2502012-07-26 05:53:35 -03002434 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002435 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002436 dst_vq->drv_priv = ctx;
2437 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2438 dst_vq->ops = &coda_qops;
2439 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002440 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002441
2442 return vb2_queue_init(dst_vq);
2443}
2444
Philipp Zabele11f3e62012-07-25 09:16:58 -03002445static int coda_next_free_instance(struct coda_dev *dev)
2446{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002447 int idx = ffz(dev->instance_mask);
2448
2449 if ((idx < 0) ||
2450 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2451 return -EBUSY;
2452
2453 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002454}
2455
Javier Martin186b2502012-07-26 05:53:35 -03002456static int coda_open(struct file *file)
2457{
2458 struct coda_dev *dev = video_drvdata(file);
2459 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002460 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002461 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002462
Javier Martin186b2502012-07-26 05:53:35 -03002463 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2464 if (!ctx)
2465 return -ENOMEM;
2466
Fabio Estevamf82bc202013-08-21 11:14:16 -03002467 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002468 if (idx < 0) {
2469 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002470 goto err_coda_max;
2471 }
2472 set_bit(idx, &dev->instance_mask);
2473
Philipp Zabel918c66f2013-06-27 06:59:01 -03002474 INIT_WORK(&ctx->skip_run, coda_skip_run);
Javier Martin186b2502012-07-26 05:53:35 -03002475 v4l2_fh_init(&ctx->fh, video_devdata(file));
2476 file->private_data = &ctx->fh;
2477 v4l2_fh_add(&ctx->fh);
2478 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002479 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002480 switch (dev->devtype->product) {
2481 case CODA_7541:
2482 ctx->reg_idx = 0;
2483 break;
2484 default:
2485 ctx->reg_idx = idx;
2486 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002487
Fabio Estevam79830db2013-08-21 11:14:17 -03002488 ret = clk_prepare_enable(dev->clk_per);
2489 if (ret)
2490 goto err_clk_per;
2491
2492 ret = clk_prepare_enable(dev->clk_ahb);
2493 if (ret)
2494 goto err_clk_ahb;
2495
Javier Martin186b2502012-07-26 05:53:35 -03002496 set_default_params(ctx);
2497 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2498 &coda_queue_init);
2499 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03002500 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002501
2502 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2503 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002504 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002505 }
2506 ret = coda_ctrls_setup(ctx);
2507 if (ret) {
2508 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002509 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002510 }
2511
2512 ctx->fh.ctrl_handler = &ctx->ctrls;
2513
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002514 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2515 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002516 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002517 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002518 }
2519
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002520 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2521 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2522 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2523 if (!ctx->bitstream.vaddr) {
2524 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2525 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002526 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002527 }
2528 kfifo_init(&ctx->bitstream_fifo,
2529 ctx->bitstream.vaddr, ctx->bitstream.size);
2530 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002531 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002532
Javier Martin186b2502012-07-26 05:53:35 -03002533 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002534 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002535 coda_unlock(ctx);
2536
Javier Martin186b2502012-07-26 05:53:35 -03002537 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2538 ctx->idx, ctx);
2539
2540 return 0;
2541
Fabio Estevamf82bc202013-08-21 11:14:16 -03002542err_dma_writecombine:
2543 coda_free_context_buffers(ctx);
2544 if (ctx->dev->devtype->product == CODA_DX6)
2545 coda_free_aux_buf(dev, &ctx->workbuf);
2546 coda_free_aux_buf(dev, &ctx->parabuf);
2547err_dma_alloc:
2548 v4l2_ctrl_handler_free(&ctx->ctrls);
2549err_ctrls_setup:
2550 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2551err_ctx_init:
2552 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002553err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002554 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002555err_clk_per:
Javier Martin186b2502012-07-26 05:53:35 -03002556 v4l2_fh_del(&ctx->fh);
2557 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002558 clear_bit(ctx->idx, &dev->instance_mask);
2559err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002560 kfree(ctx);
2561 return ret;
2562}
2563
2564static int coda_release(struct file *file)
2565{
2566 struct coda_dev *dev = video_drvdata(file);
2567 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2568
2569 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2570 ctx);
2571
Philipp Zabel918c66f2013-06-27 06:59:01 -03002572 /* If this instance is running, call .job_abort and wait for it to end */
2573 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2574
2575 /* In case the instance was not running, we still need to call SEQ_END */
2576 mutex_lock(&dev->coda_mutex);
2577 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2578 "%s: sent command 'SEQ_END' to coda\n", __func__);
2579 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
2580 v4l2_err(&dev->v4l2_dev,
2581 "CODA_COMMAND_SEQ_END failed\n");
2582 mutex_unlock(&dev->coda_mutex);
2583 return -ETIMEDOUT;
2584 }
2585 mutex_unlock(&dev->coda_mutex);
2586
2587 coda_free_framebuffers(ctx);
2588
Javier Martin186b2502012-07-26 05:53:35 -03002589 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002590 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002591 coda_unlock(ctx);
2592
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002593 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2594 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002595 coda_free_context_buffers(ctx);
2596 if (ctx->dev->devtype->product == CODA_DX6)
2597 coda_free_aux_buf(dev, &ctx->workbuf);
2598
2599 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002600 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002601 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002602 clk_disable_unprepare(dev->clk_per);
Javier Martin186b2502012-07-26 05:53:35 -03002603 v4l2_fh_del(&ctx->fh);
2604 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002605 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002606 kfree(ctx);
2607
2608 return 0;
2609}
2610
2611static unsigned int coda_poll(struct file *file,
2612 struct poll_table_struct *wait)
2613{
2614 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2615 int ret;
2616
2617 coda_lock(ctx);
2618 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
2619 coda_unlock(ctx);
2620 return ret;
2621}
2622
2623static int coda_mmap(struct file *file, struct vm_area_struct *vma)
2624{
2625 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2626
2627 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2628}
2629
2630static const struct v4l2_file_operations coda_fops = {
2631 .owner = THIS_MODULE,
2632 .open = coda_open,
2633 .release = coda_release,
2634 .poll = coda_poll,
2635 .unlocked_ioctl = video_ioctl2,
2636 .mmap = coda_mmap,
2637};
2638
Philipp Zabel918c66f2013-06-27 06:59:01 -03002639static void coda_finish_decode(struct coda_ctx *ctx)
2640{
2641 struct coda_dev *dev = ctx->dev;
2642 struct coda_q_data *q_data_src;
2643 struct coda_q_data *q_data_dst;
2644 struct vb2_buffer *dst_buf;
2645 int width, height;
2646 int decoded_idx;
2647 int display_idx;
2648 u32 src_fourcc;
2649 int success;
2650 u32 val;
2651
2652 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2653
2654 /* Update kfifo out pointer from coda bitstream read pointer */
2655 coda_kfifo_sync_from_device(ctx);
2656
2657 /*
2658 * in stream-end mode, the read pointer can overshoot the write pointer
2659 * by up to 512 bytes
2660 */
2661 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2662 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2663 kfifo_init(&ctx->bitstream_fifo,
2664 ctx->bitstream.vaddr, ctx->bitstream.size);
2665 }
2666
2667 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2668 src_fourcc = q_data_src->fourcc;
2669
2670 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2671 if (val != 1)
2672 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2673
2674 success = val & 0x1;
2675 if (!success)
2676 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2677
2678 if (src_fourcc == V4L2_PIX_FMT_H264) {
2679 if (val & (1 << 3))
2680 v4l2_err(&dev->v4l2_dev,
2681 "insufficient PS buffer space (%d bytes)\n",
2682 ctx->psbuf.size);
2683 if (val & (1 << 2))
2684 v4l2_err(&dev->v4l2_dev,
2685 "insufficient slice buffer space (%d bytes)\n",
2686 ctx->slicebuf.size);
2687 }
2688
2689 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2690 width = (val >> 16) & 0xffff;
2691 height = val & 0xffff;
2692
2693 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2694
Philipp Zabel918c66f2013-06-27 06:59:01 -03002695 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2696 if (val > 0)
2697 v4l2_err(&dev->v4l2_dev,
2698 "errors in %d macroblocks\n", val);
2699
2700 if (dev->devtype->product == CODA_7541) {
2701 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2702 if (val == 0) {
2703 /* not enough bitstream data */
2704 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2705 "prescan failed: %d\n", val);
2706 ctx->prescan_failed = true;
2707 return;
2708 }
2709 }
2710
2711 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2712
2713 /*
2714 * The previous display frame was copied out by the rotator,
2715 * now it can be overwritten again
2716 */
2717 if (ctx->display_idx >= 0 &&
2718 ctx->display_idx < ctx->num_internal_frames) {
2719 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
2720 coda_write(dev, ctx->frm_dis_flg,
2721 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2722 }
2723
2724 /*
2725 * The index of the last decoded frame, not necessarily in
2726 * display order, and the index of the next display frame.
2727 * The latter could have been decoded in a previous run.
2728 */
2729 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
2730 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
2731
2732 if (decoded_idx == -1) {
2733 /* no frame was decoded, but we might have a display frame */
2734 if (display_idx < 0 && ctx->display_idx < 0)
2735 ctx->prescan_failed = true;
2736 } else if (decoded_idx == -2) {
2737 /* no frame was decoded, we still return the remaining buffers */
2738 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
2739 v4l2_err(&dev->v4l2_dev,
2740 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03002741 } else {
2742 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
2743 if (val == 0)
2744 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
2745 else if (val == 1)
2746 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
2747 else
2748 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002749 }
2750
2751 if (display_idx == -1) {
2752 /*
2753 * no more frames to be decoded, but there could still
2754 * be rotator output to dequeue
2755 */
2756 ctx->prescan_failed = true;
2757 } else if (display_idx == -3) {
2758 /* possibly prescan failure */
2759 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
2760 v4l2_err(&dev->v4l2_dev,
2761 "presentation frame index out of range: %d\n",
2762 display_idx);
2763 }
2764
2765 /* If a frame was copied out, return it */
2766 if (ctx->display_idx >= 0 &&
2767 ctx->display_idx < ctx->num_internal_frames) {
2768 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
2769 dst_buf->v4l2_buf.sequence = ctx->osequence++;
2770
Philipp Zabel1a8b3812014-07-11 06:36:12 -03002771 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
2772 V4L2_BUF_FLAG_PFRAME |
2773 V4L2_BUF_FLAG_BFRAME);
2774 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
2775
Philipp Zabel918c66f2013-06-27 06:59:01 -03002776 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
2777
2778 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
2779 VB2_BUF_STATE_ERROR);
2780
2781 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2782 "job finished: decoding frame (%d) (%s)\n",
2783 dst_buf->v4l2_buf.sequence,
2784 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2785 "KEYFRAME" : "PFRAME");
2786 } else {
2787 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2788 "job finished: no frame decoded\n");
2789 }
2790
2791 /* The rotator will copy the current display frame next time */
2792 ctx->display_idx = display_idx;
2793}
2794
2795static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03002796{
Philipp Zabelec25f682012-07-20 08:54:29 -03002797 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002798 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002799 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03002800
Philipp Zabelec25f682012-07-20 08:54:29 -03002801 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2802 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002803
2804 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03002805 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002806 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
2807
Javier Martin186b2502012-07-26 05:53:35 -03002808 /* Calculate bytesused field */
2809 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03002810 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
2811 ctx->vpu_header_size[0] +
2812 ctx->vpu_header_size[1] +
2813 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002814 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03002815 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03002816 }
2817
2818 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
2819 wr_ptr - start_ptr);
2820
2821 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
2822 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
2823
Philipp Zabel51660282013-09-30 10:34:49 -03002824 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002825 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
2826 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
2827 } else {
2828 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
2829 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
2830 }
2831
Kamil Debskiccd571c2013-04-24 10:38:24 -03002832 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03002833 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
2834 dst_buf->v4l2_buf.flags |=
2835 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03002836 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
2837
Philipp Zabelec25f682012-07-20 08:54:29 -03002838 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03002839 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
2840
2841 ctx->gopcounter--;
2842 if (ctx->gopcounter < 0)
2843 ctx->gopcounter = ctx->params.gop_size - 1;
2844
2845 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2846 "job finished: encoding frame (%d) (%s)\n",
2847 dst_buf->v4l2_buf.sequence,
2848 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2849 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002850}
2851
2852static irqreturn_t coda_irq_handler(int irq, void *data)
2853{
2854 struct coda_dev *dev = data;
2855 struct coda_ctx *ctx;
2856
2857 cancel_delayed_work(&dev->timeout);
2858
2859 /* read status register to attend the IRQ */
2860 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2861 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2862 CODA_REG_BIT_INT_CLEAR);
2863
2864 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2865 if (ctx == NULL) {
2866 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
2867 mutex_unlock(&dev->coda_mutex);
2868 return IRQ_HANDLED;
2869 }
2870
2871 if (ctx->aborting) {
2872 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2873 "task has been aborted\n");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002874 goto out;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002875 }
2876
2877 if (coda_isbusy(ctx->dev)) {
2878 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2879 "coda is still busy!!!!\n");
2880 return IRQ_NONE;
2881 }
2882
Philipp Zabel918c66f2013-06-27 06:59:01 -03002883 if (ctx->inst_type == CODA_INST_DECODER)
2884 coda_finish_decode(ctx);
2885 else
2886 coda_finish_encode(ctx);
2887
2888out:
2889 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) {
2890 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2891 "%s: sent command 'SEQ_END' to coda\n", __func__);
2892 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
2893 v4l2_err(&dev->v4l2_dev,
2894 "CODA_COMMAND_SEQ_END failed\n");
2895 }
2896
2897 kfifo_init(&ctx->bitstream_fifo,
2898 ctx->bitstream.vaddr, ctx->bitstream.size);
2899
2900 coda_free_framebuffers(ctx);
2901 coda_free_context_buffers(ctx);
2902 }
Javier Martin186b2502012-07-26 05:53:35 -03002903
Philipp Zabelfcb62822013-05-23 10:43:00 -03002904 mutex_unlock(&dev->coda_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002905 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002906
Javier Martin186b2502012-07-26 05:53:35 -03002907 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
2908
2909 return IRQ_HANDLED;
2910}
2911
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002912static void coda_timeout(struct work_struct *work)
2913{
2914 struct coda_ctx *ctx;
2915 struct coda_dev *dev = container_of(to_delayed_work(work),
2916 struct coda_dev, timeout);
2917
Philipp Zabel39cc0292013-05-21 10:32:42 -03002918 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002919
2920 mutex_lock(&dev->dev_mutex);
2921 list_for_each_entry(ctx, &dev->instances, list) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002922 if (mutex_is_locked(&ctx->buffer_mutex))
2923 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002924 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2925 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2926 }
2927 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002928
2929 mutex_unlock(&dev->coda_mutex);
2930 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2931 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002932}
2933
Javier Martin186b2502012-07-26 05:53:35 -03002934static u32 coda_supported_firmwares[] = {
2935 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002936 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Javier Martin186b2502012-07-26 05:53:35 -03002937};
2938
2939static bool coda_firmware_supported(u32 vernum)
2940{
2941 int i;
2942
2943 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
2944 if (vernum == coda_supported_firmwares[i])
2945 return true;
2946 return false;
2947}
2948
Philipp Zabel87048bb2012-07-02 07:03:43 -03002949static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03002950{
2951 u16 product, major, minor, release;
2952 u32 data;
2953 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03002954 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03002955
Fabio Estevam79830db2013-08-21 11:14:17 -03002956 ret = clk_prepare_enable(dev->clk_per);
2957 if (ret)
2958 return ret;
2959
2960 ret = clk_prepare_enable(dev->clk_ahb);
2961 if (ret)
2962 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03002963
Javier Martin186b2502012-07-26 05:53:35 -03002964 /*
2965 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03002966 * The 16-bit chars in the code buffer are in memory access
2967 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03002968 * Data in this SRAM survives a reboot.
2969 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03002970 p = (u16 *)dev->codebuf.vaddr;
2971 if (dev->devtype->product == CODA_DX6) {
2972 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2973 data = CODA_DOWN_ADDRESS_SET(i) |
2974 CODA_DOWN_DATA_SET(p[i ^ 1]);
2975 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2976 }
2977 } else {
2978 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2979 data = CODA_DOWN_ADDRESS_SET(i) |
2980 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2981 3 - (i % 4)]);
2982 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2983 }
Javier Martin186b2502012-07-26 05:53:35 -03002984 }
Javier Martin186b2502012-07-26 05:53:35 -03002985
Philipp Zabel9acf7692013-05-23 10:42:56 -03002986 /* Clear registers */
2987 for (i = 0; i < 64; i++)
2988 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2989
Javier Martin186b2502012-07-26 05:53:35 -03002990 /* Tell the BIT where to find everything it needs */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002991 if (dev->devtype->product == CODA_7541) {
2992 coda_write(dev, dev->tempbuf.paddr,
2993 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002994 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002995 } else {
2996 coda_write(dev, dev->workbuf.paddr,
2997 CODA_REG_BIT_WORK_BUF_ADDR);
2998 }
Javier Martin186b2502012-07-26 05:53:35 -03002999 coda_write(dev, dev->codebuf.paddr,
3000 CODA_REG_BIT_CODE_BUF_ADDR);
3001 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3002
3003 /* Set default values */
3004 switch (dev->devtype->product) {
3005 case CODA_DX6:
3006 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3007 break;
3008 default:
3009 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3010 }
3011 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003012
3013 if (dev->devtype->product != CODA_DX6)
3014 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3015
Javier Martin186b2502012-07-26 05:53:35 -03003016 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3017 CODA_REG_BIT_INT_ENABLE);
3018
3019 /* Reset VPU and start processor */
3020 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3021 data |= CODA_REG_RESET_ENABLE;
3022 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3023 udelay(10);
3024 data &= ~CODA_REG_RESET_ENABLE;
3025 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3026 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3027
3028 /* Load firmware */
3029 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3030 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3031 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3032 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3033 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3034 if (coda_wait_timeout(dev)) {
3035 clk_disable_unprepare(dev->clk_per);
3036 clk_disable_unprepare(dev->clk_ahb);
3037 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
3038 return -EIO;
3039 }
3040
3041 /* Check we are compatible with the loaded firmware */
3042 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3043 product = CODA_FIRMWARE_PRODUCT(data);
3044 major = CODA_FIRMWARE_MAJOR(data);
3045 minor = CODA_FIRMWARE_MINOR(data);
3046 release = CODA_FIRMWARE_RELEASE(data);
3047
3048 clk_disable_unprepare(dev->clk_per);
3049 clk_disable_unprepare(dev->clk_ahb);
3050
3051 if (product != dev->devtype->product) {
3052 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3053 " Version: %u.%u.%u\n",
3054 coda_product_name(dev->devtype->product),
3055 coda_product_name(product), major, minor, release);
3056 return -EINVAL;
3057 }
3058
3059 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3060 coda_product_name(product));
3061
3062 if (coda_firmware_supported(data)) {
3063 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3064 major, minor, release);
3065 } else {
3066 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3067 "%u.%u.%u\n", major, minor, release);
3068 }
3069
3070 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003071
3072err_clk_ahb:
3073 clk_disable_unprepare(dev->clk_per);
3074 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003075}
3076
3077static void coda_fw_callback(const struct firmware *fw, void *context)
3078{
3079 struct coda_dev *dev = context;
3080 struct platform_device *pdev = dev->plat_dev;
3081 int ret;
3082
3083 if (!fw) {
3084 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3085 return;
3086 }
3087
3088 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003089 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3090 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003091 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3092 return;
3093 }
3094
Philipp Zabel87048bb2012-07-02 07:03:43 -03003095 /* Copy the whole firmware image to the code buffer */
3096 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3097 release_firmware(fw);
3098
3099 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03003100 if (ret) {
3101 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3102 return;
3103 }
3104
3105 dev->vfd.fops = &coda_fops,
3106 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3107 dev->vfd.release = video_device_release_empty,
3108 dev->vfd.lock = &dev->dev_mutex;
3109 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003110 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003111 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3112 video_set_drvdata(&dev->vfd, dev);
3113
3114 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3115 if (IS_ERR(dev->alloc_ctx)) {
3116 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3117 return;
3118 }
3119
3120 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3121 if (IS_ERR(dev->m2m_dev)) {
3122 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3123 goto rel_ctx;
3124 }
3125
3126 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3127 if (ret) {
3128 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3129 goto rel_m2m;
3130 }
3131 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3132 dev->vfd.num);
3133
3134 return;
3135
3136rel_m2m:
3137 v4l2_m2m_release(dev->m2m_dev);
3138rel_ctx:
3139 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3140}
3141
3142static int coda_firmware_request(struct coda_dev *dev)
3143{
3144 char *fw = dev->devtype->firmware;
3145
3146 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3147 coda_product_name(dev->devtype->product));
3148
3149 return request_firmware_nowait(THIS_MODULE, true,
3150 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3151}
3152
3153enum coda_platform {
3154 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003155 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03003156};
3157
Emil Goodec06d8752012-08-14 17:44:42 -03003158static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003159 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003160 .firmware = "v4l-codadx6-imx27.bin",
3161 .product = CODA_DX6,
3162 .codecs = codadx6_codecs,
3163 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003164 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003165 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003166 .firmware = "v4l-coda7541-imx53.bin",
3167 .product = CODA_7541,
3168 .codecs = coda7_codecs,
3169 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003170 },
Javier Martin186b2502012-07-26 05:53:35 -03003171};
3172
3173static struct platform_device_id coda_platform_ids[] = {
3174 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003175 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003176 { /* sentinel */ }
3177};
3178MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3179
3180#ifdef CONFIG_OF
3181static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003182 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003183 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03003184 { /* sentinel */ }
3185};
3186MODULE_DEVICE_TABLE(of, coda_dt_ids);
3187#endif
3188
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003189static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003190{
3191 const struct of_device_id *of_id =
3192 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3193 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003194 struct coda_platform_data *pdata = pdev->dev.platform_data;
3195 struct device_node *np = pdev->dev.of_node;
3196 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003197 struct coda_dev *dev;
3198 struct resource *res;
3199 int ret, irq;
3200
3201 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3202 if (!dev) {
3203 dev_err(&pdev->dev, "Not enough memory for %s\n",
3204 CODA_NAME);
3205 return -ENOMEM;
3206 }
3207
3208 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003209 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003210 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03003211
3212 dev->plat_dev = pdev;
3213 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3214 if (IS_ERR(dev->clk_per)) {
3215 dev_err(&pdev->dev, "Could not get per clock\n");
3216 return PTR_ERR(dev->clk_per);
3217 }
3218
3219 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3220 if (IS_ERR(dev->clk_ahb)) {
3221 dev_err(&pdev->dev, "Could not get ahb clock\n");
3222 return PTR_ERR(dev->clk_ahb);
3223 }
3224
3225 /* Get memory for physical registers */
3226 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003227 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3228 if (IS_ERR(dev->regs_base))
3229 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003230
3231 /* IRQ */
3232 irq = platform_get_irq(pdev, 0);
3233 if (irq < 0) {
3234 dev_err(&pdev->dev, "failed to get irq resource\n");
3235 return -ENOENT;
3236 }
3237
Philipp Zabel918c66f2013-06-27 06:59:01 -03003238 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003239 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003240 dev_err(&pdev->dev, "failed to request irq\n");
3241 return -ENOENT;
3242 }
3243
Philipp Zabel657eee72013-04-29 16:17:14 -07003244 /* Get IRAM pool from device tree or platform data */
3245 pool = of_get_named_gen_pool(np, "iram", 0);
3246 if (!pool && pdata)
3247 pool = dev_get_gen_pool(pdata->iram_dev);
3248 if (!pool) {
3249 dev_err(&pdev->dev, "iram pool not available\n");
3250 return -ENOMEM;
3251 }
3252 dev->iram_pool = pool;
3253
Javier Martin186b2502012-07-26 05:53:35 -03003254 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3255 if (ret)
3256 return ret;
3257
3258 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003259 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003260
3261 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3262
3263 if (of_id) {
3264 dev->devtype = of_id->data;
3265 } else if (pdev_id) {
3266 dev->devtype = &coda_devdata[pdev_id->driver_data];
3267 } else {
3268 v4l2_device_unregister(&dev->v4l2_dev);
3269 return -EINVAL;
3270 }
3271
3272 /* allocate auxiliary per-device buffers for the BIT processor */
3273 switch (dev->devtype->product) {
3274 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003275 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3276 CODADX6_WORK_BUF_SIZE);
3277 if (ret < 0) {
3278 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3279 v4l2_device_unregister(&dev->v4l2_dev);
3280 return ret;
3281 }
Javier Martin186b2502012-07-26 05:53:35 -03003282 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003283 case CODA_7541:
3284 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3285 break;
Javier Martin186b2502012-07-26 05:53:35 -03003286 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003287 if (dev->tempbuf.size) {
3288 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3289 dev->tempbuf.size);
3290 if (ret < 0) {
3291 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3292 v4l2_device_unregister(&dev->v4l2_dev);
3293 return ret;
3294 }
Javier Martin186b2502012-07-26 05:53:35 -03003295 }
3296
Philipp Zabel918c66f2013-06-27 06:59:01 -03003297 switch (dev->devtype->product) {
3298 case CODA_DX6:
Philipp Zabel657eee72013-04-29 16:17:14 -07003299 dev->iram_size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003300 break;
3301 case CODA_7541:
Philipp Zabel657eee72013-04-29 16:17:14 -07003302 dev->iram_size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003303 break;
3304 }
Nicolin Chen43f950f2013-11-12 15:09:56 -08003305 dev->iram_vaddr = (unsigned long)gen_pool_dma_alloc(dev->iram_pool,
3306 dev->iram_size, (dma_addr_t *)&dev->iram_paddr);
Philipp Zabel657eee72013-04-29 16:17:14 -07003307 if (!dev->iram_vaddr) {
3308 dev_err(&pdev->dev, "unable to alloc iram\n");
3309 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003310 }
3311
Javier Martin186b2502012-07-26 05:53:35 -03003312 platform_set_drvdata(pdev, dev);
3313
3314 return coda_firmware_request(dev);
3315}
3316
3317static int coda_remove(struct platform_device *pdev)
3318{
3319 struct coda_dev *dev = platform_get_drvdata(pdev);
3320
3321 video_unregister_device(&dev->vfd);
3322 if (dev->m2m_dev)
3323 v4l2_m2m_release(dev->m2m_dev);
3324 if (dev->alloc_ctx)
3325 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3326 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07003327 if (dev->iram_vaddr)
3328 gen_pool_free(dev->iram_pool, dev->iram_vaddr, dev->iram_size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003329 coda_free_aux_buf(dev, &dev->codebuf);
3330 coda_free_aux_buf(dev, &dev->tempbuf);
3331 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003332 return 0;
3333}
3334
3335static struct platform_driver coda_driver = {
3336 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003337 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003338 .driver = {
3339 .name = CODA_NAME,
3340 .owner = THIS_MODULE,
3341 .of_match_table = of_match_ptr(coda_dt_ids),
3342 },
3343 .id_table = coda_platform_ids,
3344};
3345
3346module_platform_driver(coda_driver);
3347
3348MODULE_LICENSE("GPL");
3349MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3350MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");