blob: d20343b32eedc51a276dbc180e182f118a4b83c2 [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;
Philipp Zabelcb2c0282014-07-11 06:36:33 -0300225 u32 sequence_offset;
Javier Martin186b2502012-07-26 05:53:35 -0300226 struct coda_q_data q_data[2];
227 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300228 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300229 enum v4l2_colorspace colorspace;
230 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300231 struct v4l2_ctrl_handler ctrls;
232 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300233 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300234 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300235 char vpu_header[3][64];
236 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300237 struct kfifo bitstream_fifo;
238 struct mutex bitstream_mutex;
239 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300240 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300241 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300242 struct coda_aux_buf psbuf;
243 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300244 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300245 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300246 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300247 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300248 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300249 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300250 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300251 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300252 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300253 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300254 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300255 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300256 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300257 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300258 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300259};
260
Javier Martin832fbb52012-10-29 08:34:59 -0300261static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
262 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
263static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300264
Javier Martin186b2502012-07-26 05:53:35 -0300265static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
266{
267 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
268 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
269 writel(data, dev->regs_base + reg);
270}
271
272static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
273{
274 u32 data;
275 data = readl(dev->regs_base + reg);
276 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
277 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
278 return data;
279}
280
281static inline unsigned long coda_isbusy(struct coda_dev *dev)
282{
283 return coda_read(dev, CODA_REG_BIT_BUSY);
284}
285
286static inline int coda_is_initialized(struct coda_dev *dev)
287{
288 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
289}
290
291static int coda_wait_timeout(struct coda_dev *dev)
292{
293 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
294
295 while (coda_isbusy(dev)) {
296 if (time_after(jiffies, timeout))
297 return -ETIMEDOUT;
298 }
299 return 0;
300}
301
302static void coda_command_async(struct coda_ctx *ctx, int cmd)
303{
304 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300305
Philipp Zabel89548442014-07-11 06:36:17 -0300306 if (dev->devtype->product == CODA_960 ||
307 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300308 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300309 coda_write(dev, ctx->bit_stream_param,
310 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300311 coda_write(dev, ctx->frm_dis_flg,
312 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300313 coda_write(dev, ctx->frame_mem_ctrl,
314 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300315 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
316 }
317
Philipp Zabel89548442014-07-11 06:36:17 -0300318 if (dev->devtype->product == CODA_960) {
319 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
320 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
321 }
322
Javier Martin186b2502012-07-26 05:53:35 -0300323 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
324
325 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
326 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300327 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
328
Javier Martin186b2502012-07-26 05:53:35 -0300329 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
330}
331
332static int coda_command_sync(struct coda_ctx *ctx, int cmd)
333{
334 struct coda_dev *dev = ctx->dev;
335
336 coda_command_async(ctx, cmd);
337 return coda_wait_timeout(dev);
338}
339
340static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
341 enum v4l2_buf_type type)
342{
343 switch (type) {
344 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
345 return &(ctx->q_data[V4L2_M2M_SRC]);
346 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
347 return &(ctx->q_data[V4L2_M2M_DST]);
348 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300349 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300350 }
Javier Martin186b2502012-07-26 05:53:35 -0300351}
352
353/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300354 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300355 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300356static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300357 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300358 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300359 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300360 },
361 {
362 .name = "YUV 4:2:0 Planar, YCrCb",
363 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300364 },
365 {
366 .name = "H264 Encoded Stream",
367 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300368 },
369 {
370 .name = "MPEG4 Encoded Stream",
371 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300372 },
373};
374
Philipp Zabelb96904e2013-05-23 10:42:58 -0300375#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
376 { mode, src_fourcc, dst_fourcc, max_w, max_h }
377
378/*
379 * Arrays of codecs supported by each given version of Coda:
380 * i.MX27 -> codadx6
381 * i.MX5x -> coda7
382 * i.MX6 -> coda960
383 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
384 */
385static struct coda_codec codadx6_codecs[] = {
386 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
387 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300388};
389
Philipp Zabelb96904e2013-05-23 10:42:58 -0300390static struct coda_codec coda7_codecs[] = {
391 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
392 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300393 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
394 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300395};
396
Philipp Zabel89548442014-07-11 06:36:17 -0300397static struct coda_codec coda9_codecs[] = {
398 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
399 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
400 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
401 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
402};
403
Philipp Zabelb96904e2013-05-23 10:42:58 -0300404static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300405{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300406 switch (fourcc) {
407 case V4L2_PIX_FMT_YUV420:
408 case V4L2_PIX_FMT_YVU420:
409 return true;
410 default:
411 return false;
412 }
413}
Javier Martin186b2502012-07-26 05:53:35 -0300414
Philipp Zabelb96904e2013-05-23 10:42:58 -0300415/*
416 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
417 * tables.
418 */
419static u32 coda_format_normalize_yuv(u32 fourcc)
420{
421 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
422}
423
424static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
425 int dst_fourcc)
426{
427 struct coda_codec *codecs = dev->devtype->codecs;
428 int num_codecs = dev->devtype->num_codecs;
429 int k;
430
431 src_fourcc = coda_format_normalize_yuv(src_fourcc);
432 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
433 if (src_fourcc == dst_fourcc)
434 return NULL;
435
436 for (k = 0; k < num_codecs; k++) {
437 if (codecs[k].src_fourcc == src_fourcc &&
438 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300439 break;
440 }
441
Philipp Zabelb96904e2013-05-23 10:42:58 -0300442 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300443 return NULL;
444
Philipp Zabelb96904e2013-05-23 10:42:58 -0300445 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300446}
447
Philipp Zabel57625592013-09-30 10:34:51 -0300448static void coda_get_max_dimensions(struct coda_dev *dev,
449 struct coda_codec *codec,
450 int *max_w, int *max_h)
451{
452 struct coda_codec *codecs = dev->devtype->codecs;
453 int num_codecs = dev->devtype->num_codecs;
454 unsigned int w, h;
455 int k;
456
457 if (codec) {
458 w = codec->max_w;
459 h = codec->max_h;
460 } else {
461 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
462 w = max(w, codecs[k].max_w);
463 h = max(h, codecs[k].max_h);
464 }
465 }
466
467 if (max_w)
468 *max_w = w;
469 if (max_h)
470 *max_h = h;
471}
472
Philipp Zabel927933f2013-09-30 10:34:48 -0300473static char *coda_product_name(int product)
474{
475 static char buf[9];
476
477 switch (product) {
478 case CODA_DX6:
479 return "CodaDx6";
480 case CODA_7541:
481 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300482 case CODA_960:
483 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300484 default:
485 snprintf(buf, sizeof(buf), "(0x%04x)", product);
486 return buf;
487 }
488}
489
Javier Martin186b2502012-07-26 05:53:35 -0300490/*
491 * V4L2 ioctl() operations.
492 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300493static int coda_querycap(struct file *file, void *priv,
494 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300495{
Philipp Zabel927933f2013-09-30 10:34:48 -0300496 struct coda_ctx *ctx = fh_to_ctx(priv);
497
Javier Martin186b2502012-07-26 05:53:35 -0300498 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300499 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
500 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300501 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300502 /*
503 * This is only a mem-to-mem video device. The capture and output
504 * device capability flags are left only for backward compatibility
505 * and are scheduled for removal.
506 */
507 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
508 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300509 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
510
511 return 0;
512}
513
514static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300515 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300516{
517 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300518 struct coda_codec *codecs = ctx->dev->devtype->codecs;
519 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300520 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300521 int num_codecs = ctx->dev->devtype->num_codecs;
522 int num_formats = ARRAY_SIZE(coda_formats);
523 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300524
525 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300526 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300527 if (coda_format_is_yuv(formats[i].fourcc) &&
528 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300529 if (num == f->index)
530 break;
531 ++num;
532 continue;
533 }
534 /* Compressed formats may be supported, check the codec list */
535 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300536 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300537 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300538 formats[i].fourcc == codecs[k].dst_fourcc &&
539 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300540 break;
541 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
542 formats[i].fourcc == codecs[k].src_fourcc)
543 break;
544 }
545 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300546 if (num == f->index)
547 break;
548 ++num;
549 }
550 }
551
552 if (i < num_formats) {
553 fmt = &formats[i];
554 strlcpy(f->description, fmt->name, sizeof(f->description));
555 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300556 if (!coda_format_is_yuv(fmt->fourcc))
557 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300558 return 0;
559 }
560
561 /* Format not found */
562 return -EINVAL;
563}
564
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300565static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
566 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300567{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300568 struct coda_ctx *ctx = fh_to_ctx(priv);
569 struct vb2_queue *src_vq;
570 struct coda_q_data *q_data_src;
571
572 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300573 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300574 if (vb2_is_streaming(src_vq)) {
575 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
576
577 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
578 q_data_src->fourcc);
579 }
580
581 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300582}
583
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300584static int coda_enum_fmt_vid_out(struct file *file, void *priv,
585 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300586{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300587 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300588}
589
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300590static int coda_g_fmt(struct file *file, void *priv,
591 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300592{
Javier Martin186b2502012-07-26 05:53:35 -0300593 struct coda_q_data *q_data;
594 struct coda_ctx *ctx = fh_to_ctx(priv);
595
Javier Martin186b2502012-07-26 05:53:35 -0300596 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300597 if (!q_data)
598 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300599
600 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300601 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300602 f->fmt.pix.width = q_data->width;
603 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300604 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300605 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
606 else /* encoded formats h.264/mpeg4 */
607 f->fmt.pix.bytesperline = 0;
608
609 f->fmt.pix.sizeimage = q_data->sizeimage;
610 f->fmt.pix.colorspace = ctx->colorspace;
611
612 return 0;
613}
614
Philipp Zabel57625592013-09-30 10:34:51 -0300615static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
616 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300617{
Philipp Zabel57625592013-09-30 10:34:51 -0300618 struct coda_dev *dev = ctx->dev;
619 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300620 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300621 enum v4l2_field field;
622
623 field = f->fmt.pix.field;
624 if (field == V4L2_FIELD_ANY)
625 field = V4L2_FIELD_NONE;
626 else if (V4L2_FIELD_NONE != field)
627 return -EINVAL;
628
629 /* V4L2 specification suggests the driver corrects the format struct
630 * if any of the dimensions is unsupported */
631 f->fmt.pix.field = field;
632
Philipp Zabel57625592013-09-30 10:34:51 -0300633 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
634 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
635 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
636 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300637
Philipp Zabel57625592013-09-30 10:34:51 -0300638 switch (f->fmt.pix.pixelformat) {
639 case V4L2_PIX_FMT_YUV420:
640 case V4L2_PIX_FMT_YVU420:
641 case V4L2_PIX_FMT_H264:
642 case V4L2_PIX_FMT_MPEG4:
643 case V4L2_PIX_FMT_JPEG:
644 break;
645 default:
646 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300647 if (!q_data)
648 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300649 f->fmt.pix.pixelformat = q_data->fourcc;
650 }
651
652 switch (f->fmt.pix.pixelformat) {
653 case V4L2_PIX_FMT_YUV420:
654 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300655 /* Frame stride must be multiple of 8 */
656 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
657 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300658 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300659 break;
660 case V4L2_PIX_FMT_H264:
661 case V4L2_PIX_FMT_MPEG4:
662 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300663 f->fmt.pix.bytesperline = 0;
664 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300665 break;
666 default:
667 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300668 }
669
670 return 0;
671}
672
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300673static int coda_try_fmt_vid_cap(struct file *file, void *priv,
674 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300675{
Javier Martin186b2502012-07-26 05:53:35 -0300676 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300677 struct coda_codec *codec;
678 struct vb2_queue *src_vq;
679 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300680
Philipp Zabel918c66f2013-06-27 06:59:01 -0300681 /*
682 * If the source format is already fixed, try to find a codec that
683 * converts to the given destination format
684 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300685 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300686 if (vb2_is_streaming(src_vq)) {
687 struct coda_q_data *q_data_src;
688
689 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
690 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
691 f->fmt.pix.pixelformat);
692 if (!codec)
693 return -EINVAL;
694 } else {
695 /* Otherwise determine codec by encoded format, if possible */
696 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
697 f->fmt.pix.pixelformat);
698 }
Javier Martin186b2502012-07-26 05:53:35 -0300699
700 f->fmt.pix.colorspace = ctx->colorspace;
701
Philipp Zabel57625592013-09-30 10:34:51 -0300702 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300703 if (ret < 0)
704 return ret;
705
706 /* The h.264 decoder only returns complete 16x16 macroblocks */
707 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
708 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
709 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
710 f->fmt.pix.bytesperline = f->fmt.pix.width;
711 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
712 f->fmt.pix.height * 3 / 2;
713 }
714
715 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300716}
717
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300718static int coda_try_fmt_vid_out(struct file *file, void *priv,
719 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300720{
721 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300722 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300723
Philipp Zabelb96904e2013-05-23 10:42:58 -0300724 /* Determine codec by encoded format, returns NULL if raw or invalid */
725 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
726 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300727
728 if (!f->fmt.pix.colorspace)
729 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
730
Philipp Zabel57625592013-09-30 10:34:51 -0300731 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300732}
733
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300734static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300735{
736 struct coda_q_data *q_data;
737 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300738
Philipp Zabel14604e32014-07-11 06:36:22 -0300739 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300740 if (!vq)
741 return -EINVAL;
742
743 q_data = get_q_data(ctx, f->type);
744 if (!q_data)
745 return -EINVAL;
746
747 if (vb2_is_busy(vq)) {
748 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
749 return -EBUSY;
750 }
751
Philipp Zabelb96904e2013-05-23 10:42:58 -0300752 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300753 q_data->width = f->fmt.pix.width;
754 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300755 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300756 q_data->rect.left = 0;
757 q_data->rect.top = 0;
758 q_data->rect.width = f->fmt.pix.width;
759 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300760
761 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
762 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300763 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300764
765 return 0;
766}
767
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300768static int coda_s_fmt_vid_cap(struct file *file, void *priv,
769 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300770{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300771 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300772 int ret;
773
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300774 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300775 if (ret)
776 return ret;
777
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300778 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300779}
780
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300781static int coda_s_fmt_vid_out(struct file *file, void *priv,
782 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300783{
784 struct coda_ctx *ctx = fh_to_ctx(priv);
785 int ret;
786
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300787 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300788 if (ret)
789 return ret;
790
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300791 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300792 if (ret)
793 ctx->colorspace = f->fmt.pix.colorspace;
794
795 return ret;
796}
797
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300798static int coda_qbuf(struct file *file, void *priv,
799 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300800{
801 struct coda_ctx *ctx = fh_to_ctx(priv);
802
Philipp Zabel14604e32014-07-11 06:36:22 -0300803 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300804}
805
Philipp Zabel918c66f2013-06-27 06:59:01 -0300806static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
807 struct v4l2_buffer *buf)
808{
809 struct vb2_queue *src_vq;
810
Philipp Zabel14604e32014-07-11 06:36:22 -0300811 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300812
813 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
814 (buf->sequence == (ctx->qsequence - 1)));
815}
816
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300817static int coda_dqbuf(struct file *file, void *priv,
818 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300819{
820 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300821 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300822
Philipp Zabel14604e32014-07-11 06:36:22 -0300823 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300824
825 /* If this is the last capture buffer, emit an end-of-stream event */
826 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
827 coda_buf_is_end_of_stream(ctx, buf)) {
828 const struct v4l2_event eos_event = {
829 .type = V4L2_EVENT_EOS
830 };
831
832 v4l2_event_queue_fh(&ctx->fh, &eos_event);
833 }
834
835 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300836}
837
Philipp Zabel52c41672014-07-11 06:36:19 -0300838static int coda_g_selection(struct file *file, void *fh,
839 struct v4l2_selection *s)
840{
841 struct coda_ctx *ctx = fh_to_ctx(fh);
842 struct coda_q_data *q_data;
843 struct v4l2_rect r, *rsel;
844
845 q_data = get_q_data(ctx, s->type);
846 if (!q_data)
847 return -EINVAL;
848
849 r.left = 0;
850 r.top = 0;
851 r.width = q_data->width;
852 r.height = q_data->height;
853 rsel = &q_data->rect;
854
855 switch (s->target) {
856 case V4L2_SEL_TGT_CROP_DEFAULT:
857 case V4L2_SEL_TGT_CROP_BOUNDS:
858 rsel = &r;
859 /* fallthrough */
860 case V4L2_SEL_TGT_CROP:
861 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
862 return -EINVAL;
863 break;
864 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
865 case V4L2_SEL_TGT_COMPOSE_PADDED:
866 rsel = &r;
867 /* fallthrough */
868 case V4L2_SEL_TGT_COMPOSE:
869 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
870 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
871 return -EINVAL;
872 break;
873 default:
874 return -EINVAL;
875 }
876
877 s->r = *rsel;
878
879 return 0;
880}
881
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300882static int coda_try_decoder_cmd(struct file *file, void *fh,
883 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300884{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300885 if (dc->cmd != V4L2_DEC_CMD_STOP)
886 return -EINVAL;
887
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300888 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300889 return -EINVAL;
890
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300891 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300892 return -EINVAL;
893
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300894 return 0;
895}
896
897static int coda_decoder_cmd(struct file *file, void *fh,
898 struct v4l2_decoder_cmd *dc)
899{
900 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300901 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300902 int ret;
903
904 ret = coda_try_decoder_cmd(file, fh, dc);
905 if (ret < 0)
906 return ret;
907
908 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300909 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300910 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300911
912 /* Set the strem-end flag on this context */
913 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
914
Philipp Zabel89548442014-07-11 06:36:17 -0300915 if ((dev->devtype->product == CODA_960) &&
916 coda_isbusy(dev) &&
917 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
918 /* If this context is currently running, update the hardware flag */
919 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
920 }
Michael Olbrichf3497da2014-07-11 06:36:30 -0300921 ctx->prescan_failed = false;
922 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300923
Philipp Zabel918c66f2013-06-27 06:59:01 -0300924 return 0;
925}
926
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300927static int coda_subscribe_event(struct v4l2_fh *fh,
928 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300929{
930 switch (sub->type) {
931 case V4L2_EVENT_EOS:
932 return v4l2_event_subscribe(fh, sub, 0, NULL);
933 default:
934 return v4l2_ctrl_subscribe_event(fh, sub);
935 }
Javier Martin186b2502012-07-26 05:53:35 -0300936}
937
938static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300939 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300940
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300941 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
942 .vidioc_g_fmt_vid_cap = coda_g_fmt,
943 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
944 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300945
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300946 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
947 .vidioc_g_fmt_vid_out = coda_g_fmt,
948 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
949 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300950
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300951 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
952 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300953
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300954 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300955 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300956 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300957 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300958
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300959 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
960 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300961
Philipp Zabel52c41672014-07-11 06:36:19 -0300962 .vidioc_g_selection = coda_g_selection,
963
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300964 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300965 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300966
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300967 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300968 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300969};
970
Philipp Zabel918c66f2013-06-27 06:59:01 -0300971static int coda_start_decoding(struct coda_ctx *ctx);
972
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300973static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
974{
975 return kfifo_len(&ctx->bitstream_fifo);
976}
977
978static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
979{
980 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
981 struct coda_dev *dev = ctx->dev;
982 u32 rd_ptr;
983
984 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
985 kfifo->out = (kfifo->in & ~kfifo->mask) |
986 (rd_ptr - ctx->bitstream.paddr);
987 if (kfifo->out > kfifo->in)
988 kfifo->out -= kfifo->mask + 1;
989}
990
991static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
992{
993 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
994 struct coda_dev *dev = ctx->dev;
995 u32 rd_ptr, wr_ptr;
996
997 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
998 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
999 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1000 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1001}
1002
1003static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1004{
1005 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1006 struct coda_dev *dev = ctx->dev;
1007 u32 wr_ptr;
1008
1009 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1010 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1011}
1012
1013static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1014{
1015 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1016 u32 n;
1017
1018 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1019 if (n < src_size)
1020 return -ENOSPC;
1021
1022 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1023 ctx->bitstream.size, DMA_TO_DEVICE);
1024
Philipp Zabel846ced92014-07-11 06:36:31 -03001025 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001026
1027 return 0;
1028}
1029
1030static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1031 struct vb2_buffer *src_buf)
1032{
1033 int ret;
1034
1035 if (coda_get_bitstream_payload(ctx) +
1036 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1037 return false;
1038
1039 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1040 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1041 return true;
1042 }
1043
1044 ret = coda_bitstream_queue(ctx, src_buf);
1045 if (ret < 0) {
1046 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1047 return false;
1048 }
1049 /* Sync read pointer to device */
1050 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1051 coda_kfifo_sync_to_device_write(ctx);
1052
Philipp Zabel918c66f2013-06-27 06:59:01 -03001053 ctx->prescan_failed = false;
1054
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001055 return true;
1056}
1057
1058static void coda_fill_bitstream(struct coda_ctx *ctx)
1059{
1060 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001061 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001062
Philipp Zabel14604e32014-07-11 06:36:22 -03001063 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1064 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001065
1066 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001067 /*
1068 * Source buffer is queued in the bitstream ringbuffer;
1069 * queue the timestamp and mark source buffer as done
1070 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001071 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001072
1073 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1074 if (ts) {
1075 ts->sequence = src_buf->v4l2_buf.sequence;
1076 ts->timecode = src_buf->v4l2_buf.timecode;
1077 ts->timestamp = src_buf->v4l2_buf.timestamp;
1078 list_add_tail(&ts->list, &ctx->timestamp_list);
1079 }
1080
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001081 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1082 } else {
1083 break;
1084 }
1085 }
1086}
1087
Philipp Zabel89548442014-07-11 06:36:17 -03001088static void coda_set_gdi_regs(struct coda_ctx *ctx)
1089{
1090 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1091 struct coda_dev *dev = ctx->dev;
1092 int i;
1093
1094 for (i = 0; i < 16; i++)
1095 coda_write(dev, tiled_map->xy2ca_map[i],
1096 CODA9_GDI_XY2_CAS_0 + 4 * i);
1097 for (i = 0; i < 4; i++)
1098 coda_write(dev, tiled_map->xy2ba_map[i],
1099 CODA9_GDI_XY2_BA_0 + 4 * i);
1100 for (i = 0; i < 16; i++)
1101 coda_write(dev, tiled_map->xy2ra_map[i],
1102 CODA9_GDI_XY2_RAS_0 + 4 * i);
1103 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1104 for (i = 0; i < 32; i++)
1105 coda_write(dev, tiled_map->rbc2axi_map[i],
1106 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1107}
1108
Javier Martin186b2502012-07-26 05:53:35 -03001109/*
1110 * Mem-to-mem operations.
1111 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001112static int coda_prepare_decode(struct coda_ctx *ctx)
1113{
1114 struct vb2_buffer *dst_buf;
1115 struct coda_dev *dev = ctx->dev;
1116 struct coda_q_data *q_data_dst;
1117 u32 stridey, height;
1118 u32 picture_y, picture_cb, picture_cr;
1119
Philipp Zabel14604e32014-07-11 06:36:22 -03001120 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001121 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1122
1123 if (ctx->params.rot_mode & CODA_ROT_90) {
1124 stridey = q_data_dst->height;
1125 height = q_data_dst->width;
1126 } else {
1127 stridey = q_data_dst->width;
1128 height = q_data_dst->height;
1129 }
1130
1131 /* Try to copy source buffer contents into the bitstream ringbuffer */
1132 mutex_lock(&ctx->bitstream_mutex);
1133 coda_fill_bitstream(ctx);
1134 mutex_unlock(&ctx->bitstream_mutex);
1135
1136 if (coda_get_bitstream_payload(ctx) < 512 &&
1137 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1138 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1139 "bitstream payload: %d, skipping\n",
1140 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001141 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001142 return -EAGAIN;
1143 }
1144
1145 /* Run coda_start_decoding (again) if not yet initialized */
1146 if (!ctx->initialized) {
1147 int ret = coda_start_decoding(ctx);
1148 if (ret < 0) {
1149 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001150 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001151 return -EAGAIN;
1152 } else {
1153 ctx->initialized = 1;
1154 }
1155 }
1156
Philipp Zabel89548442014-07-11 06:36:17 -03001157 if (dev->devtype->product == CODA_960)
1158 coda_set_gdi_regs(ctx);
1159
Philipp Zabel918c66f2013-06-27 06:59:01 -03001160 /* Set rotator output */
1161 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1162 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1163 /* Switch Cr and Cb for YVU420 format */
1164 picture_cr = picture_y + stridey * height;
1165 picture_cb = picture_cr + stridey / 2 * height / 2;
1166 } else {
1167 picture_cb = picture_y + stridey * height;
1168 picture_cr = picture_cb + stridey / 2 * height / 2;
1169 }
Philipp Zabel89548442014-07-11 06:36:17 -03001170
1171 if (dev->devtype->product == CODA_960) {
1172 /*
1173 * The CODA960 seems to have an internal list of buffers with
1174 * 64 entries that includes the registered frame buffers as
1175 * well as the rotator buffer output.
1176 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1177 */
1178 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1179 CODA9_CMD_DEC_PIC_ROT_INDEX);
1180 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1181 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1182 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1183 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1184 } else {
1185 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1186 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1187 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1188 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1189 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001190 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1191 CODA_CMD_DEC_PIC_ROT_MODE);
1192
1193 switch (dev->devtype->product) {
1194 case CODA_DX6:
1195 /* TBD */
1196 case CODA_7541:
1197 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1198 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001199 case CODA_960:
1200 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1201 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001202 }
1203
1204 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1205
1206 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1207 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1208
1209 return 0;
1210}
1211
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001212static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001213{
Javier Martin186b2502012-07-26 05:53:35 -03001214 struct coda_q_data *q_data_src, *q_data_dst;
1215 struct vb2_buffer *src_buf, *dst_buf;
1216 struct coda_dev *dev = ctx->dev;
1217 int force_ipicture;
1218 int quant_param = 0;
1219 u32 picture_y, picture_cb, picture_cr;
1220 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1221 u32 dst_fourcc;
1222
Philipp Zabel14604e32014-07-11 06:36:22 -03001223 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1224 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001225 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1226 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001227 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001228
Philipp Zabel918c66f2013-06-27 06:59:01 -03001229 src_buf->v4l2_buf.sequence = ctx->osequence;
1230 dst_buf->v4l2_buf.sequence = ctx->osequence;
1231 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001232
1233 /*
1234 * Workaround coda firmware BUG that only marks the first
1235 * frame as IDR. This is a problem for some decoders that can't
1236 * recover when a frame is lost.
1237 */
1238 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1239 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1240 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1241 } else {
1242 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1243 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1244 }
1245
Philipp Zabel89548442014-07-11 06:36:17 -03001246 if (dev->devtype->product == CODA_960)
1247 coda_set_gdi_regs(ctx);
1248
Javier Martin186b2502012-07-26 05:53:35 -03001249 /*
1250 * Copy headers at the beginning of the first frame for H.264 only.
1251 * In MPEG4 they are already copied by the coda.
1252 */
1253 if (src_buf->v4l2_buf.sequence == 0) {
1254 pic_stream_buffer_addr =
1255 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1256 ctx->vpu_header_size[0] +
1257 ctx->vpu_header_size[1] +
1258 ctx->vpu_header_size[2];
1259 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1260 ctx->vpu_header_size[0] -
1261 ctx->vpu_header_size[1] -
1262 ctx->vpu_header_size[2];
1263 memcpy(vb2_plane_vaddr(dst_buf, 0),
1264 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1265 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1266 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1267 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1268 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1269 ctx->vpu_header_size[2]);
1270 } else {
1271 pic_stream_buffer_addr =
1272 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1273 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1274 }
1275
1276 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1277 force_ipicture = 1;
1278 switch (dst_fourcc) {
1279 case V4L2_PIX_FMT_H264:
1280 quant_param = ctx->params.h264_intra_qp;
1281 break;
1282 case V4L2_PIX_FMT_MPEG4:
1283 quant_param = ctx->params.mpeg4_intra_qp;
1284 break;
1285 default:
1286 v4l2_warn(&ctx->dev->v4l2_dev,
1287 "cannot set intra qp, fmt not supported\n");
1288 break;
1289 }
1290 } else {
1291 force_ipicture = 0;
1292 switch (dst_fourcc) {
1293 case V4L2_PIX_FMT_H264:
1294 quant_param = ctx->params.h264_inter_qp;
1295 break;
1296 case V4L2_PIX_FMT_MPEG4:
1297 quant_param = ctx->params.mpeg4_inter_qp;
1298 break;
1299 default:
1300 v4l2_warn(&ctx->dev->v4l2_dev,
1301 "cannot set inter qp, fmt not supported\n");
1302 break;
1303 }
1304 }
1305
1306 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001307 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001308 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1309
1310
1311 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001312 switch (q_data_src->fourcc) {
1313 case V4L2_PIX_FMT_YVU420:
1314 /* Switch Cb and Cr for YVU420 format */
1315 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1316 picture_cb = picture_cr + q_data_src->width / 2 *
1317 q_data_src->height / 2;
1318 break;
1319 case V4L2_PIX_FMT_YUV420:
1320 default:
1321 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1322 picture_cr = picture_cb + q_data_src->width / 2 *
1323 q_data_src->height / 2;
1324 break;
1325 }
Javier Martin186b2502012-07-26 05:53:35 -03001326
Philipp Zabel89548442014-07-11 06:36:17 -03001327 if (dev->devtype->product == CODA_960) {
1328 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1329 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1330 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1331
1332 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1333 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1334 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1335 } else {
1336 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1337 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1338 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1339 }
Javier Martin186b2502012-07-26 05:53:35 -03001340 coda_write(dev, force_ipicture << 1 & 0x2,
1341 CODA_CMD_ENC_PIC_OPTION);
1342
1343 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1344 coda_write(dev, pic_stream_buffer_size / 1024,
1345 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001346
1347 if (!ctx->streamon_out) {
1348 /* After streamoff on the output side, set the stream end flag */
1349 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1350 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1351 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001352}
1353
1354static void coda_device_run(void *m2m_priv)
1355{
1356 struct coda_ctx *ctx = m2m_priv;
1357 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001358
1359 queue_work(dev->workqueue, &ctx->pic_run_work);
1360}
1361
1362static void coda_free_framebuffers(struct coda_ctx *ctx);
1363static void coda_free_context_buffers(struct coda_ctx *ctx);
1364
1365static void coda_seq_end_work(struct work_struct *work)
1366{
1367 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1368 struct coda_dev *dev = ctx->dev;
1369
1370 mutex_lock(&ctx->buffer_mutex);
1371 mutex_lock(&dev->coda_mutex);
1372
1373 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1374 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1375 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1376 v4l2_err(&dev->v4l2_dev,
1377 "CODA_COMMAND_SEQ_END failed\n");
1378 }
1379
1380 kfifo_init(&ctx->bitstream_fifo,
1381 ctx->bitstream.vaddr, ctx->bitstream.size);
1382
1383 coda_free_framebuffers(ctx);
1384 coda_free_context_buffers(ctx);
1385
1386 mutex_unlock(&dev->coda_mutex);
1387 mutex_unlock(&ctx->buffer_mutex);
1388}
1389
1390static void coda_finish_decode(struct coda_ctx *ctx);
1391static void coda_finish_encode(struct coda_ctx *ctx);
1392
1393static void coda_pic_run_work(struct work_struct *work)
1394{
1395 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1396 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001397 int ret;
1398
1399 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001400 mutex_lock(&dev->coda_mutex);
1401
Philipp Zabel918c66f2013-06-27 06:59:01 -03001402 if (ctx->inst_type == CODA_INST_DECODER) {
1403 ret = coda_prepare_decode(ctx);
1404 if (ret < 0) {
1405 mutex_unlock(&dev->coda_mutex);
1406 mutex_unlock(&ctx->buffer_mutex);
1407 /* job_finish scheduled by prepare_decode */
1408 return;
1409 }
1410 } else {
1411 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001412 }
1413
Philipp Zabelc2d22512013-06-21 03:55:28 -03001414 if (dev->devtype->product != CODA_DX6)
1415 coda_write(dev, ctx->iram_info.axi_sram_use,
1416 CODA7_REG_BIT_AXI_SRAM_USE);
1417
Philipp Zabel918c66f2013-06-27 06:59:01 -03001418 if (ctx->inst_type == CODA_INST_DECODER)
1419 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001420 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001421
1422 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1423 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1424 } else if (!ctx->aborting) {
1425 if (ctx->inst_type == CODA_INST_DECODER)
1426 coda_finish_decode(ctx);
1427 else
1428 coda_finish_encode(ctx);
1429 }
1430
1431 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1432 queue_work(dev->workqueue, &ctx->seq_end_work);
1433
1434 mutex_unlock(&dev->coda_mutex);
1435 mutex_unlock(&ctx->buffer_mutex);
1436
Philipp Zabel14604e32014-07-11 06:36:22 -03001437 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001438}
1439
1440static int coda_job_ready(void *m2m_priv)
1441{
1442 struct coda_ctx *ctx = m2m_priv;
1443
1444 /*
1445 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001446 * and 1 frame are needed. In the decoder case,
1447 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001448 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001449 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001450 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001451 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1452 "not ready: not enough video buffers.\n");
1453 return 0;
1454 }
1455
Philipp Zabel14604e32014-07-11 06:36:22 -03001456 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001457 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1458 "not ready: not enough video capture buffers.\n");
1459 return 0;
1460 }
1461
Philipp Zabel918c66f2013-06-27 06:59:01 -03001462 if (ctx->prescan_failed ||
1463 ((ctx->inst_type == CODA_INST_DECODER) &&
1464 (coda_get_bitstream_payload(ctx) < 512) &&
1465 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1466 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1467 "%d: not ready: not enough bitstream data.\n",
1468 ctx->idx);
1469 return 0;
1470 }
1471
Philipp Zabel3e748262013-05-23 10:43:01 -03001472 if (ctx->aborting) {
1473 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1474 "not ready: aborting\n");
1475 return 0;
1476 }
1477
Javier Martin186b2502012-07-26 05:53:35 -03001478 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1479 "job ready\n");
1480 return 1;
1481}
1482
1483static void coda_job_abort(void *priv)
1484{
1485 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001486
1487 ctx->aborting = 1;
1488
1489 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1490 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001491}
1492
1493static void coda_lock(void *m2m_priv)
1494{
1495 struct coda_ctx *ctx = m2m_priv;
1496 struct coda_dev *pcdev = ctx->dev;
1497 mutex_lock(&pcdev->dev_mutex);
1498}
1499
1500static void coda_unlock(void *m2m_priv)
1501{
1502 struct coda_ctx *ctx = m2m_priv;
1503 struct coda_dev *pcdev = ctx->dev;
1504 mutex_unlock(&pcdev->dev_mutex);
1505}
1506
1507static struct v4l2_m2m_ops coda_m2m_ops = {
1508 .device_run = coda_device_run,
1509 .job_ready = coda_job_ready,
1510 .job_abort = coda_job_abort,
1511 .lock = coda_lock,
1512 .unlock = coda_unlock,
1513};
1514
Philipp Zabel89548442014-07-11 06:36:17 -03001515static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1516{
1517 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1518 int luma_map, chro_map, i;
1519
1520 memset(tiled_map, 0, sizeof(*tiled_map));
1521
1522 luma_map = 64;
1523 chro_map = 64;
1524 tiled_map->map_type = tiled_map_type;
1525 for (i = 0; i < 16; i++)
1526 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1527 for (i = 0; i < 4; i++)
1528 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1529 for (i = 0; i < 16; i++)
1530 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1531
1532 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1533 tiled_map->xy2rbc_config = 0;
1534 } else {
1535 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1536 tiled_map_type);
1537 return;
1538 }
1539}
1540
Javier Martin186b2502012-07-26 05:53:35 -03001541static void set_default_params(struct coda_ctx *ctx)
1542{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001543 int max_w;
1544 int max_h;
1545
1546 ctx->codec = &ctx->dev->devtype->codecs[0];
1547 max_w = ctx->codec->max_w;
1548 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001549
1550 ctx->params.codec_mode = CODA_MODE_INVALID;
1551 ctx->colorspace = V4L2_COLORSPACE_REC709;
1552 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001553 ctx->aborting = 0;
1554
1555 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001556 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1557 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1558 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1559 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1560 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1561 ctx->q_data[V4L2_M2M_DST].width = max_w;
1562 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001563 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001564 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1565 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1566 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1567 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001568
1569 if (ctx->dev->devtype->product == CODA_960)
1570 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001571}
1572
1573/*
1574 * Queue operations
1575 */
1576static int coda_queue_setup(struct vb2_queue *vq,
1577 const struct v4l2_format *fmt,
1578 unsigned int *nbuffers, unsigned int *nplanes,
1579 unsigned int sizes[], void *alloc_ctxs[])
1580{
1581 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001582 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001583 unsigned int size;
1584
Philipp Zabele34db062012-08-29 08:22:00 -03001585 q_data = get_q_data(ctx, vq->type);
1586 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001587
1588 *nplanes = 1;
1589 sizes[0] = size;
1590
1591 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1592
1593 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1594 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1595
1596 return 0;
1597}
1598
1599static int coda_buf_prepare(struct vb2_buffer *vb)
1600{
1601 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1602 struct coda_q_data *q_data;
1603
1604 q_data = get_q_data(ctx, vb->vb2_queue->type);
1605
1606 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1607 v4l2_warn(&ctx->dev->v4l2_dev,
1608 "%s data will not fit into plane (%lu < %lu)\n",
1609 __func__, vb2_plane_size(vb, 0),
1610 (long)q_data->sizeimage);
1611 return -EINVAL;
1612 }
1613
Javier Martin186b2502012-07-26 05:53:35 -03001614 return 0;
1615}
1616
1617static void coda_buf_queue(struct vb2_buffer *vb)
1618{
1619 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001620 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001621 struct coda_q_data *q_data;
1622
1623 q_data = get_q_data(ctx, vb->vb2_queue->type);
1624
1625 /*
1626 * In the decoder case, immediately try to copy the buffer into the
1627 * bitstream ringbuffer and mark it as ready to be dequeued.
1628 */
1629 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1630 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1631 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001632 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001633 * the stream end
1634 */
Philipp Zabel89548442014-07-11 06:36:17 -03001635 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001636 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001637 if ((dev->devtype->product == CODA_960) &&
1638 coda_isbusy(dev) &&
1639 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1640 /* if this decoder instance is running, set the stream end flag */
1641 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1642 }
1643 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001644 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001645 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001646 coda_fill_bitstream(ctx);
1647 mutex_unlock(&ctx->bitstream_mutex);
1648 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001649 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001650 }
Javier Martin186b2502012-07-26 05:53:35 -03001651}
1652
Philipp Zabel86eda902013-05-23 10:42:57 -03001653static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1654{
1655 struct coda_dev *dev = ctx->dev;
1656 u32 *p = ctx->parabuf.vaddr;
1657
1658 if (dev->devtype->product == CODA_DX6)
1659 p[index] = value;
1660 else
1661 p[index ^ 1] = value;
1662}
1663
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001664static int coda_alloc_aux_buf(struct coda_dev *dev,
1665 struct coda_aux_buf *buf, size_t size)
1666{
1667 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1668 GFP_KERNEL);
1669 if (!buf->vaddr)
1670 return -ENOMEM;
1671
1672 buf->size = size;
1673
1674 return 0;
1675}
1676
1677static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1678 struct coda_aux_buf *buf, size_t size)
1679{
1680 return coda_alloc_aux_buf(ctx->dev, buf, size);
1681}
1682
1683static void coda_free_aux_buf(struct coda_dev *dev,
1684 struct coda_aux_buf *buf)
1685{
1686 if (buf->vaddr) {
1687 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1688 buf->vaddr, buf->paddr);
1689 buf->vaddr = NULL;
1690 buf->size = 0;
1691 }
1692}
1693
1694static void coda_free_framebuffers(struct coda_ctx *ctx)
1695{
1696 int i;
1697
1698 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1699 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1700}
1701
Philipp Zabelec25f682012-07-20 08:54:29 -03001702static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1703{
1704 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001705 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001706 dma_addr_t paddr;
1707 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001708 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001709 int i;
1710
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001711 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1712 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001713 ysize = round_up(q_data->width, 8) * height;
1714
Philipp Zabelec25f682012-07-20 08:54:29 -03001715 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001716 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001717 size_t size;
1718
Philipp Zabelaed14b02014-07-11 06:36:15 -03001719 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001720 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1721 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001722 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001723 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1724 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001725 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001726 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001727 }
1728 }
1729
1730 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001731 for (i = 0; i < ctx->num_internal_frames; i++) {
1732 paddr = ctx->internal_frames[i].paddr;
1733 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1734 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1735 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001736
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001737 /* mvcol buffer for h.264 */
1738 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1739 dev->devtype->product != CODA_DX6)
1740 coda_parabuf_write(ctx, 96 + i,
1741 ctx->internal_frames[i].paddr +
1742 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001743 }
1744
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001745 /* mvcol buffer for mpeg4 */
1746 if ((dev->devtype->product != CODA_DX6) &&
1747 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1748 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1749 ysize + ysize/4 + ysize/4);
1750
Philipp Zabelec25f682012-07-20 08:54:29 -03001751 return 0;
1752}
1753
Javier Martin3f3f5c72012-10-29 05:20:29 -03001754static int coda_h264_padding(int size, char *p)
1755{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001756 int nal_size;
1757 int diff;
1758
Javier Martin832fbb52012-10-29 08:34:59 -03001759 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001760 if (diff == 0)
1761 return 0;
1762
Javier Martin832fbb52012-10-29 08:34:59 -03001763 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001764 memcpy(p, coda_filler_nal, nal_size);
1765
1766 /* Add rbsp stop bit and trailing at the end */
1767 *(p + nal_size - 1) = 0x80;
1768
1769 return nal_size;
1770}
1771
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001772static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1773{
1774 phys_addr_t ret;
1775
1776 size = round_up(size, 1024);
1777 if (size > iram->remaining)
1778 return 0;
1779 iram->remaining -= size;
1780
1781 ret = iram->next_paddr;
1782 iram->next_paddr += size;
1783
1784 return ret;
1785}
1786
Philipp Zabelc2d22512013-06-21 03:55:28 -03001787static void coda_setup_iram(struct coda_ctx *ctx)
1788{
1789 struct coda_iram_info *iram_info = &ctx->iram_info;
1790 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001791 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001792 int dbk_bits;
1793 int bit_bits;
1794 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001795
1796 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001797 iram_info->next_paddr = dev->iram.paddr;
1798 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001799
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001800 switch (dev->devtype->product) {
1801 case CODA_7541:
1802 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1803 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1804 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1805 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001806 case CODA_960:
1807 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1808 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1809 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1810 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001811 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001812 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001813 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001814
1815 if (ctx->inst_type == CODA_INST_ENCODER) {
1816 struct coda_q_data *q_data_src;
1817
1818 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1819 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1820
1821 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001822 if (dev->devtype->product == CODA_7541) {
1823 iram_info->search_ram_size = round_up(mb_width * 16 *
1824 36 + 2048, 1024);
1825 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1826 iram_info->search_ram_size);
1827 if (!iram_info->search_ram_paddr) {
1828 pr_err("IRAM is smaller than the search ram size\n");
1829 goto out;
1830 }
1831 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1832 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001833 }
1834
1835 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001836 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1837 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1838 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001839 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001840 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001841
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001842 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1843 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001844 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001845 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001846
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001847 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1848 if (!iram_info->buf_ip_ac_dc_use)
1849 goto out;
1850 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001851
Philipp Zabel8358e762013-06-21 03:55:32 -03001852 /* OVL and BTP disabled for encoder */
1853 } else if (ctx->inst_type == CODA_INST_DECODER) {
1854 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001855
1856 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1857 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001858
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001859 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1860 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1861 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001862 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001863 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001864
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001865 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1866 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001867 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001868 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001869
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001870 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1871 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001872 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001873 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001874
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001875 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001876 }
1877
1878out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001879 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1880 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1881 "IRAM smaller than needed\n");
1882
1883 if (dev->devtype->product == CODA_7541) {
1884 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001885 if (ctx->inst_type == CODA_INST_DECODER) {
1886 /* fw 1.4.50 */
1887 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1888 CODA7_USE_IP_ENABLE);
1889 } else {
1890 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001891 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1892 CODA7_USE_HOST_DBK_ENABLE |
1893 CODA7_USE_IP_ENABLE |
1894 CODA7_USE_DBK_ENABLE);
1895 }
1896 }
1897}
1898
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001899static void coda_free_context_buffers(struct coda_ctx *ctx)
1900{
1901 struct coda_dev *dev = ctx->dev;
1902
Philipp Zabel918c66f2013-06-27 06:59:01 -03001903 coda_free_aux_buf(dev, &ctx->slicebuf);
1904 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001905 if (dev->devtype->product != CODA_DX6)
1906 coda_free_aux_buf(dev, &ctx->workbuf);
1907}
1908
1909static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1910 struct coda_q_data *q_data)
1911{
1912 struct coda_dev *dev = ctx->dev;
1913 size_t size;
1914 int ret;
1915
1916 switch (dev->devtype->product) {
1917 case CODA_7541:
1918 size = CODA7_WORK_BUF_SIZE;
1919 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001920 case CODA_960:
1921 size = CODA9_WORK_BUF_SIZE;
1922 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1923 size += CODA9_PS_SAVE_SIZE;
1924 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001925 default:
1926 return 0;
1927 }
1928
Philipp Zabel918c66f2013-06-27 06:59:01 -03001929 if (ctx->psbuf.vaddr) {
1930 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1931 return -EBUSY;
1932 }
1933 if (ctx->slicebuf.vaddr) {
1934 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1935 return -EBUSY;
1936 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001937 if (ctx->workbuf.vaddr) {
1938 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1939 ret = -EBUSY;
1940 return -ENOMEM;
1941 }
1942
Philipp Zabel918c66f2013-06-27 06:59:01 -03001943 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1944 /* worst case slice size */
1945 size = (DIV_ROUND_UP(q_data->width, 16) *
1946 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1947 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1948 if (ret < 0) {
1949 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1950 ctx->slicebuf.size);
1951 return ret;
1952 }
1953 }
1954
1955 if (dev->devtype->product == CODA_7541) {
1956 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1957 if (ret < 0) {
1958 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1959 goto err;
1960 }
1961 }
1962
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001963 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1964 if (ret < 0) {
1965 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1966 ctx->workbuf.size);
1967 goto err;
1968 }
1969
1970 return 0;
1971
1972err:
1973 coda_free_context_buffers(ctx);
1974 return ret;
1975}
1976
Philipp Zabel918c66f2013-06-27 06:59:01 -03001977static int coda_start_decoding(struct coda_ctx *ctx)
1978{
1979 struct coda_q_data *q_data_src, *q_data_dst;
1980 u32 bitstream_buf, bitstream_size;
1981 struct coda_dev *dev = ctx->dev;
1982 int width, height;
1983 u32 src_fourcc;
1984 u32 val;
1985 int ret;
1986
1987 /* Start decoding */
1988 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1989 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1990 bitstream_buf = ctx->bitstream.paddr;
1991 bitstream_size = ctx->bitstream.size;
1992 src_fourcc = q_data_src->fourcc;
1993
1994 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1995
1996 /* Update coda bitstream read and write pointers from kfifo */
1997 coda_kfifo_sync_to_device_full(ctx);
1998
1999 ctx->display_idx = -1;
2000 ctx->frm_dis_flg = 0;
2001 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2002
2003 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2004 CODA_REG_BIT_BIT_STREAM_PARAM);
2005
2006 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2007 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2008 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002009 if ((dev->devtype->product == CODA_7541) ||
2010 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002011 val |= CODA_REORDER_ENABLE;
2012 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2013
2014 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002015 if (dev->devtype->product == CODA_960 &&
2016 src_fourcc == V4L2_PIX_FMT_MPEG4)
2017 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2018 else
2019 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002020 if (src_fourcc == V4L2_PIX_FMT_H264) {
2021 if (dev->devtype->product == CODA_7541) {
2022 coda_write(dev, ctx->psbuf.paddr,
2023 CODA_CMD_DEC_SEQ_PS_BB_START);
2024 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2025 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2026 }
Philipp Zabel89548442014-07-11 06:36:17 -03002027 if (dev->devtype->product == CODA_960) {
2028 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2029 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2030 }
2031 }
2032 if (dev->devtype->product != CODA_960) {
2033 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002034 }
2035
2036 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2037 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2038 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2039 return -ETIMEDOUT;
2040 }
2041
2042 /* Update kfifo out pointer from coda bitstream read pointer */
2043 coda_kfifo_sync_from_device(ctx);
2044
2045 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2046
2047 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2048 v4l2_err(&dev->v4l2_dev,
2049 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2050 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2051 return -EAGAIN;
2052 }
2053
2054 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2055 if (dev->devtype->product == CODA_DX6) {
2056 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2057 height = val & CODADX6_PICHEIGHT_MASK;
2058 } else {
2059 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2060 height = val & CODA7_PICHEIGHT_MASK;
2061 }
2062
2063 if (width > q_data_dst->width || height > q_data_dst->height) {
2064 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2065 width, height, q_data_dst->width, q_data_dst->height);
2066 return -EINVAL;
2067 }
2068
2069 width = round_up(width, 16);
2070 height = round_up(height, 16);
2071
2072 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2073 __func__, ctx->idx, width, height);
2074
Philipp Zabela500a932014-07-11 06:36:13 -03002075 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002076 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2077 v4l2_err(&dev->v4l2_dev,
2078 "not enough framebuffers to decode (%d < %d)\n",
2079 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2080 return -EINVAL;
2081 }
2082
Philipp Zabel52c41672014-07-11 06:36:19 -03002083 if (src_fourcc == V4L2_PIX_FMT_H264) {
2084 u32 left_right;
2085 u32 top_bottom;
2086
2087 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2088 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2089
2090 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2091 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2092 q_data_dst->rect.width = width - q_data_dst->rect.left -
2093 (left_right & 0x3ff);
2094 q_data_dst->rect.height = height - q_data_dst->rect.top -
2095 (top_bottom & 0x3ff);
2096 }
2097
Philipp Zabel918c66f2013-06-27 06:59:01 -03002098 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2099 if (ret < 0)
2100 return ret;
2101
2102 /* Tell the decoder how many frame buffers we allocated. */
2103 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2104 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2105
2106 if (dev->devtype->product != CODA_DX6) {
2107 /* Set secondary AXI IRAM */
2108 coda_setup_iram(ctx);
2109
2110 coda_write(dev, ctx->iram_info.buf_bit_use,
2111 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2112 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2113 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2114 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2115 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2116 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2117 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2118 coda_write(dev, ctx->iram_info.buf_ovl_use,
2119 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002120 if (dev->devtype->product == CODA_960)
2121 coda_write(dev, ctx->iram_info.buf_btp_use,
2122 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2123 }
2124
2125 if (dev->devtype->product == CODA_960) {
2126 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2127
2128 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2129 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2130 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2131 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2132 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2133 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002134 }
2135
2136 if (src_fourcc == V4L2_PIX_FMT_H264) {
2137 coda_write(dev, ctx->slicebuf.paddr,
2138 CODA_CMD_SET_FRAME_SLICE_BB_START);
2139 coda_write(dev, ctx->slicebuf.size / 1024,
2140 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2141 }
2142
2143 if (dev->devtype->product == CODA_7541) {
2144 int max_mb_x = 1920 / 16;
2145 int max_mb_y = 1088 / 16;
2146 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002147
Philipp Zabel918c66f2013-06-27 06:59:01 -03002148 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2149 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002150 } else if (dev->devtype->product == CODA_960) {
2151 int max_mb_x = 1920 / 16;
2152 int max_mb_y = 1088 / 16;
2153 int max_mb_num = max_mb_x * max_mb_y;
2154
2155 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2156 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002157 }
2158
2159 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2160 v4l2_err(&ctx->dev->v4l2_dev,
2161 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2162 return -ETIMEDOUT;
2163 }
2164
2165 return 0;
2166}
2167
Philipp Zabeld35167a2013-05-23 10:42:59 -03002168static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2169 int header_code, u8 *header, int *size)
2170{
2171 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002172 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002173 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002174 int i;
2175
2176 if (dev->devtype->product == CODA_960)
2177 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002178
2179 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2180 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002181 bufsize = vb2_plane_size(buf, 0);
2182 if (dev->devtype->product == CODA_960)
2183 bufsize /= 1024;
2184 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002185 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2186 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2187 if (ret < 0) {
2188 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2189 return ret;
2190 }
Philipp Zabel89548442014-07-11 06:36:17 -03002191
2192 if (dev->devtype->product == CODA_960) {
2193 for (i = 63; i > 0; i--)
2194 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2195 break;
2196 *size = i + 1;
2197 } else {
2198 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2199 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2200 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002201 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2202
2203 return 0;
2204}
2205
Philipp Zabel89548442014-07-11 06:36:17 -03002206static int coda_start_encoding(struct coda_ctx *ctx);
2207
Javier Martin186b2502012-07-26 05:53:35 -03002208static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2209{
2210 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2211 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002212 struct coda_dev *dev = ctx->dev;
2213 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002214 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002215 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002216
Philipp Zabelb96904e2013-05-23 10:42:58 -03002217 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002218 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2219 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2220 if (coda_get_bitstream_payload(ctx) < 512)
2221 return -EINVAL;
2222 } else {
2223 if (count < 1)
2224 return -EINVAL;
2225 }
2226
2227 ctx->streamon_out = 1;
2228
Philipp Zabelb96904e2013-05-23 10:42:58 -03002229 if (coda_format_is_yuv(q_data_src->fourcc))
2230 ctx->inst_type = CODA_INST_ENCODER;
2231 else
2232 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002233 } else {
2234 if (count < 1)
2235 return -EINVAL;
2236
2237 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002238 }
Javier Martin186b2502012-07-26 05:53:35 -03002239
Philipp Zabelb96904e2013-05-23 10:42:58 -03002240 /* Don't start the coda unless both queues are on */
2241 if (!(ctx->streamon_out & ctx->streamon_cap))
2242 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002243
Philipp Zabeleb107512013-09-30 10:34:45 -03002244 /* Allow decoder device_run with no new buffers queued */
2245 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002246 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002247
Philipp Zabelb96904e2013-05-23 10:42:58 -03002248 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002249 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002250 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002251
Philipp Zabelb96904e2013-05-23 10:42:58 -03002252 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2253 q_data_dst->fourcc);
2254 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002255 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2256 return -EINVAL;
2257 }
2258
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002259 /* Allocate per-instance buffers */
2260 ret = coda_alloc_context_buffers(ctx, q_data_src);
2261 if (ret < 0)
2262 return ret;
2263
Philipp Zabel918c66f2013-06-27 06:59:01 -03002264 if (ctx->inst_type == CODA_INST_DECODER) {
2265 mutex_lock(&dev->coda_mutex);
2266 ret = coda_start_decoding(ctx);
2267 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002268 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002269 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002270 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002271 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002272 } else {
2273 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002274 }
2275
Philipp Zabel89548442014-07-11 06:36:17 -03002276 ctx->initialized = 1;
2277 return ret;
2278}
2279
2280static int coda_start_encoding(struct coda_ctx *ctx)
2281{
2282 struct coda_dev *dev = ctx->dev;
2283 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2284 struct coda_q_data *q_data_src, *q_data_dst;
2285 u32 bitstream_buf, bitstream_size;
2286 struct vb2_buffer *buf;
2287 int gamma, ret, value;
2288 u32 dst_fourcc;
2289
2290 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2291 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2292 dst_fourcc = q_data_dst->fourcc;
2293
Philipp Zabel14604e32014-07-11 06:36:22 -03002294 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002295 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2296 bitstream_size = q_data_dst->sizeimage;
2297
Javier Martin186b2502012-07-26 05:53:35 -03002298 if (!coda_is_initialized(dev)) {
2299 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2300 return -EFAULT;
2301 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002302
2303 mutex_lock(&dev->coda_mutex);
2304
Javier Martin186b2502012-07-26 05:53:35 -03002305 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002306 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2307 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002308 switch (dev->devtype->product) {
2309 case CODA_DX6:
2310 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2311 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2312 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002313 case CODA_960:
2314 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2315 /* fallthrough */
2316 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002317 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2318 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002319 break;
Javier Martin186b2502012-07-26 05:53:35 -03002320 }
2321
Philipp Zabel89548442014-07-11 06:36:17 -03002322 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2323 value &= ~(1 << 2 | 0x7 << 9);
2324 ctx->frame_mem_ctrl = value;
2325 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2326
Philipp Zabel10436672012-07-02 09:03:55 -03002327 if (dev->devtype->product == CODA_DX6) {
2328 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002329 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002330 }
Javier Martin186b2502012-07-26 05:53:35 -03002331
2332 /* Could set rotation here if needed */
2333 switch (dev->devtype->product) {
2334 case CODA_DX6:
2335 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002336 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002337 break;
2338 default:
2339 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002340 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002341 }
Javier Martin186b2502012-07-26 05:53:35 -03002342 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2343 coda_write(dev, ctx->params.framerate,
2344 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2345
Philipp Zabelb96904e2013-05-23 10:42:58 -03002346 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002347 switch (dst_fourcc) {
2348 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002349 if (dev->devtype->product == CODA_960)
2350 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2351 else
2352 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002353 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2354 break;
2355 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002356 if (dev->devtype->product == CODA_960)
2357 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2358 else
2359 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002360 if (ctx->params.h264_deblk_enabled) {
2361 value = ((ctx->params.h264_deblk_alpha &
2362 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2363 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2364 ((ctx->params.h264_deblk_beta &
2365 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2366 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2367 } else {
2368 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2369 }
2370 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002371 break;
2372 default:
2373 v4l2_err(v4l2_dev,
2374 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002375 ret = -EINVAL;
2376 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002377 }
2378
Philipp Zabelc566c782012-08-08 11:59:38 -03002379 switch (ctx->params.slice_mode) {
2380 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2381 value = 0;
2382 break;
2383 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2384 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2385 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002386 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002387 break;
2388 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2389 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2390 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2391 value |= 1 & CODA_SLICING_MODE_MASK;
2392 break;
2393 }
Javier Martin186b2502012-07-26 05:53:35 -03002394 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002395 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002396 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2397
2398 if (ctx->params.bitrate) {
2399 /* Rate control enabled */
2400 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2401 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002402 if (dev->devtype->product == CODA_960)
2403 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002404 } else {
2405 value = 0;
2406 }
2407 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2408
2409 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002410 coda_write(dev, ctx->params.intra_refresh,
2411 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002412
2413 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2414 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2415
Javier Martin186b2502012-07-26 05:53:35 -03002416
Philipp Zabel89548442014-07-11 06:36:17 -03002417 value = 0;
2418 if (dev->devtype->product == CODA_960)
2419 gamma = CODA9_DEFAULT_GAMMA;
2420 else
2421 gamma = CODA_DEFAULT_GAMMA;
2422 if (gamma > 0) {
2423 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2424 CODA_CMD_ENC_SEQ_RC_GAMMA);
2425 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002426
2427 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2428 coda_write(dev,
2429 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2430 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2431 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2432 }
Philipp Zabel89548442014-07-11 06:36:17 -03002433 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002434 if (ctx->params.h264_max_qp)
2435 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002436 if (CODA_DEFAULT_GAMMA > 0)
2437 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002438 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002439 if (CODA_DEFAULT_GAMMA > 0) {
2440 if (dev->devtype->product == CODA_DX6)
2441 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2442 else
2443 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2444 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002445 if (ctx->params.h264_min_qp)
2446 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2447 if (ctx->params.h264_max_qp)
2448 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002449 }
Javier Martin186b2502012-07-26 05:53:35 -03002450 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2451
Philipp Zabel89548442014-07-11 06:36:17 -03002452 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2453
Philipp Zabelc2d22512013-06-21 03:55:28 -03002454 coda_setup_iram(ctx);
2455
Javier Martin186b2502012-07-26 05:53:35 -03002456 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002457 switch (dev->devtype->product) {
2458 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002459 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002460 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002461 break;
2462 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002463 coda_write(dev, ctx->iram_info.search_ram_paddr,
2464 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2465 coda_write(dev, ctx->iram_info.search_ram_size,
2466 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002467 break;
2468 case CODA_960:
2469 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2470 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002471 }
Javier Martin186b2502012-07-26 05:53:35 -03002472 }
2473
Philipp Zabelfcb62822013-05-23 10:43:00 -03002474 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2475 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002476 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002477 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002478 }
2479
Philipp Zabelfcb62822013-05-23 10:43:00 -03002480 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2481 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2482 ret = -EFAULT;
2483 goto out;
2484 }
Javier Martin186b2502012-07-26 05:53:35 -03002485
Philipp Zabel89548442014-07-11 06:36:17 -03002486 if (dev->devtype->product == CODA_960)
2487 ctx->num_internal_frames = 4;
2488 else
2489 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002490 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002491 if (ret < 0) {
2492 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2493 goto out;
2494 }
Javier Martin186b2502012-07-26 05:53:35 -03002495
Philipp Zabelec25f682012-07-20 08:54:29 -03002496 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002497 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002498 if (dev->devtype->product == CODA_7541)
2499 coda_write(dev, round_up(q_data_src->width, 8),
2500 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002501 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002502 coda_write(dev, ctx->iram_info.buf_bit_use,
2503 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2504 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2505 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2506 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2507 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2508 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2509 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2510 coda_write(dev, ctx->iram_info.buf_ovl_use,
2511 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002512 if (dev->devtype->product == CODA_960) {
2513 coda_write(dev, ctx->iram_info.buf_btp_use,
2514 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2515
2516 /* FIXME */
2517 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2518 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2519 }
Philipp Zabel10436672012-07-02 09:03:55 -03002520 }
Philipp Zabel89548442014-07-11 06:36:17 -03002521
Philipp Zabelfcb62822013-05-23 10:43:00 -03002522 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2523 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002524 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002525 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002526 }
2527
2528 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002529 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002530 switch (dst_fourcc) {
2531 case V4L2_PIX_FMT_H264:
2532 /*
2533 * Get SPS in the first frame and copy it to an
2534 * intermediate buffer.
2535 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002536 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2537 &ctx->vpu_header[0][0],
2538 &ctx->vpu_header_size[0]);
2539 if (ret < 0)
2540 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002541
2542 /*
2543 * Get PPS in the first frame and copy it to an
2544 * intermediate buffer.
2545 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002546 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2547 &ctx->vpu_header[1][0],
2548 &ctx->vpu_header_size[1]);
2549 if (ret < 0)
2550 goto out;
2551
Javier Martin3f3f5c72012-10-29 05:20:29 -03002552 /*
2553 * Length of H.264 headers is variable and thus it might not be
2554 * aligned for the coda to append the encoded frame. In that is
2555 * the case a filler NAL must be added to header 2.
2556 */
2557 ctx->vpu_header_size[2] = coda_h264_padding(
2558 (ctx->vpu_header_size[0] +
2559 ctx->vpu_header_size[1]),
2560 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002561 break;
2562 case V4L2_PIX_FMT_MPEG4:
2563 /*
2564 * Get VOS in the first frame and copy it to an
2565 * intermediate buffer
2566 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002567 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2568 &ctx->vpu_header[0][0],
2569 &ctx->vpu_header_size[0]);
2570 if (ret < 0)
2571 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002572
Philipp Zabeld35167a2013-05-23 10:42:59 -03002573 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2574 &ctx->vpu_header[1][0],
2575 &ctx->vpu_header_size[1]);
2576 if (ret < 0)
2577 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002578
Philipp Zabeld35167a2013-05-23 10:42:59 -03002579 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2580 &ctx->vpu_header[2][0],
2581 &ctx->vpu_header_size[2]);
2582 if (ret < 0)
2583 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002584 break;
2585 default:
2586 /* No more formats need to save headers at the moment */
2587 break;
2588 }
2589
Philipp Zabeld35167a2013-05-23 10:42:59 -03002590out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002591 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002592 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002593}
2594
Hans Verkuile37559b2014-04-17 02:47:21 -03002595static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002596{
2597 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002598 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002599
2600 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002601 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002602 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002603 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002604
Philipp Zabel89548442014-07-11 06:36:17 -03002605 if (ctx->inst_type == CODA_INST_DECODER &&
2606 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2607 /* if this decoder instance is running, set the stream end flag */
2608 if (dev->devtype->product == CODA_960) {
2609 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2610
2611 val |= CODA_BIT_STREAM_END_FLAG;
2612 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2613 ctx->bit_stream_param = val;
2614 }
2615 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002616 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2617
2618 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002619 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002620 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002621 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002622 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002623
2624 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002625 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002626 }
2627
Philipp Zabel918c66f2013-06-27 06:59:01 -03002628 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002629 struct coda_timestamp *ts;
2630
2631 while (!list_empty(&ctx->timestamp_list)) {
2632 ts = list_first_entry(&ctx->timestamp_list,
2633 struct coda_timestamp, list);
2634 list_del(&ts->list);
2635 kfree(ts);
2636 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002637 kfifo_init(&ctx->bitstream_fifo,
2638 ctx->bitstream.vaddr, ctx->bitstream.size);
2639 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002640 }
Javier Martin186b2502012-07-26 05:53:35 -03002641}
2642
2643static struct vb2_ops coda_qops = {
2644 .queue_setup = coda_queue_setup,
2645 .buf_prepare = coda_buf_prepare,
2646 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002647 .start_streaming = coda_start_streaming,
2648 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002649 .wait_prepare = vb2_ops_wait_prepare,
2650 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002651};
2652
2653static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2654{
2655 struct coda_ctx *ctx =
2656 container_of(ctrl->handler, struct coda_ctx, ctrls);
2657
2658 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2659 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2660
2661 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002662 case V4L2_CID_HFLIP:
2663 if (ctrl->val)
2664 ctx->params.rot_mode |= CODA_MIR_HOR;
2665 else
2666 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2667 break;
2668 case V4L2_CID_VFLIP:
2669 if (ctrl->val)
2670 ctx->params.rot_mode |= CODA_MIR_VER;
2671 else
2672 ctx->params.rot_mode &= ~CODA_MIR_VER;
2673 break;
Javier Martin186b2502012-07-26 05:53:35 -03002674 case V4L2_CID_MPEG_VIDEO_BITRATE:
2675 ctx->params.bitrate = ctrl->val / 1000;
2676 break;
2677 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2678 ctx->params.gop_size = ctrl->val;
2679 break;
2680 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2681 ctx->params.h264_intra_qp = ctrl->val;
2682 break;
2683 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2684 ctx->params.h264_inter_qp = ctrl->val;
2685 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002686 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2687 ctx->params.h264_min_qp = ctrl->val;
2688 break;
2689 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2690 ctx->params.h264_max_qp = ctrl->val;
2691 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002692 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2693 ctx->params.h264_deblk_alpha = ctrl->val;
2694 break;
2695 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2696 ctx->params.h264_deblk_beta = ctrl->val;
2697 break;
2698 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2699 ctx->params.h264_deblk_enabled = (ctrl->val ==
2700 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2701 break;
Javier Martin186b2502012-07-26 05:53:35 -03002702 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2703 ctx->params.mpeg4_intra_qp = ctrl->val;
2704 break;
2705 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2706 ctx->params.mpeg4_inter_qp = ctrl->val;
2707 break;
2708 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2709 ctx->params.slice_mode = ctrl->val;
2710 break;
2711 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2712 ctx->params.slice_max_mb = ctrl->val;
2713 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002714 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2715 ctx->params.slice_max_bits = ctrl->val * 8;
2716 break;
Javier Martin186b2502012-07-26 05:53:35 -03002717 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2718 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002719 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2720 ctx->params.intra_refresh = ctrl->val;
2721 break;
Javier Martin186b2502012-07-26 05:53:35 -03002722 default:
2723 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2724 "Invalid control, id=%d, val=%d\n",
2725 ctrl->id, ctrl->val);
2726 return -EINVAL;
2727 }
2728
2729 return 0;
2730}
2731
2732static struct v4l2_ctrl_ops coda_ctrl_ops = {
2733 .s_ctrl = coda_s_ctrl,
2734};
2735
2736static int coda_ctrls_setup(struct coda_ctx *ctx)
2737{
2738 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2739
2740 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002741 V4L2_CID_HFLIP, 0, 1, 1, 0);
2742 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2743 V4L2_CID_VFLIP, 0, 1, 1, 0);
2744 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002745 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2746 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2747 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2748 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002749 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002750 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002751 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002752 if (ctx->dev->devtype->product != CODA_960) {
2753 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2754 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2755 }
2756 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2757 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002758 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002759 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2760 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2761 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2762 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2763 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2764 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2765 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2766 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002767 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2768 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2769 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2770 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2771 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002772 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2773 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002774 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2775 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002776 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2777 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002778 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2779 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2780 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2781 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2782 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002783 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2784 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002785
2786 if (ctx->ctrls.error) {
2787 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2788 ctx->ctrls.error);
2789 return -EINVAL;
2790 }
2791
2792 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2793}
2794
2795static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2796 struct vb2_queue *dst_vq)
2797{
2798 struct coda_ctx *ctx = priv;
2799 int ret;
2800
Javier Martin186b2502012-07-26 05:53:35 -03002801 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002802 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002803 src_vq->drv_priv = ctx;
2804 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2805 src_vq->ops = &coda_qops;
2806 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002807 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002808 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002809
2810 ret = vb2_queue_init(src_vq);
2811 if (ret)
2812 return ret;
2813
Javier Martin186b2502012-07-26 05:53:35 -03002814 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002815 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002816 dst_vq->drv_priv = ctx;
2817 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2818 dst_vq->ops = &coda_qops;
2819 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002820 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002821 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002822
2823 return vb2_queue_init(dst_vq);
2824}
2825
Philipp Zabele11f3e62012-07-25 09:16:58 -03002826static int coda_next_free_instance(struct coda_dev *dev)
2827{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002828 int idx = ffz(dev->instance_mask);
2829
2830 if ((idx < 0) ||
2831 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2832 return -EBUSY;
2833
2834 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002835}
2836
Javier Martin186b2502012-07-26 05:53:35 -03002837static int coda_open(struct file *file)
2838{
2839 struct coda_dev *dev = video_drvdata(file);
2840 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002841 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002842 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002843
Javier Martin186b2502012-07-26 05:53:35 -03002844 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2845 if (!ctx)
2846 return -ENOMEM;
2847
Fabio Estevamf82bc202013-08-21 11:14:16 -03002848 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002849 if (idx < 0) {
2850 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002851 goto err_coda_max;
2852 }
2853 set_bit(idx, &dev->instance_mask);
2854
Philipp Zabel32b6f202014-07-11 06:36:20 -03002855 init_completion(&ctx->completion);
2856 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2857 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002858 v4l2_fh_init(&ctx->fh, video_devdata(file));
2859 file->private_data = &ctx->fh;
2860 v4l2_fh_add(&ctx->fh);
2861 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002862 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002863 switch (dev->devtype->product) {
2864 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002865 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002866 ctx->reg_idx = 0;
2867 break;
2868 default:
2869 ctx->reg_idx = idx;
2870 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002871
Philipp Zabel1e172732014-07-11 06:36:23 -03002872 /* Power up and upload firmware if necessary */
2873 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2874 if (ret < 0) {
2875 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2876 goto err_pm_get;
2877 }
2878
Fabio Estevam79830db2013-08-21 11:14:17 -03002879 ret = clk_prepare_enable(dev->clk_per);
2880 if (ret)
2881 goto err_clk_per;
2882
2883 ret = clk_prepare_enable(dev->clk_ahb);
2884 if (ret)
2885 goto err_clk_ahb;
2886
Javier Martin186b2502012-07-26 05:53:35 -03002887 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002888 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002889 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002890 if (IS_ERR(ctx->fh.m2m_ctx)) {
2891 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002892
2893 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2894 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002895 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002896 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002897
Javier Martin186b2502012-07-26 05:53:35 -03002898 ret = coda_ctrls_setup(ctx);
2899 if (ret) {
2900 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002901 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002902 }
2903
2904 ctx->fh.ctrl_handler = &ctx->ctrls;
2905
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002906 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2907 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002908 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002909 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002910 }
2911
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002912 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2913 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2914 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2915 if (!ctx->bitstream.vaddr) {
2916 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2917 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002918 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002919 }
2920 kfifo_init(&ctx->bitstream_fifo,
2921 ctx->bitstream.vaddr, ctx->bitstream.size);
2922 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002923 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002924 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002925
Javier Martin186b2502012-07-26 05:53:35 -03002926 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002927 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002928 coda_unlock(ctx);
2929
Javier Martin186b2502012-07-26 05:53:35 -03002930 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2931 ctx->idx, ctx);
2932
2933 return 0;
2934
Fabio Estevamf82bc202013-08-21 11:14:16 -03002935err_dma_writecombine:
2936 coda_free_context_buffers(ctx);
2937 if (ctx->dev->devtype->product == CODA_DX6)
2938 coda_free_aux_buf(dev, &ctx->workbuf);
2939 coda_free_aux_buf(dev, &ctx->parabuf);
2940err_dma_alloc:
2941 v4l2_ctrl_handler_free(&ctx->ctrls);
2942err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002943 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002944err_ctx_init:
2945 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002946err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002947 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002948err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002949 pm_runtime_put_sync(&dev->plat_dev->dev);
2950err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002951 v4l2_fh_del(&ctx->fh);
2952 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002953 clear_bit(ctx->idx, &dev->instance_mask);
2954err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002955 kfree(ctx);
2956 return ret;
2957}
2958
2959static int coda_release(struct file *file)
2960{
2961 struct coda_dev *dev = video_drvdata(file);
2962 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2963
2964 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2965 ctx);
2966
Philipp Zabel918c66f2013-06-27 06:59:01 -03002967 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002968 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002969
2970 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002971 if (ctx->initialized) {
2972 queue_work(dev->workqueue, &ctx->seq_end_work);
2973 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002974 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002975
2976 coda_free_framebuffers(ctx);
2977
Javier Martin186b2502012-07-26 05:53:35 -03002978 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002979 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002980 coda_unlock(ctx);
2981
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002982 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2983 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002984 coda_free_context_buffers(ctx);
2985 if (ctx->dev->devtype->product == CODA_DX6)
2986 coda_free_aux_buf(dev, &ctx->workbuf);
2987
2988 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002989 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002990 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002991 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002992 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002993 v4l2_fh_del(&ctx->fh);
2994 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002995 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002996 kfree(ctx);
2997
2998 return 0;
2999}
3000
Javier Martin186b2502012-07-26 05:53:35 -03003001static const struct v4l2_file_operations coda_fops = {
3002 .owner = THIS_MODULE,
3003 .open = coda_open,
3004 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003005 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003006 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003007 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003008};
3009
Philipp Zabel918c66f2013-06-27 06:59:01 -03003010static void coda_finish_decode(struct coda_ctx *ctx)
3011{
3012 struct coda_dev *dev = ctx->dev;
3013 struct coda_q_data *q_data_src;
3014 struct coda_q_data *q_data_dst;
3015 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003016 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003017 int width, height;
3018 int decoded_idx;
3019 int display_idx;
3020 u32 src_fourcc;
3021 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003022 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003023 u32 val;
3024
Philipp Zabel14604e32014-07-11 06:36:22 -03003025 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003026
3027 /* Update kfifo out pointer from coda bitstream read pointer */
3028 coda_kfifo_sync_from_device(ctx);
3029
3030 /*
3031 * in stream-end mode, the read pointer can overshoot the write pointer
3032 * by up to 512 bytes
3033 */
3034 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3035 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3036 kfifo_init(&ctx->bitstream_fifo,
3037 ctx->bitstream.vaddr, ctx->bitstream.size);
3038 }
3039
3040 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3041 src_fourcc = q_data_src->fourcc;
3042
3043 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3044 if (val != 1)
3045 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3046
3047 success = val & 0x1;
3048 if (!success)
3049 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3050
3051 if (src_fourcc == V4L2_PIX_FMT_H264) {
3052 if (val & (1 << 3))
3053 v4l2_err(&dev->v4l2_dev,
3054 "insufficient PS buffer space (%d bytes)\n",
3055 ctx->psbuf.size);
3056 if (val & (1 << 2))
3057 v4l2_err(&dev->v4l2_dev,
3058 "insufficient slice buffer space (%d bytes)\n",
3059 ctx->slicebuf.size);
3060 }
3061
3062 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3063 width = (val >> 16) & 0xffff;
3064 height = val & 0xffff;
3065
3066 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3067
Philipp Zabel52c41672014-07-11 06:36:19 -03003068 /* frame crop information */
3069 if (src_fourcc == V4L2_PIX_FMT_H264) {
3070 u32 left_right;
3071 u32 top_bottom;
3072
3073 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3074 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3075
3076 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3077 /* Keep current crop information */
3078 } else {
3079 struct v4l2_rect *rect = &q_data_dst->rect;
3080
3081 rect->left = left_right >> 16 & 0xffff;
3082 rect->top = top_bottom >> 16 & 0xffff;
3083 rect->width = width - rect->left -
3084 (left_right & 0xffff);
3085 rect->height = height - rect->top -
3086 (top_bottom & 0xffff);
3087 }
3088 } else {
3089 /* no cropping */
3090 }
3091
Philipp Zabel410e5e42014-07-11 06:36:32 -03003092 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3093 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003094 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003095 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003096
3097 if (dev->devtype->product == CODA_7541) {
3098 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3099 if (val == 0) {
3100 /* not enough bitstream data */
3101 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3102 "prescan failed: %d\n", val);
3103 ctx->prescan_failed = true;
3104 return;
3105 }
3106 }
3107
3108 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3109
3110 /*
3111 * The previous display frame was copied out by the rotator,
3112 * now it can be overwritten again
3113 */
3114 if (ctx->display_idx >= 0 &&
3115 ctx->display_idx < ctx->num_internal_frames) {
3116 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3117 coda_write(dev, ctx->frm_dis_flg,
3118 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3119 }
3120
3121 /*
3122 * The index of the last decoded frame, not necessarily in
3123 * display order, and the index of the next display frame.
3124 * The latter could have been decoded in a previous run.
3125 */
3126 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3127 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3128
3129 if (decoded_idx == -1) {
3130 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003131 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3132 ctx->sequence_offset++;
3133 else if (ctx->display_idx < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003134 ctx->prescan_failed = true;
3135 } else if (decoded_idx == -2) {
3136 /* no frame was decoded, we still return the remaining buffers */
3137 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3138 v4l2_err(&dev->v4l2_dev,
3139 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003140 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003141 ts = list_first_entry(&ctx->timestamp_list,
3142 struct coda_timestamp, list);
3143 list_del(&ts->list);
3144 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003145 val -= ctx->sequence_offset;
3146 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003147 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003148 "sequence number mismatch (%d(%d) != %d)\n",
3149 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003150 }
3151 ctx->frame_timestamps[decoded_idx] = *ts;
3152 kfree(ts);
3153
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003154 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3155 if (val == 0)
3156 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3157 else if (val == 1)
3158 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3159 else
3160 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003161
3162 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003163 }
3164
3165 if (display_idx == -1) {
3166 /*
3167 * no more frames to be decoded, but there could still
3168 * be rotator output to dequeue
3169 */
3170 ctx->prescan_failed = true;
3171 } else if (display_idx == -3) {
3172 /* possibly prescan failure */
3173 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3174 v4l2_err(&dev->v4l2_dev,
3175 "presentation frame index out of range: %d\n",
3176 display_idx);
3177 }
3178
3179 /* If a frame was copied out, return it */
3180 if (ctx->display_idx >= 0 &&
3181 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003182 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003183 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3184
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003185 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3186 V4L2_BUF_FLAG_PFRAME |
3187 V4L2_BUF_FLAG_BFRAME);
3188 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003189 ts = &ctx->frame_timestamps[ctx->display_idx];
3190 dst_buf->v4l2_buf.timecode = ts->timecode;
3191 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003192
Philipp Zabel918c66f2013-06-27 06:59:01 -03003193 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3194
Philipp Zabel410e5e42014-07-11 06:36:32 -03003195 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3196 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003197
3198 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3199 "job finished: decoding frame (%d) (%s)\n",
3200 dst_buf->v4l2_buf.sequence,
3201 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3202 "KEYFRAME" : "PFRAME");
3203 } else {
3204 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3205 "job finished: no frame decoded\n");
3206 }
3207
3208 /* The rotator will copy the current display frame next time */
3209 ctx->display_idx = display_idx;
3210}
3211
3212static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003213{
Philipp Zabelec25f682012-07-20 08:54:29 -03003214 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003215 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003216 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003217
Philipp Zabel14604e32014-07-11 06:36:22 -03003218 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3219 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003220
3221 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003222 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003223 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3224
Javier Martin186b2502012-07-26 05:53:35 -03003225 /* Calculate bytesused field */
3226 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003227 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3228 ctx->vpu_header_size[0] +
3229 ctx->vpu_header_size[1] +
3230 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003231 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003232 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003233 }
3234
3235 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3236 wr_ptr - start_ptr);
3237
3238 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3239 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3240
Philipp Zabel51660282013-09-30 10:34:49 -03003241 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003242 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3243 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3244 } else {
3245 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3246 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3247 }
3248
Kamil Debskiccd571c2013-04-24 10:38:24 -03003249 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003250 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3251 dst_buf->v4l2_buf.flags |=
3252 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003253 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3254
Philipp Zabelec25f682012-07-20 08:54:29 -03003255 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003256
Philipp Zabel14604e32014-07-11 06:36:22 -03003257 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003258 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3259
3260 ctx->gopcounter--;
3261 if (ctx->gopcounter < 0)
3262 ctx->gopcounter = ctx->params.gop_size - 1;
3263
3264 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3265 "job finished: encoding frame (%d) (%s)\n",
3266 dst_buf->v4l2_buf.sequence,
3267 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3268 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003269}
3270
3271static irqreturn_t coda_irq_handler(int irq, void *data)
3272{
3273 struct coda_dev *dev = data;
3274 struct coda_ctx *ctx;
3275
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003276 /* read status register to attend the IRQ */
3277 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3278 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3279 CODA_REG_BIT_INT_CLEAR);
3280
3281 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3282 if (ctx == NULL) {
3283 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3284 mutex_unlock(&dev->coda_mutex);
3285 return IRQ_HANDLED;
3286 }
3287
3288 if (ctx->aborting) {
3289 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3290 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003291 }
3292
3293 if (coda_isbusy(ctx->dev)) {
3294 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3295 "coda is still busy!!!!\n");
3296 return IRQ_NONE;
3297 }
3298
Philipp Zabel32b6f202014-07-11 06:36:20 -03003299 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003300
3301 return IRQ_HANDLED;
3302}
3303
3304static u32 coda_supported_firmwares[] = {
3305 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003306 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003307 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003308};
3309
3310static bool coda_firmware_supported(u32 vernum)
3311{
3312 int i;
3313
3314 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3315 if (vernum == coda_supported_firmwares[i])
3316 return true;
3317 return false;
3318}
3319
Philipp Zabel87048bb2012-07-02 07:03:43 -03003320static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003321{
Javier Martin186b2502012-07-26 05:53:35 -03003322 u32 data;
3323 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003324 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003325
Fabio Estevam79830db2013-08-21 11:14:17 -03003326 ret = clk_prepare_enable(dev->clk_per);
3327 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003328 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003329
3330 ret = clk_prepare_enable(dev->clk_ahb);
3331 if (ret)
3332 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003333
Javier Martin186b2502012-07-26 05:53:35 -03003334 /*
3335 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003336 * The 16-bit chars in the code buffer are in memory access
3337 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003338 * Data in this SRAM survives a reboot.
3339 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003340 p = (u16 *)dev->codebuf.vaddr;
3341 if (dev->devtype->product == CODA_DX6) {
3342 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3343 data = CODA_DOWN_ADDRESS_SET(i) |
3344 CODA_DOWN_DATA_SET(p[i ^ 1]);
3345 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3346 }
3347 } else {
3348 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3349 data = CODA_DOWN_ADDRESS_SET(i) |
3350 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3351 3 - (i % 4)]);
3352 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3353 }
Javier Martin186b2502012-07-26 05:53:35 -03003354 }
Javier Martin186b2502012-07-26 05:53:35 -03003355
Philipp Zabel9acf7692013-05-23 10:42:56 -03003356 /* Clear registers */
3357 for (i = 0; i < 64; i++)
3358 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3359
Javier Martin186b2502012-07-26 05:53:35 -03003360 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003361 if (dev->devtype->product == CODA_960 ||
3362 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003363 coda_write(dev, dev->tempbuf.paddr,
3364 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003365 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003366 } else {
3367 coda_write(dev, dev->workbuf.paddr,
3368 CODA_REG_BIT_WORK_BUF_ADDR);
3369 }
Javier Martin186b2502012-07-26 05:53:35 -03003370 coda_write(dev, dev->codebuf.paddr,
3371 CODA_REG_BIT_CODE_BUF_ADDR);
3372 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3373
3374 /* Set default values */
3375 switch (dev->devtype->product) {
3376 case CODA_DX6:
3377 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3378 break;
3379 default:
3380 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3381 }
Philipp Zabel89548442014-07-11 06:36:17 -03003382 if (dev->devtype->product == CODA_960)
3383 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3384 else
3385 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003386
3387 if (dev->devtype->product != CODA_DX6)
3388 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3389
Javier Martin186b2502012-07-26 05:53:35 -03003390 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3391 CODA_REG_BIT_INT_ENABLE);
3392
3393 /* Reset VPU and start processor */
3394 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3395 data |= CODA_REG_RESET_ENABLE;
3396 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3397 udelay(10);
3398 data &= ~CODA_REG_RESET_ENABLE;
3399 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3400 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3401
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003402 clk_disable_unprepare(dev->clk_ahb);
3403 clk_disable_unprepare(dev->clk_per);
3404
3405 return 0;
3406
3407err_clk_ahb:
3408 clk_disable_unprepare(dev->clk_per);
3409err_clk_per:
3410 return ret;
3411}
3412
3413static int coda_check_firmware(struct coda_dev *dev)
3414{
3415 u16 product, major, minor, release;
3416 u32 data;
3417 int ret;
3418
3419 ret = clk_prepare_enable(dev->clk_per);
3420 if (ret)
3421 goto err_clk_per;
3422
3423 ret = clk_prepare_enable(dev->clk_ahb);
3424 if (ret)
3425 goto err_clk_ahb;
3426
Javier Martin186b2502012-07-26 05:53:35 -03003427 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3428 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3429 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3430 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3431 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3432 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003433 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003434 ret = -EIO;
3435 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003436 }
3437
Philipp Zabel89548442014-07-11 06:36:17 -03003438 if (dev->devtype->product == CODA_960) {
3439 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3440 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3441 data);
3442 }
3443
Javier Martin186b2502012-07-26 05:53:35 -03003444 /* Check we are compatible with the loaded firmware */
3445 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3446 product = CODA_FIRMWARE_PRODUCT(data);
3447 major = CODA_FIRMWARE_MAJOR(data);
3448 minor = CODA_FIRMWARE_MINOR(data);
3449 release = CODA_FIRMWARE_RELEASE(data);
3450
3451 clk_disable_unprepare(dev->clk_per);
3452 clk_disable_unprepare(dev->clk_ahb);
3453
3454 if (product != dev->devtype->product) {
3455 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3456 " Version: %u.%u.%u\n",
3457 coda_product_name(dev->devtype->product),
3458 coda_product_name(product), major, minor, release);
3459 return -EINVAL;
3460 }
3461
3462 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3463 coda_product_name(product));
3464
3465 if (coda_firmware_supported(data)) {
3466 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3467 major, minor, release);
3468 } else {
3469 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3470 "%u.%u.%u\n", major, minor, release);
3471 }
3472
3473 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003474
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003475err_run_cmd:
3476 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003477err_clk_ahb:
3478 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003479err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003480 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003481}
3482
3483static void coda_fw_callback(const struct firmware *fw, void *context)
3484{
3485 struct coda_dev *dev = context;
3486 struct platform_device *pdev = dev->plat_dev;
3487 int ret;
3488
3489 if (!fw) {
3490 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3491 return;
3492 }
3493
3494 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003495 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3496 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003497 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3498 return;
3499 }
3500
Philipp Zabel87048bb2012-07-02 07:03:43 -03003501 /* Copy the whole firmware image to the code buffer */
3502 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3503 release_firmware(fw);
3504
Philipp Zabel1e172732014-07-11 06:36:23 -03003505 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3506 /*
3507 * Enabling power temporarily will cause coda_hw_init to be
3508 * called via coda_runtime_resume by the pm domain.
3509 */
3510 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3511 if (ret < 0) {
3512 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3513 ret);
3514 return;
3515 }
3516
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003517 ret = coda_check_firmware(dev);
3518 if (ret < 0)
3519 return;
3520
Philipp Zabel1e172732014-07-11 06:36:23 -03003521 pm_runtime_put_sync(&dev->plat_dev->dev);
3522 } else {
3523 /*
3524 * If runtime pm is disabled or pm_domain is not set,
3525 * initialize once manually.
3526 */
3527 ret = coda_hw_init(dev);
3528 if (ret < 0) {
3529 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3530 return;
3531 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003532
3533 ret = coda_check_firmware(dev);
3534 if (ret < 0)
3535 return;
Javier Martin186b2502012-07-26 05:53:35 -03003536 }
3537
3538 dev->vfd.fops = &coda_fops,
3539 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3540 dev->vfd.release = video_device_release_empty,
3541 dev->vfd.lock = &dev->dev_mutex;
3542 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003543 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003544 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3545 video_set_drvdata(&dev->vfd, dev);
3546
3547 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3548 if (IS_ERR(dev->alloc_ctx)) {
3549 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3550 return;
3551 }
3552
3553 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3554 if (IS_ERR(dev->m2m_dev)) {
3555 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3556 goto rel_ctx;
3557 }
3558
3559 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3560 if (ret) {
3561 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3562 goto rel_m2m;
3563 }
3564 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3565 dev->vfd.num);
3566
3567 return;
3568
3569rel_m2m:
3570 v4l2_m2m_release(dev->m2m_dev);
3571rel_ctx:
3572 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3573}
3574
3575static int coda_firmware_request(struct coda_dev *dev)
3576{
3577 char *fw = dev->devtype->firmware;
3578
3579 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3580 coda_product_name(dev->devtype->product));
3581
3582 return request_firmware_nowait(THIS_MODULE, true,
3583 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3584}
3585
3586enum coda_platform {
3587 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003588 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003589 CODA_IMX6Q,
3590 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003591};
3592
Emil Goodec06d8752012-08-14 17:44:42 -03003593static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003594 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003595 .firmware = "v4l-codadx6-imx27.bin",
3596 .product = CODA_DX6,
3597 .codecs = codadx6_codecs,
3598 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003599 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003600 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003601 .firmware = "v4l-coda7541-imx53.bin",
3602 .product = CODA_7541,
3603 .codecs = coda7_codecs,
3604 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003605 },
Philipp Zabel89548442014-07-11 06:36:17 -03003606 [CODA_IMX6Q] = {
3607 .firmware = "v4l-coda960-imx6q.bin",
3608 .product = CODA_960,
3609 .codecs = coda9_codecs,
3610 .num_codecs = ARRAY_SIZE(coda9_codecs),
3611 },
3612 [CODA_IMX6DL] = {
3613 .firmware = "v4l-coda960-imx6dl.bin",
3614 .product = CODA_960,
3615 .codecs = coda9_codecs,
3616 .num_codecs = ARRAY_SIZE(coda9_codecs),
3617 },
Javier Martin186b2502012-07-26 05:53:35 -03003618};
3619
3620static struct platform_device_id coda_platform_ids[] = {
3621 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003622 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003623 { /* sentinel */ }
3624};
3625MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3626
3627#ifdef CONFIG_OF
3628static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003629 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003630 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003631 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3632 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003633 { /* sentinel */ }
3634};
3635MODULE_DEVICE_TABLE(of, coda_dt_ids);
3636#endif
3637
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003638static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003639{
3640 const struct of_device_id *of_id =
3641 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3642 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003643 struct coda_platform_data *pdata = pdev->dev.platform_data;
3644 struct device_node *np = pdev->dev.of_node;
3645 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003646 struct coda_dev *dev;
3647 struct resource *res;
3648 int ret, irq;
3649
3650 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3651 if (!dev) {
3652 dev_err(&pdev->dev, "Not enough memory for %s\n",
3653 CODA_NAME);
3654 return -ENOMEM;
3655 }
3656
3657 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003658 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003659
3660 dev->plat_dev = pdev;
3661 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3662 if (IS_ERR(dev->clk_per)) {
3663 dev_err(&pdev->dev, "Could not get per clock\n");
3664 return PTR_ERR(dev->clk_per);
3665 }
3666
3667 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3668 if (IS_ERR(dev->clk_ahb)) {
3669 dev_err(&pdev->dev, "Could not get ahb clock\n");
3670 return PTR_ERR(dev->clk_ahb);
3671 }
3672
3673 /* Get memory for physical registers */
3674 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003675 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3676 if (IS_ERR(dev->regs_base))
3677 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003678
3679 /* IRQ */
3680 irq = platform_get_irq(pdev, 0);
3681 if (irq < 0) {
3682 dev_err(&pdev->dev, "failed to get irq resource\n");
3683 return -ENOENT;
3684 }
3685
Philipp Zabel918c66f2013-06-27 06:59:01 -03003686 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003687 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003688 dev_err(&pdev->dev, "failed to request irq\n");
3689 return -ENOENT;
3690 }
3691
Philipp Zabel657eee72013-04-29 16:17:14 -07003692 /* Get IRAM pool from device tree or platform data */
3693 pool = of_get_named_gen_pool(np, "iram", 0);
3694 if (!pool && pdata)
3695 pool = dev_get_gen_pool(pdata->iram_dev);
3696 if (!pool) {
3697 dev_err(&pdev->dev, "iram pool not available\n");
3698 return -ENOMEM;
3699 }
3700 dev->iram_pool = pool;
3701
Javier Martin186b2502012-07-26 05:53:35 -03003702 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3703 if (ret)
3704 return ret;
3705
3706 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003707 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003708
3709 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3710
3711 if (of_id) {
3712 dev->devtype = of_id->data;
3713 } else if (pdev_id) {
3714 dev->devtype = &coda_devdata[pdev_id->driver_data];
3715 } else {
3716 v4l2_device_unregister(&dev->v4l2_dev);
3717 return -EINVAL;
3718 }
3719
3720 /* allocate auxiliary per-device buffers for the BIT processor */
3721 switch (dev->devtype->product) {
3722 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003723 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3724 CODADX6_WORK_BUF_SIZE);
3725 if (ret < 0) {
3726 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3727 v4l2_device_unregister(&dev->v4l2_dev);
3728 return ret;
3729 }
Javier Martin186b2502012-07-26 05:53:35 -03003730 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003731 case CODA_7541:
3732 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3733 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003734 case CODA_960:
3735 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3736 break;
Javier Martin186b2502012-07-26 05:53:35 -03003737 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003738 if (dev->tempbuf.size) {
3739 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3740 dev->tempbuf.size);
3741 if (ret < 0) {
3742 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3743 v4l2_device_unregister(&dev->v4l2_dev);
3744 return ret;
3745 }
Javier Martin186b2502012-07-26 05:53:35 -03003746 }
3747
Philipp Zabel918c66f2013-06-27 06:59:01 -03003748 switch (dev->devtype->product) {
3749 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003750 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003751 break;
3752 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003753 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003754 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003755 case CODA_960:
3756 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003757 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003758 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3759 &dev->iram.paddr);
3760 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003761 dev_err(&pdev->dev, "unable to alloc iram\n");
3762 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003763 }
3764
Philipp Zabel32b6f202014-07-11 06:36:20 -03003765 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3766 if (!dev->workqueue) {
3767 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3768 return -ENOMEM;
3769 }
3770
Javier Martin186b2502012-07-26 05:53:35 -03003771 platform_set_drvdata(pdev, dev);
3772
Philipp Zabel1e172732014-07-11 06:36:23 -03003773 pm_runtime_enable(&pdev->dev);
3774
Javier Martin186b2502012-07-26 05:53:35 -03003775 return coda_firmware_request(dev);
3776}
3777
3778static int coda_remove(struct platform_device *pdev)
3779{
3780 struct coda_dev *dev = platform_get_drvdata(pdev);
3781
3782 video_unregister_device(&dev->vfd);
3783 if (dev->m2m_dev)
3784 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003785 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003786 if (dev->alloc_ctx)
3787 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3788 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003789 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003790 if (dev->iram.vaddr)
3791 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3792 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003793 coda_free_aux_buf(dev, &dev->codebuf);
3794 coda_free_aux_buf(dev, &dev->tempbuf);
3795 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003796 return 0;
3797}
3798
Philipp Zabel1e172732014-07-11 06:36:23 -03003799#ifdef CONFIG_PM_RUNTIME
3800static int coda_runtime_resume(struct device *dev)
3801{
3802 struct coda_dev *cdev = dev_get_drvdata(dev);
3803 int ret = 0;
3804
3805 if (dev->pm_domain) {
3806 ret = coda_hw_init(cdev);
3807 if (ret)
3808 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3809 }
3810
3811 return ret;
3812}
3813#endif
3814
3815static const struct dev_pm_ops coda_pm_ops = {
3816 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3817};
3818
Javier Martin186b2502012-07-26 05:53:35 -03003819static struct platform_driver coda_driver = {
3820 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003821 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003822 .driver = {
3823 .name = CODA_NAME,
3824 .owner = THIS_MODULE,
3825 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003826 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003827 },
3828 .id_table = coda_platform_ids,
3829};
3830
3831module_platform_driver(coda_driver);
3832
3833MODULE_LICENSE("GPL");
3834MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3835MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");