blob: b8c3d61d98fd6a9b720d9b81c07af8e556f50417 [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>
Philipp Zabel8f452842014-07-11 06:36:35 -030030#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030031
32#include <media/v4l2-ctrls.h>
33#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030034#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030035#include <media/v4l2-ioctl.h>
36#include <media/v4l2-mem2mem.h>
37#include <media/videobuf2-core.h>
38#include <media/videobuf2-dma-contig.h>
39
40#include "coda.h"
41
42#define CODA_NAME "coda"
43
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030044#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030045
46#define CODA_FMO_BUF_SIZE 32
47#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030048#define CODA7_WORK_BUF_SIZE (128 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030049#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030050#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030051#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030052#define CODA_PARA_BUF_SIZE (10 * 1024)
53#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070054#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030055#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030056#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030057
Philipp Zabel918c66f2013-06-27 06:59:01 -030058#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030059#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030060
61#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030062
Philipp Zabelb96904e2013-05-23 10:42:58 -030063#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030064#define FMO_SLICE_SAVE_BUF_SIZE (32)
65#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030066#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030067
68#define MIN_W 176
69#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030070
71#define S_ALIGN 1 /* multiple of 2 */
72#define W_ALIGN 1 /* multiple of 2 */
73#define H_ALIGN 1 /* multiple of 2 */
74
75#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
76
77static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030078module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030079MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
80
81enum {
82 V4L2_M2M_SRC = 0,
83 V4L2_M2M_DST = 1,
84};
85
Javier Martin186b2502012-07-26 05:53:35 -030086enum coda_inst_type {
87 CODA_INST_ENCODER,
88 CODA_INST_DECODER,
89};
90
91enum coda_product {
92 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030093 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030094 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030095};
96
97struct coda_fmt {
98 char *name;
99 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300100};
101
102struct coda_codec {
103 u32 mode;
104 u32 src_fourcc;
105 u32 dst_fourcc;
106 u32 max_w;
107 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300108};
109
110struct coda_devtype {
111 char *firmware;
112 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300113 struct coda_codec *codecs;
114 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300115 size_t workbuf_size;
116};
117
118/* Per-queue, driver-specific private data */
119struct coda_q_data {
120 unsigned int width;
121 unsigned int height;
122 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300123 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300124 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300125};
126
127struct coda_aux_buf {
128 void *vaddr;
129 dma_addr_t paddr;
130 u32 size;
131};
132
133struct coda_dev {
134 struct v4l2_device v4l2_dev;
135 struct video_device vfd;
136 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300137 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300138
139 void __iomem *regs_base;
140 struct clk *clk_per;
141 struct clk *clk_ahb;
Philipp Zabel8f452842014-07-11 06:36:35 -0300142 struct reset_control *rstc;
Javier Martin186b2502012-07-26 05:53:35 -0300143
144 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300145 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300146 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700147 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300148 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300149
150 spinlock_t irqlock;
151 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300152 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300153 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300154 struct v4l2_m2m_dev *m2m_dev;
155 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300156 struct list_head instances;
157 unsigned long instance_mask;
Javier Martin186b2502012-07-26 05:53:35 -0300158};
159
160struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300161 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300162 u8 h264_intra_qp;
163 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300164 u8 h264_min_qp;
165 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300166 u8 h264_deblk_enabled;
167 u8 h264_deblk_alpha;
168 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300169 u8 mpeg4_intra_qp;
170 u8 mpeg4_inter_qp;
171 u8 gop_size;
Philipp Zabelf38f79d2014-07-11 06:36:28 -0300172 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300173 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300174 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300175 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
176 u32 framerate;
177 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300178 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300179 u32 slice_max_mb;
180};
181
Philipp Zabelc2d22512013-06-21 03:55:28 -0300182struct coda_iram_info {
183 u32 axi_sram_use;
184 phys_addr_t buf_bit_use;
185 phys_addr_t buf_ip_ac_dc_use;
186 phys_addr_t buf_dbk_y_use;
187 phys_addr_t buf_dbk_c_use;
188 phys_addr_t buf_ovl_use;
189 phys_addr_t buf_btp_use;
190 phys_addr_t search_ram_paddr;
191 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300192 int remaining;
193 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300194};
195
Philipp Zabel89548442014-07-11 06:36:17 -0300196struct gdi_tiled_map {
197 int xy2ca_map[16];
198 int xy2ba_map[16];
199 int xy2ra_map[16];
200 int rbc2axi_map[32];
201 int xy2rbc_config;
202 int map_type;
203#define GDI_LINEAR_FRAME_MAP 0
204};
205
Philipp Zabel846ced92014-07-11 06:36:31 -0300206struct coda_timestamp {
207 struct list_head list;
208 u32 sequence;
209 struct v4l2_timecode timecode;
210 struct timeval timestamp;
211};
212
Javier Martin186b2502012-07-26 05:53:35 -0300213struct coda_ctx {
214 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300215 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300216 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300217 struct work_struct pic_run_work;
218 struct work_struct seq_end_work;
219 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300220 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300221 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300222 int streamon_out;
223 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300224 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300225 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300226 u32 osequence;
Philipp Zabelcb2c0282014-07-11 06:36:33 -0300227 u32 sequence_offset;
Javier Martin186b2502012-07-26 05:53:35 -0300228 struct coda_q_data q_data[2];
229 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300230 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300231 enum v4l2_colorspace colorspace;
232 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300233 struct v4l2_ctrl_handler ctrls;
234 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300235 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300236 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300237 char vpu_header[3][64];
238 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300239 struct kfifo bitstream_fifo;
240 struct mutex bitstream_mutex;
241 struct coda_aux_buf bitstream;
Philipp Zabel84e23652014-07-11 06:36:34 -0300242 bool hold;
Javier Martin186b2502012-07-26 05:53:35 -0300243 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300244 struct coda_aux_buf psbuf;
245 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300246 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300247 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300248 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300249 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300250 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300251 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300252 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300253 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300254 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300255 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300256 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300257 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300258 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300259 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300260 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300261};
262
Javier Martin832fbb52012-10-29 08:34:59 -0300263static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
264 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
265static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300266
Javier Martin186b2502012-07-26 05:53:35 -0300267static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
268{
269 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
270 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
271 writel(data, dev->regs_base + reg);
272}
273
274static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
275{
276 u32 data;
277 data = readl(dev->regs_base + reg);
278 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
279 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
280 return data;
281}
282
283static inline unsigned long coda_isbusy(struct coda_dev *dev)
284{
285 return coda_read(dev, CODA_REG_BIT_BUSY);
286}
287
288static inline int coda_is_initialized(struct coda_dev *dev)
289{
290 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
291}
292
293static int coda_wait_timeout(struct coda_dev *dev)
294{
295 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
296
297 while (coda_isbusy(dev)) {
298 if (time_after(jiffies, timeout))
299 return -ETIMEDOUT;
300 }
301 return 0;
302}
303
304static void coda_command_async(struct coda_ctx *ctx, int cmd)
305{
306 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300307
Philipp Zabel89548442014-07-11 06:36:17 -0300308 if (dev->devtype->product == CODA_960 ||
309 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300310 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300311 coda_write(dev, ctx->bit_stream_param,
312 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300313 coda_write(dev, ctx->frm_dis_flg,
314 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300315 coda_write(dev, ctx->frame_mem_ctrl,
316 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300317 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
318 }
319
Philipp Zabel89548442014-07-11 06:36:17 -0300320 if (dev->devtype->product == CODA_960) {
321 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
322 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
323 }
324
Javier Martin186b2502012-07-26 05:53:35 -0300325 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
326
327 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
328 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300329 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
330
Javier Martin186b2502012-07-26 05:53:35 -0300331 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
332}
333
334static int coda_command_sync(struct coda_ctx *ctx, int cmd)
335{
336 struct coda_dev *dev = ctx->dev;
337
338 coda_command_async(ctx, cmd);
339 return coda_wait_timeout(dev);
340}
341
Philipp Zabel8f452842014-07-11 06:36:35 -0300342static int coda_hw_reset(struct coda_ctx *ctx)
343{
344 struct coda_dev *dev = ctx->dev;
345 unsigned long timeout;
346 unsigned int idx;
347 int ret;
348
349 if (!dev->rstc)
350 return -ENOENT;
351
352 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
353
354 timeout = jiffies + msecs_to_jiffies(100);
355 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
356 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
357 if (time_after(jiffies, timeout))
358 return -ETIME;
359 cpu_relax();
360 }
361
362 ret = reset_control_reset(dev->rstc);
363 if (ret < 0)
364 return ret;
365
366 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
367 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
368 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
369 ret = coda_wait_timeout(dev);
370 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
371
372 return ret;
373}
374
Javier Martin186b2502012-07-26 05:53:35 -0300375static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
376 enum v4l2_buf_type type)
377{
378 switch (type) {
379 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
380 return &(ctx->q_data[V4L2_M2M_SRC]);
381 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
382 return &(ctx->q_data[V4L2_M2M_DST]);
383 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300384 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300385 }
Javier Martin186b2502012-07-26 05:53:35 -0300386}
387
388/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300389 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300390 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300391static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300392 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300393 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300394 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300395 },
396 {
397 .name = "YUV 4:2:0 Planar, YCrCb",
398 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300399 },
400 {
401 .name = "H264 Encoded Stream",
402 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300403 },
404 {
405 .name = "MPEG4 Encoded Stream",
406 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300407 },
408};
409
Philipp Zabelb96904e2013-05-23 10:42:58 -0300410#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
411 { mode, src_fourcc, dst_fourcc, max_w, max_h }
412
413/*
414 * Arrays of codecs supported by each given version of Coda:
415 * i.MX27 -> codadx6
416 * i.MX5x -> coda7
417 * i.MX6 -> coda960
418 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
419 */
420static struct coda_codec codadx6_codecs[] = {
421 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
422 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300423};
424
Philipp Zabelb96904e2013-05-23 10:42:58 -0300425static struct coda_codec coda7_codecs[] = {
426 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
427 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300428 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
429 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300430};
431
Philipp Zabel89548442014-07-11 06:36:17 -0300432static struct coda_codec coda9_codecs[] = {
433 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
434 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
435 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
436 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
437};
438
Philipp Zabelb96904e2013-05-23 10:42:58 -0300439static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300440{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300441 switch (fourcc) {
442 case V4L2_PIX_FMT_YUV420:
443 case V4L2_PIX_FMT_YVU420:
444 return true;
445 default:
446 return false;
447 }
448}
Javier Martin186b2502012-07-26 05:53:35 -0300449
Philipp Zabelb96904e2013-05-23 10:42:58 -0300450/*
451 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
452 * tables.
453 */
454static u32 coda_format_normalize_yuv(u32 fourcc)
455{
456 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
457}
458
459static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
460 int dst_fourcc)
461{
462 struct coda_codec *codecs = dev->devtype->codecs;
463 int num_codecs = dev->devtype->num_codecs;
464 int k;
465
466 src_fourcc = coda_format_normalize_yuv(src_fourcc);
467 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
468 if (src_fourcc == dst_fourcc)
469 return NULL;
470
471 for (k = 0; k < num_codecs; k++) {
472 if (codecs[k].src_fourcc == src_fourcc &&
473 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300474 break;
475 }
476
Philipp Zabelb96904e2013-05-23 10:42:58 -0300477 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300478 return NULL;
479
Philipp Zabelb96904e2013-05-23 10:42:58 -0300480 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300481}
482
Philipp Zabel57625592013-09-30 10:34:51 -0300483static void coda_get_max_dimensions(struct coda_dev *dev,
484 struct coda_codec *codec,
485 int *max_w, int *max_h)
486{
487 struct coda_codec *codecs = dev->devtype->codecs;
488 int num_codecs = dev->devtype->num_codecs;
489 unsigned int w, h;
490 int k;
491
492 if (codec) {
493 w = codec->max_w;
494 h = codec->max_h;
495 } else {
496 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
497 w = max(w, codecs[k].max_w);
498 h = max(h, codecs[k].max_h);
499 }
500 }
501
502 if (max_w)
503 *max_w = w;
504 if (max_h)
505 *max_h = h;
506}
507
Philipp Zabel927933f2013-09-30 10:34:48 -0300508static char *coda_product_name(int product)
509{
510 static char buf[9];
511
512 switch (product) {
513 case CODA_DX6:
514 return "CodaDx6";
515 case CODA_7541:
516 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300517 case CODA_960:
518 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300519 default:
520 snprintf(buf, sizeof(buf), "(0x%04x)", product);
521 return buf;
522 }
523}
524
Javier Martin186b2502012-07-26 05:53:35 -0300525/*
526 * V4L2 ioctl() operations.
527 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300528static int coda_querycap(struct file *file, void *priv,
529 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300530{
Philipp Zabel927933f2013-09-30 10:34:48 -0300531 struct coda_ctx *ctx = fh_to_ctx(priv);
532
Javier Martin186b2502012-07-26 05:53:35 -0300533 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300534 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
535 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300536 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300537 /*
538 * This is only a mem-to-mem video device. The capture and output
539 * device capability flags are left only for backward compatibility
540 * and are scheduled for removal.
541 */
542 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
543 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300544 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
545
546 return 0;
547}
548
549static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300550 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300551{
552 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300553 struct coda_codec *codecs = ctx->dev->devtype->codecs;
554 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300555 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300556 int num_codecs = ctx->dev->devtype->num_codecs;
557 int num_formats = ARRAY_SIZE(coda_formats);
558 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300559
560 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300561 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300562 if (coda_format_is_yuv(formats[i].fourcc) &&
563 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300564 if (num == f->index)
565 break;
566 ++num;
567 continue;
568 }
569 /* Compressed formats may be supported, check the codec list */
570 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300571 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300572 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300573 formats[i].fourcc == codecs[k].dst_fourcc &&
574 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300575 break;
576 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
577 formats[i].fourcc == codecs[k].src_fourcc)
578 break;
579 }
580 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300581 if (num == f->index)
582 break;
583 ++num;
584 }
585 }
586
587 if (i < num_formats) {
588 fmt = &formats[i];
589 strlcpy(f->description, fmt->name, sizeof(f->description));
590 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300591 if (!coda_format_is_yuv(fmt->fourcc))
592 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300593 return 0;
594 }
595
596 /* Format not found */
597 return -EINVAL;
598}
599
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300600static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
601 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300602{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300603 struct coda_ctx *ctx = fh_to_ctx(priv);
604 struct vb2_queue *src_vq;
605 struct coda_q_data *q_data_src;
606
607 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300608 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300609 if (vb2_is_streaming(src_vq)) {
610 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
611
612 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
613 q_data_src->fourcc);
614 }
615
616 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300617}
618
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300619static int coda_enum_fmt_vid_out(struct file *file, void *priv,
620 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300621{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300622 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300623}
624
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300625static int coda_g_fmt(struct file *file, void *priv,
626 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300627{
Javier Martin186b2502012-07-26 05:53:35 -0300628 struct coda_q_data *q_data;
629 struct coda_ctx *ctx = fh_to_ctx(priv);
630
Javier Martin186b2502012-07-26 05:53:35 -0300631 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300632 if (!q_data)
633 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300634
635 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300636 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300637 f->fmt.pix.width = q_data->width;
638 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300639 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300640 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
641 else /* encoded formats h.264/mpeg4 */
642 f->fmt.pix.bytesperline = 0;
643
644 f->fmt.pix.sizeimage = q_data->sizeimage;
645 f->fmt.pix.colorspace = ctx->colorspace;
646
647 return 0;
648}
649
Philipp Zabel57625592013-09-30 10:34:51 -0300650static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
651 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300652{
Philipp Zabel57625592013-09-30 10:34:51 -0300653 struct coda_dev *dev = ctx->dev;
654 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300655 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300656 enum v4l2_field field;
657
658 field = f->fmt.pix.field;
659 if (field == V4L2_FIELD_ANY)
660 field = V4L2_FIELD_NONE;
661 else if (V4L2_FIELD_NONE != field)
662 return -EINVAL;
663
664 /* V4L2 specification suggests the driver corrects the format struct
665 * if any of the dimensions is unsupported */
666 f->fmt.pix.field = field;
667
Philipp Zabel57625592013-09-30 10:34:51 -0300668 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
669 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
670 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
671 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300672
Philipp Zabel57625592013-09-30 10:34:51 -0300673 switch (f->fmt.pix.pixelformat) {
674 case V4L2_PIX_FMT_YUV420:
675 case V4L2_PIX_FMT_YVU420:
676 case V4L2_PIX_FMT_H264:
677 case V4L2_PIX_FMT_MPEG4:
678 case V4L2_PIX_FMT_JPEG:
679 break;
680 default:
681 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300682 if (!q_data)
683 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300684 f->fmt.pix.pixelformat = q_data->fourcc;
685 }
686
687 switch (f->fmt.pix.pixelformat) {
688 case V4L2_PIX_FMT_YUV420:
689 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300690 /* Frame stride must be multiple of 8 */
691 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
692 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300693 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300694 break;
695 case V4L2_PIX_FMT_H264:
696 case V4L2_PIX_FMT_MPEG4:
697 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300698 f->fmt.pix.bytesperline = 0;
699 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300700 break;
701 default:
702 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300703 }
704
705 return 0;
706}
707
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300708static int coda_try_fmt_vid_cap(struct file *file, void *priv,
709 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300710{
Javier Martin186b2502012-07-26 05:53:35 -0300711 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300712 struct coda_codec *codec;
713 struct vb2_queue *src_vq;
714 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300715
Philipp Zabel918c66f2013-06-27 06:59:01 -0300716 /*
717 * If the source format is already fixed, try to find a codec that
718 * converts to the given destination format
719 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300720 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300721 if (vb2_is_streaming(src_vq)) {
722 struct coda_q_data *q_data_src;
723
724 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
725 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
726 f->fmt.pix.pixelformat);
727 if (!codec)
728 return -EINVAL;
729 } else {
730 /* Otherwise determine codec by encoded format, if possible */
731 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
732 f->fmt.pix.pixelformat);
733 }
Javier Martin186b2502012-07-26 05:53:35 -0300734
735 f->fmt.pix.colorspace = ctx->colorspace;
736
Philipp Zabel57625592013-09-30 10:34:51 -0300737 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300738 if (ret < 0)
739 return ret;
740
741 /* The h.264 decoder only returns complete 16x16 macroblocks */
742 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
743 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
744 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
745 f->fmt.pix.bytesperline = f->fmt.pix.width;
746 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
747 f->fmt.pix.height * 3 / 2;
748 }
749
750 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300751}
752
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300753static int coda_try_fmt_vid_out(struct file *file, void *priv,
754 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300755{
756 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300757 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300758
Philipp Zabelb96904e2013-05-23 10:42:58 -0300759 /* Determine codec by encoded format, returns NULL if raw or invalid */
760 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
761 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300762
763 if (!f->fmt.pix.colorspace)
764 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
765
Philipp Zabel57625592013-09-30 10:34:51 -0300766 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300767}
768
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300769static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300770{
771 struct coda_q_data *q_data;
772 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300773
Philipp Zabel14604e32014-07-11 06:36:22 -0300774 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300775 if (!vq)
776 return -EINVAL;
777
778 q_data = get_q_data(ctx, f->type);
779 if (!q_data)
780 return -EINVAL;
781
782 if (vb2_is_busy(vq)) {
783 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
784 return -EBUSY;
785 }
786
Philipp Zabelb96904e2013-05-23 10:42:58 -0300787 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300788 q_data->width = f->fmt.pix.width;
789 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300790 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300791 q_data->rect.left = 0;
792 q_data->rect.top = 0;
793 q_data->rect.width = f->fmt.pix.width;
794 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300795
796 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
797 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300798 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300799
800 return 0;
801}
802
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300803static int coda_s_fmt_vid_cap(struct file *file, void *priv,
804 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300805{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300806 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300807 int ret;
808
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300809 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300810 if (ret)
811 return ret;
812
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300813 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300814}
815
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300816static int coda_s_fmt_vid_out(struct file *file, void *priv,
817 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300818{
819 struct coda_ctx *ctx = fh_to_ctx(priv);
820 int ret;
821
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300822 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300823 if (ret)
824 return ret;
825
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300826 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300827 if (ret)
828 ctx->colorspace = f->fmt.pix.colorspace;
829
830 return ret;
831}
832
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300833static int coda_qbuf(struct file *file, void *priv,
834 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300835{
836 struct coda_ctx *ctx = fh_to_ctx(priv);
837
Philipp Zabel14604e32014-07-11 06:36:22 -0300838 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300839}
840
Philipp Zabel918c66f2013-06-27 06:59:01 -0300841static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
842 struct v4l2_buffer *buf)
843{
844 struct vb2_queue *src_vq;
845
Philipp Zabel14604e32014-07-11 06:36:22 -0300846 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300847
848 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
849 (buf->sequence == (ctx->qsequence - 1)));
850}
851
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300852static int coda_dqbuf(struct file *file, void *priv,
853 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300854{
855 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300856 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300857
Philipp Zabel14604e32014-07-11 06:36:22 -0300858 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300859
860 /* If this is the last capture buffer, emit an end-of-stream event */
861 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
862 coda_buf_is_end_of_stream(ctx, buf)) {
863 const struct v4l2_event eos_event = {
864 .type = V4L2_EVENT_EOS
865 };
866
867 v4l2_event_queue_fh(&ctx->fh, &eos_event);
868 }
869
870 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300871}
872
Philipp Zabel52c41672014-07-11 06:36:19 -0300873static int coda_g_selection(struct file *file, void *fh,
874 struct v4l2_selection *s)
875{
876 struct coda_ctx *ctx = fh_to_ctx(fh);
877 struct coda_q_data *q_data;
878 struct v4l2_rect r, *rsel;
879
880 q_data = get_q_data(ctx, s->type);
881 if (!q_data)
882 return -EINVAL;
883
884 r.left = 0;
885 r.top = 0;
886 r.width = q_data->width;
887 r.height = q_data->height;
888 rsel = &q_data->rect;
889
890 switch (s->target) {
891 case V4L2_SEL_TGT_CROP_DEFAULT:
892 case V4L2_SEL_TGT_CROP_BOUNDS:
893 rsel = &r;
894 /* fallthrough */
895 case V4L2_SEL_TGT_CROP:
896 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
897 return -EINVAL;
898 break;
899 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
900 case V4L2_SEL_TGT_COMPOSE_PADDED:
901 rsel = &r;
902 /* fallthrough */
903 case V4L2_SEL_TGT_COMPOSE:
904 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
905 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
906 return -EINVAL;
907 break;
908 default:
909 return -EINVAL;
910 }
911
912 s->r = *rsel;
913
914 return 0;
915}
916
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300917static int coda_try_decoder_cmd(struct file *file, void *fh,
918 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300919{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300920 if (dc->cmd != V4L2_DEC_CMD_STOP)
921 return -EINVAL;
922
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300923 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300924 return -EINVAL;
925
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300926 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300927 return -EINVAL;
928
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300929 return 0;
930}
931
932static int coda_decoder_cmd(struct file *file, void *fh,
933 struct v4l2_decoder_cmd *dc)
934{
935 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300936 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300937 int ret;
938
939 ret = coda_try_decoder_cmd(file, fh, dc);
940 if (ret < 0)
941 return ret;
942
943 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300944 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300945 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300946
947 /* Set the strem-end flag on this context */
948 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
949
Philipp Zabel89548442014-07-11 06:36:17 -0300950 if ((dev->devtype->product == CODA_960) &&
951 coda_isbusy(dev) &&
952 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
953 /* If this context is currently running, update the hardware flag */
954 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
955 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300956 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300957 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300958
Philipp Zabel918c66f2013-06-27 06:59:01 -0300959 return 0;
960}
961
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300962static int coda_subscribe_event(struct v4l2_fh *fh,
963 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300964{
965 switch (sub->type) {
966 case V4L2_EVENT_EOS:
967 return v4l2_event_subscribe(fh, sub, 0, NULL);
968 default:
969 return v4l2_ctrl_subscribe_event(fh, sub);
970 }
Javier Martin186b2502012-07-26 05:53:35 -0300971}
972
973static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300974 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300975
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300976 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
977 .vidioc_g_fmt_vid_cap = coda_g_fmt,
978 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
979 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300980
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300981 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
982 .vidioc_g_fmt_vid_out = coda_g_fmt,
983 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
984 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300985
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300986 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
987 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300988
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300989 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300990 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300991 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300992 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300993
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300994 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
995 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300996
Philipp Zabel52c41672014-07-11 06:36:19 -0300997 .vidioc_g_selection = coda_g_selection,
998
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300999 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001000 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001001
Philipp Zabel2e9e4f12013-09-30 10:34:50 -03001002 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001003 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001004};
1005
Philipp Zabel918c66f2013-06-27 06:59:01 -03001006static int coda_start_decoding(struct coda_ctx *ctx);
1007
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001008static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
1009{
1010 return kfifo_len(&ctx->bitstream_fifo);
1011}
1012
1013static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
1014{
1015 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1016 struct coda_dev *dev = ctx->dev;
1017 u32 rd_ptr;
1018
1019 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1020 kfifo->out = (kfifo->in & ~kfifo->mask) |
1021 (rd_ptr - ctx->bitstream.paddr);
1022 if (kfifo->out > kfifo->in)
1023 kfifo->out -= kfifo->mask + 1;
1024}
1025
1026static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1027{
1028 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1029 struct coda_dev *dev = ctx->dev;
1030 u32 rd_ptr, wr_ptr;
1031
1032 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1033 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1034 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1035 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1036}
1037
1038static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1039{
1040 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1041 struct coda_dev *dev = ctx->dev;
1042 u32 wr_ptr;
1043
1044 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1045 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1046}
1047
1048static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1049{
1050 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1051 u32 n;
1052
1053 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1054 if (n < src_size)
1055 return -ENOSPC;
1056
1057 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1058 ctx->bitstream.size, DMA_TO_DEVICE);
1059
Philipp Zabel846ced92014-07-11 06:36:31 -03001060 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001061
1062 return 0;
1063}
1064
1065static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1066 struct vb2_buffer *src_buf)
1067{
1068 int ret;
1069
1070 if (coda_get_bitstream_payload(ctx) +
1071 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1072 return false;
1073
1074 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1075 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1076 return true;
1077 }
1078
1079 ret = coda_bitstream_queue(ctx, src_buf);
1080 if (ret < 0) {
1081 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1082 return false;
1083 }
1084 /* Sync read pointer to device */
1085 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1086 coda_kfifo_sync_to_device_write(ctx);
1087
Philipp Zabel84e23652014-07-11 06:36:34 -03001088 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001089
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001090 return true;
1091}
1092
1093static void coda_fill_bitstream(struct coda_ctx *ctx)
1094{
1095 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001096 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001097
Philipp Zabel14604e32014-07-11 06:36:22 -03001098 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1099 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001100
1101 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001102 /*
1103 * Source buffer is queued in the bitstream ringbuffer;
1104 * queue the timestamp and mark source buffer as done
1105 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001106 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001107
1108 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1109 if (ts) {
1110 ts->sequence = src_buf->v4l2_buf.sequence;
1111 ts->timecode = src_buf->v4l2_buf.timecode;
1112 ts->timestamp = src_buf->v4l2_buf.timestamp;
1113 list_add_tail(&ts->list, &ctx->timestamp_list);
1114 }
1115
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001116 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1117 } else {
1118 break;
1119 }
1120 }
1121}
1122
Philipp Zabel89548442014-07-11 06:36:17 -03001123static void coda_set_gdi_regs(struct coda_ctx *ctx)
1124{
1125 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1126 struct coda_dev *dev = ctx->dev;
1127 int i;
1128
1129 for (i = 0; i < 16; i++)
1130 coda_write(dev, tiled_map->xy2ca_map[i],
1131 CODA9_GDI_XY2_CAS_0 + 4 * i);
1132 for (i = 0; i < 4; i++)
1133 coda_write(dev, tiled_map->xy2ba_map[i],
1134 CODA9_GDI_XY2_BA_0 + 4 * i);
1135 for (i = 0; i < 16; i++)
1136 coda_write(dev, tiled_map->xy2ra_map[i],
1137 CODA9_GDI_XY2_RAS_0 + 4 * i);
1138 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1139 for (i = 0; i < 32; i++)
1140 coda_write(dev, tiled_map->rbc2axi_map[i],
1141 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1142}
1143
Javier Martin186b2502012-07-26 05:53:35 -03001144/*
1145 * Mem-to-mem operations.
1146 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001147static int coda_prepare_decode(struct coda_ctx *ctx)
1148{
1149 struct vb2_buffer *dst_buf;
1150 struct coda_dev *dev = ctx->dev;
1151 struct coda_q_data *q_data_dst;
1152 u32 stridey, height;
1153 u32 picture_y, picture_cb, picture_cr;
1154
Philipp Zabel14604e32014-07-11 06:36:22 -03001155 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001156 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1157
1158 if (ctx->params.rot_mode & CODA_ROT_90) {
1159 stridey = q_data_dst->height;
1160 height = q_data_dst->width;
1161 } else {
1162 stridey = q_data_dst->width;
1163 height = q_data_dst->height;
1164 }
1165
1166 /* Try to copy source buffer contents into the bitstream ringbuffer */
1167 mutex_lock(&ctx->bitstream_mutex);
1168 coda_fill_bitstream(ctx);
1169 mutex_unlock(&ctx->bitstream_mutex);
1170
1171 if (coda_get_bitstream_payload(ctx) < 512 &&
1172 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1173 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1174 "bitstream payload: %d, skipping\n",
1175 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001176 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001177 return -EAGAIN;
1178 }
1179
1180 /* Run coda_start_decoding (again) if not yet initialized */
1181 if (!ctx->initialized) {
1182 int ret = coda_start_decoding(ctx);
1183 if (ret < 0) {
1184 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001185 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001186 return -EAGAIN;
1187 } else {
1188 ctx->initialized = 1;
1189 }
1190 }
1191
Philipp Zabel89548442014-07-11 06:36:17 -03001192 if (dev->devtype->product == CODA_960)
1193 coda_set_gdi_regs(ctx);
1194
Philipp Zabel918c66f2013-06-27 06:59:01 -03001195 /* Set rotator output */
1196 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1197 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1198 /* Switch Cr and Cb for YVU420 format */
1199 picture_cr = picture_y + stridey * height;
1200 picture_cb = picture_cr + stridey / 2 * height / 2;
1201 } else {
1202 picture_cb = picture_y + stridey * height;
1203 picture_cr = picture_cb + stridey / 2 * height / 2;
1204 }
Philipp Zabel89548442014-07-11 06:36:17 -03001205
1206 if (dev->devtype->product == CODA_960) {
1207 /*
1208 * The CODA960 seems to have an internal list of buffers with
1209 * 64 entries that includes the registered frame buffers as
1210 * well as the rotator buffer output.
1211 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1212 */
1213 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1214 CODA9_CMD_DEC_PIC_ROT_INDEX);
1215 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1216 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1217 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1218 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1219 } else {
1220 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1221 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1222 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1223 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1224 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001225 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1226 CODA_CMD_DEC_PIC_ROT_MODE);
1227
1228 switch (dev->devtype->product) {
1229 case CODA_DX6:
1230 /* TBD */
1231 case CODA_7541:
1232 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1233 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001234 case CODA_960:
1235 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1236 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001237 }
1238
1239 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1240
1241 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1242 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1243
1244 return 0;
1245}
1246
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001247static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001248{
Javier Martin186b2502012-07-26 05:53:35 -03001249 struct coda_q_data *q_data_src, *q_data_dst;
1250 struct vb2_buffer *src_buf, *dst_buf;
1251 struct coda_dev *dev = ctx->dev;
1252 int force_ipicture;
1253 int quant_param = 0;
1254 u32 picture_y, picture_cb, picture_cr;
1255 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1256 u32 dst_fourcc;
1257
Philipp Zabel14604e32014-07-11 06:36:22 -03001258 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1259 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001260 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1261 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001262 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001263
Philipp Zabel918c66f2013-06-27 06:59:01 -03001264 src_buf->v4l2_buf.sequence = ctx->osequence;
1265 dst_buf->v4l2_buf.sequence = ctx->osequence;
1266 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001267
1268 /*
1269 * Workaround coda firmware BUG that only marks the first
1270 * frame as IDR. This is a problem for some decoders that can't
1271 * recover when a frame is lost.
1272 */
1273 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1274 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1275 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1276 } else {
1277 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1278 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1279 }
1280
Philipp Zabel89548442014-07-11 06:36:17 -03001281 if (dev->devtype->product == CODA_960)
1282 coda_set_gdi_regs(ctx);
1283
Javier Martin186b2502012-07-26 05:53:35 -03001284 /*
1285 * Copy headers at the beginning of the first frame for H.264 only.
1286 * In MPEG4 they are already copied by the coda.
1287 */
1288 if (src_buf->v4l2_buf.sequence == 0) {
1289 pic_stream_buffer_addr =
1290 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1291 ctx->vpu_header_size[0] +
1292 ctx->vpu_header_size[1] +
1293 ctx->vpu_header_size[2];
1294 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1295 ctx->vpu_header_size[0] -
1296 ctx->vpu_header_size[1] -
1297 ctx->vpu_header_size[2];
1298 memcpy(vb2_plane_vaddr(dst_buf, 0),
1299 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1300 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1301 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1302 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1303 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1304 ctx->vpu_header_size[2]);
1305 } else {
1306 pic_stream_buffer_addr =
1307 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1308 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1309 }
1310
1311 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1312 force_ipicture = 1;
1313 switch (dst_fourcc) {
1314 case V4L2_PIX_FMT_H264:
1315 quant_param = ctx->params.h264_intra_qp;
1316 break;
1317 case V4L2_PIX_FMT_MPEG4:
1318 quant_param = ctx->params.mpeg4_intra_qp;
1319 break;
1320 default:
1321 v4l2_warn(&ctx->dev->v4l2_dev,
1322 "cannot set intra qp, fmt not supported\n");
1323 break;
1324 }
1325 } else {
1326 force_ipicture = 0;
1327 switch (dst_fourcc) {
1328 case V4L2_PIX_FMT_H264:
1329 quant_param = ctx->params.h264_inter_qp;
1330 break;
1331 case V4L2_PIX_FMT_MPEG4:
1332 quant_param = ctx->params.mpeg4_inter_qp;
1333 break;
1334 default:
1335 v4l2_warn(&ctx->dev->v4l2_dev,
1336 "cannot set inter qp, fmt not supported\n");
1337 break;
1338 }
1339 }
1340
1341 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001342 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001343 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1344
1345
1346 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001347 switch (q_data_src->fourcc) {
1348 case V4L2_PIX_FMT_YVU420:
1349 /* Switch Cb and Cr for YVU420 format */
1350 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1351 picture_cb = picture_cr + q_data_src->width / 2 *
1352 q_data_src->height / 2;
1353 break;
1354 case V4L2_PIX_FMT_YUV420:
1355 default:
1356 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1357 picture_cr = picture_cb + q_data_src->width / 2 *
1358 q_data_src->height / 2;
1359 break;
1360 }
Javier Martin186b2502012-07-26 05:53:35 -03001361
Philipp Zabel89548442014-07-11 06:36:17 -03001362 if (dev->devtype->product == CODA_960) {
1363 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1364 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1365 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1366
1367 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1368 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1369 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1370 } else {
1371 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1372 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1373 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1374 }
Javier Martin186b2502012-07-26 05:53:35 -03001375 coda_write(dev, force_ipicture << 1 & 0x2,
1376 CODA_CMD_ENC_PIC_OPTION);
1377
1378 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1379 coda_write(dev, pic_stream_buffer_size / 1024,
1380 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001381
1382 if (!ctx->streamon_out) {
1383 /* After streamoff on the output side, set the stream end flag */
1384 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1385 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1386 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001387}
1388
1389static void coda_device_run(void *m2m_priv)
1390{
1391 struct coda_ctx *ctx = m2m_priv;
1392 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001393
1394 queue_work(dev->workqueue, &ctx->pic_run_work);
1395}
1396
1397static void coda_free_framebuffers(struct coda_ctx *ctx);
1398static void coda_free_context_buffers(struct coda_ctx *ctx);
1399
1400static void coda_seq_end_work(struct work_struct *work)
1401{
1402 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1403 struct coda_dev *dev = ctx->dev;
1404
1405 mutex_lock(&ctx->buffer_mutex);
1406 mutex_lock(&dev->coda_mutex);
1407
1408 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1409 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1410 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1411 v4l2_err(&dev->v4l2_dev,
1412 "CODA_COMMAND_SEQ_END failed\n");
1413 }
1414
1415 kfifo_init(&ctx->bitstream_fifo,
1416 ctx->bitstream.vaddr, ctx->bitstream.size);
1417
1418 coda_free_framebuffers(ctx);
1419 coda_free_context_buffers(ctx);
1420
1421 mutex_unlock(&dev->coda_mutex);
1422 mutex_unlock(&ctx->buffer_mutex);
1423}
1424
1425static void coda_finish_decode(struct coda_ctx *ctx);
1426static void coda_finish_encode(struct coda_ctx *ctx);
1427
1428static void coda_pic_run_work(struct work_struct *work)
1429{
1430 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1431 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001432 int ret;
1433
1434 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001435 mutex_lock(&dev->coda_mutex);
1436
Philipp Zabel918c66f2013-06-27 06:59:01 -03001437 if (ctx->inst_type == CODA_INST_DECODER) {
1438 ret = coda_prepare_decode(ctx);
1439 if (ret < 0) {
1440 mutex_unlock(&dev->coda_mutex);
1441 mutex_unlock(&ctx->buffer_mutex);
1442 /* job_finish scheduled by prepare_decode */
1443 return;
1444 }
1445 } else {
1446 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001447 }
1448
Philipp Zabelc2d22512013-06-21 03:55:28 -03001449 if (dev->devtype->product != CODA_DX6)
1450 coda_write(dev, ctx->iram_info.axi_sram_use,
1451 CODA7_REG_BIT_AXI_SRAM_USE);
1452
Philipp Zabel918c66f2013-06-27 06:59:01 -03001453 if (ctx->inst_type == CODA_INST_DECODER)
1454 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001455 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001456
1457 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1458 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001459
1460 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001461
1462 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001463 } else if (!ctx->aborting) {
1464 if (ctx->inst_type == CODA_INST_DECODER)
1465 coda_finish_decode(ctx);
1466 else
1467 coda_finish_encode(ctx);
1468 }
1469
1470 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1471 queue_work(dev->workqueue, &ctx->seq_end_work);
1472
1473 mutex_unlock(&dev->coda_mutex);
1474 mutex_unlock(&ctx->buffer_mutex);
1475
Philipp Zabel14604e32014-07-11 06:36:22 -03001476 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001477}
1478
1479static int coda_job_ready(void *m2m_priv)
1480{
1481 struct coda_ctx *ctx = m2m_priv;
1482
1483 /*
1484 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001485 * and 1 frame are needed. In the decoder case,
1486 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001487 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001488 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001489 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001490 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1491 "not ready: not enough video buffers.\n");
1492 return 0;
1493 }
1494
Philipp Zabel14604e32014-07-11 06:36:22 -03001495 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001496 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1497 "not ready: not enough video capture buffers.\n");
1498 return 0;
1499 }
1500
Philipp Zabel84e23652014-07-11 06:36:34 -03001501 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001502 ((ctx->inst_type == CODA_INST_DECODER) &&
1503 (coda_get_bitstream_payload(ctx) < 512) &&
1504 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1505 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1506 "%d: not ready: not enough bitstream data.\n",
1507 ctx->idx);
1508 return 0;
1509 }
1510
Philipp Zabel3e748262013-05-23 10:43:01 -03001511 if (ctx->aborting) {
1512 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1513 "not ready: aborting\n");
1514 return 0;
1515 }
1516
Javier Martin186b2502012-07-26 05:53:35 -03001517 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1518 "job ready\n");
1519 return 1;
1520}
1521
1522static void coda_job_abort(void *priv)
1523{
1524 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001525
1526 ctx->aborting = 1;
1527
1528 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1529 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001530}
1531
1532static void coda_lock(void *m2m_priv)
1533{
1534 struct coda_ctx *ctx = m2m_priv;
1535 struct coda_dev *pcdev = ctx->dev;
1536 mutex_lock(&pcdev->dev_mutex);
1537}
1538
1539static void coda_unlock(void *m2m_priv)
1540{
1541 struct coda_ctx *ctx = m2m_priv;
1542 struct coda_dev *pcdev = ctx->dev;
1543 mutex_unlock(&pcdev->dev_mutex);
1544}
1545
1546static struct v4l2_m2m_ops coda_m2m_ops = {
1547 .device_run = coda_device_run,
1548 .job_ready = coda_job_ready,
1549 .job_abort = coda_job_abort,
1550 .lock = coda_lock,
1551 .unlock = coda_unlock,
1552};
1553
Philipp Zabel89548442014-07-11 06:36:17 -03001554static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1555{
1556 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1557 int luma_map, chro_map, i;
1558
1559 memset(tiled_map, 0, sizeof(*tiled_map));
1560
1561 luma_map = 64;
1562 chro_map = 64;
1563 tiled_map->map_type = tiled_map_type;
1564 for (i = 0; i < 16; i++)
1565 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1566 for (i = 0; i < 4; i++)
1567 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1568 for (i = 0; i < 16; i++)
1569 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1570
1571 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1572 tiled_map->xy2rbc_config = 0;
1573 } else {
1574 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1575 tiled_map_type);
1576 return;
1577 }
1578}
1579
Javier Martin186b2502012-07-26 05:53:35 -03001580static void set_default_params(struct coda_ctx *ctx)
1581{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001582 int max_w;
1583 int max_h;
1584
1585 ctx->codec = &ctx->dev->devtype->codecs[0];
1586 max_w = ctx->codec->max_w;
1587 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001588
1589 ctx->params.codec_mode = CODA_MODE_INVALID;
1590 ctx->colorspace = V4L2_COLORSPACE_REC709;
1591 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001592 ctx->aborting = 0;
1593
1594 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001595 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1596 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1597 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1598 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1599 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1600 ctx->q_data[V4L2_M2M_DST].width = max_w;
1601 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001602 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001603 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1604 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1605 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1606 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001607
1608 if (ctx->dev->devtype->product == CODA_960)
1609 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001610}
1611
1612/*
1613 * Queue operations
1614 */
1615static int coda_queue_setup(struct vb2_queue *vq,
1616 const struct v4l2_format *fmt,
1617 unsigned int *nbuffers, unsigned int *nplanes,
1618 unsigned int sizes[], void *alloc_ctxs[])
1619{
1620 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001621 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001622 unsigned int size;
1623
Philipp Zabele34db062012-08-29 08:22:00 -03001624 q_data = get_q_data(ctx, vq->type);
1625 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001626
1627 *nplanes = 1;
1628 sizes[0] = size;
1629
1630 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1631
1632 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1633 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1634
1635 return 0;
1636}
1637
1638static int coda_buf_prepare(struct vb2_buffer *vb)
1639{
1640 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1641 struct coda_q_data *q_data;
1642
1643 q_data = get_q_data(ctx, vb->vb2_queue->type);
1644
1645 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1646 v4l2_warn(&ctx->dev->v4l2_dev,
1647 "%s data will not fit into plane (%lu < %lu)\n",
1648 __func__, vb2_plane_size(vb, 0),
1649 (long)q_data->sizeimage);
1650 return -EINVAL;
1651 }
1652
Javier Martin186b2502012-07-26 05:53:35 -03001653 return 0;
1654}
1655
1656static void coda_buf_queue(struct vb2_buffer *vb)
1657{
1658 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001659 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001660 struct coda_q_data *q_data;
1661
1662 q_data = get_q_data(ctx, vb->vb2_queue->type);
1663
1664 /*
1665 * In the decoder case, immediately try to copy the buffer into the
1666 * bitstream ringbuffer and mark it as ready to be dequeued.
1667 */
1668 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1669 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1670 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001671 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001672 * the stream end
1673 */
Philipp Zabel89548442014-07-11 06:36:17 -03001674 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001675 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001676 if ((dev->devtype->product == CODA_960) &&
1677 coda_isbusy(dev) &&
1678 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1679 /* if this decoder instance is running, set the stream end flag */
1680 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1681 }
1682 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001683 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001684 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001685 coda_fill_bitstream(ctx);
1686 mutex_unlock(&ctx->bitstream_mutex);
1687 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001688 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001689 }
Javier Martin186b2502012-07-26 05:53:35 -03001690}
1691
Philipp Zabel86eda902013-05-23 10:42:57 -03001692static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1693{
1694 struct coda_dev *dev = ctx->dev;
1695 u32 *p = ctx->parabuf.vaddr;
1696
1697 if (dev->devtype->product == CODA_DX6)
1698 p[index] = value;
1699 else
1700 p[index ^ 1] = value;
1701}
1702
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001703static int coda_alloc_aux_buf(struct coda_dev *dev,
1704 struct coda_aux_buf *buf, size_t size)
1705{
1706 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1707 GFP_KERNEL);
1708 if (!buf->vaddr)
1709 return -ENOMEM;
1710
1711 buf->size = size;
1712
1713 return 0;
1714}
1715
1716static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1717 struct coda_aux_buf *buf, size_t size)
1718{
1719 return coda_alloc_aux_buf(ctx->dev, buf, size);
1720}
1721
1722static void coda_free_aux_buf(struct coda_dev *dev,
1723 struct coda_aux_buf *buf)
1724{
1725 if (buf->vaddr) {
1726 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1727 buf->vaddr, buf->paddr);
1728 buf->vaddr = NULL;
1729 buf->size = 0;
1730 }
1731}
1732
1733static void coda_free_framebuffers(struct coda_ctx *ctx)
1734{
1735 int i;
1736
1737 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1738 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1739}
1740
Philipp Zabelec25f682012-07-20 08:54:29 -03001741static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1742{
1743 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001744 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001745 dma_addr_t paddr;
1746 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001747 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001748 int i;
1749
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001750 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1751 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001752 ysize = round_up(q_data->width, 8) * height;
1753
Philipp Zabelec25f682012-07-20 08:54:29 -03001754 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001755 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001756 size_t size;
1757
Philipp Zabelaed14b02014-07-11 06:36:15 -03001758 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001759 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1760 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001761 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001762 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1763 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001764 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001765 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001766 }
1767 }
1768
1769 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001770 for (i = 0; i < ctx->num_internal_frames; i++) {
1771 paddr = ctx->internal_frames[i].paddr;
1772 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1773 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1774 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001775
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001776 /* mvcol buffer for h.264 */
1777 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1778 dev->devtype->product != CODA_DX6)
1779 coda_parabuf_write(ctx, 96 + i,
1780 ctx->internal_frames[i].paddr +
1781 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001782 }
1783
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001784 /* mvcol buffer for mpeg4 */
1785 if ((dev->devtype->product != CODA_DX6) &&
1786 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1787 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1788 ysize + ysize/4 + ysize/4);
1789
Philipp Zabelec25f682012-07-20 08:54:29 -03001790 return 0;
1791}
1792
Javier Martin3f3f5c72012-10-29 05:20:29 -03001793static int coda_h264_padding(int size, char *p)
1794{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001795 int nal_size;
1796 int diff;
1797
Javier Martin832fbb52012-10-29 08:34:59 -03001798 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001799 if (diff == 0)
1800 return 0;
1801
Javier Martin832fbb52012-10-29 08:34:59 -03001802 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001803 memcpy(p, coda_filler_nal, nal_size);
1804
1805 /* Add rbsp stop bit and trailing at the end */
1806 *(p + nal_size - 1) = 0x80;
1807
1808 return nal_size;
1809}
1810
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001811static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1812{
1813 phys_addr_t ret;
1814
1815 size = round_up(size, 1024);
1816 if (size > iram->remaining)
1817 return 0;
1818 iram->remaining -= size;
1819
1820 ret = iram->next_paddr;
1821 iram->next_paddr += size;
1822
1823 return ret;
1824}
1825
Philipp Zabelc2d22512013-06-21 03:55:28 -03001826static void coda_setup_iram(struct coda_ctx *ctx)
1827{
1828 struct coda_iram_info *iram_info = &ctx->iram_info;
1829 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001830 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001831 int dbk_bits;
1832 int bit_bits;
1833 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001834
1835 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001836 iram_info->next_paddr = dev->iram.paddr;
1837 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001838
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001839 switch (dev->devtype->product) {
1840 case CODA_7541:
1841 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1842 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1843 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1844 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001845 case CODA_960:
1846 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1847 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1848 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1849 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001850 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001851 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001852 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001853
1854 if (ctx->inst_type == CODA_INST_ENCODER) {
1855 struct coda_q_data *q_data_src;
1856
1857 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1858 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1859
1860 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001861 if (dev->devtype->product == CODA_7541) {
1862 iram_info->search_ram_size = round_up(mb_width * 16 *
1863 36 + 2048, 1024);
1864 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1865 iram_info->search_ram_size);
1866 if (!iram_info->search_ram_paddr) {
1867 pr_err("IRAM is smaller than the search ram size\n");
1868 goto out;
1869 }
1870 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1871 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001872 }
1873
1874 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001875 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1876 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1877 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001878 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001879 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001880
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001881 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1882 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001883 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001884 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001885
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001886 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1887 if (!iram_info->buf_ip_ac_dc_use)
1888 goto out;
1889 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001890
Philipp Zabel8358e762013-06-21 03:55:32 -03001891 /* OVL and BTP disabled for encoder */
1892 } else if (ctx->inst_type == CODA_INST_DECODER) {
1893 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001894
1895 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1896 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001897
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001898 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1899 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1900 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001901 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001902 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001903
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001904 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1905 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001906 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001907 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001908
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001909 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1910 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001911 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001912 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001913
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001914 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001915 }
1916
1917out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001918 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1919 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1920 "IRAM smaller than needed\n");
1921
1922 if (dev->devtype->product == CODA_7541) {
1923 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001924 if (ctx->inst_type == CODA_INST_DECODER) {
1925 /* fw 1.4.50 */
1926 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1927 CODA7_USE_IP_ENABLE);
1928 } else {
1929 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001930 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1931 CODA7_USE_HOST_DBK_ENABLE |
1932 CODA7_USE_IP_ENABLE |
1933 CODA7_USE_DBK_ENABLE);
1934 }
1935 }
1936}
1937
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001938static void coda_free_context_buffers(struct coda_ctx *ctx)
1939{
1940 struct coda_dev *dev = ctx->dev;
1941
Philipp Zabel918c66f2013-06-27 06:59:01 -03001942 coda_free_aux_buf(dev, &ctx->slicebuf);
1943 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001944 if (dev->devtype->product != CODA_DX6)
1945 coda_free_aux_buf(dev, &ctx->workbuf);
1946}
1947
1948static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1949 struct coda_q_data *q_data)
1950{
1951 struct coda_dev *dev = ctx->dev;
1952 size_t size;
1953 int ret;
1954
1955 switch (dev->devtype->product) {
1956 case CODA_7541:
1957 size = CODA7_WORK_BUF_SIZE;
1958 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001959 case CODA_960:
1960 size = CODA9_WORK_BUF_SIZE;
1961 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1962 size += CODA9_PS_SAVE_SIZE;
1963 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001964 default:
1965 return 0;
1966 }
1967
Philipp Zabel918c66f2013-06-27 06:59:01 -03001968 if (ctx->psbuf.vaddr) {
1969 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1970 return -EBUSY;
1971 }
1972 if (ctx->slicebuf.vaddr) {
1973 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1974 return -EBUSY;
1975 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001976 if (ctx->workbuf.vaddr) {
1977 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1978 ret = -EBUSY;
1979 return -ENOMEM;
1980 }
1981
Philipp Zabel918c66f2013-06-27 06:59:01 -03001982 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1983 /* worst case slice size */
1984 size = (DIV_ROUND_UP(q_data->width, 16) *
1985 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1986 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1987 if (ret < 0) {
1988 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1989 ctx->slicebuf.size);
1990 return ret;
1991 }
1992 }
1993
1994 if (dev->devtype->product == CODA_7541) {
1995 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1996 if (ret < 0) {
1997 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1998 goto err;
1999 }
2000 }
2001
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002002 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
2003 if (ret < 0) {
2004 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2005 ctx->workbuf.size);
2006 goto err;
2007 }
2008
2009 return 0;
2010
2011err:
2012 coda_free_context_buffers(ctx);
2013 return ret;
2014}
2015
Philipp Zabel918c66f2013-06-27 06:59:01 -03002016static int coda_start_decoding(struct coda_ctx *ctx)
2017{
2018 struct coda_q_data *q_data_src, *q_data_dst;
2019 u32 bitstream_buf, bitstream_size;
2020 struct coda_dev *dev = ctx->dev;
2021 int width, height;
2022 u32 src_fourcc;
2023 u32 val;
2024 int ret;
2025
2026 /* Start decoding */
2027 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2028 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2029 bitstream_buf = ctx->bitstream.paddr;
2030 bitstream_size = ctx->bitstream.size;
2031 src_fourcc = q_data_src->fourcc;
2032
2033 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2034
2035 /* Update coda bitstream read and write pointers from kfifo */
2036 coda_kfifo_sync_to_device_full(ctx);
2037
2038 ctx->display_idx = -1;
2039 ctx->frm_dis_flg = 0;
2040 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2041
2042 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2043 CODA_REG_BIT_BIT_STREAM_PARAM);
2044
2045 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2046 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2047 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002048 if ((dev->devtype->product == CODA_7541) ||
2049 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002050 val |= CODA_REORDER_ENABLE;
2051 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2052
2053 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002054 if (dev->devtype->product == CODA_960 &&
2055 src_fourcc == V4L2_PIX_FMT_MPEG4)
2056 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2057 else
2058 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002059 if (src_fourcc == V4L2_PIX_FMT_H264) {
2060 if (dev->devtype->product == CODA_7541) {
2061 coda_write(dev, ctx->psbuf.paddr,
2062 CODA_CMD_DEC_SEQ_PS_BB_START);
2063 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2064 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2065 }
Philipp Zabel89548442014-07-11 06:36:17 -03002066 if (dev->devtype->product == CODA_960) {
2067 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2068 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2069 }
2070 }
2071 if (dev->devtype->product != CODA_960) {
2072 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002073 }
2074
2075 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2076 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2077 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2078 return -ETIMEDOUT;
2079 }
2080
2081 /* Update kfifo out pointer from coda bitstream read pointer */
2082 coda_kfifo_sync_from_device(ctx);
2083
2084 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2085
2086 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2087 v4l2_err(&dev->v4l2_dev,
2088 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2089 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2090 return -EAGAIN;
2091 }
2092
2093 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2094 if (dev->devtype->product == CODA_DX6) {
2095 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2096 height = val & CODADX6_PICHEIGHT_MASK;
2097 } else {
2098 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2099 height = val & CODA7_PICHEIGHT_MASK;
2100 }
2101
2102 if (width > q_data_dst->width || height > q_data_dst->height) {
2103 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2104 width, height, q_data_dst->width, q_data_dst->height);
2105 return -EINVAL;
2106 }
2107
2108 width = round_up(width, 16);
2109 height = round_up(height, 16);
2110
2111 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2112 __func__, ctx->idx, width, height);
2113
Philipp Zabela500a932014-07-11 06:36:13 -03002114 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002115 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2116 v4l2_err(&dev->v4l2_dev,
2117 "not enough framebuffers to decode (%d < %d)\n",
2118 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2119 return -EINVAL;
2120 }
2121
Philipp Zabel52c41672014-07-11 06:36:19 -03002122 if (src_fourcc == V4L2_PIX_FMT_H264) {
2123 u32 left_right;
2124 u32 top_bottom;
2125
2126 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2127 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2128
2129 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2130 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2131 q_data_dst->rect.width = width - q_data_dst->rect.left -
2132 (left_right & 0x3ff);
2133 q_data_dst->rect.height = height - q_data_dst->rect.top -
2134 (top_bottom & 0x3ff);
2135 }
2136
Philipp Zabel918c66f2013-06-27 06:59:01 -03002137 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2138 if (ret < 0)
2139 return ret;
2140
2141 /* Tell the decoder how many frame buffers we allocated. */
2142 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2143 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2144
2145 if (dev->devtype->product != CODA_DX6) {
2146 /* Set secondary AXI IRAM */
2147 coda_setup_iram(ctx);
2148
2149 coda_write(dev, ctx->iram_info.buf_bit_use,
2150 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2151 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2152 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2153 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2154 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2155 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2156 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2157 coda_write(dev, ctx->iram_info.buf_ovl_use,
2158 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002159 if (dev->devtype->product == CODA_960)
2160 coda_write(dev, ctx->iram_info.buf_btp_use,
2161 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2162 }
2163
2164 if (dev->devtype->product == CODA_960) {
2165 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2166
2167 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2168 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2169 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2170 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2171 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2172 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002173 }
2174
2175 if (src_fourcc == V4L2_PIX_FMT_H264) {
2176 coda_write(dev, ctx->slicebuf.paddr,
2177 CODA_CMD_SET_FRAME_SLICE_BB_START);
2178 coda_write(dev, ctx->slicebuf.size / 1024,
2179 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2180 }
2181
2182 if (dev->devtype->product == CODA_7541) {
2183 int max_mb_x = 1920 / 16;
2184 int max_mb_y = 1088 / 16;
2185 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002186
Philipp Zabel918c66f2013-06-27 06:59:01 -03002187 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2188 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002189 } else if (dev->devtype->product == CODA_960) {
2190 int max_mb_x = 1920 / 16;
2191 int max_mb_y = 1088 / 16;
2192 int max_mb_num = max_mb_x * max_mb_y;
2193
2194 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2195 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002196 }
2197
2198 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2199 v4l2_err(&ctx->dev->v4l2_dev,
2200 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2201 return -ETIMEDOUT;
2202 }
2203
2204 return 0;
2205}
2206
Philipp Zabeld35167a2013-05-23 10:42:59 -03002207static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2208 int header_code, u8 *header, int *size)
2209{
2210 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002211 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002212 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002213 int i;
2214
2215 if (dev->devtype->product == CODA_960)
2216 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002217
2218 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2219 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002220 bufsize = vb2_plane_size(buf, 0);
2221 if (dev->devtype->product == CODA_960)
2222 bufsize /= 1024;
2223 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002224 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2225 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2226 if (ret < 0) {
2227 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2228 return ret;
2229 }
Philipp Zabel89548442014-07-11 06:36:17 -03002230
2231 if (dev->devtype->product == CODA_960) {
2232 for (i = 63; i > 0; i--)
2233 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2234 break;
2235 *size = i + 1;
2236 } else {
2237 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2238 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2239 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002240 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2241
2242 return 0;
2243}
2244
Philipp Zabel89548442014-07-11 06:36:17 -03002245static int coda_start_encoding(struct coda_ctx *ctx);
2246
Javier Martin186b2502012-07-26 05:53:35 -03002247static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2248{
2249 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2250 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002251 struct coda_dev *dev = ctx->dev;
2252 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002253 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002254 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002255
Philipp Zabelb96904e2013-05-23 10:42:58 -03002256 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002257 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2258 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2259 if (coda_get_bitstream_payload(ctx) < 512)
2260 return -EINVAL;
2261 } else {
2262 if (count < 1)
2263 return -EINVAL;
2264 }
2265
2266 ctx->streamon_out = 1;
2267
Philipp Zabelb96904e2013-05-23 10:42:58 -03002268 if (coda_format_is_yuv(q_data_src->fourcc))
2269 ctx->inst_type = CODA_INST_ENCODER;
2270 else
2271 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002272 } else {
2273 if (count < 1)
2274 return -EINVAL;
2275
2276 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002277 }
Javier Martin186b2502012-07-26 05:53:35 -03002278
Philipp Zabelb96904e2013-05-23 10:42:58 -03002279 /* Don't start the coda unless both queues are on */
2280 if (!(ctx->streamon_out & ctx->streamon_cap))
2281 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002282
Philipp Zabeleb107512013-09-30 10:34:45 -03002283 /* Allow decoder device_run with no new buffers queued */
2284 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002285 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002286
Philipp Zabelb96904e2013-05-23 10:42:58 -03002287 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002288 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002289 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002290
Philipp Zabelb96904e2013-05-23 10:42:58 -03002291 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2292 q_data_dst->fourcc);
2293 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002294 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2295 return -EINVAL;
2296 }
2297
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002298 /* Allocate per-instance buffers */
2299 ret = coda_alloc_context_buffers(ctx, q_data_src);
2300 if (ret < 0)
2301 return ret;
2302
Philipp Zabel918c66f2013-06-27 06:59:01 -03002303 if (ctx->inst_type == CODA_INST_DECODER) {
2304 mutex_lock(&dev->coda_mutex);
2305 ret = coda_start_decoding(ctx);
2306 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002307 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002308 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002309 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002310 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002311 } else {
2312 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002313 }
2314
Philipp Zabel89548442014-07-11 06:36:17 -03002315 ctx->initialized = 1;
2316 return ret;
2317}
2318
2319static int coda_start_encoding(struct coda_ctx *ctx)
2320{
2321 struct coda_dev *dev = ctx->dev;
2322 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2323 struct coda_q_data *q_data_src, *q_data_dst;
2324 u32 bitstream_buf, bitstream_size;
2325 struct vb2_buffer *buf;
2326 int gamma, ret, value;
2327 u32 dst_fourcc;
2328
2329 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2330 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2331 dst_fourcc = q_data_dst->fourcc;
2332
Philipp Zabel14604e32014-07-11 06:36:22 -03002333 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002334 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2335 bitstream_size = q_data_dst->sizeimage;
2336
Javier Martin186b2502012-07-26 05:53:35 -03002337 if (!coda_is_initialized(dev)) {
2338 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2339 return -EFAULT;
2340 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002341
2342 mutex_lock(&dev->coda_mutex);
2343
Javier Martin186b2502012-07-26 05:53:35 -03002344 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002345 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2346 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002347 switch (dev->devtype->product) {
2348 case CODA_DX6:
2349 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2350 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2351 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002352 case CODA_960:
2353 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2354 /* fallthrough */
2355 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002356 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2357 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002358 break;
Javier Martin186b2502012-07-26 05:53:35 -03002359 }
2360
Philipp Zabel89548442014-07-11 06:36:17 -03002361 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2362 value &= ~(1 << 2 | 0x7 << 9);
2363 ctx->frame_mem_ctrl = value;
2364 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2365
Philipp Zabel10436672012-07-02 09:03:55 -03002366 if (dev->devtype->product == CODA_DX6) {
2367 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002368 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002369 }
Javier Martin186b2502012-07-26 05:53:35 -03002370
2371 /* Could set rotation here if needed */
2372 switch (dev->devtype->product) {
2373 case CODA_DX6:
2374 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002375 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002376 break;
2377 default:
2378 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002379 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002380 }
Javier Martin186b2502012-07-26 05:53:35 -03002381 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2382 coda_write(dev, ctx->params.framerate,
2383 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2384
Philipp Zabelb96904e2013-05-23 10:42:58 -03002385 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002386 switch (dst_fourcc) {
2387 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002388 if (dev->devtype->product == CODA_960)
2389 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2390 else
2391 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002392 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2393 break;
2394 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002395 if (dev->devtype->product == CODA_960)
2396 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2397 else
2398 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002399 if (ctx->params.h264_deblk_enabled) {
2400 value = ((ctx->params.h264_deblk_alpha &
2401 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2402 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2403 ((ctx->params.h264_deblk_beta &
2404 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2405 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2406 } else {
2407 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2408 }
2409 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002410 break;
2411 default:
2412 v4l2_err(v4l2_dev,
2413 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002414 ret = -EINVAL;
2415 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002416 }
2417
Philipp Zabelc566c782012-08-08 11:59:38 -03002418 switch (ctx->params.slice_mode) {
2419 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2420 value = 0;
2421 break;
2422 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2423 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2424 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002425 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002426 break;
2427 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2428 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2429 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2430 value |= 1 & CODA_SLICING_MODE_MASK;
2431 break;
2432 }
Javier Martin186b2502012-07-26 05:53:35 -03002433 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002434 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002435 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2436
2437 if (ctx->params.bitrate) {
2438 /* Rate control enabled */
2439 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2440 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002441 if (dev->devtype->product == CODA_960)
2442 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002443 } else {
2444 value = 0;
2445 }
2446 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2447
2448 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002449 coda_write(dev, ctx->params.intra_refresh,
2450 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002451
2452 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2453 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2454
Javier Martin186b2502012-07-26 05:53:35 -03002455
Philipp Zabel89548442014-07-11 06:36:17 -03002456 value = 0;
2457 if (dev->devtype->product == CODA_960)
2458 gamma = CODA9_DEFAULT_GAMMA;
2459 else
2460 gamma = CODA_DEFAULT_GAMMA;
2461 if (gamma > 0) {
2462 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2463 CODA_CMD_ENC_SEQ_RC_GAMMA);
2464 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002465
2466 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2467 coda_write(dev,
2468 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2469 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2470 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2471 }
Philipp Zabel89548442014-07-11 06:36:17 -03002472 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002473 if (ctx->params.h264_max_qp)
2474 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002475 if (CODA_DEFAULT_GAMMA > 0)
2476 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002477 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002478 if (CODA_DEFAULT_GAMMA > 0) {
2479 if (dev->devtype->product == CODA_DX6)
2480 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2481 else
2482 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2483 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002484 if (ctx->params.h264_min_qp)
2485 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2486 if (ctx->params.h264_max_qp)
2487 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002488 }
Javier Martin186b2502012-07-26 05:53:35 -03002489 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2490
Philipp Zabel89548442014-07-11 06:36:17 -03002491 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2492
Philipp Zabelc2d22512013-06-21 03:55:28 -03002493 coda_setup_iram(ctx);
2494
Javier Martin186b2502012-07-26 05:53:35 -03002495 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002496 switch (dev->devtype->product) {
2497 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002498 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002499 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002500 break;
2501 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002502 coda_write(dev, ctx->iram_info.search_ram_paddr,
2503 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2504 coda_write(dev, ctx->iram_info.search_ram_size,
2505 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002506 break;
2507 case CODA_960:
2508 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2509 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002510 }
Javier Martin186b2502012-07-26 05:53:35 -03002511 }
2512
Philipp Zabelfcb62822013-05-23 10:43:00 -03002513 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2514 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002515 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002516 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002517 }
2518
Philipp Zabelfcb62822013-05-23 10:43:00 -03002519 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2520 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2521 ret = -EFAULT;
2522 goto out;
2523 }
Javier Martin186b2502012-07-26 05:53:35 -03002524
Philipp Zabel89548442014-07-11 06:36:17 -03002525 if (dev->devtype->product == CODA_960)
2526 ctx->num_internal_frames = 4;
2527 else
2528 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002529 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002530 if (ret < 0) {
2531 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2532 goto out;
2533 }
Javier Martin186b2502012-07-26 05:53:35 -03002534
Philipp Zabelec25f682012-07-20 08:54:29 -03002535 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002536 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002537 if (dev->devtype->product == CODA_7541)
2538 coda_write(dev, round_up(q_data_src->width, 8),
2539 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002540 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002541 coda_write(dev, ctx->iram_info.buf_bit_use,
2542 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2543 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2544 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2545 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2546 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2547 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2548 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2549 coda_write(dev, ctx->iram_info.buf_ovl_use,
2550 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002551 if (dev->devtype->product == CODA_960) {
2552 coda_write(dev, ctx->iram_info.buf_btp_use,
2553 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2554
2555 /* FIXME */
2556 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2557 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2558 }
Philipp Zabel10436672012-07-02 09:03:55 -03002559 }
Philipp Zabel89548442014-07-11 06:36:17 -03002560
Philipp Zabelfcb62822013-05-23 10:43:00 -03002561 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2562 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002563 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002564 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002565 }
2566
2567 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002568 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002569 switch (dst_fourcc) {
2570 case V4L2_PIX_FMT_H264:
2571 /*
2572 * Get SPS in the first frame and copy it to an
2573 * intermediate buffer.
2574 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002575 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2576 &ctx->vpu_header[0][0],
2577 &ctx->vpu_header_size[0]);
2578 if (ret < 0)
2579 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002580
2581 /*
2582 * Get PPS in the first frame and copy it to an
2583 * intermediate buffer.
2584 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002585 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2586 &ctx->vpu_header[1][0],
2587 &ctx->vpu_header_size[1]);
2588 if (ret < 0)
2589 goto out;
2590
Javier Martin3f3f5c72012-10-29 05:20:29 -03002591 /*
2592 * Length of H.264 headers is variable and thus it might not be
2593 * aligned for the coda to append the encoded frame. In that is
2594 * the case a filler NAL must be added to header 2.
2595 */
2596 ctx->vpu_header_size[2] = coda_h264_padding(
2597 (ctx->vpu_header_size[0] +
2598 ctx->vpu_header_size[1]),
2599 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002600 break;
2601 case V4L2_PIX_FMT_MPEG4:
2602 /*
2603 * Get VOS in the first frame and copy it to an
2604 * intermediate buffer
2605 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002606 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2607 &ctx->vpu_header[0][0],
2608 &ctx->vpu_header_size[0]);
2609 if (ret < 0)
2610 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002611
Philipp Zabeld35167a2013-05-23 10:42:59 -03002612 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2613 &ctx->vpu_header[1][0],
2614 &ctx->vpu_header_size[1]);
2615 if (ret < 0)
2616 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002617
Philipp Zabeld35167a2013-05-23 10:42:59 -03002618 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2619 &ctx->vpu_header[2][0],
2620 &ctx->vpu_header_size[2]);
2621 if (ret < 0)
2622 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002623 break;
2624 default:
2625 /* No more formats need to save headers at the moment */
2626 break;
2627 }
2628
Philipp Zabeld35167a2013-05-23 10:42:59 -03002629out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002630 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002631 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002632}
2633
Hans Verkuile37559b2014-04-17 02:47:21 -03002634static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002635{
2636 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002637 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002638
2639 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002640 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002641 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002642 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002643
Philipp Zabel89548442014-07-11 06:36:17 -03002644 if (ctx->inst_type == CODA_INST_DECODER &&
2645 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2646 /* if this decoder instance is running, set the stream end flag */
2647 if (dev->devtype->product == CODA_960) {
2648 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2649
2650 val |= CODA_BIT_STREAM_END_FLAG;
2651 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2652 ctx->bit_stream_param = val;
2653 }
2654 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002655 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2656
2657 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002658 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002659 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002660 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002661 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002662
2663 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002664 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002665 }
2666
Philipp Zabel918c66f2013-06-27 06:59:01 -03002667 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002668 struct coda_timestamp *ts;
2669
2670 while (!list_empty(&ctx->timestamp_list)) {
2671 ts = list_first_entry(&ctx->timestamp_list,
2672 struct coda_timestamp, list);
2673 list_del(&ts->list);
2674 kfree(ts);
2675 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002676 kfifo_init(&ctx->bitstream_fifo,
2677 ctx->bitstream.vaddr, ctx->bitstream.size);
2678 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002679 }
Javier Martin186b2502012-07-26 05:53:35 -03002680}
2681
2682static struct vb2_ops coda_qops = {
2683 .queue_setup = coda_queue_setup,
2684 .buf_prepare = coda_buf_prepare,
2685 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002686 .start_streaming = coda_start_streaming,
2687 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002688 .wait_prepare = vb2_ops_wait_prepare,
2689 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002690};
2691
2692static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2693{
2694 struct coda_ctx *ctx =
2695 container_of(ctrl->handler, struct coda_ctx, ctrls);
2696
2697 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2698 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2699
2700 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002701 case V4L2_CID_HFLIP:
2702 if (ctrl->val)
2703 ctx->params.rot_mode |= CODA_MIR_HOR;
2704 else
2705 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2706 break;
2707 case V4L2_CID_VFLIP:
2708 if (ctrl->val)
2709 ctx->params.rot_mode |= CODA_MIR_VER;
2710 else
2711 ctx->params.rot_mode &= ~CODA_MIR_VER;
2712 break;
Javier Martin186b2502012-07-26 05:53:35 -03002713 case V4L2_CID_MPEG_VIDEO_BITRATE:
2714 ctx->params.bitrate = ctrl->val / 1000;
2715 break;
2716 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2717 ctx->params.gop_size = ctrl->val;
2718 break;
2719 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2720 ctx->params.h264_intra_qp = ctrl->val;
2721 break;
2722 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2723 ctx->params.h264_inter_qp = ctrl->val;
2724 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002725 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2726 ctx->params.h264_min_qp = ctrl->val;
2727 break;
2728 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2729 ctx->params.h264_max_qp = ctrl->val;
2730 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002731 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2732 ctx->params.h264_deblk_alpha = ctrl->val;
2733 break;
2734 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2735 ctx->params.h264_deblk_beta = ctrl->val;
2736 break;
2737 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2738 ctx->params.h264_deblk_enabled = (ctrl->val ==
2739 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2740 break;
Javier Martin186b2502012-07-26 05:53:35 -03002741 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2742 ctx->params.mpeg4_intra_qp = ctrl->val;
2743 break;
2744 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2745 ctx->params.mpeg4_inter_qp = ctrl->val;
2746 break;
2747 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2748 ctx->params.slice_mode = ctrl->val;
2749 break;
2750 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2751 ctx->params.slice_max_mb = ctrl->val;
2752 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002753 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2754 ctx->params.slice_max_bits = ctrl->val * 8;
2755 break;
Javier Martin186b2502012-07-26 05:53:35 -03002756 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2757 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002758 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2759 ctx->params.intra_refresh = ctrl->val;
2760 break;
Javier Martin186b2502012-07-26 05:53:35 -03002761 default:
2762 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2763 "Invalid control, id=%d, val=%d\n",
2764 ctrl->id, ctrl->val);
2765 return -EINVAL;
2766 }
2767
2768 return 0;
2769}
2770
2771static struct v4l2_ctrl_ops coda_ctrl_ops = {
2772 .s_ctrl = coda_s_ctrl,
2773};
2774
2775static int coda_ctrls_setup(struct coda_ctx *ctx)
2776{
2777 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2778
2779 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002780 V4L2_CID_HFLIP, 0, 1, 1, 0);
2781 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2782 V4L2_CID_VFLIP, 0, 1, 1, 0);
2783 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002784 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2785 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2786 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2787 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002788 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002789 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002790 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002791 if (ctx->dev->devtype->product != CODA_960) {
2792 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2793 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2794 }
2795 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2796 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002797 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002798 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2799 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2800 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2801 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2802 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2803 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2804 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2805 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002806 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2807 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2808 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2809 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2810 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002811 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2812 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002813 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2814 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002815 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2816 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002817 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2818 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2819 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2820 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2821 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002822 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2823 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002824
2825 if (ctx->ctrls.error) {
2826 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2827 ctx->ctrls.error);
2828 return -EINVAL;
2829 }
2830
2831 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2832}
2833
2834static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2835 struct vb2_queue *dst_vq)
2836{
2837 struct coda_ctx *ctx = priv;
2838 int ret;
2839
Javier Martin186b2502012-07-26 05:53:35 -03002840 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002841 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002842 src_vq->drv_priv = ctx;
2843 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2844 src_vq->ops = &coda_qops;
2845 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002846 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002847 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002848
2849 ret = vb2_queue_init(src_vq);
2850 if (ret)
2851 return ret;
2852
Javier Martin186b2502012-07-26 05:53:35 -03002853 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002854 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002855 dst_vq->drv_priv = ctx;
2856 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2857 dst_vq->ops = &coda_qops;
2858 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002859 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002860 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002861
2862 return vb2_queue_init(dst_vq);
2863}
2864
Philipp Zabele11f3e62012-07-25 09:16:58 -03002865static int coda_next_free_instance(struct coda_dev *dev)
2866{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002867 int idx = ffz(dev->instance_mask);
2868
2869 if ((idx < 0) ||
2870 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2871 return -EBUSY;
2872
2873 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002874}
2875
Javier Martin186b2502012-07-26 05:53:35 -03002876static int coda_open(struct file *file)
2877{
2878 struct coda_dev *dev = video_drvdata(file);
2879 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002880 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002881 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002882
Javier Martin186b2502012-07-26 05:53:35 -03002883 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2884 if (!ctx)
2885 return -ENOMEM;
2886
Fabio Estevamf82bc202013-08-21 11:14:16 -03002887 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002888 if (idx < 0) {
2889 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002890 goto err_coda_max;
2891 }
2892 set_bit(idx, &dev->instance_mask);
2893
Philipp Zabel32b6f202014-07-11 06:36:20 -03002894 init_completion(&ctx->completion);
2895 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2896 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002897 v4l2_fh_init(&ctx->fh, video_devdata(file));
2898 file->private_data = &ctx->fh;
2899 v4l2_fh_add(&ctx->fh);
2900 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002901 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002902 switch (dev->devtype->product) {
2903 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002904 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002905 ctx->reg_idx = 0;
2906 break;
2907 default:
2908 ctx->reg_idx = idx;
2909 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002910
Philipp Zabel1e172732014-07-11 06:36:23 -03002911 /* Power up and upload firmware if necessary */
2912 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2913 if (ret < 0) {
2914 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2915 goto err_pm_get;
2916 }
2917
Fabio Estevam79830db2013-08-21 11:14:17 -03002918 ret = clk_prepare_enable(dev->clk_per);
2919 if (ret)
2920 goto err_clk_per;
2921
2922 ret = clk_prepare_enable(dev->clk_ahb);
2923 if (ret)
2924 goto err_clk_ahb;
2925
Javier Martin186b2502012-07-26 05:53:35 -03002926 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002927 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002928 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002929 if (IS_ERR(ctx->fh.m2m_ctx)) {
2930 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002931
2932 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2933 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002934 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002935 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002936
Javier Martin186b2502012-07-26 05:53:35 -03002937 ret = coda_ctrls_setup(ctx);
2938 if (ret) {
2939 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002940 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002941 }
2942
2943 ctx->fh.ctrl_handler = &ctx->ctrls;
2944
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002945 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2946 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002947 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002948 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002949 }
2950
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002951 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2952 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2953 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2954 if (!ctx->bitstream.vaddr) {
2955 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2956 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002957 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002958 }
2959 kfifo_init(&ctx->bitstream_fifo,
2960 ctx->bitstream.vaddr, ctx->bitstream.size);
2961 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002962 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002963 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002964
Javier Martin186b2502012-07-26 05:53:35 -03002965 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002966 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002967 coda_unlock(ctx);
2968
Javier Martin186b2502012-07-26 05:53:35 -03002969 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2970 ctx->idx, ctx);
2971
2972 return 0;
2973
Fabio Estevamf82bc202013-08-21 11:14:16 -03002974err_dma_writecombine:
2975 coda_free_context_buffers(ctx);
2976 if (ctx->dev->devtype->product == CODA_DX6)
2977 coda_free_aux_buf(dev, &ctx->workbuf);
2978 coda_free_aux_buf(dev, &ctx->parabuf);
2979err_dma_alloc:
2980 v4l2_ctrl_handler_free(&ctx->ctrls);
2981err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002982 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002983err_ctx_init:
2984 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002985err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002986 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002987err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002988 pm_runtime_put_sync(&dev->plat_dev->dev);
2989err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002990 v4l2_fh_del(&ctx->fh);
2991 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002992 clear_bit(ctx->idx, &dev->instance_mask);
2993err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002994 kfree(ctx);
2995 return ret;
2996}
2997
2998static int coda_release(struct file *file)
2999{
3000 struct coda_dev *dev = video_drvdata(file);
3001 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3002
3003 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3004 ctx);
3005
Philipp Zabel918c66f2013-06-27 06:59:01 -03003006 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003007 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003008
3009 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003010 if (ctx->initialized) {
3011 queue_work(dev->workqueue, &ctx->seq_end_work);
3012 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003013 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003014
3015 coda_free_framebuffers(ctx);
3016
Javier Martin186b2502012-07-26 05:53:35 -03003017 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003018 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003019 coda_unlock(ctx);
3020
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003021 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3022 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003023 coda_free_context_buffers(ctx);
3024 if (ctx->dev->devtype->product == CODA_DX6)
3025 coda_free_aux_buf(dev, &ctx->workbuf);
3026
3027 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003028 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003029 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003030 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003031 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003032 v4l2_fh_del(&ctx->fh);
3033 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003034 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003035 kfree(ctx);
3036
3037 return 0;
3038}
3039
Javier Martin186b2502012-07-26 05:53:35 -03003040static const struct v4l2_file_operations coda_fops = {
3041 .owner = THIS_MODULE,
3042 .open = coda_open,
3043 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003044 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003045 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003046 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003047};
3048
Philipp Zabel918c66f2013-06-27 06:59:01 -03003049static void coda_finish_decode(struct coda_ctx *ctx)
3050{
3051 struct coda_dev *dev = ctx->dev;
3052 struct coda_q_data *q_data_src;
3053 struct coda_q_data *q_data_dst;
3054 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003055 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003056 int width, height;
3057 int decoded_idx;
3058 int display_idx;
3059 u32 src_fourcc;
3060 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003061 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003062 u32 val;
3063
Philipp Zabel14604e32014-07-11 06:36:22 -03003064 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003065
3066 /* Update kfifo out pointer from coda bitstream read pointer */
3067 coda_kfifo_sync_from_device(ctx);
3068
3069 /*
3070 * in stream-end mode, the read pointer can overshoot the write pointer
3071 * by up to 512 bytes
3072 */
3073 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3074 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3075 kfifo_init(&ctx->bitstream_fifo,
3076 ctx->bitstream.vaddr, ctx->bitstream.size);
3077 }
3078
3079 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3080 src_fourcc = q_data_src->fourcc;
3081
3082 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3083 if (val != 1)
3084 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3085
3086 success = val & 0x1;
3087 if (!success)
3088 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3089
3090 if (src_fourcc == V4L2_PIX_FMT_H264) {
3091 if (val & (1 << 3))
3092 v4l2_err(&dev->v4l2_dev,
3093 "insufficient PS buffer space (%d bytes)\n",
3094 ctx->psbuf.size);
3095 if (val & (1 << 2))
3096 v4l2_err(&dev->v4l2_dev,
3097 "insufficient slice buffer space (%d bytes)\n",
3098 ctx->slicebuf.size);
3099 }
3100
3101 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3102 width = (val >> 16) & 0xffff;
3103 height = val & 0xffff;
3104
3105 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3106
Philipp Zabel52c41672014-07-11 06:36:19 -03003107 /* frame crop information */
3108 if (src_fourcc == V4L2_PIX_FMT_H264) {
3109 u32 left_right;
3110 u32 top_bottom;
3111
3112 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3113 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3114
3115 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3116 /* Keep current crop information */
3117 } else {
3118 struct v4l2_rect *rect = &q_data_dst->rect;
3119
3120 rect->left = left_right >> 16 & 0xffff;
3121 rect->top = top_bottom >> 16 & 0xffff;
3122 rect->width = width - rect->left -
3123 (left_right & 0xffff);
3124 rect->height = height - rect->top -
3125 (top_bottom & 0xffff);
3126 }
3127 } else {
3128 /* no cropping */
3129 }
3130
Philipp Zabel410e5e42014-07-11 06:36:32 -03003131 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3132 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003133 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003134 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003135
3136 if (dev->devtype->product == CODA_7541) {
3137 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3138 if (val == 0) {
3139 /* not enough bitstream data */
3140 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3141 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003142 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003143 return;
3144 }
3145 }
3146
3147 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3148
3149 /*
3150 * The previous display frame was copied out by the rotator,
3151 * now it can be overwritten again
3152 */
3153 if (ctx->display_idx >= 0 &&
3154 ctx->display_idx < ctx->num_internal_frames) {
3155 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3156 coda_write(dev, ctx->frm_dis_flg,
3157 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3158 }
3159
3160 /*
3161 * The index of the last decoded frame, not necessarily in
3162 * display order, and the index of the next display frame.
3163 * The latter could have been decoded in a previous run.
3164 */
3165 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3166 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3167
3168 if (decoded_idx == -1) {
3169 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003170 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3171 ctx->sequence_offset++;
3172 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003173 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003174 } else if (decoded_idx == -2) {
3175 /* no frame was decoded, we still return the remaining buffers */
3176 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3177 v4l2_err(&dev->v4l2_dev,
3178 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003179 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003180 ts = list_first_entry(&ctx->timestamp_list,
3181 struct coda_timestamp, list);
3182 list_del(&ts->list);
3183 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003184 val -= ctx->sequence_offset;
3185 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003186 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003187 "sequence number mismatch (%d(%d) != %d)\n",
3188 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003189 }
3190 ctx->frame_timestamps[decoded_idx] = *ts;
3191 kfree(ts);
3192
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003193 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3194 if (val == 0)
3195 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3196 else if (val == 1)
3197 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3198 else
3199 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003200
3201 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003202 }
3203
3204 if (display_idx == -1) {
3205 /*
3206 * no more frames to be decoded, but there could still
3207 * be rotator output to dequeue
3208 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003209 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003210 } else if (display_idx == -3) {
3211 /* possibly prescan failure */
3212 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3213 v4l2_err(&dev->v4l2_dev,
3214 "presentation frame index out of range: %d\n",
3215 display_idx);
3216 }
3217
3218 /* If a frame was copied out, return it */
3219 if (ctx->display_idx >= 0 &&
3220 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003221 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003222 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3223
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003224 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3225 V4L2_BUF_FLAG_PFRAME |
3226 V4L2_BUF_FLAG_BFRAME);
3227 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003228 ts = &ctx->frame_timestamps[ctx->display_idx];
3229 dst_buf->v4l2_buf.timecode = ts->timecode;
3230 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003231
Philipp Zabel918c66f2013-06-27 06:59:01 -03003232 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3233
Philipp Zabel410e5e42014-07-11 06:36:32 -03003234 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3235 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003236
3237 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3238 "job finished: decoding frame (%d) (%s)\n",
3239 dst_buf->v4l2_buf.sequence,
3240 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3241 "KEYFRAME" : "PFRAME");
3242 } else {
3243 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3244 "job finished: no frame decoded\n");
3245 }
3246
3247 /* The rotator will copy the current display frame next time */
3248 ctx->display_idx = display_idx;
3249}
3250
3251static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003252{
Philipp Zabelec25f682012-07-20 08:54:29 -03003253 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003254 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003255 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003256
Philipp Zabel14604e32014-07-11 06:36:22 -03003257 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3258 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003259
3260 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003261 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003262 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3263
Javier Martin186b2502012-07-26 05:53:35 -03003264 /* Calculate bytesused field */
3265 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003266 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3267 ctx->vpu_header_size[0] +
3268 ctx->vpu_header_size[1] +
3269 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003270 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003271 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003272 }
3273
3274 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3275 wr_ptr - start_ptr);
3276
3277 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3278 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3279
Philipp Zabel51660282013-09-30 10:34:49 -03003280 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003281 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3282 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3283 } else {
3284 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3285 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3286 }
3287
Kamil Debskiccd571c2013-04-24 10:38:24 -03003288 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003289 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3290 dst_buf->v4l2_buf.flags |=
3291 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003292 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3293
Philipp Zabelec25f682012-07-20 08:54:29 -03003294 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003295
Philipp Zabel14604e32014-07-11 06:36:22 -03003296 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003297 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3298
3299 ctx->gopcounter--;
3300 if (ctx->gopcounter < 0)
3301 ctx->gopcounter = ctx->params.gop_size - 1;
3302
3303 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3304 "job finished: encoding frame (%d) (%s)\n",
3305 dst_buf->v4l2_buf.sequence,
3306 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3307 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003308}
3309
3310static irqreturn_t coda_irq_handler(int irq, void *data)
3311{
3312 struct coda_dev *dev = data;
3313 struct coda_ctx *ctx;
3314
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003315 /* read status register to attend the IRQ */
3316 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3317 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3318 CODA_REG_BIT_INT_CLEAR);
3319
3320 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3321 if (ctx == NULL) {
3322 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3323 mutex_unlock(&dev->coda_mutex);
3324 return IRQ_HANDLED;
3325 }
3326
3327 if (ctx->aborting) {
3328 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3329 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003330 }
3331
3332 if (coda_isbusy(ctx->dev)) {
3333 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3334 "coda is still busy!!!!\n");
3335 return IRQ_NONE;
3336 }
3337
Philipp Zabel32b6f202014-07-11 06:36:20 -03003338 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003339
3340 return IRQ_HANDLED;
3341}
3342
3343static u32 coda_supported_firmwares[] = {
3344 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003345 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003346 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003347};
3348
3349static bool coda_firmware_supported(u32 vernum)
3350{
3351 int i;
3352
3353 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3354 if (vernum == coda_supported_firmwares[i])
3355 return true;
3356 return false;
3357}
3358
Philipp Zabel87048bb2012-07-02 07:03:43 -03003359static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003360{
Javier Martin186b2502012-07-26 05:53:35 -03003361 u32 data;
3362 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003363 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003364
Fabio Estevam79830db2013-08-21 11:14:17 -03003365 ret = clk_prepare_enable(dev->clk_per);
3366 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003367 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003368
3369 ret = clk_prepare_enable(dev->clk_ahb);
3370 if (ret)
3371 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003372
Philipp Zabel8f452842014-07-11 06:36:35 -03003373 if (dev->rstc)
3374 reset_control_reset(dev->rstc);
3375
Javier Martin186b2502012-07-26 05:53:35 -03003376 /*
3377 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003378 * The 16-bit chars in the code buffer are in memory access
3379 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003380 * Data in this SRAM survives a reboot.
3381 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003382 p = (u16 *)dev->codebuf.vaddr;
3383 if (dev->devtype->product == CODA_DX6) {
3384 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3385 data = CODA_DOWN_ADDRESS_SET(i) |
3386 CODA_DOWN_DATA_SET(p[i ^ 1]);
3387 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3388 }
3389 } else {
3390 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3391 data = CODA_DOWN_ADDRESS_SET(i) |
3392 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3393 3 - (i % 4)]);
3394 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3395 }
Javier Martin186b2502012-07-26 05:53:35 -03003396 }
Javier Martin186b2502012-07-26 05:53:35 -03003397
Philipp Zabel9acf7692013-05-23 10:42:56 -03003398 /* Clear registers */
3399 for (i = 0; i < 64; i++)
3400 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3401
Javier Martin186b2502012-07-26 05:53:35 -03003402 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003403 if (dev->devtype->product == CODA_960 ||
3404 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003405 coda_write(dev, dev->tempbuf.paddr,
3406 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003407 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003408 } else {
3409 coda_write(dev, dev->workbuf.paddr,
3410 CODA_REG_BIT_WORK_BUF_ADDR);
3411 }
Javier Martin186b2502012-07-26 05:53:35 -03003412 coda_write(dev, dev->codebuf.paddr,
3413 CODA_REG_BIT_CODE_BUF_ADDR);
3414 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3415
3416 /* Set default values */
3417 switch (dev->devtype->product) {
3418 case CODA_DX6:
3419 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3420 break;
3421 default:
3422 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3423 }
Philipp Zabel89548442014-07-11 06:36:17 -03003424 if (dev->devtype->product == CODA_960)
3425 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3426 else
3427 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003428
3429 if (dev->devtype->product != CODA_DX6)
3430 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3431
Javier Martin186b2502012-07-26 05:53:35 -03003432 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3433 CODA_REG_BIT_INT_ENABLE);
3434
3435 /* Reset VPU and start processor */
3436 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3437 data |= CODA_REG_RESET_ENABLE;
3438 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3439 udelay(10);
3440 data &= ~CODA_REG_RESET_ENABLE;
3441 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3442 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3443
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003444 clk_disable_unprepare(dev->clk_ahb);
3445 clk_disable_unprepare(dev->clk_per);
3446
3447 return 0;
3448
3449err_clk_ahb:
3450 clk_disable_unprepare(dev->clk_per);
3451err_clk_per:
3452 return ret;
3453}
3454
3455static int coda_check_firmware(struct coda_dev *dev)
3456{
3457 u16 product, major, minor, release;
3458 u32 data;
3459 int ret;
3460
3461 ret = clk_prepare_enable(dev->clk_per);
3462 if (ret)
3463 goto err_clk_per;
3464
3465 ret = clk_prepare_enable(dev->clk_ahb);
3466 if (ret)
3467 goto err_clk_ahb;
3468
Javier Martin186b2502012-07-26 05:53:35 -03003469 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3470 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3471 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3472 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3473 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3474 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003475 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003476 ret = -EIO;
3477 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003478 }
3479
Philipp Zabel89548442014-07-11 06:36:17 -03003480 if (dev->devtype->product == CODA_960) {
3481 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3482 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3483 data);
3484 }
3485
Javier Martin186b2502012-07-26 05:53:35 -03003486 /* Check we are compatible with the loaded firmware */
3487 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3488 product = CODA_FIRMWARE_PRODUCT(data);
3489 major = CODA_FIRMWARE_MAJOR(data);
3490 minor = CODA_FIRMWARE_MINOR(data);
3491 release = CODA_FIRMWARE_RELEASE(data);
3492
3493 clk_disable_unprepare(dev->clk_per);
3494 clk_disable_unprepare(dev->clk_ahb);
3495
3496 if (product != dev->devtype->product) {
3497 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3498 " Version: %u.%u.%u\n",
3499 coda_product_name(dev->devtype->product),
3500 coda_product_name(product), major, minor, release);
3501 return -EINVAL;
3502 }
3503
3504 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3505 coda_product_name(product));
3506
3507 if (coda_firmware_supported(data)) {
3508 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3509 major, minor, release);
3510 } else {
3511 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3512 "%u.%u.%u\n", major, minor, release);
3513 }
3514
3515 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003516
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003517err_run_cmd:
3518 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003519err_clk_ahb:
3520 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003521err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003522 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003523}
3524
3525static void coda_fw_callback(const struct firmware *fw, void *context)
3526{
3527 struct coda_dev *dev = context;
3528 struct platform_device *pdev = dev->plat_dev;
3529 int ret;
3530
3531 if (!fw) {
3532 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3533 return;
3534 }
3535
3536 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003537 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3538 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003539 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3540 return;
3541 }
3542
Philipp Zabel87048bb2012-07-02 07:03:43 -03003543 /* Copy the whole firmware image to the code buffer */
3544 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3545 release_firmware(fw);
3546
Philipp Zabel1e172732014-07-11 06:36:23 -03003547 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3548 /*
3549 * Enabling power temporarily will cause coda_hw_init to be
3550 * called via coda_runtime_resume by the pm domain.
3551 */
3552 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3553 if (ret < 0) {
3554 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3555 ret);
3556 return;
3557 }
3558
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003559 ret = coda_check_firmware(dev);
3560 if (ret < 0)
3561 return;
3562
Philipp Zabel1e172732014-07-11 06:36:23 -03003563 pm_runtime_put_sync(&dev->plat_dev->dev);
3564 } else {
3565 /*
3566 * If runtime pm is disabled or pm_domain is not set,
3567 * initialize once manually.
3568 */
3569 ret = coda_hw_init(dev);
3570 if (ret < 0) {
3571 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3572 return;
3573 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003574
3575 ret = coda_check_firmware(dev);
3576 if (ret < 0)
3577 return;
Javier Martin186b2502012-07-26 05:53:35 -03003578 }
3579
3580 dev->vfd.fops = &coda_fops,
3581 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3582 dev->vfd.release = video_device_release_empty,
3583 dev->vfd.lock = &dev->dev_mutex;
3584 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003585 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003586 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3587 video_set_drvdata(&dev->vfd, dev);
3588
3589 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3590 if (IS_ERR(dev->alloc_ctx)) {
3591 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3592 return;
3593 }
3594
3595 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3596 if (IS_ERR(dev->m2m_dev)) {
3597 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3598 goto rel_ctx;
3599 }
3600
3601 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3602 if (ret) {
3603 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3604 goto rel_m2m;
3605 }
3606 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3607 dev->vfd.num);
3608
3609 return;
3610
3611rel_m2m:
3612 v4l2_m2m_release(dev->m2m_dev);
3613rel_ctx:
3614 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3615}
3616
3617static int coda_firmware_request(struct coda_dev *dev)
3618{
3619 char *fw = dev->devtype->firmware;
3620
3621 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3622 coda_product_name(dev->devtype->product));
3623
3624 return request_firmware_nowait(THIS_MODULE, true,
3625 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3626}
3627
3628enum coda_platform {
3629 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003630 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003631 CODA_IMX6Q,
3632 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003633};
3634
Emil Goodec06d8752012-08-14 17:44:42 -03003635static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003636 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003637 .firmware = "v4l-codadx6-imx27.bin",
3638 .product = CODA_DX6,
3639 .codecs = codadx6_codecs,
3640 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003641 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003642 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003643 .firmware = "v4l-coda7541-imx53.bin",
3644 .product = CODA_7541,
3645 .codecs = coda7_codecs,
3646 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003647 },
Philipp Zabel89548442014-07-11 06:36:17 -03003648 [CODA_IMX6Q] = {
3649 .firmware = "v4l-coda960-imx6q.bin",
3650 .product = CODA_960,
3651 .codecs = coda9_codecs,
3652 .num_codecs = ARRAY_SIZE(coda9_codecs),
3653 },
3654 [CODA_IMX6DL] = {
3655 .firmware = "v4l-coda960-imx6dl.bin",
3656 .product = CODA_960,
3657 .codecs = coda9_codecs,
3658 .num_codecs = ARRAY_SIZE(coda9_codecs),
3659 },
Javier Martin186b2502012-07-26 05:53:35 -03003660};
3661
3662static struct platform_device_id coda_platform_ids[] = {
3663 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003664 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003665 { /* sentinel */ }
3666};
3667MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3668
3669#ifdef CONFIG_OF
3670static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003671 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003672 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003673 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3674 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003675 { /* sentinel */ }
3676};
3677MODULE_DEVICE_TABLE(of, coda_dt_ids);
3678#endif
3679
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003680static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003681{
3682 const struct of_device_id *of_id =
3683 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3684 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003685 struct coda_platform_data *pdata = pdev->dev.platform_data;
3686 struct device_node *np = pdev->dev.of_node;
3687 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003688 struct coda_dev *dev;
3689 struct resource *res;
3690 int ret, irq;
3691
3692 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3693 if (!dev) {
3694 dev_err(&pdev->dev, "Not enough memory for %s\n",
3695 CODA_NAME);
3696 return -ENOMEM;
3697 }
3698
3699 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003700 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003701
3702 dev->plat_dev = pdev;
3703 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3704 if (IS_ERR(dev->clk_per)) {
3705 dev_err(&pdev->dev, "Could not get per clock\n");
3706 return PTR_ERR(dev->clk_per);
3707 }
3708
3709 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3710 if (IS_ERR(dev->clk_ahb)) {
3711 dev_err(&pdev->dev, "Could not get ahb clock\n");
3712 return PTR_ERR(dev->clk_ahb);
3713 }
3714
3715 /* Get memory for physical registers */
3716 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003717 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3718 if (IS_ERR(dev->regs_base))
3719 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003720
3721 /* IRQ */
3722 irq = platform_get_irq(pdev, 0);
3723 if (irq < 0) {
3724 dev_err(&pdev->dev, "failed to get irq resource\n");
3725 return -ENOENT;
3726 }
3727
Philipp Zabel918c66f2013-06-27 06:59:01 -03003728 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003729 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003730 dev_err(&pdev->dev, "failed to request irq\n");
3731 return -ENOENT;
3732 }
3733
Philipp Zabel8f452842014-07-11 06:36:35 -03003734 dev->rstc = devm_reset_control_get(&pdev->dev, NULL);
3735 if (IS_ERR(dev->rstc)) {
3736 ret = PTR_ERR(dev->rstc);
3737 if (ret == -ENOENT) {
3738 dev->rstc = NULL;
3739 } else {
3740 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3741 return ret;
3742 }
3743 }
3744
Philipp Zabel657eee72013-04-29 16:17:14 -07003745 /* Get IRAM pool from device tree or platform data */
3746 pool = of_get_named_gen_pool(np, "iram", 0);
3747 if (!pool && pdata)
3748 pool = dev_get_gen_pool(pdata->iram_dev);
3749 if (!pool) {
3750 dev_err(&pdev->dev, "iram pool not available\n");
3751 return -ENOMEM;
3752 }
3753 dev->iram_pool = pool;
3754
Javier Martin186b2502012-07-26 05:53:35 -03003755 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3756 if (ret)
3757 return ret;
3758
3759 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003760 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003761
3762 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3763
3764 if (of_id) {
3765 dev->devtype = of_id->data;
3766 } else if (pdev_id) {
3767 dev->devtype = &coda_devdata[pdev_id->driver_data];
3768 } else {
3769 v4l2_device_unregister(&dev->v4l2_dev);
3770 return -EINVAL;
3771 }
3772
3773 /* allocate auxiliary per-device buffers for the BIT processor */
3774 switch (dev->devtype->product) {
3775 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003776 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3777 CODADX6_WORK_BUF_SIZE);
3778 if (ret < 0) {
3779 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3780 v4l2_device_unregister(&dev->v4l2_dev);
3781 return ret;
3782 }
Javier Martin186b2502012-07-26 05:53:35 -03003783 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003784 case CODA_7541:
3785 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3786 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003787 case CODA_960:
3788 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3789 break;
Javier Martin186b2502012-07-26 05:53:35 -03003790 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003791 if (dev->tempbuf.size) {
3792 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3793 dev->tempbuf.size);
3794 if (ret < 0) {
3795 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3796 v4l2_device_unregister(&dev->v4l2_dev);
3797 return ret;
3798 }
Javier Martin186b2502012-07-26 05:53:35 -03003799 }
3800
Philipp Zabel918c66f2013-06-27 06:59:01 -03003801 switch (dev->devtype->product) {
3802 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003803 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003804 break;
3805 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003806 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003807 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003808 case CODA_960:
3809 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003810 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003811 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3812 &dev->iram.paddr);
3813 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003814 dev_err(&pdev->dev, "unable to alloc iram\n");
3815 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003816 }
3817
Philipp Zabel32b6f202014-07-11 06:36:20 -03003818 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3819 if (!dev->workqueue) {
3820 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3821 return -ENOMEM;
3822 }
3823
Javier Martin186b2502012-07-26 05:53:35 -03003824 platform_set_drvdata(pdev, dev);
3825
Philipp Zabel1e172732014-07-11 06:36:23 -03003826 pm_runtime_enable(&pdev->dev);
3827
Javier Martin186b2502012-07-26 05:53:35 -03003828 return coda_firmware_request(dev);
3829}
3830
3831static int coda_remove(struct platform_device *pdev)
3832{
3833 struct coda_dev *dev = platform_get_drvdata(pdev);
3834
3835 video_unregister_device(&dev->vfd);
3836 if (dev->m2m_dev)
3837 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003838 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003839 if (dev->alloc_ctx)
3840 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3841 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003842 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003843 if (dev->iram.vaddr)
3844 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3845 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003846 coda_free_aux_buf(dev, &dev->codebuf);
3847 coda_free_aux_buf(dev, &dev->tempbuf);
3848 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003849 return 0;
3850}
3851
Philipp Zabel1e172732014-07-11 06:36:23 -03003852#ifdef CONFIG_PM_RUNTIME
3853static int coda_runtime_resume(struct device *dev)
3854{
3855 struct coda_dev *cdev = dev_get_drvdata(dev);
3856 int ret = 0;
3857
3858 if (dev->pm_domain) {
3859 ret = coda_hw_init(cdev);
3860 if (ret)
3861 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3862 }
3863
3864 return ret;
3865}
3866#endif
3867
3868static const struct dev_pm_ops coda_pm_ops = {
3869 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3870};
3871
Javier Martin186b2502012-07-26 05:53:35 -03003872static struct platform_driver coda_driver = {
3873 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003874 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003875 .driver = {
3876 .name = CODA_NAME,
3877 .owner = THIS_MODULE,
3878 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003879 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003880 },
3881 .id_table = coda_platform_ids,
3882};
3883
3884module_platform_driver(coda_driver);
3885
3886MODULE_LICENSE("GPL");
3887MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3888MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");