blob: c7e557a5788fc70088e6c46fce270152bc2501fa [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];
246 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300247 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300248 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300249 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300250 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300251 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300252 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300253 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300254 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300255 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300256 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300257};
258
Javier Martin832fbb52012-10-29 08:34:59 -0300259static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
260 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
261static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300262
Javier Martin186b2502012-07-26 05:53:35 -0300263static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
264{
265 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
266 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
267 writel(data, dev->regs_base + reg);
268}
269
270static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
271{
272 u32 data;
273 data = readl(dev->regs_base + reg);
274 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
275 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
276 return data;
277}
278
279static inline unsigned long coda_isbusy(struct coda_dev *dev)
280{
281 return coda_read(dev, CODA_REG_BIT_BUSY);
282}
283
284static inline int coda_is_initialized(struct coda_dev *dev)
285{
286 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
287}
288
289static int coda_wait_timeout(struct coda_dev *dev)
290{
291 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
292
293 while (coda_isbusy(dev)) {
294 if (time_after(jiffies, timeout))
295 return -ETIMEDOUT;
296 }
297 return 0;
298}
299
300static void coda_command_async(struct coda_ctx *ctx, int cmd)
301{
302 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300303
Philipp Zabel89548442014-07-11 06:36:17 -0300304 if (dev->devtype->product == CODA_960 ||
305 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300306 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300307 coda_write(dev, ctx->bit_stream_param,
308 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300309 coda_write(dev, ctx->frm_dis_flg,
310 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300311 coda_write(dev, ctx->frame_mem_ctrl,
312 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300313 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
314 }
315
Philipp Zabel89548442014-07-11 06:36:17 -0300316 if (dev->devtype->product == CODA_960) {
317 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
318 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
319 }
320
Javier Martin186b2502012-07-26 05:53:35 -0300321 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
322
323 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
324 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300325 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
326
Javier Martin186b2502012-07-26 05:53:35 -0300327 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
328}
329
330static int coda_command_sync(struct coda_ctx *ctx, int cmd)
331{
332 struct coda_dev *dev = ctx->dev;
333
334 coda_command_async(ctx, cmd);
335 return coda_wait_timeout(dev);
336}
337
338static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
339 enum v4l2_buf_type type)
340{
341 switch (type) {
342 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
343 return &(ctx->q_data[V4L2_M2M_SRC]);
344 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
345 return &(ctx->q_data[V4L2_M2M_DST]);
346 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300347 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300348 }
Javier Martin186b2502012-07-26 05:53:35 -0300349}
350
351/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300352 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300353 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300354static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300355 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300356 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300357 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300358 },
359 {
360 .name = "YUV 4:2:0 Planar, YCrCb",
361 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300362 },
363 {
364 .name = "H264 Encoded Stream",
365 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300366 },
367 {
368 .name = "MPEG4 Encoded Stream",
369 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300370 },
371};
372
Philipp Zabelb96904e2013-05-23 10:42:58 -0300373#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
374 { mode, src_fourcc, dst_fourcc, max_w, max_h }
375
376/*
377 * Arrays of codecs supported by each given version of Coda:
378 * i.MX27 -> codadx6
379 * i.MX5x -> coda7
380 * i.MX6 -> coda960
381 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
382 */
383static struct coda_codec codadx6_codecs[] = {
384 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
385 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300386};
387
Philipp Zabelb96904e2013-05-23 10:42:58 -0300388static struct coda_codec coda7_codecs[] = {
389 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
390 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300391 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
392 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300393};
394
Philipp Zabel89548442014-07-11 06:36:17 -0300395static struct coda_codec coda9_codecs[] = {
396 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
397 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
398 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
399 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
400};
401
Philipp Zabelb96904e2013-05-23 10:42:58 -0300402static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300403{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300404 switch (fourcc) {
405 case V4L2_PIX_FMT_YUV420:
406 case V4L2_PIX_FMT_YVU420:
407 return true;
408 default:
409 return false;
410 }
411}
Javier Martin186b2502012-07-26 05:53:35 -0300412
Philipp Zabelb96904e2013-05-23 10:42:58 -0300413/*
414 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
415 * tables.
416 */
417static u32 coda_format_normalize_yuv(u32 fourcc)
418{
419 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
420}
421
422static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
423 int dst_fourcc)
424{
425 struct coda_codec *codecs = dev->devtype->codecs;
426 int num_codecs = dev->devtype->num_codecs;
427 int k;
428
429 src_fourcc = coda_format_normalize_yuv(src_fourcc);
430 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
431 if (src_fourcc == dst_fourcc)
432 return NULL;
433
434 for (k = 0; k < num_codecs; k++) {
435 if (codecs[k].src_fourcc == src_fourcc &&
436 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300437 break;
438 }
439
Philipp Zabelb96904e2013-05-23 10:42:58 -0300440 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300441 return NULL;
442
Philipp Zabelb96904e2013-05-23 10:42:58 -0300443 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300444}
445
Philipp Zabel57625592013-09-30 10:34:51 -0300446static void coda_get_max_dimensions(struct coda_dev *dev,
447 struct coda_codec *codec,
448 int *max_w, int *max_h)
449{
450 struct coda_codec *codecs = dev->devtype->codecs;
451 int num_codecs = dev->devtype->num_codecs;
452 unsigned int w, h;
453 int k;
454
455 if (codec) {
456 w = codec->max_w;
457 h = codec->max_h;
458 } else {
459 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
460 w = max(w, codecs[k].max_w);
461 h = max(h, codecs[k].max_h);
462 }
463 }
464
465 if (max_w)
466 *max_w = w;
467 if (max_h)
468 *max_h = h;
469}
470
Philipp Zabel927933f2013-09-30 10:34:48 -0300471static char *coda_product_name(int product)
472{
473 static char buf[9];
474
475 switch (product) {
476 case CODA_DX6:
477 return "CodaDx6";
478 case CODA_7541:
479 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300480 case CODA_960:
481 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300482 default:
483 snprintf(buf, sizeof(buf), "(0x%04x)", product);
484 return buf;
485 }
486}
487
Javier Martin186b2502012-07-26 05:53:35 -0300488/*
489 * V4L2 ioctl() operations.
490 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300491static int coda_querycap(struct file *file, void *priv,
492 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300493{
Philipp Zabel927933f2013-09-30 10:34:48 -0300494 struct coda_ctx *ctx = fh_to_ctx(priv);
495
Javier Martin186b2502012-07-26 05:53:35 -0300496 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300497 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
498 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300499 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300500 /*
501 * This is only a mem-to-mem video device. The capture and output
502 * device capability flags are left only for backward compatibility
503 * and are scheduled for removal.
504 */
505 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
506 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300507 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
508
509 return 0;
510}
511
512static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300513 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300514{
515 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300516 struct coda_codec *codecs = ctx->dev->devtype->codecs;
517 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300518 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300519 int num_codecs = ctx->dev->devtype->num_codecs;
520 int num_formats = ARRAY_SIZE(coda_formats);
521 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300522
523 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300524 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300525 if (coda_format_is_yuv(formats[i].fourcc) &&
526 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300527 if (num == f->index)
528 break;
529 ++num;
530 continue;
531 }
532 /* Compressed formats may be supported, check the codec list */
533 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300534 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300535 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300536 formats[i].fourcc == codecs[k].dst_fourcc &&
537 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300538 break;
539 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
540 formats[i].fourcc == codecs[k].src_fourcc)
541 break;
542 }
543 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300544 if (num == f->index)
545 break;
546 ++num;
547 }
548 }
549
550 if (i < num_formats) {
551 fmt = &formats[i];
552 strlcpy(f->description, fmt->name, sizeof(f->description));
553 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300554 if (!coda_format_is_yuv(fmt->fourcc))
555 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300556 return 0;
557 }
558
559 /* Format not found */
560 return -EINVAL;
561}
562
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300563static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
564 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300565{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300566 struct coda_ctx *ctx = fh_to_ctx(priv);
567 struct vb2_queue *src_vq;
568 struct coda_q_data *q_data_src;
569
570 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300571 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300572 if (vb2_is_streaming(src_vq)) {
573 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
574
575 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
576 q_data_src->fourcc);
577 }
578
579 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300580}
581
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300582static int coda_enum_fmt_vid_out(struct file *file, void *priv,
583 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300584{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300585 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300586}
587
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300588static int coda_g_fmt(struct file *file, void *priv,
589 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300590{
Javier Martin186b2502012-07-26 05:53:35 -0300591 struct coda_q_data *q_data;
592 struct coda_ctx *ctx = fh_to_ctx(priv);
593
Javier Martin186b2502012-07-26 05:53:35 -0300594 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300595 if (!q_data)
596 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300597
598 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300599 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300600 f->fmt.pix.width = q_data->width;
601 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300602 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300603 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
604 else /* encoded formats h.264/mpeg4 */
605 f->fmt.pix.bytesperline = 0;
606
607 f->fmt.pix.sizeimage = q_data->sizeimage;
608 f->fmt.pix.colorspace = ctx->colorspace;
609
610 return 0;
611}
612
Philipp Zabel57625592013-09-30 10:34:51 -0300613static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
614 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300615{
Philipp Zabel57625592013-09-30 10:34:51 -0300616 struct coda_dev *dev = ctx->dev;
617 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300618 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300619 enum v4l2_field field;
620
621 field = f->fmt.pix.field;
622 if (field == V4L2_FIELD_ANY)
623 field = V4L2_FIELD_NONE;
624 else if (V4L2_FIELD_NONE != field)
625 return -EINVAL;
626
627 /* V4L2 specification suggests the driver corrects the format struct
628 * if any of the dimensions is unsupported */
629 f->fmt.pix.field = field;
630
Philipp Zabel57625592013-09-30 10:34:51 -0300631 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
632 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
633 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
634 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300635
Philipp Zabel57625592013-09-30 10:34:51 -0300636 switch (f->fmt.pix.pixelformat) {
637 case V4L2_PIX_FMT_YUV420:
638 case V4L2_PIX_FMT_YVU420:
639 case V4L2_PIX_FMT_H264:
640 case V4L2_PIX_FMT_MPEG4:
641 case V4L2_PIX_FMT_JPEG:
642 break;
643 default:
644 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300645 if (!q_data)
646 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300647 f->fmt.pix.pixelformat = q_data->fourcc;
648 }
649
650 switch (f->fmt.pix.pixelformat) {
651 case V4L2_PIX_FMT_YUV420:
652 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300653 /* Frame stride must be multiple of 8 */
654 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
655 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300656 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300657 break;
658 case V4L2_PIX_FMT_H264:
659 case V4L2_PIX_FMT_MPEG4:
660 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300661 f->fmt.pix.bytesperline = 0;
662 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300663 break;
664 default:
665 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300666 }
667
668 return 0;
669}
670
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300671static int coda_try_fmt_vid_cap(struct file *file, void *priv,
672 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300673{
Javier Martin186b2502012-07-26 05:53:35 -0300674 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300675 struct coda_codec *codec;
676 struct vb2_queue *src_vq;
677 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300678
Philipp Zabel918c66f2013-06-27 06:59:01 -0300679 /*
680 * If the source format is already fixed, try to find a codec that
681 * converts to the given destination format
682 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300683 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300684 if (vb2_is_streaming(src_vq)) {
685 struct coda_q_data *q_data_src;
686
687 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
688 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
689 f->fmt.pix.pixelformat);
690 if (!codec)
691 return -EINVAL;
692 } else {
693 /* Otherwise determine codec by encoded format, if possible */
694 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
695 f->fmt.pix.pixelformat);
696 }
Javier Martin186b2502012-07-26 05:53:35 -0300697
698 f->fmt.pix.colorspace = ctx->colorspace;
699
Philipp Zabel57625592013-09-30 10:34:51 -0300700 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300701 if (ret < 0)
702 return ret;
703
704 /* The h.264 decoder only returns complete 16x16 macroblocks */
705 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
706 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
707 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
708 f->fmt.pix.bytesperline = f->fmt.pix.width;
709 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
710 f->fmt.pix.height * 3 / 2;
711 }
712
713 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300714}
715
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300716static int coda_try_fmt_vid_out(struct file *file, void *priv,
717 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300718{
719 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300720 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300721
Philipp Zabelb96904e2013-05-23 10:42:58 -0300722 /* Determine codec by encoded format, returns NULL if raw or invalid */
723 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
724 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300725
726 if (!f->fmt.pix.colorspace)
727 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
728
Philipp Zabel57625592013-09-30 10:34:51 -0300729 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300730}
731
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300732static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300733{
734 struct coda_q_data *q_data;
735 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300736
Philipp Zabel14604e32014-07-11 06:36:22 -0300737 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300738 if (!vq)
739 return -EINVAL;
740
741 q_data = get_q_data(ctx, f->type);
742 if (!q_data)
743 return -EINVAL;
744
745 if (vb2_is_busy(vq)) {
746 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
747 return -EBUSY;
748 }
749
Philipp Zabelb96904e2013-05-23 10:42:58 -0300750 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300751 q_data->width = f->fmt.pix.width;
752 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300753 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300754 q_data->rect.left = 0;
755 q_data->rect.top = 0;
756 q_data->rect.width = f->fmt.pix.width;
757 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300758
759 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
760 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300761 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300762
763 return 0;
764}
765
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300766static int coda_s_fmt_vid_cap(struct file *file, void *priv,
767 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300768{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300769 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300770 int ret;
771
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300772 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300773 if (ret)
774 return ret;
775
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300776 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300777}
778
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300779static int coda_s_fmt_vid_out(struct file *file, void *priv,
780 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300781{
782 struct coda_ctx *ctx = fh_to_ctx(priv);
783 int ret;
784
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300785 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300786 if (ret)
787 return ret;
788
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300789 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300790 if (ret)
791 ctx->colorspace = f->fmt.pix.colorspace;
792
793 return ret;
794}
795
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300796static int coda_qbuf(struct file *file, void *priv,
797 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300798{
799 struct coda_ctx *ctx = fh_to_ctx(priv);
800
Philipp Zabel14604e32014-07-11 06:36:22 -0300801 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300802}
803
Philipp Zabel918c66f2013-06-27 06:59:01 -0300804static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
805 struct v4l2_buffer *buf)
806{
807 struct vb2_queue *src_vq;
808
Philipp Zabel14604e32014-07-11 06:36:22 -0300809 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300810
811 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
812 (buf->sequence == (ctx->qsequence - 1)));
813}
814
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300815static int coda_dqbuf(struct file *file, void *priv,
816 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300817{
818 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300819 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300820
Philipp Zabel14604e32014-07-11 06:36:22 -0300821 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300822
823 /* If this is the last capture buffer, emit an end-of-stream event */
824 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
825 coda_buf_is_end_of_stream(ctx, buf)) {
826 const struct v4l2_event eos_event = {
827 .type = V4L2_EVENT_EOS
828 };
829
830 v4l2_event_queue_fh(&ctx->fh, &eos_event);
831 }
832
833 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300834}
835
Philipp Zabel52c41672014-07-11 06:36:19 -0300836static int coda_g_selection(struct file *file, void *fh,
837 struct v4l2_selection *s)
838{
839 struct coda_ctx *ctx = fh_to_ctx(fh);
840 struct coda_q_data *q_data;
841 struct v4l2_rect r, *rsel;
842
843 q_data = get_q_data(ctx, s->type);
844 if (!q_data)
845 return -EINVAL;
846
847 r.left = 0;
848 r.top = 0;
849 r.width = q_data->width;
850 r.height = q_data->height;
851 rsel = &q_data->rect;
852
853 switch (s->target) {
854 case V4L2_SEL_TGT_CROP_DEFAULT:
855 case V4L2_SEL_TGT_CROP_BOUNDS:
856 rsel = &r;
857 /* fallthrough */
858 case V4L2_SEL_TGT_CROP:
859 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
860 return -EINVAL;
861 break;
862 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
863 case V4L2_SEL_TGT_COMPOSE_PADDED:
864 rsel = &r;
865 /* fallthrough */
866 case V4L2_SEL_TGT_COMPOSE:
867 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
868 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
869 return -EINVAL;
870 break;
871 default:
872 return -EINVAL;
873 }
874
875 s->r = *rsel;
876
877 return 0;
878}
879
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300880static int coda_try_decoder_cmd(struct file *file, void *fh,
881 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300882{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300883 if (dc->cmd != V4L2_DEC_CMD_STOP)
884 return -EINVAL;
885
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300886 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300887 return -EINVAL;
888
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300889 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300890 return -EINVAL;
891
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300892 return 0;
893}
894
895static int coda_decoder_cmd(struct file *file, void *fh,
896 struct v4l2_decoder_cmd *dc)
897{
898 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300899 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300900 int ret;
901
902 ret = coda_try_decoder_cmd(file, fh, dc);
903 if (ret < 0)
904 return ret;
905
906 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300907 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300908 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300909
910 /* Set the strem-end flag on this context */
911 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
912
Philipp Zabel89548442014-07-11 06:36:17 -0300913 if ((dev->devtype->product == CODA_960) &&
914 coda_isbusy(dev) &&
915 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
916 /* If this context is currently running, update the hardware flag */
917 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
918 }
Michael Olbrichf3497da2014-07-11 06:36:30 -0300919 ctx->prescan_failed = false;
920 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300921
Philipp Zabel918c66f2013-06-27 06:59:01 -0300922 return 0;
923}
924
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300925static int coda_subscribe_event(struct v4l2_fh *fh,
926 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300927{
928 switch (sub->type) {
929 case V4L2_EVENT_EOS:
930 return v4l2_event_subscribe(fh, sub, 0, NULL);
931 default:
932 return v4l2_ctrl_subscribe_event(fh, sub);
933 }
Javier Martin186b2502012-07-26 05:53:35 -0300934}
935
936static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300937 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300938
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300939 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
940 .vidioc_g_fmt_vid_cap = coda_g_fmt,
941 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
942 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300943
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300944 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
945 .vidioc_g_fmt_vid_out = coda_g_fmt,
946 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
947 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300948
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300949 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
950 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300951
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300952 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300953 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300954 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300955 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300956
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300957 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
958 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300959
Philipp Zabel52c41672014-07-11 06:36:19 -0300960 .vidioc_g_selection = coda_g_selection,
961
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300962 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300963 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300964
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300965 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300966 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300967};
968
Philipp Zabel918c66f2013-06-27 06:59:01 -0300969static int coda_start_decoding(struct coda_ctx *ctx);
970
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300971static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
972{
973 return kfifo_len(&ctx->bitstream_fifo);
974}
975
976static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
977{
978 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
979 struct coda_dev *dev = ctx->dev;
980 u32 rd_ptr;
981
982 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
983 kfifo->out = (kfifo->in & ~kfifo->mask) |
984 (rd_ptr - ctx->bitstream.paddr);
985 if (kfifo->out > kfifo->in)
986 kfifo->out -= kfifo->mask + 1;
987}
988
989static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
990{
991 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
992 struct coda_dev *dev = ctx->dev;
993 u32 rd_ptr, wr_ptr;
994
995 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
996 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
997 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
998 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
999}
1000
1001static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1002{
1003 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1004 struct coda_dev *dev = ctx->dev;
1005 u32 wr_ptr;
1006
1007 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1008 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1009}
1010
1011static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1012{
1013 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1014 u32 n;
1015
1016 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1017 if (n < src_size)
1018 return -ENOSPC;
1019
1020 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1021 ctx->bitstream.size, DMA_TO_DEVICE);
1022
Philipp Zabel846ced92014-07-11 06:36:31 -03001023 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001024
1025 return 0;
1026}
1027
1028static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1029 struct vb2_buffer *src_buf)
1030{
1031 int ret;
1032
1033 if (coda_get_bitstream_payload(ctx) +
1034 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1035 return false;
1036
1037 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1038 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1039 return true;
1040 }
1041
1042 ret = coda_bitstream_queue(ctx, src_buf);
1043 if (ret < 0) {
1044 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1045 return false;
1046 }
1047 /* Sync read pointer to device */
1048 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1049 coda_kfifo_sync_to_device_write(ctx);
1050
Philipp Zabel918c66f2013-06-27 06:59:01 -03001051 ctx->prescan_failed = false;
1052
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001053 return true;
1054}
1055
1056static void coda_fill_bitstream(struct coda_ctx *ctx)
1057{
1058 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001059 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001060
Philipp Zabel14604e32014-07-11 06:36:22 -03001061 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1062 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001063
1064 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001065 /*
1066 * Source buffer is queued in the bitstream ringbuffer;
1067 * queue the timestamp and mark source buffer as done
1068 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001069 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001070
1071 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1072 if (ts) {
1073 ts->sequence = src_buf->v4l2_buf.sequence;
1074 ts->timecode = src_buf->v4l2_buf.timecode;
1075 ts->timestamp = src_buf->v4l2_buf.timestamp;
1076 list_add_tail(&ts->list, &ctx->timestamp_list);
1077 }
1078
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001079 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1080 } else {
1081 break;
1082 }
1083 }
1084}
1085
Philipp Zabel89548442014-07-11 06:36:17 -03001086static void coda_set_gdi_regs(struct coda_ctx *ctx)
1087{
1088 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1089 struct coda_dev *dev = ctx->dev;
1090 int i;
1091
1092 for (i = 0; i < 16; i++)
1093 coda_write(dev, tiled_map->xy2ca_map[i],
1094 CODA9_GDI_XY2_CAS_0 + 4 * i);
1095 for (i = 0; i < 4; i++)
1096 coda_write(dev, tiled_map->xy2ba_map[i],
1097 CODA9_GDI_XY2_BA_0 + 4 * i);
1098 for (i = 0; i < 16; i++)
1099 coda_write(dev, tiled_map->xy2ra_map[i],
1100 CODA9_GDI_XY2_RAS_0 + 4 * i);
1101 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1102 for (i = 0; i < 32; i++)
1103 coda_write(dev, tiled_map->rbc2axi_map[i],
1104 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1105}
1106
Javier Martin186b2502012-07-26 05:53:35 -03001107/*
1108 * Mem-to-mem operations.
1109 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001110static int coda_prepare_decode(struct coda_ctx *ctx)
1111{
1112 struct vb2_buffer *dst_buf;
1113 struct coda_dev *dev = ctx->dev;
1114 struct coda_q_data *q_data_dst;
1115 u32 stridey, height;
1116 u32 picture_y, picture_cb, picture_cr;
1117
Philipp Zabel14604e32014-07-11 06:36:22 -03001118 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001119 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1120
1121 if (ctx->params.rot_mode & CODA_ROT_90) {
1122 stridey = q_data_dst->height;
1123 height = q_data_dst->width;
1124 } else {
1125 stridey = q_data_dst->width;
1126 height = q_data_dst->height;
1127 }
1128
1129 /* Try to copy source buffer contents into the bitstream ringbuffer */
1130 mutex_lock(&ctx->bitstream_mutex);
1131 coda_fill_bitstream(ctx);
1132 mutex_unlock(&ctx->bitstream_mutex);
1133
1134 if (coda_get_bitstream_payload(ctx) < 512 &&
1135 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1136 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1137 "bitstream payload: %d, skipping\n",
1138 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001139 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001140 return -EAGAIN;
1141 }
1142
1143 /* Run coda_start_decoding (again) if not yet initialized */
1144 if (!ctx->initialized) {
1145 int ret = coda_start_decoding(ctx);
1146 if (ret < 0) {
1147 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001148 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001149 return -EAGAIN;
1150 } else {
1151 ctx->initialized = 1;
1152 }
1153 }
1154
Philipp Zabel89548442014-07-11 06:36:17 -03001155 if (dev->devtype->product == CODA_960)
1156 coda_set_gdi_regs(ctx);
1157
Philipp Zabel918c66f2013-06-27 06:59:01 -03001158 /* Set rotator output */
1159 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1160 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1161 /* Switch Cr and Cb for YVU420 format */
1162 picture_cr = picture_y + stridey * height;
1163 picture_cb = picture_cr + stridey / 2 * height / 2;
1164 } else {
1165 picture_cb = picture_y + stridey * height;
1166 picture_cr = picture_cb + stridey / 2 * height / 2;
1167 }
Philipp Zabel89548442014-07-11 06:36:17 -03001168
1169 if (dev->devtype->product == CODA_960) {
1170 /*
1171 * The CODA960 seems to have an internal list of buffers with
1172 * 64 entries that includes the registered frame buffers as
1173 * well as the rotator buffer output.
1174 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1175 */
1176 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1177 CODA9_CMD_DEC_PIC_ROT_INDEX);
1178 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1179 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1180 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1181 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1182 } else {
1183 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1184 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1185 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1186 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1187 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001188 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1189 CODA_CMD_DEC_PIC_ROT_MODE);
1190
1191 switch (dev->devtype->product) {
1192 case CODA_DX6:
1193 /* TBD */
1194 case CODA_7541:
1195 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1196 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001197 case CODA_960:
1198 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1199 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001200 }
1201
1202 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1203
1204 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1205 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1206
1207 return 0;
1208}
1209
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001210static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001211{
Javier Martin186b2502012-07-26 05:53:35 -03001212 struct coda_q_data *q_data_src, *q_data_dst;
1213 struct vb2_buffer *src_buf, *dst_buf;
1214 struct coda_dev *dev = ctx->dev;
1215 int force_ipicture;
1216 int quant_param = 0;
1217 u32 picture_y, picture_cb, picture_cr;
1218 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1219 u32 dst_fourcc;
1220
Philipp Zabel14604e32014-07-11 06:36:22 -03001221 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1222 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001223 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1224 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001225 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001226
Philipp Zabel918c66f2013-06-27 06:59:01 -03001227 src_buf->v4l2_buf.sequence = ctx->osequence;
1228 dst_buf->v4l2_buf.sequence = ctx->osequence;
1229 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001230
1231 /*
1232 * Workaround coda firmware BUG that only marks the first
1233 * frame as IDR. This is a problem for some decoders that can't
1234 * recover when a frame is lost.
1235 */
1236 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1237 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1238 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1239 } else {
1240 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1241 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1242 }
1243
Philipp Zabel89548442014-07-11 06:36:17 -03001244 if (dev->devtype->product == CODA_960)
1245 coda_set_gdi_regs(ctx);
1246
Javier Martin186b2502012-07-26 05:53:35 -03001247 /*
1248 * Copy headers at the beginning of the first frame for H.264 only.
1249 * In MPEG4 they are already copied by the coda.
1250 */
1251 if (src_buf->v4l2_buf.sequence == 0) {
1252 pic_stream_buffer_addr =
1253 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1254 ctx->vpu_header_size[0] +
1255 ctx->vpu_header_size[1] +
1256 ctx->vpu_header_size[2];
1257 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1258 ctx->vpu_header_size[0] -
1259 ctx->vpu_header_size[1] -
1260 ctx->vpu_header_size[2];
1261 memcpy(vb2_plane_vaddr(dst_buf, 0),
1262 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1263 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1264 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1265 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1266 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1267 ctx->vpu_header_size[2]);
1268 } else {
1269 pic_stream_buffer_addr =
1270 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1271 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1272 }
1273
1274 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1275 force_ipicture = 1;
1276 switch (dst_fourcc) {
1277 case V4L2_PIX_FMT_H264:
1278 quant_param = ctx->params.h264_intra_qp;
1279 break;
1280 case V4L2_PIX_FMT_MPEG4:
1281 quant_param = ctx->params.mpeg4_intra_qp;
1282 break;
1283 default:
1284 v4l2_warn(&ctx->dev->v4l2_dev,
1285 "cannot set intra qp, fmt not supported\n");
1286 break;
1287 }
1288 } else {
1289 force_ipicture = 0;
1290 switch (dst_fourcc) {
1291 case V4L2_PIX_FMT_H264:
1292 quant_param = ctx->params.h264_inter_qp;
1293 break;
1294 case V4L2_PIX_FMT_MPEG4:
1295 quant_param = ctx->params.mpeg4_inter_qp;
1296 break;
1297 default:
1298 v4l2_warn(&ctx->dev->v4l2_dev,
1299 "cannot set inter qp, fmt not supported\n");
1300 break;
1301 }
1302 }
1303
1304 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001305 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001306 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1307
1308
1309 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001310 switch (q_data_src->fourcc) {
1311 case V4L2_PIX_FMT_YVU420:
1312 /* Switch Cb and Cr for YVU420 format */
1313 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1314 picture_cb = picture_cr + q_data_src->width / 2 *
1315 q_data_src->height / 2;
1316 break;
1317 case V4L2_PIX_FMT_YUV420:
1318 default:
1319 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1320 picture_cr = picture_cb + q_data_src->width / 2 *
1321 q_data_src->height / 2;
1322 break;
1323 }
Javier Martin186b2502012-07-26 05:53:35 -03001324
Philipp Zabel89548442014-07-11 06:36:17 -03001325 if (dev->devtype->product == CODA_960) {
1326 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1327 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1328 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1329
1330 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1331 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1332 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1333 } else {
1334 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1335 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1336 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1337 }
Javier Martin186b2502012-07-26 05:53:35 -03001338 coda_write(dev, force_ipicture << 1 & 0x2,
1339 CODA_CMD_ENC_PIC_OPTION);
1340
1341 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1342 coda_write(dev, pic_stream_buffer_size / 1024,
1343 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001344
1345 if (!ctx->streamon_out) {
1346 /* After streamoff on the output side, set the stream end flag */
1347 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1348 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1349 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001350}
1351
1352static void coda_device_run(void *m2m_priv)
1353{
1354 struct coda_ctx *ctx = m2m_priv;
1355 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001356
1357 queue_work(dev->workqueue, &ctx->pic_run_work);
1358}
1359
1360static void coda_free_framebuffers(struct coda_ctx *ctx);
1361static void coda_free_context_buffers(struct coda_ctx *ctx);
1362
1363static void coda_seq_end_work(struct work_struct *work)
1364{
1365 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1366 struct coda_dev *dev = ctx->dev;
1367
1368 mutex_lock(&ctx->buffer_mutex);
1369 mutex_lock(&dev->coda_mutex);
1370
1371 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1372 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1373 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1374 v4l2_err(&dev->v4l2_dev,
1375 "CODA_COMMAND_SEQ_END failed\n");
1376 }
1377
1378 kfifo_init(&ctx->bitstream_fifo,
1379 ctx->bitstream.vaddr, ctx->bitstream.size);
1380
1381 coda_free_framebuffers(ctx);
1382 coda_free_context_buffers(ctx);
1383
1384 mutex_unlock(&dev->coda_mutex);
1385 mutex_unlock(&ctx->buffer_mutex);
1386}
1387
1388static void coda_finish_decode(struct coda_ctx *ctx);
1389static void coda_finish_encode(struct coda_ctx *ctx);
1390
1391static void coda_pic_run_work(struct work_struct *work)
1392{
1393 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1394 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001395 int ret;
1396
1397 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001398 mutex_lock(&dev->coda_mutex);
1399
Philipp Zabel918c66f2013-06-27 06:59:01 -03001400 if (ctx->inst_type == CODA_INST_DECODER) {
1401 ret = coda_prepare_decode(ctx);
1402 if (ret < 0) {
1403 mutex_unlock(&dev->coda_mutex);
1404 mutex_unlock(&ctx->buffer_mutex);
1405 /* job_finish scheduled by prepare_decode */
1406 return;
1407 }
1408 } else {
1409 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001410 }
1411
Philipp Zabelc2d22512013-06-21 03:55:28 -03001412 if (dev->devtype->product != CODA_DX6)
1413 coda_write(dev, ctx->iram_info.axi_sram_use,
1414 CODA7_REG_BIT_AXI_SRAM_USE);
1415
Philipp Zabel918c66f2013-06-27 06:59:01 -03001416 if (ctx->inst_type == CODA_INST_DECODER)
1417 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001418 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001419
1420 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1421 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1422 } else if (!ctx->aborting) {
1423 if (ctx->inst_type == CODA_INST_DECODER)
1424 coda_finish_decode(ctx);
1425 else
1426 coda_finish_encode(ctx);
1427 }
1428
1429 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1430 queue_work(dev->workqueue, &ctx->seq_end_work);
1431
1432 mutex_unlock(&dev->coda_mutex);
1433 mutex_unlock(&ctx->buffer_mutex);
1434
Philipp Zabel14604e32014-07-11 06:36:22 -03001435 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001436}
1437
1438static int coda_job_ready(void *m2m_priv)
1439{
1440 struct coda_ctx *ctx = m2m_priv;
1441
1442 /*
1443 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001444 * and 1 frame are needed. In the decoder case,
1445 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001446 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001447 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001448 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001449 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1450 "not ready: not enough video buffers.\n");
1451 return 0;
1452 }
1453
Philipp Zabel14604e32014-07-11 06:36:22 -03001454 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001455 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1456 "not ready: not enough video capture buffers.\n");
1457 return 0;
1458 }
1459
Philipp Zabel918c66f2013-06-27 06:59:01 -03001460 if (ctx->prescan_failed ||
1461 ((ctx->inst_type == CODA_INST_DECODER) &&
1462 (coda_get_bitstream_payload(ctx) < 512) &&
1463 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1464 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1465 "%d: not ready: not enough bitstream data.\n",
1466 ctx->idx);
1467 return 0;
1468 }
1469
Philipp Zabel3e748262013-05-23 10:43:01 -03001470 if (ctx->aborting) {
1471 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1472 "not ready: aborting\n");
1473 return 0;
1474 }
1475
Javier Martin186b2502012-07-26 05:53:35 -03001476 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1477 "job ready\n");
1478 return 1;
1479}
1480
1481static void coda_job_abort(void *priv)
1482{
1483 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001484
1485 ctx->aborting = 1;
1486
1487 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1488 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001489}
1490
1491static void coda_lock(void *m2m_priv)
1492{
1493 struct coda_ctx *ctx = m2m_priv;
1494 struct coda_dev *pcdev = ctx->dev;
1495 mutex_lock(&pcdev->dev_mutex);
1496}
1497
1498static void coda_unlock(void *m2m_priv)
1499{
1500 struct coda_ctx *ctx = m2m_priv;
1501 struct coda_dev *pcdev = ctx->dev;
1502 mutex_unlock(&pcdev->dev_mutex);
1503}
1504
1505static struct v4l2_m2m_ops coda_m2m_ops = {
1506 .device_run = coda_device_run,
1507 .job_ready = coda_job_ready,
1508 .job_abort = coda_job_abort,
1509 .lock = coda_lock,
1510 .unlock = coda_unlock,
1511};
1512
Philipp Zabel89548442014-07-11 06:36:17 -03001513static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1514{
1515 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1516 int luma_map, chro_map, i;
1517
1518 memset(tiled_map, 0, sizeof(*tiled_map));
1519
1520 luma_map = 64;
1521 chro_map = 64;
1522 tiled_map->map_type = tiled_map_type;
1523 for (i = 0; i < 16; i++)
1524 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1525 for (i = 0; i < 4; i++)
1526 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1527 for (i = 0; i < 16; i++)
1528 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1529
1530 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1531 tiled_map->xy2rbc_config = 0;
1532 } else {
1533 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1534 tiled_map_type);
1535 return;
1536 }
1537}
1538
Javier Martin186b2502012-07-26 05:53:35 -03001539static void set_default_params(struct coda_ctx *ctx)
1540{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001541 int max_w;
1542 int max_h;
1543
1544 ctx->codec = &ctx->dev->devtype->codecs[0];
1545 max_w = ctx->codec->max_w;
1546 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001547
1548 ctx->params.codec_mode = CODA_MODE_INVALID;
1549 ctx->colorspace = V4L2_COLORSPACE_REC709;
1550 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001551 ctx->aborting = 0;
1552
1553 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001554 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1555 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1556 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1557 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1558 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1559 ctx->q_data[V4L2_M2M_DST].width = max_w;
1560 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001561 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001562 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1563 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1564 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1565 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001566
1567 if (ctx->dev->devtype->product == CODA_960)
1568 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001569}
1570
1571/*
1572 * Queue operations
1573 */
1574static int coda_queue_setup(struct vb2_queue *vq,
1575 const struct v4l2_format *fmt,
1576 unsigned int *nbuffers, unsigned int *nplanes,
1577 unsigned int sizes[], void *alloc_ctxs[])
1578{
1579 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001580 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001581 unsigned int size;
1582
Philipp Zabele34db062012-08-29 08:22:00 -03001583 q_data = get_q_data(ctx, vq->type);
1584 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001585
1586 *nplanes = 1;
1587 sizes[0] = size;
1588
1589 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1590
1591 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1592 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1593
1594 return 0;
1595}
1596
1597static int coda_buf_prepare(struct vb2_buffer *vb)
1598{
1599 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1600 struct coda_q_data *q_data;
1601
1602 q_data = get_q_data(ctx, vb->vb2_queue->type);
1603
1604 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1605 v4l2_warn(&ctx->dev->v4l2_dev,
1606 "%s data will not fit into plane (%lu < %lu)\n",
1607 __func__, vb2_plane_size(vb, 0),
1608 (long)q_data->sizeimage);
1609 return -EINVAL;
1610 }
1611
Javier Martin186b2502012-07-26 05:53:35 -03001612 return 0;
1613}
1614
1615static void coda_buf_queue(struct vb2_buffer *vb)
1616{
1617 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001618 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001619 struct coda_q_data *q_data;
1620
1621 q_data = get_q_data(ctx, vb->vb2_queue->type);
1622
1623 /*
1624 * In the decoder case, immediately try to copy the buffer into the
1625 * bitstream ringbuffer and mark it as ready to be dequeued.
1626 */
1627 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1628 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1629 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001630 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001631 * the stream end
1632 */
Philipp Zabel89548442014-07-11 06:36:17 -03001633 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001634 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001635 if ((dev->devtype->product == CODA_960) &&
1636 coda_isbusy(dev) &&
1637 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1638 /* if this decoder instance is running, set the stream end flag */
1639 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1640 }
1641 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001642 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001643 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001644 coda_fill_bitstream(ctx);
1645 mutex_unlock(&ctx->bitstream_mutex);
1646 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001647 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001648 }
Javier Martin186b2502012-07-26 05:53:35 -03001649}
1650
Philipp Zabel86eda902013-05-23 10:42:57 -03001651static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1652{
1653 struct coda_dev *dev = ctx->dev;
1654 u32 *p = ctx->parabuf.vaddr;
1655
1656 if (dev->devtype->product == CODA_DX6)
1657 p[index] = value;
1658 else
1659 p[index ^ 1] = value;
1660}
1661
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001662static int coda_alloc_aux_buf(struct coda_dev *dev,
1663 struct coda_aux_buf *buf, size_t size)
1664{
1665 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1666 GFP_KERNEL);
1667 if (!buf->vaddr)
1668 return -ENOMEM;
1669
1670 buf->size = size;
1671
1672 return 0;
1673}
1674
1675static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1676 struct coda_aux_buf *buf, size_t size)
1677{
1678 return coda_alloc_aux_buf(ctx->dev, buf, size);
1679}
1680
1681static void coda_free_aux_buf(struct coda_dev *dev,
1682 struct coda_aux_buf *buf)
1683{
1684 if (buf->vaddr) {
1685 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1686 buf->vaddr, buf->paddr);
1687 buf->vaddr = NULL;
1688 buf->size = 0;
1689 }
1690}
1691
1692static void coda_free_framebuffers(struct coda_ctx *ctx)
1693{
1694 int i;
1695
1696 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1697 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1698}
1699
Philipp Zabelec25f682012-07-20 08:54:29 -03001700static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1701{
1702 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001703 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001704 dma_addr_t paddr;
1705 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001706 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001707 int i;
1708
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001709 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1710 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001711 ysize = round_up(q_data->width, 8) * height;
1712
Philipp Zabelec25f682012-07-20 08:54:29 -03001713 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001714 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001715 size_t size;
1716
Philipp Zabelaed14b02014-07-11 06:36:15 -03001717 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001718 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1719 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001720 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001721 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1722 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001723 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001724 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001725 }
1726 }
1727
1728 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001729 for (i = 0; i < ctx->num_internal_frames; i++) {
1730 paddr = ctx->internal_frames[i].paddr;
1731 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1732 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1733 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001734
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001735 /* mvcol buffer for h.264 */
1736 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1737 dev->devtype->product != CODA_DX6)
1738 coda_parabuf_write(ctx, 96 + i,
1739 ctx->internal_frames[i].paddr +
1740 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001741 }
1742
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001743 /* mvcol buffer for mpeg4 */
1744 if ((dev->devtype->product != CODA_DX6) &&
1745 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1746 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1747 ysize + ysize/4 + ysize/4);
1748
Philipp Zabelec25f682012-07-20 08:54:29 -03001749 return 0;
1750}
1751
Javier Martin3f3f5c72012-10-29 05:20:29 -03001752static int coda_h264_padding(int size, char *p)
1753{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001754 int nal_size;
1755 int diff;
1756
Javier Martin832fbb52012-10-29 08:34:59 -03001757 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001758 if (diff == 0)
1759 return 0;
1760
Javier Martin832fbb52012-10-29 08:34:59 -03001761 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001762 memcpy(p, coda_filler_nal, nal_size);
1763
1764 /* Add rbsp stop bit and trailing at the end */
1765 *(p + nal_size - 1) = 0x80;
1766
1767 return nal_size;
1768}
1769
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001770static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1771{
1772 phys_addr_t ret;
1773
1774 size = round_up(size, 1024);
1775 if (size > iram->remaining)
1776 return 0;
1777 iram->remaining -= size;
1778
1779 ret = iram->next_paddr;
1780 iram->next_paddr += size;
1781
1782 return ret;
1783}
1784
Philipp Zabelc2d22512013-06-21 03:55:28 -03001785static void coda_setup_iram(struct coda_ctx *ctx)
1786{
1787 struct coda_iram_info *iram_info = &ctx->iram_info;
1788 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001789 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001790 int dbk_bits;
1791 int bit_bits;
1792 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001793
1794 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001795 iram_info->next_paddr = dev->iram.paddr;
1796 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001797
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001798 switch (dev->devtype->product) {
1799 case CODA_7541:
1800 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1801 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1802 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1803 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001804 case CODA_960:
1805 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1806 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1807 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1808 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001809 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001810 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001811 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001812
1813 if (ctx->inst_type == CODA_INST_ENCODER) {
1814 struct coda_q_data *q_data_src;
1815
1816 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1817 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1818
1819 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001820 if (dev->devtype->product == CODA_7541) {
1821 iram_info->search_ram_size = round_up(mb_width * 16 *
1822 36 + 2048, 1024);
1823 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1824 iram_info->search_ram_size);
1825 if (!iram_info->search_ram_paddr) {
1826 pr_err("IRAM is smaller than the search ram size\n");
1827 goto out;
1828 }
1829 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1830 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001831 }
1832
1833 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001834 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1835 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1836 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001837 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001838 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001839
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001840 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1841 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001842 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001843 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001844
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001845 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1846 if (!iram_info->buf_ip_ac_dc_use)
1847 goto out;
1848 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001849
Philipp Zabel8358e762013-06-21 03:55:32 -03001850 /* OVL and BTP disabled for encoder */
1851 } else if (ctx->inst_type == CODA_INST_DECODER) {
1852 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001853
1854 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1855 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001856
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001857 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1858 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1859 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001860 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001861 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001862
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001863 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1864 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001865 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001866 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001867
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001868 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1869 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001870 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001871 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001872
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001873 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001874 }
1875
1876out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001877 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1878 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1879 "IRAM smaller than needed\n");
1880
1881 if (dev->devtype->product == CODA_7541) {
1882 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001883 if (ctx->inst_type == CODA_INST_DECODER) {
1884 /* fw 1.4.50 */
1885 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1886 CODA7_USE_IP_ENABLE);
1887 } else {
1888 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001889 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1890 CODA7_USE_HOST_DBK_ENABLE |
1891 CODA7_USE_IP_ENABLE |
1892 CODA7_USE_DBK_ENABLE);
1893 }
1894 }
1895}
1896
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001897static void coda_free_context_buffers(struct coda_ctx *ctx)
1898{
1899 struct coda_dev *dev = ctx->dev;
1900
Philipp Zabel918c66f2013-06-27 06:59:01 -03001901 coda_free_aux_buf(dev, &ctx->slicebuf);
1902 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001903 if (dev->devtype->product != CODA_DX6)
1904 coda_free_aux_buf(dev, &ctx->workbuf);
1905}
1906
1907static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1908 struct coda_q_data *q_data)
1909{
1910 struct coda_dev *dev = ctx->dev;
1911 size_t size;
1912 int ret;
1913
1914 switch (dev->devtype->product) {
1915 case CODA_7541:
1916 size = CODA7_WORK_BUF_SIZE;
1917 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001918 case CODA_960:
1919 size = CODA9_WORK_BUF_SIZE;
1920 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1921 size += CODA9_PS_SAVE_SIZE;
1922 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001923 default:
1924 return 0;
1925 }
1926
Philipp Zabel918c66f2013-06-27 06:59:01 -03001927 if (ctx->psbuf.vaddr) {
1928 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1929 return -EBUSY;
1930 }
1931 if (ctx->slicebuf.vaddr) {
1932 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1933 return -EBUSY;
1934 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001935 if (ctx->workbuf.vaddr) {
1936 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1937 ret = -EBUSY;
1938 return -ENOMEM;
1939 }
1940
Philipp Zabel918c66f2013-06-27 06:59:01 -03001941 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1942 /* worst case slice size */
1943 size = (DIV_ROUND_UP(q_data->width, 16) *
1944 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1945 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1946 if (ret < 0) {
1947 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1948 ctx->slicebuf.size);
1949 return ret;
1950 }
1951 }
1952
1953 if (dev->devtype->product == CODA_7541) {
1954 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1955 if (ret < 0) {
1956 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1957 goto err;
1958 }
1959 }
1960
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001961 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1962 if (ret < 0) {
1963 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1964 ctx->workbuf.size);
1965 goto err;
1966 }
1967
1968 return 0;
1969
1970err:
1971 coda_free_context_buffers(ctx);
1972 return ret;
1973}
1974
Philipp Zabel918c66f2013-06-27 06:59:01 -03001975static int coda_start_decoding(struct coda_ctx *ctx)
1976{
1977 struct coda_q_data *q_data_src, *q_data_dst;
1978 u32 bitstream_buf, bitstream_size;
1979 struct coda_dev *dev = ctx->dev;
1980 int width, height;
1981 u32 src_fourcc;
1982 u32 val;
1983 int ret;
1984
1985 /* Start decoding */
1986 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1987 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1988 bitstream_buf = ctx->bitstream.paddr;
1989 bitstream_size = ctx->bitstream.size;
1990 src_fourcc = q_data_src->fourcc;
1991
1992 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1993
1994 /* Update coda bitstream read and write pointers from kfifo */
1995 coda_kfifo_sync_to_device_full(ctx);
1996
1997 ctx->display_idx = -1;
1998 ctx->frm_dis_flg = 0;
1999 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2000
2001 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2002 CODA_REG_BIT_BIT_STREAM_PARAM);
2003
2004 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2005 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2006 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002007 if ((dev->devtype->product == CODA_7541) ||
2008 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002009 val |= CODA_REORDER_ENABLE;
2010 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2011
2012 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002013 if (dev->devtype->product == CODA_960 &&
2014 src_fourcc == V4L2_PIX_FMT_MPEG4)
2015 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2016 else
2017 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002018 if (src_fourcc == V4L2_PIX_FMT_H264) {
2019 if (dev->devtype->product == CODA_7541) {
2020 coda_write(dev, ctx->psbuf.paddr,
2021 CODA_CMD_DEC_SEQ_PS_BB_START);
2022 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2023 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2024 }
Philipp Zabel89548442014-07-11 06:36:17 -03002025 if (dev->devtype->product == CODA_960) {
2026 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2027 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2028 }
2029 }
2030 if (dev->devtype->product != CODA_960) {
2031 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002032 }
2033
2034 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2035 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2036 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2037 return -ETIMEDOUT;
2038 }
2039
2040 /* Update kfifo out pointer from coda bitstream read pointer */
2041 coda_kfifo_sync_from_device(ctx);
2042
2043 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2044
2045 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2046 v4l2_err(&dev->v4l2_dev,
2047 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2048 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2049 return -EAGAIN;
2050 }
2051
2052 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2053 if (dev->devtype->product == CODA_DX6) {
2054 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2055 height = val & CODADX6_PICHEIGHT_MASK;
2056 } else {
2057 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2058 height = val & CODA7_PICHEIGHT_MASK;
2059 }
2060
2061 if (width > q_data_dst->width || height > q_data_dst->height) {
2062 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2063 width, height, q_data_dst->width, q_data_dst->height);
2064 return -EINVAL;
2065 }
2066
2067 width = round_up(width, 16);
2068 height = round_up(height, 16);
2069
2070 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2071 __func__, ctx->idx, width, height);
2072
Philipp Zabela500a932014-07-11 06:36:13 -03002073 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002074 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2075 v4l2_err(&dev->v4l2_dev,
2076 "not enough framebuffers to decode (%d < %d)\n",
2077 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2078 return -EINVAL;
2079 }
2080
Philipp Zabel52c41672014-07-11 06:36:19 -03002081 if (src_fourcc == V4L2_PIX_FMT_H264) {
2082 u32 left_right;
2083 u32 top_bottom;
2084
2085 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2086 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2087
2088 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2089 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2090 q_data_dst->rect.width = width - q_data_dst->rect.left -
2091 (left_right & 0x3ff);
2092 q_data_dst->rect.height = height - q_data_dst->rect.top -
2093 (top_bottom & 0x3ff);
2094 }
2095
Philipp Zabel918c66f2013-06-27 06:59:01 -03002096 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2097 if (ret < 0)
2098 return ret;
2099
2100 /* Tell the decoder how many frame buffers we allocated. */
2101 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2102 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2103
2104 if (dev->devtype->product != CODA_DX6) {
2105 /* Set secondary AXI IRAM */
2106 coda_setup_iram(ctx);
2107
2108 coda_write(dev, ctx->iram_info.buf_bit_use,
2109 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2110 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2111 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2112 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2113 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2114 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2115 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2116 coda_write(dev, ctx->iram_info.buf_ovl_use,
2117 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002118 if (dev->devtype->product == CODA_960)
2119 coda_write(dev, ctx->iram_info.buf_btp_use,
2120 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2121 }
2122
2123 if (dev->devtype->product == CODA_960) {
2124 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2125
2126 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2127 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2128 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2129 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2130 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2131 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002132 }
2133
2134 if (src_fourcc == V4L2_PIX_FMT_H264) {
2135 coda_write(dev, ctx->slicebuf.paddr,
2136 CODA_CMD_SET_FRAME_SLICE_BB_START);
2137 coda_write(dev, ctx->slicebuf.size / 1024,
2138 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2139 }
2140
2141 if (dev->devtype->product == CODA_7541) {
2142 int max_mb_x = 1920 / 16;
2143 int max_mb_y = 1088 / 16;
2144 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002145
Philipp Zabel918c66f2013-06-27 06:59:01 -03002146 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2147 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002148 } else if (dev->devtype->product == CODA_960) {
2149 int max_mb_x = 1920 / 16;
2150 int max_mb_y = 1088 / 16;
2151 int max_mb_num = max_mb_x * max_mb_y;
2152
2153 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2154 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002155 }
2156
2157 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2158 v4l2_err(&ctx->dev->v4l2_dev,
2159 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2160 return -ETIMEDOUT;
2161 }
2162
2163 return 0;
2164}
2165
Philipp Zabeld35167a2013-05-23 10:42:59 -03002166static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2167 int header_code, u8 *header, int *size)
2168{
2169 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002170 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002171 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002172 int i;
2173
2174 if (dev->devtype->product == CODA_960)
2175 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002176
2177 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2178 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002179 bufsize = vb2_plane_size(buf, 0);
2180 if (dev->devtype->product == CODA_960)
2181 bufsize /= 1024;
2182 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002183 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2184 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2185 if (ret < 0) {
2186 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2187 return ret;
2188 }
Philipp Zabel89548442014-07-11 06:36:17 -03002189
2190 if (dev->devtype->product == CODA_960) {
2191 for (i = 63; i > 0; i--)
2192 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2193 break;
2194 *size = i + 1;
2195 } else {
2196 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2197 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2198 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002199 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2200
2201 return 0;
2202}
2203
Philipp Zabel89548442014-07-11 06:36:17 -03002204static int coda_start_encoding(struct coda_ctx *ctx);
2205
Javier Martin186b2502012-07-26 05:53:35 -03002206static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2207{
2208 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2209 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002210 struct coda_dev *dev = ctx->dev;
2211 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002212 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002213 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002214
Philipp Zabelb96904e2013-05-23 10:42:58 -03002215 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002216 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2217 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2218 if (coda_get_bitstream_payload(ctx) < 512)
2219 return -EINVAL;
2220 } else {
2221 if (count < 1)
2222 return -EINVAL;
2223 }
2224
2225 ctx->streamon_out = 1;
2226
Philipp Zabelb96904e2013-05-23 10:42:58 -03002227 if (coda_format_is_yuv(q_data_src->fourcc))
2228 ctx->inst_type = CODA_INST_ENCODER;
2229 else
2230 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002231 } else {
2232 if (count < 1)
2233 return -EINVAL;
2234
2235 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002236 }
Javier Martin186b2502012-07-26 05:53:35 -03002237
Philipp Zabelb96904e2013-05-23 10:42:58 -03002238 /* Don't start the coda unless both queues are on */
2239 if (!(ctx->streamon_out & ctx->streamon_cap))
2240 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002241
Philipp Zabeleb107512013-09-30 10:34:45 -03002242 /* Allow decoder device_run with no new buffers queued */
2243 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002244 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002245
Philipp Zabelb96904e2013-05-23 10:42:58 -03002246 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002247 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002248 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002249
Philipp Zabelb96904e2013-05-23 10:42:58 -03002250 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2251 q_data_dst->fourcc);
2252 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002253 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2254 return -EINVAL;
2255 }
2256
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002257 /* Allocate per-instance buffers */
2258 ret = coda_alloc_context_buffers(ctx, q_data_src);
2259 if (ret < 0)
2260 return ret;
2261
Philipp Zabel918c66f2013-06-27 06:59:01 -03002262 if (ctx->inst_type == CODA_INST_DECODER) {
2263 mutex_lock(&dev->coda_mutex);
2264 ret = coda_start_decoding(ctx);
2265 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002266 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002267 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002268 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002269 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002270 } else {
2271 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002272 }
2273
Philipp Zabel89548442014-07-11 06:36:17 -03002274 ctx->initialized = 1;
2275 return ret;
2276}
2277
2278static int coda_start_encoding(struct coda_ctx *ctx)
2279{
2280 struct coda_dev *dev = ctx->dev;
2281 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2282 struct coda_q_data *q_data_src, *q_data_dst;
2283 u32 bitstream_buf, bitstream_size;
2284 struct vb2_buffer *buf;
2285 int gamma, ret, value;
2286 u32 dst_fourcc;
2287
2288 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2289 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2290 dst_fourcc = q_data_dst->fourcc;
2291
Philipp Zabel14604e32014-07-11 06:36:22 -03002292 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002293 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2294 bitstream_size = q_data_dst->sizeimage;
2295
Javier Martin186b2502012-07-26 05:53:35 -03002296 if (!coda_is_initialized(dev)) {
2297 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2298 return -EFAULT;
2299 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002300
2301 mutex_lock(&dev->coda_mutex);
2302
Javier Martin186b2502012-07-26 05:53:35 -03002303 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002304 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2305 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002306 switch (dev->devtype->product) {
2307 case CODA_DX6:
2308 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2309 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2310 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002311 case CODA_960:
2312 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2313 /* fallthrough */
2314 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002315 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2316 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002317 break;
Javier Martin186b2502012-07-26 05:53:35 -03002318 }
2319
Philipp Zabel89548442014-07-11 06:36:17 -03002320 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2321 value &= ~(1 << 2 | 0x7 << 9);
2322 ctx->frame_mem_ctrl = value;
2323 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2324
Philipp Zabel10436672012-07-02 09:03:55 -03002325 if (dev->devtype->product == CODA_DX6) {
2326 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002327 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002328 }
Javier Martin186b2502012-07-26 05:53:35 -03002329
2330 /* Could set rotation here if needed */
2331 switch (dev->devtype->product) {
2332 case CODA_DX6:
2333 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002334 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002335 break;
2336 default:
2337 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002338 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002339 }
Javier Martin186b2502012-07-26 05:53:35 -03002340 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2341 coda_write(dev, ctx->params.framerate,
2342 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2343
Philipp Zabelb96904e2013-05-23 10:42:58 -03002344 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002345 switch (dst_fourcc) {
2346 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002347 if (dev->devtype->product == CODA_960)
2348 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2349 else
2350 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002351 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2352 break;
2353 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002354 if (dev->devtype->product == CODA_960)
2355 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2356 else
2357 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002358 if (ctx->params.h264_deblk_enabled) {
2359 value = ((ctx->params.h264_deblk_alpha &
2360 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2361 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2362 ((ctx->params.h264_deblk_beta &
2363 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2364 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2365 } else {
2366 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2367 }
2368 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002369 break;
2370 default:
2371 v4l2_err(v4l2_dev,
2372 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002373 ret = -EINVAL;
2374 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002375 }
2376
Philipp Zabelc566c782012-08-08 11:59:38 -03002377 switch (ctx->params.slice_mode) {
2378 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2379 value = 0;
2380 break;
2381 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2382 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2383 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002384 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002385 break;
2386 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2387 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2388 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2389 value |= 1 & CODA_SLICING_MODE_MASK;
2390 break;
2391 }
Javier Martin186b2502012-07-26 05:53:35 -03002392 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002393 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002394 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2395
2396 if (ctx->params.bitrate) {
2397 /* Rate control enabled */
2398 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2399 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002400 if (dev->devtype->product == CODA_960)
2401 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002402 } else {
2403 value = 0;
2404 }
2405 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2406
2407 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002408 coda_write(dev, ctx->params.intra_refresh,
2409 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002410
2411 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2412 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2413
Javier Martin186b2502012-07-26 05:53:35 -03002414
Philipp Zabel89548442014-07-11 06:36:17 -03002415 value = 0;
2416 if (dev->devtype->product == CODA_960)
2417 gamma = CODA9_DEFAULT_GAMMA;
2418 else
2419 gamma = CODA_DEFAULT_GAMMA;
2420 if (gamma > 0) {
2421 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2422 CODA_CMD_ENC_SEQ_RC_GAMMA);
2423 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002424
2425 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2426 coda_write(dev,
2427 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2428 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2429 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2430 }
Philipp Zabel89548442014-07-11 06:36:17 -03002431 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002432 if (ctx->params.h264_max_qp)
2433 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002434 if (CODA_DEFAULT_GAMMA > 0)
2435 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002436 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002437 if (CODA_DEFAULT_GAMMA > 0) {
2438 if (dev->devtype->product == CODA_DX6)
2439 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2440 else
2441 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2442 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002443 if (ctx->params.h264_min_qp)
2444 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2445 if (ctx->params.h264_max_qp)
2446 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002447 }
Javier Martin186b2502012-07-26 05:53:35 -03002448 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2449
Philipp Zabel89548442014-07-11 06:36:17 -03002450 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2451
Philipp Zabelc2d22512013-06-21 03:55:28 -03002452 coda_setup_iram(ctx);
2453
Javier Martin186b2502012-07-26 05:53:35 -03002454 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002455 switch (dev->devtype->product) {
2456 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002457 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002458 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002459 break;
2460 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002461 coda_write(dev, ctx->iram_info.search_ram_paddr,
2462 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2463 coda_write(dev, ctx->iram_info.search_ram_size,
2464 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002465 break;
2466 case CODA_960:
2467 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2468 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002469 }
Javier Martin186b2502012-07-26 05:53:35 -03002470 }
2471
Philipp Zabelfcb62822013-05-23 10:43:00 -03002472 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2473 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002474 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002475 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002476 }
2477
Philipp Zabelfcb62822013-05-23 10:43:00 -03002478 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2479 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2480 ret = -EFAULT;
2481 goto out;
2482 }
Javier Martin186b2502012-07-26 05:53:35 -03002483
Philipp Zabel89548442014-07-11 06:36:17 -03002484 if (dev->devtype->product == CODA_960)
2485 ctx->num_internal_frames = 4;
2486 else
2487 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002488 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002489 if (ret < 0) {
2490 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2491 goto out;
2492 }
Javier Martin186b2502012-07-26 05:53:35 -03002493
Philipp Zabelec25f682012-07-20 08:54:29 -03002494 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002495 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002496 if (dev->devtype->product == CODA_7541)
2497 coda_write(dev, round_up(q_data_src->width, 8),
2498 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002499 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002500 coda_write(dev, ctx->iram_info.buf_bit_use,
2501 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2502 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2503 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2504 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2505 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2506 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2507 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2508 coda_write(dev, ctx->iram_info.buf_ovl_use,
2509 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002510 if (dev->devtype->product == CODA_960) {
2511 coda_write(dev, ctx->iram_info.buf_btp_use,
2512 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2513
2514 /* FIXME */
2515 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2516 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2517 }
Philipp Zabel10436672012-07-02 09:03:55 -03002518 }
Philipp Zabel89548442014-07-11 06:36:17 -03002519
Philipp Zabelfcb62822013-05-23 10:43:00 -03002520 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2521 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002522 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002523 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002524 }
2525
2526 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002527 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002528 switch (dst_fourcc) {
2529 case V4L2_PIX_FMT_H264:
2530 /*
2531 * Get SPS in the first frame and copy it to an
2532 * intermediate buffer.
2533 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002534 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2535 &ctx->vpu_header[0][0],
2536 &ctx->vpu_header_size[0]);
2537 if (ret < 0)
2538 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002539
2540 /*
2541 * Get PPS in the first frame and copy it to an
2542 * intermediate buffer.
2543 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002544 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2545 &ctx->vpu_header[1][0],
2546 &ctx->vpu_header_size[1]);
2547 if (ret < 0)
2548 goto out;
2549
Javier Martin3f3f5c72012-10-29 05:20:29 -03002550 /*
2551 * Length of H.264 headers is variable and thus it might not be
2552 * aligned for the coda to append the encoded frame. In that is
2553 * the case a filler NAL must be added to header 2.
2554 */
2555 ctx->vpu_header_size[2] = coda_h264_padding(
2556 (ctx->vpu_header_size[0] +
2557 ctx->vpu_header_size[1]),
2558 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002559 break;
2560 case V4L2_PIX_FMT_MPEG4:
2561 /*
2562 * Get VOS in the first frame and copy it to an
2563 * intermediate buffer
2564 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002565 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2566 &ctx->vpu_header[0][0],
2567 &ctx->vpu_header_size[0]);
2568 if (ret < 0)
2569 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002570
Philipp Zabeld35167a2013-05-23 10:42:59 -03002571 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2572 &ctx->vpu_header[1][0],
2573 &ctx->vpu_header_size[1]);
2574 if (ret < 0)
2575 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002576
Philipp Zabeld35167a2013-05-23 10:42:59 -03002577 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2578 &ctx->vpu_header[2][0],
2579 &ctx->vpu_header_size[2]);
2580 if (ret < 0)
2581 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002582 break;
2583 default:
2584 /* No more formats need to save headers at the moment */
2585 break;
2586 }
2587
Philipp Zabeld35167a2013-05-23 10:42:59 -03002588out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002589 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002590 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002591}
2592
Hans Verkuile37559b2014-04-17 02:47:21 -03002593static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002594{
2595 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002596 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002597
2598 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002599 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002600 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002601 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002602
Philipp Zabel89548442014-07-11 06:36:17 -03002603 if (ctx->inst_type == CODA_INST_DECODER &&
2604 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2605 /* if this decoder instance is running, set the stream end flag */
2606 if (dev->devtype->product == CODA_960) {
2607 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2608
2609 val |= CODA_BIT_STREAM_END_FLAG;
2610 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2611 ctx->bit_stream_param = val;
2612 }
2613 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002614 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2615
2616 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002617 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002618 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002619 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002620 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002621
2622 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002623 }
2624
Philipp Zabel918c66f2013-06-27 06:59:01 -03002625 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002626 struct coda_timestamp *ts;
2627
2628 while (!list_empty(&ctx->timestamp_list)) {
2629 ts = list_first_entry(&ctx->timestamp_list,
2630 struct coda_timestamp, list);
2631 list_del(&ts->list);
2632 kfree(ts);
2633 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002634 kfifo_init(&ctx->bitstream_fifo,
2635 ctx->bitstream.vaddr, ctx->bitstream.size);
2636 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002637 }
Javier Martin186b2502012-07-26 05:53:35 -03002638}
2639
2640static struct vb2_ops coda_qops = {
2641 .queue_setup = coda_queue_setup,
2642 .buf_prepare = coda_buf_prepare,
2643 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002644 .start_streaming = coda_start_streaming,
2645 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002646 .wait_prepare = vb2_ops_wait_prepare,
2647 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002648};
2649
2650static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2651{
2652 struct coda_ctx *ctx =
2653 container_of(ctrl->handler, struct coda_ctx, ctrls);
2654
2655 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2656 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2657
2658 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002659 case V4L2_CID_HFLIP:
2660 if (ctrl->val)
2661 ctx->params.rot_mode |= CODA_MIR_HOR;
2662 else
2663 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2664 break;
2665 case V4L2_CID_VFLIP:
2666 if (ctrl->val)
2667 ctx->params.rot_mode |= CODA_MIR_VER;
2668 else
2669 ctx->params.rot_mode &= ~CODA_MIR_VER;
2670 break;
Javier Martin186b2502012-07-26 05:53:35 -03002671 case V4L2_CID_MPEG_VIDEO_BITRATE:
2672 ctx->params.bitrate = ctrl->val / 1000;
2673 break;
2674 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2675 ctx->params.gop_size = ctrl->val;
2676 break;
2677 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2678 ctx->params.h264_intra_qp = ctrl->val;
2679 break;
2680 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2681 ctx->params.h264_inter_qp = ctrl->val;
2682 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002683 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2684 ctx->params.h264_min_qp = ctrl->val;
2685 break;
2686 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2687 ctx->params.h264_max_qp = ctrl->val;
2688 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002689 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2690 ctx->params.h264_deblk_alpha = ctrl->val;
2691 break;
2692 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2693 ctx->params.h264_deblk_beta = ctrl->val;
2694 break;
2695 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2696 ctx->params.h264_deblk_enabled = (ctrl->val ==
2697 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2698 break;
Javier Martin186b2502012-07-26 05:53:35 -03002699 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2700 ctx->params.mpeg4_intra_qp = ctrl->val;
2701 break;
2702 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2703 ctx->params.mpeg4_inter_qp = ctrl->val;
2704 break;
2705 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2706 ctx->params.slice_mode = ctrl->val;
2707 break;
2708 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2709 ctx->params.slice_max_mb = ctrl->val;
2710 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002711 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2712 ctx->params.slice_max_bits = ctrl->val * 8;
2713 break;
Javier Martin186b2502012-07-26 05:53:35 -03002714 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2715 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002716 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2717 ctx->params.intra_refresh = ctrl->val;
2718 break;
Javier Martin186b2502012-07-26 05:53:35 -03002719 default:
2720 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2721 "Invalid control, id=%d, val=%d\n",
2722 ctrl->id, ctrl->val);
2723 return -EINVAL;
2724 }
2725
2726 return 0;
2727}
2728
2729static struct v4l2_ctrl_ops coda_ctrl_ops = {
2730 .s_ctrl = coda_s_ctrl,
2731};
2732
2733static int coda_ctrls_setup(struct coda_ctx *ctx)
2734{
2735 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2736
2737 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002738 V4L2_CID_HFLIP, 0, 1, 1, 0);
2739 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2740 V4L2_CID_VFLIP, 0, 1, 1, 0);
2741 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002742 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2743 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2744 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2745 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002746 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002747 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002748 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002749 if (ctx->dev->devtype->product != CODA_960) {
2750 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2751 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2752 }
2753 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2754 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002755 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002756 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2757 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2758 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2759 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2760 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2761 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2762 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2763 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002764 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2765 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2766 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2767 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2768 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002769 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2770 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002771 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2772 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002773 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2774 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002775 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2776 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2777 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2778 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2779 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002780 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2781 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002782
2783 if (ctx->ctrls.error) {
2784 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2785 ctx->ctrls.error);
2786 return -EINVAL;
2787 }
2788
2789 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2790}
2791
2792static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2793 struct vb2_queue *dst_vq)
2794{
2795 struct coda_ctx *ctx = priv;
2796 int ret;
2797
Javier Martin186b2502012-07-26 05:53:35 -03002798 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002799 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002800 src_vq->drv_priv = ctx;
2801 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2802 src_vq->ops = &coda_qops;
2803 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002804 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002805 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002806
2807 ret = vb2_queue_init(src_vq);
2808 if (ret)
2809 return ret;
2810
Javier Martin186b2502012-07-26 05:53:35 -03002811 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002812 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002813 dst_vq->drv_priv = ctx;
2814 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2815 dst_vq->ops = &coda_qops;
2816 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002817 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002818 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002819
2820 return vb2_queue_init(dst_vq);
2821}
2822
Philipp Zabele11f3e62012-07-25 09:16:58 -03002823static int coda_next_free_instance(struct coda_dev *dev)
2824{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002825 int idx = ffz(dev->instance_mask);
2826
2827 if ((idx < 0) ||
2828 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2829 return -EBUSY;
2830
2831 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002832}
2833
Javier Martin186b2502012-07-26 05:53:35 -03002834static int coda_open(struct file *file)
2835{
2836 struct coda_dev *dev = video_drvdata(file);
2837 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002838 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002839 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002840
Javier Martin186b2502012-07-26 05:53:35 -03002841 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2842 if (!ctx)
2843 return -ENOMEM;
2844
Fabio Estevamf82bc202013-08-21 11:14:16 -03002845 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002846 if (idx < 0) {
2847 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002848 goto err_coda_max;
2849 }
2850 set_bit(idx, &dev->instance_mask);
2851
Philipp Zabel32b6f202014-07-11 06:36:20 -03002852 init_completion(&ctx->completion);
2853 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2854 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002855 v4l2_fh_init(&ctx->fh, video_devdata(file));
2856 file->private_data = &ctx->fh;
2857 v4l2_fh_add(&ctx->fh);
2858 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002859 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002860 switch (dev->devtype->product) {
2861 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002862 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002863 ctx->reg_idx = 0;
2864 break;
2865 default:
2866 ctx->reg_idx = idx;
2867 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002868
Philipp Zabel1e172732014-07-11 06:36:23 -03002869 /* Power up and upload firmware if necessary */
2870 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2871 if (ret < 0) {
2872 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2873 goto err_pm_get;
2874 }
2875
Fabio Estevam79830db2013-08-21 11:14:17 -03002876 ret = clk_prepare_enable(dev->clk_per);
2877 if (ret)
2878 goto err_clk_per;
2879
2880 ret = clk_prepare_enable(dev->clk_ahb);
2881 if (ret)
2882 goto err_clk_ahb;
2883
Javier Martin186b2502012-07-26 05:53:35 -03002884 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002885 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002886 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002887 if (IS_ERR(ctx->fh.m2m_ctx)) {
2888 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002889
2890 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2891 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002892 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002893 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002894
Javier Martin186b2502012-07-26 05:53:35 -03002895 ret = coda_ctrls_setup(ctx);
2896 if (ret) {
2897 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002898 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002899 }
2900
2901 ctx->fh.ctrl_handler = &ctx->ctrls;
2902
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002903 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2904 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002905 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002906 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002907 }
2908
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002909 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2910 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2911 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2912 if (!ctx->bitstream.vaddr) {
2913 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2914 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002915 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002916 }
2917 kfifo_init(&ctx->bitstream_fifo,
2918 ctx->bitstream.vaddr, ctx->bitstream.size);
2919 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002920 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002921 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002922
Javier Martin186b2502012-07-26 05:53:35 -03002923 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002924 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002925 coda_unlock(ctx);
2926
Javier Martin186b2502012-07-26 05:53:35 -03002927 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2928 ctx->idx, ctx);
2929
2930 return 0;
2931
Fabio Estevamf82bc202013-08-21 11:14:16 -03002932err_dma_writecombine:
2933 coda_free_context_buffers(ctx);
2934 if (ctx->dev->devtype->product == CODA_DX6)
2935 coda_free_aux_buf(dev, &ctx->workbuf);
2936 coda_free_aux_buf(dev, &ctx->parabuf);
2937err_dma_alloc:
2938 v4l2_ctrl_handler_free(&ctx->ctrls);
2939err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002940 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002941err_ctx_init:
2942 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002943err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002944 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002945err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002946 pm_runtime_put_sync(&dev->plat_dev->dev);
2947err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002948 v4l2_fh_del(&ctx->fh);
2949 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002950 clear_bit(ctx->idx, &dev->instance_mask);
2951err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002952 kfree(ctx);
2953 return ret;
2954}
2955
2956static int coda_release(struct file *file)
2957{
2958 struct coda_dev *dev = video_drvdata(file);
2959 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2960
2961 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2962 ctx);
2963
Philipp Zabel918c66f2013-06-27 06:59:01 -03002964 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002965 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002966
2967 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002968 if (ctx->initialized) {
2969 queue_work(dev->workqueue, &ctx->seq_end_work);
2970 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002971 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002972
2973 coda_free_framebuffers(ctx);
2974
Javier Martin186b2502012-07-26 05:53:35 -03002975 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002976 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002977 coda_unlock(ctx);
2978
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002979 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2980 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002981 coda_free_context_buffers(ctx);
2982 if (ctx->dev->devtype->product == CODA_DX6)
2983 coda_free_aux_buf(dev, &ctx->workbuf);
2984
2985 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002986 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002987 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002988 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002989 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002990 v4l2_fh_del(&ctx->fh);
2991 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002992 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002993 kfree(ctx);
2994
2995 return 0;
2996}
2997
Javier Martin186b2502012-07-26 05:53:35 -03002998static const struct v4l2_file_operations coda_fops = {
2999 .owner = THIS_MODULE,
3000 .open = coda_open,
3001 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003002 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003003 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003004 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003005};
3006
Philipp Zabel918c66f2013-06-27 06:59:01 -03003007static void coda_finish_decode(struct coda_ctx *ctx)
3008{
3009 struct coda_dev *dev = ctx->dev;
3010 struct coda_q_data *q_data_src;
3011 struct coda_q_data *q_data_dst;
3012 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003013 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003014 int width, height;
3015 int decoded_idx;
3016 int display_idx;
3017 u32 src_fourcc;
3018 int success;
3019 u32 val;
3020
Philipp Zabel14604e32014-07-11 06:36:22 -03003021 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003022
3023 /* Update kfifo out pointer from coda bitstream read pointer */
3024 coda_kfifo_sync_from_device(ctx);
3025
3026 /*
3027 * in stream-end mode, the read pointer can overshoot the write pointer
3028 * by up to 512 bytes
3029 */
3030 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3031 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3032 kfifo_init(&ctx->bitstream_fifo,
3033 ctx->bitstream.vaddr, ctx->bitstream.size);
3034 }
3035
3036 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3037 src_fourcc = q_data_src->fourcc;
3038
3039 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3040 if (val != 1)
3041 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3042
3043 success = val & 0x1;
3044 if (!success)
3045 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3046
3047 if (src_fourcc == V4L2_PIX_FMT_H264) {
3048 if (val & (1 << 3))
3049 v4l2_err(&dev->v4l2_dev,
3050 "insufficient PS buffer space (%d bytes)\n",
3051 ctx->psbuf.size);
3052 if (val & (1 << 2))
3053 v4l2_err(&dev->v4l2_dev,
3054 "insufficient slice buffer space (%d bytes)\n",
3055 ctx->slicebuf.size);
3056 }
3057
3058 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3059 width = (val >> 16) & 0xffff;
3060 height = val & 0xffff;
3061
3062 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3063
Philipp Zabel52c41672014-07-11 06:36:19 -03003064 /* frame crop information */
3065 if (src_fourcc == V4L2_PIX_FMT_H264) {
3066 u32 left_right;
3067 u32 top_bottom;
3068
3069 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3070 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3071
3072 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3073 /* Keep current crop information */
3074 } else {
3075 struct v4l2_rect *rect = &q_data_dst->rect;
3076
3077 rect->left = left_right >> 16 & 0xffff;
3078 rect->top = top_bottom >> 16 & 0xffff;
3079 rect->width = width - rect->left -
3080 (left_right & 0xffff);
3081 rect->height = height - rect->top -
3082 (top_bottom & 0xffff);
3083 }
3084 } else {
3085 /* no cropping */
3086 }
3087
Philipp Zabel918c66f2013-06-27 06:59:01 -03003088 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3089 if (val > 0)
3090 v4l2_err(&dev->v4l2_dev,
3091 "errors in %d macroblocks\n", val);
3092
3093 if (dev->devtype->product == CODA_7541) {
3094 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3095 if (val == 0) {
3096 /* not enough bitstream data */
3097 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3098 "prescan failed: %d\n", val);
3099 ctx->prescan_failed = true;
3100 return;
3101 }
3102 }
3103
3104 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3105
3106 /*
3107 * The previous display frame was copied out by the rotator,
3108 * now it can be overwritten again
3109 */
3110 if (ctx->display_idx >= 0 &&
3111 ctx->display_idx < ctx->num_internal_frames) {
3112 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3113 coda_write(dev, ctx->frm_dis_flg,
3114 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3115 }
3116
3117 /*
3118 * The index of the last decoded frame, not necessarily in
3119 * display order, and the index of the next display frame.
3120 * The latter could have been decoded in a previous run.
3121 */
3122 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3123 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3124
3125 if (decoded_idx == -1) {
3126 /* no frame was decoded, but we might have a display frame */
3127 if (display_idx < 0 && ctx->display_idx < 0)
3128 ctx->prescan_failed = true;
3129 } else if (decoded_idx == -2) {
3130 /* no frame was decoded, we still return the remaining buffers */
3131 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3132 v4l2_err(&dev->v4l2_dev,
3133 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003134 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003135 ts = list_first_entry(&ctx->timestamp_list,
3136 struct coda_timestamp, list);
3137 list_del(&ts->list);
3138 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
3139 if (val != ts->sequence) {
3140 v4l2_err(&dev->v4l2_dev,
3141 "sequence number mismatch (%d != %d)\n",
3142 val, ts->sequence);
3143 }
3144 ctx->frame_timestamps[decoded_idx] = *ts;
3145 kfree(ts);
3146
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003147 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3148 if (val == 0)
3149 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3150 else if (val == 1)
3151 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3152 else
3153 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003154 }
3155
3156 if (display_idx == -1) {
3157 /*
3158 * no more frames to be decoded, but there could still
3159 * be rotator output to dequeue
3160 */
3161 ctx->prescan_failed = true;
3162 } else if (display_idx == -3) {
3163 /* possibly prescan failure */
3164 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3165 v4l2_err(&dev->v4l2_dev,
3166 "presentation frame index out of range: %d\n",
3167 display_idx);
3168 }
3169
3170 /* If a frame was copied out, return it */
3171 if (ctx->display_idx >= 0 &&
3172 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003173 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003174 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3175
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003176 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3177 V4L2_BUF_FLAG_PFRAME |
3178 V4L2_BUF_FLAG_BFRAME);
3179 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003180 ts = &ctx->frame_timestamps[ctx->display_idx];
3181 dst_buf->v4l2_buf.timecode = ts->timecode;
3182 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003183
Philipp Zabel918c66f2013-06-27 06:59:01 -03003184 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3185
3186 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3187 VB2_BUF_STATE_ERROR);
3188
3189 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3190 "job finished: decoding frame (%d) (%s)\n",
3191 dst_buf->v4l2_buf.sequence,
3192 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3193 "KEYFRAME" : "PFRAME");
3194 } else {
3195 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3196 "job finished: no frame decoded\n");
3197 }
3198
3199 /* The rotator will copy the current display frame next time */
3200 ctx->display_idx = display_idx;
3201}
3202
3203static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003204{
Philipp Zabelec25f682012-07-20 08:54:29 -03003205 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003206 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003207 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003208
Philipp Zabel14604e32014-07-11 06:36:22 -03003209 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3210 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003211
3212 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003213 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003214 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3215
Javier Martin186b2502012-07-26 05:53:35 -03003216 /* Calculate bytesused field */
3217 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003218 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3219 ctx->vpu_header_size[0] +
3220 ctx->vpu_header_size[1] +
3221 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003222 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003223 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003224 }
3225
3226 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3227 wr_ptr - start_ptr);
3228
3229 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3230 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3231
Philipp Zabel51660282013-09-30 10:34:49 -03003232 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003233 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3234 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3235 } else {
3236 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3237 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3238 }
3239
Kamil Debskiccd571c2013-04-24 10:38:24 -03003240 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003241 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3242 dst_buf->v4l2_buf.flags |=
3243 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003244 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3245
Philipp Zabelec25f682012-07-20 08:54:29 -03003246 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003247
Philipp Zabel14604e32014-07-11 06:36:22 -03003248 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003249 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3250
3251 ctx->gopcounter--;
3252 if (ctx->gopcounter < 0)
3253 ctx->gopcounter = ctx->params.gop_size - 1;
3254
3255 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3256 "job finished: encoding frame (%d) (%s)\n",
3257 dst_buf->v4l2_buf.sequence,
3258 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3259 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003260}
3261
3262static irqreturn_t coda_irq_handler(int irq, void *data)
3263{
3264 struct coda_dev *dev = data;
3265 struct coda_ctx *ctx;
3266
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003267 /* read status register to attend the IRQ */
3268 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3269 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3270 CODA_REG_BIT_INT_CLEAR);
3271
3272 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3273 if (ctx == NULL) {
3274 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3275 mutex_unlock(&dev->coda_mutex);
3276 return IRQ_HANDLED;
3277 }
3278
3279 if (ctx->aborting) {
3280 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3281 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003282 }
3283
3284 if (coda_isbusy(ctx->dev)) {
3285 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3286 "coda is still busy!!!!\n");
3287 return IRQ_NONE;
3288 }
3289
Philipp Zabel32b6f202014-07-11 06:36:20 -03003290 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003291
3292 return IRQ_HANDLED;
3293}
3294
3295static u32 coda_supported_firmwares[] = {
3296 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003297 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003298 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003299};
3300
3301static bool coda_firmware_supported(u32 vernum)
3302{
3303 int i;
3304
3305 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3306 if (vernum == coda_supported_firmwares[i])
3307 return true;
3308 return false;
3309}
3310
Philipp Zabel87048bb2012-07-02 07:03:43 -03003311static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003312{
Javier Martin186b2502012-07-26 05:53:35 -03003313 u32 data;
3314 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003315 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003316
Fabio Estevam79830db2013-08-21 11:14:17 -03003317 ret = clk_prepare_enable(dev->clk_per);
3318 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003319 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003320
3321 ret = clk_prepare_enable(dev->clk_ahb);
3322 if (ret)
3323 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003324
Javier Martin186b2502012-07-26 05:53:35 -03003325 /*
3326 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003327 * The 16-bit chars in the code buffer are in memory access
3328 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003329 * Data in this SRAM survives a reboot.
3330 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003331 p = (u16 *)dev->codebuf.vaddr;
3332 if (dev->devtype->product == CODA_DX6) {
3333 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3334 data = CODA_DOWN_ADDRESS_SET(i) |
3335 CODA_DOWN_DATA_SET(p[i ^ 1]);
3336 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3337 }
3338 } else {
3339 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3340 data = CODA_DOWN_ADDRESS_SET(i) |
3341 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3342 3 - (i % 4)]);
3343 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3344 }
Javier Martin186b2502012-07-26 05:53:35 -03003345 }
Javier Martin186b2502012-07-26 05:53:35 -03003346
Philipp Zabel9acf7692013-05-23 10:42:56 -03003347 /* Clear registers */
3348 for (i = 0; i < 64; i++)
3349 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3350
Javier Martin186b2502012-07-26 05:53:35 -03003351 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003352 if (dev->devtype->product == CODA_960 ||
3353 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003354 coda_write(dev, dev->tempbuf.paddr,
3355 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003356 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003357 } else {
3358 coda_write(dev, dev->workbuf.paddr,
3359 CODA_REG_BIT_WORK_BUF_ADDR);
3360 }
Javier Martin186b2502012-07-26 05:53:35 -03003361 coda_write(dev, dev->codebuf.paddr,
3362 CODA_REG_BIT_CODE_BUF_ADDR);
3363 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3364
3365 /* Set default values */
3366 switch (dev->devtype->product) {
3367 case CODA_DX6:
3368 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3369 break;
3370 default:
3371 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3372 }
Philipp Zabel89548442014-07-11 06:36:17 -03003373 if (dev->devtype->product == CODA_960)
3374 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3375 else
3376 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003377
3378 if (dev->devtype->product != CODA_DX6)
3379 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3380
Javier Martin186b2502012-07-26 05:53:35 -03003381 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3382 CODA_REG_BIT_INT_ENABLE);
3383
3384 /* Reset VPU and start processor */
3385 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3386 data |= CODA_REG_RESET_ENABLE;
3387 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3388 udelay(10);
3389 data &= ~CODA_REG_RESET_ENABLE;
3390 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3391 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3392
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003393 clk_disable_unprepare(dev->clk_ahb);
3394 clk_disable_unprepare(dev->clk_per);
3395
3396 return 0;
3397
3398err_clk_ahb:
3399 clk_disable_unprepare(dev->clk_per);
3400err_clk_per:
3401 return ret;
3402}
3403
3404static int coda_check_firmware(struct coda_dev *dev)
3405{
3406 u16 product, major, minor, release;
3407 u32 data;
3408 int ret;
3409
3410 ret = clk_prepare_enable(dev->clk_per);
3411 if (ret)
3412 goto err_clk_per;
3413
3414 ret = clk_prepare_enable(dev->clk_ahb);
3415 if (ret)
3416 goto err_clk_ahb;
3417
Javier Martin186b2502012-07-26 05:53:35 -03003418 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3419 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3420 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3421 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3422 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3423 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003424 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003425 ret = -EIO;
3426 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003427 }
3428
Philipp Zabel89548442014-07-11 06:36:17 -03003429 if (dev->devtype->product == CODA_960) {
3430 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3431 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3432 data);
3433 }
3434
Javier Martin186b2502012-07-26 05:53:35 -03003435 /* Check we are compatible with the loaded firmware */
3436 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3437 product = CODA_FIRMWARE_PRODUCT(data);
3438 major = CODA_FIRMWARE_MAJOR(data);
3439 minor = CODA_FIRMWARE_MINOR(data);
3440 release = CODA_FIRMWARE_RELEASE(data);
3441
3442 clk_disable_unprepare(dev->clk_per);
3443 clk_disable_unprepare(dev->clk_ahb);
3444
3445 if (product != dev->devtype->product) {
3446 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3447 " Version: %u.%u.%u\n",
3448 coda_product_name(dev->devtype->product),
3449 coda_product_name(product), major, minor, release);
3450 return -EINVAL;
3451 }
3452
3453 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3454 coda_product_name(product));
3455
3456 if (coda_firmware_supported(data)) {
3457 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3458 major, minor, release);
3459 } else {
3460 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3461 "%u.%u.%u\n", major, minor, release);
3462 }
3463
3464 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003465
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003466err_run_cmd:
3467 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003468err_clk_ahb:
3469 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003470err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003471 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003472}
3473
3474static void coda_fw_callback(const struct firmware *fw, void *context)
3475{
3476 struct coda_dev *dev = context;
3477 struct platform_device *pdev = dev->plat_dev;
3478 int ret;
3479
3480 if (!fw) {
3481 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3482 return;
3483 }
3484
3485 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003486 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3487 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003488 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3489 return;
3490 }
3491
Philipp Zabel87048bb2012-07-02 07:03:43 -03003492 /* Copy the whole firmware image to the code buffer */
3493 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3494 release_firmware(fw);
3495
Philipp Zabel1e172732014-07-11 06:36:23 -03003496 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3497 /*
3498 * Enabling power temporarily will cause coda_hw_init to be
3499 * called via coda_runtime_resume by the pm domain.
3500 */
3501 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3502 if (ret < 0) {
3503 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3504 ret);
3505 return;
3506 }
3507
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003508 ret = coda_check_firmware(dev);
3509 if (ret < 0)
3510 return;
3511
Philipp Zabel1e172732014-07-11 06:36:23 -03003512 pm_runtime_put_sync(&dev->plat_dev->dev);
3513 } else {
3514 /*
3515 * If runtime pm is disabled or pm_domain is not set,
3516 * initialize once manually.
3517 */
3518 ret = coda_hw_init(dev);
3519 if (ret < 0) {
3520 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3521 return;
3522 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003523
3524 ret = coda_check_firmware(dev);
3525 if (ret < 0)
3526 return;
Javier Martin186b2502012-07-26 05:53:35 -03003527 }
3528
3529 dev->vfd.fops = &coda_fops,
3530 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3531 dev->vfd.release = video_device_release_empty,
3532 dev->vfd.lock = &dev->dev_mutex;
3533 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003534 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003535 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3536 video_set_drvdata(&dev->vfd, dev);
3537
3538 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3539 if (IS_ERR(dev->alloc_ctx)) {
3540 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3541 return;
3542 }
3543
3544 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3545 if (IS_ERR(dev->m2m_dev)) {
3546 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3547 goto rel_ctx;
3548 }
3549
3550 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3551 if (ret) {
3552 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3553 goto rel_m2m;
3554 }
3555 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3556 dev->vfd.num);
3557
3558 return;
3559
3560rel_m2m:
3561 v4l2_m2m_release(dev->m2m_dev);
3562rel_ctx:
3563 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3564}
3565
3566static int coda_firmware_request(struct coda_dev *dev)
3567{
3568 char *fw = dev->devtype->firmware;
3569
3570 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3571 coda_product_name(dev->devtype->product));
3572
3573 return request_firmware_nowait(THIS_MODULE, true,
3574 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3575}
3576
3577enum coda_platform {
3578 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003579 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003580 CODA_IMX6Q,
3581 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003582};
3583
Emil Goodec06d8752012-08-14 17:44:42 -03003584static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003585 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003586 .firmware = "v4l-codadx6-imx27.bin",
3587 .product = CODA_DX6,
3588 .codecs = codadx6_codecs,
3589 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003590 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003591 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003592 .firmware = "v4l-coda7541-imx53.bin",
3593 .product = CODA_7541,
3594 .codecs = coda7_codecs,
3595 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003596 },
Philipp Zabel89548442014-07-11 06:36:17 -03003597 [CODA_IMX6Q] = {
3598 .firmware = "v4l-coda960-imx6q.bin",
3599 .product = CODA_960,
3600 .codecs = coda9_codecs,
3601 .num_codecs = ARRAY_SIZE(coda9_codecs),
3602 },
3603 [CODA_IMX6DL] = {
3604 .firmware = "v4l-coda960-imx6dl.bin",
3605 .product = CODA_960,
3606 .codecs = coda9_codecs,
3607 .num_codecs = ARRAY_SIZE(coda9_codecs),
3608 },
Javier Martin186b2502012-07-26 05:53:35 -03003609};
3610
3611static struct platform_device_id coda_platform_ids[] = {
3612 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003613 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003614 { /* sentinel */ }
3615};
3616MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3617
3618#ifdef CONFIG_OF
3619static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003620 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003621 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003622 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3623 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003624 { /* sentinel */ }
3625};
3626MODULE_DEVICE_TABLE(of, coda_dt_ids);
3627#endif
3628
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003629static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003630{
3631 const struct of_device_id *of_id =
3632 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3633 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003634 struct coda_platform_data *pdata = pdev->dev.platform_data;
3635 struct device_node *np = pdev->dev.of_node;
3636 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003637 struct coda_dev *dev;
3638 struct resource *res;
3639 int ret, irq;
3640
3641 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3642 if (!dev) {
3643 dev_err(&pdev->dev, "Not enough memory for %s\n",
3644 CODA_NAME);
3645 return -ENOMEM;
3646 }
3647
3648 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003649 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003650
3651 dev->plat_dev = pdev;
3652 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3653 if (IS_ERR(dev->clk_per)) {
3654 dev_err(&pdev->dev, "Could not get per clock\n");
3655 return PTR_ERR(dev->clk_per);
3656 }
3657
3658 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3659 if (IS_ERR(dev->clk_ahb)) {
3660 dev_err(&pdev->dev, "Could not get ahb clock\n");
3661 return PTR_ERR(dev->clk_ahb);
3662 }
3663
3664 /* Get memory for physical registers */
3665 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003666 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3667 if (IS_ERR(dev->regs_base))
3668 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003669
3670 /* IRQ */
3671 irq = platform_get_irq(pdev, 0);
3672 if (irq < 0) {
3673 dev_err(&pdev->dev, "failed to get irq resource\n");
3674 return -ENOENT;
3675 }
3676
Philipp Zabel918c66f2013-06-27 06:59:01 -03003677 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003678 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003679 dev_err(&pdev->dev, "failed to request irq\n");
3680 return -ENOENT;
3681 }
3682
Philipp Zabel657eee72013-04-29 16:17:14 -07003683 /* Get IRAM pool from device tree or platform data */
3684 pool = of_get_named_gen_pool(np, "iram", 0);
3685 if (!pool && pdata)
3686 pool = dev_get_gen_pool(pdata->iram_dev);
3687 if (!pool) {
3688 dev_err(&pdev->dev, "iram pool not available\n");
3689 return -ENOMEM;
3690 }
3691 dev->iram_pool = pool;
3692
Javier Martin186b2502012-07-26 05:53:35 -03003693 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3694 if (ret)
3695 return ret;
3696
3697 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003698 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003699
3700 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3701
3702 if (of_id) {
3703 dev->devtype = of_id->data;
3704 } else if (pdev_id) {
3705 dev->devtype = &coda_devdata[pdev_id->driver_data];
3706 } else {
3707 v4l2_device_unregister(&dev->v4l2_dev);
3708 return -EINVAL;
3709 }
3710
3711 /* allocate auxiliary per-device buffers for the BIT processor */
3712 switch (dev->devtype->product) {
3713 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003714 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3715 CODADX6_WORK_BUF_SIZE);
3716 if (ret < 0) {
3717 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3718 v4l2_device_unregister(&dev->v4l2_dev);
3719 return ret;
3720 }
Javier Martin186b2502012-07-26 05:53:35 -03003721 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003722 case CODA_7541:
3723 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3724 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003725 case CODA_960:
3726 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3727 break;
Javier Martin186b2502012-07-26 05:53:35 -03003728 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003729 if (dev->tempbuf.size) {
3730 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3731 dev->tempbuf.size);
3732 if (ret < 0) {
3733 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3734 v4l2_device_unregister(&dev->v4l2_dev);
3735 return ret;
3736 }
Javier Martin186b2502012-07-26 05:53:35 -03003737 }
3738
Philipp Zabel918c66f2013-06-27 06:59:01 -03003739 switch (dev->devtype->product) {
3740 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003741 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003742 break;
3743 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003744 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003745 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003746 case CODA_960:
3747 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003748 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003749 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3750 &dev->iram.paddr);
3751 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003752 dev_err(&pdev->dev, "unable to alloc iram\n");
3753 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003754 }
3755
Philipp Zabel32b6f202014-07-11 06:36:20 -03003756 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3757 if (!dev->workqueue) {
3758 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3759 return -ENOMEM;
3760 }
3761
Javier Martin186b2502012-07-26 05:53:35 -03003762 platform_set_drvdata(pdev, dev);
3763
Philipp Zabel1e172732014-07-11 06:36:23 -03003764 pm_runtime_enable(&pdev->dev);
3765
Javier Martin186b2502012-07-26 05:53:35 -03003766 return coda_firmware_request(dev);
3767}
3768
3769static int coda_remove(struct platform_device *pdev)
3770{
3771 struct coda_dev *dev = platform_get_drvdata(pdev);
3772
3773 video_unregister_device(&dev->vfd);
3774 if (dev->m2m_dev)
3775 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003776 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003777 if (dev->alloc_ctx)
3778 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3779 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003780 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003781 if (dev->iram.vaddr)
3782 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3783 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003784 coda_free_aux_buf(dev, &dev->codebuf);
3785 coda_free_aux_buf(dev, &dev->tempbuf);
3786 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003787 return 0;
3788}
3789
Philipp Zabel1e172732014-07-11 06:36:23 -03003790#ifdef CONFIG_PM_RUNTIME
3791static int coda_runtime_resume(struct device *dev)
3792{
3793 struct coda_dev *cdev = dev_get_drvdata(dev);
3794 int ret = 0;
3795
3796 if (dev->pm_domain) {
3797 ret = coda_hw_init(cdev);
3798 if (ret)
3799 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3800 }
3801
3802 return ret;
3803}
3804#endif
3805
3806static const struct dev_pm_ops coda_pm_ops = {
3807 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3808};
3809
Javier Martin186b2502012-07-26 05:53:35 -03003810static struct platform_driver coda_driver = {
3811 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003812 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003813 .driver = {
3814 .name = CODA_NAME,
3815 .owner = THIS_MODULE,
3816 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003817 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003818 },
3819 .id_table = coda_platform_ids,
3820};
3821
3822module_platform_driver(coda_driver);
3823
3824MODULE_LICENSE("GPL");
3825MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3826MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");