blob: 0c77366284c7fa7b8579a84833d241be374547d8 [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;
Javier Martin186b2502012-07-26 05:53:35 -0300150 struct v4l2_m2m_dev *m2m_dev;
151 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300152 struct list_head instances;
153 unsigned long instance_mask;
Philipp Zabel2fb57f02012-07-25 09:22:07 -0300154 struct delayed_work timeout;
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 Zabel918c66f2013-06-27 06:59:01 -0300201 struct work_struct skip_run;
Javier Martin186b2502012-07-26 05:53:35 -0300202 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300203 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300204 int streamon_out;
205 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300206 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300207 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300208 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300209 struct coda_q_data q_data[2];
210 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300211 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300212 enum v4l2_colorspace colorspace;
213 struct coda_params params;
214 struct v4l2_m2m_ctx *m2m_ctx;
215 struct v4l2_ctrl_handler ctrls;
216 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300217 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300218 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300219 char vpu_header[3][64];
220 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300221 struct kfifo bitstream_fifo;
222 struct mutex bitstream_mutex;
223 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300224 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300225 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300226 struct coda_aux_buf psbuf;
227 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300228 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300229 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300230 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300231 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300232 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300233 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300234 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300235 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300236 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300237 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300238 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300239 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300240};
241
Javier Martin832fbb52012-10-29 08:34:59 -0300242static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
243 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
244static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300245
Javier Martin186b2502012-07-26 05:53:35 -0300246static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
247{
248 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
249 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
250 writel(data, dev->regs_base + reg);
251}
252
253static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
254{
255 u32 data;
256 data = readl(dev->regs_base + reg);
257 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
258 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
259 return data;
260}
261
262static inline unsigned long coda_isbusy(struct coda_dev *dev)
263{
264 return coda_read(dev, CODA_REG_BIT_BUSY);
265}
266
267static inline int coda_is_initialized(struct coda_dev *dev)
268{
269 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
270}
271
272static int coda_wait_timeout(struct coda_dev *dev)
273{
274 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
275
276 while (coda_isbusy(dev)) {
277 if (time_after(jiffies, timeout))
278 return -ETIMEDOUT;
279 }
280 return 0;
281}
282
283static void coda_command_async(struct coda_ctx *ctx, int cmd)
284{
285 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300286
Philipp Zabel89548442014-07-11 06:36:17 -0300287 if (dev->devtype->product == CODA_960 ||
288 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300289 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300290 coda_write(dev, ctx->bit_stream_param,
291 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300292 coda_write(dev, ctx->frm_dis_flg,
293 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300294 coda_write(dev, ctx->frame_mem_ctrl,
295 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300296 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
297 }
298
Philipp Zabel89548442014-07-11 06:36:17 -0300299 if (dev->devtype->product == CODA_960) {
300 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
301 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
302 }
303
Javier Martin186b2502012-07-26 05:53:35 -0300304 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
305
306 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
307 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300308 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
309
Javier Martin186b2502012-07-26 05:53:35 -0300310 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
311}
312
313static int coda_command_sync(struct coda_ctx *ctx, int cmd)
314{
315 struct coda_dev *dev = ctx->dev;
316
317 coda_command_async(ctx, cmd);
318 return coda_wait_timeout(dev);
319}
320
321static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
322 enum v4l2_buf_type type)
323{
324 switch (type) {
325 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
326 return &(ctx->q_data[V4L2_M2M_SRC]);
327 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
328 return &(ctx->q_data[V4L2_M2M_DST]);
329 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300330 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300331 }
Javier Martin186b2502012-07-26 05:53:35 -0300332}
333
334/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300335 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300336 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300337static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300338 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300339 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300340 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300341 },
342 {
343 .name = "YUV 4:2:0 Planar, YCrCb",
344 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300345 },
346 {
347 .name = "H264 Encoded Stream",
348 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300349 },
350 {
351 .name = "MPEG4 Encoded Stream",
352 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300353 },
354};
355
Philipp Zabelb96904e2013-05-23 10:42:58 -0300356#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
357 { mode, src_fourcc, dst_fourcc, max_w, max_h }
358
359/*
360 * Arrays of codecs supported by each given version of Coda:
361 * i.MX27 -> codadx6
362 * i.MX5x -> coda7
363 * i.MX6 -> coda960
364 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
365 */
366static struct coda_codec codadx6_codecs[] = {
367 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
368 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300369};
370
Philipp Zabelb96904e2013-05-23 10:42:58 -0300371static struct coda_codec coda7_codecs[] = {
372 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
373 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300374 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
375 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300376};
377
Philipp Zabel89548442014-07-11 06:36:17 -0300378static struct coda_codec coda9_codecs[] = {
379 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
380 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
381 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
382 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
383};
384
Philipp Zabelb96904e2013-05-23 10:42:58 -0300385static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300386{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300387 switch (fourcc) {
388 case V4L2_PIX_FMT_YUV420:
389 case V4L2_PIX_FMT_YVU420:
390 return true;
391 default:
392 return false;
393 }
394}
Javier Martin186b2502012-07-26 05:53:35 -0300395
Philipp Zabelb96904e2013-05-23 10:42:58 -0300396/*
397 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
398 * tables.
399 */
400static u32 coda_format_normalize_yuv(u32 fourcc)
401{
402 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
403}
404
405static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
406 int dst_fourcc)
407{
408 struct coda_codec *codecs = dev->devtype->codecs;
409 int num_codecs = dev->devtype->num_codecs;
410 int k;
411
412 src_fourcc = coda_format_normalize_yuv(src_fourcc);
413 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
414 if (src_fourcc == dst_fourcc)
415 return NULL;
416
417 for (k = 0; k < num_codecs; k++) {
418 if (codecs[k].src_fourcc == src_fourcc &&
419 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300420 break;
421 }
422
Philipp Zabelb96904e2013-05-23 10:42:58 -0300423 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300424 return NULL;
425
Philipp Zabelb96904e2013-05-23 10:42:58 -0300426 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300427}
428
Philipp Zabel57625592013-09-30 10:34:51 -0300429static void coda_get_max_dimensions(struct coda_dev *dev,
430 struct coda_codec *codec,
431 int *max_w, int *max_h)
432{
433 struct coda_codec *codecs = dev->devtype->codecs;
434 int num_codecs = dev->devtype->num_codecs;
435 unsigned int w, h;
436 int k;
437
438 if (codec) {
439 w = codec->max_w;
440 h = codec->max_h;
441 } else {
442 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
443 w = max(w, codecs[k].max_w);
444 h = max(h, codecs[k].max_h);
445 }
446 }
447
448 if (max_w)
449 *max_w = w;
450 if (max_h)
451 *max_h = h;
452}
453
Philipp Zabel927933f2013-09-30 10:34:48 -0300454static char *coda_product_name(int product)
455{
456 static char buf[9];
457
458 switch (product) {
459 case CODA_DX6:
460 return "CodaDx6";
461 case CODA_7541:
462 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300463 case CODA_960:
464 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300465 default:
466 snprintf(buf, sizeof(buf), "(0x%04x)", product);
467 return buf;
468 }
469}
470
Javier Martin186b2502012-07-26 05:53:35 -0300471/*
472 * V4L2 ioctl() operations.
473 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300474static int coda_querycap(struct file *file, void *priv,
475 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300476{
Philipp Zabel927933f2013-09-30 10:34:48 -0300477 struct coda_ctx *ctx = fh_to_ctx(priv);
478
Javier Martin186b2502012-07-26 05:53:35 -0300479 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300480 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
481 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300482 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300483 /*
484 * This is only a mem-to-mem video device. The capture and output
485 * device capability flags are left only for backward compatibility
486 * and are scheduled for removal.
487 */
488 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
489 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300490 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
491
492 return 0;
493}
494
495static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300496 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300497{
498 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300499 struct coda_codec *codecs = ctx->dev->devtype->codecs;
500 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300501 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300502 int num_codecs = ctx->dev->devtype->num_codecs;
503 int num_formats = ARRAY_SIZE(coda_formats);
504 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300505
506 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300507 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300508 if (coda_format_is_yuv(formats[i].fourcc) &&
509 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300510 if (num == f->index)
511 break;
512 ++num;
513 continue;
514 }
515 /* Compressed formats may be supported, check the codec list */
516 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300517 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300518 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300519 formats[i].fourcc == codecs[k].dst_fourcc &&
520 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300521 break;
522 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
523 formats[i].fourcc == codecs[k].src_fourcc)
524 break;
525 }
526 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300527 if (num == f->index)
528 break;
529 ++num;
530 }
531 }
532
533 if (i < num_formats) {
534 fmt = &formats[i];
535 strlcpy(f->description, fmt->name, sizeof(f->description));
536 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300537 if (!coda_format_is_yuv(fmt->fourcc))
538 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300539 return 0;
540 }
541
542 /* Format not found */
543 return -EINVAL;
544}
545
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300546static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
547 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300548{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300549 struct coda_ctx *ctx = fh_to_ctx(priv);
550 struct vb2_queue *src_vq;
551 struct coda_q_data *q_data_src;
552
553 /* If the source format is already fixed, only list matching formats */
554 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
555 if (vb2_is_streaming(src_vq)) {
556 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
557
558 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
559 q_data_src->fourcc);
560 }
561
562 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300563}
564
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300565static int coda_enum_fmt_vid_out(struct file *file, void *priv,
566 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300567{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300568 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300569}
570
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300571static int coda_g_fmt(struct file *file, void *priv,
572 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300573{
Javier Martin186b2502012-07-26 05:53:35 -0300574 struct coda_q_data *q_data;
575 struct coda_ctx *ctx = fh_to_ctx(priv);
576
Javier Martin186b2502012-07-26 05:53:35 -0300577 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300578 if (!q_data)
579 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300580
581 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300582 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300583 f->fmt.pix.width = q_data->width;
584 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300585 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300586 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
587 else /* encoded formats h.264/mpeg4 */
588 f->fmt.pix.bytesperline = 0;
589
590 f->fmt.pix.sizeimage = q_data->sizeimage;
591 f->fmt.pix.colorspace = ctx->colorspace;
592
593 return 0;
594}
595
Philipp Zabel57625592013-09-30 10:34:51 -0300596static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
597 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300598{
Philipp Zabel57625592013-09-30 10:34:51 -0300599 struct coda_dev *dev = ctx->dev;
600 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300601 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300602 enum v4l2_field field;
603
604 field = f->fmt.pix.field;
605 if (field == V4L2_FIELD_ANY)
606 field = V4L2_FIELD_NONE;
607 else if (V4L2_FIELD_NONE != field)
608 return -EINVAL;
609
610 /* V4L2 specification suggests the driver corrects the format struct
611 * if any of the dimensions is unsupported */
612 f->fmt.pix.field = field;
613
Philipp Zabel57625592013-09-30 10:34:51 -0300614 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
615 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
616 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
617 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300618
Philipp Zabel57625592013-09-30 10:34:51 -0300619 switch (f->fmt.pix.pixelformat) {
620 case V4L2_PIX_FMT_YUV420:
621 case V4L2_PIX_FMT_YVU420:
622 case V4L2_PIX_FMT_H264:
623 case V4L2_PIX_FMT_MPEG4:
624 case V4L2_PIX_FMT_JPEG:
625 break;
626 default:
627 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300628 if (!q_data)
629 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300630 f->fmt.pix.pixelformat = q_data->fourcc;
631 }
632
633 switch (f->fmt.pix.pixelformat) {
634 case V4L2_PIX_FMT_YUV420:
635 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300636 /* Frame stride must be multiple of 8 */
637 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
638 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300639 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300640 break;
641 case V4L2_PIX_FMT_H264:
642 case V4L2_PIX_FMT_MPEG4:
643 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300644 f->fmt.pix.bytesperline = 0;
645 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300646 break;
647 default:
648 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300649 }
650
651 return 0;
652}
653
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300654static int coda_try_fmt_vid_cap(struct file *file, void *priv,
655 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300656{
Javier Martin186b2502012-07-26 05:53:35 -0300657 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300658 struct coda_codec *codec;
659 struct vb2_queue *src_vq;
660 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300661
Philipp Zabel918c66f2013-06-27 06:59:01 -0300662 /*
663 * If the source format is already fixed, try to find a codec that
664 * converts to the given destination format
665 */
666 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
667 if (vb2_is_streaming(src_vq)) {
668 struct coda_q_data *q_data_src;
669
670 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
671 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
672 f->fmt.pix.pixelformat);
673 if (!codec)
674 return -EINVAL;
675 } else {
676 /* Otherwise determine codec by encoded format, if possible */
677 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
678 f->fmt.pix.pixelformat);
679 }
Javier Martin186b2502012-07-26 05:53:35 -0300680
681 f->fmt.pix.colorspace = ctx->colorspace;
682
Philipp Zabel57625592013-09-30 10:34:51 -0300683 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300684 if (ret < 0)
685 return ret;
686
687 /* The h.264 decoder only returns complete 16x16 macroblocks */
688 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
689 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
690 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
691 f->fmt.pix.bytesperline = f->fmt.pix.width;
692 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
693 f->fmt.pix.height * 3 / 2;
694 }
695
696 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300697}
698
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300699static int coda_try_fmt_vid_out(struct file *file, void *priv,
700 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300701{
702 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300703 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300704
Philipp Zabelb96904e2013-05-23 10:42:58 -0300705 /* Determine codec by encoded format, returns NULL if raw or invalid */
706 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
707 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300708
709 if (!f->fmt.pix.colorspace)
710 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
711
Philipp Zabel57625592013-09-30 10:34:51 -0300712 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300713}
714
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300715static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300716{
717 struct coda_q_data *q_data;
718 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300719
720 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
721 if (!vq)
722 return -EINVAL;
723
724 q_data = get_q_data(ctx, f->type);
725 if (!q_data)
726 return -EINVAL;
727
728 if (vb2_is_busy(vq)) {
729 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
730 return -EBUSY;
731 }
732
Philipp Zabelb96904e2013-05-23 10:42:58 -0300733 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300734 q_data->width = f->fmt.pix.width;
735 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300736 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300737 q_data->rect.left = 0;
738 q_data->rect.top = 0;
739 q_data->rect.width = f->fmt.pix.width;
740 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300741
742 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
743 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300744 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300745
746 return 0;
747}
748
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300749static int coda_s_fmt_vid_cap(struct file *file, void *priv,
750 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300751{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300752 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300753 int ret;
754
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300755 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300756 if (ret)
757 return ret;
758
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300759 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300760}
761
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300762static int coda_s_fmt_vid_out(struct file *file, void *priv,
763 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300764{
765 struct coda_ctx *ctx = fh_to_ctx(priv);
766 int ret;
767
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300768 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300769 if (ret)
770 return ret;
771
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300772 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300773 if (ret)
774 ctx->colorspace = f->fmt.pix.colorspace;
775
776 return ret;
777}
778
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300779static int coda_reqbufs(struct file *file, void *priv,
780 struct v4l2_requestbuffers *reqbufs)
Javier Martin186b2502012-07-26 05:53:35 -0300781{
782 struct coda_ctx *ctx = fh_to_ctx(priv);
783
784 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
785}
786
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300787static int coda_querybuf(struct file *file, void *priv,
788 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300789{
790 struct coda_ctx *ctx = fh_to_ctx(priv);
791
792 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
793}
794
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300795static int coda_qbuf(struct file *file, void *priv,
796 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300797{
798 struct coda_ctx *ctx = fh_to_ctx(priv);
799
800 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
801}
802
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300803static int coda_expbuf(struct file *file, void *priv,
804 struct v4l2_exportbuffer *eb)
Philipp Zabel419869c2013-05-23 07:06:30 -0300805{
806 struct coda_ctx *ctx = fh_to_ctx(priv);
807
808 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
809}
810
Philipp Zabel918c66f2013-06-27 06:59:01 -0300811static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
812 struct v4l2_buffer *buf)
813{
814 struct vb2_queue *src_vq;
815
816 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
817
818 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
819 (buf->sequence == (ctx->qsequence - 1)));
820}
821
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300822static int coda_dqbuf(struct file *file, void *priv,
823 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300824{
825 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300826 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300827
Philipp Zabel918c66f2013-06-27 06:59:01 -0300828 ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
829
830 /* If this is the last capture buffer, emit an end-of-stream event */
831 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
832 coda_buf_is_end_of_stream(ctx, buf)) {
833 const struct v4l2_event eos_event = {
834 .type = V4L2_EVENT_EOS
835 };
836
837 v4l2_event_queue_fh(&ctx->fh, &eos_event);
838 }
839
840 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300841}
842
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300843static int coda_create_bufs(struct file *file, void *priv,
844 struct v4l2_create_buffers *create)
Philipp Zabel8fdf94a2013-05-21 04:16:29 -0300845{
846 struct coda_ctx *ctx = fh_to_ctx(priv);
847
848 return v4l2_m2m_create_bufs(file, ctx->m2m_ctx, create);
849}
850
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300851static int coda_streamon(struct file *file, void *priv,
852 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300853{
854 struct coda_ctx *ctx = fh_to_ctx(priv);
855
856 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
857}
858
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300859static int coda_streamoff(struct file *file, void *priv,
860 enum v4l2_buf_type type)
Javier Martin186b2502012-07-26 05:53:35 -0300861{
862 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300863 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300864
Philipp Zabel918c66f2013-06-27 06:59:01 -0300865 /*
866 * This indirectly calls __vb2_queue_cancel, which dequeues all buffers.
867 * We therefore have to lock it against running hardware in this context,
868 * which still needs the buffers.
869 */
870 mutex_lock(&ctx->buffer_mutex);
871 ret = v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
872 mutex_unlock(&ctx->buffer_mutex);
873
874 return ret;
875}
876
Philipp Zabel52c41672014-07-11 06:36:19 -0300877static int coda_g_selection(struct file *file, void *fh,
878 struct v4l2_selection *s)
879{
880 struct coda_ctx *ctx = fh_to_ctx(fh);
881 struct coda_q_data *q_data;
882 struct v4l2_rect r, *rsel;
883
884 q_data = get_q_data(ctx, s->type);
885 if (!q_data)
886 return -EINVAL;
887
888 r.left = 0;
889 r.top = 0;
890 r.width = q_data->width;
891 r.height = q_data->height;
892 rsel = &q_data->rect;
893
894 switch (s->target) {
895 case V4L2_SEL_TGT_CROP_DEFAULT:
896 case V4L2_SEL_TGT_CROP_BOUNDS:
897 rsel = &r;
898 /* fallthrough */
899 case V4L2_SEL_TGT_CROP:
900 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
901 return -EINVAL;
902 break;
903 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
904 case V4L2_SEL_TGT_COMPOSE_PADDED:
905 rsel = &r;
906 /* fallthrough */
907 case V4L2_SEL_TGT_COMPOSE:
908 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
909 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
910 return -EINVAL;
911 break;
912 default:
913 return -EINVAL;
914 }
915
916 s->r = *rsel;
917
918 return 0;
919}
920
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300921static int coda_try_decoder_cmd(struct file *file, void *fh,
922 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300923{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300924 if (dc->cmd != V4L2_DEC_CMD_STOP)
925 return -EINVAL;
926
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300927 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300928 return -EINVAL;
929
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300930 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300931 return -EINVAL;
932
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300933 return 0;
934}
935
936static int coda_decoder_cmd(struct file *file, void *fh,
937 struct v4l2_decoder_cmd *dc)
938{
939 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300940 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300941 int ret;
942
943 ret = coda_try_decoder_cmd(file, fh, dc);
944 if (ret < 0)
945 return ret;
946
947 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300948 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300949 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300950
951 /* Set the strem-end flag on this context */
952 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
953
Philipp Zabel89548442014-07-11 06:36:17 -0300954 if ((dev->devtype->product == CODA_960) &&
955 coda_isbusy(dev) &&
956 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
957 /* If this context is currently running, update the hardware flag */
958 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
959 }
960
Philipp Zabel918c66f2013-06-27 06:59:01 -0300961 return 0;
962}
963
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300964static int coda_subscribe_event(struct v4l2_fh *fh,
965 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300966{
967 switch (sub->type) {
968 case V4L2_EVENT_EOS:
969 return v4l2_event_subscribe(fh, sub, 0, NULL);
970 default:
971 return v4l2_ctrl_subscribe_event(fh, sub);
972 }
Javier Martin186b2502012-07-26 05:53:35 -0300973}
974
975static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300976 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300977
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300978 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
979 .vidioc_g_fmt_vid_cap = coda_g_fmt,
980 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
981 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300982
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300983 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
984 .vidioc_g_fmt_vid_out = coda_g_fmt,
985 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
986 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300987
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300988 .vidioc_reqbufs = coda_reqbufs,
989 .vidioc_querybuf = coda_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300990
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300991 .vidioc_qbuf = coda_qbuf,
992 .vidioc_expbuf = coda_expbuf,
993 .vidioc_dqbuf = coda_dqbuf,
994 .vidioc_create_bufs = coda_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300995
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300996 .vidioc_streamon = coda_streamon,
997 .vidioc_streamoff = coda_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300998
Philipp Zabel52c41672014-07-11 06:36:19 -0300999 .vidioc_g_selection = coda_g_selection,
1000
Philipp Zabel64ba40a2013-09-30 10:34:52 -03001001 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001002 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001003
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001004 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001005 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001006};
1007
Philipp Zabel918c66f2013-06-27 06:59:01 -03001008static int coda_start_decoding(struct coda_ctx *ctx);
1009
1010static void coda_skip_run(struct work_struct *work)
1011{
1012 struct coda_ctx *ctx = container_of(work, struct coda_ctx, skip_run);
1013
1014 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
1015}
1016
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001017static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
1018{
1019 return kfifo_len(&ctx->bitstream_fifo);
1020}
1021
1022static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
1023{
1024 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1025 struct coda_dev *dev = ctx->dev;
1026 u32 rd_ptr;
1027
1028 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1029 kfifo->out = (kfifo->in & ~kfifo->mask) |
1030 (rd_ptr - ctx->bitstream.paddr);
1031 if (kfifo->out > kfifo->in)
1032 kfifo->out -= kfifo->mask + 1;
1033}
1034
1035static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1036{
1037 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1038 struct coda_dev *dev = ctx->dev;
1039 u32 rd_ptr, wr_ptr;
1040
1041 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1042 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1043 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1044 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1045}
1046
1047static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1048{
1049 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1050 struct coda_dev *dev = ctx->dev;
1051 u32 wr_ptr;
1052
1053 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1054 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1055}
1056
1057static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1058{
1059 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1060 u32 n;
1061
1062 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1063 if (n < src_size)
1064 return -ENOSPC;
1065
1066 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1067 ctx->bitstream.size, DMA_TO_DEVICE);
1068
1069 ctx->qsequence++;
1070
1071 return 0;
1072}
1073
1074static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1075 struct vb2_buffer *src_buf)
1076{
1077 int ret;
1078
1079 if (coda_get_bitstream_payload(ctx) +
1080 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1081 return false;
1082
1083 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1084 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1085 return true;
1086 }
1087
1088 ret = coda_bitstream_queue(ctx, src_buf);
1089 if (ret < 0) {
1090 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1091 return false;
1092 }
1093 /* Sync read pointer to device */
1094 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1095 coda_kfifo_sync_to_device_write(ctx);
1096
Philipp Zabel918c66f2013-06-27 06:59:01 -03001097 ctx->prescan_failed = false;
1098
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001099 return true;
1100}
1101
1102static void coda_fill_bitstream(struct coda_ctx *ctx)
1103{
1104 struct vb2_buffer *src_buf;
1105
1106 while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
1107 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1108
1109 if (coda_bitstream_try_queue(ctx, src_buf)) {
1110 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1111 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1112 } else {
1113 break;
1114 }
1115 }
1116}
1117
Philipp Zabel89548442014-07-11 06:36:17 -03001118static void coda_set_gdi_regs(struct coda_ctx *ctx)
1119{
1120 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1121 struct coda_dev *dev = ctx->dev;
1122 int i;
1123
1124 for (i = 0; i < 16; i++)
1125 coda_write(dev, tiled_map->xy2ca_map[i],
1126 CODA9_GDI_XY2_CAS_0 + 4 * i);
1127 for (i = 0; i < 4; i++)
1128 coda_write(dev, tiled_map->xy2ba_map[i],
1129 CODA9_GDI_XY2_BA_0 + 4 * i);
1130 for (i = 0; i < 16; i++)
1131 coda_write(dev, tiled_map->xy2ra_map[i],
1132 CODA9_GDI_XY2_RAS_0 + 4 * i);
1133 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1134 for (i = 0; i < 32; i++)
1135 coda_write(dev, tiled_map->rbc2axi_map[i],
1136 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1137}
1138
Javier Martin186b2502012-07-26 05:53:35 -03001139/*
1140 * Mem-to-mem operations.
1141 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001142static int coda_prepare_decode(struct coda_ctx *ctx)
1143{
1144 struct vb2_buffer *dst_buf;
1145 struct coda_dev *dev = ctx->dev;
1146 struct coda_q_data *q_data_dst;
1147 u32 stridey, height;
1148 u32 picture_y, picture_cb, picture_cr;
1149
1150 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1151 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1152
1153 if (ctx->params.rot_mode & CODA_ROT_90) {
1154 stridey = q_data_dst->height;
1155 height = q_data_dst->width;
1156 } else {
1157 stridey = q_data_dst->width;
1158 height = q_data_dst->height;
1159 }
1160
1161 /* Try to copy source buffer contents into the bitstream ringbuffer */
1162 mutex_lock(&ctx->bitstream_mutex);
1163 coda_fill_bitstream(ctx);
1164 mutex_unlock(&ctx->bitstream_mutex);
1165
1166 if (coda_get_bitstream_payload(ctx) < 512 &&
1167 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1168 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1169 "bitstream payload: %d, skipping\n",
1170 coda_get_bitstream_payload(ctx));
1171 schedule_work(&ctx->skip_run);
1172 return -EAGAIN;
1173 }
1174
1175 /* Run coda_start_decoding (again) if not yet initialized */
1176 if (!ctx->initialized) {
1177 int ret = coda_start_decoding(ctx);
1178 if (ret < 0) {
1179 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
1180 schedule_work(&ctx->skip_run);
1181 return -EAGAIN;
1182 } else {
1183 ctx->initialized = 1;
1184 }
1185 }
1186
Philipp Zabel89548442014-07-11 06:36:17 -03001187 if (dev->devtype->product == CODA_960)
1188 coda_set_gdi_regs(ctx);
1189
Philipp Zabel918c66f2013-06-27 06:59:01 -03001190 /* Set rotator output */
1191 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1192 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1193 /* Switch Cr and Cb for YVU420 format */
1194 picture_cr = picture_y + stridey * height;
1195 picture_cb = picture_cr + stridey / 2 * height / 2;
1196 } else {
1197 picture_cb = picture_y + stridey * height;
1198 picture_cr = picture_cb + stridey / 2 * height / 2;
1199 }
Philipp Zabel89548442014-07-11 06:36:17 -03001200
1201 if (dev->devtype->product == CODA_960) {
1202 /*
1203 * The CODA960 seems to have an internal list of buffers with
1204 * 64 entries that includes the registered frame buffers as
1205 * well as the rotator buffer output.
1206 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1207 */
1208 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1209 CODA9_CMD_DEC_PIC_ROT_INDEX);
1210 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1211 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1212 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1213 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1214 } else {
1215 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1216 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1217 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1218 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1219 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001220 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1221 CODA_CMD_DEC_PIC_ROT_MODE);
1222
1223 switch (dev->devtype->product) {
1224 case CODA_DX6:
1225 /* TBD */
1226 case CODA_7541:
1227 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1228 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001229 case CODA_960:
1230 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1231 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001232 }
1233
1234 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1235
1236 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1237 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1238
1239 return 0;
1240}
1241
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001242static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001243{
Javier Martin186b2502012-07-26 05:53:35 -03001244 struct coda_q_data *q_data_src, *q_data_dst;
1245 struct vb2_buffer *src_buf, *dst_buf;
1246 struct coda_dev *dev = ctx->dev;
1247 int force_ipicture;
1248 int quant_param = 0;
1249 u32 picture_y, picture_cb, picture_cr;
1250 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1251 u32 dst_fourcc;
1252
1253 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1254 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
1255 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1256 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001257 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001258
Philipp Zabel918c66f2013-06-27 06:59:01 -03001259 src_buf->v4l2_buf.sequence = ctx->osequence;
1260 dst_buf->v4l2_buf.sequence = ctx->osequence;
1261 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001262
1263 /*
1264 * Workaround coda firmware BUG that only marks the first
1265 * frame as IDR. This is a problem for some decoders that can't
1266 * recover when a frame is lost.
1267 */
1268 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1269 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1270 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1271 } else {
1272 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1273 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1274 }
1275
Philipp Zabel89548442014-07-11 06:36:17 -03001276 if (dev->devtype->product == CODA_960)
1277 coda_set_gdi_regs(ctx);
1278
Javier Martin186b2502012-07-26 05:53:35 -03001279 /*
1280 * Copy headers at the beginning of the first frame for H.264 only.
1281 * In MPEG4 they are already copied by the coda.
1282 */
1283 if (src_buf->v4l2_buf.sequence == 0) {
1284 pic_stream_buffer_addr =
1285 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1286 ctx->vpu_header_size[0] +
1287 ctx->vpu_header_size[1] +
1288 ctx->vpu_header_size[2];
1289 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1290 ctx->vpu_header_size[0] -
1291 ctx->vpu_header_size[1] -
1292 ctx->vpu_header_size[2];
1293 memcpy(vb2_plane_vaddr(dst_buf, 0),
1294 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1295 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1296 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1297 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1298 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1299 ctx->vpu_header_size[2]);
1300 } else {
1301 pic_stream_buffer_addr =
1302 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1303 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1304 }
1305
1306 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1307 force_ipicture = 1;
1308 switch (dst_fourcc) {
1309 case V4L2_PIX_FMT_H264:
1310 quant_param = ctx->params.h264_intra_qp;
1311 break;
1312 case V4L2_PIX_FMT_MPEG4:
1313 quant_param = ctx->params.mpeg4_intra_qp;
1314 break;
1315 default:
1316 v4l2_warn(&ctx->dev->v4l2_dev,
1317 "cannot set intra qp, fmt not supported\n");
1318 break;
1319 }
1320 } else {
1321 force_ipicture = 0;
1322 switch (dst_fourcc) {
1323 case V4L2_PIX_FMT_H264:
1324 quant_param = ctx->params.h264_inter_qp;
1325 break;
1326 case V4L2_PIX_FMT_MPEG4:
1327 quant_param = ctx->params.mpeg4_inter_qp;
1328 break;
1329 default:
1330 v4l2_warn(&ctx->dev->v4l2_dev,
1331 "cannot set inter qp, fmt not supported\n");
1332 break;
1333 }
1334 }
1335
1336 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001337 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001338 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1339
1340
1341 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001342 switch (q_data_src->fourcc) {
1343 case V4L2_PIX_FMT_YVU420:
1344 /* Switch Cb and Cr for YVU420 format */
1345 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1346 picture_cb = picture_cr + q_data_src->width / 2 *
1347 q_data_src->height / 2;
1348 break;
1349 case V4L2_PIX_FMT_YUV420:
1350 default:
1351 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1352 picture_cr = picture_cb + q_data_src->width / 2 *
1353 q_data_src->height / 2;
1354 break;
1355 }
Javier Martin186b2502012-07-26 05:53:35 -03001356
Philipp Zabel89548442014-07-11 06:36:17 -03001357 if (dev->devtype->product == CODA_960) {
1358 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1359 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1360 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1361
1362 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1363 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1364 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1365 } else {
1366 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1367 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1368 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1369 }
Javier Martin186b2502012-07-26 05:53:35 -03001370 coda_write(dev, force_ipicture << 1 & 0x2,
1371 CODA_CMD_ENC_PIC_OPTION);
1372
1373 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1374 coda_write(dev, pic_stream_buffer_size / 1024,
1375 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001376
1377 if (!ctx->streamon_out) {
1378 /* After streamoff on the output side, set the stream end flag */
1379 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1380 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1381 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001382}
1383
1384static void coda_device_run(void *m2m_priv)
1385{
1386 struct coda_ctx *ctx = m2m_priv;
1387 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001388 int ret;
1389
1390 mutex_lock(&ctx->buffer_mutex);
1391
1392 /*
1393 * If streamoff dequeued all buffers before we could get the lock,
1394 * just bail out immediately.
1395 */
1396 if ((!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1397 ctx->inst_type != CODA_INST_DECODER) ||
1398 !v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1399 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1400 "%d: device_run without buffers\n", ctx->idx);
1401 mutex_unlock(&ctx->buffer_mutex);
1402 schedule_work(&ctx->skip_run);
1403 return;
1404 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001405
1406 mutex_lock(&dev->coda_mutex);
1407
Philipp Zabel918c66f2013-06-27 06:59:01 -03001408 if (ctx->inst_type == CODA_INST_DECODER) {
1409 ret = coda_prepare_decode(ctx);
1410 if (ret < 0) {
1411 mutex_unlock(&dev->coda_mutex);
1412 mutex_unlock(&ctx->buffer_mutex);
1413 /* job_finish scheduled by prepare_decode */
1414 return;
1415 }
1416 } else {
1417 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001418 }
1419
Philipp Zabelc2d22512013-06-21 03:55:28 -03001420 if (dev->devtype->product != CODA_DX6)
1421 coda_write(dev, ctx->iram_info.axi_sram_use,
1422 CODA7_REG_BIT_AXI_SRAM_USE);
1423
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001424 /* 1 second timeout in case CODA locks up */
1425 schedule_delayed_work(&dev->timeout, HZ);
1426
Philipp Zabel918c66f2013-06-27 06:59:01 -03001427 if (ctx->inst_type == CODA_INST_DECODER)
1428 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001429 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1430}
1431
1432static int coda_job_ready(void *m2m_priv)
1433{
1434 struct coda_ctx *ctx = m2m_priv;
1435
1436 /*
1437 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001438 * and 1 frame are needed. In the decoder case,
1439 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001440 */
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001441 if (!v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) &&
1442 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001443 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1444 "not ready: not enough video buffers.\n");
1445 return 0;
1446 }
1447
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001448 if (!v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)) {
1449 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1450 "not ready: not enough video capture buffers.\n");
1451 return 0;
1452 }
1453
Philipp Zabel918c66f2013-06-27 06:59:01 -03001454 if (ctx->prescan_failed ||
1455 ((ctx->inst_type == CODA_INST_DECODER) &&
1456 (coda_get_bitstream_payload(ctx) < 512) &&
1457 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1458 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1459 "%d: not ready: not enough bitstream data.\n",
1460 ctx->idx);
1461 return 0;
1462 }
1463
Philipp Zabel3e7482682013-05-23 10:43:01 -03001464 if (ctx->aborting) {
1465 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1466 "not ready: aborting\n");
1467 return 0;
1468 }
1469
Javier Martin186b2502012-07-26 05:53:35 -03001470 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1471 "job ready\n");
1472 return 1;
1473}
1474
1475static void coda_job_abort(void *priv)
1476{
1477 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001478
1479 ctx->aborting = 1;
1480
1481 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1482 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001483}
1484
1485static void coda_lock(void *m2m_priv)
1486{
1487 struct coda_ctx *ctx = m2m_priv;
1488 struct coda_dev *pcdev = ctx->dev;
1489 mutex_lock(&pcdev->dev_mutex);
1490}
1491
1492static void coda_unlock(void *m2m_priv)
1493{
1494 struct coda_ctx *ctx = m2m_priv;
1495 struct coda_dev *pcdev = ctx->dev;
1496 mutex_unlock(&pcdev->dev_mutex);
1497}
1498
1499static struct v4l2_m2m_ops coda_m2m_ops = {
1500 .device_run = coda_device_run,
1501 .job_ready = coda_job_ready,
1502 .job_abort = coda_job_abort,
1503 .lock = coda_lock,
1504 .unlock = coda_unlock,
1505};
1506
Philipp Zabel89548442014-07-11 06:36:17 -03001507static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1508{
1509 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1510 int luma_map, chro_map, i;
1511
1512 memset(tiled_map, 0, sizeof(*tiled_map));
1513
1514 luma_map = 64;
1515 chro_map = 64;
1516 tiled_map->map_type = tiled_map_type;
1517 for (i = 0; i < 16; i++)
1518 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1519 for (i = 0; i < 4; i++)
1520 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1521 for (i = 0; i < 16; i++)
1522 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1523
1524 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1525 tiled_map->xy2rbc_config = 0;
1526 } else {
1527 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1528 tiled_map_type);
1529 return;
1530 }
1531}
1532
Javier Martin186b2502012-07-26 05:53:35 -03001533static void set_default_params(struct coda_ctx *ctx)
1534{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001535 int max_w;
1536 int max_h;
1537
1538 ctx->codec = &ctx->dev->devtype->codecs[0];
1539 max_w = ctx->codec->max_w;
1540 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001541
1542 ctx->params.codec_mode = CODA_MODE_INVALID;
1543 ctx->colorspace = V4L2_COLORSPACE_REC709;
1544 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001545 ctx->aborting = 0;
1546
1547 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001548 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1549 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1550 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1551 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1552 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1553 ctx->q_data[V4L2_M2M_DST].width = max_w;
1554 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001555 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001556 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1557 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1558 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1559 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001560
1561 if (ctx->dev->devtype->product == CODA_960)
1562 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001563}
1564
1565/*
1566 * Queue operations
1567 */
1568static int coda_queue_setup(struct vb2_queue *vq,
1569 const struct v4l2_format *fmt,
1570 unsigned int *nbuffers, unsigned int *nplanes,
1571 unsigned int sizes[], void *alloc_ctxs[])
1572{
1573 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001574 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001575 unsigned int size;
1576
Philipp Zabele34db062012-08-29 08:22:00 -03001577 q_data = get_q_data(ctx, vq->type);
1578 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001579
1580 *nplanes = 1;
1581 sizes[0] = size;
1582
1583 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1584
1585 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1586 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1587
1588 return 0;
1589}
1590
1591static int coda_buf_prepare(struct vb2_buffer *vb)
1592{
1593 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1594 struct coda_q_data *q_data;
1595
1596 q_data = get_q_data(ctx, vb->vb2_queue->type);
1597
1598 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1599 v4l2_warn(&ctx->dev->v4l2_dev,
1600 "%s data will not fit into plane (%lu < %lu)\n",
1601 __func__, vb2_plane_size(vb, 0),
1602 (long)q_data->sizeimage);
1603 return -EINVAL;
1604 }
1605
Javier Martin186b2502012-07-26 05:53:35 -03001606 return 0;
1607}
1608
1609static void coda_buf_queue(struct vb2_buffer *vb)
1610{
1611 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001612 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001613 struct coda_q_data *q_data;
1614
1615 q_data = get_q_data(ctx, vb->vb2_queue->type);
1616
1617 /*
1618 * In the decoder case, immediately try to copy the buffer into the
1619 * bitstream ringbuffer and mark it as ready to be dequeued.
1620 */
1621 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1622 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1623 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001624 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001625 * the stream end
1626 */
Philipp Zabel89548442014-07-11 06:36:17 -03001627 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001628 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001629 if ((dev->devtype->product == CODA_960) &&
1630 coda_isbusy(dev) &&
1631 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1632 /* if this decoder instance is running, set the stream end flag */
1633 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1634 }
1635 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001636 mutex_lock(&ctx->bitstream_mutex);
1637 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1638 coda_fill_bitstream(ctx);
1639 mutex_unlock(&ctx->bitstream_mutex);
1640 } else {
1641 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
1642 }
Javier Martin186b2502012-07-26 05:53:35 -03001643}
1644
1645static void coda_wait_prepare(struct vb2_queue *q)
1646{
1647 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1648 coda_unlock(ctx);
1649}
1650
1651static void coda_wait_finish(struct vb2_queue *q)
1652{
1653 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1654 coda_lock(ctx);
1655}
1656
Philipp Zabel86eda902013-05-23 10:42:57 -03001657static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1658{
1659 struct coda_dev *dev = ctx->dev;
1660 u32 *p = ctx->parabuf.vaddr;
1661
1662 if (dev->devtype->product == CODA_DX6)
1663 p[index] = value;
1664 else
1665 p[index ^ 1] = value;
1666}
1667
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001668static int coda_alloc_aux_buf(struct coda_dev *dev,
1669 struct coda_aux_buf *buf, size_t size)
1670{
1671 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1672 GFP_KERNEL);
1673 if (!buf->vaddr)
1674 return -ENOMEM;
1675
1676 buf->size = size;
1677
1678 return 0;
1679}
1680
1681static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1682 struct coda_aux_buf *buf, size_t size)
1683{
1684 return coda_alloc_aux_buf(ctx->dev, buf, size);
1685}
1686
1687static void coda_free_aux_buf(struct coda_dev *dev,
1688 struct coda_aux_buf *buf)
1689{
1690 if (buf->vaddr) {
1691 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1692 buf->vaddr, buf->paddr);
1693 buf->vaddr = NULL;
1694 buf->size = 0;
1695 }
1696}
1697
1698static void coda_free_framebuffers(struct coda_ctx *ctx)
1699{
1700 int i;
1701
1702 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1703 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1704}
1705
Philipp Zabelec25f682012-07-20 08:54:29 -03001706static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1707{
1708 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001709 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001710 dma_addr_t paddr;
1711 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001712 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001713 int i;
1714
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001715 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1716 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001717 ysize = round_up(q_data->width, 8) * height;
1718
Philipp Zabelec25f682012-07-20 08:54:29 -03001719 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001720 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001721 size_t size;
1722
Philipp Zabelaed14b02014-07-11 06:36:15 -03001723 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001724 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1725 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001726 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001727 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1728 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001729 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001730 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001731 }
1732 }
1733
1734 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001735 for (i = 0; i < ctx->num_internal_frames; i++) {
1736 paddr = ctx->internal_frames[i].paddr;
1737 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1738 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1739 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001740
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001741 /* mvcol buffer for h.264 */
1742 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1743 dev->devtype->product != CODA_DX6)
1744 coda_parabuf_write(ctx, 96 + i,
1745 ctx->internal_frames[i].paddr +
1746 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001747 }
1748
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001749 /* mvcol buffer for mpeg4 */
1750 if ((dev->devtype->product != CODA_DX6) &&
1751 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1752 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1753 ysize + ysize/4 + ysize/4);
1754
Philipp Zabelec25f682012-07-20 08:54:29 -03001755 return 0;
1756}
1757
Javier Martin3f3f5c72012-10-29 05:20:29 -03001758static int coda_h264_padding(int size, char *p)
1759{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001760 int nal_size;
1761 int diff;
1762
Javier Martin832fbb52012-10-29 08:34:59 -03001763 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001764 if (diff == 0)
1765 return 0;
1766
Javier Martin832fbb52012-10-29 08:34:59 -03001767 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001768 memcpy(p, coda_filler_nal, nal_size);
1769
1770 /* Add rbsp stop bit and trailing at the end */
1771 *(p + nal_size - 1) = 0x80;
1772
1773 return nal_size;
1774}
1775
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001776static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1777{
1778 phys_addr_t ret;
1779
1780 size = round_up(size, 1024);
1781 if (size > iram->remaining)
1782 return 0;
1783 iram->remaining -= size;
1784
1785 ret = iram->next_paddr;
1786 iram->next_paddr += size;
1787
1788 return ret;
1789}
1790
Philipp Zabelc2d22512013-06-21 03:55:28 -03001791static void coda_setup_iram(struct coda_ctx *ctx)
1792{
1793 struct coda_iram_info *iram_info = &ctx->iram_info;
1794 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001795 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001796 int dbk_bits;
1797 int bit_bits;
1798 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001799
1800 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001801 iram_info->next_paddr = dev->iram.paddr;
1802 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001803
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001804 switch (dev->devtype->product) {
1805 case CODA_7541:
1806 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1807 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1808 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1809 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001810 case CODA_960:
1811 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1812 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1813 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1814 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001815 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001816 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001817 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001818
1819 if (ctx->inst_type == CODA_INST_ENCODER) {
1820 struct coda_q_data *q_data_src;
1821
1822 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1823 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1824
1825 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001826 if (dev->devtype->product == CODA_7541) {
1827 iram_info->search_ram_size = round_up(mb_width * 16 *
1828 36 + 2048, 1024);
1829 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1830 iram_info->search_ram_size);
1831 if (!iram_info->search_ram_paddr) {
1832 pr_err("IRAM is smaller than the search ram size\n");
1833 goto out;
1834 }
1835 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1836 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001837 }
1838
1839 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001840 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1841 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1842 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001843 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001844 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001845
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001846 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1847 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001848 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001849 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001850
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001851 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1852 if (!iram_info->buf_ip_ac_dc_use)
1853 goto out;
1854 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001855
Philipp Zabel8358e762013-06-21 03:55:32 -03001856 /* OVL and BTP disabled for encoder */
1857 } else if (ctx->inst_type == CODA_INST_DECODER) {
1858 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001859
1860 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1861 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001862
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001863 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1864 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1865 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001866 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001867 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001868
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001869 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1870 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001871 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001872 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001873
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001874 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1875 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001876 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001877 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001878
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001879 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001880 }
1881
1882out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001883 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1884 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1885 "IRAM smaller than needed\n");
1886
1887 if (dev->devtype->product == CODA_7541) {
1888 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001889 if (ctx->inst_type == CODA_INST_DECODER) {
1890 /* fw 1.4.50 */
1891 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1892 CODA7_USE_IP_ENABLE);
1893 } else {
1894 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001895 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1896 CODA7_USE_HOST_DBK_ENABLE |
1897 CODA7_USE_IP_ENABLE |
1898 CODA7_USE_DBK_ENABLE);
1899 }
1900 }
1901}
1902
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001903static void coda_free_context_buffers(struct coda_ctx *ctx)
1904{
1905 struct coda_dev *dev = ctx->dev;
1906
Philipp Zabel918c66f2013-06-27 06:59:01 -03001907 coda_free_aux_buf(dev, &ctx->slicebuf);
1908 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001909 if (dev->devtype->product != CODA_DX6)
1910 coda_free_aux_buf(dev, &ctx->workbuf);
1911}
1912
1913static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1914 struct coda_q_data *q_data)
1915{
1916 struct coda_dev *dev = ctx->dev;
1917 size_t size;
1918 int ret;
1919
1920 switch (dev->devtype->product) {
1921 case CODA_7541:
1922 size = CODA7_WORK_BUF_SIZE;
1923 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001924 case CODA_960:
1925 size = CODA9_WORK_BUF_SIZE;
1926 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1927 size += CODA9_PS_SAVE_SIZE;
1928 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001929 default:
1930 return 0;
1931 }
1932
Philipp Zabel918c66f2013-06-27 06:59:01 -03001933 if (ctx->psbuf.vaddr) {
1934 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1935 return -EBUSY;
1936 }
1937 if (ctx->slicebuf.vaddr) {
1938 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1939 return -EBUSY;
1940 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001941 if (ctx->workbuf.vaddr) {
1942 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1943 ret = -EBUSY;
1944 return -ENOMEM;
1945 }
1946
Philipp Zabel918c66f2013-06-27 06:59:01 -03001947 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1948 /* worst case slice size */
1949 size = (DIV_ROUND_UP(q_data->width, 16) *
1950 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1951 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1952 if (ret < 0) {
1953 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1954 ctx->slicebuf.size);
1955 return ret;
1956 }
1957 }
1958
1959 if (dev->devtype->product == CODA_7541) {
1960 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1961 if (ret < 0) {
1962 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1963 goto err;
1964 }
1965 }
1966
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001967 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1968 if (ret < 0) {
1969 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1970 ctx->workbuf.size);
1971 goto err;
1972 }
1973
1974 return 0;
1975
1976err:
1977 coda_free_context_buffers(ctx);
1978 return ret;
1979}
1980
Philipp Zabel918c66f2013-06-27 06:59:01 -03001981static int coda_start_decoding(struct coda_ctx *ctx)
1982{
1983 struct coda_q_data *q_data_src, *q_data_dst;
1984 u32 bitstream_buf, bitstream_size;
1985 struct coda_dev *dev = ctx->dev;
1986 int width, height;
1987 u32 src_fourcc;
1988 u32 val;
1989 int ret;
1990
1991 /* Start decoding */
1992 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1993 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1994 bitstream_buf = ctx->bitstream.paddr;
1995 bitstream_size = ctx->bitstream.size;
1996 src_fourcc = q_data_src->fourcc;
1997
1998 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1999
2000 /* Update coda bitstream read and write pointers from kfifo */
2001 coda_kfifo_sync_to_device_full(ctx);
2002
2003 ctx->display_idx = -1;
2004 ctx->frm_dis_flg = 0;
2005 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2006
2007 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2008 CODA_REG_BIT_BIT_STREAM_PARAM);
2009
2010 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2011 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2012 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002013 if ((dev->devtype->product == CODA_7541) ||
2014 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002015 val |= CODA_REORDER_ENABLE;
2016 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2017
2018 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002019 if (dev->devtype->product == CODA_960 &&
2020 src_fourcc == V4L2_PIX_FMT_MPEG4)
2021 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2022 else
2023 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002024 if (src_fourcc == V4L2_PIX_FMT_H264) {
2025 if (dev->devtype->product == CODA_7541) {
2026 coda_write(dev, ctx->psbuf.paddr,
2027 CODA_CMD_DEC_SEQ_PS_BB_START);
2028 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2029 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2030 }
Philipp Zabel89548442014-07-11 06:36:17 -03002031 if (dev->devtype->product == CODA_960) {
2032 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2033 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2034 }
2035 }
2036 if (dev->devtype->product != CODA_960) {
2037 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002038 }
2039
2040 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2041 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2042 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2043 return -ETIMEDOUT;
2044 }
2045
2046 /* Update kfifo out pointer from coda bitstream read pointer */
2047 coda_kfifo_sync_from_device(ctx);
2048
2049 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2050
2051 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2052 v4l2_err(&dev->v4l2_dev,
2053 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2054 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2055 return -EAGAIN;
2056 }
2057
2058 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2059 if (dev->devtype->product == CODA_DX6) {
2060 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2061 height = val & CODADX6_PICHEIGHT_MASK;
2062 } else {
2063 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2064 height = val & CODA7_PICHEIGHT_MASK;
2065 }
2066
2067 if (width > q_data_dst->width || height > q_data_dst->height) {
2068 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2069 width, height, q_data_dst->width, q_data_dst->height);
2070 return -EINVAL;
2071 }
2072
2073 width = round_up(width, 16);
2074 height = round_up(height, 16);
2075
2076 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2077 __func__, ctx->idx, width, height);
2078
Philipp Zabela500a932014-07-11 06:36:13 -03002079 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002080 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2081 v4l2_err(&dev->v4l2_dev,
2082 "not enough framebuffers to decode (%d < %d)\n",
2083 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2084 return -EINVAL;
2085 }
2086
Philipp Zabel52c41672014-07-11 06:36:19 -03002087 if (src_fourcc == V4L2_PIX_FMT_H264) {
2088 u32 left_right;
2089 u32 top_bottom;
2090
2091 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2092 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2093
2094 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2095 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2096 q_data_dst->rect.width = width - q_data_dst->rect.left -
2097 (left_right & 0x3ff);
2098 q_data_dst->rect.height = height - q_data_dst->rect.top -
2099 (top_bottom & 0x3ff);
2100 }
2101
Philipp Zabel918c66f2013-06-27 06:59:01 -03002102 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2103 if (ret < 0)
2104 return ret;
2105
2106 /* Tell the decoder how many frame buffers we allocated. */
2107 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2108 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2109
2110 if (dev->devtype->product != CODA_DX6) {
2111 /* Set secondary AXI IRAM */
2112 coda_setup_iram(ctx);
2113
2114 coda_write(dev, ctx->iram_info.buf_bit_use,
2115 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2116 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2117 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2118 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2119 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2120 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2121 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2122 coda_write(dev, ctx->iram_info.buf_ovl_use,
2123 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002124 if (dev->devtype->product == CODA_960)
2125 coda_write(dev, ctx->iram_info.buf_btp_use,
2126 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2127 }
2128
2129 if (dev->devtype->product == CODA_960) {
2130 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2131
2132 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2133 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2134 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2135 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2136 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2137 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002138 }
2139
2140 if (src_fourcc == V4L2_PIX_FMT_H264) {
2141 coda_write(dev, ctx->slicebuf.paddr,
2142 CODA_CMD_SET_FRAME_SLICE_BB_START);
2143 coda_write(dev, ctx->slicebuf.size / 1024,
2144 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2145 }
2146
2147 if (dev->devtype->product == CODA_7541) {
2148 int max_mb_x = 1920 / 16;
2149 int max_mb_y = 1088 / 16;
2150 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002151
Philipp Zabel918c66f2013-06-27 06:59:01 -03002152 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2153 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002154 } else if (dev->devtype->product == CODA_960) {
2155 int max_mb_x = 1920 / 16;
2156 int max_mb_y = 1088 / 16;
2157 int max_mb_num = max_mb_x * max_mb_y;
2158
2159 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2160 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002161 }
2162
2163 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2164 v4l2_err(&ctx->dev->v4l2_dev,
2165 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2166 return -ETIMEDOUT;
2167 }
2168
2169 return 0;
2170}
2171
Philipp Zabeld35167a2013-05-23 10:42:59 -03002172static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2173 int header_code, u8 *header, int *size)
2174{
2175 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002176 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002177 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002178 int i;
2179
2180 if (dev->devtype->product == CODA_960)
2181 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002182
2183 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2184 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002185 bufsize = vb2_plane_size(buf, 0);
2186 if (dev->devtype->product == CODA_960)
2187 bufsize /= 1024;
2188 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002189 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2190 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2191 if (ret < 0) {
2192 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2193 return ret;
2194 }
Philipp Zabel89548442014-07-11 06:36:17 -03002195
2196 if (dev->devtype->product == CODA_960) {
2197 for (i = 63; i > 0; i--)
2198 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2199 break;
2200 *size = i + 1;
2201 } else {
2202 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2203 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2204 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002205 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2206
2207 return 0;
2208}
2209
Philipp Zabel89548442014-07-11 06:36:17 -03002210static int coda_start_encoding(struct coda_ctx *ctx);
2211
Javier Martin186b2502012-07-26 05:53:35 -03002212static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2213{
2214 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2215 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002216 struct coda_dev *dev = ctx->dev;
2217 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002218 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002219 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002220
Philipp Zabelb96904e2013-05-23 10:42:58 -03002221 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002222 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2223 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2224 if (coda_get_bitstream_payload(ctx) < 512)
2225 return -EINVAL;
2226 } else {
2227 if (count < 1)
2228 return -EINVAL;
2229 }
2230
2231 ctx->streamon_out = 1;
2232
Philipp Zabelb96904e2013-05-23 10:42:58 -03002233 if (coda_format_is_yuv(q_data_src->fourcc))
2234 ctx->inst_type = CODA_INST_ENCODER;
2235 else
2236 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002237 } else {
2238 if (count < 1)
2239 return -EINVAL;
2240
2241 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002242 }
Javier Martin186b2502012-07-26 05:53:35 -03002243
Philipp Zabelb96904e2013-05-23 10:42:58 -03002244 /* Don't start the coda unless both queues are on */
2245 if (!(ctx->streamon_out & ctx->streamon_cap))
2246 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002247
Philipp Zabeleb107512013-09-30 10:34:45 -03002248 /* Allow decoder device_run with no new buffers queued */
2249 if (ctx->inst_type == CODA_INST_DECODER)
2250 v4l2_m2m_set_src_buffered(ctx->m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002251
Philipp Zabelb96904e2013-05-23 10:42:58 -03002252 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002253 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002254 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002255
Philipp Zabelb96904e2013-05-23 10:42:58 -03002256 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2257 q_data_dst->fourcc);
2258 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002259 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2260 return -EINVAL;
2261 }
2262
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002263 /* Allocate per-instance buffers */
2264 ret = coda_alloc_context_buffers(ctx, q_data_src);
2265 if (ret < 0)
2266 return ret;
2267
Philipp Zabel918c66f2013-06-27 06:59:01 -03002268 if (ctx->inst_type == CODA_INST_DECODER) {
2269 mutex_lock(&dev->coda_mutex);
2270 ret = coda_start_decoding(ctx);
2271 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002272 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002273 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002274 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002275 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002276 } else {
2277 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002278 }
2279
Philipp Zabel89548442014-07-11 06:36:17 -03002280 ctx->initialized = 1;
2281 return ret;
2282}
2283
2284static int coda_start_encoding(struct coda_ctx *ctx)
2285{
2286 struct coda_dev *dev = ctx->dev;
2287 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2288 struct coda_q_data *q_data_src, *q_data_dst;
2289 u32 bitstream_buf, bitstream_size;
2290 struct vb2_buffer *buf;
2291 int gamma, ret, value;
2292 u32 dst_fourcc;
2293
2294 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2295 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2296 dst_fourcc = q_data_dst->fourcc;
2297
2298 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2299 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2300 bitstream_size = q_data_dst->sizeimage;
2301
Javier Martin186b2502012-07-26 05:53:35 -03002302 if (!coda_is_initialized(dev)) {
2303 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2304 return -EFAULT;
2305 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002306
2307 mutex_lock(&dev->coda_mutex);
2308
Javier Martin186b2502012-07-26 05:53:35 -03002309 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002310 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2311 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002312 switch (dev->devtype->product) {
2313 case CODA_DX6:
2314 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2315 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2316 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002317 case CODA_960:
2318 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2319 /* fallthrough */
2320 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002321 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2322 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002323 break;
Javier Martin186b2502012-07-26 05:53:35 -03002324 }
2325
Philipp Zabel89548442014-07-11 06:36:17 -03002326 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2327 value &= ~(1 << 2 | 0x7 << 9);
2328 ctx->frame_mem_ctrl = value;
2329 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2330
Philipp Zabel10436672012-07-02 09:03:55 -03002331 if (dev->devtype->product == CODA_DX6) {
2332 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002333 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002334 }
Javier Martin186b2502012-07-26 05:53:35 -03002335
2336 /* Could set rotation here if needed */
2337 switch (dev->devtype->product) {
2338 case CODA_DX6:
2339 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002340 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002341 break;
2342 default:
2343 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002344 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002345 }
Javier Martin186b2502012-07-26 05:53:35 -03002346 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2347 coda_write(dev, ctx->params.framerate,
2348 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2349
Philipp Zabelb96904e2013-05-23 10:42:58 -03002350 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002351 switch (dst_fourcc) {
2352 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002353 if (dev->devtype->product == CODA_960)
2354 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2355 else
2356 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002357 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2358 break;
2359 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002360 if (dev->devtype->product == CODA_960)
2361 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2362 else
2363 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002364 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2365 break;
2366 default:
2367 v4l2_err(v4l2_dev,
2368 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002369 ret = -EINVAL;
2370 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002371 }
2372
Philipp Zabelc566c782012-08-08 11:59:38 -03002373 switch (ctx->params.slice_mode) {
2374 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2375 value = 0;
2376 break;
2377 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2378 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2379 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002380 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002381 break;
2382 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2383 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2384 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2385 value |= 1 & CODA_SLICING_MODE_MASK;
2386 break;
2387 }
Javier Martin186b2502012-07-26 05:53:35 -03002388 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002389 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002390 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2391
2392 if (ctx->params.bitrate) {
2393 /* Rate control enabled */
2394 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2395 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002396 if (dev->devtype->product == CODA_960)
2397 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002398 } else {
2399 value = 0;
2400 }
2401 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2402
2403 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2404 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2405
2406 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2407 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2408
Javier Martin186b2502012-07-26 05:53:35 -03002409
Philipp Zabel89548442014-07-11 06:36:17 -03002410 value = 0;
2411 if (dev->devtype->product == CODA_960)
2412 gamma = CODA9_DEFAULT_GAMMA;
2413 else
2414 gamma = CODA_DEFAULT_GAMMA;
2415 if (gamma > 0) {
2416 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2417 CODA_CMD_ENC_SEQ_RC_GAMMA);
2418 }
2419 if (dev->devtype->product == CODA_960) {
2420 if (CODA_DEFAULT_GAMMA > 0)
2421 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002422 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002423 if (CODA_DEFAULT_GAMMA > 0) {
2424 if (dev->devtype->product == CODA_DX6)
2425 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2426 else
2427 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2428 }
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002429 }
Javier Martin186b2502012-07-26 05:53:35 -03002430 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2431
Philipp Zabel89548442014-07-11 06:36:17 -03002432 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2433
Philipp Zabelc2d22512013-06-21 03:55:28 -03002434 coda_setup_iram(ctx);
2435
Javier Martin186b2502012-07-26 05:53:35 -03002436 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002437 switch (dev->devtype->product) {
2438 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002439 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002440 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002441 break;
2442 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002443 coda_write(dev, ctx->iram_info.search_ram_paddr,
2444 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2445 coda_write(dev, ctx->iram_info.search_ram_size,
2446 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002447 break;
2448 case CODA_960:
2449 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2450 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002451 }
Javier Martin186b2502012-07-26 05:53:35 -03002452 }
2453
Philipp Zabelfcb62822013-05-23 10:43:00 -03002454 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2455 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002456 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002457 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002458 }
2459
Philipp Zabelfcb62822013-05-23 10:43:00 -03002460 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2461 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2462 ret = -EFAULT;
2463 goto out;
2464 }
Javier Martin186b2502012-07-26 05:53:35 -03002465
Philipp Zabel89548442014-07-11 06:36:17 -03002466 if (dev->devtype->product == CODA_960)
2467 ctx->num_internal_frames = 4;
2468 else
2469 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002470 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002471 if (ret < 0) {
2472 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2473 goto out;
2474 }
Javier Martin186b2502012-07-26 05:53:35 -03002475
Philipp Zabelec25f682012-07-20 08:54:29 -03002476 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002477 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002478 if (dev->devtype->product == CODA_7541)
2479 coda_write(dev, round_up(q_data_src->width, 8),
2480 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002481 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002482 coda_write(dev, ctx->iram_info.buf_bit_use,
2483 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2484 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2485 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2486 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2487 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2488 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2489 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2490 coda_write(dev, ctx->iram_info.buf_ovl_use,
2491 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002492 if (dev->devtype->product == CODA_960) {
2493 coda_write(dev, ctx->iram_info.buf_btp_use,
2494 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2495
2496 /* FIXME */
2497 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2498 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2499 }
Philipp Zabel10436672012-07-02 09:03:55 -03002500 }
Philipp Zabel89548442014-07-11 06:36:17 -03002501
Philipp Zabelfcb62822013-05-23 10:43:00 -03002502 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2503 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002504 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002505 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002506 }
2507
2508 /* Save stream headers */
2509 buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2510 switch (dst_fourcc) {
2511 case V4L2_PIX_FMT_H264:
2512 /*
2513 * Get SPS in the first frame and copy it to an
2514 * intermediate buffer.
2515 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002516 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2517 &ctx->vpu_header[0][0],
2518 &ctx->vpu_header_size[0]);
2519 if (ret < 0)
2520 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002521
2522 /*
2523 * Get PPS in the first frame and copy it to an
2524 * intermediate buffer.
2525 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002526 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2527 &ctx->vpu_header[1][0],
2528 &ctx->vpu_header_size[1]);
2529 if (ret < 0)
2530 goto out;
2531
Javier Martin3f3f5c72012-10-29 05:20:29 -03002532 /*
2533 * Length of H.264 headers is variable and thus it might not be
2534 * aligned for the coda to append the encoded frame. In that is
2535 * the case a filler NAL must be added to header 2.
2536 */
2537 ctx->vpu_header_size[2] = coda_h264_padding(
2538 (ctx->vpu_header_size[0] +
2539 ctx->vpu_header_size[1]),
2540 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002541 break;
2542 case V4L2_PIX_FMT_MPEG4:
2543 /*
2544 * Get VOS in the first frame and copy it to an
2545 * intermediate buffer
2546 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002547 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2548 &ctx->vpu_header[0][0],
2549 &ctx->vpu_header_size[0]);
2550 if (ret < 0)
2551 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002552
Philipp Zabeld35167a2013-05-23 10:42:59 -03002553 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2554 &ctx->vpu_header[1][0],
2555 &ctx->vpu_header_size[1]);
2556 if (ret < 0)
2557 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002558
Philipp Zabeld35167a2013-05-23 10:42:59 -03002559 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2560 &ctx->vpu_header[2][0],
2561 &ctx->vpu_header_size[2]);
2562 if (ret < 0)
2563 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002564 break;
2565 default:
2566 /* No more formats need to save headers at the moment */
2567 break;
2568 }
2569
Philipp Zabeld35167a2013-05-23 10:42:59 -03002570out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002571 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002572 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002573}
2574
Hans Verkuile37559b2014-04-17 02:47:21 -03002575static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002576{
2577 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002578 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002579
2580 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002581 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002582 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002583 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002584
Philipp Zabel89548442014-07-11 06:36:17 -03002585 if (ctx->inst_type == CODA_INST_DECODER &&
2586 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2587 /* if this decoder instance is running, set the stream end flag */
2588 if (dev->devtype->product == CODA_960) {
2589 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2590
2591 val |= CODA_BIT_STREAM_END_FLAG;
2592 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2593 ctx->bit_stream_param = val;
2594 }
2595 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002596 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2597
2598 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002599 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002600 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002601 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002602 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002603
2604 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002605 }
2606
Philipp Zabel918c66f2013-06-27 06:59:01 -03002607 if (!ctx->streamon_out && !ctx->streamon_cap) {
2608 kfifo_init(&ctx->bitstream_fifo,
2609 ctx->bitstream.vaddr, ctx->bitstream.size);
2610 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002611 }
Javier Martin186b2502012-07-26 05:53:35 -03002612}
2613
2614static struct vb2_ops coda_qops = {
2615 .queue_setup = coda_queue_setup,
2616 .buf_prepare = coda_buf_prepare,
2617 .buf_queue = coda_buf_queue,
2618 .wait_prepare = coda_wait_prepare,
2619 .wait_finish = coda_wait_finish,
2620 .start_streaming = coda_start_streaming,
2621 .stop_streaming = coda_stop_streaming,
2622};
2623
2624static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2625{
2626 struct coda_ctx *ctx =
2627 container_of(ctrl->handler, struct coda_ctx, ctrls);
2628
2629 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2630 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2631
2632 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002633 case V4L2_CID_HFLIP:
2634 if (ctrl->val)
2635 ctx->params.rot_mode |= CODA_MIR_HOR;
2636 else
2637 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2638 break;
2639 case V4L2_CID_VFLIP:
2640 if (ctrl->val)
2641 ctx->params.rot_mode |= CODA_MIR_VER;
2642 else
2643 ctx->params.rot_mode &= ~CODA_MIR_VER;
2644 break;
Javier Martin186b2502012-07-26 05:53:35 -03002645 case V4L2_CID_MPEG_VIDEO_BITRATE:
2646 ctx->params.bitrate = ctrl->val / 1000;
2647 break;
2648 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2649 ctx->params.gop_size = ctrl->val;
2650 break;
2651 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2652 ctx->params.h264_intra_qp = ctrl->val;
2653 break;
2654 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2655 ctx->params.h264_inter_qp = ctrl->val;
2656 break;
2657 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2658 ctx->params.mpeg4_intra_qp = ctrl->val;
2659 break;
2660 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2661 ctx->params.mpeg4_inter_qp = ctrl->val;
2662 break;
2663 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2664 ctx->params.slice_mode = ctrl->val;
2665 break;
2666 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2667 ctx->params.slice_max_mb = ctrl->val;
2668 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002669 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2670 ctx->params.slice_max_bits = ctrl->val * 8;
2671 break;
Javier Martin186b2502012-07-26 05:53:35 -03002672 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2673 break;
2674 default:
2675 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2676 "Invalid control, id=%d, val=%d\n",
2677 ctrl->id, ctrl->val);
2678 return -EINVAL;
2679 }
2680
2681 return 0;
2682}
2683
2684static struct v4l2_ctrl_ops coda_ctrl_ops = {
2685 .s_ctrl = coda_s_ctrl,
2686};
2687
2688static int coda_ctrls_setup(struct coda_ctx *ctx)
2689{
2690 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2691
2692 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002693 V4L2_CID_HFLIP, 0, 1, 1, 0);
2694 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2695 V4L2_CID_VFLIP, 0, 1, 1, 0);
2696 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002697 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2698 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2699 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2700 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002701 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002702 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002703 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002704 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2705 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2706 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2707 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2708 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2709 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002710 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2711 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002712 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2713 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002714 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2715 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002716 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2717 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2718 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2719 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2720 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2721
2722 if (ctx->ctrls.error) {
2723 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2724 ctx->ctrls.error);
2725 return -EINVAL;
2726 }
2727
2728 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2729}
2730
2731static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2732 struct vb2_queue *dst_vq)
2733{
2734 struct coda_ctx *ctx = priv;
2735 int ret;
2736
Javier Martin186b2502012-07-26 05:53:35 -03002737 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002738 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002739 src_vq->drv_priv = ctx;
2740 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2741 src_vq->ops = &coda_qops;
2742 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002743 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002744
2745 ret = vb2_queue_init(src_vq);
2746 if (ret)
2747 return ret;
2748
Javier Martin186b2502012-07-26 05:53:35 -03002749 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002750 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002751 dst_vq->drv_priv = ctx;
2752 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2753 dst_vq->ops = &coda_qops;
2754 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002755 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Javier Martin186b2502012-07-26 05:53:35 -03002756
2757 return vb2_queue_init(dst_vq);
2758}
2759
Philipp Zabele11f3e62012-07-25 09:16:58 -03002760static int coda_next_free_instance(struct coda_dev *dev)
2761{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002762 int idx = ffz(dev->instance_mask);
2763
2764 if ((idx < 0) ||
2765 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2766 return -EBUSY;
2767
2768 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002769}
2770
Javier Martin186b2502012-07-26 05:53:35 -03002771static int coda_open(struct file *file)
2772{
2773 struct coda_dev *dev = video_drvdata(file);
2774 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002775 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002776 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002777
Javier Martin186b2502012-07-26 05:53:35 -03002778 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2779 if (!ctx)
2780 return -ENOMEM;
2781
Fabio Estevamf82bc202013-08-21 11:14:16 -03002782 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002783 if (idx < 0) {
2784 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002785 goto err_coda_max;
2786 }
2787 set_bit(idx, &dev->instance_mask);
2788
Philipp Zabel918c66f2013-06-27 06:59:01 -03002789 INIT_WORK(&ctx->skip_run, coda_skip_run);
Javier Martin186b2502012-07-26 05:53:35 -03002790 v4l2_fh_init(&ctx->fh, video_devdata(file));
2791 file->private_data = &ctx->fh;
2792 v4l2_fh_add(&ctx->fh);
2793 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002794 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002795 switch (dev->devtype->product) {
2796 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002797 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002798 ctx->reg_idx = 0;
2799 break;
2800 default:
2801 ctx->reg_idx = idx;
2802 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002803
Fabio Estevam79830db2013-08-21 11:14:17 -03002804 ret = clk_prepare_enable(dev->clk_per);
2805 if (ret)
2806 goto err_clk_per;
2807
2808 ret = clk_prepare_enable(dev->clk_ahb);
2809 if (ret)
2810 goto err_clk_ahb;
2811
Javier Martin186b2502012-07-26 05:53:35 -03002812 set_default_params(ctx);
2813 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2814 &coda_queue_init);
2815 if (IS_ERR(ctx->m2m_ctx)) {
Philipp Zabel72720ff2013-05-21 10:31:25 -03002816 ret = PTR_ERR(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002817
2818 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2819 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002820 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002821 }
2822 ret = coda_ctrls_setup(ctx);
2823 if (ret) {
2824 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002825 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002826 }
2827
2828 ctx->fh.ctrl_handler = &ctx->ctrls;
2829
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002830 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2831 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002832 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002833 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002834 }
2835
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002836 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2837 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2838 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2839 if (!ctx->bitstream.vaddr) {
2840 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2841 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002842 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002843 }
2844 kfifo_init(&ctx->bitstream_fifo,
2845 ctx->bitstream.vaddr, ctx->bitstream.size);
2846 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002847 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002848
Javier Martin186b2502012-07-26 05:53:35 -03002849 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002850 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002851 coda_unlock(ctx);
2852
Javier Martin186b2502012-07-26 05:53:35 -03002853 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2854 ctx->idx, ctx);
2855
2856 return 0;
2857
Fabio Estevamf82bc202013-08-21 11:14:16 -03002858err_dma_writecombine:
2859 coda_free_context_buffers(ctx);
2860 if (ctx->dev->devtype->product == CODA_DX6)
2861 coda_free_aux_buf(dev, &ctx->workbuf);
2862 coda_free_aux_buf(dev, &ctx->parabuf);
2863err_dma_alloc:
2864 v4l2_ctrl_handler_free(&ctx->ctrls);
2865err_ctrls_setup:
2866 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2867err_ctx_init:
2868 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002869err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002870 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002871err_clk_per:
Javier Martin186b2502012-07-26 05:53:35 -03002872 v4l2_fh_del(&ctx->fh);
2873 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002874 clear_bit(ctx->idx, &dev->instance_mask);
2875err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002876 kfree(ctx);
2877 return ret;
2878}
2879
2880static int coda_release(struct file *file)
2881{
2882 struct coda_dev *dev = video_drvdata(file);
2883 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2884
2885 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2886 ctx);
2887
Philipp Zabel918c66f2013-06-27 06:59:01 -03002888 /* If this instance is running, call .job_abort and wait for it to end */
2889 v4l2_m2m_ctx_release(ctx->m2m_ctx);
2890
2891 /* In case the instance was not running, we still need to call SEQ_END */
2892 mutex_lock(&dev->coda_mutex);
2893 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2894 "%s: sent command 'SEQ_END' to coda\n", __func__);
2895 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
2896 v4l2_err(&dev->v4l2_dev,
2897 "CODA_COMMAND_SEQ_END failed\n");
2898 mutex_unlock(&dev->coda_mutex);
2899 return -ETIMEDOUT;
2900 }
2901 mutex_unlock(&dev->coda_mutex);
2902
2903 coda_free_framebuffers(ctx);
2904
Javier Martin186b2502012-07-26 05:53:35 -03002905 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002906 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002907 coda_unlock(ctx);
2908
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002909 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2910 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002911 coda_free_context_buffers(ctx);
2912 if (ctx->dev->devtype->product == CODA_DX6)
2913 coda_free_aux_buf(dev, &ctx->workbuf);
2914
2915 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002916 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002917 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002918 clk_disable_unprepare(dev->clk_per);
Javier Martin186b2502012-07-26 05:53:35 -03002919 v4l2_fh_del(&ctx->fh);
2920 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002921 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002922 kfree(ctx);
2923
2924 return 0;
2925}
2926
2927static unsigned int coda_poll(struct file *file,
2928 struct poll_table_struct *wait)
2929{
2930 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2931 int ret;
2932
2933 coda_lock(ctx);
2934 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
2935 coda_unlock(ctx);
2936 return ret;
2937}
2938
2939static int coda_mmap(struct file *file, struct vm_area_struct *vma)
2940{
2941 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2942
2943 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
2944}
2945
2946static const struct v4l2_file_operations coda_fops = {
2947 .owner = THIS_MODULE,
2948 .open = coda_open,
2949 .release = coda_release,
2950 .poll = coda_poll,
2951 .unlocked_ioctl = video_ioctl2,
2952 .mmap = coda_mmap,
2953};
2954
Philipp Zabel918c66f2013-06-27 06:59:01 -03002955static void coda_finish_decode(struct coda_ctx *ctx)
2956{
2957 struct coda_dev *dev = ctx->dev;
2958 struct coda_q_data *q_data_src;
2959 struct coda_q_data *q_data_dst;
2960 struct vb2_buffer *dst_buf;
2961 int width, height;
2962 int decoded_idx;
2963 int display_idx;
2964 u32 src_fourcc;
2965 int success;
2966 u32 val;
2967
2968 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
2969
2970 /* Update kfifo out pointer from coda bitstream read pointer */
2971 coda_kfifo_sync_from_device(ctx);
2972
2973 /*
2974 * in stream-end mode, the read pointer can overshoot the write pointer
2975 * by up to 512 bytes
2976 */
2977 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2978 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2979 kfifo_init(&ctx->bitstream_fifo,
2980 ctx->bitstream.vaddr, ctx->bitstream.size);
2981 }
2982
2983 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2984 src_fourcc = q_data_src->fourcc;
2985
2986 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2987 if (val != 1)
2988 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2989
2990 success = val & 0x1;
2991 if (!success)
2992 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2993
2994 if (src_fourcc == V4L2_PIX_FMT_H264) {
2995 if (val & (1 << 3))
2996 v4l2_err(&dev->v4l2_dev,
2997 "insufficient PS buffer space (%d bytes)\n",
2998 ctx->psbuf.size);
2999 if (val & (1 << 2))
3000 v4l2_err(&dev->v4l2_dev,
3001 "insufficient slice buffer space (%d bytes)\n",
3002 ctx->slicebuf.size);
3003 }
3004
3005 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3006 width = (val >> 16) & 0xffff;
3007 height = val & 0xffff;
3008
3009 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3010
Philipp Zabel52c41672014-07-11 06:36:19 -03003011 /* frame crop information */
3012 if (src_fourcc == V4L2_PIX_FMT_H264) {
3013 u32 left_right;
3014 u32 top_bottom;
3015
3016 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3017 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3018
3019 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3020 /* Keep current crop information */
3021 } else {
3022 struct v4l2_rect *rect = &q_data_dst->rect;
3023
3024 rect->left = left_right >> 16 & 0xffff;
3025 rect->top = top_bottom >> 16 & 0xffff;
3026 rect->width = width - rect->left -
3027 (left_right & 0xffff);
3028 rect->height = height - rect->top -
3029 (top_bottom & 0xffff);
3030 }
3031 } else {
3032 /* no cropping */
3033 }
3034
Philipp Zabel918c66f2013-06-27 06:59:01 -03003035 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3036 if (val > 0)
3037 v4l2_err(&dev->v4l2_dev,
3038 "errors in %d macroblocks\n", val);
3039
3040 if (dev->devtype->product == CODA_7541) {
3041 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3042 if (val == 0) {
3043 /* not enough bitstream data */
3044 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3045 "prescan failed: %d\n", val);
3046 ctx->prescan_failed = true;
3047 return;
3048 }
3049 }
3050
3051 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3052
3053 /*
3054 * The previous display frame was copied out by the rotator,
3055 * now it can be overwritten again
3056 */
3057 if (ctx->display_idx >= 0 &&
3058 ctx->display_idx < ctx->num_internal_frames) {
3059 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3060 coda_write(dev, ctx->frm_dis_flg,
3061 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3062 }
3063
3064 /*
3065 * The index of the last decoded frame, not necessarily in
3066 * display order, and the index of the next display frame.
3067 * The latter could have been decoded in a previous run.
3068 */
3069 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3070 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3071
3072 if (decoded_idx == -1) {
3073 /* no frame was decoded, but we might have a display frame */
3074 if (display_idx < 0 && ctx->display_idx < 0)
3075 ctx->prescan_failed = true;
3076 } else if (decoded_idx == -2) {
3077 /* no frame was decoded, we still return the remaining buffers */
3078 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3079 v4l2_err(&dev->v4l2_dev,
3080 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003081 } else {
3082 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3083 if (val == 0)
3084 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3085 else if (val == 1)
3086 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3087 else
3088 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003089 }
3090
3091 if (display_idx == -1) {
3092 /*
3093 * no more frames to be decoded, but there could still
3094 * be rotator output to dequeue
3095 */
3096 ctx->prescan_failed = true;
3097 } else if (display_idx == -3) {
3098 /* possibly prescan failure */
3099 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3100 v4l2_err(&dev->v4l2_dev,
3101 "presentation frame index out of range: %d\n",
3102 display_idx);
3103 }
3104
3105 /* If a frame was copied out, return it */
3106 if (ctx->display_idx >= 0 &&
3107 ctx->display_idx < ctx->num_internal_frames) {
3108 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
3109 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3110
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003111 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3112 V4L2_BUF_FLAG_PFRAME |
3113 V4L2_BUF_FLAG_BFRAME);
3114 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
3115
Philipp Zabel918c66f2013-06-27 06:59:01 -03003116 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3117
3118 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3119 VB2_BUF_STATE_ERROR);
3120
3121 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3122 "job finished: decoding frame (%d) (%s)\n",
3123 dst_buf->v4l2_buf.sequence,
3124 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3125 "KEYFRAME" : "PFRAME");
3126 } else {
3127 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3128 "job finished: no frame decoded\n");
3129 }
3130
3131 /* The rotator will copy the current display frame next time */
3132 ctx->display_idx = display_idx;
3133}
3134
3135static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003136{
Philipp Zabelec25f682012-07-20 08:54:29 -03003137 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003138 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003139 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003140
Philipp Zabelec25f682012-07-20 08:54:29 -03003141 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03003142 dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003143
3144 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003145 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003146 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3147
Javier Martin186b2502012-07-26 05:53:35 -03003148 /* Calculate bytesused field */
3149 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003150 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3151 ctx->vpu_header_size[0] +
3152 ctx->vpu_header_size[1] +
3153 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003154 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003155 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003156 }
3157
3158 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3159 wr_ptr - start_ptr);
3160
3161 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3162 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3163
Philipp Zabel51660282013-09-30 10:34:49 -03003164 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003165 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3166 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3167 } else {
3168 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3169 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3170 }
3171
Kamil Debskiccd571c2013-04-24 10:38:24 -03003172 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003173 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3174 dst_buf->v4l2_buf.flags |=
3175 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003176 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3177
Philipp Zabelec25f682012-07-20 08:54:29 -03003178 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003179
3180 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003181 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3182
3183 ctx->gopcounter--;
3184 if (ctx->gopcounter < 0)
3185 ctx->gopcounter = ctx->params.gop_size - 1;
3186
3187 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3188 "job finished: encoding frame (%d) (%s)\n",
3189 dst_buf->v4l2_buf.sequence,
3190 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3191 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003192}
3193
3194static irqreturn_t coda_irq_handler(int irq, void *data)
3195{
3196 struct coda_dev *dev = data;
3197 struct coda_ctx *ctx;
3198
3199 cancel_delayed_work(&dev->timeout);
3200
3201 /* read status register to attend the IRQ */
3202 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3203 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3204 CODA_REG_BIT_INT_CLEAR);
3205
3206 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3207 if (ctx == NULL) {
3208 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3209 mutex_unlock(&dev->coda_mutex);
3210 return IRQ_HANDLED;
3211 }
3212
3213 if (ctx->aborting) {
3214 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3215 "task has been aborted\n");
Philipp Zabel918c66f2013-06-27 06:59:01 -03003216 goto out;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003217 }
3218
3219 if (coda_isbusy(ctx->dev)) {
3220 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3221 "coda is still busy!!!!\n");
3222 return IRQ_NONE;
3223 }
3224
Philipp Zabel918c66f2013-06-27 06:59:01 -03003225 if (ctx->inst_type == CODA_INST_DECODER)
3226 coda_finish_decode(ctx);
3227 else
3228 coda_finish_encode(ctx);
3229
3230out:
3231 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) {
3232 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3233 "%s: sent command 'SEQ_END' to coda\n", __func__);
3234 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
3235 v4l2_err(&dev->v4l2_dev,
3236 "CODA_COMMAND_SEQ_END failed\n");
3237 }
3238
3239 kfifo_init(&ctx->bitstream_fifo,
3240 ctx->bitstream.vaddr, ctx->bitstream.size);
3241
3242 coda_free_framebuffers(ctx);
3243 coda_free_context_buffers(ctx);
3244 }
Javier Martin186b2502012-07-26 05:53:35 -03003245
Philipp Zabelfcb62822013-05-23 10:43:00 -03003246 mutex_unlock(&dev->coda_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003247 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003248
Javier Martin186b2502012-07-26 05:53:35 -03003249 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
3250
3251 return IRQ_HANDLED;
3252}
3253
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003254static void coda_timeout(struct work_struct *work)
3255{
3256 struct coda_ctx *ctx;
3257 struct coda_dev *dev = container_of(to_delayed_work(work),
3258 struct coda_dev, timeout);
3259
Philipp Zabel39cc0292013-05-21 10:32:42 -03003260 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout, stopping all streams\n");
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003261
3262 mutex_lock(&dev->dev_mutex);
3263 list_for_each_entry(ctx, &dev->instances, list) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03003264 if (mutex_is_locked(&ctx->buffer_mutex))
3265 mutex_unlock(&ctx->buffer_mutex);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003266 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3267 v4l2_m2m_streamoff(NULL, ctx->m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3268 }
3269 mutex_unlock(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003270
3271 mutex_unlock(&dev->coda_mutex);
3272 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3273 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->m2m_ctx);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003274}
3275
Javier Martin186b2502012-07-26 05:53:35 -03003276static u32 coda_supported_firmwares[] = {
3277 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003278 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003279 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003280};
3281
3282static bool coda_firmware_supported(u32 vernum)
3283{
3284 int i;
3285
3286 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3287 if (vernum == coda_supported_firmwares[i])
3288 return true;
3289 return false;
3290}
3291
Philipp Zabel87048bb2012-07-02 07:03:43 -03003292static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003293{
3294 u16 product, major, minor, release;
3295 u32 data;
3296 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003297 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003298
Fabio Estevam79830db2013-08-21 11:14:17 -03003299 ret = clk_prepare_enable(dev->clk_per);
3300 if (ret)
3301 return ret;
3302
3303 ret = clk_prepare_enable(dev->clk_ahb);
3304 if (ret)
3305 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003306
Javier Martin186b2502012-07-26 05:53:35 -03003307 /*
3308 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003309 * The 16-bit chars in the code buffer are in memory access
3310 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003311 * Data in this SRAM survives a reboot.
3312 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003313 p = (u16 *)dev->codebuf.vaddr;
3314 if (dev->devtype->product == CODA_DX6) {
3315 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3316 data = CODA_DOWN_ADDRESS_SET(i) |
3317 CODA_DOWN_DATA_SET(p[i ^ 1]);
3318 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3319 }
3320 } else {
3321 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3322 data = CODA_DOWN_ADDRESS_SET(i) |
3323 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3324 3 - (i % 4)]);
3325 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3326 }
Javier Martin186b2502012-07-26 05:53:35 -03003327 }
Javier Martin186b2502012-07-26 05:53:35 -03003328
Philipp Zabel9acf7692013-05-23 10:42:56 -03003329 /* Clear registers */
3330 for (i = 0; i < 64; i++)
3331 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3332
Javier Martin186b2502012-07-26 05:53:35 -03003333 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003334 if (dev->devtype->product == CODA_960 ||
3335 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003336 coda_write(dev, dev->tempbuf.paddr,
3337 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003338 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003339 } else {
3340 coda_write(dev, dev->workbuf.paddr,
3341 CODA_REG_BIT_WORK_BUF_ADDR);
3342 }
Javier Martin186b2502012-07-26 05:53:35 -03003343 coda_write(dev, dev->codebuf.paddr,
3344 CODA_REG_BIT_CODE_BUF_ADDR);
3345 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3346
3347 /* Set default values */
3348 switch (dev->devtype->product) {
3349 case CODA_DX6:
3350 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3351 break;
3352 default:
3353 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3354 }
Philipp Zabel89548442014-07-11 06:36:17 -03003355 if (dev->devtype->product == CODA_960)
3356 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3357 else
3358 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003359
3360 if (dev->devtype->product != CODA_DX6)
3361 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3362
Javier Martin186b2502012-07-26 05:53:35 -03003363 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3364 CODA_REG_BIT_INT_ENABLE);
3365
3366 /* Reset VPU and start processor */
3367 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3368 data |= CODA_REG_RESET_ENABLE;
3369 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3370 udelay(10);
3371 data &= ~CODA_REG_RESET_ENABLE;
3372 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3373 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3374
3375 /* Load firmware */
3376 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3377 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3378 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3379 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3380 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3381 if (coda_wait_timeout(dev)) {
3382 clk_disable_unprepare(dev->clk_per);
3383 clk_disable_unprepare(dev->clk_ahb);
3384 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
3385 return -EIO;
3386 }
3387
Philipp Zabel89548442014-07-11 06:36:17 -03003388 if (dev->devtype->product == CODA_960) {
3389 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3390 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3391 data);
3392 }
3393
Javier Martin186b2502012-07-26 05:53:35 -03003394 /* Check we are compatible with the loaded firmware */
3395 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3396 product = CODA_FIRMWARE_PRODUCT(data);
3397 major = CODA_FIRMWARE_MAJOR(data);
3398 minor = CODA_FIRMWARE_MINOR(data);
3399 release = CODA_FIRMWARE_RELEASE(data);
3400
3401 clk_disable_unprepare(dev->clk_per);
3402 clk_disable_unprepare(dev->clk_ahb);
3403
3404 if (product != dev->devtype->product) {
3405 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3406 " Version: %u.%u.%u\n",
3407 coda_product_name(dev->devtype->product),
3408 coda_product_name(product), major, minor, release);
3409 return -EINVAL;
3410 }
3411
3412 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3413 coda_product_name(product));
3414
3415 if (coda_firmware_supported(data)) {
3416 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3417 major, minor, release);
3418 } else {
3419 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3420 "%u.%u.%u\n", major, minor, release);
3421 }
3422
3423 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003424
3425err_clk_ahb:
3426 clk_disable_unprepare(dev->clk_per);
3427 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003428}
3429
3430static void coda_fw_callback(const struct firmware *fw, void *context)
3431{
3432 struct coda_dev *dev = context;
3433 struct platform_device *pdev = dev->plat_dev;
3434 int ret;
3435
3436 if (!fw) {
3437 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3438 return;
3439 }
3440
3441 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003442 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3443 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003444 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3445 return;
3446 }
3447
Philipp Zabel87048bb2012-07-02 07:03:43 -03003448 /* Copy the whole firmware image to the code buffer */
3449 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3450 release_firmware(fw);
3451
3452 ret = coda_hw_init(dev);
Javier Martin186b2502012-07-26 05:53:35 -03003453 if (ret) {
3454 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3455 return;
3456 }
3457
3458 dev->vfd.fops = &coda_fops,
3459 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3460 dev->vfd.release = video_device_release_empty,
3461 dev->vfd.lock = &dev->dev_mutex;
3462 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003463 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003464 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3465 video_set_drvdata(&dev->vfd, dev);
3466
3467 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3468 if (IS_ERR(dev->alloc_ctx)) {
3469 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3470 return;
3471 }
3472
3473 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3474 if (IS_ERR(dev->m2m_dev)) {
3475 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3476 goto rel_ctx;
3477 }
3478
3479 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3480 if (ret) {
3481 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3482 goto rel_m2m;
3483 }
3484 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3485 dev->vfd.num);
3486
3487 return;
3488
3489rel_m2m:
3490 v4l2_m2m_release(dev->m2m_dev);
3491rel_ctx:
3492 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3493}
3494
3495static int coda_firmware_request(struct coda_dev *dev)
3496{
3497 char *fw = dev->devtype->firmware;
3498
3499 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3500 coda_product_name(dev->devtype->product));
3501
3502 return request_firmware_nowait(THIS_MODULE, true,
3503 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3504}
3505
3506enum coda_platform {
3507 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003508 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003509 CODA_IMX6Q,
3510 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003511};
3512
Emil Goodec06d8752012-08-14 17:44:42 -03003513static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003514 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003515 .firmware = "v4l-codadx6-imx27.bin",
3516 .product = CODA_DX6,
3517 .codecs = codadx6_codecs,
3518 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003519 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003520 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003521 .firmware = "v4l-coda7541-imx53.bin",
3522 .product = CODA_7541,
3523 .codecs = coda7_codecs,
3524 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003525 },
Philipp Zabel89548442014-07-11 06:36:17 -03003526 [CODA_IMX6Q] = {
3527 .firmware = "v4l-coda960-imx6q.bin",
3528 .product = CODA_960,
3529 .codecs = coda9_codecs,
3530 .num_codecs = ARRAY_SIZE(coda9_codecs),
3531 },
3532 [CODA_IMX6DL] = {
3533 .firmware = "v4l-coda960-imx6dl.bin",
3534 .product = CODA_960,
3535 .codecs = coda9_codecs,
3536 .num_codecs = ARRAY_SIZE(coda9_codecs),
3537 },
Javier Martin186b2502012-07-26 05:53:35 -03003538};
3539
3540static struct platform_device_id coda_platform_ids[] = {
3541 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003542 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003543 { /* sentinel */ }
3544};
3545MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3546
3547#ifdef CONFIG_OF
3548static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003549 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003550 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003551 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3552 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003553 { /* sentinel */ }
3554};
3555MODULE_DEVICE_TABLE(of, coda_dt_ids);
3556#endif
3557
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003558static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003559{
3560 const struct of_device_id *of_id =
3561 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3562 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003563 struct coda_platform_data *pdata = pdev->dev.platform_data;
3564 struct device_node *np = pdev->dev.of_node;
3565 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003566 struct coda_dev *dev;
3567 struct resource *res;
3568 int ret, irq;
3569
3570 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3571 if (!dev) {
3572 dev_err(&pdev->dev, "Not enough memory for %s\n",
3573 CODA_NAME);
3574 return -ENOMEM;
3575 }
3576
3577 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003578 INIT_LIST_HEAD(&dev->instances);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03003579 INIT_DELAYED_WORK(&dev->timeout, coda_timeout);
Javier Martin186b2502012-07-26 05:53:35 -03003580
3581 dev->plat_dev = pdev;
3582 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3583 if (IS_ERR(dev->clk_per)) {
3584 dev_err(&pdev->dev, "Could not get per clock\n");
3585 return PTR_ERR(dev->clk_per);
3586 }
3587
3588 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3589 if (IS_ERR(dev->clk_ahb)) {
3590 dev_err(&pdev->dev, "Could not get ahb clock\n");
3591 return PTR_ERR(dev->clk_ahb);
3592 }
3593
3594 /* Get memory for physical registers */
3595 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003596 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3597 if (IS_ERR(dev->regs_base))
3598 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003599
3600 /* IRQ */
3601 irq = platform_get_irq(pdev, 0);
3602 if (irq < 0) {
3603 dev_err(&pdev->dev, "failed to get irq resource\n");
3604 return -ENOENT;
3605 }
3606
Philipp Zabel918c66f2013-06-27 06:59:01 -03003607 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003608 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003609 dev_err(&pdev->dev, "failed to request irq\n");
3610 return -ENOENT;
3611 }
3612
Philipp Zabel657eee72013-04-29 16:17:14 -07003613 /* Get IRAM pool from device tree or platform data */
3614 pool = of_get_named_gen_pool(np, "iram", 0);
3615 if (!pool && pdata)
3616 pool = dev_get_gen_pool(pdata->iram_dev);
3617 if (!pool) {
3618 dev_err(&pdev->dev, "iram pool not available\n");
3619 return -ENOMEM;
3620 }
3621 dev->iram_pool = pool;
3622
Javier Martin186b2502012-07-26 05:53:35 -03003623 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3624 if (ret)
3625 return ret;
3626
3627 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003628 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003629
3630 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3631
3632 if (of_id) {
3633 dev->devtype = of_id->data;
3634 } else if (pdev_id) {
3635 dev->devtype = &coda_devdata[pdev_id->driver_data];
3636 } else {
3637 v4l2_device_unregister(&dev->v4l2_dev);
3638 return -EINVAL;
3639 }
3640
3641 /* allocate auxiliary per-device buffers for the BIT processor */
3642 switch (dev->devtype->product) {
3643 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003644 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3645 CODADX6_WORK_BUF_SIZE);
3646 if (ret < 0) {
3647 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3648 v4l2_device_unregister(&dev->v4l2_dev);
3649 return ret;
3650 }
Javier Martin186b2502012-07-26 05:53:35 -03003651 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003652 case CODA_7541:
3653 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3654 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003655 case CODA_960:
3656 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3657 break;
Javier Martin186b2502012-07-26 05:53:35 -03003658 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003659 if (dev->tempbuf.size) {
3660 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3661 dev->tempbuf.size);
3662 if (ret < 0) {
3663 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3664 v4l2_device_unregister(&dev->v4l2_dev);
3665 return ret;
3666 }
Javier Martin186b2502012-07-26 05:53:35 -03003667 }
3668
Philipp Zabel918c66f2013-06-27 06:59:01 -03003669 switch (dev->devtype->product) {
3670 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003671 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003672 break;
3673 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003674 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003675 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003676 case CODA_960:
3677 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003678 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003679 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3680 &dev->iram.paddr);
3681 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003682 dev_err(&pdev->dev, "unable to alloc iram\n");
3683 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003684 }
3685
Javier Martin186b2502012-07-26 05:53:35 -03003686 platform_set_drvdata(pdev, dev);
3687
3688 return coda_firmware_request(dev);
3689}
3690
3691static int coda_remove(struct platform_device *pdev)
3692{
3693 struct coda_dev *dev = platform_get_drvdata(pdev);
3694
3695 video_unregister_device(&dev->vfd);
3696 if (dev->m2m_dev)
3697 v4l2_m2m_release(dev->m2m_dev);
3698 if (dev->alloc_ctx)
3699 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3700 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003701 if (dev->iram.vaddr)
3702 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3703 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003704 coda_free_aux_buf(dev, &dev->codebuf);
3705 coda_free_aux_buf(dev, &dev->tempbuf);
3706 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003707 return 0;
3708}
3709
3710static struct platform_driver coda_driver = {
3711 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003712 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003713 .driver = {
3714 .name = CODA_NAME,
3715 .owner = THIS_MODULE,
3716 .of_match_table = of_match_ptr(coda_dt_ids),
3717 },
3718 .id_table = coda_platform_ids,
3719};
3720
3721module_platform_driver(coda_driver);
3722
3723MODULE_LICENSE("GPL");
3724MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3725MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");