blob: 5ac455f532562c4432a5e059db952fed619f6465 [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>
Philipp Zabel1e172732014-07-11 06:36:23 -030025#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030026#include <linux/slab.h>
27#include <linux/videodev2.h>
28#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070029#include <linux/platform_data/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030030#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030031
32#include <media/v4l2-ctrls.h>
33#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030034#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030035#include <media/v4l2-ioctl.h>
36#include <media/v4l2-mem2mem.h>
37#include <media/videobuf2-core.h>
38#include <media/videobuf2-dma-contig.h>
39
40#include "coda.h"
41
42#define CODA_NAME "coda"
43
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030044#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030045
46#define CODA_FMO_BUF_SIZE 32
47#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030048#define CODA7_WORK_BUF_SIZE (128 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030049#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030050#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030051#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030052#define CODA_PARA_BUF_SIZE (10 * 1024)
53#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070054#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030055#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030056#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030057
Philipp Zabel918c66f2013-06-27 06:59:01 -030058#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030059#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030060
61#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030062
Philipp Zabelb96904e2013-05-23 10:42:58 -030063#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030064#define FMO_SLICE_SAVE_BUF_SIZE (32)
65#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030066#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030067
68#define MIN_W 176
69#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030070
71#define S_ALIGN 1 /* multiple of 2 */
72#define W_ALIGN 1 /* multiple of 2 */
73#define H_ALIGN 1 /* multiple of 2 */
74
75#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
76
77static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030078module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030079MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
80
81enum {
82 V4L2_M2M_SRC = 0,
83 V4L2_M2M_DST = 1,
84};
85
Javier Martin186b2502012-07-26 05:53:35 -030086enum coda_inst_type {
87 CODA_INST_ENCODER,
88 CODA_INST_DECODER,
89};
90
91enum coda_product {
92 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030093 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030094 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030095};
96
97struct coda_fmt {
98 char *name;
99 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300100};
101
102struct coda_codec {
103 u32 mode;
104 u32 src_fourcc;
105 u32 dst_fourcc;
106 u32 max_w;
107 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300108};
109
110struct coda_devtype {
111 char *firmware;
112 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300113 struct coda_codec *codecs;
114 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300115 size_t workbuf_size;
116};
117
118/* Per-queue, driver-specific private data */
119struct coda_q_data {
120 unsigned int width;
121 unsigned int height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300122 unsigned int bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300123 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300124 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300125 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300126};
127
128struct coda_aux_buf {
129 void *vaddr;
130 dma_addr_t paddr;
131 u32 size;
132};
133
134struct coda_dev {
135 struct v4l2_device v4l2_dev;
136 struct video_device vfd;
137 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300138 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300139
140 void __iomem *regs_base;
141 struct clk *clk_per;
142 struct clk *clk_ahb;
Philipp Zabel8f452842014-07-11 06:36:35 -0300143 struct reset_control *rstc;
Javier Martin186b2502012-07-26 05:53:35 -0300144
145 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300146 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300147 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700148 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300149 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300150
151 spinlock_t irqlock;
152 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300153 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300154 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300155 struct v4l2_m2m_dev *m2m_dev;
156 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300157 struct list_head instances;
158 unsigned long instance_mask;
Javier Martin186b2502012-07-26 05:53:35 -0300159};
160
161struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300162 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300163 u8 h264_intra_qp;
164 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300165 u8 h264_min_qp;
166 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300167 u8 h264_deblk_enabled;
168 u8 h264_deblk_alpha;
169 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300170 u8 mpeg4_intra_qp;
171 u8 mpeg4_inter_qp;
172 u8 gop_size;
Philipp Zabelf38f79d2014-07-11 06:36:28 -0300173 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300174 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300175 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300176 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
177 u32 framerate;
178 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300179 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300180 u32 slice_max_mb;
181};
182
Philipp Zabelc2d22512013-06-21 03:55:28 -0300183struct coda_iram_info {
184 u32 axi_sram_use;
185 phys_addr_t buf_bit_use;
186 phys_addr_t buf_ip_ac_dc_use;
187 phys_addr_t buf_dbk_y_use;
188 phys_addr_t buf_dbk_c_use;
189 phys_addr_t buf_ovl_use;
190 phys_addr_t buf_btp_use;
191 phys_addr_t search_ram_paddr;
192 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300193 int remaining;
194 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300195};
196
Philipp Zabel89548442014-07-11 06:36:17 -0300197struct gdi_tiled_map {
198 int xy2ca_map[16];
199 int xy2ba_map[16];
200 int xy2ra_map[16];
201 int rbc2axi_map[32];
202 int xy2rbc_config;
203 int map_type;
204#define GDI_LINEAR_FRAME_MAP 0
205};
206
Philipp Zabel846ced92014-07-11 06:36:31 -0300207struct coda_timestamp {
208 struct list_head list;
209 u32 sequence;
210 struct v4l2_timecode timecode;
211 struct timeval timestamp;
212};
213
Javier Martin186b2502012-07-26 05:53:35 -0300214struct coda_ctx {
215 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300216 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300217 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300218 struct work_struct pic_run_work;
219 struct work_struct seq_end_work;
220 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300221 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300222 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300223 int streamon_out;
224 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300225 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300226 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300227 u32 osequence;
Philipp Zabelcb2c0282014-07-11 06:36:33 -0300228 u32 sequence_offset;
Javier Martin186b2502012-07-26 05:53:35 -0300229 struct coda_q_data q_data[2];
230 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300231 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300232 enum v4l2_colorspace colorspace;
233 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300234 struct v4l2_ctrl_handler ctrls;
235 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300236 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300237 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300238 char vpu_header[3][64];
239 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300240 struct kfifo bitstream_fifo;
241 struct mutex bitstream_mutex;
242 struct coda_aux_buf bitstream;
Philipp Zabel84e23652014-07-11 06:36:34 -0300243 bool hold;
Javier Martin186b2502012-07-26 05:53:35 -0300244 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300245 struct coda_aux_buf psbuf;
246 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300247 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300248 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300249 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300250 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300251 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300252 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300253 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300254 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300255 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300256 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300257 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300258 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300259 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300260 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300261 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300262};
263
Javier Martin832fbb52012-10-29 08:34:59 -0300264static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
265 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
266static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300267
Javier Martin186b2502012-07-26 05:53:35 -0300268static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
269{
270 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
271 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
272 writel(data, dev->regs_base + reg);
273}
274
275static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
276{
277 u32 data;
278 data = readl(dev->regs_base + reg);
279 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
280 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
281 return data;
282}
283
284static inline unsigned long coda_isbusy(struct coda_dev *dev)
285{
286 return coda_read(dev, CODA_REG_BIT_BUSY);
287}
288
289static inline int coda_is_initialized(struct coda_dev *dev)
290{
291 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
292}
293
294static int coda_wait_timeout(struct coda_dev *dev)
295{
296 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
297
298 while (coda_isbusy(dev)) {
299 if (time_after(jiffies, timeout))
300 return -ETIMEDOUT;
301 }
302 return 0;
303}
304
305static void coda_command_async(struct coda_ctx *ctx, int cmd)
306{
307 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300308
Philipp Zabel89548442014-07-11 06:36:17 -0300309 if (dev->devtype->product == CODA_960 ||
310 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300311 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300312 coda_write(dev, ctx->bit_stream_param,
313 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300314 coda_write(dev, ctx->frm_dis_flg,
315 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300316 coda_write(dev, ctx->frame_mem_ctrl,
317 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300318 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
319 }
320
Philipp Zabel89548442014-07-11 06:36:17 -0300321 if (dev->devtype->product == CODA_960) {
322 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
323 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
324 }
325
Javier Martin186b2502012-07-26 05:53:35 -0300326 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
327
328 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
329 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300330 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
331
Javier Martin186b2502012-07-26 05:53:35 -0300332 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
333}
334
335static int coda_command_sync(struct coda_ctx *ctx, int cmd)
336{
337 struct coda_dev *dev = ctx->dev;
338
339 coda_command_async(ctx, cmd);
340 return coda_wait_timeout(dev);
341}
342
Philipp Zabel8f452842014-07-11 06:36:35 -0300343static int coda_hw_reset(struct coda_ctx *ctx)
344{
345 struct coda_dev *dev = ctx->dev;
346 unsigned long timeout;
347 unsigned int idx;
348 int ret;
349
350 if (!dev->rstc)
351 return -ENOENT;
352
353 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
354
355 timeout = jiffies + msecs_to_jiffies(100);
356 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
357 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
358 if (time_after(jiffies, timeout))
359 return -ETIME;
360 cpu_relax();
361 }
362
363 ret = reset_control_reset(dev->rstc);
364 if (ret < 0)
365 return ret;
366
367 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
368 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
369 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
370 ret = coda_wait_timeout(dev);
371 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
372
373 return ret;
374}
375
Javier Martin186b2502012-07-26 05:53:35 -0300376static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
377 enum v4l2_buf_type type)
378{
379 switch (type) {
380 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
381 return &(ctx->q_data[V4L2_M2M_SRC]);
382 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
383 return &(ctx->q_data[V4L2_M2M_DST]);
384 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300385 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300386 }
Javier Martin186b2502012-07-26 05:53:35 -0300387}
388
389/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300390 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300391 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300392static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300393 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300394 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300395 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300396 },
397 {
398 .name = "YUV 4:2:0 Planar, YCrCb",
399 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300400 },
401 {
402 .name = "H264 Encoded Stream",
403 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300404 },
405 {
406 .name = "MPEG4 Encoded Stream",
407 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300408 },
409};
410
Philipp Zabelb96904e2013-05-23 10:42:58 -0300411#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
412 { mode, src_fourcc, dst_fourcc, max_w, max_h }
413
414/*
415 * Arrays of codecs supported by each given version of Coda:
416 * i.MX27 -> codadx6
417 * i.MX5x -> coda7
418 * i.MX6 -> coda960
419 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
420 */
421static struct coda_codec codadx6_codecs[] = {
422 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
423 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300424};
425
Philipp Zabelb96904e2013-05-23 10:42:58 -0300426static struct coda_codec coda7_codecs[] = {
427 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
428 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300429 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
430 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300431};
432
Philipp Zabel89548442014-07-11 06:36:17 -0300433static struct coda_codec coda9_codecs[] = {
434 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
435 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
436 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
437 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
438};
439
Philipp Zabelb96904e2013-05-23 10:42:58 -0300440static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300441{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300442 switch (fourcc) {
443 case V4L2_PIX_FMT_YUV420:
444 case V4L2_PIX_FMT_YVU420:
445 return true;
446 default:
447 return false;
448 }
449}
Javier Martin186b2502012-07-26 05:53:35 -0300450
Philipp Zabelb96904e2013-05-23 10:42:58 -0300451/*
452 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
453 * tables.
454 */
455static u32 coda_format_normalize_yuv(u32 fourcc)
456{
457 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
458}
459
460static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
461 int dst_fourcc)
462{
463 struct coda_codec *codecs = dev->devtype->codecs;
464 int num_codecs = dev->devtype->num_codecs;
465 int k;
466
467 src_fourcc = coda_format_normalize_yuv(src_fourcc);
468 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
469 if (src_fourcc == dst_fourcc)
470 return NULL;
471
472 for (k = 0; k < num_codecs; k++) {
473 if (codecs[k].src_fourcc == src_fourcc &&
474 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300475 break;
476 }
477
Philipp Zabelb96904e2013-05-23 10:42:58 -0300478 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300479 return NULL;
480
Philipp Zabelb96904e2013-05-23 10:42:58 -0300481 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300482}
483
Philipp Zabel57625592013-09-30 10:34:51 -0300484static void coda_get_max_dimensions(struct coda_dev *dev,
485 struct coda_codec *codec,
486 int *max_w, int *max_h)
487{
488 struct coda_codec *codecs = dev->devtype->codecs;
489 int num_codecs = dev->devtype->num_codecs;
490 unsigned int w, h;
491 int k;
492
493 if (codec) {
494 w = codec->max_w;
495 h = codec->max_h;
496 } else {
497 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
498 w = max(w, codecs[k].max_w);
499 h = max(h, codecs[k].max_h);
500 }
501 }
502
503 if (max_w)
504 *max_w = w;
505 if (max_h)
506 *max_h = h;
507}
508
Philipp Zabel927933f2013-09-30 10:34:48 -0300509static char *coda_product_name(int product)
510{
511 static char buf[9];
512
513 switch (product) {
514 case CODA_DX6:
515 return "CodaDx6";
516 case CODA_7541:
517 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300518 case CODA_960:
519 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300520 default:
521 snprintf(buf, sizeof(buf), "(0x%04x)", product);
522 return buf;
523 }
524}
525
Javier Martin186b2502012-07-26 05:53:35 -0300526/*
527 * V4L2 ioctl() operations.
528 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300529static int coda_querycap(struct file *file, void *priv,
530 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300531{
Philipp Zabel927933f2013-09-30 10:34:48 -0300532 struct coda_ctx *ctx = fh_to_ctx(priv);
533
Javier Martin186b2502012-07-26 05:53:35 -0300534 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300535 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
536 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300537 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300538 /*
539 * This is only a mem-to-mem video device. The capture and output
540 * device capability flags are left only for backward compatibility
541 * and are scheduled for removal.
542 */
543 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
544 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300545 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
546
547 return 0;
548}
549
550static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300551 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300552{
553 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300554 struct coda_codec *codecs = ctx->dev->devtype->codecs;
555 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300556 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300557 int num_codecs = ctx->dev->devtype->num_codecs;
558 int num_formats = ARRAY_SIZE(coda_formats);
559 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300560
561 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300562 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300563 if (coda_format_is_yuv(formats[i].fourcc) &&
564 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300565 if (num == f->index)
566 break;
567 ++num;
568 continue;
569 }
570 /* Compressed formats may be supported, check the codec list */
571 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300572 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300573 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300574 formats[i].fourcc == codecs[k].dst_fourcc &&
575 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300576 break;
577 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
578 formats[i].fourcc == codecs[k].src_fourcc)
579 break;
580 }
581 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300582 if (num == f->index)
583 break;
584 ++num;
585 }
586 }
587
588 if (i < num_formats) {
589 fmt = &formats[i];
590 strlcpy(f->description, fmt->name, sizeof(f->description));
591 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300592 if (!coda_format_is_yuv(fmt->fourcc))
593 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300594 return 0;
595 }
596
597 /* Format not found */
598 return -EINVAL;
599}
600
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300601static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
602 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300603{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300604 struct coda_ctx *ctx = fh_to_ctx(priv);
605 struct vb2_queue *src_vq;
606 struct coda_q_data *q_data_src;
607
608 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300609 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300610 if (vb2_is_streaming(src_vq)) {
611 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
612
613 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
614 q_data_src->fourcc);
615 }
616
617 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300618}
619
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300620static int coda_enum_fmt_vid_out(struct file *file, void *priv,
621 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300622{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300623 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300624}
625
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300626static int coda_g_fmt(struct file *file, void *priv,
627 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300628{
Javier Martin186b2502012-07-26 05:53:35 -0300629 struct coda_q_data *q_data;
630 struct coda_ctx *ctx = fh_to_ctx(priv);
631
Javier Martin186b2502012-07-26 05:53:35 -0300632 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300633 if (!q_data)
634 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300635
636 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300637 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300638 f->fmt.pix.width = q_data->width;
639 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300640 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300641
642 f->fmt.pix.sizeimage = q_data->sizeimage;
643 f->fmt.pix.colorspace = ctx->colorspace;
644
645 return 0;
646}
647
Philipp Zabel57625592013-09-30 10:34:51 -0300648static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
649 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300650{
Philipp Zabel57625592013-09-30 10:34:51 -0300651 struct coda_dev *dev = ctx->dev;
652 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300653 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300654 enum v4l2_field field;
655
656 field = f->fmt.pix.field;
657 if (field == V4L2_FIELD_ANY)
658 field = V4L2_FIELD_NONE;
659 else if (V4L2_FIELD_NONE != field)
660 return -EINVAL;
661
662 /* V4L2 specification suggests the driver corrects the format struct
663 * if any of the dimensions is unsupported */
664 f->fmt.pix.field = field;
665
Philipp Zabel57625592013-09-30 10:34:51 -0300666 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
667 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
668 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
669 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300670
Philipp Zabel57625592013-09-30 10:34:51 -0300671 switch (f->fmt.pix.pixelformat) {
672 case V4L2_PIX_FMT_YUV420:
673 case V4L2_PIX_FMT_YVU420:
674 case V4L2_PIX_FMT_H264:
675 case V4L2_PIX_FMT_MPEG4:
676 case V4L2_PIX_FMT_JPEG:
677 break;
678 default:
679 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300680 if (!q_data)
681 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300682 f->fmt.pix.pixelformat = q_data->fourcc;
683 }
684
685 switch (f->fmt.pix.pixelformat) {
686 case V4L2_PIX_FMT_YUV420:
687 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300688 /* Frame stride must be multiple of 8 */
689 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
690 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300691 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300692 break;
693 case V4L2_PIX_FMT_H264:
694 case V4L2_PIX_FMT_MPEG4:
695 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300696 f->fmt.pix.bytesperline = 0;
697 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300698 break;
699 default:
700 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300701 }
702
703 return 0;
704}
705
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300706static int coda_try_fmt_vid_cap(struct file *file, void *priv,
707 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300708{
Javier Martin186b2502012-07-26 05:53:35 -0300709 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300710 struct coda_codec *codec;
711 struct vb2_queue *src_vq;
712 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300713
Philipp Zabel918c66f2013-06-27 06:59:01 -0300714 /*
715 * If the source format is already fixed, try to find a codec that
716 * converts to the given destination format
717 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300718 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300719 if (vb2_is_streaming(src_vq)) {
720 struct coda_q_data *q_data_src;
721
722 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
723 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
724 f->fmt.pix.pixelformat);
725 if (!codec)
726 return -EINVAL;
727 } else {
728 /* Otherwise determine codec by encoded format, if possible */
729 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
730 f->fmt.pix.pixelformat);
731 }
Javier Martin186b2502012-07-26 05:53:35 -0300732
733 f->fmt.pix.colorspace = ctx->colorspace;
734
Philipp Zabel57625592013-09-30 10:34:51 -0300735 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300736 if (ret < 0)
737 return ret;
738
739 /* The h.264 decoder only returns complete 16x16 macroblocks */
740 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
741 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
742 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
743 f->fmt.pix.bytesperline = f->fmt.pix.width;
744 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
745 f->fmt.pix.height * 3 / 2;
746 }
747
748 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300749}
750
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300751static int coda_try_fmt_vid_out(struct file *file, void *priv,
752 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300753{
754 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300755 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300756
Philipp Zabelb96904e2013-05-23 10:42:58 -0300757 /* Determine codec by encoded format, returns NULL if raw or invalid */
758 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
759 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300760
761 if (!f->fmt.pix.colorspace)
762 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
763
Philipp Zabel57625592013-09-30 10:34:51 -0300764 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300765}
766
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300767static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300768{
769 struct coda_q_data *q_data;
770 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300771
Philipp Zabel14604e32014-07-11 06:36:22 -0300772 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300773 if (!vq)
774 return -EINVAL;
775
776 q_data = get_q_data(ctx, f->type);
777 if (!q_data)
778 return -EINVAL;
779
780 if (vb2_is_busy(vq)) {
781 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
782 return -EBUSY;
783 }
784
Philipp Zabelb96904e2013-05-23 10:42:58 -0300785 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300786 q_data->width = f->fmt.pix.width;
787 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300788 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300789 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300790 q_data->rect.left = 0;
791 q_data->rect.top = 0;
792 q_data->rect.width = f->fmt.pix.width;
793 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300794
795 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
796 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300797 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300798
799 return 0;
800}
801
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300802static int coda_s_fmt_vid_cap(struct file *file, void *priv,
803 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300804{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300805 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300806 int ret;
807
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300808 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300809 if (ret)
810 return ret;
811
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300812 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300813}
814
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300815static int coda_s_fmt_vid_out(struct file *file, void *priv,
816 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300817{
818 struct coda_ctx *ctx = fh_to_ctx(priv);
819 int ret;
820
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300821 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300822 if (ret)
823 return ret;
824
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300825 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300826 if (ret)
827 ctx->colorspace = f->fmt.pix.colorspace;
828
829 return ret;
830}
831
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300832static int coda_qbuf(struct file *file, void *priv,
833 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300834{
835 struct coda_ctx *ctx = fh_to_ctx(priv);
836
Philipp Zabel14604e32014-07-11 06:36:22 -0300837 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300838}
839
Philipp Zabel918c66f2013-06-27 06:59:01 -0300840static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
841 struct v4l2_buffer *buf)
842{
843 struct vb2_queue *src_vq;
844
Philipp Zabel14604e32014-07-11 06:36:22 -0300845 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300846
847 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
848 (buf->sequence == (ctx->qsequence - 1)));
849}
850
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300851static int coda_dqbuf(struct file *file, void *priv,
852 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300853{
854 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300855 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300856
Philipp Zabel14604e32014-07-11 06:36:22 -0300857 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300858
859 /* If this is the last capture buffer, emit an end-of-stream event */
860 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
861 coda_buf_is_end_of_stream(ctx, buf)) {
862 const struct v4l2_event eos_event = {
863 .type = V4L2_EVENT_EOS
864 };
865
866 v4l2_event_queue_fh(&ctx->fh, &eos_event);
867 }
868
869 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300870}
871
Philipp Zabel52c41672014-07-11 06:36:19 -0300872static int coda_g_selection(struct file *file, void *fh,
873 struct v4l2_selection *s)
874{
875 struct coda_ctx *ctx = fh_to_ctx(fh);
876 struct coda_q_data *q_data;
877 struct v4l2_rect r, *rsel;
878
879 q_data = get_q_data(ctx, s->type);
880 if (!q_data)
881 return -EINVAL;
882
883 r.left = 0;
884 r.top = 0;
885 r.width = q_data->width;
886 r.height = q_data->height;
887 rsel = &q_data->rect;
888
889 switch (s->target) {
890 case V4L2_SEL_TGT_CROP_DEFAULT:
891 case V4L2_SEL_TGT_CROP_BOUNDS:
892 rsel = &r;
893 /* fallthrough */
894 case V4L2_SEL_TGT_CROP:
895 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
896 return -EINVAL;
897 break;
898 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
899 case V4L2_SEL_TGT_COMPOSE_PADDED:
900 rsel = &r;
901 /* fallthrough */
902 case V4L2_SEL_TGT_COMPOSE:
903 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
904 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
905 return -EINVAL;
906 break;
907 default:
908 return -EINVAL;
909 }
910
911 s->r = *rsel;
912
913 return 0;
914}
915
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300916static int coda_try_decoder_cmd(struct file *file, void *fh,
917 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300918{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300919 if (dc->cmd != V4L2_DEC_CMD_STOP)
920 return -EINVAL;
921
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300922 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300923 return -EINVAL;
924
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300925 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300926 return -EINVAL;
927
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300928 return 0;
929}
930
931static int coda_decoder_cmd(struct file *file, void *fh,
932 struct v4l2_decoder_cmd *dc)
933{
934 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300935 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300936 int ret;
937
938 ret = coda_try_decoder_cmd(file, fh, dc);
939 if (ret < 0)
940 return ret;
941
942 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300943 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300944 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300945
946 /* Set the strem-end flag on this context */
947 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
948
Philipp Zabel89548442014-07-11 06:36:17 -0300949 if ((dev->devtype->product == CODA_960) &&
950 coda_isbusy(dev) &&
951 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
952 /* If this context is currently running, update the hardware flag */
953 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
954 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300955 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300956 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300957
Philipp Zabel918c66f2013-06-27 06:59:01 -0300958 return 0;
959}
960
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300961static int coda_subscribe_event(struct v4l2_fh *fh,
962 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300963{
964 switch (sub->type) {
965 case V4L2_EVENT_EOS:
966 return v4l2_event_subscribe(fh, sub, 0, NULL);
967 default:
968 return v4l2_ctrl_subscribe_event(fh, sub);
969 }
Javier Martin186b2502012-07-26 05:53:35 -0300970}
971
972static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300973 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300974
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300975 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
976 .vidioc_g_fmt_vid_cap = coda_g_fmt,
977 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
978 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300979
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300980 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
981 .vidioc_g_fmt_vid_out = coda_g_fmt,
982 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
983 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300984
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300985 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
986 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300987
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300988 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300989 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300990 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300991 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300992
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300993 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
994 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300995
Philipp Zabel52c41672014-07-11 06:36:19 -0300996 .vidioc_g_selection = coda_g_selection,
997
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300998 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300999 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001000
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001001 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001002 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001003};
1004
Philipp Zabel918c66f2013-06-27 06:59:01 -03001005static int coda_start_decoding(struct coda_ctx *ctx);
1006
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001007static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
1008{
1009 return kfifo_len(&ctx->bitstream_fifo);
1010}
1011
1012static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
1013{
1014 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1015 struct coda_dev *dev = ctx->dev;
1016 u32 rd_ptr;
1017
1018 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1019 kfifo->out = (kfifo->in & ~kfifo->mask) |
1020 (rd_ptr - ctx->bitstream.paddr);
1021 if (kfifo->out > kfifo->in)
1022 kfifo->out -= kfifo->mask + 1;
1023}
1024
1025static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1026{
1027 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1028 struct coda_dev *dev = ctx->dev;
1029 u32 rd_ptr, wr_ptr;
1030
1031 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1032 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1033 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1034 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1035}
1036
1037static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1038{
1039 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1040 struct coda_dev *dev = ctx->dev;
1041 u32 wr_ptr;
1042
1043 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1044 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1045}
1046
1047static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1048{
1049 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1050 u32 n;
1051
1052 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1053 if (n < src_size)
1054 return -ENOSPC;
1055
1056 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1057 ctx->bitstream.size, DMA_TO_DEVICE);
1058
Philipp Zabel846ced92014-07-11 06:36:31 -03001059 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001060
1061 return 0;
1062}
1063
1064static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1065 struct vb2_buffer *src_buf)
1066{
1067 int ret;
1068
1069 if (coda_get_bitstream_payload(ctx) +
1070 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1071 return false;
1072
1073 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1074 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1075 return true;
1076 }
1077
1078 ret = coda_bitstream_queue(ctx, src_buf);
1079 if (ret < 0) {
1080 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1081 return false;
1082 }
1083 /* Sync read pointer to device */
1084 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1085 coda_kfifo_sync_to_device_write(ctx);
1086
Philipp Zabel84e23652014-07-11 06:36:34 -03001087 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001088
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001089 return true;
1090}
1091
1092static void coda_fill_bitstream(struct coda_ctx *ctx)
1093{
1094 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001095 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001096
Philipp Zabel14604e32014-07-11 06:36:22 -03001097 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1098 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001099
1100 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001101 /*
1102 * Source buffer is queued in the bitstream ringbuffer;
1103 * queue the timestamp and mark source buffer as done
1104 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001105 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001106
1107 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1108 if (ts) {
1109 ts->sequence = src_buf->v4l2_buf.sequence;
1110 ts->timecode = src_buf->v4l2_buf.timecode;
1111 ts->timestamp = src_buf->v4l2_buf.timestamp;
1112 list_add_tail(&ts->list, &ctx->timestamp_list);
1113 }
1114
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001115 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1116 } else {
1117 break;
1118 }
1119 }
1120}
1121
Philipp Zabel89548442014-07-11 06:36:17 -03001122static void coda_set_gdi_regs(struct coda_ctx *ctx)
1123{
1124 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1125 struct coda_dev *dev = ctx->dev;
1126 int i;
1127
1128 for (i = 0; i < 16; i++)
1129 coda_write(dev, tiled_map->xy2ca_map[i],
1130 CODA9_GDI_XY2_CAS_0 + 4 * i);
1131 for (i = 0; i < 4; i++)
1132 coda_write(dev, tiled_map->xy2ba_map[i],
1133 CODA9_GDI_XY2_BA_0 + 4 * i);
1134 for (i = 0; i < 16; i++)
1135 coda_write(dev, tiled_map->xy2ra_map[i],
1136 CODA9_GDI_XY2_RAS_0 + 4 * i);
1137 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1138 for (i = 0; i < 32; i++)
1139 coda_write(dev, tiled_map->rbc2axi_map[i],
1140 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1141}
1142
Javier Martin186b2502012-07-26 05:53:35 -03001143/*
1144 * Mem-to-mem operations.
1145 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001146static int coda_prepare_decode(struct coda_ctx *ctx)
1147{
1148 struct vb2_buffer *dst_buf;
1149 struct coda_dev *dev = ctx->dev;
1150 struct coda_q_data *q_data_dst;
1151 u32 stridey, height;
1152 u32 picture_y, picture_cb, picture_cr;
1153
Philipp Zabel14604e32014-07-11 06:36:22 -03001154 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001155 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1156
1157 if (ctx->params.rot_mode & CODA_ROT_90) {
1158 stridey = q_data_dst->height;
1159 height = q_data_dst->width;
1160 } else {
1161 stridey = q_data_dst->width;
1162 height = q_data_dst->height;
1163 }
1164
1165 /* Try to copy source buffer contents into the bitstream ringbuffer */
1166 mutex_lock(&ctx->bitstream_mutex);
1167 coda_fill_bitstream(ctx);
1168 mutex_unlock(&ctx->bitstream_mutex);
1169
1170 if (coda_get_bitstream_payload(ctx) < 512 &&
1171 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1172 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1173 "bitstream payload: %d, skipping\n",
1174 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001175 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001176 return -EAGAIN;
1177 }
1178
1179 /* Run coda_start_decoding (again) if not yet initialized */
1180 if (!ctx->initialized) {
1181 int ret = coda_start_decoding(ctx);
1182 if (ret < 0) {
1183 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001184 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001185 return -EAGAIN;
1186 } else {
1187 ctx->initialized = 1;
1188 }
1189 }
1190
Philipp Zabel89548442014-07-11 06:36:17 -03001191 if (dev->devtype->product == CODA_960)
1192 coda_set_gdi_regs(ctx);
1193
Philipp Zabel918c66f2013-06-27 06:59:01 -03001194 /* Set rotator output */
1195 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1196 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1197 /* Switch Cr and Cb for YVU420 format */
1198 picture_cr = picture_y + stridey * height;
1199 picture_cb = picture_cr + stridey / 2 * height / 2;
1200 } else {
1201 picture_cb = picture_y + stridey * height;
1202 picture_cr = picture_cb + stridey / 2 * height / 2;
1203 }
Philipp Zabel89548442014-07-11 06:36:17 -03001204
1205 if (dev->devtype->product == CODA_960) {
1206 /*
1207 * The CODA960 seems to have an internal list of buffers with
1208 * 64 entries that includes the registered frame buffers as
1209 * well as the rotator buffer output.
1210 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1211 */
1212 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1213 CODA9_CMD_DEC_PIC_ROT_INDEX);
1214 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1215 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1216 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1217 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1218 } else {
1219 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1220 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1221 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1222 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1223 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001224 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1225 CODA_CMD_DEC_PIC_ROT_MODE);
1226
1227 switch (dev->devtype->product) {
1228 case CODA_DX6:
1229 /* TBD */
1230 case CODA_7541:
1231 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1232 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001233 case CODA_960:
1234 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1235 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001236 }
1237
1238 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1239
1240 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1241 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1242
1243 return 0;
1244}
1245
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001246static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001247{
Javier Martin186b2502012-07-26 05:53:35 -03001248 struct coda_q_data *q_data_src, *q_data_dst;
1249 struct vb2_buffer *src_buf, *dst_buf;
1250 struct coda_dev *dev = ctx->dev;
1251 int force_ipicture;
1252 int quant_param = 0;
1253 u32 picture_y, picture_cb, picture_cr;
1254 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1255 u32 dst_fourcc;
1256
Philipp Zabel14604e32014-07-11 06:36:22 -03001257 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1258 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001259 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1260 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001261 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001262
Philipp Zabel918c66f2013-06-27 06:59:01 -03001263 src_buf->v4l2_buf.sequence = ctx->osequence;
1264 dst_buf->v4l2_buf.sequence = ctx->osequence;
1265 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001266
1267 /*
1268 * Workaround coda firmware BUG that only marks the first
1269 * frame as IDR. This is a problem for some decoders that can't
1270 * recover when a frame is lost.
1271 */
1272 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1273 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1274 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1275 } else {
1276 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1277 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1278 }
1279
Philipp Zabel89548442014-07-11 06:36:17 -03001280 if (dev->devtype->product == CODA_960)
1281 coda_set_gdi_regs(ctx);
1282
Javier Martin186b2502012-07-26 05:53:35 -03001283 /*
1284 * Copy headers at the beginning of the first frame for H.264 only.
1285 * In MPEG4 they are already copied by the coda.
1286 */
1287 if (src_buf->v4l2_buf.sequence == 0) {
1288 pic_stream_buffer_addr =
1289 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1290 ctx->vpu_header_size[0] +
1291 ctx->vpu_header_size[1] +
1292 ctx->vpu_header_size[2];
1293 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1294 ctx->vpu_header_size[0] -
1295 ctx->vpu_header_size[1] -
1296 ctx->vpu_header_size[2];
1297 memcpy(vb2_plane_vaddr(dst_buf, 0),
1298 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1299 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1300 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1301 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1302 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1303 ctx->vpu_header_size[2]);
1304 } else {
1305 pic_stream_buffer_addr =
1306 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1307 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1308 }
1309
1310 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1311 force_ipicture = 1;
1312 switch (dst_fourcc) {
1313 case V4L2_PIX_FMT_H264:
1314 quant_param = ctx->params.h264_intra_qp;
1315 break;
1316 case V4L2_PIX_FMT_MPEG4:
1317 quant_param = ctx->params.mpeg4_intra_qp;
1318 break;
1319 default:
1320 v4l2_warn(&ctx->dev->v4l2_dev,
1321 "cannot set intra qp, fmt not supported\n");
1322 break;
1323 }
1324 } else {
1325 force_ipicture = 0;
1326 switch (dst_fourcc) {
1327 case V4L2_PIX_FMT_H264:
1328 quant_param = ctx->params.h264_inter_qp;
1329 break;
1330 case V4L2_PIX_FMT_MPEG4:
1331 quant_param = ctx->params.mpeg4_inter_qp;
1332 break;
1333 default:
1334 v4l2_warn(&ctx->dev->v4l2_dev,
1335 "cannot set inter qp, fmt not supported\n");
1336 break;
1337 }
1338 }
1339
1340 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001341 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001342 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1343
1344
1345 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001346 switch (q_data_src->fourcc) {
1347 case V4L2_PIX_FMT_YVU420:
1348 /* Switch Cb and Cr for YVU420 format */
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001349 picture_cr = picture_y + q_data_src->bytesperline *
1350 q_data_src->height;
1351 picture_cb = picture_cr + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001352 q_data_src->height / 2;
1353 break;
1354 case V4L2_PIX_FMT_YUV420:
1355 default:
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001356 picture_cb = picture_y + q_data_src->bytesperline *
1357 q_data_src->height;
1358 picture_cr = picture_cb + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001359 q_data_src->height / 2;
1360 break;
1361 }
Javier Martin186b2502012-07-26 05:53:35 -03001362
Philipp Zabel89548442014-07-11 06:36:17 -03001363 if (dev->devtype->product == CODA_960) {
1364 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1365 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1366 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1367
1368 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1369 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1370 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1371 } else {
1372 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1373 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1374 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1375 }
Javier Martin186b2502012-07-26 05:53:35 -03001376 coda_write(dev, force_ipicture << 1 & 0x2,
1377 CODA_CMD_ENC_PIC_OPTION);
1378
1379 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1380 coda_write(dev, pic_stream_buffer_size / 1024,
1381 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001382
1383 if (!ctx->streamon_out) {
1384 /* After streamoff on the output side, set the stream end flag */
1385 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1386 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1387 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001388}
1389
1390static void coda_device_run(void *m2m_priv)
1391{
1392 struct coda_ctx *ctx = m2m_priv;
1393 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001394
1395 queue_work(dev->workqueue, &ctx->pic_run_work);
1396}
1397
1398static void coda_free_framebuffers(struct coda_ctx *ctx);
1399static void coda_free_context_buffers(struct coda_ctx *ctx);
1400
1401static void coda_seq_end_work(struct work_struct *work)
1402{
1403 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1404 struct coda_dev *dev = ctx->dev;
1405
1406 mutex_lock(&ctx->buffer_mutex);
1407 mutex_lock(&dev->coda_mutex);
1408
1409 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1410 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1411 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1412 v4l2_err(&dev->v4l2_dev,
1413 "CODA_COMMAND_SEQ_END failed\n");
1414 }
1415
1416 kfifo_init(&ctx->bitstream_fifo,
1417 ctx->bitstream.vaddr, ctx->bitstream.size);
1418
1419 coda_free_framebuffers(ctx);
1420 coda_free_context_buffers(ctx);
1421
1422 mutex_unlock(&dev->coda_mutex);
1423 mutex_unlock(&ctx->buffer_mutex);
1424}
1425
1426static void coda_finish_decode(struct coda_ctx *ctx);
1427static void coda_finish_encode(struct coda_ctx *ctx);
1428
1429static void coda_pic_run_work(struct work_struct *work)
1430{
1431 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1432 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001433 int ret;
1434
1435 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001436 mutex_lock(&dev->coda_mutex);
1437
Philipp Zabel918c66f2013-06-27 06:59:01 -03001438 if (ctx->inst_type == CODA_INST_DECODER) {
1439 ret = coda_prepare_decode(ctx);
1440 if (ret < 0) {
1441 mutex_unlock(&dev->coda_mutex);
1442 mutex_unlock(&ctx->buffer_mutex);
1443 /* job_finish scheduled by prepare_decode */
1444 return;
1445 }
1446 } else {
1447 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001448 }
1449
Philipp Zabelc2d22512013-06-21 03:55:28 -03001450 if (dev->devtype->product != CODA_DX6)
1451 coda_write(dev, ctx->iram_info.axi_sram_use,
1452 CODA7_REG_BIT_AXI_SRAM_USE);
1453
Philipp Zabel918c66f2013-06-27 06:59:01 -03001454 if (ctx->inst_type == CODA_INST_DECODER)
1455 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001456 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001457
1458 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1459 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001460
1461 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001462
1463 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001464 } else if (!ctx->aborting) {
1465 if (ctx->inst_type == CODA_INST_DECODER)
1466 coda_finish_decode(ctx);
1467 else
1468 coda_finish_encode(ctx);
1469 }
1470
1471 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1472 queue_work(dev->workqueue, &ctx->seq_end_work);
1473
1474 mutex_unlock(&dev->coda_mutex);
1475 mutex_unlock(&ctx->buffer_mutex);
1476
Philipp Zabel14604e32014-07-11 06:36:22 -03001477 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001478}
1479
1480static int coda_job_ready(void *m2m_priv)
1481{
1482 struct coda_ctx *ctx = m2m_priv;
1483
1484 /*
1485 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001486 * and 1 frame are needed. In the decoder case,
1487 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001488 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001489 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001490 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001491 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1492 "not ready: not enough video buffers.\n");
1493 return 0;
1494 }
1495
Philipp Zabel14604e32014-07-11 06:36:22 -03001496 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001497 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1498 "not ready: not enough video capture buffers.\n");
1499 return 0;
1500 }
1501
Philipp Zabel84e23652014-07-11 06:36:34 -03001502 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001503 ((ctx->inst_type == CODA_INST_DECODER) &&
1504 (coda_get_bitstream_payload(ctx) < 512) &&
1505 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1506 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1507 "%d: not ready: not enough bitstream data.\n",
1508 ctx->idx);
1509 return 0;
1510 }
1511
Philipp Zabel3e748262013-05-23 10:43:01 -03001512 if (ctx->aborting) {
1513 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1514 "not ready: aborting\n");
1515 return 0;
1516 }
1517
Javier Martin186b2502012-07-26 05:53:35 -03001518 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1519 "job ready\n");
1520 return 1;
1521}
1522
1523static void coda_job_abort(void *priv)
1524{
1525 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001526
1527 ctx->aborting = 1;
1528
1529 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1530 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001531}
1532
1533static void coda_lock(void *m2m_priv)
1534{
1535 struct coda_ctx *ctx = m2m_priv;
1536 struct coda_dev *pcdev = ctx->dev;
1537 mutex_lock(&pcdev->dev_mutex);
1538}
1539
1540static void coda_unlock(void *m2m_priv)
1541{
1542 struct coda_ctx *ctx = m2m_priv;
1543 struct coda_dev *pcdev = ctx->dev;
1544 mutex_unlock(&pcdev->dev_mutex);
1545}
1546
1547static struct v4l2_m2m_ops coda_m2m_ops = {
1548 .device_run = coda_device_run,
1549 .job_ready = coda_job_ready,
1550 .job_abort = coda_job_abort,
1551 .lock = coda_lock,
1552 .unlock = coda_unlock,
1553};
1554
Philipp Zabel89548442014-07-11 06:36:17 -03001555static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1556{
1557 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1558 int luma_map, chro_map, i;
1559
1560 memset(tiled_map, 0, sizeof(*tiled_map));
1561
1562 luma_map = 64;
1563 chro_map = 64;
1564 tiled_map->map_type = tiled_map_type;
1565 for (i = 0; i < 16; i++)
1566 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1567 for (i = 0; i < 4; i++)
1568 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1569 for (i = 0; i < 16; i++)
1570 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1571
1572 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1573 tiled_map->xy2rbc_config = 0;
1574 } else {
1575 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1576 tiled_map_type);
1577 return;
1578 }
1579}
1580
Javier Martin186b2502012-07-26 05:53:35 -03001581static void set_default_params(struct coda_ctx *ctx)
1582{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001583 int max_w;
1584 int max_h;
1585
1586 ctx->codec = &ctx->dev->devtype->codecs[0];
1587 max_w = ctx->codec->max_w;
1588 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001589
1590 ctx->params.codec_mode = CODA_MODE_INVALID;
1591 ctx->colorspace = V4L2_COLORSPACE_REC709;
1592 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001593 ctx->aborting = 0;
1594
1595 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001596 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1597 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1598 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1599 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001600 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001601 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1602 ctx->q_data[V4L2_M2M_DST].width = max_w;
1603 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001604 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001605 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001606 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1607 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1608 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1609 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001610
1611 if (ctx->dev->devtype->product == CODA_960)
1612 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001613}
1614
1615/*
1616 * Queue operations
1617 */
1618static int coda_queue_setup(struct vb2_queue *vq,
1619 const struct v4l2_format *fmt,
1620 unsigned int *nbuffers, unsigned int *nplanes,
1621 unsigned int sizes[], void *alloc_ctxs[])
1622{
1623 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001624 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001625 unsigned int size;
1626
Philipp Zabele34db062012-08-29 08:22:00 -03001627 q_data = get_q_data(ctx, vq->type);
1628 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001629
1630 *nplanes = 1;
1631 sizes[0] = size;
1632
1633 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1634
1635 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1636 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1637
1638 return 0;
1639}
1640
1641static int coda_buf_prepare(struct vb2_buffer *vb)
1642{
1643 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1644 struct coda_q_data *q_data;
1645
1646 q_data = get_q_data(ctx, vb->vb2_queue->type);
1647
1648 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1649 v4l2_warn(&ctx->dev->v4l2_dev,
1650 "%s data will not fit into plane (%lu < %lu)\n",
1651 __func__, vb2_plane_size(vb, 0),
1652 (long)q_data->sizeimage);
1653 return -EINVAL;
1654 }
1655
Javier Martin186b2502012-07-26 05:53:35 -03001656 return 0;
1657}
1658
1659static void coda_buf_queue(struct vb2_buffer *vb)
1660{
1661 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001662 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001663 struct coda_q_data *q_data;
1664
1665 q_data = get_q_data(ctx, vb->vb2_queue->type);
1666
1667 /*
1668 * In the decoder case, immediately try to copy the buffer into the
1669 * bitstream ringbuffer and mark it as ready to be dequeued.
1670 */
1671 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1672 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1673 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001674 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001675 * the stream end
1676 */
Philipp Zabel89548442014-07-11 06:36:17 -03001677 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001678 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001679 if ((dev->devtype->product == CODA_960) &&
1680 coda_isbusy(dev) &&
1681 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1682 /* if this decoder instance is running, set the stream end flag */
1683 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1684 }
1685 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001686 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001687 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001688 coda_fill_bitstream(ctx);
1689 mutex_unlock(&ctx->bitstream_mutex);
1690 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001691 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001692 }
Javier Martin186b2502012-07-26 05:53:35 -03001693}
1694
Philipp Zabel86eda902013-05-23 10:42:57 -03001695static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1696{
1697 struct coda_dev *dev = ctx->dev;
1698 u32 *p = ctx->parabuf.vaddr;
1699
1700 if (dev->devtype->product == CODA_DX6)
1701 p[index] = value;
1702 else
1703 p[index ^ 1] = value;
1704}
1705
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001706static int coda_alloc_aux_buf(struct coda_dev *dev,
1707 struct coda_aux_buf *buf, size_t size)
1708{
1709 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1710 GFP_KERNEL);
1711 if (!buf->vaddr)
1712 return -ENOMEM;
1713
1714 buf->size = size;
1715
1716 return 0;
1717}
1718
1719static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1720 struct coda_aux_buf *buf, size_t size)
1721{
1722 return coda_alloc_aux_buf(ctx->dev, buf, size);
1723}
1724
1725static void coda_free_aux_buf(struct coda_dev *dev,
1726 struct coda_aux_buf *buf)
1727{
1728 if (buf->vaddr) {
1729 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1730 buf->vaddr, buf->paddr);
1731 buf->vaddr = NULL;
1732 buf->size = 0;
1733 }
1734}
1735
1736static void coda_free_framebuffers(struct coda_ctx *ctx)
1737{
1738 int i;
1739
1740 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1741 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1742}
1743
Philipp Zabelec25f682012-07-20 08:54:29 -03001744static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1745{
1746 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001747 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001748 dma_addr_t paddr;
1749 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001750 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001751 int i;
1752
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001753 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1754 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001755 ysize = round_up(q_data->width, 8) * height;
1756
Philipp Zabelec25f682012-07-20 08:54:29 -03001757 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001758 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001759 size_t size;
1760
Philipp Zabelaed14b02014-07-11 06:36:15 -03001761 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001762 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1763 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001764 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001765 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1766 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001767 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001768 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001769 }
1770 }
1771
1772 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001773 for (i = 0; i < ctx->num_internal_frames; i++) {
1774 paddr = ctx->internal_frames[i].paddr;
1775 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1776 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1777 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001778
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001779 /* mvcol buffer for h.264 */
1780 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1781 dev->devtype->product != CODA_DX6)
1782 coda_parabuf_write(ctx, 96 + i,
1783 ctx->internal_frames[i].paddr +
1784 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001785 }
1786
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001787 /* mvcol buffer for mpeg4 */
1788 if ((dev->devtype->product != CODA_DX6) &&
1789 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1790 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1791 ysize + ysize/4 + ysize/4);
1792
Philipp Zabelec25f682012-07-20 08:54:29 -03001793 return 0;
1794}
1795
Javier Martin3f3f5c72012-10-29 05:20:29 -03001796static int coda_h264_padding(int size, char *p)
1797{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001798 int nal_size;
1799 int diff;
1800
Javier Martin832fbb52012-10-29 08:34:59 -03001801 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001802 if (diff == 0)
1803 return 0;
1804
Javier Martin832fbb52012-10-29 08:34:59 -03001805 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001806 memcpy(p, coda_filler_nal, nal_size);
1807
1808 /* Add rbsp stop bit and trailing at the end */
1809 *(p + nal_size - 1) = 0x80;
1810
1811 return nal_size;
1812}
1813
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001814static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1815{
1816 phys_addr_t ret;
1817
1818 size = round_up(size, 1024);
1819 if (size > iram->remaining)
1820 return 0;
1821 iram->remaining -= size;
1822
1823 ret = iram->next_paddr;
1824 iram->next_paddr += size;
1825
1826 return ret;
1827}
1828
Philipp Zabelc2d22512013-06-21 03:55:28 -03001829static void coda_setup_iram(struct coda_ctx *ctx)
1830{
1831 struct coda_iram_info *iram_info = &ctx->iram_info;
1832 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001833 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001834 int dbk_bits;
1835 int bit_bits;
1836 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001837
1838 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001839 iram_info->next_paddr = dev->iram.paddr;
1840 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001841
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001842 switch (dev->devtype->product) {
1843 case CODA_7541:
1844 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1845 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1846 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1847 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001848 case CODA_960:
1849 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1850 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1851 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1852 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001853 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001854 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001855 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001856
1857 if (ctx->inst_type == CODA_INST_ENCODER) {
1858 struct coda_q_data *q_data_src;
1859
1860 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1861 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1862
1863 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001864 if (dev->devtype->product == CODA_7541) {
1865 iram_info->search_ram_size = round_up(mb_width * 16 *
1866 36 + 2048, 1024);
1867 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1868 iram_info->search_ram_size);
1869 if (!iram_info->search_ram_paddr) {
1870 pr_err("IRAM is smaller than the search ram size\n");
1871 goto out;
1872 }
1873 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1874 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001875 }
1876
1877 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001878 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1879 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1880 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001881 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001882 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001883
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001884 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1885 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001886 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001887 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001888
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001889 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1890 if (!iram_info->buf_ip_ac_dc_use)
1891 goto out;
1892 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001893
Philipp Zabel8358e762013-06-21 03:55:32 -03001894 /* OVL and BTP disabled for encoder */
1895 } else if (ctx->inst_type == CODA_INST_DECODER) {
1896 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001897
1898 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1899 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001900
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001901 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1902 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1903 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001904 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001905 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001906
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001907 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1908 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001909 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001910 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001911
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001912 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1913 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001914 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001915 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001916
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001917 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001918 }
1919
1920out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001921 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1922 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1923 "IRAM smaller than needed\n");
1924
1925 if (dev->devtype->product == CODA_7541) {
1926 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001927 if (ctx->inst_type == CODA_INST_DECODER) {
1928 /* fw 1.4.50 */
1929 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1930 CODA7_USE_IP_ENABLE);
1931 } else {
1932 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001933 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1934 CODA7_USE_HOST_DBK_ENABLE |
1935 CODA7_USE_IP_ENABLE |
1936 CODA7_USE_DBK_ENABLE);
1937 }
1938 }
1939}
1940
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001941static void coda_free_context_buffers(struct coda_ctx *ctx)
1942{
1943 struct coda_dev *dev = ctx->dev;
1944
Philipp Zabel918c66f2013-06-27 06:59:01 -03001945 coda_free_aux_buf(dev, &ctx->slicebuf);
1946 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001947 if (dev->devtype->product != CODA_DX6)
1948 coda_free_aux_buf(dev, &ctx->workbuf);
1949}
1950
1951static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1952 struct coda_q_data *q_data)
1953{
1954 struct coda_dev *dev = ctx->dev;
1955 size_t size;
1956 int ret;
1957
1958 switch (dev->devtype->product) {
1959 case CODA_7541:
1960 size = CODA7_WORK_BUF_SIZE;
1961 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001962 case CODA_960:
1963 size = CODA9_WORK_BUF_SIZE;
1964 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1965 size += CODA9_PS_SAVE_SIZE;
1966 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001967 default:
1968 return 0;
1969 }
1970
Philipp Zabel918c66f2013-06-27 06:59:01 -03001971 if (ctx->psbuf.vaddr) {
1972 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1973 return -EBUSY;
1974 }
1975 if (ctx->slicebuf.vaddr) {
1976 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1977 return -EBUSY;
1978 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001979 if (ctx->workbuf.vaddr) {
1980 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1981 ret = -EBUSY;
1982 return -ENOMEM;
1983 }
1984
Philipp Zabel918c66f2013-06-27 06:59:01 -03001985 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1986 /* worst case slice size */
1987 size = (DIV_ROUND_UP(q_data->width, 16) *
1988 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1989 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1990 if (ret < 0) {
1991 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1992 ctx->slicebuf.size);
1993 return ret;
1994 }
1995 }
1996
1997 if (dev->devtype->product == CODA_7541) {
1998 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1999 if (ret < 0) {
2000 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
2001 goto err;
2002 }
2003 }
2004
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002005 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
2006 if (ret < 0) {
2007 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2008 ctx->workbuf.size);
2009 goto err;
2010 }
2011
2012 return 0;
2013
2014err:
2015 coda_free_context_buffers(ctx);
2016 return ret;
2017}
2018
Philipp Zabel918c66f2013-06-27 06:59:01 -03002019static int coda_start_decoding(struct coda_ctx *ctx)
2020{
2021 struct coda_q_data *q_data_src, *q_data_dst;
2022 u32 bitstream_buf, bitstream_size;
2023 struct coda_dev *dev = ctx->dev;
2024 int width, height;
2025 u32 src_fourcc;
2026 u32 val;
2027 int ret;
2028
2029 /* Start decoding */
2030 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2031 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2032 bitstream_buf = ctx->bitstream.paddr;
2033 bitstream_size = ctx->bitstream.size;
2034 src_fourcc = q_data_src->fourcc;
2035
2036 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2037
2038 /* Update coda bitstream read and write pointers from kfifo */
2039 coda_kfifo_sync_to_device_full(ctx);
2040
2041 ctx->display_idx = -1;
2042 ctx->frm_dis_flg = 0;
2043 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2044
2045 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2046 CODA_REG_BIT_BIT_STREAM_PARAM);
2047
2048 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2049 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2050 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002051 if ((dev->devtype->product == CODA_7541) ||
2052 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002053 val |= CODA_REORDER_ENABLE;
2054 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2055
2056 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002057 if (dev->devtype->product == CODA_960 &&
2058 src_fourcc == V4L2_PIX_FMT_MPEG4)
2059 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2060 else
2061 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002062 if (src_fourcc == V4L2_PIX_FMT_H264) {
2063 if (dev->devtype->product == CODA_7541) {
2064 coda_write(dev, ctx->psbuf.paddr,
2065 CODA_CMD_DEC_SEQ_PS_BB_START);
2066 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2067 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2068 }
Philipp Zabel89548442014-07-11 06:36:17 -03002069 if (dev->devtype->product == CODA_960) {
2070 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2071 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2072 }
2073 }
2074 if (dev->devtype->product != CODA_960) {
2075 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002076 }
2077
2078 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2079 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2080 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2081 return -ETIMEDOUT;
2082 }
2083
2084 /* Update kfifo out pointer from coda bitstream read pointer */
2085 coda_kfifo_sync_from_device(ctx);
2086
2087 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2088
2089 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2090 v4l2_err(&dev->v4l2_dev,
2091 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2092 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2093 return -EAGAIN;
2094 }
2095
2096 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2097 if (dev->devtype->product == CODA_DX6) {
2098 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2099 height = val & CODADX6_PICHEIGHT_MASK;
2100 } else {
2101 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2102 height = val & CODA7_PICHEIGHT_MASK;
2103 }
2104
2105 if (width > q_data_dst->width || height > q_data_dst->height) {
2106 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2107 width, height, q_data_dst->width, q_data_dst->height);
2108 return -EINVAL;
2109 }
2110
2111 width = round_up(width, 16);
2112 height = round_up(height, 16);
2113
2114 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2115 __func__, ctx->idx, width, height);
2116
Philipp Zabela500a932014-07-11 06:36:13 -03002117 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002118 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2119 v4l2_err(&dev->v4l2_dev,
2120 "not enough framebuffers to decode (%d < %d)\n",
2121 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2122 return -EINVAL;
2123 }
2124
Philipp Zabel52c41672014-07-11 06:36:19 -03002125 if (src_fourcc == V4L2_PIX_FMT_H264) {
2126 u32 left_right;
2127 u32 top_bottom;
2128
2129 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2130 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2131
2132 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2133 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2134 q_data_dst->rect.width = width - q_data_dst->rect.left -
2135 (left_right & 0x3ff);
2136 q_data_dst->rect.height = height - q_data_dst->rect.top -
2137 (top_bottom & 0x3ff);
2138 }
2139
Philipp Zabel918c66f2013-06-27 06:59:01 -03002140 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2141 if (ret < 0)
2142 return ret;
2143
2144 /* Tell the decoder how many frame buffers we allocated. */
2145 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2146 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2147
2148 if (dev->devtype->product != CODA_DX6) {
2149 /* Set secondary AXI IRAM */
2150 coda_setup_iram(ctx);
2151
2152 coda_write(dev, ctx->iram_info.buf_bit_use,
2153 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2154 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2155 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2156 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2157 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2158 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2159 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2160 coda_write(dev, ctx->iram_info.buf_ovl_use,
2161 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002162 if (dev->devtype->product == CODA_960)
2163 coda_write(dev, ctx->iram_info.buf_btp_use,
2164 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2165 }
2166
2167 if (dev->devtype->product == CODA_960) {
2168 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2169
2170 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2171 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2172 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2173 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2174 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2175 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002176 }
2177
2178 if (src_fourcc == V4L2_PIX_FMT_H264) {
2179 coda_write(dev, ctx->slicebuf.paddr,
2180 CODA_CMD_SET_FRAME_SLICE_BB_START);
2181 coda_write(dev, ctx->slicebuf.size / 1024,
2182 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2183 }
2184
2185 if (dev->devtype->product == CODA_7541) {
2186 int max_mb_x = 1920 / 16;
2187 int max_mb_y = 1088 / 16;
2188 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002189
Philipp Zabel918c66f2013-06-27 06:59:01 -03002190 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2191 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002192 } else if (dev->devtype->product == CODA_960) {
2193 int max_mb_x = 1920 / 16;
2194 int max_mb_y = 1088 / 16;
2195 int max_mb_num = max_mb_x * max_mb_y;
2196
2197 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2198 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002199 }
2200
2201 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2202 v4l2_err(&ctx->dev->v4l2_dev,
2203 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2204 return -ETIMEDOUT;
2205 }
2206
2207 return 0;
2208}
2209
Philipp Zabeld35167a2013-05-23 10:42:59 -03002210static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2211 int header_code, u8 *header, int *size)
2212{
2213 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002214 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002215 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002216 int i;
2217
2218 if (dev->devtype->product == CODA_960)
2219 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002220
2221 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2222 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002223 bufsize = vb2_plane_size(buf, 0);
2224 if (dev->devtype->product == CODA_960)
2225 bufsize /= 1024;
2226 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002227 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2228 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2229 if (ret < 0) {
2230 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2231 return ret;
2232 }
Philipp Zabel89548442014-07-11 06:36:17 -03002233
2234 if (dev->devtype->product == CODA_960) {
2235 for (i = 63; i > 0; i--)
2236 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2237 break;
2238 *size = i + 1;
2239 } else {
2240 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2241 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2242 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002243 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2244
2245 return 0;
2246}
2247
Philipp Zabel89548442014-07-11 06:36:17 -03002248static int coda_start_encoding(struct coda_ctx *ctx);
2249
Javier Martin186b2502012-07-26 05:53:35 -03002250static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2251{
2252 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2253 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002254 struct coda_dev *dev = ctx->dev;
2255 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002256 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002257 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002258
Philipp Zabelb96904e2013-05-23 10:42:58 -03002259 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002260 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2261 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2262 if (coda_get_bitstream_payload(ctx) < 512)
2263 return -EINVAL;
2264 } else {
2265 if (count < 1)
2266 return -EINVAL;
2267 }
2268
2269 ctx->streamon_out = 1;
2270
Philipp Zabelb96904e2013-05-23 10:42:58 -03002271 if (coda_format_is_yuv(q_data_src->fourcc))
2272 ctx->inst_type = CODA_INST_ENCODER;
2273 else
2274 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002275 } else {
2276 if (count < 1)
2277 return -EINVAL;
2278
2279 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002280 }
Javier Martin186b2502012-07-26 05:53:35 -03002281
Philipp Zabelb96904e2013-05-23 10:42:58 -03002282 /* Don't start the coda unless both queues are on */
2283 if (!(ctx->streamon_out & ctx->streamon_cap))
2284 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002285
Philipp Zabeleb107512013-09-30 10:34:45 -03002286 /* Allow decoder device_run with no new buffers queued */
2287 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002288 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002289
Philipp Zabelb96904e2013-05-23 10:42:58 -03002290 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002291 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002292 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002293
Philipp Zabelb96904e2013-05-23 10:42:58 -03002294 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2295 q_data_dst->fourcc);
2296 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002297 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2298 return -EINVAL;
2299 }
2300
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002301 /* Allocate per-instance buffers */
2302 ret = coda_alloc_context_buffers(ctx, q_data_src);
2303 if (ret < 0)
2304 return ret;
2305
Philipp Zabel918c66f2013-06-27 06:59:01 -03002306 if (ctx->inst_type == CODA_INST_DECODER) {
2307 mutex_lock(&dev->coda_mutex);
2308 ret = coda_start_decoding(ctx);
2309 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002310 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002311 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002312 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002313 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002314 } else {
2315 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002316 }
2317
Philipp Zabel89548442014-07-11 06:36:17 -03002318 ctx->initialized = 1;
2319 return ret;
2320}
2321
2322static int coda_start_encoding(struct coda_ctx *ctx)
2323{
2324 struct coda_dev *dev = ctx->dev;
2325 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2326 struct coda_q_data *q_data_src, *q_data_dst;
2327 u32 bitstream_buf, bitstream_size;
2328 struct vb2_buffer *buf;
2329 int gamma, ret, value;
2330 u32 dst_fourcc;
2331
2332 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2333 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2334 dst_fourcc = q_data_dst->fourcc;
2335
Philipp Zabel14604e32014-07-11 06:36:22 -03002336 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002337 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2338 bitstream_size = q_data_dst->sizeimage;
2339
Javier Martin186b2502012-07-26 05:53:35 -03002340 if (!coda_is_initialized(dev)) {
2341 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2342 return -EFAULT;
2343 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002344
2345 mutex_lock(&dev->coda_mutex);
2346
Javier Martin186b2502012-07-26 05:53:35 -03002347 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002348 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2349 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002350 switch (dev->devtype->product) {
2351 case CODA_DX6:
2352 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2353 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2354 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002355 case CODA_960:
2356 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2357 /* fallthrough */
2358 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002359 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2360 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002361 break;
Javier Martin186b2502012-07-26 05:53:35 -03002362 }
2363
Philipp Zabel89548442014-07-11 06:36:17 -03002364 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2365 value &= ~(1 << 2 | 0x7 << 9);
2366 ctx->frame_mem_ctrl = value;
2367 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2368
Philipp Zabel10436672012-07-02 09:03:55 -03002369 if (dev->devtype->product == CODA_DX6) {
2370 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002371 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002372 }
Javier Martin186b2502012-07-26 05:53:35 -03002373
2374 /* Could set rotation here if needed */
2375 switch (dev->devtype->product) {
2376 case CODA_DX6:
2377 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002378 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002379 break;
2380 default:
2381 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002382 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002383 }
Javier Martin186b2502012-07-26 05:53:35 -03002384 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2385 coda_write(dev, ctx->params.framerate,
2386 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2387
Philipp Zabelb96904e2013-05-23 10:42:58 -03002388 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002389 switch (dst_fourcc) {
2390 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002391 if (dev->devtype->product == CODA_960)
2392 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2393 else
2394 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002395 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2396 break;
2397 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002398 if (dev->devtype->product == CODA_960)
2399 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2400 else
2401 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002402 if (ctx->params.h264_deblk_enabled) {
2403 value = ((ctx->params.h264_deblk_alpha &
2404 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2405 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2406 ((ctx->params.h264_deblk_beta &
2407 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2408 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2409 } else {
2410 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2411 }
2412 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002413 break;
2414 default:
2415 v4l2_err(v4l2_dev,
2416 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002417 ret = -EINVAL;
2418 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002419 }
2420
Philipp Zabelc566c782012-08-08 11:59:38 -03002421 switch (ctx->params.slice_mode) {
2422 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2423 value = 0;
2424 break;
2425 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2426 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2427 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002428 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002429 break;
2430 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2431 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2432 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2433 value |= 1 & CODA_SLICING_MODE_MASK;
2434 break;
2435 }
Javier Martin186b2502012-07-26 05:53:35 -03002436 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002437 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002438 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2439
2440 if (ctx->params.bitrate) {
2441 /* Rate control enabled */
2442 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2443 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002444 if (dev->devtype->product == CODA_960)
2445 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002446 } else {
2447 value = 0;
2448 }
2449 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2450
2451 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002452 coda_write(dev, ctx->params.intra_refresh,
2453 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002454
2455 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2456 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2457
Javier Martin186b2502012-07-26 05:53:35 -03002458
Philipp Zabel89548442014-07-11 06:36:17 -03002459 value = 0;
2460 if (dev->devtype->product == CODA_960)
2461 gamma = CODA9_DEFAULT_GAMMA;
2462 else
2463 gamma = CODA_DEFAULT_GAMMA;
2464 if (gamma > 0) {
2465 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2466 CODA_CMD_ENC_SEQ_RC_GAMMA);
2467 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002468
2469 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2470 coda_write(dev,
2471 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2472 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2473 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2474 }
Philipp Zabel89548442014-07-11 06:36:17 -03002475 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002476 if (ctx->params.h264_max_qp)
2477 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002478 if (CODA_DEFAULT_GAMMA > 0)
2479 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002480 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002481 if (CODA_DEFAULT_GAMMA > 0) {
2482 if (dev->devtype->product == CODA_DX6)
2483 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2484 else
2485 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2486 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002487 if (ctx->params.h264_min_qp)
2488 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2489 if (ctx->params.h264_max_qp)
2490 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002491 }
Javier Martin186b2502012-07-26 05:53:35 -03002492 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2493
Philipp Zabel89548442014-07-11 06:36:17 -03002494 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2495
Philipp Zabelc2d22512013-06-21 03:55:28 -03002496 coda_setup_iram(ctx);
2497
Javier Martin186b2502012-07-26 05:53:35 -03002498 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002499 switch (dev->devtype->product) {
2500 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002501 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002502 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002503 break;
2504 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002505 coda_write(dev, ctx->iram_info.search_ram_paddr,
2506 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2507 coda_write(dev, ctx->iram_info.search_ram_size,
2508 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002509 break;
2510 case CODA_960:
2511 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2512 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002513 }
Javier Martin186b2502012-07-26 05:53:35 -03002514 }
2515
Philipp Zabelfcb62822013-05-23 10:43:00 -03002516 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2517 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002518 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002519 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002520 }
2521
Philipp Zabelfcb62822013-05-23 10:43:00 -03002522 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2523 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2524 ret = -EFAULT;
2525 goto out;
2526 }
Javier Martin186b2502012-07-26 05:53:35 -03002527
Philipp Zabel89548442014-07-11 06:36:17 -03002528 if (dev->devtype->product == CODA_960)
2529 ctx->num_internal_frames = 4;
2530 else
2531 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002532 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002533 if (ret < 0) {
2534 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2535 goto out;
2536 }
Javier Martin186b2502012-07-26 05:53:35 -03002537
Philipp Zabelec25f682012-07-20 08:54:29 -03002538 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002539 coda_write(dev, q_data_src->bytesperline,
2540 CODA_CMD_SET_FRAME_BUF_STRIDE);
2541 if (dev->devtype->product == CODA_7541) {
2542 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002543 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002544 }
Philipp Zabel10436672012-07-02 09:03:55 -03002545 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002546 coda_write(dev, ctx->iram_info.buf_bit_use,
2547 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2548 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2549 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2550 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2551 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2552 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2553 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2554 coda_write(dev, ctx->iram_info.buf_ovl_use,
2555 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002556 if (dev->devtype->product == CODA_960) {
2557 coda_write(dev, ctx->iram_info.buf_btp_use,
2558 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2559
2560 /* FIXME */
2561 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2562 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2563 }
Philipp Zabel10436672012-07-02 09:03:55 -03002564 }
Philipp Zabel89548442014-07-11 06:36:17 -03002565
Philipp Zabelfcb62822013-05-23 10:43:00 -03002566 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2567 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002568 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002569 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002570 }
2571
2572 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002573 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002574 switch (dst_fourcc) {
2575 case V4L2_PIX_FMT_H264:
2576 /*
2577 * Get SPS 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_H264_SPS,
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
2586 /*
2587 * Get PPS in the first frame and copy it to an
2588 * intermediate buffer.
2589 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002590 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2591 &ctx->vpu_header[1][0],
2592 &ctx->vpu_header_size[1]);
2593 if (ret < 0)
2594 goto out;
2595
Javier Martin3f3f5c72012-10-29 05:20:29 -03002596 /*
2597 * Length of H.264 headers is variable and thus it might not be
2598 * aligned for the coda to append the encoded frame. In that is
2599 * the case a filler NAL must be added to header 2.
2600 */
2601 ctx->vpu_header_size[2] = coda_h264_padding(
2602 (ctx->vpu_header_size[0] +
2603 ctx->vpu_header_size[1]),
2604 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002605 break;
2606 case V4L2_PIX_FMT_MPEG4:
2607 /*
2608 * Get VOS in the first frame and copy it to an
2609 * intermediate buffer
2610 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002611 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2612 &ctx->vpu_header[0][0],
2613 &ctx->vpu_header_size[0]);
2614 if (ret < 0)
2615 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002616
Philipp Zabeld35167a2013-05-23 10:42:59 -03002617 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2618 &ctx->vpu_header[1][0],
2619 &ctx->vpu_header_size[1]);
2620 if (ret < 0)
2621 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002622
Philipp Zabeld35167a2013-05-23 10:42:59 -03002623 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2624 &ctx->vpu_header[2][0],
2625 &ctx->vpu_header_size[2]);
2626 if (ret < 0)
2627 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002628 break;
2629 default:
2630 /* No more formats need to save headers at the moment */
2631 break;
2632 }
2633
Philipp Zabeld35167a2013-05-23 10:42:59 -03002634out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002635 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002636 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002637}
2638
Hans Verkuile37559b2014-04-17 02:47:21 -03002639static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002640{
2641 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002642 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002643
2644 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002645 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002646 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002647 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002648
Philipp Zabel89548442014-07-11 06:36:17 -03002649 if (ctx->inst_type == CODA_INST_DECODER &&
2650 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2651 /* if this decoder instance is running, set the stream end flag */
2652 if (dev->devtype->product == CODA_960) {
2653 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2654
2655 val |= CODA_BIT_STREAM_END_FLAG;
2656 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2657 ctx->bit_stream_param = val;
2658 }
2659 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002660 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2661
2662 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002663 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002664 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002665 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002666 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002667
2668 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002669 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002670 }
2671
Philipp Zabel918c66f2013-06-27 06:59:01 -03002672 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002673 struct coda_timestamp *ts;
2674
2675 while (!list_empty(&ctx->timestamp_list)) {
2676 ts = list_first_entry(&ctx->timestamp_list,
2677 struct coda_timestamp, list);
2678 list_del(&ts->list);
2679 kfree(ts);
2680 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002681 kfifo_init(&ctx->bitstream_fifo,
2682 ctx->bitstream.vaddr, ctx->bitstream.size);
2683 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002684 }
Javier Martin186b2502012-07-26 05:53:35 -03002685}
2686
2687static struct vb2_ops coda_qops = {
2688 .queue_setup = coda_queue_setup,
2689 .buf_prepare = coda_buf_prepare,
2690 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002691 .start_streaming = coda_start_streaming,
2692 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002693 .wait_prepare = vb2_ops_wait_prepare,
2694 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002695};
2696
2697static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2698{
2699 struct coda_ctx *ctx =
2700 container_of(ctrl->handler, struct coda_ctx, ctrls);
2701
2702 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2703 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2704
2705 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002706 case V4L2_CID_HFLIP:
2707 if (ctrl->val)
2708 ctx->params.rot_mode |= CODA_MIR_HOR;
2709 else
2710 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2711 break;
2712 case V4L2_CID_VFLIP:
2713 if (ctrl->val)
2714 ctx->params.rot_mode |= CODA_MIR_VER;
2715 else
2716 ctx->params.rot_mode &= ~CODA_MIR_VER;
2717 break;
Javier Martin186b2502012-07-26 05:53:35 -03002718 case V4L2_CID_MPEG_VIDEO_BITRATE:
2719 ctx->params.bitrate = ctrl->val / 1000;
2720 break;
2721 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2722 ctx->params.gop_size = ctrl->val;
2723 break;
2724 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2725 ctx->params.h264_intra_qp = ctrl->val;
2726 break;
2727 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2728 ctx->params.h264_inter_qp = ctrl->val;
2729 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002730 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2731 ctx->params.h264_min_qp = ctrl->val;
2732 break;
2733 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2734 ctx->params.h264_max_qp = ctrl->val;
2735 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002736 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2737 ctx->params.h264_deblk_alpha = ctrl->val;
2738 break;
2739 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2740 ctx->params.h264_deblk_beta = ctrl->val;
2741 break;
2742 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2743 ctx->params.h264_deblk_enabled = (ctrl->val ==
2744 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2745 break;
Javier Martin186b2502012-07-26 05:53:35 -03002746 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2747 ctx->params.mpeg4_intra_qp = ctrl->val;
2748 break;
2749 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2750 ctx->params.mpeg4_inter_qp = ctrl->val;
2751 break;
2752 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2753 ctx->params.slice_mode = ctrl->val;
2754 break;
2755 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2756 ctx->params.slice_max_mb = ctrl->val;
2757 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002758 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2759 ctx->params.slice_max_bits = ctrl->val * 8;
2760 break;
Javier Martin186b2502012-07-26 05:53:35 -03002761 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2762 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002763 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2764 ctx->params.intra_refresh = ctrl->val;
2765 break;
Javier Martin186b2502012-07-26 05:53:35 -03002766 default:
2767 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2768 "Invalid control, id=%d, val=%d\n",
2769 ctrl->id, ctrl->val);
2770 return -EINVAL;
2771 }
2772
2773 return 0;
2774}
2775
2776static struct v4l2_ctrl_ops coda_ctrl_ops = {
2777 .s_ctrl = coda_s_ctrl,
2778};
2779
2780static int coda_ctrls_setup(struct coda_ctx *ctx)
2781{
2782 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2783
2784 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002785 V4L2_CID_HFLIP, 0, 1, 1, 0);
2786 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2787 V4L2_CID_VFLIP, 0, 1, 1, 0);
2788 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002789 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2790 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2791 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2792 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002793 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002794 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002795 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002796 if (ctx->dev->devtype->product != CODA_960) {
2797 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2798 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2799 }
2800 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2801 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002802 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002803 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2804 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2805 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2806 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2807 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2808 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2809 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2810 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002811 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2812 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2813 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2814 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2815 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002816 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2817 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002818 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2819 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002820 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2821 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002822 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2823 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2824 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2825 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2826 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002827 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2828 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002829
2830 if (ctx->ctrls.error) {
2831 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2832 ctx->ctrls.error);
2833 return -EINVAL;
2834 }
2835
2836 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2837}
2838
2839static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2840 struct vb2_queue *dst_vq)
2841{
2842 struct coda_ctx *ctx = priv;
2843 int ret;
2844
Javier Martin186b2502012-07-26 05:53:35 -03002845 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002846 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002847 src_vq->drv_priv = ctx;
2848 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2849 src_vq->ops = &coda_qops;
2850 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002851 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002852 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002853
2854 ret = vb2_queue_init(src_vq);
2855 if (ret)
2856 return ret;
2857
Javier Martin186b2502012-07-26 05:53:35 -03002858 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002859 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002860 dst_vq->drv_priv = ctx;
2861 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2862 dst_vq->ops = &coda_qops;
2863 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002864 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002865 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002866
2867 return vb2_queue_init(dst_vq);
2868}
2869
Philipp Zabele11f3e62012-07-25 09:16:58 -03002870static int coda_next_free_instance(struct coda_dev *dev)
2871{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002872 int idx = ffz(dev->instance_mask);
2873
2874 if ((idx < 0) ||
2875 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2876 return -EBUSY;
2877
2878 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002879}
2880
Javier Martin186b2502012-07-26 05:53:35 -03002881static int coda_open(struct file *file)
2882{
2883 struct coda_dev *dev = video_drvdata(file);
2884 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002885 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002886 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002887
Javier Martin186b2502012-07-26 05:53:35 -03002888 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2889 if (!ctx)
2890 return -ENOMEM;
2891
Fabio Estevamf82bc202013-08-21 11:14:16 -03002892 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002893 if (idx < 0) {
2894 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002895 goto err_coda_max;
2896 }
2897 set_bit(idx, &dev->instance_mask);
2898
Philipp Zabel32b6f202014-07-11 06:36:20 -03002899 init_completion(&ctx->completion);
2900 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2901 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002902 v4l2_fh_init(&ctx->fh, video_devdata(file));
2903 file->private_data = &ctx->fh;
2904 v4l2_fh_add(&ctx->fh);
2905 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002906 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002907 switch (dev->devtype->product) {
2908 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002909 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002910 ctx->reg_idx = 0;
2911 break;
2912 default:
2913 ctx->reg_idx = idx;
2914 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002915
Philipp Zabel1e172732014-07-11 06:36:23 -03002916 /* Power up and upload firmware if necessary */
2917 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2918 if (ret < 0) {
2919 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2920 goto err_pm_get;
2921 }
2922
Fabio Estevam79830db2013-08-21 11:14:17 -03002923 ret = clk_prepare_enable(dev->clk_per);
2924 if (ret)
2925 goto err_clk_per;
2926
2927 ret = clk_prepare_enable(dev->clk_ahb);
2928 if (ret)
2929 goto err_clk_ahb;
2930
Javier Martin186b2502012-07-26 05:53:35 -03002931 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002932 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002933 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002934 if (IS_ERR(ctx->fh.m2m_ctx)) {
2935 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002936
2937 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2938 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002939 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002940 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002941
Javier Martin186b2502012-07-26 05:53:35 -03002942 ret = coda_ctrls_setup(ctx);
2943 if (ret) {
2944 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002945 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002946 }
2947
2948 ctx->fh.ctrl_handler = &ctx->ctrls;
2949
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002950 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2951 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002952 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002953 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002954 }
2955
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002956 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2957 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2958 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2959 if (!ctx->bitstream.vaddr) {
2960 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2961 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002962 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002963 }
2964 kfifo_init(&ctx->bitstream_fifo,
2965 ctx->bitstream.vaddr, ctx->bitstream.size);
2966 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002967 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002968 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002969
Javier Martin186b2502012-07-26 05:53:35 -03002970 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002971 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002972 coda_unlock(ctx);
2973
Javier Martin186b2502012-07-26 05:53:35 -03002974 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2975 ctx->idx, ctx);
2976
2977 return 0;
2978
Fabio Estevamf82bc202013-08-21 11:14:16 -03002979err_dma_writecombine:
2980 coda_free_context_buffers(ctx);
2981 if (ctx->dev->devtype->product == CODA_DX6)
2982 coda_free_aux_buf(dev, &ctx->workbuf);
2983 coda_free_aux_buf(dev, &ctx->parabuf);
2984err_dma_alloc:
2985 v4l2_ctrl_handler_free(&ctx->ctrls);
2986err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002987 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002988err_ctx_init:
2989 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002990err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002991 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002992err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002993 pm_runtime_put_sync(&dev->plat_dev->dev);
2994err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002995 v4l2_fh_del(&ctx->fh);
2996 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002997 clear_bit(ctx->idx, &dev->instance_mask);
2998err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002999 kfree(ctx);
3000 return ret;
3001}
3002
3003static int coda_release(struct file *file)
3004{
3005 struct coda_dev *dev = video_drvdata(file);
3006 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3007
3008 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3009 ctx);
3010
Philipp Zabel918c66f2013-06-27 06:59:01 -03003011 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003012 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003013
3014 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003015 if (ctx->initialized) {
3016 queue_work(dev->workqueue, &ctx->seq_end_work);
3017 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003018 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003019
3020 coda_free_framebuffers(ctx);
3021
Javier Martin186b2502012-07-26 05:53:35 -03003022 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003023 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003024 coda_unlock(ctx);
3025
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003026 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3027 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003028 coda_free_context_buffers(ctx);
3029 if (ctx->dev->devtype->product == CODA_DX6)
3030 coda_free_aux_buf(dev, &ctx->workbuf);
3031
3032 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003033 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003034 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003035 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003036 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003037 v4l2_fh_del(&ctx->fh);
3038 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003039 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003040 kfree(ctx);
3041
3042 return 0;
3043}
3044
Javier Martin186b2502012-07-26 05:53:35 -03003045static const struct v4l2_file_operations coda_fops = {
3046 .owner = THIS_MODULE,
3047 .open = coda_open,
3048 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003049 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003050 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003051 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003052};
3053
Philipp Zabel918c66f2013-06-27 06:59:01 -03003054static void coda_finish_decode(struct coda_ctx *ctx)
3055{
3056 struct coda_dev *dev = ctx->dev;
3057 struct coda_q_data *q_data_src;
3058 struct coda_q_data *q_data_dst;
3059 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003060 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003061 int width, height;
3062 int decoded_idx;
3063 int display_idx;
3064 u32 src_fourcc;
3065 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003066 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003067 u32 val;
3068
Philipp Zabel14604e32014-07-11 06:36:22 -03003069 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003070
3071 /* Update kfifo out pointer from coda bitstream read pointer */
3072 coda_kfifo_sync_from_device(ctx);
3073
3074 /*
3075 * in stream-end mode, the read pointer can overshoot the write pointer
3076 * by up to 512 bytes
3077 */
3078 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3079 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3080 kfifo_init(&ctx->bitstream_fifo,
3081 ctx->bitstream.vaddr, ctx->bitstream.size);
3082 }
3083
3084 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3085 src_fourcc = q_data_src->fourcc;
3086
3087 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3088 if (val != 1)
3089 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3090
3091 success = val & 0x1;
3092 if (!success)
3093 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3094
3095 if (src_fourcc == V4L2_PIX_FMT_H264) {
3096 if (val & (1 << 3))
3097 v4l2_err(&dev->v4l2_dev,
3098 "insufficient PS buffer space (%d bytes)\n",
3099 ctx->psbuf.size);
3100 if (val & (1 << 2))
3101 v4l2_err(&dev->v4l2_dev,
3102 "insufficient slice buffer space (%d bytes)\n",
3103 ctx->slicebuf.size);
3104 }
3105
3106 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3107 width = (val >> 16) & 0xffff;
3108 height = val & 0xffff;
3109
3110 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3111
Philipp Zabel52c41672014-07-11 06:36:19 -03003112 /* frame crop information */
3113 if (src_fourcc == V4L2_PIX_FMT_H264) {
3114 u32 left_right;
3115 u32 top_bottom;
3116
3117 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3118 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3119
3120 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3121 /* Keep current crop information */
3122 } else {
3123 struct v4l2_rect *rect = &q_data_dst->rect;
3124
3125 rect->left = left_right >> 16 & 0xffff;
3126 rect->top = top_bottom >> 16 & 0xffff;
3127 rect->width = width - rect->left -
3128 (left_right & 0xffff);
3129 rect->height = height - rect->top -
3130 (top_bottom & 0xffff);
3131 }
3132 } else {
3133 /* no cropping */
3134 }
3135
Philipp Zabel410e5e42014-07-11 06:36:32 -03003136 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3137 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003138 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003139 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003140
3141 if (dev->devtype->product == CODA_7541) {
3142 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3143 if (val == 0) {
3144 /* not enough bitstream data */
3145 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3146 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003147 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003148 return;
3149 }
3150 }
3151
3152 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3153
3154 /*
3155 * The previous display frame was copied out by the rotator,
3156 * now it can be overwritten again
3157 */
3158 if (ctx->display_idx >= 0 &&
3159 ctx->display_idx < ctx->num_internal_frames) {
3160 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3161 coda_write(dev, ctx->frm_dis_flg,
3162 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3163 }
3164
3165 /*
3166 * The index of the last decoded frame, not necessarily in
3167 * display order, and the index of the next display frame.
3168 * The latter could have been decoded in a previous run.
3169 */
3170 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3171 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3172
3173 if (decoded_idx == -1) {
3174 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003175 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3176 ctx->sequence_offset++;
3177 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003178 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003179 } else if (decoded_idx == -2) {
3180 /* no frame was decoded, we still return the remaining buffers */
3181 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3182 v4l2_err(&dev->v4l2_dev,
3183 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003184 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003185 ts = list_first_entry(&ctx->timestamp_list,
3186 struct coda_timestamp, list);
3187 list_del(&ts->list);
3188 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003189 val -= ctx->sequence_offset;
3190 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003191 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003192 "sequence number mismatch (%d(%d) != %d)\n",
3193 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003194 }
3195 ctx->frame_timestamps[decoded_idx] = *ts;
3196 kfree(ts);
3197
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003198 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3199 if (val == 0)
3200 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3201 else if (val == 1)
3202 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3203 else
3204 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003205
3206 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003207 }
3208
3209 if (display_idx == -1) {
3210 /*
3211 * no more frames to be decoded, but there could still
3212 * be rotator output to dequeue
3213 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003214 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003215 } else if (display_idx == -3) {
3216 /* possibly prescan failure */
3217 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3218 v4l2_err(&dev->v4l2_dev,
3219 "presentation frame index out of range: %d\n",
3220 display_idx);
3221 }
3222
3223 /* If a frame was copied out, return it */
3224 if (ctx->display_idx >= 0 &&
3225 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003226 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003227 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3228
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003229 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3230 V4L2_BUF_FLAG_PFRAME |
3231 V4L2_BUF_FLAG_BFRAME);
3232 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003233 ts = &ctx->frame_timestamps[ctx->display_idx];
3234 dst_buf->v4l2_buf.timecode = ts->timecode;
3235 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003236
Philipp Zabel918c66f2013-06-27 06:59:01 -03003237 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3238
Philipp Zabel410e5e42014-07-11 06:36:32 -03003239 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3240 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003241
3242 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3243 "job finished: decoding frame (%d) (%s)\n",
3244 dst_buf->v4l2_buf.sequence,
3245 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3246 "KEYFRAME" : "PFRAME");
3247 } else {
3248 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3249 "job finished: no frame decoded\n");
3250 }
3251
3252 /* The rotator will copy the current display frame next time */
3253 ctx->display_idx = display_idx;
3254}
3255
3256static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003257{
Philipp Zabelec25f682012-07-20 08:54:29 -03003258 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003259 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003260 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003261
Philipp Zabel14604e32014-07-11 06:36:22 -03003262 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3263 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003264
3265 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003266 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003267 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3268
Javier Martin186b2502012-07-26 05:53:35 -03003269 /* Calculate bytesused field */
3270 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003271 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3272 ctx->vpu_header_size[0] +
3273 ctx->vpu_header_size[1] +
3274 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003275 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003276 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003277 }
3278
3279 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3280 wr_ptr - start_ptr);
3281
3282 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3283 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3284
Philipp Zabel51660282013-09-30 10:34:49 -03003285 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003286 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3287 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3288 } else {
3289 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3290 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3291 }
3292
Kamil Debskiccd571c2013-04-24 10:38:24 -03003293 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003294 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3295 dst_buf->v4l2_buf.flags |=
3296 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003297 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3298
Philipp Zabelec25f682012-07-20 08:54:29 -03003299 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003300
Philipp Zabel14604e32014-07-11 06:36:22 -03003301 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003302 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3303
3304 ctx->gopcounter--;
3305 if (ctx->gopcounter < 0)
3306 ctx->gopcounter = ctx->params.gop_size - 1;
3307
3308 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3309 "job finished: encoding frame (%d) (%s)\n",
3310 dst_buf->v4l2_buf.sequence,
3311 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3312 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003313}
3314
3315static irqreturn_t coda_irq_handler(int irq, void *data)
3316{
3317 struct coda_dev *dev = data;
3318 struct coda_ctx *ctx;
3319
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003320 /* read status register to attend the IRQ */
3321 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3322 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3323 CODA_REG_BIT_INT_CLEAR);
3324
3325 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3326 if (ctx == NULL) {
3327 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3328 mutex_unlock(&dev->coda_mutex);
3329 return IRQ_HANDLED;
3330 }
3331
3332 if (ctx->aborting) {
3333 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3334 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003335 }
3336
3337 if (coda_isbusy(ctx->dev)) {
3338 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3339 "coda is still busy!!!!\n");
3340 return IRQ_NONE;
3341 }
3342
Philipp Zabel32b6f202014-07-11 06:36:20 -03003343 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003344
3345 return IRQ_HANDLED;
3346}
3347
3348static u32 coda_supported_firmwares[] = {
3349 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003350 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003351 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003352};
3353
3354static bool coda_firmware_supported(u32 vernum)
3355{
3356 int i;
3357
3358 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3359 if (vernum == coda_supported_firmwares[i])
3360 return true;
3361 return false;
3362}
3363
Philipp Zabel87048bb2012-07-02 07:03:43 -03003364static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003365{
Javier Martin186b2502012-07-26 05:53:35 -03003366 u32 data;
3367 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003368 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003369
Fabio Estevam79830db2013-08-21 11:14:17 -03003370 ret = clk_prepare_enable(dev->clk_per);
3371 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003372 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003373
3374 ret = clk_prepare_enable(dev->clk_ahb);
3375 if (ret)
3376 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003377
Philipp Zabel8f452842014-07-11 06:36:35 -03003378 if (dev->rstc)
3379 reset_control_reset(dev->rstc);
3380
Javier Martin186b2502012-07-26 05:53:35 -03003381 /*
3382 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003383 * The 16-bit chars in the code buffer are in memory access
3384 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003385 * Data in this SRAM survives a reboot.
3386 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003387 p = (u16 *)dev->codebuf.vaddr;
3388 if (dev->devtype->product == CODA_DX6) {
3389 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3390 data = CODA_DOWN_ADDRESS_SET(i) |
3391 CODA_DOWN_DATA_SET(p[i ^ 1]);
3392 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3393 }
3394 } else {
3395 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3396 data = CODA_DOWN_ADDRESS_SET(i) |
3397 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3398 3 - (i % 4)]);
3399 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3400 }
Javier Martin186b2502012-07-26 05:53:35 -03003401 }
Javier Martin186b2502012-07-26 05:53:35 -03003402
Philipp Zabel9acf7692013-05-23 10:42:56 -03003403 /* Clear registers */
3404 for (i = 0; i < 64; i++)
3405 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3406
Javier Martin186b2502012-07-26 05:53:35 -03003407 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003408 if (dev->devtype->product == CODA_960 ||
3409 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003410 coda_write(dev, dev->tempbuf.paddr,
3411 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003412 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003413 } else {
3414 coda_write(dev, dev->workbuf.paddr,
3415 CODA_REG_BIT_WORK_BUF_ADDR);
3416 }
Javier Martin186b2502012-07-26 05:53:35 -03003417 coda_write(dev, dev->codebuf.paddr,
3418 CODA_REG_BIT_CODE_BUF_ADDR);
3419 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3420
3421 /* Set default values */
3422 switch (dev->devtype->product) {
3423 case CODA_DX6:
3424 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3425 break;
3426 default:
3427 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3428 }
Philipp Zabel89548442014-07-11 06:36:17 -03003429 if (dev->devtype->product == CODA_960)
3430 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3431 else
3432 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003433
3434 if (dev->devtype->product != CODA_DX6)
3435 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3436
Javier Martin186b2502012-07-26 05:53:35 -03003437 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3438 CODA_REG_BIT_INT_ENABLE);
3439
3440 /* Reset VPU and start processor */
3441 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3442 data |= CODA_REG_RESET_ENABLE;
3443 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3444 udelay(10);
3445 data &= ~CODA_REG_RESET_ENABLE;
3446 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3447 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3448
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003449 clk_disable_unprepare(dev->clk_ahb);
3450 clk_disable_unprepare(dev->clk_per);
3451
3452 return 0;
3453
3454err_clk_ahb:
3455 clk_disable_unprepare(dev->clk_per);
3456err_clk_per:
3457 return ret;
3458}
3459
3460static int coda_check_firmware(struct coda_dev *dev)
3461{
3462 u16 product, major, minor, release;
3463 u32 data;
3464 int ret;
3465
3466 ret = clk_prepare_enable(dev->clk_per);
3467 if (ret)
3468 goto err_clk_per;
3469
3470 ret = clk_prepare_enable(dev->clk_ahb);
3471 if (ret)
3472 goto err_clk_ahb;
3473
Javier Martin186b2502012-07-26 05:53:35 -03003474 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3475 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3476 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3477 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3478 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3479 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003480 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003481 ret = -EIO;
3482 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003483 }
3484
Philipp Zabel89548442014-07-11 06:36:17 -03003485 if (dev->devtype->product == CODA_960) {
3486 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3487 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3488 data);
3489 }
3490
Javier Martin186b2502012-07-26 05:53:35 -03003491 /* Check we are compatible with the loaded firmware */
3492 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3493 product = CODA_FIRMWARE_PRODUCT(data);
3494 major = CODA_FIRMWARE_MAJOR(data);
3495 minor = CODA_FIRMWARE_MINOR(data);
3496 release = CODA_FIRMWARE_RELEASE(data);
3497
3498 clk_disable_unprepare(dev->clk_per);
3499 clk_disable_unprepare(dev->clk_ahb);
3500
3501 if (product != dev->devtype->product) {
3502 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3503 " Version: %u.%u.%u\n",
3504 coda_product_name(dev->devtype->product),
3505 coda_product_name(product), major, minor, release);
3506 return -EINVAL;
3507 }
3508
3509 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3510 coda_product_name(product));
3511
3512 if (coda_firmware_supported(data)) {
3513 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3514 major, minor, release);
3515 } else {
3516 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3517 "%u.%u.%u\n", major, minor, release);
3518 }
3519
3520 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003521
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003522err_run_cmd:
3523 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003524err_clk_ahb:
3525 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003526err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003527 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003528}
3529
3530static void coda_fw_callback(const struct firmware *fw, void *context)
3531{
3532 struct coda_dev *dev = context;
3533 struct platform_device *pdev = dev->plat_dev;
3534 int ret;
3535
3536 if (!fw) {
3537 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3538 return;
3539 }
3540
3541 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003542 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3543 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003544 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3545 return;
3546 }
3547
Philipp Zabel87048bb2012-07-02 07:03:43 -03003548 /* Copy the whole firmware image to the code buffer */
3549 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3550 release_firmware(fw);
3551
Philipp Zabel1e172732014-07-11 06:36:23 -03003552 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3553 /*
3554 * Enabling power temporarily will cause coda_hw_init to be
3555 * called via coda_runtime_resume by the pm domain.
3556 */
3557 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3558 if (ret < 0) {
3559 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3560 ret);
3561 return;
3562 }
3563
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003564 ret = coda_check_firmware(dev);
3565 if (ret < 0)
3566 return;
3567
Philipp Zabel1e172732014-07-11 06:36:23 -03003568 pm_runtime_put_sync(&dev->plat_dev->dev);
3569 } else {
3570 /*
3571 * If runtime pm is disabled or pm_domain is not set,
3572 * initialize once manually.
3573 */
3574 ret = coda_hw_init(dev);
3575 if (ret < 0) {
3576 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3577 return;
3578 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003579
3580 ret = coda_check_firmware(dev);
3581 if (ret < 0)
3582 return;
Javier Martin186b2502012-07-26 05:53:35 -03003583 }
3584
3585 dev->vfd.fops = &coda_fops,
3586 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3587 dev->vfd.release = video_device_release_empty,
3588 dev->vfd.lock = &dev->dev_mutex;
3589 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003590 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003591 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3592 video_set_drvdata(&dev->vfd, dev);
3593
3594 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3595 if (IS_ERR(dev->alloc_ctx)) {
3596 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3597 return;
3598 }
3599
3600 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3601 if (IS_ERR(dev->m2m_dev)) {
3602 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3603 goto rel_ctx;
3604 }
3605
3606 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3607 if (ret) {
3608 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3609 goto rel_m2m;
3610 }
3611 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3612 dev->vfd.num);
3613
3614 return;
3615
3616rel_m2m:
3617 v4l2_m2m_release(dev->m2m_dev);
3618rel_ctx:
3619 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3620}
3621
3622static int coda_firmware_request(struct coda_dev *dev)
3623{
3624 char *fw = dev->devtype->firmware;
3625
3626 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3627 coda_product_name(dev->devtype->product));
3628
3629 return request_firmware_nowait(THIS_MODULE, true,
3630 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3631}
3632
3633enum coda_platform {
3634 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003635 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003636 CODA_IMX6Q,
3637 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003638};
3639
Emil Goodec06d8752012-08-14 17:44:42 -03003640static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003641 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003642 .firmware = "v4l-codadx6-imx27.bin",
3643 .product = CODA_DX6,
3644 .codecs = codadx6_codecs,
3645 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003646 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003647 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003648 .firmware = "v4l-coda7541-imx53.bin",
3649 .product = CODA_7541,
3650 .codecs = coda7_codecs,
3651 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003652 },
Philipp Zabel89548442014-07-11 06:36:17 -03003653 [CODA_IMX6Q] = {
3654 .firmware = "v4l-coda960-imx6q.bin",
3655 .product = CODA_960,
3656 .codecs = coda9_codecs,
3657 .num_codecs = ARRAY_SIZE(coda9_codecs),
3658 },
3659 [CODA_IMX6DL] = {
3660 .firmware = "v4l-coda960-imx6dl.bin",
3661 .product = CODA_960,
3662 .codecs = coda9_codecs,
3663 .num_codecs = ARRAY_SIZE(coda9_codecs),
3664 },
Javier Martin186b2502012-07-26 05:53:35 -03003665};
3666
3667static struct platform_device_id coda_platform_ids[] = {
3668 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003669 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003670 { /* sentinel */ }
3671};
3672MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3673
3674#ifdef CONFIG_OF
3675static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003676 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003677 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003678 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3679 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003680 { /* sentinel */ }
3681};
3682MODULE_DEVICE_TABLE(of, coda_dt_ids);
3683#endif
3684
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003685static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003686{
3687 const struct of_device_id *of_id =
3688 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3689 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003690 struct coda_platform_data *pdata = pdev->dev.platform_data;
3691 struct device_node *np = pdev->dev.of_node;
3692 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003693 struct coda_dev *dev;
3694 struct resource *res;
3695 int ret, irq;
3696
3697 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3698 if (!dev) {
3699 dev_err(&pdev->dev, "Not enough memory for %s\n",
3700 CODA_NAME);
3701 return -ENOMEM;
3702 }
3703
3704 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003705 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003706
3707 dev->plat_dev = pdev;
3708 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3709 if (IS_ERR(dev->clk_per)) {
3710 dev_err(&pdev->dev, "Could not get per clock\n");
3711 return PTR_ERR(dev->clk_per);
3712 }
3713
3714 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3715 if (IS_ERR(dev->clk_ahb)) {
3716 dev_err(&pdev->dev, "Could not get ahb clock\n");
3717 return PTR_ERR(dev->clk_ahb);
3718 }
3719
3720 /* Get memory for physical registers */
3721 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003722 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3723 if (IS_ERR(dev->regs_base))
3724 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003725
3726 /* IRQ */
3727 irq = platform_get_irq(pdev, 0);
3728 if (irq < 0) {
3729 dev_err(&pdev->dev, "failed to get irq resource\n");
3730 return -ENOENT;
3731 }
3732
Philipp Zabel918c66f2013-06-27 06:59:01 -03003733 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003734 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003735 dev_err(&pdev->dev, "failed to request irq\n");
3736 return -ENOENT;
3737 }
3738
Philipp Zabel8f452842014-07-11 06:36:35 -03003739 dev->rstc = devm_reset_control_get(&pdev->dev, NULL);
3740 if (IS_ERR(dev->rstc)) {
3741 ret = PTR_ERR(dev->rstc);
3742 if (ret == -ENOENT) {
3743 dev->rstc = NULL;
3744 } else {
3745 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3746 return ret;
3747 }
3748 }
3749
Philipp Zabel657eee72013-04-29 16:17:14 -07003750 /* Get IRAM pool from device tree or platform data */
3751 pool = of_get_named_gen_pool(np, "iram", 0);
3752 if (!pool && pdata)
3753 pool = dev_get_gen_pool(pdata->iram_dev);
3754 if (!pool) {
3755 dev_err(&pdev->dev, "iram pool not available\n");
3756 return -ENOMEM;
3757 }
3758 dev->iram_pool = pool;
3759
Javier Martin186b2502012-07-26 05:53:35 -03003760 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3761 if (ret)
3762 return ret;
3763
3764 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003765 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003766
3767 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3768
3769 if (of_id) {
3770 dev->devtype = of_id->data;
3771 } else if (pdev_id) {
3772 dev->devtype = &coda_devdata[pdev_id->driver_data];
3773 } else {
3774 v4l2_device_unregister(&dev->v4l2_dev);
3775 return -EINVAL;
3776 }
3777
3778 /* allocate auxiliary per-device buffers for the BIT processor */
3779 switch (dev->devtype->product) {
3780 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003781 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3782 CODADX6_WORK_BUF_SIZE);
3783 if (ret < 0) {
3784 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3785 v4l2_device_unregister(&dev->v4l2_dev);
3786 return ret;
3787 }
Javier Martin186b2502012-07-26 05:53:35 -03003788 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003789 case CODA_7541:
3790 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3791 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003792 case CODA_960:
3793 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3794 break;
Javier Martin186b2502012-07-26 05:53:35 -03003795 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003796 if (dev->tempbuf.size) {
3797 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3798 dev->tempbuf.size);
3799 if (ret < 0) {
3800 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3801 v4l2_device_unregister(&dev->v4l2_dev);
3802 return ret;
3803 }
Javier Martin186b2502012-07-26 05:53:35 -03003804 }
3805
Philipp Zabel918c66f2013-06-27 06:59:01 -03003806 switch (dev->devtype->product) {
3807 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003808 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003809 break;
3810 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003811 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003812 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003813 case CODA_960:
3814 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003815 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003816 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3817 &dev->iram.paddr);
3818 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003819 dev_err(&pdev->dev, "unable to alloc iram\n");
3820 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003821 }
3822
Philipp Zabel32b6f202014-07-11 06:36:20 -03003823 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3824 if (!dev->workqueue) {
3825 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3826 return -ENOMEM;
3827 }
3828
Javier Martin186b2502012-07-26 05:53:35 -03003829 platform_set_drvdata(pdev, dev);
3830
Philipp Zabel1e172732014-07-11 06:36:23 -03003831 pm_runtime_enable(&pdev->dev);
3832
Javier Martin186b2502012-07-26 05:53:35 -03003833 return coda_firmware_request(dev);
3834}
3835
3836static int coda_remove(struct platform_device *pdev)
3837{
3838 struct coda_dev *dev = platform_get_drvdata(pdev);
3839
3840 video_unregister_device(&dev->vfd);
3841 if (dev->m2m_dev)
3842 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003843 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003844 if (dev->alloc_ctx)
3845 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3846 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003847 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003848 if (dev->iram.vaddr)
3849 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3850 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003851 coda_free_aux_buf(dev, &dev->codebuf);
3852 coda_free_aux_buf(dev, &dev->tempbuf);
3853 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003854 return 0;
3855}
3856
Philipp Zabel1e172732014-07-11 06:36:23 -03003857#ifdef CONFIG_PM_RUNTIME
3858static int coda_runtime_resume(struct device *dev)
3859{
3860 struct coda_dev *cdev = dev_get_drvdata(dev);
3861 int ret = 0;
3862
3863 if (dev->pm_domain) {
3864 ret = coda_hw_init(cdev);
3865 if (ret)
3866 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3867 }
3868
3869 return ret;
3870}
3871#endif
3872
3873static const struct dev_pm_ops coda_pm_ops = {
3874 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3875};
3876
Javier Martin186b2502012-07-26 05:53:35 -03003877static struct platform_driver coda_driver = {
3878 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003879 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003880 .driver = {
3881 .name = CODA_NAME,
3882 .owner = THIS_MODULE,
3883 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003884 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003885 },
3886 .id_table = coda_platform_ids,
3887};
3888
3889module_platform_driver(coda_driver);
3890
3891MODULE_LICENSE("GPL");
3892MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3893MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");