blob: 87cc22606e2266c49168bc85722cb831b82828d3 [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070017#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030018#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030021#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030022#include <linux/module.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030025#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030026#include <linux/slab.h>
27#include <linux/videodev2.h>
28#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070029#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030030
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030033#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030034#include <media/v4l2-ioctl.h>
35#include <media/v4l2-mem2mem.h>
36#include <media/videobuf2-core.h>
37#include <media/videobuf2-dma-contig.h>
38
39#include "coda.h"
40
41#define CODA_NAME "coda"
42
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030043#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030044
45#define CODA_FMO_BUF_SIZE 32
46#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030047#define CODA7_WORK_BUF_SIZE (128 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030048#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030049#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030050#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030051#define CODA_PARA_BUF_SIZE (10 * 1024)
52#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070053#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030054#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030055#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030056
Philipp Zabel918c66f2013-06-27 06:59:01 -030057#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030058#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030059
60#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030061
Philipp Zabelb96904e2013-05-23 10:42:58 -030062#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030063#define FMO_SLICE_SAVE_BUF_SIZE (32)
64#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030065#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030066
67#define MIN_W 176
68#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030069
70#define S_ALIGN 1 /* multiple of 2 */
71#define W_ALIGN 1 /* multiple of 2 */
72#define H_ALIGN 1 /* multiple of 2 */
73
74#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
75
76static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030077module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030078MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
79
80enum {
81 V4L2_M2M_SRC = 0,
82 V4L2_M2M_DST = 1,
83};
84
Javier Martin186b2502012-07-26 05:53:35 -030085enum coda_inst_type {
86 CODA_INST_ENCODER,
87 CODA_INST_DECODER,
88};
89
90enum coda_product {
91 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030092 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030093 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030094};
95
96struct coda_fmt {
97 char *name;
98 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030099};
100
101struct coda_codec {
102 u32 mode;
103 u32 src_fourcc;
104 u32 dst_fourcc;
105 u32 max_w;
106 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300107};
108
109struct coda_devtype {
110 char *firmware;
111 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300112 struct coda_codec *codecs;
113 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300114 size_t workbuf_size;
115};
116
117/* Per-queue, driver-specific private data */
118struct coda_q_data {
119 unsigned int width;
120 unsigned int height;
121 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300122 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300123 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300124};
125
126struct coda_aux_buf {
127 void *vaddr;
128 dma_addr_t paddr;
129 u32 size;
130};
131
132struct coda_dev {
133 struct v4l2_device v4l2_dev;
134 struct video_device vfd;
135 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300136 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300137
138 void __iomem *regs_base;
139 struct clk *clk_per;
140 struct clk *clk_ahb;
141
142 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300143 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300144 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700145 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300146 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300147
148 spinlock_t irqlock;
149 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300150 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300151 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300152 struct v4l2_m2m_dev *m2m_dev;
153 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300154 struct list_head instances;
155 unsigned long instance_mask;
Javier Martin186b2502012-07-26 05:53:35 -0300156};
157
158struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300159 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300160 u8 h264_intra_qp;
161 u8 h264_inter_qp;
162 u8 mpeg4_intra_qp;
163 u8 mpeg4_inter_qp;
164 u8 gop_size;
165 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300166 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300167 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
168 u32 framerate;
169 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300170 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300171 u32 slice_max_mb;
172};
173
Philipp Zabelc2d22512013-06-21 03:55:28 -0300174struct coda_iram_info {
175 u32 axi_sram_use;
176 phys_addr_t buf_bit_use;
177 phys_addr_t buf_ip_ac_dc_use;
178 phys_addr_t buf_dbk_y_use;
179 phys_addr_t buf_dbk_c_use;
180 phys_addr_t buf_ovl_use;
181 phys_addr_t buf_btp_use;
182 phys_addr_t search_ram_paddr;
183 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300184 int remaining;
185 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300186};
187
Philipp Zabel89548442014-07-11 06:36:17 -0300188struct gdi_tiled_map {
189 int xy2ca_map[16];
190 int xy2ba_map[16];
191 int xy2ra_map[16];
192 int rbc2axi_map[32];
193 int xy2rbc_config;
194 int map_type;
195#define GDI_LINEAR_FRAME_MAP 0
196};
197
Javier Martin186b2502012-07-26 05:53:35 -0300198struct coda_ctx {
199 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300200 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300201 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300202 struct work_struct pic_run_work;
203 struct work_struct seq_end_work;
204 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300205 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300206 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300207 int streamon_out;
208 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300209 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300210 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300211 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300212 struct coda_q_data q_data[2];
213 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300214 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300215 enum v4l2_colorspace colorspace;
216 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300217 struct v4l2_ctrl_handler ctrls;
218 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300219 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300220 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300221 char vpu_header[3][64];
222 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300223 struct kfifo bitstream_fifo;
224 struct mutex bitstream_mutex;
225 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300226 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300227 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300228 struct coda_aux_buf psbuf;
229 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300230 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300231 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300232 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300233 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300234 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300235 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300236 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300237 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300238 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300239 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300240 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300241 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300242};
243
Javier Martin832fbb52012-10-29 08:34:59 -0300244static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
246static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300247
Javier Martin186b2502012-07-26 05:53:35 -0300248static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
249{
250 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
251 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
252 writel(data, dev->regs_base + reg);
253}
254
255static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
256{
257 u32 data;
258 data = readl(dev->regs_base + reg);
259 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
260 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
261 return data;
262}
263
264static inline unsigned long coda_isbusy(struct coda_dev *dev)
265{
266 return coda_read(dev, CODA_REG_BIT_BUSY);
267}
268
269static inline int coda_is_initialized(struct coda_dev *dev)
270{
271 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
272}
273
274static int coda_wait_timeout(struct coda_dev *dev)
275{
276 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
277
278 while (coda_isbusy(dev)) {
279 if (time_after(jiffies, timeout))
280 return -ETIMEDOUT;
281 }
282 return 0;
283}
284
285static void coda_command_async(struct coda_ctx *ctx, int cmd)
286{
287 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300288
Philipp Zabel89548442014-07-11 06:36:17 -0300289 if (dev->devtype->product == CODA_960 ||
290 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300291 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300292 coda_write(dev, ctx->bit_stream_param,
293 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300294 coda_write(dev, ctx->frm_dis_flg,
295 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300296 coda_write(dev, ctx->frame_mem_ctrl,
297 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300298 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
299 }
300
Philipp Zabel89548442014-07-11 06:36:17 -0300301 if (dev->devtype->product == CODA_960) {
302 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
303 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
304 }
305
Javier Martin186b2502012-07-26 05:53:35 -0300306 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
307
308 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
309 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300310 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
311
Javier Martin186b2502012-07-26 05:53:35 -0300312 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
313}
314
315static int coda_command_sync(struct coda_ctx *ctx, int cmd)
316{
317 struct coda_dev *dev = ctx->dev;
318
319 coda_command_async(ctx, cmd);
320 return coda_wait_timeout(dev);
321}
322
323static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
324 enum v4l2_buf_type type)
325{
326 switch (type) {
327 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
328 return &(ctx->q_data[V4L2_M2M_SRC]);
329 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
330 return &(ctx->q_data[V4L2_M2M_DST]);
331 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300332 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300333 }
Javier Martin186b2502012-07-26 05:53:35 -0300334}
335
336/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300337 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300338 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300339static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300340 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300341 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300342 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300343 },
344 {
345 .name = "YUV 4:2:0 Planar, YCrCb",
346 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300347 },
348 {
349 .name = "H264 Encoded Stream",
350 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300351 },
352 {
353 .name = "MPEG4 Encoded Stream",
354 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300355 },
356};
357
Philipp Zabelb96904e2013-05-23 10:42:58 -0300358#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
359 { mode, src_fourcc, dst_fourcc, max_w, max_h }
360
361/*
362 * Arrays of codecs supported by each given version of Coda:
363 * i.MX27 -> codadx6
364 * i.MX5x -> coda7
365 * i.MX6 -> coda960
366 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
367 */
368static struct coda_codec codadx6_codecs[] = {
369 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
370 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300371};
372
Philipp Zabelb96904e2013-05-23 10:42:58 -0300373static struct coda_codec coda7_codecs[] = {
374 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
375 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300376 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
377 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300378};
379
Philipp Zabel89548442014-07-11 06:36:17 -0300380static struct coda_codec coda9_codecs[] = {
381 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
382 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
383 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
384 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
385};
386
Philipp Zabelb96904e2013-05-23 10:42:58 -0300387static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300388{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300389 switch (fourcc) {
390 case V4L2_PIX_FMT_YUV420:
391 case V4L2_PIX_FMT_YVU420:
392 return true;
393 default:
394 return false;
395 }
396}
Javier Martin186b2502012-07-26 05:53:35 -0300397
Philipp Zabelb96904e2013-05-23 10:42:58 -0300398/*
399 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
400 * tables.
401 */
402static u32 coda_format_normalize_yuv(u32 fourcc)
403{
404 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
405}
406
407static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
408 int dst_fourcc)
409{
410 struct coda_codec *codecs = dev->devtype->codecs;
411 int num_codecs = dev->devtype->num_codecs;
412 int k;
413
414 src_fourcc = coda_format_normalize_yuv(src_fourcc);
415 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
416 if (src_fourcc == dst_fourcc)
417 return NULL;
418
419 for (k = 0; k < num_codecs; k++) {
420 if (codecs[k].src_fourcc == src_fourcc &&
421 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300422 break;
423 }
424
Philipp Zabelb96904e2013-05-23 10:42:58 -0300425 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300426 return NULL;
427
Philipp Zabelb96904e2013-05-23 10:42:58 -0300428 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300429}
430
Philipp Zabel57625592013-09-30 10:34:51 -0300431static void coda_get_max_dimensions(struct coda_dev *dev,
432 struct coda_codec *codec,
433 int *max_w, int *max_h)
434{
435 struct coda_codec *codecs = dev->devtype->codecs;
436 int num_codecs = dev->devtype->num_codecs;
437 unsigned int w, h;
438 int k;
439
440 if (codec) {
441 w = codec->max_w;
442 h = codec->max_h;
443 } else {
444 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
445 w = max(w, codecs[k].max_w);
446 h = max(h, codecs[k].max_h);
447 }
448 }
449
450 if (max_w)
451 *max_w = w;
452 if (max_h)
453 *max_h = h;
454}
455
Philipp Zabel927933f2013-09-30 10:34:48 -0300456static char *coda_product_name(int product)
457{
458 static char buf[9];
459
460 switch (product) {
461 case CODA_DX6:
462 return "CodaDx6";
463 case CODA_7541:
464 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300465 case CODA_960:
466 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300467 default:
468 snprintf(buf, sizeof(buf), "(0x%04x)", product);
469 return buf;
470 }
471}
472
Javier Martin186b2502012-07-26 05:53:35 -0300473/*
474 * V4L2 ioctl() operations.
475 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300476static int coda_querycap(struct file *file, void *priv,
477 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300478{
Philipp Zabel927933f2013-09-30 10:34:48 -0300479 struct coda_ctx *ctx = fh_to_ctx(priv);
480
Javier Martin186b2502012-07-26 05:53:35 -0300481 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300482 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
483 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300484 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300485 /*
486 * This is only a mem-to-mem video device. The capture and output
487 * device capability flags are left only for backward compatibility
488 * and are scheduled for removal.
489 */
490 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
491 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300492 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
493
494 return 0;
495}
496
497static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300498 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300499{
500 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300501 struct coda_codec *codecs = ctx->dev->devtype->codecs;
502 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300503 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300504 int num_codecs = ctx->dev->devtype->num_codecs;
505 int num_formats = ARRAY_SIZE(coda_formats);
506 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300507
508 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300509 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300510 if (coda_format_is_yuv(formats[i].fourcc) &&
511 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300512 if (num == f->index)
513 break;
514 ++num;
515 continue;
516 }
517 /* Compressed formats may be supported, check the codec list */
518 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300519 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300520 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300521 formats[i].fourcc == codecs[k].dst_fourcc &&
522 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300523 break;
524 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
525 formats[i].fourcc == codecs[k].src_fourcc)
526 break;
527 }
528 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300529 if (num == f->index)
530 break;
531 ++num;
532 }
533 }
534
535 if (i < num_formats) {
536 fmt = &formats[i];
537 strlcpy(f->description, fmt->name, sizeof(f->description));
538 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300539 if (!coda_format_is_yuv(fmt->fourcc))
540 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300541 return 0;
542 }
543
544 /* Format not found */
545 return -EINVAL;
546}
547
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300548static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
549 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300550{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300551 struct coda_ctx *ctx = fh_to_ctx(priv);
552 struct vb2_queue *src_vq;
553 struct coda_q_data *q_data_src;
554
555 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300556 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300557 if (vb2_is_streaming(src_vq)) {
558 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
559
560 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
561 q_data_src->fourcc);
562 }
563
564 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300565}
566
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300567static int coda_enum_fmt_vid_out(struct file *file, void *priv,
568 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300569{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300570 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300571}
572
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300573static int coda_g_fmt(struct file *file, void *priv,
574 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300575{
Javier Martin186b2502012-07-26 05:53:35 -0300576 struct coda_q_data *q_data;
577 struct coda_ctx *ctx = fh_to_ctx(priv);
578
Javier Martin186b2502012-07-26 05:53:35 -0300579 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300580 if (!q_data)
581 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300582
583 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300584 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300585 f->fmt.pix.width = q_data->width;
586 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300587 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300588 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
589 else /* encoded formats h.264/mpeg4 */
590 f->fmt.pix.bytesperline = 0;
591
592 f->fmt.pix.sizeimage = q_data->sizeimage;
593 f->fmt.pix.colorspace = ctx->colorspace;
594
595 return 0;
596}
597
Philipp Zabel57625592013-09-30 10:34:51 -0300598static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
599 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300600{
Philipp Zabel57625592013-09-30 10:34:51 -0300601 struct coda_dev *dev = ctx->dev;
602 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300603 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300604 enum v4l2_field field;
605
606 field = f->fmt.pix.field;
607 if (field == V4L2_FIELD_ANY)
608 field = V4L2_FIELD_NONE;
609 else if (V4L2_FIELD_NONE != field)
610 return -EINVAL;
611
612 /* V4L2 specification suggests the driver corrects the format struct
613 * if any of the dimensions is unsupported */
614 f->fmt.pix.field = field;
615
Philipp Zabel57625592013-09-30 10:34:51 -0300616 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
617 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
618 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
619 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300620
Philipp Zabel57625592013-09-30 10:34:51 -0300621 switch (f->fmt.pix.pixelformat) {
622 case V4L2_PIX_FMT_YUV420:
623 case V4L2_PIX_FMT_YVU420:
624 case V4L2_PIX_FMT_H264:
625 case V4L2_PIX_FMT_MPEG4:
626 case V4L2_PIX_FMT_JPEG:
627 break;
628 default:
629 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300630 if (!q_data)
631 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300632 f->fmt.pix.pixelformat = q_data->fourcc;
633 }
634
635 switch (f->fmt.pix.pixelformat) {
636 case V4L2_PIX_FMT_YUV420:
637 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300638 /* Frame stride must be multiple of 8 */
639 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
640 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300641 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300642 break;
643 case V4L2_PIX_FMT_H264:
644 case V4L2_PIX_FMT_MPEG4:
645 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300646 f->fmt.pix.bytesperline = 0;
647 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300648 break;
649 default:
650 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300651 }
652
653 return 0;
654}
655
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300656static int coda_try_fmt_vid_cap(struct file *file, void *priv,
657 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300658{
Javier Martin186b2502012-07-26 05:53:35 -0300659 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300660 struct coda_codec *codec;
661 struct vb2_queue *src_vq;
662 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300663
Philipp Zabel918c66f2013-06-27 06:59:01 -0300664 /*
665 * If the source format is already fixed, try to find a codec that
666 * converts to the given destination format
667 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300668 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300669 if (vb2_is_streaming(src_vq)) {
670 struct coda_q_data *q_data_src;
671
672 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
673 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
674 f->fmt.pix.pixelformat);
675 if (!codec)
676 return -EINVAL;
677 } else {
678 /* Otherwise determine codec by encoded format, if possible */
679 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
680 f->fmt.pix.pixelformat);
681 }
Javier Martin186b2502012-07-26 05:53:35 -0300682
683 f->fmt.pix.colorspace = ctx->colorspace;
684
Philipp Zabel57625592013-09-30 10:34:51 -0300685 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300686 if (ret < 0)
687 return ret;
688
689 /* The h.264 decoder only returns complete 16x16 macroblocks */
690 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
691 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
692 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
693 f->fmt.pix.bytesperline = f->fmt.pix.width;
694 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
695 f->fmt.pix.height * 3 / 2;
696 }
697
698 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300699}
700
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300701static int coda_try_fmt_vid_out(struct file *file, void *priv,
702 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300703{
704 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300705 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300706
Philipp Zabelb96904e2013-05-23 10:42:58 -0300707 /* Determine codec by encoded format, returns NULL if raw or invalid */
708 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
709 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300710
711 if (!f->fmt.pix.colorspace)
712 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
713
Philipp Zabel57625592013-09-30 10:34:51 -0300714 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300715}
716
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300717static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300718{
719 struct coda_q_data *q_data;
720 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300721
Philipp Zabel14604e32014-07-11 06:36:22 -0300722 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300723 if (!vq)
724 return -EINVAL;
725
726 q_data = get_q_data(ctx, f->type);
727 if (!q_data)
728 return -EINVAL;
729
730 if (vb2_is_busy(vq)) {
731 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
732 return -EBUSY;
733 }
734
Philipp Zabelb96904e2013-05-23 10:42:58 -0300735 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300736 q_data->width = f->fmt.pix.width;
737 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300738 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300739 q_data->rect.left = 0;
740 q_data->rect.top = 0;
741 q_data->rect.width = f->fmt.pix.width;
742 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300743
744 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
745 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300746 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300747
748 return 0;
749}
750
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300751static int coda_s_fmt_vid_cap(struct file *file, void *priv,
752 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300753{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300754 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300755 int ret;
756
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300757 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300758 if (ret)
759 return ret;
760
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300761 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300762}
763
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300764static int coda_s_fmt_vid_out(struct file *file, void *priv,
765 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300766{
767 struct coda_ctx *ctx = fh_to_ctx(priv);
768 int ret;
769
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300770 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300771 if (ret)
772 return ret;
773
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300774 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300775 if (ret)
776 ctx->colorspace = f->fmt.pix.colorspace;
777
778 return ret;
779}
780
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300781static int coda_qbuf(struct file *file, void *priv,
782 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300783{
784 struct coda_ctx *ctx = fh_to_ctx(priv);
785
Philipp Zabel14604e32014-07-11 06:36:22 -0300786 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300787}
788
Philipp Zabel918c66f2013-06-27 06:59:01 -0300789static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
790 struct v4l2_buffer *buf)
791{
792 struct vb2_queue *src_vq;
793
Philipp Zabel14604e32014-07-11 06:36:22 -0300794 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300795
796 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
797 (buf->sequence == (ctx->qsequence - 1)));
798}
799
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300800static int coda_dqbuf(struct file *file, void *priv,
801 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300802{
803 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300804 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300805
Philipp Zabel14604e32014-07-11 06:36:22 -0300806 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300807
808 /* If this is the last capture buffer, emit an end-of-stream event */
809 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
810 coda_buf_is_end_of_stream(ctx, buf)) {
811 const struct v4l2_event eos_event = {
812 .type = V4L2_EVENT_EOS
813 };
814
815 v4l2_event_queue_fh(&ctx->fh, &eos_event);
816 }
817
818 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300819}
820
Philipp Zabel52c41672014-07-11 06:36:19 -0300821static int coda_g_selection(struct file *file, void *fh,
822 struct v4l2_selection *s)
823{
824 struct coda_ctx *ctx = fh_to_ctx(fh);
825 struct coda_q_data *q_data;
826 struct v4l2_rect r, *rsel;
827
828 q_data = get_q_data(ctx, s->type);
829 if (!q_data)
830 return -EINVAL;
831
832 r.left = 0;
833 r.top = 0;
834 r.width = q_data->width;
835 r.height = q_data->height;
836 rsel = &q_data->rect;
837
838 switch (s->target) {
839 case V4L2_SEL_TGT_CROP_DEFAULT:
840 case V4L2_SEL_TGT_CROP_BOUNDS:
841 rsel = &r;
842 /* fallthrough */
843 case V4L2_SEL_TGT_CROP:
844 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
845 return -EINVAL;
846 break;
847 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
848 case V4L2_SEL_TGT_COMPOSE_PADDED:
849 rsel = &r;
850 /* fallthrough */
851 case V4L2_SEL_TGT_COMPOSE:
852 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
853 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
854 return -EINVAL;
855 break;
856 default:
857 return -EINVAL;
858 }
859
860 s->r = *rsel;
861
862 return 0;
863}
864
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300865static int coda_try_decoder_cmd(struct file *file, void *fh,
866 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300867{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300868 if (dc->cmd != V4L2_DEC_CMD_STOP)
869 return -EINVAL;
870
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300871 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300872 return -EINVAL;
873
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300874 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300875 return -EINVAL;
876
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300877 return 0;
878}
879
880static int coda_decoder_cmd(struct file *file, void *fh,
881 struct v4l2_decoder_cmd *dc)
882{
883 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300884 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300885 int ret;
886
887 ret = coda_try_decoder_cmd(file, fh, dc);
888 if (ret < 0)
889 return ret;
890
891 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300892 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300893 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300894
895 /* Set the strem-end flag on this context */
896 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
897
Philipp Zabel89548442014-07-11 06:36:17 -0300898 if ((dev->devtype->product == CODA_960) &&
899 coda_isbusy(dev) &&
900 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
901 /* If this context is currently running, update the hardware flag */
902 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
903 }
904
Philipp Zabel918c66f2013-06-27 06:59:01 -0300905 return 0;
906}
907
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300908static int coda_subscribe_event(struct v4l2_fh *fh,
909 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300910{
911 switch (sub->type) {
912 case V4L2_EVENT_EOS:
913 return v4l2_event_subscribe(fh, sub, 0, NULL);
914 default:
915 return v4l2_ctrl_subscribe_event(fh, sub);
916 }
Javier Martin186b2502012-07-26 05:53:35 -0300917}
918
919static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300920 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300921
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300922 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
923 .vidioc_g_fmt_vid_cap = coda_g_fmt,
924 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
925 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300926
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300927 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
928 .vidioc_g_fmt_vid_out = coda_g_fmt,
929 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
930 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300931
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300932 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
933 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300934
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300935 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300936 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300937 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300938 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300939
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300940 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
941 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300942
Philipp Zabel52c41672014-07-11 06:36:19 -0300943 .vidioc_g_selection = coda_g_selection,
944
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300945 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300946 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300947
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300948 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300949 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300950};
951
Philipp Zabel918c66f2013-06-27 06:59:01 -0300952static int coda_start_decoding(struct coda_ctx *ctx);
953
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300954static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
955{
956 return kfifo_len(&ctx->bitstream_fifo);
957}
958
959static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
960{
961 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
962 struct coda_dev *dev = ctx->dev;
963 u32 rd_ptr;
964
965 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
966 kfifo->out = (kfifo->in & ~kfifo->mask) |
967 (rd_ptr - ctx->bitstream.paddr);
968 if (kfifo->out > kfifo->in)
969 kfifo->out -= kfifo->mask + 1;
970}
971
972static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
973{
974 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
975 struct coda_dev *dev = ctx->dev;
976 u32 rd_ptr, wr_ptr;
977
978 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
979 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
980 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
981 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
982}
983
984static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
985{
986 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
987 struct coda_dev *dev = ctx->dev;
988 u32 wr_ptr;
989
990 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
991 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
992}
993
994static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
995{
996 u32 src_size = vb2_get_plane_payload(src_buf, 0);
997 u32 n;
998
999 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1000 if (n < src_size)
1001 return -ENOSPC;
1002
1003 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1004 ctx->bitstream.size, DMA_TO_DEVICE);
1005
1006 ctx->qsequence++;
1007
1008 return 0;
1009}
1010
1011static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1012 struct vb2_buffer *src_buf)
1013{
1014 int ret;
1015
1016 if (coda_get_bitstream_payload(ctx) +
1017 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1018 return false;
1019
1020 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1021 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1022 return true;
1023 }
1024
1025 ret = coda_bitstream_queue(ctx, src_buf);
1026 if (ret < 0) {
1027 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1028 return false;
1029 }
1030 /* Sync read pointer to device */
1031 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1032 coda_kfifo_sync_to_device_write(ctx);
1033
Philipp Zabel918c66f2013-06-27 06:59:01 -03001034 ctx->prescan_failed = false;
1035
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001036 return true;
1037}
1038
1039static void coda_fill_bitstream(struct coda_ctx *ctx)
1040{
1041 struct vb2_buffer *src_buf;
1042
Philipp Zabel14604e32014-07-11 06:36:22 -03001043 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1044 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001045
1046 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel14604e32014-07-11 06:36:22 -03001047 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001048 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1049 } else {
1050 break;
1051 }
1052 }
1053}
1054
Philipp Zabel89548442014-07-11 06:36:17 -03001055static void coda_set_gdi_regs(struct coda_ctx *ctx)
1056{
1057 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1058 struct coda_dev *dev = ctx->dev;
1059 int i;
1060
1061 for (i = 0; i < 16; i++)
1062 coda_write(dev, tiled_map->xy2ca_map[i],
1063 CODA9_GDI_XY2_CAS_0 + 4 * i);
1064 for (i = 0; i < 4; i++)
1065 coda_write(dev, tiled_map->xy2ba_map[i],
1066 CODA9_GDI_XY2_BA_0 + 4 * i);
1067 for (i = 0; i < 16; i++)
1068 coda_write(dev, tiled_map->xy2ra_map[i],
1069 CODA9_GDI_XY2_RAS_0 + 4 * i);
1070 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1071 for (i = 0; i < 32; i++)
1072 coda_write(dev, tiled_map->rbc2axi_map[i],
1073 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1074}
1075
Javier Martin186b2502012-07-26 05:53:35 -03001076/*
1077 * Mem-to-mem operations.
1078 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001079static int coda_prepare_decode(struct coda_ctx *ctx)
1080{
1081 struct vb2_buffer *dst_buf;
1082 struct coda_dev *dev = ctx->dev;
1083 struct coda_q_data *q_data_dst;
1084 u32 stridey, height;
1085 u32 picture_y, picture_cb, picture_cr;
1086
Philipp Zabel14604e32014-07-11 06:36:22 -03001087 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001088 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1089
1090 if (ctx->params.rot_mode & CODA_ROT_90) {
1091 stridey = q_data_dst->height;
1092 height = q_data_dst->width;
1093 } else {
1094 stridey = q_data_dst->width;
1095 height = q_data_dst->height;
1096 }
1097
1098 /* Try to copy source buffer contents into the bitstream ringbuffer */
1099 mutex_lock(&ctx->bitstream_mutex);
1100 coda_fill_bitstream(ctx);
1101 mutex_unlock(&ctx->bitstream_mutex);
1102
1103 if (coda_get_bitstream_payload(ctx) < 512 &&
1104 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1105 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1106 "bitstream payload: %d, skipping\n",
1107 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001108 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001109 return -EAGAIN;
1110 }
1111
1112 /* Run coda_start_decoding (again) if not yet initialized */
1113 if (!ctx->initialized) {
1114 int ret = coda_start_decoding(ctx);
1115 if (ret < 0) {
1116 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001117 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001118 return -EAGAIN;
1119 } else {
1120 ctx->initialized = 1;
1121 }
1122 }
1123
Philipp Zabel89548442014-07-11 06:36:17 -03001124 if (dev->devtype->product == CODA_960)
1125 coda_set_gdi_regs(ctx);
1126
Philipp Zabel918c66f2013-06-27 06:59:01 -03001127 /* Set rotator output */
1128 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1129 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1130 /* Switch Cr and Cb for YVU420 format */
1131 picture_cr = picture_y + stridey * height;
1132 picture_cb = picture_cr + stridey / 2 * height / 2;
1133 } else {
1134 picture_cb = picture_y + stridey * height;
1135 picture_cr = picture_cb + stridey / 2 * height / 2;
1136 }
Philipp Zabel89548442014-07-11 06:36:17 -03001137
1138 if (dev->devtype->product == CODA_960) {
1139 /*
1140 * The CODA960 seems to have an internal list of buffers with
1141 * 64 entries that includes the registered frame buffers as
1142 * well as the rotator buffer output.
1143 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1144 */
1145 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1146 CODA9_CMD_DEC_PIC_ROT_INDEX);
1147 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1148 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1149 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1150 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1151 } else {
1152 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1153 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1154 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1155 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1156 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001157 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1158 CODA_CMD_DEC_PIC_ROT_MODE);
1159
1160 switch (dev->devtype->product) {
1161 case CODA_DX6:
1162 /* TBD */
1163 case CODA_7541:
1164 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1165 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001166 case CODA_960:
1167 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1168 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001169 }
1170
1171 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1172
1173 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1174 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1175
1176 return 0;
1177}
1178
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001179static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001180{
Javier Martin186b2502012-07-26 05:53:35 -03001181 struct coda_q_data *q_data_src, *q_data_dst;
1182 struct vb2_buffer *src_buf, *dst_buf;
1183 struct coda_dev *dev = ctx->dev;
1184 int force_ipicture;
1185 int quant_param = 0;
1186 u32 picture_y, picture_cb, picture_cr;
1187 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1188 u32 dst_fourcc;
1189
Philipp Zabel14604e32014-07-11 06:36:22 -03001190 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1191 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001192 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1193 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001194 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001195
Philipp Zabel918c66f2013-06-27 06:59:01 -03001196 src_buf->v4l2_buf.sequence = ctx->osequence;
1197 dst_buf->v4l2_buf.sequence = ctx->osequence;
1198 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001199
1200 /*
1201 * Workaround coda firmware BUG that only marks the first
1202 * frame as IDR. This is a problem for some decoders that can't
1203 * recover when a frame is lost.
1204 */
1205 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1206 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1207 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1208 } else {
1209 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1210 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1211 }
1212
Philipp Zabel89548442014-07-11 06:36:17 -03001213 if (dev->devtype->product == CODA_960)
1214 coda_set_gdi_regs(ctx);
1215
Javier Martin186b2502012-07-26 05:53:35 -03001216 /*
1217 * Copy headers at the beginning of the first frame for H.264 only.
1218 * In MPEG4 they are already copied by the coda.
1219 */
1220 if (src_buf->v4l2_buf.sequence == 0) {
1221 pic_stream_buffer_addr =
1222 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1223 ctx->vpu_header_size[0] +
1224 ctx->vpu_header_size[1] +
1225 ctx->vpu_header_size[2];
1226 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1227 ctx->vpu_header_size[0] -
1228 ctx->vpu_header_size[1] -
1229 ctx->vpu_header_size[2];
1230 memcpy(vb2_plane_vaddr(dst_buf, 0),
1231 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1232 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1233 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1234 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1235 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1236 ctx->vpu_header_size[2]);
1237 } else {
1238 pic_stream_buffer_addr =
1239 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1240 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1241 }
1242
1243 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1244 force_ipicture = 1;
1245 switch (dst_fourcc) {
1246 case V4L2_PIX_FMT_H264:
1247 quant_param = ctx->params.h264_intra_qp;
1248 break;
1249 case V4L2_PIX_FMT_MPEG4:
1250 quant_param = ctx->params.mpeg4_intra_qp;
1251 break;
1252 default:
1253 v4l2_warn(&ctx->dev->v4l2_dev,
1254 "cannot set intra qp, fmt not supported\n");
1255 break;
1256 }
1257 } else {
1258 force_ipicture = 0;
1259 switch (dst_fourcc) {
1260 case V4L2_PIX_FMT_H264:
1261 quant_param = ctx->params.h264_inter_qp;
1262 break;
1263 case V4L2_PIX_FMT_MPEG4:
1264 quant_param = ctx->params.mpeg4_inter_qp;
1265 break;
1266 default:
1267 v4l2_warn(&ctx->dev->v4l2_dev,
1268 "cannot set inter qp, fmt not supported\n");
1269 break;
1270 }
1271 }
1272
1273 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001274 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001275 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1276
1277
1278 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001279 switch (q_data_src->fourcc) {
1280 case V4L2_PIX_FMT_YVU420:
1281 /* Switch Cb and Cr for YVU420 format */
1282 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1283 picture_cb = picture_cr + q_data_src->width / 2 *
1284 q_data_src->height / 2;
1285 break;
1286 case V4L2_PIX_FMT_YUV420:
1287 default:
1288 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1289 picture_cr = picture_cb + q_data_src->width / 2 *
1290 q_data_src->height / 2;
1291 break;
1292 }
Javier Martin186b2502012-07-26 05:53:35 -03001293
Philipp Zabel89548442014-07-11 06:36:17 -03001294 if (dev->devtype->product == CODA_960) {
1295 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1296 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1297 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1298
1299 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1300 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1301 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1302 } else {
1303 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1304 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1305 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1306 }
Javier Martin186b2502012-07-26 05:53:35 -03001307 coda_write(dev, force_ipicture << 1 & 0x2,
1308 CODA_CMD_ENC_PIC_OPTION);
1309
1310 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1311 coda_write(dev, pic_stream_buffer_size / 1024,
1312 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001313
1314 if (!ctx->streamon_out) {
1315 /* After streamoff on the output side, set the stream end flag */
1316 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1317 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1318 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001319}
1320
1321static void coda_device_run(void *m2m_priv)
1322{
1323 struct coda_ctx *ctx = m2m_priv;
1324 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001325
1326 queue_work(dev->workqueue, &ctx->pic_run_work);
1327}
1328
1329static void coda_free_framebuffers(struct coda_ctx *ctx);
1330static void coda_free_context_buffers(struct coda_ctx *ctx);
1331
1332static void coda_seq_end_work(struct work_struct *work)
1333{
1334 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1335 struct coda_dev *dev = ctx->dev;
1336
1337 mutex_lock(&ctx->buffer_mutex);
1338 mutex_lock(&dev->coda_mutex);
1339
1340 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1341 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1342 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1343 v4l2_err(&dev->v4l2_dev,
1344 "CODA_COMMAND_SEQ_END failed\n");
1345 }
1346
1347 kfifo_init(&ctx->bitstream_fifo,
1348 ctx->bitstream.vaddr, ctx->bitstream.size);
1349
1350 coda_free_framebuffers(ctx);
1351 coda_free_context_buffers(ctx);
1352
1353 mutex_unlock(&dev->coda_mutex);
1354 mutex_unlock(&ctx->buffer_mutex);
1355}
1356
1357static void coda_finish_decode(struct coda_ctx *ctx);
1358static void coda_finish_encode(struct coda_ctx *ctx);
1359
1360static void coda_pic_run_work(struct work_struct *work)
1361{
1362 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1363 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001364 int ret;
1365
1366 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001367 mutex_lock(&dev->coda_mutex);
1368
Philipp Zabel918c66f2013-06-27 06:59:01 -03001369 if (ctx->inst_type == CODA_INST_DECODER) {
1370 ret = coda_prepare_decode(ctx);
1371 if (ret < 0) {
1372 mutex_unlock(&dev->coda_mutex);
1373 mutex_unlock(&ctx->buffer_mutex);
1374 /* job_finish scheduled by prepare_decode */
1375 return;
1376 }
1377 } else {
1378 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001379 }
1380
Philipp Zabelc2d22512013-06-21 03:55:28 -03001381 if (dev->devtype->product != CODA_DX6)
1382 coda_write(dev, ctx->iram_info.axi_sram_use,
1383 CODA7_REG_BIT_AXI_SRAM_USE);
1384
Philipp Zabel918c66f2013-06-27 06:59:01 -03001385 if (ctx->inst_type == CODA_INST_DECODER)
1386 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001387 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001388
1389 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1390 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1391 } else if (!ctx->aborting) {
1392 if (ctx->inst_type == CODA_INST_DECODER)
1393 coda_finish_decode(ctx);
1394 else
1395 coda_finish_encode(ctx);
1396 }
1397
1398 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1399 queue_work(dev->workqueue, &ctx->seq_end_work);
1400
1401 mutex_unlock(&dev->coda_mutex);
1402 mutex_unlock(&ctx->buffer_mutex);
1403
Philipp Zabel14604e32014-07-11 06:36:22 -03001404 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001405}
1406
1407static int coda_job_ready(void *m2m_priv)
1408{
1409 struct coda_ctx *ctx = m2m_priv;
1410
1411 /*
1412 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001413 * and 1 frame are needed. In the decoder case,
1414 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001415 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001416 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001417 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001418 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1419 "not ready: not enough video buffers.\n");
1420 return 0;
1421 }
1422
Philipp Zabel14604e32014-07-11 06:36:22 -03001423 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001424 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1425 "not ready: not enough video capture buffers.\n");
1426 return 0;
1427 }
1428
Philipp Zabel918c66f2013-06-27 06:59:01 -03001429 if (ctx->prescan_failed ||
1430 ((ctx->inst_type == CODA_INST_DECODER) &&
1431 (coda_get_bitstream_payload(ctx) < 512) &&
1432 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1433 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1434 "%d: not ready: not enough bitstream data.\n",
1435 ctx->idx);
1436 return 0;
1437 }
1438
Philipp Zabel3e7482682013-05-23 10:43:01 -03001439 if (ctx->aborting) {
1440 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1441 "not ready: aborting\n");
1442 return 0;
1443 }
1444
Javier Martin186b2502012-07-26 05:53:35 -03001445 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1446 "job ready\n");
1447 return 1;
1448}
1449
1450static void coda_job_abort(void *priv)
1451{
1452 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001453
1454 ctx->aborting = 1;
1455
1456 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1457 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001458}
1459
1460static void coda_lock(void *m2m_priv)
1461{
1462 struct coda_ctx *ctx = m2m_priv;
1463 struct coda_dev *pcdev = ctx->dev;
1464 mutex_lock(&pcdev->dev_mutex);
1465}
1466
1467static void coda_unlock(void *m2m_priv)
1468{
1469 struct coda_ctx *ctx = m2m_priv;
1470 struct coda_dev *pcdev = ctx->dev;
1471 mutex_unlock(&pcdev->dev_mutex);
1472}
1473
1474static struct v4l2_m2m_ops coda_m2m_ops = {
1475 .device_run = coda_device_run,
1476 .job_ready = coda_job_ready,
1477 .job_abort = coda_job_abort,
1478 .lock = coda_lock,
1479 .unlock = coda_unlock,
1480};
1481
Philipp Zabel89548442014-07-11 06:36:17 -03001482static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1483{
1484 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1485 int luma_map, chro_map, i;
1486
1487 memset(tiled_map, 0, sizeof(*tiled_map));
1488
1489 luma_map = 64;
1490 chro_map = 64;
1491 tiled_map->map_type = tiled_map_type;
1492 for (i = 0; i < 16; i++)
1493 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1494 for (i = 0; i < 4; i++)
1495 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1496 for (i = 0; i < 16; i++)
1497 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1498
1499 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1500 tiled_map->xy2rbc_config = 0;
1501 } else {
1502 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1503 tiled_map_type);
1504 return;
1505 }
1506}
1507
Javier Martin186b2502012-07-26 05:53:35 -03001508static void set_default_params(struct coda_ctx *ctx)
1509{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001510 int max_w;
1511 int max_h;
1512
1513 ctx->codec = &ctx->dev->devtype->codecs[0];
1514 max_w = ctx->codec->max_w;
1515 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001516
1517 ctx->params.codec_mode = CODA_MODE_INVALID;
1518 ctx->colorspace = V4L2_COLORSPACE_REC709;
1519 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001520 ctx->aborting = 0;
1521
1522 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001523 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1524 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1525 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1526 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1527 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1528 ctx->q_data[V4L2_M2M_DST].width = max_w;
1529 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001530 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001531 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1532 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1533 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1534 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001535
1536 if (ctx->dev->devtype->product == CODA_960)
1537 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001538}
1539
1540/*
1541 * Queue operations
1542 */
1543static int coda_queue_setup(struct vb2_queue *vq,
1544 const struct v4l2_format *fmt,
1545 unsigned int *nbuffers, unsigned int *nplanes,
1546 unsigned int sizes[], void *alloc_ctxs[])
1547{
1548 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001549 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001550 unsigned int size;
1551
Philipp Zabele34db062012-08-29 08:22:00 -03001552 q_data = get_q_data(ctx, vq->type);
1553 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001554
1555 *nplanes = 1;
1556 sizes[0] = size;
1557
1558 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1559
1560 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1561 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1562
1563 return 0;
1564}
1565
1566static int coda_buf_prepare(struct vb2_buffer *vb)
1567{
1568 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1569 struct coda_q_data *q_data;
1570
1571 q_data = get_q_data(ctx, vb->vb2_queue->type);
1572
1573 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1574 v4l2_warn(&ctx->dev->v4l2_dev,
1575 "%s data will not fit into plane (%lu < %lu)\n",
1576 __func__, vb2_plane_size(vb, 0),
1577 (long)q_data->sizeimage);
1578 return -EINVAL;
1579 }
1580
Javier Martin186b2502012-07-26 05:53:35 -03001581 return 0;
1582}
1583
1584static void coda_buf_queue(struct vb2_buffer *vb)
1585{
1586 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001587 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001588 struct coda_q_data *q_data;
1589
1590 q_data = get_q_data(ctx, vb->vb2_queue->type);
1591
1592 /*
1593 * In the decoder case, immediately try to copy the buffer into the
1594 * bitstream ringbuffer and mark it as ready to be dequeued.
1595 */
1596 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1597 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1598 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001599 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001600 * the stream end
1601 */
Philipp Zabel89548442014-07-11 06:36:17 -03001602 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001603 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001604 if ((dev->devtype->product == CODA_960) &&
1605 coda_isbusy(dev) &&
1606 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1607 /* if this decoder instance is running, set the stream end flag */
1608 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1609 }
1610 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001611 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001612 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001613 coda_fill_bitstream(ctx);
1614 mutex_unlock(&ctx->bitstream_mutex);
1615 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001616 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001617 }
Javier Martin186b2502012-07-26 05:53:35 -03001618}
1619
Philipp Zabel86eda902013-05-23 10:42:57 -03001620static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1621{
1622 struct coda_dev *dev = ctx->dev;
1623 u32 *p = ctx->parabuf.vaddr;
1624
1625 if (dev->devtype->product == CODA_DX6)
1626 p[index] = value;
1627 else
1628 p[index ^ 1] = value;
1629}
1630
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001631static int coda_alloc_aux_buf(struct coda_dev *dev,
1632 struct coda_aux_buf *buf, size_t size)
1633{
1634 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1635 GFP_KERNEL);
1636 if (!buf->vaddr)
1637 return -ENOMEM;
1638
1639 buf->size = size;
1640
1641 return 0;
1642}
1643
1644static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1645 struct coda_aux_buf *buf, size_t size)
1646{
1647 return coda_alloc_aux_buf(ctx->dev, buf, size);
1648}
1649
1650static void coda_free_aux_buf(struct coda_dev *dev,
1651 struct coda_aux_buf *buf)
1652{
1653 if (buf->vaddr) {
1654 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1655 buf->vaddr, buf->paddr);
1656 buf->vaddr = NULL;
1657 buf->size = 0;
1658 }
1659}
1660
1661static void coda_free_framebuffers(struct coda_ctx *ctx)
1662{
1663 int i;
1664
1665 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1666 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1667}
1668
Philipp Zabelec25f682012-07-20 08:54:29 -03001669static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1670{
1671 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001672 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001673 dma_addr_t paddr;
1674 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001675 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001676 int i;
1677
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001678 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1679 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001680 ysize = round_up(q_data->width, 8) * height;
1681
Philipp Zabelec25f682012-07-20 08:54:29 -03001682 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001683 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001684 size_t size;
1685
Philipp Zabelaed14b02014-07-11 06:36:15 -03001686 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001687 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1688 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001689 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001690 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1691 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001692 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001693 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001694 }
1695 }
1696
1697 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001698 for (i = 0; i < ctx->num_internal_frames; i++) {
1699 paddr = ctx->internal_frames[i].paddr;
1700 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1701 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1702 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001703
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001704 /* mvcol buffer for h.264 */
1705 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1706 dev->devtype->product != CODA_DX6)
1707 coda_parabuf_write(ctx, 96 + i,
1708 ctx->internal_frames[i].paddr +
1709 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001710 }
1711
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001712 /* mvcol buffer for mpeg4 */
1713 if ((dev->devtype->product != CODA_DX6) &&
1714 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1715 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1716 ysize + ysize/4 + ysize/4);
1717
Philipp Zabelec25f682012-07-20 08:54:29 -03001718 return 0;
1719}
1720
Javier Martin3f3f5c72012-10-29 05:20:29 -03001721static int coda_h264_padding(int size, char *p)
1722{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001723 int nal_size;
1724 int diff;
1725
Javier Martin832fbb52012-10-29 08:34:59 -03001726 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001727 if (diff == 0)
1728 return 0;
1729
Javier Martin832fbb52012-10-29 08:34:59 -03001730 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001731 memcpy(p, coda_filler_nal, nal_size);
1732
1733 /* Add rbsp stop bit and trailing at the end */
1734 *(p + nal_size - 1) = 0x80;
1735
1736 return nal_size;
1737}
1738
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001739static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1740{
1741 phys_addr_t ret;
1742
1743 size = round_up(size, 1024);
1744 if (size > iram->remaining)
1745 return 0;
1746 iram->remaining -= size;
1747
1748 ret = iram->next_paddr;
1749 iram->next_paddr += size;
1750
1751 return ret;
1752}
1753
Philipp Zabelc2d22512013-06-21 03:55:28 -03001754static void coda_setup_iram(struct coda_ctx *ctx)
1755{
1756 struct coda_iram_info *iram_info = &ctx->iram_info;
1757 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001758 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001759 int dbk_bits;
1760 int bit_bits;
1761 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001762
1763 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001764 iram_info->next_paddr = dev->iram.paddr;
1765 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001766
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001767 switch (dev->devtype->product) {
1768 case CODA_7541:
1769 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1770 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1771 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1772 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001773 case CODA_960:
1774 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1775 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1776 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1777 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001778 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001779 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001780 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001781
1782 if (ctx->inst_type == CODA_INST_ENCODER) {
1783 struct coda_q_data *q_data_src;
1784
1785 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1786 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1787
1788 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001789 if (dev->devtype->product == CODA_7541) {
1790 iram_info->search_ram_size = round_up(mb_width * 16 *
1791 36 + 2048, 1024);
1792 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1793 iram_info->search_ram_size);
1794 if (!iram_info->search_ram_paddr) {
1795 pr_err("IRAM is smaller than the search ram size\n");
1796 goto out;
1797 }
1798 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1799 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001800 }
1801
1802 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001803 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1804 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1805 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001806 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001807 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001808
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001809 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1810 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001811 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001812 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001813
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001814 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1815 if (!iram_info->buf_ip_ac_dc_use)
1816 goto out;
1817 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001818
Philipp Zabel8358e762013-06-21 03:55:32 -03001819 /* OVL and BTP disabled for encoder */
1820 } else if (ctx->inst_type == CODA_INST_DECODER) {
1821 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001822
1823 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1824 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001825
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001826 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1827 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1828 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001829 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001830 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001831
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001832 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1833 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001834 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001835 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001836
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001837 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1838 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001839 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001840 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001841
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001842 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001843 }
1844
1845out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001846 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1847 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1848 "IRAM smaller than needed\n");
1849
1850 if (dev->devtype->product == CODA_7541) {
1851 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001852 if (ctx->inst_type == CODA_INST_DECODER) {
1853 /* fw 1.4.50 */
1854 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1855 CODA7_USE_IP_ENABLE);
1856 } else {
1857 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001858 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1859 CODA7_USE_HOST_DBK_ENABLE |
1860 CODA7_USE_IP_ENABLE |
1861 CODA7_USE_DBK_ENABLE);
1862 }
1863 }
1864}
1865
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001866static void coda_free_context_buffers(struct coda_ctx *ctx)
1867{
1868 struct coda_dev *dev = ctx->dev;
1869
Philipp Zabel918c66f2013-06-27 06:59:01 -03001870 coda_free_aux_buf(dev, &ctx->slicebuf);
1871 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001872 if (dev->devtype->product != CODA_DX6)
1873 coda_free_aux_buf(dev, &ctx->workbuf);
1874}
1875
1876static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1877 struct coda_q_data *q_data)
1878{
1879 struct coda_dev *dev = ctx->dev;
1880 size_t size;
1881 int ret;
1882
1883 switch (dev->devtype->product) {
1884 case CODA_7541:
1885 size = CODA7_WORK_BUF_SIZE;
1886 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001887 case CODA_960:
1888 size = CODA9_WORK_BUF_SIZE;
1889 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1890 size += CODA9_PS_SAVE_SIZE;
1891 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001892 default:
1893 return 0;
1894 }
1895
Philipp Zabel918c66f2013-06-27 06:59:01 -03001896 if (ctx->psbuf.vaddr) {
1897 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1898 return -EBUSY;
1899 }
1900 if (ctx->slicebuf.vaddr) {
1901 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1902 return -EBUSY;
1903 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001904 if (ctx->workbuf.vaddr) {
1905 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1906 ret = -EBUSY;
1907 return -ENOMEM;
1908 }
1909
Philipp Zabel918c66f2013-06-27 06:59:01 -03001910 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1911 /* worst case slice size */
1912 size = (DIV_ROUND_UP(q_data->width, 16) *
1913 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1914 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1915 if (ret < 0) {
1916 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1917 ctx->slicebuf.size);
1918 return ret;
1919 }
1920 }
1921
1922 if (dev->devtype->product == CODA_7541) {
1923 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1924 if (ret < 0) {
1925 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1926 goto err;
1927 }
1928 }
1929
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001930 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1931 if (ret < 0) {
1932 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1933 ctx->workbuf.size);
1934 goto err;
1935 }
1936
1937 return 0;
1938
1939err:
1940 coda_free_context_buffers(ctx);
1941 return ret;
1942}
1943
Philipp Zabel918c66f2013-06-27 06:59:01 -03001944static int coda_start_decoding(struct coda_ctx *ctx)
1945{
1946 struct coda_q_data *q_data_src, *q_data_dst;
1947 u32 bitstream_buf, bitstream_size;
1948 struct coda_dev *dev = ctx->dev;
1949 int width, height;
1950 u32 src_fourcc;
1951 u32 val;
1952 int ret;
1953
1954 /* Start decoding */
1955 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1956 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1957 bitstream_buf = ctx->bitstream.paddr;
1958 bitstream_size = ctx->bitstream.size;
1959 src_fourcc = q_data_src->fourcc;
1960
1961 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1962
1963 /* Update coda bitstream read and write pointers from kfifo */
1964 coda_kfifo_sync_to_device_full(ctx);
1965
1966 ctx->display_idx = -1;
1967 ctx->frm_dis_flg = 0;
1968 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1969
1970 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1971 CODA_REG_BIT_BIT_STREAM_PARAM);
1972
1973 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1974 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1975 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001976 if ((dev->devtype->product == CODA_7541) ||
1977 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03001978 val |= CODA_REORDER_ENABLE;
1979 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1980
1981 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03001982 if (dev->devtype->product == CODA_960 &&
1983 src_fourcc == V4L2_PIX_FMT_MPEG4)
1984 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1985 else
1986 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001987 if (src_fourcc == V4L2_PIX_FMT_H264) {
1988 if (dev->devtype->product == CODA_7541) {
1989 coda_write(dev, ctx->psbuf.paddr,
1990 CODA_CMD_DEC_SEQ_PS_BB_START);
1991 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1992 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1993 }
Philipp Zabel89548442014-07-11 06:36:17 -03001994 if (dev->devtype->product == CODA_960) {
1995 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1996 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1997 }
1998 }
1999 if (dev->devtype->product != CODA_960) {
2000 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002001 }
2002
2003 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2004 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2005 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2006 return -ETIMEDOUT;
2007 }
2008
2009 /* Update kfifo out pointer from coda bitstream read pointer */
2010 coda_kfifo_sync_from_device(ctx);
2011
2012 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2013
2014 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2015 v4l2_err(&dev->v4l2_dev,
2016 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2017 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2018 return -EAGAIN;
2019 }
2020
2021 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2022 if (dev->devtype->product == CODA_DX6) {
2023 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2024 height = val & CODADX6_PICHEIGHT_MASK;
2025 } else {
2026 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2027 height = val & CODA7_PICHEIGHT_MASK;
2028 }
2029
2030 if (width > q_data_dst->width || height > q_data_dst->height) {
2031 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2032 width, height, q_data_dst->width, q_data_dst->height);
2033 return -EINVAL;
2034 }
2035
2036 width = round_up(width, 16);
2037 height = round_up(height, 16);
2038
2039 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2040 __func__, ctx->idx, width, height);
2041
Philipp Zabela500a932014-07-11 06:36:13 -03002042 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002043 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2044 v4l2_err(&dev->v4l2_dev,
2045 "not enough framebuffers to decode (%d < %d)\n",
2046 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2047 return -EINVAL;
2048 }
2049
Philipp Zabel52c41672014-07-11 06:36:19 -03002050 if (src_fourcc == V4L2_PIX_FMT_H264) {
2051 u32 left_right;
2052 u32 top_bottom;
2053
2054 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2055 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2056
2057 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2058 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2059 q_data_dst->rect.width = width - q_data_dst->rect.left -
2060 (left_right & 0x3ff);
2061 q_data_dst->rect.height = height - q_data_dst->rect.top -
2062 (top_bottom & 0x3ff);
2063 }
2064
Philipp Zabel918c66f2013-06-27 06:59:01 -03002065 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2066 if (ret < 0)
2067 return ret;
2068
2069 /* Tell the decoder how many frame buffers we allocated. */
2070 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2071 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2072
2073 if (dev->devtype->product != CODA_DX6) {
2074 /* Set secondary AXI IRAM */
2075 coda_setup_iram(ctx);
2076
2077 coda_write(dev, ctx->iram_info.buf_bit_use,
2078 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2079 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2080 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2081 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2082 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2083 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2084 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2085 coda_write(dev, ctx->iram_info.buf_ovl_use,
2086 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002087 if (dev->devtype->product == CODA_960)
2088 coda_write(dev, ctx->iram_info.buf_btp_use,
2089 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2090 }
2091
2092 if (dev->devtype->product == CODA_960) {
2093 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2094
2095 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2096 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2097 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2098 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2099 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2100 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002101 }
2102
2103 if (src_fourcc == V4L2_PIX_FMT_H264) {
2104 coda_write(dev, ctx->slicebuf.paddr,
2105 CODA_CMD_SET_FRAME_SLICE_BB_START);
2106 coda_write(dev, ctx->slicebuf.size / 1024,
2107 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2108 }
2109
2110 if (dev->devtype->product == CODA_7541) {
2111 int max_mb_x = 1920 / 16;
2112 int max_mb_y = 1088 / 16;
2113 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002114
Philipp Zabel918c66f2013-06-27 06:59:01 -03002115 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2116 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002117 } else if (dev->devtype->product == CODA_960) {
2118 int max_mb_x = 1920 / 16;
2119 int max_mb_y = 1088 / 16;
2120 int max_mb_num = max_mb_x * max_mb_y;
2121
2122 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2123 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002124 }
2125
2126 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2127 v4l2_err(&ctx->dev->v4l2_dev,
2128 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2129 return -ETIMEDOUT;
2130 }
2131
2132 return 0;
2133}
2134
Philipp Zabeld35167a2013-05-23 10:42:59 -03002135static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2136 int header_code, u8 *header, int *size)
2137{
2138 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002139 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002140 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002141 int i;
2142
2143 if (dev->devtype->product == CODA_960)
2144 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002145
2146 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2147 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002148 bufsize = vb2_plane_size(buf, 0);
2149 if (dev->devtype->product == CODA_960)
2150 bufsize /= 1024;
2151 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002152 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2153 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2154 if (ret < 0) {
2155 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2156 return ret;
2157 }
Philipp Zabel89548442014-07-11 06:36:17 -03002158
2159 if (dev->devtype->product == CODA_960) {
2160 for (i = 63; i > 0; i--)
2161 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2162 break;
2163 *size = i + 1;
2164 } else {
2165 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2166 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2167 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002168 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2169
2170 return 0;
2171}
2172
Philipp Zabel89548442014-07-11 06:36:17 -03002173static int coda_start_encoding(struct coda_ctx *ctx);
2174
Javier Martin186b2502012-07-26 05:53:35 -03002175static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2176{
2177 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2178 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002179 struct coda_dev *dev = ctx->dev;
2180 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002181 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002182 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002183
Philipp Zabelb96904e2013-05-23 10:42:58 -03002184 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002185 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2186 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2187 if (coda_get_bitstream_payload(ctx) < 512)
2188 return -EINVAL;
2189 } else {
2190 if (count < 1)
2191 return -EINVAL;
2192 }
2193
2194 ctx->streamon_out = 1;
2195
Philipp Zabelb96904e2013-05-23 10:42:58 -03002196 if (coda_format_is_yuv(q_data_src->fourcc))
2197 ctx->inst_type = CODA_INST_ENCODER;
2198 else
2199 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002200 } else {
2201 if (count < 1)
2202 return -EINVAL;
2203
2204 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002205 }
Javier Martin186b2502012-07-26 05:53:35 -03002206
Philipp Zabelb96904e2013-05-23 10:42:58 -03002207 /* Don't start the coda unless both queues are on */
2208 if (!(ctx->streamon_out & ctx->streamon_cap))
2209 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002210
Philipp Zabeleb107512013-09-30 10:34:45 -03002211 /* Allow decoder device_run with no new buffers queued */
2212 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002213 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002214
Philipp Zabelb96904e2013-05-23 10:42:58 -03002215 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002216 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002217 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002218
Philipp Zabelb96904e2013-05-23 10:42:58 -03002219 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2220 q_data_dst->fourcc);
2221 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002222 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2223 return -EINVAL;
2224 }
2225
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002226 /* Allocate per-instance buffers */
2227 ret = coda_alloc_context_buffers(ctx, q_data_src);
2228 if (ret < 0)
2229 return ret;
2230
Philipp Zabel918c66f2013-06-27 06:59:01 -03002231 if (ctx->inst_type == CODA_INST_DECODER) {
2232 mutex_lock(&dev->coda_mutex);
2233 ret = coda_start_decoding(ctx);
2234 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002235 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002236 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002237 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002238 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002239 } else {
2240 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002241 }
2242
Philipp Zabel89548442014-07-11 06:36:17 -03002243 ctx->initialized = 1;
2244 return ret;
2245}
2246
2247static int coda_start_encoding(struct coda_ctx *ctx)
2248{
2249 struct coda_dev *dev = ctx->dev;
2250 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2251 struct coda_q_data *q_data_src, *q_data_dst;
2252 u32 bitstream_buf, bitstream_size;
2253 struct vb2_buffer *buf;
2254 int gamma, ret, value;
2255 u32 dst_fourcc;
2256
2257 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2258 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2259 dst_fourcc = q_data_dst->fourcc;
2260
Philipp Zabel14604e32014-07-11 06:36:22 -03002261 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002262 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2263 bitstream_size = q_data_dst->sizeimage;
2264
Javier Martin186b2502012-07-26 05:53:35 -03002265 if (!coda_is_initialized(dev)) {
2266 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2267 return -EFAULT;
2268 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002269
2270 mutex_lock(&dev->coda_mutex);
2271
Javier Martin186b2502012-07-26 05:53:35 -03002272 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002273 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2274 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002275 switch (dev->devtype->product) {
2276 case CODA_DX6:
2277 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2278 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2279 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002280 case CODA_960:
2281 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2282 /* fallthrough */
2283 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002284 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2285 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002286 break;
Javier Martin186b2502012-07-26 05:53:35 -03002287 }
2288
Philipp Zabel89548442014-07-11 06:36:17 -03002289 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2290 value &= ~(1 << 2 | 0x7 << 9);
2291 ctx->frame_mem_ctrl = value;
2292 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2293
Philipp Zabel10436672012-07-02 09:03:55 -03002294 if (dev->devtype->product == CODA_DX6) {
2295 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002296 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002297 }
Javier Martin186b2502012-07-26 05:53:35 -03002298
2299 /* Could set rotation here if needed */
2300 switch (dev->devtype->product) {
2301 case CODA_DX6:
2302 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002303 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002304 break;
2305 default:
2306 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002307 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002308 }
Javier Martin186b2502012-07-26 05:53:35 -03002309 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2310 coda_write(dev, ctx->params.framerate,
2311 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2312
Philipp Zabelb96904e2013-05-23 10:42:58 -03002313 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002314 switch (dst_fourcc) {
2315 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002316 if (dev->devtype->product == CODA_960)
2317 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2318 else
2319 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002320 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2321 break;
2322 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002323 if (dev->devtype->product == CODA_960)
2324 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2325 else
2326 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002327 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2328 break;
2329 default:
2330 v4l2_err(v4l2_dev,
2331 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002332 ret = -EINVAL;
2333 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002334 }
2335
Philipp Zabelc566c782012-08-08 11:59:38 -03002336 switch (ctx->params.slice_mode) {
2337 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2338 value = 0;
2339 break;
2340 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2341 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2342 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002343 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002344 break;
2345 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2346 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2347 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2348 value |= 1 & CODA_SLICING_MODE_MASK;
2349 break;
2350 }
Javier Martin186b2502012-07-26 05:53:35 -03002351 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002352 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002353 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2354
2355 if (ctx->params.bitrate) {
2356 /* Rate control enabled */
2357 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2358 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002359 if (dev->devtype->product == CODA_960)
2360 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002361 } else {
2362 value = 0;
2363 }
2364 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2365
2366 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2367 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2368
2369 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2370 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2371
Javier Martin186b2502012-07-26 05:53:35 -03002372
Philipp Zabel89548442014-07-11 06:36:17 -03002373 value = 0;
2374 if (dev->devtype->product == CODA_960)
2375 gamma = CODA9_DEFAULT_GAMMA;
2376 else
2377 gamma = CODA_DEFAULT_GAMMA;
2378 if (gamma > 0) {
2379 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2380 CODA_CMD_ENC_SEQ_RC_GAMMA);
2381 }
2382 if (dev->devtype->product == CODA_960) {
2383 if (CODA_DEFAULT_GAMMA > 0)
2384 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002385 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002386 if (CODA_DEFAULT_GAMMA > 0) {
2387 if (dev->devtype->product == CODA_DX6)
2388 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2389 else
2390 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2391 }
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002392 }
Javier Martin186b2502012-07-26 05:53:35 -03002393 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2394
Philipp Zabel89548442014-07-11 06:36:17 -03002395 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2396
Philipp Zabelc2d22512013-06-21 03:55:28 -03002397 coda_setup_iram(ctx);
2398
Javier Martin186b2502012-07-26 05:53:35 -03002399 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002400 switch (dev->devtype->product) {
2401 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002402 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002403 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002404 break;
2405 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002406 coda_write(dev, ctx->iram_info.search_ram_paddr,
2407 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2408 coda_write(dev, ctx->iram_info.search_ram_size,
2409 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002410 break;
2411 case CODA_960:
2412 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2413 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002414 }
Javier Martin186b2502012-07-26 05:53:35 -03002415 }
2416
Philipp Zabelfcb62822013-05-23 10:43:00 -03002417 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2418 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002419 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002420 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002421 }
2422
Philipp Zabelfcb62822013-05-23 10:43:00 -03002423 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2424 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2425 ret = -EFAULT;
2426 goto out;
2427 }
Javier Martin186b2502012-07-26 05:53:35 -03002428
Philipp Zabel89548442014-07-11 06:36:17 -03002429 if (dev->devtype->product == CODA_960)
2430 ctx->num_internal_frames = 4;
2431 else
2432 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002433 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002434 if (ret < 0) {
2435 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2436 goto out;
2437 }
Javier Martin186b2502012-07-26 05:53:35 -03002438
Philipp Zabelec25f682012-07-20 08:54:29 -03002439 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002440 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002441 if (dev->devtype->product == CODA_7541)
2442 coda_write(dev, round_up(q_data_src->width, 8),
2443 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002444 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002445 coda_write(dev, ctx->iram_info.buf_bit_use,
2446 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2447 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2448 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2449 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2450 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2451 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2452 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2453 coda_write(dev, ctx->iram_info.buf_ovl_use,
2454 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002455 if (dev->devtype->product == CODA_960) {
2456 coda_write(dev, ctx->iram_info.buf_btp_use,
2457 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2458
2459 /* FIXME */
2460 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2461 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2462 }
Philipp Zabel10436672012-07-02 09:03:55 -03002463 }
Philipp Zabel89548442014-07-11 06:36:17 -03002464
Philipp Zabelfcb62822013-05-23 10:43:00 -03002465 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2466 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002467 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002468 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002469 }
2470
2471 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002472 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002473 switch (dst_fourcc) {
2474 case V4L2_PIX_FMT_H264:
2475 /*
2476 * Get SPS in the first frame and copy it to an
2477 * intermediate buffer.
2478 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002479 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2480 &ctx->vpu_header[0][0],
2481 &ctx->vpu_header_size[0]);
2482 if (ret < 0)
2483 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002484
2485 /*
2486 * Get PPS in the first frame and copy it to an
2487 * intermediate buffer.
2488 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002489 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2490 &ctx->vpu_header[1][0],
2491 &ctx->vpu_header_size[1]);
2492 if (ret < 0)
2493 goto out;
2494
Javier Martin3f3f5c72012-10-29 05:20:29 -03002495 /*
2496 * Length of H.264 headers is variable and thus it might not be
2497 * aligned for the coda to append the encoded frame. In that is
2498 * the case a filler NAL must be added to header 2.
2499 */
2500 ctx->vpu_header_size[2] = coda_h264_padding(
2501 (ctx->vpu_header_size[0] +
2502 ctx->vpu_header_size[1]),
2503 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002504 break;
2505 case V4L2_PIX_FMT_MPEG4:
2506 /*
2507 * Get VOS in the first frame and copy it to an
2508 * intermediate buffer
2509 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002510 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2511 &ctx->vpu_header[0][0],
2512 &ctx->vpu_header_size[0]);
2513 if (ret < 0)
2514 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002515
Philipp Zabeld35167a2013-05-23 10:42:59 -03002516 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2517 &ctx->vpu_header[1][0],
2518 &ctx->vpu_header_size[1]);
2519 if (ret < 0)
2520 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002521
Philipp Zabeld35167a2013-05-23 10:42:59 -03002522 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2523 &ctx->vpu_header[2][0],
2524 &ctx->vpu_header_size[2]);
2525 if (ret < 0)
2526 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002527 break;
2528 default:
2529 /* No more formats need to save headers at the moment */
2530 break;
2531 }
2532
Philipp Zabeld35167a2013-05-23 10:42:59 -03002533out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002534 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002535 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002536}
2537
Hans Verkuile37559b2014-04-17 02:47:21 -03002538static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002539{
2540 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002541 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002542
2543 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002544 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002545 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002546 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002547
Philipp Zabel89548442014-07-11 06:36:17 -03002548 if (ctx->inst_type == CODA_INST_DECODER &&
2549 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2550 /* if this decoder instance is running, set the stream end flag */
2551 if (dev->devtype->product == CODA_960) {
2552 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2553
2554 val |= CODA_BIT_STREAM_END_FLAG;
2555 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2556 ctx->bit_stream_param = val;
2557 }
2558 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002559 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2560
2561 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002562 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002563 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002564 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002565 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002566
2567 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002568 }
2569
Philipp Zabel918c66f2013-06-27 06:59:01 -03002570 if (!ctx->streamon_out && !ctx->streamon_cap) {
2571 kfifo_init(&ctx->bitstream_fifo,
2572 ctx->bitstream.vaddr, ctx->bitstream.size);
2573 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002574 }
Javier Martin186b2502012-07-26 05:53:35 -03002575}
2576
2577static struct vb2_ops coda_qops = {
2578 .queue_setup = coda_queue_setup,
2579 .buf_prepare = coda_buf_prepare,
2580 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002581 .start_streaming = coda_start_streaming,
2582 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002583 .wait_prepare = vb2_ops_wait_prepare,
2584 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002585};
2586
2587static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2588{
2589 struct coda_ctx *ctx =
2590 container_of(ctrl->handler, struct coda_ctx, ctrls);
2591
2592 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2593 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2594
2595 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002596 case V4L2_CID_HFLIP:
2597 if (ctrl->val)
2598 ctx->params.rot_mode |= CODA_MIR_HOR;
2599 else
2600 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2601 break;
2602 case V4L2_CID_VFLIP:
2603 if (ctrl->val)
2604 ctx->params.rot_mode |= CODA_MIR_VER;
2605 else
2606 ctx->params.rot_mode &= ~CODA_MIR_VER;
2607 break;
Javier Martin186b2502012-07-26 05:53:35 -03002608 case V4L2_CID_MPEG_VIDEO_BITRATE:
2609 ctx->params.bitrate = ctrl->val / 1000;
2610 break;
2611 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2612 ctx->params.gop_size = ctrl->val;
2613 break;
2614 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2615 ctx->params.h264_intra_qp = ctrl->val;
2616 break;
2617 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2618 ctx->params.h264_inter_qp = ctrl->val;
2619 break;
2620 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2621 ctx->params.mpeg4_intra_qp = ctrl->val;
2622 break;
2623 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2624 ctx->params.mpeg4_inter_qp = ctrl->val;
2625 break;
2626 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2627 ctx->params.slice_mode = ctrl->val;
2628 break;
2629 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2630 ctx->params.slice_max_mb = ctrl->val;
2631 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002632 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2633 ctx->params.slice_max_bits = ctrl->val * 8;
2634 break;
Javier Martin186b2502012-07-26 05:53:35 -03002635 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2636 break;
2637 default:
2638 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2639 "Invalid control, id=%d, val=%d\n",
2640 ctrl->id, ctrl->val);
2641 return -EINVAL;
2642 }
2643
2644 return 0;
2645}
2646
2647static struct v4l2_ctrl_ops coda_ctrl_ops = {
2648 .s_ctrl = coda_s_ctrl,
2649};
2650
2651static int coda_ctrls_setup(struct coda_ctx *ctx)
2652{
2653 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2654
2655 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002656 V4L2_CID_HFLIP, 0, 1, 1, 0);
2657 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2658 V4L2_CID_VFLIP, 0, 1, 1, 0);
2659 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002660 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2661 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2662 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2663 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002664 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002665 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002666 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002667 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2668 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2669 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2670 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2671 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2672 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002673 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2674 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002675 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2676 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002677 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2678 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002679 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2680 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2681 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2682 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2683 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2684
2685 if (ctx->ctrls.error) {
2686 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2687 ctx->ctrls.error);
2688 return -EINVAL;
2689 }
2690
2691 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2692}
2693
2694static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2695 struct vb2_queue *dst_vq)
2696{
2697 struct coda_ctx *ctx = priv;
2698 int ret;
2699
Javier Martin186b2502012-07-26 05:53:35 -03002700 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002701 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002702 src_vq->drv_priv = ctx;
2703 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2704 src_vq->ops = &coda_qops;
2705 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002706 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002707 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002708
2709 ret = vb2_queue_init(src_vq);
2710 if (ret)
2711 return ret;
2712
Javier Martin186b2502012-07-26 05:53:35 -03002713 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002714 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002715 dst_vq->drv_priv = ctx;
2716 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2717 dst_vq->ops = &coda_qops;
2718 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002719 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002720 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002721
2722 return vb2_queue_init(dst_vq);
2723}
2724
Philipp Zabele11f3e62012-07-25 09:16:58 -03002725static int coda_next_free_instance(struct coda_dev *dev)
2726{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002727 int idx = ffz(dev->instance_mask);
2728
2729 if ((idx < 0) ||
2730 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2731 return -EBUSY;
2732
2733 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002734}
2735
Javier Martin186b2502012-07-26 05:53:35 -03002736static int coda_open(struct file *file)
2737{
2738 struct coda_dev *dev = video_drvdata(file);
2739 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002740 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002741 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002742
Javier Martin186b2502012-07-26 05:53:35 -03002743 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2744 if (!ctx)
2745 return -ENOMEM;
2746
Fabio Estevamf82bc202013-08-21 11:14:16 -03002747 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002748 if (idx < 0) {
2749 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002750 goto err_coda_max;
2751 }
2752 set_bit(idx, &dev->instance_mask);
2753
Philipp Zabel32b6f202014-07-11 06:36:20 -03002754 init_completion(&ctx->completion);
2755 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2756 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002757 v4l2_fh_init(&ctx->fh, video_devdata(file));
2758 file->private_data = &ctx->fh;
2759 v4l2_fh_add(&ctx->fh);
2760 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002761 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002762 switch (dev->devtype->product) {
2763 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002764 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002765 ctx->reg_idx = 0;
2766 break;
2767 default:
2768 ctx->reg_idx = idx;
2769 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002770
Philipp Zabel1e172732014-07-11 06:36:23 -03002771 /* Power up and upload firmware if necessary */
2772 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2773 if (ret < 0) {
2774 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2775 goto err_pm_get;
2776 }
2777
Fabio Estevam79830db2013-08-21 11:14:17 -03002778 ret = clk_prepare_enable(dev->clk_per);
2779 if (ret)
2780 goto err_clk_per;
2781
2782 ret = clk_prepare_enable(dev->clk_ahb);
2783 if (ret)
2784 goto err_clk_ahb;
2785
Javier Martin186b2502012-07-26 05:53:35 -03002786 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002787 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002788 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002789 if (IS_ERR(ctx->fh.m2m_ctx)) {
2790 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002791
2792 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2793 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002794 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002795 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002796
Javier Martin186b2502012-07-26 05:53:35 -03002797 ret = coda_ctrls_setup(ctx);
2798 if (ret) {
2799 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002800 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002801 }
2802
2803 ctx->fh.ctrl_handler = &ctx->ctrls;
2804
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002805 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2806 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002807 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002808 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002809 }
2810
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002811 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2812 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2813 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2814 if (!ctx->bitstream.vaddr) {
2815 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2816 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002817 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002818 }
2819 kfifo_init(&ctx->bitstream_fifo,
2820 ctx->bitstream.vaddr, ctx->bitstream.size);
2821 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002822 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002823
Javier Martin186b2502012-07-26 05:53:35 -03002824 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002825 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002826 coda_unlock(ctx);
2827
Javier Martin186b2502012-07-26 05:53:35 -03002828 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2829 ctx->idx, ctx);
2830
2831 return 0;
2832
Fabio Estevamf82bc202013-08-21 11:14:16 -03002833err_dma_writecombine:
2834 coda_free_context_buffers(ctx);
2835 if (ctx->dev->devtype->product == CODA_DX6)
2836 coda_free_aux_buf(dev, &ctx->workbuf);
2837 coda_free_aux_buf(dev, &ctx->parabuf);
2838err_dma_alloc:
2839 v4l2_ctrl_handler_free(&ctx->ctrls);
2840err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002841 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002842err_ctx_init:
2843 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002844err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002845 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002846err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002847 pm_runtime_put_sync(&dev->plat_dev->dev);
2848err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002849 v4l2_fh_del(&ctx->fh);
2850 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002851 clear_bit(ctx->idx, &dev->instance_mask);
2852err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002853 kfree(ctx);
2854 return ret;
2855}
2856
2857static int coda_release(struct file *file)
2858{
2859 struct coda_dev *dev = video_drvdata(file);
2860 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2861
2862 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2863 ctx);
2864
Philipp Zabel918c66f2013-06-27 06:59:01 -03002865 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002866 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002867
2868 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002869 if (ctx->initialized) {
2870 queue_work(dev->workqueue, &ctx->seq_end_work);
2871 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002872 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002873
2874 coda_free_framebuffers(ctx);
2875
Javier Martin186b2502012-07-26 05:53:35 -03002876 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002877 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002878 coda_unlock(ctx);
2879
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002880 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2881 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002882 coda_free_context_buffers(ctx);
2883 if (ctx->dev->devtype->product == CODA_DX6)
2884 coda_free_aux_buf(dev, &ctx->workbuf);
2885
2886 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002887 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002888 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002889 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002890 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002891 v4l2_fh_del(&ctx->fh);
2892 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002893 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002894 kfree(ctx);
2895
2896 return 0;
2897}
2898
Javier Martin186b2502012-07-26 05:53:35 -03002899static const struct v4l2_file_operations coda_fops = {
2900 .owner = THIS_MODULE,
2901 .open = coda_open,
2902 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002903 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002904 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002905 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002906};
2907
Philipp Zabel918c66f2013-06-27 06:59:01 -03002908static void coda_finish_decode(struct coda_ctx *ctx)
2909{
2910 struct coda_dev *dev = ctx->dev;
2911 struct coda_q_data *q_data_src;
2912 struct coda_q_data *q_data_dst;
2913 struct vb2_buffer *dst_buf;
2914 int width, height;
2915 int decoded_idx;
2916 int display_idx;
2917 u32 src_fourcc;
2918 int success;
2919 u32 val;
2920
Philipp Zabel14604e32014-07-11 06:36:22 -03002921 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002922
2923 /* Update kfifo out pointer from coda bitstream read pointer */
2924 coda_kfifo_sync_from_device(ctx);
2925
2926 /*
2927 * in stream-end mode, the read pointer can overshoot the write pointer
2928 * by up to 512 bytes
2929 */
2930 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2931 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2932 kfifo_init(&ctx->bitstream_fifo,
2933 ctx->bitstream.vaddr, ctx->bitstream.size);
2934 }
2935
2936 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2937 src_fourcc = q_data_src->fourcc;
2938
2939 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2940 if (val != 1)
2941 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2942
2943 success = val & 0x1;
2944 if (!success)
2945 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2946
2947 if (src_fourcc == V4L2_PIX_FMT_H264) {
2948 if (val & (1 << 3))
2949 v4l2_err(&dev->v4l2_dev,
2950 "insufficient PS buffer space (%d bytes)\n",
2951 ctx->psbuf.size);
2952 if (val & (1 << 2))
2953 v4l2_err(&dev->v4l2_dev,
2954 "insufficient slice buffer space (%d bytes)\n",
2955 ctx->slicebuf.size);
2956 }
2957
2958 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2959 width = (val >> 16) & 0xffff;
2960 height = val & 0xffff;
2961
2962 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2963
Philipp Zabel52c41672014-07-11 06:36:19 -03002964 /* frame crop information */
2965 if (src_fourcc == V4L2_PIX_FMT_H264) {
2966 u32 left_right;
2967 u32 top_bottom;
2968
2969 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
2970 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
2971
2972 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
2973 /* Keep current crop information */
2974 } else {
2975 struct v4l2_rect *rect = &q_data_dst->rect;
2976
2977 rect->left = left_right >> 16 & 0xffff;
2978 rect->top = top_bottom >> 16 & 0xffff;
2979 rect->width = width - rect->left -
2980 (left_right & 0xffff);
2981 rect->height = height - rect->top -
2982 (top_bottom & 0xffff);
2983 }
2984 } else {
2985 /* no cropping */
2986 }
2987
Philipp Zabel918c66f2013-06-27 06:59:01 -03002988 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
2989 if (val > 0)
2990 v4l2_err(&dev->v4l2_dev,
2991 "errors in %d macroblocks\n", val);
2992
2993 if (dev->devtype->product == CODA_7541) {
2994 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
2995 if (val == 0) {
2996 /* not enough bitstream data */
2997 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
2998 "prescan failed: %d\n", val);
2999 ctx->prescan_failed = true;
3000 return;
3001 }
3002 }
3003
3004 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3005
3006 /*
3007 * The previous display frame was copied out by the rotator,
3008 * now it can be overwritten again
3009 */
3010 if (ctx->display_idx >= 0 &&
3011 ctx->display_idx < ctx->num_internal_frames) {
3012 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3013 coda_write(dev, ctx->frm_dis_flg,
3014 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3015 }
3016
3017 /*
3018 * The index of the last decoded frame, not necessarily in
3019 * display order, and the index of the next display frame.
3020 * The latter could have been decoded in a previous run.
3021 */
3022 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3023 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3024
3025 if (decoded_idx == -1) {
3026 /* no frame was decoded, but we might have a display frame */
3027 if (display_idx < 0 && ctx->display_idx < 0)
3028 ctx->prescan_failed = true;
3029 } else if (decoded_idx == -2) {
3030 /* no frame was decoded, we still return the remaining buffers */
3031 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3032 v4l2_err(&dev->v4l2_dev,
3033 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003034 } else {
3035 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3036 if (val == 0)
3037 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3038 else if (val == 1)
3039 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3040 else
3041 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003042 }
3043
3044 if (display_idx == -1) {
3045 /*
3046 * no more frames to be decoded, but there could still
3047 * be rotator output to dequeue
3048 */
3049 ctx->prescan_failed = true;
3050 } else if (display_idx == -3) {
3051 /* possibly prescan failure */
3052 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3053 v4l2_err(&dev->v4l2_dev,
3054 "presentation frame index out of range: %d\n",
3055 display_idx);
3056 }
3057
3058 /* If a frame was copied out, return it */
3059 if (ctx->display_idx >= 0 &&
3060 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003061 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003062 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3063
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003064 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3065 V4L2_BUF_FLAG_PFRAME |
3066 V4L2_BUF_FLAG_BFRAME);
3067 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
3068
Philipp Zabel918c66f2013-06-27 06:59:01 -03003069 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3070
3071 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3072 VB2_BUF_STATE_ERROR);
3073
3074 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3075 "job finished: decoding frame (%d) (%s)\n",
3076 dst_buf->v4l2_buf.sequence,
3077 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3078 "KEYFRAME" : "PFRAME");
3079 } else {
3080 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3081 "job finished: no frame decoded\n");
3082 }
3083
3084 /* The rotator will copy the current display frame next time */
3085 ctx->display_idx = display_idx;
3086}
3087
3088static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003089{
Philipp Zabelec25f682012-07-20 08:54:29 -03003090 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003091 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003092 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003093
Philipp Zabel14604e32014-07-11 06:36:22 -03003094 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3095 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003096
3097 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003098 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003099 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3100
Javier Martin186b2502012-07-26 05:53:35 -03003101 /* Calculate bytesused field */
3102 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003103 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3104 ctx->vpu_header_size[0] +
3105 ctx->vpu_header_size[1] +
3106 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003107 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003108 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003109 }
3110
3111 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3112 wr_ptr - start_ptr);
3113
3114 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3115 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3116
Philipp Zabel51660282013-09-30 10:34:49 -03003117 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003118 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3119 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3120 } else {
3121 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3122 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3123 }
3124
Kamil Debskiccd571c2013-04-24 10:38:24 -03003125 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003126 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3127 dst_buf->v4l2_buf.flags |=
3128 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003129 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3130
Philipp Zabelec25f682012-07-20 08:54:29 -03003131 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003132
Philipp Zabel14604e32014-07-11 06:36:22 -03003133 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003134 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3135
3136 ctx->gopcounter--;
3137 if (ctx->gopcounter < 0)
3138 ctx->gopcounter = ctx->params.gop_size - 1;
3139
3140 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3141 "job finished: encoding frame (%d) (%s)\n",
3142 dst_buf->v4l2_buf.sequence,
3143 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3144 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003145}
3146
3147static irqreturn_t coda_irq_handler(int irq, void *data)
3148{
3149 struct coda_dev *dev = data;
3150 struct coda_ctx *ctx;
3151
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003152 /* read status register to attend the IRQ */
3153 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3154 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3155 CODA_REG_BIT_INT_CLEAR);
3156
3157 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3158 if (ctx == NULL) {
3159 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3160 mutex_unlock(&dev->coda_mutex);
3161 return IRQ_HANDLED;
3162 }
3163
3164 if (ctx->aborting) {
3165 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3166 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003167 }
3168
3169 if (coda_isbusy(ctx->dev)) {
3170 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3171 "coda is still busy!!!!\n");
3172 return IRQ_NONE;
3173 }
3174
Philipp Zabel32b6f202014-07-11 06:36:20 -03003175 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003176
3177 return IRQ_HANDLED;
3178}
3179
3180static u32 coda_supported_firmwares[] = {
3181 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003182 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003183 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003184};
3185
3186static bool coda_firmware_supported(u32 vernum)
3187{
3188 int i;
3189
3190 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3191 if (vernum == coda_supported_firmwares[i])
3192 return true;
3193 return false;
3194}
3195
Philipp Zabel87048bb2012-07-02 07:03:43 -03003196static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003197{
Javier Martin186b2502012-07-26 05:53:35 -03003198 u32 data;
3199 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003200 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003201
Fabio Estevam79830db2013-08-21 11:14:17 -03003202 ret = clk_prepare_enable(dev->clk_per);
3203 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003204 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003205
3206 ret = clk_prepare_enable(dev->clk_ahb);
3207 if (ret)
3208 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003209
Javier Martin186b2502012-07-26 05:53:35 -03003210 /*
3211 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003212 * The 16-bit chars in the code buffer are in memory access
3213 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003214 * Data in this SRAM survives a reboot.
3215 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003216 p = (u16 *)dev->codebuf.vaddr;
3217 if (dev->devtype->product == CODA_DX6) {
3218 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3219 data = CODA_DOWN_ADDRESS_SET(i) |
3220 CODA_DOWN_DATA_SET(p[i ^ 1]);
3221 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3222 }
3223 } else {
3224 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3225 data = CODA_DOWN_ADDRESS_SET(i) |
3226 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3227 3 - (i % 4)]);
3228 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3229 }
Javier Martin186b2502012-07-26 05:53:35 -03003230 }
Javier Martin186b2502012-07-26 05:53:35 -03003231
Philipp Zabel9acf7692013-05-23 10:42:56 -03003232 /* Clear registers */
3233 for (i = 0; i < 64; i++)
3234 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3235
Javier Martin186b2502012-07-26 05:53:35 -03003236 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003237 if (dev->devtype->product == CODA_960 ||
3238 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003239 coda_write(dev, dev->tempbuf.paddr,
3240 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003241 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003242 } else {
3243 coda_write(dev, dev->workbuf.paddr,
3244 CODA_REG_BIT_WORK_BUF_ADDR);
3245 }
Javier Martin186b2502012-07-26 05:53:35 -03003246 coda_write(dev, dev->codebuf.paddr,
3247 CODA_REG_BIT_CODE_BUF_ADDR);
3248 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3249
3250 /* Set default values */
3251 switch (dev->devtype->product) {
3252 case CODA_DX6:
3253 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3254 break;
3255 default:
3256 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3257 }
Philipp Zabel89548442014-07-11 06:36:17 -03003258 if (dev->devtype->product == CODA_960)
3259 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3260 else
3261 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003262
3263 if (dev->devtype->product != CODA_DX6)
3264 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3265
Javier Martin186b2502012-07-26 05:53:35 -03003266 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3267 CODA_REG_BIT_INT_ENABLE);
3268
3269 /* Reset VPU and start processor */
3270 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3271 data |= CODA_REG_RESET_ENABLE;
3272 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3273 udelay(10);
3274 data &= ~CODA_REG_RESET_ENABLE;
3275 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3276 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3277
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003278 clk_disable_unprepare(dev->clk_ahb);
3279 clk_disable_unprepare(dev->clk_per);
3280
3281 return 0;
3282
3283err_clk_ahb:
3284 clk_disable_unprepare(dev->clk_per);
3285err_clk_per:
3286 return ret;
3287}
3288
3289static int coda_check_firmware(struct coda_dev *dev)
3290{
3291 u16 product, major, minor, release;
3292 u32 data;
3293 int ret;
3294
3295 ret = clk_prepare_enable(dev->clk_per);
3296 if (ret)
3297 goto err_clk_per;
3298
3299 ret = clk_prepare_enable(dev->clk_ahb);
3300 if (ret)
3301 goto err_clk_ahb;
3302
Javier Martin186b2502012-07-26 05:53:35 -03003303 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3304 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3305 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3306 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3307 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3308 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003309 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003310 ret = -EIO;
3311 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003312 }
3313
Philipp Zabel89548442014-07-11 06:36:17 -03003314 if (dev->devtype->product == CODA_960) {
3315 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3316 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3317 data);
3318 }
3319
Javier Martin186b2502012-07-26 05:53:35 -03003320 /* Check we are compatible with the loaded firmware */
3321 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3322 product = CODA_FIRMWARE_PRODUCT(data);
3323 major = CODA_FIRMWARE_MAJOR(data);
3324 minor = CODA_FIRMWARE_MINOR(data);
3325 release = CODA_FIRMWARE_RELEASE(data);
3326
3327 clk_disable_unprepare(dev->clk_per);
3328 clk_disable_unprepare(dev->clk_ahb);
3329
3330 if (product != dev->devtype->product) {
3331 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3332 " Version: %u.%u.%u\n",
3333 coda_product_name(dev->devtype->product),
3334 coda_product_name(product), major, minor, release);
3335 return -EINVAL;
3336 }
3337
3338 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3339 coda_product_name(product));
3340
3341 if (coda_firmware_supported(data)) {
3342 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3343 major, minor, release);
3344 } else {
3345 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3346 "%u.%u.%u\n", major, minor, release);
3347 }
3348
3349 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003350
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003351err_run_cmd:
3352 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003353err_clk_ahb:
3354 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003355err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003356 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003357}
3358
3359static void coda_fw_callback(const struct firmware *fw, void *context)
3360{
3361 struct coda_dev *dev = context;
3362 struct platform_device *pdev = dev->plat_dev;
3363 int ret;
3364
3365 if (!fw) {
3366 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3367 return;
3368 }
3369
3370 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003371 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3372 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003373 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3374 return;
3375 }
3376
Philipp Zabel87048bb2012-07-02 07:03:43 -03003377 /* Copy the whole firmware image to the code buffer */
3378 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3379 release_firmware(fw);
3380
Philipp Zabel1e172732014-07-11 06:36:23 -03003381 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3382 /*
3383 * Enabling power temporarily will cause coda_hw_init to be
3384 * called via coda_runtime_resume by the pm domain.
3385 */
3386 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3387 if (ret < 0) {
3388 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3389 ret);
3390 return;
3391 }
3392
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003393 ret = coda_check_firmware(dev);
3394 if (ret < 0)
3395 return;
3396
Philipp Zabel1e172732014-07-11 06:36:23 -03003397 pm_runtime_put_sync(&dev->plat_dev->dev);
3398 } else {
3399 /*
3400 * If runtime pm is disabled or pm_domain is not set,
3401 * initialize once manually.
3402 */
3403 ret = coda_hw_init(dev);
3404 if (ret < 0) {
3405 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3406 return;
3407 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003408
3409 ret = coda_check_firmware(dev);
3410 if (ret < 0)
3411 return;
Javier Martin186b2502012-07-26 05:53:35 -03003412 }
3413
3414 dev->vfd.fops = &coda_fops,
3415 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3416 dev->vfd.release = video_device_release_empty,
3417 dev->vfd.lock = &dev->dev_mutex;
3418 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003419 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003420 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3421 video_set_drvdata(&dev->vfd, dev);
3422
3423 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3424 if (IS_ERR(dev->alloc_ctx)) {
3425 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3426 return;
3427 }
3428
3429 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3430 if (IS_ERR(dev->m2m_dev)) {
3431 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3432 goto rel_ctx;
3433 }
3434
3435 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3436 if (ret) {
3437 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3438 goto rel_m2m;
3439 }
3440 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3441 dev->vfd.num);
3442
3443 return;
3444
3445rel_m2m:
3446 v4l2_m2m_release(dev->m2m_dev);
3447rel_ctx:
3448 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3449}
3450
3451static int coda_firmware_request(struct coda_dev *dev)
3452{
3453 char *fw = dev->devtype->firmware;
3454
3455 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3456 coda_product_name(dev->devtype->product));
3457
3458 return request_firmware_nowait(THIS_MODULE, true,
3459 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3460}
3461
3462enum coda_platform {
3463 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003464 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003465 CODA_IMX6Q,
3466 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003467};
3468
Emil Goodec06d8752012-08-14 17:44:42 -03003469static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003470 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003471 .firmware = "v4l-codadx6-imx27.bin",
3472 .product = CODA_DX6,
3473 .codecs = codadx6_codecs,
3474 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003475 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003476 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003477 .firmware = "v4l-coda7541-imx53.bin",
3478 .product = CODA_7541,
3479 .codecs = coda7_codecs,
3480 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003481 },
Philipp Zabel89548442014-07-11 06:36:17 -03003482 [CODA_IMX6Q] = {
3483 .firmware = "v4l-coda960-imx6q.bin",
3484 .product = CODA_960,
3485 .codecs = coda9_codecs,
3486 .num_codecs = ARRAY_SIZE(coda9_codecs),
3487 },
3488 [CODA_IMX6DL] = {
3489 .firmware = "v4l-coda960-imx6dl.bin",
3490 .product = CODA_960,
3491 .codecs = coda9_codecs,
3492 .num_codecs = ARRAY_SIZE(coda9_codecs),
3493 },
Javier Martin186b2502012-07-26 05:53:35 -03003494};
3495
3496static struct platform_device_id coda_platform_ids[] = {
3497 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003498 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003499 { /* sentinel */ }
3500};
3501MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3502
3503#ifdef CONFIG_OF
3504static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003505 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003506 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003507 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3508 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003509 { /* sentinel */ }
3510};
3511MODULE_DEVICE_TABLE(of, coda_dt_ids);
3512#endif
3513
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003514static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003515{
3516 const struct of_device_id *of_id =
3517 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3518 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003519 struct coda_platform_data *pdata = pdev->dev.platform_data;
3520 struct device_node *np = pdev->dev.of_node;
3521 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003522 struct coda_dev *dev;
3523 struct resource *res;
3524 int ret, irq;
3525
3526 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3527 if (!dev) {
3528 dev_err(&pdev->dev, "Not enough memory for %s\n",
3529 CODA_NAME);
3530 return -ENOMEM;
3531 }
3532
3533 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003534 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003535
3536 dev->plat_dev = pdev;
3537 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3538 if (IS_ERR(dev->clk_per)) {
3539 dev_err(&pdev->dev, "Could not get per clock\n");
3540 return PTR_ERR(dev->clk_per);
3541 }
3542
3543 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3544 if (IS_ERR(dev->clk_ahb)) {
3545 dev_err(&pdev->dev, "Could not get ahb clock\n");
3546 return PTR_ERR(dev->clk_ahb);
3547 }
3548
3549 /* Get memory for physical registers */
3550 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003551 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3552 if (IS_ERR(dev->regs_base))
3553 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003554
3555 /* IRQ */
3556 irq = platform_get_irq(pdev, 0);
3557 if (irq < 0) {
3558 dev_err(&pdev->dev, "failed to get irq resource\n");
3559 return -ENOENT;
3560 }
3561
Philipp Zabel918c66f2013-06-27 06:59:01 -03003562 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003563 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003564 dev_err(&pdev->dev, "failed to request irq\n");
3565 return -ENOENT;
3566 }
3567
Philipp Zabel657eee72013-04-29 16:17:14 -07003568 /* Get IRAM pool from device tree or platform data */
3569 pool = of_get_named_gen_pool(np, "iram", 0);
3570 if (!pool && pdata)
3571 pool = dev_get_gen_pool(pdata->iram_dev);
3572 if (!pool) {
3573 dev_err(&pdev->dev, "iram pool not available\n");
3574 return -ENOMEM;
3575 }
3576 dev->iram_pool = pool;
3577
Javier Martin186b2502012-07-26 05:53:35 -03003578 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3579 if (ret)
3580 return ret;
3581
3582 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003583 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003584
3585 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3586
3587 if (of_id) {
3588 dev->devtype = of_id->data;
3589 } else if (pdev_id) {
3590 dev->devtype = &coda_devdata[pdev_id->driver_data];
3591 } else {
3592 v4l2_device_unregister(&dev->v4l2_dev);
3593 return -EINVAL;
3594 }
3595
3596 /* allocate auxiliary per-device buffers for the BIT processor */
3597 switch (dev->devtype->product) {
3598 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003599 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3600 CODADX6_WORK_BUF_SIZE);
3601 if (ret < 0) {
3602 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3603 v4l2_device_unregister(&dev->v4l2_dev);
3604 return ret;
3605 }
Javier Martin186b2502012-07-26 05:53:35 -03003606 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003607 case CODA_7541:
3608 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3609 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003610 case CODA_960:
3611 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3612 break;
Javier Martin186b2502012-07-26 05:53:35 -03003613 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003614 if (dev->tempbuf.size) {
3615 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3616 dev->tempbuf.size);
3617 if (ret < 0) {
3618 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3619 v4l2_device_unregister(&dev->v4l2_dev);
3620 return ret;
3621 }
Javier Martin186b2502012-07-26 05:53:35 -03003622 }
3623
Philipp Zabel918c66f2013-06-27 06:59:01 -03003624 switch (dev->devtype->product) {
3625 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003626 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003627 break;
3628 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003629 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003630 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003631 case CODA_960:
3632 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003633 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003634 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3635 &dev->iram.paddr);
3636 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003637 dev_err(&pdev->dev, "unable to alloc iram\n");
3638 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003639 }
3640
Philipp Zabel32b6f202014-07-11 06:36:20 -03003641 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3642 if (!dev->workqueue) {
3643 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3644 return -ENOMEM;
3645 }
3646
Javier Martin186b2502012-07-26 05:53:35 -03003647 platform_set_drvdata(pdev, dev);
3648
Philipp Zabel1e172732014-07-11 06:36:23 -03003649 pm_runtime_enable(&pdev->dev);
3650
Javier Martin186b2502012-07-26 05:53:35 -03003651 return coda_firmware_request(dev);
3652}
3653
3654static int coda_remove(struct platform_device *pdev)
3655{
3656 struct coda_dev *dev = platform_get_drvdata(pdev);
3657
3658 video_unregister_device(&dev->vfd);
3659 if (dev->m2m_dev)
3660 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003661 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003662 if (dev->alloc_ctx)
3663 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3664 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003665 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003666 if (dev->iram.vaddr)
3667 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3668 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003669 coda_free_aux_buf(dev, &dev->codebuf);
3670 coda_free_aux_buf(dev, &dev->tempbuf);
3671 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003672 return 0;
3673}
3674
Philipp Zabel1e172732014-07-11 06:36:23 -03003675#ifdef CONFIG_PM_RUNTIME
3676static int coda_runtime_resume(struct device *dev)
3677{
3678 struct coda_dev *cdev = dev_get_drvdata(dev);
3679 int ret = 0;
3680
3681 if (dev->pm_domain) {
3682 ret = coda_hw_init(cdev);
3683 if (ret)
3684 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3685 }
3686
3687 return ret;
3688}
3689#endif
3690
3691static const struct dev_pm_ops coda_pm_ops = {
3692 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3693};
3694
Javier Martin186b2502012-07-26 05:53:35 -03003695static struct platform_driver coda_driver = {
3696 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003697 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003698 .driver = {
3699 .name = CODA_NAME,
3700 .owner = THIS_MODULE,
3701 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003702 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003703 },
3704 .id_table = coda_platform_ids,
3705};
3706
3707module_platform_driver(coda_driver);
3708
3709MODULE_LICENSE("GPL");
3710MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3711MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");