blob: 43349d94c93ecbf01f98ac171e4e6bd0948239a8 [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070017#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030018#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030021#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030022#include <linux/module.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030025#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030026#include <linux/slab.h>
27#include <linux/videodev2.h>
28#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070029#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030030
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030033#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030034#include <media/v4l2-ioctl.h>
35#include <media/v4l2-mem2mem.h>
36#include <media/videobuf2-core.h>
37#include <media/videobuf2-dma-contig.h>
38
39#include "coda.h"
40
41#define CODA_NAME "coda"
42
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030043#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030044
45#define CODA_FMO_BUF_SIZE 32
46#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030047#define CODA7_WORK_BUF_SIZE (128 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030048#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030049#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030050#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030051#define CODA_PARA_BUF_SIZE (10 * 1024)
52#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070053#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030054#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030055#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030056
Philipp Zabel918c66f2013-06-27 06:59:01 -030057#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030058#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030059
60#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030061
Philipp Zabelb96904e2013-05-23 10:42:58 -030062#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030063#define FMO_SLICE_SAVE_BUF_SIZE (32)
64#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030065#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030066
67#define MIN_W 176
68#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030069
70#define S_ALIGN 1 /* multiple of 2 */
71#define W_ALIGN 1 /* multiple of 2 */
72#define H_ALIGN 1 /* multiple of 2 */
73
74#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
75
76static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030077module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030078MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
79
80enum {
81 V4L2_M2M_SRC = 0,
82 V4L2_M2M_DST = 1,
83};
84
Javier Martin186b2502012-07-26 05:53:35 -030085enum coda_inst_type {
86 CODA_INST_ENCODER,
87 CODA_INST_DECODER,
88};
89
90enum coda_product {
91 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030092 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030093 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030094};
95
96struct coda_fmt {
97 char *name;
98 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030099};
100
101struct coda_codec {
102 u32 mode;
103 u32 src_fourcc;
104 u32 dst_fourcc;
105 u32 max_w;
106 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300107};
108
109struct coda_devtype {
110 char *firmware;
111 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300112 struct coda_codec *codecs;
113 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300114 size_t workbuf_size;
115};
116
117/* Per-queue, driver-specific private data */
118struct coda_q_data {
119 unsigned int width;
120 unsigned int height;
121 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300122 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300123 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300124};
125
126struct coda_aux_buf {
127 void *vaddr;
128 dma_addr_t paddr;
129 u32 size;
130};
131
132struct coda_dev {
133 struct v4l2_device v4l2_dev;
134 struct video_device vfd;
135 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300136 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300137
138 void __iomem *regs_base;
139 struct clk *clk_per;
140 struct clk *clk_ahb;
141
142 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300143 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300144 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700145 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300146 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300147
148 spinlock_t irqlock;
149 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300150 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300151 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300152 struct v4l2_m2m_dev *m2m_dev;
153 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300154 struct list_head instances;
155 unsigned long instance_mask;
Javier Martin186b2502012-07-26 05:53:35 -0300156};
157
158struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300159 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300160 u8 h264_intra_qp;
161 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300162 u8 h264_min_qp;
163 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300164 u8 h264_deblk_enabled;
165 u8 h264_deblk_alpha;
166 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300167 u8 mpeg4_intra_qp;
168 u8 mpeg4_inter_qp;
169 u8 gop_size;
Philipp Zabelf38f79d2014-07-11 06:36:28 -0300170 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300171 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300172 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300173 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
174 u32 framerate;
175 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300176 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300177 u32 slice_max_mb;
178};
179
Philipp Zabelc2d22512013-06-21 03:55:28 -0300180struct coda_iram_info {
181 u32 axi_sram_use;
182 phys_addr_t buf_bit_use;
183 phys_addr_t buf_ip_ac_dc_use;
184 phys_addr_t buf_dbk_y_use;
185 phys_addr_t buf_dbk_c_use;
186 phys_addr_t buf_ovl_use;
187 phys_addr_t buf_btp_use;
188 phys_addr_t search_ram_paddr;
189 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300190 int remaining;
191 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300192};
193
Philipp Zabel89548442014-07-11 06:36:17 -0300194struct gdi_tiled_map {
195 int xy2ca_map[16];
196 int xy2ba_map[16];
197 int xy2ra_map[16];
198 int rbc2axi_map[32];
199 int xy2rbc_config;
200 int map_type;
201#define GDI_LINEAR_FRAME_MAP 0
202};
203
Javier Martin186b2502012-07-26 05:53:35 -0300204struct coda_ctx {
205 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300206 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300207 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300208 struct work_struct pic_run_work;
209 struct work_struct seq_end_work;
210 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300211 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300212 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300213 int streamon_out;
214 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300215 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300216 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300217 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300218 struct coda_q_data q_data[2];
219 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300220 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300221 enum v4l2_colorspace colorspace;
222 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300223 struct v4l2_ctrl_handler ctrls;
224 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300225 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300226 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300227 char vpu_header[3][64];
228 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300229 struct kfifo bitstream_fifo;
230 struct mutex bitstream_mutex;
231 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300232 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300233 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300234 struct coda_aux_buf psbuf;
235 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300236 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300237 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300238 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300239 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300240 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300241 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300242 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300243 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300244 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300245 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300246 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300247 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300248};
249
Javier Martin832fbb52012-10-29 08:34:59 -0300250static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
251 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
252static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300253
Javier Martin186b2502012-07-26 05:53:35 -0300254static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
255{
256 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
257 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
258 writel(data, dev->regs_base + reg);
259}
260
261static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
262{
263 u32 data;
264 data = readl(dev->regs_base + reg);
265 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
266 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
267 return data;
268}
269
270static inline unsigned long coda_isbusy(struct coda_dev *dev)
271{
272 return coda_read(dev, CODA_REG_BIT_BUSY);
273}
274
275static inline int coda_is_initialized(struct coda_dev *dev)
276{
277 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
278}
279
280static int coda_wait_timeout(struct coda_dev *dev)
281{
282 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
283
284 while (coda_isbusy(dev)) {
285 if (time_after(jiffies, timeout))
286 return -ETIMEDOUT;
287 }
288 return 0;
289}
290
291static void coda_command_async(struct coda_ctx *ctx, int cmd)
292{
293 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300294
Philipp Zabel89548442014-07-11 06:36:17 -0300295 if (dev->devtype->product == CODA_960 ||
296 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300297 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300298 coda_write(dev, ctx->bit_stream_param,
299 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300300 coda_write(dev, ctx->frm_dis_flg,
301 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300302 coda_write(dev, ctx->frame_mem_ctrl,
303 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300304 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
305 }
306
Philipp Zabel89548442014-07-11 06:36:17 -0300307 if (dev->devtype->product == CODA_960) {
308 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
309 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
310 }
311
Javier Martin186b2502012-07-26 05:53:35 -0300312 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
313
314 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
315 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300316 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
317
Javier Martin186b2502012-07-26 05:53:35 -0300318 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
319}
320
321static int coda_command_sync(struct coda_ctx *ctx, int cmd)
322{
323 struct coda_dev *dev = ctx->dev;
324
325 coda_command_async(ctx, cmd);
326 return coda_wait_timeout(dev);
327}
328
329static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
330 enum v4l2_buf_type type)
331{
332 switch (type) {
333 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
334 return &(ctx->q_data[V4L2_M2M_SRC]);
335 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
336 return &(ctx->q_data[V4L2_M2M_DST]);
337 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300338 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300339 }
Javier Martin186b2502012-07-26 05:53:35 -0300340}
341
342/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300343 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300344 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300345static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300346 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300347 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300348 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300349 },
350 {
351 .name = "YUV 4:2:0 Planar, YCrCb",
352 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300353 },
354 {
355 .name = "H264 Encoded Stream",
356 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300357 },
358 {
359 .name = "MPEG4 Encoded Stream",
360 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300361 },
362};
363
Philipp Zabelb96904e2013-05-23 10:42:58 -0300364#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
365 { mode, src_fourcc, dst_fourcc, max_w, max_h }
366
367/*
368 * Arrays of codecs supported by each given version of Coda:
369 * i.MX27 -> codadx6
370 * i.MX5x -> coda7
371 * i.MX6 -> coda960
372 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
373 */
374static struct coda_codec codadx6_codecs[] = {
375 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
376 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300377};
378
Philipp Zabelb96904e2013-05-23 10:42:58 -0300379static struct coda_codec coda7_codecs[] = {
380 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
381 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300382 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
383 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300384};
385
Philipp Zabel89548442014-07-11 06:36:17 -0300386static struct coda_codec coda9_codecs[] = {
387 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
388 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
389 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
390 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
391};
392
Philipp Zabelb96904e2013-05-23 10:42:58 -0300393static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300394{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300395 switch (fourcc) {
396 case V4L2_PIX_FMT_YUV420:
397 case V4L2_PIX_FMT_YVU420:
398 return true;
399 default:
400 return false;
401 }
402}
Javier Martin186b2502012-07-26 05:53:35 -0300403
Philipp Zabelb96904e2013-05-23 10:42:58 -0300404/*
405 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
406 * tables.
407 */
408static u32 coda_format_normalize_yuv(u32 fourcc)
409{
410 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
411}
412
413static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
414 int dst_fourcc)
415{
416 struct coda_codec *codecs = dev->devtype->codecs;
417 int num_codecs = dev->devtype->num_codecs;
418 int k;
419
420 src_fourcc = coda_format_normalize_yuv(src_fourcc);
421 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
422 if (src_fourcc == dst_fourcc)
423 return NULL;
424
425 for (k = 0; k < num_codecs; k++) {
426 if (codecs[k].src_fourcc == src_fourcc &&
427 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300428 break;
429 }
430
Philipp Zabelb96904e2013-05-23 10:42:58 -0300431 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300432 return NULL;
433
Philipp Zabelb96904e2013-05-23 10:42:58 -0300434 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300435}
436
Philipp Zabel57625592013-09-30 10:34:51 -0300437static void coda_get_max_dimensions(struct coda_dev *dev,
438 struct coda_codec *codec,
439 int *max_w, int *max_h)
440{
441 struct coda_codec *codecs = dev->devtype->codecs;
442 int num_codecs = dev->devtype->num_codecs;
443 unsigned int w, h;
444 int k;
445
446 if (codec) {
447 w = codec->max_w;
448 h = codec->max_h;
449 } else {
450 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
451 w = max(w, codecs[k].max_w);
452 h = max(h, codecs[k].max_h);
453 }
454 }
455
456 if (max_w)
457 *max_w = w;
458 if (max_h)
459 *max_h = h;
460}
461
Philipp Zabel927933f2013-09-30 10:34:48 -0300462static char *coda_product_name(int product)
463{
464 static char buf[9];
465
466 switch (product) {
467 case CODA_DX6:
468 return "CodaDx6";
469 case CODA_7541:
470 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300471 case CODA_960:
472 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300473 default:
474 snprintf(buf, sizeof(buf), "(0x%04x)", product);
475 return buf;
476 }
477}
478
Javier Martin186b2502012-07-26 05:53:35 -0300479/*
480 * V4L2 ioctl() operations.
481 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300482static int coda_querycap(struct file *file, void *priv,
483 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300484{
Philipp Zabel927933f2013-09-30 10:34:48 -0300485 struct coda_ctx *ctx = fh_to_ctx(priv);
486
Javier Martin186b2502012-07-26 05:53:35 -0300487 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300488 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
489 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300490 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300491 /*
492 * This is only a mem-to-mem video device. The capture and output
493 * device capability flags are left only for backward compatibility
494 * and are scheduled for removal.
495 */
496 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
497 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300498 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
499
500 return 0;
501}
502
503static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300504 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300505{
506 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300507 struct coda_codec *codecs = ctx->dev->devtype->codecs;
508 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300509 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300510 int num_codecs = ctx->dev->devtype->num_codecs;
511 int num_formats = ARRAY_SIZE(coda_formats);
512 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300513
514 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300515 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300516 if (coda_format_is_yuv(formats[i].fourcc) &&
517 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300518 if (num == f->index)
519 break;
520 ++num;
521 continue;
522 }
523 /* Compressed formats may be supported, check the codec list */
524 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300525 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300526 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300527 formats[i].fourcc == codecs[k].dst_fourcc &&
528 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300529 break;
530 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
531 formats[i].fourcc == codecs[k].src_fourcc)
532 break;
533 }
534 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300535 if (num == f->index)
536 break;
537 ++num;
538 }
539 }
540
541 if (i < num_formats) {
542 fmt = &formats[i];
543 strlcpy(f->description, fmt->name, sizeof(f->description));
544 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300545 if (!coda_format_is_yuv(fmt->fourcc))
546 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300547 return 0;
548 }
549
550 /* Format not found */
551 return -EINVAL;
552}
553
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300554static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
555 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300556{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300557 struct coda_ctx *ctx = fh_to_ctx(priv);
558 struct vb2_queue *src_vq;
559 struct coda_q_data *q_data_src;
560
561 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300562 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300563 if (vb2_is_streaming(src_vq)) {
564 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
565
566 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
567 q_data_src->fourcc);
568 }
569
570 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300571}
572
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300573static int coda_enum_fmt_vid_out(struct file *file, void *priv,
574 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300575{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300576 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300577}
578
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300579static int coda_g_fmt(struct file *file, void *priv,
580 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300581{
Javier Martin186b2502012-07-26 05:53:35 -0300582 struct coda_q_data *q_data;
583 struct coda_ctx *ctx = fh_to_ctx(priv);
584
Javier Martin186b2502012-07-26 05:53:35 -0300585 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300586 if (!q_data)
587 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300588
589 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300590 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300591 f->fmt.pix.width = q_data->width;
592 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300593 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300594 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
595 else /* encoded formats h.264/mpeg4 */
596 f->fmt.pix.bytesperline = 0;
597
598 f->fmt.pix.sizeimage = q_data->sizeimage;
599 f->fmt.pix.colorspace = ctx->colorspace;
600
601 return 0;
602}
603
Philipp Zabel57625592013-09-30 10:34:51 -0300604static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
605 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300606{
Philipp Zabel57625592013-09-30 10:34:51 -0300607 struct coda_dev *dev = ctx->dev;
608 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300609 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300610 enum v4l2_field field;
611
612 field = f->fmt.pix.field;
613 if (field == V4L2_FIELD_ANY)
614 field = V4L2_FIELD_NONE;
615 else if (V4L2_FIELD_NONE != field)
616 return -EINVAL;
617
618 /* V4L2 specification suggests the driver corrects the format struct
619 * if any of the dimensions is unsupported */
620 f->fmt.pix.field = field;
621
Philipp Zabel57625592013-09-30 10:34:51 -0300622 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
623 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
624 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
625 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300626
Philipp Zabel57625592013-09-30 10:34:51 -0300627 switch (f->fmt.pix.pixelformat) {
628 case V4L2_PIX_FMT_YUV420:
629 case V4L2_PIX_FMT_YVU420:
630 case V4L2_PIX_FMT_H264:
631 case V4L2_PIX_FMT_MPEG4:
632 case V4L2_PIX_FMT_JPEG:
633 break;
634 default:
635 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300636 if (!q_data)
637 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300638 f->fmt.pix.pixelformat = q_data->fourcc;
639 }
640
641 switch (f->fmt.pix.pixelformat) {
642 case V4L2_PIX_FMT_YUV420:
643 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300644 /* Frame stride must be multiple of 8 */
645 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
646 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300647 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300648 break;
649 case V4L2_PIX_FMT_H264:
650 case V4L2_PIX_FMT_MPEG4:
651 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300652 f->fmt.pix.bytesperline = 0;
653 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300654 break;
655 default:
656 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300657 }
658
659 return 0;
660}
661
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300662static int coda_try_fmt_vid_cap(struct file *file, void *priv,
663 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300664{
Javier Martin186b2502012-07-26 05:53:35 -0300665 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300666 struct coda_codec *codec;
667 struct vb2_queue *src_vq;
668 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300669
Philipp Zabel918c66f2013-06-27 06:59:01 -0300670 /*
671 * If the source format is already fixed, try to find a codec that
672 * converts to the given destination format
673 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300674 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300675 if (vb2_is_streaming(src_vq)) {
676 struct coda_q_data *q_data_src;
677
678 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
679 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
680 f->fmt.pix.pixelformat);
681 if (!codec)
682 return -EINVAL;
683 } else {
684 /* Otherwise determine codec by encoded format, if possible */
685 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
686 f->fmt.pix.pixelformat);
687 }
Javier Martin186b2502012-07-26 05:53:35 -0300688
689 f->fmt.pix.colorspace = ctx->colorspace;
690
Philipp Zabel57625592013-09-30 10:34:51 -0300691 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300692 if (ret < 0)
693 return ret;
694
695 /* The h.264 decoder only returns complete 16x16 macroblocks */
696 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
697 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
698 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
699 f->fmt.pix.bytesperline = f->fmt.pix.width;
700 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
701 f->fmt.pix.height * 3 / 2;
702 }
703
704 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300705}
706
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300707static int coda_try_fmt_vid_out(struct file *file, void *priv,
708 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300709{
710 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300711 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300712
Philipp Zabelb96904e2013-05-23 10:42:58 -0300713 /* Determine codec by encoded format, returns NULL if raw or invalid */
714 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
715 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300716
717 if (!f->fmt.pix.colorspace)
718 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
719
Philipp Zabel57625592013-09-30 10:34:51 -0300720 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300721}
722
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300723static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300724{
725 struct coda_q_data *q_data;
726 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300727
Philipp Zabel14604e32014-07-11 06:36:22 -0300728 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300729 if (!vq)
730 return -EINVAL;
731
732 q_data = get_q_data(ctx, f->type);
733 if (!q_data)
734 return -EINVAL;
735
736 if (vb2_is_busy(vq)) {
737 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
738 return -EBUSY;
739 }
740
Philipp Zabelb96904e2013-05-23 10:42:58 -0300741 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300742 q_data->width = f->fmt.pix.width;
743 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300744 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300745 q_data->rect.left = 0;
746 q_data->rect.top = 0;
747 q_data->rect.width = f->fmt.pix.width;
748 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300749
750 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
751 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300752 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300753
754 return 0;
755}
756
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300757static int coda_s_fmt_vid_cap(struct file *file, void *priv,
758 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300759{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300760 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300761 int ret;
762
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300763 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300764 if (ret)
765 return ret;
766
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300767 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300768}
769
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300770static int coda_s_fmt_vid_out(struct file *file, void *priv,
771 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300772{
773 struct coda_ctx *ctx = fh_to_ctx(priv);
774 int ret;
775
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300776 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300777 if (ret)
778 return ret;
779
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300780 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300781 if (ret)
782 ctx->colorspace = f->fmt.pix.colorspace;
783
784 return ret;
785}
786
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300787static int coda_qbuf(struct file *file, void *priv,
788 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300789{
790 struct coda_ctx *ctx = fh_to_ctx(priv);
791
Philipp Zabel14604e32014-07-11 06:36:22 -0300792 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300793}
794
Philipp Zabel918c66f2013-06-27 06:59:01 -0300795static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
796 struct v4l2_buffer *buf)
797{
798 struct vb2_queue *src_vq;
799
Philipp Zabel14604e32014-07-11 06:36:22 -0300800 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300801
802 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
803 (buf->sequence == (ctx->qsequence - 1)));
804}
805
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300806static int coda_dqbuf(struct file *file, void *priv,
807 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300808{
809 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300810 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300811
Philipp Zabel14604e32014-07-11 06:36:22 -0300812 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300813
814 /* If this is the last capture buffer, emit an end-of-stream event */
815 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
816 coda_buf_is_end_of_stream(ctx, buf)) {
817 const struct v4l2_event eos_event = {
818 .type = V4L2_EVENT_EOS
819 };
820
821 v4l2_event_queue_fh(&ctx->fh, &eos_event);
822 }
823
824 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300825}
826
Philipp Zabel52c41672014-07-11 06:36:19 -0300827static int coda_g_selection(struct file *file, void *fh,
828 struct v4l2_selection *s)
829{
830 struct coda_ctx *ctx = fh_to_ctx(fh);
831 struct coda_q_data *q_data;
832 struct v4l2_rect r, *rsel;
833
834 q_data = get_q_data(ctx, s->type);
835 if (!q_data)
836 return -EINVAL;
837
838 r.left = 0;
839 r.top = 0;
840 r.width = q_data->width;
841 r.height = q_data->height;
842 rsel = &q_data->rect;
843
844 switch (s->target) {
845 case V4L2_SEL_TGT_CROP_DEFAULT:
846 case V4L2_SEL_TGT_CROP_BOUNDS:
847 rsel = &r;
848 /* fallthrough */
849 case V4L2_SEL_TGT_CROP:
850 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
851 return -EINVAL;
852 break;
853 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
854 case V4L2_SEL_TGT_COMPOSE_PADDED:
855 rsel = &r;
856 /* fallthrough */
857 case V4L2_SEL_TGT_COMPOSE:
858 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
859 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
860 return -EINVAL;
861 break;
862 default:
863 return -EINVAL;
864 }
865
866 s->r = *rsel;
867
868 return 0;
869}
870
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300871static int coda_try_decoder_cmd(struct file *file, void *fh,
872 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300873{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300874 if (dc->cmd != V4L2_DEC_CMD_STOP)
875 return -EINVAL;
876
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300877 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300878 return -EINVAL;
879
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300880 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300881 return -EINVAL;
882
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300883 return 0;
884}
885
886static int coda_decoder_cmd(struct file *file, void *fh,
887 struct v4l2_decoder_cmd *dc)
888{
889 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300890 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300891 int ret;
892
893 ret = coda_try_decoder_cmd(file, fh, dc);
894 if (ret < 0)
895 return ret;
896
897 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300898 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300899 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300900
901 /* Set the strem-end flag on this context */
902 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
903
Philipp Zabel89548442014-07-11 06:36:17 -0300904 if ((dev->devtype->product == CODA_960) &&
905 coda_isbusy(dev) &&
906 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
907 /* If this context is currently running, update the hardware flag */
908 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
909 }
910
Philipp Zabel918c66f2013-06-27 06:59:01 -0300911 return 0;
912}
913
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300914static int coda_subscribe_event(struct v4l2_fh *fh,
915 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300916{
917 switch (sub->type) {
918 case V4L2_EVENT_EOS:
919 return v4l2_event_subscribe(fh, sub, 0, NULL);
920 default:
921 return v4l2_ctrl_subscribe_event(fh, sub);
922 }
Javier Martin186b2502012-07-26 05:53:35 -0300923}
924
925static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300926 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300927
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300928 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
929 .vidioc_g_fmt_vid_cap = coda_g_fmt,
930 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
931 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300932
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300933 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
934 .vidioc_g_fmt_vid_out = coda_g_fmt,
935 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
936 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300937
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300938 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
939 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300940
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300941 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300942 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300943 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300944 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300945
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300946 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
947 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300948
Philipp Zabel52c41672014-07-11 06:36:19 -0300949 .vidioc_g_selection = coda_g_selection,
950
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300951 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300952 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300953
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300954 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300955 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300956};
957
Philipp Zabel918c66f2013-06-27 06:59:01 -0300958static int coda_start_decoding(struct coda_ctx *ctx);
959
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300960static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
961{
962 return kfifo_len(&ctx->bitstream_fifo);
963}
964
965static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
966{
967 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
968 struct coda_dev *dev = ctx->dev;
969 u32 rd_ptr;
970
971 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
972 kfifo->out = (kfifo->in & ~kfifo->mask) |
973 (rd_ptr - ctx->bitstream.paddr);
974 if (kfifo->out > kfifo->in)
975 kfifo->out -= kfifo->mask + 1;
976}
977
978static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
979{
980 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
981 struct coda_dev *dev = ctx->dev;
982 u32 rd_ptr, wr_ptr;
983
984 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
985 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
986 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
987 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
988}
989
990static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
991{
992 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
993 struct coda_dev *dev = ctx->dev;
994 u32 wr_ptr;
995
996 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
997 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
998}
999
1000static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1001{
1002 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1003 u32 n;
1004
1005 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1006 if (n < src_size)
1007 return -ENOSPC;
1008
1009 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1010 ctx->bitstream.size, DMA_TO_DEVICE);
1011
1012 ctx->qsequence++;
1013
1014 return 0;
1015}
1016
1017static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1018 struct vb2_buffer *src_buf)
1019{
1020 int ret;
1021
1022 if (coda_get_bitstream_payload(ctx) +
1023 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1024 return false;
1025
1026 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1027 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1028 return true;
1029 }
1030
1031 ret = coda_bitstream_queue(ctx, src_buf);
1032 if (ret < 0) {
1033 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1034 return false;
1035 }
1036 /* Sync read pointer to device */
1037 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1038 coda_kfifo_sync_to_device_write(ctx);
1039
Philipp Zabel918c66f2013-06-27 06:59:01 -03001040 ctx->prescan_failed = false;
1041
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001042 return true;
1043}
1044
1045static void coda_fill_bitstream(struct coda_ctx *ctx)
1046{
1047 struct vb2_buffer *src_buf;
1048
Philipp Zabel14604e32014-07-11 06:36:22 -03001049 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1050 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001051
1052 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel14604e32014-07-11 06:36:22 -03001053 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001054 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1055 } else {
1056 break;
1057 }
1058 }
1059}
1060
Philipp Zabel89548442014-07-11 06:36:17 -03001061static void coda_set_gdi_regs(struct coda_ctx *ctx)
1062{
1063 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1064 struct coda_dev *dev = ctx->dev;
1065 int i;
1066
1067 for (i = 0; i < 16; i++)
1068 coda_write(dev, tiled_map->xy2ca_map[i],
1069 CODA9_GDI_XY2_CAS_0 + 4 * i);
1070 for (i = 0; i < 4; i++)
1071 coda_write(dev, tiled_map->xy2ba_map[i],
1072 CODA9_GDI_XY2_BA_0 + 4 * i);
1073 for (i = 0; i < 16; i++)
1074 coda_write(dev, tiled_map->xy2ra_map[i],
1075 CODA9_GDI_XY2_RAS_0 + 4 * i);
1076 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1077 for (i = 0; i < 32; i++)
1078 coda_write(dev, tiled_map->rbc2axi_map[i],
1079 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1080}
1081
Javier Martin186b2502012-07-26 05:53:35 -03001082/*
1083 * Mem-to-mem operations.
1084 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001085static int coda_prepare_decode(struct coda_ctx *ctx)
1086{
1087 struct vb2_buffer *dst_buf;
1088 struct coda_dev *dev = ctx->dev;
1089 struct coda_q_data *q_data_dst;
1090 u32 stridey, height;
1091 u32 picture_y, picture_cb, picture_cr;
1092
Philipp Zabel14604e32014-07-11 06:36:22 -03001093 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001094 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1095
1096 if (ctx->params.rot_mode & CODA_ROT_90) {
1097 stridey = q_data_dst->height;
1098 height = q_data_dst->width;
1099 } else {
1100 stridey = q_data_dst->width;
1101 height = q_data_dst->height;
1102 }
1103
1104 /* Try to copy source buffer contents into the bitstream ringbuffer */
1105 mutex_lock(&ctx->bitstream_mutex);
1106 coda_fill_bitstream(ctx);
1107 mutex_unlock(&ctx->bitstream_mutex);
1108
1109 if (coda_get_bitstream_payload(ctx) < 512 &&
1110 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1111 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1112 "bitstream payload: %d, skipping\n",
1113 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001114 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001115 return -EAGAIN;
1116 }
1117
1118 /* Run coda_start_decoding (again) if not yet initialized */
1119 if (!ctx->initialized) {
1120 int ret = coda_start_decoding(ctx);
1121 if (ret < 0) {
1122 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001123 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001124 return -EAGAIN;
1125 } else {
1126 ctx->initialized = 1;
1127 }
1128 }
1129
Philipp Zabel89548442014-07-11 06:36:17 -03001130 if (dev->devtype->product == CODA_960)
1131 coda_set_gdi_regs(ctx);
1132
Philipp Zabel918c66f2013-06-27 06:59:01 -03001133 /* Set rotator output */
1134 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1135 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1136 /* Switch Cr and Cb for YVU420 format */
1137 picture_cr = picture_y + stridey * height;
1138 picture_cb = picture_cr + stridey / 2 * height / 2;
1139 } else {
1140 picture_cb = picture_y + stridey * height;
1141 picture_cr = picture_cb + stridey / 2 * height / 2;
1142 }
Philipp Zabel89548442014-07-11 06:36:17 -03001143
1144 if (dev->devtype->product == CODA_960) {
1145 /*
1146 * The CODA960 seems to have an internal list of buffers with
1147 * 64 entries that includes the registered frame buffers as
1148 * well as the rotator buffer output.
1149 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1150 */
1151 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1152 CODA9_CMD_DEC_PIC_ROT_INDEX);
1153 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1154 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1155 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1156 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1157 } else {
1158 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1159 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1160 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1161 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1162 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001163 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1164 CODA_CMD_DEC_PIC_ROT_MODE);
1165
1166 switch (dev->devtype->product) {
1167 case CODA_DX6:
1168 /* TBD */
1169 case CODA_7541:
1170 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1171 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001172 case CODA_960:
1173 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1174 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001175 }
1176
1177 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1178
1179 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1180 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1181
1182 return 0;
1183}
1184
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001185static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001186{
Javier Martin186b2502012-07-26 05:53:35 -03001187 struct coda_q_data *q_data_src, *q_data_dst;
1188 struct vb2_buffer *src_buf, *dst_buf;
1189 struct coda_dev *dev = ctx->dev;
1190 int force_ipicture;
1191 int quant_param = 0;
1192 u32 picture_y, picture_cb, picture_cr;
1193 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1194 u32 dst_fourcc;
1195
Philipp Zabel14604e32014-07-11 06:36:22 -03001196 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1197 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001198 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1199 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001200 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001201
Philipp Zabel918c66f2013-06-27 06:59:01 -03001202 src_buf->v4l2_buf.sequence = ctx->osequence;
1203 dst_buf->v4l2_buf.sequence = ctx->osequence;
1204 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001205
1206 /*
1207 * Workaround coda firmware BUG that only marks the first
1208 * frame as IDR. This is a problem for some decoders that can't
1209 * recover when a frame is lost.
1210 */
1211 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1212 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1213 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1214 } else {
1215 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1216 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1217 }
1218
Philipp Zabel89548442014-07-11 06:36:17 -03001219 if (dev->devtype->product == CODA_960)
1220 coda_set_gdi_regs(ctx);
1221
Javier Martin186b2502012-07-26 05:53:35 -03001222 /*
1223 * Copy headers at the beginning of the first frame for H.264 only.
1224 * In MPEG4 they are already copied by the coda.
1225 */
1226 if (src_buf->v4l2_buf.sequence == 0) {
1227 pic_stream_buffer_addr =
1228 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1229 ctx->vpu_header_size[0] +
1230 ctx->vpu_header_size[1] +
1231 ctx->vpu_header_size[2];
1232 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1233 ctx->vpu_header_size[0] -
1234 ctx->vpu_header_size[1] -
1235 ctx->vpu_header_size[2];
1236 memcpy(vb2_plane_vaddr(dst_buf, 0),
1237 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1238 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1239 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1240 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1241 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1242 ctx->vpu_header_size[2]);
1243 } else {
1244 pic_stream_buffer_addr =
1245 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1246 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1247 }
1248
1249 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1250 force_ipicture = 1;
1251 switch (dst_fourcc) {
1252 case V4L2_PIX_FMT_H264:
1253 quant_param = ctx->params.h264_intra_qp;
1254 break;
1255 case V4L2_PIX_FMT_MPEG4:
1256 quant_param = ctx->params.mpeg4_intra_qp;
1257 break;
1258 default:
1259 v4l2_warn(&ctx->dev->v4l2_dev,
1260 "cannot set intra qp, fmt not supported\n");
1261 break;
1262 }
1263 } else {
1264 force_ipicture = 0;
1265 switch (dst_fourcc) {
1266 case V4L2_PIX_FMT_H264:
1267 quant_param = ctx->params.h264_inter_qp;
1268 break;
1269 case V4L2_PIX_FMT_MPEG4:
1270 quant_param = ctx->params.mpeg4_inter_qp;
1271 break;
1272 default:
1273 v4l2_warn(&ctx->dev->v4l2_dev,
1274 "cannot set inter qp, fmt not supported\n");
1275 break;
1276 }
1277 }
1278
1279 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001280 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001281 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1282
1283
1284 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001285 switch (q_data_src->fourcc) {
1286 case V4L2_PIX_FMT_YVU420:
1287 /* Switch Cb and Cr for YVU420 format */
1288 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1289 picture_cb = picture_cr + q_data_src->width / 2 *
1290 q_data_src->height / 2;
1291 break;
1292 case V4L2_PIX_FMT_YUV420:
1293 default:
1294 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1295 picture_cr = picture_cb + q_data_src->width / 2 *
1296 q_data_src->height / 2;
1297 break;
1298 }
Javier Martin186b2502012-07-26 05:53:35 -03001299
Philipp Zabel89548442014-07-11 06:36:17 -03001300 if (dev->devtype->product == CODA_960) {
1301 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1302 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1303 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1304
1305 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1306 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1307 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1308 } else {
1309 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1310 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1311 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1312 }
Javier Martin186b2502012-07-26 05:53:35 -03001313 coda_write(dev, force_ipicture << 1 & 0x2,
1314 CODA_CMD_ENC_PIC_OPTION);
1315
1316 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1317 coda_write(dev, pic_stream_buffer_size / 1024,
1318 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001319
1320 if (!ctx->streamon_out) {
1321 /* After streamoff on the output side, set the stream end flag */
1322 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1323 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1324 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001325}
1326
1327static void coda_device_run(void *m2m_priv)
1328{
1329 struct coda_ctx *ctx = m2m_priv;
1330 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001331
1332 queue_work(dev->workqueue, &ctx->pic_run_work);
1333}
1334
1335static void coda_free_framebuffers(struct coda_ctx *ctx);
1336static void coda_free_context_buffers(struct coda_ctx *ctx);
1337
1338static void coda_seq_end_work(struct work_struct *work)
1339{
1340 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1341 struct coda_dev *dev = ctx->dev;
1342
1343 mutex_lock(&ctx->buffer_mutex);
1344 mutex_lock(&dev->coda_mutex);
1345
1346 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1347 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1348 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1349 v4l2_err(&dev->v4l2_dev,
1350 "CODA_COMMAND_SEQ_END failed\n");
1351 }
1352
1353 kfifo_init(&ctx->bitstream_fifo,
1354 ctx->bitstream.vaddr, ctx->bitstream.size);
1355
1356 coda_free_framebuffers(ctx);
1357 coda_free_context_buffers(ctx);
1358
1359 mutex_unlock(&dev->coda_mutex);
1360 mutex_unlock(&ctx->buffer_mutex);
1361}
1362
1363static void coda_finish_decode(struct coda_ctx *ctx);
1364static void coda_finish_encode(struct coda_ctx *ctx);
1365
1366static void coda_pic_run_work(struct work_struct *work)
1367{
1368 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1369 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001370 int ret;
1371
1372 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001373 mutex_lock(&dev->coda_mutex);
1374
Philipp Zabel918c66f2013-06-27 06:59:01 -03001375 if (ctx->inst_type == CODA_INST_DECODER) {
1376 ret = coda_prepare_decode(ctx);
1377 if (ret < 0) {
1378 mutex_unlock(&dev->coda_mutex);
1379 mutex_unlock(&ctx->buffer_mutex);
1380 /* job_finish scheduled by prepare_decode */
1381 return;
1382 }
1383 } else {
1384 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001385 }
1386
Philipp Zabelc2d22512013-06-21 03:55:28 -03001387 if (dev->devtype->product != CODA_DX6)
1388 coda_write(dev, ctx->iram_info.axi_sram_use,
1389 CODA7_REG_BIT_AXI_SRAM_USE);
1390
Philipp Zabel918c66f2013-06-27 06:59:01 -03001391 if (ctx->inst_type == CODA_INST_DECODER)
1392 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001393 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001394
1395 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1396 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1397 } else if (!ctx->aborting) {
1398 if (ctx->inst_type == CODA_INST_DECODER)
1399 coda_finish_decode(ctx);
1400 else
1401 coda_finish_encode(ctx);
1402 }
1403
1404 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1405 queue_work(dev->workqueue, &ctx->seq_end_work);
1406
1407 mutex_unlock(&dev->coda_mutex);
1408 mutex_unlock(&ctx->buffer_mutex);
1409
Philipp Zabel14604e32014-07-11 06:36:22 -03001410 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001411}
1412
1413static int coda_job_ready(void *m2m_priv)
1414{
1415 struct coda_ctx *ctx = m2m_priv;
1416
1417 /*
1418 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001419 * and 1 frame are needed. In the decoder case,
1420 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001421 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001422 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001423 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001424 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1425 "not ready: not enough video buffers.\n");
1426 return 0;
1427 }
1428
Philipp Zabel14604e32014-07-11 06:36:22 -03001429 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001430 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1431 "not ready: not enough video capture buffers.\n");
1432 return 0;
1433 }
1434
Philipp Zabel918c66f2013-06-27 06:59:01 -03001435 if (ctx->prescan_failed ||
1436 ((ctx->inst_type == CODA_INST_DECODER) &&
1437 (coda_get_bitstream_payload(ctx) < 512) &&
1438 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1439 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1440 "%d: not ready: not enough bitstream data.\n",
1441 ctx->idx);
1442 return 0;
1443 }
1444
Philipp Zabel3e748262013-05-23 10:43:01 -03001445 if (ctx->aborting) {
1446 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1447 "not ready: aborting\n");
1448 return 0;
1449 }
1450
Javier Martin186b2502012-07-26 05:53:35 -03001451 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1452 "job ready\n");
1453 return 1;
1454}
1455
1456static void coda_job_abort(void *priv)
1457{
1458 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001459
1460 ctx->aborting = 1;
1461
1462 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1463 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001464}
1465
1466static void coda_lock(void *m2m_priv)
1467{
1468 struct coda_ctx *ctx = m2m_priv;
1469 struct coda_dev *pcdev = ctx->dev;
1470 mutex_lock(&pcdev->dev_mutex);
1471}
1472
1473static void coda_unlock(void *m2m_priv)
1474{
1475 struct coda_ctx *ctx = m2m_priv;
1476 struct coda_dev *pcdev = ctx->dev;
1477 mutex_unlock(&pcdev->dev_mutex);
1478}
1479
1480static struct v4l2_m2m_ops coda_m2m_ops = {
1481 .device_run = coda_device_run,
1482 .job_ready = coda_job_ready,
1483 .job_abort = coda_job_abort,
1484 .lock = coda_lock,
1485 .unlock = coda_unlock,
1486};
1487
Philipp Zabel89548442014-07-11 06:36:17 -03001488static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1489{
1490 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1491 int luma_map, chro_map, i;
1492
1493 memset(tiled_map, 0, sizeof(*tiled_map));
1494
1495 luma_map = 64;
1496 chro_map = 64;
1497 tiled_map->map_type = tiled_map_type;
1498 for (i = 0; i < 16; i++)
1499 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1500 for (i = 0; i < 4; i++)
1501 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1502 for (i = 0; i < 16; i++)
1503 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1504
1505 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1506 tiled_map->xy2rbc_config = 0;
1507 } else {
1508 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1509 tiled_map_type);
1510 return;
1511 }
1512}
1513
Javier Martin186b2502012-07-26 05:53:35 -03001514static void set_default_params(struct coda_ctx *ctx)
1515{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001516 int max_w;
1517 int max_h;
1518
1519 ctx->codec = &ctx->dev->devtype->codecs[0];
1520 max_w = ctx->codec->max_w;
1521 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001522
1523 ctx->params.codec_mode = CODA_MODE_INVALID;
1524 ctx->colorspace = V4L2_COLORSPACE_REC709;
1525 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001526 ctx->aborting = 0;
1527
1528 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001529 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1530 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1531 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1532 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1533 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1534 ctx->q_data[V4L2_M2M_DST].width = max_w;
1535 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001536 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001537 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1538 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1539 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1540 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001541
1542 if (ctx->dev->devtype->product == CODA_960)
1543 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001544}
1545
1546/*
1547 * Queue operations
1548 */
1549static int coda_queue_setup(struct vb2_queue *vq,
1550 const struct v4l2_format *fmt,
1551 unsigned int *nbuffers, unsigned int *nplanes,
1552 unsigned int sizes[], void *alloc_ctxs[])
1553{
1554 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001555 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001556 unsigned int size;
1557
Philipp Zabele34db062012-08-29 08:22:00 -03001558 q_data = get_q_data(ctx, vq->type);
1559 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001560
1561 *nplanes = 1;
1562 sizes[0] = size;
1563
1564 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1565
1566 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1567 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1568
1569 return 0;
1570}
1571
1572static int coda_buf_prepare(struct vb2_buffer *vb)
1573{
1574 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1575 struct coda_q_data *q_data;
1576
1577 q_data = get_q_data(ctx, vb->vb2_queue->type);
1578
1579 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1580 v4l2_warn(&ctx->dev->v4l2_dev,
1581 "%s data will not fit into plane (%lu < %lu)\n",
1582 __func__, vb2_plane_size(vb, 0),
1583 (long)q_data->sizeimage);
1584 return -EINVAL;
1585 }
1586
Javier Martin186b2502012-07-26 05:53:35 -03001587 return 0;
1588}
1589
1590static void coda_buf_queue(struct vb2_buffer *vb)
1591{
1592 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001593 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001594 struct coda_q_data *q_data;
1595
1596 q_data = get_q_data(ctx, vb->vb2_queue->type);
1597
1598 /*
1599 * In the decoder case, immediately try to copy the buffer into the
1600 * bitstream ringbuffer and mark it as ready to be dequeued.
1601 */
1602 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1603 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1604 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001605 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001606 * the stream end
1607 */
Philipp Zabel89548442014-07-11 06:36:17 -03001608 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001609 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001610 if ((dev->devtype->product == CODA_960) &&
1611 coda_isbusy(dev) &&
1612 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1613 /* if this decoder instance is running, set the stream end flag */
1614 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1615 }
1616 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001617 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001618 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001619 coda_fill_bitstream(ctx);
1620 mutex_unlock(&ctx->bitstream_mutex);
1621 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001622 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001623 }
Javier Martin186b2502012-07-26 05:53:35 -03001624}
1625
Philipp Zabel86eda902013-05-23 10:42:57 -03001626static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1627{
1628 struct coda_dev *dev = ctx->dev;
1629 u32 *p = ctx->parabuf.vaddr;
1630
1631 if (dev->devtype->product == CODA_DX6)
1632 p[index] = value;
1633 else
1634 p[index ^ 1] = value;
1635}
1636
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001637static int coda_alloc_aux_buf(struct coda_dev *dev,
1638 struct coda_aux_buf *buf, size_t size)
1639{
1640 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1641 GFP_KERNEL);
1642 if (!buf->vaddr)
1643 return -ENOMEM;
1644
1645 buf->size = size;
1646
1647 return 0;
1648}
1649
1650static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1651 struct coda_aux_buf *buf, size_t size)
1652{
1653 return coda_alloc_aux_buf(ctx->dev, buf, size);
1654}
1655
1656static void coda_free_aux_buf(struct coda_dev *dev,
1657 struct coda_aux_buf *buf)
1658{
1659 if (buf->vaddr) {
1660 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1661 buf->vaddr, buf->paddr);
1662 buf->vaddr = NULL;
1663 buf->size = 0;
1664 }
1665}
1666
1667static void coda_free_framebuffers(struct coda_ctx *ctx)
1668{
1669 int i;
1670
1671 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1672 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1673}
1674
Philipp Zabelec25f682012-07-20 08:54:29 -03001675static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1676{
1677 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001678 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001679 dma_addr_t paddr;
1680 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001681 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001682 int i;
1683
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001684 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1685 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001686 ysize = round_up(q_data->width, 8) * height;
1687
Philipp Zabelec25f682012-07-20 08:54:29 -03001688 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001689 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001690 size_t size;
1691
Philipp Zabelaed14b02014-07-11 06:36:15 -03001692 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001693 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1694 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001695 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001696 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1697 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001698 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001699 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001700 }
1701 }
1702
1703 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001704 for (i = 0; i < ctx->num_internal_frames; i++) {
1705 paddr = ctx->internal_frames[i].paddr;
1706 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1707 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1708 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001709
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001710 /* mvcol buffer for h.264 */
1711 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1712 dev->devtype->product != CODA_DX6)
1713 coda_parabuf_write(ctx, 96 + i,
1714 ctx->internal_frames[i].paddr +
1715 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001716 }
1717
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001718 /* mvcol buffer for mpeg4 */
1719 if ((dev->devtype->product != CODA_DX6) &&
1720 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1721 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1722 ysize + ysize/4 + ysize/4);
1723
Philipp Zabelec25f682012-07-20 08:54:29 -03001724 return 0;
1725}
1726
Javier Martin3f3f5c72012-10-29 05:20:29 -03001727static int coda_h264_padding(int size, char *p)
1728{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001729 int nal_size;
1730 int diff;
1731
Javier Martin832fbb52012-10-29 08:34:59 -03001732 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001733 if (diff == 0)
1734 return 0;
1735
Javier Martin832fbb52012-10-29 08:34:59 -03001736 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001737 memcpy(p, coda_filler_nal, nal_size);
1738
1739 /* Add rbsp stop bit and trailing at the end */
1740 *(p + nal_size - 1) = 0x80;
1741
1742 return nal_size;
1743}
1744
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001745static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1746{
1747 phys_addr_t ret;
1748
1749 size = round_up(size, 1024);
1750 if (size > iram->remaining)
1751 return 0;
1752 iram->remaining -= size;
1753
1754 ret = iram->next_paddr;
1755 iram->next_paddr += size;
1756
1757 return ret;
1758}
1759
Philipp Zabelc2d22512013-06-21 03:55:28 -03001760static void coda_setup_iram(struct coda_ctx *ctx)
1761{
1762 struct coda_iram_info *iram_info = &ctx->iram_info;
1763 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001764 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001765 int dbk_bits;
1766 int bit_bits;
1767 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001768
1769 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001770 iram_info->next_paddr = dev->iram.paddr;
1771 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001772
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001773 switch (dev->devtype->product) {
1774 case CODA_7541:
1775 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1776 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1777 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1778 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001779 case CODA_960:
1780 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1781 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1782 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1783 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001784 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001785 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001786 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001787
1788 if (ctx->inst_type == CODA_INST_ENCODER) {
1789 struct coda_q_data *q_data_src;
1790
1791 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1792 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1793
1794 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001795 if (dev->devtype->product == CODA_7541) {
1796 iram_info->search_ram_size = round_up(mb_width * 16 *
1797 36 + 2048, 1024);
1798 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1799 iram_info->search_ram_size);
1800 if (!iram_info->search_ram_paddr) {
1801 pr_err("IRAM is smaller than the search ram size\n");
1802 goto out;
1803 }
1804 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1805 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001806 }
1807
1808 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001809 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1810 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1811 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001812 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001813 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001814
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001815 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1816 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001817 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001818 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001819
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001820 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1821 if (!iram_info->buf_ip_ac_dc_use)
1822 goto out;
1823 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001824
Philipp Zabel8358e762013-06-21 03:55:32 -03001825 /* OVL and BTP disabled for encoder */
1826 } else if (ctx->inst_type == CODA_INST_DECODER) {
1827 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001828
1829 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1830 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001831
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001832 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1833 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1834 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001835 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001836 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001837
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001838 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1839 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001840 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001841 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001842
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001843 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1844 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001845 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001846 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001847
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001848 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001849 }
1850
1851out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001852 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1853 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1854 "IRAM smaller than needed\n");
1855
1856 if (dev->devtype->product == CODA_7541) {
1857 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001858 if (ctx->inst_type == CODA_INST_DECODER) {
1859 /* fw 1.4.50 */
1860 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1861 CODA7_USE_IP_ENABLE);
1862 } else {
1863 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001864 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1865 CODA7_USE_HOST_DBK_ENABLE |
1866 CODA7_USE_IP_ENABLE |
1867 CODA7_USE_DBK_ENABLE);
1868 }
1869 }
1870}
1871
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001872static void coda_free_context_buffers(struct coda_ctx *ctx)
1873{
1874 struct coda_dev *dev = ctx->dev;
1875
Philipp Zabel918c66f2013-06-27 06:59:01 -03001876 coda_free_aux_buf(dev, &ctx->slicebuf);
1877 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001878 if (dev->devtype->product != CODA_DX6)
1879 coda_free_aux_buf(dev, &ctx->workbuf);
1880}
1881
1882static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1883 struct coda_q_data *q_data)
1884{
1885 struct coda_dev *dev = ctx->dev;
1886 size_t size;
1887 int ret;
1888
1889 switch (dev->devtype->product) {
1890 case CODA_7541:
1891 size = CODA7_WORK_BUF_SIZE;
1892 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001893 case CODA_960:
1894 size = CODA9_WORK_BUF_SIZE;
1895 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1896 size += CODA9_PS_SAVE_SIZE;
1897 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001898 default:
1899 return 0;
1900 }
1901
Philipp Zabel918c66f2013-06-27 06:59:01 -03001902 if (ctx->psbuf.vaddr) {
1903 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1904 return -EBUSY;
1905 }
1906 if (ctx->slicebuf.vaddr) {
1907 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1908 return -EBUSY;
1909 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001910 if (ctx->workbuf.vaddr) {
1911 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1912 ret = -EBUSY;
1913 return -ENOMEM;
1914 }
1915
Philipp Zabel918c66f2013-06-27 06:59:01 -03001916 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1917 /* worst case slice size */
1918 size = (DIV_ROUND_UP(q_data->width, 16) *
1919 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1920 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1921 if (ret < 0) {
1922 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1923 ctx->slicebuf.size);
1924 return ret;
1925 }
1926 }
1927
1928 if (dev->devtype->product == CODA_7541) {
1929 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1930 if (ret < 0) {
1931 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1932 goto err;
1933 }
1934 }
1935
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001936 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1937 if (ret < 0) {
1938 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1939 ctx->workbuf.size);
1940 goto err;
1941 }
1942
1943 return 0;
1944
1945err:
1946 coda_free_context_buffers(ctx);
1947 return ret;
1948}
1949
Philipp Zabel918c66f2013-06-27 06:59:01 -03001950static int coda_start_decoding(struct coda_ctx *ctx)
1951{
1952 struct coda_q_data *q_data_src, *q_data_dst;
1953 u32 bitstream_buf, bitstream_size;
1954 struct coda_dev *dev = ctx->dev;
1955 int width, height;
1956 u32 src_fourcc;
1957 u32 val;
1958 int ret;
1959
1960 /* Start decoding */
1961 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1962 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1963 bitstream_buf = ctx->bitstream.paddr;
1964 bitstream_size = ctx->bitstream.size;
1965 src_fourcc = q_data_src->fourcc;
1966
1967 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1968
1969 /* Update coda bitstream read and write pointers from kfifo */
1970 coda_kfifo_sync_to_device_full(ctx);
1971
1972 ctx->display_idx = -1;
1973 ctx->frm_dis_flg = 0;
1974 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1975
1976 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1977 CODA_REG_BIT_BIT_STREAM_PARAM);
1978
1979 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1980 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1981 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001982 if ((dev->devtype->product == CODA_7541) ||
1983 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03001984 val |= CODA_REORDER_ENABLE;
1985 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1986
1987 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03001988 if (dev->devtype->product == CODA_960 &&
1989 src_fourcc == V4L2_PIX_FMT_MPEG4)
1990 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1991 else
1992 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001993 if (src_fourcc == V4L2_PIX_FMT_H264) {
1994 if (dev->devtype->product == CODA_7541) {
1995 coda_write(dev, ctx->psbuf.paddr,
1996 CODA_CMD_DEC_SEQ_PS_BB_START);
1997 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1998 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1999 }
Philipp Zabel89548442014-07-11 06:36:17 -03002000 if (dev->devtype->product == CODA_960) {
2001 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2002 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2003 }
2004 }
2005 if (dev->devtype->product != CODA_960) {
2006 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002007 }
2008
2009 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2010 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2011 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2012 return -ETIMEDOUT;
2013 }
2014
2015 /* Update kfifo out pointer from coda bitstream read pointer */
2016 coda_kfifo_sync_from_device(ctx);
2017
2018 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2019
2020 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2021 v4l2_err(&dev->v4l2_dev,
2022 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2023 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2024 return -EAGAIN;
2025 }
2026
2027 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2028 if (dev->devtype->product == CODA_DX6) {
2029 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2030 height = val & CODADX6_PICHEIGHT_MASK;
2031 } else {
2032 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2033 height = val & CODA7_PICHEIGHT_MASK;
2034 }
2035
2036 if (width > q_data_dst->width || height > q_data_dst->height) {
2037 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2038 width, height, q_data_dst->width, q_data_dst->height);
2039 return -EINVAL;
2040 }
2041
2042 width = round_up(width, 16);
2043 height = round_up(height, 16);
2044
2045 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2046 __func__, ctx->idx, width, height);
2047
Philipp Zabela500a932014-07-11 06:36:13 -03002048 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002049 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2050 v4l2_err(&dev->v4l2_dev,
2051 "not enough framebuffers to decode (%d < %d)\n",
2052 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2053 return -EINVAL;
2054 }
2055
Philipp Zabel52c41672014-07-11 06:36:19 -03002056 if (src_fourcc == V4L2_PIX_FMT_H264) {
2057 u32 left_right;
2058 u32 top_bottom;
2059
2060 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2061 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2062
2063 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2064 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2065 q_data_dst->rect.width = width - q_data_dst->rect.left -
2066 (left_right & 0x3ff);
2067 q_data_dst->rect.height = height - q_data_dst->rect.top -
2068 (top_bottom & 0x3ff);
2069 }
2070
Philipp Zabel918c66f2013-06-27 06:59:01 -03002071 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2072 if (ret < 0)
2073 return ret;
2074
2075 /* Tell the decoder how many frame buffers we allocated. */
2076 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2077 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2078
2079 if (dev->devtype->product != CODA_DX6) {
2080 /* Set secondary AXI IRAM */
2081 coda_setup_iram(ctx);
2082
2083 coda_write(dev, ctx->iram_info.buf_bit_use,
2084 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2085 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2086 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2087 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2088 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2089 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2090 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2091 coda_write(dev, ctx->iram_info.buf_ovl_use,
2092 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002093 if (dev->devtype->product == CODA_960)
2094 coda_write(dev, ctx->iram_info.buf_btp_use,
2095 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2096 }
2097
2098 if (dev->devtype->product == CODA_960) {
2099 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2100
2101 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2102 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2103 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2104 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2105 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2106 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002107 }
2108
2109 if (src_fourcc == V4L2_PIX_FMT_H264) {
2110 coda_write(dev, ctx->slicebuf.paddr,
2111 CODA_CMD_SET_FRAME_SLICE_BB_START);
2112 coda_write(dev, ctx->slicebuf.size / 1024,
2113 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2114 }
2115
2116 if (dev->devtype->product == CODA_7541) {
2117 int max_mb_x = 1920 / 16;
2118 int max_mb_y = 1088 / 16;
2119 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002120
Philipp Zabel918c66f2013-06-27 06:59:01 -03002121 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2122 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002123 } else if (dev->devtype->product == CODA_960) {
2124 int max_mb_x = 1920 / 16;
2125 int max_mb_y = 1088 / 16;
2126 int max_mb_num = max_mb_x * max_mb_y;
2127
2128 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2129 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002130 }
2131
2132 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2133 v4l2_err(&ctx->dev->v4l2_dev,
2134 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2135 return -ETIMEDOUT;
2136 }
2137
2138 return 0;
2139}
2140
Philipp Zabeld35167a2013-05-23 10:42:59 -03002141static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2142 int header_code, u8 *header, int *size)
2143{
2144 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002145 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002146 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002147 int i;
2148
2149 if (dev->devtype->product == CODA_960)
2150 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002151
2152 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2153 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002154 bufsize = vb2_plane_size(buf, 0);
2155 if (dev->devtype->product == CODA_960)
2156 bufsize /= 1024;
2157 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002158 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2159 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2160 if (ret < 0) {
2161 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2162 return ret;
2163 }
Philipp Zabel89548442014-07-11 06:36:17 -03002164
2165 if (dev->devtype->product == CODA_960) {
2166 for (i = 63; i > 0; i--)
2167 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2168 break;
2169 *size = i + 1;
2170 } else {
2171 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2172 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2173 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002174 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2175
2176 return 0;
2177}
2178
Philipp Zabel89548442014-07-11 06:36:17 -03002179static int coda_start_encoding(struct coda_ctx *ctx);
2180
Javier Martin186b2502012-07-26 05:53:35 -03002181static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2182{
2183 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2184 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002185 struct coda_dev *dev = ctx->dev;
2186 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002187 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002188 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002189
Philipp Zabelb96904e2013-05-23 10:42:58 -03002190 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002191 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2192 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2193 if (coda_get_bitstream_payload(ctx) < 512)
2194 return -EINVAL;
2195 } else {
2196 if (count < 1)
2197 return -EINVAL;
2198 }
2199
2200 ctx->streamon_out = 1;
2201
Philipp Zabelb96904e2013-05-23 10:42:58 -03002202 if (coda_format_is_yuv(q_data_src->fourcc))
2203 ctx->inst_type = CODA_INST_ENCODER;
2204 else
2205 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002206 } else {
2207 if (count < 1)
2208 return -EINVAL;
2209
2210 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002211 }
Javier Martin186b2502012-07-26 05:53:35 -03002212
Philipp Zabelb96904e2013-05-23 10:42:58 -03002213 /* Don't start the coda unless both queues are on */
2214 if (!(ctx->streamon_out & ctx->streamon_cap))
2215 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002216
Philipp Zabeleb107512013-09-30 10:34:45 -03002217 /* Allow decoder device_run with no new buffers queued */
2218 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002219 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002220
Philipp Zabelb96904e2013-05-23 10:42:58 -03002221 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002222 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002223 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002224
Philipp Zabelb96904e2013-05-23 10:42:58 -03002225 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2226 q_data_dst->fourcc);
2227 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002228 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2229 return -EINVAL;
2230 }
2231
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002232 /* Allocate per-instance buffers */
2233 ret = coda_alloc_context_buffers(ctx, q_data_src);
2234 if (ret < 0)
2235 return ret;
2236
Philipp Zabel918c66f2013-06-27 06:59:01 -03002237 if (ctx->inst_type == CODA_INST_DECODER) {
2238 mutex_lock(&dev->coda_mutex);
2239 ret = coda_start_decoding(ctx);
2240 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002241 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002242 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002243 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002244 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002245 } else {
2246 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002247 }
2248
Philipp Zabel89548442014-07-11 06:36:17 -03002249 ctx->initialized = 1;
2250 return ret;
2251}
2252
2253static int coda_start_encoding(struct coda_ctx *ctx)
2254{
2255 struct coda_dev *dev = ctx->dev;
2256 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2257 struct coda_q_data *q_data_src, *q_data_dst;
2258 u32 bitstream_buf, bitstream_size;
2259 struct vb2_buffer *buf;
2260 int gamma, ret, value;
2261 u32 dst_fourcc;
2262
2263 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2264 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2265 dst_fourcc = q_data_dst->fourcc;
2266
Philipp Zabel14604e32014-07-11 06:36:22 -03002267 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002268 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2269 bitstream_size = q_data_dst->sizeimage;
2270
Javier Martin186b2502012-07-26 05:53:35 -03002271 if (!coda_is_initialized(dev)) {
2272 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2273 return -EFAULT;
2274 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002275
2276 mutex_lock(&dev->coda_mutex);
2277
Javier Martin186b2502012-07-26 05:53:35 -03002278 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002279 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2280 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002281 switch (dev->devtype->product) {
2282 case CODA_DX6:
2283 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2284 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2285 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002286 case CODA_960:
2287 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2288 /* fallthrough */
2289 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002290 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2291 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002292 break;
Javier Martin186b2502012-07-26 05:53:35 -03002293 }
2294
Philipp Zabel89548442014-07-11 06:36:17 -03002295 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2296 value &= ~(1 << 2 | 0x7 << 9);
2297 ctx->frame_mem_ctrl = value;
2298 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2299
Philipp Zabel10436672012-07-02 09:03:55 -03002300 if (dev->devtype->product == CODA_DX6) {
2301 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002302 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002303 }
Javier Martin186b2502012-07-26 05:53:35 -03002304
2305 /* Could set rotation here if needed */
2306 switch (dev->devtype->product) {
2307 case CODA_DX6:
2308 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002309 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002310 break;
2311 default:
2312 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002313 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002314 }
Javier Martin186b2502012-07-26 05:53:35 -03002315 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2316 coda_write(dev, ctx->params.framerate,
2317 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2318
Philipp Zabelb96904e2013-05-23 10:42:58 -03002319 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002320 switch (dst_fourcc) {
2321 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002322 if (dev->devtype->product == CODA_960)
2323 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2324 else
2325 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002326 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2327 break;
2328 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002329 if (dev->devtype->product == CODA_960)
2330 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2331 else
2332 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002333 if (ctx->params.h264_deblk_enabled) {
2334 value = ((ctx->params.h264_deblk_alpha &
2335 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2336 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2337 ((ctx->params.h264_deblk_beta &
2338 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2339 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2340 } else {
2341 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2342 }
2343 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002344 break;
2345 default:
2346 v4l2_err(v4l2_dev,
2347 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002348 ret = -EINVAL;
2349 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002350 }
2351
Philipp Zabelc566c782012-08-08 11:59:38 -03002352 switch (ctx->params.slice_mode) {
2353 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2354 value = 0;
2355 break;
2356 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2357 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2358 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002359 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002360 break;
2361 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2362 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2363 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2364 value |= 1 & CODA_SLICING_MODE_MASK;
2365 break;
2366 }
Javier Martin186b2502012-07-26 05:53:35 -03002367 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002368 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002369 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2370
2371 if (ctx->params.bitrate) {
2372 /* Rate control enabled */
2373 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2374 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002375 if (dev->devtype->product == CODA_960)
2376 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002377 } else {
2378 value = 0;
2379 }
2380 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2381
2382 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002383 coda_write(dev, ctx->params.intra_refresh,
2384 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002385
2386 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2387 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2388
Javier Martin186b2502012-07-26 05:53:35 -03002389
Philipp Zabel89548442014-07-11 06:36:17 -03002390 value = 0;
2391 if (dev->devtype->product == CODA_960)
2392 gamma = CODA9_DEFAULT_GAMMA;
2393 else
2394 gamma = CODA_DEFAULT_GAMMA;
2395 if (gamma > 0) {
2396 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2397 CODA_CMD_ENC_SEQ_RC_GAMMA);
2398 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002399
2400 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2401 coda_write(dev,
2402 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2403 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2404 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2405 }
Philipp Zabel89548442014-07-11 06:36:17 -03002406 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002407 if (ctx->params.h264_max_qp)
2408 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002409 if (CODA_DEFAULT_GAMMA > 0)
2410 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002411 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002412 if (CODA_DEFAULT_GAMMA > 0) {
2413 if (dev->devtype->product == CODA_DX6)
2414 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2415 else
2416 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2417 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002418 if (ctx->params.h264_min_qp)
2419 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2420 if (ctx->params.h264_max_qp)
2421 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002422 }
Javier Martin186b2502012-07-26 05:53:35 -03002423 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2424
Philipp Zabel89548442014-07-11 06:36:17 -03002425 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2426
Philipp Zabelc2d22512013-06-21 03:55:28 -03002427 coda_setup_iram(ctx);
2428
Javier Martin186b2502012-07-26 05:53:35 -03002429 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002430 switch (dev->devtype->product) {
2431 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002432 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002433 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002434 break;
2435 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002436 coda_write(dev, ctx->iram_info.search_ram_paddr,
2437 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2438 coda_write(dev, ctx->iram_info.search_ram_size,
2439 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002440 break;
2441 case CODA_960:
2442 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2443 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002444 }
Javier Martin186b2502012-07-26 05:53:35 -03002445 }
2446
Philipp Zabelfcb62822013-05-23 10:43:00 -03002447 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2448 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002449 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002450 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002451 }
2452
Philipp Zabelfcb62822013-05-23 10:43:00 -03002453 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2454 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2455 ret = -EFAULT;
2456 goto out;
2457 }
Javier Martin186b2502012-07-26 05:53:35 -03002458
Philipp Zabel89548442014-07-11 06:36:17 -03002459 if (dev->devtype->product == CODA_960)
2460 ctx->num_internal_frames = 4;
2461 else
2462 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002463 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002464 if (ret < 0) {
2465 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2466 goto out;
2467 }
Javier Martin186b2502012-07-26 05:53:35 -03002468
Philipp Zabelec25f682012-07-20 08:54:29 -03002469 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002470 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002471 if (dev->devtype->product == CODA_7541)
2472 coda_write(dev, round_up(q_data_src->width, 8),
2473 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002474 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002475 coda_write(dev, ctx->iram_info.buf_bit_use,
2476 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2477 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2478 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2479 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2480 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2481 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2482 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2483 coda_write(dev, ctx->iram_info.buf_ovl_use,
2484 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002485 if (dev->devtype->product == CODA_960) {
2486 coda_write(dev, ctx->iram_info.buf_btp_use,
2487 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2488
2489 /* FIXME */
2490 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2491 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2492 }
Philipp Zabel10436672012-07-02 09:03:55 -03002493 }
Philipp Zabel89548442014-07-11 06:36:17 -03002494
Philipp Zabelfcb62822013-05-23 10:43:00 -03002495 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2496 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002497 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002498 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002499 }
2500
2501 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002502 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002503 switch (dst_fourcc) {
2504 case V4L2_PIX_FMT_H264:
2505 /*
2506 * Get SPS in the first frame and copy it to an
2507 * intermediate buffer.
2508 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002509 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2510 &ctx->vpu_header[0][0],
2511 &ctx->vpu_header_size[0]);
2512 if (ret < 0)
2513 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002514
2515 /*
2516 * Get PPS in the first frame and copy it to an
2517 * intermediate buffer.
2518 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002519 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2520 &ctx->vpu_header[1][0],
2521 &ctx->vpu_header_size[1]);
2522 if (ret < 0)
2523 goto out;
2524
Javier Martin3f3f5c72012-10-29 05:20:29 -03002525 /*
2526 * Length of H.264 headers is variable and thus it might not be
2527 * aligned for the coda to append the encoded frame. In that is
2528 * the case a filler NAL must be added to header 2.
2529 */
2530 ctx->vpu_header_size[2] = coda_h264_padding(
2531 (ctx->vpu_header_size[0] +
2532 ctx->vpu_header_size[1]),
2533 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002534 break;
2535 case V4L2_PIX_FMT_MPEG4:
2536 /*
2537 * Get VOS in the first frame and copy it to an
2538 * intermediate buffer
2539 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002540 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2541 &ctx->vpu_header[0][0],
2542 &ctx->vpu_header_size[0]);
2543 if (ret < 0)
2544 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002545
Philipp Zabeld35167a2013-05-23 10:42:59 -03002546 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2547 &ctx->vpu_header[1][0],
2548 &ctx->vpu_header_size[1]);
2549 if (ret < 0)
2550 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002551
Philipp Zabeld35167a2013-05-23 10:42:59 -03002552 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2553 &ctx->vpu_header[2][0],
2554 &ctx->vpu_header_size[2]);
2555 if (ret < 0)
2556 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002557 break;
2558 default:
2559 /* No more formats need to save headers at the moment */
2560 break;
2561 }
2562
Philipp Zabeld35167a2013-05-23 10:42:59 -03002563out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002564 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002565 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002566}
2567
Hans Verkuile37559b2014-04-17 02:47:21 -03002568static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002569{
2570 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002571 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002572
2573 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002574 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002575 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002576 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002577
Philipp Zabel89548442014-07-11 06:36:17 -03002578 if (ctx->inst_type == CODA_INST_DECODER &&
2579 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2580 /* if this decoder instance is running, set the stream end flag */
2581 if (dev->devtype->product == CODA_960) {
2582 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2583
2584 val |= CODA_BIT_STREAM_END_FLAG;
2585 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2586 ctx->bit_stream_param = val;
2587 }
2588 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002589 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2590
2591 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002592 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002593 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002594 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002595 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002596
2597 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002598 }
2599
Philipp Zabel918c66f2013-06-27 06:59:01 -03002600 if (!ctx->streamon_out && !ctx->streamon_cap) {
2601 kfifo_init(&ctx->bitstream_fifo,
2602 ctx->bitstream.vaddr, ctx->bitstream.size);
2603 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002604 }
Javier Martin186b2502012-07-26 05:53:35 -03002605}
2606
2607static struct vb2_ops coda_qops = {
2608 .queue_setup = coda_queue_setup,
2609 .buf_prepare = coda_buf_prepare,
2610 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002611 .start_streaming = coda_start_streaming,
2612 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002613 .wait_prepare = vb2_ops_wait_prepare,
2614 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002615};
2616
2617static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2618{
2619 struct coda_ctx *ctx =
2620 container_of(ctrl->handler, struct coda_ctx, ctrls);
2621
2622 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2623 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2624
2625 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002626 case V4L2_CID_HFLIP:
2627 if (ctrl->val)
2628 ctx->params.rot_mode |= CODA_MIR_HOR;
2629 else
2630 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2631 break;
2632 case V4L2_CID_VFLIP:
2633 if (ctrl->val)
2634 ctx->params.rot_mode |= CODA_MIR_VER;
2635 else
2636 ctx->params.rot_mode &= ~CODA_MIR_VER;
2637 break;
Javier Martin186b2502012-07-26 05:53:35 -03002638 case V4L2_CID_MPEG_VIDEO_BITRATE:
2639 ctx->params.bitrate = ctrl->val / 1000;
2640 break;
2641 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2642 ctx->params.gop_size = ctrl->val;
2643 break;
2644 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2645 ctx->params.h264_intra_qp = ctrl->val;
2646 break;
2647 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2648 ctx->params.h264_inter_qp = ctrl->val;
2649 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002650 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2651 ctx->params.h264_min_qp = ctrl->val;
2652 break;
2653 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2654 ctx->params.h264_max_qp = ctrl->val;
2655 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002656 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2657 ctx->params.h264_deblk_alpha = ctrl->val;
2658 break;
2659 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2660 ctx->params.h264_deblk_beta = ctrl->val;
2661 break;
2662 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2663 ctx->params.h264_deblk_enabled = (ctrl->val ==
2664 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2665 break;
Javier Martin186b2502012-07-26 05:53:35 -03002666 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2667 ctx->params.mpeg4_intra_qp = ctrl->val;
2668 break;
2669 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2670 ctx->params.mpeg4_inter_qp = ctrl->val;
2671 break;
2672 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2673 ctx->params.slice_mode = ctrl->val;
2674 break;
2675 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2676 ctx->params.slice_max_mb = ctrl->val;
2677 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002678 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2679 ctx->params.slice_max_bits = ctrl->val * 8;
2680 break;
Javier Martin186b2502012-07-26 05:53:35 -03002681 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2682 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002683 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2684 ctx->params.intra_refresh = ctrl->val;
2685 break;
Javier Martin186b2502012-07-26 05:53:35 -03002686 default:
2687 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2688 "Invalid control, id=%d, val=%d\n",
2689 ctrl->id, ctrl->val);
2690 return -EINVAL;
2691 }
2692
2693 return 0;
2694}
2695
2696static struct v4l2_ctrl_ops coda_ctrl_ops = {
2697 .s_ctrl = coda_s_ctrl,
2698};
2699
2700static int coda_ctrls_setup(struct coda_ctx *ctx)
2701{
2702 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2703
2704 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002705 V4L2_CID_HFLIP, 0, 1, 1, 0);
2706 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2707 V4L2_CID_VFLIP, 0, 1, 1, 0);
2708 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002709 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2710 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2711 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2712 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002713 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002714 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002715 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002716 if (ctx->dev->devtype->product != CODA_960) {
2717 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2718 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2719 }
2720 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2721 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002722 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002723 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2724 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2725 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2726 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2727 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2728 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2729 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2730 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002731 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2732 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2733 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2734 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2735 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002736 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2737 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002738 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2739 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002740 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2741 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002742 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2743 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2744 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2745 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2746 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002747 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2748 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002749
2750 if (ctx->ctrls.error) {
2751 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2752 ctx->ctrls.error);
2753 return -EINVAL;
2754 }
2755
2756 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2757}
2758
2759static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2760 struct vb2_queue *dst_vq)
2761{
2762 struct coda_ctx *ctx = priv;
2763 int ret;
2764
Javier Martin186b2502012-07-26 05:53:35 -03002765 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002766 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002767 src_vq->drv_priv = ctx;
2768 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2769 src_vq->ops = &coda_qops;
2770 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002771 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002772 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002773
2774 ret = vb2_queue_init(src_vq);
2775 if (ret)
2776 return ret;
2777
Javier Martin186b2502012-07-26 05:53:35 -03002778 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002779 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002780 dst_vq->drv_priv = ctx;
2781 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2782 dst_vq->ops = &coda_qops;
2783 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002784 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002785 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002786
2787 return vb2_queue_init(dst_vq);
2788}
2789
Philipp Zabele11f3e62012-07-25 09:16:58 -03002790static int coda_next_free_instance(struct coda_dev *dev)
2791{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002792 int idx = ffz(dev->instance_mask);
2793
2794 if ((idx < 0) ||
2795 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2796 return -EBUSY;
2797
2798 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002799}
2800
Javier Martin186b2502012-07-26 05:53:35 -03002801static int coda_open(struct file *file)
2802{
2803 struct coda_dev *dev = video_drvdata(file);
2804 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002805 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002806 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002807
Javier Martin186b2502012-07-26 05:53:35 -03002808 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2809 if (!ctx)
2810 return -ENOMEM;
2811
Fabio Estevamf82bc202013-08-21 11:14:16 -03002812 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002813 if (idx < 0) {
2814 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002815 goto err_coda_max;
2816 }
2817 set_bit(idx, &dev->instance_mask);
2818
Philipp Zabel32b6f202014-07-11 06:36:20 -03002819 init_completion(&ctx->completion);
2820 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2821 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002822 v4l2_fh_init(&ctx->fh, video_devdata(file));
2823 file->private_data = &ctx->fh;
2824 v4l2_fh_add(&ctx->fh);
2825 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002826 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002827 switch (dev->devtype->product) {
2828 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002829 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002830 ctx->reg_idx = 0;
2831 break;
2832 default:
2833 ctx->reg_idx = idx;
2834 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002835
Philipp Zabel1e172732014-07-11 06:36:23 -03002836 /* Power up and upload firmware if necessary */
2837 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2838 if (ret < 0) {
2839 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2840 goto err_pm_get;
2841 }
2842
Fabio Estevam79830db2013-08-21 11:14:17 -03002843 ret = clk_prepare_enable(dev->clk_per);
2844 if (ret)
2845 goto err_clk_per;
2846
2847 ret = clk_prepare_enable(dev->clk_ahb);
2848 if (ret)
2849 goto err_clk_ahb;
2850
Javier Martin186b2502012-07-26 05:53:35 -03002851 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002852 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002853 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002854 if (IS_ERR(ctx->fh.m2m_ctx)) {
2855 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002856
2857 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2858 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002859 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002860 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002861
Javier Martin186b2502012-07-26 05:53:35 -03002862 ret = coda_ctrls_setup(ctx);
2863 if (ret) {
2864 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002865 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002866 }
2867
2868 ctx->fh.ctrl_handler = &ctx->ctrls;
2869
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002870 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2871 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002872 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002873 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002874 }
2875
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002876 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2877 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2878 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2879 if (!ctx->bitstream.vaddr) {
2880 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2881 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002882 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002883 }
2884 kfifo_init(&ctx->bitstream_fifo,
2885 ctx->bitstream.vaddr, ctx->bitstream.size);
2886 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002887 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002888
Javier Martin186b2502012-07-26 05:53:35 -03002889 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002890 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002891 coda_unlock(ctx);
2892
Javier Martin186b2502012-07-26 05:53:35 -03002893 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2894 ctx->idx, ctx);
2895
2896 return 0;
2897
Fabio Estevamf82bc202013-08-21 11:14:16 -03002898err_dma_writecombine:
2899 coda_free_context_buffers(ctx);
2900 if (ctx->dev->devtype->product == CODA_DX6)
2901 coda_free_aux_buf(dev, &ctx->workbuf);
2902 coda_free_aux_buf(dev, &ctx->parabuf);
2903err_dma_alloc:
2904 v4l2_ctrl_handler_free(&ctx->ctrls);
2905err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002906 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002907err_ctx_init:
2908 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002909err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002910 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002911err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002912 pm_runtime_put_sync(&dev->plat_dev->dev);
2913err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002914 v4l2_fh_del(&ctx->fh);
2915 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002916 clear_bit(ctx->idx, &dev->instance_mask);
2917err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002918 kfree(ctx);
2919 return ret;
2920}
2921
2922static int coda_release(struct file *file)
2923{
2924 struct coda_dev *dev = video_drvdata(file);
2925 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2926
2927 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2928 ctx);
2929
Philipp Zabel918c66f2013-06-27 06:59:01 -03002930 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002931 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002932
2933 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002934 if (ctx->initialized) {
2935 queue_work(dev->workqueue, &ctx->seq_end_work);
2936 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002937 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002938
2939 coda_free_framebuffers(ctx);
2940
Javier Martin186b2502012-07-26 05:53:35 -03002941 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002942 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002943 coda_unlock(ctx);
2944
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002945 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2946 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002947 coda_free_context_buffers(ctx);
2948 if (ctx->dev->devtype->product == CODA_DX6)
2949 coda_free_aux_buf(dev, &ctx->workbuf);
2950
2951 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002952 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002953 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002954 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002955 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002956 v4l2_fh_del(&ctx->fh);
2957 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002958 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002959 kfree(ctx);
2960
2961 return 0;
2962}
2963
Javier Martin186b2502012-07-26 05:53:35 -03002964static const struct v4l2_file_operations coda_fops = {
2965 .owner = THIS_MODULE,
2966 .open = coda_open,
2967 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002968 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002969 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002970 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002971};
2972
Philipp Zabel918c66f2013-06-27 06:59:01 -03002973static void coda_finish_decode(struct coda_ctx *ctx)
2974{
2975 struct coda_dev *dev = ctx->dev;
2976 struct coda_q_data *q_data_src;
2977 struct coda_q_data *q_data_dst;
2978 struct vb2_buffer *dst_buf;
2979 int width, height;
2980 int decoded_idx;
2981 int display_idx;
2982 u32 src_fourcc;
2983 int success;
2984 u32 val;
2985
Philipp Zabel14604e32014-07-11 06:36:22 -03002986 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002987
2988 /* Update kfifo out pointer from coda bitstream read pointer */
2989 coda_kfifo_sync_from_device(ctx);
2990
2991 /*
2992 * in stream-end mode, the read pointer can overshoot the write pointer
2993 * by up to 512 bytes
2994 */
2995 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2996 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2997 kfifo_init(&ctx->bitstream_fifo,
2998 ctx->bitstream.vaddr, ctx->bitstream.size);
2999 }
3000
3001 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3002 src_fourcc = q_data_src->fourcc;
3003
3004 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3005 if (val != 1)
3006 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3007
3008 success = val & 0x1;
3009 if (!success)
3010 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3011
3012 if (src_fourcc == V4L2_PIX_FMT_H264) {
3013 if (val & (1 << 3))
3014 v4l2_err(&dev->v4l2_dev,
3015 "insufficient PS buffer space (%d bytes)\n",
3016 ctx->psbuf.size);
3017 if (val & (1 << 2))
3018 v4l2_err(&dev->v4l2_dev,
3019 "insufficient slice buffer space (%d bytes)\n",
3020 ctx->slicebuf.size);
3021 }
3022
3023 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3024 width = (val >> 16) & 0xffff;
3025 height = val & 0xffff;
3026
3027 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3028
Philipp Zabel52c41672014-07-11 06:36:19 -03003029 /* frame crop information */
3030 if (src_fourcc == V4L2_PIX_FMT_H264) {
3031 u32 left_right;
3032 u32 top_bottom;
3033
3034 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3035 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3036
3037 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3038 /* Keep current crop information */
3039 } else {
3040 struct v4l2_rect *rect = &q_data_dst->rect;
3041
3042 rect->left = left_right >> 16 & 0xffff;
3043 rect->top = top_bottom >> 16 & 0xffff;
3044 rect->width = width - rect->left -
3045 (left_right & 0xffff);
3046 rect->height = height - rect->top -
3047 (top_bottom & 0xffff);
3048 }
3049 } else {
3050 /* no cropping */
3051 }
3052
Philipp Zabel918c66f2013-06-27 06:59:01 -03003053 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3054 if (val > 0)
3055 v4l2_err(&dev->v4l2_dev,
3056 "errors in %d macroblocks\n", val);
3057
3058 if (dev->devtype->product == CODA_7541) {
3059 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3060 if (val == 0) {
3061 /* not enough bitstream data */
3062 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3063 "prescan failed: %d\n", val);
3064 ctx->prescan_failed = true;
3065 return;
3066 }
3067 }
3068
3069 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3070
3071 /*
3072 * The previous display frame was copied out by the rotator,
3073 * now it can be overwritten again
3074 */
3075 if (ctx->display_idx >= 0 &&
3076 ctx->display_idx < ctx->num_internal_frames) {
3077 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3078 coda_write(dev, ctx->frm_dis_flg,
3079 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3080 }
3081
3082 /*
3083 * The index of the last decoded frame, not necessarily in
3084 * display order, and the index of the next display frame.
3085 * The latter could have been decoded in a previous run.
3086 */
3087 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3088 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3089
3090 if (decoded_idx == -1) {
3091 /* no frame was decoded, but we might have a display frame */
3092 if (display_idx < 0 && ctx->display_idx < 0)
3093 ctx->prescan_failed = true;
3094 } else if (decoded_idx == -2) {
3095 /* no frame was decoded, we still return the remaining buffers */
3096 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3097 v4l2_err(&dev->v4l2_dev,
3098 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003099 } else {
3100 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3101 if (val == 0)
3102 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3103 else if (val == 1)
3104 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3105 else
3106 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003107 }
3108
3109 if (display_idx == -1) {
3110 /*
3111 * no more frames to be decoded, but there could still
3112 * be rotator output to dequeue
3113 */
3114 ctx->prescan_failed = true;
3115 } else if (display_idx == -3) {
3116 /* possibly prescan failure */
3117 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3118 v4l2_err(&dev->v4l2_dev,
3119 "presentation frame index out of range: %d\n",
3120 display_idx);
3121 }
3122
3123 /* If a frame was copied out, return it */
3124 if (ctx->display_idx >= 0 &&
3125 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003126 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003127 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3128
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003129 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3130 V4L2_BUF_FLAG_PFRAME |
3131 V4L2_BUF_FLAG_BFRAME);
3132 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
3133
Philipp Zabel918c66f2013-06-27 06:59:01 -03003134 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3135
3136 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3137 VB2_BUF_STATE_ERROR);
3138
3139 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3140 "job finished: decoding frame (%d) (%s)\n",
3141 dst_buf->v4l2_buf.sequence,
3142 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3143 "KEYFRAME" : "PFRAME");
3144 } else {
3145 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3146 "job finished: no frame decoded\n");
3147 }
3148
3149 /* The rotator will copy the current display frame next time */
3150 ctx->display_idx = display_idx;
3151}
3152
3153static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003154{
Philipp Zabelec25f682012-07-20 08:54:29 -03003155 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003156 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003157 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003158
Philipp Zabel14604e32014-07-11 06:36:22 -03003159 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3160 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003161
3162 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003163 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003164 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3165
Javier Martin186b2502012-07-26 05:53:35 -03003166 /* Calculate bytesused field */
3167 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003168 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3169 ctx->vpu_header_size[0] +
3170 ctx->vpu_header_size[1] +
3171 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003172 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003173 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003174 }
3175
3176 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3177 wr_ptr - start_ptr);
3178
3179 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3180 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3181
Philipp Zabel51660282013-09-30 10:34:49 -03003182 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003183 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3184 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3185 } else {
3186 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3187 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3188 }
3189
Kamil Debskiccd571c2013-04-24 10:38:24 -03003190 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003191 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3192 dst_buf->v4l2_buf.flags |=
3193 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003194 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3195
Philipp Zabelec25f682012-07-20 08:54:29 -03003196 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003197
Philipp Zabel14604e32014-07-11 06:36:22 -03003198 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003199 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3200
3201 ctx->gopcounter--;
3202 if (ctx->gopcounter < 0)
3203 ctx->gopcounter = ctx->params.gop_size - 1;
3204
3205 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3206 "job finished: encoding frame (%d) (%s)\n",
3207 dst_buf->v4l2_buf.sequence,
3208 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3209 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003210}
3211
3212static irqreturn_t coda_irq_handler(int irq, void *data)
3213{
3214 struct coda_dev *dev = data;
3215 struct coda_ctx *ctx;
3216
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003217 /* read status register to attend the IRQ */
3218 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3219 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3220 CODA_REG_BIT_INT_CLEAR);
3221
3222 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3223 if (ctx == NULL) {
3224 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3225 mutex_unlock(&dev->coda_mutex);
3226 return IRQ_HANDLED;
3227 }
3228
3229 if (ctx->aborting) {
3230 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3231 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003232 }
3233
3234 if (coda_isbusy(ctx->dev)) {
3235 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3236 "coda is still busy!!!!\n");
3237 return IRQ_NONE;
3238 }
3239
Philipp Zabel32b6f202014-07-11 06:36:20 -03003240 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003241
3242 return IRQ_HANDLED;
3243}
3244
3245static u32 coda_supported_firmwares[] = {
3246 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003247 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003248 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003249};
3250
3251static bool coda_firmware_supported(u32 vernum)
3252{
3253 int i;
3254
3255 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3256 if (vernum == coda_supported_firmwares[i])
3257 return true;
3258 return false;
3259}
3260
Philipp Zabel87048bb2012-07-02 07:03:43 -03003261static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003262{
Javier Martin186b2502012-07-26 05:53:35 -03003263 u32 data;
3264 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003265 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003266
Fabio Estevam79830db2013-08-21 11:14:17 -03003267 ret = clk_prepare_enable(dev->clk_per);
3268 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003269 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003270
3271 ret = clk_prepare_enable(dev->clk_ahb);
3272 if (ret)
3273 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003274
Javier Martin186b2502012-07-26 05:53:35 -03003275 /*
3276 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003277 * The 16-bit chars in the code buffer are in memory access
3278 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003279 * Data in this SRAM survives a reboot.
3280 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003281 p = (u16 *)dev->codebuf.vaddr;
3282 if (dev->devtype->product == CODA_DX6) {
3283 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3284 data = CODA_DOWN_ADDRESS_SET(i) |
3285 CODA_DOWN_DATA_SET(p[i ^ 1]);
3286 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3287 }
3288 } else {
3289 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3290 data = CODA_DOWN_ADDRESS_SET(i) |
3291 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3292 3 - (i % 4)]);
3293 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3294 }
Javier Martin186b2502012-07-26 05:53:35 -03003295 }
Javier Martin186b2502012-07-26 05:53:35 -03003296
Philipp Zabel9acf7692013-05-23 10:42:56 -03003297 /* Clear registers */
3298 for (i = 0; i < 64; i++)
3299 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3300
Javier Martin186b2502012-07-26 05:53:35 -03003301 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003302 if (dev->devtype->product == CODA_960 ||
3303 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003304 coda_write(dev, dev->tempbuf.paddr,
3305 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003306 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003307 } else {
3308 coda_write(dev, dev->workbuf.paddr,
3309 CODA_REG_BIT_WORK_BUF_ADDR);
3310 }
Javier Martin186b2502012-07-26 05:53:35 -03003311 coda_write(dev, dev->codebuf.paddr,
3312 CODA_REG_BIT_CODE_BUF_ADDR);
3313 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3314
3315 /* Set default values */
3316 switch (dev->devtype->product) {
3317 case CODA_DX6:
3318 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3319 break;
3320 default:
3321 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3322 }
Philipp Zabel89548442014-07-11 06:36:17 -03003323 if (dev->devtype->product == CODA_960)
3324 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3325 else
3326 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003327
3328 if (dev->devtype->product != CODA_DX6)
3329 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3330
Javier Martin186b2502012-07-26 05:53:35 -03003331 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3332 CODA_REG_BIT_INT_ENABLE);
3333
3334 /* Reset VPU and start processor */
3335 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3336 data |= CODA_REG_RESET_ENABLE;
3337 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3338 udelay(10);
3339 data &= ~CODA_REG_RESET_ENABLE;
3340 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3341 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3342
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003343 clk_disable_unprepare(dev->clk_ahb);
3344 clk_disable_unprepare(dev->clk_per);
3345
3346 return 0;
3347
3348err_clk_ahb:
3349 clk_disable_unprepare(dev->clk_per);
3350err_clk_per:
3351 return ret;
3352}
3353
3354static int coda_check_firmware(struct coda_dev *dev)
3355{
3356 u16 product, major, minor, release;
3357 u32 data;
3358 int ret;
3359
3360 ret = clk_prepare_enable(dev->clk_per);
3361 if (ret)
3362 goto err_clk_per;
3363
3364 ret = clk_prepare_enable(dev->clk_ahb);
3365 if (ret)
3366 goto err_clk_ahb;
3367
Javier Martin186b2502012-07-26 05:53:35 -03003368 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3369 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3370 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3371 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3372 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3373 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003374 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003375 ret = -EIO;
3376 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003377 }
3378
Philipp Zabel89548442014-07-11 06:36:17 -03003379 if (dev->devtype->product == CODA_960) {
3380 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3381 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3382 data);
3383 }
3384
Javier Martin186b2502012-07-26 05:53:35 -03003385 /* Check we are compatible with the loaded firmware */
3386 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3387 product = CODA_FIRMWARE_PRODUCT(data);
3388 major = CODA_FIRMWARE_MAJOR(data);
3389 minor = CODA_FIRMWARE_MINOR(data);
3390 release = CODA_FIRMWARE_RELEASE(data);
3391
3392 clk_disable_unprepare(dev->clk_per);
3393 clk_disable_unprepare(dev->clk_ahb);
3394
3395 if (product != dev->devtype->product) {
3396 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3397 " Version: %u.%u.%u\n",
3398 coda_product_name(dev->devtype->product),
3399 coda_product_name(product), major, minor, release);
3400 return -EINVAL;
3401 }
3402
3403 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3404 coda_product_name(product));
3405
3406 if (coda_firmware_supported(data)) {
3407 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3408 major, minor, release);
3409 } else {
3410 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3411 "%u.%u.%u\n", major, minor, release);
3412 }
3413
3414 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003415
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003416err_run_cmd:
3417 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003418err_clk_ahb:
3419 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003420err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003421 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003422}
3423
3424static void coda_fw_callback(const struct firmware *fw, void *context)
3425{
3426 struct coda_dev *dev = context;
3427 struct platform_device *pdev = dev->plat_dev;
3428 int ret;
3429
3430 if (!fw) {
3431 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3432 return;
3433 }
3434
3435 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003436 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3437 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003438 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3439 return;
3440 }
3441
Philipp Zabel87048bb2012-07-02 07:03:43 -03003442 /* Copy the whole firmware image to the code buffer */
3443 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3444 release_firmware(fw);
3445
Philipp Zabel1e172732014-07-11 06:36:23 -03003446 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3447 /*
3448 * Enabling power temporarily will cause coda_hw_init to be
3449 * called via coda_runtime_resume by the pm domain.
3450 */
3451 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3452 if (ret < 0) {
3453 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3454 ret);
3455 return;
3456 }
3457
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003458 ret = coda_check_firmware(dev);
3459 if (ret < 0)
3460 return;
3461
Philipp Zabel1e172732014-07-11 06:36:23 -03003462 pm_runtime_put_sync(&dev->plat_dev->dev);
3463 } else {
3464 /*
3465 * If runtime pm is disabled or pm_domain is not set,
3466 * initialize once manually.
3467 */
3468 ret = coda_hw_init(dev);
3469 if (ret < 0) {
3470 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3471 return;
3472 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003473
3474 ret = coda_check_firmware(dev);
3475 if (ret < 0)
3476 return;
Javier Martin186b2502012-07-26 05:53:35 -03003477 }
3478
3479 dev->vfd.fops = &coda_fops,
3480 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3481 dev->vfd.release = video_device_release_empty,
3482 dev->vfd.lock = &dev->dev_mutex;
3483 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003484 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003485 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3486 video_set_drvdata(&dev->vfd, dev);
3487
3488 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3489 if (IS_ERR(dev->alloc_ctx)) {
3490 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3491 return;
3492 }
3493
3494 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3495 if (IS_ERR(dev->m2m_dev)) {
3496 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3497 goto rel_ctx;
3498 }
3499
3500 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3501 if (ret) {
3502 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3503 goto rel_m2m;
3504 }
3505 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3506 dev->vfd.num);
3507
3508 return;
3509
3510rel_m2m:
3511 v4l2_m2m_release(dev->m2m_dev);
3512rel_ctx:
3513 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3514}
3515
3516static int coda_firmware_request(struct coda_dev *dev)
3517{
3518 char *fw = dev->devtype->firmware;
3519
3520 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3521 coda_product_name(dev->devtype->product));
3522
3523 return request_firmware_nowait(THIS_MODULE, true,
3524 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3525}
3526
3527enum coda_platform {
3528 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003529 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003530 CODA_IMX6Q,
3531 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003532};
3533
Emil Goodec06d8752012-08-14 17:44:42 -03003534static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003535 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003536 .firmware = "v4l-codadx6-imx27.bin",
3537 .product = CODA_DX6,
3538 .codecs = codadx6_codecs,
3539 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003540 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003541 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003542 .firmware = "v4l-coda7541-imx53.bin",
3543 .product = CODA_7541,
3544 .codecs = coda7_codecs,
3545 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003546 },
Philipp Zabel89548442014-07-11 06:36:17 -03003547 [CODA_IMX6Q] = {
3548 .firmware = "v4l-coda960-imx6q.bin",
3549 .product = CODA_960,
3550 .codecs = coda9_codecs,
3551 .num_codecs = ARRAY_SIZE(coda9_codecs),
3552 },
3553 [CODA_IMX6DL] = {
3554 .firmware = "v4l-coda960-imx6dl.bin",
3555 .product = CODA_960,
3556 .codecs = coda9_codecs,
3557 .num_codecs = ARRAY_SIZE(coda9_codecs),
3558 },
Javier Martin186b2502012-07-26 05:53:35 -03003559};
3560
3561static struct platform_device_id coda_platform_ids[] = {
3562 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003563 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003564 { /* sentinel */ }
3565};
3566MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3567
3568#ifdef CONFIG_OF
3569static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003570 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003571 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003572 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3573 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003574 { /* sentinel */ }
3575};
3576MODULE_DEVICE_TABLE(of, coda_dt_ids);
3577#endif
3578
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003579static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003580{
3581 const struct of_device_id *of_id =
3582 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3583 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003584 struct coda_platform_data *pdata = pdev->dev.platform_data;
3585 struct device_node *np = pdev->dev.of_node;
3586 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003587 struct coda_dev *dev;
3588 struct resource *res;
3589 int ret, irq;
3590
3591 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3592 if (!dev) {
3593 dev_err(&pdev->dev, "Not enough memory for %s\n",
3594 CODA_NAME);
3595 return -ENOMEM;
3596 }
3597
3598 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003599 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003600
3601 dev->plat_dev = pdev;
3602 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3603 if (IS_ERR(dev->clk_per)) {
3604 dev_err(&pdev->dev, "Could not get per clock\n");
3605 return PTR_ERR(dev->clk_per);
3606 }
3607
3608 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3609 if (IS_ERR(dev->clk_ahb)) {
3610 dev_err(&pdev->dev, "Could not get ahb clock\n");
3611 return PTR_ERR(dev->clk_ahb);
3612 }
3613
3614 /* Get memory for physical registers */
3615 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003616 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3617 if (IS_ERR(dev->regs_base))
3618 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003619
3620 /* IRQ */
3621 irq = platform_get_irq(pdev, 0);
3622 if (irq < 0) {
3623 dev_err(&pdev->dev, "failed to get irq resource\n");
3624 return -ENOENT;
3625 }
3626
Philipp Zabel918c66f2013-06-27 06:59:01 -03003627 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003628 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003629 dev_err(&pdev->dev, "failed to request irq\n");
3630 return -ENOENT;
3631 }
3632
Philipp Zabel657eee72013-04-29 16:17:14 -07003633 /* Get IRAM pool from device tree or platform data */
3634 pool = of_get_named_gen_pool(np, "iram", 0);
3635 if (!pool && pdata)
3636 pool = dev_get_gen_pool(pdata->iram_dev);
3637 if (!pool) {
3638 dev_err(&pdev->dev, "iram pool not available\n");
3639 return -ENOMEM;
3640 }
3641 dev->iram_pool = pool;
3642
Javier Martin186b2502012-07-26 05:53:35 -03003643 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3644 if (ret)
3645 return ret;
3646
3647 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003648 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003649
3650 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3651
3652 if (of_id) {
3653 dev->devtype = of_id->data;
3654 } else if (pdev_id) {
3655 dev->devtype = &coda_devdata[pdev_id->driver_data];
3656 } else {
3657 v4l2_device_unregister(&dev->v4l2_dev);
3658 return -EINVAL;
3659 }
3660
3661 /* allocate auxiliary per-device buffers for the BIT processor */
3662 switch (dev->devtype->product) {
3663 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003664 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3665 CODADX6_WORK_BUF_SIZE);
3666 if (ret < 0) {
3667 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3668 v4l2_device_unregister(&dev->v4l2_dev);
3669 return ret;
3670 }
Javier Martin186b2502012-07-26 05:53:35 -03003671 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003672 case CODA_7541:
3673 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3674 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003675 case CODA_960:
3676 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3677 break;
Javier Martin186b2502012-07-26 05:53:35 -03003678 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003679 if (dev->tempbuf.size) {
3680 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3681 dev->tempbuf.size);
3682 if (ret < 0) {
3683 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3684 v4l2_device_unregister(&dev->v4l2_dev);
3685 return ret;
3686 }
Javier Martin186b2502012-07-26 05:53:35 -03003687 }
3688
Philipp Zabel918c66f2013-06-27 06:59:01 -03003689 switch (dev->devtype->product) {
3690 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003691 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003692 break;
3693 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003694 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003695 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003696 case CODA_960:
3697 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003698 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003699 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3700 &dev->iram.paddr);
3701 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003702 dev_err(&pdev->dev, "unable to alloc iram\n");
3703 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003704 }
3705
Philipp Zabel32b6f202014-07-11 06:36:20 -03003706 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3707 if (!dev->workqueue) {
3708 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3709 return -ENOMEM;
3710 }
3711
Javier Martin186b2502012-07-26 05:53:35 -03003712 platform_set_drvdata(pdev, dev);
3713
Philipp Zabel1e172732014-07-11 06:36:23 -03003714 pm_runtime_enable(&pdev->dev);
3715
Javier Martin186b2502012-07-26 05:53:35 -03003716 return coda_firmware_request(dev);
3717}
3718
3719static int coda_remove(struct platform_device *pdev)
3720{
3721 struct coda_dev *dev = platform_get_drvdata(pdev);
3722
3723 video_unregister_device(&dev->vfd);
3724 if (dev->m2m_dev)
3725 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003726 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003727 if (dev->alloc_ctx)
3728 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3729 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003730 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003731 if (dev->iram.vaddr)
3732 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3733 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003734 coda_free_aux_buf(dev, &dev->codebuf);
3735 coda_free_aux_buf(dev, &dev->tempbuf);
3736 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003737 return 0;
3738}
3739
Philipp Zabel1e172732014-07-11 06:36:23 -03003740#ifdef CONFIG_PM_RUNTIME
3741static int coda_runtime_resume(struct device *dev)
3742{
3743 struct coda_dev *cdev = dev_get_drvdata(dev);
3744 int ret = 0;
3745
3746 if (dev->pm_domain) {
3747 ret = coda_hw_init(cdev);
3748 if (ret)
3749 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3750 }
3751
3752 return ret;
3753}
3754#endif
3755
3756static const struct dev_pm_ops coda_pm_ops = {
3757 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3758};
3759
Javier Martin186b2502012-07-26 05:53:35 -03003760static struct platform_driver coda_driver = {
3761 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003762 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003763 .driver = {
3764 .name = CODA_NAME,
3765 .owner = THIS_MODULE,
3766 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003767 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003768 },
3769 .id_table = coda_platform_ids,
3770};
3771
3772module_platform_driver(coda_driver);
3773
3774MODULE_LICENSE("GPL");
3775MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3776MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");