blob: c1e950aacd36ed731f5b5a08a0f39a41455729df [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
Javier Martin186b2502012-07-26 05:53:35 -030047#define CODA_PARA_BUF_SIZE (10 * 1024)
48#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070049#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030050#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030051#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030052
Philipp Zabel918c66f2013-06-27 06:59:01 -030053#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030054#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030055
56#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030057
Philipp Zabelb96904e2013-05-23 10:42:58 -030058#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030059#define FMO_SLICE_SAVE_BUF_SIZE (32)
60#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030061#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030062
63#define MIN_W 176
64#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030065
66#define S_ALIGN 1 /* multiple of 2 */
67#define W_ALIGN 1 /* multiple of 2 */
68#define H_ALIGN 1 /* multiple of 2 */
69
70#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
71
72static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030073module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030074MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
75
76enum {
77 V4L2_M2M_SRC = 0,
78 V4L2_M2M_DST = 1,
79};
80
Javier Martin186b2502012-07-26 05:53:35 -030081enum coda_inst_type {
82 CODA_INST_ENCODER,
83 CODA_INST_DECODER,
84};
85
86enum coda_product {
87 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030088 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030089 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030090};
91
92struct coda_fmt {
93 char *name;
94 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030095};
96
97struct coda_codec {
98 u32 mode;
99 u32 src_fourcc;
100 u32 dst_fourcc;
101 u32 max_w;
102 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300103};
104
105struct coda_devtype {
106 char *firmware;
107 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300108 struct coda_codec *codecs;
109 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300110 size_t workbuf_size;
Philipp Zabel5d73fad2014-07-11 06:36:42 -0300111 size_t tempbuf_size;
Javier Martin186b2502012-07-26 05:53:35 -0300112};
113
114/* Per-queue, driver-specific private data */
115struct coda_q_data {
116 unsigned int width;
117 unsigned int height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300118 unsigned int bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300119 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300120 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300121 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300122};
123
124struct coda_aux_buf {
125 void *vaddr;
126 dma_addr_t paddr;
127 u32 size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300128 struct debugfs_blob_wrapper blob;
129 struct dentry *dentry;
Javier Martin186b2502012-07-26 05:53:35 -0300130};
131
132struct coda_dev {
133 struct v4l2_device v4l2_dev;
134 struct video_device vfd;
135 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300136 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300137
138 void __iomem *regs_base;
139 struct clk *clk_per;
140 struct clk *clk_ahb;
Philipp Zabel8f452842014-07-11 06:36:35 -0300141 struct reset_control *rstc;
Javier Martin186b2502012-07-26 05:53:35 -0300142
143 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300144 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300145 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700146 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300147 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300148
149 spinlock_t irqlock;
150 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300151 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300152 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300153 struct v4l2_m2m_dev *m2m_dev;
154 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300155 struct list_head instances;
156 unsigned long instance_mask;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300157 struct dentry *debugfs_root;
Javier Martin186b2502012-07-26 05:53:35 -0300158};
159
160struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300161 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300162 u8 h264_intra_qp;
163 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300164 u8 h264_min_qp;
165 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300166 u8 h264_deblk_enabled;
167 u8 h264_deblk_alpha;
168 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300169 u8 mpeg4_intra_qp;
170 u8 mpeg4_inter_qp;
171 u8 gop_size;
Philipp Zabelf38f79d52014-07-11 06:36:28 -0300172 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300173 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300174 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300175 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
176 u32 framerate;
177 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300178 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300179 u32 slice_max_mb;
180};
181
Philipp Zabelc2d22512013-06-21 03:55:28 -0300182struct coda_iram_info {
183 u32 axi_sram_use;
184 phys_addr_t buf_bit_use;
185 phys_addr_t buf_ip_ac_dc_use;
186 phys_addr_t buf_dbk_y_use;
187 phys_addr_t buf_dbk_c_use;
188 phys_addr_t buf_ovl_use;
189 phys_addr_t buf_btp_use;
190 phys_addr_t search_ram_paddr;
191 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300192 int remaining;
193 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300194};
195
Philipp Zabel89548442014-07-11 06:36:17 -0300196struct gdi_tiled_map {
197 int xy2ca_map[16];
198 int xy2ba_map[16];
199 int xy2ra_map[16];
200 int rbc2axi_map[32];
201 int xy2rbc_config;
202 int map_type;
203#define GDI_LINEAR_FRAME_MAP 0
204};
205
Philipp Zabel846ced92014-07-11 06:36:31 -0300206struct coda_timestamp {
207 struct list_head list;
208 u32 sequence;
209 struct v4l2_timecode timecode;
210 struct timeval timestamp;
211};
212
Javier Martin186b2502012-07-26 05:53:35 -0300213struct coda_ctx {
214 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300215 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300216 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300217 struct work_struct pic_run_work;
218 struct work_struct seq_end_work;
219 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300220 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300221 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300222 int streamon_out;
223 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300224 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300225 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300226 u32 osequence;
Philipp Zabelcb2c0282014-07-11 06:36:33 -0300227 u32 sequence_offset;
Javier Martin186b2502012-07-26 05:53:35 -0300228 struct coda_q_data q_data[2];
229 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300230 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300231 enum v4l2_colorspace colorspace;
232 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300233 struct v4l2_ctrl_handler ctrls;
234 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300235 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300236 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300237 char vpu_header[3][64];
238 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300239 struct kfifo bitstream_fifo;
240 struct mutex bitstream_mutex;
241 struct coda_aux_buf bitstream;
Philipp Zabel84e23652014-07-11 06:36:34 -0300242 bool hold;
Javier Martin186b2502012-07-26 05:53:35 -0300243 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300244 struct coda_aux_buf psbuf;
245 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300246 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300247 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300248 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300249 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300250 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300251 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300252 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300253 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300254 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300255 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300256 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300257 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300258 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300259 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300260 int display_idx;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300261 struct dentry *debugfs_entry;
Javier Martin186b2502012-07-26 05:53:35 -0300262};
263
Javier Martin832fbb52012-10-29 08:34:59 -0300264static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
265 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
266static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300267
Javier Martin186b2502012-07-26 05:53:35 -0300268static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
269{
270 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
271 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
272 writel(data, dev->regs_base + reg);
273}
274
275static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
276{
277 u32 data;
278 data = readl(dev->regs_base + reg);
279 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
280 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
281 return data;
282}
283
284static inline unsigned long coda_isbusy(struct coda_dev *dev)
285{
286 return coda_read(dev, CODA_REG_BIT_BUSY);
287}
288
289static inline int coda_is_initialized(struct coda_dev *dev)
290{
291 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
292}
293
294static int coda_wait_timeout(struct coda_dev *dev)
295{
296 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
297
298 while (coda_isbusy(dev)) {
299 if (time_after(jiffies, timeout))
300 return -ETIMEDOUT;
301 }
302 return 0;
303}
304
305static void coda_command_async(struct coda_ctx *ctx, int cmd)
306{
307 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300308
Philipp Zabel89548442014-07-11 06:36:17 -0300309 if (dev->devtype->product == CODA_960 ||
310 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300311 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300312 coda_write(dev, ctx->bit_stream_param,
313 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300314 coda_write(dev, ctx->frm_dis_flg,
315 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300316 coda_write(dev, ctx->frame_mem_ctrl,
317 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300318 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
319 }
320
Philipp Zabel89548442014-07-11 06:36:17 -0300321 if (dev->devtype->product == CODA_960) {
322 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
323 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
324 }
325
Javier Martin186b2502012-07-26 05:53:35 -0300326 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
327
328 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
329 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300330 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
331
Javier Martin186b2502012-07-26 05:53:35 -0300332 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
333}
334
335static int coda_command_sync(struct coda_ctx *ctx, int cmd)
336{
337 struct coda_dev *dev = ctx->dev;
338
339 coda_command_async(ctx, cmd);
340 return coda_wait_timeout(dev);
341}
342
Philipp Zabel8f452842014-07-11 06:36:35 -0300343static int coda_hw_reset(struct coda_ctx *ctx)
344{
345 struct coda_dev *dev = ctx->dev;
346 unsigned long timeout;
347 unsigned int idx;
348 int ret;
349
350 if (!dev->rstc)
351 return -ENOENT;
352
353 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
354
355 timeout = jiffies + msecs_to_jiffies(100);
356 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
357 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
358 if (time_after(jiffies, timeout))
359 return -ETIME;
360 cpu_relax();
361 }
362
363 ret = reset_control_reset(dev->rstc);
364 if (ret < 0)
365 return ret;
366
367 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
368 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
369 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
370 ret = coda_wait_timeout(dev);
371 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
372
373 return ret;
374}
375
Javier Martin186b2502012-07-26 05:53:35 -0300376static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
377 enum v4l2_buf_type type)
378{
379 switch (type) {
380 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
381 return &(ctx->q_data[V4L2_M2M_SRC]);
382 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
383 return &(ctx->q_data[V4L2_M2M_DST]);
384 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300385 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300386 }
Javier Martin186b2502012-07-26 05:53:35 -0300387}
388
389/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300390 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300391 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300392static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300393 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300394 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300395 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300396 },
397 {
398 .name = "YUV 4:2:0 Planar, YCrCb",
399 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300400 },
401 {
402 .name = "H264 Encoded Stream",
403 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300404 },
405 {
406 .name = "MPEG4 Encoded Stream",
407 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300408 },
409};
410
Philipp Zabelb96904e2013-05-23 10:42:58 -0300411#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
412 { mode, src_fourcc, dst_fourcc, max_w, max_h }
413
414/*
415 * Arrays of codecs supported by each given version of Coda:
416 * i.MX27 -> codadx6
417 * i.MX5x -> coda7
418 * i.MX6 -> coda960
419 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
420 */
421static struct coda_codec codadx6_codecs[] = {
422 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
423 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300424};
425
Philipp Zabelb96904e2013-05-23 10:42:58 -0300426static struct coda_codec coda7_codecs[] = {
427 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
428 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300429 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
430 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300431};
432
Philipp Zabel89548442014-07-11 06:36:17 -0300433static struct coda_codec coda9_codecs[] = {
434 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
435 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
436 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
437 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
438};
439
Philipp Zabelb96904e2013-05-23 10:42:58 -0300440static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300441{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300442 switch (fourcc) {
443 case V4L2_PIX_FMT_YUV420:
444 case V4L2_PIX_FMT_YVU420:
445 return true;
446 default:
447 return false;
448 }
449}
Javier Martin186b2502012-07-26 05:53:35 -0300450
Philipp Zabelb96904e2013-05-23 10:42:58 -0300451/*
452 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
453 * tables.
454 */
455static u32 coda_format_normalize_yuv(u32 fourcc)
456{
457 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
458}
459
460static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
461 int dst_fourcc)
462{
463 struct coda_codec *codecs = dev->devtype->codecs;
464 int num_codecs = dev->devtype->num_codecs;
465 int k;
466
467 src_fourcc = coda_format_normalize_yuv(src_fourcc);
468 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
469 if (src_fourcc == dst_fourcc)
470 return NULL;
471
472 for (k = 0; k < num_codecs; k++) {
473 if (codecs[k].src_fourcc == src_fourcc &&
474 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300475 break;
476 }
477
Philipp Zabelb96904e2013-05-23 10:42:58 -0300478 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300479 return NULL;
480
Philipp Zabelb96904e2013-05-23 10:42:58 -0300481 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300482}
483
Philipp Zabel57625592013-09-30 10:34:51 -0300484static void coda_get_max_dimensions(struct coda_dev *dev,
485 struct coda_codec *codec,
486 int *max_w, int *max_h)
487{
488 struct coda_codec *codecs = dev->devtype->codecs;
489 int num_codecs = dev->devtype->num_codecs;
490 unsigned int w, h;
491 int k;
492
493 if (codec) {
494 w = codec->max_w;
495 h = codec->max_h;
496 } else {
497 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
498 w = max(w, codecs[k].max_w);
499 h = max(h, codecs[k].max_h);
500 }
501 }
502
503 if (max_w)
504 *max_w = w;
505 if (max_h)
506 *max_h = h;
507}
508
Philipp Zabel927933f2013-09-30 10:34:48 -0300509static char *coda_product_name(int product)
510{
511 static char buf[9];
512
513 switch (product) {
514 case CODA_DX6:
515 return "CodaDx6";
516 case CODA_7541:
517 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300518 case CODA_960:
519 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300520 default:
521 snprintf(buf, sizeof(buf), "(0x%04x)", product);
522 return buf;
523 }
524}
525
Javier Martin186b2502012-07-26 05:53:35 -0300526/*
527 * V4L2 ioctl() operations.
528 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300529static int coda_querycap(struct file *file, void *priv,
530 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300531{
Philipp Zabel927933f2013-09-30 10:34:48 -0300532 struct coda_ctx *ctx = fh_to_ctx(priv);
533
Javier Martin186b2502012-07-26 05:53:35 -0300534 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300535 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
536 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300537 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300538 /*
539 * This is only a mem-to-mem video device. The capture and output
540 * device capability flags are left only for backward compatibility
541 * and are scheduled for removal.
542 */
543 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
544 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300545 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
546
547 return 0;
548}
549
550static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300551 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300552{
553 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300554 struct coda_codec *codecs = ctx->dev->devtype->codecs;
555 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300556 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300557 int num_codecs = ctx->dev->devtype->num_codecs;
558 int num_formats = ARRAY_SIZE(coda_formats);
559 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300560
561 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300562 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300563 if (coda_format_is_yuv(formats[i].fourcc) &&
564 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300565 if (num == f->index)
566 break;
567 ++num;
568 continue;
569 }
570 /* Compressed formats may be supported, check the codec list */
571 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300572 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300573 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300574 formats[i].fourcc == codecs[k].dst_fourcc &&
575 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300576 break;
577 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
578 formats[i].fourcc == codecs[k].src_fourcc)
579 break;
580 }
581 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300582 if (num == f->index)
583 break;
584 ++num;
585 }
586 }
587
588 if (i < num_formats) {
589 fmt = &formats[i];
590 strlcpy(f->description, fmt->name, sizeof(f->description));
591 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300592 if (!coda_format_is_yuv(fmt->fourcc))
593 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300594 return 0;
595 }
596
597 /* Format not found */
598 return -EINVAL;
599}
600
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300601static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
602 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300603{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300604 struct coda_ctx *ctx = fh_to_ctx(priv);
605 struct vb2_queue *src_vq;
606 struct coda_q_data *q_data_src;
607
608 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300609 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300610 if (vb2_is_streaming(src_vq)) {
611 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
612
613 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
614 q_data_src->fourcc);
615 }
616
617 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300618}
619
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300620static int coda_enum_fmt_vid_out(struct file *file, void *priv,
621 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300622{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300623 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300624}
625
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300626static int coda_g_fmt(struct file *file, void *priv,
627 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300628{
Javier Martin186b2502012-07-26 05:53:35 -0300629 struct coda_q_data *q_data;
630 struct coda_ctx *ctx = fh_to_ctx(priv);
631
Javier Martin186b2502012-07-26 05:53:35 -0300632 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300633 if (!q_data)
634 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300635
636 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300637 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300638 f->fmt.pix.width = q_data->width;
639 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300640 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300641
642 f->fmt.pix.sizeimage = q_data->sizeimage;
643 f->fmt.pix.colorspace = ctx->colorspace;
644
645 return 0;
646}
647
Philipp Zabel57625592013-09-30 10:34:51 -0300648static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
649 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300650{
Philipp Zabel57625592013-09-30 10:34:51 -0300651 struct coda_dev *dev = ctx->dev;
652 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300653 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300654 enum v4l2_field field;
655
656 field = f->fmt.pix.field;
657 if (field == V4L2_FIELD_ANY)
658 field = V4L2_FIELD_NONE;
659 else if (V4L2_FIELD_NONE != field)
660 return -EINVAL;
661
662 /* V4L2 specification suggests the driver corrects the format struct
663 * if any of the dimensions is unsupported */
664 f->fmt.pix.field = field;
665
Philipp Zabel57625592013-09-30 10:34:51 -0300666 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
667 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
668 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
669 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300670
Philipp Zabel57625592013-09-30 10:34:51 -0300671 switch (f->fmt.pix.pixelformat) {
672 case V4L2_PIX_FMT_YUV420:
673 case V4L2_PIX_FMT_YVU420:
674 case V4L2_PIX_FMT_H264:
675 case V4L2_PIX_FMT_MPEG4:
676 case V4L2_PIX_FMT_JPEG:
677 break;
678 default:
679 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300680 if (!q_data)
681 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300682 f->fmt.pix.pixelformat = q_data->fourcc;
683 }
684
685 switch (f->fmt.pix.pixelformat) {
686 case V4L2_PIX_FMT_YUV420:
687 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300688 /* Frame stride must be multiple of 8, but 16 for h.264 */
689 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300690 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300691 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300692 break;
693 case V4L2_PIX_FMT_H264:
694 case V4L2_PIX_FMT_MPEG4:
695 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300696 f->fmt.pix.bytesperline = 0;
697 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300698 break;
699 default:
700 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300701 }
702
703 return 0;
704}
705
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300706static int coda_try_fmt_vid_cap(struct file *file, void *priv,
707 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300708{
Javier Martin186b2502012-07-26 05:53:35 -0300709 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300710 struct coda_codec *codec;
711 struct vb2_queue *src_vq;
712 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300713
Philipp Zabel918c66f2013-06-27 06:59:01 -0300714 /*
715 * If the source format is already fixed, try to find a codec that
716 * converts to the given destination format
717 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300718 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300719 if (vb2_is_streaming(src_vq)) {
720 struct coda_q_data *q_data_src;
721
722 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
723 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
724 f->fmt.pix.pixelformat);
725 if (!codec)
726 return -EINVAL;
727 } else {
728 /* Otherwise determine codec by encoded format, if possible */
729 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
730 f->fmt.pix.pixelformat);
731 }
Javier Martin186b2502012-07-26 05:53:35 -0300732
733 f->fmt.pix.colorspace = ctx->colorspace;
734
Philipp Zabel57625592013-09-30 10:34:51 -0300735 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300736 if (ret < 0)
737 return ret;
738
739 /* The h.264 decoder only returns complete 16x16 macroblocks */
740 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300741 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300742 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300743 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300744 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
745 f->fmt.pix.height * 3 / 2;
746 }
747
748 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300749}
750
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300751static int coda_try_fmt_vid_out(struct file *file, void *priv,
752 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300753{
754 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300755 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300756
Philipp Zabelb96904e2013-05-23 10:42:58 -0300757 /* Determine codec by encoded format, returns NULL if raw or invalid */
758 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
759 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300760
761 if (!f->fmt.pix.colorspace)
762 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
763
Philipp Zabel57625592013-09-30 10:34:51 -0300764 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300765}
766
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300767static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300768{
769 struct coda_q_data *q_data;
770 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300771
Philipp Zabel14604e32014-07-11 06:36:22 -0300772 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300773 if (!vq)
774 return -EINVAL;
775
776 q_data = get_q_data(ctx, f->type);
777 if (!q_data)
778 return -EINVAL;
779
780 if (vb2_is_busy(vq)) {
781 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
782 return -EBUSY;
783 }
784
Philipp Zabelb96904e2013-05-23 10:42:58 -0300785 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300786 q_data->width = f->fmt.pix.width;
787 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300788 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300789 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300790 q_data->rect.left = 0;
791 q_data->rect.top = 0;
792 q_data->rect.width = f->fmt.pix.width;
793 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300794
795 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
796 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300797 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300798
799 return 0;
800}
801
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300802static int coda_s_fmt_vid_cap(struct file *file, void *priv,
803 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300804{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300805 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300806 int ret;
807
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300808 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300809 if (ret)
810 return ret;
811
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300812 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300813}
814
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300815static int coda_s_fmt_vid_out(struct file *file, void *priv,
816 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300817{
818 struct coda_ctx *ctx = fh_to_ctx(priv);
819 int ret;
820
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300821 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300822 if (ret)
823 return ret;
824
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300825 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300826 if (ret)
827 ctx->colorspace = f->fmt.pix.colorspace;
828
829 return ret;
830}
831
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300832static int coda_qbuf(struct file *file, void *priv,
833 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300834{
835 struct coda_ctx *ctx = fh_to_ctx(priv);
836
Philipp Zabel14604e32014-07-11 06:36:22 -0300837 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300838}
839
Philipp Zabel918c66f2013-06-27 06:59:01 -0300840static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
841 struct v4l2_buffer *buf)
842{
843 struct vb2_queue *src_vq;
844
Philipp Zabel14604e32014-07-11 06:36:22 -0300845 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300846
847 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
848 (buf->sequence == (ctx->qsequence - 1)));
849}
850
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300851static int coda_dqbuf(struct file *file, void *priv,
852 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300853{
854 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300855 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300856
Philipp Zabel14604e32014-07-11 06:36:22 -0300857 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300858
859 /* If this is the last capture buffer, emit an end-of-stream event */
860 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
861 coda_buf_is_end_of_stream(ctx, buf)) {
862 const struct v4l2_event eos_event = {
863 .type = V4L2_EVENT_EOS
864 };
865
866 v4l2_event_queue_fh(&ctx->fh, &eos_event);
867 }
868
869 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300870}
871
Philipp Zabel52c41672014-07-11 06:36:19 -0300872static int coda_g_selection(struct file *file, void *fh,
873 struct v4l2_selection *s)
874{
875 struct coda_ctx *ctx = fh_to_ctx(fh);
876 struct coda_q_data *q_data;
877 struct v4l2_rect r, *rsel;
878
879 q_data = get_q_data(ctx, s->type);
880 if (!q_data)
881 return -EINVAL;
882
883 r.left = 0;
884 r.top = 0;
885 r.width = q_data->width;
886 r.height = q_data->height;
887 rsel = &q_data->rect;
888
889 switch (s->target) {
890 case V4L2_SEL_TGT_CROP_DEFAULT:
891 case V4L2_SEL_TGT_CROP_BOUNDS:
892 rsel = &r;
893 /* fallthrough */
894 case V4L2_SEL_TGT_CROP:
895 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
896 return -EINVAL;
897 break;
898 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
899 case V4L2_SEL_TGT_COMPOSE_PADDED:
900 rsel = &r;
901 /* fallthrough */
902 case V4L2_SEL_TGT_COMPOSE:
903 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
904 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
905 return -EINVAL;
906 break;
907 default:
908 return -EINVAL;
909 }
910
911 s->r = *rsel;
912
913 return 0;
914}
915
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300916static int coda_try_decoder_cmd(struct file *file, void *fh,
917 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300918{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300919 if (dc->cmd != V4L2_DEC_CMD_STOP)
920 return -EINVAL;
921
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300922 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300923 return -EINVAL;
924
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300925 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300926 return -EINVAL;
927
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300928 return 0;
929}
930
931static int coda_decoder_cmd(struct file *file, void *fh,
932 struct v4l2_decoder_cmd *dc)
933{
934 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300935 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300936 int ret;
937
938 ret = coda_try_decoder_cmd(file, fh, dc);
939 if (ret < 0)
940 return ret;
941
942 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300943 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300944 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300945
946 /* Set the strem-end flag on this context */
947 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
948
Philipp Zabel89548442014-07-11 06:36:17 -0300949 if ((dev->devtype->product == CODA_960) &&
950 coda_isbusy(dev) &&
951 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
952 /* If this context is currently running, update the hardware flag */
953 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
954 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300955 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300956 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300957
Philipp Zabel918c66f2013-06-27 06:59:01 -0300958 return 0;
959}
960
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300961static int coda_subscribe_event(struct v4l2_fh *fh,
962 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300963{
964 switch (sub->type) {
965 case V4L2_EVENT_EOS:
966 return v4l2_event_subscribe(fh, sub, 0, NULL);
967 default:
968 return v4l2_ctrl_subscribe_event(fh, sub);
969 }
Javier Martin186b2502012-07-26 05:53:35 -0300970}
971
972static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300973 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300974
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300975 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
976 .vidioc_g_fmt_vid_cap = coda_g_fmt,
977 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
978 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300979
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300980 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
981 .vidioc_g_fmt_vid_out = coda_g_fmt,
982 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
983 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300984
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300985 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
986 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300987
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300988 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300989 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300990 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300991 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300992
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300993 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
994 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300995
Philipp Zabel52c41672014-07-11 06:36:19 -0300996 .vidioc_g_selection = coda_g_selection,
997
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300998 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300999 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001000
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001001 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001002 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001003};
1004
Philipp Zabel918c66f2013-06-27 06:59:01 -03001005static int coda_start_decoding(struct coda_ctx *ctx);
1006
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001007static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
1008{
1009 return kfifo_len(&ctx->bitstream_fifo);
1010}
1011
1012static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
1013{
1014 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1015 struct coda_dev *dev = ctx->dev;
1016 u32 rd_ptr;
1017
1018 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1019 kfifo->out = (kfifo->in & ~kfifo->mask) |
1020 (rd_ptr - ctx->bitstream.paddr);
1021 if (kfifo->out > kfifo->in)
1022 kfifo->out -= kfifo->mask + 1;
1023}
1024
1025static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1026{
1027 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1028 struct coda_dev *dev = ctx->dev;
1029 u32 rd_ptr, wr_ptr;
1030
1031 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1032 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1033 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1034 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1035}
1036
1037static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1038{
1039 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1040 struct coda_dev *dev = ctx->dev;
1041 u32 wr_ptr;
1042
1043 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1044 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1045}
1046
1047static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1048{
1049 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1050 u32 n;
1051
1052 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1053 if (n < src_size)
1054 return -ENOSPC;
1055
1056 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1057 ctx->bitstream.size, DMA_TO_DEVICE);
1058
Philipp Zabel846ced92014-07-11 06:36:31 -03001059 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001060
1061 return 0;
1062}
1063
1064static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1065 struct vb2_buffer *src_buf)
1066{
1067 int ret;
1068
1069 if (coda_get_bitstream_payload(ctx) +
1070 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1071 return false;
1072
1073 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1074 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1075 return true;
1076 }
1077
1078 ret = coda_bitstream_queue(ctx, src_buf);
1079 if (ret < 0) {
1080 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1081 return false;
1082 }
1083 /* Sync read pointer to device */
1084 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1085 coda_kfifo_sync_to_device_write(ctx);
1086
Philipp Zabel84e23652014-07-11 06:36:34 -03001087 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001088
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001089 return true;
1090}
1091
1092static void coda_fill_bitstream(struct coda_ctx *ctx)
1093{
1094 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001095 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001096
Philipp Zabel14604e32014-07-11 06:36:22 -03001097 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1098 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001099
1100 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001101 /*
1102 * Source buffer is queued in the bitstream ringbuffer;
1103 * queue the timestamp and mark source buffer as done
1104 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001105 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001106
1107 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1108 if (ts) {
1109 ts->sequence = src_buf->v4l2_buf.sequence;
1110 ts->timecode = src_buf->v4l2_buf.timecode;
1111 ts->timestamp = src_buf->v4l2_buf.timestamp;
1112 list_add_tail(&ts->list, &ctx->timestamp_list);
1113 }
1114
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001115 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1116 } else {
1117 break;
1118 }
1119 }
1120}
1121
Philipp Zabel89548442014-07-11 06:36:17 -03001122static void coda_set_gdi_regs(struct coda_ctx *ctx)
1123{
1124 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1125 struct coda_dev *dev = ctx->dev;
1126 int i;
1127
1128 for (i = 0; i < 16; i++)
1129 coda_write(dev, tiled_map->xy2ca_map[i],
1130 CODA9_GDI_XY2_CAS_0 + 4 * i);
1131 for (i = 0; i < 4; i++)
1132 coda_write(dev, tiled_map->xy2ba_map[i],
1133 CODA9_GDI_XY2_BA_0 + 4 * i);
1134 for (i = 0; i < 16; i++)
1135 coda_write(dev, tiled_map->xy2ra_map[i],
1136 CODA9_GDI_XY2_RAS_0 + 4 * i);
1137 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1138 for (i = 0; i < 32; i++)
1139 coda_write(dev, tiled_map->rbc2axi_map[i],
1140 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1141}
1142
Javier Martin186b2502012-07-26 05:53:35 -03001143/*
1144 * Mem-to-mem operations.
1145 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001146static int coda_prepare_decode(struct coda_ctx *ctx)
1147{
1148 struct vb2_buffer *dst_buf;
1149 struct coda_dev *dev = ctx->dev;
1150 struct coda_q_data *q_data_dst;
1151 u32 stridey, height;
1152 u32 picture_y, picture_cb, picture_cr;
1153
Philipp Zabel14604e32014-07-11 06:36:22 -03001154 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001155 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1156
1157 if (ctx->params.rot_mode & CODA_ROT_90) {
1158 stridey = q_data_dst->height;
1159 height = q_data_dst->width;
1160 } else {
1161 stridey = q_data_dst->width;
1162 height = q_data_dst->height;
1163 }
1164
1165 /* Try to copy source buffer contents into the bitstream ringbuffer */
1166 mutex_lock(&ctx->bitstream_mutex);
1167 coda_fill_bitstream(ctx);
1168 mutex_unlock(&ctx->bitstream_mutex);
1169
1170 if (coda_get_bitstream_payload(ctx) < 512 &&
1171 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1172 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1173 "bitstream payload: %d, skipping\n",
1174 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001175 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001176 return -EAGAIN;
1177 }
1178
1179 /* Run coda_start_decoding (again) if not yet initialized */
1180 if (!ctx->initialized) {
1181 int ret = coda_start_decoding(ctx);
1182 if (ret < 0) {
1183 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001184 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001185 return -EAGAIN;
1186 } else {
1187 ctx->initialized = 1;
1188 }
1189 }
1190
Philipp Zabel89548442014-07-11 06:36:17 -03001191 if (dev->devtype->product == CODA_960)
1192 coda_set_gdi_regs(ctx);
1193
Philipp Zabel918c66f2013-06-27 06:59:01 -03001194 /* Set rotator output */
1195 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1196 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1197 /* Switch Cr and Cb for YVU420 format */
1198 picture_cr = picture_y + stridey * height;
1199 picture_cb = picture_cr + stridey / 2 * height / 2;
1200 } else {
1201 picture_cb = picture_y + stridey * height;
1202 picture_cr = picture_cb + stridey / 2 * height / 2;
1203 }
Philipp Zabel89548442014-07-11 06:36:17 -03001204
1205 if (dev->devtype->product == CODA_960) {
1206 /*
1207 * The CODA960 seems to have an internal list of buffers with
1208 * 64 entries that includes the registered frame buffers as
1209 * well as the rotator buffer output.
1210 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1211 */
1212 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1213 CODA9_CMD_DEC_PIC_ROT_INDEX);
1214 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1215 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1216 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1217 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1218 } else {
1219 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1220 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1221 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1222 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1223 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001224 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1225 CODA_CMD_DEC_PIC_ROT_MODE);
1226
1227 switch (dev->devtype->product) {
1228 case CODA_DX6:
1229 /* TBD */
1230 case CODA_7541:
1231 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1232 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001233 case CODA_960:
1234 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1235 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001236 }
1237
1238 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1239
1240 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1241 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1242
1243 return 0;
1244}
1245
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001246static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001247{
Javier Martin186b2502012-07-26 05:53:35 -03001248 struct coda_q_data *q_data_src, *q_data_dst;
1249 struct vb2_buffer *src_buf, *dst_buf;
1250 struct coda_dev *dev = ctx->dev;
1251 int force_ipicture;
1252 int quant_param = 0;
1253 u32 picture_y, picture_cb, picture_cr;
1254 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1255 u32 dst_fourcc;
1256
Philipp Zabel14604e32014-07-11 06:36:22 -03001257 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1258 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001259 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1260 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001261 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001262
Philipp Zabel918c66f2013-06-27 06:59:01 -03001263 src_buf->v4l2_buf.sequence = ctx->osequence;
1264 dst_buf->v4l2_buf.sequence = ctx->osequence;
1265 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001266
1267 /*
1268 * Workaround coda firmware BUG that only marks the first
1269 * frame as IDR. This is a problem for some decoders that can't
1270 * recover when a frame is lost.
1271 */
1272 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1273 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1274 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1275 } else {
1276 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1277 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1278 }
1279
Philipp Zabel89548442014-07-11 06:36:17 -03001280 if (dev->devtype->product == CODA_960)
1281 coda_set_gdi_regs(ctx);
1282
Javier Martin186b2502012-07-26 05:53:35 -03001283 /*
1284 * Copy headers at the beginning of the first frame for H.264 only.
1285 * In MPEG4 they are already copied by the coda.
1286 */
1287 if (src_buf->v4l2_buf.sequence == 0) {
1288 pic_stream_buffer_addr =
1289 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1290 ctx->vpu_header_size[0] +
1291 ctx->vpu_header_size[1] +
1292 ctx->vpu_header_size[2];
1293 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1294 ctx->vpu_header_size[0] -
1295 ctx->vpu_header_size[1] -
1296 ctx->vpu_header_size[2];
1297 memcpy(vb2_plane_vaddr(dst_buf, 0),
1298 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1299 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1300 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1301 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1302 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1303 ctx->vpu_header_size[2]);
1304 } else {
1305 pic_stream_buffer_addr =
1306 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1307 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1308 }
1309
1310 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1311 force_ipicture = 1;
1312 switch (dst_fourcc) {
1313 case V4L2_PIX_FMT_H264:
1314 quant_param = ctx->params.h264_intra_qp;
1315 break;
1316 case V4L2_PIX_FMT_MPEG4:
1317 quant_param = ctx->params.mpeg4_intra_qp;
1318 break;
1319 default:
1320 v4l2_warn(&ctx->dev->v4l2_dev,
1321 "cannot set intra qp, fmt not supported\n");
1322 break;
1323 }
1324 } else {
1325 force_ipicture = 0;
1326 switch (dst_fourcc) {
1327 case V4L2_PIX_FMT_H264:
1328 quant_param = ctx->params.h264_inter_qp;
1329 break;
1330 case V4L2_PIX_FMT_MPEG4:
1331 quant_param = ctx->params.mpeg4_inter_qp;
1332 break;
1333 default:
1334 v4l2_warn(&ctx->dev->v4l2_dev,
1335 "cannot set inter qp, fmt not supported\n");
1336 break;
1337 }
1338 }
1339
1340 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001341 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001342 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1343
1344
1345 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001346 switch (q_data_src->fourcc) {
1347 case V4L2_PIX_FMT_YVU420:
1348 /* Switch Cb and Cr for YVU420 format */
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001349 picture_cr = picture_y + q_data_src->bytesperline *
1350 q_data_src->height;
1351 picture_cb = picture_cr + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001352 q_data_src->height / 2;
1353 break;
1354 case V4L2_PIX_FMT_YUV420:
1355 default:
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001356 picture_cb = picture_y + q_data_src->bytesperline *
1357 q_data_src->height;
1358 picture_cr = picture_cb + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001359 q_data_src->height / 2;
1360 break;
1361 }
Javier Martin186b2502012-07-26 05:53:35 -03001362
Philipp Zabel89548442014-07-11 06:36:17 -03001363 if (dev->devtype->product == CODA_960) {
1364 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1365 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1366 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1367
1368 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1369 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1370 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1371 } else {
1372 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1373 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1374 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1375 }
Javier Martin186b2502012-07-26 05:53:35 -03001376 coda_write(dev, force_ipicture << 1 & 0x2,
1377 CODA_CMD_ENC_PIC_OPTION);
1378
1379 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1380 coda_write(dev, pic_stream_buffer_size / 1024,
1381 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001382
1383 if (!ctx->streamon_out) {
1384 /* After streamoff on the output side, set the stream end flag */
1385 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1386 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1387 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001388}
1389
1390static void coda_device_run(void *m2m_priv)
1391{
1392 struct coda_ctx *ctx = m2m_priv;
1393 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001394
1395 queue_work(dev->workqueue, &ctx->pic_run_work);
1396}
1397
1398static void coda_free_framebuffers(struct coda_ctx *ctx);
1399static void coda_free_context_buffers(struct coda_ctx *ctx);
1400
1401static void coda_seq_end_work(struct work_struct *work)
1402{
1403 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1404 struct coda_dev *dev = ctx->dev;
1405
1406 mutex_lock(&ctx->buffer_mutex);
1407 mutex_lock(&dev->coda_mutex);
1408
1409 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1410 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1411 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1412 v4l2_err(&dev->v4l2_dev,
1413 "CODA_COMMAND_SEQ_END failed\n");
1414 }
1415
1416 kfifo_init(&ctx->bitstream_fifo,
1417 ctx->bitstream.vaddr, ctx->bitstream.size);
1418
1419 coda_free_framebuffers(ctx);
1420 coda_free_context_buffers(ctx);
1421
1422 mutex_unlock(&dev->coda_mutex);
1423 mutex_unlock(&ctx->buffer_mutex);
1424}
1425
1426static void coda_finish_decode(struct coda_ctx *ctx);
1427static void coda_finish_encode(struct coda_ctx *ctx);
1428
1429static void coda_pic_run_work(struct work_struct *work)
1430{
1431 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1432 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001433 int ret;
1434
1435 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001436 mutex_lock(&dev->coda_mutex);
1437
Philipp Zabel918c66f2013-06-27 06:59:01 -03001438 if (ctx->inst_type == CODA_INST_DECODER) {
1439 ret = coda_prepare_decode(ctx);
1440 if (ret < 0) {
1441 mutex_unlock(&dev->coda_mutex);
1442 mutex_unlock(&ctx->buffer_mutex);
1443 /* job_finish scheduled by prepare_decode */
1444 return;
1445 }
1446 } else {
1447 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001448 }
1449
Philipp Zabelc2d22512013-06-21 03:55:28 -03001450 if (dev->devtype->product != CODA_DX6)
1451 coda_write(dev, ctx->iram_info.axi_sram_use,
1452 CODA7_REG_BIT_AXI_SRAM_USE);
1453
Philipp Zabel918c66f2013-06-27 06:59:01 -03001454 if (ctx->inst_type == CODA_INST_DECODER)
1455 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001456 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001457
1458 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1459 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001460
1461 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001462
1463 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001464 } else if (!ctx->aborting) {
1465 if (ctx->inst_type == CODA_INST_DECODER)
1466 coda_finish_decode(ctx);
1467 else
1468 coda_finish_encode(ctx);
1469 }
1470
1471 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1472 queue_work(dev->workqueue, &ctx->seq_end_work);
1473
1474 mutex_unlock(&dev->coda_mutex);
1475 mutex_unlock(&ctx->buffer_mutex);
1476
Philipp Zabel14604e32014-07-11 06:36:22 -03001477 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001478}
1479
1480static int coda_job_ready(void *m2m_priv)
1481{
1482 struct coda_ctx *ctx = m2m_priv;
1483
1484 /*
1485 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001486 * and 1 frame are needed. In the decoder case,
1487 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001488 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001489 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001490 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001491 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1492 "not ready: not enough video buffers.\n");
1493 return 0;
1494 }
1495
Philipp Zabel14604e32014-07-11 06:36:22 -03001496 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001497 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1498 "not ready: not enough video capture buffers.\n");
1499 return 0;
1500 }
1501
Philipp Zabel84e23652014-07-11 06:36:34 -03001502 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001503 ((ctx->inst_type == CODA_INST_DECODER) &&
1504 (coda_get_bitstream_payload(ctx) < 512) &&
1505 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1506 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1507 "%d: not ready: not enough bitstream data.\n",
1508 ctx->idx);
1509 return 0;
1510 }
1511
Philipp Zabel3e7482682013-05-23 10:43:01 -03001512 if (ctx->aborting) {
1513 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1514 "not ready: aborting\n");
1515 return 0;
1516 }
1517
Javier Martin186b2502012-07-26 05:53:35 -03001518 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1519 "job ready\n");
1520 return 1;
1521}
1522
1523static void coda_job_abort(void *priv)
1524{
1525 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001526
1527 ctx->aborting = 1;
1528
1529 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1530 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001531}
1532
1533static void coda_lock(void *m2m_priv)
1534{
1535 struct coda_ctx *ctx = m2m_priv;
1536 struct coda_dev *pcdev = ctx->dev;
1537 mutex_lock(&pcdev->dev_mutex);
1538}
1539
1540static void coda_unlock(void *m2m_priv)
1541{
1542 struct coda_ctx *ctx = m2m_priv;
1543 struct coda_dev *pcdev = ctx->dev;
1544 mutex_unlock(&pcdev->dev_mutex);
1545}
1546
1547static struct v4l2_m2m_ops coda_m2m_ops = {
1548 .device_run = coda_device_run,
1549 .job_ready = coda_job_ready,
1550 .job_abort = coda_job_abort,
1551 .lock = coda_lock,
1552 .unlock = coda_unlock,
1553};
1554
Philipp Zabel89548442014-07-11 06:36:17 -03001555static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1556{
1557 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1558 int luma_map, chro_map, i;
1559
1560 memset(tiled_map, 0, sizeof(*tiled_map));
1561
1562 luma_map = 64;
1563 chro_map = 64;
1564 tiled_map->map_type = tiled_map_type;
1565 for (i = 0; i < 16; i++)
1566 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1567 for (i = 0; i < 4; i++)
1568 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1569 for (i = 0; i < 16; i++)
1570 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1571
1572 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1573 tiled_map->xy2rbc_config = 0;
1574 } else {
1575 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1576 tiled_map_type);
1577 return;
1578 }
1579}
1580
Javier Martin186b2502012-07-26 05:53:35 -03001581static void set_default_params(struct coda_ctx *ctx)
1582{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001583 int max_w;
1584 int max_h;
1585
1586 ctx->codec = &ctx->dev->devtype->codecs[0];
1587 max_w = ctx->codec->max_w;
1588 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001589
1590 ctx->params.codec_mode = CODA_MODE_INVALID;
1591 ctx->colorspace = V4L2_COLORSPACE_REC709;
1592 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001593 ctx->aborting = 0;
1594
1595 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001596 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1597 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1598 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1599 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001600 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001601 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1602 ctx->q_data[V4L2_M2M_DST].width = max_w;
1603 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001604 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001605 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001606 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1607 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1608 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1609 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001610
1611 if (ctx->dev->devtype->product == CODA_960)
1612 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001613}
1614
1615/*
1616 * Queue operations
1617 */
1618static int coda_queue_setup(struct vb2_queue *vq,
1619 const struct v4l2_format *fmt,
1620 unsigned int *nbuffers, unsigned int *nplanes,
1621 unsigned int sizes[], void *alloc_ctxs[])
1622{
1623 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001624 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001625 unsigned int size;
1626
Philipp Zabele34db062012-08-29 08:22:00 -03001627 q_data = get_q_data(ctx, vq->type);
1628 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001629
1630 *nplanes = 1;
1631 sizes[0] = size;
1632
1633 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1634
1635 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1636 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1637
1638 return 0;
1639}
1640
1641static int coda_buf_prepare(struct vb2_buffer *vb)
1642{
1643 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1644 struct coda_q_data *q_data;
1645
1646 q_data = get_q_data(ctx, vb->vb2_queue->type);
1647
1648 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1649 v4l2_warn(&ctx->dev->v4l2_dev,
1650 "%s data will not fit into plane (%lu < %lu)\n",
1651 __func__, vb2_plane_size(vb, 0),
1652 (long)q_data->sizeimage);
1653 return -EINVAL;
1654 }
1655
Javier Martin186b2502012-07-26 05:53:35 -03001656 return 0;
1657}
1658
1659static void coda_buf_queue(struct vb2_buffer *vb)
1660{
1661 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001662 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001663 struct coda_q_data *q_data;
1664
1665 q_data = get_q_data(ctx, vb->vb2_queue->type);
1666
1667 /*
1668 * In the decoder case, immediately try to copy the buffer into the
1669 * bitstream ringbuffer and mark it as ready to be dequeued.
1670 */
1671 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1672 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1673 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001674 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001675 * the stream end
1676 */
Philipp Zabel89548442014-07-11 06:36:17 -03001677 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001678 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001679 if ((dev->devtype->product == CODA_960) &&
1680 coda_isbusy(dev) &&
1681 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1682 /* if this decoder instance is running, set the stream end flag */
1683 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1684 }
1685 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001686 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001687 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001688 coda_fill_bitstream(ctx);
1689 mutex_unlock(&ctx->bitstream_mutex);
1690 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001691 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001692 }
Javier Martin186b2502012-07-26 05:53:35 -03001693}
1694
Philipp Zabel86eda902013-05-23 10:42:57 -03001695static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1696{
1697 struct coda_dev *dev = ctx->dev;
1698 u32 *p = ctx->parabuf.vaddr;
1699
1700 if (dev->devtype->product == CODA_DX6)
1701 p[index] = value;
1702 else
1703 p[index ^ 1] = value;
1704}
1705
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001706static int coda_alloc_aux_buf(struct coda_dev *dev,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001707 struct coda_aux_buf *buf, size_t size,
1708 const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001709{
1710 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1711 GFP_KERNEL);
1712 if (!buf->vaddr)
1713 return -ENOMEM;
1714
1715 buf->size = size;
1716
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001717 if (name && parent) {
1718 buf->blob.data = buf->vaddr;
1719 buf->blob.size = size;
1720 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
1721 if (!buf->dentry)
1722 dev_warn(&dev->plat_dev->dev,
1723 "failed to create debugfs entry %s\n", name);
1724 }
1725
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001726 return 0;
1727}
1728
1729static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001730 struct coda_aux_buf *buf, size_t size,
1731 const char *name)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001732{
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001733 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001734}
1735
1736static void coda_free_aux_buf(struct coda_dev *dev,
1737 struct coda_aux_buf *buf)
1738{
1739 if (buf->vaddr) {
1740 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1741 buf->vaddr, buf->paddr);
1742 buf->vaddr = NULL;
1743 buf->size = 0;
1744 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001745 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001746}
1747
1748static void coda_free_framebuffers(struct coda_ctx *ctx)
1749{
1750 int i;
1751
1752 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1753 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1754}
1755
Philipp Zabelec25f682012-07-20 08:54:29 -03001756static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1757{
1758 struct coda_dev *dev = ctx->dev;
Philipp Zabel7c3df782014-07-11 06:36:38 -03001759 int width, height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001760 dma_addr_t paddr;
1761 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001762 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001763 int i;
1764
Philipp Zabel7c3df782014-07-11 06:36:38 -03001765 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
1766 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
1767 width = round_up(q_data->width, 16);
1768 height = round_up(q_data->height, 16);
1769 } else {
1770 width = round_up(q_data->width, 8);
1771 height = q_data->height;
1772 }
1773 ysize = width * height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001774
Philipp Zabelec25f682012-07-20 08:54:29 -03001775 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001776 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001777 size_t size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001778 char *name;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001779
Philipp Zabelaed14b02014-07-11 06:36:15 -03001780 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001781 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1782 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001783 size += ysize / 4;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001784 name = kasprintf(GFP_KERNEL, "fb%d", i);
1785 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
1786 size, name);
1787 kfree(name);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001788 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001789 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001790 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001791 }
1792 }
1793
1794 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001795 for (i = 0; i < ctx->num_internal_frames; i++) {
1796 paddr = ctx->internal_frames[i].paddr;
1797 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1798 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1799 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001800
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001801 /* mvcol buffer for h.264 */
1802 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1803 dev->devtype->product != CODA_DX6)
1804 coda_parabuf_write(ctx, 96 + i,
1805 ctx->internal_frames[i].paddr +
1806 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001807 }
1808
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001809 /* mvcol buffer for mpeg4 */
1810 if ((dev->devtype->product != CODA_DX6) &&
1811 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1812 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1813 ysize + ysize/4 + ysize/4);
1814
Philipp Zabelec25f682012-07-20 08:54:29 -03001815 return 0;
1816}
1817
Javier Martin3f3f5c72012-10-29 05:20:29 -03001818static int coda_h264_padding(int size, char *p)
1819{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001820 int nal_size;
1821 int diff;
1822
Javier Martin832fbb52012-10-29 08:34:59 -03001823 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001824 if (diff == 0)
1825 return 0;
1826
Javier Martin832fbb52012-10-29 08:34:59 -03001827 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001828 memcpy(p, coda_filler_nal, nal_size);
1829
1830 /* Add rbsp stop bit and trailing at the end */
1831 *(p + nal_size - 1) = 0x80;
1832
1833 return nal_size;
1834}
1835
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001836static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1837{
1838 phys_addr_t ret;
1839
1840 size = round_up(size, 1024);
1841 if (size > iram->remaining)
1842 return 0;
1843 iram->remaining -= size;
1844
1845 ret = iram->next_paddr;
1846 iram->next_paddr += size;
1847
1848 return ret;
1849}
1850
Philipp Zabelc2d22512013-06-21 03:55:28 -03001851static void coda_setup_iram(struct coda_ctx *ctx)
1852{
1853 struct coda_iram_info *iram_info = &ctx->iram_info;
1854 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001855 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001856 int dbk_bits;
1857 int bit_bits;
1858 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001859
1860 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001861 iram_info->next_paddr = dev->iram.paddr;
1862 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001863
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001864 switch (dev->devtype->product) {
1865 case CODA_7541:
1866 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1867 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1868 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1869 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001870 case CODA_960:
1871 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1872 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1873 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1874 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001875 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001876 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001877 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001878
1879 if (ctx->inst_type == CODA_INST_ENCODER) {
1880 struct coda_q_data *q_data_src;
1881
1882 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1883 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1884
1885 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001886 if (dev->devtype->product == CODA_7541) {
1887 iram_info->search_ram_size = round_up(mb_width * 16 *
1888 36 + 2048, 1024);
1889 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1890 iram_info->search_ram_size);
1891 if (!iram_info->search_ram_paddr) {
1892 pr_err("IRAM is smaller than the search ram size\n");
1893 goto out;
1894 }
1895 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1896 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001897 }
1898
1899 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001900 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1901 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1902 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001903 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001904 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001905
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001906 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1907 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001908 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001909 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001910
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001911 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1912 if (!iram_info->buf_ip_ac_dc_use)
1913 goto out;
1914 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001915
Philipp Zabel8358e762013-06-21 03:55:32 -03001916 /* OVL and BTP disabled for encoder */
1917 } else if (ctx->inst_type == CODA_INST_DECODER) {
1918 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001919
1920 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1921 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001922
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001923 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1924 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1925 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001926 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001927 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001928
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001929 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1930 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001931 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001932 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001933
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001934 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1935 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001936 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001937 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001938
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001939 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001940 }
1941
1942out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001943 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1944 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1945 "IRAM smaller than needed\n");
1946
1947 if (dev->devtype->product == CODA_7541) {
1948 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001949 if (ctx->inst_type == CODA_INST_DECODER) {
1950 /* fw 1.4.50 */
1951 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1952 CODA7_USE_IP_ENABLE);
1953 } else {
1954 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001955 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1956 CODA7_USE_HOST_DBK_ENABLE |
1957 CODA7_USE_IP_ENABLE |
1958 CODA7_USE_DBK_ENABLE);
1959 }
1960 }
1961}
1962
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001963static void coda_free_context_buffers(struct coda_ctx *ctx)
1964{
1965 struct coda_dev *dev = ctx->dev;
1966
Philipp Zabel918c66f2013-06-27 06:59:01 -03001967 coda_free_aux_buf(dev, &ctx->slicebuf);
1968 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001969 if (dev->devtype->product != CODA_DX6)
1970 coda_free_aux_buf(dev, &ctx->workbuf);
1971}
1972
1973static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1974 struct coda_q_data *q_data)
1975{
1976 struct coda_dev *dev = ctx->dev;
1977 size_t size;
1978 int ret;
1979
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001980 if (dev->devtype->product == CODA_DX6)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001981 return 0;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001982
Philipp Zabel918c66f2013-06-27 06:59:01 -03001983 if (ctx->psbuf.vaddr) {
1984 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1985 return -EBUSY;
1986 }
1987 if (ctx->slicebuf.vaddr) {
1988 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1989 return -EBUSY;
1990 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001991 if (ctx->workbuf.vaddr) {
1992 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1993 ret = -EBUSY;
1994 return -ENOMEM;
1995 }
1996
Philipp Zabel918c66f2013-06-27 06:59:01 -03001997 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1998 /* worst case slice size */
1999 size = (DIV_ROUND_UP(q_data->width, 16) *
2000 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002001 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, "slicebuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002002 if (ret < 0) {
2003 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
2004 ctx->slicebuf.size);
2005 return ret;
2006 }
2007 }
2008
2009 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002010 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002011 if (ret < 0) {
2012 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
2013 goto err;
2014 }
2015 }
2016
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002017 size = dev->devtype->workbuf_size;
2018 if (dev->devtype->product == CODA_960 &&
2019 q_data->fourcc == V4L2_PIX_FMT_H264)
2020 size += CODA9_PS_SAVE_SIZE;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002021 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002022 if (ret < 0) {
2023 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2024 ctx->workbuf.size);
2025 goto err;
2026 }
2027
2028 return 0;
2029
2030err:
2031 coda_free_context_buffers(ctx);
2032 return ret;
2033}
2034
Philipp Zabel918c66f2013-06-27 06:59:01 -03002035static int coda_start_decoding(struct coda_ctx *ctx)
2036{
2037 struct coda_q_data *q_data_src, *q_data_dst;
2038 u32 bitstream_buf, bitstream_size;
2039 struct coda_dev *dev = ctx->dev;
2040 int width, height;
2041 u32 src_fourcc;
2042 u32 val;
2043 int ret;
2044
2045 /* Start decoding */
2046 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2047 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2048 bitstream_buf = ctx->bitstream.paddr;
2049 bitstream_size = ctx->bitstream.size;
2050 src_fourcc = q_data_src->fourcc;
2051
2052 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2053
2054 /* Update coda bitstream read and write pointers from kfifo */
2055 coda_kfifo_sync_to_device_full(ctx);
2056
2057 ctx->display_idx = -1;
2058 ctx->frm_dis_flg = 0;
2059 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2060
2061 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2062 CODA_REG_BIT_BIT_STREAM_PARAM);
2063
2064 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2065 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2066 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002067 if ((dev->devtype->product == CODA_7541) ||
2068 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002069 val |= CODA_REORDER_ENABLE;
2070 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2071
2072 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002073 if (dev->devtype->product == CODA_960 &&
2074 src_fourcc == V4L2_PIX_FMT_MPEG4)
2075 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2076 else
2077 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002078 if (src_fourcc == V4L2_PIX_FMT_H264) {
2079 if (dev->devtype->product == CODA_7541) {
2080 coda_write(dev, ctx->psbuf.paddr,
2081 CODA_CMD_DEC_SEQ_PS_BB_START);
2082 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2083 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2084 }
Philipp Zabel89548442014-07-11 06:36:17 -03002085 if (dev->devtype->product == CODA_960) {
2086 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2087 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2088 }
2089 }
2090 if (dev->devtype->product != CODA_960) {
2091 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002092 }
2093
2094 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2095 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2096 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2097 return -ETIMEDOUT;
2098 }
2099
2100 /* Update kfifo out pointer from coda bitstream read pointer */
2101 coda_kfifo_sync_from_device(ctx);
2102
2103 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2104
2105 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2106 v4l2_err(&dev->v4l2_dev,
2107 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2108 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2109 return -EAGAIN;
2110 }
2111
2112 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2113 if (dev->devtype->product == CODA_DX6) {
2114 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2115 height = val & CODADX6_PICHEIGHT_MASK;
2116 } else {
2117 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2118 height = val & CODA7_PICHEIGHT_MASK;
2119 }
2120
2121 if (width > q_data_dst->width || height > q_data_dst->height) {
2122 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2123 width, height, q_data_dst->width, q_data_dst->height);
2124 return -EINVAL;
2125 }
2126
2127 width = round_up(width, 16);
2128 height = round_up(height, 16);
2129
2130 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2131 __func__, ctx->idx, width, height);
2132
Philipp Zabela500a932014-07-11 06:36:13 -03002133 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002134 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2135 v4l2_err(&dev->v4l2_dev,
2136 "not enough framebuffers to decode (%d < %d)\n",
2137 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2138 return -EINVAL;
2139 }
2140
Philipp Zabel52c41672014-07-11 06:36:19 -03002141 if (src_fourcc == V4L2_PIX_FMT_H264) {
2142 u32 left_right;
2143 u32 top_bottom;
2144
2145 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2146 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2147
2148 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2149 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2150 q_data_dst->rect.width = width - q_data_dst->rect.left -
2151 (left_right & 0x3ff);
2152 q_data_dst->rect.height = height - q_data_dst->rect.top -
2153 (top_bottom & 0x3ff);
2154 }
2155
Philipp Zabel918c66f2013-06-27 06:59:01 -03002156 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2157 if (ret < 0)
2158 return ret;
2159
2160 /* Tell the decoder how many frame buffers we allocated. */
2161 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2162 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2163
2164 if (dev->devtype->product != CODA_DX6) {
2165 /* Set secondary AXI IRAM */
2166 coda_setup_iram(ctx);
2167
2168 coda_write(dev, ctx->iram_info.buf_bit_use,
2169 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2170 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2171 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2172 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2173 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2174 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2175 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2176 coda_write(dev, ctx->iram_info.buf_ovl_use,
2177 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002178 if (dev->devtype->product == CODA_960)
2179 coda_write(dev, ctx->iram_info.buf_btp_use,
2180 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2181 }
2182
2183 if (dev->devtype->product == CODA_960) {
2184 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2185
2186 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2187 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2188 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2189 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2190 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2191 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002192 }
2193
2194 if (src_fourcc == V4L2_PIX_FMT_H264) {
2195 coda_write(dev, ctx->slicebuf.paddr,
2196 CODA_CMD_SET_FRAME_SLICE_BB_START);
2197 coda_write(dev, ctx->slicebuf.size / 1024,
2198 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2199 }
2200
2201 if (dev->devtype->product == CODA_7541) {
2202 int max_mb_x = 1920 / 16;
2203 int max_mb_y = 1088 / 16;
2204 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002205
Philipp Zabel918c66f2013-06-27 06:59:01 -03002206 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2207 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002208 } else if (dev->devtype->product == CODA_960) {
2209 int max_mb_x = 1920 / 16;
2210 int max_mb_y = 1088 / 16;
2211 int max_mb_num = max_mb_x * max_mb_y;
2212
2213 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2214 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002215 }
2216
2217 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2218 v4l2_err(&ctx->dev->v4l2_dev,
2219 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2220 return -ETIMEDOUT;
2221 }
2222
2223 return 0;
2224}
2225
Philipp Zabeld35167a2013-05-23 10:42:59 -03002226static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2227 int header_code, u8 *header, int *size)
2228{
2229 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002230 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002231 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002232 int i;
2233
2234 if (dev->devtype->product == CODA_960)
2235 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002236
2237 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2238 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002239 bufsize = vb2_plane_size(buf, 0);
2240 if (dev->devtype->product == CODA_960)
2241 bufsize /= 1024;
2242 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002243 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2244 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2245 if (ret < 0) {
2246 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2247 return ret;
2248 }
Philipp Zabel89548442014-07-11 06:36:17 -03002249
2250 if (dev->devtype->product == CODA_960) {
2251 for (i = 63; i > 0; i--)
2252 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2253 break;
2254 *size = i + 1;
2255 } else {
2256 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2257 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2258 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002259 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2260
2261 return 0;
2262}
2263
Philipp Zabel89548442014-07-11 06:36:17 -03002264static int coda_start_encoding(struct coda_ctx *ctx);
2265
Javier Martin186b2502012-07-26 05:53:35 -03002266static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2267{
2268 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2269 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002270 struct coda_dev *dev = ctx->dev;
2271 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002272 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002273 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002274
Philipp Zabelb96904e2013-05-23 10:42:58 -03002275 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002276 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2277 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2278 if (coda_get_bitstream_payload(ctx) < 512)
2279 return -EINVAL;
2280 } else {
2281 if (count < 1)
2282 return -EINVAL;
2283 }
2284
2285 ctx->streamon_out = 1;
2286
Philipp Zabelb96904e2013-05-23 10:42:58 -03002287 if (coda_format_is_yuv(q_data_src->fourcc))
2288 ctx->inst_type = CODA_INST_ENCODER;
2289 else
2290 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002291 } else {
2292 if (count < 1)
2293 return -EINVAL;
2294
2295 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002296 }
Javier Martin186b2502012-07-26 05:53:35 -03002297
Philipp Zabelb96904e2013-05-23 10:42:58 -03002298 /* Don't start the coda unless both queues are on */
2299 if (!(ctx->streamon_out & ctx->streamon_cap))
2300 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002301
Philipp Zabeleb107512013-09-30 10:34:45 -03002302 /* Allow decoder device_run with no new buffers queued */
2303 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002304 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002305
Philipp Zabelb96904e2013-05-23 10:42:58 -03002306 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002307 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002308 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002309
Philipp Zabelb96904e2013-05-23 10:42:58 -03002310 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2311 q_data_dst->fourcc);
2312 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002313 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2314 return -EINVAL;
2315 }
2316
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002317 /* Allocate per-instance buffers */
2318 ret = coda_alloc_context_buffers(ctx, q_data_src);
2319 if (ret < 0)
2320 return ret;
2321
Philipp Zabel918c66f2013-06-27 06:59:01 -03002322 if (ctx->inst_type == CODA_INST_DECODER) {
2323 mutex_lock(&dev->coda_mutex);
2324 ret = coda_start_decoding(ctx);
2325 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002326 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002327 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002328 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002329 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002330 } else {
2331 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002332 }
2333
Philipp Zabel89548442014-07-11 06:36:17 -03002334 ctx->initialized = 1;
2335 return ret;
2336}
2337
2338static int coda_start_encoding(struct coda_ctx *ctx)
2339{
2340 struct coda_dev *dev = ctx->dev;
2341 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2342 struct coda_q_data *q_data_src, *q_data_dst;
2343 u32 bitstream_buf, bitstream_size;
2344 struct vb2_buffer *buf;
2345 int gamma, ret, value;
2346 u32 dst_fourcc;
2347
2348 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2349 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2350 dst_fourcc = q_data_dst->fourcc;
2351
Philipp Zabel14604e32014-07-11 06:36:22 -03002352 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002353 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2354 bitstream_size = q_data_dst->sizeimage;
2355
Javier Martin186b2502012-07-26 05:53:35 -03002356 if (!coda_is_initialized(dev)) {
2357 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2358 return -EFAULT;
2359 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002360
2361 mutex_lock(&dev->coda_mutex);
2362
Javier Martin186b2502012-07-26 05:53:35 -03002363 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002364 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2365 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002366 switch (dev->devtype->product) {
2367 case CODA_DX6:
2368 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2369 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2370 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002371 case CODA_960:
2372 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2373 /* fallthrough */
2374 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002375 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2376 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002377 break;
Javier Martin186b2502012-07-26 05:53:35 -03002378 }
2379
Philipp Zabel89548442014-07-11 06:36:17 -03002380 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2381 value &= ~(1 << 2 | 0x7 << 9);
2382 ctx->frame_mem_ctrl = value;
2383 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2384
Philipp Zabel10436672012-07-02 09:03:55 -03002385 if (dev->devtype->product == CODA_DX6) {
2386 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002387 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002388 }
Javier Martin186b2502012-07-26 05:53:35 -03002389
2390 /* Could set rotation here if needed */
2391 switch (dev->devtype->product) {
2392 case CODA_DX6:
2393 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002394 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002395 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002396 case CODA_7541:
2397 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2398 value = (round_up(q_data_src->width, 16) &
2399 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2400 value |= (round_up(q_data_src->height, 16) &
2401 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2402 break;
2403 }
2404 /* fallthrough */
2405 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002406 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002407 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002408 }
Javier Martin186b2502012-07-26 05:53:35 -03002409 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2410 coda_write(dev, ctx->params.framerate,
2411 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2412
Philipp Zabelb96904e2013-05-23 10:42:58 -03002413 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002414 switch (dst_fourcc) {
2415 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002416 if (dev->devtype->product == CODA_960)
2417 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2418 else
2419 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002420 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2421 break;
2422 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002423 if (dev->devtype->product == CODA_960)
2424 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2425 else
2426 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002427 if (ctx->params.h264_deblk_enabled) {
2428 value = ((ctx->params.h264_deblk_alpha &
2429 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2430 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2431 ((ctx->params.h264_deblk_beta &
2432 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2433 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2434 } else {
2435 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2436 }
2437 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002438 break;
2439 default:
2440 v4l2_err(v4l2_dev,
2441 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002442 ret = -EINVAL;
2443 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002444 }
2445
Philipp Zabelc566c782012-08-08 11:59:38 -03002446 switch (ctx->params.slice_mode) {
2447 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2448 value = 0;
2449 break;
2450 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2451 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2452 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002453 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002454 break;
2455 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2456 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2457 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2458 value |= 1 & CODA_SLICING_MODE_MASK;
2459 break;
2460 }
Javier Martin186b2502012-07-26 05:53:35 -03002461 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002462 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002463 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2464
2465 if (ctx->params.bitrate) {
2466 /* Rate control enabled */
2467 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2468 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002469 if (dev->devtype->product == CODA_960)
2470 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002471 } else {
2472 value = 0;
2473 }
2474 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2475
2476 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002477 coda_write(dev, ctx->params.intra_refresh,
2478 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002479
2480 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2481 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2482
Javier Martin186b2502012-07-26 05:53:35 -03002483
Philipp Zabel89548442014-07-11 06:36:17 -03002484 value = 0;
2485 if (dev->devtype->product == CODA_960)
2486 gamma = CODA9_DEFAULT_GAMMA;
2487 else
2488 gamma = CODA_DEFAULT_GAMMA;
2489 if (gamma > 0) {
2490 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2491 CODA_CMD_ENC_SEQ_RC_GAMMA);
2492 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002493
2494 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2495 coda_write(dev,
2496 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2497 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2498 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2499 }
Philipp Zabel89548442014-07-11 06:36:17 -03002500 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002501 if (ctx->params.h264_max_qp)
2502 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002503 if (CODA_DEFAULT_GAMMA > 0)
2504 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002505 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002506 if (CODA_DEFAULT_GAMMA > 0) {
2507 if (dev->devtype->product == CODA_DX6)
2508 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2509 else
2510 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2511 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002512 if (ctx->params.h264_min_qp)
2513 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2514 if (ctx->params.h264_max_qp)
2515 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002516 }
Javier Martin186b2502012-07-26 05:53:35 -03002517 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2518
Philipp Zabel89548442014-07-11 06:36:17 -03002519 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2520
Philipp Zabelc2d22512013-06-21 03:55:28 -03002521 coda_setup_iram(ctx);
2522
Javier Martin186b2502012-07-26 05:53:35 -03002523 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002524 switch (dev->devtype->product) {
2525 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002526 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002527 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002528 break;
2529 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002530 coda_write(dev, ctx->iram_info.search_ram_paddr,
2531 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2532 coda_write(dev, ctx->iram_info.search_ram_size,
2533 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002534 break;
2535 case CODA_960:
2536 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2537 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002538 }
Javier Martin186b2502012-07-26 05:53:35 -03002539 }
2540
Philipp Zabelfcb62822013-05-23 10:43:00 -03002541 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2542 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002543 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002544 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002545 }
2546
Philipp Zabelfcb62822013-05-23 10:43:00 -03002547 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2548 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2549 ret = -EFAULT;
2550 goto out;
2551 }
Javier Martin186b2502012-07-26 05:53:35 -03002552
Philipp Zabel89548442014-07-11 06:36:17 -03002553 if (dev->devtype->product == CODA_960)
2554 ctx->num_internal_frames = 4;
2555 else
2556 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002557 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002558 if (ret < 0) {
2559 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2560 goto out;
2561 }
Javier Martin186b2502012-07-26 05:53:35 -03002562
Philipp Zabelec25f682012-07-20 08:54:29 -03002563 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002564 coda_write(dev, q_data_src->bytesperline,
2565 CODA_CMD_SET_FRAME_BUF_STRIDE);
2566 if (dev->devtype->product == CODA_7541) {
2567 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002568 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002569 }
Philipp Zabel10436672012-07-02 09:03:55 -03002570 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002571 coda_write(dev, ctx->iram_info.buf_bit_use,
2572 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2573 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2574 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2575 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2576 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2577 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2578 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2579 coda_write(dev, ctx->iram_info.buf_ovl_use,
2580 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002581 if (dev->devtype->product == CODA_960) {
2582 coda_write(dev, ctx->iram_info.buf_btp_use,
2583 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2584
2585 /* FIXME */
2586 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2587 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2588 }
Philipp Zabel10436672012-07-02 09:03:55 -03002589 }
Philipp Zabel89548442014-07-11 06:36:17 -03002590
Philipp Zabelfcb62822013-05-23 10:43:00 -03002591 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2592 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002593 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002594 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002595 }
2596
2597 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002598 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002599 switch (dst_fourcc) {
2600 case V4L2_PIX_FMT_H264:
2601 /*
2602 * Get SPS in the first frame and copy it to an
2603 * intermediate buffer.
2604 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002605 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2606 &ctx->vpu_header[0][0],
2607 &ctx->vpu_header_size[0]);
2608 if (ret < 0)
2609 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002610
2611 /*
2612 * Get PPS in the first frame and copy it to an
2613 * intermediate buffer.
2614 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002615 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2616 &ctx->vpu_header[1][0],
2617 &ctx->vpu_header_size[1]);
2618 if (ret < 0)
2619 goto out;
2620
Javier Martin3f3f5c72012-10-29 05:20:29 -03002621 /*
2622 * Length of H.264 headers is variable and thus it might not be
2623 * aligned for the coda to append the encoded frame. In that is
2624 * the case a filler NAL must be added to header 2.
2625 */
2626 ctx->vpu_header_size[2] = coda_h264_padding(
2627 (ctx->vpu_header_size[0] +
2628 ctx->vpu_header_size[1]),
2629 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002630 break;
2631 case V4L2_PIX_FMT_MPEG4:
2632 /*
2633 * Get VOS in the first frame and copy it to an
2634 * intermediate buffer
2635 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002636 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2637 &ctx->vpu_header[0][0],
2638 &ctx->vpu_header_size[0]);
2639 if (ret < 0)
2640 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002641
Philipp Zabeld35167a2013-05-23 10:42:59 -03002642 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2643 &ctx->vpu_header[1][0],
2644 &ctx->vpu_header_size[1]);
2645 if (ret < 0)
2646 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002647
Philipp Zabeld35167a2013-05-23 10:42:59 -03002648 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2649 &ctx->vpu_header[2][0],
2650 &ctx->vpu_header_size[2]);
2651 if (ret < 0)
2652 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002653 break;
2654 default:
2655 /* No more formats need to save headers at the moment */
2656 break;
2657 }
2658
Philipp Zabeld35167a2013-05-23 10:42:59 -03002659out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002660 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002661 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002662}
2663
Hans Verkuile37559b2014-04-17 02:47:21 -03002664static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002665{
2666 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002667 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002668
2669 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002670 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002671 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002672 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002673
Philipp Zabel89548442014-07-11 06:36:17 -03002674 if (ctx->inst_type == CODA_INST_DECODER &&
2675 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2676 /* if this decoder instance is running, set the stream end flag */
2677 if (dev->devtype->product == CODA_960) {
2678 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2679
2680 val |= CODA_BIT_STREAM_END_FLAG;
2681 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2682 ctx->bit_stream_param = val;
2683 }
2684 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002685 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2686
2687 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002688 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002689 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002690 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002691 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002692
2693 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002694 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002695 }
2696
Philipp Zabel918c66f2013-06-27 06:59:01 -03002697 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002698 struct coda_timestamp *ts;
2699
2700 while (!list_empty(&ctx->timestamp_list)) {
2701 ts = list_first_entry(&ctx->timestamp_list,
2702 struct coda_timestamp, list);
2703 list_del(&ts->list);
2704 kfree(ts);
2705 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002706 kfifo_init(&ctx->bitstream_fifo,
2707 ctx->bitstream.vaddr, ctx->bitstream.size);
2708 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002709 }
Javier Martin186b2502012-07-26 05:53:35 -03002710}
2711
2712static struct vb2_ops coda_qops = {
2713 .queue_setup = coda_queue_setup,
2714 .buf_prepare = coda_buf_prepare,
2715 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002716 .start_streaming = coda_start_streaming,
2717 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002718 .wait_prepare = vb2_ops_wait_prepare,
2719 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002720};
2721
2722static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2723{
2724 struct coda_ctx *ctx =
2725 container_of(ctrl->handler, struct coda_ctx, ctrls);
2726
2727 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2728 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2729
2730 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002731 case V4L2_CID_HFLIP:
2732 if (ctrl->val)
2733 ctx->params.rot_mode |= CODA_MIR_HOR;
2734 else
2735 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2736 break;
2737 case V4L2_CID_VFLIP:
2738 if (ctrl->val)
2739 ctx->params.rot_mode |= CODA_MIR_VER;
2740 else
2741 ctx->params.rot_mode &= ~CODA_MIR_VER;
2742 break;
Javier Martin186b2502012-07-26 05:53:35 -03002743 case V4L2_CID_MPEG_VIDEO_BITRATE:
2744 ctx->params.bitrate = ctrl->val / 1000;
2745 break;
2746 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2747 ctx->params.gop_size = ctrl->val;
2748 break;
2749 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2750 ctx->params.h264_intra_qp = ctrl->val;
2751 break;
2752 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2753 ctx->params.h264_inter_qp = ctrl->val;
2754 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002755 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2756 ctx->params.h264_min_qp = ctrl->val;
2757 break;
2758 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2759 ctx->params.h264_max_qp = ctrl->val;
2760 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002761 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2762 ctx->params.h264_deblk_alpha = ctrl->val;
2763 break;
2764 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2765 ctx->params.h264_deblk_beta = ctrl->val;
2766 break;
2767 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2768 ctx->params.h264_deblk_enabled = (ctrl->val ==
2769 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2770 break;
Javier Martin186b2502012-07-26 05:53:35 -03002771 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2772 ctx->params.mpeg4_intra_qp = ctrl->val;
2773 break;
2774 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2775 ctx->params.mpeg4_inter_qp = ctrl->val;
2776 break;
2777 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2778 ctx->params.slice_mode = ctrl->val;
2779 break;
2780 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2781 ctx->params.slice_max_mb = ctrl->val;
2782 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002783 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2784 ctx->params.slice_max_bits = ctrl->val * 8;
2785 break;
Javier Martin186b2502012-07-26 05:53:35 -03002786 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2787 break;
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002788 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2789 ctx->params.intra_refresh = ctrl->val;
2790 break;
Javier Martin186b2502012-07-26 05:53:35 -03002791 default:
2792 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2793 "Invalid control, id=%d, val=%d\n",
2794 ctrl->id, ctrl->val);
2795 return -EINVAL;
2796 }
2797
2798 return 0;
2799}
2800
2801static struct v4l2_ctrl_ops coda_ctrl_ops = {
2802 .s_ctrl = coda_s_ctrl,
2803};
2804
2805static int coda_ctrls_setup(struct coda_ctx *ctx)
2806{
2807 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2808
2809 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002810 V4L2_CID_HFLIP, 0, 1, 1, 0);
2811 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2812 V4L2_CID_VFLIP, 0, 1, 1, 0);
2813 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002814 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2815 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2816 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2817 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002818 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002819 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002820 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002821 if (ctx->dev->devtype->product != CODA_960) {
2822 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2823 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2824 }
2825 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2826 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002827 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002828 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2829 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2830 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2831 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2832 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2833 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2834 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2835 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002836 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2837 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2838 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2839 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2840 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002841 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2842 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002843 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2844 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002845 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2846 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002847 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2848 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2849 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2850 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2851 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002852 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2853 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002854
2855 if (ctx->ctrls.error) {
2856 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2857 ctx->ctrls.error);
2858 return -EINVAL;
2859 }
2860
2861 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2862}
2863
2864static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2865 struct vb2_queue *dst_vq)
2866{
2867 struct coda_ctx *ctx = priv;
2868 int ret;
2869
Javier Martin186b2502012-07-26 05:53:35 -03002870 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002871 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002872 src_vq->drv_priv = ctx;
2873 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2874 src_vq->ops = &coda_qops;
2875 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002876 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002877 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002878
2879 ret = vb2_queue_init(src_vq);
2880 if (ret)
2881 return ret;
2882
Javier Martin186b2502012-07-26 05:53:35 -03002883 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002884 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002885 dst_vq->drv_priv = ctx;
2886 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2887 dst_vq->ops = &coda_qops;
2888 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002889 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002890 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002891
2892 return vb2_queue_init(dst_vq);
2893}
2894
Philipp Zabele11f3e62012-07-25 09:16:58 -03002895static int coda_next_free_instance(struct coda_dev *dev)
2896{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002897 int idx = ffz(dev->instance_mask);
2898
2899 if ((idx < 0) ||
2900 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2901 return -EBUSY;
2902
2903 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002904}
2905
Javier Martin186b2502012-07-26 05:53:35 -03002906static int coda_open(struct file *file)
2907{
2908 struct coda_dev *dev = video_drvdata(file);
2909 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002910 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002911 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002912 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002913
Javier Martin186b2502012-07-26 05:53:35 -03002914 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2915 if (!ctx)
2916 return -ENOMEM;
2917
Fabio Estevamf82bc202013-08-21 11:14:16 -03002918 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002919 if (idx < 0) {
2920 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002921 goto err_coda_max;
2922 }
2923 set_bit(idx, &dev->instance_mask);
2924
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002925 name = kasprintf(GFP_KERNEL, "context%d", idx);
2926 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2927 kfree(name);
2928
Philipp Zabel32b6f202014-07-11 06:36:20 -03002929 init_completion(&ctx->completion);
2930 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2931 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002932 v4l2_fh_init(&ctx->fh, video_devdata(file));
2933 file->private_data = &ctx->fh;
2934 v4l2_fh_add(&ctx->fh);
2935 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002936 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002937 switch (dev->devtype->product) {
2938 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002939 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002940 ctx->reg_idx = 0;
2941 break;
2942 default:
2943 ctx->reg_idx = idx;
2944 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002945
Philipp Zabel1e172732014-07-11 06:36:23 -03002946 /* Power up and upload firmware if necessary */
2947 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2948 if (ret < 0) {
2949 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2950 goto err_pm_get;
2951 }
2952
Fabio Estevam79830db2013-08-21 11:14:17 -03002953 ret = clk_prepare_enable(dev->clk_per);
2954 if (ret)
2955 goto err_clk_per;
2956
2957 ret = clk_prepare_enable(dev->clk_ahb);
2958 if (ret)
2959 goto err_clk_ahb;
2960
Javier Martin186b2502012-07-26 05:53:35 -03002961 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002962 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002963 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002964 if (IS_ERR(ctx->fh.m2m_ctx)) {
2965 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002966
2967 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2968 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002969 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002970 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002971
Javier Martin186b2502012-07-26 05:53:35 -03002972 ret = coda_ctrls_setup(ctx);
2973 if (ret) {
2974 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002975 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002976 }
2977
2978 ctx->fh.ctrl_handler = &ctx->ctrls;
2979
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002980 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
2981 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002982 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002983 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002984 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002985 }
2986
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002987 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2988 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2989 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2990 if (!ctx->bitstream.vaddr) {
2991 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2992 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002993 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002994 }
2995 kfifo_init(&ctx->bitstream_fifo,
2996 ctx->bitstream.vaddr, ctx->bitstream.size);
2997 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002998 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002999 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003000
Javier Martin186b2502012-07-26 05:53:35 -03003001 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003002 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003003 coda_unlock(ctx);
3004
Javier Martin186b2502012-07-26 05:53:35 -03003005 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
3006 ctx->idx, ctx);
3007
3008 return 0;
3009
Fabio Estevamf82bc202013-08-21 11:14:16 -03003010err_dma_writecombine:
3011 coda_free_context_buffers(ctx);
3012 if (ctx->dev->devtype->product == CODA_DX6)
3013 coda_free_aux_buf(dev, &ctx->workbuf);
3014 coda_free_aux_buf(dev, &ctx->parabuf);
3015err_dma_alloc:
3016 v4l2_ctrl_handler_free(&ctx->ctrls);
3017err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03003018 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003019err_ctx_init:
3020 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003021err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03003022 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03003023err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03003024 pm_runtime_put_sync(&dev->plat_dev->dev);
3025err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03003026 v4l2_fh_del(&ctx->fh);
3027 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003028 clear_bit(ctx->idx, &dev->instance_mask);
3029err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03003030 kfree(ctx);
3031 return ret;
3032}
3033
3034static int coda_release(struct file *file)
3035{
3036 struct coda_dev *dev = video_drvdata(file);
3037 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3038
3039 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3040 ctx);
3041
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003042 debugfs_remove_recursive(ctx->debugfs_entry);
3043
Philipp Zabel918c66f2013-06-27 06:59:01 -03003044 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003045 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003046
3047 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003048 if (ctx->initialized) {
3049 queue_work(dev->workqueue, &ctx->seq_end_work);
3050 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003051 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003052
3053 coda_free_framebuffers(ctx);
3054
Javier Martin186b2502012-07-26 05:53:35 -03003055 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003056 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003057 coda_unlock(ctx);
3058
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003059 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3060 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003061 coda_free_context_buffers(ctx);
3062 if (ctx->dev->devtype->product == CODA_DX6)
3063 coda_free_aux_buf(dev, &ctx->workbuf);
3064
3065 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003066 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003067 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003068 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003069 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003070 v4l2_fh_del(&ctx->fh);
3071 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003072 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003073 kfree(ctx);
3074
3075 return 0;
3076}
3077
Javier Martin186b2502012-07-26 05:53:35 -03003078static const struct v4l2_file_operations coda_fops = {
3079 .owner = THIS_MODULE,
3080 .open = coda_open,
3081 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003082 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003083 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003084 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003085};
3086
Philipp Zabel918c66f2013-06-27 06:59:01 -03003087static void coda_finish_decode(struct coda_ctx *ctx)
3088{
3089 struct coda_dev *dev = ctx->dev;
3090 struct coda_q_data *q_data_src;
3091 struct coda_q_data *q_data_dst;
3092 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003093 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003094 int width, height;
3095 int decoded_idx;
3096 int display_idx;
3097 u32 src_fourcc;
3098 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003099 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003100 u32 val;
3101
Philipp Zabel14604e32014-07-11 06:36:22 -03003102 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003103
3104 /* Update kfifo out pointer from coda bitstream read pointer */
3105 coda_kfifo_sync_from_device(ctx);
3106
3107 /*
3108 * in stream-end mode, the read pointer can overshoot the write pointer
3109 * by up to 512 bytes
3110 */
3111 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3112 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3113 kfifo_init(&ctx->bitstream_fifo,
3114 ctx->bitstream.vaddr, ctx->bitstream.size);
3115 }
3116
3117 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3118 src_fourcc = q_data_src->fourcc;
3119
3120 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3121 if (val != 1)
3122 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3123
3124 success = val & 0x1;
3125 if (!success)
3126 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3127
3128 if (src_fourcc == V4L2_PIX_FMT_H264) {
3129 if (val & (1 << 3))
3130 v4l2_err(&dev->v4l2_dev,
3131 "insufficient PS buffer space (%d bytes)\n",
3132 ctx->psbuf.size);
3133 if (val & (1 << 2))
3134 v4l2_err(&dev->v4l2_dev,
3135 "insufficient slice buffer space (%d bytes)\n",
3136 ctx->slicebuf.size);
3137 }
3138
3139 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3140 width = (val >> 16) & 0xffff;
3141 height = val & 0xffff;
3142
3143 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3144
Philipp Zabel52c41672014-07-11 06:36:19 -03003145 /* frame crop information */
3146 if (src_fourcc == V4L2_PIX_FMT_H264) {
3147 u32 left_right;
3148 u32 top_bottom;
3149
3150 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3151 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3152
3153 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3154 /* Keep current crop information */
3155 } else {
3156 struct v4l2_rect *rect = &q_data_dst->rect;
3157
3158 rect->left = left_right >> 16 & 0xffff;
3159 rect->top = top_bottom >> 16 & 0xffff;
3160 rect->width = width - rect->left -
3161 (left_right & 0xffff);
3162 rect->height = height - rect->top -
3163 (top_bottom & 0xffff);
3164 }
3165 } else {
3166 /* no cropping */
3167 }
3168
Philipp Zabel410e5e42014-07-11 06:36:32 -03003169 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3170 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003171 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003172 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003173
3174 if (dev->devtype->product == CODA_7541) {
3175 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3176 if (val == 0) {
3177 /* not enough bitstream data */
3178 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3179 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003180 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003181 return;
3182 }
3183 }
3184
3185 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3186
3187 /*
3188 * The previous display frame was copied out by the rotator,
3189 * now it can be overwritten again
3190 */
3191 if (ctx->display_idx >= 0 &&
3192 ctx->display_idx < ctx->num_internal_frames) {
3193 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3194 coda_write(dev, ctx->frm_dis_flg,
3195 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3196 }
3197
3198 /*
3199 * The index of the last decoded frame, not necessarily in
3200 * display order, and the index of the next display frame.
3201 * The latter could have been decoded in a previous run.
3202 */
3203 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3204 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3205
3206 if (decoded_idx == -1) {
3207 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003208 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3209 ctx->sequence_offset++;
3210 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003211 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003212 } else if (decoded_idx == -2) {
3213 /* no frame was decoded, we still return the remaining buffers */
3214 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3215 v4l2_err(&dev->v4l2_dev,
3216 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003217 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003218 ts = list_first_entry(&ctx->timestamp_list,
3219 struct coda_timestamp, list);
3220 list_del(&ts->list);
3221 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003222 val -= ctx->sequence_offset;
3223 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003224 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003225 "sequence number mismatch (%d(%d) != %d)\n",
3226 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003227 }
3228 ctx->frame_timestamps[decoded_idx] = *ts;
3229 kfree(ts);
3230
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003231 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3232 if (val == 0)
3233 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3234 else if (val == 1)
3235 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3236 else
3237 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003238
3239 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003240 }
3241
3242 if (display_idx == -1) {
3243 /*
3244 * no more frames to be decoded, but there could still
3245 * be rotator output to dequeue
3246 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003247 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003248 } else if (display_idx == -3) {
3249 /* possibly prescan failure */
3250 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3251 v4l2_err(&dev->v4l2_dev,
3252 "presentation frame index out of range: %d\n",
3253 display_idx);
3254 }
3255
3256 /* If a frame was copied out, return it */
3257 if (ctx->display_idx >= 0 &&
3258 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003259 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003260 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3261
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003262 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3263 V4L2_BUF_FLAG_PFRAME |
3264 V4L2_BUF_FLAG_BFRAME);
3265 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003266 ts = &ctx->frame_timestamps[ctx->display_idx];
3267 dst_buf->v4l2_buf.timecode = ts->timecode;
3268 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003269
Philipp Zabel918c66f2013-06-27 06:59:01 -03003270 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3271
Philipp Zabel410e5e42014-07-11 06:36:32 -03003272 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3273 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003274
3275 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3276 "job finished: decoding frame (%d) (%s)\n",
3277 dst_buf->v4l2_buf.sequence,
3278 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3279 "KEYFRAME" : "PFRAME");
3280 } else {
3281 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3282 "job finished: no frame decoded\n");
3283 }
3284
3285 /* The rotator will copy the current display frame next time */
3286 ctx->display_idx = display_idx;
3287}
3288
3289static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003290{
Philipp Zabelec25f682012-07-20 08:54:29 -03003291 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003292 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003293 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003294
Philipp Zabel14604e32014-07-11 06:36:22 -03003295 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3296 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003297
3298 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003299 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003300 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3301
Javier Martin186b2502012-07-26 05:53:35 -03003302 /* Calculate bytesused field */
3303 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003304 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3305 ctx->vpu_header_size[0] +
3306 ctx->vpu_header_size[1] +
3307 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003308 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003309 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003310 }
3311
3312 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3313 wr_ptr - start_ptr);
3314
3315 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3316 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3317
Philipp Zabel51660282013-09-30 10:34:49 -03003318 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003319 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3320 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3321 } else {
3322 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3323 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3324 }
3325
Kamil Debskiccd571c2013-04-24 10:38:24 -03003326 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003327 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3328 dst_buf->v4l2_buf.flags |=
3329 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003330 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3331
Philipp Zabelec25f682012-07-20 08:54:29 -03003332 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003333
Philipp Zabel14604e32014-07-11 06:36:22 -03003334 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003335 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3336
3337 ctx->gopcounter--;
3338 if (ctx->gopcounter < 0)
3339 ctx->gopcounter = ctx->params.gop_size - 1;
3340
3341 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3342 "job finished: encoding frame (%d) (%s)\n",
3343 dst_buf->v4l2_buf.sequence,
3344 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3345 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003346}
3347
3348static irqreturn_t coda_irq_handler(int irq, void *data)
3349{
3350 struct coda_dev *dev = data;
3351 struct coda_ctx *ctx;
3352
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003353 /* read status register to attend the IRQ */
3354 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3355 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3356 CODA_REG_BIT_INT_CLEAR);
3357
3358 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3359 if (ctx == NULL) {
3360 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3361 mutex_unlock(&dev->coda_mutex);
3362 return IRQ_HANDLED;
3363 }
3364
3365 if (ctx->aborting) {
3366 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3367 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003368 }
3369
3370 if (coda_isbusy(ctx->dev)) {
3371 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3372 "coda is still busy!!!!\n");
3373 return IRQ_NONE;
3374 }
3375
Philipp Zabel32b6f202014-07-11 06:36:20 -03003376 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003377
3378 return IRQ_HANDLED;
3379}
3380
3381static u32 coda_supported_firmwares[] = {
3382 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003383 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003384 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003385};
3386
3387static bool coda_firmware_supported(u32 vernum)
3388{
3389 int i;
3390
3391 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3392 if (vernum == coda_supported_firmwares[i])
3393 return true;
3394 return false;
3395}
3396
Philipp Zabel87048bb2012-07-02 07:03:43 -03003397static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003398{
Javier Martin186b2502012-07-26 05:53:35 -03003399 u32 data;
3400 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003401 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003402
Fabio Estevam79830db2013-08-21 11:14:17 -03003403 ret = clk_prepare_enable(dev->clk_per);
3404 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003405 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003406
3407 ret = clk_prepare_enable(dev->clk_ahb);
3408 if (ret)
3409 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003410
Philipp Zabel8f452842014-07-11 06:36:35 -03003411 if (dev->rstc)
3412 reset_control_reset(dev->rstc);
3413
Javier Martin186b2502012-07-26 05:53:35 -03003414 /*
3415 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003416 * The 16-bit chars in the code buffer are in memory access
3417 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003418 * Data in this SRAM survives a reboot.
3419 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003420 p = (u16 *)dev->codebuf.vaddr;
3421 if (dev->devtype->product == CODA_DX6) {
3422 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3423 data = CODA_DOWN_ADDRESS_SET(i) |
3424 CODA_DOWN_DATA_SET(p[i ^ 1]);
3425 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3426 }
3427 } else {
3428 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3429 data = CODA_DOWN_ADDRESS_SET(i) |
3430 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3431 3 - (i % 4)]);
3432 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3433 }
Javier Martin186b2502012-07-26 05:53:35 -03003434 }
Javier Martin186b2502012-07-26 05:53:35 -03003435
Philipp Zabel9acf7692013-05-23 10:42:56 -03003436 /* Clear registers */
3437 for (i = 0; i < 64; i++)
3438 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3439
Javier Martin186b2502012-07-26 05:53:35 -03003440 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003441 if (dev->devtype->product == CODA_960 ||
3442 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003443 coda_write(dev, dev->tempbuf.paddr,
3444 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003445 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003446 } else {
3447 coda_write(dev, dev->workbuf.paddr,
3448 CODA_REG_BIT_WORK_BUF_ADDR);
3449 }
Javier Martin186b2502012-07-26 05:53:35 -03003450 coda_write(dev, dev->codebuf.paddr,
3451 CODA_REG_BIT_CODE_BUF_ADDR);
3452 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3453
3454 /* Set default values */
3455 switch (dev->devtype->product) {
3456 case CODA_DX6:
3457 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3458 break;
3459 default:
3460 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3461 }
Philipp Zabel89548442014-07-11 06:36:17 -03003462 if (dev->devtype->product == CODA_960)
3463 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3464 else
3465 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003466
3467 if (dev->devtype->product != CODA_DX6)
3468 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3469
Javier Martin186b2502012-07-26 05:53:35 -03003470 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3471 CODA_REG_BIT_INT_ENABLE);
3472
3473 /* Reset VPU and start processor */
3474 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3475 data |= CODA_REG_RESET_ENABLE;
3476 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3477 udelay(10);
3478 data &= ~CODA_REG_RESET_ENABLE;
3479 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3480 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3481
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003482 clk_disable_unprepare(dev->clk_ahb);
3483 clk_disable_unprepare(dev->clk_per);
3484
3485 return 0;
3486
3487err_clk_ahb:
3488 clk_disable_unprepare(dev->clk_per);
3489err_clk_per:
3490 return ret;
3491}
3492
3493static int coda_check_firmware(struct coda_dev *dev)
3494{
3495 u16 product, major, minor, release;
3496 u32 data;
3497 int ret;
3498
3499 ret = clk_prepare_enable(dev->clk_per);
3500 if (ret)
3501 goto err_clk_per;
3502
3503 ret = clk_prepare_enable(dev->clk_ahb);
3504 if (ret)
3505 goto err_clk_ahb;
3506
Javier Martin186b2502012-07-26 05:53:35 -03003507 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3508 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3509 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3510 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3511 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3512 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003513 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003514 ret = -EIO;
3515 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003516 }
3517
Philipp Zabel89548442014-07-11 06:36:17 -03003518 if (dev->devtype->product == CODA_960) {
3519 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3520 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3521 data);
3522 }
3523
Javier Martin186b2502012-07-26 05:53:35 -03003524 /* Check we are compatible with the loaded firmware */
3525 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3526 product = CODA_FIRMWARE_PRODUCT(data);
3527 major = CODA_FIRMWARE_MAJOR(data);
3528 minor = CODA_FIRMWARE_MINOR(data);
3529 release = CODA_FIRMWARE_RELEASE(data);
3530
3531 clk_disable_unprepare(dev->clk_per);
3532 clk_disable_unprepare(dev->clk_ahb);
3533
3534 if (product != dev->devtype->product) {
3535 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3536 " Version: %u.%u.%u\n",
3537 coda_product_name(dev->devtype->product),
3538 coda_product_name(product), major, minor, release);
3539 return -EINVAL;
3540 }
3541
3542 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3543 coda_product_name(product));
3544
3545 if (coda_firmware_supported(data)) {
3546 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3547 major, minor, release);
3548 } else {
3549 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3550 "%u.%u.%u\n", major, minor, release);
3551 }
3552
3553 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003554
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003555err_run_cmd:
3556 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003557err_clk_ahb:
3558 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003559err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003560 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003561}
3562
3563static void coda_fw_callback(const struct firmware *fw, void *context)
3564{
3565 struct coda_dev *dev = context;
3566 struct platform_device *pdev = dev->plat_dev;
3567 int ret;
3568
3569 if (!fw) {
3570 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3571 return;
3572 }
3573
3574 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003575 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3576 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003577 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003578 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3579 return;
3580 }
3581
Philipp Zabel87048bb2012-07-02 07:03:43 -03003582 /* Copy the whole firmware image to the code buffer */
3583 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3584 release_firmware(fw);
3585
Philipp Zabel1e172732014-07-11 06:36:23 -03003586 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3587 /*
3588 * Enabling power temporarily will cause coda_hw_init to be
3589 * called via coda_runtime_resume by the pm domain.
3590 */
3591 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3592 if (ret < 0) {
3593 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3594 ret);
3595 return;
3596 }
3597
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003598 ret = coda_check_firmware(dev);
3599 if (ret < 0)
3600 return;
3601
Philipp Zabel1e172732014-07-11 06:36:23 -03003602 pm_runtime_put_sync(&dev->plat_dev->dev);
3603 } else {
3604 /*
3605 * If runtime pm is disabled or pm_domain is not set,
3606 * initialize once manually.
3607 */
3608 ret = coda_hw_init(dev);
3609 if (ret < 0) {
3610 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3611 return;
3612 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003613
3614 ret = coda_check_firmware(dev);
3615 if (ret < 0)
3616 return;
Javier Martin186b2502012-07-26 05:53:35 -03003617 }
3618
3619 dev->vfd.fops = &coda_fops,
3620 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3621 dev->vfd.release = video_device_release_empty,
3622 dev->vfd.lock = &dev->dev_mutex;
3623 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003624 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003625 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3626 video_set_drvdata(&dev->vfd, dev);
3627
3628 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3629 if (IS_ERR(dev->alloc_ctx)) {
3630 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3631 return;
3632 }
3633
3634 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3635 if (IS_ERR(dev->m2m_dev)) {
3636 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3637 goto rel_ctx;
3638 }
3639
3640 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3641 if (ret) {
3642 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3643 goto rel_m2m;
3644 }
3645 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3646 dev->vfd.num);
3647
3648 return;
3649
3650rel_m2m:
3651 v4l2_m2m_release(dev->m2m_dev);
3652rel_ctx:
3653 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3654}
3655
3656static int coda_firmware_request(struct coda_dev *dev)
3657{
3658 char *fw = dev->devtype->firmware;
3659
3660 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3661 coda_product_name(dev->devtype->product));
3662
3663 return request_firmware_nowait(THIS_MODULE, true,
3664 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3665}
3666
3667enum coda_platform {
3668 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003669 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003670 CODA_IMX6Q,
3671 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003672};
3673
Emil Goodec06d8752012-08-14 17:44:42 -03003674static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003675 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003676 .firmware = "v4l-codadx6-imx27.bin",
3677 .product = CODA_DX6,
3678 .codecs = codadx6_codecs,
3679 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3680 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Javier Martin186b2502012-07-26 05:53:35 -03003681 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003682 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003683 .firmware = "v4l-coda7541-imx53.bin",
3684 .product = CODA_7541,
3685 .codecs = coda7_codecs,
3686 .num_codecs = ARRAY_SIZE(coda7_codecs),
3687 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003688 .tempbuf_size = 304 * 1024,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003689 },
Philipp Zabel89548442014-07-11 06:36:17 -03003690 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003691 .firmware = "v4l-coda960-imx6q.bin",
3692 .product = CODA_960,
3693 .codecs = coda9_codecs,
3694 .num_codecs = ARRAY_SIZE(coda9_codecs),
3695 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003696 .tempbuf_size = 204 * 1024,
Philipp Zabel89548442014-07-11 06:36:17 -03003697 },
3698 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003699 .firmware = "v4l-coda960-imx6dl.bin",
3700 .product = CODA_960,
3701 .codecs = coda9_codecs,
3702 .num_codecs = ARRAY_SIZE(coda9_codecs),
3703 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003704 .tempbuf_size = 204 * 1024,
Philipp Zabel89548442014-07-11 06:36:17 -03003705 },
Javier Martin186b2502012-07-26 05:53:35 -03003706};
3707
3708static struct platform_device_id coda_platform_ids[] = {
3709 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003710 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003711 { /* sentinel */ }
3712};
3713MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3714
3715#ifdef CONFIG_OF
3716static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003717 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003718 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003719 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3720 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003721 { /* sentinel */ }
3722};
3723MODULE_DEVICE_TABLE(of, coda_dt_ids);
3724#endif
3725
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003726static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003727{
3728 const struct of_device_id *of_id =
3729 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3730 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003731 struct coda_platform_data *pdata = pdev->dev.platform_data;
3732 struct device_node *np = pdev->dev.of_node;
3733 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003734 struct coda_dev *dev;
3735 struct resource *res;
3736 int ret, irq;
3737
3738 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3739 if (!dev) {
3740 dev_err(&pdev->dev, "Not enough memory for %s\n",
3741 CODA_NAME);
3742 return -ENOMEM;
3743 }
3744
3745 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003746 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003747
3748 dev->plat_dev = pdev;
3749 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3750 if (IS_ERR(dev->clk_per)) {
3751 dev_err(&pdev->dev, "Could not get per clock\n");
3752 return PTR_ERR(dev->clk_per);
3753 }
3754
3755 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3756 if (IS_ERR(dev->clk_ahb)) {
3757 dev_err(&pdev->dev, "Could not get ahb clock\n");
3758 return PTR_ERR(dev->clk_ahb);
3759 }
3760
3761 /* Get memory for physical registers */
3762 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003763 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3764 if (IS_ERR(dev->regs_base))
3765 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003766
3767 /* IRQ */
3768 irq = platform_get_irq(pdev, 0);
3769 if (irq < 0) {
3770 dev_err(&pdev->dev, "failed to get irq resource\n");
3771 return -ENOENT;
3772 }
3773
Philipp Zabel918c66f2013-06-27 06:59:01 -03003774 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003775 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003776 dev_err(&pdev->dev, "failed to request irq\n");
3777 return -ENOENT;
3778 }
3779
Philipp Zabel8f452842014-07-11 06:36:35 -03003780 dev->rstc = devm_reset_control_get(&pdev->dev, NULL);
3781 if (IS_ERR(dev->rstc)) {
3782 ret = PTR_ERR(dev->rstc);
3783 if (ret == -ENOENT) {
3784 dev->rstc = NULL;
3785 } else {
3786 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3787 return ret;
3788 }
3789 }
3790
Philipp Zabel657eee72013-04-29 16:17:14 -07003791 /* Get IRAM pool from device tree or platform data */
3792 pool = of_get_named_gen_pool(np, "iram", 0);
3793 if (!pool && pdata)
3794 pool = dev_get_gen_pool(pdata->iram_dev);
3795 if (!pool) {
3796 dev_err(&pdev->dev, "iram pool not available\n");
3797 return -ENOMEM;
3798 }
3799 dev->iram_pool = pool;
3800
Javier Martin186b2502012-07-26 05:53:35 -03003801 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3802 if (ret)
3803 return ret;
3804
3805 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003806 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003807
3808 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3809
3810 if (of_id) {
3811 dev->devtype = of_id->data;
3812 } else if (pdev_id) {
3813 dev->devtype = &coda_devdata[pdev_id->driver_data];
3814 } else {
3815 v4l2_device_unregister(&dev->v4l2_dev);
3816 return -EINVAL;
3817 }
3818
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003819 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3820 if (!dev->debugfs_root)
3821 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3822
Javier Martin186b2502012-07-26 05:53:35 -03003823 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003824 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003825 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003826 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003827 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003828 if (ret < 0) {
3829 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3830 v4l2_device_unregister(&dev->v4l2_dev);
3831 return ret;
3832 }
Javier Martin186b2502012-07-26 05:53:35 -03003833 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003834
3835 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003836 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003837 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003838 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003839 if (ret < 0) {
3840 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3841 v4l2_device_unregister(&dev->v4l2_dev);
3842 return ret;
3843 }
Javier Martin186b2502012-07-26 05:53:35 -03003844 }
3845
Philipp Zabel918c66f2013-06-27 06:59:01 -03003846 switch (dev->devtype->product) {
3847 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003848 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003849 break;
3850 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003851 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003852 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003853 case CODA_960:
3854 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003855 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003856 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3857 &dev->iram.paddr);
3858 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003859 dev_err(&pdev->dev, "unable to alloc iram\n");
3860 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003861 }
3862
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003863 dev->iram.blob.data = dev->iram.vaddr;
3864 dev->iram.blob.size = dev->iram.size;
3865 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
3866 &dev->iram.blob);
3867
Philipp Zabel32b6f202014-07-11 06:36:20 -03003868 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3869 if (!dev->workqueue) {
3870 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3871 return -ENOMEM;
3872 }
3873
Javier Martin186b2502012-07-26 05:53:35 -03003874 platform_set_drvdata(pdev, dev);
3875
Philipp Zabel1e172732014-07-11 06:36:23 -03003876 pm_runtime_enable(&pdev->dev);
3877
Javier Martin186b2502012-07-26 05:53:35 -03003878 return coda_firmware_request(dev);
3879}
3880
3881static int coda_remove(struct platform_device *pdev)
3882{
3883 struct coda_dev *dev = platform_get_drvdata(pdev);
3884
3885 video_unregister_device(&dev->vfd);
3886 if (dev->m2m_dev)
3887 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003888 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003889 if (dev->alloc_ctx)
3890 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3891 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003892 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003893 if (dev->iram.vaddr)
3894 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3895 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003896 coda_free_aux_buf(dev, &dev->codebuf);
3897 coda_free_aux_buf(dev, &dev->tempbuf);
3898 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003899 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03003900 return 0;
3901}
3902
Philipp Zabel1e172732014-07-11 06:36:23 -03003903#ifdef CONFIG_PM_RUNTIME
3904static int coda_runtime_resume(struct device *dev)
3905{
3906 struct coda_dev *cdev = dev_get_drvdata(dev);
3907 int ret = 0;
3908
3909 if (dev->pm_domain) {
3910 ret = coda_hw_init(cdev);
3911 if (ret)
3912 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3913 }
3914
3915 return ret;
3916}
3917#endif
3918
3919static const struct dev_pm_ops coda_pm_ops = {
3920 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3921};
3922
Javier Martin186b2502012-07-26 05:53:35 -03003923static struct platform_driver coda_driver = {
3924 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003925 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003926 .driver = {
3927 .name = CODA_NAME,
3928 .owner = THIS_MODULE,
3929 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003930 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003931 },
3932 .id_table = coda_platform_ids,
3933};
3934
3935module_platform_driver(coda_driver);
3936
3937MODULE_LICENSE("GPL");
3938MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3939MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");