blob: aa80020960cdf52bb64a0ac502c95f2fb44f238b [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) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300741 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300742 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300743 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300744 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 Zabel7c3df782014-07-11 06:36:38 -03001747 int width, 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 Zabel7c3df782014-07-11 06:36:38 -03001753 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
1754 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
1755 width = round_up(q_data->width, 16);
1756 height = round_up(q_data->height, 16);
1757 } else {
1758 width = round_up(q_data->width, 8);
1759 height = q_data->height;
1760 }
1761 ysize = width * height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001762
Philipp Zabelec25f682012-07-20 08:54:29 -03001763 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001764 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001765 size_t size;
1766
Philipp Zabelaed14b02014-07-11 06:36:15 -03001767 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001768 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1769 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001770 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001771 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1772 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001773 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001774 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001775 }
1776 }
1777
1778 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001779 for (i = 0; i < ctx->num_internal_frames; i++) {
1780 paddr = ctx->internal_frames[i].paddr;
1781 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1782 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1783 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001784
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001785 /* mvcol buffer for h.264 */
1786 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1787 dev->devtype->product != CODA_DX6)
1788 coda_parabuf_write(ctx, 96 + i,
1789 ctx->internal_frames[i].paddr +
1790 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001791 }
1792
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001793 /* mvcol buffer for mpeg4 */
1794 if ((dev->devtype->product != CODA_DX6) &&
1795 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1796 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1797 ysize + ysize/4 + ysize/4);
1798
Philipp Zabelec25f682012-07-20 08:54:29 -03001799 return 0;
1800}
1801
Javier Martin3f3f5c72012-10-29 05:20:29 -03001802static int coda_h264_padding(int size, char *p)
1803{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001804 int nal_size;
1805 int diff;
1806
Javier Martin832fbb52012-10-29 08:34:59 -03001807 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001808 if (diff == 0)
1809 return 0;
1810
Javier Martin832fbb52012-10-29 08:34:59 -03001811 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001812 memcpy(p, coda_filler_nal, nal_size);
1813
1814 /* Add rbsp stop bit and trailing at the end */
1815 *(p + nal_size - 1) = 0x80;
1816
1817 return nal_size;
1818}
1819
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001820static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1821{
1822 phys_addr_t ret;
1823
1824 size = round_up(size, 1024);
1825 if (size > iram->remaining)
1826 return 0;
1827 iram->remaining -= size;
1828
1829 ret = iram->next_paddr;
1830 iram->next_paddr += size;
1831
1832 return ret;
1833}
1834
Philipp Zabelc2d22512013-06-21 03:55:28 -03001835static void coda_setup_iram(struct coda_ctx *ctx)
1836{
1837 struct coda_iram_info *iram_info = &ctx->iram_info;
1838 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001839 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001840 int dbk_bits;
1841 int bit_bits;
1842 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001843
1844 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001845 iram_info->next_paddr = dev->iram.paddr;
1846 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001847
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001848 switch (dev->devtype->product) {
1849 case CODA_7541:
1850 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1851 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1852 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1853 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001854 case CODA_960:
1855 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1856 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1857 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1858 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001859 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001860 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001861 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001862
1863 if (ctx->inst_type == CODA_INST_ENCODER) {
1864 struct coda_q_data *q_data_src;
1865
1866 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1867 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1868
1869 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001870 if (dev->devtype->product == CODA_7541) {
1871 iram_info->search_ram_size = round_up(mb_width * 16 *
1872 36 + 2048, 1024);
1873 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1874 iram_info->search_ram_size);
1875 if (!iram_info->search_ram_paddr) {
1876 pr_err("IRAM is smaller than the search ram size\n");
1877 goto out;
1878 }
1879 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1880 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001881 }
1882
1883 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001884 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1885 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1886 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001887 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001888 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001889
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001890 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1891 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001892 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001893 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001894
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001895 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1896 if (!iram_info->buf_ip_ac_dc_use)
1897 goto out;
1898 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001899
Philipp Zabel8358e762013-06-21 03:55:32 -03001900 /* OVL and BTP disabled for encoder */
1901 } else if (ctx->inst_type == CODA_INST_DECODER) {
1902 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001903
1904 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1905 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001906
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001907 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1908 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1909 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001910 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001911 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001912
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001913 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1914 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001915 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001916 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001917
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001918 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1919 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001920 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001921 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001922
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001923 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001924 }
1925
1926out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001927 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1928 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1929 "IRAM smaller than needed\n");
1930
1931 if (dev->devtype->product == CODA_7541) {
1932 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001933 if (ctx->inst_type == CODA_INST_DECODER) {
1934 /* fw 1.4.50 */
1935 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1936 CODA7_USE_IP_ENABLE);
1937 } else {
1938 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001939 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1940 CODA7_USE_HOST_DBK_ENABLE |
1941 CODA7_USE_IP_ENABLE |
1942 CODA7_USE_DBK_ENABLE);
1943 }
1944 }
1945}
1946
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001947static void coda_free_context_buffers(struct coda_ctx *ctx)
1948{
1949 struct coda_dev *dev = ctx->dev;
1950
Philipp Zabel918c66f2013-06-27 06:59:01 -03001951 coda_free_aux_buf(dev, &ctx->slicebuf);
1952 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001953 if (dev->devtype->product != CODA_DX6)
1954 coda_free_aux_buf(dev, &ctx->workbuf);
1955}
1956
1957static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1958 struct coda_q_data *q_data)
1959{
1960 struct coda_dev *dev = ctx->dev;
1961 size_t size;
1962 int ret;
1963
1964 switch (dev->devtype->product) {
1965 case CODA_7541:
1966 size = CODA7_WORK_BUF_SIZE;
1967 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001968 case CODA_960:
1969 size = CODA9_WORK_BUF_SIZE;
1970 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1971 size += CODA9_PS_SAVE_SIZE;
1972 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001973 default:
1974 return 0;
1975 }
1976
Philipp Zabel918c66f2013-06-27 06:59:01 -03001977 if (ctx->psbuf.vaddr) {
1978 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1979 return -EBUSY;
1980 }
1981 if (ctx->slicebuf.vaddr) {
1982 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1983 return -EBUSY;
1984 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001985 if (ctx->workbuf.vaddr) {
1986 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1987 ret = -EBUSY;
1988 return -ENOMEM;
1989 }
1990
Philipp Zabel918c66f2013-06-27 06:59:01 -03001991 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1992 /* worst case slice size */
1993 size = (DIV_ROUND_UP(q_data->width, 16) *
1994 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1995 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1996 if (ret < 0) {
1997 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1998 ctx->slicebuf.size);
1999 return ret;
2000 }
2001 }
2002
2003 if (dev->devtype->product == CODA_7541) {
2004 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
2005 if (ret < 0) {
2006 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
2007 goto err;
2008 }
2009 }
2010
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002011 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
2012 if (ret < 0) {
2013 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2014 ctx->workbuf.size);
2015 goto err;
2016 }
2017
2018 return 0;
2019
2020err:
2021 coda_free_context_buffers(ctx);
2022 return ret;
2023}
2024
Philipp Zabel918c66f2013-06-27 06:59:01 -03002025static int coda_start_decoding(struct coda_ctx *ctx)
2026{
2027 struct coda_q_data *q_data_src, *q_data_dst;
2028 u32 bitstream_buf, bitstream_size;
2029 struct coda_dev *dev = ctx->dev;
2030 int width, height;
2031 u32 src_fourcc;
2032 u32 val;
2033 int ret;
2034
2035 /* Start decoding */
2036 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2037 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2038 bitstream_buf = ctx->bitstream.paddr;
2039 bitstream_size = ctx->bitstream.size;
2040 src_fourcc = q_data_src->fourcc;
2041
2042 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2043
2044 /* Update coda bitstream read and write pointers from kfifo */
2045 coda_kfifo_sync_to_device_full(ctx);
2046
2047 ctx->display_idx = -1;
2048 ctx->frm_dis_flg = 0;
2049 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2050
2051 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2052 CODA_REG_BIT_BIT_STREAM_PARAM);
2053
2054 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2055 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2056 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002057 if ((dev->devtype->product == CODA_7541) ||
2058 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002059 val |= CODA_REORDER_ENABLE;
2060 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2061
2062 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002063 if (dev->devtype->product == CODA_960 &&
2064 src_fourcc == V4L2_PIX_FMT_MPEG4)
2065 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2066 else
2067 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002068 if (src_fourcc == V4L2_PIX_FMT_H264) {
2069 if (dev->devtype->product == CODA_7541) {
2070 coda_write(dev, ctx->psbuf.paddr,
2071 CODA_CMD_DEC_SEQ_PS_BB_START);
2072 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2073 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2074 }
Philipp Zabel89548442014-07-11 06:36:17 -03002075 if (dev->devtype->product == CODA_960) {
2076 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2077 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2078 }
2079 }
2080 if (dev->devtype->product != CODA_960) {
2081 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002082 }
2083
2084 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2085 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2086 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2087 return -ETIMEDOUT;
2088 }
2089
2090 /* Update kfifo out pointer from coda bitstream read pointer */
2091 coda_kfifo_sync_from_device(ctx);
2092
2093 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2094
2095 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2096 v4l2_err(&dev->v4l2_dev,
2097 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2098 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2099 return -EAGAIN;
2100 }
2101
2102 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2103 if (dev->devtype->product == CODA_DX6) {
2104 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2105 height = val & CODADX6_PICHEIGHT_MASK;
2106 } else {
2107 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2108 height = val & CODA7_PICHEIGHT_MASK;
2109 }
2110
2111 if (width > q_data_dst->width || height > q_data_dst->height) {
2112 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2113 width, height, q_data_dst->width, q_data_dst->height);
2114 return -EINVAL;
2115 }
2116
2117 width = round_up(width, 16);
2118 height = round_up(height, 16);
2119
2120 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2121 __func__, ctx->idx, width, height);
2122
Philipp Zabela500a932014-07-11 06:36:13 -03002123 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002124 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2125 v4l2_err(&dev->v4l2_dev,
2126 "not enough framebuffers to decode (%d < %d)\n",
2127 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2128 return -EINVAL;
2129 }
2130
Philipp Zabel52c41672014-07-11 06:36:19 -03002131 if (src_fourcc == V4L2_PIX_FMT_H264) {
2132 u32 left_right;
2133 u32 top_bottom;
2134
2135 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2136 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2137
2138 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2139 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2140 q_data_dst->rect.width = width - q_data_dst->rect.left -
2141 (left_right & 0x3ff);
2142 q_data_dst->rect.height = height - q_data_dst->rect.top -
2143 (top_bottom & 0x3ff);
2144 }
2145
Philipp Zabel918c66f2013-06-27 06:59:01 -03002146 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2147 if (ret < 0)
2148 return ret;
2149
2150 /* Tell the decoder how many frame buffers we allocated. */
2151 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2152 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2153
2154 if (dev->devtype->product != CODA_DX6) {
2155 /* Set secondary AXI IRAM */
2156 coda_setup_iram(ctx);
2157
2158 coda_write(dev, ctx->iram_info.buf_bit_use,
2159 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2160 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2161 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2162 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2163 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2164 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2165 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2166 coda_write(dev, ctx->iram_info.buf_ovl_use,
2167 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002168 if (dev->devtype->product == CODA_960)
2169 coda_write(dev, ctx->iram_info.buf_btp_use,
2170 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2171 }
2172
2173 if (dev->devtype->product == CODA_960) {
2174 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2175
2176 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2177 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2178 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2179 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2180 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2181 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002182 }
2183
2184 if (src_fourcc == V4L2_PIX_FMT_H264) {
2185 coda_write(dev, ctx->slicebuf.paddr,
2186 CODA_CMD_SET_FRAME_SLICE_BB_START);
2187 coda_write(dev, ctx->slicebuf.size / 1024,
2188 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2189 }
2190
2191 if (dev->devtype->product == CODA_7541) {
2192 int max_mb_x = 1920 / 16;
2193 int max_mb_y = 1088 / 16;
2194 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002195
Philipp Zabel918c66f2013-06-27 06:59:01 -03002196 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2197 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002198 } else if (dev->devtype->product == CODA_960) {
2199 int max_mb_x = 1920 / 16;
2200 int max_mb_y = 1088 / 16;
2201 int max_mb_num = max_mb_x * max_mb_y;
2202
2203 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2204 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002205 }
2206
2207 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2208 v4l2_err(&ctx->dev->v4l2_dev,
2209 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2210 return -ETIMEDOUT;
2211 }
2212
2213 return 0;
2214}
2215
Philipp Zabeld35167a2013-05-23 10:42:59 -03002216static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2217 int header_code, u8 *header, int *size)
2218{
2219 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002220 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002221 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002222 int i;
2223
2224 if (dev->devtype->product == CODA_960)
2225 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002226
2227 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2228 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002229 bufsize = vb2_plane_size(buf, 0);
2230 if (dev->devtype->product == CODA_960)
2231 bufsize /= 1024;
2232 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002233 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2234 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2235 if (ret < 0) {
2236 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2237 return ret;
2238 }
Philipp Zabel89548442014-07-11 06:36:17 -03002239
2240 if (dev->devtype->product == CODA_960) {
2241 for (i = 63; i > 0; i--)
2242 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2243 break;
2244 *size = i + 1;
2245 } else {
2246 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2247 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2248 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002249 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2250
2251 return 0;
2252}
2253
Philipp Zabel89548442014-07-11 06:36:17 -03002254static int coda_start_encoding(struct coda_ctx *ctx);
2255
Javier Martin186b2502012-07-26 05:53:35 -03002256static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2257{
2258 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2259 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002260 struct coda_dev *dev = ctx->dev;
2261 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002262 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002263 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002264
Philipp Zabelb96904e2013-05-23 10:42:58 -03002265 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002266 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2267 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2268 if (coda_get_bitstream_payload(ctx) < 512)
2269 return -EINVAL;
2270 } else {
2271 if (count < 1)
2272 return -EINVAL;
2273 }
2274
2275 ctx->streamon_out = 1;
2276
Philipp Zabelb96904e2013-05-23 10:42:58 -03002277 if (coda_format_is_yuv(q_data_src->fourcc))
2278 ctx->inst_type = CODA_INST_ENCODER;
2279 else
2280 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002281 } else {
2282 if (count < 1)
2283 return -EINVAL;
2284
2285 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002286 }
Javier Martin186b2502012-07-26 05:53:35 -03002287
Philipp Zabelb96904e2013-05-23 10:42:58 -03002288 /* Don't start the coda unless both queues are on */
2289 if (!(ctx->streamon_out & ctx->streamon_cap))
2290 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002291
Philipp Zabeleb107512013-09-30 10:34:45 -03002292 /* Allow decoder device_run with no new buffers queued */
2293 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002294 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002295
Philipp Zabelb96904e2013-05-23 10:42:58 -03002296 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002297 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002298 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002299
Philipp Zabelb96904e2013-05-23 10:42:58 -03002300 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2301 q_data_dst->fourcc);
2302 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002303 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2304 return -EINVAL;
2305 }
2306
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002307 /* Allocate per-instance buffers */
2308 ret = coda_alloc_context_buffers(ctx, q_data_src);
2309 if (ret < 0)
2310 return ret;
2311
Philipp Zabel918c66f2013-06-27 06:59:01 -03002312 if (ctx->inst_type == CODA_INST_DECODER) {
2313 mutex_lock(&dev->coda_mutex);
2314 ret = coda_start_decoding(ctx);
2315 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002316 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002317 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002318 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002319 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002320 } else {
2321 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002322 }
2323
Philipp Zabel89548442014-07-11 06:36:17 -03002324 ctx->initialized = 1;
2325 return ret;
2326}
2327
2328static int coda_start_encoding(struct coda_ctx *ctx)
2329{
2330 struct coda_dev *dev = ctx->dev;
2331 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2332 struct coda_q_data *q_data_src, *q_data_dst;
2333 u32 bitstream_buf, bitstream_size;
2334 struct vb2_buffer *buf;
2335 int gamma, ret, value;
2336 u32 dst_fourcc;
2337
2338 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2339 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2340 dst_fourcc = q_data_dst->fourcc;
2341
Philipp Zabel14604e32014-07-11 06:36:22 -03002342 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002343 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2344 bitstream_size = q_data_dst->sizeimage;
2345
Javier Martin186b2502012-07-26 05:53:35 -03002346 if (!coda_is_initialized(dev)) {
2347 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2348 return -EFAULT;
2349 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002350
2351 mutex_lock(&dev->coda_mutex);
2352
Javier Martin186b2502012-07-26 05:53:35 -03002353 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002354 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2355 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002356 switch (dev->devtype->product) {
2357 case CODA_DX6:
2358 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2359 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2360 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002361 case CODA_960:
2362 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2363 /* fallthrough */
2364 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002365 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2366 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002367 break;
Javier Martin186b2502012-07-26 05:53:35 -03002368 }
2369
Philipp Zabel89548442014-07-11 06:36:17 -03002370 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2371 value &= ~(1 << 2 | 0x7 << 9);
2372 ctx->frame_mem_ctrl = value;
2373 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2374
Philipp Zabel10436672012-07-02 09:03:55 -03002375 if (dev->devtype->product == CODA_DX6) {
2376 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002377 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002378 }
Javier Martin186b2502012-07-26 05:53:35 -03002379
2380 /* Could set rotation here if needed */
2381 switch (dev->devtype->product) {
2382 case CODA_DX6:
2383 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002384 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002385 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002386 case CODA_7541:
2387 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2388 value = (round_up(q_data_src->width, 16) &
2389 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2390 value |= (round_up(q_data_src->height, 16) &
2391 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2392 break;
2393 }
2394 /* fallthrough */
2395 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002396 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002397 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002398 }
Javier Martin186b2502012-07-26 05:53:35 -03002399 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2400 coda_write(dev, ctx->params.framerate,
2401 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2402
Philipp Zabelb96904e2013-05-23 10:42:58 -03002403 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002404 switch (dst_fourcc) {
2405 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002406 if (dev->devtype->product == CODA_960)
2407 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2408 else
2409 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002410 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2411 break;
2412 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002413 if (dev->devtype->product == CODA_960)
2414 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2415 else
2416 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002417 if (ctx->params.h264_deblk_enabled) {
2418 value = ((ctx->params.h264_deblk_alpha &
2419 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2420 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2421 ((ctx->params.h264_deblk_beta &
2422 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2423 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2424 } else {
2425 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2426 }
2427 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002428 break;
2429 default:
2430 v4l2_err(v4l2_dev,
2431 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002432 ret = -EINVAL;
2433 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002434 }
2435
Philipp Zabelc566c782012-08-08 11:59:38 -03002436 switch (ctx->params.slice_mode) {
2437 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2438 value = 0;
2439 break;
2440 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2441 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2442 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002443 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002444 break;
2445 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2446 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2447 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2448 value |= 1 & CODA_SLICING_MODE_MASK;
2449 break;
2450 }
Javier Martin186b2502012-07-26 05:53:35 -03002451 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002452 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002453 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2454
2455 if (ctx->params.bitrate) {
2456 /* Rate control enabled */
2457 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2458 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002459 if (dev->devtype->product == CODA_960)
2460 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002461 } else {
2462 value = 0;
2463 }
2464 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2465
2466 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002467 coda_write(dev, ctx->params.intra_refresh,
2468 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002469
2470 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2471 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2472
Javier Martin186b2502012-07-26 05:53:35 -03002473
Philipp Zabel89548442014-07-11 06:36:17 -03002474 value = 0;
2475 if (dev->devtype->product == CODA_960)
2476 gamma = CODA9_DEFAULT_GAMMA;
2477 else
2478 gamma = CODA_DEFAULT_GAMMA;
2479 if (gamma > 0) {
2480 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2481 CODA_CMD_ENC_SEQ_RC_GAMMA);
2482 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002483
2484 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2485 coda_write(dev,
2486 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2487 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2488 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2489 }
Philipp Zabel89548442014-07-11 06:36:17 -03002490 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002491 if (ctx->params.h264_max_qp)
2492 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002493 if (CODA_DEFAULT_GAMMA > 0)
2494 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002495 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002496 if (CODA_DEFAULT_GAMMA > 0) {
2497 if (dev->devtype->product == CODA_DX6)
2498 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2499 else
2500 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2501 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002502 if (ctx->params.h264_min_qp)
2503 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2504 if (ctx->params.h264_max_qp)
2505 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002506 }
Javier Martin186b2502012-07-26 05:53:35 -03002507 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2508
Philipp Zabel89548442014-07-11 06:36:17 -03002509 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2510
Philipp Zabelc2d22512013-06-21 03:55:28 -03002511 coda_setup_iram(ctx);
2512
Javier Martin186b2502012-07-26 05:53:35 -03002513 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002514 switch (dev->devtype->product) {
2515 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002516 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002517 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002518 break;
2519 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002520 coda_write(dev, ctx->iram_info.search_ram_paddr,
2521 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2522 coda_write(dev, ctx->iram_info.search_ram_size,
2523 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002524 break;
2525 case CODA_960:
2526 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2527 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002528 }
Javier Martin186b2502012-07-26 05:53:35 -03002529 }
2530
Philipp Zabelfcb62822013-05-23 10:43:00 -03002531 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2532 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002533 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002534 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002535 }
2536
Philipp Zabelfcb62822013-05-23 10:43:00 -03002537 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2538 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2539 ret = -EFAULT;
2540 goto out;
2541 }
Javier Martin186b2502012-07-26 05:53:35 -03002542
Philipp Zabel89548442014-07-11 06:36:17 -03002543 if (dev->devtype->product == CODA_960)
2544 ctx->num_internal_frames = 4;
2545 else
2546 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002547 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002548 if (ret < 0) {
2549 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2550 goto out;
2551 }
Javier Martin186b2502012-07-26 05:53:35 -03002552
Philipp Zabelec25f682012-07-20 08:54:29 -03002553 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002554 coda_write(dev, q_data_src->bytesperline,
2555 CODA_CMD_SET_FRAME_BUF_STRIDE);
2556 if (dev->devtype->product == CODA_7541) {
2557 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002558 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002559 }
Philipp Zabel10436672012-07-02 09:03:55 -03002560 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002561 coda_write(dev, ctx->iram_info.buf_bit_use,
2562 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2563 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2564 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2565 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2566 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2567 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2568 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2569 coda_write(dev, ctx->iram_info.buf_ovl_use,
2570 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002571 if (dev->devtype->product == CODA_960) {
2572 coda_write(dev, ctx->iram_info.buf_btp_use,
2573 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2574
2575 /* FIXME */
2576 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2577 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2578 }
Philipp Zabel10436672012-07-02 09:03:55 -03002579 }
Philipp Zabel89548442014-07-11 06:36:17 -03002580
Philipp Zabelfcb62822013-05-23 10:43:00 -03002581 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2582 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002583 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002584 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002585 }
2586
2587 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002588 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002589 switch (dst_fourcc) {
2590 case V4L2_PIX_FMT_H264:
2591 /*
2592 * Get SPS in the first frame and copy it to an
2593 * intermediate buffer.
2594 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002595 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2596 &ctx->vpu_header[0][0],
2597 &ctx->vpu_header_size[0]);
2598 if (ret < 0)
2599 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002600
2601 /*
2602 * Get PPS in the first frame and copy it to an
2603 * intermediate buffer.
2604 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002605 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2606 &ctx->vpu_header[1][0],
2607 &ctx->vpu_header_size[1]);
2608 if (ret < 0)
2609 goto out;
2610
Javier Martin3f3f5c72012-10-29 05:20:29 -03002611 /*
2612 * Length of H.264 headers is variable and thus it might not be
2613 * aligned for the coda to append the encoded frame. In that is
2614 * the case a filler NAL must be added to header 2.
2615 */
2616 ctx->vpu_header_size[2] = coda_h264_padding(
2617 (ctx->vpu_header_size[0] +
2618 ctx->vpu_header_size[1]),
2619 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002620 break;
2621 case V4L2_PIX_FMT_MPEG4:
2622 /*
2623 * Get VOS in the first frame and copy it to an
2624 * intermediate buffer
2625 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002626 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2627 &ctx->vpu_header[0][0],
2628 &ctx->vpu_header_size[0]);
2629 if (ret < 0)
2630 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002631
Philipp Zabeld35167a2013-05-23 10:42:59 -03002632 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2633 &ctx->vpu_header[1][0],
2634 &ctx->vpu_header_size[1]);
2635 if (ret < 0)
2636 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002637
Philipp Zabeld35167a2013-05-23 10:42:59 -03002638 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2639 &ctx->vpu_header[2][0],
2640 &ctx->vpu_header_size[2]);
2641 if (ret < 0)
2642 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002643 break;
2644 default:
2645 /* No more formats need to save headers at the moment */
2646 break;
2647 }
2648
Philipp Zabeld35167a2013-05-23 10:42:59 -03002649out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002650 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002651 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002652}
2653
Hans Verkuile37559b2014-04-17 02:47:21 -03002654static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002655{
2656 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002657 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002658
2659 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002660 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002661 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002662 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002663
Philipp Zabel89548442014-07-11 06:36:17 -03002664 if (ctx->inst_type == CODA_INST_DECODER &&
2665 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2666 /* if this decoder instance is running, set the stream end flag */
2667 if (dev->devtype->product == CODA_960) {
2668 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2669
2670 val |= CODA_BIT_STREAM_END_FLAG;
2671 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2672 ctx->bit_stream_param = val;
2673 }
2674 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002675 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2676
2677 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002678 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002679 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002680 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002681 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002682
2683 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002684 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002685 }
2686
Philipp Zabel918c66f2013-06-27 06:59:01 -03002687 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002688 struct coda_timestamp *ts;
2689
2690 while (!list_empty(&ctx->timestamp_list)) {
2691 ts = list_first_entry(&ctx->timestamp_list,
2692 struct coda_timestamp, list);
2693 list_del(&ts->list);
2694 kfree(ts);
2695 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002696 kfifo_init(&ctx->bitstream_fifo,
2697 ctx->bitstream.vaddr, ctx->bitstream.size);
2698 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002699 }
Javier Martin186b2502012-07-26 05:53:35 -03002700}
2701
2702static struct vb2_ops coda_qops = {
2703 .queue_setup = coda_queue_setup,
2704 .buf_prepare = coda_buf_prepare,
2705 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002706 .start_streaming = coda_start_streaming,
2707 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002708 .wait_prepare = vb2_ops_wait_prepare,
2709 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002710};
2711
2712static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2713{
2714 struct coda_ctx *ctx =
2715 container_of(ctrl->handler, struct coda_ctx, ctrls);
2716
2717 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2718 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2719
2720 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002721 case V4L2_CID_HFLIP:
2722 if (ctrl->val)
2723 ctx->params.rot_mode |= CODA_MIR_HOR;
2724 else
2725 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2726 break;
2727 case V4L2_CID_VFLIP:
2728 if (ctrl->val)
2729 ctx->params.rot_mode |= CODA_MIR_VER;
2730 else
2731 ctx->params.rot_mode &= ~CODA_MIR_VER;
2732 break;
Javier Martin186b2502012-07-26 05:53:35 -03002733 case V4L2_CID_MPEG_VIDEO_BITRATE:
2734 ctx->params.bitrate = ctrl->val / 1000;
2735 break;
2736 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2737 ctx->params.gop_size = ctrl->val;
2738 break;
2739 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2740 ctx->params.h264_intra_qp = ctrl->val;
2741 break;
2742 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2743 ctx->params.h264_inter_qp = ctrl->val;
2744 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002745 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2746 ctx->params.h264_min_qp = ctrl->val;
2747 break;
2748 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2749 ctx->params.h264_max_qp = ctrl->val;
2750 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002751 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2752 ctx->params.h264_deblk_alpha = ctrl->val;
2753 break;
2754 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2755 ctx->params.h264_deblk_beta = ctrl->val;
2756 break;
2757 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2758 ctx->params.h264_deblk_enabled = (ctrl->val ==
2759 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2760 break;
Javier Martin186b2502012-07-26 05:53:35 -03002761 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2762 ctx->params.mpeg4_intra_qp = ctrl->val;
2763 break;
2764 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2765 ctx->params.mpeg4_inter_qp = ctrl->val;
2766 break;
2767 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2768 ctx->params.slice_mode = ctrl->val;
2769 break;
2770 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2771 ctx->params.slice_max_mb = ctrl->val;
2772 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002773 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2774 ctx->params.slice_max_bits = ctrl->val * 8;
2775 break;
Javier Martin186b2502012-07-26 05:53:35 -03002776 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2777 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002778 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2779 ctx->params.intra_refresh = ctrl->val;
2780 break;
Javier Martin186b2502012-07-26 05:53:35 -03002781 default:
2782 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2783 "Invalid control, id=%d, val=%d\n",
2784 ctrl->id, ctrl->val);
2785 return -EINVAL;
2786 }
2787
2788 return 0;
2789}
2790
2791static struct v4l2_ctrl_ops coda_ctrl_ops = {
2792 .s_ctrl = coda_s_ctrl,
2793};
2794
2795static int coda_ctrls_setup(struct coda_ctx *ctx)
2796{
2797 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2798
2799 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002800 V4L2_CID_HFLIP, 0, 1, 1, 0);
2801 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2802 V4L2_CID_VFLIP, 0, 1, 1, 0);
2803 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002804 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2805 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2806 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2807 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002808 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002809 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002810 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002811 if (ctx->dev->devtype->product != CODA_960) {
2812 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2813 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2814 }
2815 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2816 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002817 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002818 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2819 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2820 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2821 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2822 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2823 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2824 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2825 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002826 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2827 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2828 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2829 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2830 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002831 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2832 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002833 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2834 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002835 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2836 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002837 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2838 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2839 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2840 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2841 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002842 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2843 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002844
2845 if (ctx->ctrls.error) {
2846 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2847 ctx->ctrls.error);
2848 return -EINVAL;
2849 }
2850
2851 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2852}
2853
2854static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2855 struct vb2_queue *dst_vq)
2856{
2857 struct coda_ctx *ctx = priv;
2858 int ret;
2859
Javier Martin186b2502012-07-26 05:53:35 -03002860 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002861 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002862 src_vq->drv_priv = ctx;
2863 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2864 src_vq->ops = &coda_qops;
2865 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002866 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002867 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002868
2869 ret = vb2_queue_init(src_vq);
2870 if (ret)
2871 return ret;
2872
Javier Martin186b2502012-07-26 05:53:35 -03002873 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002874 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002875 dst_vq->drv_priv = ctx;
2876 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2877 dst_vq->ops = &coda_qops;
2878 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002879 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002880 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002881
2882 return vb2_queue_init(dst_vq);
2883}
2884
Philipp Zabele11f3e62012-07-25 09:16:58 -03002885static int coda_next_free_instance(struct coda_dev *dev)
2886{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002887 int idx = ffz(dev->instance_mask);
2888
2889 if ((idx < 0) ||
2890 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2891 return -EBUSY;
2892
2893 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002894}
2895
Javier Martin186b2502012-07-26 05:53:35 -03002896static int coda_open(struct file *file)
2897{
2898 struct coda_dev *dev = video_drvdata(file);
2899 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002900 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002901 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002902
Javier Martin186b2502012-07-26 05:53:35 -03002903 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2904 if (!ctx)
2905 return -ENOMEM;
2906
Fabio Estevamf82bc202013-08-21 11:14:16 -03002907 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002908 if (idx < 0) {
2909 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002910 goto err_coda_max;
2911 }
2912 set_bit(idx, &dev->instance_mask);
2913
Philipp Zabel32b6f202014-07-11 06:36:20 -03002914 init_completion(&ctx->completion);
2915 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2916 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002917 v4l2_fh_init(&ctx->fh, video_devdata(file));
2918 file->private_data = &ctx->fh;
2919 v4l2_fh_add(&ctx->fh);
2920 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002921 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002922 switch (dev->devtype->product) {
2923 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002924 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002925 ctx->reg_idx = 0;
2926 break;
2927 default:
2928 ctx->reg_idx = idx;
2929 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002930
Philipp Zabel1e172732014-07-11 06:36:23 -03002931 /* Power up and upload firmware if necessary */
2932 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2933 if (ret < 0) {
2934 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2935 goto err_pm_get;
2936 }
2937
Fabio Estevam79830db2013-08-21 11:14:17 -03002938 ret = clk_prepare_enable(dev->clk_per);
2939 if (ret)
2940 goto err_clk_per;
2941
2942 ret = clk_prepare_enable(dev->clk_ahb);
2943 if (ret)
2944 goto err_clk_ahb;
2945
Javier Martin186b2502012-07-26 05:53:35 -03002946 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002947 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002948 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002949 if (IS_ERR(ctx->fh.m2m_ctx)) {
2950 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002951
2952 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2953 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002954 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002955 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002956
Javier Martin186b2502012-07-26 05:53:35 -03002957 ret = coda_ctrls_setup(ctx);
2958 if (ret) {
2959 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002960 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002961 }
2962
2963 ctx->fh.ctrl_handler = &ctx->ctrls;
2964
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002965 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2966 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002967 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002968 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002969 }
2970
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002971 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2972 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2973 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2974 if (!ctx->bitstream.vaddr) {
2975 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2976 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002977 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002978 }
2979 kfifo_init(&ctx->bitstream_fifo,
2980 ctx->bitstream.vaddr, ctx->bitstream.size);
2981 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002982 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002983 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002984
Javier Martin186b2502012-07-26 05:53:35 -03002985 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002986 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002987 coda_unlock(ctx);
2988
Javier Martin186b2502012-07-26 05:53:35 -03002989 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2990 ctx->idx, ctx);
2991
2992 return 0;
2993
Fabio Estevamf82bc202013-08-21 11:14:16 -03002994err_dma_writecombine:
2995 coda_free_context_buffers(ctx);
2996 if (ctx->dev->devtype->product == CODA_DX6)
2997 coda_free_aux_buf(dev, &ctx->workbuf);
2998 coda_free_aux_buf(dev, &ctx->parabuf);
2999err_dma_alloc:
3000 v4l2_ctrl_handler_free(&ctx->ctrls);
3001err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03003002 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003003err_ctx_init:
3004 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003005err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03003006 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03003007err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03003008 pm_runtime_put_sync(&dev->plat_dev->dev);
3009err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03003010 v4l2_fh_del(&ctx->fh);
3011 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003012 clear_bit(ctx->idx, &dev->instance_mask);
3013err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03003014 kfree(ctx);
3015 return ret;
3016}
3017
3018static int coda_release(struct file *file)
3019{
3020 struct coda_dev *dev = video_drvdata(file);
3021 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3022
3023 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3024 ctx);
3025
Philipp Zabel918c66f2013-06-27 06:59:01 -03003026 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003027 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003028
3029 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003030 if (ctx->initialized) {
3031 queue_work(dev->workqueue, &ctx->seq_end_work);
3032 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003033 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003034
3035 coda_free_framebuffers(ctx);
3036
Javier Martin186b2502012-07-26 05:53:35 -03003037 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003038 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003039 coda_unlock(ctx);
3040
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003041 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3042 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003043 coda_free_context_buffers(ctx);
3044 if (ctx->dev->devtype->product == CODA_DX6)
3045 coda_free_aux_buf(dev, &ctx->workbuf);
3046
3047 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003048 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003049 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003050 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003051 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003052 v4l2_fh_del(&ctx->fh);
3053 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003054 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003055 kfree(ctx);
3056
3057 return 0;
3058}
3059
Javier Martin186b2502012-07-26 05:53:35 -03003060static const struct v4l2_file_operations coda_fops = {
3061 .owner = THIS_MODULE,
3062 .open = coda_open,
3063 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003064 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003065 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003066 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003067};
3068
Philipp Zabel918c66f2013-06-27 06:59:01 -03003069static void coda_finish_decode(struct coda_ctx *ctx)
3070{
3071 struct coda_dev *dev = ctx->dev;
3072 struct coda_q_data *q_data_src;
3073 struct coda_q_data *q_data_dst;
3074 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003075 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003076 int width, height;
3077 int decoded_idx;
3078 int display_idx;
3079 u32 src_fourcc;
3080 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003081 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003082 u32 val;
3083
Philipp Zabel14604e32014-07-11 06:36:22 -03003084 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003085
3086 /* Update kfifo out pointer from coda bitstream read pointer */
3087 coda_kfifo_sync_from_device(ctx);
3088
3089 /*
3090 * in stream-end mode, the read pointer can overshoot the write pointer
3091 * by up to 512 bytes
3092 */
3093 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3094 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3095 kfifo_init(&ctx->bitstream_fifo,
3096 ctx->bitstream.vaddr, ctx->bitstream.size);
3097 }
3098
3099 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3100 src_fourcc = q_data_src->fourcc;
3101
3102 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3103 if (val != 1)
3104 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3105
3106 success = val & 0x1;
3107 if (!success)
3108 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3109
3110 if (src_fourcc == V4L2_PIX_FMT_H264) {
3111 if (val & (1 << 3))
3112 v4l2_err(&dev->v4l2_dev,
3113 "insufficient PS buffer space (%d bytes)\n",
3114 ctx->psbuf.size);
3115 if (val & (1 << 2))
3116 v4l2_err(&dev->v4l2_dev,
3117 "insufficient slice buffer space (%d bytes)\n",
3118 ctx->slicebuf.size);
3119 }
3120
3121 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3122 width = (val >> 16) & 0xffff;
3123 height = val & 0xffff;
3124
3125 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3126
Philipp Zabel52c41672014-07-11 06:36:19 -03003127 /* frame crop information */
3128 if (src_fourcc == V4L2_PIX_FMT_H264) {
3129 u32 left_right;
3130 u32 top_bottom;
3131
3132 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3133 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3134
3135 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3136 /* Keep current crop information */
3137 } else {
3138 struct v4l2_rect *rect = &q_data_dst->rect;
3139
3140 rect->left = left_right >> 16 & 0xffff;
3141 rect->top = top_bottom >> 16 & 0xffff;
3142 rect->width = width - rect->left -
3143 (left_right & 0xffff);
3144 rect->height = height - rect->top -
3145 (top_bottom & 0xffff);
3146 }
3147 } else {
3148 /* no cropping */
3149 }
3150
Philipp Zabel410e5e42014-07-11 06:36:32 -03003151 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3152 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003153 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003154 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003155
3156 if (dev->devtype->product == CODA_7541) {
3157 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3158 if (val == 0) {
3159 /* not enough bitstream data */
3160 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3161 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003162 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003163 return;
3164 }
3165 }
3166
3167 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3168
3169 /*
3170 * The previous display frame was copied out by the rotator,
3171 * now it can be overwritten again
3172 */
3173 if (ctx->display_idx >= 0 &&
3174 ctx->display_idx < ctx->num_internal_frames) {
3175 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3176 coda_write(dev, ctx->frm_dis_flg,
3177 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3178 }
3179
3180 /*
3181 * The index of the last decoded frame, not necessarily in
3182 * display order, and the index of the next display frame.
3183 * The latter could have been decoded in a previous run.
3184 */
3185 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3186 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3187
3188 if (decoded_idx == -1) {
3189 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003190 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3191 ctx->sequence_offset++;
3192 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003193 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003194 } else if (decoded_idx == -2) {
3195 /* no frame was decoded, we still return the remaining buffers */
3196 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3197 v4l2_err(&dev->v4l2_dev,
3198 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003199 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003200 ts = list_first_entry(&ctx->timestamp_list,
3201 struct coda_timestamp, list);
3202 list_del(&ts->list);
3203 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003204 val -= ctx->sequence_offset;
3205 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003206 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003207 "sequence number mismatch (%d(%d) != %d)\n",
3208 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003209 }
3210 ctx->frame_timestamps[decoded_idx] = *ts;
3211 kfree(ts);
3212
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003213 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3214 if (val == 0)
3215 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3216 else if (val == 1)
3217 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3218 else
3219 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003220
3221 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003222 }
3223
3224 if (display_idx == -1) {
3225 /*
3226 * no more frames to be decoded, but there could still
3227 * be rotator output to dequeue
3228 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003229 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003230 } else if (display_idx == -3) {
3231 /* possibly prescan failure */
3232 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3233 v4l2_err(&dev->v4l2_dev,
3234 "presentation frame index out of range: %d\n",
3235 display_idx);
3236 }
3237
3238 /* If a frame was copied out, return it */
3239 if (ctx->display_idx >= 0 &&
3240 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003241 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003242 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3243
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003244 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3245 V4L2_BUF_FLAG_PFRAME |
3246 V4L2_BUF_FLAG_BFRAME);
3247 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003248 ts = &ctx->frame_timestamps[ctx->display_idx];
3249 dst_buf->v4l2_buf.timecode = ts->timecode;
3250 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003251
Philipp Zabel918c66f2013-06-27 06:59:01 -03003252 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3253
Philipp Zabel410e5e42014-07-11 06:36:32 -03003254 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3255 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003256
3257 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3258 "job finished: decoding frame (%d) (%s)\n",
3259 dst_buf->v4l2_buf.sequence,
3260 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3261 "KEYFRAME" : "PFRAME");
3262 } else {
3263 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3264 "job finished: no frame decoded\n");
3265 }
3266
3267 /* The rotator will copy the current display frame next time */
3268 ctx->display_idx = display_idx;
3269}
3270
3271static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003272{
Philipp Zabelec25f682012-07-20 08:54:29 -03003273 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003274 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003275 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003276
Philipp Zabel14604e32014-07-11 06:36:22 -03003277 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3278 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003279
3280 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003281 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003282 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3283
Javier Martin186b2502012-07-26 05:53:35 -03003284 /* Calculate bytesused field */
3285 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003286 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3287 ctx->vpu_header_size[0] +
3288 ctx->vpu_header_size[1] +
3289 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003290 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003291 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003292 }
3293
3294 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3295 wr_ptr - start_ptr);
3296
3297 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3298 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3299
Philipp Zabel51660282013-09-30 10:34:49 -03003300 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003301 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3302 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3303 } else {
3304 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3305 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3306 }
3307
Kamil Debskiccd571c2013-04-24 10:38:24 -03003308 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003309 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3310 dst_buf->v4l2_buf.flags |=
3311 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003312 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3313
Philipp Zabelec25f682012-07-20 08:54:29 -03003314 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003315
Philipp Zabel14604e32014-07-11 06:36:22 -03003316 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003317 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3318
3319 ctx->gopcounter--;
3320 if (ctx->gopcounter < 0)
3321 ctx->gopcounter = ctx->params.gop_size - 1;
3322
3323 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3324 "job finished: encoding frame (%d) (%s)\n",
3325 dst_buf->v4l2_buf.sequence,
3326 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3327 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003328}
3329
3330static irqreturn_t coda_irq_handler(int irq, void *data)
3331{
3332 struct coda_dev *dev = data;
3333 struct coda_ctx *ctx;
3334
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003335 /* read status register to attend the IRQ */
3336 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3337 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3338 CODA_REG_BIT_INT_CLEAR);
3339
3340 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3341 if (ctx == NULL) {
3342 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3343 mutex_unlock(&dev->coda_mutex);
3344 return IRQ_HANDLED;
3345 }
3346
3347 if (ctx->aborting) {
3348 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3349 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003350 }
3351
3352 if (coda_isbusy(ctx->dev)) {
3353 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3354 "coda is still busy!!!!\n");
3355 return IRQ_NONE;
3356 }
3357
Philipp Zabel32b6f202014-07-11 06:36:20 -03003358 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003359
3360 return IRQ_HANDLED;
3361}
3362
3363static u32 coda_supported_firmwares[] = {
3364 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003365 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003366 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003367};
3368
3369static bool coda_firmware_supported(u32 vernum)
3370{
3371 int i;
3372
3373 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3374 if (vernum == coda_supported_firmwares[i])
3375 return true;
3376 return false;
3377}
3378
Philipp Zabel87048bb2012-07-02 07:03:43 -03003379static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003380{
Javier Martin186b2502012-07-26 05:53:35 -03003381 u32 data;
3382 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003383 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003384
Fabio Estevam79830db2013-08-21 11:14:17 -03003385 ret = clk_prepare_enable(dev->clk_per);
3386 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003387 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003388
3389 ret = clk_prepare_enable(dev->clk_ahb);
3390 if (ret)
3391 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003392
Philipp Zabel8f452842014-07-11 06:36:35 -03003393 if (dev->rstc)
3394 reset_control_reset(dev->rstc);
3395
Javier Martin186b2502012-07-26 05:53:35 -03003396 /*
3397 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003398 * The 16-bit chars in the code buffer are in memory access
3399 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003400 * Data in this SRAM survives a reboot.
3401 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003402 p = (u16 *)dev->codebuf.vaddr;
3403 if (dev->devtype->product == CODA_DX6) {
3404 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3405 data = CODA_DOWN_ADDRESS_SET(i) |
3406 CODA_DOWN_DATA_SET(p[i ^ 1]);
3407 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3408 }
3409 } else {
3410 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3411 data = CODA_DOWN_ADDRESS_SET(i) |
3412 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3413 3 - (i % 4)]);
3414 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3415 }
Javier Martin186b2502012-07-26 05:53:35 -03003416 }
Javier Martin186b2502012-07-26 05:53:35 -03003417
Philipp Zabel9acf7692013-05-23 10:42:56 -03003418 /* Clear registers */
3419 for (i = 0; i < 64; i++)
3420 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3421
Javier Martin186b2502012-07-26 05:53:35 -03003422 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003423 if (dev->devtype->product == CODA_960 ||
3424 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003425 coda_write(dev, dev->tempbuf.paddr,
3426 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003427 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003428 } else {
3429 coda_write(dev, dev->workbuf.paddr,
3430 CODA_REG_BIT_WORK_BUF_ADDR);
3431 }
Javier Martin186b2502012-07-26 05:53:35 -03003432 coda_write(dev, dev->codebuf.paddr,
3433 CODA_REG_BIT_CODE_BUF_ADDR);
3434 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3435
3436 /* Set default values */
3437 switch (dev->devtype->product) {
3438 case CODA_DX6:
3439 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3440 break;
3441 default:
3442 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3443 }
Philipp Zabel89548442014-07-11 06:36:17 -03003444 if (dev->devtype->product == CODA_960)
3445 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3446 else
3447 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003448
3449 if (dev->devtype->product != CODA_DX6)
3450 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3451
Javier Martin186b2502012-07-26 05:53:35 -03003452 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3453 CODA_REG_BIT_INT_ENABLE);
3454
3455 /* Reset VPU and start processor */
3456 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3457 data |= CODA_REG_RESET_ENABLE;
3458 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3459 udelay(10);
3460 data &= ~CODA_REG_RESET_ENABLE;
3461 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3462 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3463
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003464 clk_disable_unprepare(dev->clk_ahb);
3465 clk_disable_unprepare(dev->clk_per);
3466
3467 return 0;
3468
3469err_clk_ahb:
3470 clk_disable_unprepare(dev->clk_per);
3471err_clk_per:
3472 return ret;
3473}
3474
3475static int coda_check_firmware(struct coda_dev *dev)
3476{
3477 u16 product, major, minor, release;
3478 u32 data;
3479 int ret;
3480
3481 ret = clk_prepare_enable(dev->clk_per);
3482 if (ret)
3483 goto err_clk_per;
3484
3485 ret = clk_prepare_enable(dev->clk_ahb);
3486 if (ret)
3487 goto err_clk_ahb;
3488
Javier Martin186b2502012-07-26 05:53:35 -03003489 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3490 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3491 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3492 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3493 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3494 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003495 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003496 ret = -EIO;
3497 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003498 }
3499
Philipp Zabel89548442014-07-11 06:36:17 -03003500 if (dev->devtype->product == CODA_960) {
3501 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3502 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3503 data);
3504 }
3505
Javier Martin186b2502012-07-26 05:53:35 -03003506 /* Check we are compatible with the loaded firmware */
3507 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3508 product = CODA_FIRMWARE_PRODUCT(data);
3509 major = CODA_FIRMWARE_MAJOR(data);
3510 minor = CODA_FIRMWARE_MINOR(data);
3511 release = CODA_FIRMWARE_RELEASE(data);
3512
3513 clk_disable_unprepare(dev->clk_per);
3514 clk_disable_unprepare(dev->clk_ahb);
3515
3516 if (product != dev->devtype->product) {
3517 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3518 " Version: %u.%u.%u\n",
3519 coda_product_name(dev->devtype->product),
3520 coda_product_name(product), major, minor, release);
3521 return -EINVAL;
3522 }
3523
3524 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3525 coda_product_name(product));
3526
3527 if (coda_firmware_supported(data)) {
3528 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3529 major, minor, release);
3530 } else {
3531 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3532 "%u.%u.%u\n", major, minor, release);
3533 }
3534
3535 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003536
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003537err_run_cmd:
3538 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003539err_clk_ahb:
3540 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003541err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003542 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003543}
3544
3545static void coda_fw_callback(const struct firmware *fw, void *context)
3546{
3547 struct coda_dev *dev = context;
3548 struct platform_device *pdev = dev->plat_dev;
3549 int ret;
3550
3551 if (!fw) {
3552 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3553 return;
3554 }
3555
3556 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003557 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3558 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003559 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3560 return;
3561 }
3562
Philipp Zabel87048bb2012-07-02 07:03:43 -03003563 /* Copy the whole firmware image to the code buffer */
3564 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3565 release_firmware(fw);
3566
Philipp Zabel1e172732014-07-11 06:36:23 -03003567 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3568 /*
3569 * Enabling power temporarily will cause coda_hw_init to be
3570 * called via coda_runtime_resume by the pm domain.
3571 */
3572 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3573 if (ret < 0) {
3574 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3575 ret);
3576 return;
3577 }
3578
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003579 ret = coda_check_firmware(dev);
3580 if (ret < 0)
3581 return;
3582
Philipp Zabel1e172732014-07-11 06:36:23 -03003583 pm_runtime_put_sync(&dev->plat_dev->dev);
3584 } else {
3585 /*
3586 * If runtime pm is disabled or pm_domain is not set,
3587 * initialize once manually.
3588 */
3589 ret = coda_hw_init(dev);
3590 if (ret < 0) {
3591 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3592 return;
3593 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003594
3595 ret = coda_check_firmware(dev);
3596 if (ret < 0)
3597 return;
Javier Martin186b2502012-07-26 05:53:35 -03003598 }
3599
3600 dev->vfd.fops = &coda_fops,
3601 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3602 dev->vfd.release = video_device_release_empty,
3603 dev->vfd.lock = &dev->dev_mutex;
3604 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003605 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003606 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3607 video_set_drvdata(&dev->vfd, dev);
3608
3609 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3610 if (IS_ERR(dev->alloc_ctx)) {
3611 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3612 return;
3613 }
3614
3615 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3616 if (IS_ERR(dev->m2m_dev)) {
3617 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3618 goto rel_ctx;
3619 }
3620
3621 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3622 if (ret) {
3623 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3624 goto rel_m2m;
3625 }
3626 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3627 dev->vfd.num);
3628
3629 return;
3630
3631rel_m2m:
3632 v4l2_m2m_release(dev->m2m_dev);
3633rel_ctx:
3634 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3635}
3636
3637static int coda_firmware_request(struct coda_dev *dev)
3638{
3639 char *fw = dev->devtype->firmware;
3640
3641 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3642 coda_product_name(dev->devtype->product));
3643
3644 return request_firmware_nowait(THIS_MODULE, true,
3645 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3646}
3647
3648enum coda_platform {
3649 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003650 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003651 CODA_IMX6Q,
3652 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003653};
3654
Emil Goodec06d8752012-08-14 17:44:42 -03003655static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003656 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003657 .firmware = "v4l-codadx6-imx27.bin",
3658 .product = CODA_DX6,
3659 .codecs = codadx6_codecs,
3660 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003661 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003662 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003663 .firmware = "v4l-coda7541-imx53.bin",
3664 .product = CODA_7541,
3665 .codecs = coda7_codecs,
3666 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003667 },
Philipp Zabel89548442014-07-11 06:36:17 -03003668 [CODA_IMX6Q] = {
3669 .firmware = "v4l-coda960-imx6q.bin",
3670 .product = CODA_960,
3671 .codecs = coda9_codecs,
3672 .num_codecs = ARRAY_SIZE(coda9_codecs),
3673 },
3674 [CODA_IMX6DL] = {
3675 .firmware = "v4l-coda960-imx6dl.bin",
3676 .product = CODA_960,
3677 .codecs = coda9_codecs,
3678 .num_codecs = ARRAY_SIZE(coda9_codecs),
3679 },
Javier Martin186b2502012-07-26 05:53:35 -03003680};
3681
3682static struct platform_device_id coda_platform_ids[] = {
3683 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003684 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003685 { /* sentinel */ }
3686};
3687MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3688
3689#ifdef CONFIG_OF
3690static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003691 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003692 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003693 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3694 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003695 { /* sentinel */ }
3696};
3697MODULE_DEVICE_TABLE(of, coda_dt_ids);
3698#endif
3699
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003700static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003701{
3702 const struct of_device_id *of_id =
3703 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3704 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003705 struct coda_platform_data *pdata = pdev->dev.platform_data;
3706 struct device_node *np = pdev->dev.of_node;
3707 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003708 struct coda_dev *dev;
3709 struct resource *res;
3710 int ret, irq;
3711
3712 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3713 if (!dev) {
3714 dev_err(&pdev->dev, "Not enough memory for %s\n",
3715 CODA_NAME);
3716 return -ENOMEM;
3717 }
3718
3719 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003720 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003721
3722 dev->plat_dev = pdev;
3723 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3724 if (IS_ERR(dev->clk_per)) {
3725 dev_err(&pdev->dev, "Could not get per clock\n");
3726 return PTR_ERR(dev->clk_per);
3727 }
3728
3729 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3730 if (IS_ERR(dev->clk_ahb)) {
3731 dev_err(&pdev->dev, "Could not get ahb clock\n");
3732 return PTR_ERR(dev->clk_ahb);
3733 }
3734
3735 /* Get memory for physical registers */
3736 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003737 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3738 if (IS_ERR(dev->regs_base))
3739 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003740
3741 /* IRQ */
3742 irq = platform_get_irq(pdev, 0);
3743 if (irq < 0) {
3744 dev_err(&pdev->dev, "failed to get irq resource\n");
3745 return -ENOENT;
3746 }
3747
Philipp Zabel918c66f2013-06-27 06:59:01 -03003748 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003749 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003750 dev_err(&pdev->dev, "failed to request irq\n");
3751 return -ENOENT;
3752 }
3753
Philipp Zabel8f452842014-07-11 06:36:35 -03003754 dev->rstc = devm_reset_control_get(&pdev->dev, NULL);
3755 if (IS_ERR(dev->rstc)) {
3756 ret = PTR_ERR(dev->rstc);
3757 if (ret == -ENOENT) {
3758 dev->rstc = NULL;
3759 } else {
3760 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3761 return ret;
3762 }
3763 }
3764
Philipp Zabel657eee72013-04-29 16:17:14 -07003765 /* Get IRAM pool from device tree or platform data */
3766 pool = of_get_named_gen_pool(np, "iram", 0);
3767 if (!pool && pdata)
3768 pool = dev_get_gen_pool(pdata->iram_dev);
3769 if (!pool) {
3770 dev_err(&pdev->dev, "iram pool not available\n");
3771 return -ENOMEM;
3772 }
3773 dev->iram_pool = pool;
3774
Javier Martin186b2502012-07-26 05:53:35 -03003775 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3776 if (ret)
3777 return ret;
3778
3779 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003780 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003781
3782 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3783
3784 if (of_id) {
3785 dev->devtype = of_id->data;
3786 } else if (pdev_id) {
3787 dev->devtype = &coda_devdata[pdev_id->driver_data];
3788 } else {
3789 v4l2_device_unregister(&dev->v4l2_dev);
3790 return -EINVAL;
3791 }
3792
3793 /* allocate auxiliary per-device buffers for the BIT processor */
3794 switch (dev->devtype->product) {
3795 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003796 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3797 CODADX6_WORK_BUF_SIZE);
3798 if (ret < 0) {
3799 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3800 v4l2_device_unregister(&dev->v4l2_dev);
3801 return ret;
3802 }
Javier Martin186b2502012-07-26 05:53:35 -03003803 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003804 case CODA_7541:
3805 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3806 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003807 case CODA_960:
3808 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3809 break;
Javier Martin186b2502012-07-26 05:53:35 -03003810 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003811 if (dev->tempbuf.size) {
3812 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3813 dev->tempbuf.size);
3814 if (ret < 0) {
3815 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3816 v4l2_device_unregister(&dev->v4l2_dev);
3817 return ret;
3818 }
Javier Martin186b2502012-07-26 05:53:35 -03003819 }
3820
Philipp Zabel918c66f2013-06-27 06:59:01 -03003821 switch (dev->devtype->product) {
3822 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003823 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003824 break;
3825 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003826 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003827 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003828 case CODA_960:
3829 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003830 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003831 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3832 &dev->iram.paddr);
3833 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003834 dev_err(&pdev->dev, "unable to alloc iram\n");
3835 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003836 }
3837
Philipp Zabel32b6f202014-07-11 06:36:20 -03003838 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3839 if (!dev->workqueue) {
3840 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3841 return -ENOMEM;
3842 }
3843
Javier Martin186b2502012-07-26 05:53:35 -03003844 platform_set_drvdata(pdev, dev);
3845
Philipp Zabel1e172732014-07-11 06:36:23 -03003846 pm_runtime_enable(&pdev->dev);
3847
Javier Martin186b2502012-07-26 05:53:35 -03003848 return coda_firmware_request(dev);
3849}
3850
3851static int coda_remove(struct platform_device *pdev)
3852{
3853 struct coda_dev *dev = platform_get_drvdata(pdev);
3854
3855 video_unregister_device(&dev->vfd);
3856 if (dev->m2m_dev)
3857 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003858 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003859 if (dev->alloc_ctx)
3860 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3861 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003862 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003863 if (dev->iram.vaddr)
3864 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3865 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003866 coda_free_aux_buf(dev, &dev->codebuf);
3867 coda_free_aux_buf(dev, &dev->tempbuf);
3868 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003869 return 0;
3870}
3871
Philipp Zabel1e172732014-07-11 06:36:23 -03003872#ifdef CONFIG_PM_RUNTIME
3873static int coda_runtime_resume(struct device *dev)
3874{
3875 struct coda_dev *cdev = dev_get_drvdata(dev);
3876 int ret = 0;
3877
3878 if (dev->pm_domain) {
3879 ret = coda_hw_init(cdev);
3880 if (ret)
3881 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3882 }
3883
3884 return ret;
3885}
3886#endif
3887
3888static const struct dev_pm_ops coda_pm_ops = {
3889 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3890};
3891
Javier Martin186b2502012-07-26 05:53:35 -03003892static struct platform_driver coda_driver = {
3893 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003894 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003895 .driver = {
3896 .name = CODA_NAME,
3897 .owner = THIS_MODULE,
3898 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003899 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003900 },
3901 .id_table = coda_platform_ids,
3902};
3903
3904module_platform_driver(coda_driver);
3905
3906MODULE_LICENSE("GPL");
3907MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3908MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");