blob: 667149485d998baaeccb1d2a4e6a6acfaddded6b [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>
25#include <linux/slab.h>
26#include <linux/videodev2.h>
27#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070028#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030029
30#include <media/v4l2-ctrls.h>
31#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030032#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030033#include <media/v4l2-ioctl.h>
34#include <media/v4l2-mem2mem.h>
35#include <media/videobuf2-core.h>
36#include <media/videobuf2-dma-contig.h>
37
38#include "coda.h"
39
40#define CODA_NAME "coda"
41
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030042#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030043
44#define CODA_FMO_BUF_SIZE 32
45#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030046#define CODA7_WORK_BUF_SIZE (128 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030047#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030048#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030049#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030050#define CODA_PARA_BUF_SIZE (10 * 1024)
51#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070052#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030053#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030054#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030055
Philipp Zabel918c66f2013-06-27 06:59:01 -030056#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030057#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030058
59#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030060
Philipp Zabelb96904e2013-05-23 10:42:58 -030061#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030062#define FMO_SLICE_SAVE_BUF_SIZE (32)
63#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030064#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030065
66#define MIN_W 176
67#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030068
69#define S_ALIGN 1 /* multiple of 2 */
70#define W_ALIGN 1 /* multiple of 2 */
71#define H_ALIGN 1 /* multiple of 2 */
72
73#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
74
75static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030076module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030077MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
78
79enum {
80 V4L2_M2M_SRC = 0,
81 V4L2_M2M_DST = 1,
82};
83
Javier Martin186b2502012-07-26 05:53:35 -030084enum coda_inst_type {
85 CODA_INST_ENCODER,
86 CODA_INST_DECODER,
87};
88
89enum coda_product {
90 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030091 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030092 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030093};
94
95struct coda_fmt {
96 char *name;
97 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030098};
99
100struct coda_codec {
101 u32 mode;
102 u32 src_fourcc;
103 u32 dst_fourcc;
104 u32 max_w;
105 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300106};
107
108struct coda_devtype {
109 char *firmware;
110 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300111 struct coda_codec *codecs;
112 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300113 size_t workbuf_size;
114};
115
116/* Per-queue, driver-specific private data */
117struct coda_q_data {
118 unsigned int width;
119 unsigned int height;
120 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300121 unsigned int fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300122};
123
124struct coda_aux_buf {
125 void *vaddr;
126 dma_addr_t paddr;
127 u32 size;
128};
129
130struct coda_dev {
131 struct v4l2_device v4l2_dev;
132 struct video_device vfd;
133 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300134 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300135
136 void __iomem *regs_base;
137 struct clk *clk_per;
138 struct clk *clk_ahb;
139
140 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300141 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300142 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700143 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300144 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300145
146 spinlock_t irqlock;
147 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300148 struct mutex coda_mutex;
Javier Martin186b2502012-07-26 05:53:35 -0300149 struct v4l2_m2m_dev *m2m_dev;
150 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300151 struct list_head instances;
152 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300153 struct delayed_work timeout;
Javier Martin186b2502012-07-26 05:53:35 -0300154};
155
156struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300157 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300158 u8 h264_intra_qp;
159 u8 h264_inter_qp;
160 u8 mpeg4_intra_qp;
161 u8 mpeg4_inter_qp;
162 u8 gop_size;
163 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300164 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300165 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
166 u32 framerate;
167 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300168 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300169 u32 slice_max_mb;
170};
171
Philipp Zabelc2d22512013-06-21 03:55:28 -0300172struct coda_iram_info {
173 u32 axi_sram_use;
174 phys_addr_t buf_bit_use;
175 phys_addr_t buf_ip_ac_dc_use;
176 phys_addr_t buf_dbk_y_use;
177 phys_addr_t buf_dbk_c_use;
178 phys_addr_t buf_ovl_use;
179 phys_addr_t buf_btp_use;
180 phys_addr_t search_ram_paddr;
181 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300182 int remaining;
183 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300184};
185
Philipp Zabel89548442014-07-11 06:36:17 -0300186struct gdi_tiled_map {
187 int xy2ca_map[16];
188 int xy2ba_map[16];
189 int xy2ra_map[16];
190 int rbc2axi_map[32];
191 int xy2rbc_config;
192 int map_type;
193#define GDI_LINEAR_FRAME_MAP 0
194};
195
Javier Martin186b2502012-07-26 05:53:35 -0300196struct coda_ctx {
197 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300198 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300199 struct list_head list;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300200 struct work_struct skip_run;
Javier Martin186b2502012-07-26 05:53:35 -0300201 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300202 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300203 int streamon_out;
204 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300205 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300206 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300207 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300208 struct coda_q_data q_data[2];
209 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300210 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300211 enum v4l2_colorspace colorspace;
212 struct coda_params params;
213 struct v4l2_m2m_ctx *m2m_ctx;
214 struct v4l2_ctrl_handler ctrls;
215 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300216 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300217 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300218 char vpu_header[3][64];
219 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300220 struct kfifo bitstream_fifo;
221 struct mutex bitstream_mutex;
222 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300223 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300224 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300225 struct coda_aux_buf psbuf;
226 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300227 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300228 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300229 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300230 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300231 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300232 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300233 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300234 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300235 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300236 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300237 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300238 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300239};
240
Javier Martin832fbb52012-10-29 08:34:59 -0300241static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
242 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
243static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300244
Javier Martin186b2502012-07-26 05:53:35 -0300245static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
246{
247 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
248 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
249 writel(data, dev->regs_base + reg);
250}
251
252static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
253{
254 u32 data;
255 data = readl(dev->regs_base + reg);
256 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
257 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
258 return data;
259}
260
261static inline unsigned long coda_isbusy(struct coda_dev *dev)
262{
263 return coda_read(dev, CODA_REG_BIT_BUSY);
264}
265
266static inline int coda_is_initialized(struct coda_dev *dev)
267{
268 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
269}
270
271static int coda_wait_timeout(struct coda_dev *dev)
272{
273 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
274
275 while (coda_isbusy(dev)) {
276 if (time_after(jiffies, timeout))
277 return -ETIMEDOUT;
278 }
279 return 0;
280}
281
282static void coda_command_async(struct coda_ctx *ctx, int cmd)
283{
284 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300285
Philipp Zabel89548442014-07-11 06:36:17 -0300286 if (dev->devtype->product == CODA_960 ||
287 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300288 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300289 coda_write(dev, ctx->bit_stream_param,
290 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300291 coda_write(dev, ctx->frm_dis_flg,
292 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300293 coda_write(dev, ctx->frame_mem_ctrl,
294 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300295 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
296 }
297
Philipp Zabel89548442014-07-11 06:36:17 -0300298 if (dev->devtype->product == CODA_960) {
299 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
300 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
301 }
302
Javier Martin186b2502012-07-26 05:53:35 -0300303 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
304
305 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
306 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300307 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
308
Javier Martin186b2502012-07-26 05:53:35 -0300309 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
310}
311
312static int coda_command_sync(struct coda_ctx *ctx, int cmd)
313{
314 struct coda_dev *dev = ctx->dev;
315
316 coda_command_async(ctx, cmd);
317 return coda_wait_timeout(dev);
318}
319
320static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
321 enum v4l2_buf_type type)
322{
323 switch (type) {
324 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
325 return &(ctx->q_data[V4L2_M2M_SRC]);
326 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
327 return &(ctx->q_data[V4L2_M2M_DST]);
328 default:
329 BUG();
330 }
331 return NULL;
332}
333
334/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300335 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300336 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300337static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300338 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300339 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300340 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300341 },
342 {
343 .name = "YUV 4:2:0 Planar, YCrCb",
344 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300345 },
346 {
347 .name = "H264 Encoded Stream",
348 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300349 },
350 {
351 .name = "MPEG4 Encoded Stream",
352 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300353 },
354};
355
Philipp Zabelb96904e2013-05-23 10:42:58 -0300356#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
357 { mode, src_fourcc, dst_fourcc, max_w, max_h }
358
359/*
360 * Arrays of codecs supported by each given version of Coda:
361 * i.MX27 -> codadx6
362 * i.MX5x -> coda7
363 * i.MX6 -> coda960
364 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
365 */
366static struct coda_codec codadx6_codecs[] = {
367 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
368 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300369};
370
Philipp Zabelb96904e2013-05-23 10:42:58 -0300371static struct coda_codec coda7_codecs[] = {
372 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
373 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300374 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
375 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300376};
377
Philipp Zabel89548442014-07-11 06:36:17 -0300378static struct coda_codec coda9_codecs[] = {
379 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
380 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
381 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
382 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
383};
384
Philipp Zabelb96904e2013-05-23 10:42:58 -0300385static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300386{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300387 switch (fourcc) {
388 case V4L2_PIX_FMT_YUV420:
389 case V4L2_PIX_FMT_YVU420:
390 return true;
391 default:
392 return false;
393 }
394}
Javier Martin186b2502012-07-26 05:53:35 -0300395
Philipp Zabelb96904e2013-05-23 10:42:58 -0300396/*
397 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
398 * tables.
399 */
400static u32 coda_format_normalize_yuv(u32 fourcc)
401{
402 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
403}
404
405static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
406 int dst_fourcc)
407{
408 struct coda_codec *codecs = dev->devtype->codecs;
409 int num_codecs = dev->devtype->num_codecs;
410 int k;
411
412 src_fourcc = coda_format_normalize_yuv(src_fourcc);
413 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
414 if (src_fourcc == dst_fourcc)
415 return NULL;
416
417 for (k = 0; k < num_codecs; k++) {
418 if (codecs[k].src_fourcc == src_fourcc &&
419 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300420 break;
421 }
422
Philipp Zabelb96904e2013-05-23 10:42:58 -0300423 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300424 return NULL;
425
Philipp Zabelb96904e2013-05-23 10:42:58 -0300426 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300427}
428
Philipp Zabel57625592013-09-30 10:34:51 -0300429static void coda_get_max_dimensions(struct coda_dev *dev,
430 struct coda_codec *codec,
431 int *max_w, int *max_h)
432{
433 struct coda_codec *codecs = dev->devtype->codecs;
434 int num_codecs = dev->devtype->num_codecs;
435 unsigned int w, h;
436 int k;
437
438 if (codec) {
439 w = codec->max_w;
440 h = codec->max_h;
441 } else {
442 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
443 w = max(w, codecs[k].max_w);
444 h = max(h, codecs[k].max_h);
445 }
446 }
447
448 if (max_w)
449 *max_w = w;
450 if (max_h)
451 *max_h = h;
452}
453
Philipp Zabel927933f2013-09-30 10:34:48 -0300454static char *coda_product_name(int product)
455{
456 static char buf[9];
457
458 switch (product) {
459 case CODA_DX6:
460 return "CodaDx6";
461 case CODA_7541:
462 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300463 case CODA_960:
464 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300465 default:
466 snprintf(buf, sizeof(buf), "(0x%04x)", product);
467 return buf;
468 }
469}
470
Javier Martin186b2502012-07-26 05:53:35 -0300471/*
472 * V4L2 ioctl() operations.
473 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300474static int coda_querycap(struct file *file, void *priv,
475 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300476{
Philipp Zabel927933f2013-09-30 10:34:48 -0300477 struct coda_ctx *ctx = fh_to_ctx(priv);
478
Javier Martin186b2502012-07-26 05:53:35 -0300479 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300480 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
481 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300482 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300483 /*
484 * This is only a mem-to-mem video device. The capture and output
485 * device capability flags are left only for backward compatibility
486 * and are scheduled for removal.
487 */
488 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
489 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300490 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
491
492 return 0;
493}
494
495static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300496 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300497{
498 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300499 struct coda_codec *codecs = ctx->dev->devtype->codecs;
500 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300501 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300502 int num_codecs = ctx->dev->devtype->num_codecs;
503 int num_formats = ARRAY_SIZE(coda_formats);
504 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300505
506 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300507 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300508 if (coda_format_is_yuv(formats[i].fourcc) &&
509 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300510 if (num == f->index)
511 break;
512 ++num;
513 continue;
514 }
515 /* Compressed formats may be supported, check the codec list */
516 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300517 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300518 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300519 formats[i].fourcc == codecs[k].dst_fourcc &&
520 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300521 break;
522 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
523 formats[i].fourcc == codecs[k].src_fourcc)
524 break;
525 }
526 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300527 if (num == f->index)
528 break;
529 ++num;
530 }
531 }
532
533 if (i < num_formats) {
534 fmt = &formats[i];
535 strlcpy(f->description, fmt->name, sizeof(f->description));
536 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300537 if (!coda_format_is_yuv(fmt->fourcc))
538 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300539 return 0;
540 }
541
542 /* Format not found */
543 return -EINVAL;
544}
545
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300546static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
547 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300548{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300549 struct coda_ctx *ctx = fh_to_ctx(priv);
550 struct vb2_queue *src_vq;
551 struct coda_q_data *q_data_src;
552
553 /* If the source format is already fixed, only list matching formats */
554 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
555 if (vb2_is_streaming(src_vq)) {
556 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
557
558 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
559 q_data_src->fourcc);
560 }
561
562 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300563}
564
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300565static int coda_enum_fmt_vid_out(struct file *file, void *priv,
566 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300567{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300568 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300569}
570
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300571static int coda_g_fmt(struct file *file, void *priv,
572 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300573{
574 struct vb2_queue *vq;
575 struct coda_q_data *q_data;
576 struct coda_ctx *ctx = fh_to_ctx(priv);
577
578 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
579 if (!vq)
580 return -EINVAL;
581
582 q_data = get_q_data(ctx, f->type);
583
584 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300585 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300586 f->fmt.pix.width = q_data->width;
587 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300588 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300589 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
590 else /* encoded formats h.264/mpeg4 */
591 f->fmt.pix.bytesperline = 0;
592
593 f->fmt.pix.sizeimage = q_data->sizeimage;
594 f->fmt.pix.colorspace = ctx->colorspace;
595
596 return 0;
597}
598
Philipp Zabel57625592013-09-30 10:34:51 -0300599static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
600 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300601{
Philipp Zabel57625592013-09-30 10:34:51 -0300602 struct coda_dev *dev = ctx->dev;
603 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300604 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300605 enum v4l2_field field;
606
607 field = f->fmt.pix.field;
608 if (field == V4L2_FIELD_ANY)
609 field = V4L2_FIELD_NONE;
610 else if (V4L2_FIELD_NONE != field)
611 return -EINVAL;
612
613 /* V4L2 specification suggests the driver corrects the format struct
614 * if any of the dimensions is unsupported */
615 f->fmt.pix.field = field;
616
Philipp Zabel57625592013-09-30 10:34:51 -0300617 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
618 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
619 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
620 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300621
Philipp Zabel57625592013-09-30 10:34:51 -0300622 switch (f->fmt.pix.pixelformat) {
623 case V4L2_PIX_FMT_YUV420:
624 case V4L2_PIX_FMT_YVU420:
625 case V4L2_PIX_FMT_H264:
626 case V4L2_PIX_FMT_MPEG4:
627 case V4L2_PIX_FMT_JPEG:
628 break;
629 default:
630 q_data = get_q_data(ctx, f->type);
631 f->fmt.pix.pixelformat = q_data->fourcc;
632 }
633
634 switch (f->fmt.pix.pixelformat) {
635 case V4L2_PIX_FMT_YUV420:
636 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300637 /* Frame stride must be multiple of 8 */
638 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
639 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300640 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300641 break;
642 case V4L2_PIX_FMT_H264:
643 case V4L2_PIX_FMT_MPEG4:
644 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300645 f->fmt.pix.bytesperline = 0;
646 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300647 break;
648 default:
649 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300650 }
651
652 return 0;
653}
654
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300655static int coda_try_fmt_vid_cap(struct file *file, void *priv,
656 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300657{
Javier Martin186b2502012-07-26 05:53:35 -0300658 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300659 struct coda_codec *codec;
660 struct vb2_queue *src_vq;
661 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300662
Philipp Zabel918c66f2013-06-27 06:59:01 -0300663 /*
664 * If the source format is already fixed, try to find a codec that
665 * converts to the given destination format
666 */
667 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
668 if (vb2_is_streaming(src_vq)) {
669 struct coda_q_data *q_data_src;
670
671 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
672 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
673 f->fmt.pix.pixelformat);
674 if (!codec)
675 return -EINVAL;
676 } else {
677 /* Otherwise determine codec by encoded format, if possible */
678 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
679 f->fmt.pix.pixelformat);
680 }
Javier Martin186b2502012-07-26 05:53:35 -0300681
682 f->fmt.pix.colorspace = ctx->colorspace;
683
Philipp Zabel57625592013-09-30 10:34:51 -0300684 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300685 if (ret < 0)
686 return ret;
687
688 /* The h.264 decoder only returns complete 16x16 macroblocks */
689 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
690 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
691 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
692 f->fmt.pix.bytesperline = f->fmt.pix.width;
693 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
694 f->fmt.pix.height * 3 / 2;
695 }
696
697 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300698}
699
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300700static int coda_try_fmt_vid_out(struct file *file, void *priv,
701 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300702{
703 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300704 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300705
Philipp Zabelb96904e2013-05-23 10:42:58 -0300706 /* Determine codec by encoded format, returns NULL if raw or invalid */
707 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
708 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300709
710 if (!f->fmt.pix.colorspace)
711 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
712
Philipp Zabel57625592013-09-30 10:34:51 -0300713 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300714}
715
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300716static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300717{
718 struct coda_q_data *q_data;
719 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300720
721 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
722 if (!vq)
723 return -EINVAL;
724
725 q_data = get_q_data(ctx, f->type);
726 if (!q_data)
727 return -EINVAL;
728
729 if (vb2_is_busy(vq)) {
730 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
731 return -EBUSY;
732 }
733
Philipp Zabelb96904e2013-05-23 10:42:58 -0300734 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300735 q_data->width = f->fmt.pix.width;
736 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300737 q_data->sizeimage = f->fmt.pix.sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300738
739 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
740 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300741 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300742
743 return 0;
744}
745
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300746static int coda_s_fmt_vid_cap(struct file *file, void *priv,
747 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300748{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300749 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300750 int ret;
751
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300752 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300753 if (ret)
754 return ret;
755
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300756 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300757}
758
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300759static int coda_s_fmt_vid_out(struct file *file, void *priv,
760 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300761{
762 struct coda_ctx *ctx = fh_to_ctx(priv);
763 int ret;
764
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300765 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300766 if (ret)
767 return ret;
768
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300769 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300770 if (ret)
771 ctx->colorspace = f->fmt.pix.colorspace;
772
773 return ret;
774}
775
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300776static int coda_reqbufs(struct file *file, void *priv,
777 struct v4l2_requestbuffers *reqbufs)
Javier Martin186b2502012-07-26 05:53:35 -0300778{
779 struct coda_ctx *ctx = fh_to_ctx(priv);
780
781 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
782}
783
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300784static int coda_querybuf(struct file *file, void *priv,
785 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300786{
787 struct coda_ctx *ctx = fh_to_ctx(priv);
788
789 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
790}
791
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300792static int coda_qbuf(struct file *file, void *priv,
793 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300794{
795 struct coda_ctx *ctx = fh_to_ctx(priv);
796
797 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
798}
799
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300800static int coda_expbuf(struct file *file, void *priv,
801 struct v4l2_exportbuffer *eb)
Philipp Zabel419869c2013-05-23 07:06:30 -0300802{
803 struct coda_ctx *ctx = fh_to_ctx(priv);
804
805 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
806}
807
Philipp Zabel918c66f2013-06-27 06:59:01 -0300808static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
809 struct v4l2_buffer *buf)
810{
811 struct vb2_queue *src_vq;
812
813 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
814
815 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
816 (buf->sequence == (ctx->qsequence - 1)));
817}
818
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300819static int coda_dqbuf(struct file *file, void *priv,
820 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300821{
822 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300823 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300824
Philipp Zabel918c66f2013-06-27 06:59:01 -0300825 ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
826
827 /* If this is the last capture buffer, emit an end-of-stream event */
828 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
829 coda_buf_is_end_of_stream(ctx, buf)) {
830 const struct v4l2_event eos_event = {
831 .type = V4L2_EVENT_EOS
832 };
833
834 v4l2_event_queue_fh(&ctx->fh, &eos_event);
835 }
836
837 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300838}
839
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300840static int coda_create_bufs(struct file *file, void *priv,
841 struct v4l2_create_buffers *create)
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300842{
843 struct coda_ctx *ctx = fh_to_ctx(priv);
844
845 return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
846}
847
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300848static int coda_streamon(struct file *file, void *priv,
849 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300850{
851 struct coda_ctx *ctx = fh_to_ctx(priv);
852
853 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
854}
855
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300856static int coda_streamoff(struct file *file, void *priv,
857 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300858{
859 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300860 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300861
Philipp Zabel918c66f2013-06-27 06:59:01 -0300862 /*
863 * This indirectly calls __vb2_queue_cancel, which dequeues all buffers.
864 * We therefore have to lock it against running hardware in this context,
865 * which still needs the buffers.
866 */
867 mutex_lock(&ctx->buffer_mutex);
868 ret = v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
869 mutex_unlock(&ctx->buffer_mutex);
870
871 return ret;
872}
873
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300874static int coda_try_decoder_cmd(struct file *file, void *fh,
875 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300876{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300877 if (dc->cmd != V4L2_DEC_CMD_STOP)
878 return -EINVAL;
879
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300880 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300881 return -EINVAL;
882
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300883 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300884 return -EINVAL;
885
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300886 return 0;
887}
888
889static int coda_decoder_cmd(struct file *file, void *fh,
890 struct v4l2_decoder_cmd *dc)
891{
892 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300893 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300894 int ret;
895
896 ret = coda_try_decoder_cmd(file, fh, dc);
897 if (ret < 0)
898 return ret;
899
900 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300901 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300902 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300903
904 /* Set the strem-end flag on this context */
905 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
906
Philipp Zabel89548442014-07-11 06:36:17 -0300907 if ((dev->devtype->product == CODA_960) &&
908 coda_isbusy(dev) &&
909 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
910 /* If this context is currently running, update the hardware flag */
911 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
912 }
913
Philipp Zabel918c66f2013-06-27 06:59:01 -0300914 return 0;
915}
916
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300917static int coda_subscribe_event(struct v4l2_fh *fh,
918 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300919{
920 switch (sub->type) {
921 case V4L2_EVENT_EOS:
922 return v4l2_event_subscribe(fh, sub, 0, NULL);
923 default:
924 return v4l2_ctrl_subscribe_event(fh, sub);
925 }
Javier Martin186b2502012-07-26 05:53:35 -0300926}
927
928static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300929 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300930
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300931 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
932 .vidioc_g_fmt_vid_cap = coda_g_fmt,
933 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
934 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300935
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300936 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
937 .vidioc_g_fmt_vid_out = coda_g_fmt,
938 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
939 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300940
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300941 .vidioc_reqbufs = coda_reqbufs,
942 .vidioc_querybuf = coda_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300943
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300944 .vidioc_qbuf = coda_qbuf,
945 .vidioc_expbuf = coda_expbuf,
946 .vidioc_dqbuf = coda_dqbuf,
947 .vidioc_create_bufs = coda_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300948
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300949 .vidioc_streamon = coda_streamon,
950 .vidioc_streamoff = coda_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300951
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300952 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300953 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300954
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300955 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300956 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300957};
958
Philipp Zabel918c66f2013-06-27 06:59:01 -0300959static int coda_start_decoding(struct coda_ctx *ctx);
960
961static void coda_skip_run(struct work_struct *work)
962{
963 struct coda_ctx *ctx = container_of(work, struct coda_ctx, skip_run);
964
965 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
966}
967
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300968static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
969{
970 return kfifo_len(&ctx->bitstream_fifo);
971}
972
973static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
974{
975 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
976 struct coda_dev *dev = ctx->dev;
977 u32 rd_ptr;
978
979 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
980 kfifo->out = (kfifo->in & ~kfifo->mask) |
981 (rd_ptr - ctx->bitstream.paddr);
982 if (kfifo->out > kfifo->in)
983 kfifo->out -= kfifo->mask + 1;
984}
985
986static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
987{
988 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
989 struct coda_dev *dev = ctx->dev;
990 u32 rd_ptr, wr_ptr;
991
992 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
993 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
994 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
995 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
996}
997
998static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
999{
1000 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1001 struct coda_dev *dev = ctx->dev;
1002 u32 wr_ptr;
1003
1004 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1005 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1006}
1007
1008static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1009{
1010 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1011 u32 n;
1012
1013 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1014 if (n < src_size)
1015 return -ENOSPC;
1016
1017 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1018 ctx->bitstream.size, DMA_TO_DEVICE);
1019
1020 ctx->qsequence++;
1021
1022 return 0;
1023}
1024
1025static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1026 struct vb2_buffer *src_buf)
1027{
1028 int ret;
1029
1030 if (coda_get_bitstream_payload(ctx) +
1031 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1032 return false;
1033
1034 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1035 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1036 return true;
1037 }
1038
1039 ret = coda_bitstream_queue(ctx, src_buf);
1040 if (ret < 0) {
1041 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1042 return false;
1043 }
1044 /* Sync read pointer to device */
1045 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1046 coda_kfifo_sync_to_device_write(ctx);
1047
Philipp Zabel918c66f2013-06-27 06:59:01 -03001048 ctx->prescan_failed = false;
1049
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001050 return true;
1051}
1052
1053static void coda_fill_bitstream(struct coda_ctx *ctx)
1054{
1055 struct vb2_buffer *src_buf;
1056
1057 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
1058 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1059
1060 if (coda_bitstream_try_queue(ctx, src_buf)) {
1061 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1062 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1063 } else {
1064 break;
1065 }
1066 }
1067}
1068
Philipp Zabel89548442014-07-11 06:36:17 -03001069static void coda_set_gdi_regs(struct coda_ctx *ctx)
1070{
1071 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1072 struct coda_dev *dev = ctx->dev;
1073 int i;
1074
1075 for (i = 0; i < 16; i++)
1076 coda_write(dev, tiled_map->xy2ca_map[i],
1077 CODA9_GDI_XY2_CAS_0 + 4 * i);
1078 for (i = 0; i < 4; i++)
1079 coda_write(dev, tiled_map->xy2ba_map[i],
1080 CODA9_GDI_XY2_BA_0 + 4 * i);
1081 for (i = 0; i < 16; i++)
1082 coda_write(dev, tiled_map->xy2ra_map[i],
1083 CODA9_GDI_XY2_RAS_0 + 4 * i);
1084 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1085 for (i = 0; i < 32; i++)
1086 coda_write(dev, tiled_map->rbc2axi_map[i],
1087 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1088}
1089
Javier Martin186b2502012-07-26 05:53:35 -03001090/*
1091 * Mem-to-mem operations.
1092 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001093static int coda_prepare_decode(struct coda_ctx *ctx)
1094{
1095 struct vb2_buffer *dst_buf;
1096 struct coda_dev *dev = ctx->dev;
1097 struct coda_q_data *q_data_dst;
1098 u32 stridey, height;
1099 u32 picture_y, picture_cb, picture_cr;
1100
1101 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1102 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1103
1104 if (ctx->params.rot_mode & CODA_ROT_90) {
1105 stridey = q_data_dst->height;
1106 height = q_data_dst->width;
1107 } else {
1108 stridey = q_data_dst->width;
1109 height = q_data_dst->height;
1110 }
1111
1112 /* Try to copy source buffer contents into the bitstream ringbuffer */
1113 mutex_lock(&ctx->bitstream_mutex);
1114 coda_fill_bitstream(ctx);
1115 mutex_unlock(&ctx->bitstream_mutex);
1116
1117 if (coda_get_bitstream_payload(ctx) < 512 &&
1118 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1119 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1120 "bitstream payload: %d, skipping\n",
1121 coda_get_bitstream_payload(ctx));
1122 schedule_work(&ctx->skip_run);
1123 return -EAGAIN;
1124 }
1125
1126 /* Run coda_start_decoding (again) if not yet initialized */
1127 if (!ctx->initialized) {
1128 int ret = coda_start_decoding(ctx);
1129 if (ret < 0) {
1130 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1131 schedule_work(&ctx->skip_run);
1132 return -EAGAIN;
1133 } else {
1134 ctx->initialized = 1;
1135 }
1136 }
1137
Philipp Zabel89548442014-07-11 06:36:17 -03001138 if (dev->devtype->product == CODA_960)
1139 coda_set_gdi_regs(ctx);
1140
Philipp Zabel918c66f2013-06-27 06:59:01 -03001141 /* Set rotator output */
1142 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1143 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1144 /* Switch Cr and Cb for YVU420 format */
1145 picture_cr = picture_y + stridey * height;
1146 picture_cb = picture_cr + stridey / 2 * height / 2;
1147 } else {
1148 picture_cb = picture_y + stridey * height;
1149 picture_cr = picture_cb + stridey / 2 * height / 2;
1150 }
Philipp Zabel89548442014-07-11 06:36:17 -03001151
1152 if (dev->devtype->product == CODA_960) {
1153 /*
1154 * The CODA960 seems to have an internal list of buffers with
1155 * 64 entries that includes the registered frame buffers as
1156 * well as the rotator buffer output.
1157 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1158 */
1159 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1160 CODA9_CMD_DEC_PIC_ROT_INDEX);
1161 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1162 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1163 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1164 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1165 } else {
1166 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1167 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1168 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1169 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1170 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001171 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1172 CODA_CMD_DEC_PIC_ROT_MODE);
1173
1174 switch (dev->devtype->product) {
1175 case CODA_DX6:
1176 /* TBD */
1177 case CODA_7541:
1178 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1179 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001180 case CODA_960:
1181 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1182 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001183 }
1184
1185 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1186
1187 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1188 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1189
1190 return 0;
1191}
1192
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001193static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001194{
Javier Martin186b2502012-07-26 05:53:35 -03001195 struct coda_q_data *q_data_src, *q_data_dst;
1196 struct vb2_buffer *src_buf, *dst_buf;
1197 struct coda_dev *dev = ctx->dev;
1198 int force_ipicture;
1199 int quant_param = 0;
1200 u32 picture_y, picture_cb, picture_cr;
1201 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1202 u32 dst_fourcc;
1203
1204 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1205 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1206 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1207 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001208 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001209
Philipp Zabel918c66f2013-06-27 06:59:01 -03001210 src_buf->v4l2_buf.sequence = ctx->osequence;
1211 dst_buf->v4l2_buf.sequence = ctx->osequence;
1212 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001213
1214 /*
1215 * Workaround coda firmware BUG that only marks the first
1216 * frame as IDR. This is a problem for some decoders that can't
1217 * recover when a frame is lost.
1218 */
1219 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1220 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1221 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1222 } else {
1223 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1224 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1225 }
1226
Philipp Zabel89548442014-07-11 06:36:17 -03001227 if (dev->devtype->product == CODA_960)
1228 coda_set_gdi_regs(ctx);
1229
Javier Martin186b2502012-07-26 05:53:35 -03001230 /*
1231 * Copy headers at the beginning of the first frame for H.264 only.
1232 * In MPEG4 they are already copied by the coda.
1233 */
1234 if (src_buf->v4l2_buf.sequence == 0) {
1235 pic_stream_buffer_addr =
1236 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1237 ctx->vpu_header_size[0] +
1238 ctx->vpu_header_size[1] +
1239 ctx->vpu_header_size[2];
1240 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1241 ctx->vpu_header_size[0] -
1242 ctx->vpu_header_size[1] -
1243 ctx->vpu_header_size[2];
1244 memcpy(vb2_plane_vaddr(dst_buf, 0),
1245 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1246 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1247 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1248 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1249 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1250 ctx->vpu_header_size[2]);
1251 } else {
1252 pic_stream_buffer_addr =
1253 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1254 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1255 }
1256
1257 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1258 force_ipicture = 1;
1259 switch (dst_fourcc) {
1260 case V4L2_PIX_FMT_H264:
1261 quant_param = ctx->params.h264_intra_qp;
1262 break;
1263 case V4L2_PIX_FMT_MPEG4:
1264 quant_param = ctx->params.mpeg4_intra_qp;
1265 break;
1266 default:
1267 v4l2_warn(&ctx->dev->v4l2_dev,
1268 "cannot set intra qp, fmt not supported\n");
1269 break;
1270 }
1271 } else {
1272 force_ipicture = 0;
1273 switch (dst_fourcc) {
1274 case V4L2_PIX_FMT_H264:
1275 quant_param = ctx->params.h264_inter_qp;
1276 break;
1277 case V4L2_PIX_FMT_MPEG4:
1278 quant_param = ctx->params.mpeg4_inter_qp;
1279 break;
1280 default:
1281 v4l2_warn(&ctx->dev->v4l2_dev,
1282 "cannot set inter qp, fmt not supported\n");
1283 break;
1284 }
1285 }
1286
1287 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001288 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001289 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1290
1291
1292 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001293 switch (q_data_src->fourcc) {
1294 case V4L2_PIX_FMT_YVU420:
1295 /* Switch Cb and Cr for YVU420 format */
1296 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1297 picture_cb = picture_cr + q_data_src->width / 2 *
1298 q_data_src->height / 2;
1299 break;
1300 case V4L2_PIX_FMT_YUV420:
1301 default:
1302 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1303 picture_cr = picture_cb + q_data_src->width / 2 *
1304 q_data_src->height / 2;
1305 break;
1306 }
Javier Martin186b2502012-07-26 05:53:35 -03001307
Philipp Zabel89548442014-07-11 06:36:17 -03001308 if (dev->devtype->product == CODA_960) {
1309 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1310 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1311 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1312
1313 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1314 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1315 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1316 } else {
1317 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1318 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1319 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1320 }
Javier Martin186b2502012-07-26 05:53:35 -03001321 coda_write(dev, force_ipicture << 1 & 0x2,
1322 CODA_CMD_ENC_PIC_OPTION);
1323
1324 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1325 coda_write(dev, pic_stream_buffer_size / 1024,
1326 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001327
1328 if (!ctx->streamon_out) {
1329 /* After streamoff on the output side, set the stream end flag */
1330 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1331 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1332 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001333}
1334
1335static void coda_device_run(void *m2m_priv)
1336{
1337 struct coda_ctx *ctx = m2m_priv;
1338 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001339 int ret;
1340
1341 mutex_lock(&ctx->buffer_mutex);
1342
1343 /*
1344 * If streamoff dequeued all buffers before we could get the lock,
1345 * just bail out immediately.
1346 */
1347 if ((!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1348 ctx->inst_type != CODA_INST_DECODER) ||
1349 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1350 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1351 "%d: device_run without buffers\n", ctx->idx);
1352 mutex_unlock(&ctx->buffer_mutex);
1353 schedule_work(&ctx->skip_run);
1354 return;
1355 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001356
1357 mutex_lock(&dev->coda_mutex);
1358
Philipp Zabel918c66f2013-06-27 06:59:01 -03001359 if (ctx->inst_type == CODA_INST_DECODER) {
1360 ret = coda_prepare_decode(ctx);
1361 if (ret < 0) {
1362 mutex_unlock(&dev->coda_mutex);
1363 mutex_unlock(&ctx->buffer_mutex);
1364 /* job_finish scheduled by prepare_decode */
1365 return;
1366 }
1367 } else {
1368 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001369 }
1370
Philipp Zabelc2d22512013-06-21 03:55:28 -03001371 if (dev->devtype->product != CODA_DX6)
1372 coda_write(dev, ctx->iram_info.axi_sram_use,
1373 CODA7_REG_BIT_AXI_SRAM_USE);
1374
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001375 /* 1 second timeout in case CODA locks up */
1376 schedule_delayed_work(&dev->timeout, HZ);
1377
Philipp Zabel918c66f2013-06-27 06:59:01 -03001378 if (ctx->inst_type == CODA_INST_DECODER)
1379 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001380 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1381}
1382
1383static int coda_job_ready(void *m2m_priv)
1384{
1385 struct coda_ctx *ctx = m2m_priv;
1386
1387 /*
1388 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001389 * and 1 frame are needed. In the decoder case,
1390 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001391 */
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001392 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1393 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001394 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1395 "not ready: not enough video buffers.\n");
1396 return 0;
1397 }
1398
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001399 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1400 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1401 "not ready: not enough video capture buffers.\n");
1402 return 0;
1403 }
1404
Philipp Zabel918c66f2013-06-27 06:59:01 -03001405 if (ctx->prescan_failed ||
1406 ((ctx->inst_type == CODA_INST_DECODER) &&
1407 (coda_get_bitstream_payload(ctx) < 512) &&
1408 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1409 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1410 "%d: not ready: not enough bitstream data.\n",
1411 ctx->idx);
1412 return 0;
1413 }
1414
Philipp Zabel3e748262013-05-23 10:43:01 -03001415 if (ctx->aborting) {
1416 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1417 "not ready: aborting\n");
1418 return 0;
1419 }
1420
Javier Martin186b2502012-07-26 05:53:35 -03001421 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1422 "job ready\n");
1423 return 1;
1424}
1425
1426static void coda_job_abort(void *priv)
1427{
1428 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001429
1430 ctx->aborting = 1;
1431
1432 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1433 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001434}
1435
1436static void coda_lock(void *m2m_priv)
1437{
1438 struct coda_ctx *ctx = m2m_priv;
1439 struct coda_dev *pcdev = ctx->dev;
1440 mutex_lock(&pcdev->dev_mutex);
1441}
1442
1443static void coda_unlock(void *m2m_priv)
1444{
1445 struct coda_ctx *ctx = m2m_priv;
1446 struct coda_dev *pcdev = ctx->dev;
1447 mutex_unlock(&pcdev->dev_mutex);
1448}
1449
1450static struct v4l2_m2m_ops coda_m2m_ops = {
1451 .device_run = coda_device_run,
1452 .job_ready = coda_job_ready,
1453 .job_abort = coda_job_abort,
1454 .lock = coda_lock,
1455 .unlock = coda_unlock,
1456};
1457
Philipp Zabel89548442014-07-11 06:36:17 -03001458static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1459{
1460 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1461 int luma_map, chro_map, i;
1462
1463 memset(tiled_map, 0, sizeof(*tiled_map));
1464
1465 luma_map = 64;
1466 chro_map = 64;
1467 tiled_map->map_type = tiled_map_type;
1468 for (i = 0; i < 16; i++)
1469 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1470 for (i = 0; i < 4; i++)
1471 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1472 for (i = 0; i < 16; i++)
1473 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1474
1475 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1476 tiled_map->xy2rbc_config = 0;
1477 } else {
1478 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1479 tiled_map_type);
1480 return;
1481 }
1482}
1483
Javier Martin186b2502012-07-26 05:53:35 -03001484static void set_default_params(struct coda_ctx *ctx)
1485{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001486 int max_w;
1487 int max_h;
1488
1489 ctx->codec = &ctx->dev->devtype->codecs[0];
1490 max_w = ctx->codec->max_w;
1491 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001492
1493 ctx->params.codec_mode = CODA_MODE_INVALID;
1494 ctx->colorspace = V4L2_COLORSPACE_REC709;
1495 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001496 ctx->aborting = 0;
1497
1498 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001499 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1500 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1501 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1502 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1503 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1504 ctx->q_data[V4L2_M2M_DST].width = max_w;
1505 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001506 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel89548442014-07-11 06:36:17 -03001507
1508 if (ctx->dev->devtype->product == CODA_960)
1509 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001510}
1511
1512/*
1513 * Queue operations
1514 */
1515static int coda_queue_setup(struct vb2_queue *vq,
1516 const struct v4l2_format *fmt,
1517 unsigned int *nbuffers, unsigned int *nplanes,
1518 unsigned int sizes[], void *alloc_ctxs[])
1519{
1520 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001521 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001522 unsigned int size;
1523
Philipp Zabele34db062012-08-29 08:22:00 -03001524 q_data = get_q_data(ctx, vq->type);
1525 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001526
1527 *nplanes = 1;
1528 sizes[0] = size;
1529
1530 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1531
1532 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1533 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1534
1535 return 0;
1536}
1537
1538static int coda_buf_prepare(struct vb2_buffer *vb)
1539{
1540 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1541 struct coda_q_data *q_data;
1542
1543 q_data = get_q_data(ctx, vb->vb2_queue->type);
1544
1545 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1546 v4l2_warn(&ctx->dev->v4l2_dev,
1547 "%s data will not fit into plane (%lu < %lu)\n",
1548 __func__, vb2_plane_size(vb, 0),
1549 (long)q_data->sizeimage);
1550 return -EINVAL;
1551 }
1552
Javier Martin186b2502012-07-26 05:53:35 -03001553 return 0;
1554}
1555
1556static void coda_buf_queue(struct vb2_buffer *vb)
1557{
1558 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001559 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001560 struct coda_q_data *q_data;
1561
1562 q_data = get_q_data(ctx, vb->vb2_queue->type);
1563
1564 /*
1565 * In the decoder case, immediately try to copy the buffer into the
1566 * bitstream ringbuffer and mark it as ready to be dequeued.
1567 */
1568 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1569 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1570 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001571 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001572 * the stream end
1573 */
Philipp Zabel89548442014-07-11 06:36:17 -03001574 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001575 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001576 if ((dev->devtype->product == CODA_960) &&
1577 coda_isbusy(dev) &&
1578 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1579 /* if this decoder instance is running, set the stream end flag */
1580 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1581 }
1582 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001583 mutex_lock(&ctx->bitstream_mutex);
1584 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1585 coda_fill_bitstream(ctx);
1586 mutex_unlock(&ctx->bitstream_mutex);
1587 } else {
1588 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1589 }
Javier Martin186b2502012-07-26 05:53:35 -03001590}
1591
1592static void coda_wait_prepare(struct vb2_queue *q)
1593{
1594 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1595 coda_unlock(ctx);
1596}
1597
1598static void coda_wait_finish(struct vb2_queue *q)
1599{
1600 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1601 coda_lock(ctx);
1602}
1603
Philipp Zabel86eda902013-05-23 10:42:57 -03001604static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1605{
1606 struct coda_dev *dev = ctx->dev;
1607 u32 *p = ctx->parabuf.vaddr;
1608
1609 if (dev->devtype->product == CODA_DX6)
1610 p[index] = value;
1611 else
1612 p[index ^ 1] = value;
1613}
1614
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001615static int coda_alloc_aux_buf(struct coda_dev *dev,
1616 struct coda_aux_buf *buf, size_t size)
1617{
1618 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1619 GFP_KERNEL);
1620 if (!buf->vaddr)
1621 return -ENOMEM;
1622
1623 buf->size = size;
1624
1625 return 0;
1626}
1627
1628static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1629 struct coda_aux_buf *buf, size_t size)
1630{
1631 return coda_alloc_aux_buf(ctx->dev, buf, size);
1632}
1633
1634static void coda_free_aux_buf(struct coda_dev *dev,
1635 struct coda_aux_buf *buf)
1636{
1637 if (buf->vaddr) {
1638 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1639 buf->vaddr, buf->paddr);
1640 buf->vaddr = NULL;
1641 buf->size = 0;
1642 }
1643}
1644
1645static void coda_free_framebuffers(struct coda_ctx *ctx)
1646{
1647 int i;
1648
1649 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1650 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1651}
1652
Philipp Zabelec25f682012-07-20 08:54:29 -03001653static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1654{
1655 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001656 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001657 dma_addr_t paddr;
1658 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001659 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001660 int i;
1661
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001662 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1663 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001664 ysize = round_up(q_data->width, 8) * height;
1665
Philipp Zabelec25f682012-07-20 08:54:29 -03001666 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001667 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001668 size_t size;
1669
Philipp Zabelaed14b02014-07-11 06:36:15 -03001670 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001671 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1672 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001673 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001674 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1675 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001676 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001677 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001678 }
1679 }
1680
1681 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001682 for (i = 0; i < ctx->num_internal_frames; i++) {
1683 paddr = ctx->internal_frames[i].paddr;
1684 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1685 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1686 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001687
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001688 /* mvcol buffer for h.264 */
1689 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1690 dev->devtype->product != CODA_DX6)
1691 coda_parabuf_write(ctx, 96 + i,
1692 ctx->internal_frames[i].paddr +
1693 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001694 }
1695
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001696 /* mvcol buffer for mpeg4 */
1697 if ((dev->devtype->product != CODA_DX6) &&
1698 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1699 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1700 ysize + ysize/4 + ysize/4);
1701
Philipp Zabelec25f682012-07-20 08:54:29 -03001702 return 0;
1703}
1704
Javier Martin3f3f5c72012-10-29 05:20:29 -03001705static int coda_h264_padding(int size, char *p)
1706{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001707 int nal_size;
1708 int diff;
1709
Javier Martin832fbb52012-10-29 08:34:59 -03001710 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001711 if (diff == 0)
1712 return 0;
1713
Javier Martin832fbb52012-10-29 08:34:59 -03001714 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001715 memcpy(p, coda_filler_nal, nal_size);
1716
1717 /* Add rbsp stop bit and trailing at the end */
1718 *(p + nal_size - 1) = 0x80;
1719
1720 return nal_size;
1721}
1722
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001723static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1724{
1725 phys_addr_t ret;
1726
1727 size = round_up(size, 1024);
1728 if (size > iram->remaining)
1729 return 0;
1730 iram->remaining -= size;
1731
1732 ret = iram->next_paddr;
1733 iram->next_paddr += size;
1734
1735 return ret;
1736}
1737
Philipp Zabelc2d22512013-06-21 03:55:28 -03001738static void coda_setup_iram(struct coda_ctx *ctx)
1739{
1740 struct coda_iram_info *iram_info = &ctx->iram_info;
1741 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001742 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001743 int dbk_bits;
1744 int bit_bits;
1745 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001746
1747 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001748 iram_info->next_paddr = dev->iram.paddr;
1749 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001750
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001751 switch (dev->devtype->product) {
1752 case CODA_7541:
1753 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1754 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1755 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1756 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001757 case CODA_960:
1758 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1759 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1760 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1761 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001762 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001763 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001764 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001765
1766 if (ctx->inst_type == CODA_INST_ENCODER) {
1767 struct coda_q_data *q_data_src;
1768
1769 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1770 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1771
1772 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001773 if (dev->devtype->product == CODA_7541) {
1774 iram_info->search_ram_size = round_up(mb_width * 16 *
1775 36 + 2048, 1024);
1776 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1777 iram_info->search_ram_size);
1778 if (!iram_info->search_ram_paddr) {
1779 pr_err("IRAM is smaller than the search ram size\n");
1780 goto out;
1781 }
1782 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1783 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001784 }
1785
1786 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001787 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1788 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1789 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001790 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001791 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001792
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001793 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1794 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001795 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001796 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001797
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001798 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1799 if (!iram_info->buf_ip_ac_dc_use)
1800 goto out;
1801 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001802
Philipp Zabel8358e762013-06-21 03:55:32 -03001803 /* OVL and BTP disabled for encoder */
1804 } else if (ctx->inst_type == CODA_INST_DECODER) {
1805 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001806
1807 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1808 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001809
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001810 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1811 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1812 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001813 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001814 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001815
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001816 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1817 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001818 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001819 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001820
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001821 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1822 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001823 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001824 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001825
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001826 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001827 }
1828
1829out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001830 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1831 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1832 "IRAM smaller than needed\n");
1833
1834 if (dev->devtype->product == CODA_7541) {
1835 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001836 if (ctx->inst_type == CODA_INST_DECODER) {
1837 /* fw 1.4.50 */
1838 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1839 CODA7_USE_IP_ENABLE);
1840 } else {
1841 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001842 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1843 CODA7_USE_HOST_DBK_ENABLE |
1844 CODA7_USE_IP_ENABLE |
1845 CODA7_USE_DBK_ENABLE);
1846 }
1847 }
1848}
1849
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001850static void coda_free_context_buffers(struct coda_ctx *ctx)
1851{
1852 struct coda_dev *dev = ctx->dev;
1853
Philipp Zabel918c66f2013-06-27 06:59:01 -03001854 coda_free_aux_buf(dev, &ctx->slicebuf);
1855 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001856 if (dev->devtype->product != CODA_DX6)
1857 coda_free_aux_buf(dev, &ctx->workbuf);
1858}
1859
1860static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1861 struct coda_q_data *q_data)
1862{
1863 struct coda_dev *dev = ctx->dev;
1864 size_t size;
1865 int ret;
1866
1867 switch (dev->devtype->product) {
1868 case CODA_7541:
1869 size = CODA7_WORK_BUF_SIZE;
1870 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001871 case CODA_960:
1872 size = CODA9_WORK_BUF_SIZE;
1873 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1874 size += CODA9_PS_SAVE_SIZE;
1875 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001876 default:
1877 return 0;
1878 }
1879
Philipp Zabel918c66f2013-06-27 06:59:01 -03001880 if (ctx->psbuf.vaddr) {
1881 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1882 return -EBUSY;
1883 }
1884 if (ctx->slicebuf.vaddr) {
1885 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1886 return -EBUSY;
1887 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001888 if (ctx->workbuf.vaddr) {
1889 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1890 ret = -EBUSY;
1891 return -ENOMEM;
1892 }
1893
Philipp Zabel918c66f2013-06-27 06:59:01 -03001894 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1895 /* worst case slice size */
1896 size = (DIV_ROUND_UP(q_data->width, 16) *
1897 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1898 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1899 if (ret < 0) {
1900 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1901 ctx->slicebuf.size);
1902 return ret;
1903 }
1904 }
1905
1906 if (dev->devtype->product == CODA_7541) {
1907 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1908 if (ret < 0) {
1909 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1910 goto err;
1911 }
1912 }
1913
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001914 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1915 if (ret < 0) {
1916 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1917 ctx->workbuf.size);
1918 goto err;
1919 }
1920
1921 return 0;
1922
1923err:
1924 coda_free_context_buffers(ctx);
1925 return ret;
1926}
1927
Philipp Zabel918c66f2013-06-27 06:59:01 -03001928static int coda_start_decoding(struct coda_ctx *ctx)
1929{
1930 struct coda_q_data *q_data_src, *q_data_dst;
1931 u32 bitstream_buf, bitstream_size;
1932 struct coda_dev *dev = ctx->dev;
1933 int width, height;
1934 u32 src_fourcc;
1935 u32 val;
1936 int ret;
1937
1938 /* Start decoding */
1939 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1940 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1941 bitstream_buf = ctx->bitstream.paddr;
1942 bitstream_size = ctx->bitstream.size;
1943 src_fourcc = q_data_src->fourcc;
1944
1945 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1946
1947 /* Update coda bitstream read and write pointers from kfifo */
1948 coda_kfifo_sync_to_device_full(ctx);
1949
1950 ctx->display_idx = -1;
1951 ctx->frm_dis_flg = 0;
1952 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1953
1954 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1955 CODA_REG_BIT_BIT_STREAM_PARAM);
1956
1957 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1958 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1959 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001960 if ((dev->devtype->product == CODA_7541) ||
1961 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03001962 val |= CODA_REORDER_ENABLE;
1963 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1964
1965 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03001966 if (dev->devtype->product == CODA_960 &&
1967 src_fourcc == V4L2_PIX_FMT_MPEG4)
1968 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1969 else
1970 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001971 if (src_fourcc == V4L2_PIX_FMT_H264) {
1972 if (dev->devtype->product == CODA_7541) {
1973 coda_write(dev, ctx->psbuf.paddr,
1974 CODA_CMD_DEC_SEQ_PS_BB_START);
1975 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1976 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1977 }
Philipp Zabel89548442014-07-11 06:36:17 -03001978 if (dev->devtype->product == CODA_960) {
1979 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1980 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1981 }
1982 }
1983 if (dev->devtype->product != CODA_960) {
1984 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001985 }
1986
1987 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1988 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1989 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1990 return -ETIMEDOUT;
1991 }
1992
1993 /* Update kfifo out pointer from coda bitstream read pointer */
1994 coda_kfifo_sync_from_device(ctx);
1995
1996 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1997
1998 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1999 v4l2_err(&dev->v4l2_dev,
2000 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2001 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2002 return -EAGAIN;
2003 }
2004
2005 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2006 if (dev->devtype->product == CODA_DX6) {
2007 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2008 height = val & CODADX6_PICHEIGHT_MASK;
2009 } else {
2010 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2011 height = val & CODA7_PICHEIGHT_MASK;
2012 }
2013
2014 if (width > q_data_dst->width || height > q_data_dst->height) {
2015 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2016 width, height, q_data_dst->width, q_data_dst->height);
2017 return -EINVAL;
2018 }
2019
2020 width = round_up(width, 16);
2021 height = round_up(height, 16);
2022
2023 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2024 __func__, ctx->idx, width, height);
2025
Philipp Zabela500a932014-07-11 06:36:13 -03002026 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002027 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2028 v4l2_err(&dev->v4l2_dev,
2029 "not enough framebuffers to decode (%d < %d)\n",
2030 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2031 return -EINVAL;
2032 }
2033
2034 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2035 if (ret < 0)
2036 return ret;
2037
2038 /* Tell the decoder how many frame buffers we allocated. */
2039 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2040 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2041
2042 if (dev->devtype->product != CODA_DX6) {
2043 /* Set secondary AXI IRAM */
2044 coda_setup_iram(ctx);
2045
2046 coda_write(dev, ctx->iram_info.buf_bit_use,
2047 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2048 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2049 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2050 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2051 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2052 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2053 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2054 coda_write(dev, ctx->iram_info.buf_ovl_use,
2055 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002056 if (dev->devtype->product == CODA_960)
2057 coda_write(dev, ctx->iram_info.buf_btp_use,
2058 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2059 }
2060
2061 if (dev->devtype->product == CODA_960) {
2062 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2063
2064 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2065 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2066 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2067 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2068 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2069 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002070 }
2071
2072 if (src_fourcc == V4L2_PIX_FMT_H264) {
2073 coda_write(dev, ctx->slicebuf.paddr,
2074 CODA_CMD_SET_FRAME_SLICE_BB_START);
2075 coda_write(dev, ctx->slicebuf.size / 1024,
2076 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2077 }
2078
2079 if (dev->devtype->product == CODA_7541) {
2080 int max_mb_x = 1920 / 16;
2081 int max_mb_y = 1088 / 16;
2082 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002083
Philipp Zabel918c66f2013-06-27 06:59:01 -03002084 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2085 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002086 } else if (dev->devtype->product == CODA_960) {
2087 int max_mb_x = 1920 / 16;
2088 int max_mb_y = 1088 / 16;
2089 int max_mb_num = max_mb_x * max_mb_y;
2090
2091 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2092 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002093 }
2094
2095 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2096 v4l2_err(&ctx->dev->v4l2_dev,
2097 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2098 return -ETIMEDOUT;
2099 }
2100
2101 return 0;
2102}
2103
Philipp Zabeld35167a2013-05-23 10:42:59 -03002104static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2105 int header_code, u8 *header, int *size)
2106{
2107 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002108 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002109 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002110 int i;
2111
2112 if (dev->devtype->product == CODA_960)
2113 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002114
2115 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2116 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002117 bufsize = vb2_plane_size(buf, 0);
2118 if (dev->devtype->product == CODA_960)
2119 bufsize /= 1024;
2120 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002121 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2122 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2123 if (ret < 0) {
2124 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2125 return ret;
2126 }
Philipp Zabel89548442014-07-11 06:36:17 -03002127
2128 if (dev->devtype->product == CODA_960) {
2129 for (i = 63; i > 0; i--)
2130 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2131 break;
2132 *size = i + 1;
2133 } else {
2134 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2135 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2136 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002137 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2138
2139 return 0;
2140}
2141
Philipp Zabel89548442014-07-11 06:36:17 -03002142static int coda_start_encoding(struct coda_ctx *ctx);
2143
Javier Martin186b2502012-07-26 05:53:35 -03002144static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2145{
2146 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2147 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002148 struct coda_dev *dev = ctx->dev;
2149 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002150 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002151 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002152
Philipp Zabelb96904e2013-05-23 10:42:58 -03002153 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002154 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2155 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2156 if (coda_get_bitstream_payload(ctx) < 512)
2157 return -EINVAL;
2158 } else {
2159 if (count < 1)
2160 return -EINVAL;
2161 }
2162
2163 ctx->streamon_out = 1;
2164
Philipp Zabelb96904e2013-05-23 10:42:58 -03002165 if (coda_format_is_yuv(q_data_src->fourcc))
2166 ctx->inst_type = CODA_INST_ENCODER;
2167 else
2168 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002169 } else {
2170 if (count < 1)
2171 return -EINVAL;
2172
2173 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002174 }
Javier Martin186b2502012-07-26 05:53:35 -03002175
Philipp Zabelb96904e2013-05-23 10:42:58 -03002176 /* Don't start the coda unless both queues are on */
2177 if (!(ctx->streamon_out & ctx->streamon_cap))
2178 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002179
Philipp Zabeleb107512013-09-30 10:34:45 -03002180 /* Allow decoder device_run with no new buffers queued */
2181 if (ctx->inst_type == CODA_INST_DECODER)
2182 v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002183
Philipp Zabelb96904e2013-05-23 10:42:58 -03002184 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002185 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002186 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002187
Philipp Zabelb96904e2013-05-23 10:42:58 -03002188 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2189 q_data_dst->fourcc);
2190 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002191 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2192 return -EINVAL;
2193 }
2194
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002195 /* Allocate per-instance buffers */
2196 ret = coda_alloc_context_buffers(ctx, q_data_src);
2197 if (ret < 0)
2198 return ret;
2199
Philipp Zabel918c66f2013-06-27 06:59:01 -03002200 if (ctx->inst_type == CODA_INST_DECODER) {
2201 mutex_lock(&dev->coda_mutex);
2202 ret = coda_start_decoding(ctx);
2203 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002204 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002205 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002206 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002207 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002208 } else {
2209 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002210 }
2211
Philipp Zabel89548442014-07-11 06:36:17 -03002212 ctx->initialized = 1;
2213 return ret;
2214}
2215
2216static int coda_start_encoding(struct coda_ctx *ctx)
2217{
2218 struct coda_dev *dev = ctx->dev;
2219 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2220 struct coda_q_data *q_data_src, *q_data_dst;
2221 u32 bitstream_buf, bitstream_size;
2222 struct vb2_buffer *buf;
2223 int gamma, ret, value;
2224 u32 dst_fourcc;
2225
2226 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2227 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2228 dst_fourcc = q_data_dst->fourcc;
2229
2230 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2231 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2232 bitstream_size = q_data_dst->sizeimage;
2233
Javier Martin186b2502012-07-26 05:53:35 -03002234 if (!coda_is_initialized(dev)) {
2235 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2236 return -EFAULT;
2237 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002238
2239 mutex_lock(&dev->coda_mutex);
2240
Javier Martin186b2502012-07-26 05:53:35 -03002241 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002242 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2243 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002244 switch (dev->devtype->product) {
2245 case CODA_DX6:
2246 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2247 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2248 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002249 case CODA_960:
2250 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2251 /* fallthrough */
2252 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002253 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2254 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002255 break;
Javier Martin186b2502012-07-26 05:53:35 -03002256 }
2257
Philipp Zabel89548442014-07-11 06:36:17 -03002258 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2259 value &= ~(1 << 2 | 0x7 << 9);
2260 ctx->frame_mem_ctrl = value;
2261 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2262
Philipp Zabel10436672012-07-02 09:03:55 -03002263 if (dev->devtype->product == CODA_DX6) {
2264 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002265 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002266 }
Javier Martin186b2502012-07-26 05:53:35 -03002267
2268 /* Could set rotation here if needed */
2269 switch (dev->devtype->product) {
2270 case CODA_DX6:
2271 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002272 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002273 break;
2274 default:
2275 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002276 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002277 }
Javier Martin186b2502012-07-26 05:53:35 -03002278 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2279 coda_write(dev, ctx->params.framerate,
2280 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2281
Philipp Zabelb96904e2013-05-23 10:42:58 -03002282 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002283 switch (dst_fourcc) {
2284 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002285 if (dev->devtype->product == CODA_960)
2286 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2287 else
2288 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002289 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2290 break;
2291 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002292 if (dev->devtype->product == CODA_960)
2293 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2294 else
2295 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002296 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2297 break;
2298 default:
2299 v4l2_err(v4l2_dev,
2300 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002301 ret = -EINVAL;
2302 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002303 }
2304
Philipp Zabelc566c782012-08-08 11:59:38 -03002305 switch (ctx->params.slice_mode) {
2306 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2307 value = 0;
2308 break;
2309 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2310 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2311 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002312 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002313 break;
2314 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2315 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2316 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2317 value |= 1 & CODA_SLICING_MODE_MASK;
2318 break;
2319 }
Javier Martin186b2502012-07-26 05:53:35 -03002320 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002321 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002322 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2323
2324 if (ctx->params.bitrate) {
2325 /* Rate control enabled */
2326 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2327 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002328 if (dev->devtype->product == CODA_960)
2329 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002330 } else {
2331 value = 0;
2332 }
2333 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2334
2335 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2336 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2337
2338 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2339 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2340
Javier Martin186b2502012-07-26 05:53:35 -03002341
Philipp Zabel89548442014-07-11 06:36:17 -03002342 value = 0;
2343 if (dev->devtype->product == CODA_960)
2344 gamma = CODA9_DEFAULT_GAMMA;
2345 else
2346 gamma = CODA_DEFAULT_GAMMA;
2347 if (gamma > 0) {
2348 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2349 CODA_CMD_ENC_SEQ_RC_GAMMA);
2350 }
2351 if (dev->devtype->product == CODA_960) {
2352 if (CODA_DEFAULT_GAMMA > 0)
2353 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002354 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002355 if (CODA_DEFAULT_GAMMA > 0) {
2356 if (dev->devtype->product == CODA_DX6)
2357 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2358 else
2359 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2360 }
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002361 }
Javier Martin186b2502012-07-26 05:53:35 -03002362 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2363
Philipp Zabel89548442014-07-11 06:36:17 -03002364 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2365
Philipp Zabelc2d22512013-06-21 03:55:28 -03002366 coda_setup_iram(ctx);
2367
Javier Martin186b2502012-07-26 05:53:35 -03002368 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002369 switch (dev->devtype->product) {
2370 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002371 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002372 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002373 break;
2374 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002375 coda_write(dev, ctx->iram_info.search_ram_paddr,
2376 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2377 coda_write(dev, ctx->iram_info.search_ram_size,
2378 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002379 break;
2380 case CODA_960:
2381 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2382 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002383 }
Javier Martin186b2502012-07-26 05:53:35 -03002384 }
2385
Philipp Zabelfcb62822013-05-23 10:43:00 -03002386 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2387 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002388 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002389 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002390 }
2391
Philipp Zabelfcb62822013-05-23 10:43:00 -03002392 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2393 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2394 ret = -EFAULT;
2395 goto out;
2396 }
Javier Martin186b2502012-07-26 05:53:35 -03002397
Philipp Zabel89548442014-07-11 06:36:17 -03002398 if (dev->devtype->product == CODA_960)
2399 ctx->num_internal_frames = 4;
2400 else
2401 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002402 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002403 if (ret < 0) {
2404 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2405 goto out;
2406 }
Javier Martin186b2502012-07-26 05:53:35 -03002407
Philipp Zabelec25f682012-07-20 08:54:29 -03002408 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002409 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002410 if (dev->devtype->product == CODA_7541)
2411 coda_write(dev, round_up(q_data_src->width, 8),
2412 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002413 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002414 coda_write(dev, ctx->iram_info.buf_bit_use,
2415 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2416 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2417 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2418 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2419 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2420 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2421 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2422 coda_write(dev, ctx->iram_info.buf_ovl_use,
2423 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002424 if (dev->devtype->product == CODA_960) {
2425 coda_write(dev, ctx->iram_info.buf_btp_use,
2426 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2427
2428 /* FIXME */
2429 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2430 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2431 }
Philipp Zabel10436672012-07-02 09:03:55 -03002432 }
Philipp Zabel89548442014-07-11 06:36:17 -03002433
Philipp Zabelfcb62822013-05-23 10:43:00 -03002434 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2435 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002436 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002437 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002438 }
2439
2440 /* Save stream headers */
2441 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2442 switch (dst_fourcc) {
2443 case V4L2_PIX_FMT_H264:
2444 /*
2445 * Get SPS in the first frame and copy it to an
2446 * intermediate buffer.
2447 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002448 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2449 &ctx->vpu_header[0][0],
2450 &ctx->vpu_header_size[0]);
2451 if (ret < 0)
2452 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002453
2454 /*
2455 * Get PPS in the first frame and copy it to an
2456 * intermediate buffer.
2457 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002458 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2459 &ctx->vpu_header[1][0],
2460 &ctx->vpu_header_size[1]);
2461 if (ret < 0)
2462 goto out;
2463
Javier Martin3f3f5c72012-10-29 05:20:29 -03002464 /*
2465 * Length of H.264 headers is variable and thus it might not be
2466 * aligned for the coda to append the encoded frame. In that is
2467 * the case a filler NAL must be added to header 2.
2468 */
2469 ctx->vpu_header_size[2] = coda_h264_padding(
2470 (ctx->vpu_header_size[0] +
2471 ctx->vpu_header_size[1]),
2472 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002473 break;
2474 case V4L2_PIX_FMT_MPEG4:
2475 /*
2476 * Get VOS in the first frame and copy it to an
2477 * intermediate buffer
2478 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002479 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2480 &ctx->vpu_header[0][0],
2481 &ctx->vpu_header_size[0]);
2482 if (ret < 0)
2483 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002484
Philipp Zabeld35167a2013-05-23 10:42:59 -03002485 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2486 &ctx->vpu_header[1][0],
2487 &ctx->vpu_header_size[1]);
2488 if (ret < 0)
2489 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002490
Philipp Zabeld35167a2013-05-23 10:42:59 -03002491 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2492 &ctx->vpu_header[2][0],
2493 &ctx->vpu_header_size[2]);
2494 if (ret < 0)
2495 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002496 break;
2497 default:
2498 /* No more formats need to save headers at the moment */
2499 break;
2500 }
2501
Philipp Zabeld35167a2013-05-23 10:42:59 -03002502out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002503 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002504 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002505}
2506
Hans Verkuile37559b2014-04-17 02:47:21 -03002507static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002508{
2509 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002510 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002511
2512 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002513 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002514 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002515 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002516
Philipp Zabel89548442014-07-11 06:36:17 -03002517 if (ctx->inst_type == CODA_INST_DECODER &&
2518 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2519 /* if this decoder instance is running, set the stream end flag */
2520 if (dev->devtype->product == CODA_960) {
2521 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2522
2523 val |= CODA_BIT_STREAM_END_FLAG;
2524 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2525 ctx->bit_stream_param = val;
2526 }
2527 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002528 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2529
2530 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002531 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002532 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002533 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002534 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002535
2536 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002537 }
2538
Philipp Zabel918c66f2013-06-27 06:59:01 -03002539 if (!ctx->streamon_out && !ctx->streamon_cap) {
2540 kfifo_init(&ctx->bitstream_fifo,
2541 ctx->bitstream.vaddr, ctx->bitstream.size);
2542 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002543 }
Javier Martin186b2502012-07-26 05:53:35 -03002544}
2545
2546static struct vb2_ops coda_qops = {
2547 .queue_setup = coda_queue_setup,
2548 .buf_prepare = coda_buf_prepare,
2549 .buf_queue = coda_buf_queue,
2550 .wait_prepare = coda_wait_prepare,
2551 .wait_finish = coda_wait_finish,
2552 .start_streaming = coda_start_streaming,
2553 .stop_streaming = coda_stop_streaming,
2554};
2555
2556static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2557{
2558 struct coda_ctx *ctx =
2559 container_of(ctrl->handler, struct coda_ctx, ctrls);
2560
2561 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2562 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2563
2564 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002565 case V4L2_CID_HFLIP:
2566 if (ctrl->val)
2567 ctx->params.rot_mode |= CODA_MIR_HOR;
2568 else
2569 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2570 break;
2571 case V4L2_CID_VFLIP:
2572 if (ctrl->val)
2573 ctx->params.rot_mode |= CODA_MIR_VER;
2574 else
2575 ctx->params.rot_mode &= ~CODA_MIR_VER;
2576 break;
Javier Martin186b2502012-07-26 05:53:35 -03002577 case V4L2_CID_MPEG_VIDEO_BITRATE:
2578 ctx->params.bitrate = ctrl->val / 1000;
2579 break;
2580 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2581 ctx->params.gop_size = ctrl->val;
2582 break;
2583 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2584 ctx->params.h264_intra_qp = ctrl->val;
2585 break;
2586 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2587 ctx->params.h264_inter_qp = ctrl->val;
2588 break;
2589 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2590 ctx->params.mpeg4_intra_qp = ctrl->val;
2591 break;
2592 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2593 ctx->params.mpeg4_inter_qp = ctrl->val;
2594 break;
2595 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2596 ctx->params.slice_mode = ctrl->val;
2597 break;
2598 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2599 ctx->params.slice_max_mb = ctrl->val;
2600 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002601 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2602 ctx->params.slice_max_bits = ctrl->val * 8;
2603 break;
Javier Martin186b2502012-07-26 05:53:35 -03002604 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2605 break;
2606 default:
2607 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2608 "Invalid control, id=%d, val=%d\n",
2609 ctrl->id, ctrl->val);
2610 return -EINVAL;
2611 }
2612
2613 return 0;
2614}
2615
2616static struct v4l2_ctrl_ops coda_ctrl_ops = {
2617 .s_ctrl = coda_s_ctrl,
2618};
2619
2620static int coda_ctrls_setup(struct coda_ctx *ctx)
2621{
2622 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2623
2624 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002625 V4L2_CID_HFLIP, 0, 1, 1, 0);
2626 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2627 V4L2_CID_VFLIP, 0, 1, 1, 0);
2628 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002629 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2630 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2631 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2632 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002633 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002634 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002635 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002636 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2637 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2638 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2639 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2640 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2641 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002642 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2643 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002644 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2645 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002646 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2647 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002648 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2649 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2650 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2651 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2652 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2653
2654 if (ctx->ctrls.error) {
2655 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2656 ctx->ctrls.error);
2657 return -EINVAL;
2658 }
2659
2660 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2661}
2662
2663static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2664 struct vb2_queue *dst_vq)
2665{
2666 struct coda_ctx *ctx = priv;
2667 int ret;
2668
Javier Martin186b2502012-07-26 05:53:35 -03002669 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002670 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002671 src_vq->drv_priv = ctx;
2672 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2673 src_vq->ops = &coda_qops;
2674 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002675 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002676
2677 ret = vb2_queue_init(src_vq);
2678 if (ret)
2679 return ret;
2680
Javier Martin186b2502012-07-26 05:53:35 -03002681 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002682 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002683 dst_vq->drv_priv = ctx;
2684 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2685 dst_vq->ops = &coda_qops;
2686 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002687 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002688
2689 return vb2_queue_init(dst_vq);
2690}
2691
Philipp Zabele11f3e62012-07-25 09:16:58 -03002692static int coda_next_free_instance(struct coda_dev *dev)
2693{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002694 int idx = ffz(dev->instance_mask);
2695
2696 if ((idx < 0) ||
2697 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2698 return -EBUSY;
2699
2700 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002701}
2702
Javier Martin186b2502012-07-26 05:53:35 -03002703static int coda_open(struct file *file)
2704{
2705 struct coda_dev *dev = video_drvdata(file);
2706 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002707 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002708 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002709
Javier Martin186b2502012-07-26 05:53:35 -03002710 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2711 if (!ctx)
2712 return -ENOMEM;
2713
Fabio Estevamf82bc202013-08-21 11:14:16 -03002714 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002715 if (idx < 0) {
2716 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002717 goto err_coda_max;
2718 }
2719 set_bit(idx, &dev->instance_mask);
2720
Philipp Zabel918c66f2013-06-27 06:59:01 -03002721 INIT_WORK(&ctx->skip_run, coda_skip_run);
Javier Martin186b2502012-07-26 05:53:35 -03002722 v4l2_fh_init(&ctx->fh, video_devdata(file));
2723 file->private_data = &ctx->fh;
2724 v4l2_fh_add(&ctx->fh);
2725 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002726 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002727 switch (dev->devtype->product) {
2728 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002729 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002730 ctx->reg_idx = 0;
2731 break;
2732 default:
2733 ctx->reg_idx = idx;
2734 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002735
Fabio Estevam79830db2013-08-21 11:14:17 -03002736 ret = clk_prepare_enable(dev->clk_per);
2737 if (ret)
2738 goto err_clk_per;
2739
2740 ret = clk_prepare_enable(dev->clk_ahb);
2741 if (ret)
2742 goto err_clk_ahb;
2743
Javier Martin186b2502012-07-26 05:53:35 -03002744 set_default_params(ctx);
2745 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2746 &coda_queue_init);
2747 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03002748 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002749
2750 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2751 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002752 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002753 }
2754 ret = coda_ctrls_setup(ctx);
2755 if (ret) {
2756 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002757 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002758 }
2759
2760 ctx->fh.ctrl_handler = &ctx->ctrls;
2761
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002762 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2763 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002764 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002765 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002766 }
2767
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002768 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2769 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2770 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2771 if (!ctx->bitstream.vaddr) {
2772 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2773 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002774 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002775 }
2776 kfifo_init(&ctx->bitstream_fifo,
2777 ctx->bitstream.vaddr, ctx->bitstream.size);
2778 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002779 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002780
Javier Martin186b2502012-07-26 05:53:35 -03002781 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002782 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002783 coda_unlock(ctx);
2784
Javier Martin186b2502012-07-26 05:53:35 -03002785 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2786 ctx->idx, ctx);
2787
2788 return 0;
2789
Fabio Estevamf82bc202013-08-21 11:14:16 -03002790err_dma_writecombine:
2791 coda_free_context_buffers(ctx);
2792 if (ctx->dev->devtype->product == CODA_DX6)
2793 coda_free_aux_buf(dev, &ctx->workbuf);
2794 coda_free_aux_buf(dev, &ctx->parabuf);
2795err_dma_alloc:
2796 v4l2_ctrl_handler_free(&ctx->ctrls);
2797err_ctrls_setup:
2798 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2799err_ctx_init:
2800 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002801err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002802 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002803err_clk_per:
Javier Martin186b2502012-07-26 05:53:35 -03002804 v4l2_fh_del(&ctx->fh);
2805 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002806 clear_bit(ctx->idx, &dev->instance_mask);
2807err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002808 kfree(ctx);
2809 return ret;
2810}
2811
2812static int coda_release(struct file *file)
2813{
2814 struct coda_dev *dev = video_drvdata(file);
2815 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2816
2817 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2818 ctx);
2819
Philipp Zabel918c66f2013-06-27 06:59:01 -03002820 /* If this instance is running, call .job_abort and wait for it to end */
2821 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2822
2823 /* In case the instance was not running, we still need to call SEQ_END */
2824 mutex_lock(&dev->coda_mutex);
2825 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2826 "%s: sent command 'SEQ_END' to coda\n", __func__);
2827 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
2828 v4l2_err(&dev->v4l2_dev,
2829 "CODA_COMMAND_SEQ_END failed\n");
2830 mutex_unlock(&dev->coda_mutex);
2831 return -ETIMEDOUT;
2832 }
2833 mutex_unlock(&dev->coda_mutex);
2834
2835 coda_free_framebuffers(ctx);
2836
Javier Martin186b2502012-07-26 05:53:35 -03002837 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002838 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002839 coda_unlock(ctx);
2840
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002841 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2842 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002843 coda_free_context_buffers(ctx);
2844 if (ctx->dev->devtype->product == CODA_DX6)
2845 coda_free_aux_buf(dev, &ctx->workbuf);
2846
2847 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002848 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002849 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002850 clk_disable_unprepare(dev->clk_per);
Javier Martin186b2502012-07-26 05:53:35 -03002851 v4l2_fh_del(&ctx->fh);
2852 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002853 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002854 kfree(ctx);
2855
2856 return 0;
2857}
2858
2859static unsigned int coda_poll(struct file *file,
2860 struct poll_table_struct *wait)
2861{
2862 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2863 int ret;
2864
2865 coda_lock(ctx);
2866 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
2867 coda_unlock(ctx);
2868 return ret;
2869}
2870
2871static int coda_mmap(struct file *file, struct vm_area_struct *vma)
2872{
2873 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2874
2875 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2876}
2877
2878static const struct v4l2_file_operations coda_fops = {
2879 .owner = THIS_MODULE,
2880 .open = coda_open,
2881 .release = coda_release,
2882 .poll = coda_poll,
2883 .unlocked_ioctl = video_ioctl2,
2884 .mmap = coda_mmap,
2885};
2886
Philipp Zabel918c66f2013-06-27 06:59:01 -03002887static void coda_finish_decode(struct coda_ctx *ctx)
2888{
2889 struct coda_dev *dev = ctx->dev;
2890 struct coda_q_data *q_data_src;
2891 struct coda_q_data *q_data_dst;
2892 struct vb2_buffer *dst_buf;
2893 int width, height;
2894 int decoded_idx;
2895 int display_idx;
2896 u32 src_fourcc;
2897 int success;
2898 u32 val;
2899
2900 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2901
2902 /* Update kfifo out pointer from coda bitstream read pointer */
2903 coda_kfifo_sync_from_device(ctx);
2904
2905 /*
2906 * in stream-end mode, the read pointer can overshoot the write pointer
2907 * by up to 512 bytes
2908 */
2909 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2910 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2911 kfifo_init(&ctx->bitstream_fifo,
2912 ctx->bitstream.vaddr, ctx->bitstream.size);
2913 }
2914
2915 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2916 src_fourcc = q_data_src->fourcc;
2917
2918 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2919 if (val != 1)
2920 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2921
2922 success = val & 0x1;
2923 if (!success)
2924 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2925
2926 if (src_fourcc == V4L2_PIX_FMT_H264) {
2927 if (val & (1 << 3))
2928 v4l2_err(&dev->v4l2_dev,
2929 "insufficient PS buffer space (%d bytes)\n",
2930 ctx->psbuf.size);
2931 if (val & (1 << 2))
2932 v4l2_err(&dev->v4l2_dev,
2933 "insufficient slice buffer space (%d bytes)\n",
2934 ctx->slicebuf.size);
2935 }
2936
2937 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2938 width = (val >> 16) & 0xffff;
2939 height = val & 0xffff;
2940
2941 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2942
Philipp Zabel918c66f2013-06-27 06:59:01 -03002943 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2944 if (val > 0)
2945 v4l2_err(&dev->v4l2_dev,
2946 "errors in %d macroblocks\n", val);
2947
2948 if (dev->devtype->product == CODA_7541) {
2949 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2950 if (val == 0) {
2951 /* not enough bitstream data */
2952 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2953 "prescan failed: %d\n", val);
2954 ctx->prescan_failed = true;
2955 return;
2956 }
2957 }
2958
2959 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2960
2961 /*
2962 * The previous display frame was copied out by the rotator,
2963 * now it can be overwritten again
2964 */
2965 if (ctx->display_idx >= 0 &&
2966 ctx->display_idx < ctx->num_internal_frames) {
2967 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
2968 coda_write(dev, ctx->frm_dis_flg,
2969 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2970 }
2971
2972 /*
2973 * The index of the last decoded frame, not necessarily in
2974 * display order, and the index of the next display frame.
2975 * The latter could have been decoded in a previous run.
2976 */
2977 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
2978 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
2979
2980 if (decoded_idx == -1) {
2981 /* no frame was decoded, but we might have a display frame */
2982 if (display_idx < 0 && ctx->display_idx < 0)
2983 ctx->prescan_failed = true;
2984 } else if (decoded_idx == -2) {
2985 /* no frame was decoded, we still return the remaining buffers */
2986 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
2987 v4l2_err(&dev->v4l2_dev,
2988 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03002989 } else {
2990 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
2991 if (val == 0)
2992 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
2993 else if (val == 1)
2994 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
2995 else
2996 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002997 }
2998
2999 if (display_idx == -1) {
3000 /*
3001 * no more frames to be decoded, but there could still
3002 * be rotator output to dequeue
3003 */
3004 ctx->prescan_failed = true;
3005 } else if (display_idx == -3) {
3006 /* possibly prescan failure */
3007 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3008 v4l2_err(&dev->v4l2_dev,
3009 "presentation frame index out of range: %d\n",
3010 display_idx);
3011 }
3012
3013 /* If a frame was copied out, return it */
3014 if (ctx->display_idx >= 0 &&
3015 ctx->display_idx < ctx->num_internal_frames) {
3016 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
3017 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3018
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003019 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3020 V4L2_BUF_FLAG_PFRAME |
3021 V4L2_BUF_FLAG_BFRAME);
3022 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
3023
Philipp Zabel918c66f2013-06-27 06:59:01 -03003024 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3025
3026 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3027 VB2_BUF_STATE_ERROR);
3028
3029 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3030 "job finished: decoding frame (%d) (%s)\n",
3031 dst_buf->v4l2_buf.sequence,
3032 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3033 "KEYFRAME" : "PFRAME");
3034 } else {
3035 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3036 "job finished: no frame decoded\n");
3037 }
3038
3039 /* The rotator will copy the current display frame next time */
3040 ctx->display_idx = display_idx;
3041}
3042
3043static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003044{
Philipp Zabelec25f682012-07-20 08:54:29 -03003045 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003046 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003047 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003048
Philipp Zabelec25f682012-07-20 08:54:29 -03003049 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03003050 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003051
3052 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003053 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003054 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3055
Javier Martin186b2502012-07-26 05:53:35 -03003056 /* Calculate bytesused field */
3057 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003058 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3059 ctx->vpu_header_size[0] +
3060 ctx->vpu_header_size[1] +
3061 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003062 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003063 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003064 }
3065
3066 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3067 wr_ptr - start_ptr);
3068
3069 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3070 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3071
Philipp Zabel51660282013-09-30 10:34:49 -03003072 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003073 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3074 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3075 } else {
3076 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3077 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3078 }
3079
Kamil Debskiccd571c2013-04-24 10:38:24 -03003080 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003081 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3082 dst_buf->v4l2_buf.flags |=
3083 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003084 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3085
Philipp Zabelec25f682012-07-20 08:54:29 -03003086 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003087
3088 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003089 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3090
3091 ctx->gopcounter--;
3092 if (ctx->gopcounter < 0)
3093 ctx->gopcounter = ctx->params.gop_size - 1;
3094
3095 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3096 "job finished: encoding frame (%d) (%s)\n",
3097 dst_buf->v4l2_buf.sequence,
3098 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3099 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003100}
3101
3102static irqreturn_t coda_irq_handler(int irq, void *data)
3103{
3104 struct coda_dev *dev = data;
3105 struct coda_ctx *ctx;
3106
3107 cancel_delayed_work(&dev->timeout);
3108
3109 /* read status register to attend the IRQ */
3110 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3111 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3112 CODA_REG_BIT_INT_CLEAR);
3113
3114 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3115 if (ctx == NULL) {
3116 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3117 mutex_unlock(&dev->coda_mutex);
3118 return IRQ_HANDLED;
3119 }
3120
3121 if (ctx->aborting) {
3122 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3123 "task has been aborted\n");
Philipp Zabel918c66f2013-06-27 06:59:01 -03003124 goto out;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003125 }
3126
3127 if (coda_isbusy(ctx->dev)) {
3128 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3129 "coda is still busy!!!!\n");
3130 return IRQ_NONE;
3131 }
3132
Philipp Zabel918c66f2013-06-27 06:59:01 -03003133 if (ctx->inst_type == CODA_INST_DECODER)
3134 coda_finish_decode(ctx);
3135 else
3136 coda_finish_encode(ctx);
3137
3138out:
3139 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) {
3140 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3141 "%s: sent command 'SEQ_END' to coda\n", __func__);
3142 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
3143 v4l2_err(&dev->v4l2_dev,
3144 "CODA_COMMAND_SEQ_END failed\n");
3145 }
3146
3147 kfifo_init(&ctx->bitstream_fifo,
3148 ctx->bitstream.vaddr, ctx->bitstream.size);
3149
3150 coda_free_framebuffers(ctx);
3151 coda_free_context_buffers(ctx);
3152 }
Javier Martin186b2502012-07-26 05:53:35 -03003153
Philipp Zabelfcb62822013-05-23 10:43:00 -03003154 mutex_unlock(&dev->coda_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003155 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003156
Javier Martin186b2502012-07-26 05:53:35 -03003157 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
3158
3159 return IRQ_HANDLED;
3160}
3161
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003162static void coda_timeout(struct work_struct *work)
3163{
3164 struct coda_ctx *ctx;
3165 struct coda_dev *dev = container_of(to_delayed_work(work),
3166 struct coda_dev, timeout);
3167
Philipp Zabel39cc0292013-05-21 10:32:42 -03003168 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003169
3170 mutex_lock(&dev->dev_mutex);
3171 list_for_each_entry(ctx, &dev->instances, list) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03003172 if (mutex_is_locked(&ctx->buffer_mutex))
3173 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003174 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3175 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3176 }
3177 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003178
3179 mutex_unlock(&dev->coda_mutex);
3180 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3181 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003182}
3183
Javier Martin186b2502012-07-26 05:53:35 -03003184static u32 coda_supported_firmwares[] = {
3185 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003186 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003187 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003188};
3189
3190static bool coda_firmware_supported(u32 vernum)
3191{
3192 int i;
3193
3194 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3195 if (vernum == coda_supported_firmwares[i])
3196 return true;
3197 return false;
3198}
3199
Philipp Zabel87048bb2012-07-02 07:03:43 -03003200static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003201{
3202 u16 product, major, minor, release;
3203 u32 data;
3204 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003205 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003206
Fabio Estevam79830db2013-08-21 11:14:17 -03003207 ret = clk_prepare_enable(dev->clk_per);
3208 if (ret)
3209 return ret;
3210
3211 ret = clk_prepare_enable(dev->clk_ahb);
3212 if (ret)
3213 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003214
Javier Martin186b2502012-07-26 05:53:35 -03003215 /*
3216 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003217 * The 16-bit chars in the code buffer are in memory access
3218 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003219 * Data in this SRAM survives a reboot.
3220 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003221 p = (u16 *)dev->codebuf.vaddr;
3222 if (dev->devtype->product == CODA_DX6) {
3223 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3224 data = CODA_DOWN_ADDRESS_SET(i) |
3225 CODA_DOWN_DATA_SET(p[i ^ 1]);
3226 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3227 }
3228 } else {
3229 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3230 data = CODA_DOWN_ADDRESS_SET(i) |
3231 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3232 3 - (i % 4)]);
3233 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3234 }
Javier Martin186b2502012-07-26 05:53:35 -03003235 }
Javier Martin186b2502012-07-26 05:53:35 -03003236
Philipp Zabel9acf7692013-05-23 10:42:56 -03003237 /* Clear registers */
3238 for (i = 0; i < 64; i++)
3239 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3240
Javier Martin186b2502012-07-26 05:53:35 -03003241 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003242 if (dev->devtype->product == CODA_960 ||
3243 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003244 coda_write(dev, dev->tempbuf.paddr,
3245 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003246 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003247 } else {
3248 coda_write(dev, dev->workbuf.paddr,
3249 CODA_REG_BIT_WORK_BUF_ADDR);
3250 }
Javier Martin186b2502012-07-26 05:53:35 -03003251 coda_write(dev, dev->codebuf.paddr,
3252 CODA_REG_BIT_CODE_BUF_ADDR);
3253 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3254
3255 /* Set default values */
3256 switch (dev->devtype->product) {
3257 case CODA_DX6:
3258 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3259 break;
3260 default:
3261 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3262 }
Philipp Zabel89548442014-07-11 06:36:17 -03003263 if (dev->devtype->product == CODA_960)
3264 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3265 else
3266 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003267
3268 if (dev->devtype->product != CODA_DX6)
3269 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3270
Javier Martin186b2502012-07-26 05:53:35 -03003271 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3272 CODA_REG_BIT_INT_ENABLE);
3273
3274 /* Reset VPU and start processor */
3275 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3276 data |= CODA_REG_RESET_ENABLE;
3277 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3278 udelay(10);
3279 data &= ~CODA_REG_RESET_ENABLE;
3280 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3281 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3282
3283 /* Load firmware */
3284 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3285 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3286 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3287 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3288 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3289 if (coda_wait_timeout(dev)) {
3290 clk_disable_unprepare(dev->clk_per);
3291 clk_disable_unprepare(dev->clk_ahb);
3292 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
3293 return -EIO;
3294 }
3295
Philipp Zabel89548442014-07-11 06:36:17 -03003296 if (dev->devtype->product == CODA_960) {
3297 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3298 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3299 data);
3300 }
3301
Javier Martin186b2502012-07-26 05:53:35 -03003302 /* Check we are compatible with the loaded firmware */
3303 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3304 product = CODA_FIRMWARE_PRODUCT(data);
3305 major = CODA_FIRMWARE_MAJOR(data);
3306 minor = CODA_FIRMWARE_MINOR(data);
3307 release = CODA_FIRMWARE_RELEASE(data);
3308
3309 clk_disable_unprepare(dev->clk_per);
3310 clk_disable_unprepare(dev->clk_ahb);
3311
3312 if (product != dev->devtype->product) {
3313 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3314 " Version: %u.%u.%u\n",
3315 coda_product_name(dev->devtype->product),
3316 coda_product_name(product), major, minor, release);
3317 return -EINVAL;
3318 }
3319
3320 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3321 coda_product_name(product));
3322
3323 if (coda_firmware_supported(data)) {
3324 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3325 major, minor, release);
3326 } else {
3327 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3328 "%u.%u.%u\n", major, minor, release);
3329 }
3330
3331 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003332
3333err_clk_ahb:
3334 clk_disable_unprepare(dev->clk_per);
3335 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003336}
3337
3338static void coda_fw_callback(const struct firmware *fw, void *context)
3339{
3340 struct coda_dev *dev = context;
3341 struct platform_device *pdev = dev->plat_dev;
3342 int ret;
3343
3344 if (!fw) {
3345 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3346 return;
3347 }
3348
3349 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003350 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3351 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003352 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3353 return;
3354 }
3355
Philipp Zabel87048bb2012-07-02 07:03:43 -03003356 /* Copy the whole firmware image to the code buffer */
3357 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3358 release_firmware(fw);
3359
3360 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03003361 if (ret) {
3362 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3363 return;
3364 }
3365
3366 dev->vfd.fops = &coda_fops,
3367 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3368 dev->vfd.release = video_device_release_empty,
3369 dev->vfd.lock = &dev->dev_mutex;
3370 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003371 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003372 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3373 video_set_drvdata(&dev->vfd, dev);
3374
3375 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3376 if (IS_ERR(dev->alloc_ctx)) {
3377 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3378 return;
3379 }
3380
3381 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3382 if (IS_ERR(dev->m2m_dev)) {
3383 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3384 goto rel_ctx;
3385 }
3386
3387 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3388 if (ret) {
3389 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3390 goto rel_m2m;
3391 }
3392 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3393 dev->vfd.num);
3394
3395 return;
3396
3397rel_m2m:
3398 v4l2_m2m_release(dev->m2m_dev);
3399rel_ctx:
3400 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3401}
3402
3403static int coda_firmware_request(struct coda_dev *dev)
3404{
3405 char *fw = dev->devtype->firmware;
3406
3407 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3408 coda_product_name(dev->devtype->product));
3409
3410 return request_firmware_nowait(THIS_MODULE, true,
3411 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3412}
3413
3414enum coda_platform {
3415 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003416 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003417 CODA_IMX6Q,
3418 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003419};
3420
Emil Goodec06d8752012-08-14 17:44:42 -03003421static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003422 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003423 .firmware = "v4l-codadx6-imx27.bin",
3424 .product = CODA_DX6,
3425 .codecs = codadx6_codecs,
3426 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003427 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003428 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003429 .firmware = "v4l-coda7541-imx53.bin",
3430 .product = CODA_7541,
3431 .codecs = coda7_codecs,
3432 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003433 },
Philipp Zabel89548442014-07-11 06:36:17 -03003434 [CODA_IMX6Q] = {
3435 .firmware = "v4l-coda960-imx6q.bin",
3436 .product = CODA_960,
3437 .codecs = coda9_codecs,
3438 .num_codecs = ARRAY_SIZE(coda9_codecs),
3439 },
3440 [CODA_IMX6DL] = {
3441 .firmware = "v4l-coda960-imx6dl.bin",
3442 .product = CODA_960,
3443 .codecs = coda9_codecs,
3444 .num_codecs = ARRAY_SIZE(coda9_codecs),
3445 },
Javier Martin186b2502012-07-26 05:53:35 -03003446};
3447
3448static struct platform_device_id coda_platform_ids[] = {
3449 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003450 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003451 { /* sentinel */ }
3452};
3453MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3454
3455#ifdef CONFIG_OF
3456static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003457 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003458 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003459 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3460 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003461 { /* sentinel */ }
3462};
3463MODULE_DEVICE_TABLE(of, coda_dt_ids);
3464#endif
3465
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003466static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003467{
3468 const struct of_device_id *of_id =
3469 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3470 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003471 struct coda_platform_data *pdata = pdev->dev.platform_data;
3472 struct device_node *np = pdev->dev.of_node;
3473 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003474 struct coda_dev *dev;
3475 struct resource *res;
3476 int ret, irq;
3477
3478 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3479 if (!dev) {
3480 dev_err(&pdev->dev, "Not enough memory for %s\n",
3481 CODA_NAME);
3482 return -ENOMEM;
3483 }
3484
3485 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003486 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003487 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03003488
3489 dev->plat_dev = pdev;
3490 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3491 if (IS_ERR(dev->clk_per)) {
3492 dev_err(&pdev->dev, "Could not get per clock\n");
3493 return PTR_ERR(dev->clk_per);
3494 }
3495
3496 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3497 if (IS_ERR(dev->clk_ahb)) {
3498 dev_err(&pdev->dev, "Could not get ahb clock\n");
3499 return PTR_ERR(dev->clk_ahb);
3500 }
3501
3502 /* Get memory for physical registers */
3503 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003504 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3505 if (IS_ERR(dev->regs_base))
3506 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003507
3508 /* IRQ */
3509 irq = platform_get_irq(pdev, 0);
3510 if (irq < 0) {
3511 dev_err(&pdev->dev, "failed to get irq resource\n");
3512 return -ENOENT;
3513 }
3514
Philipp Zabel918c66f2013-06-27 06:59:01 -03003515 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003516 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003517 dev_err(&pdev->dev, "failed to request irq\n");
3518 return -ENOENT;
3519 }
3520
Philipp Zabel657eee72013-04-29 16:17:14 -07003521 /* Get IRAM pool from device tree or platform data */
3522 pool = of_get_named_gen_pool(np, "iram", 0);
3523 if (!pool && pdata)
3524 pool = dev_get_gen_pool(pdata->iram_dev);
3525 if (!pool) {
3526 dev_err(&pdev->dev, "iram pool not available\n");
3527 return -ENOMEM;
3528 }
3529 dev->iram_pool = pool;
3530
Javier Martin186b2502012-07-26 05:53:35 -03003531 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3532 if (ret)
3533 return ret;
3534
3535 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003536 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003537
3538 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3539
3540 if (of_id) {
3541 dev->devtype = of_id->data;
3542 } else if (pdev_id) {
3543 dev->devtype = &coda_devdata[pdev_id->driver_data];
3544 } else {
3545 v4l2_device_unregister(&dev->v4l2_dev);
3546 return -EINVAL;
3547 }
3548
3549 /* allocate auxiliary per-device buffers for the BIT processor */
3550 switch (dev->devtype->product) {
3551 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003552 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3553 CODADX6_WORK_BUF_SIZE);
3554 if (ret < 0) {
3555 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3556 v4l2_device_unregister(&dev->v4l2_dev);
3557 return ret;
3558 }
Javier Martin186b2502012-07-26 05:53:35 -03003559 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003560 case CODA_7541:
3561 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3562 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003563 case CODA_960:
3564 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3565 break;
Javier Martin186b2502012-07-26 05:53:35 -03003566 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003567 if (dev->tempbuf.size) {
3568 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3569 dev->tempbuf.size);
3570 if (ret < 0) {
3571 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3572 v4l2_device_unregister(&dev->v4l2_dev);
3573 return ret;
3574 }
Javier Martin186b2502012-07-26 05:53:35 -03003575 }
3576
Philipp Zabel918c66f2013-06-27 06:59:01 -03003577 switch (dev->devtype->product) {
3578 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003579 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003580 break;
3581 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003582 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003583 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003584 case CODA_960:
3585 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003586 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003587 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3588 &dev->iram.paddr);
3589 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003590 dev_err(&pdev->dev, "unable to alloc iram\n");
3591 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003592 }
3593
Javier Martin186b2502012-07-26 05:53:35 -03003594 platform_set_drvdata(pdev, dev);
3595
3596 return coda_firmware_request(dev);
3597}
3598
3599static int coda_remove(struct platform_device *pdev)
3600{
3601 struct coda_dev *dev = platform_get_drvdata(pdev);
3602
3603 video_unregister_device(&dev->vfd);
3604 if (dev->m2m_dev)
3605 v4l2_m2m_release(dev->m2m_dev);
3606 if (dev->alloc_ctx)
3607 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3608 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003609 if (dev->iram.vaddr)
3610 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3611 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003612 coda_free_aux_buf(dev, &dev->codebuf);
3613 coda_free_aux_buf(dev, &dev->tempbuf);
3614 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003615 return 0;
3616}
3617
3618static struct platform_driver coda_driver = {
3619 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003620 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003621 .driver = {
3622 .name = CODA_NAME,
3623 .owner = THIS_MODULE,
3624 .of_match_table = of_match_ptr(coda_dt_ids),
3625 },
3626 .id_table = coda_platform_ids,
3627};
3628
3629module_platform_driver(coda_driver);
3630
3631MODULE_LICENSE("GPL");
3632MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3633MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");