blob: 45fd7c964d64390f309f2564a85b20bf432910c1 [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_reqbufs(struct file *file, void *priv,
782 struct v4l2_requestbuffers *reqbufs)
Javier Martin186b2502012-07-26 05:53:35 -0300783{
784 struct coda_ctx *ctx = fh_to_ctx(priv);
785
786 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
787}
788
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300789static int coda_querybuf(struct file *file, void *priv,
790 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300791{
792 struct coda_ctx *ctx = fh_to_ctx(priv);
793
794 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
795}
796
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300797static int coda_qbuf(struct file *file, void *priv,
798 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300799{
800 struct coda_ctx *ctx = fh_to_ctx(priv);
801
802 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
803}
804
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300805static int coda_expbuf(struct file *file, void *priv,
806 struct v4l2_exportbuffer *eb)
Philipp Zabel419869c2013-05-23 07:06:30 -0300807{
808 struct coda_ctx *ctx = fh_to_ctx(priv);
809
810 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
811}
812
Philipp Zabel918c66f2013-06-27 06:59:01 -0300813static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
814 struct v4l2_buffer *buf)
815{
816 struct vb2_queue *src_vq;
817
818 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
819
820 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
821 (buf->sequence == (ctx->qsequence - 1)));
822}
823
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300824static int coda_dqbuf(struct file *file, void *priv,
825 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300826{
827 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300828 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300829
Philipp Zabel918c66f2013-06-27 06:59:01 -0300830 ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
831
832 /* If this is the last capture buffer, emit an end-of-stream event */
833 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
834 coda_buf_is_end_of_stream(ctx, buf)) {
835 const struct v4l2_event eos_event = {
836 .type = V4L2_EVENT_EOS
837 };
838
839 v4l2_event_queue_fh(&ctx->fh, &eos_event);
840 }
841
842 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300843}
844
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300845static int coda_create_bufs(struct file *file, void *priv,
846 struct v4l2_create_buffers *create)
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300847{
848 struct coda_ctx *ctx = fh_to_ctx(priv);
849
850 return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
851}
852
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300853static int coda_streamon(struct file *file, void *priv,
854 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300855{
856 struct coda_ctx *ctx = fh_to_ctx(priv);
857
858 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
859}
860
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300861static int coda_streamoff(struct file *file, void *priv,
862 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300863{
864 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300865 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300866
Philipp Zabel918c66f2013-06-27 06:59:01 -0300867 /*
868 * This indirectly calls __vb2_queue_cancel, which dequeues all buffers.
869 * We therefore have to lock it against running hardware in this context,
870 * which still needs the buffers.
871 */
872 mutex_lock(&ctx->buffer_mutex);
873 ret = v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
874 mutex_unlock(&ctx->buffer_mutex);
875
876 return ret;
877}
878
Philipp Zabel52c41672014-07-11 06:36:19 -0300879static int coda_g_selection(struct file *file, void *fh,
880 struct v4l2_selection *s)
881{
882 struct coda_ctx *ctx = fh_to_ctx(fh);
883 struct coda_q_data *q_data;
884 struct v4l2_rect r, *rsel;
885
886 q_data = get_q_data(ctx, s->type);
887 if (!q_data)
888 return -EINVAL;
889
890 r.left = 0;
891 r.top = 0;
892 r.width = q_data->width;
893 r.height = q_data->height;
894 rsel = &q_data->rect;
895
896 switch (s->target) {
897 case V4L2_SEL_TGT_CROP_DEFAULT:
898 case V4L2_SEL_TGT_CROP_BOUNDS:
899 rsel = &r;
900 /* fallthrough */
901 case V4L2_SEL_TGT_CROP:
902 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
903 return -EINVAL;
904 break;
905 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
906 case V4L2_SEL_TGT_COMPOSE_PADDED:
907 rsel = &r;
908 /* fallthrough */
909 case V4L2_SEL_TGT_COMPOSE:
910 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
911 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
912 return -EINVAL;
913 break;
914 default:
915 return -EINVAL;
916 }
917
918 s->r = *rsel;
919
920 return 0;
921}
922
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300923static int coda_try_decoder_cmd(struct file *file, void *fh,
924 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300925{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300926 if (dc->cmd != V4L2_DEC_CMD_STOP)
927 return -EINVAL;
928
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300929 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300930 return -EINVAL;
931
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300932 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300933 return -EINVAL;
934
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300935 return 0;
936}
937
938static int coda_decoder_cmd(struct file *file, void *fh,
939 struct v4l2_decoder_cmd *dc)
940{
941 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300942 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300943 int ret;
944
945 ret = coda_try_decoder_cmd(file, fh, dc);
946 if (ret < 0)
947 return ret;
948
949 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300950 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300951 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300952
953 /* Set the strem-end flag on this context */
954 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
955
Philipp Zabel89548442014-07-11 06:36:17 -0300956 if ((dev->devtype->product == CODA_960) &&
957 coda_isbusy(dev) &&
958 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
959 /* If this context is currently running, update the hardware flag */
960 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
961 }
962
Philipp Zabel918c66f2013-06-27 06:59:01 -0300963 return 0;
964}
965
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300966static int coda_subscribe_event(struct v4l2_fh *fh,
967 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300968{
969 switch (sub->type) {
970 case V4L2_EVENT_EOS:
971 return v4l2_event_subscribe(fh, sub, 0, NULL);
972 default:
973 return v4l2_ctrl_subscribe_event(fh, sub);
974 }
Javier Martin186b2502012-07-26 05:53:35 -0300975}
976
977static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300978 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300979
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300980 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
981 .vidioc_g_fmt_vid_cap = coda_g_fmt,
982 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
983 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300984
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300985 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
986 .vidioc_g_fmt_vid_out = coda_g_fmt,
987 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
988 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300989
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300990 .vidioc_reqbufs = coda_reqbufs,
991 .vidioc_querybuf = coda_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300992
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300993 .vidioc_qbuf = coda_qbuf,
994 .vidioc_expbuf = coda_expbuf,
995 .vidioc_dqbuf = coda_dqbuf,
996 .vidioc_create_bufs = coda_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300997
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300998 .vidioc_streamon = coda_streamon,
999 .vidioc_streamoff = coda_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001000
Philipp Zabel52c41672014-07-11 06:36:19 -03001001 .vidioc_g_selection = coda_g_selection,
1002
Philipp Zabel64ba40a2013-09-30 10:34:52 -03001003 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001004 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001005
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001006 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001007 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001008};
1009
Philipp Zabel918c66f2013-06-27 06:59:01 -03001010static int coda_start_decoding(struct coda_ctx *ctx);
1011
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001012static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
1013{
1014 return kfifo_len(&ctx->bitstream_fifo);
1015}
1016
1017static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
1018{
1019 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1020 struct coda_dev *dev = ctx->dev;
1021 u32 rd_ptr;
1022
1023 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1024 kfifo->out = (kfifo->in & ~kfifo->mask) |
1025 (rd_ptr - ctx->bitstream.paddr);
1026 if (kfifo->out > kfifo->in)
1027 kfifo->out -= kfifo->mask + 1;
1028}
1029
1030static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1031{
1032 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1033 struct coda_dev *dev = ctx->dev;
1034 u32 rd_ptr, wr_ptr;
1035
1036 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1037 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1038 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1039 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1040}
1041
1042static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1043{
1044 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1045 struct coda_dev *dev = ctx->dev;
1046 u32 wr_ptr;
1047
1048 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1049 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1050}
1051
1052static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1053{
1054 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1055 u32 n;
1056
1057 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1058 if (n < src_size)
1059 return -ENOSPC;
1060
1061 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1062 ctx->bitstream.size, DMA_TO_DEVICE);
1063
1064 ctx->qsequence++;
1065
1066 return 0;
1067}
1068
1069static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1070 struct vb2_buffer *src_buf)
1071{
1072 int ret;
1073
1074 if (coda_get_bitstream_payload(ctx) +
1075 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1076 return false;
1077
1078 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1079 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1080 return true;
1081 }
1082
1083 ret = coda_bitstream_queue(ctx, src_buf);
1084 if (ret < 0) {
1085 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1086 return false;
1087 }
1088 /* Sync read pointer to device */
1089 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1090 coda_kfifo_sync_to_device_write(ctx);
1091
Philipp Zabel918c66f2013-06-27 06:59:01 -03001092 ctx->prescan_failed = false;
1093
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001094 return true;
1095}
1096
1097static void coda_fill_bitstream(struct coda_ctx *ctx)
1098{
1099 struct vb2_buffer *src_buf;
1100
1101 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
1102 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1103
1104 if (coda_bitstream_try_queue(ctx, src_buf)) {
1105 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1106 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1107 } else {
1108 break;
1109 }
1110 }
1111}
1112
Philipp Zabel89548442014-07-11 06:36:17 -03001113static void coda_set_gdi_regs(struct coda_ctx *ctx)
1114{
1115 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1116 struct coda_dev *dev = ctx->dev;
1117 int i;
1118
1119 for (i = 0; i < 16; i++)
1120 coda_write(dev, tiled_map->xy2ca_map[i],
1121 CODA9_GDI_XY2_CAS_0 + 4 * i);
1122 for (i = 0; i < 4; i++)
1123 coda_write(dev, tiled_map->xy2ba_map[i],
1124 CODA9_GDI_XY2_BA_0 + 4 * i);
1125 for (i = 0; i < 16; i++)
1126 coda_write(dev, tiled_map->xy2ra_map[i],
1127 CODA9_GDI_XY2_RAS_0 + 4 * i);
1128 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1129 for (i = 0; i < 32; i++)
1130 coda_write(dev, tiled_map->rbc2axi_map[i],
1131 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1132}
1133
Javier Martin186b2502012-07-26 05:53:35 -03001134/*
1135 * Mem-to-mem operations.
1136 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001137static int coda_prepare_decode(struct coda_ctx *ctx)
1138{
1139 struct vb2_buffer *dst_buf;
1140 struct coda_dev *dev = ctx->dev;
1141 struct coda_q_data *q_data_dst;
1142 u32 stridey, height;
1143 u32 picture_y, picture_cb, picture_cr;
1144
1145 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1146 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1147
1148 if (ctx->params.rot_mode & CODA_ROT_90) {
1149 stridey = q_data_dst->height;
1150 height = q_data_dst->width;
1151 } else {
1152 stridey = q_data_dst->width;
1153 height = q_data_dst->height;
1154 }
1155
1156 /* Try to copy source buffer contents into the bitstream ringbuffer */
1157 mutex_lock(&ctx->bitstream_mutex);
1158 coda_fill_bitstream(ctx);
1159 mutex_unlock(&ctx->bitstream_mutex);
1160
1161 if (coda_get_bitstream_payload(ctx) < 512 &&
1162 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1163 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1164 "bitstream payload: %d, skipping\n",
1165 coda_get_bitstream_payload(ctx));
Philipp Zabel32b6f202014-07-11 06:36:20 -03001166 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001167 return -EAGAIN;
1168 }
1169
1170 /* Run coda_start_decoding (again) if not yet initialized */
1171 if (!ctx->initialized) {
1172 int ret = coda_start_decoding(ctx);
1173 if (ret < 0) {
1174 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel32b6f202014-07-11 06:36:20 -03001175 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001176 return -EAGAIN;
1177 } else {
1178 ctx->initialized = 1;
1179 }
1180 }
1181
Philipp Zabel89548442014-07-11 06:36:17 -03001182 if (dev->devtype->product == CODA_960)
1183 coda_set_gdi_regs(ctx);
1184
Philipp Zabel918c66f2013-06-27 06:59:01 -03001185 /* Set rotator output */
1186 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1187 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1188 /* Switch Cr and Cb for YVU420 format */
1189 picture_cr = picture_y + stridey * height;
1190 picture_cb = picture_cr + stridey / 2 * height / 2;
1191 } else {
1192 picture_cb = picture_y + stridey * height;
1193 picture_cr = picture_cb + stridey / 2 * height / 2;
1194 }
Philipp Zabel89548442014-07-11 06:36:17 -03001195
1196 if (dev->devtype->product == CODA_960) {
1197 /*
1198 * The CODA960 seems to have an internal list of buffers with
1199 * 64 entries that includes the registered frame buffers as
1200 * well as the rotator buffer output.
1201 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1202 */
1203 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1204 CODA9_CMD_DEC_PIC_ROT_INDEX);
1205 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1206 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1207 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1208 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1209 } else {
1210 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1211 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1212 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1213 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1214 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001215 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1216 CODA_CMD_DEC_PIC_ROT_MODE);
1217
1218 switch (dev->devtype->product) {
1219 case CODA_DX6:
1220 /* TBD */
1221 case CODA_7541:
1222 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1223 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001224 case CODA_960:
1225 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1226 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001227 }
1228
1229 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1230
1231 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1232 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1233
1234 return 0;
1235}
1236
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001237static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001238{
Javier Martin186b2502012-07-26 05:53:35 -03001239 struct coda_q_data *q_data_src, *q_data_dst;
1240 struct vb2_buffer *src_buf, *dst_buf;
1241 struct coda_dev *dev = ctx->dev;
1242 int force_ipicture;
1243 int quant_param = 0;
1244 u32 picture_y, picture_cb, picture_cr;
1245 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1246 u32 dst_fourcc;
1247
1248 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1249 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1250 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1251 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001252 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001253
Philipp Zabel918c66f2013-06-27 06:59:01 -03001254 src_buf->v4l2_buf.sequence = ctx->osequence;
1255 dst_buf->v4l2_buf.sequence = ctx->osequence;
1256 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001257
1258 /*
1259 * Workaround coda firmware BUG that only marks the first
1260 * frame as IDR. This is a problem for some decoders that can't
1261 * recover when a frame is lost.
1262 */
1263 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1264 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1265 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1266 } else {
1267 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1268 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1269 }
1270
Philipp Zabel89548442014-07-11 06:36:17 -03001271 if (dev->devtype->product == CODA_960)
1272 coda_set_gdi_regs(ctx);
1273
Javier Martin186b2502012-07-26 05:53:35 -03001274 /*
1275 * Copy headers at the beginning of the first frame for H.264 only.
1276 * In MPEG4 they are already copied by the coda.
1277 */
1278 if (src_buf->v4l2_buf.sequence == 0) {
1279 pic_stream_buffer_addr =
1280 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1281 ctx->vpu_header_size[0] +
1282 ctx->vpu_header_size[1] +
1283 ctx->vpu_header_size[2];
1284 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1285 ctx->vpu_header_size[0] -
1286 ctx->vpu_header_size[1] -
1287 ctx->vpu_header_size[2];
1288 memcpy(vb2_plane_vaddr(dst_buf, 0),
1289 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1290 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1291 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1292 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1293 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1294 ctx->vpu_header_size[2]);
1295 } else {
1296 pic_stream_buffer_addr =
1297 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1298 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1299 }
1300
1301 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1302 force_ipicture = 1;
1303 switch (dst_fourcc) {
1304 case V4L2_PIX_FMT_H264:
1305 quant_param = ctx->params.h264_intra_qp;
1306 break;
1307 case V4L2_PIX_FMT_MPEG4:
1308 quant_param = ctx->params.mpeg4_intra_qp;
1309 break;
1310 default:
1311 v4l2_warn(&ctx->dev->v4l2_dev,
1312 "cannot set intra qp, fmt not supported\n");
1313 break;
1314 }
1315 } else {
1316 force_ipicture = 0;
1317 switch (dst_fourcc) {
1318 case V4L2_PIX_FMT_H264:
1319 quant_param = ctx->params.h264_inter_qp;
1320 break;
1321 case V4L2_PIX_FMT_MPEG4:
1322 quant_param = ctx->params.mpeg4_inter_qp;
1323 break;
1324 default:
1325 v4l2_warn(&ctx->dev->v4l2_dev,
1326 "cannot set inter qp, fmt not supported\n");
1327 break;
1328 }
1329 }
1330
1331 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001332 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001333 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1334
1335
1336 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001337 switch (q_data_src->fourcc) {
1338 case V4L2_PIX_FMT_YVU420:
1339 /* Switch Cb and Cr for YVU420 format */
1340 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1341 picture_cb = picture_cr + q_data_src->width / 2 *
1342 q_data_src->height / 2;
1343 break;
1344 case V4L2_PIX_FMT_YUV420:
1345 default:
1346 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1347 picture_cr = picture_cb + q_data_src->width / 2 *
1348 q_data_src->height / 2;
1349 break;
1350 }
Javier Martin186b2502012-07-26 05:53:35 -03001351
Philipp Zabel89548442014-07-11 06:36:17 -03001352 if (dev->devtype->product == CODA_960) {
1353 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1354 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1355 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1356
1357 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1358 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1359 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1360 } else {
1361 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1362 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1363 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1364 }
Javier Martin186b2502012-07-26 05:53:35 -03001365 coda_write(dev, force_ipicture << 1 & 0x2,
1366 CODA_CMD_ENC_PIC_OPTION);
1367
1368 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1369 coda_write(dev, pic_stream_buffer_size / 1024,
1370 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001371
1372 if (!ctx->streamon_out) {
1373 /* After streamoff on the output side, set the stream end flag */
1374 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1375 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1376 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001377}
1378
1379static void coda_device_run(void *m2m_priv)
1380{
1381 struct coda_ctx *ctx = m2m_priv;
1382 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001383
1384 queue_work(dev->workqueue, &ctx->pic_run_work);
1385}
1386
1387static void coda_free_framebuffers(struct coda_ctx *ctx);
1388static void coda_free_context_buffers(struct coda_ctx *ctx);
1389
1390static void coda_seq_end_work(struct work_struct *work)
1391{
1392 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1393 struct coda_dev *dev = ctx->dev;
1394
1395 mutex_lock(&ctx->buffer_mutex);
1396 mutex_lock(&dev->coda_mutex);
1397
1398 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1399 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1400 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1401 v4l2_err(&dev->v4l2_dev,
1402 "CODA_COMMAND_SEQ_END failed\n");
1403 }
1404
1405 kfifo_init(&ctx->bitstream_fifo,
1406 ctx->bitstream.vaddr, ctx->bitstream.size);
1407
1408 coda_free_framebuffers(ctx);
1409 coda_free_context_buffers(ctx);
1410
1411 mutex_unlock(&dev->coda_mutex);
1412 mutex_unlock(&ctx->buffer_mutex);
1413}
1414
1415static void coda_finish_decode(struct coda_ctx *ctx);
1416static void coda_finish_encode(struct coda_ctx *ctx);
1417
1418static void coda_pic_run_work(struct work_struct *work)
1419{
1420 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1421 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001422 int ret;
1423
1424 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001425 mutex_lock(&dev->coda_mutex);
1426
Philipp Zabel918c66f2013-06-27 06:59:01 -03001427 if (ctx->inst_type == CODA_INST_DECODER) {
1428 ret = coda_prepare_decode(ctx);
1429 if (ret < 0) {
1430 mutex_unlock(&dev->coda_mutex);
1431 mutex_unlock(&ctx->buffer_mutex);
1432 /* job_finish scheduled by prepare_decode */
1433 return;
1434 }
1435 } else {
1436 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001437 }
1438
Philipp Zabelc2d22512013-06-21 03:55:28 -03001439 if (dev->devtype->product != CODA_DX6)
1440 coda_write(dev, ctx->iram_info.axi_sram_use,
1441 CODA7_REG_BIT_AXI_SRAM_USE);
1442
Philipp Zabel918c66f2013-06-27 06:59:01 -03001443 if (ctx->inst_type == CODA_INST_DECODER)
1444 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001445 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001446
1447 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1448 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1449 } else if (!ctx->aborting) {
1450 if (ctx->inst_type == CODA_INST_DECODER)
1451 coda_finish_decode(ctx);
1452 else
1453 coda_finish_encode(ctx);
1454 }
1455
1456 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1457 queue_work(dev->workqueue, &ctx->seq_end_work);
1458
1459 mutex_unlock(&dev->coda_mutex);
1460 mutex_unlock(&ctx->buffer_mutex);
1461
1462 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001463}
1464
1465static int coda_job_ready(void *m2m_priv)
1466{
1467 struct coda_ctx *ctx = m2m_priv;
1468
1469 /*
1470 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001471 * and 1 frame are needed. In the decoder case,
1472 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001473 */
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001474 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1475 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001476 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1477 "not ready: not enough video buffers.\n");
1478 return 0;
1479 }
1480
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001481 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1482 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1483 "not ready: not enough video capture buffers.\n");
1484 return 0;
1485 }
1486
Philipp Zabel918c66f2013-06-27 06:59:01 -03001487 if (ctx->prescan_failed ||
1488 ((ctx->inst_type == CODA_INST_DECODER) &&
1489 (coda_get_bitstream_payload(ctx) < 512) &&
1490 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1491 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1492 "%d: not ready: not enough bitstream data.\n",
1493 ctx->idx);
1494 return 0;
1495 }
1496
Philipp Zabel3e748262013-05-23 10:43:01 -03001497 if (ctx->aborting) {
1498 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1499 "not ready: aborting\n");
1500 return 0;
1501 }
1502
Javier Martin186b2502012-07-26 05:53:35 -03001503 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1504 "job ready\n");
1505 return 1;
1506}
1507
1508static void coda_job_abort(void *priv)
1509{
1510 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001511
1512 ctx->aborting = 1;
1513
1514 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1515 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001516}
1517
1518static void coda_lock(void *m2m_priv)
1519{
1520 struct coda_ctx *ctx = m2m_priv;
1521 struct coda_dev *pcdev = ctx->dev;
1522 mutex_lock(&pcdev->dev_mutex);
1523}
1524
1525static void coda_unlock(void *m2m_priv)
1526{
1527 struct coda_ctx *ctx = m2m_priv;
1528 struct coda_dev *pcdev = ctx->dev;
1529 mutex_unlock(&pcdev->dev_mutex);
1530}
1531
1532static struct v4l2_m2m_ops coda_m2m_ops = {
1533 .device_run = coda_device_run,
1534 .job_ready = coda_job_ready,
1535 .job_abort = coda_job_abort,
1536 .lock = coda_lock,
1537 .unlock = coda_unlock,
1538};
1539
Philipp Zabel89548442014-07-11 06:36:17 -03001540static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1541{
1542 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1543 int luma_map, chro_map, i;
1544
1545 memset(tiled_map, 0, sizeof(*tiled_map));
1546
1547 luma_map = 64;
1548 chro_map = 64;
1549 tiled_map->map_type = tiled_map_type;
1550 for (i = 0; i < 16; i++)
1551 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1552 for (i = 0; i < 4; i++)
1553 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1554 for (i = 0; i < 16; i++)
1555 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1556
1557 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1558 tiled_map->xy2rbc_config = 0;
1559 } else {
1560 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1561 tiled_map_type);
1562 return;
1563 }
1564}
1565
Javier Martin186b2502012-07-26 05:53:35 -03001566static void set_default_params(struct coda_ctx *ctx)
1567{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001568 int max_w;
1569 int max_h;
1570
1571 ctx->codec = &ctx->dev->devtype->codecs[0];
1572 max_w = ctx->codec->max_w;
1573 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001574
1575 ctx->params.codec_mode = CODA_MODE_INVALID;
1576 ctx->colorspace = V4L2_COLORSPACE_REC709;
1577 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001578 ctx->aborting = 0;
1579
1580 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001581 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1582 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1583 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1584 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1585 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1586 ctx->q_data[V4L2_M2M_DST].width = max_w;
1587 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001588 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001589 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1590 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1591 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1592 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001593
1594 if (ctx->dev->devtype->product == CODA_960)
1595 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001596}
1597
1598/*
1599 * Queue operations
1600 */
1601static int coda_queue_setup(struct vb2_queue *vq,
1602 const struct v4l2_format *fmt,
1603 unsigned int *nbuffers, unsigned int *nplanes,
1604 unsigned int sizes[], void *alloc_ctxs[])
1605{
1606 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001607 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001608 unsigned int size;
1609
Philipp Zabele34db062012-08-29 08:22:00 -03001610 q_data = get_q_data(ctx, vq->type);
1611 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001612
1613 *nplanes = 1;
1614 sizes[0] = size;
1615
1616 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1617
1618 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1619 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1620
1621 return 0;
1622}
1623
1624static int coda_buf_prepare(struct vb2_buffer *vb)
1625{
1626 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1627 struct coda_q_data *q_data;
1628
1629 q_data = get_q_data(ctx, vb->vb2_queue->type);
1630
1631 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1632 v4l2_warn(&ctx->dev->v4l2_dev,
1633 "%s data will not fit into plane (%lu < %lu)\n",
1634 __func__, vb2_plane_size(vb, 0),
1635 (long)q_data->sizeimage);
1636 return -EINVAL;
1637 }
1638
Javier Martin186b2502012-07-26 05:53:35 -03001639 return 0;
1640}
1641
1642static void coda_buf_queue(struct vb2_buffer *vb)
1643{
1644 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001645 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001646 struct coda_q_data *q_data;
1647
1648 q_data = get_q_data(ctx, vb->vb2_queue->type);
1649
1650 /*
1651 * In the decoder case, immediately try to copy the buffer into the
1652 * bitstream ringbuffer and mark it as ready to be dequeued.
1653 */
1654 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1655 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1656 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001657 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001658 * the stream end
1659 */
Philipp Zabel89548442014-07-11 06:36:17 -03001660 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001661 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001662 if ((dev->devtype->product == CODA_960) &&
1663 coda_isbusy(dev) &&
1664 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1665 /* if this decoder instance is running, set the stream end flag */
1666 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1667 }
1668 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001669 mutex_lock(&ctx->bitstream_mutex);
1670 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1671 coda_fill_bitstream(ctx);
1672 mutex_unlock(&ctx->bitstream_mutex);
1673 } else {
1674 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1675 }
Javier Martin186b2502012-07-26 05:53:35 -03001676}
1677
1678static void coda_wait_prepare(struct vb2_queue *q)
1679{
1680 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1681 coda_unlock(ctx);
1682}
1683
1684static void coda_wait_finish(struct vb2_queue *q)
1685{
1686 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1687 coda_lock(ctx);
1688}
1689
Philipp Zabel86eda902013-05-23 10:42:57 -03001690static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1691{
1692 struct coda_dev *dev = ctx->dev;
1693 u32 *p = ctx->parabuf.vaddr;
1694
1695 if (dev->devtype->product == CODA_DX6)
1696 p[index] = value;
1697 else
1698 p[index ^ 1] = value;
1699}
1700
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001701static int coda_alloc_aux_buf(struct coda_dev *dev,
1702 struct coda_aux_buf *buf, size_t size)
1703{
1704 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1705 GFP_KERNEL);
1706 if (!buf->vaddr)
1707 return -ENOMEM;
1708
1709 buf->size = size;
1710
1711 return 0;
1712}
1713
1714static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1715 struct coda_aux_buf *buf, size_t size)
1716{
1717 return coda_alloc_aux_buf(ctx->dev, buf, size);
1718}
1719
1720static void coda_free_aux_buf(struct coda_dev *dev,
1721 struct coda_aux_buf *buf)
1722{
1723 if (buf->vaddr) {
1724 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1725 buf->vaddr, buf->paddr);
1726 buf->vaddr = NULL;
1727 buf->size = 0;
1728 }
1729}
1730
1731static void coda_free_framebuffers(struct coda_ctx *ctx)
1732{
1733 int i;
1734
1735 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1736 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1737}
1738
Philipp Zabelec25f682012-07-20 08:54:29 -03001739static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1740{
1741 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001742 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001743 dma_addr_t paddr;
1744 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001745 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001746 int i;
1747
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001748 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1749 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001750 ysize = round_up(q_data->width, 8) * height;
1751
Philipp Zabelec25f682012-07-20 08:54:29 -03001752 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001753 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001754 size_t size;
1755
Philipp Zabelaed14b02014-07-11 06:36:15 -03001756 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001757 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1758 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001759 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001760 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1761 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001762 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001763 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001764 }
1765 }
1766
1767 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001768 for (i = 0; i < ctx->num_internal_frames; i++) {
1769 paddr = ctx->internal_frames[i].paddr;
1770 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1771 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1772 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001773
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001774 /* mvcol buffer for h.264 */
1775 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1776 dev->devtype->product != CODA_DX6)
1777 coda_parabuf_write(ctx, 96 + i,
1778 ctx->internal_frames[i].paddr +
1779 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001780 }
1781
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001782 /* mvcol buffer for mpeg4 */
1783 if ((dev->devtype->product != CODA_DX6) &&
1784 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1785 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1786 ysize + ysize/4 + ysize/4);
1787
Philipp Zabelec25f682012-07-20 08:54:29 -03001788 return 0;
1789}
1790
Javier Martin3f3f5c72012-10-29 05:20:29 -03001791static int coda_h264_padding(int size, char *p)
1792{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001793 int nal_size;
1794 int diff;
1795
Javier Martin832fbb52012-10-29 08:34:59 -03001796 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001797 if (diff == 0)
1798 return 0;
1799
Javier Martin832fbb52012-10-29 08:34:59 -03001800 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001801 memcpy(p, coda_filler_nal, nal_size);
1802
1803 /* Add rbsp stop bit and trailing at the end */
1804 *(p + nal_size - 1) = 0x80;
1805
1806 return nal_size;
1807}
1808
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001809static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1810{
1811 phys_addr_t ret;
1812
1813 size = round_up(size, 1024);
1814 if (size > iram->remaining)
1815 return 0;
1816 iram->remaining -= size;
1817
1818 ret = iram->next_paddr;
1819 iram->next_paddr += size;
1820
1821 return ret;
1822}
1823
Philipp Zabelc2d22512013-06-21 03:55:28 -03001824static void coda_setup_iram(struct coda_ctx *ctx)
1825{
1826 struct coda_iram_info *iram_info = &ctx->iram_info;
1827 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001828 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001829 int dbk_bits;
1830 int bit_bits;
1831 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001832
1833 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001834 iram_info->next_paddr = dev->iram.paddr;
1835 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001836
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001837 switch (dev->devtype->product) {
1838 case CODA_7541:
1839 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1840 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1841 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1842 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001843 case CODA_960:
1844 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1845 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1846 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1847 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001848 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001849 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001850 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001851
1852 if (ctx->inst_type == CODA_INST_ENCODER) {
1853 struct coda_q_data *q_data_src;
1854
1855 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1856 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1857
1858 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001859 if (dev->devtype->product == CODA_7541) {
1860 iram_info->search_ram_size = round_up(mb_width * 16 *
1861 36 + 2048, 1024);
1862 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1863 iram_info->search_ram_size);
1864 if (!iram_info->search_ram_paddr) {
1865 pr_err("IRAM is smaller than the search ram size\n");
1866 goto out;
1867 }
1868 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1869 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001870 }
1871
1872 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001873 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1874 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1875 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001876 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001877 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001878
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001879 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1880 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001881 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001882 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001883
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001884 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1885 if (!iram_info->buf_ip_ac_dc_use)
1886 goto out;
1887 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001888
Philipp Zabel8358e762013-06-21 03:55:32 -03001889 /* OVL and BTP disabled for encoder */
1890 } else if (ctx->inst_type == CODA_INST_DECODER) {
1891 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001892
1893 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1894 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001895
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001896 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1897 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1898 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001899 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001900 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001901
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001902 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1903 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001904 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001905 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001906
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001907 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1908 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001909 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001910 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001911
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001912 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001913 }
1914
1915out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001916 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1917 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1918 "IRAM smaller than needed\n");
1919
1920 if (dev->devtype->product == CODA_7541) {
1921 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001922 if (ctx->inst_type == CODA_INST_DECODER) {
1923 /* fw 1.4.50 */
1924 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1925 CODA7_USE_IP_ENABLE);
1926 } else {
1927 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001928 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1929 CODA7_USE_HOST_DBK_ENABLE |
1930 CODA7_USE_IP_ENABLE |
1931 CODA7_USE_DBK_ENABLE);
1932 }
1933 }
1934}
1935
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001936static void coda_free_context_buffers(struct coda_ctx *ctx)
1937{
1938 struct coda_dev *dev = ctx->dev;
1939
Philipp Zabel918c66f2013-06-27 06:59:01 -03001940 coda_free_aux_buf(dev, &ctx->slicebuf);
1941 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001942 if (dev->devtype->product != CODA_DX6)
1943 coda_free_aux_buf(dev, &ctx->workbuf);
1944}
1945
1946static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1947 struct coda_q_data *q_data)
1948{
1949 struct coda_dev *dev = ctx->dev;
1950 size_t size;
1951 int ret;
1952
1953 switch (dev->devtype->product) {
1954 case CODA_7541:
1955 size = CODA7_WORK_BUF_SIZE;
1956 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001957 case CODA_960:
1958 size = CODA9_WORK_BUF_SIZE;
1959 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1960 size += CODA9_PS_SAVE_SIZE;
1961 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001962 default:
1963 return 0;
1964 }
1965
Philipp Zabel918c66f2013-06-27 06:59:01 -03001966 if (ctx->psbuf.vaddr) {
1967 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1968 return -EBUSY;
1969 }
1970 if (ctx->slicebuf.vaddr) {
1971 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1972 return -EBUSY;
1973 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001974 if (ctx->workbuf.vaddr) {
1975 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1976 ret = -EBUSY;
1977 return -ENOMEM;
1978 }
1979
Philipp Zabel918c66f2013-06-27 06:59:01 -03001980 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1981 /* worst case slice size */
1982 size = (DIV_ROUND_UP(q_data->width, 16) *
1983 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1984 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1985 if (ret < 0) {
1986 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1987 ctx->slicebuf.size);
1988 return ret;
1989 }
1990 }
1991
1992 if (dev->devtype->product == CODA_7541) {
1993 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1994 if (ret < 0) {
1995 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1996 goto err;
1997 }
1998 }
1999
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002000 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
2001 if (ret < 0) {
2002 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2003 ctx->workbuf.size);
2004 goto err;
2005 }
2006
2007 return 0;
2008
2009err:
2010 coda_free_context_buffers(ctx);
2011 return ret;
2012}
2013
Philipp Zabel918c66f2013-06-27 06:59:01 -03002014static int coda_start_decoding(struct coda_ctx *ctx)
2015{
2016 struct coda_q_data *q_data_src, *q_data_dst;
2017 u32 bitstream_buf, bitstream_size;
2018 struct coda_dev *dev = ctx->dev;
2019 int width, height;
2020 u32 src_fourcc;
2021 u32 val;
2022 int ret;
2023
2024 /* Start decoding */
2025 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2026 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2027 bitstream_buf = ctx->bitstream.paddr;
2028 bitstream_size = ctx->bitstream.size;
2029 src_fourcc = q_data_src->fourcc;
2030
2031 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2032
2033 /* Update coda bitstream read and write pointers from kfifo */
2034 coda_kfifo_sync_to_device_full(ctx);
2035
2036 ctx->display_idx = -1;
2037 ctx->frm_dis_flg = 0;
2038 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2039
2040 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2041 CODA_REG_BIT_BIT_STREAM_PARAM);
2042
2043 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2044 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2045 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002046 if ((dev->devtype->product == CODA_7541) ||
2047 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002048 val |= CODA_REORDER_ENABLE;
2049 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2050
2051 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002052 if (dev->devtype->product == CODA_960 &&
2053 src_fourcc == V4L2_PIX_FMT_MPEG4)
2054 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2055 else
2056 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002057 if (src_fourcc == V4L2_PIX_FMT_H264) {
2058 if (dev->devtype->product == CODA_7541) {
2059 coda_write(dev, ctx->psbuf.paddr,
2060 CODA_CMD_DEC_SEQ_PS_BB_START);
2061 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2062 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2063 }
Philipp Zabel89548442014-07-11 06:36:17 -03002064 if (dev->devtype->product == CODA_960) {
2065 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2066 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2067 }
2068 }
2069 if (dev->devtype->product != CODA_960) {
2070 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002071 }
2072
2073 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2074 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2075 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2076 return -ETIMEDOUT;
2077 }
2078
2079 /* Update kfifo out pointer from coda bitstream read pointer */
2080 coda_kfifo_sync_from_device(ctx);
2081
2082 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2083
2084 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2085 v4l2_err(&dev->v4l2_dev,
2086 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2087 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2088 return -EAGAIN;
2089 }
2090
2091 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2092 if (dev->devtype->product == CODA_DX6) {
2093 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2094 height = val & CODADX6_PICHEIGHT_MASK;
2095 } else {
2096 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2097 height = val & CODA7_PICHEIGHT_MASK;
2098 }
2099
2100 if (width > q_data_dst->width || height > q_data_dst->height) {
2101 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2102 width, height, q_data_dst->width, q_data_dst->height);
2103 return -EINVAL;
2104 }
2105
2106 width = round_up(width, 16);
2107 height = round_up(height, 16);
2108
2109 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2110 __func__, ctx->idx, width, height);
2111
Philipp Zabela500a932014-07-11 06:36:13 -03002112 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002113 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2114 v4l2_err(&dev->v4l2_dev,
2115 "not enough framebuffers to decode (%d < %d)\n",
2116 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2117 return -EINVAL;
2118 }
2119
Philipp Zabel52c41672014-07-11 06:36:19 -03002120 if (src_fourcc == V4L2_PIX_FMT_H264) {
2121 u32 left_right;
2122 u32 top_bottom;
2123
2124 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2125 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2126
2127 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2128 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2129 q_data_dst->rect.width = width - q_data_dst->rect.left -
2130 (left_right & 0x3ff);
2131 q_data_dst->rect.height = height - q_data_dst->rect.top -
2132 (top_bottom & 0x3ff);
2133 }
2134
Philipp Zabel918c66f2013-06-27 06:59:01 -03002135 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2136 if (ret < 0)
2137 return ret;
2138
2139 /* Tell the decoder how many frame buffers we allocated. */
2140 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2141 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2142
2143 if (dev->devtype->product != CODA_DX6) {
2144 /* Set secondary AXI IRAM */
2145 coda_setup_iram(ctx);
2146
2147 coda_write(dev, ctx->iram_info.buf_bit_use,
2148 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2149 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2150 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2151 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2152 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2153 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2154 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2155 coda_write(dev, ctx->iram_info.buf_ovl_use,
2156 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002157 if (dev->devtype->product == CODA_960)
2158 coda_write(dev, ctx->iram_info.buf_btp_use,
2159 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2160 }
2161
2162 if (dev->devtype->product == CODA_960) {
2163 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2164
2165 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2166 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2167 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2168 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2169 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2170 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002171 }
2172
2173 if (src_fourcc == V4L2_PIX_FMT_H264) {
2174 coda_write(dev, ctx->slicebuf.paddr,
2175 CODA_CMD_SET_FRAME_SLICE_BB_START);
2176 coda_write(dev, ctx->slicebuf.size / 1024,
2177 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2178 }
2179
2180 if (dev->devtype->product == CODA_7541) {
2181 int max_mb_x = 1920 / 16;
2182 int max_mb_y = 1088 / 16;
2183 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002184
Philipp Zabel918c66f2013-06-27 06:59:01 -03002185 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2186 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002187 } else if (dev->devtype->product == CODA_960) {
2188 int max_mb_x = 1920 / 16;
2189 int max_mb_y = 1088 / 16;
2190 int max_mb_num = max_mb_x * max_mb_y;
2191
2192 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2193 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002194 }
2195
2196 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2197 v4l2_err(&ctx->dev->v4l2_dev,
2198 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2199 return -ETIMEDOUT;
2200 }
2201
2202 return 0;
2203}
2204
Philipp Zabeld35167a2013-05-23 10:42:59 -03002205static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2206 int header_code, u8 *header, int *size)
2207{
2208 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002209 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002210 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002211 int i;
2212
2213 if (dev->devtype->product == CODA_960)
2214 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002215
2216 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2217 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002218 bufsize = vb2_plane_size(buf, 0);
2219 if (dev->devtype->product == CODA_960)
2220 bufsize /= 1024;
2221 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002222 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2223 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2224 if (ret < 0) {
2225 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2226 return ret;
2227 }
Philipp Zabel89548442014-07-11 06:36:17 -03002228
2229 if (dev->devtype->product == CODA_960) {
2230 for (i = 63; i > 0; i--)
2231 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2232 break;
2233 *size = i + 1;
2234 } else {
2235 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2236 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2237 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002238 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2239
2240 return 0;
2241}
2242
Philipp Zabel89548442014-07-11 06:36:17 -03002243static int coda_start_encoding(struct coda_ctx *ctx);
2244
Javier Martin186b2502012-07-26 05:53:35 -03002245static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2246{
2247 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2248 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002249 struct coda_dev *dev = ctx->dev;
2250 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002251 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002252 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002253
Philipp Zabelb96904e2013-05-23 10:42:58 -03002254 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002255 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2256 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2257 if (coda_get_bitstream_payload(ctx) < 512)
2258 return -EINVAL;
2259 } else {
2260 if (count < 1)
2261 return -EINVAL;
2262 }
2263
2264 ctx->streamon_out = 1;
2265
Philipp Zabelb96904e2013-05-23 10:42:58 -03002266 if (coda_format_is_yuv(q_data_src->fourcc))
2267 ctx->inst_type = CODA_INST_ENCODER;
2268 else
2269 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002270 } else {
2271 if (count < 1)
2272 return -EINVAL;
2273
2274 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002275 }
Javier Martin186b2502012-07-26 05:53:35 -03002276
Philipp Zabelb96904e2013-05-23 10:42:58 -03002277 /* Don't start the coda unless both queues are on */
2278 if (!(ctx->streamon_out & ctx->streamon_cap))
2279 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002280
Philipp Zabeleb107512013-09-30 10:34:45 -03002281 /* Allow decoder device_run with no new buffers queued */
2282 if (ctx->inst_type == CODA_INST_DECODER)
2283 v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002284
Philipp Zabelb96904e2013-05-23 10:42:58 -03002285 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002286 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002287 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002288
Philipp Zabelb96904e2013-05-23 10:42:58 -03002289 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2290 q_data_dst->fourcc);
2291 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002292 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2293 return -EINVAL;
2294 }
2295
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002296 /* Allocate per-instance buffers */
2297 ret = coda_alloc_context_buffers(ctx, q_data_src);
2298 if (ret < 0)
2299 return ret;
2300
Philipp Zabel918c66f2013-06-27 06:59:01 -03002301 if (ctx->inst_type == CODA_INST_DECODER) {
2302 mutex_lock(&dev->coda_mutex);
2303 ret = coda_start_decoding(ctx);
2304 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002305 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002306 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002307 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002308 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002309 } else {
2310 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002311 }
2312
Philipp Zabel89548442014-07-11 06:36:17 -03002313 ctx->initialized = 1;
2314 return ret;
2315}
2316
2317static int coda_start_encoding(struct coda_ctx *ctx)
2318{
2319 struct coda_dev *dev = ctx->dev;
2320 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2321 struct coda_q_data *q_data_src, *q_data_dst;
2322 u32 bitstream_buf, bitstream_size;
2323 struct vb2_buffer *buf;
2324 int gamma, ret, value;
2325 u32 dst_fourcc;
2326
2327 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2328 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2329 dst_fourcc = q_data_dst->fourcc;
2330
2331 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2332 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2333 bitstream_size = q_data_dst->sizeimage;
2334
Javier Martin186b2502012-07-26 05:53:35 -03002335 if (!coda_is_initialized(dev)) {
2336 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2337 return -EFAULT;
2338 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002339
2340 mutex_lock(&dev->coda_mutex);
2341
Javier Martin186b2502012-07-26 05:53:35 -03002342 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002343 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2344 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002345 switch (dev->devtype->product) {
2346 case CODA_DX6:
2347 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2348 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2349 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002350 case CODA_960:
2351 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2352 /* fallthrough */
2353 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002354 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2355 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002356 break;
Javier Martin186b2502012-07-26 05:53:35 -03002357 }
2358
Philipp Zabel89548442014-07-11 06:36:17 -03002359 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2360 value &= ~(1 << 2 | 0x7 << 9);
2361 ctx->frame_mem_ctrl = value;
2362 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2363
Philipp Zabel10436672012-07-02 09:03:55 -03002364 if (dev->devtype->product == CODA_DX6) {
2365 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002366 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002367 }
Javier Martin186b2502012-07-26 05:53:35 -03002368
2369 /* Could set rotation here if needed */
2370 switch (dev->devtype->product) {
2371 case CODA_DX6:
2372 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002373 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002374 break;
2375 default:
2376 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002377 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002378 }
Javier Martin186b2502012-07-26 05:53:35 -03002379 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2380 coda_write(dev, ctx->params.framerate,
2381 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2382
Philipp Zabelb96904e2013-05-23 10:42:58 -03002383 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002384 switch (dst_fourcc) {
2385 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002386 if (dev->devtype->product == CODA_960)
2387 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2388 else
2389 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002390 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2391 break;
2392 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002393 if (dev->devtype->product == CODA_960)
2394 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2395 else
2396 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002397 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2398 break;
2399 default:
2400 v4l2_err(v4l2_dev,
2401 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002402 ret = -EINVAL;
2403 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002404 }
2405
Philipp Zabelc566c782012-08-08 11:59:38 -03002406 switch (ctx->params.slice_mode) {
2407 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2408 value = 0;
2409 break;
2410 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2411 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2412 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002413 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002414 break;
2415 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2416 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2417 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2418 value |= 1 & CODA_SLICING_MODE_MASK;
2419 break;
2420 }
Javier Martin186b2502012-07-26 05:53:35 -03002421 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002422 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002423 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2424
2425 if (ctx->params.bitrate) {
2426 /* Rate control enabled */
2427 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2428 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002429 if (dev->devtype->product == CODA_960)
2430 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002431 } else {
2432 value = 0;
2433 }
2434 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2435
2436 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2437 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2438
2439 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2440 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2441
Javier Martin186b2502012-07-26 05:53:35 -03002442
Philipp Zabel89548442014-07-11 06:36:17 -03002443 value = 0;
2444 if (dev->devtype->product == CODA_960)
2445 gamma = CODA9_DEFAULT_GAMMA;
2446 else
2447 gamma = CODA_DEFAULT_GAMMA;
2448 if (gamma > 0) {
2449 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2450 CODA_CMD_ENC_SEQ_RC_GAMMA);
2451 }
2452 if (dev->devtype->product == CODA_960) {
2453 if (CODA_DEFAULT_GAMMA > 0)
2454 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002455 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002456 if (CODA_DEFAULT_GAMMA > 0) {
2457 if (dev->devtype->product == CODA_DX6)
2458 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2459 else
2460 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2461 }
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002462 }
Javier Martin186b2502012-07-26 05:53:35 -03002463 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2464
Philipp Zabel89548442014-07-11 06:36:17 -03002465 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2466
Philipp Zabelc2d22512013-06-21 03:55:28 -03002467 coda_setup_iram(ctx);
2468
Javier Martin186b2502012-07-26 05:53:35 -03002469 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002470 switch (dev->devtype->product) {
2471 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002472 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002473 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002474 break;
2475 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002476 coda_write(dev, ctx->iram_info.search_ram_paddr,
2477 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2478 coda_write(dev, ctx->iram_info.search_ram_size,
2479 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002480 break;
2481 case CODA_960:
2482 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2483 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002484 }
Javier Martin186b2502012-07-26 05:53:35 -03002485 }
2486
Philipp Zabelfcb62822013-05-23 10:43:00 -03002487 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2488 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002489 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002490 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002491 }
2492
Philipp Zabelfcb62822013-05-23 10:43:00 -03002493 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2494 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2495 ret = -EFAULT;
2496 goto out;
2497 }
Javier Martin186b2502012-07-26 05:53:35 -03002498
Philipp Zabel89548442014-07-11 06:36:17 -03002499 if (dev->devtype->product == CODA_960)
2500 ctx->num_internal_frames = 4;
2501 else
2502 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002503 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002504 if (ret < 0) {
2505 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2506 goto out;
2507 }
Javier Martin186b2502012-07-26 05:53:35 -03002508
Philipp Zabelec25f682012-07-20 08:54:29 -03002509 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002510 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002511 if (dev->devtype->product == CODA_7541)
2512 coda_write(dev, round_up(q_data_src->width, 8),
2513 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002514 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002515 coda_write(dev, ctx->iram_info.buf_bit_use,
2516 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2517 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2518 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2519 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2520 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2521 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2522 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2523 coda_write(dev, ctx->iram_info.buf_ovl_use,
2524 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002525 if (dev->devtype->product == CODA_960) {
2526 coda_write(dev, ctx->iram_info.buf_btp_use,
2527 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2528
2529 /* FIXME */
2530 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2531 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2532 }
Philipp Zabel10436672012-07-02 09:03:55 -03002533 }
Philipp Zabel89548442014-07-11 06:36:17 -03002534
Philipp Zabelfcb62822013-05-23 10:43:00 -03002535 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2536 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002537 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002538 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002539 }
2540
2541 /* Save stream headers */
2542 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2543 switch (dst_fourcc) {
2544 case V4L2_PIX_FMT_H264:
2545 /*
2546 * Get SPS in the first frame and copy it to an
2547 * intermediate buffer.
2548 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002549 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2550 &ctx->vpu_header[0][0],
2551 &ctx->vpu_header_size[0]);
2552 if (ret < 0)
2553 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002554
2555 /*
2556 * Get PPS in the first frame and copy it to an
2557 * intermediate buffer.
2558 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002559 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2560 &ctx->vpu_header[1][0],
2561 &ctx->vpu_header_size[1]);
2562 if (ret < 0)
2563 goto out;
2564
Javier Martin3f3f5c72012-10-29 05:20:29 -03002565 /*
2566 * Length of H.264 headers is variable and thus it might not be
2567 * aligned for the coda to append the encoded frame. In that is
2568 * the case a filler NAL must be added to header 2.
2569 */
2570 ctx->vpu_header_size[2] = coda_h264_padding(
2571 (ctx->vpu_header_size[0] +
2572 ctx->vpu_header_size[1]),
2573 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002574 break;
2575 case V4L2_PIX_FMT_MPEG4:
2576 /*
2577 * Get VOS in the first frame and copy it to an
2578 * intermediate buffer
2579 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002580 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2581 &ctx->vpu_header[0][0],
2582 &ctx->vpu_header_size[0]);
2583 if (ret < 0)
2584 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002585
Philipp Zabeld35167a2013-05-23 10:42:59 -03002586 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2587 &ctx->vpu_header[1][0],
2588 &ctx->vpu_header_size[1]);
2589 if (ret < 0)
2590 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002591
Philipp Zabeld35167a2013-05-23 10:42:59 -03002592 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2593 &ctx->vpu_header[2][0],
2594 &ctx->vpu_header_size[2]);
2595 if (ret < 0)
2596 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002597 break;
2598 default:
2599 /* No more formats need to save headers at the moment */
2600 break;
2601 }
2602
Philipp Zabeld35167a2013-05-23 10:42:59 -03002603out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002604 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002605 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002606}
2607
Hans Verkuile37559b2014-04-17 02:47:21 -03002608static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002609{
2610 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002611 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002612
2613 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002614 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002615 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002616 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002617
Philipp Zabel89548442014-07-11 06:36:17 -03002618 if (ctx->inst_type == CODA_INST_DECODER &&
2619 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2620 /* if this decoder instance is running, set the stream end flag */
2621 if (dev->devtype->product == CODA_960) {
2622 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2623
2624 val |= CODA_BIT_STREAM_END_FLAG;
2625 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2626 ctx->bit_stream_param = val;
2627 }
2628 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002629 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2630
2631 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002632 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002633 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002634 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002635 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002636
2637 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002638 }
2639
Philipp Zabel918c66f2013-06-27 06:59:01 -03002640 if (!ctx->streamon_out && !ctx->streamon_cap) {
2641 kfifo_init(&ctx->bitstream_fifo,
2642 ctx->bitstream.vaddr, ctx->bitstream.size);
2643 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002644 }
Javier Martin186b2502012-07-26 05:53:35 -03002645}
2646
2647static struct vb2_ops coda_qops = {
2648 .queue_setup = coda_queue_setup,
2649 .buf_prepare = coda_buf_prepare,
2650 .buf_queue = coda_buf_queue,
2651 .wait_prepare = coda_wait_prepare,
2652 .wait_finish = coda_wait_finish,
2653 .start_streaming = coda_start_streaming,
2654 .stop_streaming = coda_stop_streaming,
2655};
2656
2657static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2658{
2659 struct coda_ctx *ctx =
2660 container_of(ctrl->handler, struct coda_ctx, ctrls);
2661
2662 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2663 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2664
2665 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002666 case V4L2_CID_HFLIP:
2667 if (ctrl->val)
2668 ctx->params.rot_mode |= CODA_MIR_HOR;
2669 else
2670 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2671 break;
2672 case V4L2_CID_VFLIP:
2673 if (ctrl->val)
2674 ctx->params.rot_mode |= CODA_MIR_VER;
2675 else
2676 ctx->params.rot_mode &= ~CODA_MIR_VER;
2677 break;
Javier Martin186b2502012-07-26 05:53:35 -03002678 case V4L2_CID_MPEG_VIDEO_BITRATE:
2679 ctx->params.bitrate = ctrl->val / 1000;
2680 break;
2681 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2682 ctx->params.gop_size = ctrl->val;
2683 break;
2684 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2685 ctx->params.h264_intra_qp = ctrl->val;
2686 break;
2687 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2688 ctx->params.h264_inter_qp = ctrl->val;
2689 break;
2690 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2691 ctx->params.mpeg4_intra_qp = ctrl->val;
2692 break;
2693 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2694 ctx->params.mpeg4_inter_qp = ctrl->val;
2695 break;
2696 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2697 ctx->params.slice_mode = ctrl->val;
2698 break;
2699 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2700 ctx->params.slice_max_mb = ctrl->val;
2701 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002702 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2703 ctx->params.slice_max_bits = ctrl->val * 8;
2704 break;
Javier Martin186b2502012-07-26 05:53:35 -03002705 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2706 break;
2707 default:
2708 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2709 "Invalid control, id=%d, val=%d\n",
2710 ctrl->id, ctrl->val);
2711 return -EINVAL;
2712 }
2713
2714 return 0;
2715}
2716
2717static struct v4l2_ctrl_ops coda_ctrl_ops = {
2718 .s_ctrl = coda_s_ctrl,
2719};
2720
2721static int coda_ctrls_setup(struct coda_ctx *ctx)
2722{
2723 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2724
2725 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002726 V4L2_CID_HFLIP, 0, 1, 1, 0);
2727 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2728 V4L2_CID_VFLIP, 0, 1, 1, 0);
2729 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002730 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2731 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2732 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2733 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002734 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002735 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002736 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002737 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2738 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2739 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2740 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2741 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2742 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002743 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2744 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002745 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2746 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002747 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2748 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002749 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2750 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2751 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2752 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2753 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2754
2755 if (ctx->ctrls.error) {
2756 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2757 ctx->ctrls.error);
2758 return -EINVAL;
2759 }
2760
2761 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2762}
2763
2764static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2765 struct vb2_queue *dst_vq)
2766{
2767 struct coda_ctx *ctx = priv;
2768 int ret;
2769
Javier Martin186b2502012-07-26 05:53:35 -03002770 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002771 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002772 src_vq->drv_priv = ctx;
2773 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2774 src_vq->ops = &coda_qops;
2775 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002776 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002777
2778 ret = vb2_queue_init(src_vq);
2779 if (ret)
2780 return ret;
2781
Javier Martin186b2502012-07-26 05:53:35 -03002782 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002783 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002784 dst_vq->drv_priv = ctx;
2785 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2786 dst_vq->ops = &coda_qops;
2787 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002788 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002789
2790 return vb2_queue_init(dst_vq);
2791}
2792
Philipp Zabele11f3e62012-07-25 09:16:58 -03002793static int coda_next_free_instance(struct coda_dev *dev)
2794{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002795 int idx = ffz(dev->instance_mask);
2796
2797 if ((idx < 0) ||
2798 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2799 return -EBUSY;
2800
2801 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002802}
2803
Javier Martin186b2502012-07-26 05:53:35 -03002804static int coda_open(struct file *file)
2805{
2806 struct coda_dev *dev = video_drvdata(file);
2807 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002808 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002809 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002810
Javier Martin186b2502012-07-26 05:53:35 -03002811 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2812 if (!ctx)
2813 return -ENOMEM;
2814
Fabio Estevamf82bc202013-08-21 11:14:16 -03002815 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002816 if (idx < 0) {
2817 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002818 goto err_coda_max;
2819 }
2820 set_bit(idx, &dev->instance_mask);
2821
Philipp Zabel32b6f202014-07-11 06:36:20 -03002822 init_completion(&ctx->completion);
2823 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2824 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002825 v4l2_fh_init(&ctx->fh, video_devdata(file));
2826 file->private_data = &ctx->fh;
2827 v4l2_fh_add(&ctx->fh);
2828 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002829 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002830 switch (dev->devtype->product) {
2831 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002832 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002833 ctx->reg_idx = 0;
2834 break;
2835 default:
2836 ctx->reg_idx = idx;
2837 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002838
Fabio Estevam79830db2013-08-21 11:14:17 -03002839 ret = clk_prepare_enable(dev->clk_per);
2840 if (ret)
2841 goto err_clk_per;
2842
2843 ret = clk_prepare_enable(dev->clk_ahb);
2844 if (ret)
2845 goto err_clk_ahb;
2846
Javier Martin186b2502012-07-26 05:53:35 -03002847 set_default_params(ctx);
2848 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2849 &coda_queue_init);
2850 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03002851 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002852
2853 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2854 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002855 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002856 }
2857 ret = coda_ctrls_setup(ctx);
2858 if (ret) {
2859 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002860 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002861 }
2862
2863 ctx->fh.ctrl_handler = &ctx->ctrls;
2864
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002865 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2866 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002867 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002868 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002869 }
2870
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002871 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2872 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2873 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2874 if (!ctx->bitstream.vaddr) {
2875 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2876 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002877 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002878 }
2879 kfifo_init(&ctx->bitstream_fifo,
2880 ctx->bitstream.vaddr, ctx->bitstream.size);
2881 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002882 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002883
Javier Martin186b2502012-07-26 05:53:35 -03002884 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002885 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002886 coda_unlock(ctx);
2887
Javier Martin186b2502012-07-26 05:53:35 -03002888 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2889 ctx->idx, ctx);
2890
2891 return 0;
2892
Fabio Estevamf82bc202013-08-21 11:14:16 -03002893err_dma_writecombine:
2894 coda_free_context_buffers(ctx);
2895 if (ctx->dev->devtype->product == CODA_DX6)
2896 coda_free_aux_buf(dev, &ctx->workbuf);
2897 coda_free_aux_buf(dev, &ctx->parabuf);
2898err_dma_alloc:
2899 v4l2_ctrl_handler_free(&ctx->ctrls);
2900err_ctrls_setup:
2901 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2902err_ctx_init:
2903 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002904err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002905 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002906err_clk_per:
Javier Martin186b2502012-07-26 05:53:35 -03002907 v4l2_fh_del(&ctx->fh);
2908 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002909 clear_bit(ctx->idx, &dev->instance_mask);
2910err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002911 kfree(ctx);
2912 return ret;
2913}
2914
2915static int coda_release(struct file *file)
2916{
2917 struct coda_dev *dev = video_drvdata(file);
2918 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2919
2920 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2921 ctx);
2922
Philipp Zabel918c66f2013-06-27 06:59:01 -03002923 /* If this instance is running, call .job_abort and wait for it to end */
2924 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2925
2926 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002927 if (ctx->initialized) {
2928 queue_work(dev->workqueue, &ctx->seq_end_work);
2929 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002930 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002931
2932 coda_free_framebuffers(ctx);
2933
Javier Martin186b2502012-07-26 05:53:35 -03002934 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002935 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002936 coda_unlock(ctx);
2937
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002938 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2939 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002940 coda_free_context_buffers(ctx);
2941 if (ctx->dev->devtype->product == CODA_DX6)
2942 coda_free_aux_buf(dev, &ctx->workbuf);
2943
2944 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002945 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002946 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002947 clk_disable_unprepare(dev->clk_per);
Javier Martin186b2502012-07-26 05:53:35 -03002948 v4l2_fh_del(&ctx->fh);
2949 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002950 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002951 kfree(ctx);
2952
2953 return 0;
2954}
2955
2956static unsigned int coda_poll(struct file *file,
2957 struct poll_table_struct *wait)
2958{
2959 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2960 int ret;
2961
2962 coda_lock(ctx);
2963 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
2964 coda_unlock(ctx);
2965 return ret;
2966}
2967
2968static int coda_mmap(struct file *file, struct vm_area_struct *vma)
2969{
2970 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2971
2972 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2973}
2974
2975static const struct v4l2_file_operations coda_fops = {
2976 .owner = THIS_MODULE,
2977 .open = coda_open,
2978 .release = coda_release,
2979 .poll = coda_poll,
2980 .unlocked_ioctl = video_ioctl2,
2981 .mmap = coda_mmap,
2982};
2983
Philipp Zabel918c66f2013-06-27 06:59:01 -03002984static void coda_finish_decode(struct coda_ctx *ctx)
2985{
2986 struct coda_dev *dev = ctx->dev;
2987 struct coda_q_data *q_data_src;
2988 struct coda_q_data *q_data_dst;
2989 struct vb2_buffer *dst_buf;
2990 int width, height;
2991 int decoded_idx;
2992 int display_idx;
2993 u32 src_fourcc;
2994 int success;
2995 u32 val;
2996
2997 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2998
2999 /* Update kfifo out pointer from coda bitstream read pointer */
3000 coda_kfifo_sync_from_device(ctx);
3001
3002 /*
3003 * in stream-end mode, the read pointer can overshoot the write pointer
3004 * by up to 512 bytes
3005 */
3006 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3007 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3008 kfifo_init(&ctx->bitstream_fifo,
3009 ctx->bitstream.vaddr, ctx->bitstream.size);
3010 }
3011
3012 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3013 src_fourcc = q_data_src->fourcc;
3014
3015 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3016 if (val != 1)
3017 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3018
3019 success = val & 0x1;
3020 if (!success)
3021 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3022
3023 if (src_fourcc == V4L2_PIX_FMT_H264) {
3024 if (val & (1 << 3))
3025 v4l2_err(&dev->v4l2_dev,
3026 "insufficient PS buffer space (%d bytes)\n",
3027 ctx->psbuf.size);
3028 if (val & (1 << 2))
3029 v4l2_err(&dev->v4l2_dev,
3030 "insufficient slice buffer space (%d bytes)\n",
3031 ctx->slicebuf.size);
3032 }
3033
3034 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3035 width = (val >> 16) & 0xffff;
3036 height = val & 0xffff;
3037
3038 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3039
Philipp Zabel52c41672014-07-11 06:36:19 -03003040 /* frame crop information */
3041 if (src_fourcc == V4L2_PIX_FMT_H264) {
3042 u32 left_right;
3043 u32 top_bottom;
3044
3045 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3046 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3047
3048 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3049 /* Keep current crop information */
3050 } else {
3051 struct v4l2_rect *rect = &q_data_dst->rect;
3052
3053 rect->left = left_right >> 16 & 0xffff;
3054 rect->top = top_bottom >> 16 & 0xffff;
3055 rect->width = width - rect->left -
3056 (left_right & 0xffff);
3057 rect->height = height - rect->top -
3058 (top_bottom & 0xffff);
3059 }
3060 } else {
3061 /* no cropping */
3062 }
3063
Philipp Zabel918c66f2013-06-27 06:59:01 -03003064 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3065 if (val > 0)
3066 v4l2_err(&dev->v4l2_dev,
3067 "errors in %d macroblocks\n", val);
3068
3069 if (dev->devtype->product == CODA_7541) {
3070 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3071 if (val == 0) {
3072 /* not enough bitstream data */
3073 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3074 "prescan failed: %d\n", val);
3075 ctx->prescan_failed = true;
3076 return;
3077 }
3078 }
3079
3080 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3081
3082 /*
3083 * The previous display frame was copied out by the rotator,
3084 * now it can be overwritten again
3085 */
3086 if (ctx->display_idx >= 0 &&
3087 ctx->display_idx < ctx->num_internal_frames) {
3088 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3089 coda_write(dev, ctx->frm_dis_flg,
3090 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3091 }
3092
3093 /*
3094 * The index of the last decoded frame, not necessarily in
3095 * display order, and the index of the next display frame.
3096 * The latter could have been decoded in a previous run.
3097 */
3098 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3099 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3100
3101 if (decoded_idx == -1) {
3102 /* no frame was decoded, but we might have a display frame */
3103 if (display_idx < 0 && ctx->display_idx < 0)
3104 ctx->prescan_failed = true;
3105 } else if (decoded_idx == -2) {
3106 /* no frame was decoded, we still return the remaining buffers */
3107 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3108 v4l2_err(&dev->v4l2_dev,
3109 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003110 } else {
3111 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3112 if (val == 0)
3113 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3114 else if (val == 1)
3115 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3116 else
3117 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003118 }
3119
3120 if (display_idx == -1) {
3121 /*
3122 * no more frames to be decoded, but there could still
3123 * be rotator output to dequeue
3124 */
3125 ctx->prescan_failed = true;
3126 } else if (display_idx == -3) {
3127 /* possibly prescan failure */
3128 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3129 v4l2_err(&dev->v4l2_dev,
3130 "presentation frame index out of range: %d\n",
3131 display_idx);
3132 }
3133
3134 /* If a frame was copied out, return it */
3135 if (ctx->display_idx >= 0 &&
3136 ctx->display_idx < ctx->num_internal_frames) {
3137 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
3138 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3139
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003140 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3141 V4L2_BUF_FLAG_PFRAME |
3142 V4L2_BUF_FLAG_BFRAME);
3143 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
3144
Philipp Zabel918c66f2013-06-27 06:59:01 -03003145 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3146
3147 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3148 VB2_BUF_STATE_ERROR);
3149
3150 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3151 "job finished: decoding frame (%d) (%s)\n",
3152 dst_buf->v4l2_buf.sequence,
3153 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3154 "KEYFRAME" : "PFRAME");
3155 } else {
3156 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3157 "job finished: no frame decoded\n");
3158 }
3159
3160 /* The rotator will copy the current display frame next time */
3161 ctx->display_idx = display_idx;
3162}
3163
3164static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003165{
Philipp Zabelec25f682012-07-20 08:54:29 -03003166 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003167 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003168 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003169
Philipp Zabelec25f682012-07-20 08:54:29 -03003170 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03003171 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003172
3173 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003174 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003175 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3176
Javier Martin186b2502012-07-26 05:53:35 -03003177 /* Calculate bytesused field */
3178 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003179 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3180 ctx->vpu_header_size[0] +
3181 ctx->vpu_header_size[1] +
3182 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003183 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003184 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003185 }
3186
3187 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3188 wr_ptr - start_ptr);
3189
3190 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3191 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3192
Philipp Zabel51660282013-09-30 10:34:49 -03003193 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003194 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3195 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3196 } else {
3197 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3198 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3199 }
3200
Kamil Debskiccd571c2013-04-24 10:38:24 -03003201 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003202 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3203 dst_buf->v4l2_buf.flags |=
3204 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003205 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3206
Philipp Zabelec25f682012-07-20 08:54:29 -03003207 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003208
3209 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003210 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3211
3212 ctx->gopcounter--;
3213 if (ctx->gopcounter < 0)
3214 ctx->gopcounter = ctx->params.gop_size - 1;
3215
3216 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3217 "job finished: encoding frame (%d) (%s)\n",
3218 dst_buf->v4l2_buf.sequence,
3219 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3220 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003221}
3222
3223static irqreturn_t coda_irq_handler(int irq, void *data)
3224{
3225 struct coda_dev *dev = data;
3226 struct coda_ctx *ctx;
3227
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003228 /* read status register to attend the IRQ */
3229 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3230 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3231 CODA_REG_BIT_INT_CLEAR);
3232
3233 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3234 if (ctx == NULL) {
3235 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3236 mutex_unlock(&dev->coda_mutex);
3237 return IRQ_HANDLED;
3238 }
3239
3240 if (ctx->aborting) {
3241 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3242 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003243 }
3244
3245 if (coda_isbusy(ctx->dev)) {
3246 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3247 "coda is still busy!!!!\n");
3248 return IRQ_NONE;
3249 }
3250
Philipp Zabel32b6f202014-07-11 06:36:20 -03003251 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003252
3253 return IRQ_HANDLED;
3254}
3255
3256static u32 coda_supported_firmwares[] = {
3257 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003258 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003259 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003260};
3261
3262static bool coda_firmware_supported(u32 vernum)
3263{
3264 int i;
3265
3266 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3267 if (vernum == coda_supported_firmwares[i])
3268 return true;
3269 return false;
3270}
3271
Philipp Zabel87048bb2012-07-02 07:03:43 -03003272static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003273{
3274 u16 product, major, minor, release;
3275 u32 data;
3276 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003277 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003278
Fabio Estevam79830db2013-08-21 11:14:17 -03003279 ret = clk_prepare_enable(dev->clk_per);
3280 if (ret)
3281 return ret;
3282
3283 ret = clk_prepare_enable(dev->clk_ahb);
3284 if (ret)
3285 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003286
Javier Martin186b2502012-07-26 05:53:35 -03003287 /*
3288 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003289 * The 16-bit chars in the code buffer are in memory access
3290 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003291 * Data in this SRAM survives a reboot.
3292 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003293 p = (u16 *)dev->codebuf.vaddr;
3294 if (dev->devtype->product == CODA_DX6) {
3295 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3296 data = CODA_DOWN_ADDRESS_SET(i) |
3297 CODA_DOWN_DATA_SET(p[i ^ 1]);
3298 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3299 }
3300 } else {
3301 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3302 data = CODA_DOWN_ADDRESS_SET(i) |
3303 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3304 3 - (i % 4)]);
3305 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3306 }
Javier Martin186b2502012-07-26 05:53:35 -03003307 }
Javier Martin186b2502012-07-26 05:53:35 -03003308
Philipp Zabel9acf7692013-05-23 10:42:56 -03003309 /* Clear registers */
3310 for (i = 0; i < 64; i++)
3311 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3312
Javier Martin186b2502012-07-26 05:53:35 -03003313 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003314 if (dev->devtype->product == CODA_960 ||
3315 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003316 coda_write(dev, dev->tempbuf.paddr,
3317 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003318 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003319 } else {
3320 coda_write(dev, dev->workbuf.paddr,
3321 CODA_REG_BIT_WORK_BUF_ADDR);
3322 }
Javier Martin186b2502012-07-26 05:53:35 -03003323 coda_write(dev, dev->codebuf.paddr,
3324 CODA_REG_BIT_CODE_BUF_ADDR);
3325 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3326
3327 /* Set default values */
3328 switch (dev->devtype->product) {
3329 case CODA_DX6:
3330 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3331 break;
3332 default:
3333 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3334 }
Philipp Zabel89548442014-07-11 06:36:17 -03003335 if (dev->devtype->product == CODA_960)
3336 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3337 else
3338 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003339
3340 if (dev->devtype->product != CODA_DX6)
3341 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3342
Javier Martin186b2502012-07-26 05:53:35 -03003343 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3344 CODA_REG_BIT_INT_ENABLE);
3345
3346 /* Reset VPU and start processor */
3347 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3348 data |= CODA_REG_RESET_ENABLE;
3349 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3350 udelay(10);
3351 data &= ~CODA_REG_RESET_ENABLE;
3352 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3353 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3354
3355 /* Load firmware */
3356 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3357 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3358 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3359 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3360 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3361 if (coda_wait_timeout(dev)) {
3362 clk_disable_unprepare(dev->clk_per);
3363 clk_disable_unprepare(dev->clk_ahb);
3364 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
3365 return -EIO;
3366 }
3367
Philipp Zabel89548442014-07-11 06:36:17 -03003368 if (dev->devtype->product == CODA_960) {
3369 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3370 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3371 data);
3372 }
3373
Javier Martin186b2502012-07-26 05:53:35 -03003374 /* Check we are compatible with the loaded firmware */
3375 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3376 product = CODA_FIRMWARE_PRODUCT(data);
3377 major = CODA_FIRMWARE_MAJOR(data);
3378 minor = CODA_FIRMWARE_MINOR(data);
3379 release = CODA_FIRMWARE_RELEASE(data);
3380
3381 clk_disable_unprepare(dev->clk_per);
3382 clk_disable_unprepare(dev->clk_ahb);
3383
3384 if (product != dev->devtype->product) {
3385 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3386 " Version: %u.%u.%u\n",
3387 coda_product_name(dev->devtype->product),
3388 coda_product_name(product), major, minor, release);
3389 return -EINVAL;
3390 }
3391
3392 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3393 coda_product_name(product));
3394
3395 if (coda_firmware_supported(data)) {
3396 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3397 major, minor, release);
3398 } else {
3399 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3400 "%u.%u.%u\n", major, minor, release);
3401 }
3402
3403 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003404
3405err_clk_ahb:
3406 clk_disable_unprepare(dev->clk_per);
3407 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003408}
3409
3410static void coda_fw_callback(const struct firmware *fw, void *context)
3411{
3412 struct coda_dev *dev = context;
3413 struct platform_device *pdev = dev->plat_dev;
3414 int ret;
3415
3416 if (!fw) {
3417 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3418 return;
3419 }
3420
3421 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003422 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3423 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003424 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3425 return;
3426 }
3427
Philipp Zabel87048bb2012-07-02 07:03:43 -03003428 /* Copy the whole firmware image to the code buffer */
3429 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3430 release_firmware(fw);
3431
3432 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03003433 if (ret) {
3434 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3435 return;
3436 }
3437
3438 dev->vfd.fops = &coda_fops,
3439 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3440 dev->vfd.release = video_device_release_empty,
3441 dev->vfd.lock = &dev->dev_mutex;
3442 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003443 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003444 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3445 video_set_drvdata(&dev->vfd, dev);
3446
3447 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3448 if (IS_ERR(dev->alloc_ctx)) {
3449 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3450 return;
3451 }
3452
3453 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3454 if (IS_ERR(dev->m2m_dev)) {
3455 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3456 goto rel_ctx;
3457 }
3458
3459 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3460 if (ret) {
3461 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3462 goto rel_m2m;
3463 }
3464 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3465 dev->vfd.num);
3466
3467 return;
3468
3469rel_m2m:
3470 v4l2_m2m_release(dev->m2m_dev);
3471rel_ctx:
3472 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3473}
3474
3475static int coda_firmware_request(struct coda_dev *dev)
3476{
3477 char *fw = dev->devtype->firmware;
3478
3479 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3480 coda_product_name(dev->devtype->product));
3481
3482 return request_firmware_nowait(THIS_MODULE, true,
3483 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3484}
3485
3486enum coda_platform {
3487 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003488 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003489 CODA_IMX6Q,
3490 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003491};
3492
Emil Goodec06d8752012-08-14 17:44:42 -03003493static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003494 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003495 .firmware = "v4l-codadx6-imx27.bin",
3496 .product = CODA_DX6,
3497 .codecs = codadx6_codecs,
3498 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003499 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003500 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003501 .firmware = "v4l-coda7541-imx53.bin",
3502 .product = CODA_7541,
3503 .codecs = coda7_codecs,
3504 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003505 },
Philipp Zabel89548442014-07-11 06:36:17 -03003506 [CODA_IMX6Q] = {
3507 .firmware = "v4l-coda960-imx6q.bin",
3508 .product = CODA_960,
3509 .codecs = coda9_codecs,
3510 .num_codecs = ARRAY_SIZE(coda9_codecs),
3511 },
3512 [CODA_IMX6DL] = {
3513 .firmware = "v4l-coda960-imx6dl.bin",
3514 .product = CODA_960,
3515 .codecs = coda9_codecs,
3516 .num_codecs = ARRAY_SIZE(coda9_codecs),
3517 },
Javier Martin186b2502012-07-26 05:53:35 -03003518};
3519
3520static struct platform_device_id coda_platform_ids[] = {
3521 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003522 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003523 { /* sentinel */ }
3524};
3525MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3526
3527#ifdef CONFIG_OF
3528static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003529 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003530 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003531 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3532 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003533 { /* sentinel */ }
3534};
3535MODULE_DEVICE_TABLE(of, coda_dt_ids);
3536#endif
3537
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003538static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003539{
3540 const struct of_device_id *of_id =
3541 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3542 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003543 struct coda_platform_data *pdata = pdev->dev.platform_data;
3544 struct device_node *np = pdev->dev.of_node;
3545 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003546 struct coda_dev *dev;
3547 struct resource *res;
3548 int ret, irq;
3549
3550 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3551 if (!dev) {
3552 dev_err(&pdev->dev, "Not enough memory for %s\n",
3553 CODA_NAME);
3554 return -ENOMEM;
3555 }
3556
3557 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003558 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003559
3560 dev->plat_dev = pdev;
3561 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3562 if (IS_ERR(dev->clk_per)) {
3563 dev_err(&pdev->dev, "Could not get per clock\n");
3564 return PTR_ERR(dev->clk_per);
3565 }
3566
3567 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3568 if (IS_ERR(dev->clk_ahb)) {
3569 dev_err(&pdev->dev, "Could not get ahb clock\n");
3570 return PTR_ERR(dev->clk_ahb);
3571 }
3572
3573 /* Get memory for physical registers */
3574 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003575 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3576 if (IS_ERR(dev->regs_base))
3577 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003578
3579 /* IRQ */
3580 irq = platform_get_irq(pdev, 0);
3581 if (irq < 0) {
3582 dev_err(&pdev->dev, "failed to get irq resource\n");
3583 return -ENOENT;
3584 }
3585
Philipp Zabel918c66f2013-06-27 06:59:01 -03003586 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003587 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003588 dev_err(&pdev->dev, "failed to request irq\n");
3589 return -ENOENT;
3590 }
3591
Philipp Zabel657eee72013-04-29 16:17:14 -07003592 /* Get IRAM pool from device tree or platform data */
3593 pool = of_get_named_gen_pool(np, "iram", 0);
3594 if (!pool && pdata)
3595 pool = dev_get_gen_pool(pdata->iram_dev);
3596 if (!pool) {
3597 dev_err(&pdev->dev, "iram pool not available\n");
3598 return -ENOMEM;
3599 }
3600 dev->iram_pool = pool;
3601
Javier Martin186b2502012-07-26 05:53:35 -03003602 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3603 if (ret)
3604 return ret;
3605
3606 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003607 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003608
3609 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3610
3611 if (of_id) {
3612 dev->devtype = of_id->data;
3613 } else if (pdev_id) {
3614 dev->devtype = &coda_devdata[pdev_id->driver_data];
3615 } else {
3616 v4l2_device_unregister(&dev->v4l2_dev);
3617 return -EINVAL;
3618 }
3619
3620 /* allocate auxiliary per-device buffers for the BIT processor */
3621 switch (dev->devtype->product) {
3622 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003623 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3624 CODADX6_WORK_BUF_SIZE);
3625 if (ret < 0) {
3626 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3627 v4l2_device_unregister(&dev->v4l2_dev);
3628 return ret;
3629 }
Javier Martin186b2502012-07-26 05:53:35 -03003630 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003631 case CODA_7541:
3632 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3633 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003634 case CODA_960:
3635 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3636 break;
Javier Martin186b2502012-07-26 05:53:35 -03003637 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003638 if (dev->tempbuf.size) {
3639 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3640 dev->tempbuf.size);
3641 if (ret < 0) {
3642 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3643 v4l2_device_unregister(&dev->v4l2_dev);
3644 return ret;
3645 }
Javier Martin186b2502012-07-26 05:53:35 -03003646 }
3647
Philipp Zabel918c66f2013-06-27 06:59:01 -03003648 switch (dev->devtype->product) {
3649 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003650 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003651 break;
3652 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003653 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003654 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003655 case CODA_960:
3656 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003657 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003658 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3659 &dev->iram.paddr);
3660 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003661 dev_err(&pdev->dev, "unable to alloc iram\n");
3662 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003663 }
3664
Philipp Zabel32b6f202014-07-11 06:36:20 -03003665 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3666 if (!dev->workqueue) {
3667 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3668 return -ENOMEM;
3669 }
3670
Javier Martin186b2502012-07-26 05:53:35 -03003671 platform_set_drvdata(pdev, dev);
3672
3673 return coda_firmware_request(dev);
3674}
3675
3676static int coda_remove(struct platform_device *pdev)
3677{
3678 struct coda_dev *dev = platform_get_drvdata(pdev);
3679
3680 video_unregister_device(&dev->vfd);
3681 if (dev->m2m_dev)
3682 v4l2_m2m_release(dev->m2m_dev);
3683 if (dev->alloc_ctx)
3684 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3685 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003686 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003687 if (dev->iram.vaddr)
3688 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3689 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003690 coda_free_aux_buf(dev, &dev->codebuf);
3691 coda_free_aux_buf(dev, &dev->tempbuf);
3692 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003693 return 0;
3694}
3695
3696static struct platform_driver coda_driver = {
3697 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003698 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003699 .driver = {
3700 .name = CODA_NAME,
3701 .owner = THIS_MODULE,
3702 .of_match_table = of_match_ptr(coda_dt_ids),
3703 },
3704 .id_table = coda_platform_ids,
3705};
3706
3707module_platform_driver(coda_driver);
3708
3709MODULE_LICENSE("GPL");
3710MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3711MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");