blob: d4e700391bbaecb709fd711dfc0597628f467a25 [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070017#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030018#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030021#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030022#include <linux/module.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030025#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030026#include <linux/slab.h>
27#include <linux/videodev2.h>
28#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070029#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030030
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030033#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030034#include <media/v4l2-ioctl.h>
35#include <media/v4l2-mem2mem.h>
36#include <media/videobuf2-core.h>
37#include <media/videobuf2-dma-contig.h>
38
39#include "coda.h"
40
41#define CODA_NAME "coda"
42
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030043#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030044
45#define CODA_FMO_BUF_SIZE 32
46#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030047#define CODA7_WORK_BUF_SIZE (128 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030048#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030049#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030050#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030051#define CODA_PARA_BUF_SIZE (10 * 1024)
52#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070053#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030054#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030055#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030056
Philipp Zabel918c66f2013-06-27 06:59:01 -030057#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030058#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030059
60#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030061
Philipp Zabelb96904e2013-05-23 10:42:58 -030062#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030063#define FMO_SLICE_SAVE_BUF_SIZE (32)
64#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030065#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030066
67#define MIN_W 176
68#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030069
70#define S_ALIGN 1 /* multiple of 2 */
71#define W_ALIGN 1 /* multiple of 2 */
72#define H_ALIGN 1 /* multiple of 2 */
73
74#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
75
76static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030077module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030078MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
79
80enum {
81 V4L2_M2M_SRC = 0,
82 V4L2_M2M_DST = 1,
83};
84
Javier Martin186b2502012-07-26 05:53:35 -030085enum coda_inst_type {
86 CODA_INST_ENCODER,
87 CODA_INST_DECODER,
88};
89
90enum coda_product {
91 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030092 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030093 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030094};
95
96struct coda_fmt {
97 char *name;
98 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030099};
100
101struct coda_codec {
102 u32 mode;
103 u32 src_fourcc;
104 u32 dst_fourcc;
105 u32 max_w;
106 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300107};
108
109struct coda_devtype {
110 char *firmware;
111 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300112 struct coda_codec *codecs;
113 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300114 size_t workbuf_size;
115};
116
117/* Per-queue, driver-specific private data */
118struct coda_q_data {
119 unsigned int width;
120 unsigned int height;
121 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300122 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300123 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300124};
125
126struct coda_aux_buf {
127 void *vaddr;
128 dma_addr_t paddr;
129 u32 size;
130};
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;
141
142 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300143 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300144 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700145 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300146 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300147
148 spinlock_t irqlock;
149 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300150 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300151 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300152 struct v4l2_m2m_dev *m2m_dev;
153 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300154 struct list_head instances;
155 unsigned long instance_mask;
Javier Martin186b2502012-07-26 05:53:35 -0300156};
157
158struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300159 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300160 u8 h264_intra_qp;
161 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300162 u8 h264_min_qp;
163 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300164 u8 h264_deblk_enabled;
165 u8 h264_deblk_alpha;
166 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300167 u8 mpeg4_intra_qp;
168 u8 mpeg4_inter_qp;
169 u8 gop_size;
Philipp Zabelf38f79d2014-07-11 06:36:28 -0300170 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300171 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300172 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300173 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
174 u32 framerate;
175 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300176 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300177 u32 slice_max_mb;
178};
179
Philipp Zabelc2d22512013-06-21 03:55:28 -0300180struct coda_iram_info {
181 u32 axi_sram_use;
182 phys_addr_t buf_bit_use;
183 phys_addr_t buf_ip_ac_dc_use;
184 phys_addr_t buf_dbk_y_use;
185 phys_addr_t buf_dbk_c_use;
186 phys_addr_t buf_ovl_use;
187 phys_addr_t buf_btp_use;
188 phys_addr_t search_ram_paddr;
189 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300190 int remaining;
191 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300192};
193
Philipp Zabel89548442014-07-11 06:36:17 -0300194struct gdi_tiled_map {
195 int xy2ca_map[16];
196 int xy2ba_map[16];
197 int xy2ra_map[16];
198 int rbc2axi_map[32];
199 int xy2rbc_config;
200 int map_type;
201#define GDI_LINEAR_FRAME_MAP 0
202};
203
Philipp Zabel846ced92014-07-11 06:36:31 -0300204struct coda_timestamp {
205 struct list_head list;
206 u32 sequence;
207 struct v4l2_timecode timecode;
208 struct timeval timestamp;
209};
210
Javier Martin186b2502012-07-26 05:53:35 -0300211struct coda_ctx {
212 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300213 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300214 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300215 struct work_struct pic_run_work;
216 struct work_struct seq_end_work;
217 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300218 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300219 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300220 int streamon_out;
221 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300222 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300223 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300224 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300225 struct coda_q_data q_data[2];
226 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300227 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300228 enum v4l2_colorspace colorspace;
229 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300230 struct v4l2_ctrl_handler ctrls;
231 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300232 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300233 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300234 char vpu_header[3][64];
235 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300236 struct kfifo bitstream_fifo;
237 struct mutex bitstream_mutex;
238 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300239 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300240 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300241 struct coda_aux_buf psbuf;
242 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300243 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300244 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300245 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300246 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300247 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300248 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300249 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300250 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300251 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300252 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300253 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300254 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300255 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300256 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300257 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300258};
259
Javier Martin832fbb52012-10-29 08:34:59 -0300260static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
261 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
262static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300263
Javier Martin186b2502012-07-26 05:53:35 -0300264static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
265{
266 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
267 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
268 writel(data, dev->regs_base + reg);
269}
270
271static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
272{
273 u32 data;
274 data = readl(dev->regs_base + reg);
275 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
276 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
277 return data;
278}
279
280static inline unsigned long coda_isbusy(struct coda_dev *dev)
281{
282 return coda_read(dev, CODA_REG_BIT_BUSY);
283}
284
285static inline int coda_is_initialized(struct coda_dev *dev)
286{
287 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
288}
289
290static int coda_wait_timeout(struct coda_dev *dev)
291{
292 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
293
294 while (coda_isbusy(dev)) {
295 if (time_after(jiffies, timeout))
296 return -ETIMEDOUT;
297 }
298 return 0;
299}
300
301static void coda_command_async(struct coda_ctx *ctx, int cmd)
302{
303 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300304
Philipp Zabel89548442014-07-11 06:36:17 -0300305 if (dev->devtype->product == CODA_960 ||
306 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300307 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300308 coda_write(dev, ctx->bit_stream_param,
309 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300310 coda_write(dev, ctx->frm_dis_flg,
311 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300312 coda_write(dev, ctx->frame_mem_ctrl,
313 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300314 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
315 }
316
Philipp Zabel89548442014-07-11 06:36:17 -0300317 if (dev->devtype->product == CODA_960) {
318 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
319 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
320 }
321
Javier Martin186b2502012-07-26 05:53:35 -0300322 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
323
324 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
325 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300326 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
327
Javier Martin186b2502012-07-26 05:53:35 -0300328 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
329}
330
331static int coda_command_sync(struct coda_ctx *ctx, int cmd)
332{
333 struct coda_dev *dev = ctx->dev;
334
335 coda_command_async(ctx, cmd);
336 return coda_wait_timeout(dev);
337}
338
339static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
340 enum v4l2_buf_type type)
341{
342 switch (type) {
343 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
344 return &(ctx->q_data[V4L2_M2M_SRC]);
345 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
346 return &(ctx->q_data[V4L2_M2M_DST]);
347 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300348 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300349 }
Javier Martin186b2502012-07-26 05:53:35 -0300350}
351
352/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300353 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300354 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300355static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300356 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300357 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300358 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300359 },
360 {
361 .name = "YUV 4:2:0 Planar, YCrCb",
362 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300363 },
364 {
365 .name = "H264 Encoded Stream",
366 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300367 },
368 {
369 .name = "MPEG4 Encoded Stream",
370 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300371 },
372};
373
Philipp Zabelb96904e2013-05-23 10:42:58 -0300374#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
375 { mode, src_fourcc, dst_fourcc, max_w, max_h }
376
377/*
378 * Arrays of codecs supported by each given version of Coda:
379 * i.MX27 -> codadx6
380 * i.MX5x -> coda7
381 * i.MX6 -> coda960
382 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
383 */
384static struct coda_codec codadx6_codecs[] = {
385 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
386 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300387};
388
Philipp Zabelb96904e2013-05-23 10:42:58 -0300389static struct coda_codec coda7_codecs[] = {
390 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
391 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300392 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
393 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300394};
395
Philipp Zabel89548442014-07-11 06:36:17 -0300396static struct coda_codec coda9_codecs[] = {
397 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
398 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
399 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
400 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
401};
402
Philipp Zabelb96904e2013-05-23 10:42:58 -0300403static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300404{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300405 switch (fourcc) {
406 case V4L2_PIX_FMT_YUV420:
407 case V4L2_PIX_FMT_YVU420:
408 return true;
409 default:
410 return false;
411 }
412}
Javier Martin186b2502012-07-26 05:53:35 -0300413
Philipp Zabelb96904e2013-05-23 10:42:58 -0300414/*
415 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
416 * tables.
417 */
418static u32 coda_format_normalize_yuv(u32 fourcc)
419{
420 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
421}
422
423static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
424 int dst_fourcc)
425{
426 struct coda_codec *codecs = dev->devtype->codecs;
427 int num_codecs = dev->devtype->num_codecs;
428 int k;
429
430 src_fourcc = coda_format_normalize_yuv(src_fourcc);
431 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
432 if (src_fourcc == dst_fourcc)
433 return NULL;
434
435 for (k = 0; k < num_codecs; k++) {
436 if (codecs[k].src_fourcc == src_fourcc &&
437 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300438 break;
439 }
440
Philipp Zabelb96904e2013-05-23 10:42:58 -0300441 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300442 return NULL;
443
Philipp Zabelb96904e2013-05-23 10:42:58 -0300444 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300445}
446
Philipp Zabel57625592013-09-30 10:34:51 -0300447static void coda_get_max_dimensions(struct coda_dev *dev,
448 struct coda_codec *codec,
449 int *max_w, int *max_h)
450{
451 struct coda_codec *codecs = dev->devtype->codecs;
452 int num_codecs = dev->devtype->num_codecs;
453 unsigned int w, h;
454 int k;
455
456 if (codec) {
457 w = codec->max_w;
458 h = codec->max_h;
459 } else {
460 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
461 w = max(w, codecs[k].max_w);
462 h = max(h, codecs[k].max_h);
463 }
464 }
465
466 if (max_w)
467 *max_w = w;
468 if (max_h)
469 *max_h = h;
470}
471
Philipp Zabel927933f2013-09-30 10:34:48 -0300472static char *coda_product_name(int product)
473{
474 static char buf[9];
475
476 switch (product) {
477 case CODA_DX6:
478 return "CodaDx6";
479 case CODA_7541:
480 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300481 case CODA_960:
482 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300483 default:
484 snprintf(buf, sizeof(buf), "(0x%04x)", product);
485 return buf;
486 }
487}
488
Javier Martin186b2502012-07-26 05:53:35 -0300489/*
490 * V4L2 ioctl() operations.
491 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300492static int coda_querycap(struct file *file, void *priv,
493 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300494{
Philipp Zabel927933f2013-09-30 10:34:48 -0300495 struct coda_ctx *ctx = fh_to_ctx(priv);
496
Javier Martin186b2502012-07-26 05:53:35 -0300497 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300498 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
499 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300500 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300501 /*
502 * This is only a mem-to-mem video device. The capture and output
503 * device capability flags are left only for backward compatibility
504 * and are scheduled for removal.
505 */
506 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
507 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300508 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
509
510 return 0;
511}
512
513static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300514 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300515{
516 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300517 struct coda_codec *codecs = ctx->dev->devtype->codecs;
518 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300519 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300520 int num_codecs = ctx->dev->devtype->num_codecs;
521 int num_formats = ARRAY_SIZE(coda_formats);
522 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300523
524 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300525 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300526 if (coda_format_is_yuv(formats[i].fourcc) &&
527 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300528 if (num == f->index)
529 break;
530 ++num;
531 continue;
532 }
533 /* Compressed formats may be supported, check the codec list */
534 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300535 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300536 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300537 formats[i].fourcc == codecs[k].dst_fourcc &&
538 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300539 break;
540 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
541 formats[i].fourcc == codecs[k].src_fourcc)
542 break;
543 }
544 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300545 if (num == f->index)
546 break;
547 ++num;
548 }
549 }
550
551 if (i < num_formats) {
552 fmt = &formats[i];
553 strlcpy(f->description, fmt->name, sizeof(f->description));
554 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300555 if (!coda_format_is_yuv(fmt->fourcc))
556 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300557 return 0;
558 }
559
560 /* Format not found */
561 return -EINVAL;
562}
563
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300564static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
565 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300566{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300567 struct coda_ctx *ctx = fh_to_ctx(priv);
568 struct vb2_queue *src_vq;
569 struct coda_q_data *q_data_src;
570
571 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300572 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300573 if (vb2_is_streaming(src_vq)) {
574 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
575
576 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
577 q_data_src->fourcc);
578 }
579
580 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300581}
582
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300583static int coda_enum_fmt_vid_out(struct file *file, void *priv,
584 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300585{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300586 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300587}
588
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300589static int coda_g_fmt(struct file *file, void *priv,
590 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300591{
Javier Martin186b2502012-07-26 05:53:35 -0300592 struct coda_q_data *q_data;
593 struct coda_ctx *ctx = fh_to_ctx(priv);
594
Javier Martin186b2502012-07-26 05:53:35 -0300595 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300596 if (!q_data)
597 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300598
599 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300600 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300601 f->fmt.pix.width = q_data->width;
602 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300603 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300604 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
605 else /* encoded formats h.264/mpeg4 */
606 f->fmt.pix.bytesperline = 0;
607
608 f->fmt.pix.sizeimage = q_data->sizeimage;
609 f->fmt.pix.colorspace = ctx->colorspace;
610
611 return 0;
612}
613
Philipp Zabel57625592013-09-30 10:34:51 -0300614static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
615 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300616{
Philipp Zabel57625592013-09-30 10:34:51 -0300617 struct coda_dev *dev = ctx->dev;
618 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300619 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300620 enum v4l2_field field;
621
622 field = f->fmt.pix.field;
623 if (field == V4L2_FIELD_ANY)
624 field = V4L2_FIELD_NONE;
625 else if (V4L2_FIELD_NONE != field)
626 return -EINVAL;
627
628 /* V4L2 specification suggests the driver corrects the format struct
629 * if any of the dimensions is unsupported */
630 f->fmt.pix.field = field;
631
Philipp Zabel57625592013-09-30 10:34:51 -0300632 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
633 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
634 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
635 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300636
Philipp Zabel57625592013-09-30 10:34:51 -0300637 switch (f->fmt.pix.pixelformat) {
638 case V4L2_PIX_FMT_YUV420:
639 case V4L2_PIX_FMT_YVU420:
640 case V4L2_PIX_FMT_H264:
641 case V4L2_PIX_FMT_MPEG4:
642 case V4L2_PIX_FMT_JPEG:
643 break;
644 default:
645 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300646 if (!q_data)
647 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300648 f->fmt.pix.pixelformat = q_data->fourcc;
649 }
650
651 switch (f->fmt.pix.pixelformat) {
652 case V4L2_PIX_FMT_YUV420:
653 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300654 /* Frame stride must be multiple of 8 */
655 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
656 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300657 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300658 break;
659 case V4L2_PIX_FMT_H264:
660 case V4L2_PIX_FMT_MPEG4:
661 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300662 f->fmt.pix.bytesperline = 0;
663 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300664 break;
665 default:
666 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300667 }
668
669 return 0;
670}
671
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300672static int coda_try_fmt_vid_cap(struct file *file, void *priv,
673 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300674{
Javier Martin186b2502012-07-26 05:53:35 -0300675 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300676 struct coda_codec *codec;
677 struct vb2_queue *src_vq;
678 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300679
Philipp Zabel918c66f2013-06-27 06:59:01 -0300680 /*
681 * If the source format is already fixed, try to find a codec that
682 * converts to the given destination format
683 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300684 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300685 if (vb2_is_streaming(src_vq)) {
686 struct coda_q_data *q_data_src;
687
688 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
689 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
690 f->fmt.pix.pixelformat);
691 if (!codec)
692 return -EINVAL;
693 } else {
694 /* Otherwise determine codec by encoded format, if possible */
695 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
696 f->fmt.pix.pixelformat);
697 }
Javier Martin186b2502012-07-26 05:53:35 -0300698
699 f->fmt.pix.colorspace = ctx->colorspace;
700
Philipp Zabel57625592013-09-30 10:34:51 -0300701 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300702 if (ret < 0)
703 return ret;
704
705 /* The h.264 decoder only returns complete 16x16 macroblocks */
706 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
707 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
708 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
709 f->fmt.pix.bytesperline = f->fmt.pix.width;
710 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
711 f->fmt.pix.height * 3 / 2;
712 }
713
714 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300715}
716
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300717static int coda_try_fmt_vid_out(struct file *file, void *priv,
718 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300719{
720 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300721 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300722
Philipp Zabelb96904e2013-05-23 10:42:58 -0300723 /* Determine codec by encoded format, returns NULL if raw or invalid */
724 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
725 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300726
727 if (!f->fmt.pix.colorspace)
728 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
729
Philipp Zabel57625592013-09-30 10:34:51 -0300730 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300731}
732
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300733static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300734{
735 struct coda_q_data *q_data;
736 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300737
Philipp Zabel14604e32014-07-11 06:36:22 -0300738 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300739 if (!vq)
740 return -EINVAL;
741
742 q_data = get_q_data(ctx, f->type);
743 if (!q_data)
744 return -EINVAL;
745
746 if (vb2_is_busy(vq)) {
747 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
748 return -EBUSY;
749 }
750
Philipp Zabelb96904e2013-05-23 10:42:58 -0300751 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300752 q_data->width = f->fmt.pix.width;
753 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300754 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300755 q_data->rect.left = 0;
756 q_data->rect.top = 0;
757 q_data->rect.width = f->fmt.pix.width;
758 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300759
760 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
761 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300762 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300763
764 return 0;
765}
766
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300767static int coda_s_fmt_vid_cap(struct file *file, void *priv,
768 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300769{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300770 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300771 int ret;
772
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300773 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300774 if (ret)
775 return ret;
776
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300777 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300778}
779
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300780static int coda_s_fmt_vid_out(struct file *file, void *priv,
781 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300782{
783 struct coda_ctx *ctx = fh_to_ctx(priv);
784 int ret;
785
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300786 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300787 if (ret)
788 return ret;
789
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300790 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300791 if (ret)
792 ctx->colorspace = f->fmt.pix.colorspace;
793
794 return ret;
795}
796
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300797static int coda_qbuf(struct file *file, void *priv,
798 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300799{
800 struct coda_ctx *ctx = fh_to_ctx(priv);
801
Philipp Zabel14604e32014-07-11 06:36:22 -0300802 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300803}
804
Philipp Zabel918c66f2013-06-27 06:59:01 -0300805static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
806 struct v4l2_buffer *buf)
807{
808 struct vb2_queue *src_vq;
809
Philipp Zabel14604e32014-07-11 06:36:22 -0300810 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300811
812 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
813 (buf->sequence == (ctx->qsequence - 1)));
814}
815
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300816static int coda_dqbuf(struct file *file, void *priv,
817 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300818{
819 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300820 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300821
Philipp Zabel14604e32014-07-11 06:36:22 -0300822 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300823
824 /* If this is the last capture buffer, emit an end-of-stream event */
825 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
826 coda_buf_is_end_of_stream(ctx, buf)) {
827 const struct v4l2_event eos_event = {
828 .type = V4L2_EVENT_EOS
829 };
830
831 v4l2_event_queue_fh(&ctx->fh, &eos_event);
832 }
833
834 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300835}
836
Philipp Zabel52c41672014-07-11 06:36:19 -0300837static int coda_g_selection(struct file *file, void *fh,
838 struct v4l2_selection *s)
839{
840 struct coda_ctx *ctx = fh_to_ctx(fh);
841 struct coda_q_data *q_data;
842 struct v4l2_rect r, *rsel;
843
844 q_data = get_q_data(ctx, s->type);
845 if (!q_data)
846 return -EINVAL;
847
848 r.left = 0;
849 r.top = 0;
850 r.width = q_data->width;
851 r.height = q_data->height;
852 rsel = &q_data->rect;
853
854 switch (s->target) {
855 case V4L2_SEL_TGT_CROP_DEFAULT:
856 case V4L2_SEL_TGT_CROP_BOUNDS:
857 rsel = &r;
858 /* fallthrough */
859 case V4L2_SEL_TGT_CROP:
860 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
861 return -EINVAL;
862 break;
863 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
864 case V4L2_SEL_TGT_COMPOSE_PADDED:
865 rsel = &r;
866 /* fallthrough */
867 case V4L2_SEL_TGT_COMPOSE:
868 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
869 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
870 return -EINVAL;
871 break;
872 default:
873 return -EINVAL;
874 }
875
876 s->r = *rsel;
877
878 return 0;
879}
880
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300881static int coda_try_decoder_cmd(struct file *file, void *fh,
882 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300883{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300884 if (dc->cmd != V4L2_DEC_CMD_STOP)
885 return -EINVAL;
886
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300887 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300888 return -EINVAL;
889
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300890 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300891 return -EINVAL;
892
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300893 return 0;
894}
895
896static int coda_decoder_cmd(struct file *file, void *fh,
897 struct v4l2_decoder_cmd *dc)
898{
899 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300900 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300901 int ret;
902
903 ret = coda_try_decoder_cmd(file, fh, dc);
904 if (ret < 0)
905 return ret;
906
907 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300908 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300909 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300910
911 /* Set the strem-end flag on this context */
912 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
913
Philipp Zabel89548442014-07-11 06:36:17 -0300914 if ((dev->devtype->product == CODA_960) &&
915 coda_isbusy(dev) &&
916 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
917 /* If this context is currently running, update the hardware flag */
918 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
919 }
Michael Olbrichf3497da2014-07-11 06:36:30 -0300920 ctx->prescan_failed = false;
921 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300922
Philipp Zabel918c66f2013-06-27 06:59:01 -0300923 return 0;
924}
925
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300926static int coda_subscribe_event(struct v4l2_fh *fh,
927 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300928{
929 switch (sub->type) {
930 case V4L2_EVENT_EOS:
931 return v4l2_event_subscribe(fh, sub, 0, NULL);
932 default:
933 return v4l2_ctrl_subscribe_event(fh, sub);
934 }
Javier Martin186b2502012-07-26 05:53:35 -0300935}
936
937static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300938 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300939
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300940 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
941 .vidioc_g_fmt_vid_cap = coda_g_fmt,
942 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
943 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300944
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300945 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
946 .vidioc_g_fmt_vid_out = coda_g_fmt,
947 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
948 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300949
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300950 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
951 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300952
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300953 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300954 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300955 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300956 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300957
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300958 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
959 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300960
Philipp Zabel52c41672014-07-11 06:36:19 -0300961 .vidioc_g_selection = coda_g_selection,
962
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300963 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300964 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300965
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300966 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300967 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300968};
969
Philipp Zabel918c66f2013-06-27 06:59:01 -0300970static int coda_start_decoding(struct coda_ctx *ctx);
971
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300972static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
973{
974 return kfifo_len(&ctx->bitstream_fifo);
975}
976
977static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
978{
979 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
980 struct coda_dev *dev = ctx->dev;
981 u32 rd_ptr;
982
983 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
984 kfifo->out = (kfifo->in & ~kfifo->mask) |
985 (rd_ptr - ctx->bitstream.paddr);
986 if (kfifo->out > kfifo->in)
987 kfifo->out -= kfifo->mask + 1;
988}
989
990static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
991{
992 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
993 struct coda_dev *dev = ctx->dev;
994 u32 rd_ptr, wr_ptr;
995
996 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
997 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
998 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
999 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1000}
1001
1002static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1003{
1004 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1005 struct coda_dev *dev = ctx->dev;
1006 u32 wr_ptr;
1007
1008 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1009 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1010}
1011
1012static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1013{
1014 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1015 u32 n;
1016
1017 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1018 if (n < src_size)
1019 return -ENOSPC;
1020
1021 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1022 ctx->bitstream.size, DMA_TO_DEVICE);
1023
Philipp Zabel846ced92014-07-11 06:36:31 -03001024 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001025
1026 return 0;
1027}
1028
1029static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1030 struct vb2_buffer *src_buf)
1031{
1032 int ret;
1033
1034 if (coda_get_bitstream_payload(ctx) +
1035 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1036 return false;
1037
1038 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1039 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1040 return true;
1041 }
1042
1043 ret = coda_bitstream_queue(ctx, src_buf);
1044 if (ret < 0) {
1045 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1046 return false;
1047 }
1048 /* Sync read pointer to device */
1049 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1050 coda_kfifo_sync_to_device_write(ctx);
1051
Philipp Zabel918c66f2013-06-27 06:59:01 -03001052 ctx->prescan_failed = false;
1053
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001054 return true;
1055}
1056
1057static void coda_fill_bitstream(struct coda_ctx *ctx)
1058{
1059 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001060 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001061
Philipp Zabel14604e32014-07-11 06:36:22 -03001062 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1063 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001064
1065 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001066 /*
1067 * Source buffer is queued in the bitstream ringbuffer;
1068 * queue the timestamp and mark source buffer as done
1069 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001070 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001071
1072 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1073 if (ts) {
1074 ts->sequence = src_buf->v4l2_buf.sequence;
1075 ts->timecode = src_buf->v4l2_buf.timecode;
1076 ts->timestamp = src_buf->v4l2_buf.timestamp;
1077 list_add_tail(&ts->list, &ctx->timestamp_list);
1078 }
1079
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001080 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1081 } else {
1082 break;
1083 }
1084 }
1085}
1086
Philipp Zabel89548442014-07-11 06:36:17 -03001087static void coda_set_gdi_regs(struct coda_ctx *ctx)
1088{
1089 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1090 struct coda_dev *dev = ctx->dev;
1091 int i;
1092
1093 for (i = 0; i < 16; i++)
1094 coda_write(dev, tiled_map->xy2ca_map[i],
1095 CODA9_GDI_XY2_CAS_0 + 4 * i);
1096 for (i = 0; i < 4; i++)
1097 coda_write(dev, tiled_map->xy2ba_map[i],
1098 CODA9_GDI_XY2_BA_0 + 4 * i);
1099 for (i = 0; i < 16; i++)
1100 coda_write(dev, tiled_map->xy2ra_map[i],
1101 CODA9_GDI_XY2_RAS_0 + 4 * i);
1102 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1103 for (i = 0; i < 32; i++)
1104 coda_write(dev, tiled_map->rbc2axi_map[i],
1105 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1106}
1107
Javier Martin186b2502012-07-26 05:53:35 -03001108/*
1109 * Mem-to-mem operations.
1110 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001111static int coda_prepare_decode(struct coda_ctx *ctx)
1112{
1113 struct vb2_buffer *dst_buf;
1114 struct coda_dev *dev = ctx->dev;
1115 struct coda_q_data *q_data_dst;
1116 u32 stridey, height;
1117 u32 picture_y, picture_cb, picture_cr;
1118
Philipp Zabel14604e32014-07-11 06:36:22 -03001119 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001120 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1121
1122 if (ctx->params.rot_mode & CODA_ROT_90) {
1123 stridey = q_data_dst->height;
1124 height = q_data_dst->width;
1125 } else {
1126 stridey = q_data_dst->width;
1127 height = q_data_dst->height;
1128 }
1129
1130 /* Try to copy source buffer contents into the bitstream ringbuffer */
1131 mutex_lock(&ctx->bitstream_mutex);
1132 coda_fill_bitstream(ctx);
1133 mutex_unlock(&ctx->bitstream_mutex);
1134
1135 if (coda_get_bitstream_payload(ctx) < 512 &&
1136 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1137 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1138 "bitstream payload: %d, skipping\n",
1139 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001140 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001141 return -EAGAIN;
1142 }
1143
1144 /* Run coda_start_decoding (again) if not yet initialized */
1145 if (!ctx->initialized) {
1146 int ret = coda_start_decoding(ctx);
1147 if (ret < 0) {
1148 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001149 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001150 return -EAGAIN;
1151 } else {
1152 ctx->initialized = 1;
1153 }
1154 }
1155
Philipp Zabel89548442014-07-11 06:36:17 -03001156 if (dev->devtype->product == CODA_960)
1157 coda_set_gdi_regs(ctx);
1158
Philipp Zabel918c66f2013-06-27 06:59:01 -03001159 /* Set rotator output */
1160 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1161 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1162 /* Switch Cr and Cb for YVU420 format */
1163 picture_cr = picture_y + stridey * height;
1164 picture_cb = picture_cr + stridey / 2 * height / 2;
1165 } else {
1166 picture_cb = picture_y + stridey * height;
1167 picture_cr = picture_cb + stridey / 2 * height / 2;
1168 }
Philipp Zabel89548442014-07-11 06:36:17 -03001169
1170 if (dev->devtype->product == CODA_960) {
1171 /*
1172 * The CODA960 seems to have an internal list of buffers with
1173 * 64 entries that includes the registered frame buffers as
1174 * well as the rotator buffer output.
1175 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1176 */
1177 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1178 CODA9_CMD_DEC_PIC_ROT_INDEX);
1179 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1180 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1181 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1182 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1183 } else {
1184 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1185 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1186 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1187 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1188 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001189 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1190 CODA_CMD_DEC_PIC_ROT_MODE);
1191
1192 switch (dev->devtype->product) {
1193 case CODA_DX6:
1194 /* TBD */
1195 case CODA_7541:
1196 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1197 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001198 case CODA_960:
1199 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1200 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001201 }
1202
1203 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1204
1205 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1206 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1207
1208 return 0;
1209}
1210
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001211static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001212{
Javier Martin186b2502012-07-26 05:53:35 -03001213 struct coda_q_data *q_data_src, *q_data_dst;
1214 struct vb2_buffer *src_buf, *dst_buf;
1215 struct coda_dev *dev = ctx->dev;
1216 int force_ipicture;
1217 int quant_param = 0;
1218 u32 picture_y, picture_cb, picture_cr;
1219 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1220 u32 dst_fourcc;
1221
Philipp Zabel14604e32014-07-11 06:36:22 -03001222 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1223 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001224 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1225 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001226 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001227
Philipp Zabel918c66f2013-06-27 06:59:01 -03001228 src_buf->v4l2_buf.sequence = ctx->osequence;
1229 dst_buf->v4l2_buf.sequence = ctx->osequence;
1230 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001231
1232 /*
1233 * Workaround coda firmware BUG that only marks the first
1234 * frame as IDR. This is a problem for some decoders that can't
1235 * recover when a frame is lost.
1236 */
1237 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1238 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1239 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1240 } else {
1241 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1242 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1243 }
1244
Philipp Zabel89548442014-07-11 06:36:17 -03001245 if (dev->devtype->product == CODA_960)
1246 coda_set_gdi_regs(ctx);
1247
Javier Martin186b2502012-07-26 05:53:35 -03001248 /*
1249 * Copy headers at the beginning of the first frame for H.264 only.
1250 * In MPEG4 they are already copied by the coda.
1251 */
1252 if (src_buf->v4l2_buf.sequence == 0) {
1253 pic_stream_buffer_addr =
1254 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1255 ctx->vpu_header_size[0] +
1256 ctx->vpu_header_size[1] +
1257 ctx->vpu_header_size[2];
1258 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1259 ctx->vpu_header_size[0] -
1260 ctx->vpu_header_size[1] -
1261 ctx->vpu_header_size[2];
1262 memcpy(vb2_plane_vaddr(dst_buf, 0),
1263 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1264 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1265 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1266 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1267 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1268 ctx->vpu_header_size[2]);
1269 } else {
1270 pic_stream_buffer_addr =
1271 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1272 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1273 }
1274
1275 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1276 force_ipicture = 1;
1277 switch (dst_fourcc) {
1278 case V4L2_PIX_FMT_H264:
1279 quant_param = ctx->params.h264_intra_qp;
1280 break;
1281 case V4L2_PIX_FMT_MPEG4:
1282 quant_param = ctx->params.mpeg4_intra_qp;
1283 break;
1284 default:
1285 v4l2_warn(&ctx->dev->v4l2_dev,
1286 "cannot set intra qp, fmt not supported\n");
1287 break;
1288 }
1289 } else {
1290 force_ipicture = 0;
1291 switch (dst_fourcc) {
1292 case V4L2_PIX_FMT_H264:
1293 quant_param = ctx->params.h264_inter_qp;
1294 break;
1295 case V4L2_PIX_FMT_MPEG4:
1296 quant_param = ctx->params.mpeg4_inter_qp;
1297 break;
1298 default:
1299 v4l2_warn(&ctx->dev->v4l2_dev,
1300 "cannot set inter qp, fmt not supported\n");
1301 break;
1302 }
1303 }
1304
1305 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001306 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001307 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1308
1309
1310 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001311 switch (q_data_src->fourcc) {
1312 case V4L2_PIX_FMT_YVU420:
1313 /* Switch Cb and Cr for YVU420 format */
1314 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1315 picture_cb = picture_cr + q_data_src->width / 2 *
1316 q_data_src->height / 2;
1317 break;
1318 case V4L2_PIX_FMT_YUV420:
1319 default:
1320 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1321 picture_cr = picture_cb + q_data_src->width / 2 *
1322 q_data_src->height / 2;
1323 break;
1324 }
Javier Martin186b2502012-07-26 05:53:35 -03001325
Philipp Zabel89548442014-07-11 06:36:17 -03001326 if (dev->devtype->product == CODA_960) {
1327 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1328 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1329 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1330
1331 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1332 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1333 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1334 } else {
1335 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1336 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1337 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1338 }
Javier Martin186b2502012-07-26 05:53:35 -03001339 coda_write(dev, force_ipicture << 1 & 0x2,
1340 CODA_CMD_ENC_PIC_OPTION);
1341
1342 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1343 coda_write(dev, pic_stream_buffer_size / 1024,
1344 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001345
1346 if (!ctx->streamon_out) {
1347 /* After streamoff on the output side, set the stream end flag */
1348 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1349 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1350 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001351}
1352
1353static void coda_device_run(void *m2m_priv)
1354{
1355 struct coda_ctx *ctx = m2m_priv;
1356 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001357
1358 queue_work(dev->workqueue, &ctx->pic_run_work);
1359}
1360
1361static void coda_free_framebuffers(struct coda_ctx *ctx);
1362static void coda_free_context_buffers(struct coda_ctx *ctx);
1363
1364static void coda_seq_end_work(struct work_struct *work)
1365{
1366 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1367 struct coda_dev *dev = ctx->dev;
1368
1369 mutex_lock(&ctx->buffer_mutex);
1370 mutex_lock(&dev->coda_mutex);
1371
1372 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1373 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1374 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1375 v4l2_err(&dev->v4l2_dev,
1376 "CODA_COMMAND_SEQ_END failed\n");
1377 }
1378
1379 kfifo_init(&ctx->bitstream_fifo,
1380 ctx->bitstream.vaddr, ctx->bitstream.size);
1381
1382 coda_free_framebuffers(ctx);
1383 coda_free_context_buffers(ctx);
1384
1385 mutex_unlock(&dev->coda_mutex);
1386 mutex_unlock(&ctx->buffer_mutex);
1387}
1388
1389static void coda_finish_decode(struct coda_ctx *ctx);
1390static void coda_finish_encode(struct coda_ctx *ctx);
1391
1392static void coda_pic_run_work(struct work_struct *work)
1393{
1394 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1395 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001396 int ret;
1397
1398 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001399 mutex_lock(&dev->coda_mutex);
1400
Philipp Zabel918c66f2013-06-27 06:59:01 -03001401 if (ctx->inst_type == CODA_INST_DECODER) {
1402 ret = coda_prepare_decode(ctx);
1403 if (ret < 0) {
1404 mutex_unlock(&dev->coda_mutex);
1405 mutex_unlock(&ctx->buffer_mutex);
1406 /* job_finish scheduled by prepare_decode */
1407 return;
1408 }
1409 } else {
1410 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001411 }
1412
Philipp Zabelc2d22512013-06-21 03:55:28 -03001413 if (dev->devtype->product != CODA_DX6)
1414 coda_write(dev, ctx->iram_info.axi_sram_use,
1415 CODA7_REG_BIT_AXI_SRAM_USE);
1416
Philipp Zabel918c66f2013-06-27 06:59:01 -03001417 if (ctx->inst_type == CODA_INST_DECODER)
1418 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001419 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001420
1421 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1422 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1423 } else if (!ctx->aborting) {
1424 if (ctx->inst_type == CODA_INST_DECODER)
1425 coda_finish_decode(ctx);
1426 else
1427 coda_finish_encode(ctx);
1428 }
1429
1430 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1431 queue_work(dev->workqueue, &ctx->seq_end_work);
1432
1433 mutex_unlock(&dev->coda_mutex);
1434 mutex_unlock(&ctx->buffer_mutex);
1435
Philipp Zabel14604e32014-07-11 06:36:22 -03001436 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001437}
1438
1439static int coda_job_ready(void *m2m_priv)
1440{
1441 struct coda_ctx *ctx = m2m_priv;
1442
1443 /*
1444 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001445 * and 1 frame are needed. In the decoder case,
1446 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001447 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001448 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001449 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001450 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1451 "not ready: not enough video buffers.\n");
1452 return 0;
1453 }
1454
Philipp Zabel14604e32014-07-11 06:36:22 -03001455 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001456 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1457 "not ready: not enough video capture buffers.\n");
1458 return 0;
1459 }
1460
Philipp Zabel918c66f2013-06-27 06:59:01 -03001461 if (ctx->prescan_failed ||
1462 ((ctx->inst_type == CODA_INST_DECODER) &&
1463 (coda_get_bitstream_payload(ctx) < 512) &&
1464 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1465 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1466 "%d: not ready: not enough bitstream data.\n",
1467 ctx->idx);
1468 return 0;
1469 }
1470
Philipp Zabel3e748262013-05-23 10:43:01 -03001471 if (ctx->aborting) {
1472 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1473 "not ready: aborting\n");
1474 return 0;
1475 }
1476
Javier Martin186b2502012-07-26 05:53:35 -03001477 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1478 "job ready\n");
1479 return 1;
1480}
1481
1482static void coda_job_abort(void *priv)
1483{
1484 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001485
1486 ctx->aborting = 1;
1487
1488 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1489 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001490}
1491
1492static void coda_lock(void *m2m_priv)
1493{
1494 struct coda_ctx *ctx = m2m_priv;
1495 struct coda_dev *pcdev = ctx->dev;
1496 mutex_lock(&pcdev->dev_mutex);
1497}
1498
1499static void coda_unlock(void *m2m_priv)
1500{
1501 struct coda_ctx *ctx = m2m_priv;
1502 struct coda_dev *pcdev = ctx->dev;
1503 mutex_unlock(&pcdev->dev_mutex);
1504}
1505
1506static struct v4l2_m2m_ops coda_m2m_ops = {
1507 .device_run = coda_device_run,
1508 .job_ready = coda_job_ready,
1509 .job_abort = coda_job_abort,
1510 .lock = coda_lock,
1511 .unlock = coda_unlock,
1512};
1513
Philipp Zabel89548442014-07-11 06:36:17 -03001514static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1515{
1516 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1517 int luma_map, chro_map, i;
1518
1519 memset(tiled_map, 0, sizeof(*tiled_map));
1520
1521 luma_map = 64;
1522 chro_map = 64;
1523 tiled_map->map_type = tiled_map_type;
1524 for (i = 0; i < 16; i++)
1525 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1526 for (i = 0; i < 4; i++)
1527 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1528 for (i = 0; i < 16; i++)
1529 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1530
1531 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1532 tiled_map->xy2rbc_config = 0;
1533 } else {
1534 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1535 tiled_map_type);
1536 return;
1537 }
1538}
1539
Javier Martin186b2502012-07-26 05:53:35 -03001540static void set_default_params(struct coda_ctx *ctx)
1541{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001542 int max_w;
1543 int max_h;
1544
1545 ctx->codec = &ctx->dev->devtype->codecs[0];
1546 max_w = ctx->codec->max_w;
1547 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001548
1549 ctx->params.codec_mode = CODA_MODE_INVALID;
1550 ctx->colorspace = V4L2_COLORSPACE_REC709;
1551 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001552 ctx->aborting = 0;
1553
1554 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001555 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1556 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1557 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1558 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1559 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1560 ctx->q_data[V4L2_M2M_DST].width = max_w;
1561 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001562 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001563 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1564 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1565 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1566 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001567
1568 if (ctx->dev->devtype->product == CODA_960)
1569 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001570}
1571
1572/*
1573 * Queue operations
1574 */
1575static int coda_queue_setup(struct vb2_queue *vq,
1576 const struct v4l2_format *fmt,
1577 unsigned int *nbuffers, unsigned int *nplanes,
1578 unsigned int sizes[], void *alloc_ctxs[])
1579{
1580 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001581 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001582 unsigned int size;
1583
Philipp Zabele34db062012-08-29 08:22:00 -03001584 q_data = get_q_data(ctx, vq->type);
1585 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001586
1587 *nplanes = 1;
1588 sizes[0] = size;
1589
1590 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1591
1592 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1593 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1594
1595 return 0;
1596}
1597
1598static int coda_buf_prepare(struct vb2_buffer *vb)
1599{
1600 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1601 struct coda_q_data *q_data;
1602
1603 q_data = get_q_data(ctx, vb->vb2_queue->type);
1604
1605 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1606 v4l2_warn(&ctx->dev->v4l2_dev,
1607 "%s data will not fit into plane (%lu < %lu)\n",
1608 __func__, vb2_plane_size(vb, 0),
1609 (long)q_data->sizeimage);
1610 return -EINVAL;
1611 }
1612
Javier Martin186b2502012-07-26 05:53:35 -03001613 return 0;
1614}
1615
1616static void coda_buf_queue(struct vb2_buffer *vb)
1617{
1618 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001619 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001620 struct coda_q_data *q_data;
1621
1622 q_data = get_q_data(ctx, vb->vb2_queue->type);
1623
1624 /*
1625 * In the decoder case, immediately try to copy the buffer into the
1626 * bitstream ringbuffer and mark it as ready to be dequeued.
1627 */
1628 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1629 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1630 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001631 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001632 * the stream end
1633 */
Philipp Zabel89548442014-07-11 06:36:17 -03001634 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001635 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001636 if ((dev->devtype->product == CODA_960) &&
1637 coda_isbusy(dev) &&
1638 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1639 /* if this decoder instance is running, set the stream end flag */
1640 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1641 }
1642 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001643 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001644 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001645 coda_fill_bitstream(ctx);
1646 mutex_unlock(&ctx->bitstream_mutex);
1647 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001648 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001649 }
Javier Martin186b2502012-07-26 05:53:35 -03001650}
1651
Philipp Zabel86eda902013-05-23 10:42:57 -03001652static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1653{
1654 struct coda_dev *dev = ctx->dev;
1655 u32 *p = ctx->parabuf.vaddr;
1656
1657 if (dev->devtype->product == CODA_DX6)
1658 p[index] = value;
1659 else
1660 p[index ^ 1] = value;
1661}
1662
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001663static int coda_alloc_aux_buf(struct coda_dev *dev,
1664 struct coda_aux_buf *buf, size_t size)
1665{
1666 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1667 GFP_KERNEL);
1668 if (!buf->vaddr)
1669 return -ENOMEM;
1670
1671 buf->size = size;
1672
1673 return 0;
1674}
1675
1676static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1677 struct coda_aux_buf *buf, size_t size)
1678{
1679 return coda_alloc_aux_buf(ctx->dev, buf, size);
1680}
1681
1682static void coda_free_aux_buf(struct coda_dev *dev,
1683 struct coda_aux_buf *buf)
1684{
1685 if (buf->vaddr) {
1686 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1687 buf->vaddr, buf->paddr);
1688 buf->vaddr = NULL;
1689 buf->size = 0;
1690 }
1691}
1692
1693static void coda_free_framebuffers(struct coda_ctx *ctx)
1694{
1695 int i;
1696
1697 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1698 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1699}
1700
Philipp Zabelec25f682012-07-20 08:54:29 -03001701static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1702{
1703 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001704 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001705 dma_addr_t paddr;
1706 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001707 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001708 int i;
1709
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001710 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1711 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001712 ysize = round_up(q_data->width, 8) * height;
1713
Philipp Zabelec25f682012-07-20 08:54:29 -03001714 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001715 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001716 size_t size;
1717
Philipp Zabelaed14b02014-07-11 06:36:15 -03001718 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001719 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1720 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001721 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001722 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1723 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001724 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001725 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001726 }
1727 }
1728
1729 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001730 for (i = 0; i < ctx->num_internal_frames; i++) {
1731 paddr = ctx->internal_frames[i].paddr;
1732 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1733 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1734 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001735
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001736 /* mvcol buffer for h.264 */
1737 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1738 dev->devtype->product != CODA_DX6)
1739 coda_parabuf_write(ctx, 96 + i,
1740 ctx->internal_frames[i].paddr +
1741 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001742 }
1743
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001744 /* mvcol buffer for mpeg4 */
1745 if ((dev->devtype->product != CODA_DX6) &&
1746 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1747 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1748 ysize + ysize/4 + ysize/4);
1749
Philipp Zabelec25f682012-07-20 08:54:29 -03001750 return 0;
1751}
1752
Javier Martin3f3f5c72012-10-29 05:20:29 -03001753static int coda_h264_padding(int size, char *p)
1754{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001755 int nal_size;
1756 int diff;
1757
Javier Martin832fbb52012-10-29 08:34:59 -03001758 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001759 if (diff == 0)
1760 return 0;
1761
Javier Martin832fbb52012-10-29 08:34:59 -03001762 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001763 memcpy(p, coda_filler_nal, nal_size);
1764
1765 /* Add rbsp stop bit and trailing at the end */
1766 *(p + nal_size - 1) = 0x80;
1767
1768 return nal_size;
1769}
1770
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001771static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1772{
1773 phys_addr_t ret;
1774
1775 size = round_up(size, 1024);
1776 if (size > iram->remaining)
1777 return 0;
1778 iram->remaining -= size;
1779
1780 ret = iram->next_paddr;
1781 iram->next_paddr += size;
1782
1783 return ret;
1784}
1785
Philipp Zabelc2d22512013-06-21 03:55:28 -03001786static void coda_setup_iram(struct coda_ctx *ctx)
1787{
1788 struct coda_iram_info *iram_info = &ctx->iram_info;
1789 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001790 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001791 int dbk_bits;
1792 int bit_bits;
1793 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001794
1795 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001796 iram_info->next_paddr = dev->iram.paddr;
1797 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001798
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001799 switch (dev->devtype->product) {
1800 case CODA_7541:
1801 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1802 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1803 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1804 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001805 case CODA_960:
1806 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1807 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1808 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1809 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001810 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001811 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001812 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001813
1814 if (ctx->inst_type == CODA_INST_ENCODER) {
1815 struct coda_q_data *q_data_src;
1816
1817 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1818 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1819
1820 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001821 if (dev->devtype->product == CODA_7541) {
1822 iram_info->search_ram_size = round_up(mb_width * 16 *
1823 36 + 2048, 1024);
1824 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1825 iram_info->search_ram_size);
1826 if (!iram_info->search_ram_paddr) {
1827 pr_err("IRAM is smaller than the search ram size\n");
1828 goto out;
1829 }
1830 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1831 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001832 }
1833
1834 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001835 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1836 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1837 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001838 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001839 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001840
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001841 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1842 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001843 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001844 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001845
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001846 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1847 if (!iram_info->buf_ip_ac_dc_use)
1848 goto out;
1849 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001850
Philipp Zabel8358e762013-06-21 03:55:32 -03001851 /* OVL and BTP disabled for encoder */
1852 } else if (ctx->inst_type == CODA_INST_DECODER) {
1853 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001854
1855 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1856 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001857
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001858 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1859 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1860 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001861 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001862 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001863
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001864 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1865 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001866 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001867 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001868
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001869 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1870 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001871 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001872 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001873
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001874 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001875 }
1876
1877out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001878 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1879 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1880 "IRAM smaller than needed\n");
1881
1882 if (dev->devtype->product == CODA_7541) {
1883 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001884 if (ctx->inst_type == CODA_INST_DECODER) {
1885 /* fw 1.4.50 */
1886 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1887 CODA7_USE_IP_ENABLE);
1888 } else {
1889 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001890 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1891 CODA7_USE_HOST_DBK_ENABLE |
1892 CODA7_USE_IP_ENABLE |
1893 CODA7_USE_DBK_ENABLE);
1894 }
1895 }
1896}
1897
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001898static void coda_free_context_buffers(struct coda_ctx *ctx)
1899{
1900 struct coda_dev *dev = ctx->dev;
1901
Philipp Zabel918c66f2013-06-27 06:59:01 -03001902 coda_free_aux_buf(dev, &ctx->slicebuf);
1903 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001904 if (dev->devtype->product != CODA_DX6)
1905 coda_free_aux_buf(dev, &ctx->workbuf);
1906}
1907
1908static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1909 struct coda_q_data *q_data)
1910{
1911 struct coda_dev *dev = ctx->dev;
1912 size_t size;
1913 int ret;
1914
1915 switch (dev->devtype->product) {
1916 case CODA_7541:
1917 size = CODA7_WORK_BUF_SIZE;
1918 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001919 case CODA_960:
1920 size = CODA9_WORK_BUF_SIZE;
1921 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1922 size += CODA9_PS_SAVE_SIZE;
1923 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001924 default:
1925 return 0;
1926 }
1927
Philipp Zabel918c66f2013-06-27 06:59:01 -03001928 if (ctx->psbuf.vaddr) {
1929 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1930 return -EBUSY;
1931 }
1932 if (ctx->slicebuf.vaddr) {
1933 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1934 return -EBUSY;
1935 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001936 if (ctx->workbuf.vaddr) {
1937 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1938 ret = -EBUSY;
1939 return -ENOMEM;
1940 }
1941
Philipp Zabel918c66f2013-06-27 06:59:01 -03001942 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1943 /* worst case slice size */
1944 size = (DIV_ROUND_UP(q_data->width, 16) *
1945 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1946 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1947 if (ret < 0) {
1948 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1949 ctx->slicebuf.size);
1950 return ret;
1951 }
1952 }
1953
1954 if (dev->devtype->product == CODA_7541) {
1955 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1956 if (ret < 0) {
1957 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1958 goto err;
1959 }
1960 }
1961
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001962 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1963 if (ret < 0) {
1964 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1965 ctx->workbuf.size);
1966 goto err;
1967 }
1968
1969 return 0;
1970
1971err:
1972 coda_free_context_buffers(ctx);
1973 return ret;
1974}
1975
Philipp Zabel918c66f2013-06-27 06:59:01 -03001976static int coda_start_decoding(struct coda_ctx *ctx)
1977{
1978 struct coda_q_data *q_data_src, *q_data_dst;
1979 u32 bitstream_buf, bitstream_size;
1980 struct coda_dev *dev = ctx->dev;
1981 int width, height;
1982 u32 src_fourcc;
1983 u32 val;
1984 int ret;
1985
1986 /* Start decoding */
1987 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1988 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1989 bitstream_buf = ctx->bitstream.paddr;
1990 bitstream_size = ctx->bitstream.size;
1991 src_fourcc = q_data_src->fourcc;
1992
1993 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1994
1995 /* Update coda bitstream read and write pointers from kfifo */
1996 coda_kfifo_sync_to_device_full(ctx);
1997
1998 ctx->display_idx = -1;
1999 ctx->frm_dis_flg = 0;
2000 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2001
2002 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2003 CODA_REG_BIT_BIT_STREAM_PARAM);
2004
2005 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2006 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2007 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002008 if ((dev->devtype->product == CODA_7541) ||
2009 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002010 val |= CODA_REORDER_ENABLE;
2011 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2012
2013 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002014 if (dev->devtype->product == CODA_960 &&
2015 src_fourcc == V4L2_PIX_FMT_MPEG4)
2016 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2017 else
2018 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002019 if (src_fourcc == V4L2_PIX_FMT_H264) {
2020 if (dev->devtype->product == CODA_7541) {
2021 coda_write(dev, ctx->psbuf.paddr,
2022 CODA_CMD_DEC_SEQ_PS_BB_START);
2023 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2024 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2025 }
Philipp Zabel89548442014-07-11 06:36:17 -03002026 if (dev->devtype->product == CODA_960) {
2027 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2028 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2029 }
2030 }
2031 if (dev->devtype->product != CODA_960) {
2032 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002033 }
2034
2035 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2036 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2037 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2038 return -ETIMEDOUT;
2039 }
2040
2041 /* Update kfifo out pointer from coda bitstream read pointer */
2042 coda_kfifo_sync_from_device(ctx);
2043
2044 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2045
2046 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2047 v4l2_err(&dev->v4l2_dev,
2048 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2049 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2050 return -EAGAIN;
2051 }
2052
2053 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2054 if (dev->devtype->product == CODA_DX6) {
2055 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2056 height = val & CODADX6_PICHEIGHT_MASK;
2057 } else {
2058 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2059 height = val & CODA7_PICHEIGHT_MASK;
2060 }
2061
2062 if (width > q_data_dst->width || height > q_data_dst->height) {
2063 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2064 width, height, q_data_dst->width, q_data_dst->height);
2065 return -EINVAL;
2066 }
2067
2068 width = round_up(width, 16);
2069 height = round_up(height, 16);
2070
2071 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2072 __func__, ctx->idx, width, height);
2073
Philipp Zabela500a932014-07-11 06:36:13 -03002074 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002075 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2076 v4l2_err(&dev->v4l2_dev,
2077 "not enough framebuffers to decode (%d < %d)\n",
2078 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2079 return -EINVAL;
2080 }
2081
Philipp Zabel52c41672014-07-11 06:36:19 -03002082 if (src_fourcc == V4L2_PIX_FMT_H264) {
2083 u32 left_right;
2084 u32 top_bottom;
2085
2086 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2087 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2088
2089 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2090 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2091 q_data_dst->rect.width = width - q_data_dst->rect.left -
2092 (left_right & 0x3ff);
2093 q_data_dst->rect.height = height - q_data_dst->rect.top -
2094 (top_bottom & 0x3ff);
2095 }
2096
Philipp Zabel918c66f2013-06-27 06:59:01 -03002097 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2098 if (ret < 0)
2099 return ret;
2100
2101 /* Tell the decoder how many frame buffers we allocated. */
2102 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2103 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2104
2105 if (dev->devtype->product != CODA_DX6) {
2106 /* Set secondary AXI IRAM */
2107 coda_setup_iram(ctx);
2108
2109 coda_write(dev, ctx->iram_info.buf_bit_use,
2110 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2111 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2112 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2113 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2114 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2115 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2116 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2117 coda_write(dev, ctx->iram_info.buf_ovl_use,
2118 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002119 if (dev->devtype->product == CODA_960)
2120 coda_write(dev, ctx->iram_info.buf_btp_use,
2121 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2122 }
2123
2124 if (dev->devtype->product == CODA_960) {
2125 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2126
2127 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2128 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2129 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2130 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2131 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2132 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002133 }
2134
2135 if (src_fourcc == V4L2_PIX_FMT_H264) {
2136 coda_write(dev, ctx->slicebuf.paddr,
2137 CODA_CMD_SET_FRAME_SLICE_BB_START);
2138 coda_write(dev, ctx->slicebuf.size / 1024,
2139 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2140 }
2141
2142 if (dev->devtype->product == CODA_7541) {
2143 int max_mb_x = 1920 / 16;
2144 int max_mb_y = 1088 / 16;
2145 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002146
Philipp Zabel918c66f2013-06-27 06:59:01 -03002147 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2148 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002149 } else if (dev->devtype->product == CODA_960) {
2150 int max_mb_x = 1920 / 16;
2151 int max_mb_y = 1088 / 16;
2152 int max_mb_num = max_mb_x * max_mb_y;
2153
2154 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2155 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002156 }
2157
2158 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2159 v4l2_err(&ctx->dev->v4l2_dev,
2160 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2161 return -ETIMEDOUT;
2162 }
2163
2164 return 0;
2165}
2166
Philipp Zabeld35167a2013-05-23 10:42:59 -03002167static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2168 int header_code, u8 *header, int *size)
2169{
2170 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002171 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002172 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002173 int i;
2174
2175 if (dev->devtype->product == CODA_960)
2176 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002177
2178 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2179 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002180 bufsize = vb2_plane_size(buf, 0);
2181 if (dev->devtype->product == CODA_960)
2182 bufsize /= 1024;
2183 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002184 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2185 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2186 if (ret < 0) {
2187 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2188 return ret;
2189 }
Philipp Zabel89548442014-07-11 06:36:17 -03002190
2191 if (dev->devtype->product == CODA_960) {
2192 for (i = 63; i > 0; i--)
2193 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2194 break;
2195 *size = i + 1;
2196 } else {
2197 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2198 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2199 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002200 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2201
2202 return 0;
2203}
2204
Philipp Zabel89548442014-07-11 06:36:17 -03002205static int coda_start_encoding(struct coda_ctx *ctx);
2206
Javier Martin186b2502012-07-26 05:53:35 -03002207static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2208{
2209 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2210 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002211 struct coda_dev *dev = ctx->dev;
2212 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002213 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002214 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002215
Philipp Zabelb96904e2013-05-23 10:42:58 -03002216 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002217 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2218 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2219 if (coda_get_bitstream_payload(ctx) < 512)
2220 return -EINVAL;
2221 } else {
2222 if (count < 1)
2223 return -EINVAL;
2224 }
2225
2226 ctx->streamon_out = 1;
2227
Philipp Zabelb96904e2013-05-23 10:42:58 -03002228 if (coda_format_is_yuv(q_data_src->fourcc))
2229 ctx->inst_type = CODA_INST_ENCODER;
2230 else
2231 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002232 } else {
2233 if (count < 1)
2234 return -EINVAL;
2235
2236 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002237 }
Javier Martin186b2502012-07-26 05:53:35 -03002238
Philipp Zabelb96904e2013-05-23 10:42:58 -03002239 /* Don't start the coda unless both queues are on */
2240 if (!(ctx->streamon_out & ctx->streamon_cap))
2241 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002242
Philipp Zabeleb107512013-09-30 10:34:45 -03002243 /* Allow decoder device_run with no new buffers queued */
2244 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002245 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002246
Philipp Zabelb96904e2013-05-23 10:42:58 -03002247 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002248 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002249 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002250
Philipp Zabelb96904e2013-05-23 10:42:58 -03002251 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2252 q_data_dst->fourcc);
2253 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002254 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2255 return -EINVAL;
2256 }
2257
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002258 /* Allocate per-instance buffers */
2259 ret = coda_alloc_context_buffers(ctx, q_data_src);
2260 if (ret < 0)
2261 return ret;
2262
Philipp Zabel918c66f2013-06-27 06:59:01 -03002263 if (ctx->inst_type == CODA_INST_DECODER) {
2264 mutex_lock(&dev->coda_mutex);
2265 ret = coda_start_decoding(ctx);
2266 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002267 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002268 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002269 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002270 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002271 } else {
2272 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002273 }
2274
Philipp Zabel89548442014-07-11 06:36:17 -03002275 ctx->initialized = 1;
2276 return ret;
2277}
2278
2279static int coda_start_encoding(struct coda_ctx *ctx)
2280{
2281 struct coda_dev *dev = ctx->dev;
2282 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2283 struct coda_q_data *q_data_src, *q_data_dst;
2284 u32 bitstream_buf, bitstream_size;
2285 struct vb2_buffer *buf;
2286 int gamma, ret, value;
2287 u32 dst_fourcc;
2288
2289 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2290 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2291 dst_fourcc = q_data_dst->fourcc;
2292
Philipp Zabel14604e32014-07-11 06:36:22 -03002293 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002294 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2295 bitstream_size = q_data_dst->sizeimage;
2296
Javier Martin186b2502012-07-26 05:53:35 -03002297 if (!coda_is_initialized(dev)) {
2298 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2299 return -EFAULT;
2300 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002301
2302 mutex_lock(&dev->coda_mutex);
2303
Javier Martin186b2502012-07-26 05:53:35 -03002304 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002305 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2306 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002307 switch (dev->devtype->product) {
2308 case CODA_DX6:
2309 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2310 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2311 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002312 case CODA_960:
2313 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2314 /* fallthrough */
2315 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002316 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2317 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002318 break;
Javier Martin186b2502012-07-26 05:53:35 -03002319 }
2320
Philipp Zabel89548442014-07-11 06:36:17 -03002321 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2322 value &= ~(1 << 2 | 0x7 << 9);
2323 ctx->frame_mem_ctrl = value;
2324 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2325
Philipp Zabel10436672012-07-02 09:03:55 -03002326 if (dev->devtype->product == CODA_DX6) {
2327 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002328 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002329 }
Javier Martin186b2502012-07-26 05:53:35 -03002330
2331 /* Could set rotation here if needed */
2332 switch (dev->devtype->product) {
2333 case CODA_DX6:
2334 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002335 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002336 break;
2337 default:
2338 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002339 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002340 }
Javier Martin186b2502012-07-26 05:53:35 -03002341 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2342 coda_write(dev, ctx->params.framerate,
2343 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2344
Philipp Zabelb96904e2013-05-23 10:42:58 -03002345 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002346 switch (dst_fourcc) {
2347 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002348 if (dev->devtype->product == CODA_960)
2349 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2350 else
2351 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002352 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2353 break;
2354 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002355 if (dev->devtype->product == CODA_960)
2356 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2357 else
2358 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002359 if (ctx->params.h264_deblk_enabled) {
2360 value = ((ctx->params.h264_deblk_alpha &
2361 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2362 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2363 ((ctx->params.h264_deblk_beta &
2364 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2365 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2366 } else {
2367 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2368 }
2369 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002370 break;
2371 default:
2372 v4l2_err(v4l2_dev,
2373 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002374 ret = -EINVAL;
2375 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002376 }
2377
Philipp Zabelc566c782012-08-08 11:59:38 -03002378 switch (ctx->params.slice_mode) {
2379 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2380 value = 0;
2381 break;
2382 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2383 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2384 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002385 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002386 break;
2387 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2388 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2389 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2390 value |= 1 & CODA_SLICING_MODE_MASK;
2391 break;
2392 }
Javier Martin186b2502012-07-26 05:53:35 -03002393 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002394 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002395 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2396
2397 if (ctx->params.bitrate) {
2398 /* Rate control enabled */
2399 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2400 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002401 if (dev->devtype->product == CODA_960)
2402 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002403 } else {
2404 value = 0;
2405 }
2406 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2407
2408 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002409 coda_write(dev, ctx->params.intra_refresh,
2410 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002411
2412 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2413 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2414
Javier Martin186b2502012-07-26 05:53:35 -03002415
Philipp Zabel89548442014-07-11 06:36:17 -03002416 value = 0;
2417 if (dev->devtype->product == CODA_960)
2418 gamma = CODA9_DEFAULT_GAMMA;
2419 else
2420 gamma = CODA_DEFAULT_GAMMA;
2421 if (gamma > 0) {
2422 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2423 CODA_CMD_ENC_SEQ_RC_GAMMA);
2424 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002425
2426 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2427 coda_write(dev,
2428 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2429 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2430 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2431 }
Philipp Zabel89548442014-07-11 06:36:17 -03002432 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002433 if (ctx->params.h264_max_qp)
2434 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002435 if (CODA_DEFAULT_GAMMA > 0)
2436 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002437 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002438 if (CODA_DEFAULT_GAMMA > 0) {
2439 if (dev->devtype->product == CODA_DX6)
2440 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2441 else
2442 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2443 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002444 if (ctx->params.h264_min_qp)
2445 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2446 if (ctx->params.h264_max_qp)
2447 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002448 }
Javier Martin186b2502012-07-26 05:53:35 -03002449 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2450
Philipp Zabel89548442014-07-11 06:36:17 -03002451 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2452
Philipp Zabelc2d22512013-06-21 03:55:28 -03002453 coda_setup_iram(ctx);
2454
Javier Martin186b2502012-07-26 05:53:35 -03002455 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002456 switch (dev->devtype->product) {
2457 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002458 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002459 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002460 break;
2461 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002462 coda_write(dev, ctx->iram_info.search_ram_paddr,
2463 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2464 coda_write(dev, ctx->iram_info.search_ram_size,
2465 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002466 break;
2467 case CODA_960:
2468 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2469 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002470 }
Javier Martin186b2502012-07-26 05:53:35 -03002471 }
2472
Philipp Zabelfcb62822013-05-23 10:43:00 -03002473 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2474 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002475 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002476 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002477 }
2478
Philipp Zabelfcb62822013-05-23 10:43:00 -03002479 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2480 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2481 ret = -EFAULT;
2482 goto out;
2483 }
Javier Martin186b2502012-07-26 05:53:35 -03002484
Philipp Zabel89548442014-07-11 06:36:17 -03002485 if (dev->devtype->product == CODA_960)
2486 ctx->num_internal_frames = 4;
2487 else
2488 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002489 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002490 if (ret < 0) {
2491 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2492 goto out;
2493 }
Javier Martin186b2502012-07-26 05:53:35 -03002494
Philipp Zabelec25f682012-07-20 08:54:29 -03002495 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002496 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002497 if (dev->devtype->product == CODA_7541)
2498 coda_write(dev, round_up(q_data_src->width, 8),
2499 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002500 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002501 coda_write(dev, ctx->iram_info.buf_bit_use,
2502 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2503 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2504 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2505 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2506 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2507 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2508 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2509 coda_write(dev, ctx->iram_info.buf_ovl_use,
2510 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002511 if (dev->devtype->product == CODA_960) {
2512 coda_write(dev, ctx->iram_info.buf_btp_use,
2513 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2514
2515 /* FIXME */
2516 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2517 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2518 }
Philipp Zabel10436672012-07-02 09:03:55 -03002519 }
Philipp Zabel89548442014-07-11 06:36:17 -03002520
Philipp Zabelfcb62822013-05-23 10:43:00 -03002521 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2522 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002523 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002524 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002525 }
2526
2527 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002528 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002529 switch (dst_fourcc) {
2530 case V4L2_PIX_FMT_H264:
2531 /*
2532 * Get SPS in the first frame and copy it to an
2533 * intermediate buffer.
2534 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002535 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2536 &ctx->vpu_header[0][0],
2537 &ctx->vpu_header_size[0]);
2538 if (ret < 0)
2539 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002540
2541 /*
2542 * Get PPS in the first frame and copy it to an
2543 * intermediate buffer.
2544 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002545 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2546 &ctx->vpu_header[1][0],
2547 &ctx->vpu_header_size[1]);
2548 if (ret < 0)
2549 goto out;
2550
Javier Martin3f3f5c72012-10-29 05:20:29 -03002551 /*
2552 * Length of H.264 headers is variable and thus it might not be
2553 * aligned for the coda to append the encoded frame. In that is
2554 * the case a filler NAL must be added to header 2.
2555 */
2556 ctx->vpu_header_size[2] = coda_h264_padding(
2557 (ctx->vpu_header_size[0] +
2558 ctx->vpu_header_size[1]),
2559 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002560 break;
2561 case V4L2_PIX_FMT_MPEG4:
2562 /*
2563 * Get VOS in the first frame and copy it to an
2564 * intermediate buffer
2565 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002566 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2567 &ctx->vpu_header[0][0],
2568 &ctx->vpu_header_size[0]);
2569 if (ret < 0)
2570 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002571
Philipp Zabeld35167a2013-05-23 10:42:59 -03002572 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2573 &ctx->vpu_header[1][0],
2574 &ctx->vpu_header_size[1]);
2575 if (ret < 0)
2576 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002577
Philipp Zabeld35167a2013-05-23 10:42:59 -03002578 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2579 &ctx->vpu_header[2][0],
2580 &ctx->vpu_header_size[2]);
2581 if (ret < 0)
2582 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002583 break;
2584 default:
2585 /* No more formats need to save headers at the moment */
2586 break;
2587 }
2588
Philipp Zabeld35167a2013-05-23 10:42:59 -03002589out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002590 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002591 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002592}
2593
Hans Verkuile37559b2014-04-17 02:47:21 -03002594static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002595{
2596 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002597 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002598
2599 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002600 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002601 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002602 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002603
Philipp Zabel89548442014-07-11 06:36:17 -03002604 if (ctx->inst_type == CODA_INST_DECODER &&
2605 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2606 /* if this decoder instance is running, set the stream end flag */
2607 if (dev->devtype->product == CODA_960) {
2608 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2609
2610 val |= CODA_BIT_STREAM_END_FLAG;
2611 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2612 ctx->bit_stream_param = val;
2613 }
2614 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002615 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2616
2617 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002618 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002619 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002620 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002621 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002622
2623 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002624 }
2625
Philipp Zabel918c66f2013-06-27 06:59:01 -03002626 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002627 struct coda_timestamp *ts;
2628
2629 while (!list_empty(&ctx->timestamp_list)) {
2630 ts = list_first_entry(&ctx->timestamp_list,
2631 struct coda_timestamp, list);
2632 list_del(&ts->list);
2633 kfree(ts);
2634 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002635 kfifo_init(&ctx->bitstream_fifo,
2636 ctx->bitstream.vaddr, ctx->bitstream.size);
2637 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002638 }
Javier Martin186b2502012-07-26 05:53:35 -03002639}
2640
2641static struct vb2_ops coda_qops = {
2642 .queue_setup = coda_queue_setup,
2643 .buf_prepare = coda_buf_prepare,
2644 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002645 .start_streaming = coda_start_streaming,
2646 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002647 .wait_prepare = vb2_ops_wait_prepare,
2648 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002649};
2650
2651static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2652{
2653 struct coda_ctx *ctx =
2654 container_of(ctrl->handler, struct coda_ctx, ctrls);
2655
2656 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2657 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2658
2659 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002660 case V4L2_CID_HFLIP:
2661 if (ctrl->val)
2662 ctx->params.rot_mode |= CODA_MIR_HOR;
2663 else
2664 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2665 break;
2666 case V4L2_CID_VFLIP:
2667 if (ctrl->val)
2668 ctx->params.rot_mode |= CODA_MIR_VER;
2669 else
2670 ctx->params.rot_mode &= ~CODA_MIR_VER;
2671 break;
Javier Martin186b2502012-07-26 05:53:35 -03002672 case V4L2_CID_MPEG_VIDEO_BITRATE:
2673 ctx->params.bitrate = ctrl->val / 1000;
2674 break;
2675 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2676 ctx->params.gop_size = ctrl->val;
2677 break;
2678 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2679 ctx->params.h264_intra_qp = ctrl->val;
2680 break;
2681 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2682 ctx->params.h264_inter_qp = ctrl->val;
2683 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002684 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2685 ctx->params.h264_min_qp = ctrl->val;
2686 break;
2687 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2688 ctx->params.h264_max_qp = ctrl->val;
2689 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002690 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2691 ctx->params.h264_deblk_alpha = ctrl->val;
2692 break;
2693 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2694 ctx->params.h264_deblk_beta = ctrl->val;
2695 break;
2696 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2697 ctx->params.h264_deblk_enabled = (ctrl->val ==
2698 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2699 break;
Javier Martin186b2502012-07-26 05:53:35 -03002700 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2701 ctx->params.mpeg4_intra_qp = ctrl->val;
2702 break;
2703 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2704 ctx->params.mpeg4_inter_qp = ctrl->val;
2705 break;
2706 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2707 ctx->params.slice_mode = ctrl->val;
2708 break;
2709 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2710 ctx->params.slice_max_mb = ctrl->val;
2711 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002712 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2713 ctx->params.slice_max_bits = ctrl->val * 8;
2714 break;
Javier Martin186b2502012-07-26 05:53:35 -03002715 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2716 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002717 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2718 ctx->params.intra_refresh = ctrl->val;
2719 break;
Javier Martin186b2502012-07-26 05:53:35 -03002720 default:
2721 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2722 "Invalid control, id=%d, val=%d\n",
2723 ctrl->id, ctrl->val);
2724 return -EINVAL;
2725 }
2726
2727 return 0;
2728}
2729
2730static struct v4l2_ctrl_ops coda_ctrl_ops = {
2731 .s_ctrl = coda_s_ctrl,
2732};
2733
2734static int coda_ctrls_setup(struct coda_ctx *ctx)
2735{
2736 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2737
2738 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002739 V4L2_CID_HFLIP, 0, 1, 1, 0);
2740 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2741 V4L2_CID_VFLIP, 0, 1, 1, 0);
2742 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002743 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2744 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2745 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2746 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002747 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002748 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002749 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002750 if (ctx->dev->devtype->product != CODA_960) {
2751 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2752 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2753 }
2754 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2755 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002756 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002757 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2758 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2759 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2760 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2761 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2762 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2763 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2764 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002765 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2766 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2767 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2768 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2769 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002770 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2771 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002772 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2773 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002774 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2775 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002776 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2777 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2778 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2779 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2780 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002781 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2782 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002783
2784 if (ctx->ctrls.error) {
2785 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2786 ctx->ctrls.error);
2787 return -EINVAL;
2788 }
2789
2790 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2791}
2792
2793static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2794 struct vb2_queue *dst_vq)
2795{
2796 struct coda_ctx *ctx = priv;
2797 int ret;
2798
Javier Martin186b2502012-07-26 05:53:35 -03002799 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002800 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002801 src_vq->drv_priv = ctx;
2802 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2803 src_vq->ops = &coda_qops;
2804 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002805 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002806 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002807
2808 ret = vb2_queue_init(src_vq);
2809 if (ret)
2810 return ret;
2811
Javier Martin186b2502012-07-26 05:53:35 -03002812 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002813 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002814 dst_vq->drv_priv = ctx;
2815 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2816 dst_vq->ops = &coda_qops;
2817 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002818 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002819 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002820
2821 return vb2_queue_init(dst_vq);
2822}
2823
Philipp Zabele11f3e62012-07-25 09:16:58 -03002824static int coda_next_free_instance(struct coda_dev *dev)
2825{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002826 int idx = ffz(dev->instance_mask);
2827
2828 if ((idx < 0) ||
2829 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2830 return -EBUSY;
2831
2832 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002833}
2834
Javier Martin186b2502012-07-26 05:53:35 -03002835static int coda_open(struct file *file)
2836{
2837 struct coda_dev *dev = video_drvdata(file);
2838 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002839 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002840 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002841
Javier Martin186b2502012-07-26 05:53:35 -03002842 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2843 if (!ctx)
2844 return -ENOMEM;
2845
Fabio Estevamf82bc202013-08-21 11:14:16 -03002846 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002847 if (idx < 0) {
2848 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002849 goto err_coda_max;
2850 }
2851 set_bit(idx, &dev->instance_mask);
2852
Philipp Zabel32b6f202014-07-11 06:36:20 -03002853 init_completion(&ctx->completion);
2854 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2855 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002856 v4l2_fh_init(&ctx->fh, video_devdata(file));
2857 file->private_data = &ctx->fh;
2858 v4l2_fh_add(&ctx->fh);
2859 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002860 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002861 switch (dev->devtype->product) {
2862 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002863 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002864 ctx->reg_idx = 0;
2865 break;
2866 default:
2867 ctx->reg_idx = idx;
2868 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002869
Philipp Zabel1e172732014-07-11 06:36:23 -03002870 /* Power up and upload firmware if necessary */
2871 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2872 if (ret < 0) {
2873 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2874 goto err_pm_get;
2875 }
2876
Fabio Estevam79830db2013-08-21 11:14:17 -03002877 ret = clk_prepare_enable(dev->clk_per);
2878 if (ret)
2879 goto err_clk_per;
2880
2881 ret = clk_prepare_enable(dev->clk_ahb);
2882 if (ret)
2883 goto err_clk_ahb;
2884
Javier Martin186b2502012-07-26 05:53:35 -03002885 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002886 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002887 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002888 if (IS_ERR(ctx->fh.m2m_ctx)) {
2889 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002890
2891 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2892 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002893 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002894 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002895
Javier Martin186b2502012-07-26 05:53:35 -03002896 ret = coda_ctrls_setup(ctx);
2897 if (ret) {
2898 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002899 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002900 }
2901
2902 ctx->fh.ctrl_handler = &ctx->ctrls;
2903
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002904 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2905 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002906 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002907 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002908 }
2909
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002910 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2911 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2912 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2913 if (!ctx->bitstream.vaddr) {
2914 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2915 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002916 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002917 }
2918 kfifo_init(&ctx->bitstream_fifo,
2919 ctx->bitstream.vaddr, ctx->bitstream.size);
2920 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002921 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002922 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002923
Javier Martin186b2502012-07-26 05:53:35 -03002924 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002925 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002926 coda_unlock(ctx);
2927
Javier Martin186b2502012-07-26 05:53:35 -03002928 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2929 ctx->idx, ctx);
2930
2931 return 0;
2932
Fabio Estevamf82bc202013-08-21 11:14:16 -03002933err_dma_writecombine:
2934 coda_free_context_buffers(ctx);
2935 if (ctx->dev->devtype->product == CODA_DX6)
2936 coda_free_aux_buf(dev, &ctx->workbuf);
2937 coda_free_aux_buf(dev, &ctx->parabuf);
2938err_dma_alloc:
2939 v4l2_ctrl_handler_free(&ctx->ctrls);
2940err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002941 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002942err_ctx_init:
2943 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002944err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002945 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002946err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002947 pm_runtime_put_sync(&dev->plat_dev->dev);
2948err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002949 v4l2_fh_del(&ctx->fh);
2950 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002951 clear_bit(ctx->idx, &dev->instance_mask);
2952err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002953 kfree(ctx);
2954 return ret;
2955}
2956
2957static int coda_release(struct file *file)
2958{
2959 struct coda_dev *dev = video_drvdata(file);
2960 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2961
2962 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2963 ctx);
2964
Philipp Zabel918c66f2013-06-27 06:59:01 -03002965 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002966 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002967
2968 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002969 if (ctx->initialized) {
2970 queue_work(dev->workqueue, &ctx->seq_end_work);
2971 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002972 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002973
2974 coda_free_framebuffers(ctx);
2975
Javier Martin186b2502012-07-26 05:53:35 -03002976 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002977 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002978 coda_unlock(ctx);
2979
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002980 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2981 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002982 coda_free_context_buffers(ctx);
2983 if (ctx->dev->devtype->product == CODA_DX6)
2984 coda_free_aux_buf(dev, &ctx->workbuf);
2985
2986 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002987 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002988 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002989 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002990 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002991 v4l2_fh_del(&ctx->fh);
2992 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002993 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002994 kfree(ctx);
2995
2996 return 0;
2997}
2998
Javier Martin186b2502012-07-26 05:53:35 -03002999static const struct v4l2_file_operations coda_fops = {
3000 .owner = THIS_MODULE,
3001 .open = coda_open,
3002 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003003 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003004 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003005 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003006};
3007
Philipp Zabel918c66f2013-06-27 06:59:01 -03003008static void coda_finish_decode(struct coda_ctx *ctx)
3009{
3010 struct coda_dev *dev = ctx->dev;
3011 struct coda_q_data *q_data_src;
3012 struct coda_q_data *q_data_dst;
3013 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003014 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003015 int width, height;
3016 int decoded_idx;
3017 int display_idx;
3018 u32 src_fourcc;
3019 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003020 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003021 u32 val;
3022
Philipp Zabel14604e32014-07-11 06:36:22 -03003023 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003024
3025 /* Update kfifo out pointer from coda bitstream read pointer */
3026 coda_kfifo_sync_from_device(ctx);
3027
3028 /*
3029 * in stream-end mode, the read pointer can overshoot the write pointer
3030 * by up to 512 bytes
3031 */
3032 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3033 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3034 kfifo_init(&ctx->bitstream_fifo,
3035 ctx->bitstream.vaddr, ctx->bitstream.size);
3036 }
3037
3038 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3039 src_fourcc = q_data_src->fourcc;
3040
3041 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3042 if (val != 1)
3043 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3044
3045 success = val & 0x1;
3046 if (!success)
3047 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3048
3049 if (src_fourcc == V4L2_PIX_FMT_H264) {
3050 if (val & (1 << 3))
3051 v4l2_err(&dev->v4l2_dev,
3052 "insufficient PS buffer space (%d bytes)\n",
3053 ctx->psbuf.size);
3054 if (val & (1 << 2))
3055 v4l2_err(&dev->v4l2_dev,
3056 "insufficient slice buffer space (%d bytes)\n",
3057 ctx->slicebuf.size);
3058 }
3059
3060 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3061 width = (val >> 16) & 0xffff;
3062 height = val & 0xffff;
3063
3064 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3065
Philipp Zabel52c41672014-07-11 06:36:19 -03003066 /* frame crop information */
3067 if (src_fourcc == V4L2_PIX_FMT_H264) {
3068 u32 left_right;
3069 u32 top_bottom;
3070
3071 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3072 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3073
3074 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3075 /* Keep current crop information */
3076 } else {
3077 struct v4l2_rect *rect = &q_data_dst->rect;
3078
3079 rect->left = left_right >> 16 & 0xffff;
3080 rect->top = top_bottom >> 16 & 0xffff;
3081 rect->width = width - rect->left -
3082 (left_right & 0xffff);
3083 rect->height = height - rect->top -
3084 (top_bottom & 0xffff);
3085 }
3086 } else {
3087 /* no cropping */
3088 }
3089
Philipp Zabel410e5e42014-07-11 06:36:32 -03003090 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3091 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003092 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003093 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003094
3095 if (dev->devtype->product == CODA_7541) {
3096 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3097 if (val == 0) {
3098 /* not enough bitstream data */
3099 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3100 "prescan failed: %d\n", val);
3101 ctx->prescan_failed = true;
3102 return;
3103 }
3104 }
3105
3106 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3107
3108 /*
3109 * The previous display frame was copied out by the rotator,
3110 * now it can be overwritten again
3111 */
3112 if (ctx->display_idx >= 0 &&
3113 ctx->display_idx < ctx->num_internal_frames) {
3114 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3115 coda_write(dev, ctx->frm_dis_flg,
3116 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3117 }
3118
3119 /*
3120 * The index of the last decoded frame, not necessarily in
3121 * display order, and the index of the next display frame.
3122 * The latter could have been decoded in a previous run.
3123 */
3124 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3125 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3126
3127 if (decoded_idx == -1) {
3128 /* no frame was decoded, but we might have a display frame */
3129 if (display_idx < 0 && ctx->display_idx < 0)
3130 ctx->prescan_failed = true;
3131 } else if (decoded_idx == -2) {
3132 /* no frame was decoded, we still return the remaining buffers */
3133 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3134 v4l2_err(&dev->v4l2_dev,
3135 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003136 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003137 ts = list_first_entry(&ctx->timestamp_list,
3138 struct coda_timestamp, list);
3139 list_del(&ts->list);
3140 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
3141 if (val != ts->sequence) {
3142 v4l2_err(&dev->v4l2_dev,
3143 "sequence number mismatch (%d != %d)\n",
3144 val, ts->sequence);
3145 }
3146 ctx->frame_timestamps[decoded_idx] = *ts;
3147 kfree(ts);
3148
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003149 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3150 if (val == 0)
3151 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3152 else if (val == 1)
3153 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3154 else
3155 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003156
3157 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003158 }
3159
3160 if (display_idx == -1) {
3161 /*
3162 * no more frames to be decoded, but there could still
3163 * be rotator output to dequeue
3164 */
3165 ctx->prescan_failed = true;
3166 } else if (display_idx == -3) {
3167 /* possibly prescan failure */
3168 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3169 v4l2_err(&dev->v4l2_dev,
3170 "presentation frame index out of range: %d\n",
3171 display_idx);
3172 }
3173
3174 /* If a frame was copied out, return it */
3175 if (ctx->display_idx >= 0 &&
3176 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003177 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003178 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3179
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003180 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3181 V4L2_BUF_FLAG_PFRAME |
3182 V4L2_BUF_FLAG_BFRAME);
3183 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003184 ts = &ctx->frame_timestamps[ctx->display_idx];
3185 dst_buf->v4l2_buf.timecode = ts->timecode;
3186 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003187
Philipp Zabel918c66f2013-06-27 06:59:01 -03003188 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3189
Philipp Zabel410e5e42014-07-11 06:36:32 -03003190 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3191 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003192
3193 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3194 "job finished: decoding frame (%d) (%s)\n",
3195 dst_buf->v4l2_buf.sequence,
3196 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3197 "KEYFRAME" : "PFRAME");
3198 } else {
3199 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3200 "job finished: no frame decoded\n");
3201 }
3202
3203 /* The rotator will copy the current display frame next time */
3204 ctx->display_idx = display_idx;
3205}
3206
3207static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003208{
Philipp Zabelec25f682012-07-20 08:54:29 -03003209 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003210 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003211 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003212
Philipp Zabel14604e32014-07-11 06:36:22 -03003213 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3214 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003215
3216 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003217 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003218 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3219
Javier Martin186b2502012-07-26 05:53:35 -03003220 /* Calculate bytesused field */
3221 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003222 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3223 ctx->vpu_header_size[0] +
3224 ctx->vpu_header_size[1] +
3225 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003226 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003227 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003228 }
3229
3230 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3231 wr_ptr - start_ptr);
3232
3233 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3234 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3235
Philipp Zabel51660282013-09-30 10:34:49 -03003236 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003237 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3238 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3239 } else {
3240 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3241 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3242 }
3243
Kamil Debskiccd571c2013-04-24 10:38:24 -03003244 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003245 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3246 dst_buf->v4l2_buf.flags |=
3247 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003248 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3249
Philipp Zabelec25f682012-07-20 08:54:29 -03003250 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003251
Philipp Zabel14604e32014-07-11 06:36:22 -03003252 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003253 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3254
3255 ctx->gopcounter--;
3256 if (ctx->gopcounter < 0)
3257 ctx->gopcounter = ctx->params.gop_size - 1;
3258
3259 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3260 "job finished: encoding frame (%d) (%s)\n",
3261 dst_buf->v4l2_buf.sequence,
3262 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3263 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003264}
3265
3266static irqreturn_t coda_irq_handler(int irq, void *data)
3267{
3268 struct coda_dev *dev = data;
3269 struct coda_ctx *ctx;
3270
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003271 /* read status register to attend the IRQ */
3272 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3273 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3274 CODA_REG_BIT_INT_CLEAR);
3275
3276 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3277 if (ctx == NULL) {
3278 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3279 mutex_unlock(&dev->coda_mutex);
3280 return IRQ_HANDLED;
3281 }
3282
3283 if (ctx->aborting) {
3284 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3285 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003286 }
3287
3288 if (coda_isbusy(ctx->dev)) {
3289 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3290 "coda is still busy!!!!\n");
3291 return IRQ_NONE;
3292 }
3293
Philipp Zabel32b6f202014-07-11 06:36:20 -03003294 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003295
3296 return IRQ_HANDLED;
3297}
3298
3299static u32 coda_supported_firmwares[] = {
3300 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003301 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003302 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003303};
3304
3305static bool coda_firmware_supported(u32 vernum)
3306{
3307 int i;
3308
3309 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3310 if (vernum == coda_supported_firmwares[i])
3311 return true;
3312 return false;
3313}
3314
Philipp Zabel87048bb2012-07-02 07:03:43 -03003315static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003316{
Javier Martin186b2502012-07-26 05:53:35 -03003317 u32 data;
3318 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003319 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003320
Fabio Estevam79830db2013-08-21 11:14:17 -03003321 ret = clk_prepare_enable(dev->clk_per);
3322 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003323 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003324
3325 ret = clk_prepare_enable(dev->clk_ahb);
3326 if (ret)
3327 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003328
Javier Martin186b2502012-07-26 05:53:35 -03003329 /*
3330 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003331 * The 16-bit chars in the code buffer are in memory access
3332 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003333 * Data in this SRAM survives a reboot.
3334 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003335 p = (u16 *)dev->codebuf.vaddr;
3336 if (dev->devtype->product == CODA_DX6) {
3337 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3338 data = CODA_DOWN_ADDRESS_SET(i) |
3339 CODA_DOWN_DATA_SET(p[i ^ 1]);
3340 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3341 }
3342 } else {
3343 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3344 data = CODA_DOWN_ADDRESS_SET(i) |
3345 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3346 3 - (i % 4)]);
3347 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3348 }
Javier Martin186b2502012-07-26 05:53:35 -03003349 }
Javier Martin186b2502012-07-26 05:53:35 -03003350
Philipp Zabel9acf7692013-05-23 10:42:56 -03003351 /* Clear registers */
3352 for (i = 0; i < 64; i++)
3353 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3354
Javier Martin186b2502012-07-26 05:53:35 -03003355 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003356 if (dev->devtype->product == CODA_960 ||
3357 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003358 coda_write(dev, dev->tempbuf.paddr,
3359 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003360 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003361 } else {
3362 coda_write(dev, dev->workbuf.paddr,
3363 CODA_REG_BIT_WORK_BUF_ADDR);
3364 }
Javier Martin186b2502012-07-26 05:53:35 -03003365 coda_write(dev, dev->codebuf.paddr,
3366 CODA_REG_BIT_CODE_BUF_ADDR);
3367 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3368
3369 /* Set default values */
3370 switch (dev->devtype->product) {
3371 case CODA_DX6:
3372 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3373 break;
3374 default:
3375 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3376 }
Philipp Zabel89548442014-07-11 06:36:17 -03003377 if (dev->devtype->product == CODA_960)
3378 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3379 else
3380 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003381
3382 if (dev->devtype->product != CODA_DX6)
3383 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3384
Javier Martin186b2502012-07-26 05:53:35 -03003385 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3386 CODA_REG_BIT_INT_ENABLE);
3387
3388 /* Reset VPU and start processor */
3389 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3390 data |= CODA_REG_RESET_ENABLE;
3391 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3392 udelay(10);
3393 data &= ~CODA_REG_RESET_ENABLE;
3394 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3395 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3396
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003397 clk_disable_unprepare(dev->clk_ahb);
3398 clk_disable_unprepare(dev->clk_per);
3399
3400 return 0;
3401
3402err_clk_ahb:
3403 clk_disable_unprepare(dev->clk_per);
3404err_clk_per:
3405 return ret;
3406}
3407
3408static int coda_check_firmware(struct coda_dev *dev)
3409{
3410 u16 product, major, minor, release;
3411 u32 data;
3412 int ret;
3413
3414 ret = clk_prepare_enable(dev->clk_per);
3415 if (ret)
3416 goto err_clk_per;
3417
3418 ret = clk_prepare_enable(dev->clk_ahb);
3419 if (ret)
3420 goto err_clk_ahb;
3421
Javier Martin186b2502012-07-26 05:53:35 -03003422 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3423 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3424 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3425 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3426 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3427 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003428 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003429 ret = -EIO;
3430 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003431 }
3432
Philipp Zabel89548442014-07-11 06:36:17 -03003433 if (dev->devtype->product == CODA_960) {
3434 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3435 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3436 data);
3437 }
3438
Javier Martin186b2502012-07-26 05:53:35 -03003439 /* Check we are compatible with the loaded firmware */
3440 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3441 product = CODA_FIRMWARE_PRODUCT(data);
3442 major = CODA_FIRMWARE_MAJOR(data);
3443 minor = CODA_FIRMWARE_MINOR(data);
3444 release = CODA_FIRMWARE_RELEASE(data);
3445
3446 clk_disable_unprepare(dev->clk_per);
3447 clk_disable_unprepare(dev->clk_ahb);
3448
3449 if (product != dev->devtype->product) {
3450 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3451 " Version: %u.%u.%u\n",
3452 coda_product_name(dev->devtype->product),
3453 coda_product_name(product), major, minor, release);
3454 return -EINVAL;
3455 }
3456
3457 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3458 coda_product_name(product));
3459
3460 if (coda_firmware_supported(data)) {
3461 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3462 major, minor, release);
3463 } else {
3464 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3465 "%u.%u.%u\n", major, minor, release);
3466 }
3467
3468 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003469
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003470err_run_cmd:
3471 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003472err_clk_ahb:
3473 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003474err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003475 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003476}
3477
3478static void coda_fw_callback(const struct firmware *fw, void *context)
3479{
3480 struct coda_dev *dev = context;
3481 struct platform_device *pdev = dev->plat_dev;
3482 int ret;
3483
3484 if (!fw) {
3485 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3486 return;
3487 }
3488
3489 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003490 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3491 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003492 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3493 return;
3494 }
3495
Philipp Zabel87048bb2012-07-02 07:03:43 -03003496 /* Copy the whole firmware image to the code buffer */
3497 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3498 release_firmware(fw);
3499
Philipp Zabel1e172732014-07-11 06:36:23 -03003500 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3501 /*
3502 * Enabling power temporarily will cause coda_hw_init to be
3503 * called via coda_runtime_resume by the pm domain.
3504 */
3505 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3506 if (ret < 0) {
3507 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3508 ret);
3509 return;
3510 }
3511
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003512 ret = coda_check_firmware(dev);
3513 if (ret < 0)
3514 return;
3515
Philipp Zabel1e172732014-07-11 06:36:23 -03003516 pm_runtime_put_sync(&dev->plat_dev->dev);
3517 } else {
3518 /*
3519 * If runtime pm is disabled or pm_domain is not set,
3520 * initialize once manually.
3521 */
3522 ret = coda_hw_init(dev);
3523 if (ret < 0) {
3524 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3525 return;
3526 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003527
3528 ret = coda_check_firmware(dev);
3529 if (ret < 0)
3530 return;
Javier Martin186b2502012-07-26 05:53:35 -03003531 }
3532
3533 dev->vfd.fops = &coda_fops,
3534 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3535 dev->vfd.release = video_device_release_empty,
3536 dev->vfd.lock = &dev->dev_mutex;
3537 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003538 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003539 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3540 video_set_drvdata(&dev->vfd, dev);
3541
3542 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3543 if (IS_ERR(dev->alloc_ctx)) {
3544 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3545 return;
3546 }
3547
3548 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3549 if (IS_ERR(dev->m2m_dev)) {
3550 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3551 goto rel_ctx;
3552 }
3553
3554 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3555 if (ret) {
3556 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3557 goto rel_m2m;
3558 }
3559 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3560 dev->vfd.num);
3561
3562 return;
3563
3564rel_m2m:
3565 v4l2_m2m_release(dev->m2m_dev);
3566rel_ctx:
3567 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3568}
3569
3570static int coda_firmware_request(struct coda_dev *dev)
3571{
3572 char *fw = dev->devtype->firmware;
3573
3574 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3575 coda_product_name(dev->devtype->product));
3576
3577 return request_firmware_nowait(THIS_MODULE, true,
3578 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3579}
3580
3581enum coda_platform {
3582 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003583 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003584 CODA_IMX6Q,
3585 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003586};
3587
Emil Goodec06d8752012-08-14 17:44:42 -03003588static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003589 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003590 .firmware = "v4l-codadx6-imx27.bin",
3591 .product = CODA_DX6,
3592 .codecs = codadx6_codecs,
3593 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003594 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003595 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003596 .firmware = "v4l-coda7541-imx53.bin",
3597 .product = CODA_7541,
3598 .codecs = coda7_codecs,
3599 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003600 },
Philipp Zabel89548442014-07-11 06:36:17 -03003601 [CODA_IMX6Q] = {
3602 .firmware = "v4l-coda960-imx6q.bin",
3603 .product = CODA_960,
3604 .codecs = coda9_codecs,
3605 .num_codecs = ARRAY_SIZE(coda9_codecs),
3606 },
3607 [CODA_IMX6DL] = {
3608 .firmware = "v4l-coda960-imx6dl.bin",
3609 .product = CODA_960,
3610 .codecs = coda9_codecs,
3611 .num_codecs = ARRAY_SIZE(coda9_codecs),
3612 },
Javier Martin186b2502012-07-26 05:53:35 -03003613};
3614
3615static struct platform_device_id coda_platform_ids[] = {
3616 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003617 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003618 { /* sentinel */ }
3619};
3620MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3621
3622#ifdef CONFIG_OF
3623static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003624 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003625 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003626 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3627 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003628 { /* sentinel */ }
3629};
3630MODULE_DEVICE_TABLE(of, coda_dt_ids);
3631#endif
3632
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003633static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003634{
3635 const struct of_device_id *of_id =
3636 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3637 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003638 struct coda_platform_data *pdata = pdev->dev.platform_data;
3639 struct device_node *np = pdev->dev.of_node;
3640 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003641 struct coda_dev *dev;
3642 struct resource *res;
3643 int ret, irq;
3644
3645 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3646 if (!dev) {
3647 dev_err(&pdev->dev, "Not enough memory for %s\n",
3648 CODA_NAME);
3649 return -ENOMEM;
3650 }
3651
3652 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003653 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003654
3655 dev->plat_dev = pdev;
3656 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3657 if (IS_ERR(dev->clk_per)) {
3658 dev_err(&pdev->dev, "Could not get per clock\n");
3659 return PTR_ERR(dev->clk_per);
3660 }
3661
3662 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3663 if (IS_ERR(dev->clk_ahb)) {
3664 dev_err(&pdev->dev, "Could not get ahb clock\n");
3665 return PTR_ERR(dev->clk_ahb);
3666 }
3667
3668 /* Get memory for physical registers */
3669 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003670 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3671 if (IS_ERR(dev->regs_base))
3672 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003673
3674 /* IRQ */
3675 irq = platform_get_irq(pdev, 0);
3676 if (irq < 0) {
3677 dev_err(&pdev->dev, "failed to get irq resource\n");
3678 return -ENOENT;
3679 }
3680
Philipp Zabel918c66f2013-06-27 06:59:01 -03003681 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003682 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003683 dev_err(&pdev->dev, "failed to request irq\n");
3684 return -ENOENT;
3685 }
3686
Philipp Zabel657eee72013-04-29 16:17:14 -07003687 /* Get IRAM pool from device tree or platform data */
3688 pool = of_get_named_gen_pool(np, "iram", 0);
3689 if (!pool && pdata)
3690 pool = dev_get_gen_pool(pdata->iram_dev);
3691 if (!pool) {
3692 dev_err(&pdev->dev, "iram pool not available\n");
3693 return -ENOMEM;
3694 }
3695 dev->iram_pool = pool;
3696
Javier Martin186b2502012-07-26 05:53:35 -03003697 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3698 if (ret)
3699 return ret;
3700
3701 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003702 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003703
3704 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3705
3706 if (of_id) {
3707 dev->devtype = of_id->data;
3708 } else if (pdev_id) {
3709 dev->devtype = &coda_devdata[pdev_id->driver_data];
3710 } else {
3711 v4l2_device_unregister(&dev->v4l2_dev);
3712 return -EINVAL;
3713 }
3714
3715 /* allocate auxiliary per-device buffers for the BIT processor */
3716 switch (dev->devtype->product) {
3717 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003718 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3719 CODADX6_WORK_BUF_SIZE);
3720 if (ret < 0) {
3721 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3722 v4l2_device_unregister(&dev->v4l2_dev);
3723 return ret;
3724 }
Javier Martin186b2502012-07-26 05:53:35 -03003725 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003726 case CODA_7541:
3727 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3728 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003729 case CODA_960:
3730 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3731 break;
Javier Martin186b2502012-07-26 05:53:35 -03003732 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003733 if (dev->tempbuf.size) {
3734 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3735 dev->tempbuf.size);
3736 if (ret < 0) {
3737 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3738 v4l2_device_unregister(&dev->v4l2_dev);
3739 return ret;
3740 }
Javier Martin186b2502012-07-26 05:53:35 -03003741 }
3742
Philipp Zabel918c66f2013-06-27 06:59:01 -03003743 switch (dev->devtype->product) {
3744 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003745 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003746 break;
3747 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003748 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003749 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003750 case CODA_960:
3751 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003752 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003753 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3754 &dev->iram.paddr);
3755 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003756 dev_err(&pdev->dev, "unable to alloc iram\n");
3757 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003758 }
3759
Philipp Zabel32b6f202014-07-11 06:36:20 -03003760 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3761 if (!dev->workqueue) {
3762 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3763 return -ENOMEM;
3764 }
3765
Javier Martin186b2502012-07-26 05:53:35 -03003766 platform_set_drvdata(pdev, dev);
3767
Philipp Zabel1e172732014-07-11 06:36:23 -03003768 pm_runtime_enable(&pdev->dev);
3769
Javier Martin186b2502012-07-26 05:53:35 -03003770 return coda_firmware_request(dev);
3771}
3772
3773static int coda_remove(struct platform_device *pdev)
3774{
3775 struct coda_dev *dev = platform_get_drvdata(pdev);
3776
3777 video_unregister_device(&dev->vfd);
3778 if (dev->m2m_dev)
3779 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003780 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003781 if (dev->alloc_ctx)
3782 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3783 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003784 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003785 if (dev->iram.vaddr)
3786 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3787 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003788 coda_free_aux_buf(dev, &dev->codebuf);
3789 coda_free_aux_buf(dev, &dev->tempbuf);
3790 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003791 return 0;
3792}
3793
Philipp Zabel1e172732014-07-11 06:36:23 -03003794#ifdef CONFIG_PM_RUNTIME
3795static int coda_runtime_resume(struct device *dev)
3796{
3797 struct coda_dev *cdev = dev_get_drvdata(dev);
3798 int ret = 0;
3799
3800 if (dev->pm_domain) {
3801 ret = coda_hw_init(cdev);
3802 if (ret)
3803 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3804 }
3805
3806 return ret;
3807}
3808#endif
3809
3810static const struct dev_pm_ops coda_pm_ops = {
3811 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3812};
3813
Javier Martin186b2502012-07-26 05:53:35 -03003814static struct platform_driver coda_driver = {
3815 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003816 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003817 .driver = {
3818 .name = CODA_NAME,
3819 .owner = THIS_MODULE,
3820 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003821 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003822 },
3823 .id_table = coda_platform_ids,
3824};
3825
3826module_platform_driver(coda_driver);
3827
3828MODULE_LICENSE("GPL");
3829MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3830MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");