blob: 237937988955da0d7cf76837017e54a4361fda62 [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 Zabelf38f79d52014-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 Zabel84e23652014-07-11 06:36:34 -0300240 bool hold;
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 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300921 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300922 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 Zabel84e23652014-07-11 06:36:34 -03001053 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001054
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");
Philipp Zabel84e23652014-07-11 06:36:34 -03001424
1425 ctx->hold = true;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001426 } else if (!ctx->aborting) {
1427 if (ctx->inst_type == CODA_INST_DECODER)
1428 coda_finish_decode(ctx);
1429 else
1430 coda_finish_encode(ctx);
1431 }
1432
1433 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1434 queue_work(dev->workqueue, &ctx->seq_end_work);
1435
1436 mutex_unlock(&dev->coda_mutex);
1437 mutex_unlock(&ctx->buffer_mutex);
1438
Philipp Zabel14604e32014-07-11 06:36:22 -03001439 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001440}
1441
1442static int coda_job_ready(void *m2m_priv)
1443{
1444 struct coda_ctx *ctx = m2m_priv;
1445
1446 /*
1447 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001448 * and 1 frame are needed. In the decoder case,
1449 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001450 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001451 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001452 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001453 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1454 "not ready: not enough video buffers.\n");
1455 return 0;
1456 }
1457
Philipp Zabel14604e32014-07-11 06:36:22 -03001458 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001459 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1460 "not ready: not enough video capture buffers.\n");
1461 return 0;
1462 }
1463
Philipp Zabel84e23652014-07-11 06:36:34 -03001464 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001465 ((ctx->inst_type == CODA_INST_DECODER) &&
1466 (coda_get_bitstream_payload(ctx) < 512) &&
1467 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1468 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1469 "%d: not ready: not enough bitstream data.\n",
1470 ctx->idx);
1471 return 0;
1472 }
1473
Philipp Zabel3e7482682013-05-23 10:43:01 -03001474 if (ctx->aborting) {
1475 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1476 "not ready: aborting\n");
1477 return 0;
1478 }
1479
Javier Martin186b2502012-07-26 05:53:35 -03001480 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1481 "job ready\n");
1482 return 1;
1483}
1484
1485static void coda_job_abort(void *priv)
1486{
1487 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001488
1489 ctx->aborting = 1;
1490
1491 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1492 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001493}
1494
1495static void coda_lock(void *m2m_priv)
1496{
1497 struct coda_ctx *ctx = m2m_priv;
1498 struct coda_dev *pcdev = ctx->dev;
1499 mutex_lock(&pcdev->dev_mutex);
1500}
1501
1502static void coda_unlock(void *m2m_priv)
1503{
1504 struct coda_ctx *ctx = m2m_priv;
1505 struct coda_dev *pcdev = ctx->dev;
1506 mutex_unlock(&pcdev->dev_mutex);
1507}
1508
1509static struct v4l2_m2m_ops coda_m2m_ops = {
1510 .device_run = coda_device_run,
1511 .job_ready = coda_job_ready,
1512 .job_abort = coda_job_abort,
1513 .lock = coda_lock,
1514 .unlock = coda_unlock,
1515};
1516
Philipp Zabel89548442014-07-11 06:36:17 -03001517static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1518{
1519 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1520 int luma_map, chro_map, i;
1521
1522 memset(tiled_map, 0, sizeof(*tiled_map));
1523
1524 luma_map = 64;
1525 chro_map = 64;
1526 tiled_map->map_type = tiled_map_type;
1527 for (i = 0; i < 16; i++)
1528 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1529 for (i = 0; i < 4; i++)
1530 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1531 for (i = 0; i < 16; i++)
1532 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1533
1534 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1535 tiled_map->xy2rbc_config = 0;
1536 } else {
1537 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1538 tiled_map_type);
1539 return;
1540 }
1541}
1542
Javier Martin186b2502012-07-26 05:53:35 -03001543static void set_default_params(struct coda_ctx *ctx)
1544{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001545 int max_w;
1546 int max_h;
1547
1548 ctx->codec = &ctx->dev->devtype->codecs[0];
1549 max_w = ctx->codec->max_w;
1550 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001551
1552 ctx->params.codec_mode = CODA_MODE_INVALID;
1553 ctx->colorspace = V4L2_COLORSPACE_REC709;
1554 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001555 ctx->aborting = 0;
1556
1557 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001558 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1559 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1560 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1561 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1562 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1563 ctx->q_data[V4L2_M2M_DST].width = max_w;
1564 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001565 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001566 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1567 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1568 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1569 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001570
1571 if (ctx->dev->devtype->product == CODA_960)
1572 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001573}
1574
1575/*
1576 * Queue operations
1577 */
1578static int coda_queue_setup(struct vb2_queue *vq,
1579 const struct v4l2_format *fmt,
1580 unsigned int *nbuffers, unsigned int *nplanes,
1581 unsigned int sizes[], void *alloc_ctxs[])
1582{
1583 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001584 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001585 unsigned int size;
1586
Philipp Zabele34db062012-08-29 08:22:00 -03001587 q_data = get_q_data(ctx, vq->type);
1588 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001589
1590 *nplanes = 1;
1591 sizes[0] = size;
1592
1593 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1594
1595 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1596 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1597
1598 return 0;
1599}
1600
1601static int coda_buf_prepare(struct vb2_buffer *vb)
1602{
1603 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1604 struct coda_q_data *q_data;
1605
1606 q_data = get_q_data(ctx, vb->vb2_queue->type);
1607
1608 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1609 v4l2_warn(&ctx->dev->v4l2_dev,
1610 "%s data will not fit into plane (%lu < %lu)\n",
1611 __func__, vb2_plane_size(vb, 0),
1612 (long)q_data->sizeimage);
1613 return -EINVAL;
1614 }
1615
Javier Martin186b2502012-07-26 05:53:35 -03001616 return 0;
1617}
1618
1619static void coda_buf_queue(struct vb2_buffer *vb)
1620{
1621 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001622 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001623 struct coda_q_data *q_data;
1624
1625 q_data = get_q_data(ctx, vb->vb2_queue->type);
1626
1627 /*
1628 * In the decoder case, immediately try to copy the buffer into the
1629 * bitstream ringbuffer and mark it as ready to be dequeued.
1630 */
1631 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1632 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1633 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001634 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001635 * the stream end
1636 */
Philipp Zabel89548442014-07-11 06:36:17 -03001637 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001638 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001639 if ((dev->devtype->product == CODA_960) &&
1640 coda_isbusy(dev) &&
1641 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1642 /* if this decoder instance is running, set the stream end flag */
1643 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1644 }
1645 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001646 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001647 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001648 coda_fill_bitstream(ctx);
1649 mutex_unlock(&ctx->bitstream_mutex);
1650 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001651 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001652 }
Javier Martin186b2502012-07-26 05:53:35 -03001653}
1654
Philipp Zabel86eda902013-05-23 10:42:57 -03001655static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1656{
1657 struct coda_dev *dev = ctx->dev;
1658 u32 *p = ctx->parabuf.vaddr;
1659
1660 if (dev->devtype->product == CODA_DX6)
1661 p[index] = value;
1662 else
1663 p[index ^ 1] = value;
1664}
1665
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001666static int coda_alloc_aux_buf(struct coda_dev *dev,
1667 struct coda_aux_buf *buf, size_t size)
1668{
1669 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1670 GFP_KERNEL);
1671 if (!buf->vaddr)
1672 return -ENOMEM;
1673
1674 buf->size = size;
1675
1676 return 0;
1677}
1678
1679static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1680 struct coda_aux_buf *buf, size_t size)
1681{
1682 return coda_alloc_aux_buf(ctx->dev, buf, size);
1683}
1684
1685static void coda_free_aux_buf(struct coda_dev *dev,
1686 struct coda_aux_buf *buf)
1687{
1688 if (buf->vaddr) {
1689 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1690 buf->vaddr, buf->paddr);
1691 buf->vaddr = NULL;
1692 buf->size = 0;
1693 }
1694}
1695
1696static void coda_free_framebuffers(struct coda_ctx *ctx)
1697{
1698 int i;
1699
1700 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1701 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1702}
1703
Philipp Zabelec25f682012-07-20 08:54:29 -03001704static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1705{
1706 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001707 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001708 dma_addr_t paddr;
1709 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001710 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001711 int i;
1712
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001713 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1714 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001715 ysize = round_up(q_data->width, 8) * height;
1716
Philipp Zabelec25f682012-07-20 08:54:29 -03001717 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001718 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001719 size_t size;
1720
Philipp Zabelaed14b02014-07-11 06:36:15 -03001721 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001722 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1723 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001724 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001725 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1726 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001727 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001728 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001729 }
1730 }
1731
1732 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001733 for (i = 0; i < ctx->num_internal_frames; i++) {
1734 paddr = ctx->internal_frames[i].paddr;
1735 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1736 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1737 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001738
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001739 /* mvcol buffer for h.264 */
1740 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1741 dev->devtype->product != CODA_DX6)
1742 coda_parabuf_write(ctx, 96 + i,
1743 ctx->internal_frames[i].paddr +
1744 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001745 }
1746
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001747 /* mvcol buffer for mpeg4 */
1748 if ((dev->devtype->product != CODA_DX6) &&
1749 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1750 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1751 ysize + ysize/4 + ysize/4);
1752
Philipp Zabelec25f682012-07-20 08:54:29 -03001753 return 0;
1754}
1755
Javier Martin3f3f5c72012-10-29 05:20:29 -03001756static int coda_h264_padding(int size, char *p)
1757{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001758 int nal_size;
1759 int diff;
1760
Javier Martin832fbb52012-10-29 08:34:59 -03001761 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001762 if (diff == 0)
1763 return 0;
1764
Javier Martin832fbb52012-10-29 08:34:59 -03001765 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001766 memcpy(p, coda_filler_nal, nal_size);
1767
1768 /* Add rbsp stop bit and trailing at the end */
1769 *(p + nal_size - 1) = 0x80;
1770
1771 return nal_size;
1772}
1773
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001774static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1775{
1776 phys_addr_t ret;
1777
1778 size = round_up(size, 1024);
1779 if (size > iram->remaining)
1780 return 0;
1781 iram->remaining -= size;
1782
1783 ret = iram->next_paddr;
1784 iram->next_paddr += size;
1785
1786 return ret;
1787}
1788
Philipp Zabelc2d22512013-06-21 03:55:28 -03001789static void coda_setup_iram(struct coda_ctx *ctx)
1790{
1791 struct coda_iram_info *iram_info = &ctx->iram_info;
1792 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001793 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001794 int dbk_bits;
1795 int bit_bits;
1796 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001797
1798 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001799 iram_info->next_paddr = dev->iram.paddr;
1800 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001801
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001802 switch (dev->devtype->product) {
1803 case CODA_7541:
1804 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1805 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1806 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1807 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001808 case CODA_960:
1809 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1810 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1811 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1812 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001813 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001814 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001815 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001816
1817 if (ctx->inst_type == CODA_INST_ENCODER) {
1818 struct coda_q_data *q_data_src;
1819
1820 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1821 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1822
1823 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001824 if (dev->devtype->product == CODA_7541) {
1825 iram_info->search_ram_size = round_up(mb_width * 16 *
1826 36 + 2048, 1024);
1827 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1828 iram_info->search_ram_size);
1829 if (!iram_info->search_ram_paddr) {
1830 pr_err("IRAM is smaller than the search ram size\n");
1831 goto out;
1832 }
1833 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1834 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001835 }
1836
1837 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001838 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1839 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1840 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001841 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001842 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001843
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001844 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1845 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001846 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001847 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001848
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001849 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1850 if (!iram_info->buf_ip_ac_dc_use)
1851 goto out;
1852 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001853
Philipp Zabel8358e762013-06-21 03:55:32 -03001854 /* OVL and BTP disabled for encoder */
1855 } else if (ctx->inst_type == CODA_INST_DECODER) {
1856 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001857
1858 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1859 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001860
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001861 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1862 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1863 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001864 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001865 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001866
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001867 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1868 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001869 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001870 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001871
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001872 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1873 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001874 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001875 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001876
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001877 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001878 }
1879
1880out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001881 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1882 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1883 "IRAM smaller than needed\n");
1884
1885 if (dev->devtype->product == CODA_7541) {
1886 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001887 if (ctx->inst_type == CODA_INST_DECODER) {
1888 /* fw 1.4.50 */
1889 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1890 CODA7_USE_IP_ENABLE);
1891 } else {
1892 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001893 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1894 CODA7_USE_HOST_DBK_ENABLE |
1895 CODA7_USE_IP_ENABLE |
1896 CODA7_USE_DBK_ENABLE);
1897 }
1898 }
1899}
1900
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001901static void coda_free_context_buffers(struct coda_ctx *ctx)
1902{
1903 struct coda_dev *dev = ctx->dev;
1904
Philipp Zabel918c66f2013-06-27 06:59:01 -03001905 coda_free_aux_buf(dev, &ctx->slicebuf);
1906 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001907 if (dev->devtype->product != CODA_DX6)
1908 coda_free_aux_buf(dev, &ctx->workbuf);
1909}
1910
1911static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1912 struct coda_q_data *q_data)
1913{
1914 struct coda_dev *dev = ctx->dev;
1915 size_t size;
1916 int ret;
1917
1918 switch (dev->devtype->product) {
1919 case CODA_7541:
1920 size = CODA7_WORK_BUF_SIZE;
1921 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001922 case CODA_960:
1923 size = CODA9_WORK_BUF_SIZE;
1924 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1925 size += CODA9_PS_SAVE_SIZE;
1926 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001927 default:
1928 return 0;
1929 }
1930
Philipp Zabel918c66f2013-06-27 06:59:01 -03001931 if (ctx->psbuf.vaddr) {
1932 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1933 return -EBUSY;
1934 }
1935 if (ctx->slicebuf.vaddr) {
1936 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1937 return -EBUSY;
1938 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001939 if (ctx->workbuf.vaddr) {
1940 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1941 ret = -EBUSY;
1942 return -ENOMEM;
1943 }
1944
Philipp Zabel918c66f2013-06-27 06:59:01 -03001945 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1946 /* worst case slice size */
1947 size = (DIV_ROUND_UP(q_data->width, 16) *
1948 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1949 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1950 if (ret < 0) {
1951 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1952 ctx->slicebuf.size);
1953 return ret;
1954 }
1955 }
1956
1957 if (dev->devtype->product == CODA_7541) {
1958 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1959 if (ret < 0) {
1960 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1961 goto err;
1962 }
1963 }
1964
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001965 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1966 if (ret < 0) {
1967 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1968 ctx->workbuf.size);
1969 goto err;
1970 }
1971
1972 return 0;
1973
1974err:
1975 coda_free_context_buffers(ctx);
1976 return ret;
1977}
1978
Philipp Zabel918c66f2013-06-27 06:59:01 -03001979static int coda_start_decoding(struct coda_ctx *ctx)
1980{
1981 struct coda_q_data *q_data_src, *q_data_dst;
1982 u32 bitstream_buf, bitstream_size;
1983 struct coda_dev *dev = ctx->dev;
1984 int width, height;
1985 u32 src_fourcc;
1986 u32 val;
1987 int ret;
1988
1989 /* Start decoding */
1990 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1991 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1992 bitstream_buf = ctx->bitstream.paddr;
1993 bitstream_size = ctx->bitstream.size;
1994 src_fourcc = q_data_src->fourcc;
1995
1996 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1997
1998 /* Update coda bitstream read and write pointers from kfifo */
1999 coda_kfifo_sync_to_device_full(ctx);
2000
2001 ctx->display_idx = -1;
2002 ctx->frm_dis_flg = 0;
2003 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2004
2005 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2006 CODA_REG_BIT_BIT_STREAM_PARAM);
2007
2008 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2009 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2010 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002011 if ((dev->devtype->product == CODA_7541) ||
2012 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002013 val |= CODA_REORDER_ENABLE;
2014 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2015
2016 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002017 if (dev->devtype->product == CODA_960 &&
2018 src_fourcc == V4L2_PIX_FMT_MPEG4)
2019 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2020 else
2021 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002022 if (src_fourcc == V4L2_PIX_FMT_H264) {
2023 if (dev->devtype->product == CODA_7541) {
2024 coda_write(dev, ctx->psbuf.paddr,
2025 CODA_CMD_DEC_SEQ_PS_BB_START);
2026 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2027 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2028 }
Philipp Zabel89548442014-07-11 06:36:17 -03002029 if (dev->devtype->product == CODA_960) {
2030 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2031 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2032 }
2033 }
2034 if (dev->devtype->product != CODA_960) {
2035 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002036 }
2037
2038 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2039 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2040 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2041 return -ETIMEDOUT;
2042 }
2043
2044 /* Update kfifo out pointer from coda bitstream read pointer */
2045 coda_kfifo_sync_from_device(ctx);
2046
2047 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2048
2049 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2050 v4l2_err(&dev->v4l2_dev,
2051 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2052 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2053 return -EAGAIN;
2054 }
2055
2056 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2057 if (dev->devtype->product == CODA_DX6) {
2058 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2059 height = val & CODADX6_PICHEIGHT_MASK;
2060 } else {
2061 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2062 height = val & CODA7_PICHEIGHT_MASK;
2063 }
2064
2065 if (width > q_data_dst->width || height > q_data_dst->height) {
2066 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2067 width, height, q_data_dst->width, q_data_dst->height);
2068 return -EINVAL;
2069 }
2070
2071 width = round_up(width, 16);
2072 height = round_up(height, 16);
2073
2074 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2075 __func__, ctx->idx, width, height);
2076
Philipp Zabela500a932014-07-11 06:36:13 -03002077 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002078 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2079 v4l2_err(&dev->v4l2_dev,
2080 "not enough framebuffers to decode (%d < %d)\n",
2081 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2082 return -EINVAL;
2083 }
2084
Philipp Zabel52c41672014-07-11 06:36:19 -03002085 if (src_fourcc == V4L2_PIX_FMT_H264) {
2086 u32 left_right;
2087 u32 top_bottom;
2088
2089 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2090 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2091
2092 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2093 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2094 q_data_dst->rect.width = width - q_data_dst->rect.left -
2095 (left_right & 0x3ff);
2096 q_data_dst->rect.height = height - q_data_dst->rect.top -
2097 (top_bottom & 0x3ff);
2098 }
2099
Philipp Zabel918c66f2013-06-27 06:59:01 -03002100 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2101 if (ret < 0)
2102 return ret;
2103
2104 /* Tell the decoder how many frame buffers we allocated. */
2105 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2106 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2107
2108 if (dev->devtype->product != CODA_DX6) {
2109 /* Set secondary AXI IRAM */
2110 coda_setup_iram(ctx);
2111
2112 coda_write(dev, ctx->iram_info.buf_bit_use,
2113 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2114 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2115 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2116 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2117 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2118 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2119 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2120 coda_write(dev, ctx->iram_info.buf_ovl_use,
2121 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002122 if (dev->devtype->product == CODA_960)
2123 coda_write(dev, ctx->iram_info.buf_btp_use,
2124 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2125 }
2126
2127 if (dev->devtype->product == CODA_960) {
2128 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2129
2130 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2131 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2132 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2133 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2134 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2135 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002136 }
2137
2138 if (src_fourcc == V4L2_PIX_FMT_H264) {
2139 coda_write(dev, ctx->slicebuf.paddr,
2140 CODA_CMD_SET_FRAME_SLICE_BB_START);
2141 coda_write(dev, ctx->slicebuf.size / 1024,
2142 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2143 }
2144
2145 if (dev->devtype->product == CODA_7541) {
2146 int max_mb_x = 1920 / 16;
2147 int max_mb_y = 1088 / 16;
2148 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002149
Philipp Zabel918c66f2013-06-27 06:59:01 -03002150 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2151 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002152 } else if (dev->devtype->product == CODA_960) {
2153 int max_mb_x = 1920 / 16;
2154 int max_mb_y = 1088 / 16;
2155 int max_mb_num = max_mb_x * max_mb_y;
2156
2157 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2158 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002159 }
2160
2161 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2162 v4l2_err(&ctx->dev->v4l2_dev,
2163 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2164 return -ETIMEDOUT;
2165 }
2166
2167 return 0;
2168}
2169
Philipp Zabeld35167a2013-05-23 10:42:59 -03002170static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2171 int header_code, u8 *header, int *size)
2172{
2173 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002174 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002175 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002176 int i;
2177
2178 if (dev->devtype->product == CODA_960)
2179 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002180
2181 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2182 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002183 bufsize = vb2_plane_size(buf, 0);
2184 if (dev->devtype->product == CODA_960)
2185 bufsize /= 1024;
2186 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002187 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2188 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2189 if (ret < 0) {
2190 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2191 return ret;
2192 }
Philipp Zabel89548442014-07-11 06:36:17 -03002193
2194 if (dev->devtype->product == CODA_960) {
2195 for (i = 63; i > 0; i--)
2196 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2197 break;
2198 *size = i + 1;
2199 } else {
2200 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2201 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2202 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002203 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2204
2205 return 0;
2206}
2207
Philipp Zabel89548442014-07-11 06:36:17 -03002208static int coda_start_encoding(struct coda_ctx *ctx);
2209
Javier Martin186b2502012-07-26 05:53:35 -03002210static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2211{
2212 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2213 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002214 struct coda_dev *dev = ctx->dev;
2215 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002216 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002217 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002218
Philipp Zabelb96904e2013-05-23 10:42:58 -03002219 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002220 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2221 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2222 if (coda_get_bitstream_payload(ctx) < 512)
2223 return -EINVAL;
2224 } else {
2225 if (count < 1)
2226 return -EINVAL;
2227 }
2228
2229 ctx->streamon_out = 1;
2230
Philipp Zabelb96904e2013-05-23 10:42:58 -03002231 if (coda_format_is_yuv(q_data_src->fourcc))
2232 ctx->inst_type = CODA_INST_ENCODER;
2233 else
2234 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002235 } else {
2236 if (count < 1)
2237 return -EINVAL;
2238
2239 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002240 }
Javier Martin186b2502012-07-26 05:53:35 -03002241
Philipp Zabelb96904e2013-05-23 10:42:58 -03002242 /* Don't start the coda unless both queues are on */
2243 if (!(ctx->streamon_out & ctx->streamon_cap))
2244 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002245
Philipp Zabeleb107512013-09-30 10:34:45 -03002246 /* Allow decoder device_run with no new buffers queued */
2247 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002248 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002249
Philipp Zabelb96904e2013-05-23 10:42:58 -03002250 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002251 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002252 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002253
Philipp Zabelb96904e2013-05-23 10:42:58 -03002254 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2255 q_data_dst->fourcc);
2256 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002257 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2258 return -EINVAL;
2259 }
2260
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002261 /* Allocate per-instance buffers */
2262 ret = coda_alloc_context_buffers(ctx, q_data_src);
2263 if (ret < 0)
2264 return ret;
2265
Philipp Zabel918c66f2013-06-27 06:59:01 -03002266 if (ctx->inst_type == CODA_INST_DECODER) {
2267 mutex_lock(&dev->coda_mutex);
2268 ret = coda_start_decoding(ctx);
2269 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002270 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002271 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002272 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002273 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002274 } else {
2275 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002276 }
2277
Philipp Zabel89548442014-07-11 06:36:17 -03002278 ctx->initialized = 1;
2279 return ret;
2280}
2281
2282static int coda_start_encoding(struct coda_ctx *ctx)
2283{
2284 struct coda_dev *dev = ctx->dev;
2285 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2286 struct coda_q_data *q_data_src, *q_data_dst;
2287 u32 bitstream_buf, bitstream_size;
2288 struct vb2_buffer *buf;
2289 int gamma, ret, value;
2290 u32 dst_fourcc;
2291
2292 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2293 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2294 dst_fourcc = q_data_dst->fourcc;
2295
Philipp Zabel14604e32014-07-11 06:36:22 -03002296 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002297 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2298 bitstream_size = q_data_dst->sizeimage;
2299
Javier Martin186b2502012-07-26 05:53:35 -03002300 if (!coda_is_initialized(dev)) {
2301 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2302 return -EFAULT;
2303 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002304
2305 mutex_lock(&dev->coda_mutex);
2306
Javier Martin186b2502012-07-26 05:53:35 -03002307 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002308 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2309 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002310 switch (dev->devtype->product) {
2311 case CODA_DX6:
2312 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2313 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2314 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002315 case CODA_960:
2316 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2317 /* fallthrough */
2318 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002319 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2320 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002321 break;
Javier Martin186b2502012-07-26 05:53:35 -03002322 }
2323
Philipp Zabel89548442014-07-11 06:36:17 -03002324 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2325 value &= ~(1 << 2 | 0x7 << 9);
2326 ctx->frame_mem_ctrl = value;
2327 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2328
Philipp Zabel10436672012-07-02 09:03:55 -03002329 if (dev->devtype->product == CODA_DX6) {
2330 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002331 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002332 }
Javier Martin186b2502012-07-26 05:53:35 -03002333
2334 /* Could set rotation here if needed */
2335 switch (dev->devtype->product) {
2336 case CODA_DX6:
2337 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002338 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002339 break;
2340 default:
2341 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002342 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002343 }
Javier Martin186b2502012-07-26 05:53:35 -03002344 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2345 coda_write(dev, ctx->params.framerate,
2346 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2347
Philipp Zabelb96904e2013-05-23 10:42:58 -03002348 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002349 switch (dst_fourcc) {
2350 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002351 if (dev->devtype->product == CODA_960)
2352 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2353 else
2354 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002355 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2356 break;
2357 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002358 if (dev->devtype->product == CODA_960)
2359 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2360 else
2361 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002362 if (ctx->params.h264_deblk_enabled) {
2363 value = ((ctx->params.h264_deblk_alpha &
2364 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2365 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2366 ((ctx->params.h264_deblk_beta &
2367 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2368 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2369 } else {
2370 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2371 }
2372 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002373 break;
2374 default:
2375 v4l2_err(v4l2_dev,
2376 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002377 ret = -EINVAL;
2378 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002379 }
2380
Philipp Zabelc566c782012-08-08 11:59:38 -03002381 switch (ctx->params.slice_mode) {
2382 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2383 value = 0;
2384 break;
2385 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2386 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2387 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002388 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002389 break;
2390 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2391 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2392 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2393 value |= 1 & CODA_SLICING_MODE_MASK;
2394 break;
2395 }
Javier Martin186b2502012-07-26 05:53:35 -03002396 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002397 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002398 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2399
2400 if (ctx->params.bitrate) {
2401 /* Rate control enabled */
2402 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2403 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002404 if (dev->devtype->product == CODA_960)
2405 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002406 } else {
2407 value = 0;
2408 }
2409 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2410
2411 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002412 coda_write(dev, ctx->params.intra_refresh,
2413 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002414
2415 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2416 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2417
Javier Martin186b2502012-07-26 05:53:35 -03002418
Philipp Zabel89548442014-07-11 06:36:17 -03002419 value = 0;
2420 if (dev->devtype->product == CODA_960)
2421 gamma = CODA9_DEFAULT_GAMMA;
2422 else
2423 gamma = CODA_DEFAULT_GAMMA;
2424 if (gamma > 0) {
2425 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2426 CODA_CMD_ENC_SEQ_RC_GAMMA);
2427 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002428
2429 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2430 coda_write(dev,
2431 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2432 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2433 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2434 }
Philipp Zabel89548442014-07-11 06:36:17 -03002435 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002436 if (ctx->params.h264_max_qp)
2437 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002438 if (CODA_DEFAULT_GAMMA > 0)
2439 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002440 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002441 if (CODA_DEFAULT_GAMMA > 0) {
2442 if (dev->devtype->product == CODA_DX6)
2443 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2444 else
2445 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2446 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002447 if (ctx->params.h264_min_qp)
2448 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2449 if (ctx->params.h264_max_qp)
2450 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002451 }
Javier Martin186b2502012-07-26 05:53:35 -03002452 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2453
Philipp Zabel89548442014-07-11 06:36:17 -03002454 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2455
Philipp Zabelc2d22512013-06-21 03:55:28 -03002456 coda_setup_iram(ctx);
2457
Javier Martin186b2502012-07-26 05:53:35 -03002458 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002459 switch (dev->devtype->product) {
2460 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002461 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002462 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002463 break;
2464 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002465 coda_write(dev, ctx->iram_info.search_ram_paddr,
2466 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2467 coda_write(dev, ctx->iram_info.search_ram_size,
2468 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002469 break;
2470 case CODA_960:
2471 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2472 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002473 }
Javier Martin186b2502012-07-26 05:53:35 -03002474 }
2475
Philipp Zabelfcb62822013-05-23 10:43:00 -03002476 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2477 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002478 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002479 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002480 }
2481
Philipp Zabelfcb62822013-05-23 10:43:00 -03002482 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2483 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2484 ret = -EFAULT;
2485 goto out;
2486 }
Javier Martin186b2502012-07-26 05:53:35 -03002487
Philipp Zabel89548442014-07-11 06:36:17 -03002488 if (dev->devtype->product == CODA_960)
2489 ctx->num_internal_frames = 4;
2490 else
2491 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002492 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002493 if (ret < 0) {
2494 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2495 goto out;
2496 }
Javier Martin186b2502012-07-26 05:53:35 -03002497
Philipp Zabelec25f682012-07-20 08:54:29 -03002498 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002499 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002500 if (dev->devtype->product == CODA_7541)
2501 coda_write(dev, round_up(q_data_src->width, 8),
2502 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002503 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002504 coda_write(dev, ctx->iram_info.buf_bit_use,
2505 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2506 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2507 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2508 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2509 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2510 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2511 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2512 coda_write(dev, ctx->iram_info.buf_ovl_use,
2513 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002514 if (dev->devtype->product == CODA_960) {
2515 coda_write(dev, ctx->iram_info.buf_btp_use,
2516 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2517
2518 /* FIXME */
2519 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2520 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2521 }
Philipp Zabel10436672012-07-02 09:03:55 -03002522 }
Philipp Zabel89548442014-07-11 06:36:17 -03002523
Philipp Zabelfcb62822013-05-23 10:43:00 -03002524 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2525 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002526 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002527 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002528 }
2529
2530 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002531 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002532 switch (dst_fourcc) {
2533 case V4L2_PIX_FMT_H264:
2534 /*
2535 * Get SPS in the first frame and copy it to an
2536 * intermediate buffer.
2537 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002538 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2539 &ctx->vpu_header[0][0],
2540 &ctx->vpu_header_size[0]);
2541 if (ret < 0)
2542 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002543
2544 /*
2545 * Get PPS in the first frame and copy it to an
2546 * intermediate buffer.
2547 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002548 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2549 &ctx->vpu_header[1][0],
2550 &ctx->vpu_header_size[1]);
2551 if (ret < 0)
2552 goto out;
2553
Javier Martin3f3f5c72012-10-29 05:20:29 -03002554 /*
2555 * Length of H.264 headers is variable and thus it might not be
2556 * aligned for the coda to append the encoded frame. In that is
2557 * the case a filler NAL must be added to header 2.
2558 */
2559 ctx->vpu_header_size[2] = coda_h264_padding(
2560 (ctx->vpu_header_size[0] +
2561 ctx->vpu_header_size[1]),
2562 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002563 break;
2564 case V4L2_PIX_FMT_MPEG4:
2565 /*
2566 * Get VOS in the first frame and copy it to an
2567 * intermediate buffer
2568 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002569 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2570 &ctx->vpu_header[0][0],
2571 &ctx->vpu_header_size[0]);
2572 if (ret < 0)
2573 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002574
Philipp Zabeld35167a2013-05-23 10:42:59 -03002575 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2576 &ctx->vpu_header[1][0],
2577 &ctx->vpu_header_size[1]);
2578 if (ret < 0)
2579 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002580
Philipp Zabeld35167a2013-05-23 10:42:59 -03002581 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2582 &ctx->vpu_header[2][0],
2583 &ctx->vpu_header_size[2]);
2584 if (ret < 0)
2585 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002586 break;
2587 default:
2588 /* No more formats need to save headers at the moment */
2589 break;
2590 }
2591
Philipp Zabeld35167a2013-05-23 10:42:59 -03002592out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002593 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002594 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002595}
2596
Hans Verkuile37559b2014-04-17 02:47:21 -03002597static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002598{
2599 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002600 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002601
2602 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002603 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002604 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002605 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002606
Philipp Zabel89548442014-07-11 06:36:17 -03002607 if (ctx->inst_type == CODA_INST_DECODER &&
2608 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2609 /* if this decoder instance is running, set the stream end flag */
2610 if (dev->devtype->product == CODA_960) {
2611 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2612
2613 val |= CODA_BIT_STREAM_END_FLAG;
2614 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2615 ctx->bit_stream_param = val;
2616 }
2617 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002618 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2619
2620 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002621 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002622 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002623 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002624 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002625
2626 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002627 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002628 }
2629
Philipp Zabel918c66f2013-06-27 06:59:01 -03002630 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002631 struct coda_timestamp *ts;
2632
2633 while (!list_empty(&ctx->timestamp_list)) {
2634 ts = list_first_entry(&ctx->timestamp_list,
2635 struct coda_timestamp, list);
2636 list_del(&ts->list);
2637 kfree(ts);
2638 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002639 kfifo_init(&ctx->bitstream_fifo,
2640 ctx->bitstream.vaddr, ctx->bitstream.size);
2641 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002642 }
Javier Martin186b2502012-07-26 05:53:35 -03002643}
2644
2645static struct vb2_ops coda_qops = {
2646 .queue_setup = coda_queue_setup,
2647 .buf_prepare = coda_buf_prepare,
2648 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002649 .start_streaming = coda_start_streaming,
2650 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002651 .wait_prepare = vb2_ops_wait_prepare,
2652 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002653};
2654
2655static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2656{
2657 struct coda_ctx *ctx =
2658 container_of(ctrl->handler, struct coda_ctx, ctrls);
2659
2660 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2661 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2662
2663 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002664 case V4L2_CID_HFLIP:
2665 if (ctrl->val)
2666 ctx->params.rot_mode |= CODA_MIR_HOR;
2667 else
2668 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2669 break;
2670 case V4L2_CID_VFLIP:
2671 if (ctrl->val)
2672 ctx->params.rot_mode |= CODA_MIR_VER;
2673 else
2674 ctx->params.rot_mode &= ~CODA_MIR_VER;
2675 break;
Javier Martin186b2502012-07-26 05:53:35 -03002676 case V4L2_CID_MPEG_VIDEO_BITRATE:
2677 ctx->params.bitrate = ctrl->val / 1000;
2678 break;
2679 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2680 ctx->params.gop_size = ctrl->val;
2681 break;
2682 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2683 ctx->params.h264_intra_qp = ctrl->val;
2684 break;
2685 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2686 ctx->params.h264_inter_qp = ctrl->val;
2687 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002688 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2689 ctx->params.h264_min_qp = ctrl->val;
2690 break;
2691 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2692 ctx->params.h264_max_qp = ctrl->val;
2693 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002694 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2695 ctx->params.h264_deblk_alpha = ctrl->val;
2696 break;
2697 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2698 ctx->params.h264_deblk_beta = ctrl->val;
2699 break;
2700 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2701 ctx->params.h264_deblk_enabled = (ctrl->val ==
2702 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2703 break;
Javier Martin186b2502012-07-26 05:53:35 -03002704 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2705 ctx->params.mpeg4_intra_qp = ctrl->val;
2706 break;
2707 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2708 ctx->params.mpeg4_inter_qp = ctrl->val;
2709 break;
2710 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2711 ctx->params.slice_mode = ctrl->val;
2712 break;
2713 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2714 ctx->params.slice_max_mb = ctrl->val;
2715 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002716 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2717 ctx->params.slice_max_bits = ctrl->val * 8;
2718 break;
Javier Martin186b2502012-07-26 05:53:35 -03002719 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2720 break;
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002721 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2722 ctx->params.intra_refresh = ctrl->val;
2723 break;
Javier Martin186b2502012-07-26 05:53:35 -03002724 default:
2725 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2726 "Invalid control, id=%d, val=%d\n",
2727 ctrl->id, ctrl->val);
2728 return -EINVAL;
2729 }
2730
2731 return 0;
2732}
2733
2734static struct v4l2_ctrl_ops coda_ctrl_ops = {
2735 .s_ctrl = coda_s_ctrl,
2736};
2737
2738static int coda_ctrls_setup(struct coda_ctx *ctx)
2739{
2740 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2741
2742 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002743 V4L2_CID_HFLIP, 0, 1, 1, 0);
2744 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2745 V4L2_CID_VFLIP, 0, 1, 1, 0);
2746 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002747 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2748 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2749 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2750 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002751 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002752 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002753 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002754 if (ctx->dev->devtype->product != CODA_960) {
2755 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2756 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2757 }
2758 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2759 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002760 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002761 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2762 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2763 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2764 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2765 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2766 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2767 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2768 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002769 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2770 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2771 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2772 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2773 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002774 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2775 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002776 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2777 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002778 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2779 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002780 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2781 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2782 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2783 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2784 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d52014-07-11 06:36:28 -03002785 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2786 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002787
2788 if (ctx->ctrls.error) {
2789 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2790 ctx->ctrls.error);
2791 return -EINVAL;
2792 }
2793
2794 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2795}
2796
2797static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2798 struct vb2_queue *dst_vq)
2799{
2800 struct coda_ctx *ctx = priv;
2801 int ret;
2802
Javier Martin186b2502012-07-26 05:53:35 -03002803 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002804 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002805 src_vq->drv_priv = ctx;
2806 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2807 src_vq->ops = &coda_qops;
2808 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002809 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002810 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002811
2812 ret = vb2_queue_init(src_vq);
2813 if (ret)
2814 return ret;
2815
Javier Martin186b2502012-07-26 05:53:35 -03002816 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002817 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002818 dst_vq->drv_priv = ctx;
2819 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2820 dst_vq->ops = &coda_qops;
2821 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002822 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002823 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002824
2825 return vb2_queue_init(dst_vq);
2826}
2827
Philipp Zabele11f3e62012-07-25 09:16:58 -03002828static int coda_next_free_instance(struct coda_dev *dev)
2829{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002830 int idx = ffz(dev->instance_mask);
2831
2832 if ((idx < 0) ||
2833 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2834 return -EBUSY;
2835
2836 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002837}
2838
Javier Martin186b2502012-07-26 05:53:35 -03002839static int coda_open(struct file *file)
2840{
2841 struct coda_dev *dev = video_drvdata(file);
2842 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002843 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002844 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002845
Javier Martin186b2502012-07-26 05:53:35 -03002846 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2847 if (!ctx)
2848 return -ENOMEM;
2849
Fabio Estevamf82bc202013-08-21 11:14:16 -03002850 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002851 if (idx < 0) {
2852 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002853 goto err_coda_max;
2854 }
2855 set_bit(idx, &dev->instance_mask);
2856
Philipp Zabel32b6f202014-07-11 06:36:20 -03002857 init_completion(&ctx->completion);
2858 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2859 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002860 v4l2_fh_init(&ctx->fh, video_devdata(file));
2861 file->private_data = &ctx->fh;
2862 v4l2_fh_add(&ctx->fh);
2863 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002864 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002865 switch (dev->devtype->product) {
2866 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002867 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002868 ctx->reg_idx = 0;
2869 break;
2870 default:
2871 ctx->reg_idx = idx;
2872 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002873
Philipp Zabel1e172732014-07-11 06:36:23 -03002874 /* Power up and upload firmware if necessary */
2875 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2876 if (ret < 0) {
2877 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2878 goto err_pm_get;
2879 }
2880
Fabio Estevam79830db2013-08-21 11:14:17 -03002881 ret = clk_prepare_enable(dev->clk_per);
2882 if (ret)
2883 goto err_clk_per;
2884
2885 ret = clk_prepare_enable(dev->clk_ahb);
2886 if (ret)
2887 goto err_clk_ahb;
2888
Javier Martin186b2502012-07-26 05:53:35 -03002889 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002890 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002891 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002892 if (IS_ERR(ctx->fh.m2m_ctx)) {
2893 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002894
2895 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2896 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002897 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002898 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002899
Javier Martin186b2502012-07-26 05:53:35 -03002900 ret = coda_ctrls_setup(ctx);
2901 if (ret) {
2902 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002903 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002904 }
2905
2906 ctx->fh.ctrl_handler = &ctx->ctrls;
2907
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002908 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2909 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002910 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002911 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002912 }
2913
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002914 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2915 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2916 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2917 if (!ctx->bitstream.vaddr) {
2918 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2919 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002920 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002921 }
2922 kfifo_init(&ctx->bitstream_fifo,
2923 ctx->bitstream.vaddr, ctx->bitstream.size);
2924 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002925 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002926 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002927
Javier Martin186b2502012-07-26 05:53:35 -03002928 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002929 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002930 coda_unlock(ctx);
2931
Javier Martin186b2502012-07-26 05:53:35 -03002932 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2933 ctx->idx, ctx);
2934
2935 return 0;
2936
Fabio Estevamf82bc202013-08-21 11:14:16 -03002937err_dma_writecombine:
2938 coda_free_context_buffers(ctx);
2939 if (ctx->dev->devtype->product == CODA_DX6)
2940 coda_free_aux_buf(dev, &ctx->workbuf);
2941 coda_free_aux_buf(dev, &ctx->parabuf);
2942err_dma_alloc:
2943 v4l2_ctrl_handler_free(&ctx->ctrls);
2944err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002945 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002946err_ctx_init:
2947 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002948err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002949 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002950err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002951 pm_runtime_put_sync(&dev->plat_dev->dev);
2952err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002953 v4l2_fh_del(&ctx->fh);
2954 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002955 clear_bit(ctx->idx, &dev->instance_mask);
2956err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002957 kfree(ctx);
2958 return ret;
2959}
2960
2961static int coda_release(struct file *file)
2962{
2963 struct coda_dev *dev = video_drvdata(file);
2964 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2965
2966 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2967 ctx);
2968
Philipp Zabel918c66f2013-06-27 06:59:01 -03002969 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002970 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002971
2972 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002973 if (ctx->initialized) {
2974 queue_work(dev->workqueue, &ctx->seq_end_work);
2975 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002976 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002977
2978 coda_free_framebuffers(ctx);
2979
Javier Martin186b2502012-07-26 05:53:35 -03002980 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002981 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002982 coda_unlock(ctx);
2983
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002984 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2985 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002986 coda_free_context_buffers(ctx);
2987 if (ctx->dev->devtype->product == CODA_DX6)
2988 coda_free_aux_buf(dev, &ctx->workbuf);
2989
2990 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002991 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002992 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002993 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002994 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002995 v4l2_fh_del(&ctx->fh);
2996 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002997 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002998 kfree(ctx);
2999
3000 return 0;
3001}
3002
Javier Martin186b2502012-07-26 05:53:35 -03003003static const struct v4l2_file_operations coda_fops = {
3004 .owner = THIS_MODULE,
3005 .open = coda_open,
3006 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003007 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003008 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003009 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003010};
3011
Philipp Zabel918c66f2013-06-27 06:59:01 -03003012static void coda_finish_decode(struct coda_ctx *ctx)
3013{
3014 struct coda_dev *dev = ctx->dev;
3015 struct coda_q_data *q_data_src;
3016 struct coda_q_data *q_data_dst;
3017 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003018 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003019 int width, height;
3020 int decoded_idx;
3021 int display_idx;
3022 u32 src_fourcc;
3023 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003024 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003025 u32 val;
3026
Philipp Zabel14604e32014-07-11 06:36:22 -03003027 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003028
3029 /* Update kfifo out pointer from coda bitstream read pointer */
3030 coda_kfifo_sync_from_device(ctx);
3031
3032 /*
3033 * in stream-end mode, the read pointer can overshoot the write pointer
3034 * by up to 512 bytes
3035 */
3036 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3037 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3038 kfifo_init(&ctx->bitstream_fifo,
3039 ctx->bitstream.vaddr, ctx->bitstream.size);
3040 }
3041
3042 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3043 src_fourcc = q_data_src->fourcc;
3044
3045 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3046 if (val != 1)
3047 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3048
3049 success = val & 0x1;
3050 if (!success)
3051 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3052
3053 if (src_fourcc == V4L2_PIX_FMT_H264) {
3054 if (val & (1 << 3))
3055 v4l2_err(&dev->v4l2_dev,
3056 "insufficient PS buffer space (%d bytes)\n",
3057 ctx->psbuf.size);
3058 if (val & (1 << 2))
3059 v4l2_err(&dev->v4l2_dev,
3060 "insufficient slice buffer space (%d bytes)\n",
3061 ctx->slicebuf.size);
3062 }
3063
3064 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3065 width = (val >> 16) & 0xffff;
3066 height = val & 0xffff;
3067
3068 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3069
Philipp Zabel52c41672014-07-11 06:36:19 -03003070 /* frame crop information */
3071 if (src_fourcc == V4L2_PIX_FMT_H264) {
3072 u32 left_right;
3073 u32 top_bottom;
3074
3075 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3076 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3077
3078 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3079 /* Keep current crop information */
3080 } else {
3081 struct v4l2_rect *rect = &q_data_dst->rect;
3082
3083 rect->left = left_right >> 16 & 0xffff;
3084 rect->top = top_bottom >> 16 & 0xffff;
3085 rect->width = width - rect->left -
3086 (left_right & 0xffff);
3087 rect->height = height - rect->top -
3088 (top_bottom & 0xffff);
3089 }
3090 } else {
3091 /* no cropping */
3092 }
3093
Philipp Zabel410e5e42014-07-11 06:36:32 -03003094 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3095 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003096 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003097 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003098
3099 if (dev->devtype->product == CODA_7541) {
3100 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3101 if (val == 0) {
3102 /* not enough bitstream data */
3103 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3104 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003105 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003106 return;
3107 }
3108 }
3109
3110 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3111
3112 /*
3113 * The previous display frame was copied out by the rotator,
3114 * now it can be overwritten again
3115 */
3116 if (ctx->display_idx >= 0 &&
3117 ctx->display_idx < ctx->num_internal_frames) {
3118 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3119 coda_write(dev, ctx->frm_dis_flg,
3120 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3121 }
3122
3123 /*
3124 * The index of the last decoded frame, not necessarily in
3125 * display order, and the index of the next display frame.
3126 * The latter could have been decoded in a previous run.
3127 */
3128 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3129 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3130
3131 if (decoded_idx == -1) {
3132 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003133 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3134 ctx->sequence_offset++;
3135 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003136 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003137 } else if (decoded_idx == -2) {
3138 /* no frame was decoded, we still return the remaining buffers */
3139 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3140 v4l2_err(&dev->v4l2_dev,
3141 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003142 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003143 ts = list_first_entry(&ctx->timestamp_list,
3144 struct coda_timestamp, list);
3145 list_del(&ts->list);
3146 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003147 val -= ctx->sequence_offset;
3148 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003149 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003150 "sequence number mismatch (%d(%d) != %d)\n",
3151 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003152 }
3153 ctx->frame_timestamps[decoded_idx] = *ts;
3154 kfree(ts);
3155
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003156 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3157 if (val == 0)
3158 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3159 else if (val == 1)
3160 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3161 else
3162 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003163
3164 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003165 }
3166
3167 if (display_idx == -1) {
3168 /*
3169 * no more frames to be decoded, but there could still
3170 * be rotator output to dequeue
3171 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003172 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003173 } else if (display_idx == -3) {
3174 /* possibly prescan failure */
3175 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3176 v4l2_err(&dev->v4l2_dev,
3177 "presentation frame index out of range: %d\n",
3178 display_idx);
3179 }
3180
3181 /* If a frame was copied out, return it */
3182 if (ctx->display_idx >= 0 &&
3183 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003184 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003185 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3186
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003187 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3188 V4L2_BUF_FLAG_PFRAME |
3189 V4L2_BUF_FLAG_BFRAME);
3190 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003191 ts = &ctx->frame_timestamps[ctx->display_idx];
3192 dst_buf->v4l2_buf.timecode = ts->timecode;
3193 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003194
Philipp Zabel918c66f2013-06-27 06:59:01 -03003195 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3196
Philipp Zabel410e5e42014-07-11 06:36:32 -03003197 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3198 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003199
3200 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3201 "job finished: decoding frame (%d) (%s)\n",
3202 dst_buf->v4l2_buf.sequence,
3203 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3204 "KEYFRAME" : "PFRAME");
3205 } else {
3206 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3207 "job finished: no frame decoded\n");
3208 }
3209
3210 /* The rotator will copy the current display frame next time */
3211 ctx->display_idx = display_idx;
3212}
3213
3214static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003215{
Philipp Zabelec25f682012-07-20 08:54:29 -03003216 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003217 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003218 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003219
Philipp Zabel14604e32014-07-11 06:36:22 -03003220 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3221 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003222
3223 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003224 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003225 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3226
Javier Martin186b2502012-07-26 05:53:35 -03003227 /* Calculate bytesused field */
3228 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003229 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3230 ctx->vpu_header_size[0] +
3231 ctx->vpu_header_size[1] +
3232 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003233 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003234 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003235 }
3236
3237 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3238 wr_ptr - start_ptr);
3239
3240 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3241 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3242
Philipp Zabel51660282013-09-30 10:34:49 -03003243 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003244 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3245 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3246 } else {
3247 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3248 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3249 }
3250
Kamil Debskiccd571c2013-04-24 10:38:24 -03003251 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003252 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3253 dst_buf->v4l2_buf.flags |=
3254 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003255 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3256
Philipp Zabelec25f682012-07-20 08:54:29 -03003257 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003258
Philipp Zabel14604e32014-07-11 06:36:22 -03003259 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003260 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3261
3262 ctx->gopcounter--;
3263 if (ctx->gopcounter < 0)
3264 ctx->gopcounter = ctx->params.gop_size - 1;
3265
3266 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3267 "job finished: encoding frame (%d) (%s)\n",
3268 dst_buf->v4l2_buf.sequence,
3269 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3270 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003271}
3272
3273static irqreturn_t coda_irq_handler(int irq, void *data)
3274{
3275 struct coda_dev *dev = data;
3276 struct coda_ctx *ctx;
3277
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003278 /* read status register to attend the IRQ */
3279 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3280 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3281 CODA_REG_BIT_INT_CLEAR);
3282
3283 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3284 if (ctx == NULL) {
3285 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3286 mutex_unlock(&dev->coda_mutex);
3287 return IRQ_HANDLED;
3288 }
3289
3290 if (ctx->aborting) {
3291 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3292 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003293 }
3294
3295 if (coda_isbusy(ctx->dev)) {
3296 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3297 "coda is still busy!!!!\n");
3298 return IRQ_NONE;
3299 }
3300
Philipp Zabel32b6f202014-07-11 06:36:20 -03003301 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003302
3303 return IRQ_HANDLED;
3304}
3305
3306static u32 coda_supported_firmwares[] = {
3307 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003308 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003309 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003310};
3311
3312static bool coda_firmware_supported(u32 vernum)
3313{
3314 int i;
3315
3316 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3317 if (vernum == coda_supported_firmwares[i])
3318 return true;
3319 return false;
3320}
3321
Philipp Zabel87048bb2012-07-02 07:03:43 -03003322static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003323{
Javier Martin186b2502012-07-26 05:53:35 -03003324 u32 data;
3325 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003326 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003327
Fabio Estevam79830db2013-08-21 11:14:17 -03003328 ret = clk_prepare_enable(dev->clk_per);
3329 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003330 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003331
3332 ret = clk_prepare_enable(dev->clk_ahb);
3333 if (ret)
3334 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003335
Javier Martin186b2502012-07-26 05:53:35 -03003336 /*
3337 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003338 * The 16-bit chars in the code buffer are in memory access
3339 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003340 * Data in this SRAM survives a reboot.
3341 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003342 p = (u16 *)dev->codebuf.vaddr;
3343 if (dev->devtype->product == CODA_DX6) {
3344 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3345 data = CODA_DOWN_ADDRESS_SET(i) |
3346 CODA_DOWN_DATA_SET(p[i ^ 1]);
3347 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3348 }
3349 } else {
3350 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3351 data = CODA_DOWN_ADDRESS_SET(i) |
3352 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3353 3 - (i % 4)]);
3354 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3355 }
Javier Martin186b2502012-07-26 05:53:35 -03003356 }
Javier Martin186b2502012-07-26 05:53:35 -03003357
Philipp Zabel9acf7692013-05-23 10:42:56 -03003358 /* Clear registers */
3359 for (i = 0; i < 64; i++)
3360 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3361
Javier Martin186b2502012-07-26 05:53:35 -03003362 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003363 if (dev->devtype->product == CODA_960 ||
3364 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003365 coda_write(dev, dev->tempbuf.paddr,
3366 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003367 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003368 } else {
3369 coda_write(dev, dev->workbuf.paddr,
3370 CODA_REG_BIT_WORK_BUF_ADDR);
3371 }
Javier Martin186b2502012-07-26 05:53:35 -03003372 coda_write(dev, dev->codebuf.paddr,
3373 CODA_REG_BIT_CODE_BUF_ADDR);
3374 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3375
3376 /* Set default values */
3377 switch (dev->devtype->product) {
3378 case CODA_DX6:
3379 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3380 break;
3381 default:
3382 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3383 }
Philipp Zabel89548442014-07-11 06:36:17 -03003384 if (dev->devtype->product == CODA_960)
3385 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3386 else
3387 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003388
3389 if (dev->devtype->product != CODA_DX6)
3390 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3391
Javier Martin186b2502012-07-26 05:53:35 -03003392 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3393 CODA_REG_BIT_INT_ENABLE);
3394
3395 /* Reset VPU and start processor */
3396 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3397 data |= CODA_REG_RESET_ENABLE;
3398 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3399 udelay(10);
3400 data &= ~CODA_REG_RESET_ENABLE;
3401 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3402 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3403
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003404 clk_disable_unprepare(dev->clk_ahb);
3405 clk_disable_unprepare(dev->clk_per);
3406
3407 return 0;
3408
3409err_clk_ahb:
3410 clk_disable_unprepare(dev->clk_per);
3411err_clk_per:
3412 return ret;
3413}
3414
3415static int coda_check_firmware(struct coda_dev *dev)
3416{
3417 u16 product, major, minor, release;
3418 u32 data;
3419 int ret;
3420
3421 ret = clk_prepare_enable(dev->clk_per);
3422 if (ret)
3423 goto err_clk_per;
3424
3425 ret = clk_prepare_enable(dev->clk_ahb);
3426 if (ret)
3427 goto err_clk_ahb;
3428
Javier Martin186b2502012-07-26 05:53:35 -03003429 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3430 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3431 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3432 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3433 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3434 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003435 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003436 ret = -EIO;
3437 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003438 }
3439
Philipp Zabel89548442014-07-11 06:36:17 -03003440 if (dev->devtype->product == CODA_960) {
3441 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3442 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3443 data);
3444 }
3445
Javier Martin186b2502012-07-26 05:53:35 -03003446 /* Check we are compatible with the loaded firmware */
3447 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3448 product = CODA_FIRMWARE_PRODUCT(data);
3449 major = CODA_FIRMWARE_MAJOR(data);
3450 minor = CODA_FIRMWARE_MINOR(data);
3451 release = CODA_FIRMWARE_RELEASE(data);
3452
3453 clk_disable_unprepare(dev->clk_per);
3454 clk_disable_unprepare(dev->clk_ahb);
3455
3456 if (product != dev->devtype->product) {
3457 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3458 " Version: %u.%u.%u\n",
3459 coda_product_name(dev->devtype->product),
3460 coda_product_name(product), major, minor, release);
3461 return -EINVAL;
3462 }
3463
3464 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3465 coda_product_name(product));
3466
3467 if (coda_firmware_supported(data)) {
3468 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3469 major, minor, release);
3470 } else {
3471 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3472 "%u.%u.%u\n", major, minor, release);
3473 }
3474
3475 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003476
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003477err_run_cmd:
3478 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003479err_clk_ahb:
3480 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003481err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003482 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003483}
3484
3485static void coda_fw_callback(const struct firmware *fw, void *context)
3486{
3487 struct coda_dev *dev = context;
3488 struct platform_device *pdev = dev->plat_dev;
3489 int ret;
3490
3491 if (!fw) {
3492 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3493 return;
3494 }
3495
3496 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003497 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3498 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003499 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3500 return;
3501 }
3502
Philipp Zabel87048bb2012-07-02 07:03:43 -03003503 /* Copy the whole firmware image to the code buffer */
3504 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3505 release_firmware(fw);
3506
Philipp Zabel1e172732014-07-11 06:36:23 -03003507 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3508 /*
3509 * Enabling power temporarily will cause coda_hw_init to be
3510 * called via coda_runtime_resume by the pm domain.
3511 */
3512 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3513 if (ret < 0) {
3514 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3515 ret);
3516 return;
3517 }
3518
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003519 ret = coda_check_firmware(dev);
3520 if (ret < 0)
3521 return;
3522
Philipp Zabel1e172732014-07-11 06:36:23 -03003523 pm_runtime_put_sync(&dev->plat_dev->dev);
3524 } else {
3525 /*
3526 * If runtime pm is disabled or pm_domain is not set,
3527 * initialize once manually.
3528 */
3529 ret = coda_hw_init(dev);
3530 if (ret < 0) {
3531 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3532 return;
3533 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003534
3535 ret = coda_check_firmware(dev);
3536 if (ret < 0)
3537 return;
Javier Martin186b2502012-07-26 05:53:35 -03003538 }
3539
3540 dev->vfd.fops = &coda_fops,
3541 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3542 dev->vfd.release = video_device_release_empty,
3543 dev->vfd.lock = &dev->dev_mutex;
3544 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003545 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003546 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3547 video_set_drvdata(&dev->vfd, dev);
3548
3549 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3550 if (IS_ERR(dev->alloc_ctx)) {
3551 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3552 return;
3553 }
3554
3555 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3556 if (IS_ERR(dev->m2m_dev)) {
3557 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3558 goto rel_ctx;
3559 }
3560
3561 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3562 if (ret) {
3563 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3564 goto rel_m2m;
3565 }
3566 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3567 dev->vfd.num);
3568
3569 return;
3570
3571rel_m2m:
3572 v4l2_m2m_release(dev->m2m_dev);
3573rel_ctx:
3574 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3575}
3576
3577static int coda_firmware_request(struct coda_dev *dev)
3578{
3579 char *fw = dev->devtype->firmware;
3580
3581 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3582 coda_product_name(dev->devtype->product));
3583
3584 return request_firmware_nowait(THIS_MODULE, true,
3585 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3586}
3587
3588enum coda_platform {
3589 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003590 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003591 CODA_IMX6Q,
3592 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003593};
3594
Emil Goodec06d8752012-08-14 17:44:42 -03003595static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003596 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003597 .firmware = "v4l-codadx6-imx27.bin",
3598 .product = CODA_DX6,
3599 .codecs = codadx6_codecs,
3600 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003601 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003602 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003603 .firmware = "v4l-coda7541-imx53.bin",
3604 .product = CODA_7541,
3605 .codecs = coda7_codecs,
3606 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003607 },
Philipp Zabel89548442014-07-11 06:36:17 -03003608 [CODA_IMX6Q] = {
3609 .firmware = "v4l-coda960-imx6q.bin",
3610 .product = CODA_960,
3611 .codecs = coda9_codecs,
3612 .num_codecs = ARRAY_SIZE(coda9_codecs),
3613 },
3614 [CODA_IMX6DL] = {
3615 .firmware = "v4l-coda960-imx6dl.bin",
3616 .product = CODA_960,
3617 .codecs = coda9_codecs,
3618 .num_codecs = ARRAY_SIZE(coda9_codecs),
3619 },
Javier Martin186b2502012-07-26 05:53:35 -03003620};
3621
3622static struct platform_device_id coda_platform_ids[] = {
3623 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003624 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003625 { /* sentinel */ }
3626};
3627MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3628
3629#ifdef CONFIG_OF
3630static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003631 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003632 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003633 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3634 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003635 { /* sentinel */ }
3636};
3637MODULE_DEVICE_TABLE(of, coda_dt_ids);
3638#endif
3639
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003640static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003641{
3642 const struct of_device_id *of_id =
3643 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3644 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003645 struct coda_platform_data *pdata = pdev->dev.platform_data;
3646 struct device_node *np = pdev->dev.of_node;
3647 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003648 struct coda_dev *dev;
3649 struct resource *res;
3650 int ret, irq;
3651
3652 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3653 if (!dev) {
3654 dev_err(&pdev->dev, "Not enough memory for %s\n",
3655 CODA_NAME);
3656 return -ENOMEM;
3657 }
3658
3659 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003660 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003661
3662 dev->plat_dev = pdev;
3663 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3664 if (IS_ERR(dev->clk_per)) {
3665 dev_err(&pdev->dev, "Could not get per clock\n");
3666 return PTR_ERR(dev->clk_per);
3667 }
3668
3669 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3670 if (IS_ERR(dev->clk_ahb)) {
3671 dev_err(&pdev->dev, "Could not get ahb clock\n");
3672 return PTR_ERR(dev->clk_ahb);
3673 }
3674
3675 /* Get memory for physical registers */
3676 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003677 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3678 if (IS_ERR(dev->regs_base))
3679 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003680
3681 /* IRQ */
3682 irq = platform_get_irq(pdev, 0);
3683 if (irq < 0) {
3684 dev_err(&pdev->dev, "failed to get irq resource\n");
3685 return -ENOENT;
3686 }
3687
Philipp Zabel918c66f2013-06-27 06:59:01 -03003688 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003689 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003690 dev_err(&pdev->dev, "failed to request irq\n");
3691 return -ENOENT;
3692 }
3693
Philipp Zabel657eee72013-04-29 16:17:14 -07003694 /* Get IRAM pool from device tree or platform data */
3695 pool = of_get_named_gen_pool(np, "iram", 0);
3696 if (!pool && pdata)
3697 pool = dev_get_gen_pool(pdata->iram_dev);
3698 if (!pool) {
3699 dev_err(&pdev->dev, "iram pool not available\n");
3700 return -ENOMEM;
3701 }
3702 dev->iram_pool = pool;
3703
Javier Martin186b2502012-07-26 05:53:35 -03003704 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3705 if (ret)
3706 return ret;
3707
3708 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003709 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003710
3711 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3712
3713 if (of_id) {
3714 dev->devtype = of_id->data;
3715 } else if (pdev_id) {
3716 dev->devtype = &coda_devdata[pdev_id->driver_data];
3717 } else {
3718 v4l2_device_unregister(&dev->v4l2_dev);
3719 return -EINVAL;
3720 }
3721
3722 /* allocate auxiliary per-device buffers for the BIT processor */
3723 switch (dev->devtype->product) {
3724 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003725 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3726 CODADX6_WORK_BUF_SIZE);
3727 if (ret < 0) {
3728 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3729 v4l2_device_unregister(&dev->v4l2_dev);
3730 return ret;
3731 }
Javier Martin186b2502012-07-26 05:53:35 -03003732 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003733 case CODA_7541:
3734 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3735 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003736 case CODA_960:
3737 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3738 break;
Javier Martin186b2502012-07-26 05:53:35 -03003739 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003740 if (dev->tempbuf.size) {
3741 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3742 dev->tempbuf.size);
3743 if (ret < 0) {
3744 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3745 v4l2_device_unregister(&dev->v4l2_dev);
3746 return ret;
3747 }
Javier Martin186b2502012-07-26 05:53:35 -03003748 }
3749
Philipp Zabel918c66f2013-06-27 06:59:01 -03003750 switch (dev->devtype->product) {
3751 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003752 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003753 break;
3754 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003755 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003756 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003757 case CODA_960:
3758 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003759 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003760 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3761 &dev->iram.paddr);
3762 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003763 dev_err(&pdev->dev, "unable to alloc iram\n");
3764 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003765 }
3766
Philipp Zabel32b6f202014-07-11 06:36:20 -03003767 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3768 if (!dev->workqueue) {
3769 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3770 return -ENOMEM;
3771 }
3772
Javier Martin186b2502012-07-26 05:53:35 -03003773 platform_set_drvdata(pdev, dev);
3774
Philipp Zabel1e172732014-07-11 06:36:23 -03003775 pm_runtime_enable(&pdev->dev);
3776
Javier Martin186b2502012-07-26 05:53:35 -03003777 return coda_firmware_request(dev);
3778}
3779
3780static int coda_remove(struct platform_device *pdev)
3781{
3782 struct coda_dev *dev = platform_get_drvdata(pdev);
3783
3784 video_unregister_device(&dev->vfd);
3785 if (dev->m2m_dev)
3786 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003787 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003788 if (dev->alloc_ctx)
3789 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3790 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003791 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003792 if (dev->iram.vaddr)
3793 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3794 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003795 coda_free_aux_buf(dev, &dev->codebuf);
3796 coda_free_aux_buf(dev, &dev->tempbuf);
3797 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003798 return 0;
3799}
3800
Philipp Zabel1e172732014-07-11 06:36:23 -03003801#ifdef CONFIG_PM_RUNTIME
3802static int coda_runtime_resume(struct device *dev)
3803{
3804 struct coda_dev *cdev = dev_get_drvdata(dev);
3805 int ret = 0;
3806
3807 if (dev->pm_domain) {
3808 ret = coda_hw_init(cdev);
3809 if (ret)
3810 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3811 }
3812
3813 return ret;
3814}
3815#endif
3816
3817static const struct dev_pm_ops coda_pm_ops = {
3818 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3819};
3820
Javier Martin186b2502012-07-26 05:53:35 -03003821static struct platform_driver coda_driver = {
3822 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003823 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003824 .driver = {
3825 .name = CODA_NAME,
3826 .owner = THIS_MODULE,
3827 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003828 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003829 },
3830 .id_table = coda_platform_ids,
3831};
3832
3833module_platform_driver(coda_driver);
3834
3835MODULE_LICENSE("GPL");
3836MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3837MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");