blob: 04a2d8be20d495d3d103cf0fadb6836f5327b394 [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>
Philipp Zabelf61c0b12014-07-11 06:36:40 -030015#include <linux/debugfs.h>
Javier Martin186b2502012-07-26 05:53:35 -030016#include <linux/delay.h>
17#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070018#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030022#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030023#include <linux/module.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030026#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030027#include <linux/slab.h>
28#include <linux/videodev2.h>
29#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070030#include <linux/platform_data/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030031#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030032
33#include <media/v4l2-ctrls.h>
34#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030035#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030036#include <media/v4l2-ioctl.h>
37#include <media/v4l2-mem2mem.h>
38#include <media/videobuf2-core.h>
39#include <media/videobuf2-dma-contig.h>
40
41#include "coda.h"
42
43#define CODA_NAME "coda"
44
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030045#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030046
47#define CODA_FMO_BUF_SIZE 32
48#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030049#define CODA7_WORK_BUF_SIZE (128 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030050#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030051#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030052#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030053#define CODA_PARA_BUF_SIZE (10 * 1024)
54#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070055#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030056#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030057#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030058
Philipp Zabel918c66f2013-06-27 06:59:01 -030059#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030060#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030061
62#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030063
Philipp Zabelb96904e2013-05-23 10:42:58 -030064#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030065#define FMO_SLICE_SAVE_BUF_SIZE (32)
66#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030067#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030068
69#define MIN_W 176
70#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030071
72#define S_ALIGN 1 /* multiple of 2 */
73#define W_ALIGN 1 /* multiple of 2 */
74#define H_ALIGN 1 /* multiple of 2 */
75
76#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
77
78static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030079module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030080MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
81
82enum {
83 V4L2_M2M_SRC = 0,
84 V4L2_M2M_DST = 1,
85};
86
Javier Martin186b2502012-07-26 05:53:35 -030087enum coda_inst_type {
88 CODA_INST_ENCODER,
89 CODA_INST_DECODER,
90};
91
92enum coda_product {
93 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030094 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030095 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030096};
97
98struct coda_fmt {
99 char *name;
100 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300101};
102
103struct coda_codec {
104 u32 mode;
105 u32 src_fourcc;
106 u32 dst_fourcc;
107 u32 max_w;
108 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300109};
110
111struct coda_devtype {
112 char *firmware;
113 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300114 struct coda_codec *codecs;
115 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300116 size_t workbuf_size;
117};
118
119/* Per-queue, driver-specific private data */
120struct coda_q_data {
121 unsigned int width;
122 unsigned int height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300123 unsigned int bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300124 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300125 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300126 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300127};
128
129struct coda_aux_buf {
130 void *vaddr;
131 dma_addr_t paddr;
132 u32 size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300133 struct debugfs_blob_wrapper blob;
134 struct dentry *dentry;
Javier Martin186b2502012-07-26 05:53:35 -0300135};
136
137struct coda_dev {
138 struct v4l2_device v4l2_dev;
139 struct video_device vfd;
140 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300141 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300142
143 void __iomem *regs_base;
144 struct clk *clk_per;
145 struct clk *clk_ahb;
Philipp Zabel8f452842014-07-11 06:36:35 -0300146 struct reset_control *rstc;
Javier Martin186b2502012-07-26 05:53:35 -0300147
148 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300149 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300150 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700151 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300152 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300153
154 spinlock_t irqlock;
155 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300156 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300157 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300158 struct v4l2_m2m_dev *m2m_dev;
159 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300160 struct list_head instances;
161 unsigned long instance_mask;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300162 struct dentry *debugfs_root;
Javier Martin186b2502012-07-26 05:53:35 -0300163};
164
165struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300166 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300167 u8 h264_intra_qp;
168 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300169 u8 h264_min_qp;
170 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300171 u8 h264_deblk_enabled;
172 u8 h264_deblk_alpha;
173 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300174 u8 mpeg4_intra_qp;
175 u8 mpeg4_inter_qp;
176 u8 gop_size;
Philipp Zabelf38f79d52014-07-11 06:36:28 -0300177 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300178 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300179 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300180 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
181 u32 framerate;
182 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300183 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300184 u32 slice_max_mb;
185};
186
Philipp Zabelc2d22512013-06-21 03:55:28 -0300187struct coda_iram_info {
188 u32 axi_sram_use;
189 phys_addr_t buf_bit_use;
190 phys_addr_t buf_ip_ac_dc_use;
191 phys_addr_t buf_dbk_y_use;
192 phys_addr_t buf_dbk_c_use;
193 phys_addr_t buf_ovl_use;
194 phys_addr_t buf_btp_use;
195 phys_addr_t search_ram_paddr;
196 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300197 int remaining;
198 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300199};
200
Philipp Zabel89548442014-07-11 06:36:17 -0300201struct gdi_tiled_map {
202 int xy2ca_map[16];
203 int xy2ba_map[16];
204 int xy2ra_map[16];
205 int rbc2axi_map[32];
206 int xy2rbc_config;
207 int map_type;
208#define GDI_LINEAR_FRAME_MAP 0
209};
210
Philipp Zabel846ced92014-07-11 06:36:31 -0300211struct coda_timestamp {
212 struct list_head list;
213 u32 sequence;
214 struct v4l2_timecode timecode;
215 struct timeval timestamp;
216};
217
Javier Martin186b2502012-07-26 05:53:35 -0300218struct coda_ctx {
219 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300220 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300221 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300222 struct work_struct pic_run_work;
223 struct work_struct seq_end_work;
224 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300225 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300226 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300227 int streamon_out;
228 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300229 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300230 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300231 u32 osequence;
Philipp Zabelcb2c0282014-07-11 06:36:33 -0300232 u32 sequence_offset;
Javier Martin186b2502012-07-26 05:53:35 -0300233 struct coda_q_data q_data[2];
234 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300235 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300236 enum v4l2_colorspace colorspace;
237 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300238 struct v4l2_ctrl_handler ctrls;
239 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300240 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300241 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300242 char vpu_header[3][64];
243 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300244 struct kfifo bitstream_fifo;
245 struct mutex bitstream_mutex;
246 struct coda_aux_buf bitstream;
Philipp Zabel84e23652014-07-11 06:36:34 -0300247 bool hold;
Javier Martin186b2502012-07-26 05:53:35 -0300248 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300249 struct coda_aux_buf psbuf;
250 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300251 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300252 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300253 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300254 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300255 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300256 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300257 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300258 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300259 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300260 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300261 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300262 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300263 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300264 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300265 int display_idx;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300266 struct dentry *debugfs_entry;
Javier Martin186b2502012-07-26 05:53:35 -0300267};
268
Javier Martin832fbb52012-10-29 08:34:59 -0300269static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
270 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
271static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300272
Javier Martin186b2502012-07-26 05:53:35 -0300273static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
274{
275 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
276 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
277 writel(data, dev->regs_base + reg);
278}
279
280static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
281{
282 u32 data;
283 data = readl(dev->regs_base + reg);
284 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
285 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
286 return data;
287}
288
289static inline unsigned long coda_isbusy(struct coda_dev *dev)
290{
291 return coda_read(dev, CODA_REG_BIT_BUSY);
292}
293
294static inline int coda_is_initialized(struct coda_dev *dev)
295{
296 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
297}
298
299static int coda_wait_timeout(struct coda_dev *dev)
300{
301 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
302
303 while (coda_isbusy(dev)) {
304 if (time_after(jiffies, timeout))
305 return -ETIMEDOUT;
306 }
307 return 0;
308}
309
310static void coda_command_async(struct coda_ctx *ctx, int cmd)
311{
312 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300313
Philipp Zabel89548442014-07-11 06:36:17 -0300314 if (dev->devtype->product == CODA_960 ||
315 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300316 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300317 coda_write(dev, ctx->bit_stream_param,
318 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300319 coda_write(dev, ctx->frm_dis_flg,
320 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300321 coda_write(dev, ctx->frame_mem_ctrl,
322 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300323 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
324 }
325
Philipp Zabel89548442014-07-11 06:36:17 -0300326 if (dev->devtype->product == CODA_960) {
327 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
328 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
329 }
330
Javier Martin186b2502012-07-26 05:53:35 -0300331 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
332
333 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
334 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300335 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
336
Javier Martin186b2502012-07-26 05:53:35 -0300337 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
338}
339
340static int coda_command_sync(struct coda_ctx *ctx, int cmd)
341{
342 struct coda_dev *dev = ctx->dev;
343
344 coda_command_async(ctx, cmd);
345 return coda_wait_timeout(dev);
346}
347
Philipp Zabel8f452842014-07-11 06:36:35 -0300348static int coda_hw_reset(struct coda_ctx *ctx)
349{
350 struct coda_dev *dev = ctx->dev;
351 unsigned long timeout;
352 unsigned int idx;
353 int ret;
354
355 if (!dev->rstc)
356 return -ENOENT;
357
358 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
359
360 timeout = jiffies + msecs_to_jiffies(100);
361 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
362 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
363 if (time_after(jiffies, timeout))
364 return -ETIME;
365 cpu_relax();
366 }
367
368 ret = reset_control_reset(dev->rstc);
369 if (ret < 0)
370 return ret;
371
372 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
373 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
374 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
375 ret = coda_wait_timeout(dev);
376 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
377
378 return ret;
379}
380
Javier Martin186b2502012-07-26 05:53:35 -0300381static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
382 enum v4l2_buf_type type)
383{
384 switch (type) {
385 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
386 return &(ctx->q_data[V4L2_M2M_SRC]);
387 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
388 return &(ctx->q_data[V4L2_M2M_DST]);
389 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300390 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300391 }
Javier Martin186b2502012-07-26 05:53:35 -0300392}
393
394/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300395 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300396 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300397static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300398 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300399 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300400 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300401 },
402 {
403 .name = "YUV 4:2:0 Planar, YCrCb",
404 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300405 },
406 {
407 .name = "H264 Encoded Stream",
408 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300409 },
410 {
411 .name = "MPEG4 Encoded Stream",
412 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300413 },
414};
415
Philipp Zabelb96904e2013-05-23 10:42:58 -0300416#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
417 { mode, src_fourcc, dst_fourcc, max_w, max_h }
418
419/*
420 * Arrays of codecs supported by each given version of Coda:
421 * i.MX27 -> codadx6
422 * i.MX5x -> coda7
423 * i.MX6 -> coda960
424 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
425 */
426static struct coda_codec codadx6_codecs[] = {
427 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
428 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300429};
430
Philipp Zabelb96904e2013-05-23 10:42:58 -0300431static struct coda_codec coda7_codecs[] = {
432 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
433 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300434 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
435 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300436};
437
Philipp Zabel89548442014-07-11 06:36:17 -0300438static struct coda_codec coda9_codecs[] = {
439 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
440 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
441 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
442 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
443};
444
Philipp Zabelb96904e2013-05-23 10:42:58 -0300445static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300446{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300447 switch (fourcc) {
448 case V4L2_PIX_FMT_YUV420:
449 case V4L2_PIX_FMT_YVU420:
450 return true;
451 default:
452 return false;
453 }
454}
Javier Martin186b2502012-07-26 05:53:35 -0300455
Philipp Zabelb96904e2013-05-23 10:42:58 -0300456/*
457 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
458 * tables.
459 */
460static u32 coda_format_normalize_yuv(u32 fourcc)
461{
462 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
463}
464
465static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
466 int dst_fourcc)
467{
468 struct coda_codec *codecs = dev->devtype->codecs;
469 int num_codecs = dev->devtype->num_codecs;
470 int k;
471
472 src_fourcc = coda_format_normalize_yuv(src_fourcc);
473 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
474 if (src_fourcc == dst_fourcc)
475 return NULL;
476
477 for (k = 0; k < num_codecs; k++) {
478 if (codecs[k].src_fourcc == src_fourcc &&
479 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300480 break;
481 }
482
Philipp Zabelb96904e2013-05-23 10:42:58 -0300483 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300484 return NULL;
485
Philipp Zabelb96904e2013-05-23 10:42:58 -0300486 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300487}
488
Philipp Zabel57625592013-09-30 10:34:51 -0300489static void coda_get_max_dimensions(struct coda_dev *dev,
490 struct coda_codec *codec,
491 int *max_w, int *max_h)
492{
493 struct coda_codec *codecs = dev->devtype->codecs;
494 int num_codecs = dev->devtype->num_codecs;
495 unsigned int w, h;
496 int k;
497
498 if (codec) {
499 w = codec->max_w;
500 h = codec->max_h;
501 } else {
502 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
503 w = max(w, codecs[k].max_w);
504 h = max(h, codecs[k].max_h);
505 }
506 }
507
508 if (max_w)
509 *max_w = w;
510 if (max_h)
511 *max_h = h;
512}
513
Philipp Zabel927933f2013-09-30 10:34:48 -0300514static char *coda_product_name(int product)
515{
516 static char buf[9];
517
518 switch (product) {
519 case CODA_DX6:
520 return "CodaDx6";
521 case CODA_7541:
522 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300523 case CODA_960:
524 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300525 default:
526 snprintf(buf, sizeof(buf), "(0x%04x)", product);
527 return buf;
528 }
529}
530
Javier Martin186b2502012-07-26 05:53:35 -0300531/*
532 * V4L2 ioctl() operations.
533 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300534static int coda_querycap(struct file *file, void *priv,
535 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300536{
Philipp Zabel927933f2013-09-30 10:34:48 -0300537 struct coda_ctx *ctx = fh_to_ctx(priv);
538
Javier Martin186b2502012-07-26 05:53:35 -0300539 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300540 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
541 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300542 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300543 /*
544 * This is only a mem-to-mem video device. The capture and output
545 * device capability flags are left only for backward compatibility
546 * and are scheduled for removal.
547 */
548 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
549 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300550 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
551
552 return 0;
553}
554
555static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300556 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300557{
558 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300559 struct coda_codec *codecs = ctx->dev->devtype->codecs;
560 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300561 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300562 int num_codecs = ctx->dev->devtype->num_codecs;
563 int num_formats = ARRAY_SIZE(coda_formats);
564 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300565
566 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300567 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300568 if (coda_format_is_yuv(formats[i].fourcc) &&
569 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300570 if (num == f->index)
571 break;
572 ++num;
573 continue;
574 }
575 /* Compressed formats may be supported, check the codec list */
576 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300577 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300578 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300579 formats[i].fourcc == codecs[k].dst_fourcc &&
580 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300581 break;
582 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
583 formats[i].fourcc == codecs[k].src_fourcc)
584 break;
585 }
586 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300587 if (num == f->index)
588 break;
589 ++num;
590 }
591 }
592
593 if (i < num_formats) {
594 fmt = &formats[i];
595 strlcpy(f->description, fmt->name, sizeof(f->description));
596 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300597 if (!coda_format_is_yuv(fmt->fourcc))
598 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300599 return 0;
600 }
601
602 /* Format not found */
603 return -EINVAL;
604}
605
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300606static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
607 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300608{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300609 struct coda_ctx *ctx = fh_to_ctx(priv);
610 struct vb2_queue *src_vq;
611 struct coda_q_data *q_data_src;
612
613 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300614 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300615 if (vb2_is_streaming(src_vq)) {
616 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
617
618 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
619 q_data_src->fourcc);
620 }
621
622 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300623}
624
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300625static int coda_enum_fmt_vid_out(struct file *file, void *priv,
626 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300627{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300628 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300629}
630
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300631static int coda_g_fmt(struct file *file, void *priv,
632 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300633{
Javier Martin186b2502012-07-26 05:53:35 -0300634 struct coda_q_data *q_data;
635 struct coda_ctx *ctx = fh_to_ctx(priv);
636
Javier Martin186b2502012-07-26 05:53:35 -0300637 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300638 if (!q_data)
639 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300640
641 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300642 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300643 f->fmt.pix.width = q_data->width;
644 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300645 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300646
647 f->fmt.pix.sizeimage = q_data->sizeimage;
648 f->fmt.pix.colorspace = ctx->colorspace;
649
650 return 0;
651}
652
Philipp Zabel57625592013-09-30 10:34:51 -0300653static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
654 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300655{
Philipp Zabel57625592013-09-30 10:34:51 -0300656 struct coda_dev *dev = ctx->dev;
657 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300658 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300659 enum v4l2_field field;
660
661 field = f->fmt.pix.field;
662 if (field == V4L2_FIELD_ANY)
663 field = V4L2_FIELD_NONE;
664 else if (V4L2_FIELD_NONE != field)
665 return -EINVAL;
666
667 /* V4L2 specification suggests the driver corrects the format struct
668 * if any of the dimensions is unsupported */
669 f->fmt.pix.field = field;
670
Philipp Zabel57625592013-09-30 10:34:51 -0300671 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
672 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
673 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
674 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300675
Philipp Zabel57625592013-09-30 10:34:51 -0300676 switch (f->fmt.pix.pixelformat) {
677 case V4L2_PIX_FMT_YUV420:
678 case V4L2_PIX_FMT_YVU420:
679 case V4L2_PIX_FMT_H264:
680 case V4L2_PIX_FMT_MPEG4:
681 case V4L2_PIX_FMT_JPEG:
682 break;
683 default:
684 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300685 if (!q_data)
686 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300687 f->fmt.pix.pixelformat = q_data->fourcc;
688 }
689
690 switch (f->fmt.pix.pixelformat) {
691 case V4L2_PIX_FMT_YUV420:
692 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300693 /* Frame stride must be multiple of 8, but 16 for h.264 */
694 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300695 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300696 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300697 break;
698 case V4L2_PIX_FMT_H264:
699 case V4L2_PIX_FMT_MPEG4:
700 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300701 f->fmt.pix.bytesperline = 0;
702 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300703 break;
704 default:
705 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300706 }
707
708 return 0;
709}
710
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300711static int coda_try_fmt_vid_cap(struct file *file, void *priv,
712 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300713{
Javier Martin186b2502012-07-26 05:53:35 -0300714 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300715 struct coda_codec *codec;
716 struct vb2_queue *src_vq;
717 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300718
Philipp Zabel918c66f2013-06-27 06:59:01 -0300719 /*
720 * If the source format is already fixed, try to find a codec that
721 * converts to the given destination format
722 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300723 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300724 if (vb2_is_streaming(src_vq)) {
725 struct coda_q_data *q_data_src;
726
727 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
728 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
729 f->fmt.pix.pixelformat);
730 if (!codec)
731 return -EINVAL;
732 } else {
733 /* Otherwise determine codec by encoded format, if possible */
734 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
735 f->fmt.pix.pixelformat);
736 }
Javier Martin186b2502012-07-26 05:53:35 -0300737
738 f->fmt.pix.colorspace = ctx->colorspace;
739
Philipp Zabel57625592013-09-30 10:34:51 -0300740 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300741 if (ret < 0)
742 return ret;
743
744 /* The h.264 decoder only returns complete 16x16 macroblocks */
745 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300746 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300747 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300748 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300749 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
750 f->fmt.pix.height * 3 / 2;
751 }
752
753 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300754}
755
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300756static int coda_try_fmt_vid_out(struct file *file, void *priv,
757 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300758{
759 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300760 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300761
Philipp Zabelb96904e2013-05-23 10:42:58 -0300762 /* Determine codec by encoded format, returns NULL if raw or invalid */
763 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
764 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300765
766 if (!f->fmt.pix.colorspace)
767 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
768
Philipp Zabel57625592013-09-30 10:34:51 -0300769 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300770}
771
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300772static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300773{
774 struct coda_q_data *q_data;
775 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300776
Philipp Zabel14604e32014-07-11 06:36:22 -0300777 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300778 if (!vq)
779 return -EINVAL;
780
781 q_data = get_q_data(ctx, f->type);
782 if (!q_data)
783 return -EINVAL;
784
785 if (vb2_is_busy(vq)) {
786 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
787 return -EBUSY;
788 }
789
Philipp Zabelb96904e2013-05-23 10:42:58 -0300790 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300791 q_data->width = f->fmt.pix.width;
792 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300793 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300794 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300795 q_data->rect.left = 0;
796 q_data->rect.top = 0;
797 q_data->rect.width = f->fmt.pix.width;
798 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300799
800 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
801 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300802 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300803
804 return 0;
805}
806
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300807static int coda_s_fmt_vid_cap(struct file *file, void *priv,
808 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300809{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300810 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300811 int ret;
812
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300813 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300814 if (ret)
815 return ret;
816
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300817 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300818}
819
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300820static int coda_s_fmt_vid_out(struct file *file, void *priv,
821 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300822{
823 struct coda_ctx *ctx = fh_to_ctx(priv);
824 int ret;
825
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300826 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300827 if (ret)
828 return ret;
829
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300830 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300831 if (ret)
832 ctx->colorspace = f->fmt.pix.colorspace;
833
834 return ret;
835}
836
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300837static int coda_qbuf(struct file *file, void *priv,
838 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300839{
840 struct coda_ctx *ctx = fh_to_ctx(priv);
841
Philipp Zabel14604e32014-07-11 06:36:22 -0300842 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300843}
844
Philipp Zabel918c66f2013-06-27 06:59:01 -0300845static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
846 struct v4l2_buffer *buf)
847{
848 struct vb2_queue *src_vq;
849
Philipp Zabel14604e32014-07-11 06:36:22 -0300850 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300851
852 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
853 (buf->sequence == (ctx->qsequence - 1)));
854}
855
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300856static int coda_dqbuf(struct file *file, void *priv,
857 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300858{
859 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300860 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300861
Philipp Zabel14604e32014-07-11 06:36:22 -0300862 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300863
864 /* If this is the last capture buffer, emit an end-of-stream event */
865 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
866 coda_buf_is_end_of_stream(ctx, buf)) {
867 const struct v4l2_event eos_event = {
868 .type = V4L2_EVENT_EOS
869 };
870
871 v4l2_event_queue_fh(&ctx->fh, &eos_event);
872 }
873
874 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300875}
876
Philipp Zabel52c41672014-07-11 06:36:19 -0300877static int coda_g_selection(struct file *file, void *fh,
878 struct v4l2_selection *s)
879{
880 struct coda_ctx *ctx = fh_to_ctx(fh);
881 struct coda_q_data *q_data;
882 struct v4l2_rect r, *rsel;
883
884 q_data = get_q_data(ctx, s->type);
885 if (!q_data)
886 return -EINVAL;
887
888 r.left = 0;
889 r.top = 0;
890 r.width = q_data->width;
891 r.height = q_data->height;
892 rsel = &q_data->rect;
893
894 switch (s->target) {
895 case V4L2_SEL_TGT_CROP_DEFAULT:
896 case V4L2_SEL_TGT_CROP_BOUNDS:
897 rsel = &r;
898 /* fallthrough */
899 case V4L2_SEL_TGT_CROP:
900 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
901 return -EINVAL;
902 break;
903 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
904 case V4L2_SEL_TGT_COMPOSE_PADDED:
905 rsel = &r;
906 /* fallthrough */
907 case V4L2_SEL_TGT_COMPOSE:
908 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
909 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
910 return -EINVAL;
911 break;
912 default:
913 return -EINVAL;
914 }
915
916 s->r = *rsel;
917
918 return 0;
919}
920
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300921static int coda_try_decoder_cmd(struct file *file, void *fh,
922 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300923{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300924 if (dc->cmd != V4L2_DEC_CMD_STOP)
925 return -EINVAL;
926
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300927 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300928 return -EINVAL;
929
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300930 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300931 return -EINVAL;
932
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300933 return 0;
934}
935
936static int coda_decoder_cmd(struct file *file, void *fh,
937 struct v4l2_decoder_cmd *dc)
938{
939 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300940 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300941 int ret;
942
943 ret = coda_try_decoder_cmd(file, fh, dc);
944 if (ret < 0)
945 return ret;
946
947 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300948 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300949 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300950
951 /* Set the strem-end flag on this context */
952 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
953
Philipp Zabel89548442014-07-11 06:36:17 -0300954 if ((dev->devtype->product == CODA_960) &&
955 coda_isbusy(dev) &&
956 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
957 /* If this context is currently running, update the hardware flag */
958 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
959 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300960 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300961 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300962
Philipp Zabel918c66f2013-06-27 06:59:01 -0300963 return 0;
964}
965
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300966static int coda_subscribe_event(struct v4l2_fh *fh,
967 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300968{
969 switch (sub->type) {
970 case V4L2_EVENT_EOS:
971 return v4l2_event_subscribe(fh, sub, 0, NULL);
972 default:
973 return v4l2_ctrl_subscribe_event(fh, sub);
974 }
Javier Martin186b2502012-07-26 05:53:35 -0300975}
976
977static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300978 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300979
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300980 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
981 .vidioc_g_fmt_vid_cap = coda_g_fmt,
982 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
983 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300984
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300985 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
986 .vidioc_g_fmt_vid_out = coda_g_fmt,
987 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
988 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300989
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300990 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
991 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300992
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300993 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300994 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300995 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300996 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300997
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300998 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
999 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001000
Philipp Zabel52c41672014-07-11 06:36:19 -03001001 .vidioc_g_selection = coda_g_selection,
1002
Philipp Zabel64ba40a2013-09-30 10:34:52 -03001003 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001004 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001005
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001006 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001007 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001008};
1009
Philipp Zabel918c66f2013-06-27 06:59:01 -03001010static int coda_start_decoding(struct coda_ctx *ctx);
1011
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001012static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
1013{
1014 return kfifo_len(&ctx->bitstream_fifo);
1015}
1016
1017static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
1018{
1019 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1020 struct coda_dev *dev = ctx->dev;
1021 u32 rd_ptr;
1022
1023 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1024 kfifo->out = (kfifo->in & ~kfifo->mask) |
1025 (rd_ptr - ctx->bitstream.paddr);
1026 if (kfifo->out > kfifo->in)
1027 kfifo->out -= kfifo->mask + 1;
1028}
1029
1030static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1031{
1032 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1033 struct coda_dev *dev = ctx->dev;
1034 u32 rd_ptr, wr_ptr;
1035
1036 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1037 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1038 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1039 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1040}
1041
1042static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1043{
1044 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1045 struct coda_dev *dev = ctx->dev;
1046 u32 wr_ptr;
1047
1048 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1049 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1050}
1051
1052static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1053{
1054 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1055 u32 n;
1056
1057 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1058 if (n < src_size)
1059 return -ENOSPC;
1060
1061 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1062 ctx->bitstream.size, DMA_TO_DEVICE);
1063
Philipp Zabel846ced92014-07-11 06:36:31 -03001064 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001065
1066 return 0;
1067}
1068
1069static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1070 struct vb2_buffer *src_buf)
1071{
1072 int ret;
1073
1074 if (coda_get_bitstream_payload(ctx) +
1075 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1076 return false;
1077
1078 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1079 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1080 return true;
1081 }
1082
1083 ret = coda_bitstream_queue(ctx, src_buf);
1084 if (ret < 0) {
1085 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1086 return false;
1087 }
1088 /* Sync read pointer to device */
1089 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1090 coda_kfifo_sync_to_device_write(ctx);
1091
Philipp Zabel84e23652014-07-11 06:36:34 -03001092 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001093
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001094 return true;
1095}
1096
1097static void coda_fill_bitstream(struct coda_ctx *ctx)
1098{
1099 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001100 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001101
Philipp Zabel14604e32014-07-11 06:36:22 -03001102 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1103 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001104
1105 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001106 /*
1107 * Source buffer is queued in the bitstream ringbuffer;
1108 * queue the timestamp and mark source buffer as done
1109 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001110 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001111
1112 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1113 if (ts) {
1114 ts->sequence = src_buf->v4l2_buf.sequence;
1115 ts->timecode = src_buf->v4l2_buf.timecode;
1116 ts->timestamp = src_buf->v4l2_buf.timestamp;
1117 list_add_tail(&ts->list, &ctx->timestamp_list);
1118 }
1119
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001120 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1121 } else {
1122 break;
1123 }
1124 }
1125}
1126
Philipp Zabel89548442014-07-11 06:36:17 -03001127static void coda_set_gdi_regs(struct coda_ctx *ctx)
1128{
1129 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1130 struct coda_dev *dev = ctx->dev;
1131 int i;
1132
1133 for (i = 0; i < 16; i++)
1134 coda_write(dev, tiled_map->xy2ca_map[i],
1135 CODA9_GDI_XY2_CAS_0 + 4 * i);
1136 for (i = 0; i < 4; i++)
1137 coda_write(dev, tiled_map->xy2ba_map[i],
1138 CODA9_GDI_XY2_BA_0 + 4 * i);
1139 for (i = 0; i < 16; i++)
1140 coda_write(dev, tiled_map->xy2ra_map[i],
1141 CODA9_GDI_XY2_RAS_0 + 4 * i);
1142 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1143 for (i = 0; i < 32; i++)
1144 coda_write(dev, tiled_map->rbc2axi_map[i],
1145 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1146}
1147
Javier Martin186b2502012-07-26 05:53:35 -03001148/*
1149 * Mem-to-mem operations.
1150 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001151static int coda_prepare_decode(struct coda_ctx *ctx)
1152{
1153 struct vb2_buffer *dst_buf;
1154 struct coda_dev *dev = ctx->dev;
1155 struct coda_q_data *q_data_dst;
1156 u32 stridey, height;
1157 u32 picture_y, picture_cb, picture_cr;
1158
Philipp Zabel14604e32014-07-11 06:36:22 -03001159 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001160 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1161
1162 if (ctx->params.rot_mode & CODA_ROT_90) {
1163 stridey = q_data_dst->height;
1164 height = q_data_dst->width;
1165 } else {
1166 stridey = q_data_dst->width;
1167 height = q_data_dst->height;
1168 }
1169
1170 /* Try to copy source buffer contents into the bitstream ringbuffer */
1171 mutex_lock(&ctx->bitstream_mutex);
1172 coda_fill_bitstream(ctx);
1173 mutex_unlock(&ctx->bitstream_mutex);
1174
1175 if (coda_get_bitstream_payload(ctx) < 512 &&
1176 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1177 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1178 "bitstream payload: %d, skipping\n",
1179 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001180 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001181 return -EAGAIN;
1182 }
1183
1184 /* Run coda_start_decoding (again) if not yet initialized */
1185 if (!ctx->initialized) {
1186 int ret = coda_start_decoding(ctx);
1187 if (ret < 0) {
1188 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001189 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001190 return -EAGAIN;
1191 } else {
1192 ctx->initialized = 1;
1193 }
1194 }
1195
Philipp Zabel89548442014-07-11 06:36:17 -03001196 if (dev->devtype->product == CODA_960)
1197 coda_set_gdi_regs(ctx);
1198
Philipp Zabel918c66f2013-06-27 06:59:01 -03001199 /* Set rotator output */
1200 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1201 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1202 /* Switch Cr and Cb for YVU420 format */
1203 picture_cr = picture_y + stridey * height;
1204 picture_cb = picture_cr + stridey / 2 * height / 2;
1205 } else {
1206 picture_cb = picture_y + stridey * height;
1207 picture_cr = picture_cb + stridey / 2 * height / 2;
1208 }
Philipp Zabel89548442014-07-11 06:36:17 -03001209
1210 if (dev->devtype->product == CODA_960) {
1211 /*
1212 * The CODA960 seems to have an internal list of buffers with
1213 * 64 entries that includes the registered frame buffers as
1214 * well as the rotator buffer output.
1215 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1216 */
1217 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1218 CODA9_CMD_DEC_PIC_ROT_INDEX);
1219 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1220 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1221 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1222 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1223 } else {
1224 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1225 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1226 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1227 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1228 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001229 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1230 CODA_CMD_DEC_PIC_ROT_MODE);
1231
1232 switch (dev->devtype->product) {
1233 case CODA_DX6:
1234 /* TBD */
1235 case CODA_7541:
1236 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1237 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001238 case CODA_960:
1239 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1240 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001241 }
1242
1243 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1244
1245 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1246 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1247
1248 return 0;
1249}
1250
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001251static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001252{
Javier Martin186b2502012-07-26 05:53:35 -03001253 struct coda_q_data *q_data_src, *q_data_dst;
1254 struct vb2_buffer *src_buf, *dst_buf;
1255 struct coda_dev *dev = ctx->dev;
1256 int force_ipicture;
1257 int quant_param = 0;
1258 u32 picture_y, picture_cb, picture_cr;
1259 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1260 u32 dst_fourcc;
1261
Philipp Zabel14604e32014-07-11 06:36:22 -03001262 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1263 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001264 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1265 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001266 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001267
Philipp Zabel918c66f2013-06-27 06:59:01 -03001268 src_buf->v4l2_buf.sequence = ctx->osequence;
1269 dst_buf->v4l2_buf.sequence = ctx->osequence;
1270 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001271
1272 /*
1273 * Workaround coda firmware BUG that only marks the first
1274 * frame as IDR. This is a problem for some decoders that can't
1275 * recover when a frame is lost.
1276 */
1277 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1278 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1279 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1280 } else {
1281 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1282 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1283 }
1284
Philipp Zabel89548442014-07-11 06:36:17 -03001285 if (dev->devtype->product == CODA_960)
1286 coda_set_gdi_regs(ctx);
1287
Javier Martin186b2502012-07-26 05:53:35 -03001288 /*
1289 * Copy headers at the beginning of the first frame for H.264 only.
1290 * In MPEG4 they are already copied by the coda.
1291 */
1292 if (src_buf->v4l2_buf.sequence == 0) {
1293 pic_stream_buffer_addr =
1294 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1295 ctx->vpu_header_size[0] +
1296 ctx->vpu_header_size[1] +
1297 ctx->vpu_header_size[2];
1298 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1299 ctx->vpu_header_size[0] -
1300 ctx->vpu_header_size[1] -
1301 ctx->vpu_header_size[2];
1302 memcpy(vb2_plane_vaddr(dst_buf, 0),
1303 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1304 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1305 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1306 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1307 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1308 ctx->vpu_header_size[2]);
1309 } else {
1310 pic_stream_buffer_addr =
1311 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1312 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1313 }
1314
1315 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1316 force_ipicture = 1;
1317 switch (dst_fourcc) {
1318 case V4L2_PIX_FMT_H264:
1319 quant_param = ctx->params.h264_intra_qp;
1320 break;
1321 case V4L2_PIX_FMT_MPEG4:
1322 quant_param = ctx->params.mpeg4_intra_qp;
1323 break;
1324 default:
1325 v4l2_warn(&ctx->dev->v4l2_dev,
1326 "cannot set intra qp, fmt not supported\n");
1327 break;
1328 }
1329 } else {
1330 force_ipicture = 0;
1331 switch (dst_fourcc) {
1332 case V4L2_PIX_FMT_H264:
1333 quant_param = ctx->params.h264_inter_qp;
1334 break;
1335 case V4L2_PIX_FMT_MPEG4:
1336 quant_param = ctx->params.mpeg4_inter_qp;
1337 break;
1338 default:
1339 v4l2_warn(&ctx->dev->v4l2_dev,
1340 "cannot set inter qp, fmt not supported\n");
1341 break;
1342 }
1343 }
1344
1345 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001346 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001347 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1348
1349
1350 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001351 switch (q_data_src->fourcc) {
1352 case V4L2_PIX_FMT_YVU420:
1353 /* Switch Cb and Cr for YVU420 format */
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001354 picture_cr = picture_y + q_data_src->bytesperline *
1355 q_data_src->height;
1356 picture_cb = picture_cr + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001357 q_data_src->height / 2;
1358 break;
1359 case V4L2_PIX_FMT_YUV420:
1360 default:
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001361 picture_cb = picture_y + q_data_src->bytesperline *
1362 q_data_src->height;
1363 picture_cr = picture_cb + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001364 q_data_src->height / 2;
1365 break;
1366 }
Javier Martin186b2502012-07-26 05:53:35 -03001367
Philipp Zabel89548442014-07-11 06:36:17 -03001368 if (dev->devtype->product == CODA_960) {
1369 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1370 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1371 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1372
1373 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1374 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1375 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1376 } else {
1377 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1378 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1379 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1380 }
Javier Martin186b2502012-07-26 05:53:35 -03001381 coda_write(dev, force_ipicture << 1 & 0x2,
1382 CODA_CMD_ENC_PIC_OPTION);
1383
1384 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1385 coda_write(dev, pic_stream_buffer_size / 1024,
1386 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001387
1388 if (!ctx->streamon_out) {
1389 /* After streamoff on the output side, set the stream end flag */
1390 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1391 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1392 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001393}
1394
1395static void coda_device_run(void *m2m_priv)
1396{
1397 struct coda_ctx *ctx = m2m_priv;
1398 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001399
1400 queue_work(dev->workqueue, &ctx->pic_run_work);
1401}
1402
1403static void coda_free_framebuffers(struct coda_ctx *ctx);
1404static void coda_free_context_buffers(struct coda_ctx *ctx);
1405
1406static void coda_seq_end_work(struct work_struct *work)
1407{
1408 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1409 struct coda_dev *dev = ctx->dev;
1410
1411 mutex_lock(&ctx->buffer_mutex);
1412 mutex_lock(&dev->coda_mutex);
1413
1414 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1415 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1416 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1417 v4l2_err(&dev->v4l2_dev,
1418 "CODA_COMMAND_SEQ_END failed\n");
1419 }
1420
1421 kfifo_init(&ctx->bitstream_fifo,
1422 ctx->bitstream.vaddr, ctx->bitstream.size);
1423
1424 coda_free_framebuffers(ctx);
1425 coda_free_context_buffers(ctx);
1426
1427 mutex_unlock(&dev->coda_mutex);
1428 mutex_unlock(&ctx->buffer_mutex);
1429}
1430
1431static void coda_finish_decode(struct coda_ctx *ctx);
1432static void coda_finish_encode(struct coda_ctx *ctx);
1433
1434static void coda_pic_run_work(struct work_struct *work)
1435{
1436 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1437 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001438 int ret;
1439
1440 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001441 mutex_lock(&dev->coda_mutex);
1442
Philipp Zabel918c66f2013-06-27 06:59:01 -03001443 if (ctx->inst_type == CODA_INST_DECODER) {
1444 ret = coda_prepare_decode(ctx);
1445 if (ret < 0) {
1446 mutex_unlock(&dev->coda_mutex);
1447 mutex_unlock(&ctx->buffer_mutex);
1448 /* job_finish scheduled by prepare_decode */
1449 return;
1450 }
1451 } else {
1452 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001453 }
1454
Philipp Zabelc2d22512013-06-21 03:55:28 -03001455 if (dev->devtype->product != CODA_DX6)
1456 coda_write(dev, ctx->iram_info.axi_sram_use,
1457 CODA7_REG_BIT_AXI_SRAM_USE);
1458
Philipp Zabel918c66f2013-06-27 06:59:01 -03001459 if (ctx->inst_type == CODA_INST_DECODER)
1460 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001461 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001462
1463 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1464 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001465
1466 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001467
1468 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001469 } else if (!ctx->aborting) {
1470 if (ctx->inst_type == CODA_INST_DECODER)
1471 coda_finish_decode(ctx);
1472 else
1473 coda_finish_encode(ctx);
1474 }
1475
1476 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1477 queue_work(dev->workqueue, &ctx->seq_end_work);
1478
1479 mutex_unlock(&dev->coda_mutex);
1480 mutex_unlock(&ctx->buffer_mutex);
1481
Philipp Zabel14604e32014-07-11 06:36:22 -03001482 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001483}
1484
1485static int coda_job_ready(void *m2m_priv)
1486{
1487 struct coda_ctx *ctx = m2m_priv;
1488
1489 /*
1490 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001491 * and 1 frame are needed. In the decoder case,
1492 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001493 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001494 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001495 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001496 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1497 "not ready: not enough video buffers.\n");
1498 return 0;
1499 }
1500
Philipp Zabel14604e32014-07-11 06:36:22 -03001501 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001502 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1503 "not ready: not enough video capture buffers.\n");
1504 return 0;
1505 }
1506
Philipp Zabel84e23652014-07-11 06:36:34 -03001507 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001508 ((ctx->inst_type == CODA_INST_DECODER) &&
1509 (coda_get_bitstream_payload(ctx) < 512) &&
1510 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1511 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1512 "%d: not ready: not enough bitstream data.\n",
1513 ctx->idx);
1514 return 0;
1515 }
1516
Philipp Zabel3e7482682013-05-23 10:43:01 -03001517 if (ctx->aborting) {
1518 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1519 "not ready: aborting\n");
1520 return 0;
1521 }
1522
Javier Martin186b2502012-07-26 05:53:35 -03001523 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1524 "job ready\n");
1525 return 1;
1526}
1527
1528static void coda_job_abort(void *priv)
1529{
1530 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001531
1532 ctx->aborting = 1;
1533
1534 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1535 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001536}
1537
1538static void coda_lock(void *m2m_priv)
1539{
1540 struct coda_ctx *ctx = m2m_priv;
1541 struct coda_dev *pcdev = ctx->dev;
1542 mutex_lock(&pcdev->dev_mutex);
1543}
1544
1545static void coda_unlock(void *m2m_priv)
1546{
1547 struct coda_ctx *ctx = m2m_priv;
1548 struct coda_dev *pcdev = ctx->dev;
1549 mutex_unlock(&pcdev->dev_mutex);
1550}
1551
1552static struct v4l2_m2m_ops coda_m2m_ops = {
1553 .device_run = coda_device_run,
1554 .job_ready = coda_job_ready,
1555 .job_abort = coda_job_abort,
1556 .lock = coda_lock,
1557 .unlock = coda_unlock,
1558};
1559
Philipp Zabel89548442014-07-11 06:36:17 -03001560static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1561{
1562 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1563 int luma_map, chro_map, i;
1564
1565 memset(tiled_map, 0, sizeof(*tiled_map));
1566
1567 luma_map = 64;
1568 chro_map = 64;
1569 tiled_map->map_type = tiled_map_type;
1570 for (i = 0; i < 16; i++)
1571 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1572 for (i = 0; i < 4; i++)
1573 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1574 for (i = 0; i < 16; i++)
1575 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1576
1577 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1578 tiled_map->xy2rbc_config = 0;
1579 } else {
1580 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1581 tiled_map_type);
1582 return;
1583 }
1584}
1585
Javier Martin186b2502012-07-26 05:53:35 -03001586static void set_default_params(struct coda_ctx *ctx)
1587{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001588 int max_w;
1589 int max_h;
1590
1591 ctx->codec = &ctx->dev->devtype->codecs[0];
1592 max_w = ctx->codec->max_w;
1593 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001594
1595 ctx->params.codec_mode = CODA_MODE_INVALID;
1596 ctx->colorspace = V4L2_COLORSPACE_REC709;
1597 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001598 ctx->aborting = 0;
1599
1600 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001601 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1602 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1603 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1604 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001605 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001606 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1607 ctx->q_data[V4L2_M2M_DST].width = max_w;
1608 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001609 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001610 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001611 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1612 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1613 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1614 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001615
1616 if (ctx->dev->devtype->product == CODA_960)
1617 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001618}
1619
1620/*
1621 * Queue operations
1622 */
1623static int coda_queue_setup(struct vb2_queue *vq,
1624 const struct v4l2_format *fmt,
1625 unsigned int *nbuffers, unsigned int *nplanes,
1626 unsigned int sizes[], void *alloc_ctxs[])
1627{
1628 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001629 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001630 unsigned int size;
1631
Philipp Zabele34db062012-08-29 08:22:00 -03001632 q_data = get_q_data(ctx, vq->type);
1633 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001634
1635 *nplanes = 1;
1636 sizes[0] = size;
1637
1638 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1639
1640 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1641 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1642
1643 return 0;
1644}
1645
1646static int coda_buf_prepare(struct vb2_buffer *vb)
1647{
1648 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1649 struct coda_q_data *q_data;
1650
1651 q_data = get_q_data(ctx, vb->vb2_queue->type);
1652
1653 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1654 v4l2_warn(&ctx->dev->v4l2_dev,
1655 "%s data will not fit into plane (%lu < %lu)\n",
1656 __func__, vb2_plane_size(vb, 0),
1657 (long)q_data->sizeimage);
1658 return -EINVAL;
1659 }
1660
Javier Martin186b2502012-07-26 05:53:35 -03001661 return 0;
1662}
1663
1664static void coda_buf_queue(struct vb2_buffer *vb)
1665{
1666 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001667 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001668 struct coda_q_data *q_data;
1669
1670 q_data = get_q_data(ctx, vb->vb2_queue->type);
1671
1672 /*
1673 * In the decoder case, immediately try to copy the buffer into the
1674 * bitstream ringbuffer and mark it as ready to be dequeued.
1675 */
1676 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1677 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1678 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001679 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001680 * the stream end
1681 */
Philipp Zabel89548442014-07-11 06:36:17 -03001682 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001683 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001684 if ((dev->devtype->product == CODA_960) &&
1685 coda_isbusy(dev) &&
1686 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1687 /* if this decoder instance is running, set the stream end flag */
1688 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1689 }
1690 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001691 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001692 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001693 coda_fill_bitstream(ctx);
1694 mutex_unlock(&ctx->bitstream_mutex);
1695 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001696 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001697 }
Javier Martin186b2502012-07-26 05:53:35 -03001698}
1699
Philipp Zabel86eda902013-05-23 10:42:57 -03001700static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1701{
1702 struct coda_dev *dev = ctx->dev;
1703 u32 *p = ctx->parabuf.vaddr;
1704
1705 if (dev->devtype->product == CODA_DX6)
1706 p[index] = value;
1707 else
1708 p[index ^ 1] = value;
1709}
1710
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001711static int coda_alloc_aux_buf(struct coda_dev *dev,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001712 struct coda_aux_buf *buf, size_t size,
1713 const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001714{
1715 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1716 GFP_KERNEL);
1717 if (!buf->vaddr)
1718 return -ENOMEM;
1719
1720 buf->size = size;
1721
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001722 if (name && parent) {
1723 buf->blob.data = buf->vaddr;
1724 buf->blob.size = size;
1725 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
1726 if (!buf->dentry)
1727 dev_warn(&dev->plat_dev->dev,
1728 "failed to create debugfs entry %s\n", name);
1729 }
1730
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001731 return 0;
1732}
1733
1734static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001735 struct coda_aux_buf *buf, size_t size,
1736 const char *name)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001737{
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001738 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001739}
1740
1741static void coda_free_aux_buf(struct coda_dev *dev,
1742 struct coda_aux_buf *buf)
1743{
1744 if (buf->vaddr) {
1745 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1746 buf->vaddr, buf->paddr);
1747 buf->vaddr = NULL;
1748 buf->size = 0;
1749 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001750 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001751}
1752
1753static void coda_free_framebuffers(struct coda_ctx *ctx)
1754{
1755 int i;
1756
1757 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1758 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1759}
1760
Philipp Zabelec25f682012-07-20 08:54:29 -03001761static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1762{
1763 struct coda_dev *dev = ctx->dev;
Philipp Zabel7c3df782014-07-11 06:36:38 -03001764 int width, height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001765 dma_addr_t paddr;
1766 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001767 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001768 int i;
1769
Philipp Zabel7c3df782014-07-11 06:36:38 -03001770 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
1771 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
1772 width = round_up(q_data->width, 16);
1773 height = round_up(q_data->height, 16);
1774 } else {
1775 width = round_up(q_data->width, 8);
1776 height = q_data->height;
1777 }
1778 ysize = width * height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001779
Philipp Zabelec25f682012-07-20 08:54:29 -03001780 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001781 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001782 size_t size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001783 char *name;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001784
Philipp Zabelaed14b02014-07-11 06:36:15 -03001785 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001786 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1787 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001788 size += ysize / 4;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001789 name = kasprintf(GFP_KERNEL, "fb%d", i);
1790 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
1791 size, name);
1792 kfree(name);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001793 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001794 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001795 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001796 }
1797 }
1798
1799 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001800 for (i = 0; i < ctx->num_internal_frames; i++) {
1801 paddr = ctx->internal_frames[i].paddr;
1802 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1803 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1804 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001805
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001806 /* mvcol buffer for h.264 */
1807 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1808 dev->devtype->product != CODA_DX6)
1809 coda_parabuf_write(ctx, 96 + i,
1810 ctx->internal_frames[i].paddr +
1811 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001812 }
1813
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001814 /* mvcol buffer for mpeg4 */
1815 if ((dev->devtype->product != CODA_DX6) &&
1816 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1817 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1818 ysize + ysize/4 + ysize/4);
1819
Philipp Zabelec25f682012-07-20 08:54:29 -03001820 return 0;
1821}
1822
Javier Martin3f3f5c72012-10-29 05:20:29 -03001823static int coda_h264_padding(int size, char *p)
1824{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001825 int nal_size;
1826 int diff;
1827
Javier Martin832fbb52012-10-29 08:34:59 -03001828 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001829 if (diff == 0)
1830 return 0;
1831
Javier Martin832fbb52012-10-29 08:34:59 -03001832 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001833 memcpy(p, coda_filler_nal, nal_size);
1834
1835 /* Add rbsp stop bit and trailing at the end */
1836 *(p + nal_size - 1) = 0x80;
1837
1838 return nal_size;
1839}
1840
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001841static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1842{
1843 phys_addr_t ret;
1844
1845 size = round_up(size, 1024);
1846 if (size > iram->remaining)
1847 return 0;
1848 iram->remaining -= size;
1849
1850 ret = iram->next_paddr;
1851 iram->next_paddr += size;
1852
1853 return ret;
1854}
1855
Philipp Zabelc2d22512013-06-21 03:55:28 -03001856static void coda_setup_iram(struct coda_ctx *ctx)
1857{
1858 struct coda_iram_info *iram_info = &ctx->iram_info;
1859 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001860 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001861 int dbk_bits;
1862 int bit_bits;
1863 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001864
1865 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001866 iram_info->next_paddr = dev->iram.paddr;
1867 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001868
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001869 switch (dev->devtype->product) {
1870 case CODA_7541:
1871 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1872 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1873 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1874 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001875 case CODA_960:
1876 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1877 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1878 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1879 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001880 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001881 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001882 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001883
1884 if (ctx->inst_type == CODA_INST_ENCODER) {
1885 struct coda_q_data *q_data_src;
1886
1887 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1888 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1889
1890 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001891 if (dev->devtype->product == CODA_7541) {
1892 iram_info->search_ram_size = round_up(mb_width * 16 *
1893 36 + 2048, 1024);
1894 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1895 iram_info->search_ram_size);
1896 if (!iram_info->search_ram_paddr) {
1897 pr_err("IRAM is smaller than the search ram size\n");
1898 goto out;
1899 }
1900 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1901 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001902 }
1903
1904 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001905 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1906 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1907 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001908 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001909 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001910
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001911 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1912 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001913 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001914 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001915
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001916 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1917 if (!iram_info->buf_ip_ac_dc_use)
1918 goto out;
1919 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001920
Philipp Zabel8358e762013-06-21 03:55:32 -03001921 /* OVL and BTP disabled for encoder */
1922 } else if (ctx->inst_type == CODA_INST_DECODER) {
1923 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001924
1925 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1926 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001927
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001928 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1929 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1930 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001931 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001932 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001933
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001934 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1935 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001936 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001937 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001938
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001939 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1940 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001941 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001942 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001943
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001944 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001945 }
1946
1947out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001948 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1949 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1950 "IRAM smaller than needed\n");
1951
1952 if (dev->devtype->product == CODA_7541) {
1953 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001954 if (ctx->inst_type == CODA_INST_DECODER) {
1955 /* fw 1.4.50 */
1956 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1957 CODA7_USE_IP_ENABLE);
1958 } else {
1959 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001960 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1961 CODA7_USE_HOST_DBK_ENABLE |
1962 CODA7_USE_IP_ENABLE |
1963 CODA7_USE_DBK_ENABLE);
1964 }
1965 }
1966}
1967
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001968static void coda_free_context_buffers(struct coda_ctx *ctx)
1969{
1970 struct coda_dev *dev = ctx->dev;
1971
Philipp Zabel918c66f2013-06-27 06:59:01 -03001972 coda_free_aux_buf(dev, &ctx->slicebuf);
1973 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001974 if (dev->devtype->product != CODA_DX6)
1975 coda_free_aux_buf(dev, &ctx->workbuf);
1976}
1977
1978static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1979 struct coda_q_data *q_data)
1980{
1981 struct coda_dev *dev = ctx->dev;
1982 size_t size;
1983 int ret;
1984
1985 switch (dev->devtype->product) {
1986 case CODA_7541:
1987 size = CODA7_WORK_BUF_SIZE;
1988 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001989 case CODA_960:
1990 size = CODA9_WORK_BUF_SIZE;
1991 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1992 size += CODA9_PS_SAVE_SIZE;
1993 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001994 default:
1995 return 0;
1996 }
1997
Philipp Zabel918c66f2013-06-27 06:59:01 -03001998 if (ctx->psbuf.vaddr) {
1999 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
2000 return -EBUSY;
2001 }
2002 if (ctx->slicebuf.vaddr) {
2003 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
2004 return -EBUSY;
2005 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002006 if (ctx->workbuf.vaddr) {
2007 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
2008 ret = -EBUSY;
2009 return -ENOMEM;
2010 }
2011
Philipp Zabel918c66f2013-06-27 06:59:01 -03002012 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
2013 /* worst case slice size */
2014 size = (DIV_ROUND_UP(q_data->width, 16) *
2015 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002016 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, "slicebuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002017 if (ret < 0) {
2018 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
2019 ctx->slicebuf.size);
2020 return ret;
2021 }
2022 }
2023
2024 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002025 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002026 if (ret < 0) {
2027 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
2028 goto err;
2029 }
2030 }
2031
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002032 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002033 if (ret < 0) {
2034 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2035 ctx->workbuf.size);
2036 goto err;
2037 }
2038
2039 return 0;
2040
2041err:
2042 coda_free_context_buffers(ctx);
2043 return ret;
2044}
2045
Philipp Zabel918c66f2013-06-27 06:59:01 -03002046static int coda_start_decoding(struct coda_ctx *ctx)
2047{
2048 struct coda_q_data *q_data_src, *q_data_dst;
2049 u32 bitstream_buf, bitstream_size;
2050 struct coda_dev *dev = ctx->dev;
2051 int width, height;
2052 u32 src_fourcc;
2053 u32 val;
2054 int ret;
2055
2056 /* Start decoding */
2057 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2058 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2059 bitstream_buf = ctx->bitstream.paddr;
2060 bitstream_size = ctx->bitstream.size;
2061 src_fourcc = q_data_src->fourcc;
2062
2063 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2064
2065 /* Update coda bitstream read and write pointers from kfifo */
2066 coda_kfifo_sync_to_device_full(ctx);
2067
2068 ctx->display_idx = -1;
2069 ctx->frm_dis_flg = 0;
2070 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2071
2072 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2073 CODA_REG_BIT_BIT_STREAM_PARAM);
2074
2075 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2076 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2077 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002078 if ((dev->devtype->product == CODA_7541) ||
2079 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002080 val |= CODA_REORDER_ENABLE;
2081 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2082
2083 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002084 if (dev->devtype->product == CODA_960 &&
2085 src_fourcc == V4L2_PIX_FMT_MPEG4)
2086 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2087 else
2088 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002089 if (src_fourcc == V4L2_PIX_FMT_H264) {
2090 if (dev->devtype->product == CODA_7541) {
2091 coda_write(dev, ctx->psbuf.paddr,
2092 CODA_CMD_DEC_SEQ_PS_BB_START);
2093 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2094 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2095 }
Philipp Zabel89548442014-07-11 06:36:17 -03002096 if (dev->devtype->product == CODA_960) {
2097 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2098 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2099 }
2100 }
2101 if (dev->devtype->product != CODA_960) {
2102 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002103 }
2104
2105 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2106 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2107 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2108 return -ETIMEDOUT;
2109 }
2110
2111 /* Update kfifo out pointer from coda bitstream read pointer */
2112 coda_kfifo_sync_from_device(ctx);
2113
2114 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2115
2116 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2117 v4l2_err(&dev->v4l2_dev,
2118 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2119 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2120 return -EAGAIN;
2121 }
2122
2123 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2124 if (dev->devtype->product == CODA_DX6) {
2125 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2126 height = val & CODADX6_PICHEIGHT_MASK;
2127 } else {
2128 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2129 height = val & CODA7_PICHEIGHT_MASK;
2130 }
2131
2132 if (width > q_data_dst->width || height > q_data_dst->height) {
2133 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2134 width, height, q_data_dst->width, q_data_dst->height);
2135 return -EINVAL;
2136 }
2137
2138 width = round_up(width, 16);
2139 height = round_up(height, 16);
2140
2141 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2142 __func__, ctx->idx, width, height);
2143
Philipp Zabela500a932014-07-11 06:36:13 -03002144 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002145 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2146 v4l2_err(&dev->v4l2_dev,
2147 "not enough framebuffers to decode (%d < %d)\n",
2148 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2149 return -EINVAL;
2150 }
2151
Philipp Zabel52c41672014-07-11 06:36:19 -03002152 if (src_fourcc == V4L2_PIX_FMT_H264) {
2153 u32 left_right;
2154 u32 top_bottom;
2155
2156 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2157 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2158
2159 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2160 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2161 q_data_dst->rect.width = width - q_data_dst->rect.left -
2162 (left_right & 0x3ff);
2163 q_data_dst->rect.height = height - q_data_dst->rect.top -
2164 (top_bottom & 0x3ff);
2165 }
2166
Philipp Zabel918c66f2013-06-27 06:59:01 -03002167 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2168 if (ret < 0)
2169 return ret;
2170
2171 /* Tell the decoder how many frame buffers we allocated. */
2172 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2173 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2174
2175 if (dev->devtype->product != CODA_DX6) {
2176 /* Set secondary AXI IRAM */
2177 coda_setup_iram(ctx);
2178
2179 coda_write(dev, ctx->iram_info.buf_bit_use,
2180 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2181 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2182 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2183 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2184 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2185 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2186 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2187 coda_write(dev, ctx->iram_info.buf_ovl_use,
2188 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002189 if (dev->devtype->product == CODA_960)
2190 coda_write(dev, ctx->iram_info.buf_btp_use,
2191 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2192 }
2193
2194 if (dev->devtype->product == CODA_960) {
2195 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2196
2197 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2198 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2199 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2200 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2201 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2202 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002203 }
2204
2205 if (src_fourcc == V4L2_PIX_FMT_H264) {
2206 coda_write(dev, ctx->slicebuf.paddr,
2207 CODA_CMD_SET_FRAME_SLICE_BB_START);
2208 coda_write(dev, ctx->slicebuf.size / 1024,
2209 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2210 }
2211
2212 if (dev->devtype->product == CODA_7541) {
2213 int max_mb_x = 1920 / 16;
2214 int max_mb_y = 1088 / 16;
2215 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002216
Philipp Zabel918c66f2013-06-27 06:59:01 -03002217 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2218 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002219 } else if (dev->devtype->product == CODA_960) {
2220 int max_mb_x = 1920 / 16;
2221 int max_mb_y = 1088 / 16;
2222 int max_mb_num = max_mb_x * max_mb_y;
2223
2224 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2225 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002226 }
2227
2228 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2229 v4l2_err(&ctx->dev->v4l2_dev,
2230 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2231 return -ETIMEDOUT;
2232 }
2233
2234 return 0;
2235}
2236
Philipp Zabeld35167a2013-05-23 10:42:59 -03002237static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2238 int header_code, u8 *header, int *size)
2239{
2240 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002241 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002242 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002243 int i;
2244
2245 if (dev->devtype->product == CODA_960)
2246 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002247
2248 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2249 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002250 bufsize = vb2_plane_size(buf, 0);
2251 if (dev->devtype->product == CODA_960)
2252 bufsize /= 1024;
2253 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002254 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2255 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2256 if (ret < 0) {
2257 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2258 return ret;
2259 }
Philipp Zabel89548442014-07-11 06:36:17 -03002260
2261 if (dev->devtype->product == CODA_960) {
2262 for (i = 63; i > 0; i--)
2263 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2264 break;
2265 *size = i + 1;
2266 } else {
2267 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2268 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2269 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002270 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2271
2272 return 0;
2273}
2274
Philipp Zabel89548442014-07-11 06:36:17 -03002275static int coda_start_encoding(struct coda_ctx *ctx);
2276
Javier Martin186b2502012-07-26 05:53:35 -03002277static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2278{
2279 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2280 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002281 struct coda_dev *dev = ctx->dev;
2282 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002283 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002284 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002285
Philipp Zabelb96904e2013-05-23 10:42:58 -03002286 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002287 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2288 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2289 if (coda_get_bitstream_payload(ctx) < 512)
2290 return -EINVAL;
2291 } else {
2292 if (count < 1)
2293 return -EINVAL;
2294 }
2295
2296 ctx->streamon_out = 1;
2297
Philipp Zabelb96904e2013-05-23 10:42:58 -03002298 if (coda_format_is_yuv(q_data_src->fourcc))
2299 ctx->inst_type = CODA_INST_ENCODER;
2300 else
2301 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002302 } else {
2303 if (count < 1)
2304 return -EINVAL;
2305
2306 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002307 }
Javier Martin186b2502012-07-26 05:53:35 -03002308
Philipp Zabelb96904e2013-05-23 10:42:58 -03002309 /* Don't start the coda unless both queues are on */
2310 if (!(ctx->streamon_out & ctx->streamon_cap))
2311 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002312
Philipp Zabeleb107512013-09-30 10:34:45 -03002313 /* Allow decoder device_run with no new buffers queued */
2314 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002315 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002316
Philipp Zabelb96904e2013-05-23 10:42:58 -03002317 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002318 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002319 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002320
Philipp Zabelb96904e2013-05-23 10:42:58 -03002321 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2322 q_data_dst->fourcc);
2323 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002324 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2325 return -EINVAL;
2326 }
2327
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002328 /* Allocate per-instance buffers */
2329 ret = coda_alloc_context_buffers(ctx, q_data_src);
2330 if (ret < 0)
2331 return ret;
2332
Philipp Zabel918c66f2013-06-27 06:59:01 -03002333 if (ctx->inst_type == CODA_INST_DECODER) {
2334 mutex_lock(&dev->coda_mutex);
2335 ret = coda_start_decoding(ctx);
2336 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002337 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002338 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002339 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002340 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002341 } else {
2342 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002343 }
2344
Philipp Zabel89548442014-07-11 06:36:17 -03002345 ctx->initialized = 1;
2346 return ret;
2347}
2348
2349static int coda_start_encoding(struct coda_ctx *ctx)
2350{
2351 struct coda_dev *dev = ctx->dev;
2352 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2353 struct coda_q_data *q_data_src, *q_data_dst;
2354 u32 bitstream_buf, bitstream_size;
2355 struct vb2_buffer *buf;
2356 int gamma, ret, value;
2357 u32 dst_fourcc;
2358
2359 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2360 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2361 dst_fourcc = q_data_dst->fourcc;
2362
Philipp Zabel14604e32014-07-11 06:36:22 -03002363 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002364 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2365 bitstream_size = q_data_dst->sizeimage;
2366
Javier Martin186b2502012-07-26 05:53:35 -03002367 if (!coda_is_initialized(dev)) {
2368 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2369 return -EFAULT;
2370 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002371
2372 mutex_lock(&dev->coda_mutex);
2373
Javier Martin186b2502012-07-26 05:53:35 -03002374 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002375 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2376 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002377 switch (dev->devtype->product) {
2378 case CODA_DX6:
2379 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2380 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2381 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002382 case CODA_960:
2383 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2384 /* fallthrough */
2385 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002386 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2387 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002388 break;
Javier Martin186b2502012-07-26 05:53:35 -03002389 }
2390
Philipp Zabel89548442014-07-11 06:36:17 -03002391 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2392 value &= ~(1 << 2 | 0x7 << 9);
2393 ctx->frame_mem_ctrl = value;
2394 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2395
Philipp Zabel10436672012-07-02 09:03:55 -03002396 if (dev->devtype->product == CODA_DX6) {
2397 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002398 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002399 }
Javier Martin186b2502012-07-26 05:53:35 -03002400
2401 /* Could set rotation here if needed */
2402 switch (dev->devtype->product) {
2403 case CODA_DX6:
2404 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002405 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002406 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002407 case CODA_7541:
2408 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2409 value = (round_up(q_data_src->width, 16) &
2410 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2411 value |= (round_up(q_data_src->height, 16) &
2412 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2413 break;
2414 }
2415 /* fallthrough */
2416 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002417 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002418 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002419 }
Javier Martin186b2502012-07-26 05:53:35 -03002420 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2421 coda_write(dev, ctx->params.framerate,
2422 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2423
Philipp Zabelb96904e2013-05-23 10:42:58 -03002424 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002425 switch (dst_fourcc) {
2426 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002427 if (dev->devtype->product == CODA_960)
2428 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2429 else
2430 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002431 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2432 break;
2433 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002434 if (dev->devtype->product == CODA_960)
2435 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2436 else
2437 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002438 if (ctx->params.h264_deblk_enabled) {
2439 value = ((ctx->params.h264_deblk_alpha &
2440 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2441 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2442 ((ctx->params.h264_deblk_beta &
2443 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2444 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2445 } else {
2446 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2447 }
2448 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002449 break;
2450 default:
2451 v4l2_err(v4l2_dev,
2452 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002453 ret = -EINVAL;
2454 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002455 }
2456
Philipp Zabelc566c782012-08-08 11:59:38 -03002457 switch (ctx->params.slice_mode) {
2458 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2459 value = 0;
2460 break;
2461 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2462 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2463 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002464 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002465 break;
2466 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2467 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2468 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2469 value |= 1 & CODA_SLICING_MODE_MASK;
2470 break;
2471 }
Javier Martin186b2502012-07-26 05:53:35 -03002472 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002473 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002474 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2475
2476 if (ctx->params.bitrate) {
2477 /* Rate control enabled */
2478 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2479 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002480 if (dev->devtype->product == CODA_960)
2481 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002482 } else {
2483 value = 0;
2484 }
2485 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2486
2487 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002488 coda_write(dev, ctx->params.intra_refresh,
2489 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002490
2491 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2492 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2493
Javier Martin186b2502012-07-26 05:53:35 -03002494
Philipp Zabel89548442014-07-11 06:36:17 -03002495 value = 0;
2496 if (dev->devtype->product == CODA_960)
2497 gamma = CODA9_DEFAULT_GAMMA;
2498 else
2499 gamma = CODA_DEFAULT_GAMMA;
2500 if (gamma > 0) {
2501 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2502 CODA_CMD_ENC_SEQ_RC_GAMMA);
2503 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002504
2505 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2506 coda_write(dev,
2507 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2508 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2509 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2510 }
Philipp Zabel89548442014-07-11 06:36:17 -03002511 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002512 if (ctx->params.h264_max_qp)
2513 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002514 if (CODA_DEFAULT_GAMMA > 0)
2515 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002516 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002517 if (CODA_DEFAULT_GAMMA > 0) {
2518 if (dev->devtype->product == CODA_DX6)
2519 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2520 else
2521 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2522 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002523 if (ctx->params.h264_min_qp)
2524 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2525 if (ctx->params.h264_max_qp)
2526 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002527 }
Javier Martin186b2502012-07-26 05:53:35 -03002528 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2529
Philipp Zabel89548442014-07-11 06:36:17 -03002530 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2531
Philipp Zabelc2d22512013-06-21 03:55:28 -03002532 coda_setup_iram(ctx);
2533
Javier Martin186b2502012-07-26 05:53:35 -03002534 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002535 switch (dev->devtype->product) {
2536 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002537 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002538 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002539 break;
2540 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002541 coda_write(dev, ctx->iram_info.search_ram_paddr,
2542 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2543 coda_write(dev, ctx->iram_info.search_ram_size,
2544 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002545 break;
2546 case CODA_960:
2547 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2548 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002549 }
Javier Martin186b2502012-07-26 05:53:35 -03002550 }
2551
Philipp Zabelfcb62822013-05-23 10:43:00 -03002552 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2553 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002554 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002555 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002556 }
2557
Philipp Zabelfcb62822013-05-23 10:43:00 -03002558 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2559 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2560 ret = -EFAULT;
2561 goto out;
2562 }
Javier Martin186b2502012-07-26 05:53:35 -03002563
Philipp Zabel89548442014-07-11 06:36:17 -03002564 if (dev->devtype->product == CODA_960)
2565 ctx->num_internal_frames = 4;
2566 else
2567 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002568 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002569 if (ret < 0) {
2570 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2571 goto out;
2572 }
Javier Martin186b2502012-07-26 05:53:35 -03002573
Philipp Zabelec25f682012-07-20 08:54:29 -03002574 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002575 coda_write(dev, q_data_src->bytesperline,
2576 CODA_CMD_SET_FRAME_BUF_STRIDE);
2577 if (dev->devtype->product == CODA_7541) {
2578 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002579 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002580 }
Philipp Zabel10436672012-07-02 09:03:55 -03002581 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002582 coda_write(dev, ctx->iram_info.buf_bit_use,
2583 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2584 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2585 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2586 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2587 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2588 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2589 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2590 coda_write(dev, ctx->iram_info.buf_ovl_use,
2591 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002592 if (dev->devtype->product == CODA_960) {
2593 coda_write(dev, ctx->iram_info.buf_btp_use,
2594 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2595
2596 /* FIXME */
2597 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2598 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2599 }
Philipp Zabel10436672012-07-02 09:03:55 -03002600 }
Philipp Zabel89548442014-07-11 06:36:17 -03002601
Philipp Zabelfcb62822013-05-23 10:43:00 -03002602 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2603 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002604 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002605 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002606 }
2607
2608 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002609 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002610 switch (dst_fourcc) {
2611 case V4L2_PIX_FMT_H264:
2612 /*
2613 * Get SPS in the first frame and copy it to an
2614 * intermediate buffer.
2615 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002616 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2617 &ctx->vpu_header[0][0],
2618 &ctx->vpu_header_size[0]);
2619 if (ret < 0)
2620 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002621
2622 /*
2623 * Get PPS 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_H264_PPS,
2627 &ctx->vpu_header[1][0],
2628 &ctx->vpu_header_size[1]);
2629 if (ret < 0)
2630 goto out;
2631
Javier Martin3f3f5c72012-10-29 05:20:29 -03002632 /*
2633 * Length of H.264 headers is variable and thus it might not be
2634 * aligned for the coda to append the encoded frame. In that is
2635 * the case a filler NAL must be added to header 2.
2636 */
2637 ctx->vpu_header_size[2] = coda_h264_padding(
2638 (ctx->vpu_header_size[0] +
2639 ctx->vpu_header_size[1]),
2640 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002641 break;
2642 case V4L2_PIX_FMT_MPEG4:
2643 /*
2644 * Get VOS in the first frame and copy it to an
2645 * intermediate buffer
2646 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002647 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2648 &ctx->vpu_header[0][0],
2649 &ctx->vpu_header_size[0]);
2650 if (ret < 0)
2651 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002652
Philipp Zabeld35167a2013-05-23 10:42:59 -03002653 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2654 &ctx->vpu_header[1][0],
2655 &ctx->vpu_header_size[1]);
2656 if (ret < 0)
2657 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002658
Philipp Zabeld35167a2013-05-23 10:42:59 -03002659 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2660 &ctx->vpu_header[2][0],
2661 &ctx->vpu_header_size[2]);
2662 if (ret < 0)
2663 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002664 break;
2665 default:
2666 /* No more formats need to save headers at the moment */
2667 break;
2668 }
2669
Philipp Zabeld35167a2013-05-23 10:42:59 -03002670out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002671 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002672 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002673}
2674
Hans Verkuile37559b2014-04-17 02:47:21 -03002675static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002676{
2677 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002678 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002679
2680 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002681 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002682 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002683 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002684
Philipp Zabel89548442014-07-11 06:36:17 -03002685 if (ctx->inst_type == CODA_INST_DECODER &&
2686 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2687 /* if this decoder instance is running, set the stream end flag */
2688 if (dev->devtype->product == CODA_960) {
2689 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2690
2691 val |= CODA_BIT_STREAM_END_FLAG;
2692 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2693 ctx->bit_stream_param = val;
2694 }
2695 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002696 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2697
2698 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002699 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002700 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002701 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002702 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002703
2704 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002705 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002706 }
2707
Philipp Zabel918c66f2013-06-27 06:59:01 -03002708 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002709 struct coda_timestamp *ts;
2710
2711 while (!list_empty(&ctx->timestamp_list)) {
2712 ts = list_first_entry(&ctx->timestamp_list,
2713 struct coda_timestamp, list);
2714 list_del(&ts->list);
2715 kfree(ts);
2716 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002717 kfifo_init(&ctx->bitstream_fifo,
2718 ctx->bitstream.vaddr, ctx->bitstream.size);
2719 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002720 }
Javier Martin186b2502012-07-26 05:53:35 -03002721}
2722
2723static struct vb2_ops coda_qops = {
2724 .queue_setup = coda_queue_setup,
2725 .buf_prepare = coda_buf_prepare,
2726 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002727 .start_streaming = coda_start_streaming,
2728 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002729 .wait_prepare = vb2_ops_wait_prepare,
2730 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002731};
2732
2733static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2734{
2735 struct coda_ctx *ctx =
2736 container_of(ctrl->handler, struct coda_ctx, ctrls);
2737
2738 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2739 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2740
2741 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002742 case V4L2_CID_HFLIP:
2743 if (ctrl->val)
2744 ctx->params.rot_mode |= CODA_MIR_HOR;
2745 else
2746 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2747 break;
2748 case V4L2_CID_VFLIP:
2749 if (ctrl->val)
2750 ctx->params.rot_mode |= CODA_MIR_VER;
2751 else
2752 ctx->params.rot_mode &= ~CODA_MIR_VER;
2753 break;
Javier Martin186b2502012-07-26 05:53:35 -03002754 case V4L2_CID_MPEG_VIDEO_BITRATE:
2755 ctx->params.bitrate = ctrl->val / 1000;
2756 break;
2757 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2758 ctx->params.gop_size = ctrl->val;
2759 break;
2760 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2761 ctx->params.h264_intra_qp = ctrl->val;
2762 break;
2763 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2764 ctx->params.h264_inter_qp = ctrl->val;
2765 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002766 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2767 ctx->params.h264_min_qp = ctrl->val;
2768 break;
2769 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2770 ctx->params.h264_max_qp = ctrl->val;
2771 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002772 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2773 ctx->params.h264_deblk_alpha = ctrl->val;
2774 break;
2775 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2776 ctx->params.h264_deblk_beta = ctrl->val;
2777 break;
2778 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2779 ctx->params.h264_deblk_enabled = (ctrl->val ==
2780 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2781 break;
Javier Martin186b2502012-07-26 05:53:35 -03002782 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2783 ctx->params.mpeg4_intra_qp = ctrl->val;
2784 break;
2785 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2786 ctx->params.mpeg4_inter_qp = ctrl->val;
2787 break;
2788 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2789 ctx->params.slice_mode = ctrl->val;
2790 break;
2791 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2792 ctx->params.slice_max_mb = ctrl->val;
2793 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002794 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2795 ctx->params.slice_max_bits = ctrl->val * 8;
2796 break;
Javier Martin186b2502012-07-26 05:53:35 -03002797 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2798 break;
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002799 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2800 ctx->params.intra_refresh = ctrl->val;
2801 break;
Javier Martin186b2502012-07-26 05:53:35 -03002802 default:
2803 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2804 "Invalid control, id=%d, val=%d\n",
2805 ctrl->id, ctrl->val);
2806 return -EINVAL;
2807 }
2808
2809 return 0;
2810}
2811
2812static struct v4l2_ctrl_ops coda_ctrl_ops = {
2813 .s_ctrl = coda_s_ctrl,
2814};
2815
2816static int coda_ctrls_setup(struct coda_ctx *ctx)
2817{
2818 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2819
2820 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002821 V4L2_CID_HFLIP, 0, 1, 1, 0);
2822 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2823 V4L2_CID_VFLIP, 0, 1, 1, 0);
2824 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002825 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2826 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2827 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2828 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002829 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002830 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002831 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002832 if (ctx->dev->devtype->product != CODA_960) {
2833 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2834 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2835 }
2836 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2837 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002838 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002839 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2840 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2841 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2842 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2843 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2844 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2845 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2846 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002847 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2848 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2849 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2850 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2851 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002852 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2853 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002854 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2855 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002856 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2857 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002858 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2859 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2860 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2861 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2862 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002863 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2864 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002865
2866 if (ctx->ctrls.error) {
2867 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2868 ctx->ctrls.error);
2869 return -EINVAL;
2870 }
2871
2872 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2873}
2874
2875static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2876 struct vb2_queue *dst_vq)
2877{
2878 struct coda_ctx *ctx = priv;
2879 int ret;
2880
Javier Martin186b2502012-07-26 05:53:35 -03002881 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002882 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002883 src_vq->drv_priv = ctx;
2884 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2885 src_vq->ops = &coda_qops;
2886 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002887 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002888 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002889
2890 ret = vb2_queue_init(src_vq);
2891 if (ret)
2892 return ret;
2893
Javier Martin186b2502012-07-26 05:53:35 -03002894 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002895 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002896 dst_vq->drv_priv = ctx;
2897 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2898 dst_vq->ops = &coda_qops;
2899 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002900 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002901 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002902
2903 return vb2_queue_init(dst_vq);
2904}
2905
Philipp Zabele11f3e62012-07-25 09:16:58 -03002906static int coda_next_free_instance(struct coda_dev *dev)
2907{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002908 int idx = ffz(dev->instance_mask);
2909
2910 if ((idx < 0) ||
2911 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2912 return -EBUSY;
2913
2914 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002915}
2916
Javier Martin186b2502012-07-26 05:53:35 -03002917static int coda_open(struct file *file)
2918{
2919 struct coda_dev *dev = video_drvdata(file);
2920 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002921 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002922 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002923 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002924
Javier Martin186b2502012-07-26 05:53:35 -03002925 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2926 if (!ctx)
2927 return -ENOMEM;
2928
Fabio Estevamf82bc202013-08-21 11:14:16 -03002929 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002930 if (idx < 0) {
2931 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002932 goto err_coda_max;
2933 }
2934 set_bit(idx, &dev->instance_mask);
2935
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002936 name = kasprintf(GFP_KERNEL, "context%d", idx);
2937 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2938 kfree(name);
2939
Philipp Zabel32b6f202014-07-11 06:36:20 -03002940 init_completion(&ctx->completion);
2941 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2942 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002943 v4l2_fh_init(&ctx->fh, video_devdata(file));
2944 file->private_data = &ctx->fh;
2945 v4l2_fh_add(&ctx->fh);
2946 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002947 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002948 switch (dev->devtype->product) {
2949 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002950 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002951 ctx->reg_idx = 0;
2952 break;
2953 default:
2954 ctx->reg_idx = idx;
2955 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002956
Philipp Zabel1e172732014-07-11 06:36:23 -03002957 /* Power up and upload firmware if necessary */
2958 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2959 if (ret < 0) {
2960 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2961 goto err_pm_get;
2962 }
2963
Fabio Estevam79830db2013-08-21 11:14:17 -03002964 ret = clk_prepare_enable(dev->clk_per);
2965 if (ret)
2966 goto err_clk_per;
2967
2968 ret = clk_prepare_enable(dev->clk_ahb);
2969 if (ret)
2970 goto err_clk_ahb;
2971
Javier Martin186b2502012-07-26 05:53:35 -03002972 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002973 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002974 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002975 if (IS_ERR(ctx->fh.m2m_ctx)) {
2976 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002977
2978 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2979 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002980 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002981 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002982
Javier Martin186b2502012-07-26 05:53:35 -03002983 ret = coda_ctrls_setup(ctx);
2984 if (ret) {
2985 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002986 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002987 }
2988
2989 ctx->fh.ctrl_handler = &ctx->ctrls;
2990
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002991 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
2992 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002993 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002994 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002995 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002996 }
2997
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002998 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2999 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
3000 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
3001 if (!ctx->bitstream.vaddr) {
3002 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
3003 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03003004 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003005 }
3006 kfifo_init(&ctx->bitstream_fifo,
3007 ctx->bitstream.vaddr, ctx->bitstream.size);
3008 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003009 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03003010 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003011
Javier Martin186b2502012-07-26 05:53:35 -03003012 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003013 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003014 coda_unlock(ctx);
3015
Javier Martin186b2502012-07-26 05:53:35 -03003016 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
3017 ctx->idx, ctx);
3018
3019 return 0;
3020
Fabio Estevamf82bc202013-08-21 11:14:16 -03003021err_dma_writecombine:
3022 coda_free_context_buffers(ctx);
3023 if (ctx->dev->devtype->product == CODA_DX6)
3024 coda_free_aux_buf(dev, &ctx->workbuf);
3025 coda_free_aux_buf(dev, &ctx->parabuf);
3026err_dma_alloc:
3027 v4l2_ctrl_handler_free(&ctx->ctrls);
3028err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03003029 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003030err_ctx_init:
3031 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003032err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03003033 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03003034err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03003035 pm_runtime_put_sync(&dev->plat_dev->dev);
3036err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03003037 v4l2_fh_del(&ctx->fh);
3038 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003039 clear_bit(ctx->idx, &dev->instance_mask);
3040err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03003041 kfree(ctx);
3042 return ret;
3043}
3044
3045static int coda_release(struct file *file)
3046{
3047 struct coda_dev *dev = video_drvdata(file);
3048 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3049
3050 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3051 ctx);
3052
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003053 debugfs_remove_recursive(ctx->debugfs_entry);
3054
Philipp Zabel918c66f2013-06-27 06:59:01 -03003055 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003056 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003057
3058 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003059 if (ctx->initialized) {
3060 queue_work(dev->workqueue, &ctx->seq_end_work);
3061 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003062 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003063
3064 coda_free_framebuffers(ctx);
3065
Javier Martin186b2502012-07-26 05:53:35 -03003066 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003067 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003068 coda_unlock(ctx);
3069
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003070 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3071 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003072 coda_free_context_buffers(ctx);
3073 if (ctx->dev->devtype->product == CODA_DX6)
3074 coda_free_aux_buf(dev, &ctx->workbuf);
3075
3076 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003077 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003078 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003079 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003080 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003081 v4l2_fh_del(&ctx->fh);
3082 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003083 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003084 kfree(ctx);
3085
3086 return 0;
3087}
3088
Javier Martin186b2502012-07-26 05:53:35 -03003089static const struct v4l2_file_operations coda_fops = {
3090 .owner = THIS_MODULE,
3091 .open = coda_open,
3092 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003093 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003094 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003095 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003096};
3097
Philipp Zabel918c66f2013-06-27 06:59:01 -03003098static void coda_finish_decode(struct coda_ctx *ctx)
3099{
3100 struct coda_dev *dev = ctx->dev;
3101 struct coda_q_data *q_data_src;
3102 struct coda_q_data *q_data_dst;
3103 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003104 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003105 int width, height;
3106 int decoded_idx;
3107 int display_idx;
3108 u32 src_fourcc;
3109 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003110 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003111 u32 val;
3112
Philipp Zabel14604e32014-07-11 06:36:22 -03003113 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003114
3115 /* Update kfifo out pointer from coda bitstream read pointer */
3116 coda_kfifo_sync_from_device(ctx);
3117
3118 /*
3119 * in stream-end mode, the read pointer can overshoot the write pointer
3120 * by up to 512 bytes
3121 */
3122 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3123 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3124 kfifo_init(&ctx->bitstream_fifo,
3125 ctx->bitstream.vaddr, ctx->bitstream.size);
3126 }
3127
3128 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3129 src_fourcc = q_data_src->fourcc;
3130
3131 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3132 if (val != 1)
3133 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3134
3135 success = val & 0x1;
3136 if (!success)
3137 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3138
3139 if (src_fourcc == V4L2_PIX_FMT_H264) {
3140 if (val & (1 << 3))
3141 v4l2_err(&dev->v4l2_dev,
3142 "insufficient PS buffer space (%d bytes)\n",
3143 ctx->psbuf.size);
3144 if (val & (1 << 2))
3145 v4l2_err(&dev->v4l2_dev,
3146 "insufficient slice buffer space (%d bytes)\n",
3147 ctx->slicebuf.size);
3148 }
3149
3150 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3151 width = (val >> 16) & 0xffff;
3152 height = val & 0xffff;
3153
3154 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3155
Philipp Zabel52c41672014-07-11 06:36:19 -03003156 /* frame crop information */
3157 if (src_fourcc == V4L2_PIX_FMT_H264) {
3158 u32 left_right;
3159 u32 top_bottom;
3160
3161 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3162 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3163
3164 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3165 /* Keep current crop information */
3166 } else {
3167 struct v4l2_rect *rect = &q_data_dst->rect;
3168
3169 rect->left = left_right >> 16 & 0xffff;
3170 rect->top = top_bottom >> 16 & 0xffff;
3171 rect->width = width - rect->left -
3172 (left_right & 0xffff);
3173 rect->height = height - rect->top -
3174 (top_bottom & 0xffff);
3175 }
3176 } else {
3177 /* no cropping */
3178 }
3179
Philipp Zabel410e5e42014-07-11 06:36:32 -03003180 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3181 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003182 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003183 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003184
3185 if (dev->devtype->product == CODA_7541) {
3186 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3187 if (val == 0) {
3188 /* not enough bitstream data */
3189 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3190 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003191 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003192 return;
3193 }
3194 }
3195
3196 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3197
3198 /*
3199 * The previous display frame was copied out by the rotator,
3200 * now it can be overwritten again
3201 */
3202 if (ctx->display_idx >= 0 &&
3203 ctx->display_idx < ctx->num_internal_frames) {
3204 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3205 coda_write(dev, ctx->frm_dis_flg,
3206 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3207 }
3208
3209 /*
3210 * The index of the last decoded frame, not necessarily in
3211 * display order, and the index of the next display frame.
3212 * The latter could have been decoded in a previous run.
3213 */
3214 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3215 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3216
3217 if (decoded_idx == -1) {
3218 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003219 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3220 ctx->sequence_offset++;
3221 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003222 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003223 } else if (decoded_idx == -2) {
3224 /* no frame was decoded, we still return the remaining buffers */
3225 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3226 v4l2_err(&dev->v4l2_dev,
3227 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003228 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003229 ts = list_first_entry(&ctx->timestamp_list,
3230 struct coda_timestamp, list);
3231 list_del(&ts->list);
3232 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003233 val -= ctx->sequence_offset;
3234 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003235 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003236 "sequence number mismatch (%d(%d) != %d)\n",
3237 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003238 }
3239 ctx->frame_timestamps[decoded_idx] = *ts;
3240 kfree(ts);
3241
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003242 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3243 if (val == 0)
3244 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3245 else if (val == 1)
3246 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3247 else
3248 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003249
3250 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003251 }
3252
3253 if (display_idx == -1) {
3254 /*
3255 * no more frames to be decoded, but there could still
3256 * be rotator output to dequeue
3257 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003258 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003259 } else if (display_idx == -3) {
3260 /* possibly prescan failure */
3261 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3262 v4l2_err(&dev->v4l2_dev,
3263 "presentation frame index out of range: %d\n",
3264 display_idx);
3265 }
3266
3267 /* If a frame was copied out, return it */
3268 if (ctx->display_idx >= 0 &&
3269 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003270 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003271 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3272
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003273 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3274 V4L2_BUF_FLAG_PFRAME |
3275 V4L2_BUF_FLAG_BFRAME);
3276 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003277 ts = &ctx->frame_timestamps[ctx->display_idx];
3278 dst_buf->v4l2_buf.timecode = ts->timecode;
3279 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003280
Philipp Zabel918c66f2013-06-27 06:59:01 -03003281 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3282
Philipp Zabel410e5e42014-07-11 06:36:32 -03003283 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3284 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003285
3286 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3287 "job finished: decoding frame (%d) (%s)\n",
3288 dst_buf->v4l2_buf.sequence,
3289 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3290 "KEYFRAME" : "PFRAME");
3291 } else {
3292 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3293 "job finished: no frame decoded\n");
3294 }
3295
3296 /* The rotator will copy the current display frame next time */
3297 ctx->display_idx = display_idx;
3298}
3299
3300static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003301{
Philipp Zabelec25f682012-07-20 08:54:29 -03003302 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003303 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003304 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003305
Philipp Zabel14604e32014-07-11 06:36:22 -03003306 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3307 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003308
3309 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003310 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003311 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3312
Javier Martin186b2502012-07-26 05:53:35 -03003313 /* Calculate bytesused field */
3314 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003315 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3316 ctx->vpu_header_size[0] +
3317 ctx->vpu_header_size[1] +
3318 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003319 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003320 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003321 }
3322
3323 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3324 wr_ptr - start_ptr);
3325
3326 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3327 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3328
Philipp Zabel51660282013-09-30 10:34:49 -03003329 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003330 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3331 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3332 } else {
3333 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3334 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3335 }
3336
Kamil Debskiccd571c2013-04-24 10:38:24 -03003337 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003338 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3339 dst_buf->v4l2_buf.flags |=
3340 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003341 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3342
Philipp Zabelec25f682012-07-20 08:54:29 -03003343 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003344
Philipp Zabel14604e32014-07-11 06:36:22 -03003345 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003346 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3347
3348 ctx->gopcounter--;
3349 if (ctx->gopcounter < 0)
3350 ctx->gopcounter = ctx->params.gop_size - 1;
3351
3352 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3353 "job finished: encoding frame (%d) (%s)\n",
3354 dst_buf->v4l2_buf.sequence,
3355 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3356 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003357}
3358
3359static irqreturn_t coda_irq_handler(int irq, void *data)
3360{
3361 struct coda_dev *dev = data;
3362 struct coda_ctx *ctx;
3363
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003364 /* read status register to attend the IRQ */
3365 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3366 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3367 CODA_REG_BIT_INT_CLEAR);
3368
3369 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3370 if (ctx == NULL) {
3371 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3372 mutex_unlock(&dev->coda_mutex);
3373 return IRQ_HANDLED;
3374 }
3375
3376 if (ctx->aborting) {
3377 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3378 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003379 }
3380
3381 if (coda_isbusy(ctx->dev)) {
3382 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3383 "coda is still busy!!!!\n");
3384 return IRQ_NONE;
3385 }
3386
Philipp Zabel32b6f202014-07-11 06:36:20 -03003387 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003388
3389 return IRQ_HANDLED;
3390}
3391
3392static u32 coda_supported_firmwares[] = {
3393 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003394 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003395 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003396};
3397
3398static bool coda_firmware_supported(u32 vernum)
3399{
3400 int i;
3401
3402 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3403 if (vernum == coda_supported_firmwares[i])
3404 return true;
3405 return false;
3406}
3407
Philipp Zabel87048bb2012-07-02 07:03:43 -03003408static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003409{
Javier Martin186b2502012-07-26 05:53:35 -03003410 u32 data;
3411 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003412 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003413
Fabio Estevam79830db2013-08-21 11:14:17 -03003414 ret = clk_prepare_enable(dev->clk_per);
3415 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003416 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003417
3418 ret = clk_prepare_enable(dev->clk_ahb);
3419 if (ret)
3420 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003421
Philipp Zabel8f452842014-07-11 06:36:35 -03003422 if (dev->rstc)
3423 reset_control_reset(dev->rstc);
3424
Javier Martin186b2502012-07-26 05:53:35 -03003425 /*
3426 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003427 * The 16-bit chars in the code buffer are in memory access
3428 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003429 * Data in this SRAM survives a reboot.
3430 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003431 p = (u16 *)dev->codebuf.vaddr;
3432 if (dev->devtype->product == CODA_DX6) {
3433 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3434 data = CODA_DOWN_ADDRESS_SET(i) |
3435 CODA_DOWN_DATA_SET(p[i ^ 1]);
3436 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3437 }
3438 } else {
3439 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3440 data = CODA_DOWN_ADDRESS_SET(i) |
3441 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3442 3 - (i % 4)]);
3443 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3444 }
Javier Martin186b2502012-07-26 05:53:35 -03003445 }
Javier Martin186b2502012-07-26 05:53:35 -03003446
Philipp Zabel9acf7692013-05-23 10:42:56 -03003447 /* Clear registers */
3448 for (i = 0; i < 64; i++)
3449 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3450
Javier Martin186b2502012-07-26 05:53:35 -03003451 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003452 if (dev->devtype->product == CODA_960 ||
3453 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003454 coda_write(dev, dev->tempbuf.paddr,
3455 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003456 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003457 } else {
3458 coda_write(dev, dev->workbuf.paddr,
3459 CODA_REG_BIT_WORK_BUF_ADDR);
3460 }
Javier Martin186b2502012-07-26 05:53:35 -03003461 coda_write(dev, dev->codebuf.paddr,
3462 CODA_REG_BIT_CODE_BUF_ADDR);
3463 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3464
3465 /* Set default values */
3466 switch (dev->devtype->product) {
3467 case CODA_DX6:
3468 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3469 break;
3470 default:
3471 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3472 }
Philipp Zabel89548442014-07-11 06:36:17 -03003473 if (dev->devtype->product == CODA_960)
3474 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3475 else
3476 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003477
3478 if (dev->devtype->product != CODA_DX6)
3479 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3480
Javier Martin186b2502012-07-26 05:53:35 -03003481 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3482 CODA_REG_BIT_INT_ENABLE);
3483
3484 /* Reset VPU and start processor */
3485 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3486 data |= CODA_REG_RESET_ENABLE;
3487 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3488 udelay(10);
3489 data &= ~CODA_REG_RESET_ENABLE;
3490 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3491 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3492
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003493 clk_disable_unprepare(dev->clk_ahb);
3494 clk_disable_unprepare(dev->clk_per);
3495
3496 return 0;
3497
3498err_clk_ahb:
3499 clk_disable_unprepare(dev->clk_per);
3500err_clk_per:
3501 return ret;
3502}
3503
3504static int coda_check_firmware(struct coda_dev *dev)
3505{
3506 u16 product, major, minor, release;
3507 u32 data;
3508 int ret;
3509
3510 ret = clk_prepare_enable(dev->clk_per);
3511 if (ret)
3512 goto err_clk_per;
3513
3514 ret = clk_prepare_enable(dev->clk_ahb);
3515 if (ret)
3516 goto err_clk_ahb;
3517
Javier Martin186b2502012-07-26 05:53:35 -03003518 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3519 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3520 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3521 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3522 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3523 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003524 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003525 ret = -EIO;
3526 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003527 }
3528
Philipp Zabel89548442014-07-11 06:36:17 -03003529 if (dev->devtype->product == CODA_960) {
3530 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3531 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3532 data);
3533 }
3534
Javier Martin186b2502012-07-26 05:53:35 -03003535 /* Check we are compatible with the loaded firmware */
3536 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3537 product = CODA_FIRMWARE_PRODUCT(data);
3538 major = CODA_FIRMWARE_MAJOR(data);
3539 minor = CODA_FIRMWARE_MINOR(data);
3540 release = CODA_FIRMWARE_RELEASE(data);
3541
3542 clk_disable_unprepare(dev->clk_per);
3543 clk_disable_unprepare(dev->clk_ahb);
3544
3545 if (product != dev->devtype->product) {
3546 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3547 " Version: %u.%u.%u\n",
3548 coda_product_name(dev->devtype->product),
3549 coda_product_name(product), major, minor, release);
3550 return -EINVAL;
3551 }
3552
3553 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3554 coda_product_name(product));
3555
3556 if (coda_firmware_supported(data)) {
3557 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3558 major, minor, release);
3559 } else {
3560 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3561 "%u.%u.%u\n", major, minor, release);
3562 }
3563
3564 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003565
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003566err_run_cmd:
3567 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003568err_clk_ahb:
3569 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003570err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003571 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003572}
3573
3574static void coda_fw_callback(const struct firmware *fw, void *context)
3575{
3576 struct coda_dev *dev = context;
3577 struct platform_device *pdev = dev->plat_dev;
3578 int ret;
3579
3580 if (!fw) {
3581 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3582 return;
3583 }
3584
3585 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003586 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3587 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003588 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003589 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3590 return;
3591 }
3592
Philipp Zabel87048bb2012-07-02 07:03:43 -03003593 /* Copy the whole firmware image to the code buffer */
3594 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3595 release_firmware(fw);
3596
Philipp Zabel1e172732014-07-11 06:36:23 -03003597 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3598 /*
3599 * Enabling power temporarily will cause coda_hw_init to be
3600 * called via coda_runtime_resume by the pm domain.
3601 */
3602 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3603 if (ret < 0) {
3604 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3605 ret);
3606 return;
3607 }
3608
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003609 ret = coda_check_firmware(dev);
3610 if (ret < 0)
3611 return;
3612
Philipp Zabel1e172732014-07-11 06:36:23 -03003613 pm_runtime_put_sync(&dev->plat_dev->dev);
3614 } else {
3615 /*
3616 * If runtime pm is disabled or pm_domain is not set,
3617 * initialize once manually.
3618 */
3619 ret = coda_hw_init(dev);
3620 if (ret < 0) {
3621 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3622 return;
3623 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003624
3625 ret = coda_check_firmware(dev);
3626 if (ret < 0)
3627 return;
Javier Martin186b2502012-07-26 05:53:35 -03003628 }
3629
3630 dev->vfd.fops = &coda_fops,
3631 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3632 dev->vfd.release = video_device_release_empty,
3633 dev->vfd.lock = &dev->dev_mutex;
3634 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003635 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003636 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3637 video_set_drvdata(&dev->vfd, dev);
3638
3639 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3640 if (IS_ERR(dev->alloc_ctx)) {
3641 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3642 return;
3643 }
3644
3645 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3646 if (IS_ERR(dev->m2m_dev)) {
3647 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3648 goto rel_ctx;
3649 }
3650
3651 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3652 if (ret) {
3653 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3654 goto rel_m2m;
3655 }
3656 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3657 dev->vfd.num);
3658
3659 return;
3660
3661rel_m2m:
3662 v4l2_m2m_release(dev->m2m_dev);
3663rel_ctx:
3664 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3665}
3666
3667static int coda_firmware_request(struct coda_dev *dev)
3668{
3669 char *fw = dev->devtype->firmware;
3670
3671 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3672 coda_product_name(dev->devtype->product));
3673
3674 return request_firmware_nowait(THIS_MODULE, true,
3675 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3676}
3677
3678enum coda_platform {
3679 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003680 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003681 CODA_IMX6Q,
3682 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003683};
3684
Emil Goodec06d8752012-08-14 17:44:42 -03003685static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003686 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003687 .firmware = "v4l-codadx6-imx27.bin",
3688 .product = CODA_DX6,
3689 .codecs = codadx6_codecs,
3690 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003691 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003692 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003693 .firmware = "v4l-coda7541-imx53.bin",
3694 .product = CODA_7541,
3695 .codecs = coda7_codecs,
3696 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003697 },
Philipp Zabel89548442014-07-11 06:36:17 -03003698 [CODA_IMX6Q] = {
3699 .firmware = "v4l-coda960-imx6q.bin",
3700 .product = CODA_960,
3701 .codecs = coda9_codecs,
3702 .num_codecs = ARRAY_SIZE(coda9_codecs),
3703 },
3704 [CODA_IMX6DL] = {
3705 .firmware = "v4l-coda960-imx6dl.bin",
3706 .product = CODA_960,
3707 .codecs = coda9_codecs,
3708 .num_codecs = ARRAY_SIZE(coda9_codecs),
3709 },
Javier Martin186b2502012-07-26 05:53:35 -03003710};
3711
3712static struct platform_device_id coda_platform_ids[] = {
3713 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003714 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003715 { /* sentinel */ }
3716};
3717MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3718
3719#ifdef CONFIG_OF
3720static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003721 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003722 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003723 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3724 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003725 { /* sentinel */ }
3726};
3727MODULE_DEVICE_TABLE(of, coda_dt_ids);
3728#endif
3729
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003730static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003731{
3732 const struct of_device_id *of_id =
3733 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3734 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003735 struct coda_platform_data *pdata = pdev->dev.platform_data;
3736 struct device_node *np = pdev->dev.of_node;
3737 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003738 struct coda_dev *dev;
3739 struct resource *res;
3740 int ret, irq;
3741
3742 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3743 if (!dev) {
3744 dev_err(&pdev->dev, "Not enough memory for %s\n",
3745 CODA_NAME);
3746 return -ENOMEM;
3747 }
3748
3749 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003750 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003751
3752 dev->plat_dev = pdev;
3753 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3754 if (IS_ERR(dev->clk_per)) {
3755 dev_err(&pdev->dev, "Could not get per clock\n");
3756 return PTR_ERR(dev->clk_per);
3757 }
3758
3759 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3760 if (IS_ERR(dev->clk_ahb)) {
3761 dev_err(&pdev->dev, "Could not get ahb clock\n");
3762 return PTR_ERR(dev->clk_ahb);
3763 }
3764
3765 /* Get memory for physical registers */
3766 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003767 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3768 if (IS_ERR(dev->regs_base))
3769 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003770
3771 /* IRQ */
3772 irq = platform_get_irq(pdev, 0);
3773 if (irq < 0) {
3774 dev_err(&pdev->dev, "failed to get irq resource\n");
3775 return -ENOENT;
3776 }
3777
Philipp Zabel918c66f2013-06-27 06:59:01 -03003778 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003779 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003780 dev_err(&pdev->dev, "failed to request irq\n");
3781 return -ENOENT;
3782 }
3783
Philipp Zabel8f452842014-07-11 06:36:35 -03003784 dev->rstc = devm_reset_control_get(&pdev->dev, NULL);
3785 if (IS_ERR(dev->rstc)) {
3786 ret = PTR_ERR(dev->rstc);
3787 if (ret == -ENOENT) {
3788 dev->rstc = NULL;
3789 } else {
3790 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3791 return ret;
3792 }
3793 }
3794
Philipp Zabel657eee72013-04-29 16:17:14 -07003795 /* Get IRAM pool from device tree or platform data */
3796 pool = of_get_named_gen_pool(np, "iram", 0);
3797 if (!pool && pdata)
3798 pool = dev_get_gen_pool(pdata->iram_dev);
3799 if (!pool) {
3800 dev_err(&pdev->dev, "iram pool not available\n");
3801 return -ENOMEM;
3802 }
3803 dev->iram_pool = pool;
3804
Javier Martin186b2502012-07-26 05:53:35 -03003805 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3806 if (ret)
3807 return ret;
3808
3809 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003810 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003811
3812 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3813
3814 if (of_id) {
3815 dev->devtype = of_id->data;
3816 } else if (pdev_id) {
3817 dev->devtype = &coda_devdata[pdev_id->driver_data];
3818 } else {
3819 v4l2_device_unregister(&dev->v4l2_dev);
3820 return -EINVAL;
3821 }
3822
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003823 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3824 if (!dev->debugfs_root)
3825 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3826
Javier Martin186b2502012-07-26 05:53:35 -03003827 /* allocate auxiliary per-device buffers for the BIT processor */
3828 switch (dev->devtype->product) {
3829 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003830 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003831 CODADX6_WORK_BUF_SIZE, "workbuf",
3832 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003833 if (ret < 0) {
3834 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3835 v4l2_device_unregister(&dev->v4l2_dev);
3836 return ret;
3837 }
Javier Martin186b2502012-07-26 05:53:35 -03003838 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003839 case CODA_7541:
3840 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3841 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003842 case CODA_960:
3843 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3844 break;
Javier Martin186b2502012-07-26 05:53:35 -03003845 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003846 if (dev->tempbuf.size) {
3847 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003848 dev->tempbuf.size, "tempbuf",
3849 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003850 if (ret < 0) {
3851 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3852 v4l2_device_unregister(&dev->v4l2_dev);
3853 return ret;
3854 }
Javier Martin186b2502012-07-26 05:53:35 -03003855 }
3856
Philipp Zabel918c66f2013-06-27 06:59:01 -03003857 switch (dev->devtype->product) {
3858 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003859 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003860 break;
3861 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003862 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003863 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003864 case CODA_960:
3865 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003866 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003867 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3868 &dev->iram.paddr);
3869 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003870 dev_err(&pdev->dev, "unable to alloc iram\n");
3871 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003872 }
3873
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003874 dev->iram.blob.data = dev->iram.vaddr;
3875 dev->iram.blob.size = dev->iram.size;
3876 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
3877 &dev->iram.blob);
3878
Philipp Zabel32b6f202014-07-11 06:36:20 -03003879 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3880 if (!dev->workqueue) {
3881 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3882 return -ENOMEM;
3883 }
3884
Javier Martin186b2502012-07-26 05:53:35 -03003885 platform_set_drvdata(pdev, dev);
3886
Philipp Zabel1e172732014-07-11 06:36:23 -03003887 pm_runtime_enable(&pdev->dev);
3888
Javier Martin186b2502012-07-26 05:53:35 -03003889 return coda_firmware_request(dev);
3890}
3891
3892static int coda_remove(struct platform_device *pdev)
3893{
3894 struct coda_dev *dev = platform_get_drvdata(pdev);
3895
3896 video_unregister_device(&dev->vfd);
3897 if (dev->m2m_dev)
3898 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003899 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003900 if (dev->alloc_ctx)
3901 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3902 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003903 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003904 if (dev->iram.vaddr)
3905 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3906 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003907 coda_free_aux_buf(dev, &dev->codebuf);
3908 coda_free_aux_buf(dev, &dev->tempbuf);
3909 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003910 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03003911 return 0;
3912}
3913
Philipp Zabel1e172732014-07-11 06:36:23 -03003914#ifdef CONFIG_PM_RUNTIME
3915static int coda_runtime_resume(struct device *dev)
3916{
3917 struct coda_dev *cdev = dev_get_drvdata(dev);
3918 int ret = 0;
3919
3920 if (dev->pm_domain) {
3921 ret = coda_hw_init(cdev);
3922 if (ret)
3923 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3924 }
3925
3926 return ret;
3927}
3928#endif
3929
3930static const struct dev_pm_ops coda_pm_ops = {
3931 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3932};
3933
Javier Martin186b2502012-07-26 05:53:35 -03003934static struct platform_driver coda_driver = {
3935 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003936 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003937 .driver = {
3938 .name = CODA_NAME,
3939 .owner = THIS_MODULE,
3940 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003941 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003942 },
3943 .id_table = coda_platform_ids,
3944};
3945
3946module_platform_driver(coda_driver);
3947
3948MODULE_LICENSE("GPL");
3949MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3950MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");