blob: 681170342a43ac61c6e002938623e167679c2d45 [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)
Philipp Zabel89548442014-07-11 06:36:17 -030047#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030048#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030049#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030050#define CODA_PARA_BUF_SIZE (10 * 1024)
51#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070052#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030053#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030054#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030055
Philipp Zabel918c66f2013-06-27 06:59:01 -030056#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030057#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030058
59#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030060
Philipp Zabelb96904e2013-05-23 10:42:58 -030061#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030062#define FMO_SLICE_SAVE_BUF_SIZE (32)
63#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030064#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030065
66#define MIN_W 176
67#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030068
69#define S_ALIGN 1 /* multiple of 2 */
70#define W_ALIGN 1 /* multiple of 2 */
71#define H_ALIGN 1 /* multiple of 2 */
72
73#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
74
75static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030076module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030077MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
78
79enum {
80 V4L2_M2M_SRC = 0,
81 V4L2_M2M_DST = 1,
82};
83
Javier Martin186b2502012-07-26 05:53:35 -030084enum coda_inst_type {
85 CODA_INST_ENCODER,
86 CODA_INST_DECODER,
87};
88
89enum coda_product {
90 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030091 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030092 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030093};
94
95struct coda_fmt {
96 char *name;
97 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030098};
99
100struct coda_codec {
101 u32 mode;
102 u32 src_fourcc;
103 u32 dst_fourcc;
104 u32 max_w;
105 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300106};
107
108struct coda_devtype {
109 char *firmware;
110 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300111 struct coda_codec *codecs;
112 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300113 size_t workbuf_size;
114};
115
116/* Per-queue, driver-specific private data */
117struct coda_q_data {
118 unsigned int width;
119 unsigned int height;
120 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300121 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300122 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300123};
124
125struct coda_aux_buf {
126 void *vaddr;
127 dma_addr_t paddr;
128 u32 size;
129};
130
131struct coda_dev {
132 struct v4l2_device v4l2_dev;
133 struct video_device vfd;
134 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300135 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300136
137 void __iomem *regs_base;
138 struct clk *clk_per;
139 struct clk *clk_ahb;
140
141 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300142 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300143 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700144 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300145 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300146
147 spinlock_t irqlock;
148 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300149 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300150 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300151 struct v4l2_m2m_dev *m2m_dev;
152 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300153 struct list_head instances;
154 unsigned long instance_mask;
Javier Martin186b2502012-07-26 05:53:35 -0300155};
156
157struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300158 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300159 u8 h264_intra_qp;
160 u8 h264_inter_qp;
161 u8 mpeg4_intra_qp;
162 u8 mpeg4_inter_qp;
163 u8 gop_size;
164 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300165 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300166 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
167 u32 framerate;
168 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300169 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300170 u32 slice_max_mb;
171};
172
Philipp Zabelc2d22512013-06-21 03:55:28 -0300173struct coda_iram_info {
174 u32 axi_sram_use;
175 phys_addr_t buf_bit_use;
176 phys_addr_t buf_ip_ac_dc_use;
177 phys_addr_t buf_dbk_y_use;
178 phys_addr_t buf_dbk_c_use;
179 phys_addr_t buf_ovl_use;
180 phys_addr_t buf_btp_use;
181 phys_addr_t search_ram_paddr;
182 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300183 int remaining;
184 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300185};
186
Philipp Zabel89548442014-07-11 06:36:17 -0300187struct gdi_tiled_map {
188 int xy2ca_map[16];
189 int xy2ba_map[16];
190 int xy2ra_map[16];
191 int rbc2axi_map[32];
192 int xy2rbc_config;
193 int map_type;
194#define GDI_LINEAR_FRAME_MAP 0
195};
196
Javier Martin186b2502012-07-26 05:53:35 -0300197struct coda_ctx {
198 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300199 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300200 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300201 struct work_struct pic_run_work;
202 struct work_struct seq_end_work;
203 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300204 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300205 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300206 int streamon_out;
207 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300208 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300209 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300210 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300211 struct coda_q_data q_data[2];
212 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300213 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300214 enum v4l2_colorspace colorspace;
215 struct coda_params params;
216 struct v4l2_m2m_ctx *m2m_ctx;
217 struct v4l2_ctrl_handler ctrls;
218 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300219 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300220 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300221 char vpu_header[3][64];
222 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300223 struct kfifo bitstream_fifo;
224 struct mutex bitstream_mutex;
225 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300226 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300227 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300228 struct coda_aux_buf psbuf;
229 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300230 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300231 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300232 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300233 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300234 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300235 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300236 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300237 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300238 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300239 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300240 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300241 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300242};
243
Javier Martin832fbb52012-10-29 08:34:59 -0300244static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
246static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300247
Javier Martin186b2502012-07-26 05:53:35 -0300248static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
249{
250 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
251 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
252 writel(data, dev->regs_base + reg);
253}
254
255static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
256{
257 u32 data;
258 data = readl(dev->regs_base + reg);
259 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
260 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
261 return data;
262}
263
264static inline unsigned long coda_isbusy(struct coda_dev *dev)
265{
266 return coda_read(dev, CODA_REG_BIT_BUSY);
267}
268
269static inline int coda_is_initialized(struct coda_dev *dev)
270{
271 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
272}
273
274static int coda_wait_timeout(struct coda_dev *dev)
275{
276 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
277
278 while (coda_isbusy(dev)) {
279 if (time_after(jiffies, timeout))
280 return -ETIMEDOUT;
281 }
282 return 0;
283}
284
285static void coda_command_async(struct coda_ctx *ctx, int cmd)
286{
287 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300288
Philipp Zabel89548442014-07-11 06:36:17 -0300289 if (dev->devtype->product == CODA_960 ||
290 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300291 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300292 coda_write(dev, ctx->bit_stream_param,
293 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300294 coda_write(dev, ctx->frm_dis_flg,
295 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300296 coda_write(dev, ctx->frame_mem_ctrl,
297 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300298 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
299 }
300
Philipp Zabel89548442014-07-11 06:36:17 -0300301 if (dev->devtype->product == CODA_960) {
302 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
303 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
304 }
305
Javier Martin186b2502012-07-26 05:53:35 -0300306 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
307
308 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
309 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300310 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
311
Javier Martin186b2502012-07-26 05:53:35 -0300312 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
313}
314
315static int coda_command_sync(struct coda_ctx *ctx, int cmd)
316{
317 struct coda_dev *dev = ctx->dev;
318
319 coda_command_async(ctx, cmd);
320 return coda_wait_timeout(dev);
321}
322
323static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
324 enum v4l2_buf_type type)
325{
326 switch (type) {
327 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
328 return &(ctx->q_data[V4L2_M2M_SRC]);
329 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
330 return &(ctx->q_data[V4L2_M2M_DST]);
331 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300332 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300333 }
Javier Martin186b2502012-07-26 05:53:35 -0300334}
335
336/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300337 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300338 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300339static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300340 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300341 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300342 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300343 },
344 {
345 .name = "YUV 4:2:0 Planar, YCrCb",
346 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300347 },
348 {
349 .name = "H264 Encoded Stream",
350 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300351 },
352 {
353 .name = "MPEG4 Encoded Stream",
354 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300355 },
356};
357
Philipp Zabelb96904e2013-05-23 10:42:58 -0300358#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
359 { mode, src_fourcc, dst_fourcc, max_w, max_h }
360
361/*
362 * Arrays of codecs supported by each given version of Coda:
363 * i.MX27 -> codadx6
364 * i.MX5x -> coda7
365 * i.MX6 -> coda960
366 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
367 */
368static struct coda_codec codadx6_codecs[] = {
369 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
370 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300371};
372
Philipp Zabelb96904e2013-05-23 10:42:58 -0300373static struct coda_codec coda7_codecs[] = {
374 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
375 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300376 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
377 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300378};
379
Philipp Zabel89548442014-07-11 06:36:17 -0300380static struct coda_codec coda9_codecs[] = {
381 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
382 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
383 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
384 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
385};
386
Philipp Zabelb96904e2013-05-23 10:42:58 -0300387static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300388{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300389 switch (fourcc) {
390 case V4L2_PIX_FMT_YUV420:
391 case V4L2_PIX_FMT_YVU420:
392 return true;
393 default:
394 return false;
395 }
396}
Javier Martin186b2502012-07-26 05:53:35 -0300397
Philipp Zabelb96904e2013-05-23 10:42:58 -0300398/*
399 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
400 * tables.
401 */
402static u32 coda_format_normalize_yuv(u32 fourcc)
403{
404 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
405}
406
407static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
408 int dst_fourcc)
409{
410 struct coda_codec *codecs = dev->devtype->codecs;
411 int num_codecs = dev->devtype->num_codecs;
412 int k;
413
414 src_fourcc = coda_format_normalize_yuv(src_fourcc);
415 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
416 if (src_fourcc == dst_fourcc)
417 return NULL;
418
419 for (k = 0; k < num_codecs; k++) {
420 if (codecs[k].src_fourcc == src_fourcc &&
421 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300422 break;
423 }
424
Philipp Zabelb96904e2013-05-23 10:42:58 -0300425 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300426 return NULL;
427
Philipp Zabelb96904e2013-05-23 10:42:58 -0300428 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300429}
430
Philipp Zabel57625592013-09-30 10:34:51 -0300431static void coda_get_max_dimensions(struct coda_dev *dev,
432 struct coda_codec *codec,
433 int *max_w, int *max_h)
434{
435 struct coda_codec *codecs = dev->devtype->codecs;
436 int num_codecs = dev->devtype->num_codecs;
437 unsigned int w, h;
438 int k;
439
440 if (codec) {
441 w = codec->max_w;
442 h = codec->max_h;
443 } else {
444 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
445 w = max(w, codecs[k].max_w);
446 h = max(h, codecs[k].max_h);
447 }
448 }
449
450 if (max_w)
451 *max_w = w;
452 if (max_h)
453 *max_h = h;
454}
455
Philipp Zabel927933f2013-09-30 10:34:48 -0300456static char *coda_product_name(int product)
457{
458 static char buf[9];
459
460 switch (product) {
461 case CODA_DX6:
462 return "CodaDx6";
463 case CODA_7541:
464 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300465 case CODA_960:
466 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300467 default:
468 snprintf(buf, sizeof(buf), "(0x%04x)", product);
469 return buf;
470 }
471}
472
Javier Martin186b2502012-07-26 05:53:35 -0300473/*
474 * V4L2 ioctl() operations.
475 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300476static int coda_querycap(struct file *file, void *priv,
477 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300478{
Philipp Zabel927933f2013-09-30 10:34:48 -0300479 struct coda_ctx *ctx = fh_to_ctx(priv);
480
Javier Martin186b2502012-07-26 05:53:35 -0300481 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300482 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
483 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300484 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300485 /*
486 * This is only a mem-to-mem video device. The capture and output
487 * device capability flags are left only for backward compatibility
488 * and are scheduled for removal.
489 */
490 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
491 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300492 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
493
494 return 0;
495}
496
497static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300498 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300499{
500 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300501 struct coda_codec *codecs = ctx->dev->devtype->codecs;
502 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300503 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300504 int num_codecs = ctx->dev->devtype->num_codecs;
505 int num_formats = ARRAY_SIZE(coda_formats);
506 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300507
508 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300509 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300510 if (coda_format_is_yuv(formats[i].fourcc) &&
511 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300512 if (num == f->index)
513 break;
514 ++num;
515 continue;
516 }
517 /* Compressed formats may be supported, check the codec list */
518 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300519 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300520 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300521 formats[i].fourcc == codecs[k].dst_fourcc &&
522 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300523 break;
524 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
525 formats[i].fourcc == codecs[k].src_fourcc)
526 break;
527 }
528 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300529 if (num == f->index)
530 break;
531 ++num;
532 }
533 }
534
535 if (i < num_formats) {
536 fmt = &formats[i];
537 strlcpy(f->description, fmt->name, sizeof(f->description));
538 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300539 if (!coda_format_is_yuv(fmt->fourcc))
540 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300541 return 0;
542 }
543
544 /* Format not found */
545 return -EINVAL;
546}
547
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300548static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
549 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300550{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300551 struct coda_ctx *ctx = fh_to_ctx(priv);
552 struct vb2_queue *src_vq;
553 struct coda_q_data *q_data_src;
554
555 /* If the source format is already fixed, only list matching formats */
556 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
557 if (vb2_is_streaming(src_vq)) {
558 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
559
560 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
561 q_data_src->fourcc);
562 }
563
564 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300565}
566
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300567static int coda_enum_fmt_vid_out(struct file *file, void *priv,
568 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300569{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300570 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300571}
572
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300573static int coda_g_fmt(struct file *file, void *priv,
574 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300575{
Javier Martin186b2502012-07-26 05:53:35 -0300576 struct coda_q_data *q_data;
577 struct coda_ctx *ctx = fh_to_ctx(priv);
578
Javier Martin186b2502012-07-26 05:53:35 -0300579 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300580 if (!q_data)
581 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300582
583 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300584 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300585 f->fmt.pix.width = q_data->width;
586 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300587 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300588 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
589 else /* encoded formats h.264/mpeg4 */
590 f->fmt.pix.bytesperline = 0;
591
592 f->fmt.pix.sizeimage = q_data->sizeimage;
593 f->fmt.pix.colorspace = ctx->colorspace;
594
595 return 0;
596}
597
Philipp Zabel57625592013-09-30 10:34:51 -0300598static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
599 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300600{
Philipp Zabel57625592013-09-30 10:34:51 -0300601 struct coda_dev *dev = ctx->dev;
602 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300603 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300604 enum v4l2_field field;
605
606 field = f->fmt.pix.field;
607 if (field == V4L2_FIELD_ANY)
608 field = V4L2_FIELD_NONE;
609 else if (V4L2_FIELD_NONE != field)
610 return -EINVAL;
611
612 /* V4L2 specification suggests the driver corrects the format struct
613 * if any of the dimensions is unsupported */
614 f->fmt.pix.field = field;
615
Philipp Zabel57625592013-09-30 10:34:51 -0300616 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
617 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
618 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
619 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300620
Philipp Zabel57625592013-09-30 10:34:51 -0300621 switch (f->fmt.pix.pixelformat) {
622 case V4L2_PIX_FMT_YUV420:
623 case V4L2_PIX_FMT_YVU420:
624 case V4L2_PIX_FMT_H264:
625 case V4L2_PIX_FMT_MPEG4:
626 case V4L2_PIX_FMT_JPEG:
627 break;
628 default:
629 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300630 if (!q_data)
631 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300632 f->fmt.pix.pixelformat = q_data->fourcc;
633 }
634
635 switch (f->fmt.pix.pixelformat) {
636 case V4L2_PIX_FMT_YUV420:
637 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300638 /* Frame stride must be multiple of 8 */
639 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
640 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300641 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300642 break;
643 case V4L2_PIX_FMT_H264:
644 case V4L2_PIX_FMT_MPEG4:
645 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300646 f->fmt.pix.bytesperline = 0;
647 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300648 break;
649 default:
650 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300651 }
652
653 return 0;
654}
655
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300656static int coda_try_fmt_vid_cap(struct file *file, void *priv,
657 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300658{
Javier Martin186b2502012-07-26 05:53:35 -0300659 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300660 struct coda_codec *codec;
661 struct vb2_queue *src_vq;
662 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300663
Philipp Zabel918c66f2013-06-27 06:59:01 -0300664 /*
665 * If the source format is already fixed, try to find a codec that
666 * converts to the given destination format
667 */
668 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
669 if (vb2_is_streaming(src_vq)) {
670 struct coda_q_data *q_data_src;
671
672 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
673 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
674 f->fmt.pix.pixelformat);
675 if (!codec)
676 return -EINVAL;
677 } else {
678 /* Otherwise determine codec by encoded format, if possible */
679 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
680 f->fmt.pix.pixelformat);
681 }
Javier Martin186b2502012-07-26 05:53:35 -0300682
683 f->fmt.pix.colorspace = ctx->colorspace;
684
Philipp Zabel57625592013-09-30 10:34:51 -0300685 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300686 if (ret < 0)
687 return ret;
688
689 /* The h.264 decoder only returns complete 16x16 macroblocks */
690 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
691 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
692 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
693 f->fmt.pix.bytesperline = f->fmt.pix.width;
694 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
695 f->fmt.pix.height * 3 / 2;
696 }
697
698 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300699}
700
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300701static int coda_try_fmt_vid_out(struct file *file, void *priv,
702 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300703{
704 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300705 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300706
Philipp Zabelb96904e2013-05-23 10:42:58 -0300707 /* Determine codec by encoded format, returns NULL if raw or invalid */
708 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
709 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300710
711 if (!f->fmt.pix.colorspace)
712 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
713
Philipp Zabel57625592013-09-30 10:34:51 -0300714 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300715}
716
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300717static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300718{
719 struct coda_q_data *q_data;
720 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300721
722 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
723 if (!vq)
724 return -EINVAL;
725
726 q_data = get_q_data(ctx, f->type);
727 if (!q_data)
728 return -EINVAL;
729
730 if (vb2_is_busy(vq)) {
731 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
732 return -EBUSY;
733 }
734
Philipp Zabelb96904e2013-05-23 10:42:58 -0300735 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300736 q_data->width = f->fmt.pix.width;
737 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300738 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300739 q_data->rect.left = 0;
740 q_data->rect.top = 0;
741 q_data->rect.width = f->fmt.pix.width;
742 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300743
744 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
745 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300746 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300747
748 return 0;
749}
750
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300751static int coda_s_fmt_vid_cap(struct file *file, void *priv,
752 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300753{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300754 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300755 int ret;
756
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300757 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300758 if (ret)
759 return ret;
760
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300761 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300762}
763
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300764static int coda_s_fmt_vid_out(struct file *file, void *priv,
765 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300766{
767 struct coda_ctx *ctx = fh_to_ctx(priv);
768 int ret;
769
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300770 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300771 if (ret)
772 return ret;
773
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300774 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300775 if (ret)
776 ctx->colorspace = f->fmt.pix.colorspace;
777
778 return ret;
779}
780
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300781static int coda_qbuf(struct file *file, void *priv,
782 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300783{
784 struct coda_ctx *ctx = fh_to_ctx(priv);
785
786 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
787}
788
Philipp Zabel918c66f2013-06-27 06:59:01 -0300789static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
790 struct v4l2_buffer *buf)
791{
792 struct vb2_queue *src_vq;
793
794 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
795
796 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
797 (buf->sequence == (ctx->qsequence - 1)));
798}
799
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300800static int coda_dqbuf(struct file *file, void *priv,
801 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300802{
803 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300804 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300805
Philipp Zabel918c66f2013-06-27 06:59:01 -0300806 ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
807
808 /* If this is the last capture buffer, emit an end-of-stream event */
809 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
810 coda_buf_is_end_of_stream(ctx, buf)) {
811 const struct v4l2_event eos_event = {
812 .type = V4L2_EVENT_EOS
813 };
814
815 v4l2_event_queue_fh(&ctx->fh, &eos_event);
816 }
817
818 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300819}
820
Philipp Zabel52c41672014-07-11 06:36:19 -0300821static int coda_g_selection(struct file *file, void *fh,
822 struct v4l2_selection *s)
823{
824 struct coda_ctx *ctx = fh_to_ctx(fh);
825 struct coda_q_data *q_data;
826 struct v4l2_rect r, *rsel;
827
828 q_data = get_q_data(ctx, s->type);
829 if (!q_data)
830 return -EINVAL;
831
832 r.left = 0;
833 r.top = 0;
834 r.width = q_data->width;
835 r.height = q_data->height;
836 rsel = &q_data->rect;
837
838 switch (s->target) {
839 case V4L2_SEL_TGT_CROP_DEFAULT:
840 case V4L2_SEL_TGT_CROP_BOUNDS:
841 rsel = &r;
842 /* fallthrough */
843 case V4L2_SEL_TGT_CROP:
844 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
845 return -EINVAL;
846 break;
847 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
848 case V4L2_SEL_TGT_COMPOSE_PADDED:
849 rsel = &r;
850 /* fallthrough */
851 case V4L2_SEL_TGT_COMPOSE:
852 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
853 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
854 return -EINVAL;
855 break;
856 default:
857 return -EINVAL;
858 }
859
860 s->r = *rsel;
861
862 return 0;
863}
864
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300865static int coda_try_decoder_cmd(struct file *file, void *fh,
866 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300867{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300868 if (dc->cmd != V4L2_DEC_CMD_STOP)
869 return -EINVAL;
870
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300871 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300872 return -EINVAL;
873
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300874 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300875 return -EINVAL;
876
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300877 return 0;
878}
879
880static int coda_decoder_cmd(struct file *file, void *fh,
881 struct v4l2_decoder_cmd *dc)
882{
883 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300884 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300885 int ret;
886
887 ret = coda_try_decoder_cmd(file, fh, dc);
888 if (ret < 0)
889 return ret;
890
891 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300892 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300893 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300894
895 /* Set the strem-end flag on this context */
896 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
897
Philipp Zabel89548442014-07-11 06:36:17 -0300898 if ((dev->devtype->product == CODA_960) &&
899 coda_isbusy(dev) &&
900 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
901 /* If this context is currently running, update the hardware flag */
902 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
903 }
904
Philipp Zabel918c66f2013-06-27 06:59:01 -0300905 return 0;
906}
907
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300908static int coda_subscribe_event(struct v4l2_fh *fh,
909 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300910{
911 switch (sub->type) {
912 case V4L2_EVENT_EOS:
913 return v4l2_event_subscribe(fh, sub, 0, NULL);
914 default:
915 return v4l2_ctrl_subscribe_event(fh, sub);
916 }
Javier Martin186b2502012-07-26 05:53:35 -0300917}
918
919static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300920 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300921
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300922 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
923 .vidioc_g_fmt_vid_cap = coda_g_fmt,
924 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
925 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300926
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300927 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
928 .vidioc_g_fmt_vid_out = coda_g_fmt,
929 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
930 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300931
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300932 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
933 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300934
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300935 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300936 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300937 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300938 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300939
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300940 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
941 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300942
Philipp Zabel52c41672014-07-11 06:36:19 -0300943 .vidioc_g_selection = coda_g_selection,
944
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300945 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300946 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300947
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300948 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300949 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300950};
951
Philipp Zabel918c66f2013-06-27 06:59:01 -0300952static int coda_start_decoding(struct coda_ctx *ctx);
953
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300954static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
955{
956 return kfifo_len(&ctx->bitstream_fifo);
957}
958
959static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
960{
961 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
962 struct coda_dev *dev = ctx->dev;
963 u32 rd_ptr;
964
965 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
966 kfifo->out = (kfifo->in & ~kfifo->mask) |
967 (rd_ptr - ctx->bitstream.paddr);
968 if (kfifo->out > kfifo->in)
969 kfifo->out -= kfifo->mask + 1;
970}
971
972static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
973{
974 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
975 struct coda_dev *dev = ctx->dev;
976 u32 rd_ptr, wr_ptr;
977
978 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
979 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
980 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
981 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
982}
983
984static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
985{
986 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
987 struct coda_dev *dev = ctx->dev;
988 u32 wr_ptr;
989
990 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
991 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
992}
993
994static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
995{
996 u32 src_size = vb2_get_plane_payload(src_buf, 0);
997 u32 n;
998
999 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1000 if (n < src_size)
1001 return -ENOSPC;
1002
1003 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1004 ctx->bitstream.size, DMA_TO_DEVICE);
1005
1006 ctx->qsequence++;
1007
1008 return 0;
1009}
1010
1011static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1012 struct vb2_buffer *src_buf)
1013{
1014 int ret;
1015
1016 if (coda_get_bitstream_payload(ctx) +
1017 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1018 return false;
1019
1020 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1021 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1022 return true;
1023 }
1024
1025 ret = coda_bitstream_queue(ctx, src_buf);
1026 if (ret < 0) {
1027 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1028 return false;
1029 }
1030 /* Sync read pointer to device */
1031 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1032 coda_kfifo_sync_to_device_write(ctx);
1033
Philipp Zabel918c66f2013-06-27 06:59:01 -03001034 ctx->prescan_failed = false;
1035
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001036 return true;
1037}
1038
1039static void coda_fill_bitstream(struct coda_ctx *ctx)
1040{
1041 struct vb2_buffer *src_buf;
1042
1043 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
1044 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1045
1046 if (coda_bitstream_try_queue(ctx, src_buf)) {
1047 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1048 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1049 } else {
1050 break;
1051 }
1052 }
1053}
1054
Philipp Zabel89548442014-07-11 06:36:17 -03001055static void coda_set_gdi_regs(struct coda_ctx *ctx)
1056{
1057 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1058 struct coda_dev *dev = ctx->dev;
1059 int i;
1060
1061 for (i = 0; i < 16; i++)
1062 coda_write(dev, tiled_map->xy2ca_map[i],
1063 CODA9_GDI_XY2_CAS_0 + 4 * i);
1064 for (i = 0; i < 4; i++)
1065 coda_write(dev, tiled_map->xy2ba_map[i],
1066 CODA9_GDI_XY2_BA_0 + 4 * i);
1067 for (i = 0; i < 16; i++)
1068 coda_write(dev, tiled_map->xy2ra_map[i],
1069 CODA9_GDI_XY2_RAS_0 + 4 * i);
1070 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1071 for (i = 0; i < 32; i++)
1072 coda_write(dev, tiled_map->rbc2axi_map[i],
1073 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1074}
1075
Javier Martin186b2502012-07-26 05:53:35 -03001076/*
1077 * Mem-to-mem operations.
1078 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001079static int coda_prepare_decode(struct coda_ctx *ctx)
1080{
1081 struct vb2_buffer *dst_buf;
1082 struct coda_dev *dev = ctx->dev;
1083 struct coda_q_data *q_data_dst;
1084 u32 stridey, height;
1085 u32 picture_y, picture_cb, picture_cr;
1086
1087 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1088 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1089
1090 if (ctx->params.rot_mode & CODA_ROT_90) {
1091 stridey = q_data_dst->height;
1092 height = q_data_dst->width;
1093 } else {
1094 stridey = q_data_dst->width;
1095 height = q_data_dst->height;
1096 }
1097
1098 /* Try to copy source buffer contents into the bitstream ringbuffer */
1099 mutex_lock(&ctx->bitstream_mutex);
1100 coda_fill_bitstream(ctx);
1101 mutex_unlock(&ctx->bitstream_mutex);
1102
1103 if (coda_get_bitstream_payload(ctx) < 512 &&
1104 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1105 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1106 "bitstream payload: %d, skipping\n",
1107 coda_get_bitstream_payload(ctx));
Philipp Zabel32b6f202014-07-11 06:36:20 -03001108 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001109 return -EAGAIN;
1110 }
1111
1112 /* Run coda_start_decoding (again) if not yet initialized */
1113 if (!ctx->initialized) {
1114 int ret = coda_start_decoding(ctx);
1115 if (ret < 0) {
1116 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel32b6f202014-07-11 06:36:20 -03001117 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001118 return -EAGAIN;
1119 } else {
1120 ctx->initialized = 1;
1121 }
1122 }
1123
Philipp Zabel89548442014-07-11 06:36:17 -03001124 if (dev->devtype->product == CODA_960)
1125 coda_set_gdi_regs(ctx);
1126
Philipp Zabel918c66f2013-06-27 06:59:01 -03001127 /* Set rotator output */
1128 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1129 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1130 /* Switch Cr and Cb for YVU420 format */
1131 picture_cr = picture_y + stridey * height;
1132 picture_cb = picture_cr + stridey / 2 * height / 2;
1133 } else {
1134 picture_cb = picture_y + stridey * height;
1135 picture_cr = picture_cb + stridey / 2 * height / 2;
1136 }
Philipp Zabel89548442014-07-11 06:36:17 -03001137
1138 if (dev->devtype->product == CODA_960) {
1139 /*
1140 * The CODA960 seems to have an internal list of buffers with
1141 * 64 entries that includes the registered frame buffers as
1142 * well as the rotator buffer output.
1143 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1144 */
1145 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1146 CODA9_CMD_DEC_PIC_ROT_INDEX);
1147 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1148 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1149 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1150 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1151 } else {
1152 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1153 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1154 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1155 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1156 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001157 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1158 CODA_CMD_DEC_PIC_ROT_MODE);
1159
1160 switch (dev->devtype->product) {
1161 case CODA_DX6:
1162 /* TBD */
1163 case CODA_7541:
1164 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1165 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001166 case CODA_960:
1167 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1168 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001169 }
1170
1171 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1172
1173 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1174 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1175
1176 return 0;
1177}
1178
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001179static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001180{
Javier Martin186b2502012-07-26 05:53:35 -03001181 struct coda_q_data *q_data_src, *q_data_dst;
1182 struct vb2_buffer *src_buf, *dst_buf;
1183 struct coda_dev *dev = ctx->dev;
1184 int force_ipicture;
1185 int quant_param = 0;
1186 u32 picture_y, picture_cb, picture_cr;
1187 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1188 u32 dst_fourcc;
1189
1190 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1191 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1192 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1193 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001194 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001195
Philipp Zabel918c66f2013-06-27 06:59:01 -03001196 src_buf->v4l2_buf.sequence = ctx->osequence;
1197 dst_buf->v4l2_buf.sequence = ctx->osequence;
1198 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001199
1200 /*
1201 * Workaround coda firmware BUG that only marks the first
1202 * frame as IDR. This is a problem for some decoders that can't
1203 * recover when a frame is lost.
1204 */
1205 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1206 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1207 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1208 } else {
1209 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1210 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1211 }
1212
Philipp Zabel89548442014-07-11 06:36:17 -03001213 if (dev->devtype->product == CODA_960)
1214 coda_set_gdi_regs(ctx);
1215
Javier Martin186b2502012-07-26 05:53:35 -03001216 /*
1217 * Copy headers at the beginning of the first frame for H.264 only.
1218 * In MPEG4 they are already copied by the coda.
1219 */
1220 if (src_buf->v4l2_buf.sequence == 0) {
1221 pic_stream_buffer_addr =
1222 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1223 ctx->vpu_header_size[0] +
1224 ctx->vpu_header_size[1] +
1225 ctx->vpu_header_size[2];
1226 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1227 ctx->vpu_header_size[0] -
1228 ctx->vpu_header_size[1] -
1229 ctx->vpu_header_size[2];
1230 memcpy(vb2_plane_vaddr(dst_buf, 0),
1231 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1232 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1233 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1234 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1235 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1236 ctx->vpu_header_size[2]);
1237 } else {
1238 pic_stream_buffer_addr =
1239 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1240 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1241 }
1242
1243 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1244 force_ipicture = 1;
1245 switch (dst_fourcc) {
1246 case V4L2_PIX_FMT_H264:
1247 quant_param = ctx->params.h264_intra_qp;
1248 break;
1249 case V4L2_PIX_FMT_MPEG4:
1250 quant_param = ctx->params.mpeg4_intra_qp;
1251 break;
1252 default:
1253 v4l2_warn(&ctx->dev->v4l2_dev,
1254 "cannot set intra qp, fmt not supported\n");
1255 break;
1256 }
1257 } else {
1258 force_ipicture = 0;
1259 switch (dst_fourcc) {
1260 case V4L2_PIX_FMT_H264:
1261 quant_param = ctx->params.h264_inter_qp;
1262 break;
1263 case V4L2_PIX_FMT_MPEG4:
1264 quant_param = ctx->params.mpeg4_inter_qp;
1265 break;
1266 default:
1267 v4l2_warn(&ctx->dev->v4l2_dev,
1268 "cannot set inter qp, fmt not supported\n");
1269 break;
1270 }
1271 }
1272
1273 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001274 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001275 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1276
1277
1278 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001279 switch (q_data_src->fourcc) {
1280 case V4L2_PIX_FMT_YVU420:
1281 /* Switch Cb and Cr for YVU420 format */
1282 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1283 picture_cb = picture_cr + q_data_src->width / 2 *
1284 q_data_src->height / 2;
1285 break;
1286 case V4L2_PIX_FMT_YUV420:
1287 default:
1288 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1289 picture_cr = picture_cb + q_data_src->width / 2 *
1290 q_data_src->height / 2;
1291 break;
1292 }
Javier Martin186b2502012-07-26 05:53:35 -03001293
Philipp Zabel89548442014-07-11 06:36:17 -03001294 if (dev->devtype->product == CODA_960) {
1295 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1296 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1297 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1298
1299 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1300 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1301 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1302 } else {
1303 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1304 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1305 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1306 }
Javier Martin186b2502012-07-26 05:53:35 -03001307 coda_write(dev, force_ipicture << 1 & 0x2,
1308 CODA_CMD_ENC_PIC_OPTION);
1309
1310 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1311 coda_write(dev, pic_stream_buffer_size / 1024,
1312 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001313
1314 if (!ctx->streamon_out) {
1315 /* After streamoff on the output side, set the stream end flag */
1316 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1317 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1318 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001319}
1320
1321static void coda_device_run(void *m2m_priv)
1322{
1323 struct coda_ctx *ctx = m2m_priv;
1324 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001325
1326 queue_work(dev->workqueue, &ctx->pic_run_work);
1327}
1328
1329static void coda_free_framebuffers(struct coda_ctx *ctx);
1330static void coda_free_context_buffers(struct coda_ctx *ctx);
1331
1332static void coda_seq_end_work(struct work_struct *work)
1333{
1334 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1335 struct coda_dev *dev = ctx->dev;
1336
1337 mutex_lock(&ctx->buffer_mutex);
1338 mutex_lock(&dev->coda_mutex);
1339
1340 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1341 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1342 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1343 v4l2_err(&dev->v4l2_dev,
1344 "CODA_COMMAND_SEQ_END failed\n");
1345 }
1346
1347 kfifo_init(&ctx->bitstream_fifo,
1348 ctx->bitstream.vaddr, ctx->bitstream.size);
1349
1350 coda_free_framebuffers(ctx);
1351 coda_free_context_buffers(ctx);
1352
1353 mutex_unlock(&dev->coda_mutex);
1354 mutex_unlock(&ctx->buffer_mutex);
1355}
1356
1357static void coda_finish_decode(struct coda_ctx *ctx);
1358static void coda_finish_encode(struct coda_ctx *ctx);
1359
1360static void coda_pic_run_work(struct work_struct *work)
1361{
1362 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1363 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001364 int ret;
1365
1366 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001367 mutex_lock(&dev->coda_mutex);
1368
Philipp Zabel918c66f2013-06-27 06:59:01 -03001369 if (ctx->inst_type == CODA_INST_DECODER) {
1370 ret = coda_prepare_decode(ctx);
1371 if (ret < 0) {
1372 mutex_unlock(&dev->coda_mutex);
1373 mutex_unlock(&ctx->buffer_mutex);
1374 /* job_finish scheduled by prepare_decode */
1375 return;
1376 }
1377 } else {
1378 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001379 }
1380
Philipp Zabelc2d22512013-06-21 03:55:28 -03001381 if (dev->devtype->product != CODA_DX6)
1382 coda_write(dev, ctx->iram_info.axi_sram_use,
1383 CODA7_REG_BIT_AXI_SRAM_USE);
1384
Philipp Zabel918c66f2013-06-27 06:59:01 -03001385 if (ctx->inst_type == CODA_INST_DECODER)
1386 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001387 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001388
1389 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1390 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1391 } else if (!ctx->aborting) {
1392 if (ctx->inst_type == CODA_INST_DECODER)
1393 coda_finish_decode(ctx);
1394 else
1395 coda_finish_encode(ctx);
1396 }
1397
1398 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1399 queue_work(dev->workqueue, &ctx->seq_end_work);
1400
1401 mutex_unlock(&dev->coda_mutex);
1402 mutex_unlock(&ctx->buffer_mutex);
1403
1404 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001405}
1406
1407static int coda_job_ready(void *m2m_priv)
1408{
1409 struct coda_ctx *ctx = m2m_priv;
1410
1411 /*
1412 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001413 * and 1 frame are needed. In the decoder case,
1414 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001415 */
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001416 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1417 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001418 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1419 "not ready: not enough video buffers.\n");
1420 return 0;
1421 }
1422
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001423 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1424 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1425 "not ready: not enough video capture buffers.\n");
1426 return 0;
1427 }
1428
Philipp Zabel918c66f2013-06-27 06:59:01 -03001429 if (ctx->prescan_failed ||
1430 ((ctx->inst_type == CODA_INST_DECODER) &&
1431 (coda_get_bitstream_payload(ctx) < 512) &&
1432 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1433 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1434 "%d: not ready: not enough bitstream data.\n",
1435 ctx->idx);
1436 return 0;
1437 }
1438
Philipp Zabel3e748262013-05-23 10:43:01 -03001439 if (ctx->aborting) {
1440 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1441 "not ready: aborting\n");
1442 return 0;
1443 }
1444
Javier Martin186b2502012-07-26 05:53:35 -03001445 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1446 "job ready\n");
1447 return 1;
1448}
1449
1450static void coda_job_abort(void *priv)
1451{
1452 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001453
1454 ctx->aborting = 1;
1455
1456 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1457 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001458}
1459
1460static void coda_lock(void *m2m_priv)
1461{
1462 struct coda_ctx *ctx = m2m_priv;
1463 struct coda_dev *pcdev = ctx->dev;
1464 mutex_lock(&pcdev->dev_mutex);
1465}
1466
1467static void coda_unlock(void *m2m_priv)
1468{
1469 struct coda_ctx *ctx = m2m_priv;
1470 struct coda_dev *pcdev = ctx->dev;
1471 mutex_unlock(&pcdev->dev_mutex);
1472}
1473
1474static struct v4l2_m2m_ops coda_m2m_ops = {
1475 .device_run = coda_device_run,
1476 .job_ready = coda_job_ready,
1477 .job_abort = coda_job_abort,
1478 .lock = coda_lock,
1479 .unlock = coda_unlock,
1480};
1481
Philipp Zabel89548442014-07-11 06:36:17 -03001482static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1483{
1484 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1485 int luma_map, chro_map, i;
1486
1487 memset(tiled_map, 0, sizeof(*tiled_map));
1488
1489 luma_map = 64;
1490 chro_map = 64;
1491 tiled_map->map_type = tiled_map_type;
1492 for (i = 0; i < 16; i++)
1493 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1494 for (i = 0; i < 4; i++)
1495 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1496 for (i = 0; i < 16; i++)
1497 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1498
1499 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1500 tiled_map->xy2rbc_config = 0;
1501 } else {
1502 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1503 tiled_map_type);
1504 return;
1505 }
1506}
1507
Javier Martin186b2502012-07-26 05:53:35 -03001508static void set_default_params(struct coda_ctx *ctx)
1509{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001510 int max_w;
1511 int max_h;
1512
1513 ctx->codec = &ctx->dev->devtype->codecs[0];
1514 max_w = ctx->codec->max_w;
1515 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001516
1517 ctx->params.codec_mode = CODA_MODE_INVALID;
1518 ctx->colorspace = V4L2_COLORSPACE_REC709;
1519 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001520 ctx->aborting = 0;
1521
1522 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001523 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1524 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1525 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1526 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1527 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1528 ctx->q_data[V4L2_M2M_DST].width = max_w;
1529 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001530 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001531 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1532 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1533 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1534 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001535
1536 if (ctx->dev->devtype->product == CODA_960)
1537 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001538}
1539
1540/*
1541 * Queue operations
1542 */
1543static int coda_queue_setup(struct vb2_queue *vq,
1544 const struct v4l2_format *fmt,
1545 unsigned int *nbuffers, unsigned int *nplanes,
1546 unsigned int sizes[], void *alloc_ctxs[])
1547{
1548 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001549 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001550 unsigned int size;
1551
Philipp Zabele34db062012-08-29 08:22:00 -03001552 q_data = get_q_data(ctx, vq->type);
1553 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001554
1555 *nplanes = 1;
1556 sizes[0] = size;
1557
1558 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1559
1560 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1561 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1562
1563 return 0;
1564}
1565
1566static int coda_buf_prepare(struct vb2_buffer *vb)
1567{
1568 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1569 struct coda_q_data *q_data;
1570
1571 q_data = get_q_data(ctx, vb->vb2_queue->type);
1572
1573 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1574 v4l2_warn(&ctx->dev->v4l2_dev,
1575 "%s data will not fit into plane (%lu < %lu)\n",
1576 __func__, vb2_plane_size(vb, 0),
1577 (long)q_data->sizeimage);
1578 return -EINVAL;
1579 }
1580
Javier Martin186b2502012-07-26 05:53:35 -03001581 return 0;
1582}
1583
1584static void coda_buf_queue(struct vb2_buffer *vb)
1585{
1586 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001587 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001588 struct coda_q_data *q_data;
1589
1590 q_data = get_q_data(ctx, vb->vb2_queue->type);
1591
1592 /*
1593 * In the decoder case, immediately try to copy the buffer into the
1594 * bitstream ringbuffer and mark it as ready to be dequeued.
1595 */
1596 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1597 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1598 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001599 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001600 * the stream end
1601 */
Philipp Zabel89548442014-07-11 06:36:17 -03001602 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001603 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001604 if ((dev->devtype->product == CODA_960) &&
1605 coda_isbusy(dev) &&
1606 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1607 /* if this decoder instance is running, set the stream end flag */
1608 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1609 }
1610 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001611 mutex_lock(&ctx->bitstream_mutex);
1612 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1613 coda_fill_bitstream(ctx);
1614 mutex_unlock(&ctx->bitstream_mutex);
1615 } else {
1616 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1617 }
Javier Martin186b2502012-07-26 05:53:35 -03001618}
1619
Philipp Zabel86eda902013-05-23 10:42:57 -03001620static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1621{
1622 struct coda_dev *dev = ctx->dev;
1623 u32 *p = ctx->parabuf.vaddr;
1624
1625 if (dev->devtype->product == CODA_DX6)
1626 p[index] = value;
1627 else
1628 p[index ^ 1] = value;
1629}
1630
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001631static int coda_alloc_aux_buf(struct coda_dev *dev,
1632 struct coda_aux_buf *buf, size_t size)
1633{
1634 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1635 GFP_KERNEL);
1636 if (!buf->vaddr)
1637 return -ENOMEM;
1638
1639 buf->size = size;
1640
1641 return 0;
1642}
1643
1644static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1645 struct coda_aux_buf *buf, size_t size)
1646{
1647 return coda_alloc_aux_buf(ctx->dev, buf, size);
1648}
1649
1650static void coda_free_aux_buf(struct coda_dev *dev,
1651 struct coda_aux_buf *buf)
1652{
1653 if (buf->vaddr) {
1654 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1655 buf->vaddr, buf->paddr);
1656 buf->vaddr = NULL;
1657 buf->size = 0;
1658 }
1659}
1660
1661static void coda_free_framebuffers(struct coda_ctx *ctx)
1662{
1663 int i;
1664
1665 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1666 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1667}
1668
Philipp Zabelec25f682012-07-20 08:54:29 -03001669static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1670{
1671 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001672 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001673 dma_addr_t paddr;
1674 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001675 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001676 int i;
1677
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001678 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1679 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001680 ysize = round_up(q_data->width, 8) * height;
1681
Philipp Zabelec25f682012-07-20 08:54:29 -03001682 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001683 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001684 size_t size;
1685
Philipp Zabelaed14b02014-07-11 06:36:15 -03001686 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001687 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1688 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001689 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001690 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1691 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001692 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001693 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001694 }
1695 }
1696
1697 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001698 for (i = 0; i < ctx->num_internal_frames; i++) {
1699 paddr = ctx->internal_frames[i].paddr;
1700 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1701 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1702 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001703
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001704 /* mvcol buffer for h.264 */
1705 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1706 dev->devtype->product != CODA_DX6)
1707 coda_parabuf_write(ctx, 96 + i,
1708 ctx->internal_frames[i].paddr +
1709 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001710 }
1711
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001712 /* mvcol buffer for mpeg4 */
1713 if ((dev->devtype->product != CODA_DX6) &&
1714 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1715 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1716 ysize + ysize/4 + ysize/4);
1717
Philipp Zabelec25f682012-07-20 08:54:29 -03001718 return 0;
1719}
1720
Javier Martin3f3f5c72012-10-29 05:20:29 -03001721static int coda_h264_padding(int size, char *p)
1722{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001723 int nal_size;
1724 int diff;
1725
Javier Martin832fbb52012-10-29 08:34:59 -03001726 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001727 if (diff == 0)
1728 return 0;
1729
Javier Martin832fbb52012-10-29 08:34:59 -03001730 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001731 memcpy(p, coda_filler_nal, nal_size);
1732
1733 /* Add rbsp stop bit and trailing at the end */
1734 *(p + nal_size - 1) = 0x80;
1735
1736 return nal_size;
1737}
1738
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001739static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1740{
1741 phys_addr_t ret;
1742
1743 size = round_up(size, 1024);
1744 if (size > iram->remaining)
1745 return 0;
1746 iram->remaining -= size;
1747
1748 ret = iram->next_paddr;
1749 iram->next_paddr += size;
1750
1751 return ret;
1752}
1753
Philipp Zabelc2d22512013-06-21 03:55:28 -03001754static void coda_setup_iram(struct coda_ctx *ctx)
1755{
1756 struct coda_iram_info *iram_info = &ctx->iram_info;
1757 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001758 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001759 int dbk_bits;
1760 int bit_bits;
1761 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001762
1763 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001764 iram_info->next_paddr = dev->iram.paddr;
1765 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001766
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001767 switch (dev->devtype->product) {
1768 case CODA_7541:
1769 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1770 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1771 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1772 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001773 case CODA_960:
1774 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1775 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1776 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1777 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001778 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001779 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001780 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001781
1782 if (ctx->inst_type == CODA_INST_ENCODER) {
1783 struct coda_q_data *q_data_src;
1784
1785 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1786 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1787
1788 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001789 if (dev->devtype->product == CODA_7541) {
1790 iram_info->search_ram_size = round_up(mb_width * 16 *
1791 36 + 2048, 1024);
1792 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1793 iram_info->search_ram_size);
1794 if (!iram_info->search_ram_paddr) {
1795 pr_err("IRAM is smaller than the search ram size\n");
1796 goto out;
1797 }
1798 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1799 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001800 }
1801
1802 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001803 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1804 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1805 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001806 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001807 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001808
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001809 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1810 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001811 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001812 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001813
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001814 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1815 if (!iram_info->buf_ip_ac_dc_use)
1816 goto out;
1817 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001818
Philipp Zabel8358e762013-06-21 03:55:32 -03001819 /* OVL and BTP disabled for encoder */
1820 } else if (ctx->inst_type == CODA_INST_DECODER) {
1821 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001822
1823 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1824 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001825
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001826 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1827 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1828 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001829 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001830 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001831
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001832 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1833 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001834 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001835 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001836
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001837 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1838 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001839 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001840 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001841
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001842 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001843 }
1844
1845out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001846 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1847 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1848 "IRAM smaller than needed\n");
1849
1850 if (dev->devtype->product == CODA_7541) {
1851 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001852 if (ctx->inst_type == CODA_INST_DECODER) {
1853 /* fw 1.4.50 */
1854 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1855 CODA7_USE_IP_ENABLE);
1856 } else {
1857 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001858 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1859 CODA7_USE_HOST_DBK_ENABLE |
1860 CODA7_USE_IP_ENABLE |
1861 CODA7_USE_DBK_ENABLE);
1862 }
1863 }
1864}
1865
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001866static void coda_free_context_buffers(struct coda_ctx *ctx)
1867{
1868 struct coda_dev *dev = ctx->dev;
1869
Philipp Zabel918c66f2013-06-27 06:59:01 -03001870 coda_free_aux_buf(dev, &ctx->slicebuf);
1871 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001872 if (dev->devtype->product != CODA_DX6)
1873 coda_free_aux_buf(dev, &ctx->workbuf);
1874}
1875
1876static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1877 struct coda_q_data *q_data)
1878{
1879 struct coda_dev *dev = ctx->dev;
1880 size_t size;
1881 int ret;
1882
1883 switch (dev->devtype->product) {
1884 case CODA_7541:
1885 size = CODA7_WORK_BUF_SIZE;
1886 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001887 case CODA_960:
1888 size = CODA9_WORK_BUF_SIZE;
1889 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1890 size += CODA9_PS_SAVE_SIZE;
1891 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001892 default:
1893 return 0;
1894 }
1895
Philipp Zabel918c66f2013-06-27 06:59:01 -03001896 if (ctx->psbuf.vaddr) {
1897 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1898 return -EBUSY;
1899 }
1900 if (ctx->slicebuf.vaddr) {
1901 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1902 return -EBUSY;
1903 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001904 if (ctx->workbuf.vaddr) {
1905 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1906 ret = -EBUSY;
1907 return -ENOMEM;
1908 }
1909
Philipp Zabel918c66f2013-06-27 06:59:01 -03001910 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1911 /* worst case slice size */
1912 size = (DIV_ROUND_UP(q_data->width, 16) *
1913 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1914 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1915 if (ret < 0) {
1916 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1917 ctx->slicebuf.size);
1918 return ret;
1919 }
1920 }
1921
1922 if (dev->devtype->product == CODA_7541) {
1923 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1924 if (ret < 0) {
1925 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1926 goto err;
1927 }
1928 }
1929
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001930 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1931 if (ret < 0) {
1932 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1933 ctx->workbuf.size);
1934 goto err;
1935 }
1936
1937 return 0;
1938
1939err:
1940 coda_free_context_buffers(ctx);
1941 return ret;
1942}
1943
Philipp Zabel918c66f2013-06-27 06:59:01 -03001944static int coda_start_decoding(struct coda_ctx *ctx)
1945{
1946 struct coda_q_data *q_data_src, *q_data_dst;
1947 u32 bitstream_buf, bitstream_size;
1948 struct coda_dev *dev = ctx->dev;
1949 int width, height;
1950 u32 src_fourcc;
1951 u32 val;
1952 int ret;
1953
1954 /* Start decoding */
1955 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1956 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1957 bitstream_buf = ctx->bitstream.paddr;
1958 bitstream_size = ctx->bitstream.size;
1959 src_fourcc = q_data_src->fourcc;
1960
1961 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1962
1963 /* Update coda bitstream read and write pointers from kfifo */
1964 coda_kfifo_sync_to_device_full(ctx);
1965
1966 ctx->display_idx = -1;
1967 ctx->frm_dis_flg = 0;
1968 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1969
1970 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1971 CODA_REG_BIT_BIT_STREAM_PARAM);
1972
1973 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1974 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1975 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001976 if ((dev->devtype->product == CODA_7541) ||
1977 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03001978 val |= CODA_REORDER_ENABLE;
1979 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1980
1981 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03001982 if (dev->devtype->product == CODA_960 &&
1983 src_fourcc == V4L2_PIX_FMT_MPEG4)
1984 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1985 else
1986 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001987 if (src_fourcc == V4L2_PIX_FMT_H264) {
1988 if (dev->devtype->product == CODA_7541) {
1989 coda_write(dev, ctx->psbuf.paddr,
1990 CODA_CMD_DEC_SEQ_PS_BB_START);
1991 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1992 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1993 }
Philipp Zabel89548442014-07-11 06:36:17 -03001994 if (dev->devtype->product == CODA_960) {
1995 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1996 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1997 }
1998 }
1999 if (dev->devtype->product != CODA_960) {
2000 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002001 }
2002
2003 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2004 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2005 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2006 return -ETIMEDOUT;
2007 }
2008
2009 /* Update kfifo out pointer from coda bitstream read pointer */
2010 coda_kfifo_sync_from_device(ctx);
2011
2012 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2013
2014 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2015 v4l2_err(&dev->v4l2_dev,
2016 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2017 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2018 return -EAGAIN;
2019 }
2020
2021 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2022 if (dev->devtype->product == CODA_DX6) {
2023 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2024 height = val & CODADX6_PICHEIGHT_MASK;
2025 } else {
2026 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2027 height = val & CODA7_PICHEIGHT_MASK;
2028 }
2029
2030 if (width > q_data_dst->width || height > q_data_dst->height) {
2031 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2032 width, height, q_data_dst->width, q_data_dst->height);
2033 return -EINVAL;
2034 }
2035
2036 width = round_up(width, 16);
2037 height = round_up(height, 16);
2038
2039 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2040 __func__, ctx->idx, width, height);
2041
Philipp Zabela500a932014-07-11 06:36:13 -03002042 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002043 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2044 v4l2_err(&dev->v4l2_dev,
2045 "not enough framebuffers to decode (%d < %d)\n",
2046 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2047 return -EINVAL;
2048 }
2049
Philipp Zabel52c41672014-07-11 06:36:19 -03002050 if (src_fourcc == V4L2_PIX_FMT_H264) {
2051 u32 left_right;
2052 u32 top_bottom;
2053
2054 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2055 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2056
2057 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2058 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2059 q_data_dst->rect.width = width - q_data_dst->rect.left -
2060 (left_right & 0x3ff);
2061 q_data_dst->rect.height = height - q_data_dst->rect.top -
2062 (top_bottom & 0x3ff);
2063 }
2064
Philipp Zabel918c66f2013-06-27 06:59:01 -03002065 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2066 if (ret < 0)
2067 return ret;
2068
2069 /* Tell the decoder how many frame buffers we allocated. */
2070 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2071 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2072
2073 if (dev->devtype->product != CODA_DX6) {
2074 /* Set secondary AXI IRAM */
2075 coda_setup_iram(ctx);
2076
2077 coda_write(dev, ctx->iram_info.buf_bit_use,
2078 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2079 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2080 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2081 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2082 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2083 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2084 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2085 coda_write(dev, ctx->iram_info.buf_ovl_use,
2086 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002087 if (dev->devtype->product == CODA_960)
2088 coda_write(dev, ctx->iram_info.buf_btp_use,
2089 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2090 }
2091
2092 if (dev->devtype->product == CODA_960) {
2093 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2094
2095 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2096 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2097 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2098 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2099 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2100 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002101 }
2102
2103 if (src_fourcc == V4L2_PIX_FMT_H264) {
2104 coda_write(dev, ctx->slicebuf.paddr,
2105 CODA_CMD_SET_FRAME_SLICE_BB_START);
2106 coda_write(dev, ctx->slicebuf.size / 1024,
2107 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2108 }
2109
2110 if (dev->devtype->product == CODA_7541) {
2111 int max_mb_x = 1920 / 16;
2112 int max_mb_y = 1088 / 16;
2113 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002114
Philipp Zabel918c66f2013-06-27 06:59:01 -03002115 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2116 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002117 } else if (dev->devtype->product == CODA_960) {
2118 int max_mb_x = 1920 / 16;
2119 int max_mb_y = 1088 / 16;
2120 int max_mb_num = max_mb_x * max_mb_y;
2121
2122 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2123 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002124 }
2125
2126 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2127 v4l2_err(&ctx->dev->v4l2_dev,
2128 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2129 return -ETIMEDOUT;
2130 }
2131
2132 return 0;
2133}
2134
Philipp Zabeld35167a2013-05-23 10:42:59 -03002135static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2136 int header_code, u8 *header, int *size)
2137{
2138 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002139 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002140 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002141 int i;
2142
2143 if (dev->devtype->product == CODA_960)
2144 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002145
2146 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2147 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002148 bufsize = vb2_plane_size(buf, 0);
2149 if (dev->devtype->product == CODA_960)
2150 bufsize /= 1024;
2151 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002152 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2153 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2154 if (ret < 0) {
2155 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2156 return ret;
2157 }
Philipp Zabel89548442014-07-11 06:36:17 -03002158
2159 if (dev->devtype->product == CODA_960) {
2160 for (i = 63; i > 0; i--)
2161 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2162 break;
2163 *size = i + 1;
2164 } else {
2165 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2166 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2167 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002168 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2169
2170 return 0;
2171}
2172
Philipp Zabel89548442014-07-11 06:36:17 -03002173static int coda_start_encoding(struct coda_ctx *ctx);
2174
Javier Martin186b2502012-07-26 05:53:35 -03002175static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2176{
2177 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2178 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002179 struct coda_dev *dev = ctx->dev;
2180 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002181 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002182 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002183
Philipp Zabelb96904e2013-05-23 10:42:58 -03002184 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002185 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2186 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2187 if (coda_get_bitstream_payload(ctx) < 512)
2188 return -EINVAL;
2189 } else {
2190 if (count < 1)
2191 return -EINVAL;
2192 }
2193
2194 ctx->streamon_out = 1;
2195
Philipp Zabelb96904e2013-05-23 10:42:58 -03002196 if (coda_format_is_yuv(q_data_src->fourcc))
2197 ctx->inst_type = CODA_INST_ENCODER;
2198 else
2199 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002200 } else {
2201 if (count < 1)
2202 return -EINVAL;
2203
2204 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002205 }
Javier Martin186b2502012-07-26 05:53:35 -03002206
Philipp Zabelb96904e2013-05-23 10:42:58 -03002207 /* Don't start the coda unless both queues are on */
2208 if (!(ctx->streamon_out & ctx->streamon_cap))
2209 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002210
Philipp Zabeleb107512013-09-30 10:34:45 -03002211 /* Allow decoder device_run with no new buffers queued */
2212 if (ctx->inst_type == CODA_INST_DECODER)
2213 v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002214
Philipp Zabelb96904e2013-05-23 10:42:58 -03002215 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002216 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002217 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002218
Philipp Zabelb96904e2013-05-23 10:42:58 -03002219 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2220 q_data_dst->fourcc);
2221 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002222 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2223 return -EINVAL;
2224 }
2225
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002226 /* Allocate per-instance buffers */
2227 ret = coda_alloc_context_buffers(ctx, q_data_src);
2228 if (ret < 0)
2229 return ret;
2230
Philipp Zabel918c66f2013-06-27 06:59:01 -03002231 if (ctx->inst_type == CODA_INST_DECODER) {
2232 mutex_lock(&dev->coda_mutex);
2233 ret = coda_start_decoding(ctx);
2234 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002235 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002236 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002237 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002238 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002239 } else {
2240 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002241 }
2242
Philipp Zabel89548442014-07-11 06:36:17 -03002243 ctx->initialized = 1;
2244 return ret;
2245}
2246
2247static int coda_start_encoding(struct coda_ctx *ctx)
2248{
2249 struct coda_dev *dev = ctx->dev;
2250 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2251 struct coda_q_data *q_data_src, *q_data_dst;
2252 u32 bitstream_buf, bitstream_size;
2253 struct vb2_buffer *buf;
2254 int gamma, ret, value;
2255 u32 dst_fourcc;
2256
2257 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2258 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2259 dst_fourcc = q_data_dst->fourcc;
2260
2261 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2262 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2263 bitstream_size = q_data_dst->sizeimage;
2264
Javier Martin186b2502012-07-26 05:53:35 -03002265 if (!coda_is_initialized(dev)) {
2266 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2267 return -EFAULT;
2268 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002269
2270 mutex_lock(&dev->coda_mutex);
2271
Javier Martin186b2502012-07-26 05:53:35 -03002272 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002273 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2274 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002275 switch (dev->devtype->product) {
2276 case CODA_DX6:
2277 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2278 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2279 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002280 case CODA_960:
2281 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2282 /* fallthrough */
2283 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002284 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2285 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002286 break;
Javier Martin186b2502012-07-26 05:53:35 -03002287 }
2288
Philipp Zabel89548442014-07-11 06:36:17 -03002289 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2290 value &= ~(1 << 2 | 0x7 << 9);
2291 ctx->frame_mem_ctrl = value;
2292 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2293
Philipp Zabel10436672012-07-02 09:03:55 -03002294 if (dev->devtype->product == CODA_DX6) {
2295 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002296 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002297 }
Javier Martin186b2502012-07-26 05:53:35 -03002298
2299 /* Could set rotation here if needed */
2300 switch (dev->devtype->product) {
2301 case CODA_DX6:
2302 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002303 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002304 break;
2305 default:
2306 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002307 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002308 }
Javier Martin186b2502012-07-26 05:53:35 -03002309 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2310 coda_write(dev, ctx->params.framerate,
2311 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2312
Philipp Zabelb96904e2013-05-23 10:42:58 -03002313 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002314 switch (dst_fourcc) {
2315 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002316 if (dev->devtype->product == CODA_960)
2317 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2318 else
2319 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002320 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2321 break;
2322 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002323 if (dev->devtype->product == CODA_960)
2324 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2325 else
2326 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002327 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2328 break;
2329 default:
2330 v4l2_err(v4l2_dev,
2331 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002332 ret = -EINVAL;
2333 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002334 }
2335
Philipp Zabelc566c782012-08-08 11:59:38 -03002336 switch (ctx->params.slice_mode) {
2337 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2338 value = 0;
2339 break;
2340 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2341 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2342 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002343 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002344 break;
2345 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2346 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2347 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2348 value |= 1 & CODA_SLICING_MODE_MASK;
2349 break;
2350 }
Javier Martin186b2502012-07-26 05:53:35 -03002351 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002352 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002353 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2354
2355 if (ctx->params.bitrate) {
2356 /* Rate control enabled */
2357 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2358 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002359 if (dev->devtype->product == CODA_960)
2360 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002361 } else {
2362 value = 0;
2363 }
2364 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2365
2366 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2367 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2368
2369 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2370 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2371
Javier Martin186b2502012-07-26 05:53:35 -03002372
Philipp Zabel89548442014-07-11 06:36:17 -03002373 value = 0;
2374 if (dev->devtype->product == CODA_960)
2375 gamma = CODA9_DEFAULT_GAMMA;
2376 else
2377 gamma = CODA_DEFAULT_GAMMA;
2378 if (gamma > 0) {
2379 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2380 CODA_CMD_ENC_SEQ_RC_GAMMA);
2381 }
2382 if (dev->devtype->product == CODA_960) {
2383 if (CODA_DEFAULT_GAMMA > 0)
2384 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002385 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002386 if (CODA_DEFAULT_GAMMA > 0) {
2387 if (dev->devtype->product == CODA_DX6)
2388 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2389 else
2390 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2391 }
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002392 }
Javier Martin186b2502012-07-26 05:53:35 -03002393 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2394
Philipp Zabel89548442014-07-11 06:36:17 -03002395 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2396
Philipp Zabelc2d22512013-06-21 03:55:28 -03002397 coda_setup_iram(ctx);
2398
Javier Martin186b2502012-07-26 05:53:35 -03002399 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002400 switch (dev->devtype->product) {
2401 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002402 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002403 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002404 break;
2405 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002406 coda_write(dev, ctx->iram_info.search_ram_paddr,
2407 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2408 coda_write(dev, ctx->iram_info.search_ram_size,
2409 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002410 break;
2411 case CODA_960:
2412 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2413 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002414 }
Javier Martin186b2502012-07-26 05:53:35 -03002415 }
2416
Philipp Zabelfcb62822013-05-23 10:43:00 -03002417 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2418 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002419 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002420 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002421 }
2422
Philipp Zabelfcb62822013-05-23 10:43:00 -03002423 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2424 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2425 ret = -EFAULT;
2426 goto out;
2427 }
Javier Martin186b2502012-07-26 05:53:35 -03002428
Philipp Zabel89548442014-07-11 06:36:17 -03002429 if (dev->devtype->product == CODA_960)
2430 ctx->num_internal_frames = 4;
2431 else
2432 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002433 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002434 if (ret < 0) {
2435 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2436 goto out;
2437 }
Javier Martin186b2502012-07-26 05:53:35 -03002438
Philipp Zabelec25f682012-07-20 08:54:29 -03002439 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002440 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002441 if (dev->devtype->product == CODA_7541)
2442 coda_write(dev, round_up(q_data_src->width, 8),
2443 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002444 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002445 coda_write(dev, ctx->iram_info.buf_bit_use,
2446 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2447 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2448 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2449 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2450 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2451 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2452 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2453 coda_write(dev, ctx->iram_info.buf_ovl_use,
2454 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002455 if (dev->devtype->product == CODA_960) {
2456 coda_write(dev, ctx->iram_info.buf_btp_use,
2457 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2458
2459 /* FIXME */
2460 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2461 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2462 }
Philipp Zabel10436672012-07-02 09:03:55 -03002463 }
Philipp Zabel89548442014-07-11 06:36:17 -03002464
Philipp Zabelfcb62822013-05-23 10:43:00 -03002465 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2466 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002467 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002468 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002469 }
2470
2471 /* Save stream headers */
2472 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2473 switch (dst_fourcc) {
2474 case V4L2_PIX_FMT_H264:
2475 /*
2476 * Get SPS in the first frame and copy it to an
2477 * intermediate buffer.
2478 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002479 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2480 &ctx->vpu_header[0][0],
2481 &ctx->vpu_header_size[0]);
2482 if (ret < 0)
2483 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002484
2485 /*
2486 * Get PPS in the first frame and copy it to an
2487 * intermediate buffer.
2488 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002489 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2490 &ctx->vpu_header[1][0],
2491 &ctx->vpu_header_size[1]);
2492 if (ret < 0)
2493 goto out;
2494
Javier Martin3f3f5c72012-10-29 05:20:29 -03002495 /*
2496 * Length of H.264 headers is variable and thus it might not be
2497 * aligned for the coda to append the encoded frame. In that is
2498 * the case a filler NAL must be added to header 2.
2499 */
2500 ctx->vpu_header_size[2] = coda_h264_padding(
2501 (ctx->vpu_header_size[0] +
2502 ctx->vpu_header_size[1]),
2503 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002504 break;
2505 case V4L2_PIX_FMT_MPEG4:
2506 /*
2507 * Get VOS in the first frame and copy it to an
2508 * intermediate buffer
2509 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002510 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2511 &ctx->vpu_header[0][0],
2512 &ctx->vpu_header_size[0]);
2513 if (ret < 0)
2514 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002515
Philipp Zabeld35167a2013-05-23 10:42:59 -03002516 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2517 &ctx->vpu_header[1][0],
2518 &ctx->vpu_header_size[1]);
2519 if (ret < 0)
2520 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002521
Philipp Zabeld35167a2013-05-23 10:42:59 -03002522 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2523 &ctx->vpu_header[2][0],
2524 &ctx->vpu_header_size[2]);
2525 if (ret < 0)
2526 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002527 break;
2528 default:
2529 /* No more formats need to save headers at the moment */
2530 break;
2531 }
2532
Philipp Zabeld35167a2013-05-23 10:42:59 -03002533out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002534 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002535 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002536}
2537
Hans Verkuile37559b2014-04-17 02:47:21 -03002538static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002539{
2540 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002541 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002542
2543 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002544 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002545 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002546 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002547
Philipp Zabel89548442014-07-11 06:36:17 -03002548 if (ctx->inst_type == CODA_INST_DECODER &&
2549 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2550 /* if this decoder instance is running, set the stream end flag */
2551 if (dev->devtype->product == CODA_960) {
2552 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2553
2554 val |= CODA_BIT_STREAM_END_FLAG;
2555 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2556 ctx->bit_stream_param = val;
2557 }
2558 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002559 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2560
2561 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002562 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002563 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002564 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002565 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002566
2567 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002568 }
2569
Philipp Zabel918c66f2013-06-27 06:59:01 -03002570 if (!ctx->streamon_out && !ctx->streamon_cap) {
2571 kfifo_init(&ctx->bitstream_fifo,
2572 ctx->bitstream.vaddr, ctx->bitstream.size);
2573 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002574 }
Javier Martin186b2502012-07-26 05:53:35 -03002575}
2576
2577static struct vb2_ops coda_qops = {
2578 .queue_setup = coda_queue_setup,
2579 .buf_prepare = coda_buf_prepare,
2580 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002581 .start_streaming = coda_start_streaming,
2582 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002583 .wait_prepare = vb2_ops_wait_prepare,
2584 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002585};
2586
2587static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2588{
2589 struct coda_ctx *ctx =
2590 container_of(ctrl->handler, struct coda_ctx, ctrls);
2591
2592 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2593 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2594
2595 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002596 case V4L2_CID_HFLIP:
2597 if (ctrl->val)
2598 ctx->params.rot_mode |= CODA_MIR_HOR;
2599 else
2600 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2601 break;
2602 case V4L2_CID_VFLIP:
2603 if (ctrl->val)
2604 ctx->params.rot_mode |= CODA_MIR_VER;
2605 else
2606 ctx->params.rot_mode &= ~CODA_MIR_VER;
2607 break;
Javier Martin186b2502012-07-26 05:53:35 -03002608 case V4L2_CID_MPEG_VIDEO_BITRATE:
2609 ctx->params.bitrate = ctrl->val / 1000;
2610 break;
2611 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2612 ctx->params.gop_size = ctrl->val;
2613 break;
2614 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2615 ctx->params.h264_intra_qp = ctrl->val;
2616 break;
2617 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2618 ctx->params.h264_inter_qp = ctrl->val;
2619 break;
2620 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2621 ctx->params.mpeg4_intra_qp = ctrl->val;
2622 break;
2623 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2624 ctx->params.mpeg4_inter_qp = ctrl->val;
2625 break;
2626 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2627 ctx->params.slice_mode = ctrl->val;
2628 break;
2629 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2630 ctx->params.slice_max_mb = ctrl->val;
2631 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002632 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2633 ctx->params.slice_max_bits = ctrl->val * 8;
2634 break;
Javier Martin186b2502012-07-26 05:53:35 -03002635 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2636 break;
2637 default:
2638 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2639 "Invalid control, id=%d, val=%d\n",
2640 ctrl->id, ctrl->val);
2641 return -EINVAL;
2642 }
2643
2644 return 0;
2645}
2646
2647static struct v4l2_ctrl_ops coda_ctrl_ops = {
2648 .s_ctrl = coda_s_ctrl,
2649};
2650
2651static int coda_ctrls_setup(struct coda_ctx *ctx)
2652{
2653 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2654
2655 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002656 V4L2_CID_HFLIP, 0, 1, 1, 0);
2657 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2658 V4L2_CID_VFLIP, 0, 1, 1, 0);
2659 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002660 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2661 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2662 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2663 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002664 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002665 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002666 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002667 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2668 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2669 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2670 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2671 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2672 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002673 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2674 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002675 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2676 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002677 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2678 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002679 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2680 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2681 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2682 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2683 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2684
2685 if (ctx->ctrls.error) {
2686 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2687 ctx->ctrls.error);
2688 return -EINVAL;
2689 }
2690
2691 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2692}
2693
2694static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2695 struct vb2_queue *dst_vq)
2696{
2697 struct coda_ctx *ctx = priv;
2698 int ret;
2699
Javier Martin186b2502012-07-26 05:53:35 -03002700 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002701 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002702 src_vq->drv_priv = ctx;
2703 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2704 src_vq->ops = &coda_qops;
2705 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002706 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002707 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002708
2709 ret = vb2_queue_init(src_vq);
2710 if (ret)
2711 return ret;
2712
Javier Martin186b2502012-07-26 05:53:35 -03002713 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002714 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002715 dst_vq->drv_priv = ctx;
2716 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2717 dst_vq->ops = &coda_qops;
2718 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002719 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002720 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002721
2722 return vb2_queue_init(dst_vq);
2723}
2724
Philipp Zabele11f3e62012-07-25 09:16:58 -03002725static int coda_next_free_instance(struct coda_dev *dev)
2726{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002727 int idx = ffz(dev->instance_mask);
2728
2729 if ((idx < 0) ||
2730 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2731 return -EBUSY;
2732
2733 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002734}
2735
Javier Martin186b2502012-07-26 05:53:35 -03002736static int coda_open(struct file *file)
2737{
2738 struct coda_dev *dev = video_drvdata(file);
2739 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002740 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002741 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002742
Javier Martin186b2502012-07-26 05:53:35 -03002743 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2744 if (!ctx)
2745 return -ENOMEM;
2746
Fabio Estevamf82bc202013-08-21 11:14:16 -03002747 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002748 if (idx < 0) {
2749 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002750 goto err_coda_max;
2751 }
2752 set_bit(idx, &dev->instance_mask);
2753
Philipp Zabel32b6f202014-07-11 06:36:20 -03002754 init_completion(&ctx->completion);
2755 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2756 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002757 v4l2_fh_init(&ctx->fh, video_devdata(file));
2758 file->private_data = &ctx->fh;
2759 v4l2_fh_add(&ctx->fh);
2760 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002761 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002762 switch (dev->devtype->product) {
2763 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002764 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002765 ctx->reg_idx = 0;
2766 break;
2767 default:
2768 ctx->reg_idx = idx;
2769 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002770
Fabio Estevam79830db2013-08-21 11:14:17 -03002771 ret = clk_prepare_enable(dev->clk_per);
2772 if (ret)
2773 goto err_clk_per;
2774
2775 ret = clk_prepare_enable(dev->clk_ahb);
2776 if (ret)
2777 goto err_clk_ahb;
2778
Javier Martin186b2502012-07-26 05:53:35 -03002779 set_default_params(ctx);
2780 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2781 &coda_queue_init);
2782 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03002783 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002784
2785 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2786 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002787 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002788 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002789 ctx->fh.m2m_ctx = ctx->m2m_ctx;
2790
Javier Martin186b2502012-07-26 05:53:35 -03002791 ret = coda_ctrls_setup(ctx);
2792 if (ret) {
2793 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002794 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002795 }
2796
2797 ctx->fh.ctrl_handler = &ctx->ctrls;
2798
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002799 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2800 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002801 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002802 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002803 }
2804
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002805 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2806 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2807 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2808 if (!ctx->bitstream.vaddr) {
2809 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2810 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002811 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002812 }
2813 kfifo_init(&ctx->bitstream_fifo,
2814 ctx->bitstream.vaddr, ctx->bitstream.size);
2815 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002816 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002817
Javier Martin186b2502012-07-26 05:53:35 -03002818 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002819 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002820 coda_unlock(ctx);
2821
Javier Martin186b2502012-07-26 05:53:35 -03002822 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2823 ctx->idx, ctx);
2824
2825 return 0;
2826
Fabio Estevamf82bc202013-08-21 11:14:16 -03002827err_dma_writecombine:
2828 coda_free_context_buffers(ctx);
2829 if (ctx->dev->devtype->product == CODA_DX6)
2830 coda_free_aux_buf(dev, &ctx->workbuf);
2831 coda_free_aux_buf(dev, &ctx->parabuf);
2832err_dma_alloc:
2833 v4l2_ctrl_handler_free(&ctx->ctrls);
2834err_ctrls_setup:
2835 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2836err_ctx_init:
2837 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002838err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002839 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002840err_clk_per:
Javier Martin186b2502012-07-26 05:53:35 -03002841 v4l2_fh_del(&ctx->fh);
2842 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002843 clear_bit(ctx->idx, &dev->instance_mask);
2844err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002845 kfree(ctx);
2846 return ret;
2847}
2848
2849static int coda_release(struct file *file)
2850{
2851 struct coda_dev *dev = video_drvdata(file);
2852 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2853
2854 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2855 ctx);
2856
Philipp Zabel918c66f2013-06-27 06:59:01 -03002857 /* If this instance is running, call .job_abort and wait for it to end */
2858 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2859
2860 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002861 if (ctx->initialized) {
2862 queue_work(dev->workqueue, &ctx->seq_end_work);
2863 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002864 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002865
2866 coda_free_framebuffers(ctx);
2867
Javier Martin186b2502012-07-26 05:53:35 -03002868 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002869 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002870 coda_unlock(ctx);
2871
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002872 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2873 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002874 coda_free_context_buffers(ctx);
2875 if (ctx->dev->devtype->product == CODA_DX6)
2876 coda_free_aux_buf(dev, &ctx->workbuf);
2877
2878 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002879 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002880 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002881 clk_disable_unprepare(dev->clk_per);
Javier Martin186b2502012-07-26 05:53:35 -03002882 v4l2_fh_del(&ctx->fh);
2883 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002884 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002885 kfree(ctx);
2886
2887 return 0;
2888}
2889
Javier Martin186b2502012-07-26 05:53:35 -03002890static const struct v4l2_file_operations coda_fops = {
2891 .owner = THIS_MODULE,
2892 .open = coda_open,
2893 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002894 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002895 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002896 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002897};
2898
Philipp Zabel918c66f2013-06-27 06:59:01 -03002899static void coda_finish_decode(struct coda_ctx *ctx)
2900{
2901 struct coda_dev *dev = ctx->dev;
2902 struct coda_q_data *q_data_src;
2903 struct coda_q_data *q_data_dst;
2904 struct vb2_buffer *dst_buf;
2905 int width, height;
2906 int decoded_idx;
2907 int display_idx;
2908 u32 src_fourcc;
2909 int success;
2910 u32 val;
2911
2912 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2913
2914 /* Update kfifo out pointer from coda bitstream read pointer */
2915 coda_kfifo_sync_from_device(ctx);
2916
2917 /*
2918 * in stream-end mode, the read pointer can overshoot the write pointer
2919 * by up to 512 bytes
2920 */
2921 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2922 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2923 kfifo_init(&ctx->bitstream_fifo,
2924 ctx->bitstream.vaddr, ctx->bitstream.size);
2925 }
2926
2927 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2928 src_fourcc = q_data_src->fourcc;
2929
2930 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2931 if (val != 1)
2932 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2933
2934 success = val & 0x1;
2935 if (!success)
2936 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2937
2938 if (src_fourcc == V4L2_PIX_FMT_H264) {
2939 if (val & (1 << 3))
2940 v4l2_err(&dev->v4l2_dev,
2941 "insufficient PS buffer space (%d bytes)\n",
2942 ctx->psbuf.size);
2943 if (val & (1 << 2))
2944 v4l2_err(&dev->v4l2_dev,
2945 "insufficient slice buffer space (%d bytes)\n",
2946 ctx->slicebuf.size);
2947 }
2948
2949 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2950 width = (val >> 16) & 0xffff;
2951 height = val & 0xffff;
2952
2953 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2954
Philipp Zabel52c41672014-07-11 06:36:19 -03002955 /* frame crop information */
2956 if (src_fourcc == V4L2_PIX_FMT_H264) {
2957 u32 left_right;
2958 u32 top_bottom;
2959
2960 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
2961 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
2962
2963 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
2964 /* Keep current crop information */
2965 } else {
2966 struct v4l2_rect *rect = &q_data_dst->rect;
2967
2968 rect->left = left_right >> 16 & 0xffff;
2969 rect->top = top_bottom >> 16 & 0xffff;
2970 rect->width = width - rect->left -
2971 (left_right & 0xffff);
2972 rect->height = height - rect->top -
2973 (top_bottom & 0xffff);
2974 }
2975 } else {
2976 /* no cropping */
2977 }
2978
Philipp Zabel918c66f2013-06-27 06:59:01 -03002979 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2980 if (val > 0)
2981 v4l2_err(&dev->v4l2_dev,
2982 "errors in %d macroblocks\n", val);
2983
2984 if (dev->devtype->product == CODA_7541) {
2985 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2986 if (val == 0) {
2987 /* not enough bitstream data */
2988 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2989 "prescan failed: %d\n", val);
2990 ctx->prescan_failed = true;
2991 return;
2992 }
2993 }
2994
2995 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2996
2997 /*
2998 * The previous display frame was copied out by the rotator,
2999 * now it can be overwritten again
3000 */
3001 if (ctx->display_idx >= 0 &&
3002 ctx->display_idx < ctx->num_internal_frames) {
3003 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3004 coda_write(dev, ctx->frm_dis_flg,
3005 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3006 }
3007
3008 /*
3009 * The index of the last decoded frame, not necessarily in
3010 * display order, and the index of the next display frame.
3011 * The latter could have been decoded in a previous run.
3012 */
3013 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3014 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3015
3016 if (decoded_idx == -1) {
3017 /* no frame was decoded, but we might have a display frame */
3018 if (display_idx < 0 && ctx->display_idx < 0)
3019 ctx->prescan_failed = true;
3020 } else if (decoded_idx == -2) {
3021 /* no frame was decoded, we still return the remaining buffers */
3022 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3023 v4l2_err(&dev->v4l2_dev,
3024 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003025 } else {
3026 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3027 if (val == 0)
3028 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3029 else if (val == 1)
3030 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3031 else
3032 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003033 }
3034
3035 if (display_idx == -1) {
3036 /*
3037 * no more frames to be decoded, but there could still
3038 * be rotator output to dequeue
3039 */
3040 ctx->prescan_failed = true;
3041 } else if (display_idx == -3) {
3042 /* possibly prescan failure */
3043 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3044 v4l2_err(&dev->v4l2_dev,
3045 "presentation frame index out of range: %d\n",
3046 display_idx);
3047 }
3048
3049 /* If a frame was copied out, return it */
3050 if (ctx->display_idx >= 0 &&
3051 ctx->display_idx < ctx->num_internal_frames) {
3052 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
3053 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3054
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003055 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3056 V4L2_BUF_FLAG_PFRAME |
3057 V4L2_BUF_FLAG_BFRAME);
3058 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
3059
Philipp Zabel918c66f2013-06-27 06:59:01 -03003060 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3061
3062 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3063 VB2_BUF_STATE_ERROR);
3064
3065 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3066 "job finished: decoding frame (%d) (%s)\n",
3067 dst_buf->v4l2_buf.sequence,
3068 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3069 "KEYFRAME" : "PFRAME");
3070 } else {
3071 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3072 "job finished: no frame decoded\n");
3073 }
3074
3075 /* The rotator will copy the current display frame next time */
3076 ctx->display_idx = display_idx;
3077}
3078
3079static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003080{
Philipp Zabelec25f682012-07-20 08:54:29 -03003081 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003082 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003083 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003084
Philipp Zabelec25f682012-07-20 08:54:29 -03003085 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03003086 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003087
3088 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003089 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003090 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3091
Javier Martin186b2502012-07-26 05:53:35 -03003092 /* Calculate bytesused field */
3093 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003094 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3095 ctx->vpu_header_size[0] +
3096 ctx->vpu_header_size[1] +
3097 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003098 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003099 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003100 }
3101
3102 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3103 wr_ptr - start_ptr);
3104
3105 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3106 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3107
Philipp Zabel51660282013-09-30 10:34:49 -03003108 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003109 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3110 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3111 } else {
3112 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3113 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3114 }
3115
Kamil Debskiccd571c2013-04-24 10:38:24 -03003116 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003117 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3118 dst_buf->v4l2_buf.flags |=
3119 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003120 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3121
Philipp Zabelec25f682012-07-20 08:54:29 -03003122 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003123
3124 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003125 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3126
3127 ctx->gopcounter--;
3128 if (ctx->gopcounter < 0)
3129 ctx->gopcounter = ctx->params.gop_size - 1;
3130
3131 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3132 "job finished: encoding frame (%d) (%s)\n",
3133 dst_buf->v4l2_buf.sequence,
3134 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3135 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003136}
3137
3138static irqreturn_t coda_irq_handler(int irq, void *data)
3139{
3140 struct coda_dev *dev = data;
3141 struct coda_ctx *ctx;
3142
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003143 /* read status register to attend the IRQ */
3144 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3145 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3146 CODA_REG_BIT_INT_CLEAR);
3147
3148 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3149 if (ctx == NULL) {
3150 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3151 mutex_unlock(&dev->coda_mutex);
3152 return IRQ_HANDLED;
3153 }
3154
3155 if (ctx->aborting) {
3156 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3157 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003158 }
3159
3160 if (coda_isbusy(ctx->dev)) {
3161 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3162 "coda is still busy!!!!\n");
3163 return IRQ_NONE;
3164 }
3165
Philipp Zabel32b6f202014-07-11 06:36:20 -03003166 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003167
3168 return IRQ_HANDLED;
3169}
3170
3171static u32 coda_supported_firmwares[] = {
3172 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003173 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003174 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003175};
3176
3177static bool coda_firmware_supported(u32 vernum)
3178{
3179 int i;
3180
3181 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3182 if (vernum == coda_supported_firmwares[i])
3183 return true;
3184 return false;
3185}
3186
Philipp Zabel87048bb2012-07-02 07:03:43 -03003187static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003188{
3189 u16 product, major, minor, release;
3190 u32 data;
3191 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003192 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003193
Fabio Estevam79830db2013-08-21 11:14:17 -03003194 ret = clk_prepare_enable(dev->clk_per);
3195 if (ret)
3196 return ret;
3197
3198 ret = clk_prepare_enable(dev->clk_ahb);
3199 if (ret)
3200 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003201
Javier Martin186b2502012-07-26 05:53:35 -03003202 /*
3203 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003204 * The 16-bit chars in the code buffer are in memory access
3205 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003206 * Data in this SRAM survives a reboot.
3207 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003208 p = (u16 *)dev->codebuf.vaddr;
3209 if (dev->devtype->product == CODA_DX6) {
3210 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3211 data = CODA_DOWN_ADDRESS_SET(i) |
3212 CODA_DOWN_DATA_SET(p[i ^ 1]);
3213 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3214 }
3215 } else {
3216 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3217 data = CODA_DOWN_ADDRESS_SET(i) |
3218 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3219 3 - (i % 4)]);
3220 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3221 }
Javier Martin186b2502012-07-26 05:53:35 -03003222 }
Javier Martin186b2502012-07-26 05:53:35 -03003223
Philipp Zabel9acf7692013-05-23 10:42:56 -03003224 /* Clear registers */
3225 for (i = 0; i < 64; i++)
3226 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3227
Javier Martin186b2502012-07-26 05:53:35 -03003228 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003229 if (dev->devtype->product == CODA_960 ||
3230 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003231 coda_write(dev, dev->tempbuf.paddr,
3232 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003233 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003234 } else {
3235 coda_write(dev, dev->workbuf.paddr,
3236 CODA_REG_BIT_WORK_BUF_ADDR);
3237 }
Javier Martin186b2502012-07-26 05:53:35 -03003238 coda_write(dev, dev->codebuf.paddr,
3239 CODA_REG_BIT_CODE_BUF_ADDR);
3240 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3241
3242 /* Set default values */
3243 switch (dev->devtype->product) {
3244 case CODA_DX6:
3245 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3246 break;
3247 default:
3248 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3249 }
Philipp Zabel89548442014-07-11 06:36:17 -03003250 if (dev->devtype->product == CODA_960)
3251 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3252 else
3253 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003254
3255 if (dev->devtype->product != CODA_DX6)
3256 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3257
Javier Martin186b2502012-07-26 05:53:35 -03003258 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3259 CODA_REG_BIT_INT_ENABLE);
3260
3261 /* Reset VPU and start processor */
3262 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3263 data |= CODA_REG_RESET_ENABLE;
3264 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3265 udelay(10);
3266 data &= ~CODA_REG_RESET_ENABLE;
3267 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3268 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3269
3270 /* Load firmware */
3271 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3272 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3273 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3274 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3275 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3276 if (coda_wait_timeout(dev)) {
3277 clk_disable_unprepare(dev->clk_per);
3278 clk_disable_unprepare(dev->clk_ahb);
3279 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
3280 return -EIO;
3281 }
3282
Philipp Zabel89548442014-07-11 06:36:17 -03003283 if (dev->devtype->product == CODA_960) {
3284 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3285 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3286 data);
3287 }
3288
Javier Martin186b2502012-07-26 05:53:35 -03003289 /* Check we are compatible with the loaded firmware */
3290 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3291 product = CODA_FIRMWARE_PRODUCT(data);
3292 major = CODA_FIRMWARE_MAJOR(data);
3293 minor = CODA_FIRMWARE_MINOR(data);
3294 release = CODA_FIRMWARE_RELEASE(data);
3295
3296 clk_disable_unprepare(dev->clk_per);
3297 clk_disable_unprepare(dev->clk_ahb);
3298
3299 if (product != dev->devtype->product) {
3300 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3301 " Version: %u.%u.%u\n",
3302 coda_product_name(dev->devtype->product),
3303 coda_product_name(product), major, minor, release);
3304 return -EINVAL;
3305 }
3306
3307 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3308 coda_product_name(product));
3309
3310 if (coda_firmware_supported(data)) {
3311 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3312 major, minor, release);
3313 } else {
3314 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3315 "%u.%u.%u\n", major, minor, release);
3316 }
3317
3318 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003319
3320err_clk_ahb:
3321 clk_disable_unprepare(dev->clk_per);
3322 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003323}
3324
3325static void coda_fw_callback(const struct firmware *fw, void *context)
3326{
3327 struct coda_dev *dev = context;
3328 struct platform_device *pdev = dev->plat_dev;
3329 int ret;
3330
3331 if (!fw) {
3332 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3333 return;
3334 }
3335
3336 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003337 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3338 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003339 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3340 return;
3341 }
3342
Philipp Zabel87048bb2012-07-02 07:03:43 -03003343 /* Copy the whole firmware image to the code buffer */
3344 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3345 release_firmware(fw);
3346
3347 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03003348 if (ret) {
3349 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3350 return;
3351 }
3352
3353 dev->vfd.fops = &coda_fops,
3354 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3355 dev->vfd.release = video_device_release_empty,
3356 dev->vfd.lock = &dev->dev_mutex;
3357 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003358 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003359 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3360 video_set_drvdata(&dev->vfd, dev);
3361
3362 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3363 if (IS_ERR(dev->alloc_ctx)) {
3364 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3365 return;
3366 }
3367
3368 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3369 if (IS_ERR(dev->m2m_dev)) {
3370 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3371 goto rel_ctx;
3372 }
3373
3374 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3375 if (ret) {
3376 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3377 goto rel_m2m;
3378 }
3379 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3380 dev->vfd.num);
3381
3382 return;
3383
3384rel_m2m:
3385 v4l2_m2m_release(dev->m2m_dev);
3386rel_ctx:
3387 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3388}
3389
3390static int coda_firmware_request(struct coda_dev *dev)
3391{
3392 char *fw = dev->devtype->firmware;
3393
3394 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3395 coda_product_name(dev->devtype->product));
3396
3397 return request_firmware_nowait(THIS_MODULE, true,
3398 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3399}
3400
3401enum coda_platform {
3402 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003403 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003404 CODA_IMX6Q,
3405 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003406};
3407
Emil Goodec06d8752012-08-14 17:44:42 -03003408static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003409 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003410 .firmware = "v4l-codadx6-imx27.bin",
3411 .product = CODA_DX6,
3412 .codecs = codadx6_codecs,
3413 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003414 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003415 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003416 .firmware = "v4l-coda7541-imx53.bin",
3417 .product = CODA_7541,
3418 .codecs = coda7_codecs,
3419 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003420 },
Philipp Zabel89548442014-07-11 06:36:17 -03003421 [CODA_IMX6Q] = {
3422 .firmware = "v4l-coda960-imx6q.bin",
3423 .product = CODA_960,
3424 .codecs = coda9_codecs,
3425 .num_codecs = ARRAY_SIZE(coda9_codecs),
3426 },
3427 [CODA_IMX6DL] = {
3428 .firmware = "v4l-coda960-imx6dl.bin",
3429 .product = CODA_960,
3430 .codecs = coda9_codecs,
3431 .num_codecs = ARRAY_SIZE(coda9_codecs),
3432 },
Javier Martin186b2502012-07-26 05:53:35 -03003433};
3434
3435static struct platform_device_id coda_platform_ids[] = {
3436 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003437 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003438 { /* sentinel */ }
3439};
3440MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3441
3442#ifdef CONFIG_OF
3443static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003444 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003445 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003446 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3447 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003448 { /* sentinel */ }
3449};
3450MODULE_DEVICE_TABLE(of, coda_dt_ids);
3451#endif
3452
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003453static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003454{
3455 const struct of_device_id *of_id =
3456 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3457 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003458 struct coda_platform_data *pdata = pdev->dev.platform_data;
3459 struct device_node *np = pdev->dev.of_node;
3460 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003461 struct coda_dev *dev;
3462 struct resource *res;
3463 int ret, irq;
3464
3465 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3466 if (!dev) {
3467 dev_err(&pdev->dev, "Not enough memory for %s\n",
3468 CODA_NAME);
3469 return -ENOMEM;
3470 }
3471
3472 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003473 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003474
3475 dev->plat_dev = pdev;
3476 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3477 if (IS_ERR(dev->clk_per)) {
3478 dev_err(&pdev->dev, "Could not get per clock\n");
3479 return PTR_ERR(dev->clk_per);
3480 }
3481
3482 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3483 if (IS_ERR(dev->clk_ahb)) {
3484 dev_err(&pdev->dev, "Could not get ahb clock\n");
3485 return PTR_ERR(dev->clk_ahb);
3486 }
3487
3488 /* Get memory for physical registers */
3489 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003490 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3491 if (IS_ERR(dev->regs_base))
3492 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003493
3494 /* IRQ */
3495 irq = platform_get_irq(pdev, 0);
3496 if (irq < 0) {
3497 dev_err(&pdev->dev, "failed to get irq resource\n");
3498 return -ENOENT;
3499 }
3500
Philipp Zabel918c66f2013-06-27 06:59:01 -03003501 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003502 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003503 dev_err(&pdev->dev, "failed to request irq\n");
3504 return -ENOENT;
3505 }
3506
Philipp Zabel657eee72013-04-29 16:17:14 -07003507 /* Get IRAM pool from device tree or platform data */
3508 pool = of_get_named_gen_pool(np, "iram", 0);
3509 if (!pool && pdata)
3510 pool = dev_get_gen_pool(pdata->iram_dev);
3511 if (!pool) {
3512 dev_err(&pdev->dev, "iram pool not available\n");
3513 return -ENOMEM;
3514 }
3515 dev->iram_pool = pool;
3516
Javier Martin186b2502012-07-26 05:53:35 -03003517 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3518 if (ret)
3519 return ret;
3520
3521 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003522 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003523
3524 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3525
3526 if (of_id) {
3527 dev->devtype = of_id->data;
3528 } else if (pdev_id) {
3529 dev->devtype = &coda_devdata[pdev_id->driver_data];
3530 } else {
3531 v4l2_device_unregister(&dev->v4l2_dev);
3532 return -EINVAL;
3533 }
3534
3535 /* allocate auxiliary per-device buffers for the BIT processor */
3536 switch (dev->devtype->product) {
3537 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003538 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3539 CODADX6_WORK_BUF_SIZE);
3540 if (ret < 0) {
3541 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3542 v4l2_device_unregister(&dev->v4l2_dev);
3543 return ret;
3544 }
Javier Martin186b2502012-07-26 05:53:35 -03003545 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003546 case CODA_7541:
3547 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3548 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003549 case CODA_960:
3550 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3551 break;
Javier Martin186b2502012-07-26 05:53:35 -03003552 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003553 if (dev->tempbuf.size) {
3554 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3555 dev->tempbuf.size);
3556 if (ret < 0) {
3557 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3558 v4l2_device_unregister(&dev->v4l2_dev);
3559 return ret;
3560 }
Javier Martin186b2502012-07-26 05:53:35 -03003561 }
3562
Philipp Zabel918c66f2013-06-27 06:59:01 -03003563 switch (dev->devtype->product) {
3564 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003565 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003566 break;
3567 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003568 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003569 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003570 case CODA_960:
3571 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003572 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003573 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3574 &dev->iram.paddr);
3575 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003576 dev_err(&pdev->dev, "unable to alloc iram\n");
3577 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003578 }
3579
Philipp Zabel32b6f202014-07-11 06:36:20 -03003580 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3581 if (!dev->workqueue) {
3582 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3583 return -ENOMEM;
3584 }
3585
Javier Martin186b2502012-07-26 05:53:35 -03003586 platform_set_drvdata(pdev, dev);
3587
3588 return coda_firmware_request(dev);
3589}
3590
3591static int coda_remove(struct platform_device *pdev)
3592{
3593 struct coda_dev *dev = platform_get_drvdata(pdev);
3594
3595 video_unregister_device(&dev->vfd);
3596 if (dev->m2m_dev)
3597 v4l2_m2m_release(dev->m2m_dev);
3598 if (dev->alloc_ctx)
3599 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3600 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003601 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003602 if (dev->iram.vaddr)
3603 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3604 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003605 coda_free_aux_buf(dev, &dev->codebuf);
3606 coda_free_aux_buf(dev, &dev->tempbuf);
3607 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003608 return 0;
3609}
3610
3611static struct platform_driver coda_driver = {
3612 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003613 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003614 .driver = {
3615 .name = CODA_NAME,
3616 .owner = THIS_MODULE,
3617 .of_match_table = of_match_ptr(coda_dt_ids),
3618 },
3619 .id_table = coda_platform_ids,
3620};
3621
3622module_platform_driver(coda_driver);
3623
3624MODULE_LICENSE("GPL");
3625MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3626MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");