blob: 782b09e5aa52d98bc18d4b65ae3ed7aed2a62f80 [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;
Philipp Zabel52c41672014-07-11 06:36:19 -0300122 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300123};
124
125struct coda_aux_buf {
126 void *vaddr;
127 dma_addr_t paddr;
128 u32 size;
129};
130
131struct coda_dev {
132 struct v4l2_device v4l2_dev;
133 struct video_device vfd;
134 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300135 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300136
137 void __iomem *regs_base;
138 struct clk *clk_per;
139 struct clk *clk_ahb;
140
141 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300142 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300143 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700144 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300145 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300146
147 spinlock_t irqlock;
148 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300149 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300150 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300151 struct v4l2_m2m_dev *m2m_dev;
152 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300153 struct list_head instances;
154 unsigned long instance_mask;
Javier Martin186b2502012-07-26 05:53:35 -0300155};
156
157struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300158 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300159 u8 h264_intra_qp;
160 u8 h264_inter_qp;
161 u8 mpeg4_intra_qp;
162 u8 mpeg4_inter_qp;
163 u8 gop_size;
164 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300165 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300166 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
167 u32 framerate;
168 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300169 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300170 u32 slice_max_mb;
171};
172
Philipp Zabelc2d22512013-06-21 03:55:28 -0300173struct coda_iram_info {
174 u32 axi_sram_use;
175 phys_addr_t buf_bit_use;
176 phys_addr_t buf_ip_ac_dc_use;
177 phys_addr_t buf_dbk_y_use;
178 phys_addr_t buf_dbk_c_use;
179 phys_addr_t buf_ovl_use;
180 phys_addr_t buf_btp_use;
181 phys_addr_t search_ram_paddr;
182 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300183 int remaining;
184 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300185};
186
Philipp Zabel89548442014-07-11 06:36:17 -0300187struct gdi_tiled_map {
188 int xy2ca_map[16];
189 int xy2ba_map[16];
190 int xy2ra_map[16];
191 int rbc2axi_map[32];
192 int xy2rbc_config;
193 int map_type;
194#define GDI_LINEAR_FRAME_MAP 0
195};
196
Javier Martin186b2502012-07-26 05:53:35 -0300197struct coda_ctx {
198 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300199 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300200 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300201 struct work_struct pic_run_work;
202 struct work_struct seq_end_work;
203 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300204 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300205 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300206 int streamon_out;
207 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300208 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300209 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300210 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300211 struct coda_q_data q_data[2];
212 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300213 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300214 enum v4l2_colorspace colorspace;
215 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300216 struct v4l2_ctrl_handler ctrls;
217 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300218 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300219 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300220 char vpu_header[3][64];
221 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300222 struct kfifo bitstream_fifo;
223 struct mutex bitstream_mutex;
224 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300225 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300226 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300227 struct coda_aux_buf psbuf;
228 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300229 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300230 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300231 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300232 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300233 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300234 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300235 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300236 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300237 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300238 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300239 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300240 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300241};
242
Javier Martin832fbb52012-10-29 08:34:59 -0300243static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
244 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
245static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300246
Javier Martin186b2502012-07-26 05:53:35 -0300247static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
248{
249 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
250 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
251 writel(data, dev->regs_base + reg);
252}
253
254static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
255{
256 u32 data;
257 data = readl(dev->regs_base + reg);
258 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
259 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
260 return data;
261}
262
263static inline unsigned long coda_isbusy(struct coda_dev *dev)
264{
265 return coda_read(dev, CODA_REG_BIT_BUSY);
266}
267
268static inline int coda_is_initialized(struct coda_dev *dev)
269{
270 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
271}
272
273static int coda_wait_timeout(struct coda_dev *dev)
274{
275 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
276
277 while (coda_isbusy(dev)) {
278 if (time_after(jiffies, timeout))
279 return -ETIMEDOUT;
280 }
281 return 0;
282}
283
284static void coda_command_async(struct coda_ctx *ctx, int cmd)
285{
286 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300287
Philipp Zabel89548442014-07-11 06:36:17 -0300288 if (dev->devtype->product == CODA_960 ||
289 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300290 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300291 coda_write(dev, ctx->bit_stream_param,
292 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300293 coda_write(dev, ctx->frm_dis_flg,
294 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300295 coda_write(dev, ctx->frame_mem_ctrl,
296 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300297 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
298 }
299
Philipp Zabel89548442014-07-11 06:36:17 -0300300 if (dev->devtype->product == CODA_960) {
301 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
302 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
303 }
304
Javier Martin186b2502012-07-26 05:53:35 -0300305 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
306
307 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
308 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300309 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
310
Javier Martin186b2502012-07-26 05:53:35 -0300311 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
312}
313
314static int coda_command_sync(struct coda_ctx *ctx, int cmd)
315{
316 struct coda_dev *dev = ctx->dev;
317
318 coda_command_async(ctx, cmd);
319 return coda_wait_timeout(dev);
320}
321
322static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
323 enum v4l2_buf_type type)
324{
325 switch (type) {
326 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
327 return &(ctx->q_data[V4L2_M2M_SRC]);
328 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
329 return &(ctx->q_data[V4L2_M2M_DST]);
330 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300331 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300332 }
Javier Martin186b2502012-07-26 05:53:35 -0300333}
334
335/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300336 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300337 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300338static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300339 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300340 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300341 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300342 },
343 {
344 .name = "YUV 4:2:0 Planar, YCrCb",
345 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300346 },
347 {
348 .name = "H264 Encoded Stream",
349 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300350 },
351 {
352 .name = "MPEG4 Encoded Stream",
353 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300354 },
355};
356
Philipp Zabelb96904e2013-05-23 10:42:58 -0300357#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
358 { mode, src_fourcc, dst_fourcc, max_w, max_h }
359
360/*
361 * Arrays of codecs supported by each given version of Coda:
362 * i.MX27 -> codadx6
363 * i.MX5x -> coda7
364 * i.MX6 -> coda960
365 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
366 */
367static struct coda_codec codadx6_codecs[] = {
368 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
369 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300370};
371
Philipp Zabelb96904e2013-05-23 10:42:58 -0300372static struct coda_codec coda7_codecs[] = {
373 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
374 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300375 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
376 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300377};
378
Philipp Zabel89548442014-07-11 06:36:17 -0300379static struct coda_codec coda9_codecs[] = {
380 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
381 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
382 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
383 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
384};
385
Philipp Zabelb96904e2013-05-23 10:42:58 -0300386static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300387{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300388 switch (fourcc) {
389 case V4L2_PIX_FMT_YUV420:
390 case V4L2_PIX_FMT_YVU420:
391 return true;
392 default:
393 return false;
394 }
395}
Javier Martin186b2502012-07-26 05:53:35 -0300396
Philipp Zabelb96904e2013-05-23 10:42:58 -0300397/*
398 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
399 * tables.
400 */
401static u32 coda_format_normalize_yuv(u32 fourcc)
402{
403 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
404}
405
406static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
407 int dst_fourcc)
408{
409 struct coda_codec *codecs = dev->devtype->codecs;
410 int num_codecs = dev->devtype->num_codecs;
411 int k;
412
413 src_fourcc = coda_format_normalize_yuv(src_fourcc);
414 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
415 if (src_fourcc == dst_fourcc)
416 return NULL;
417
418 for (k = 0; k < num_codecs; k++) {
419 if (codecs[k].src_fourcc == src_fourcc &&
420 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300421 break;
422 }
423
Philipp Zabelb96904e2013-05-23 10:42:58 -0300424 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300425 return NULL;
426
Philipp Zabelb96904e2013-05-23 10:42:58 -0300427 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300428}
429
Philipp Zabel57625592013-09-30 10:34:51 -0300430static void coda_get_max_dimensions(struct coda_dev *dev,
431 struct coda_codec *codec,
432 int *max_w, int *max_h)
433{
434 struct coda_codec *codecs = dev->devtype->codecs;
435 int num_codecs = dev->devtype->num_codecs;
436 unsigned int w, h;
437 int k;
438
439 if (codec) {
440 w = codec->max_w;
441 h = codec->max_h;
442 } else {
443 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
444 w = max(w, codecs[k].max_w);
445 h = max(h, codecs[k].max_h);
446 }
447 }
448
449 if (max_w)
450 *max_w = w;
451 if (max_h)
452 *max_h = h;
453}
454
Philipp Zabel927933f2013-09-30 10:34:48 -0300455static char *coda_product_name(int product)
456{
457 static char buf[9];
458
459 switch (product) {
460 case CODA_DX6:
461 return "CodaDx6";
462 case CODA_7541:
463 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300464 case CODA_960:
465 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300466 default:
467 snprintf(buf, sizeof(buf), "(0x%04x)", product);
468 return buf;
469 }
470}
471
Javier Martin186b2502012-07-26 05:53:35 -0300472/*
473 * V4L2 ioctl() operations.
474 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300475static int coda_querycap(struct file *file, void *priv,
476 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300477{
Philipp Zabel927933f2013-09-30 10:34:48 -0300478 struct coda_ctx *ctx = fh_to_ctx(priv);
479
Javier Martin186b2502012-07-26 05:53:35 -0300480 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300481 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
482 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300483 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300484 /*
485 * This is only a mem-to-mem video device. The capture and output
486 * device capability flags are left only for backward compatibility
487 * and are scheduled for removal.
488 */
489 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
490 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300491 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
492
493 return 0;
494}
495
496static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300497 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300498{
499 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300500 struct coda_codec *codecs = ctx->dev->devtype->codecs;
501 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300502 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300503 int num_codecs = ctx->dev->devtype->num_codecs;
504 int num_formats = ARRAY_SIZE(coda_formats);
505 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300506
507 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300508 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300509 if (coda_format_is_yuv(formats[i].fourcc) &&
510 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300511 if (num == f->index)
512 break;
513 ++num;
514 continue;
515 }
516 /* Compressed formats may be supported, check the codec list */
517 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300518 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300519 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300520 formats[i].fourcc == codecs[k].dst_fourcc &&
521 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300522 break;
523 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
524 formats[i].fourcc == codecs[k].src_fourcc)
525 break;
526 }
527 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300528 if (num == f->index)
529 break;
530 ++num;
531 }
532 }
533
534 if (i < num_formats) {
535 fmt = &formats[i];
536 strlcpy(f->description, fmt->name, sizeof(f->description));
537 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300538 if (!coda_format_is_yuv(fmt->fourcc))
539 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300540 return 0;
541 }
542
543 /* Format not found */
544 return -EINVAL;
545}
546
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300547static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
548 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300549{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300550 struct coda_ctx *ctx = fh_to_ctx(priv);
551 struct vb2_queue *src_vq;
552 struct coda_q_data *q_data_src;
553
554 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300555 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300556 if (vb2_is_streaming(src_vq)) {
557 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
558
559 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
560 q_data_src->fourcc);
561 }
562
563 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300564}
565
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300566static int coda_enum_fmt_vid_out(struct file *file, void *priv,
567 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300568{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300569 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300570}
571
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300572static int coda_g_fmt(struct file *file, void *priv,
573 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300574{
Javier Martin186b2502012-07-26 05:53:35 -0300575 struct coda_q_data *q_data;
576 struct coda_ctx *ctx = fh_to_ctx(priv);
577
Javier Martin186b2502012-07-26 05:53:35 -0300578 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300579 if (!q_data)
580 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300581
582 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300583 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300584 f->fmt.pix.width = q_data->width;
585 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300586 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300587 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
588 else /* encoded formats h.264/mpeg4 */
589 f->fmt.pix.bytesperline = 0;
590
591 f->fmt.pix.sizeimage = q_data->sizeimage;
592 f->fmt.pix.colorspace = ctx->colorspace;
593
594 return 0;
595}
596
Philipp Zabel57625592013-09-30 10:34:51 -0300597static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
598 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300599{
Philipp Zabel57625592013-09-30 10:34:51 -0300600 struct coda_dev *dev = ctx->dev;
601 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300602 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300603 enum v4l2_field field;
604
605 field = f->fmt.pix.field;
606 if (field == V4L2_FIELD_ANY)
607 field = V4L2_FIELD_NONE;
608 else if (V4L2_FIELD_NONE != field)
609 return -EINVAL;
610
611 /* V4L2 specification suggests the driver corrects the format struct
612 * if any of the dimensions is unsupported */
613 f->fmt.pix.field = field;
614
Philipp Zabel57625592013-09-30 10:34:51 -0300615 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
616 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
617 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
618 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300619
Philipp Zabel57625592013-09-30 10:34:51 -0300620 switch (f->fmt.pix.pixelformat) {
621 case V4L2_PIX_FMT_YUV420:
622 case V4L2_PIX_FMT_YVU420:
623 case V4L2_PIX_FMT_H264:
624 case V4L2_PIX_FMT_MPEG4:
625 case V4L2_PIX_FMT_JPEG:
626 break;
627 default:
628 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300629 if (!q_data)
630 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300631 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 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300667 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300668 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
Philipp Zabel14604e32014-07-11 06:36:22 -0300721 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300722 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;
Philipp Zabel52c41672014-07-11 06:36:19 -0300738 q_data->rect.left = 0;
739 q_data->rect.top = 0;
740 q_data->rect.width = f->fmt.pix.width;
741 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300742
743 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
744 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300745 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300746
747 return 0;
748}
749
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300750static int coda_s_fmt_vid_cap(struct file *file, void *priv,
751 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300752{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300753 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300754 int ret;
755
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300756 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300757 if (ret)
758 return ret;
759
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300760 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300761}
762
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300763static int coda_s_fmt_vid_out(struct file *file, void *priv,
764 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300765{
766 struct coda_ctx *ctx = fh_to_ctx(priv);
767 int ret;
768
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300769 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300770 if (ret)
771 return ret;
772
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300773 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300774 if (ret)
775 ctx->colorspace = f->fmt.pix.colorspace;
776
777 return ret;
778}
779
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300780static int coda_qbuf(struct file *file, void *priv,
781 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300782{
783 struct coda_ctx *ctx = fh_to_ctx(priv);
784
Philipp Zabel14604e32014-07-11 06:36:22 -0300785 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300786}
787
Philipp Zabel918c66f2013-06-27 06:59:01 -0300788static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
789 struct v4l2_buffer *buf)
790{
791 struct vb2_queue *src_vq;
792
Philipp Zabel14604e32014-07-11 06:36:22 -0300793 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300794
795 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
796 (buf->sequence == (ctx->qsequence - 1)));
797}
798
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300799static int coda_dqbuf(struct file *file, void *priv,
800 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300801{
802 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300803 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300804
Philipp Zabel14604e32014-07-11 06:36:22 -0300805 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300806
807 /* If this is the last capture buffer, emit an end-of-stream event */
808 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
809 coda_buf_is_end_of_stream(ctx, buf)) {
810 const struct v4l2_event eos_event = {
811 .type = V4L2_EVENT_EOS
812 };
813
814 v4l2_event_queue_fh(&ctx->fh, &eos_event);
815 }
816
817 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300818}
819
Philipp Zabel52c41672014-07-11 06:36:19 -0300820static int coda_g_selection(struct file *file, void *fh,
821 struct v4l2_selection *s)
822{
823 struct coda_ctx *ctx = fh_to_ctx(fh);
824 struct coda_q_data *q_data;
825 struct v4l2_rect r, *rsel;
826
827 q_data = get_q_data(ctx, s->type);
828 if (!q_data)
829 return -EINVAL;
830
831 r.left = 0;
832 r.top = 0;
833 r.width = q_data->width;
834 r.height = q_data->height;
835 rsel = &q_data->rect;
836
837 switch (s->target) {
838 case V4L2_SEL_TGT_CROP_DEFAULT:
839 case V4L2_SEL_TGT_CROP_BOUNDS:
840 rsel = &r;
841 /* fallthrough */
842 case V4L2_SEL_TGT_CROP:
843 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
844 return -EINVAL;
845 break;
846 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
847 case V4L2_SEL_TGT_COMPOSE_PADDED:
848 rsel = &r;
849 /* fallthrough */
850 case V4L2_SEL_TGT_COMPOSE:
851 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
852 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
853 return -EINVAL;
854 break;
855 default:
856 return -EINVAL;
857 }
858
859 s->r = *rsel;
860
861 return 0;
862}
863
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300864static int coda_try_decoder_cmd(struct file *file, void *fh,
865 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300866{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300867 if (dc->cmd != V4L2_DEC_CMD_STOP)
868 return -EINVAL;
869
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300870 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300871 return -EINVAL;
872
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300873 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300874 return -EINVAL;
875
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300876 return 0;
877}
878
879static int coda_decoder_cmd(struct file *file, void *fh,
880 struct v4l2_decoder_cmd *dc)
881{
882 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300883 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300884 int ret;
885
886 ret = coda_try_decoder_cmd(file, fh, dc);
887 if (ret < 0)
888 return ret;
889
890 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300891 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300892 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300893
894 /* Set the strem-end flag on this context */
895 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
896
Philipp Zabel89548442014-07-11 06:36:17 -0300897 if ((dev->devtype->product == CODA_960) &&
898 coda_isbusy(dev) &&
899 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
900 /* If this context is currently running, update the hardware flag */
901 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
902 }
903
Philipp Zabel918c66f2013-06-27 06:59:01 -0300904 return 0;
905}
906
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300907static int coda_subscribe_event(struct v4l2_fh *fh,
908 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300909{
910 switch (sub->type) {
911 case V4L2_EVENT_EOS:
912 return v4l2_event_subscribe(fh, sub, 0, NULL);
913 default:
914 return v4l2_ctrl_subscribe_event(fh, sub);
915 }
Javier Martin186b2502012-07-26 05:53:35 -0300916}
917
918static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300919 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300920
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300921 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
922 .vidioc_g_fmt_vid_cap = coda_g_fmt,
923 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
924 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300925
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300926 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
927 .vidioc_g_fmt_vid_out = coda_g_fmt,
928 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
929 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300930
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300931 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
932 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300933
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300934 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300935 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300936 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300937 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300938
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300939 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
940 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300941
Philipp Zabel52c41672014-07-11 06:36:19 -0300942 .vidioc_g_selection = coda_g_selection,
943
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300944 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300945 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300946
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300947 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300948 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300949};
950
Philipp Zabel918c66f2013-06-27 06:59:01 -0300951static int coda_start_decoding(struct coda_ctx *ctx);
952
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300953static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
954{
955 return kfifo_len(&ctx->bitstream_fifo);
956}
957
958static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
959{
960 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
961 struct coda_dev *dev = ctx->dev;
962 u32 rd_ptr;
963
964 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
965 kfifo->out = (kfifo->in & ~kfifo->mask) |
966 (rd_ptr - ctx->bitstream.paddr);
967 if (kfifo->out > kfifo->in)
968 kfifo->out -= kfifo->mask + 1;
969}
970
971static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
972{
973 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
974 struct coda_dev *dev = ctx->dev;
975 u32 rd_ptr, wr_ptr;
976
977 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
978 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
979 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
980 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
981}
982
983static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
984{
985 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
986 struct coda_dev *dev = ctx->dev;
987 u32 wr_ptr;
988
989 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
990 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
991}
992
993static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
994{
995 u32 src_size = vb2_get_plane_payload(src_buf, 0);
996 u32 n;
997
998 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
999 if (n < src_size)
1000 return -ENOSPC;
1001
1002 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1003 ctx->bitstream.size, DMA_TO_DEVICE);
1004
1005 ctx->qsequence++;
1006
1007 return 0;
1008}
1009
1010static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1011 struct vb2_buffer *src_buf)
1012{
1013 int ret;
1014
1015 if (coda_get_bitstream_payload(ctx) +
1016 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1017 return false;
1018
1019 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1020 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1021 return true;
1022 }
1023
1024 ret = coda_bitstream_queue(ctx, src_buf);
1025 if (ret < 0) {
1026 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1027 return false;
1028 }
1029 /* Sync read pointer to device */
1030 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1031 coda_kfifo_sync_to_device_write(ctx);
1032
Philipp Zabel918c66f2013-06-27 06:59:01 -03001033 ctx->prescan_failed = false;
1034
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001035 return true;
1036}
1037
1038static void coda_fill_bitstream(struct coda_ctx *ctx)
1039{
1040 struct vb2_buffer *src_buf;
1041
Philipp Zabel14604e32014-07-11 06:36:22 -03001042 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1043 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001044
1045 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel14604e32014-07-11 06:36:22 -03001046 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001047 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1048 } else {
1049 break;
1050 }
1051 }
1052}
1053
Philipp Zabel89548442014-07-11 06:36:17 -03001054static void coda_set_gdi_regs(struct coda_ctx *ctx)
1055{
1056 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1057 struct coda_dev *dev = ctx->dev;
1058 int i;
1059
1060 for (i = 0; i < 16; i++)
1061 coda_write(dev, tiled_map->xy2ca_map[i],
1062 CODA9_GDI_XY2_CAS_0 + 4 * i);
1063 for (i = 0; i < 4; i++)
1064 coda_write(dev, tiled_map->xy2ba_map[i],
1065 CODA9_GDI_XY2_BA_0 + 4 * i);
1066 for (i = 0; i < 16; i++)
1067 coda_write(dev, tiled_map->xy2ra_map[i],
1068 CODA9_GDI_XY2_RAS_0 + 4 * i);
1069 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1070 for (i = 0; i < 32; i++)
1071 coda_write(dev, tiled_map->rbc2axi_map[i],
1072 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1073}
1074
Javier Martin186b2502012-07-26 05:53:35 -03001075/*
1076 * Mem-to-mem operations.
1077 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001078static int coda_prepare_decode(struct coda_ctx *ctx)
1079{
1080 struct vb2_buffer *dst_buf;
1081 struct coda_dev *dev = ctx->dev;
1082 struct coda_q_data *q_data_dst;
1083 u32 stridey, height;
1084 u32 picture_y, picture_cb, picture_cr;
1085
Philipp Zabel14604e32014-07-11 06:36:22 -03001086 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001087 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1088
1089 if (ctx->params.rot_mode & CODA_ROT_90) {
1090 stridey = q_data_dst->height;
1091 height = q_data_dst->width;
1092 } else {
1093 stridey = q_data_dst->width;
1094 height = q_data_dst->height;
1095 }
1096
1097 /* Try to copy source buffer contents into the bitstream ringbuffer */
1098 mutex_lock(&ctx->bitstream_mutex);
1099 coda_fill_bitstream(ctx);
1100 mutex_unlock(&ctx->bitstream_mutex);
1101
1102 if (coda_get_bitstream_payload(ctx) < 512 &&
1103 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1104 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1105 "bitstream payload: %d, skipping\n",
1106 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001107 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001108 return -EAGAIN;
1109 }
1110
1111 /* Run coda_start_decoding (again) if not yet initialized */
1112 if (!ctx->initialized) {
1113 int ret = coda_start_decoding(ctx);
1114 if (ret < 0) {
1115 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001116 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001117 return -EAGAIN;
1118 } else {
1119 ctx->initialized = 1;
1120 }
1121 }
1122
Philipp Zabel89548442014-07-11 06:36:17 -03001123 if (dev->devtype->product == CODA_960)
1124 coda_set_gdi_regs(ctx);
1125
Philipp Zabel918c66f2013-06-27 06:59:01 -03001126 /* Set rotator output */
1127 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1128 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1129 /* Switch Cr and Cb for YVU420 format */
1130 picture_cr = picture_y + stridey * height;
1131 picture_cb = picture_cr + stridey / 2 * height / 2;
1132 } else {
1133 picture_cb = picture_y + stridey * height;
1134 picture_cr = picture_cb + stridey / 2 * height / 2;
1135 }
Philipp Zabel89548442014-07-11 06:36:17 -03001136
1137 if (dev->devtype->product == CODA_960) {
1138 /*
1139 * The CODA960 seems to have an internal list of buffers with
1140 * 64 entries that includes the registered frame buffers as
1141 * well as the rotator buffer output.
1142 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1143 */
1144 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1145 CODA9_CMD_DEC_PIC_ROT_INDEX);
1146 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1147 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1148 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1149 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1150 } else {
1151 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1152 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1153 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1154 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1155 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001156 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1157 CODA_CMD_DEC_PIC_ROT_MODE);
1158
1159 switch (dev->devtype->product) {
1160 case CODA_DX6:
1161 /* TBD */
1162 case CODA_7541:
1163 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1164 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001165 case CODA_960:
1166 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1167 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001168 }
1169
1170 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1171
1172 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1173 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1174
1175 return 0;
1176}
1177
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001178static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001179{
Javier Martin186b2502012-07-26 05:53:35 -03001180 struct coda_q_data *q_data_src, *q_data_dst;
1181 struct vb2_buffer *src_buf, *dst_buf;
1182 struct coda_dev *dev = ctx->dev;
1183 int force_ipicture;
1184 int quant_param = 0;
1185 u32 picture_y, picture_cb, picture_cr;
1186 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1187 u32 dst_fourcc;
1188
Philipp Zabel14604e32014-07-11 06:36:22 -03001189 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1190 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001191 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1192 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001193 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001194
Philipp Zabel918c66f2013-06-27 06:59:01 -03001195 src_buf->v4l2_buf.sequence = ctx->osequence;
1196 dst_buf->v4l2_buf.sequence = ctx->osequence;
1197 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001198
1199 /*
1200 * Workaround coda firmware BUG that only marks the first
1201 * frame as IDR. This is a problem for some decoders that can't
1202 * recover when a frame is lost.
1203 */
1204 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1205 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1206 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1207 } else {
1208 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1209 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1210 }
1211
Philipp Zabel89548442014-07-11 06:36:17 -03001212 if (dev->devtype->product == CODA_960)
1213 coda_set_gdi_regs(ctx);
1214
Javier Martin186b2502012-07-26 05:53:35 -03001215 /*
1216 * Copy headers at the beginning of the first frame for H.264 only.
1217 * In MPEG4 they are already copied by the coda.
1218 */
1219 if (src_buf->v4l2_buf.sequence == 0) {
1220 pic_stream_buffer_addr =
1221 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1222 ctx->vpu_header_size[0] +
1223 ctx->vpu_header_size[1] +
1224 ctx->vpu_header_size[2];
1225 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1226 ctx->vpu_header_size[0] -
1227 ctx->vpu_header_size[1] -
1228 ctx->vpu_header_size[2];
1229 memcpy(vb2_plane_vaddr(dst_buf, 0),
1230 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1231 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1232 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1233 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1234 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1235 ctx->vpu_header_size[2]);
1236 } else {
1237 pic_stream_buffer_addr =
1238 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1239 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1240 }
1241
1242 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1243 force_ipicture = 1;
1244 switch (dst_fourcc) {
1245 case V4L2_PIX_FMT_H264:
1246 quant_param = ctx->params.h264_intra_qp;
1247 break;
1248 case V4L2_PIX_FMT_MPEG4:
1249 quant_param = ctx->params.mpeg4_intra_qp;
1250 break;
1251 default:
1252 v4l2_warn(&ctx->dev->v4l2_dev,
1253 "cannot set intra qp, fmt not supported\n");
1254 break;
1255 }
1256 } else {
1257 force_ipicture = 0;
1258 switch (dst_fourcc) {
1259 case V4L2_PIX_FMT_H264:
1260 quant_param = ctx->params.h264_inter_qp;
1261 break;
1262 case V4L2_PIX_FMT_MPEG4:
1263 quant_param = ctx->params.mpeg4_inter_qp;
1264 break;
1265 default:
1266 v4l2_warn(&ctx->dev->v4l2_dev,
1267 "cannot set inter qp, fmt not supported\n");
1268 break;
1269 }
1270 }
1271
1272 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001273 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001274 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1275
1276
1277 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001278 switch (q_data_src->fourcc) {
1279 case V4L2_PIX_FMT_YVU420:
1280 /* Switch Cb and Cr for YVU420 format */
1281 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1282 picture_cb = picture_cr + q_data_src->width / 2 *
1283 q_data_src->height / 2;
1284 break;
1285 case V4L2_PIX_FMT_YUV420:
1286 default:
1287 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1288 picture_cr = picture_cb + q_data_src->width / 2 *
1289 q_data_src->height / 2;
1290 break;
1291 }
Javier Martin186b2502012-07-26 05:53:35 -03001292
Philipp Zabel89548442014-07-11 06:36:17 -03001293 if (dev->devtype->product == CODA_960) {
1294 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1295 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1296 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1297
1298 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1299 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1300 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1301 } else {
1302 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1303 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1304 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1305 }
Javier Martin186b2502012-07-26 05:53:35 -03001306 coda_write(dev, force_ipicture << 1 & 0x2,
1307 CODA_CMD_ENC_PIC_OPTION);
1308
1309 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1310 coda_write(dev, pic_stream_buffer_size / 1024,
1311 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001312
1313 if (!ctx->streamon_out) {
1314 /* After streamoff on the output side, set the stream end flag */
1315 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1316 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1317 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001318}
1319
1320static void coda_device_run(void *m2m_priv)
1321{
1322 struct coda_ctx *ctx = m2m_priv;
1323 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001324
1325 queue_work(dev->workqueue, &ctx->pic_run_work);
1326}
1327
1328static void coda_free_framebuffers(struct coda_ctx *ctx);
1329static void coda_free_context_buffers(struct coda_ctx *ctx);
1330
1331static void coda_seq_end_work(struct work_struct *work)
1332{
1333 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1334 struct coda_dev *dev = ctx->dev;
1335
1336 mutex_lock(&ctx->buffer_mutex);
1337 mutex_lock(&dev->coda_mutex);
1338
1339 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1340 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1341 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1342 v4l2_err(&dev->v4l2_dev,
1343 "CODA_COMMAND_SEQ_END failed\n");
1344 }
1345
1346 kfifo_init(&ctx->bitstream_fifo,
1347 ctx->bitstream.vaddr, ctx->bitstream.size);
1348
1349 coda_free_framebuffers(ctx);
1350 coda_free_context_buffers(ctx);
1351
1352 mutex_unlock(&dev->coda_mutex);
1353 mutex_unlock(&ctx->buffer_mutex);
1354}
1355
1356static void coda_finish_decode(struct coda_ctx *ctx);
1357static void coda_finish_encode(struct coda_ctx *ctx);
1358
1359static void coda_pic_run_work(struct work_struct *work)
1360{
1361 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1362 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001363 int ret;
1364
1365 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001366 mutex_lock(&dev->coda_mutex);
1367
Philipp Zabel918c66f2013-06-27 06:59:01 -03001368 if (ctx->inst_type == CODA_INST_DECODER) {
1369 ret = coda_prepare_decode(ctx);
1370 if (ret < 0) {
1371 mutex_unlock(&dev->coda_mutex);
1372 mutex_unlock(&ctx->buffer_mutex);
1373 /* job_finish scheduled by prepare_decode */
1374 return;
1375 }
1376 } else {
1377 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001378 }
1379
Philipp Zabelc2d22512013-06-21 03:55:28 -03001380 if (dev->devtype->product != CODA_DX6)
1381 coda_write(dev, ctx->iram_info.axi_sram_use,
1382 CODA7_REG_BIT_AXI_SRAM_USE);
1383
Philipp Zabel918c66f2013-06-27 06:59:01 -03001384 if (ctx->inst_type == CODA_INST_DECODER)
1385 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001386 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001387
1388 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1389 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1390 } else if (!ctx->aborting) {
1391 if (ctx->inst_type == CODA_INST_DECODER)
1392 coda_finish_decode(ctx);
1393 else
1394 coda_finish_encode(ctx);
1395 }
1396
1397 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1398 queue_work(dev->workqueue, &ctx->seq_end_work);
1399
1400 mutex_unlock(&dev->coda_mutex);
1401 mutex_unlock(&ctx->buffer_mutex);
1402
Philipp Zabel14604e32014-07-11 06:36:22 -03001403 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001404}
1405
1406static int coda_job_ready(void *m2m_priv)
1407{
1408 struct coda_ctx *ctx = m2m_priv;
1409
1410 /*
1411 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001412 * and 1 frame are needed. In the decoder case,
1413 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001414 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001415 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001416 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001417 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1418 "not ready: not enough video buffers.\n");
1419 return 0;
1420 }
1421
Philipp Zabel14604e32014-07-11 06:36:22 -03001422 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001423 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1424 "not ready: not enough video capture buffers.\n");
1425 return 0;
1426 }
1427
Philipp Zabel918c66f2013-06-27 06:59:01 -03001428 if (ctx->prescan_failed ||
1429 ((ctx->inst_type == CODA_INST_DECODER) &&
1430 (coda_get_bitstream_payload(ctx) < 512) &&
1431 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1432 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1433 "%d: not ready: not enough bitstream data.\n",
1434 ctx->idx);
1435 return 0;
1436 }
1437
Philipp Zabel3e748262013-05-23 10:43:01 -03001438 if (ctx->aborting) {
1439 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1440 "not ready: aborting\n");
1441 return 0;
1442 }
1443
Javier Martin186b2502012-07-26 05:53:35 -03001444 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1445 "job ready\n");
1446 return 1;
1447}
1448
1449static void coda_job_abort(void *priv)
1450{
1451 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001452
1453 ctx->aborting = 1;
1454
1455 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1456 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001457}
1458
1459static void coda_lock(void *m2m_priv)
1460{
1461 struct coda_ctx *ctx = m2m_priv;
1462 struct coda_dev *pcdev = ctx->dev;
1463 mutex_lock(&pcdev->dev_mutex);
1464}
1465
1466static void coda_unlock(void *m2m_priv)
1467{
1468 struct coda_ctx *ctx = m2m_priv;
1469 struct coda_dev *pcdev = ctx->dev;
1470 mutex_unlock(&pcdev->dev_mutex);
1471}
1472
1473static struct v4l2_m2m_ops coda_m2m_ops = {
1474 .device_run = coda_device_run,
1475 .job_ready = coda_job_ready,
1476 .job_abort = coda_job_abort,
1477 .lock = coda_lock,
1478 .unlock = coda_unlock,
1479};
1480
Philipp Zabel89548442014-07-11 06:36:17 -03001481static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1482{
1483 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1484 int luma_map, chro_map, i;
1485
1486 memset(tiled_map, 0, sizeof(*tiled_map));
1487
1488 luma_map = 64;
1489 chro_map = 64;
1490 tiled_map->map_type = tiled_map_type;
1491 for (i = 0; i < 16; i++)
1492 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1493 for (i = 0; i < 4; i++)
1494 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1495 for (i = 0; i < 16; i++)
1496 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1497
1498 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1499 tiled_map->xy2rbc_config = 0;
1500 } else {
1501 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1502 tiled_map_type);
1503 return;
1504 }
1505}
1506
Javier Martin186b2502012-07-26 05:53:35 -03001507static void set_default_params(struct coda_ctx *ctx)
1508{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001509 int max_w;
1510 int max_h;
1511
1512 ctx->codec = &ctx->dev->devtype->codecs[0];
1513 max_w = ctx->codec->max_w;
1514 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001515
1516 ctx->params.codec_mode = CODA_MODE_INVALID;
1517 ctx->colorspace = V4L2_COLORSPACE_REC709;
1518 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001519 ctx->aborting = 0;
1520
1521 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001522 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1523 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1524 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1525 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1526 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1527 ctx->q_data[V4L2_M2M_DST].width = max_w;
1528 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001529 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001530 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1531 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1532 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1533 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001534
1535 if (ctx->dev->devtype->product == CODA_960)
1536 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001537}
1538
1539/*
1540 * Queue operations
1541 */
1542static int coda_queue_setup(struct vb2_queue *vq,
1543 const struct v4l2_format *fmt,
1544 unsigned int *nbuffers, unsigned int *nplanes,
1545 unsigned int sizes[], void *alloc_ctxs[])
1546{
1547 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001548 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001549 unsigned int size;
1550
Philipp Zabele34db062012-08-29 08:22:00 -03001551 q_data = get_q_data(ctx, vq->type);
1552 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001553
1554 *nplanes = 1;
1555 sizes[0] = size;
1556
1557 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1558
1559 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1560 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1561
1562 return 0;
1563}
1564
1565static int coda_buf_prepare(struct vb2_buffer *vb)
1566{
1567 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1568 struct coda_q_data *q_data;
1569
1570 q_data = get_q_data(ctx, vb->vb2_queue->type);
1571
1572 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1573 v4l2_warn(&ctx->dev->v4l2_dev,
1574 "%s data will not fit into plane (%lu < %lu)\n",
1575 __func__, vb2_plane_size(vb, 0),
1576 (long)q_data->sizeimage);
1577 return -EINVAL;
1578 }
1579
Javier Martin186b2502012-07-26 05:53:35 -03001580 return 0;
1581}
1582
1583static void coda_buf_queue(struct vb2_buffer *vb)
1584{
1585 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001586 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001587 struct coda_q_data *q_data;
1588
1589 q_data = get_q_data(ctx, vb->vb2_queue->type);
1590
1591 /*
1592 * In the decoder case, immediately try to copy the buffer into the
1593 * bitstream ringbuffer and mark it as ready to be dequeued.
1594 */
1595 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1596 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1597 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001598 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001599 * the stream end
1600 */
Philipp Zabel89548442014-07-11 06:36:17 -03001601 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001602 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001603 if ((dev->devtype->product == CODA_960) &&
1604 coda_isbusy(dev) &&
1605 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1606 /* if this decoder instance is running, set the stream end flag */
1607 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1608 }
1609 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001610 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001611 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001612 coda_fill_bitstream(ctx);
1613 mutex_unlock(&ctx->bitstream_mutex);
1614 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001615 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001616 }
Javier Martin186b2502012-07-26 05:53:35 -03001617}
1618
Philipp Zabel86eda902013-05-23 10:42:57 -03001619static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1620{
1621 struct coda_dev *dev = ctx->dev;
1622 u32 *p = ctx->parabuf.vaddr;
1623
1624 if (dev->devtype->product == CODA_DX6)
1625 p[index] = value;
1626 else
1627 p[index ^ 1] = value;
1628}
1629
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001630static int coda_alloc_aux_buf(struct coda_dev *dev,
1631 struct coda_aux_buf *buf, size_t size)
1632{
1633 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1634 GFP_KERNEL);
1635 if (!buf->vaddr)
1636 return -ENOMEM;
1637
1638 buf->size = size;
1639
1640 return 0;
1641}
1642
1643static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1644 struct coda_aux_buf *buf, size_t size)
1645{
1646 return coda_alloc_aux_buf(ctx->dev, buf, size);
1647}
1648
1649static void coda_free_aux_buf(struct coda_dev *dev,
1650 struct coda_aux_buf *buf)
1651{
1652 if (buf->vaddr) {
1653 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1654 buf->vaddr, buf->paddr);
1655 buf->vaddr = NULL;
1656 buf->size = 0;
1657 }
1658}
1659
1660static void coda_free_framebuffers(struct coda_ctx *ctx)
1661{
1662 int i;
1663
1664 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1665 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1666}
1667
Philipp Zabelec25f682012-07-20 08:54:29 -03001668static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1669{
1670 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001671 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001672 dma_addr_t paddr;
1673 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001674 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001675 int i;
1676
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001677 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1678 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001679 ysize = round_up(q_data->width, 8) * height;
1680
Philipp Zabelec25f682012-07-20 08:54:29 -03001681 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001682 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001683 size_t size;
1684
Philipp Zabelaed14b02014-07-11 06:36:15 -03001685 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001686 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1687 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001688 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001689 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1690 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001691 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001692 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001693 }
1694 }
1695
1696 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001697 for (i = 0; i < ctx->num_internal_frames; i++) {
1698 paddr = ctx->internal_frames[i].paddr;
1699 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1700 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1701 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001702
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001703 /* mvcol buffer for h.264 */
1704 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1705 dev->devtype->product != CODA_DX6)
1706 coda_parabuf_write(ctx, 96 + i,
1707 ctx->internal_frames[i].paddr +
1708 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001709 }
1710
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001711 /* mvcol buffer for mpeg4 */
1712 if ((dev->devtype->product != CODA_DX6) &&
1713 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1714 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1715 ysize + ysize/4 + ysize/4);
1716
Philipp Zabelec25f682012-07-20 08:54:29 -03001717 return 0;
1718}
1719
Javier Martin3f3f5c72012-10-29 05:20:29 -03001720static int coda_h264_padding(int size, char *p)
1721{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001722 int nal_size;
1723 int diff;
1724
Javier Martin832fbb52012-10-29 08:34:59 -03001725 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001726 if (diff == 0)
1727 return 0;
1728
Javier Martin832fbb52012-10-29 08:34:59 -03001729 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001730 memcpy(p, coda_filler_nal, nal_size);
1731
1732 /* Add rbsp stop bit and trailing at the end */
1733 *(p + nal_size - 1) = 0x80;
1734
1735 return nal_size;
1736}
1737
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001738static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1739{
1740 phys_addr_t ret;
1741
1742 size = round_up(size, 1024);
1743 if (size > iram->remaining)
1744 return 0;
1745 iram->remaining -= size;
1746
1747 ret = iram->next_paddr;
1748 iram->next_paddr += size;
1749
1750 return ret;
1751}
1752
Philipp Zabelc2d22512013-06-21 03:55:28 -03001753static void coda_setup_iram(struct coda_ctx *ctx)
1754{
1755 struct coda_iram_info *iram_info = &ctx->iram_info;
1756 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001757 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001758 int dbk_bits;
1759 int bit_bits;
1760 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001761
1762 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001763 iram_info->next_paddr = dev->iram.paddr;
1764 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001765
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001766 switch (dev->devtype->product) {
1767 case CODA_7541:
1768 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1769 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1770 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1771 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001772 case CODA_960:
1773 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1774 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1775 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1776 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001777 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001778 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001779 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001780
1781 if (ctx->inst_type == CODA_INST_ENCODER) {
1782 struct coda_q_data *q_data_src;
1783
1784 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1785 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1786
1787 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001788 if (dev->devtype->product == CODA_7541) {
1789 iram_info->search_ram_size = round_up(mb_width * 16 *
1790 36 + 2048, 1024);
1791 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1792 iram_info->search_ram_size);
1793 if (!iram_info->search_ram_paddr) {
1794 pr_err("IRAM is smaller than the search ram size\n");
1795 goto out;
1796 }
1797 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1798 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001799 }
1800
1801 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001802 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1803 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1804 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001805 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001806 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001807
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001808 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1809 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001810 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001811 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001812
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001813 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1814 if (!iram_info->buf_ip_ac_dc_use)
1815 goto out;
1816 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001817
Philipp Zabel8358e762013-06-21 03:55:32 -03001818 /* OVL and BTP disabled for encoder */
1819 } else if (ctx->inst_type == CODA_INST_DECODER) {
1820 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001821
1822 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1823 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001824
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001825 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1826 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1827 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001828 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001829 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001830
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001831 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1832 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001833 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001834 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001835
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001836 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1837 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001838 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001839 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001840
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001841 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001842 }
1843
1844out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001845 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1846 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1847 "IRAM smaller than needed\n");
1848
1849 if (dev->devtype->product == CODA_7541) {
1850 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001851 if (ctx->inst_type == CODA_INST_DECODER) {
1852 /* fw 1.4.50 */
1853 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1854 CODA7_USE_IP_ENABLE);
1855 } else {
1856 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001857 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1858 CODA7_USE_HOST_DBK_ENABLE |
1859 CODA7_USE_IP_ENABLE |
1860 CODA7_USE_DBK_ENABLE);
1861 }
1862 }
1863}
1864
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001865static void coda_free_context_buffers(struct coda_ctx *ctx)
1866{
1867 struct coda_dev *dev = ctx->dev;
1868
Philipp Zabel918c66f2013-06-27 06:59:01 -03001869 coda_free_aux_buf(dev, &ctx->slicebuf);
1870 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001871 if (dev->devtype->product != CODA_DX6)
1872 coda_free_aux_buf(dev, &ctx->workbuf);
1873}
1874
1875static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1876 struct coda_q_data *q_data)
1877{
1878 struct coda_dev *dev = ctx->dev;
1879 size_t size;
1880 int ret;
1881
1882 switch (dev->devtype->product) {
1883 case CODA_7541:
1884 size = CODA7_WORK_BUF_SIZE;
1885 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001886 case CODA_960:
1887 size = CODA9_WORK_BUF_SIZE;
1888 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1889 size += CODA9_PS_SAVE_SIZE;
1890 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001891 default:
1892 return 0;
1893 }
1894
Philipp Zabel918c66f2013-06-27 06:59:01 -03001895 if (ctx->psbuf.vaddr) {
1896 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1897 return -EBUSY;
1898 }
1899 if (ctx->slicebuf.vaddr) {
1900 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1901 return -EBUSY;
1902 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001903 if (ctx->workbuf.vaddr) {
1904 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1905 ret = -EBUSY;
1906 return -ENOMEM;
1907 }
1908
Philipp Zabel918c66f2013-06-27 06:59:01 -03001909 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1910 /* worst case slice size */
1911 size = (DIV_ROUND_UP(q_data->width, 16) *
1912 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1913 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1914 if (ret < 0) {
1915 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1916 ctx->slicebuf.size);
1917 return ret;
1918 }
1919 }
1920
1921 if (dev->devtype->product == CODA_7541) {
1922 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1923 if (ret < 0) {
1924 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1925 goto err;
1926 }
1927 }
1928
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001929 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1930 if (ret < 0) {
1931 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1932 ctx->workbuf.size);
1933 goto err;
1934 }
1935
1936 return 0;
1937
1938err:
1939 coda_free_context_buffers(ctx);
1940 return ret;
1941}
1942
Philipp Zabel918c66f2013-06-27 06:59:01 -03001943static int coda_start_decoding(struct coda_ctx *ctx)
1944{
1945 struct coda_q_data *q_data_src, *q_data_dst;
1946 u32 bitstream_buf, bitstream_size;
1947 struct coda_dev *dev = ctx->dev;
1948 int width, height;
1949 u32 src_fourcc;
1950 u32 val;
1951 int ret;
1952
1953 /* Start decoding */
1954 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1955 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1956 bitstream_buf = ctx->bitstream.paddr;
1957 bitstream_size = ctx->bitstream.size;
1958 src_fourcc = q_data_src->fourcc;
1959
1960 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1961
1962 /* Update coda bitstream read and write pointers from kfifo */
1963 coda_kfifo_sync_to_device_full(ctx);
1964
1965 ctx->display_idx = -1;
1966 ctx->frm_dis_flg = 0;
1967 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1968
1969 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1970 CODA_REG_BIT_BIT_STREAM_PARAM);
1971
1972 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1973 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1974 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001975 if ((dev->devtype->product == CODA_7541) ||
1976 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03001977 val |= CODA_REORDER_ENABLE;
1978 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1979
1980 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03001981 if (dev->devtype->product == CODA_960 &&
1982 src_fourcc == V4L2_PIX_FMT_MPEG4)
1983 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1984 else
1985 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001986 if (src_fourcc == V4L2_PIX_FMT_H264) {
1987 if (dev->devtype->product == CODA_7541) {
1988 coda_write(dev, ctx->psbuf.paddr,
1989 CODA_CMD_DEC_SEQ_PS_BB_START);
1990 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1991 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1992 }
Philipp Zabel89548442014-07-11 06:36:17 -03001993 if (dev->devtype->product == CODA_960) {
1994 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1995 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1996 }
1997 }
1998 if (dev->devtype->product != CODA_960) {
1999 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002000 }
2001
2002 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2003 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2004 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2005 return -ETIMEDOUT;
2006 }
2007
2008 /* Update kfifo out pointer from coda bitstream read pointer */
2009 coda_kfifo_sync_from_device(ctx);
2010
2011 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2012
2013 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2014 v4l2_err(&dev->v4l2_dev,
2015 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2016 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2017 return -EAGAIN;
2018 }
2019
2020 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2021 if (dev->devtype->product == CODA_DX6) {
2022 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2023 height = val & CODADX6_PICHEIGHT_MASK;
2024 } else {
2025 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2026 height = val & CODA7_PICHEIGHT_MASK;
2027 }
2028
2029 if (width > q_data_dst->width || height > q_data_dst->height) {
2030 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2031 width, height, q_data_dst->width, q_data_dst->height);
2032 return -EINVAL;
2033 }
2034
2035 width = round_up(width, 16);
2036 height = round_up(height, 16);
2037
2038 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2039 __func__, ctx->idx, width, height);
2040
Philipp Zabela500a932014-07-11 06:36:13 -03002041 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002042 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2043 v4l2_err(&dev->v4l2_dev,
2044 "not enough framebuffers to decode (%d < %d)\n",
2045 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2046 return -EINVAL;
2047 }
2048
Philipp Zabel52c41672014-07-11 06:36:19 -03002049 if (src_fourcc == V4L2_PIX_FMT_H264) {
2050 u32 left_right;
2051 u32 top_bottom;
2052
2053 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2054 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2055
2056 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2057 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2058 q_data_dst->rect.width = width - q_data_dst->rect.left -
2059 (left_right & 0x3ff);
2060 q_data_dst->rect.height = height - q_data_dst->rect.top -
2061 (top_bottom & 0x3ff);
2062 }
2063
Philipp Zabel918c66f2013-06-27 06:59:01 -03002064 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2065 if (ret < 0)
2066 return ret;
2067
2068 /* Tell the decoder how many frame buffers we allocated. */
2069 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2070 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2071
2072 if (dev->devtype->product != CODA_DX6) {
2073 /* Set secondary AXI IRAM */
2074 coda_setup_iram(ctx);
2075
2076 coda_write(dev, ctx->iram_info.buf_bit_use,
2077 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2078 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2079 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2080 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2081 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2082 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2083 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2084 coda_write(dev, ctx->iram_info.buf_ovl_use,
2085 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002086 if (dev->devtype->product == CODA_960)
2087 coda_write(dev, ctx->iram_info.buf_btp_use,
2088 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2089 }
2090
2091 if (dev->devtype->product == CODA_960) {
2092 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2093
2094 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2095 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2096 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2097 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2098 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2099 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002100 }
2101
2102 if (src_fourcc == V4L2_PIX_FMT_H264) {
2103 coda_write(dev, ctx->slicebuf.paddr,
2104 CODA_CMD_SET_FRAME_SLICE_BB_START);
2105 coda_write(dev, ctx->slicebuf.size / 1024,
2106 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2107 }
2108
2109 if (dev->devtype->product == CODA_7541) {
2110 int max_mb_x = 1920 / 16;
2111 int max_mb_y = 1088 / 16;
2112 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002113
Philipp Zabel918c66f2013-06-27 06:59:01 -03002114 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2115 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002116 } else if (dev->devtype->product == CODA_960) {
2117 int max_mb_x = 1920 / 16;
2118 int max_mb_y = 1088 / 16;
2119 int max_mb_num = max_mb_x * max_mb_y;
2120
2121 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2122 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002123 }
2124
2125 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2126 v4l2_err(&ctx->dev->v4l2_dev,
2127 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2128 return -ETIMEDOUT;
2129 }
2130
2131 return 0;
2132}
2133
Philipp Zabeld35167a2013-05-23 10:42:59 -03002134static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2135 int header_code, u8 *header, int *size)
2136{
2137 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002138 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002139 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002140 int i;
2141
2142 if (dev->devtype->product == CODA_960)
2143 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002144
2145 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2146 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002147 bufsize = vb2_plane_size(buf, 0);
2148 if (dev->devtype->product == CODA_960)
2149 bufsize /= 1024;
2150 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002151 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2152 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2153 if (ret < 0) {
2154 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2155 return ret;
2156 }
Philipp Zabel89548442014-07-11 06:36:17 -03002157
2158 if (dev->devtype->product == CODA_960) {
2159 for (i = 63; i > 0; i--)
2160 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2161 break;
2162 *size = i + 1;
2163 } else {
2164 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2165 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2166 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002167 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2168
2169 return 0;
2170}
2171
Philipp Zabel89548442014-07-11 06:36:17 -03002172static int coda_start_encoding(struct coda_ctx *ctx);
2173
Javier Martin186b2502012-07-26 05:53:35 -03002174static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2175{
2176 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2177 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002178 struct coda_dev *dev = ctx->dev;
2179 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002180 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002181 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002182
Philipp Zabelb96904e2013-05-23 10:42:58 -03002183 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002184 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2185 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2186 if (coda_get_bitstream_payload(ctx) < 512)
2187 return -EINVAL;
2188 } else {
2189 if (count < 1)
2190 return -EINVAL;
2191 }
2192
2193 ctx->streamon_out = 1;
2194
Philipp Zabelb96904e2013-05-23 10:42:58 -03002195 if (coda_format_is_yuv(q_data_src->fourcc))
2196 ctx->inst_type = CODA_INST_ENCODER;
2197 else
2198 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002199 } else {
2200 if (count < 1)
2201 return -EINVAL;
2202
2203 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002204 }
Javier Martin186b2502012-07-26 05:53:35 -03002205
Philipp Zabelb96904e2013-05-23 10:42:58 -03002206 /* Don't start the coda unless both queues are on */
2207 if (!(ctx->streamon_out & ctx->streamon_cap))
2208 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002209
Philipp Zabeleb107512013-09-30 10:34:45 -03002210 /* Allow decoder device_run with no new buffers queued */
2211 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002212 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002213
Philipp Zabelb96904e2013-05-23 10:42:58 -03002214 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002215 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002216 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002217
Philipp Zabelb96904e2013-05-23 10:42:58 -03002218 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2219 q_data_dst->fourcc);
2220 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002221 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2222 return -EINVAL;
2223 }
2224
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002225 /* Allocate per-instance buffers */
2226 ret = coda_alloc_context_buffers(ctx, q_data_src);
2227 if (ret < 0)
2228 return ret;
2229
Philipp Zabel918c66f2013-06-27 06:59:01 -03002230 if (ctx->inst_type == CODA_INST_DECODER) {
2231 mutex_lock(&dev->coda_mutex);
2232 ret = coda_start_decoding(ctx);
2233 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002234 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002235 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002236 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002237 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002238 } else {
2239 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002240 }
2241
Philipp Zabel89548442014-07-11 06:36:17 -03002242 ctx->initialized = 1;
2243 return ret;
2244}
2245
2246static int coda_start_encoding(struct coda_ctx *ctx)
2247{
2248 struct coda_dev *dev = ctx->dev;
2249 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2250 struct coda_q_data *q_data_src, *q_data_dst;
2251 u32 bitstream_buf, bitstream_size;
2252 struct vb2_buffer *buf;
2253 int gamma, ret, value;
2254 u32 dst_fourcc;
2255
2256 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2257 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2258 dst_fourcc = q_data_dst->fourcc;
2259
Philipp Zabel14604e32014-07-11 06:36:22 -03002260 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002261 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2262 bitstream_size = q_data_dst->sizeimage;
2263
Javier Martin186b2502012-07-26 05:53:35 -03002264 if (!coda_is_initialized(dev)) {
2265 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2266 return -EFAULT;
2267 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002268
2269 mutex_lock(&dev->coda_mutex);
2270
Javier Martin186b2502012-07-26 05:53:35 -03002271 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002272 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2273 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002274 switch (dev->devtype->product) {
2275 case CODA_DX6:
2276 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2277 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2278 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002279 case CODA_960:
2280 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2281 /* fallthrough */
2282 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002283 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2284 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002285 break;
Javier Martin186b2502012-07-26 05:53:35 -03002286 }
2287
Philipp Zabel89548442014-07-11 06:36:17 -03002288 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2289 value &= ~(1 << 2 | 0x7 << 9);
2290 ctx->frame_mem_ctrl = value;
2291 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2292
Philipp Zabel10436672012-07-02 09:03:55 -03002293 if (dev->devtype->product == CODA_DX6) {
2294 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002295 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002296 }
Javier Martin186b2502012-07-26 05:53:35 -03002297
2298 /* Could set rotation here if needed */
2299 switch (dev->devtype->product) {
2300 case CODA_DX6:
2301 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002302 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002303 break;
2304 default:
2305 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002306 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002307 }
Javier Martin186b2502012-07-26 05:53:35 -03002308 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2309 coda_write(dev, ctx->params.framerate,
2310 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2311
Philipp Zabelb96904e2013-05-23 10:42:58 -03002312 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002313 switch (dst_fourcc) {
2314 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002315 if (dev->devtype->product == CODA_960)
2316 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2317 else
2318 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002319 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2320 break;
2321 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002322 if (dev->devtype->product == CODA_960)
2323 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2324 else
2325 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002326 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2327 break;
2328 default:
2329 v4l2_err(v4l2_dev,
2330 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002331 ret = -EINVAL;
2332 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002333 }
2334
Philipp Zabelc566c782012-08-08 11:59:38 -03002335 switch (ctx->params.slice_mode) {
2336 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2337 value = 0;
2338 break;
2339 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2340 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2341 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002342 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002343 break;
2344 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2345 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2346 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2347 value |= 1 & CODA_SLICING_MODE_MASK;
2348 break;
2349 }
Javier Martin186b2502012-07-26 05:53:35 -03002350 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002351 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002352 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2353
2354 if (ctx->params.bitrate) {
2355 /* Rate control enabled */
2356 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2357 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002358 if (dev->devtype->product == CODA_960)
2359 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002360 } else {
2361 value = 0;
2362 }
2363 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2364
2365 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2366 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2367
2368 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2369 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2370
Javier Martin186b2502012-07-26 05:53:35 -03002371
Philipp Zabel89548442014-07-11 06:36:17 -03002372 value = 0;
2373 if (dev->devtype->product == CODA_960)
2374 gamma = CODA9_DEFAULT_GAMMA;
2375 else
2376 gamma = CODA_DEFAULT_GAMMA;
2377 if (gamma > 0) {
2378 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2379 CODA_CMD_ENC_SEQ_RC_GAMMA);
2380 }
2381 if (dev->devtype->product == CODA_960) {
2382 if (CODA_DEFAULT_GAMMA > 0)
2383 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002384 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002385 if (CODA_DEFAULT_GAMMA > 0) {
2386 if (dev->devtype->product == CODA_DX6)
2387 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2388 else
2389 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2390 }
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002391 }
Javier Martin186b2502012-07-26 05:53:35 -03002392 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2393
Philipp Zabel89548442014-07-11 06:36:17 -03002394 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2395
Philipp Zabelc2d22512013-06-21 03:55:28 -03002396 coda_setup_iram(ctx);
2397
Javier Martin186b2502012-07-26 05:53:35 -03002398 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002399 switch (dev->devtype->product) {
2400 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002401 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002402 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002403 break;
2404 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002405 coda_write(dev, ctx->iram_info.search_ram_paddr,
2406 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2407 coda_write(dev, ctx->iram_info.search_ram_size,
2408 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002409 break;
2410 case CODA_960:
2411 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2412 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002413 }
Javier Martin186b2502012-07-26 05:53:35 -03002414 }
2415
Philipp Zabelfcb62822013-05-23 10:43:00 -03002416 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2417 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002418 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002419 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002420 }
2421
Philipp Zabelfcb62822013-05-23 10:43:00 -03002422 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2423 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2424 ret = -EFAULT;
2425 goto out;
2426 }
Javier Martin186b2502012-07-26 05:53:35 -03002427
Philipp Zabel89548442014-07-11 06:36:17 -03002428 if (dev->devtype->product == CODA_960)
2429 ctx->num_internal_frames = 4;
2430 else
2431 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002432 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002433 if (ret < 0) {
2434 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2435 goto out;
2436 }
Javier Martin186b2502012-07-26 05:53:35 -03002437
Philipp Zabelec25f682012-07-20 08:54:29 -03002438 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002439 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002440 if (dev->devtype->product == CODA_7541)
2441 coda_write(dev, round_up(q_data_src->width, 8),
2442 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002443 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002444 coda_write(dev, ctx->iram_info.buf_bit_use,
2445 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2446 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2447 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2448 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2449 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2450 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2451 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2452 coda_write(dev, ctx->iram_info.buf_ovl_use,
2453 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002454 if (dev->devtype->product == CODA_960) {
2455 coda_write(dev, ctx->iram_info.buf_btp_use,
2456 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2457
2458 /* FIXME */
2459 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2460 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2461 }
Philipp Zabel10436672012-07-02 09:03:55 -03002462 }
Philipp Zabel89548442014-07-11 06:36:17 -03002463
Philipp Zabelfcb62822013-05-23 10:43:00 -03002464 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2465 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002466 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002467 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002468 }
2469
2470 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002471 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002472 switch (dst_fourcc) {
2473 case V4L2_PIX_FMT_H264:
2474 /*
2475 * Get SPS in the first frame and copy it to an
2476 * intermediate buffer.
2477 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002478 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2479 &ctx->vpu_header[0][0],
2480 &ctx->vpu_header_size[0]);
2481 if (ret < 0)
2482 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002483
2484 /*
2485 * Get PPS in the first frame and copy it to an
2486 * intermediate buffer.
2487 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002488 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2489 &ctx->vpu_header[1][0],
2490 &ctx->vpu_header_size[1]);
2491 if (ret < 0)
2492 goto out;
2493
Javier Martin3f3f5c72012-10-29 05:20:29 -03002494 /*
2495 * Length of H.264 headers is variable and thus it might not be
2496 * aligned for the coda to append the encoded frame. In that is
2497 * the case a filler NAL must be added to header 2.
2498 */
2499 ctx->vpu_header_size[2] = coda_h264_padding(
2500 (ctx->vpu_header_size[0] +
2501 ctx->vpu_header_size[1]),
2502 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002503 break;
2504 case V4L2_PIX_FMT_MPEG4:
2505 /*
2506 * Get VOS in the first frame and copy it to an
2507 * intermediate buffer
2508 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002509 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2510 &ctx->vpu_header[0][0],
2511 &ctx->vpu_header_size[0]);
2512 if (ret < 0)
2513 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002514
Philipp Zabeld35167a2013-05-23 10:42:59 -03002515 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2516 &ctx->vpu_header[1][0],
2517 &ctx->vpu_header_size[1]);
2518 if (ret < 0)
2519 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002520
Philipp Zabeld35167a2013-05-23 10:42:59 -03002521 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2522 &ctx->vpu_header[2][0],
2523 &ctx->vpu_header_size[2]);
2524 if (ret < 0)
2525 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002526 break;
2527 default:
2528 /* No more formats need to save headers at the moment */
2529 break;
2530 }
2531
Philipp Zabeld35167a2013-05-23 10:42:59 -03002532out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002533 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002534 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002535}
2536
Hans Verkuile37559b2014-04-17 02:47:21 -03002537static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002538{
2539 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002540 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002541
2542 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002543 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002544 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002545 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002546
Philipp Zabel89548442014-07-11 06:36:17 -03002547 if (ctx->inst_type == CODA_INST_DECODER &&
2548 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2549 /* if this decoder instance is running, set the stream end flag */
2550 if (dev->devtype->product == CODA_960) {
2551 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2552
2553 val |= CODA_BIT_STREAM_END_FLAG;
2554 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2555 ctx->bit_stream_param = val;
2556 }
2557 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002558 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2559
2560 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002561 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002562 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002563 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002564 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002565
2566 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002567 }
2568
Philipp Zabel918c66f2013-06-27 06:59:01 -03002569 if (!ctx->streamon_out && !ctx->streamon_cap) {
2570 kfifo_init(&ctx->bitstream_fifo,
2571 ctx->bitstream.vaddr, ctx->bitstream.size);
2572 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002573 }
Javier Martin186b2502012-07-26 05:53:35 -03002574}
2575
2576static struct vb2_ops coda_qops = {
2577 .queue_setup = coda_queue_setup,
2578 .buf_prepare = coda_buf_prepare,
2579 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002580 .start_streaming = coda_start_streaming,
2581 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002582 .wait_prepare = vb2_ops_wait_prepare,
2583 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002584};
2585
2586static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2587{
2588 struct coda_ctx *ctx =
2589 container_of(ctrl->handler, struct coda_ctx, ctrls);
2590
2591 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2592 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2593
2594 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002595 case V4L2_CID_HFLIP:
2596 if (ctrl->val)
2597 ctx->params.rot_mode |= CODA_MIR_HOR;
2598 else
2599 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2600 break;
2601 case V4L2_CID_VFLIP:
2602 if (ctrl->val)
2603 ctx->params.rot_mode |= CODA_MIR_VER;
2604 else
2605 ctx->params.rot_mode &= ~CODA_MIR_VER;
2606 break;
Javier Martin186b2502012-07-26 05:53:35 -03002607 case V4L2_CID_MPEG_VIDEO_BITRATE:
2608 ctx->params.bitrate = ctrl->val / 1000;
2609 break;
2610 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2611 ctx->params.gop_size = ctrl->val;
2612 break;
2613 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2614 ctx->params.h264_intra_qp = ctrl->val;
2615 break;
2616 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2617 ctx->params.h264_inter_qp = ctrl->val;
2618 break;
2619 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2620 ctx->params.mpeg4_intra_qp = ctrl->val;
2621 break;
2622 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2623 ctx->params.mpeg4_inter_qp = ctrl->val;
2624 break;
2625 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2626 ctx->params.slice_mode = ctrl->val;
2627 break;
2628 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2629 ctx->params.slice_max_mb = ctrl->val;
2630 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002631 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2632 ctx->params.slice_max_bits = ctrl->val * 8;
2633 break;
Javier Martin186b2502012-07-26 05:53:35 -03002634 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2635 break;
2636 default:
2637 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2638 "Invalid control, id=%d, val=%d\n",
2639 ctrl->id, ctrl->val);
2640 return -EINVAL;
2641 }
2642
2643 return 0;
2644}
2645
2646static struct v4l2_ctrl_ops coda_ctrl_ops = {
2647 .s_ctrl = coda_s_ctrl,
2648};
2649
2650static int coda_ctrls_setup(struct coda_ctx *ctx)
2651{
2652 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2653
2654 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002655 V4L2_CID_HFLIP, 0, 1, 1, 0);
2656 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2657 V4L2_CID_VFLIP, 0, 1, 1, 0);
2658 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002659 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2660 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2661 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2662 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002663 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002664 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002665 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002666 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2667 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2668 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2669 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2670 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2671 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002672 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2673 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002674 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2675 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002676 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2677 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002678 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2679 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2680 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2681 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2682 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2683
2684 if (ctx->ctrls.error) {
2685 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2686 ctx->ctrls.error);
2687 return -EINVAL;
2688 }
2689
2690 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2691}
2692
2693static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2694 struct vb2_queue *dst_vq)
2695{
2696 struct coda_ctx *ctx = priv;
2697 int ret;
2698
Javier Martin186b2502012-07-26 05:53:35 -03002699 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002700 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002701 src_vq->drv_priv = ctx;
2702 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2703 src_vq->ops = &coda_qops;
2704 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002705 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002706 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002707
2708 ret = vb2_queue_init(src_vq);
2709 if (ret)
2710 return ret;
2711
Javier Martin186b2502012-07-26 05:53:35 -03002712 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002713 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002714 dst_vq->drv_priv = ctx;
2715 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2716 dst_vq->ops = &coda_qops;
2717 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002718 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002719 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002720
2721 return vb2_queue_init(dst_vq);
2722}
2723
Philipp Zabele11f3e62012-07-25 09:16:58 -03002724static int coda_next_free_instance(struct coda_dev *dev)
2725{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002726 int idx = ffz(dev->instance_mask);
2727
2728 if ((idx < 0) ||
2729 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2730 return -EBUSY;
2731
2732 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002733}
2734
Javier Martin186b2502012-07-26 05:53:35 -03002735static int coda_open(struct file *file)
2736{
2737 struct coda_dev *dev = video_drvdata(file);
2738 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002739 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002740 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002741
Javier Martin186b2502012-07-26 05:53:35 -03002742 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2743 if (!ctx)
2744 return -ENOMEM;
2745
Fabio Estevamf82bc202013-08-21 11:14:16 -03002746 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002747 if (idx < 0) {
2748 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002749 goto err_coda_max;
2750 }
2751 set_bit(idx, &dev->instance_mask);
2752
Philipp Zabel32b6f202014-07-11 06:36:20 -03002753 init_completion(&ctx->completion);
2754 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2755 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002756 v4l2_fh_init(&ctx->fh, video_devdata(file));
2757 file->private_data = &ctx->fh;
2758 v4l2_fh_add(&ctx->fh);
2759 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002760 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002761 switch (dev->devtype->product) {
2762 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002763 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002764 ctx->reg_idx = 0;
2765 break;
2766 default:
2767 ctx->reg_idx = idx;
2768 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002769
Fabio Estevam79830db2013-08-21 11:14:17 -03002770 ret = clk_prepare_enable(dev->clk_per);
2771 if (ret)
2772 goto err_clk_per;
2773
2774 ret = clk_prepare_enable(dev->clk_ahb);
2775 if (ret)
2776 goto err_clk_ahb;
2777
Javier Martin186b2502012-07-26 05:53:35 -03002778 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002779 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002780 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002781 if (IS_ERR(ctx->fh.m2m_ctx)) {
2782 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002783
2784 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2785 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002786 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002787 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002788
Javier Martin186b2502012-07-26 05:53:35 -03002789 ret = coda_ctrls_setup(ctx);
2790 if (ret) {
2791 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002792 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002793 }
2794
2795 ctx->fh.ctrl_handler = &ctx->ctrls;
2796
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002797 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2798 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002799 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002800 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002801 }
2802
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002803 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2804 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2805 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2806 if (!ctx->bitstream.vaddr) {
2807 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2808 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002809 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002810 }
2811 kfifo_init(&ctx->bitstream_fifo,
2812 ctx->bitstream.vaddr, ctx->bitstream.size);
2813 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002814 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002815
Javier Martin186b2502012-07-26 05:53:35 -03002816 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002817 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002818 coda_unlock(ctx);
2819
Javier Martin186b2502012-07-26 05:53:35 -03002820 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2821 ctx->idx, ctx);
2822
2823 return 0;
2824
Fabio Estevamf82bc202013-08-21 11:14:16 -03002825err_dma_writecombine:
2826 coda_free_context_buffers(ctx);
2827 if (ctx->dev->devtype->product == CODA_DX6)
2828 coda_free_aux_buf(dev, &ctx->workbuf);
2829 coda_free_aux_buf(dev, &ctx->parabuf);
2830err_dma_alloc:
2831 v4l2_ctrl_handler_free(&ctx->ctrls);
2832err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002833 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002834err_ctx_init:
2835 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002836err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002837 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002838err_clk_per:
Javier Martin186b2502012-07-26 05:53:35 -03002839 v4l2_fh_del(&ctx->fh);
2840 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002841 clear_bit(ctx->idx, &dev->instance_mask);
2842err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002843 kfree(ctx);
2844 return ret;
2845}
2846
2847static int coda_release(struct file *file)
2848{
2849 struct coda_dev *dev = video_drvdata(file);
2850 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2851
2852 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2853 ctx);
2854
Philipp Zabel918c66f2013-06-27 06:59:01 -03002855 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002856 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002857
2858 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002859 if (ctx->initialized) {
2860 queue_work(dev->workqueue, &ctx->seq_end_work);
2861 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002862 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002863
2864 coda_free_framebuffers(ctx);
2865
Javier Martin186b2502012-07-26 05:53:35 -03002866 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002867 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002868 coda_unlock(ctx);
2869
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002870 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2871 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002872 coda_free_context_buffers(ctx);
2873 if (ctx->dev->devtype->product == CODA_DX6)
2874 coda_free_aux_buf(dev, &ctx->workbuf);
2875
2876 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002877 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002878 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002879 clk_disable_unprepare(dev->clk_per);
Javier Martin186b2502012-07-26 05:53:35 -03002880 v4l2_fh_del(&ctx->fh);
2881 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002882 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002883 kfree(ctx);
2884
2885 return 0;
2886}
2887
Javier Martin186b2502012-07-26 05:53:35 -03002888static const struct v4l2_file_operations coda_fops = {
2889 .owner = THIS_MODULE,
2890 .open = coda_open,
2891 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002892 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002893 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002894 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002895};
2896
Philipp Zabel918c66f2013-06-27 06:59:01 -03002897static void coda_finish_decode(struct coda_ctx *ctx)
2898{
2899 struct coda_dev *dev = ctx->dev;
2900 struct coda_q_data *q_data_src;
2901 struct coda_q_data *q_data_dst;
2902 struct vb2_buffer *dst_buf;
2903 int width, height;
2904 int decoded_idx;
2905 int display_idx;
2906 u32 src_fourcc;
2907 int success;
2908 u32 val;
2909
Philipp Zabel14604e32014-07-11 06:36:22 -03002910 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002911
2912 /* Update kfifo out pointer from coda bitstream read pointer */
2913 coda_kfifo_sync_from_device(ctx);
2914
2915 /*
2916 * in stream-end mode, the read pointer can overshoot the write pointer
2917 * by up to 512 bytes
2918 */
2919 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2920 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2921 kfifo_init(&ctx->bitstream_fifo,
2922 ctx->bitstream.vaddr, ctx->bitstream.size);
2923 }
2924
2925 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2926 src_fourcc = q_data_src->fourcc;
2927
2928 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2929 if (val != 1)
2930 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2931
2932 success = val & 0x1;
2933 if (!success)
2934 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2935
2936 if (src_fourcc == V4L2_PIX_FMT_H264) {
2937 if (val & (1 << 3))
2938 v4l2_err(&dev->v4l2_dev,
2939 "insufficient PS buffer space (%d bytes)\n",
2940 ctx->psbuf.size);
2941 if (val & (1 << 2))
2942 v4l2_err(&dev->v4l2_dev,
2943 "insufficient slice buffer space (%d bytes)\n",
2944 ctx->slicebuf.size);
2945 }
2946
2947 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2948 width = (val >> 16) & 0xffff;
2949 height = val & 0xffff;
2950
2951 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2952
Philipp Zabel52c41672014-07-11 06:36:19 -03002953 /* frame crop information */
2954 if (src_fourcc == V4L2_PIX_FMT_H264) {
2955 u32 left_right;
2956 u32 top_bottom;
2957
2958 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
2959 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
2960
2961 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
2962 /* Keep current crop information */
2963 } else {
2964 struct v4l2_rect *rect = &q_data_dst->rect;
2965
2966 rect->left = left_right >> 16 & 0xffff;
2967 rect->top = top_bottom >> 16 & 0xffff;
2968 rect->width = width - rect->left -
2969 (left_right & 0xffff);
2970 rect->height = height - rect->top -
2971 (top_bottom & 0xffff);
2972 }
2973 } else {
2974 /* no cropping */
2975 }
2976
Philipp Zabel918c66f2013-06-27 06:59:01 -03002977 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2978 if (val > 0)
2979 v4l2_err(&dev->v4l2_dev,
2980 "errors in %d macroblocks\n", val);
2981
2982 if (dev->devtype->product == CODA_7541) {
2983 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2984 if (val == 0) {
2985 /* not enough bitstream data */
2986 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2987 "prescan failed: %d\n", val);
2988 ctx->prescan_failed = true;
2989 return;
2990 }
2991 }
2992
2993 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2994
2995 /*
2996 * The previous display frame was copied out by the rotator,
2997 * now it can be overwritten again
2998 */
2999 if (ctx->display_idx >= 0 &&
3000 ctx->display_idx < ctx->num_internal_frames) {
3001 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3002 coda_write(dev, ctx->frm_dis_flg,
3003 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3004 }
3005
3006 /*
3007 * The index of the last decoded frame, not necessarily in
3008 * display order, and the index of the next display frame.
3009 * The latter could have been decoded in a previous run.
3010 */
3011 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3012 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3013
3014 if (decoded_idx == -1) {
3015 /* no frame was decoded, but we might have a display frame */
3016 if (display_idx < 0 && ctx->display_idx < 0)
3017 ctx->prescan_failed = true;
3018 } else if (decoded_idx == -2) {
3019 /* no frame was decoded, we still return the remaining buffers */
3020 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3021 v4l2_err(&dev->v4l2_dev,
3022 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003023 } else {
3024 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3025 if (val == 0)
3026 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3027 else if (val == 1)
3028 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3029 else
3030 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003031 }
3032
3033 if (display_idx == -1) {
3034 /*
3035 * no more frames to be decoded, but there could still
3036 * be rotator output to dequeue
3037 */
3038 ctx->prescan_failed = true;
3039 } else if (display_idx == -3) {
3040 /* possibly prescan failure */
3041 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3042 v4l2_err(&dev->v4l2_dev,
3043 "presentation frame index out of range: %d\n",
3044 display_idx);
3045 }
3046
3047 /* If a frame was copied out, return it */
3048 if (ctx->display_idx >= 0 &&
3049 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003050 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003051 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3052
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003053 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3054 V4L2_BUF_FLAG_PFRAME |
3055 V4L2_BUF_FLAG_BFRAME);
3056 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
3057
Philipp Zabel918c66f2013-06-27 06:59:01 -03003058 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3059
3060 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3061 VB2_BUF_STATE_ERROR);
3062
3063 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3064 "job finished: decoding frame (%d) (%s)\n",
3065 dst_buf->v4l2_buf.sequence,
3066 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3067 "KEYFRAME" : "PFRAME");
3068 } else {
3069 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3070 "job finished: no frame decoded\n");
3071 }
3072
3073 /* The rotator will copy the current display frame next time */
3074 ctx->display_idx = display_idx;
3075}
3076
3077static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003078{
Philipp Zabelec25f682012-07-20 08:54:29 -03003079 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003080 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003081 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003082
Philipp Zabel14604e32014-07-11 06:36:22 -03003083 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3084 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003085
3086 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003087 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003088 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3089
Javier Martin186b2502012-07-26 05:53:35 -03003090 /* Calculate bytesused field */
3091 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003092 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3093 ctx->vpu_header_size[0] +
3094 ctx->vpu_header_size[1] +
3095 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003096 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003097 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003098 }
3099
3100 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3101 wr_ptr - start_ptr);
3102
3103 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3104 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3105
Philipp Zabel51660282013-09-30 10:34:49 -03003106 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003107 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3108 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3109 } else {
3110 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3111 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3112 }
3113
Kamil Debskiccd571c2013-04-24 10:38:24 -03003114 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003115 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3116 dst_buf->v4l2_buf.flags |=
3117 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003118 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3119
Philipp Zabelec25f682012-07-20 08:54:29 -03003120 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003121
Philipp Zabel14604e32014-07-11 06:36:22 -03003122 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003123 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3124
3125 ctx->gopcounter--;
3126 if (ctx->gopcounter < 0)
3127 ctx->gopcounter = ctx->params.gop_size - 1;
3128
3129 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3130 "job finished: encoding frame (%d) (%s)\n",
3131 dst_buf->v4l2_buf.sequence,
3132 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3133 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003134}
3135
3136static irqreturn_t coda_irq_handler(int irq, void *data)
3137{
3138 struct coda_dev *dev = data;
3139 struct coda_ctx *ctx;
3140
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003141 /* read status register to attend the IRQ */
3142 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3143 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3144 CODA_REG_BIT_INT_CLEAR);
3145
3146 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3147 if (ctx == NULL) {
3148 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3149 mutex_unlock(&dev->coda_mutex);
3150 return IRQ_HANDLED;
3151 }
3152
3153 if (ctx->aborting) {
3154 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3155 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003156 }
3157
3158 if (coda_isbusy(ctx->dev)) {
3159 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3160 "coda is still busy!!!!\n");
3161 return IRQ_NONE;
3162 }
3163
Philipp Zabel32b6f202014-07-11 06:36:20 -03003164 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003165
3166 return IRQ_HANDLED;
3167}
3168
3169static u32 coda_supported_firmwares[] = {
3170 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003171 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003172 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003173};
3174
3175static bool coda_firmware_supported(u32 vernum)
3176{
3177 int i;
3178
3179 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3180 if (vernum == coda_supported_firmwares[i])
3181 return true;
3182 return false;
3183}
3184
Philipp Zabel87048bb2012-07-02 07:03:43 -03003185static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003186{
3187 u16 product, major, minor, release;
3188 u32 data;
3189 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003190 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003191
Fabio Estevam79830db2013-08-21 11:14:17 -03003192 ret = clk_prepare_enable(dev->clk_per);
3193 if (ret)
3194 return ret;
3195
3196 ret = clk_prepare_enable(dev->clk_ahb);
3197 if (ret)
3198 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003199
Javier Martin186b2502012-07-26 05:53:35 -03003200 /*
3201 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003202 * The 16-bit chars in the code buffer are in memory access
3203 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003204 * Data in this SRAM survives a reboot.
3205 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003206 p = (u16 *)dev->codebuf.vaddr;
3207 if (dev->devtype->product == CODA_DX6) {
3208 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3209 data = CODA_DOWN_ADDRESS_SET(i) |
3210 CODA_DOWN_DATA_SET(p[i ^ 1]);
3211 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3212 }
3213 } else {
3214 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3215 data = CODA_DOWN_ADDRESS_SET(i) |
3216 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3217 3 - (i % 4)]);
3218 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3219 }
Javier Martin186b2502012-07-26 05:53:35 -03003220 }
Javier Martin186b2502012-07-26 05:53:35 -03003221
Philipp Zabel9acf7692013-05-23 10:42:56 -03003222 /* Clear registers */
3223 for (i = 0; i < 64; i++)
3224 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3225
Javier Martin186b2502012-07-26 05:53:35 -03003226 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003227 if (dev->devtype->product == CODA_960 ||
3228 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003229 coda_write(dev, dev->tempbuf.paddr,
3230 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003231 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003232 } else {
3233 coda_write(dev, dev->workbuf.paddr,
3234 CODA_REG_BIT_WORK_BUF_ADDR);
3235 }
Javier Martin186b2502012-07-26 05:53:35 -03003236 coda_write(dev, dev->codebuf.paddr,
3237 CODA_REG_BIT_CODE_BUF_ADDR);
3238 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3239
3240 /* Set default values */
3241 switch (dev->devtype->product) {
3242 case CODA_DX6:
3243 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3244 break;
3245 default:
3246 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3247 }
Philipp Zabel89548442014-07-11 06:36:17 -03003248 if (dev->devtype->product == CODA_960)
3249 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3250 else
3251 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003252
3253 if (dev->devtype->product != CODA_DX6)
3254 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3255
Javier Martin186b2502012-07-26 05:53:35 -03003256 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3257 CODA_REG_BIT_INT_ENABLE);
3258
3259 /* Reset VPU and start processor */
3260 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3261 data |= CODA_REG_RESET_ENABLE;
3262 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3263 udelay(10);
3264 data &= ~CODA_REG_RESET_ENABLE;
3265 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3266 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3267
3268 /* Load firmware */
3269 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3270 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3271 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3272 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3273 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3274 if (coda_wait_timeout(dev)) {
3275 clk_disable_unprepare(dev->clk_per);
3276 clk_disable_unprepare(dev->clk_ahb);
3277 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
3278 return -EIO;
3279 }
3280
Philipp Zabel89548442014-07-11 06:36:17 -03003281 if (dev->devtype->product == CODA_960) {
3282 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3283 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3284 data);
3285 }
3286
Javier Martin186b2502012-07-26 05:53:35 -03003287 /* Check we are compatible with the loaded firmware */
3288 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3289 product = CODA_FIRMWARE_PRODUCT(data);
3290 major = CODA_FIRMWARE_MAJOR(data);
3291 minor = CODA_FIRMWARE_MINOR(data);
3292 release = CODA_FIRMWARE_RELEASE(data);
3293
3294 clk_disable_unprepare(dev->clk_per);
3295 clk_disable_unprepare(dev->clk_ahb);
3296
3297 if (product != dev->devtype->product) {
3298 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3299 " Version: %u.%u.%u\n",
3300 coda_product_name(dev->devtype->product),
3301 coda_product_name(product), major, minor, release);
3302 return -EINVAL;
3303 }
3304
3305 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3306 coda_product_name(product));
3307
3308 if (coda_firmware_supported(data)) {
3309 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3310 major, minor, release);
3311 } else {
3312 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3313 "%u.%u.%u\n", major, minor, release);
3314 }
3315
3316 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003317
3318err_clk_ahb:
3319 clk_disable_unprepare(dev->clk_per);
3320 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003321}
3322
3323static void coda_fw_callback(const struct firmware *fw, void *context)
3324{
3325 struct coda_dev *dev = context;
3326 struct platform_device *pdev = dev->plat_dev;
3327 int ret;
3328
3329 if (!fw) {
3330 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3331 return;
3332 }
3333
3334 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003335 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3336 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003337 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3338 return;
3339 }
3340
Philipp Zabel87048bb2012-07-02 07:03:43 -03003341 /* Copy the whole firmware image to the code buffer */
3342 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3343 release_firmware(fw);
3344
3345 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03003346 if (ret) {
3347 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3348 return;
3349 }
3350
3351 dev->vfd.fops = &coda_fops,
3352 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3353 dev->vfd.release = video_device_release_empty,
3354 dev->vfd.lock = &dev->dev_mutex;
3355 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003356 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003357 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3358 video_set_drvdata(&dev->vfd, dev);
3359
3360 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3361 if (IS_ERR(dev->alloc_ctx)) {
3362 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3363 return;
3364 }
3365
3366 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3367 if (IS_ERR(dev->m2m_dev)) {
3368 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3369 goto rel_ctx;
3370 }
3371
3372 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3373 if (ret) {
3374 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3375 goto rel_m2m;
3376 }
3377 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3378 dev->vfd.num);
3379
3380 return;
3381
3382rel_m2m:
3383 v4l2_m2m_release(dev->m2m_dev);
3384rel_ctx:
3385 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3386}
3387
3388static int coda_firmware_request(struct coda_dev *dev)
3389{
3390 char *fw = dev->devtype->firmware;
3391
3392 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3393 coda_product_name(dev->devtype->product));
3394
3395 return request_firmware_nowait(THIS_MODULE, true,
3396 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3397}
3398
3399enum coda_platform {
3400 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003401 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003402 CODA_IMX6Q,
3403 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003404};
3405
Emil Goodec06d8752012-08-14 17:44:42 -03003406static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003407 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003408 .firmware = "v4l-codadx6-imx27.bin",
3409 .product = CODA_DX6,
3410 .codecs = codadx6_codecs,
3411 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003412 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003413 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003414 .firmware = "v4l-coda7541-imx53.bin",
3415 .product = CODA_7541,
3416 .codecs = coda7_codecs,
3417 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003418 },
Philipp Zabel89548442014-07-11 06:36:17 -03003419 [CODA_IMX6Q] = {
3420 .firmware = "v4l-coda960-imx6q.bin",
3421 .product = CODA_960,
3422 .codecs = coda9_codecs,
3423 .num_codecs = ARRAY_SIZE(coda9_codecs),
3424 },
3425 [CODA_IMX6DL] = {
3426 .firmware = "v4l-coda960-imx6dl.bin",
3427 .product = CODA_960,
3428 .codecs = coda9_codecs,
3429 .num_codecs = ARRAY_SIZE(coda9_codecs),
3430 },
Javier Martin186b2502012-07-26 05:53:35 -03003431};
3432
3433static struct platform_device_id coda_platform_ids[] = {
3434 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003435 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003436 { /* sentinel */ }
3437};
3438MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3439
3440#ifdef CONFIG_OF
3441static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003442 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003443 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003444 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3445 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003446 { /* sentinel */ }
3447};
3448MODULE_DEVICE_TABLE(of, coda_dt_ids);
3449#endif
3450
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003451static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003452{
3453 const struct of_device_id *of_id =
3454 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3455 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003456 struct coda_platform_data *pdata = pdev->dev.platform_data;
3457 struct device_node *np = pdev->dev.of_node;
3458 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003459 struct coda_dev *dev;
3460 struct resource *res;
3461 int ret, irq;
3462
3463 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3464 if (!dev) {
3465 dev_err(&pdev->dev, "Not enough memory for %s\n",
3466 CODA_NAME);
3467 return -ENOMEM;
3468 }
3469
3470 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003471 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003472
3473 dev->plat_dev = pdev;
3474 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3475 if (IS_ERR(dev->clk_per)) {
3476 dev_err(&pdev->dev, "Could not get per clock\n");
3477 return PTR_ERR(dev->clk_per);
3478 }
3479
3480 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3481 if (IS_ERR(dev->clk_ahb)) {
3482 dev_err(&pdev->dev, "Could not get ahb clock\n");
3483 return PTR_ERR(dev->clk_ahb);
3484 }
3485
3486 /* Get memory for physical registers */
3487 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003488 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3489 if (IS_ERR(dev->regs_base))
3490 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003491
3492 /* IRQ */
3493 irq = platform_get_irq(pdev, 0);
3494 if (irq < 0) {
3495 dev_err(&pdev->dev, "failed to get irq resource\n");
3496 return -ENOENT;
3497 }
3498
Philipp Zabel918c66f2013-06-27 06:59:01 -03003499 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003500 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003501 dev_err(&pdev->dev, "failed to request irq\n");
3502 return -ENOENT;
3503 }
3504
Philipp Zabel657eee72013-04-29 16:17:14 -07003505 /* Get IRAM pool from device tree or platform data */
3506 pool = of_get_named_gen_pool(np, "iram", 0);
3507 if (!pool && pdata)
3508 pool = dev_get_gen_pool(pdata->iram_dev);
3509 if (!pool) {
3510 dev_err(&pdev->dev, "iram pool not available\n");
3511 return -ENOMEM;
3512 }
3513 dev->iram_pool = pool;
3514
Javier Martin186b2502012-07-26 05:53:35 -03003515 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3516 if (ret)
3517 return ret;
3518
3519 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003520 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003521
3522 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3523
3524 if (of_id) {
3525 dev->devtype = of_id->data;
3526 } else if (pdev_id) {
3527 dev->devtype = &coda_devdata[pdev_id->driver_data];
3528 } else {
3529 v4l2_device_unregister(&dev->v4l2_dev);
3530 return -EINVAL;
3531 }
3532
3533 /* allocate auxiliary per-device buffers for the BIT processor */
3534 switch (dev->devtype->product) {
3535 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003536 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3537 CODADX6_WORK_BUF_SIZE);
3538 if (ret < 0) {
3539 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3540 v4l2_device_unregister(&dev->v4l2_dev);
3541 return ret;
3542 }
Javier Martin186b2502012-07-26 05:53:35 -03003543 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003544 case CODA_7541:
3545 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3546 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003547 case CODA_960:
3548 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3549 break;
Javier Martin186b2502012-07-26 05:53:35 -03003550 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003551 if (dev->tempbuf.size) {
3552 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3553 dev->tempbuf.size);
3554 if (ret < 0) {
3555 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3556 v4l2_device_unregister(&dev->v4l2_dev);
3557 return ret;
3558 }
Javier Martin186b2502012-07-26 05:53:35 -03003559 }
3560
Philipp Zabel918c66f2013-06-27 06:59:01 -03003561 switch (dev->devtype->product) {
3562 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003563 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003564 break;
3565 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003566 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003567 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003568 case CODA_960:
3569 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003570 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003571 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3572 &dev->iram.paddr);
3573 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003574 dev_err(&pdev->dev, "unable to alloc iram\n");
3575 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003576 }
3577
Philipp Zabel32b6f202014-07-11 06:36:20 -03003578 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3579 if (!dev->workqueue) {
3580 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3581 return -ENOMEM;
3582 }
3583
Javier Martin186b2502012-07-26 05:53:35 -03003584 platform_set_drvdata(pdev, dev);
3585
3586 return coda_firmware_request(dev);
3587}
3588
3589static int coda_remove(struct platform_device *pdev)
3590{
3591 struct coda_dev *dev = platform_get_drvdata(pdev);
3592
3593 video_unregister_device(&dev->vfd);
3594 if (dev->m2m_dev)
3595 v4l2_m2m_release(dev->m2m_dev);
3596 if (dev->alloc_ctx)
3597 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3598 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003599 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003600 if (dev->iram.vaddr)
3601 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3602 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003603 coda_free_aux_buf(dev, &dev->codebuf);
3604 coda_free_aux_buf(dev, &dev->tempbuf);
3605 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003606 return 0;
3607}
3608
3609static struct platform_driver coda_driver = {
3610 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003611 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003612 .driver = {
3613 .name = CODA_NAME,
3614 .owner = THIS_MODULE,
3615 .of_match_table = of_match_ptr(coda_dt_ids),
3616 },
3617 .id_table = coda_platform_ids,
3618};
3619
3620module_platform_driver(coda_driver);
3621
3622MODULE_LICENSE("GPL");
3623MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3624MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");