blob: 31a1848f367424647bfae884abe690de4e2f38d1 [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;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300138 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300139
140 spinlock_t irqlock;
141 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300142 struct mutex coda_mutex;
Javier Martin186b2502012-07-26 05:53:35 -0300143 struct v4l2_m2m_dev *m2m_dev;
144 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300145 struct list_head instances;
146 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300147 struct delayed_work timeout;
Javier Martin186b2502012-07-26 05:53:35 -0300148};
149
150struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300151 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300152 u8 h264_intra_qp;
153 u8 h264_inter_qp;
154 u8 mpeg4_intra_qp;
155 u8 mpeg4_inter_qp;
156 u8 gop_size;
157 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300158 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300159 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
160 u32 framerate;
161 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300162 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300163 u32 slice_max_mb;
164};
165
Philipp Zabelc2d22512013-06-21 03:55:28 -0300166struct coda_iram_info {
167 u32 axi_sram_use;
168 phys_addr_t buf_bit_use;
169 phys_addr_t buf_ip_ac_dc_use;
170 phys_addr_t buf_dbk_y_use;
171 phys_addr_t buf_dbk_c_use;
172 phys_addr_t buf_ovl_use;
173 phys_addr_t buf_btp_use;
174 phys_addr_t search_ram_paddr;
175 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300176 int remaining;
177 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300178};
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
Philipp Zabelaed14b02014-07-11 06:36:15 -03001528 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001529 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1530 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001531 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 Zabelb313bcc2014-07-11 06:36:16 -03001581static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1582{
1583 phys_addr_t ret;
1584
1585 size = round_up(size, 1024);
1586 if (size > iram->remaining)
1587 return 0;
1588 iram->remaining -= size;
1589
1590 ret = iram->next_paddr;
1591 iram->next_paddr += size;
1592
1593 return ret;
1594}
1595
Philipp Zabelc2d22512013-06-21 03:55:28 -03001596static void coda_setup_iram(struct coda_ctx *ctx)
1597{
1598 struct coda_iram_info *iram_info = &ctx->iram_info;
1599 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001600 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001601 int dbk_bits;
1602 int bit_bits;
1603 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001604
1605 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001606 iram_info->next_paddr = dev->iram.paddr;
1607 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001608
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001609 switch (dev->devtype->product) {
1610 case CODA_7541:
1611 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1612 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1613 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1614 break;
1615 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001616 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001617 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001618
1619 if (ctx->inst_type == CODA_INST_ENCODER) {
1620 struct coda_q_data *q_data_src;
1621
1622 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1623 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1624
1625 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001626 if (dev->devtype->product == CODA_7541) {
1627 iram_info->search_ram_size = round_up(mb_width * 16 *
1628 36 + 2048, 1024);
1629 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1630 iram_info->search_ram_size);
1631 if (!iram_info->search_ram_paddr) {
1632 pr_err("IRAM is smaller than the search ram size\n");
1633 goto out;
1634 }
1635 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1636 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001637 }
1638
1639 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001640 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1641 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1642 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001643 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001644 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001645
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001646 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1647 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001648 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001649 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001650
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001651 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1652 if (!iram_info->buf_ip_ac_dc_use)
1653 goto out;
1654 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001655
Philipp Zabel8358e762013-06-21 03:55:32 -03001656 /* OVL and BTP disabled for encoder */
1657 } else if (ctx->inst_type == CODA_INST_DECODER) {
1658 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001659
1660 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1661 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001662
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001663 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1664 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1665 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001666 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001667 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001668
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001669 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1670 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001671 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001672 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001673
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001674 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1675 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001676 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001677 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001678
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001679 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001680 }
1681
1682out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001683 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1684 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1685 "IRAM smaller than needed\n");
1686
1687 if (dev->devtype->product == CODA_7541) {
1688 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001689 if (ctx->inst_type == CODA_INST_DECODER) {
1690 /* fw 1.4.50 */
1691 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1692 CODA7_USE_IP_ENABLE);
1693 } else {
1694 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001695 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1696 CODA7_USE_HOST_DBK_ENABLE |
1697 CODA7_USE_IP_ENABLE |
1698 CODA7_USE_DBK_ENABLE);
1699 }
1700 }
1701}
1702
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001703static void coda_free_context_buffers(struct coda_ctx *ctx)
1704{
1705 struct coda_dev *dev = ctx->dev;
1706
Philipp Zabel918c66f2013-06-27 06:59:01 -03001707 coda_free_aux_buf(dev, &ctx->slicebuf);
1708 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001709 if (dev->devtype->product != CODA_DX6)
1710 coda_free_aux_buf(dev, &ctx->workbuf);
1711}
1712
1713static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1714 struct coda_q_data *q_data)
1715{
1716 struct coda_dev *dev = ctx->dev;
1717 size_t size;
1718 int ret;
1719
1720 switch (dev->devtype->product) {
1721 case CODA_7541:
1722 size = CODA7_WORK_BUF_SIZE;
1723 break;
1724 default:
1725 return 0;
1726 }
1727
Philipp Zabel918c66f2013-06-27 06:59:01 -03001728 if (ctx->psbuf.vaddr) {
1729 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1730 return -EBUSY;
1731 }
1732 if (ctx->slicebuf.vaddr) {
1733 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1734 return -EBUSY;
1735 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001736 if (ctx->workbuf.vaddr) {
1737 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1738 ret = -EBUSY;
1739 return -ENOMEM;
1740 }
1741
Philipp Zabel918c66f2013-06-27 06:59:01 -03001742 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1743 /* worst case slice size */
1744 size = (DIV_ROUND_UP(q_data->width, 16) *
1745 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1746 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1747 if (ret < 0) {
1748 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1749 ctx->slicebuf.size);
1750 return ret;
1751 }
1752 }
1753
1754 if (dev->devtype->product == CODA_7541) {
1755 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1756 if (ret < 0) {
1757 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1758 goto err;
1759 }
1760 }
1761
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001762 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1763 if (ret < 0) {
1764 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1765 ctx->workbuf.size);
1766 goto err;
1767 }
1768
1769 return 0;
1770
1771err:
1772 coda_free_context_buffers(ctx);
1773 return ret;
1774}
1775
Philipp Zabel918c66f2013-06-27 06:59:01 -03001776static int coda_start_decoding(struct coda_ctx *ctx)
1777{
1778 struct coda_q_data *q_data_src, *q_data_dst;
1779 u32 bitstream_buf, bitstream_size;
1780 struct coda_dev *dev = ctx->dev;
1781 int width, height;
1782 u32 src_fourcc;
1783 u32 val;
1784 int ret;
1785
1786 /* Start decoding */
1787 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1788 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1789 bitstream_buf = ctx->bitstream.paddr;
1790 bitstream_size = ctx->bitstream.size;
1791 src_fourcc = q_data_src->fourcc;
1792
1793 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1794
1795 /* Update coda bitstream read and write pointers from kfifo */
1796 coda_kfifo_sync_to_device_full(ctx);
1797
1798 ctx->display_idx = -1;
1799 ctx->frm_dis_flg = 0;
1800 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1801
1802 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1803 CODA_REG_BIT_BIT_STREAM_PARAM);
1804
1805 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1806 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1807 val = 0;
1808 if (dev->devtype->product == CODA_7541)
1809 val |= CODA_REORDER_ENABLE;
1810 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1811
1812 ctx->params.codec_mode = ctx->codec->mode;
1813 ctx->params.codec_mode_aux = 0;
1814 if (src_fourcc == V4L2_PIX_FMT_H264) {
1815 if (dev->devtype->product == CODA_7541) {
1816 coda_write(dev, ctx->psbuf.paddr,
1817 CODA_CMD_DEC_SEQ_PS_BB_START);
1818 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1819 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1820 }
1821 }
1822
1823 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1824 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1825 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1826 return -ETIMEDOUT;
1827 }
1828
1829 /* Update kfifo out pointer from coda bitstream read pointer */
1830 coda_kfifo_sync_from_device(ctx);
1831
1832 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1833
1834 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1835 v4l2_err(&dev->v4l2_dev,
1836 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1837 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1838 return -EAGAIN;
1839 }
1840
1841 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1842 if (dev->devtype->product == CODA_DX6) {
1843 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1844 height = val & CODADX6_PICHEIGHT_MASK;
1845 } else {
1846 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1847 height = val & CODA7_PICHEIGHT_MASK;
1848 }
1849
1850 if (width > q_data_dst->width || height > q_data_dst->height) {
1851 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1852 width, height, q_data_dst->width, q_data_dst->height);
1853 return -EINVAL;
1854 }
1855
1856 width = round_up(width, 16);
1857 height = round_up(height, 16);
1858
1859 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1860 __func__, ctx->idx, width, height);
1861
Philipp Zabela500a932014-07-11 06:36:13 -03001862 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001863 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1864 v4l2_err(&dev->v4l2_dev,
1865 "not enough framebuffers to decode (%d < %d)\n",
1866 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1867 return -EINVAL;
1868 }
1869
1870 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
1871 if (ret < 0)
1872 return ret;
1873
1874 /* Tell the decoder how many frame buffers we allocated. */
1875 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1876 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1877
1878 if (dev->devtype->product != CODA_DX6) {
1879 /* Set secondary AXI IRAM */
1880 coda_setup_iram(ctx);
1881
1882 coda_write(dev, ctx->iram_info.buf_bit_use,
1883 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1884 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1885 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1886 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1887 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1888 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1889 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1890 coda_write(dev, ctx->iram_info.buf_ovl_use,
1891 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
1892 }
1893
1894 if (src_fourcc == V4L2_PIX_FMT_H264) {
1895 coda_write(dev, ctx->slicebuf.paddr,
1896 CODA_CMD_SET_FRAME_SLICE_BB_START);
1897 coda_write(dev, ctx->slicebuf.size / 1024,
1898 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
1899 }
1900
1901 if (dev->devtype->product == CODA_7541) {
1902 int max_mb_x = 1920 / 16;
1903 int max_mb_y = 1088 / 16;
1904 int max_mb_num = max_mb_x * max_mb_y;
1905 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
1906 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
1907 }
1908
1909 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
1910 v4l2_err(&ctx->dev->v4l2_dev,
1911 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
1912 return -ETIMEDOUT;
1913 }
1914
1915 return 0;
1916}
1917
Philipp Zabeld35167a2013-05-23 10:42:59 -03001918static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
1919 int header_code, u8 *header, int *size)
1920{
1921 struct coda_dev *dev = ctx->dev;
1922 int ret;
1923
1924 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
1925 CODA_CMD_ENC_HEADER_BB_START);
1926 coda_write(dev, vb2_plane_size(buf, 0), CODA_CMD_ENC_HEADER_BB_SIZE);
1927 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
1928 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
1929 if (ret < 0) {
1930 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
1931 return ret;
1932 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001933 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
Philipp Zabeld35167a2013-05-23 10:42:59 -03001934 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
1935 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
1936
1937 return 0;
1938}
1939
Javier Martin186b2502012-07-26 05:53:35 -03001940static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1941{
1942 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1943 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
1944 u32 bitstream_buf, bitstream_size;
1945 struct coda_dev *dev = ctx->dev;
1946 struct coda_q_data *q_data_src, *q_data_dst;
Javier Martin186b2502012-07-26 05:53:35 -03001947 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001948 u32 dst_fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001949 u32 value;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001950 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001951
Philipp Zabelb96904e2013-05-23 10:42:58 -03001952 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001953 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1954 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
1955 if (coda_get_bitstream_payload(ctx) < 512)
1956 return -EINVAL;
1957 } else {
1958 if (count < 1)
1959 return -EINVAL;
1960 }
1961
1962 ctx->streamon_out = 1;
1963
Philipp Zabelb96904e2013-05-23 10:42:58 -03001964 if (coda_format_is_yuv(q_data_src->fourcc))
1965 ctx->inst_type = CODA_INST_ENCODER;
1966 else
1967 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001968 } else {
1969 if (count < 1)
1970 return -EINVAL;
1971
1972 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001973 }
Javier Martin186b2502012-07-26 05:53:35 -03001974
Philipp Zabelb96904e2013-05-23 10:42:58 -03001975 /* Don't start the coda unless both queues are on */
1976 if (!(ctx->streamon_out & ctx->streamon_cap))
1977 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001978
Philipp Zabeleb107512013-09-30 10:34:45 -03001979 /* Allow decoder device_run with no new buffers queued */
1980 if (ctx->inst_type == CODA_INST_DECODER)
1981 v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001982
Philipp Zabelb96904e2013-05-23 10:42:58 -03001983 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001984 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1985 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
1986 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1987 bitstream_size = q_data_dst->sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001988 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001989
Philipp Zabelb96904e2013-05-23 10:42:58 -03001990 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1991 q_data_dst->fourcc);
1992 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001993 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
1994 return -EINVAL;
1995 }
1996
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001997 /* Allocate per-instance buffers */
1998 ret = coda_alloc_context_buffers(ctx, q_data_src);
1999 if (ret < 0)
2000 return ret;
2001
Philipp Zabel918c66f2013-06-27 06:59:01 -03002002 if (ctx->inst_type == CODA_INST_DECODER) {
2003 mutex_lock(&dev->coda_mutex);
2004 ret = coda_start_decoding(ctx);
2005 mutex_unlock(&dev->coda_mutex);
2006 if (ret == -EAGAIN) {
2007 return 0;
2008 } else if (ret < 0) {
2009 return ret;
2010 } else {
2011 ctx->initialized = 1;
2012 return 0;
2013 }
2014 }
2015
Javier Martin186b2502012-07-26 05:53:35 -03002016 if (!coda_is_initialized(dev)) {
2017 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2018 return -EFAULT;
2019 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002020
2021 mutex_lock(&dev->coda_mutex);
2022
Javier Martin186b2502012-07-26 05:53:35 -03002023 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002024 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2025 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002026 switch (dev->devtype->product) {
2027 case CODA_DX6:
2028 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2029 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2030 break;
2031 default:
2032 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2033 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2034 }
2035
Philipp Zabel10436672012-07-02 09:03:55 -03002036 if (dev->devtype->product == CODA_DX6) {
2037 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002038 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002039 }
Javier Martin186b2502012-07-26 05:53:35 -03002040
2041 /* Could set rotation here if needed */
2042 switch (dev->devtype->product) {
2043 case CODA_DX6:
2044 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002045 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002046 break;
2047 default:
2048 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002049 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002050 }
Javier Martin186b2502012-07-26 05:53:35 -03002051 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2052 coda_write(dev, ctx->params.framerate,
2053 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2054
Philipp Zabelb96904e2013-05-23 10:42:58 -03002055 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002056 switch (dst_fourcc) {
2057 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -03002058 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2059 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2060 break;
2061 case V4L2_PIX_FMT_H264:
Javier Martin186b2502012-07-26 05:53:35 -03002062 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2063 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2064 break;
2065 default:
2066 v4l2_err(v4l2_dev,
2067 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002068 ret = -EINVAL;
2069 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002070 }
2071
Philipp Zabelc566c782012-08-08 11:59:38 -03002072 switch (ctx->params.slice_mode) {
2073 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2074 value = 0;
2075 break;
2076 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2077 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2078 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002079 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002080 break;
2081 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2082 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2083 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2084 value |= 1 & CODA_SLICING_MODE_MASK;
2085 break;
2086 }
Javier Martin186b2502012-07-26 05:53:35 -03002087 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002088 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002089 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2090
2091 if (ctx->params.bitrate) {
2092 /* Rate control enabled */
2093 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2094 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
2095 } else {
2096 value = 0;
2097 }
2098 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2099
2100 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2101 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2102
2103 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2104 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2105
2106 /* set default gamma */
2107 value = (CODA_DEFAULT_GAMMA & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET;
2108 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_GAMMA);
2109
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002110 if (CODA_DEFAULT_GAMMA > 0) {
2111 if (dev->devtype->product == CODA_DX6)
2112 value = 1 << CODADX6_OPTION_GAMMA_OFFSET;
2113 else
2114 value = 1 << CODA7_OPTION_GAMMA_OFFSET;
2115 } else {
2116 value = 0;
2117 }
Javier Martin186b2502012-07-26 05:53:35 -03002118 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2119
Philipp Zabelc2d22512013-06-21 03:55:28 -03002120 coda_setup_iram(ctx);
2121
Javier Martin186b2502012-07-26 05:53:35 -03002122 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel10436672012-07-02 09:03:55 -03002123 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002124 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002125 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
2126 } else {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002127 coda_write(dev, ctx->iram_info.search_ram_paddr,
2128 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2129 coda_write(dev, ctx->iram_info.search_ram_size,
2130 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel10436672012-07-02 09:03:55 -03002131 }
Javier Martin186b2502012-07-26 05:53:35 -03002132 }
2133
Philipp Zabelfcb62822013-05-23 10:43:00 -03002134 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2135 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002136 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002137 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002138 }
2139
Philipp Zabelfcb62822013-05-23 10:43:00 -03002140 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2141 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2142 ret = -EFAULT;
2143 goto out;
2144 }
Javier Martin186b2502012-07-26 05:53:35 -03002145
Philipp Zabel20397492013-06-21 03:55:29 -03002146 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002147 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002148 if (ret < 0) {
2149 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2150 goto out;
2151 }
Javier Martin186b2502012-07-26 05:53:35 -03002152
Philipp Zabelec25f682012-07-20 08:54:29 -03002153 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002154 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002155 if (dev->devtype->product == CODA_7541)
2156 coda_write(dev, round_up(q_data_src->width, 8),
2157 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002158 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002159 coda_write(dev, ctx->iram_info.buf_bit_use,
2160 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2161 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2162 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2163 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2164 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2165 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2166 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2167 coda_write(dev, ctx->iram_info.buf_ovl_use,
2168 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002169 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002170 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2171 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002172 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002173 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002174 }
2175
2176 /* Save stream headers */
2177 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2178 switch (dst_fourcc) {
2179 case V4L2_PIX_FMT_H264:
2180 /*
2181 * Get SPS in the first frame and copy it to an
2182 * intermediate buffer.
2183 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002184 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2185 &ctx->vpu_header[0][0],
2186 &ctx->vpu_header_size[0]);
2187 if (ret < 0)
2188 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002189
2190 /*
2191 * Get PPS in the first frame and copy it to an
2192 * intermediate buffer.
2193 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002194 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2195 &ctx->vpu_header[1][0],
2196 &ctx->vpu_header_size[1]);
2197 if (ret < 0)
2198 goto out;
2199
Javier Martin3f3f5c72012-10-29 05:20:29 -03002200 /*
2201 * Length of H.264 headers is variable and thus it might not be
2202 * aligned for the coda to append the encoded frame. In that is
2203 * the case a filler NAL must be added to header 2.
2204 */
2205 ctx->vpu_header_size[2] = coda_h264_padding(
2206 (ctx->vpu_header_size[0] +
2207 ctx->vpu_header_size[1]),
2208 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002209 break;
2210 case V4L2_PIX_FMT_MPEG4:
2211 /*
2212 * Get VOS in the first frame and copy it to an
2213 * intermediate buffer
2214 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002215 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2216 &ctx->vpu_header[0][0],
2217 &ctx->vpu_header_size[0]);
2218 if (ret < 0)
2219 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002220
Philipp Zabeld35167a2013-05-23 10:42:59 -03002221 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2222 &ctx->vpu_header[1][0],
2223 &ctx->vpu_header_size[1]);
2224 if (ret < 0)
2225 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002226
Philipp Zabeld35167a2013-05-23 10:42:59 -03002227 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2228 &ctx->vpu_header[2][0],
2229 &ctx->vpu_header_size[2]);
2230 if (ret < 0)
2231 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002232 break;
2233 default:
2234 /* No more formats need to save headers at the moment */
2235 break;
2236 }
2237
Philipp Zabeld35167a2013-05-23 10:42:59 -03002238out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002239 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002240 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002241}
2242
Hans Verkuile37559b2014-04-17 02:47:21 -03002243static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002244{
2245 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002246 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002247
2248 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002249 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002250 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002251 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002252
2253 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2254
2255 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002256 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002257 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002258 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002259 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002260
2261 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002262 }
2263
Philipp Zabel918c66f2013-06-27 06:59:01 -03002264 if (!ctx->streamon_out && !ctx->streamon_cap) {
2265 kfifo_init(&ctx->bitstream_fifo,
2266 ctx->bitstream.vaddr, ctx->bitstream.size);
2267 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002268 }
Javier Martin186b2502012-07-26 05:53:35 -03002269}
2270
2271static struct vb2_ops coda_qops = {
2272 .queue_setup = coda_queue_setup,
2273 .buf_prepare = coda_buf_prepare,
2274 .buf_queue = coda_buf_queue,
2275 .wait_prepare = coda_wait_prepare,
2276 .wait_finish = coda_wait_finish,
2277 .start_streaming = coda_start_streaming,
2278 .stop_streaming = coda_stop_streaming,
2279};
2280
2281static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2282{
2283 struct coda_ctx *ctx =
2284 container_of(ctrl->handler, struct coda_ctx, ctrls);
2285
2286 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2287 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2288
2289 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002290 case V4L2_CID_HFLIP:
2291 if (ctrl->val)
2292 ctx->params.rot_mode |= CODA_MIR_HOR;
2293 else
2294 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2295 break;
2296 case V4L2_CID_VFLIP:
2297 if (ctrl->val)
2298 ctx->params.rot_mode |= CODA_MIR_VER;
2299 else
2300 ctx->params.rot_mode &= ~CODA_MIR_VER;
2301 break;
Javier Martin186b2502012-07-26 05:53:35 -03002302 case V4L2_CID_MPEG_VIDEO_BITRATE:
2303 ctx->params.bitrate = ctrl->val / 1000;
2304 break;
2305 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2306 ctx->params.gop_size = ctrl->val;
2307 break;
2308 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2309 ctx->params.h264_intra_qp = ctrl->val;
2310 break;
2311 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2312 ctx->params.h264_inter_qp = ctrl->val;
2313 break;
2314 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2315 ctx->params.mpeg4_intra_qp = ctrl->val;
2316 break;
2317 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2318 ctx->params.mpeg4_inter_qp = ctrl->val;
2319 break;
2320 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2321 ctx->params.slice_mode = ctrl->val;
2322 break;
2323 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2324 ctx->params.slice_max_mb = ctrl->val;
2325 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002326 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2327 ctx->params.slice_max_bits = ctrl->val * 8;
2328 break;
Javier Martin186b2502012-07-26 05:53:35 -03002329 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2330 break;
2331 default:
2332 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2333 "Invalid control, id=%d, val=%d\n",
2334 ctrl->id, ctrl->val);
2335 return -EINVAL;
2336 }
2337
2338 return 0;
2339}
2340
2341static struct v4l2_ctrl_ops coda_ctrl_ops = {
2342 .s_ctrl = coda_s_ctrl,
2343};
2344
2345static int coda_ctrls_setup(struct coda_ctx *ctx)
2346{
2347 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2348
2349 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002350 V4L2_CID_HFLIP, 0, 1, 1, 0);
2351 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2352 V4L2_CID_VFLIP, 0, 1, 1, 0);
2353 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002354 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2355 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2356 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2357 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002358 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002359 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002360 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002361 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2362 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2363 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2364 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2365 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2366 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002367 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2368 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002369 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2370 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002371 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2372 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002373 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2374 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2375 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2376 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2377 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2378
2379 if (ctx->ctrls.error) {
2380 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2381 ctx->ctrls.error);
2382 return -EINVAL;
2383 }
2384
2385 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2386}
2387
2388static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2389 struct vb2_queue *dst_vq)
2390{
2391 struct coda_ctx *ctx = priv;
2392 int ret;
2393
Javier Martin186b2502012-07-26 05:53:35 -03002394 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002395 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002396 src_vq->drv_priv = ctx;
2397 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2398 src_vq->ops = &coda_qops;
2399 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002400 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002401
2402 ret = vb2_queue_init(src_vq);
2403 if (ret)
2404 return ret;
2405
Javier Martin186b2502012-07-26 05:53:35 -03002406 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002407 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002408 dst_vq->drv_priv = ctx;
2409 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2410 dst_vq->ops = &coda_qops;
2411 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002412 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002413
2414 return vb2_queue_init(dst_vq);
2415}
2416
Philipp Zabele11f3e62012-07-25 09:16:58 -03002417static int coda_next_free_instance(struct coda_dev *dev)
2418{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002419 int idx = ffz(dev->instance_mask);
2420
2421 if ((idx < 0) ||
2422 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2423 return -EBUSY;
2424
2425 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002426}
2427
Javier Martin186b2502012-07-26 05:53:35 -03002428static int coda_open(struct file *file)
2429{
2430 struct coda_dev *dev = video_drvdata(file);
2431 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002432 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002433 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002434
Javier Martin186b2502012-07-26 05:53:35 -03002435 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2436 if (!ctx)
2437 return -ENOMEM;
2438
Fabio Estevamf82bc202013-08-21 11:14:16 -03002439 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002440 if (idx < 0) {
2441 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002442 goto err_coda_max;
2443 }
2444 set_bit(idx, &dev->instance_mask);
2445
Philipp Zabel918c66f2013-06-27 06:59:01 -03002446 INIT_WORK(&ctx->skip_run, coda_skip_run);
Javier Martin186b2502012-07-26 05:53:35 -03002447 v4l2_fh_init(&ctx->fh, video_devdata(file));
2448 file->private_data = &ctx->fh;
2449 v4l2_fh_add(&ctx->fh);
2450 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002451 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002452 switch (dev->devtype->product) {
2453 case CODA_7541:
2454 ctx->reg_idx = 0;
2455 break;
2456 default:
2457 ctx->reg_idx = idx;
2458 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002459
Fabio Estevam79830db2013-08-21 11:14:17 -03002460 ret = clk_prepare_enable(dev->clk_per);
2461 if (ret)
2462 goto err_clk_per;
2463
2464 ret = clk_prepare_enable(dev->clk_ahb);
2465 if (ret)
2466 goto err_clk_ahb;
2467
Javier Martin186b2502012-07-26 05:53:35 -03002468 set_default_params(ctx);
2469 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2470 &coda_queue_init);
2471 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03002472 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002473
2474 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2475 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002476 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002477 }
2478 ret = coda_ctrls_setup(ctx);
2479 if (ret) {
2480 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002481 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002482 }
2483
2484 ctx->fh.ctrl_handler = &ctx->ctrls;
2485
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002486 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2487 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002488 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002489 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002490 }
2491
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002492 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2493 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2494 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2495 if (!ctx->bitstream.vaddr) {
2496 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2497 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002498 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002499 }
2500 kfifo_init(&ctx->bitstream_fifo,
2501 ctx->bitstream.vaddr, ctx->bitstream.size);
2502 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002503 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002504
Javier Martin186b2502012-07-26 05:53:35 -03002505 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002506 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002507 coda_unlock(ctx);
2508
Javier Martin186b2502012-07-26 05:53:35 -03002509 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2510 ctx->idx, ctx);
2511
2512 return 0;
2513
Fabio Estevamf82bc202013-08-21 11:14:16 -03002514err_dma_writecombine:
2515 coda_free_context_buffers(ctx);
2516 if (ctx->dev->devtype->product == CODA_DX6)
2517 coda_free_aux_buf(dev, &ctx->workbuf);
2518 coda_free_aux_buf(dev, &ctx->parabuf);
2519err_dma_alloc:
2520 v4l2_ctrl_handler_free(&ctx->ctrls);
2521err_ctrls_setup:
2522 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2523err_ctx_init:
2524 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002525err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002526 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002527err_clk_per:
Javier Martin186b2502012-07-26 05:53:35 -03002528 v4l2_fh_del(&ctx->fh);
2529 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002530 clear_bit(ctx->idx, &dev->instance_mask);
2531err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002532 kfree(ctx);
2533 return ret;
2534}
2535
2536static int coda_release(struct file *file)
2537{
2538 struct coda_dev *dev = video_drvdata(file);
2539 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2540
2541 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2542 ctx);
2543
Philipp Zabel918c66f2013-06-27 06:59:01 -03002544 /* If this instance is running, call .job_abort and wait for it to end */
2545 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2546
2547 /* In case the instance was not running, we still need to call SEQ_END */
2548 mutex_lock(&dev->coda_mutex);
2549 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2550 "%s: sent command 'SEQ_END' to coda\n", __func__);
2551 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
2552 v4l2_err(&dev->v4l2_dev,
2553 "CODA_COMMAND_SEQ_END failed\n");
2554 mutex_unlock(&dev->coda_mutex);
2555 return -ETIMEDOUT;
2556 }
2557 mutex_unlock(&dev->coda_mutex);
2558
2559 coda_free_framebuffers(ctx);
2560
Javier Martin186b2502012-07-26 05:53:35 -03002561 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002562 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002563 coda_unlock(ctx);
2564
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002565 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2566 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002567 coda_free_context_buffers(ctx);
2568 if (ctx->dev->devtype->product == CODA_DX6)
2569 coda_free_aux_buf(dev, &ctx->workbuf);
2570
2571 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002572 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002573 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002574 clk_disable_unprepare(dev->clk_per);
Javier Martin186b2502012-07-26 05:53:35 -03002575 v4l2_fh_del(&ctx->fh);
2576 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002577 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002578 kfree(ctx);
2579
2580 return 0;
2581}
2582
2583static unsigned int coda_poll(struct file *file,
2584 struct poll_table_struct *wait)
2585{
2586 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2587 int ret;
2588
2589 coda_lock(ctx);
2590 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
2591 coda_unlock(ctx);
2592 return ret;
2593}
2594
2595static int coda_mmap(struct file *file, struct vm_area_struct *vma)
2596{
2597 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2598
2599 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2600}
2601
2602static const struct v4l2_file_operations coda_fops = {
2603 .owner = THIS_MODULE,
2604 .open = coda_open,
2605 .release = coda_release,
2606 .poll = coda_poll,
2607 .unlocked_ioctl = video_ioctl2,
2608 .mmap = coda_mmap,
2609};
2610
Philipp Zabel918c66f2013-06-27 06:59:01 -03002611static void coda_finish_decode(struct coda_ctx *ctx)
2612{
2613 struct coda_dev *dev = ctx->dev;
2614 struct coda_q_data *q_data_src;
2615 struct coda_q_data *q_data_dst;
2616 struct vb2_buffer *dst_buf;
2617 int width, height;
2618 int decoded_idx;
2619 int display_idx;
2620 u32 src_fourcc;
2621 int success;
2622 u32 val;
2623
2624 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2625
2626 /* Update kfifo out pointer from coda bitstream read pointer */
2627 coda_kfifo_sync_from_device(ctx);
2628
2629 /*
2630 * in stream-end mode, the read pointer can overshoot the write pointer
2631 * by up to 512 bytes
2632 */
2633 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2634 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2635 kfifo_init(&ctx->bitstream_fifo,
2636 ctx->bitstream.vaddr, ctx->bitstream.size);
2637 }
2638
2639 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2640 src_fourcc = q_data_src->fourcc;
2641
2642 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2643 if (val != 1)
2644 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2645
2646 success = val & 0x1;
2647 if (!success)
2648 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2649
2650 if (src_fourcc == V4L2_PIX_FMT_H264) {
2651 if (val & (1 << 3))
2652 v4l2_err(&dev->v4l2_dev,
2653 "insufficient PS buffer space (%d bytes)\n",
2654 ctx->psbuf.size);
2655 if (val & (1 << 2))
2656 v4l2_err(&dev->v4l2_dev,
2657 "insufficient slice buffer space (%d bytes)\n",
2658 ctx->slicebuf.size);
2659 }
2660
2661 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2662 width = (val >> 16) & 0xffff;
2663 height = val & 0xffff;
2664
2665 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2666
Philipp Zabel918c66f2013-06-27 06:59:01 -03002667 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2668 if (val > 0)
2669 v4l2_err(&dev->v4l2_dev,
2670 "errors in %d macroblocks\n", val);
2671
2672 if (dev->devtype->product == CODA_7541) {
2673 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2674 if (val == 0) {
2675 /* not enough bitstream data */
2676 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2677 "prescan failed: %d\n", val);
2678 ctx->prescan_failed = true;
2679 return;
2680 }
2681 }
2682
2683 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2684
2685 /*
2686 * The previous display frame was copied out by the rotator,
2687 * now it can be overwritten again
2688 */
2689 if (ctx->display_idx >= 0 &&
2690 ctx->display_idx < ctx->num_internal_frames) {
2691 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
2692 coda_write(dev, ctx->frm_dis_flg,
2693 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2694 }
2695
2696 /*
2697 * The index of the last decoded frame, not necessarily in
2698 * display order, and the index of the next display frame.
2699 * The latter could have been decoded in a previous run.
2700 */
2701 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
2702 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
2703
2704 if (decoded_idx == -1) {
2705 /* no frame was decoded, but we might have a display frame */
2706 if (display_idx < 0 && ctx->display_idx < 0)
2707 ctx->prescan_failed = true;
2708 } else if (decoded_idx == -2) {
2709 /* no frame was decoded, we still return the remaining buffers */
2710 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
2711 v4l2_err(&dev->v4l2_dev,
2712 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03002713 } else {
2714 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
2715 if (val == 0)
2716 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
2717 else if (val == 1)
2718 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
2719 else
2720 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002721 }
2722
2723 if (display_idx == -1) {
2724 /*
2725 * no more frames to be decoded, but there could still
2726 * be rotator output to dequeue
2727 */
2728 ctx->prescan_failed = true;
2729 } else if (display_idx == -3) {
2730 /* possibly prescan failure */
2731 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
2732 v4l2_err(&dev->v4l2_dev,
2733 "presentation frame index out of range: %d\n",
2734 display_idx);
2735 }
2736
2737 /* If a frame was copied out, return it */
2738 if (ctx->display_idx >= 0 &&
2739 ctx->display_idx < ctx->num_internal_frames) {
2740 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
2741 dst_buf->v4l2_buf.sequence = ctx->osequence++;
2742
Philipp Zabel1a8b3812014-07-11 06:36:12 -03002743 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
2744 V4L2_BUF_FLAG_PFRAME |
2745 V4L2_BUF_FLAG_BFRAME);
2746 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
2747
Philipp Zabel918c66f2013-06-27 06:59:01 -03002748 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
2749
2750 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
2751 VB2_BUF_STATE_ERROR);
2752
2753 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2754 "job finished: decoding frame (%d) (%s)\n",
2755 dst_buf->v4l2_buf.sequence,
2756 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2757 "KEYFRAME" : "PFRAME");
2758 } else {
2759 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2760 "job finished: no frame decoded\n");
2761 }
2762
2763 /* The rotator will copy the current display frame next time */
2764 ctx->display_idx = display_idx;
2765}
2766
2767static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03002768{
Philipp Zabelec25f682012-07-20 08:54:29 -03002769 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002770 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002771 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03002772
Philipp Zabelec25f682012-07-20 08:54:29 -03002773 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2774 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002775
2776 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03002777 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002778 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
2779
Javier Martin186b2502012-07-26 05:53:35 -03002780 /* Calculate bytesused field */
2781 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03002782 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
2783 ctx->vpu_header_size[0] +
2784 ctx->vpu_header_size[1] +
2785 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002786 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03002787 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03002788 }
2789
2790 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
2791 wr_ptr - start_ptr);
2792
2793 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
2794 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
2795
Philipp Zabel51660282013-09-30 10:34:49 -03002796 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002797 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
2798 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
2799 } else {
2800 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
2801 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
2802 }
2803
Kamil Debskiccd571c2013-04-24 10:38:24 -03002804 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03002805 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
2806 dst_buf->v4l2_buf.flags |=
2807 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03002808 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
2809
Philipp Zabelec25f682012-07-20 08:54:29 -03002810 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Javier Martin186b2502012-07-26 05:53:35 -03002811 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
2812
2813 ctx->gopcounter--;
2814 if (ctx->gopcounter < 0)
2815 ctx->gopcounter = ctx->params.gop_size - 1;
2816
2817 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2818 "job finished: encoding frame (%d) (%s)\n",
2819 dst_buf->v4l2_buf.sequence,
2820 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
2821 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002822}
2823
2824static irqreturn_t coda_irq_handler(int irq, void *data)
2825{
2826 struct coda_dev *dev = data;
2827 struct coda_ctx *ctx;
2828
2829 cancel_delayed_work(&dev->timeout);
2830
2831 /* read status register to attend the IRQ */
2832 coda_read(dev, CODA_REG_BIT_INT_STATUS);
2833 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
2834 CODA_REG_BIT_INT_CLEAR);
2835
2836 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2837 if (ctx == NULL) {
2838 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
2839 mutex_unlock(&dev->coda_mutex);
2840 return IRQ_HANDLED;
2841 }
2842
2843 if (ctx->aborting) {
2844 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2845 "task has been aborted\n");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002846 goto out;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03002847 }
2848
2849 if (coda_isbusy(ctx->dev)) {
2850 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2851 "coda is still busy!!!!\n");
2852 return IRQ_NONE;
2853 }
2854
Philipp Zabel918c66f2013-06-27 06:59:01 -03002855 if (ctx->inst_type == CODA_INST_DECODER)
2856 coda_finish_decode(ctx);
2857 else
2858 coda_finish_encode(ctx);
2859
2860out:
2861 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) {
2862 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2863 "%s: sent command 'SEQ_END' to coda\n", __func__);
2864 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
2865 v4l2_err(&dev->v4l2_dev,
2866 "CODA_COMMAND_SEQ_END failed\n");
2867 }
2868
2869 kfifo_init(&ctx->bitstream_fifo,
2870 ctx->bitstream.vaddr, ctx->bitstream.size);
2871
2872 coda_free_framebuffers(ctx);
2873 coda_free_context_buffers(ctx);
2874 }
Javier Martin186b2502012-07-26 05:53:35 -03002875
Philipp Zabelfcb62822013-05-23 10:43:00 -03002876 mutex_unlock(&dev->coda_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002877 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002878
Javier Martin186b2502012-07-26 05:53:35 -03002879 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
2880
2881 return IRQ_HANDLED;
2882}
2883
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002884static void coda_timeout(struct work_struct *work)
2885{
2886 struct coda_ctx *ctx;
2887 struct coda_dev *dev = container_of(to_delayed_work(work),
2888 struct coda_dev, timeout);
2889
Philipp Zabel39cc0292013-05-21 10:32:42 -03002890 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002891
2892 mutex_lock(&dev->dev_mutex);
2893 list_for_each_entry(ctx, &dev->instances, list) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002894 if (mutex_is_locked(&ctx->buffer_mutex))
2895 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002896 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2897 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2898 }
2899 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002900
2901 mutex_unlock(&dev->coda_mutex);
2902 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
2903 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002904}
2905
Javier Martin186b2502012-07-26 05:53:35 -03002906static u32 coda_supported_firmwares[] = {
2907 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002908 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Javier Martin186b2502012-07-26 05:53:35 -03002909};
2910
2911static bool coda_firmware_supported(u32 vernum)
2912{
2913 int i;
2914
2915 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
2916 if (vernum == coda_supported_firmwares[i])
2917 return true;
2918 return false;
2919}
2920
Philipp Zabel87048bb2012-07-02 07:03:43 -03002921static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03002922{
2923 u16 product, major, minor, release;
2924 u32 data;
2925 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03002926 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03002927
Fabio Estevam79830db2013-08-21 11:14:17 -03002928 ret = clk_prepare_enable(dev->clk_per);
2929 if (ret)
2930 return ret;
2931
2932 ret = clk_prepare_enable(dev->clk_ahb);
2933 if (ret)
2934 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03002935
Javier Martin186b2502012-07-26 05:53:35 -03002936 /*
2937 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03002938 * The 16-bit chars in the code buffer are in memory access
2939 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03002940 * Data in this SRAM survives a reboot.
2941 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03002942 p = (u16 *)dev->codebuf.vaddr;
2943 if (dev->devtype->product == CODA_DX6) {
2944 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2945 data = CODA_DOWN_ADDRESS_SET(i) |
2946 CODA_DOWN_DATA_SET(p[i ^ 1]);
2947 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2948 }
2949 } else {
2950 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
2951 data = CODA_DOWN_ADDRESS_SET(i) |
2952 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
2953 3 - (i % 4)]);
2954 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
2955 }
Javier Martin186b2502012-07-26 05:53:35 -03002956 }
Javier Martin186b2502012-07-26 05:53:35 -03002957
Philipp Zabel9acf7692013-05-23 10:42:56 -03002958 /* Clear registers */
2959 for (i = 0; i < 64; i++)
2960 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
2961
Javier Martin186b2502012-07-26 05:53:35 -03002962 /* Tell the BIT where to find everything it needs */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002963 if (dev->devtype->product == CODA_7541) {
2964 coda_write(dev, dev->tempbuf.paddr,
2965 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002966 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002967 } else {
2968 coda_write(dev, dev->workbuf.paddr,
2969 CODA_REG_BIT_WORK_BUF_ADDR);
2970 }
Javier Martin186b2502012-07-26 05:53:35 -03002971 coda_write(dev, dev->codebuf.paddr,
2972 CODA_REG_BIT_CODE_BUF_ADDR);
2973 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
2974
2975 /* Set default values */
2976 switch (dev->devtype->product) {
2977 case CODA_DX6:
2978 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2979 break;
2980 default:
2981 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
2982 }
2983 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03002984
2985 if (dev->devtype->product != CODA_DX6)
2986 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
2987
Javier Martin186b2502012-07-26 05:53:35 -03002988 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
2989 CODA_REG_BIT_INT_ENABLE);
2990
2991 /* Reset VPU and start processor */
2992 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
2993 data |= CODA_REG_RESET_ENABLE;
2994 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2995 udelay(10);
2996 data &= ~CODA_REG_RESET_ENABLE;
2997 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
2998 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
2999
3000 /* Load firmware */
3001 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3002 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3003 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3004 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3005 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3006 if (coda_wait_timeout(dev)) {
3007 clk_disable_unprepare(dev->clk_per);
3008 clk_disable_unprepare(dev->clk_ahb);
3009 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
3010 return -EIO;
3011 }
3012
3013 /* Check we are compatible with the loaded firmware */
3014 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3015 product = CODA_FIRMWARE_PRODUCT(data);
3016 major = CODA_FIRMWARE_MAJOR(data);
3017 minor = CODA_FIRMWARE_MINOR(data);
3018 release = CODA_FIRMWARE_RELEASE(data);
3019
3020 clk_disable_unprepare(dev->clk_per);
3021 clk_disable_unprepare(dev->clk_ahb);
3022
3023 if (product != dev->devtype->product) {
3024 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3025 " Version: %u.%u.%u\n",
3026 coda_product_name(dev->devtype->product),
3027 coda_product_name(product), major, minor, release);
3028 return -EINVAL;
3029 }
3030
3031 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3032 coda_product_name(product));
3033
3034 if (coda_firmware_supported(data)) {
3035 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3036 major, minor, release);
3037 } else {
3038 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3039 "%u.%u.%u\n", major, minor, release);
3040 }
3041
3042 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003043
3044err_clk_ahb:
3045 clk_disable_unprepare(dev->clk_per);
3046 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003047}
3048
3049static void coda_fw_callback(const struct firmware *fw, void *context)
3050{
3051 struct coda_dev *dev = context;
3052 struct platform_device *pdev = dev->plat_dev;
3053 int ret;
3054
3055 if (!fw) {
3056 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3057 return;
3058 }
3059
3060 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003061 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3062 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003063 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3064 return;
3065 }
3066
Philipp Zabel87048bb2012-07-02 07:03:43 -03003067 /* Copy the whole firmware image to the code buffer */
3068 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3069 release_firmware(fw);
3070
3071 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03003072 if (ret) {
3073 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3074 return;
3075 }
3076
3077 dev->vfd.fops = &coda_fops,
3078 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3079 dev->vfd.release = video_device_release_empty,
3080 dev->vfd.lock = &dev->dev_mutex;
3081 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003082 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003083 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3084 video_set_drvdata(&dev->vfd, dev);
3085
3086 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3087 if (IS_ERR(dev->alloc_ctx)) {
3088 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3089 return;
3090 }
3091
3092 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3093 if (IS_ERR(dev->m2m_dev)) {
3094 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3095 goto rel_ctx;
3096 }
3097
3098 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3099 if (ret) {
3100 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3101 goto rel_m2m;
3102 }
3103 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3104 dev->vfd.num);
3105
3106 return;
3107
3108rel_m2m:
3109 v4l2_m2m_release(dev->m2m_dev);
3110rel_ctx:
3111 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3112}
3113
3114static int coda_firmware_request(struct coda_dev *dev)
3115{
3116 char *fw = dev->devtype->firmware;
3117
3118 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3119 coda_product_name(dev->devtype->product));
3120
3121 return request_firmware_nowait(THIS_MODULE, true,
3122 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3123}
3124
3125enum coda_platform {
3126 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003127 CODA_IMX53,
Javier Martin186b2502012-07-26 05:53:35 -03003128};
3129
Emil Goodec06d8752012-08-14 17:44:42 -03003130static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003131 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003132 .firmware = "v4l-codadx6-imx27.bin",
3133 .product = CODA_DX6,
3134 .codecs = codadx6_codecs,
3135 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003136 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003137 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003138 .firmware = "v4l-coda7541-imx53.bin",
3139 .product = CODA_7541,
3140 .codecs = coda7_codecs,
3141 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003142 },
Javier Martin186b2502012-07-26 05:53:35 -03003143};
3144
3145static struct platform_device_id coda_platform_ids[] = {
3146 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003147 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003148 { /* sentinel */ }
3149};
3150MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3151
3152#ifdef CONFIG_OF
3153static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003154 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003155 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Javier Martin186b2502012-07-26 05:53:35 -03003156 { /* sentinel */ }
3157};
3158MODULE_DEVICE_TABLE(of, coda_dt_ids);
3159#endif
3160
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003161static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003162{
3163 const struct of_device_id *of_id =
3164 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3165 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003166 struct coda_platform_data *pdata = pdev->dev.platform_data;
3167 struct device_node *np = pdev->dev.of_node;
3168 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003169 struct coda_dev *dev;
3170 struct resource *res;
3171 int ret, irq;
3172
3173 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3174 if (!dev) {
3175 dev_err(&pdev->dev, "Not enough memory for %s\n",
3176 CODA_NAME);
3177 return -ENOMEM;
3178 }
3179
3180 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003181 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003182 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03003183
3184 dev->plat_dev = pdev;
3185 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3186 if (IS_ERR(dev->clk_per)) {
3187 dev_err(&pdev->dev, "Could not get per clock\n");
3188 return PTR_ERR(dev->clk_per);
3189 }
3190
3191 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3192 if (IS_ERR(dev->clk_ahb)) {
3193 dev_err(&pdev->dev, "Could not get ahb clock\n");
3194 return PTR_ERR(dev->clk_ahb);
3195 }
3196
3197 /* Get memory for physical registers */
3198 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003199 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3200 if (IS_ERR(dev->regs_base))
3201 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003202
3203 /* IRQ */
3204 irq = platform_get_irq(pdev, 0);
3205 if (irq < 0) {
3206 dev_err(&pdev->dev, "failed to get irq resource\n");
3207 return -ENOENT;
3208 }
3209
Philipp Zabel918c66f2013-06-27 06:59:01 -03003210 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003211 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003212 dev_err(&pdev->dev, "failed to request irq\n");
3213 return -ENOENT;
3214 }
3215
Philipp Zabel657eee72013-04-29 16:17:14 -07003216 /* Get IRAM pool from device tree or platform data */
3217 pool = of_get_named_gen_pool(np, "iram", 0);
3218 if (!pool && pdata)
3219 pool = dev_get_gen_pool(pdata->iram_dev);
3220 if (!pool) {
3221 dev_err(&pdev->dev, "iram pool not available\n");
3222 return -ENOMEM;
3223 }
3224 dev->iram_pool = pool;
3225
Javier Martin186b2502012-07-26 05:53:35 -03003226 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3227 if (ret)
3228 return ret;
3229
3230 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003231 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003232
3233 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3234
3235 if (of_id) {
3236 dev->devtype = of_id->data;
3237 } else if (pdev_id) {
3238 dev->devtype = &coda_devdata[pdev_id->driver_data];
3239 } else {
3240 v4l2_device_unregister(&dev->v4l2_dev);
3241 return -EINVAL;
3242 }
3243
3244 /* allocate auxiliary per-device buffers for the BIT processor */
3245 switch (dev->devtype->product) {
3246 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003247 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3248 CODADX6_WORK_BUF_SIZE);
3249 if (ret < 0) {
3250 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3251 v4l2_device_unregister(&dev->v4l2_dev);
3252 return ret;
3253 }
Javier Martin186b2502012-07-26 05:53:35 -03003254 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003255 case CODA_7541:
3256 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3257 break;
Javier Martin186b2502012-07-26 05:53:35 -03003258 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003259 if (dev->tempbuf.size) {
3260 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3261 dev->tempbuf.size);
3262 if (ret < 0) {
3263 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3264 v4l2_device_unregister(&dev->v4l2_dev);
3265 return ret;
3266 }
Javier Martin186b2502012-07-26 05:53:35 -03003267 }
3268
Philipp Zabel918c66f2013-06-27 06:59:01 -03003269 switch (dev->devtype->product) {
3270 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003271 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003272 break;
3273 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003274 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003275 break;
3276 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003277 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3278 &dev->iram.paddr);
3279 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003280 dev_err(&pdev->dev, "unable to alloc iram\n");
3281 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003282 }
3283
Javier Martin186b2502012-07-26 05:53:35 -03003284 platform_set_drvdata(pdev, dev);
3285
3286 return coda_firmware_request(dev);
3287}
3288
3289static int coda_remove(struct platform_device *pdev)
3290{
3291 struct coda_dev *dev = platform_get_drvdata(pdev);
3292
3293 video_unregister_device(&dev->vfd);
3294 if (dev->m2m_dev)
3295 v4l2_m2m_release(dev->m2m_dev);
3296 if (dev->alloc_ctx)
3297 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3298 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003299 if (dev->iram.vaddr)
3300 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3301 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003302 coda_free_aux_buf(dev, &dev->codebuf);
3303 coda_free_aux_buf(dev, &dev->tempbuf);
3304 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003305 return 0;
3306}
3307
3308static struct platform_driver coda_driver = {
3309 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003310 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003311 .driver = {
3312 .name = CODA_NAME,
3313 .owner = THIS_MODULE,
3314 .of_match_table = of_match_ptr(coda_dt_ids),
3315 },
3316 .id_table = coda_platform_ids,
3317};
3318
3319module_platform_driver(coda_driver);
3320
3321MODULE_LICENSE("GPL");
3322MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3323MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");