blob: 9493cfdb0ae690faeded66ec55ddfe46ea4784e7 [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>
Philipp Zabelf61c0b12014-07-11 06:36:40 -030015#include <linux/debugfs.h>
Javier Martin186b2502012-07-26 05:53:35 -030016#include <linux/delay.h>
17#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070018#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030022#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030023#include <linux/module.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030026#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030027#include <linux/slab.h>
28#include <linux/videodev2.h>
29#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070030#include <linux/platform_data/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030031#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030032
33#include <media/v4l2-ctrls.h>
34#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030035#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030036#include <media/v4l2-ioctl.h>
37#include <media/v4l2-mem2mem.h>
38#include <media/videobuf2-core.h>
39#include <media/videobuf2-dma-contig.h>
40
41#include "coda.h"
42
43#define CODA_NAME "coda"
44
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030045#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030046
Javier Martin186b2502012-07-26 05:53:35 -030047#define CODA_PARA_BUF_SIZE (10 * 1024)
48#define CODA_ISRAM_SIZE (2048 * 2)
49
Philipp Zabel918c66f2013-06-27 06:59:01 -030050#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030051#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030052
53#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030054
Philipp Zabelb96904e2013-05-23 10:42:58 -030055#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030056#define FMO_SLICE_SAVE_BUF_SIZE (32)
57#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030058#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030059
60#define MIN_W 176
61#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030062
63#define S_ALIGN 1 /* multiple of 2 */
64#define W_ALIGN 1 /* multiple of 2 */
65#define H_ALIGN 1 /* multiple of 2 */
66
67#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
68
69static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030070module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030071MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
72
73enum {
74 V4L2_M2M_SRC = 0,
75 V4L2_M2M_DST = 1,
76};
77
Javier Martin186b2502012-07-26 05:53:35 -030078enum coda_inst_type {
79 CODA_INST_ENCODER,
80 CODA_INST_DECODER,
81};
82
83enum coda_product {
84 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030085 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030086 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030087};
88
89struct coda_fmt {
90 char *name;
91 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030092};
93
94struct coda_codec {
95 u32 mode;
96 u32 src_fourcc;
97 u32 dst_fourcc;
98 u32 max_w;
99 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300100};
101
102struct coda_devtype {
103 char *firmware;
104 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300105 struct coda_codec *codecs;
106 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300107 size_t workbuf_size;
Philipp Zabel5d73fad2014-07-11 06:36:42 -0300108 size_t tempbuf_size;
Philipp Zabel401e9722014-07-11 06:36:43 -0300109 size_t iram_size;
Javier Martin186b2502012-07-26 05:53:35 -0300110};
111
112/* Per-queue, driver-specific private data */
113struct coda_q_data {
114 unsigned int width;
115 unsigned int height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300116 unsigned int bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300117 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300118 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300119 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300120};
121
122struct coda_aux_buf {
123 void *vaddr;
124 dma_addr_t paddr;
125 u32 size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300126 struct debugfs_blob_wrapper blob;
127 struct dentry *dentry;
Javier Martin186b2502012-07-26 05:53:35 -0300128};
129
130struct coda_dev {
131 struct v4l2_device v4l2_dev;
Philipp Zabel121cacf2014-07-18 07:22:42 -0300132 struct video_device vfd[2];
Javier Martin186b2502012-07-26 05:53:35 -0300133 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300134 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300135
136 void __iomem *regs_base;
137 struct clk *clk_per;
138 struct clk *clk_ahb;
Philipp Zabel8f452842014-07-11 06:36:35 -0300139 struct reset_control *rstc;
Javier Martin186b2502012-07-26 05:53:35 -0300140
141 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300142 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300143 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700144 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300145 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300146
147 spinlock_t irqlock;
148 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300149 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300150 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300151 struct v4l2_m2m_dev *m2m_dev;
152 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300153 struct list_head instances;
154 unsigned long instance_mask;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300155 struct dentry *debugfs_root;
Javier Martin186b2502012-07-26 05:53:35 -0300156};
157
158struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300159 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300160 u8 h264_intra_qp;
161 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300162 u8 h264_min_qp;
163 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300164 u8 h264_deblk_enabled;
165 u8 h264_deblk_alpha;
166 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300167 u8 mpeg4_intra_qp;
168 u8 mpeg4_inter_qp;
169 u8 gop_size;
Philipp Zabelf38f79d2014-07-11 06:36:28 -0300170 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300171 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300172 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300173 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
174 u32 framerate;
175 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300176 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300177 u32 slice_max_mb;
178};
179
Philipp Zabelc2d22512013-06-21 03:55:28 -0300180struct coda_iram_info {
181 u32 axi_sram_use;
182 phys_addr_t buf_bit_use;
183 phys_addr_t buf_ip_ac_dc_use;
184 phys_addr_t buf_dbk_y_use;
185 phys_addr_t buf_dbk_c_use;
186 phys_addr_t buf_ovl_use;
187 phys_addr_t buf_btp_use;
188 phys_addr_t search_ram_paddr;
189 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300190 int remaining;
191 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300192};
193
Philipp Zabel89548442014-07-11 06:36:17 -0300194struct gdi_tiled_map {
195 int xy2ca_map[16];
196 int xy2ba_map[16];
197 int xy2ra_map[16];
198 int rbc2axi_map[32];
199 int xy2rbc_config;
200 int map_type;
201#define GDI_LINEAR_FRAME_MAP 0
202};
203
Philipp Zabel846ced92014-07-11 06:36:31 -0300204struct coda_timestamp {
205 struct list_head list;
206 u32 sequence;
207 struct v4l2_timecode timecode;
208 struct timeval timestamp;
209};
210
Javier Martin186b2502012-07-26 05:53:35 -0300211struct coda_ctx {
212 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300213 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300214 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300215 struct work_struct pic_run_work;
216 struct work_struct seq_end_work;
217 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300218 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300219 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300220 int streamon_out;
221 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300222 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300223 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300224 u32 osequence;
Philipp Zabelcb2c0282014-07-11 06:36:33 -0300225 u32 sequence_offset;
Javier Martin186b2502012-07-26 05:53:35 -0300226 struct coda_q_data q_data[2];
227 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300228 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300229 enum v4l2_colorspace colorspace;
230 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300231 struct v4l2_ctrl_handler ctrls;
232 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300233 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300234 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300235 char vpu_header[3][64];
236 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300237 struct kfifo bitstream_fifo;
238 struct mutex bitstream_mutex;
239 struct coda_aux_buf bitstream;
Philipp Zabel84e23652014-07-11 06:36:34 -0300240 bool hold;
Javier Martin186b2502012-07-26 05:53:35 -0300241 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300242 struct coda_aux_buf psbuf;
243 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300244 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300245 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300246 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300247 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300248 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300249 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300250 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300251 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300252 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300253 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300254 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300255 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300256 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300257 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300258 int display_idx;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300259 struct dentry *debugfs_entry;
Javier Martin186b2502012-07-26 05:53:35 -0300260};
261
Javier Martin832fbb52012-10-29 08:34:59 -0300262static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
263 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
264static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300265
Javier Martin186b2502012-07-26 05:53:35 -0300266static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
267{
268 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
269 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
270 writel(data, dev->regs_base + reg);
271}
272
273static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
274{
275 u32 data;
276 data = readl(dev->regs_base + reg);
277 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
278 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
279 return data;
280}
281
282static inline unsigned long coda_isbusy(struct coda_dev *dev)
283{
284 return coda_read(dev, CODA_REG_BIT_BUSY);
285}
286
287static inline int coda_is_initialized(struct coda_dev *dev)
288{
289 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
290}
291
292static int coda_wait_timeout(struct coda_dev *dev)
293{
294 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
295
296 while (coda_isbusy(dev)) {
297 if (time_after(jiffies, timeout))
298 return -ETIMEDOUT;
299 }
300 return 0;
301}
302
303static void coda_command_async(struct coda_ctx *ctx, int cmd)
304{
305 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300306
Philipp Zabel89548442014-07-11 06:36:17 -0300307 if (dev->devtype->product == CODA_960 ||
308 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300309 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300310 coda_write(dev, ctx->bit_stream_param,
311 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300312 coda_write(dev, ctx->frm_dis_flg,
313 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300314 coda_write(dev, ctx->frame_mem_ctrl,
315 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300316 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
317 }
318
Philipp Zabel89548442014-07-11 06:36:17 -0300319 if (dev->devtype->product == CODA_960) {
320 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
321 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
322 }
323
Javier Martin186b2502012-07-26 05:53:35 -0300324 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
325
326 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
327 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300328 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
329
Javier Martin186b2502012-07-26 05:53:35 -0300330 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
331}
332
333static int coda_command_sync(struct coda_ctx *ctx, int cmd)
334{
335 struct coda_dev *dev = ctx->dev;
336
337 coda_command_async(ctx, cmd);
338 return coda_wait_timeout(dev);
339}
340
Philipp Zabel8f452842014-07-11 06:36:35 -0300341static int coda_hw_reset(struct coda_ctx *ctx)
342{
343 struct coda_dev *dev = ctx->dev;
344 unsigned long timeout;
345 unsigned int idx;
346 int ret;
347
348 if (!dev->rstc)
349 return -ENOENT;
350
351 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
352
Philipp Zabelae5abd22014-07-18 07:22:35 -0300353 if (dev->devtype->product == CODA_960) {
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 }
Philipp Zabel8f452842014-07-11 06:36:35 -0300361 }
362
363 ret = reset_control_reset(dev->rstc);
364 if (ret < 0)
365 return ret;
366
Philipp Zabelae5abd22014-07-18 07:22:35 -0300367 if (dev->devtype->product == CODA_960)
368 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
Philipp Zabel8f452842014-07-11 06:36:35 -0300369 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
370 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
371 ret = coda_wait_timeout(dev);
372 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
373
374 return ret;
375}
376
Javier Martin186b2502012-07-26 05:53:35 -0300377static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
378 enum v4l2_buf_type type)
379{
380 switch (type) {
381 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
382 return &(ctx->q_data[V4L2_M2M_SRC]);
383 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
384 return &(ctx->q_data[V4L2_M2M_DST]);
385 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300386 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300387 }
Javier Martin186b2502012-07-26 05:53:35 -0300388}
389
390/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300391 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300392 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300393static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300394 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300395 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300396 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300397 },
398 {
399 .name = "YUV 4:2:0 Planar, YCrCb",
400 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300401 },
402 {
403 .name = "H264 Encoded Stream",
404 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300405 },
406 {
407 .name = "MPEG4 Encoded Stream",
408 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300409 },
410};
411
Philipp Zabelb96904e2013-05-23 10:42:58 -0300412#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
413 { mode, src_fourcc, dst_fourcc, max_w, max_h }
414
415/*
416 * Arrays of codecs supported by each given version of Coda:
417 * i.MX27 -> codadx6
418 * i.MX5x -> coda7
419 * i.MX6 -> coda960
420 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
421 */
422static struct coda_codec codadx6_codecs[] = {
423 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
424 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300425};
426
Philipp Zabelb96904e2013-05-23 10:42:58 -0300427static struct coda_codec coda7_codecs[] = {
428 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
429 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300430 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
431 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300432};
433
Philipp Zabel89548442014-07-11 06:36:17 -0300434static struct coda_codec coda9_codecs[] = {
435 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
436 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
437 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
438 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
439};
440
Philipp Zabelb96904e2013-05-23 10:42:58 -0300441static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300442{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300443 switch (fourcc) {
444 case V4L2_PIX_FMT_YUV420:
445 case V4L2_PIX_FMT_YVU420:
446 return true;
447 default:
448 return false;
449 }
450}
Javier Martin186b2502012-07-26 05:53:35 -0300451
Philipp Zabelb96904e2013-05-23 10:42:58 -0300452/*
453 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
454 * tables.
455 */
456static u32 coda_format_normalize_yuv(u32 fourcc)
457{
458 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
459}
460
461static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
462 int dst_fourcc)
463{
464 struct coda_codec *codecs = dev->devtype->codecs;
465 int num_codecs = dev->devtype->num_codecs;
466 int k;
467
468 src_fourcc = coda_format_normalize_yuv(src_fourcc);
469 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
470 if (src_fourcc == dst_fourcc)
471 return NULL;
472
473 for (k = 0; k < num_codecs; k++) {
474 if (codecs[k].src_fourcc == src_fourcc &&
475 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300476 break;
477 }
478
Philipp Zabelb96904e2013-05-23 10:42:58 -0300479 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300480 return NULL;
481
Philipp Zabelb96904e2013-05-23 10:42:58 -0300482 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300483}
484
Philipp Zabel57625592013-09-30 10:34:51 -0300485static void coda_get_max_dimensions(struct coda_dev *dev,
486 struct coda_codec *codec,
487 int *max_w, int *max_h)
488{
489 struct coda_codec *codecs = dev->devtype->codecs;
490 int num_codecs = dev->devtype->num_codecs;
491 unsigned int w, h;
492 int k;
493
494 if (codec) {
495 w = codec->max_w;
496 h = codec->max_h;
497 } else {
498 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
499 w = max(w, codecs[k].max_w);
500 h = max(h, codecs[k].max_h);
501 }
502 }
503
504 if (max_w)
505 *max_w = w;
506 if (max_h)
507 *max_h = h;
508}
509
Philipp Zabel927933f2013-09-30 10:34:48 -0300510static char *coda_product_name(int product)
511{
512 static char buf[9];
513
514 switch (product) {
515 case CODA_DX6:
516 return "CodaDx6";
517 case CODA_7541:
518 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300519 case CODA_960:
520 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300521 default:
522 snprintf(buf, sizeof(buf), "(0x%04x)", product);
523 return buf;
524 }
525}
526
Javier Martin186b2502012-07-26 05:53:35 -0300527/*
528 * V4L2 ioctl() operations.
529 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300530static int coda_querycap(struct file *file, void *priv,
531 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300532{
Philipp Zabel927933f2013-09-30 10:34:48 -0300533 struct coda_ctx *ctx = fh_to_ctx(priv);
534
Javier Martin186b2502012-07-26 05:53:35 -0300535 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300536 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
537 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300538 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300539 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300540 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
541
542 return 0;
543}
544
Philipp Zabel22e244b2014-07-18 07:22:43 -0300545static int coda_enum_fmt(struct file *file, void *priv,
546 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300547{
548 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300549 struct coda_codec *codecs = ctx->dev->devtype->codecs;
550 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300551 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300552 int num_codecs = ctx->dev->devtype->num_codecs;
553 int num_formats = ARRAY_SIZE(coda_formats);
554 int i, k, num = 0;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300555 bool yuv;
556
557 if (ctx->inst_type == CODA_INST_ENCODER)
558 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
559 else
560 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300561
562 for (i = 0; i < num_formats; i++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300563 /* Skip either raw or compressed formats */
564 if (yuv != coda_format_is_yuv(formats[i].fourcc))
565 continue;
566 /* All uncompressed formats are always supported */
567 if (yuv) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300568 if (num == f->index)
569 break;
570 ++num;
571 continue;
572 }
573 /* Compressed formats may be supported, check the codec list */
574 for (k = 0; k < num_codecs; k++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300575 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
576 formats[i].fourcc == codecs[k].dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300577 break;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300578 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
Philipp Zabelb96904e2013-05-23 10:42:58 -0300579 formats[i].fourcc == codecs[k].src_fourcc)
580 break;
581 }
582 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300583 if (num == f->index)
584 break;
585 ++num;
586 }
587 }
588
589 if (i < num_formats) {
590 fmt = &formats[i];
591 strlcpy(f->description, fmt->name, sizeof(f->description));
592 f->pixelformat = fmt->fourcc;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300593 if (!yuv)
Philipp Zabelbaf70212013-09-30 10:34:46 -0300594 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300595 return 0;
596 }
597
598 /* Format not found */
599 return -EINVAL;
600}
601
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300602static int coda_g_fmt(struct file *file, void *priv,
603 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300604{
Javier Martin186b2502012-07-26 05:53:35 -0300605 struct coda_q_data *q_data;
606 struct coda_ctx *ctx = fh_to_ctx(priv);
607
Javier Martin186b2502012-07-26 05:53:35 -0300608 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300609 if (!q_data)
610 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300611
612 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300613 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300614 f->fmt.pix.width = q_data->width;
615 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300616 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300617
618 f->fmt.pix.sizeimage = q_data->sizeimage;
619 f->fmt.pix.colorspace = ctx->colorspace;
620
621 return 0;
622}
623
Philipp Zabel57625592013-09-30 10:34:51 -0300624static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
625 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300626{
Philipp Zabel57625592013-09-30 10:34:51 -0300627 struct coda_dev *dev = ctx->dev;
628 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300629 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300630 enum v4l2_field field;
631
632 field = f->fmt.pix.field;
633 if (field == V4L2_FIELD_ANY)
634 field = V4L2_FIELD_NONE;
635 else if (V4L2_FIELD_NONE != field)
636 return -EINVAL;
637
638 /* V4L2 specification suggests the driver corrects the format struct
639 * if any of the dimensions is unsupported */
640 f->fmt.pix.field = field;
641
Philipp Zabel57625592013-09-30 10:34:51 -0300642 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
643 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
644 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
645 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300646
Philipp Zabel57625592013-09-30 10:34:51 -0300647 switch (f->fmt.pix.pixelformat) {
648 case V4L2_PIX_FMT_YUV420:
649 case V4L2_PIX_FMT_YVU420:
650 case V4L2_PIX_FMT_H264:
651 case V4L2_PIX_FMT_MPEG4:
652 case V4L2_PIX_FMT_JPEG:
653 break;
654 default:
655 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300656 if (!q_data)
657 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300658 f->fmt.pix.pixelformat = q_data->fourcc;
659 }
660
661 switch (f->fmt.pix.pixelformat) {
662 case V4L2_PIX_FMT_YUV420:
663 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300664 /* Frame stride must be multiple of 8, but 16 for h.264 */
665 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300666 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300667 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300668 break;
669 case V4L2_PIX_FMT_H264:
670 case V4L2_PIX_FMT_MPEG4:
671 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300672 f->fmt.pix.bytesperline = 0;
673 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300674 break;
675 default:
676 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300677 }
678
679 return 0;
680}
681
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300682static int coda_try_fmt_vid_cap(struct file *file, void *priv,
683 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300684{
Javier Martin186b2502012-07-26 05:53:35 -0300685 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300686 struct coda_codec *codec;
687 struct vb2_queue *src_vq;
688 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300689
Philipp Zabel918c66f2013-06-27 06:59:01 -0300690 /*
691 * If the source format is already fixed, try to find a codec that
692 * converts to the given destination format
693 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300694 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300695 if (vb2_is_streaming(src_vq)) {
696 struct coda_q_data *q_data_src;
697
698 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
699 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
700 f->fmt.pix.pixelformat);
701 if (!codec)
702 return -EINVAL;
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300703
704 f->fmt.pix.width = q_data_src->width;
705 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300706 } else {
707 /* Otherwise determine codec by encoded format, if possible */
708 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
709 f->fmt.pix.pixelformat);
710 }
Javier Martin186b2502012-07-26 05:53:35 -0300711
712 f->fmt.pix.colorspace = ctx->colorspace;
713
Philipp Zabel57625592013-09-30 10:34:51 -0300714 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300715 if (ret < 0)
716 return ret;
717
718 /* The h.264 decoder only returns complete 16x16 macroblocks */
719 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300720 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300721 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300722 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300723 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
724 f->fmt.pix.height * 3 / 2;
725 }
726
727 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300728}
729
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300730static int coda_try_fmt_vid_out(struct file *file, void *priv,
731 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300732{
733 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300734 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300735
Philipp Zabelb96904e2013-05-23 10:42:58 -0300736 /* Determine codec by encoded format, returns NULL if raw or invalid */
737 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
738 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300739
740 if (!f->fmt.pix.colorspace)
741 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
742
Philipp Zabel57625592013-09-30 10:34:51 -0300743 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300744}
745
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300746static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300747{
748 struct coda_q_data *q_data;
749 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300750
Philipp Zabel14604e32014-07-11 06:36:22 -0300751 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300752 if (!vq)
753 return -EINVAL;
754
755 q_data = get_q_data(ctx, f->type);
756 if (!q_data)
757 return -EINVAL;
758
759 if (vb2_is_busy(vq)) {
760 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
761 return -EBUSY;
762 }
763
Philipp Zabelb96904e2013-05-23 10:42:58 -0300764 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300765 q_data->width = f->fmt.pix.width;
766 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300767 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300768 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300769 q_data->rect.left = 0;
770 q_data->rect.top = 0;
771 q_data->rect.width = f->fmt.pix.width;
772 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300773
774 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
775 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300776 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300777
778 return 0;
779}
780
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300781static int coda_s_fmt_vid_cap(struct file *file, void *priv,
782 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300783{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300784 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300785 int ret;
786
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300787 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300788 if (ret)
789 return ret;
790
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300791 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300792}
793
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300794static int coda_s_fmt_vid_out(struct file *file, void *priv,
795 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300796{
797 struct coda_ctx *ctx = fh_to_ctx(priv);
798 int ret;
799
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300800 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300801 if (ret)
802 return ret;
803
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300804 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300805 if (ret)
806 ctx->colorspace = f->fmt.pix.colorspace;
807
808 return ret;
809}
810
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300811static int coda_qbuf(struct file *file, void *priv,
812 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300813{
814 struct coda_ctx *ctx = fh_to_ctx(priv);
815
Philipp Zabel14604e32014-07-11 06:36:22 -0300816 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300817}
818
Philipp Zabel918c66f2013-06-27 06:59:01 -0300819static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
820 struct v4l2_buffer *buf)
821{
822 struct vb2_queue *src_vq;
823
Philipp Zabel14604e32014-07-11 06:36:22 -0300824 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300825
826 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
827 (buf->sequence == (ctx->qsequence - 1)));
828}
829
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300830static int coda_dqbuf(struct file *file, void *priv,
831 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300832{
833 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300834 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300835
Philipp Zabel14604e32014-07-11 06:36:22 -0300836 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300837
838 /* If this is the last capture buffer, emit an end-of-stream event */
839 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
840 coda_buf_is_end_of_stream(ctx, buf)) {
841 const struct v4l2_event eos_event = {
842 .type = V4L2_EVENT_EOS
843 };
844
845 v4l2_event_queue_fh(&ctx->fh, &eos_event);
846 }
847
848 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300849}
850
Philipp Zabel52c41672014-07-11 06:36:19 -0300851static int coda_g_selection(struct file *file, void *fh,
852 struct v4l2_selection *s)
853{
854 struct coda_ctx *ctx = fh_to_ctx(fh);
855 struct coda_q_data *q_data;
856 struct v4l2_rect r, *rsel;
857
858 q_data = get_q_data(ctx, s->type);
859 if (!q_data)
860 return -EINVAL;
861
862 r.left = 0;
863 r.top = 0;
864 r.width = q_data->width;
865 r.height = q_data->height;
866 rsel = &q_data->rect;
867
868 switch (s->target) {
869 case V4L2_SEL_TGT_CROP_DEFAULT:
870 case V4L2_SEL_TGT_CROP_BOUNDS:
871 rsel = &r;
872 /* fallthrough */
873 case V4L2_SEL_TGT_CROP:
874 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
875 return -EINVAL;
876 break;
877 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
878 case V4L2_SEL_TGT_COMPOSE_PADDED:
879 rsel = &r;
880 /* fallthrough */
881 case V4L2_SEL_TGT_COMPOSE:
882 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
883 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
884 return -EINVAL;
885 break;
886 default:
887 return -EINVAL;
888 }
889
890 s->r = *rsel;
891
892 return 0;
893}
894
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300895static int coda_try_decoder_cmd(struct file *file, void *fh,
896 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300897{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300898 if (dc->cmd != V4L2_DEC_CMD_STOP)
899 return -EINVAL;
900
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300901 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300902 return -EINVAL;
903
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300904 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300905 return -EINVAL;
906
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300907 return 0;
908}
909
910static int coda_decoder_cmd(struct file *file, void *fh,
911 struct v4l2_decoder_cmd *dc)
912{
913 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300914 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300915 int ret;
916
917 ret = coda_try_decoder_cmd(file, fh, dc);
918 if (ret < 0)
919 return ret;
920
921 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300922 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300923 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300924
925 /* Set the strem-end flag on this context */
926 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
927
Philipp Zabel89548442014-07-11 06:36:17 -0300928 if ((dev->devtype->product == CODA_960) &&
929 coda_isbusy(dev) &&
930 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
931 /* If this context is currently running, update the hardware flag */
932 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
933 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300934 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300935 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300936
Philipp Zabel918c66f2013-06-27 06:59:01 -0300937 return 0;
938}
939
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300940static int coda_subscribe_event(struct v4l2_fh *fh,
941 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300942{
943 switch (sub->type) {
944 case V4L2_EVENT_EOS:
945 return v4l2_event_subscribe(fh, sub, 0, NULL);
946 default:
947 return v4l2_ctrl_subscribe_event(fh, sub);
948 }
Javier Martin186b2502012-07-26 05:53:35 -0300949}
950
951static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300952 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300953
Philipp Zabel22e244b2014-07-18 07:22:43 -0300954 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300955 .vidioc_g_fmt_vid_cap = coda_g_fmt,
956 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
957 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300958
Philipp Zabel22e244b2014-07-18 07:22:43 -0300959 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300960 .vidioc_g_fmt_vid_out = coda_g_fmt,
961 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
962 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300963
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300964 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
965 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300966
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300967 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300968 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300969 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300970 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300971
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300972 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
973 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300974
Philipp Zabel52c41672014-07-11 06:36:19 -0300975 .vidioc_g_selection = coda_g_selection,
976
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300977 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300978 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300979
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300980 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300981 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300982};
983
Philipp Zabel918c66f2013-06-27 06:59:01 -0300984static int coda_start_decoding(struct coda_ctx *ctx);
985
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300986static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
987{
988 return kfifo_len(&ctx->bitstream_fifo);
989}
990
991static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
992{
993 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
994 struct coda_dev *dev = ctx->dev;
995 u32 rd_ptr;
996
997 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
998 kfifo->out = (kfifo->in & ~kfifo->mask) |
999 (rd_ptr - ctx->bitstream.paddr);
1000 if (kfifo->out > kfifo->in)
1001 kfifo->out -= kfifo->mask + 1;
1002}
1003
1004static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1005{
1006 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1007 struct coda_dev *dev = ctx->dev;
1008 u32 rd_ptr, wr_ptr;
1009
1010 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1011 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1012 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1013 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1014}
1015
1016static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1017{
1018 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1019 struct coda_dev *dev = ctx->dev;
1020 u32 wr_ptr;
1021
1022 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1023 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1024}
1025
1026static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1027{
1028 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1029 u32 n;
1030
1031 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1032 if (n < src_size)
1033 return -ENOSPC;
1034
1035 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1036 ctx->bitstream.size, DMA_TO_DEVICE);
1037
Philipp Zabel846ced92014-07-11 06:36:31 -03001038 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001039
1040 return 0;
1041}
1042
1043static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1044 struct vb2_buffer *src_buf)
1045{
1046 int ret;
1047
1048 if (coda_get_bitstream_payload(ctx) +
1049 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1050 return false;
1051
1052 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1053 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1054 return true;
1055 }
1056
1057 ret = coda_bitstream_queue(ctx, src_buf);
1058 if (ret < 0) {
1059 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1060 return false;
1061 }
1062 /* Sync read pointer to device */
1063 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1064 coda_kfifo_sync_to_device_write(ctx);
1065
Philipp Zabel84e23652014-07-11 06:36:34 -03001066 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001067
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001068 return true;
1069}
1070
1071static void coda_fill_bitstream(struct coda_ctx *ctx)
1072{
1073 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001074 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001075
Philipp Zabel14604e32014-07-11 06:36:22 -03001076 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1077 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001078
1079 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001080 /*
1081 * Source buffer is queued in the bitstream ringbuffer;
1082 * queue the timestamp and mark source buffer as done
1083 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001084 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001085
1086 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1087 if (ts) {
1088 ts->sequence = src_buf->v4l2_buf.sequence;
1089 ts->timecode = src_buf->v4l2_buf.timecode;
1090 ts->timestamp = src_buf->v4l2_buf.timestamp;
1091 list_add_tail(&ts->list, &ctx->timestamp_list);
1092 }
1093
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001094 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1095 } else {
1096 break;
1097 }
1098 }
1099}
1100
Philipp Zabel89548442014-07-11 06:36:17 -03001101static void coda_set_gdi_regs(struct coda_ctx *ctx)
1102{
1103 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1104 struct coda_dev *dev = ctx->dev;
1105 int i;
1106
1107 for (i = 0; i < 16; i++)
1108 coda_write(dev, tiled_map->xy2ca_map[i],
1109 CODA9_GDI_XY2_CAS_0 + 4 * i);
1110 for (i = 0; i < 4; i++)
1111 coda_write(dev, tiled_map->xy2ba_map[i],
1112 CODA9_GDI_XY2_BA_0 + 4 * i);
1113 for (i = 0; i < 16; i++)
1114 coda_write(dev, tiled_map->xy2ra_map[i],
1115 CODA9_GDI_XY2_RAS_0 + 4 * i);
1116 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1117 for (i = 0; i < 32; i++)
1118 coda_write(dev, tiled_map->rbc2axi_map[i],
1119 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1120}
1121
Javier Martin186b2502012-07-26 05:53:35 -03001122/*
1123 * Mem-to-mem operations.
1124 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001125static int coda_prepare_decode(struct coda_ctx *ctx)
1126{
1127 struct vb2_buffer *dst_buf;
1128 struct coda_dev *dev = ctx->dev;
1129 struct coda_q_data *q_data_dst;
1130 u32 stridey, height;
1131 u32 picture_y, picture_cb, picture_cr;
1132
Philipp Zabel14604e32014-07-11 06:36:22 -03001133 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001134 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1135
1136 if (ctx->params.rot_mode & CODA_ROT_90) {
1137 stridey = q_data_dst->height;
1138 height = q_data_dst->width;
1139 } else {
1140 stridey = q_data_dst->width;
1141 height = q_data_dst->height;
1142 }
1143
1144 /* Try to copy source buffer contents into the bitstream ringbuffer */
1145 mutex_lock(&ctx->bitstream_mutex);
1146 coda_fill_bitstream(ctx);
1147 mutex_unlock(&ctx->bitstream_mutex);
1148
1149 if (coda_get_bitstream_payload(ctx) < 512 &&
1150 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1151 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1152 "bitstream payload: %d, skipping\n",
1153 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001154 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001155 return -EAGAIN;
1156 }
1157
1158 /* Run coda_start_decoding (again) if not yet initialized */
1159 if (!ctx->initialized) {
1160 int ret = coda_start_decoding(ctx);
1161 if (ret < 0) {
1162 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001163 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001164 return -EAGAIN;
1165 } else {
1166 ctx->initialized = 1;
1167 }
1168 }
1169
Philipp Zabel89548442014-07-11 06:36:17 -03001170 if (dev->devtype->product == CODA_960)
1171 coda_set_gdi_regs(ctx);
1172
Philipp Zabel918c66f2013-06-27 06:59:01 -03001173 /* Set rotator output */
1174 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1175 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1176 /* Switch Cr and Cb for YVU420 format */
1177 picture_cr = picture_y + stridey * height;
1178 picture_cb = picture_cr + stridey / 2 * height / 2;
1179 } else {
1180 picture_cb = picture_y + stridey * height;
1181 picture_cr = picture_cb + stridey / 2 * height / 2;
1182 }
Philipp Zabel89548442014-07-11 06:36:17 -03001183
1184 if (dev->devtype->product == CODA_960) {
1185 /*
1186 * The CODA960 seems to have an internal list of buffers with
1187 * 64 entries that includes the registered frame buffers as
1188 * well as the rotator buffer output.
1189 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1190 */
1191 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1192 CODA9_CMD_DEC_PIC_ROT_INDEX);
1193 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1194 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1195 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1196 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1197 } else {
1198 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1199 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1200 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1201 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1202 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001203 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1204 CODA_CMD_DEC_PIC_ROT_MODE);
1205
1206 switch (dev->devtype->product) {
1207 case CODA_DX6:
1208 /* TBD */
1209 case CODA_7541:
1210 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1211 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001212 case CODA_960:
1213 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1214 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001215 }
1216
1217 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1218
1219 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1220 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1221
1222 return 0;
1223}
1224
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001225static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001226{
Javier Martin186b2502012-07-26 05:53:35 -03001227 struct coda_q_data *q_data_src, *q_data_dst;
1228 struct vb2_buffer *src_buf, *dst_buf;
1229 struct coda_dev *dev = ctx->dev;
1230 int force_ipicture;
1231 int quant_param = 0;
1232 u32 picture_y, picture_cb, picture_cr;
1233 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1234 u32 dst_fourcc;
1235
Philipp Zabel14604e32014-07-11 06:36:22 -03001236 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1237 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001238 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1239 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001240 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001241
Philipp Zabel918c66f2013-06-27 06:59:01 -03001242 src_buf->v4l2_buf.sequence = ctx->osequence;
1243 dst_buf->v4l2_buf.sequence = ctx->osequence;
1244 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001245
1246 /*
1247 * Workaround coda firmware BUG that only marks the first
1248 * frame as IDR. This is a problem for some decoders that can't
1249 * recover when a frame is lost.
1250 */
1251 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1252 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1253 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1254 } else {
1255 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1256 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1257 }
1258
Philipp Zabel89548442014-07-11 06:36:17 -03001259 if (dev->devtype->product == CODA_960)
1260 coda_set_gdi_regs(ctx);
1261
Javier Martin186b2502012-07-26 05:53:35 -03001262 /*
1263 * Copy headers at the beginning of the first frame for H.264 only.
1264 * In MPEG4 they are already copied by the coda.
1265 */
1266 if (src_buf->v4l2_buf.sequence == 0) {
1267 pic_stream_buffer_addr =
1268 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1269 ctx->vpu_header_size[0] +
1270 ctx->vpu_header_size[1] +
1271 ctx->vpu_header_size[2];
1272 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1273 ctx->vpu_header_size[0] -
1274 ctx->vpu_header_size[1] -
1275 ctx->vpu_header_size[2];
1276 memcpy(vb2_plane_vaddr(dst_buf, 0),
1277 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1278 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1279 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1280 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1281 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1282 ctx->vpu_header_size[2]);
1283 } else {
1284 pic_stream_buffer_addr =
1285 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1286 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1287 }
1288
1289 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1290 force_ipicture = 1;
1291 switch (dst_fourcc) {
1292 case V4L2_PIX_FMT_H264:
1293 quant_param = ctx->params.h264_intra_qp;
1294 break;
1295 case V4L2_PIX_FMT_MPEG4:
1296 quant_param = ctx->params.mpeg4_intra_qp;
1297 break;
1298 default:
1299 v4l2_warn(&ctx->dev->v4l2_dev,
1300 "cannot set intra qp, fmt not supported\n");
1301 break;
1302 }
1303 } else {
1304 force_ipicture = 0;
1305 switch (dst_fourcc) {
1306 case V4L2_PIX_FMT_H264:
1307 quant_param = ctx->params.h264_inter_qp;
1308 break;
1309 case V4L2_PIX_FMT_MPEG4:
1310 quant_param = ctx->params.mpeg4_inter_qp;
1311 break;
1312 default:
1313 v4l2_warn(&ctx->dev->v4l2_dev,
1314 "cannot set inter qp, fmt not supported\n");
1315 break;
1316 }
1317 }
1318
1319 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001320 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001321 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1322
1323
1324 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001325 switch (q_data_src->fourcc) {
1326 case V4L2_PIX_FMT_YVU420:
1327 /* Switch Cb and Cr for YVU420 format */
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001328 picture_cr = picture_y + q_data_src->bytesperline *
1329 q_data_src->height;
1330 picture_cb = picture_cr + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001331 q_data_src->height / 2;
1332 break;
1333 case V4L2_PIX_FMT_YUV420:
1334 default:
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001335 picture_cb = picture_y + q_data_src->bytesperline *
1336 q_data_src->height;
1337 picture_cr = picture_cb + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001338 q_data_src->height / 2;
1339 break;
1340 }
Javier Martin186b2502012-07-26 05:53:35 -03001341
Philipp Zabel89548442014-07-11 06:36:17 -03001342 if (dev->devtype->product == CODA_960) {
1343 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1344 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1345 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1346
1347 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1348 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1349 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1350 } else {
1351 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1352 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1353 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1354 }
Javier Martin186b2502012-07-26 05:53:35 -03001355 coda_write(dev, force_ipicture << 1 & 0x2,
1356 CODA_CMD_ENC_PIC_OPTION);
1357
1358 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1359 coda_write(dev, pic_stream_buffer_size / 1024,
1360 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001361
1362 if (!ctx->streamon_out) {
1363 /* After streamoff on the output side, set the stream end flag */
1364 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1365 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1366 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001367}
1368
1369static void coda_device_run(void *m2m_priv)
1370{
1371 struct coda_ctx *ctx = m2m_priv;
1372 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001373
1374 queue_work(dev->workqueue, &ctx->pic_run_work);
1375}
1376
1377static void coda_free_framebuffers(struct coda_ctx *ctx);
1378static void coda_free_context_buffers(struct coda_ctx *ctx);
1379
1380static void coda_seq_end_work(struct work_struct *work)
1381{
1382 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1383 struct coda_dev *dev = ctx->dev;
1384
1385 mutex_lock(&ctx->buffer_mutex);
1386 mutex_lock(&dev->coda_mutex);
1387
1388 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1389 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1390 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1391 v4l2_err(&dev->v4l2_dev,
1392 "CODA_COMMAND_SEQ_END failed\n");
1393 }
1394
1395 kfifo_init(&ctx->bitstream_fifo,
1396 ctx->bitstream.vaddr, ctx->bitstream.size);
1397
1398 coda_free_framebuffers(ctx);
1399 coda_free_context_buffers(ctx);
1400
1401 mutex_unlock(&dev->coda_mutex);
1402 mutex_unlock(&ctx->buffer_mutex);
1403}
1404
1405static void coda_finish_decode(struct coda_ctx *ctx);
1406static void coda_finish_encode(struct coda_ctx *ctx);
1407
1408static void coda_pic_run_work(struct work_struct *work)
1409{
1410 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1411 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001412 int ret;
1413
1414 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001415 mutex_lock(&dev->coda_mutex);
1416
Philipp Zabel918c66f2013-06-27 06:59:01 -03001417 if (ctx->inst_type == CODA_INST_DECODER) {
1418 ret = coda_prepare_decode(ctx);
1419 if (ret < 0) {
1420 mutex_unlock(&dev->coda_mutex);
1421 mutex_unlock(&ctx->buffer_mutex);
1422 /* job_finish scheduled by prepare_decode */
1423 return;
1424 }
1425 } else {
1426 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001427 }
1428
Philipp Zabelc2d22512013-06-21 03:55:28 -03001429 if (dev->devtype->product != CODA_DX6)
1430 coda_write(dev, ctx->iram_info.axi_sram_use,
1431 CODA7_REG_BIT_AXI_SRAM_USE);
1432
Philipp Zabel918c66f2013-06-27 06:59:01 -03001433 if (ctx->inst_type == CODA_INST_DECODER)
1434 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001435 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001436
1437 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1438 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001439
1440 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001441
1442 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001443 } else if (!ctx->aborting) {
1444 if (ctx->inst_type == CODA_INST_DECODER)
1445 coda_finish_decode(ctx);
1446 else
1447 coda_finish_encode(ctx);
1448 }
1449
1450 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1451 queue_work(dev->workqueue, &ctx->seq_end_work);
1452
1453 mutex_unlock(&dev->coda_mutex);
1454 mutex_unlock(&ctx->buffer_mutex);
1455
Philipp Zabel14604e32014-07-11 06:36:22 -03001456 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001457}
1458
1459static int coda_job_ready(void *m2m_priv)
1460{
1461 struct coda_ctx *ctx = m2m_priv;
1462
1463 /*
1464 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001465 * and 1 frame are needed. In the decoder case,
1466 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001467 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001468 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001469 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001470 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1471 "not ready: not enough video buffers.\n");
1472 return 0;
1473 }
1474
Philipp Zabel14604e32014-07-11 06:36:22 -03001475 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001476 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1477 "not ready: not enough video capture buffers.\n");
1478 return 0;
1479 }
1480
Philipp Zabel84e23652014-07-11 06:36:34 -03001481 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001482 ((ctx->inst_type == CODA_INST_DECODER) &&
1483 (coda_get_bitstream_payload(ctx) < 512) &&
1484 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1485 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1486 "%d: not ready: not enough bitstream data.\n",
1487 ctx->idx);
1488 return 0;
1489 }
1490
Philipp Zabel3e748262013-05-23 10:43:01 -03001491 if (ctx->aborting) {
1492 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1493 "not ready: aborting\n");
1494 return 0;
1495 }
1496
Javier Martin186b2502012-07-26 05:53:35 -03001497 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1498 "job ready\n");
1499 return 1;
1500}
1501
1502static void coda_job_abort(void *priv)
1503{
1504 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001505
1506 ctx->aborting = 1;
1507
1508 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1509 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001510}
1511
1512static void coda_lock(void *m2m_priv)
1513{
1514 struct coda_ctx *ctx = m2m_priv;
1515 struct coda_dev *pcdev = ctx->dev;
1516 mutex_lock(&pcdev->dev_mutex);
1517}
1518
1519static void coda_unlock(void *m2m_priv)
1520{
1521 struct coda_ctx *ctx = m2m_priv;
1522 struct coda_dev *pcdev = ctx->dev;
1523 mutex_unlock(&pcdev->dev_mutex);
1524}
1525
1526static struct v4l2_m2m_ops coda_m2m_ops = {
1527 .device_run = coda_device_run,
1528 .job_ready = coda_job_ready,
1529 .job_abort = coda_job_abort,
1530 .lock = coda_lock,
1531 .unlock = coda_unlock,
1532};
1533
Philipp Zabel89548442014-07-11 06:36:17 -03001534static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1535{
1536 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1537 int luma_map, chro_map, i;
1538
1539 memset(tiled_map, 0, sizeof(*tiled_map));
1540
1541 luma_map = 64;
1542 chro_map = 64;
1543 tiled_map->map_type = tiled_map_type;
1544 for (i = 0; i < 16; i++)
1545 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1546 for (i = 0; i < 4; i++)
1547 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1548 for (i = 0; i < 16; i++)
1549 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1550
1551 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1552 tiled_map->xy2rbc_config = 0;
1553 } else {
1554 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1555 tiled_map_type);
1556 return;
1557 }
1558}
1559
Javier Martin186b2502012-07-26 05:53:35 -03001560static void set_default_params(struct coda_ctx *ctx)
1561{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001562 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001563 int max_w;
1564 int max_h;
1565
Philipp Zabel121cacf2014-07-18 07:22:42 -03001566 if (ctx->inst_type == CODA_INST_ENCODER) {
1567 src_fourcc = V4L2_PIX_FMT_YUV420;
1568 dst_fourcc = V4L2_PIX_FMT_H264;
1569 } else {
1570 src_fourcc = V4L2_PIX_FMT_H264;
1571 dst_fourcc = V4L2_PIX_FMT_YUV420;
1572 }
1573 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001574 max_w = ctx->codec->max_w;
1575 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001576
Philipp Zabel121cacf2014-07-18 07:22:42 -03001577 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001578 ctx->colorspace = V4L2_COLORSPACE_REC709;
1579 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001580 ctx->aborting = 0;
1581
1582 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001583 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1584 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1585 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1586 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001587 ctx->q_data[V4L2_M2M_DST].width = max_w;
1588 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001589 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1590 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1591 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1592 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1593 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1594 } else {
1595 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1596 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
1597 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1598 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
1599 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001600 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1601 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1602 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1603 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001604
1605 if (ctx->dev->devtype->product == CODA_960)
1606 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001607}
1608
1609/*
1610 * Queue operations
1611 */
1612static int coda_queue_setup(struct vb2_queue *vq,
1613 const struct v4l2_format *fmt,
1614 unsigned int *nbuffers, unsigned int *nplanes,
1615 unsigned int sizes[], void *alloc_ctxs[])
1616{
1617 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001618 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001619 unsigned int size;
1620
Philipp Zabele34db062012-08-29 08:22:00 -03001621 q_data = get_q_data(ctx, vq->type);
1622 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001623
1624 *nplanes = 1;
1625 sizes[0] = size;
1626
1627 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1628
1629 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1630 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1631
1632 return 0;
1633}
1634
1635static int coda_buf_prepare(struct vb2_buffer *vb)
1636{
1637 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1638 struct coda_q_data *q_data;
1639
1640 q_data = get_q_data(ctx, vb->vb2_queue->type);
1641
1642 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1643 v4l2_warn(&ctx->dev->v4l2_dev,
1644 "%s data will not fit into plane (%lu < %lu)\n",
1645 __func__, vb2_plane_size(vb, 0),
1646 (long)q_data->sizeimage);
1647 return -EINVAL;
1648 }
1649
Javier Martin186b2502012-07-26 05:53:35 -03001650 return 0;
1651}
1652
1653static void coda_buf_queue(struct vb2_buffer *vb)
1654{
1655 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001656 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001657 struct coda_q_data *q_data;
1658
1659 q_data = get_q_data(ctx, vb->vb2_queue->type);
1660
1661 /*
1662 * In the decoder case, immediately try to copy the buffer into the
1663 * bitstream ringbuffer and mark it as ready to be dequeued.
1664 */
1665 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1666 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1667 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001668 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001669 * the stream end
1670 */
Philipp Zabel89548442014-07-11 06:36:17 -03001671 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001672 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001673 if ((dev->devtype->product == CODA_960) &&
1674 coda_isbusy(dev) &&
1675 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1676 /* if this decoder instance is running, set the stream end flag */
1677 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1678 }
1679 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001680 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001681 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001682 if (vb2_is_streaming(vb->vb2_queue))
1683 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001684 mutex_unlock(&ctx->bitstream_mutex);
1685 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001686 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001687 }
Javier Martin186b2502012-07-26 05:53:35 -03001688}
1689
Philipp Zabel86eda902013-05-23 10:42:57 -03001690static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1691{
1692 struct coda_dev *dev = ctx->dev;
1693 u32 *p = ctx->parabuf.vaddr;
1694
1695 if (dev->devtype->product == CODA_DX6)
1696 p[index] = value;
1697 else
1698 p[index ^ 1] = value;
1699}
1700
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001701static int coda_alloc_aux_buf(struct coda_dev *dev,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001702 struct coda_aux_buf *buf, size_t size,
1703 const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001704{
1705 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1706 GFP_KERNEL);
1707 if (!buf->vaddr)
1708 return -ENOMEM;
1709
1710 buf->size = size;
1711
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001712 if (name && parent) {
1713 buf->blob.data = buf->vaddr;
1714 buf->blob.size = size;
1715 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
1716 if (!buf->dentry)
1717 dev_warn(&dev->plat_dev->dev,
1718 "failed to create debugfs entry %s\n", name);
1719 }
1720
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001721 return 0;
1722}
1723
1724static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001725 struct coda_aux_buf *buf, size_t size,
1726 const char *name)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001727{
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001728 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001729}
1730
1731static void coda_free_aux_buf(struct coda_dev *dev,
1732 struct coda_aux_buf *buf)
1733{
1734 if (buf->vaddr) {
1735 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1736 buf->vaddr, buf->paddr);
1737 buf->vaddr = NULL;
1738 buf->size = 0;
1739 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001740 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001741}
1742
1743static void coda_free_framebuffers(struct coda_ctx *ctx)
1744{
1745 int i;
1746
1747 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1748 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1749}
1750
Philipp Zabelec25f682012-07-20 08:54:29 -03001751static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1752{
1753 struct coda_dev *dev = ctx->dev;
Philipp Zabel7c3df782014-07-11 06:36:38 -03001754 int width, height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001755 dma_addr_t paddr;
1756 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001757 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001758 int i;
1759
Philipp Zabel7c3df782014-07-11 06:36:38 -03001760 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
1761 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
1762 width = round_up(q_data->width, 16);
1763 height = round_up(q_data->height, 16);
1764 } else {
1765 width = round_up(q_data->width, 8);
1766 height = q_data->height;
1767 }
1768 ysize = width * height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001769
Philipp Zabelec25f682012-07-20 08:54:29 -03001770 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001771 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001772 size_t size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001773 char *name;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001774
Philipp Zabelaed14b02014-07-11 06:36:15 -03001775 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001776 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1777 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001778 size += ysize / 4;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001779 name = kasprintf(GFP_KERNEL, "fb%d", i);
1780 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
1781 size, name);
1782 kfree(name);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001783 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001784 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001785 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001786 }
1787 }
1788
1789 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001790 for (i = 0; i < ctx->num_internal_frames; i++) {
1791 paddr = ctx->internal_frames[i].paddr;
1792 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1793 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1794 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001795
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001796 /* mvcol buffer for h.264 */
1797 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1798 dev->devtype->product != CODA_DX6)
1799 coda_parabuf_write(ctx, 96 + i,
1800 ctx->internal_frames[i].paddr +
1801 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001802 }
1803
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001804 /* mvcol buffer for mpeg4 */
1805 if ((dev->devtype->product != CODA_DX6) &&
1806 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1807 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1808 ysize + ysize/4 + ysize/4);
1809
Philipp Zabelec25f682012-07-20 08:54:29 -03001810 return 0;
1811}
1812
Javier Martin3f3f5c72012-10-29 05:20:29 -03001813static int coda_h264_padding(int size, char *p)
1814{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001815 int nal_size;
1816 int diff;
1817
Javier Martin832fbb52012-10-29 08:34:59 -03001818 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001819 if (diff == 0)
1820 return 0;
1821
Javier Martin832fbb52012-10-29 08:34:59 -03001822 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001823 memcpy(p, coda_filler_nal, nal_size);
1824
1825 /* Add rbsp stop bit and trailing at the end */
1826 *(p + nal_size - 1) = 0x80;
1827
1828 return nal_size;
1829}
1830
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001831static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1832{
1833 phys_addr_t ret;
1834
1835 size = round_up(size, 1024);
1836 if (size > iram->remaining)
1837 return 0;
1838 iram->remaining -= size;
1839
1840 ret = iram->next_paddr;
1841 iram->next_paddr += size;
1842
1843 return ret;
1844}
1845
Philipp Zabelc2d22512013-06-21 03:55:28 -03001846static void coda_setup_iram(struct coda_ctx *ctx)
1847{
1848 struct coda_iram_info *iram_info = &ctx->iram_info;
1849 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001850 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001851 int dbk_bits;
1852 int bit_bits;
1853 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001854
1855 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001856 iram_info->next_paddr = dev->iram.paddr;
1857 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001858
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001859 switch (dev->devtype->product) {
1860 case CODA_7541:
1861 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1862 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1863 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1864 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001865 case CODA_960:
1866 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1867 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1868 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1869 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001870 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001871 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001872 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001873
1874 if (ctx->inst_type == CODA_INST_ENCODER) {
1875 struct coda_q_data *q_data_src;
1876
1877 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1878 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1879
1880 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001881 if (dev->devtype->product == CODA_7541) {
1882 iram_info->search_ram_size = round_up(mb_width * 16 *
1883 36 + 2048, 1024);
1884 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1885 iram_info->search_ram_size);
1886 if (!iram_info->search_ram_paddr) {
1887 pr_err("IRAM is smaller than the search ram size\n");
1888 goto out;
1889 }
1890 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1891 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001892 }
1893
1894 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001895 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1896 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1897 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001898 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001899 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001900
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001901 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1902 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001903 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001904 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001905
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001906 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1907 if (!iram_info->buf_ip_ac_dc_use)
1908 goto out;
1909 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001910
Philipp Zabel8358e762013-06-21 03:55:32 -03001911 /* OVL and BTP disabled for encoder */
1912 } else if (ctx->inst_type == CODA_INST_DECODER) {
1913 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001914
1915 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1916 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001917
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001918 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1919 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1920 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001921 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001922 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001923
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001924 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1925 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001926 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001927 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001928
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001929 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1930 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001931 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001932 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001933
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001934 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001935 }
1936
1937out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001938 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1939 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1940 "IRAM smaller than needed\n");
1941
1942 if (dev->devtype->product == CODA_7541) {
1943 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001944 if (ctx->inst_type == CODA_INST_DECODER) {
1945 /* fw 1.4.50 */
1946 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1947 CODA7_USE_IP_ENABLE);
1948 } else {
1949 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001950 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1951 CODA7_USE_HOST_DBK_ENABLE |
1952 CODA7_USE_IP_ENABLE |
1953 CODA7_USE_DBK_ENABLE);
1954 }
1955 }
1956}
1957
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001958static void coda_free_context_buffers(struct coda_ctx *ctx)
1959{
1960 struct coda_dev *dev = ctx->dev;
1961
Philipp Zabel918c66f2013-06-27 06:59:01 -03001962 coda_free_aux_buf(dev, &ctx->slicebuf);
1963 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001964 if (dev->devtype->product != CODA_DX6)
1965 coda_free_aux_buf(dev, &ctx->workbuf);
1966}
1967
1968static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1969 struct coda_q_data *q_data)
1970{
1971 struct coda_dev *dev = ctx->dev;
1972 size_t size;
1973 int ret;
1974
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001975 if (dev->devtype->product == CODA_DX6)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001976 return 0;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001977
Philipp Zabel918c66f2013-06-27 06:59:01 -03001978 if (ctx->psbuf.vaddr) {
1979 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1980 return -EBUSY;
1981 }
1982 if (ctx->slicebuf.vaddr) {
1983 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1984 return -EBUSY;
1985 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001986 if (ctx->workbuf.vaddr) {
1987 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1988 ret = -EBUSY;
1989 return -ENOMEM;
1990 }
1991
Philipp Zabel918c66f2013-06-27 06:59:01 -03001992 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1993 /* worst case slice size */
1994 size = (DIV_ROUND_UP(q_data->width, 16) *
1995 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001996 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, "slicebuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03001997 if (ret < 0) {
1998 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1999 ctx->slicebuf.size);
2000 return ret;
2001 }
2002 }
2003
2004 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002005 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002006 if (ret < 0) {
2007 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
2008 goto err;
2009 }
2010 }
2011
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002012 size = dev->devtype->workbuf_size;
2013 if (dev->devtype->product == CODA_960 &&
2014 q_data->fourcc == V4L2_PIX_FMT_H264)
2015 size += CODA9_PS_SAVE_SIZE;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002016 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002017 if (ret < 0) {
2018 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2019 ctx->workbuf.size);
2020 goto err;
2021 }
2022
2023 return 0;
2024
2025err:
2026 coda_free_context_buffers(ctx);
2027 return ret;
2028}
2029
Philipp Zabel918c66f2013-06-27 06:59:01 -03002030static int coda_start_decoding(struct coda_ctx *ctx)
2031{
2032 struct coda_q_data *q_data_src, *q_data_dst;
2033 u32 bitstream_buf, bitstream_size;
2034 struct coda_dev *dev = ctx->dev;
2035 int width, height;
2036 u32 src_fourcc;
2037 u32 val;
2038 int ret;
2039
2040 /* Start decoding */
2041 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2042 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2043 bitstream_buf = ctx->bitstream.paddr;
2044 bitstream_size = ctx->bitstream.size;
2045 src_fourcc = q_data_src->fourcc;
2046
2047 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2048
2049 /* Update coda bitstream read and write pointers from kfifo */
2050 coda_kfifo_sync_to_device_full(ctx);
2051
2052 ctx->display_idx = -1;
2053 ctx->frm_dis_flg = 0;
2054 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2055
2056 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2057 CODA_REG_BIT_BIT_STREAM_PARAM);
2058
2059 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2060 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2061 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002062 if ((dev->devtype->product == CODA_7541) ||
2063 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002064 val |= CODA_REORDER_ENABLE;
2065 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2066
2067 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002068 if (dev->devtype->product == CODA_960 &&
2069 src_fourcc == V4L2_PIX_FMT_MPEG4)
2070 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2071 else
2072 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002073 if (src_fourcc == V4L2_PIX_FMT_H264) {
2074 if (dev->devtype->product == CODA_7541) {
2075 coda_write(dev, ctx->psbuf.paddr,
2076 CODA_CMD_DEC_SEQ_PS_BB_START);
2077 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2078 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2079 }
Philipp Zabel89548442014-07-11 06:36:17 -03002080 if (dev->devtype->product == CODA_960) {
2081 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2082 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2083 }
2084 }
2085 if (dev->devtype->product != CODA_960) {
2086 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002087 }
2088
2089 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2090 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2091 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2092 return -ETIMEDOUT;
2093 }
2094
2095 /* Update kfifo out pointer from coda bitstream read pointer */
2096 coda_kfifo_sync_from_device(ctx);
2097
2098 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2099
2100 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2101 v4l2_err(&dev->v4l2_dev,
2102 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2103 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2104 return -EAGAIN;
2105 }
2106
2107 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2108 if (dev->devtype->product == CODA_DX6) {
2109 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2110 height = val & CODADX6_PICHEIGHT_MASK;
2111 } else {
2112 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2113 height = val & CODA7_PICHEIGHT_MASK;
2114 }
2115
2116 if (width > q_data_dst->width || height > q_data_dst->height) {
2117 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2118 width, height, q_data_dst->width, q_data_dst->height);
2119 return -EINVAL;
2120 }
2121
2122 width = round_up(width, 16);
2123 height = round_up(height, 16);
2124
2125 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2126 __func__, ctx->idx, width, height);
2127
Philipp Zabela500a932014-07-11 06:36:13 -03002128 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002129 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2130 v4l2_err(&dev->v4l2_dev,
2131 "not enough framebuffers to decode (%d < %d)\n",
2132 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2133 return -EINVAL;
2134 }
2135
Philipp Zabel52c41672014-07-11 06:36:19 -03002136 if (src_fourcc == V4L2_PIX_FMT_H264) {
2137 u32 left_right;
2138 u32 top_bottom;
2139
2140 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2141 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2142
2143 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2144 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2145 q_data_dst->rect.width = width - q_data_dst->rect.left -
2146 (left_right & 0x3ff);
2147 q_data_dst->rect.height = height - q_data_dst->rect.top -
2148 (top_bottom & 0x3ff);
2149 }
2150
Philipp Zabel918c66f2013-06-27 06:59:01 -03002151 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2152 if (ret < 0)
2153 return ret;
2154
2155 /* Tell the decoder how many frame buffers we allocated. */
2156 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2157 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2158
2159 if (dev->devtype->product != CODA_DX6) {
2160 /* Set secondary AXI IRAM */
2161 coda_setup_iram(ctx);
2162
2163 coda_write(dev, ctx->iram_info.buf_bit_use,
2164 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2165 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2166 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2167 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2168 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2169 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2170 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2171 coda_write(dev, ctx->iram_info.buf_ovl_use,
2172 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002173 if (dev->devtype->product == CODA_960)
2174 coda_write(dev, ctx->iram_info.buf_btp_use,
2175 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2176 }
2177
2178 if (dev->devtype->product == CODA_960) {
2179 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2180
2181 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2182 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2183 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2184 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2185 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2186 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002187 }
2188
2189 if (src_fourcc == V4L2_PIX_FMT_H264) {
2190 coda_write(dev, ctx->slicebuf.paddr,
2191 CODA_CMD_SET_FRAME_SLICE_BB_START);
2192 coda_write(dev, ctx->slicebuf.size / 1024,
2193 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2194 }
2195
2196 if (dev->devtype->product == CODA_7541) {
2197 int max_mb_x = 1920 / 16;
2198 int max_mb_y = 1088 / 16;
2199 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002200
Philipp Zabel918c66f2013-06-27 06:59:01 -03002201 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2202 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002203 } else if (dev->devtype->product == CODA_960) {
2204 int max_mb_x = 1920 / 16;
2205 int max_mb_y = 1088 / 16;
2206 int max_mb_num = max_mb_x * max_mb_y;
2207
2208 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2209 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002210 }
2211
2212 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2213 v4l2_err(&ctx->dev->v4l2_dev,
2214 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2215 return -ETIMEDOUT;
2216 }
2217
2218 return 0;
2219}
2220
Philipp Zabeld35167a2013-05-23 10:42:59 -03002221static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2222 int header_code, u8 *header, int *size)
2223{
2224 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002225 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002226 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002227 int i;
2228
2229 if (dev->devtype->product == CODA_960)
2230 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002231
2232 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2233 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002234 bufsize = vb2_plane_size(buf, 0);
2235 if (dev->devtype->product == CODA_960)
2236 bufsize /= 1024;
2237 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002238 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2239 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2240 if (ret < 0) {
2241 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2242 return ret;
2243 }
Philipp Zabel89548442014-07-11 06:36:17 -03002244
2245 if (dev->devtype->product == CODA_960) {
2246 for (i = 63; i > 0; i--)
2247 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2248 break;
2249 *size = i + 1;
2250 } else {
2251 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2252 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2253 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002254 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2255
2256 return 0;
2257}
2258
Philipp Zabel89548442014-07-11 06:36:17 -03002259static int coda_start_encoding(struct coda_ctx *ctx);
2260
Javier Martin186b2502012-07-26 05:53:35 -03002261static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2262{
2263 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2264 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002265 struct coda_dev *dev = ctx->dev;
2266 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002267 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002268 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002269
Philipp Zabelb96904e2013-05-23 10:42:58 -03002270 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002271 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2272 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03002273 /* copy the buffers that where queued before streamon */
2274 mutex_lock(&ctx->bitstream_mutex);
2275 coda_fill_bitstream(ctx);
2276 mutex_unlock(&ctx->bitstream_mutex);
2277
Philipp Zabel918c66f2013-06-27 06:59:01 -03002278 if (coda_get_bitstream_payload(ctx) < 512)
2279 return -EINVAL;
2280 } else {
2281 if (count < 1)
2282 return -EINVAL;
2283 }
2284
2285 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002286 } else {
2287 if (count < 1)
2288 return -EINVAL;
2289
2290 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002291 }
Javier Martin186b2502012-07-26 05:53:35 -03002292
Philipp Zabelb96904e2013-05-23 10:42:58 -03002293 /* Don't start the coda unless both queues are on */
2294 if (!(ctx->streamon_out & ctx->streamon_cap))
2295 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002296
Philipp Zabeleb107512013-09-30 10:34:45 -03002297 /* Allow decoder device_run with no new buffers queued */
2298 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002299 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002300
Philipp Zabelb96904e2013-05-23 10:42:58 -03002301 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002302 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002303 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002304
Philipp Zabelb96904e2013-05-23 10:42:58 -03002305 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2306 q_data_dst->fourcc);
2307 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002308 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2309 return -EINVAL;
2310 }
2311
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002312 /* Allocate per-instance buffers */
2313 ret = coda_alloc_context_buffers(ctx, q_data_src);
2314 if (ret < 0)
2315 return ret;
2316
Philipp Zabel918c66f2013-06-27 06:59:01 -03002317 if (ctx->inst_type == CODA_INST_DECODER) {
2318 mutex_lock(&dev->coda_mutex);
2319 ret = coda_start_decoding(ctx);
2320 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002321 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002322 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002323 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002324 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002325 } else {
2326 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002327 }
2328
Philipp Zabel89548442014-07-11 06:36:17 -03002329 ctx->initialized = 1;
2330 return ret;
2331}
2332
2333static int coda_start_encoding(struct coda_ctx *ctx)
2334{
2335 struct coda_dev *dev = ctx->dev;
2336 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2337 struct coda_q_data *q_data_src, *q_data_dst;
2338 u32 bitstream_buf, bitstream_size;
2339 struct vb2_buffer *buf;
2340 int gamma, ret, value;
2341 u32 dst_fourcc;
2342
2343 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2344 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2345 dst_fourcc = q_data_dst->fourcc;
2346
Philipp Zabel14604e32014-07-11 06:36:22 -03002347 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002348 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2349 bitstream_size = q_data_dst->sizeimage;
2350
Javier Martin186b2502012-07-26 05:53:35 -03002351 if (!coda_is_initialized(dev)) {
2352 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2353 return -EFAULT;
2354 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002355
2356 mutex_lock(&dev->coda_mutex);
2357
Javier Martin186b2502012-07-26 05:53:35 -03002358 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002359 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2360 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002361 switch (dev->devtype->product) {
2362 case CODA_DX6:
2363 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2364 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2365 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002366 case CODA_960:
2367 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2368 /* fallthrough */
2369 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002370 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2371 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002372 break;
Javier Martin186b2502012-07-26 05:53:35 -03002373 }
2374
Philipp Zabel89548442014-07-11 06:36:17 -03002375 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2376 value &= ~(1 << 2 | 0x7 << 9);
2377 ctx->frame_mem_ctrl = value;
2378 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2379
Philipp Zabel10436672012-07-02 09:03:55 -03002380 if (dev->devtype->product == CODA_DX6) {
2381 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002382 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002383 }
Javier Martin186b2502012-07-26 05:53:35 -03002384
2385 /* Could set rotation here if needed */
2386 switch (dev->devtype->product) {
2387 case CODA_DX6:
2388 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002389 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002390 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002391 case CODA_7541:
2392 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2393 value = (round_up(q_data_src->width, 16) &
2394 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2395 value |= (round_up(q_data_src->height, 16) &
2396 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2397 break;
2398 }
2399 /* fallthrough */
2400 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002401 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002402 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002403 }
Javier Martin186b2502012-07-26 05:53:35 -03002404 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2405 coda_write(dev, ctx->params.framerate,
2406 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2407
Philipp Zabelb96904e2013-05-23 10:42:58 -03002408 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002409 switch (dst_fourcc) {
2410 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002411 if (dev->devtype->product == CODA_960)
2412 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2413 else
2414 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002415 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2416 break;
2417 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002418 if (dev->devtype->product == CODA_960)
2419 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2420 else
2421 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002422 if (ctx->params.h264_deblk_enabled) {
2423 value = ((ctx->params.h264_deblk_alpha &
2424 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2425 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2426 ((ctx->params.h264_deblk_beta &
2427 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2428 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2429 } else {
2430 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2431 }
2432 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002433 break;
2434 default:
2435 v4l2_err(v4l2_dev,
2436 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002437 ret = -EINVAL;
2438 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002439 }
2440
Philipp Zabelc566c782012-08-08 11:59:38 -03002441 switch (ctx->params.slice_mode) {
2442 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2443 value = 0;
2444 break;
2445 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2446 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2447 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002448 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002449 break;
2450 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2451 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2452 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2453 value |= 1 & CODA_SLICING_MODE_MASK;
2454 break;
2455 }
Javier Martin186b2502012-07-26 05:53:35 -03002456 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002457 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002458 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2459
2460 if (ctx->params.bitrate) {
2461 /* Rate control enabled */
2462 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2463 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002464 if (dev->devtype->product == CODA_960)
2465 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002466 } else {
2467 value = 0;
2468 }
2469 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2470
2471 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002472 coda_write(dev, ctx->params.intra_refresh,
2473 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002474
2475 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2476 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2477
Javier Martin186b2502012-07-26 05:53:35 -03002478
Philipp Zabel89548442014-07-11 06:36:17 -03002479 value = 0;
2480 if (dev->devtype->product == CODA_960)
2481 gamma = CODA9_DEFAULT_GAMMA;
2482 else
2483 gamma = CODA_DEFAULT_GAMMA;
2484 if (gamma > 0) {
2485 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2486 CODA_CMD_ENC_SEQ_RC_GAMMA);
2487 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002488
2489 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2490 coda_write(dev,
2491 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2492 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2493 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2494 }
Philipp Zabel89548442014-07-11 06:36:17 -03002495 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002496 if (ctx->params.h264_max_qp)
2497 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002498 if (CODA_DEFAULT_GAMMA > 0)
2499 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002500 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002501 if (CODA_DEFAULT_GAMMA > 0) {
2502 if (dev->devtype->product == CODA_DX6)
2503 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2504 else
2505 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2506 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002507 if (ctx->params.h264_min_qp)
2508 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2509 if (ctx->params.h264_max_qp)
2510 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002511 }
Javier Martin186b2502012-07-26 05:53:35 -03002512 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2513
Philipp Zabel89548442014-07-11 06:36:17 -03002514 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2515
Philipp Zabelc2d22512013-06-21 03:55:28 -03002516 coda_setup_iram(ctx);
2517
Javier Martin186b2502012-07-26 05:53:35 -03002518 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002519 switch (dev->devtype->product) {
2520 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002521 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002522 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002523 break;
2524 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002525 coda_write(dev, ctx->iram_info.search_ram_paddr,
2526 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2527 coda_write(dev, ctx->iram_info.search_ram_size,
2528 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002529 break;
2530 case CODA_960:
2531 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2532 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002533 }
Javier Martin186b2502012-07-26 05:53:35 -03002534 }
2535
Philipp Zabelfcb62822013-05-23 10:43:00 -03002536 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2537 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002538 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002539 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002540 }
2541
Philipp Zabelfcb62822013-05-23 10:43:00 -03002542 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2543 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2544 ret = -EFAULT;
2545 goto out;
2546 }
Javier Martin186b2502012-07-26 05:53:35 -03002547
Philipp Zabel89548442014-07-11 06:36:17 -03002548 if (dev->devtype->product == CODA_960)
2549 ctx->num_internal_frames = 4;
2550 else
2551 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002552 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002553 if (ret < 0) {
2554 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2555 goto out;
2556 }
Javier Martin186b2502012-07-26 05:53:35 -03002557
Philipp Zabelec25f682012-07-20 08:54:29 -03002558 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002559 coda_write(dev, q_data_src->bytesperline,
2560 CODA_CMD_SET_FRAME_BUF_STRIDE);
2561 if (dev->devtype->product == CODA_7541) {
2562 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002563 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002564 }
Philipp Zabel10436672012-07-02 09:03:55 -03002565 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002566 coda_write(dev, ctx->iram_info.buf_bit_use,
2567 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2568 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2569 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2570 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2571 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2572 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2573 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2574 coda_write(dev, ctx->iram_info.buf_ovl_use,
2575 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002576 if (dev->devtype->product == CODA_960) {
2577 coda_write(dev, ctx->iram_info.buf_btp_use,
2578 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2579
2580 /* FIXME */
2581 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2582 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2583 }
Philipp Zabel10436672012-07-02 09:03:55 -03002584 }
Philipp Zabel89548442014-07-11 06:36:17 -03002585
Philipp Zabelfcb62822013-05-23 10:43:00 -03002586 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2587 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002588 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002589 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002590 }
2591
2592 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002593 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002594 switch (dst_fourcc) {
2595 case V4L2_PIX_FMT_H264:
2596 /*
2597 * Get SPS in the first frame and copy it to an
2598 * intermediate buffer.
2599 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002600 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2601 &ctx->vpu_header[0][0],
2602 &ctx->vpu_header_size[0]);
2603 if (ret < 0)
2604 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002605
2606 /*
2607 * Get PPS in the first frame and copy it to an
2608 * intermediate buffer.
2609 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002610 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2611 &ctx->vpu_header[1][0],
2612 &ctx->vpu_header_size[1]);
2613 if (ret < 0)
2614 goto out;
2615
Javier Martin3f3f5c72012-10-29 05:20:29 -03002616 /*
2617 * Length of H.264 headers is variable and thus it might not be
2618 * aligned for the coda to append the encoded frame. In that is
2619 * the case a filler NAL must be added to header 2.
2620 */
2621 ctx->vpu_header_size[2] = coda_h264_padding(
2622 (ctx->vpu_header_size[0] +
2623 ctx->vpu_header_size[1]),
2624 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002625 break;
2626 case V4L2_PIX_FMT_MPEG4:
2627 /*
2628 * Get VOS in the first frame and copy it to an
2629 * intermediate buffer
2630 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002631 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2632 &ctx->vpu_header[0][0],
2633 &ctx->vpu_header_size[0]);
2634 if (ret < 0)
2635 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002636
Philipp Zabeld35167a2013-05-23 10:42:59 -03002637 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2638 &ctx->vpu_header[1][0],
2639 &ctx->vpu_header_size[1]);
2640 if (ret < 0)
2641 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002642
Philipp Zabeld35167a2013-05-23 10:42:59 -03002643 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2644 &ctx->vpu_header[2][0],
2645 &ctx->vpu_header_size[2]);
2646 if (ret < 0)
2647 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002648 break;
2649 default:
2650 /* No more formats need to save headers at the moment */
2651 break;
2652 }
2653
Philipp Zabeld35167a2013-05-23 10:42:59 -03002654out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002655 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002656 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002657}
2658
Hans Verkuile37559b2014-04-17 02:47:21 -03002659static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002660{
2661 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002662 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002663
2664 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002665 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002666 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002667 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002668
Philipp Zabel89548442014-07-11 06:36:17 -03002669 if (ctx->inst_type == CODA_INST_DECODER &&
2670 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2671 /* if this decoder instance is running, set the stream end flag */
2672 if (dev->devtype->product == CODA_960) {
2673 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2674
2675 val |= CODA_BIT_STREAM_END_FLAG;
2676 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2677 ctx->bit_stream_param = val;
2678 }
2679 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002680 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2681
2682 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002683 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002684 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002685 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002686 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002687
2688 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002689 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002690 }
2691
Philipp Zabel918c66f2013-06-27 06:59:01 -03002692 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002693 struct coda_timestamp *ts;
2694
2695 while (!list_empty(&ctx->timestamp_list)) {
2696 ts = list_first_entry(&ctx->timestamp_list,
2697 struct coda_timestamp, list);
2698 list_del(&ts->list);
2699 kfree(ts);
2700 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002701 kfifo_init(&ctx->bitstream_fifo,
2702 ctx->bitstream.vaddr, ctx->bitstream.size);
2703 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002704 }
Javier Martin186b2502012-07-26 05:53:35 -03002705}
2706
Philipp Zabel121cacf2014-07-18 07:22:42 -03002707static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03002708 .queue_setup = coda_queue_setup,
2709 .buf_prepare = coda_buf_prepare,
2710 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002711 .start_streaming = coda_start_streaming,
2712 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002713 .wait_prepare = vb2_ops_wait_prepare,
2714 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002715};
2716
2717static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2718{
2719 struct coda_ctx *ctx =
2720 container_of(ctrl->handler, struct coda_ctx, ctrls);
2721
2722 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2723 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2724
2725 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002726 case V4L2_CID_HFLIP:
2727 if (ctrl->val)
2728 ctx->params.rot_mode |= CODA_MIR_HOR;
2729 else
2730 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2731 break;
2732 case V4L2_CID_VFLIP:
2733 if (ctrl->val)
2734 ctx->params.rot_mode |= CODA_MIR_VER;
2735 else
2736 ctx->params.rot_mode &= ~CODA_MIR_VER;
2737 break;
Javier Martin186b2502012-07-26 05:53:35 -03002738 case V4L2_CID_MPEG_VIDEO_BITRATE:
2739 ctx->params.bitrate = ctrl->val / 1000;
2740 break;
2741 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2742 ctx->params.gop_size = ctrl->val;
2743 break;
2744 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2745 ctx->params.h264_intra_qp = ctrl->val;
2746 break;
2747 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2748 ctx->params.h264_inter_qp = ctrl->val;
2749 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002750 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2751 ctx->params.h264_min_qp = ctrl->val;
2752 break;
2753 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2754 ctx->params.h264_max_qp = ctrl->val;
2755 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002756 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2757 ctx->params.h264_deblk_alpha = ctrl->val;
2758 break;
2759 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2760 ctx->params.h264_deblk_beta = ctrl->val;
2761 break;
2762 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2763 ctx->params.h264_deblk_enabled = (ctrl->val ==
2764 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2765 break;
Javier Martin186b2502012-07-26 05:53:35 -03002766 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2767 ctx->params.mpeg4_intra_qp = ctrl->val;
2768 break;
2769 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2770 ctx->params.mpeg4_inter_qp = ctrl->val;
2771 break;
2772 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2773 ctx->params.slice_mode = ctrl->val;
2774 break;
2775 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2776 ctx->params.slice_max_mb = ctrl->val;
2777 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002778 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2779 ctx->params.slice_max_bits = ctrl->val * 8;
2780 break;
Javier Martin186b2502012-07-26 05:53:35 -03002781 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2782 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002783 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2784 ctx->params.intra_refresh = ctrl->val;
2785 break;
Javier Martin186b2502012-07-26 05:53:35 -03002786 default:
2787 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2788 "Invalid control, id=%d, val=%d\n",
2789 ctrl->id, ctrl->val);
2790 return -EINVAL;
2791 }
2792
2793 return 0;
2794}
2795
2796static struct v4l2_ctrl_ops coda_ctrl_ops = {
2797 .s_ctrl = coda_s_ctrl,
2798};
2799
2800static int coda_ctrls_setup(struct coda_ctx *ctx)
2801{
2802 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2803
2804 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002805 V4L2_CID_HFLIP, 0, 1, 1, 0);
2806 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2807 V4L2_CID_VFLIP, 0, 1, 1, 0);
2808 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002809 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2810 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2811 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2812 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002813 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002814 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002815 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002816 if (ctx->dev->devtype->product != CODA_960) {
2817 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2818 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2819 }
2820 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2821 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002822 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002823 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2824 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2825 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2826 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2827 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2828 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2829 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2830 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002831 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2832 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2833 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2834 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2835 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002836 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2837 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002838 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2839 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002840 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2841 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002842 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2843 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2844 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2845 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2846 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002847 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2848 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002849
2850 if (ctx->ctrls.error) {
2851 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2852 ctx->ctrls.error);
2853 return -EINVAL;
2854 }
2855
2856 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2857}
2858
Philipp Zabel121cacf2014-07-18 07:22:42 -03002859static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03002860{
Philipp Zabel121cacf2014-07-18 07:22:42 -03002861 vq->drv_priv = ctx;
2862 vq->ops = &coda_qops;
2863 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2864 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2865 vq->lock = &ctx->dev->dev_mutex;
2866
2867 return vb2_queue_init(vq);
2868}
2869
2870static int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2871 struct vb2_queue *dst_vq)
2872{
Javier Martin186b2502012-07-26 05:53:35 -03002873 int ret;
2874
Javier Martin186b2502012-07-26 05:53:35 -03002875 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002876 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002877 src_vq->mem_ops = &vb2_dma_contig_memops;
2878
Philipp Zabel121cacf2014-07-18 07:22:42 -03002879 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002880 if (ret)
2881 return ret;
2882
Javier Martin186b2502012-07-26 05:53:35 -03002883 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002884 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002885 dst_vq->mem_ops = &vb2_dma_contig_memops;
2886
Philipp Zabel121cacf2014-07-18 07:22:42 -03002887 return coda_queue_init(priv, dst_vq);
2888}
2889
2890static int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2891 struct vb2_queue *dst_vq)
2892{
2893 int ret;
2894
2895 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2896 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2897 src_vq->mem_ops = &vb2_dma_contig_memops;
2898
2899 ret = coda_queue_init(priv, src_vq);
2900 if (ret)
2901 return ret;
2902
2903 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2904 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2905 dst_vq->mem_ops = &vb2_dma_contig_memops;
2906
2907 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002908}
2909
Philipp Zabele11f3e62012-07-25 09:16:58 -03002910static int coda_next_free_instance(struct coda_dev *dev)
2911{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002912 int idx = ffz(dev->instance_mask);
2913
2914 if ((idx < 0) ||
2915 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2916 return -EBUSY;
2917
2918 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002919}
2920
Philipp Zabel121cacf2014-07-18 07:22:42 -03002921static int coda_open(struct file *file, enum coda_inst_type inst_type)
Javier Martin186b2502012-07-26 05:53:35 -03002922{
Philipp Zabel121cacf2014-07-18 07:22:42 -03002923 int (*queue_init)(void *priv, struct vb2_queue *src_vq,
2924 struct vb2_queue *dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002925 struct coda_dev *dev = video_drvdata(file);
2926 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002927 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002928 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002929 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002930
Javier Martin186b2502012-07-26 05:53:35 -03002931 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2932 if (!ctx)
2933 return -ENOMEM;
2934
Fabio Estevamf82bc202013-08-21 11:14:16 -03002935 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002936 if (idx < 0) {
2937 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002938 goto err_coda_max;
2939 }
2940 set_bit(idx, &dev->instance_mask);
2941
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002942 name = kasprintf(GFP_KERNEL, "context%d", idx);
2943 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2944 kfree(name);
2945
Philipp Zabel121cacf2014-07-18 07:22:42 -03002946 ctx->inst_type = inst_type;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002947 init_completion(&ctx->completion);
2948 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2949 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002950 v4l2_fh_init(&ctx->fh, video_devdata(file));
2951 file->private_data = &ctx->fh;
2952 v4l2_fh_add(&ctx->fh);
2953 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002954 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002955 switch (dev->devtype->product) {
2956 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002957 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002958 ctx->reg_idx = 0;
2959 break;
2960 default:
2961 ctx->reg_idx = idx;
2962 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002963
Philipp Zabel1e172732014-07-11 06:36:23 -03002964 /* Power up and upload firmware if necessary */
2965 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2966 if (ret < 0) {
2967 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2968 goto err_pm_get;
2969 }
2970
Fabio Estevam79830db2013-08-21 11:14:17 -03002971 ret = clk_prepare_enable(dev->clk_per);
2972 if (ret)
2973 goto err_clk_per;
2974
2975 ret = clk_prepare_enable(dev->clk_ahb);
2976 if (ret)
2977 goto err_clk_ahb;
2978
Javier Martin186b2502012-07-26 05:53:35 -03002979 set_default_params(ctx);
Philipp Zabel121cacf2014-07-18 07:22:42 -03002980 if (inst_type == CODA_INST_ENCODER)
2981 queue_init = coda_encoder_queue_init;
2982 else
2983 queue_init = coda_decoder_queue_init;
2984 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002985 if (IS_ERR(ctx->fh.m2m_ctx)) {
2986 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002987
2988 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2989 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002990 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002991 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002992
Javier Martin186b2502012-07-26 05:53:35 -03002993 ret = coda_ctrls_setup(ctx);
2994 if (ret) {
2995 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002996 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002997 }
2998
2999 ctx->fh.ctrl_handler = &ctx->ctrls;
3000
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003001 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
3002 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003003 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003004 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03003005 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03003006 }
3007
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003008 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
3009 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
3010 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
3011 if (!ctx->bitstream.vaddr) {
3012 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
3013 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03003014 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003015 }
3016 kfifo_init(&ctx->bitstream_fifo,
3017 ctx->bitstream.vaddr, ctx->bitstream.size);
3018 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003019 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03003020 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003021
Javier Martin186b2502012-07-26 05:53:35 -03003022 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003023 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003024 coda_unlock(ctx);
3025
Javier Martin186b2502012-07-26 05:53:35 -03003026 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
3027 ctx->idx, ctx);
3028
3029 return 0;
3030
Fabio Estevamf82bc202013-08-21 11:14:16 -03003031err_dma_writecombine:
3032 coda_free_context_buffers(ctx);
3033 if (ctx->dev->devtype->product == CODA_DX6)
3034 coda_free_aux_buf(dev, &ctx->workbuf);
3035 coda_free_aux_buf(dev, &ctx->parabuf);
3036err_dma_alloc:
3037 v4l2_ctrl_handler_free(&ctx->ctrls);
3038err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03003039 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003040err_ctx_init:
3041 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003042err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03003043 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03003044err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03003045 pm_runtime_put_sync(&dev->plat_dev->dev);
3046err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03003047 v4l2_fh_del(&ctx->fh);
3048 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003049 clear_bit(ctx->idx, &dev->instance_mask);
3050err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03003051 kfree(ctx);
3052 return ret;
3053}
3054
Philipp Zabel121cacf2014-07-18 07:22:42 -03003055static int coda_encoder_open(struct file *file)
3056{
3057 return coda_open(file, CODA_INST_ENCODER);
3058}
3059
3060static int coda_decoder_open(struct file *file)
3061{
3062 return coda_open(file, CODA_INST_DECODER);
3063}
3064
Javier Martin186b2502012-07-26 05:53:35 -03003065static int coda_release(struct file *file)
3066{
3067 struct coda_dev *dev = video_drvdata(file);
3068 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3069
3070 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3071 ctx);
3072
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003073 debugfs_remove_recursive(ctx->debugfs_entry);
3074
Philipp Zabel918c66f2013-06-27 06:59:01 -03003075 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003076 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003077
3078 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003079 if (ctx->initialized) {
3080 queue_work(dev->workqueue, &ctx->seq_end_work);
3081 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003082 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003083
3084 coda_free_framebuffers(ctx);
3085
Javier Martin186b2502012-07-26 05:53:35 -03003086 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003087 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003088 coda_unlock(ctx);
3089
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003090 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3091 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003092 coda_free_context_buffers(ctx);
3093 if (ctx->dev->devtype->product == CODA_DX6)
3094 coda_free_aux_buf(dev, &ctx->workbuf);
3095
3096 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003097 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003098 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003099 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003100 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003101 v4l2_fh_del(&ctx->fh);
3102 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003103 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003104 kfree(ctx);
3105
3106 return 0;
3107}
3108
Philipp Zabel121cacf2014-07-18 07:22:42 -03003109static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03003110 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03003111 .open = coda_encoder_open,
3112 .release = coda_release,
3113 .poll = v4l2_m2m_fop_poll,
3114 .unlocked_ioctl = video_ioctl2,
3115 .mmap = v4l2_m2m_fop_mmap,
3116};
3117
3118static const struct v4l2_file_operations coda_decoder_fops = {
3119 .owner = THIS_MODULE,
3120 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03003121 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003122 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003123 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003124 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003125};
3126
Philipp Zabel918c66f2013-06-27 06:59:01 -03003127static void coda_finish_decode(struct coda_ctx *ctx)
3128{
3129 struct coda_dev *dev = ctx->dev;
3130 struct coda_q_data *q_data_src;
3131 struct coda_q_data *q_data_dst;
3132 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003133 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003134 int width, height;
3135 int decoded_idx;
3136 int display_idx;
3137 u32 src_fourcc;
3138 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003139 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003140 u32 val;
3141
Philipp Zabel14604e32014-07-11 06:36:22 -03003142 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003143
3144 /* Update kfifo out pointer from coda bitstream read pointer */
3145 coda_kfifo_sync_from_device(ctx);
3146
3147 /*
3148 * in stream-end mode, the read pointer can overshoot the write pointer
3149 * by up to 512 bytes
3150 */
3151 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
Michael Olbrich390503b2014-07-18 07:22:39 -03003152 if (coda_get_bitstream_payload(ctx) >= CODA_MAX_FRAME_SIZE - 512)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003153 kfifo_init(&ctx->bitstream_fifo,
3154 ctx->bitstream.vaddr, ctx->bitstream.size);
3155 }
3156
3157 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3158 src_fourcc = q_data_src->fourcc;
3159
3160 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3161 if (val != 1)
3162 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3163
3164 success = val & 0x1;
3165 if (!success)
3166 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3167
3168 if (src_fourcc == V4L2_PIX_FMT_H264) {
3169 if (val & (1 << 3))
3170 v4l2_err(&dev->v4l2_dev,
3171 "insufficient PS buffer space (%d bytes)\n",
3172 ctx->psbuf.size);
3173 if (val & (1 << 2))
3174 v4l2_err(&dev->v4l2_dev,
3175 "insufficient slice buffer space (%d bytes)\n",
3176 ctx->slicebuf.size);
3177 }
3178
3179 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3180 width = (val >> 16) & 0xffff;
3181 height = val & 0xffff;
3182
3183 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3184
Philipp Zabel52c41672014-07-11 06:36:19 -03003185 /* frame crop information */
3186 if (src_fourcc == V4L2_PIX_FMT_H264) {
3187 u32 left_right;
3188 u32 top_bottom;
3189
3190 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3191 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3192
3193 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3194 /* Keep current crop information */
3195 } else {
3196 struct v4l2_rect *rect = &q_data_dst->rect;
3197
3198 rect->left = left_right >> 16 & 0xffff;
3199 rect->top = top_bottom >> 16 & 0xffff;
3200 rect->width = width - rect->left -
3201 (left_right & 0xffff);
3202 rect->height = height - rect->top -
3203 (top_bottom & 0xffff);
3204 }
3205 } else {
3206 /* no cropping */
3207 }
3208
Philipp Zabel410e5e42014-07-11 06:36:32 -03003209 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3210 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003211 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003212 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003213
3214 if (dev->devtype->product == CODA_7541) {
3215 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3216 if (val == 0) {
3217 /* not enough bitstream data */
3218 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3219 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003220 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003221 return;
3222 }
3223 }
3224
3225 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3226
3227 /*
3228 * The previous display frame was copied out by the rotator,
3229 * now it can be overwritten again
3230 */
3231 if (ctx->display_idx >= 0 &&
3232 ctx->display_idx < ctx->num_internal_frames) {
3233 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3234 coda_write(dev, ctx->frm_dis_flg,
3235 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3236 }
3237
3238 /*
3239 * The index of the last decoded frame, not necessarily in
3240 * display order, and the index of the next display frame.
3241 * The latter could have been decoded in a previous run.
3242 */
3243 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3244 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3245
3246 if (decoded_idx == -1) {
3247 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003248 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3249 ctx->sequence_offset++;
3250 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003251 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003252 } else if (decoded_idx == -2) {
3253 /* no frame was decoded, we still return the remaining buffers */
3254 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3255 v4l2_err(&dev->v4l2_dev,
3256 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003257 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003258 ts = list_first_entry(&ctx->timestamp_list,
3259 struct coda_timestamp, list);
3260 list_del(&ts->list);
3261 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003262 val -= ctx->sequence_offset;
3263 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003264 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003265 "sequence number mismatch (%d(%d) != %d)\n",
3266 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003267 }
3268 ctx->frame_timestamps[decoded_idx] = *ts;
3269 kfree(ts);
3270
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003271 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3272 if (val == 0)
3273 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3274 else if (val == 1)
3275 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3276 else
3277 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003278
3279 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003280 }
3281
3282 if (display_idx == -1) {
3283 /*
3284 * no more frames to be decoded, but there could still
3285 * be rotator output to dequeue
3286 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003287 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003288 } else if (display_idx == -3) {
3289 /* possibly prescan failure */
3290 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3291 v4l2_err(&dev->v4l2_dev,
3292 "presentation frame index out of range: %d\n",
3293 display_idx);
3294 }
3295
3296 /* If a frame was copied out, return it */
3297 if (ctx->display_idx >= 0 &&
3298 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003299 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003300 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3301
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003302 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3303 V4L2_BUF_FLAG_PFRAME |
3304 V4L2_BUF_FLAG_BFRAME);
3305 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003306 ts = &ctx->frame_timestamps[ctx->display_idx];
3307 dst_buf->v4l2_buf.timecode = ts->timecode;
3308 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003309
Philipp Zabel918c66f2013-06-27 06:59:01 -03003310 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3311
Philipp Zabel410e5e42014-07-11 06:36:32 -03003312 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3313 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003314
3315 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3316 "job finished: decoding frame (%d) (%s)\n",
3317 dst_buf->v4l2_buf.sequence,
3318 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3319 "KEYFRAME" : "PFRAME");
3320 } else {
3321 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3322 "job finished: no frame decoded\n");
3323 }
3324
3325 /* The rotator will copy the current display frame next time */
3326 ctx->display_idx = display_idx;
3327}
3328
3329static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003330{
Philipp Zabelec25f682012-07-20 08:54:29 -03003331 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003332 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003333 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003334
Philipp Zabel14604e32014-07-11 06:36:22 -03003335 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3336 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003337
3338 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003339 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003340 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3341
Javier Martin186b2502012-07-26 05:53:35 -03003342 /* Calculate bytesused field */
3343 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003344 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3345 ctx->vpu_header_size[0] +
3346 ctx->vpu_header_size[1] +
3347 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003348 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003349 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003350 }
3351
3352 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3353 wr_ptr - start_ptr);
3354
3355 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3356 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3357
Philipp Zabel51660282013-09-30 10:34:49 -03003358 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003359 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3360 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3361 } else {
3362 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3363 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3364 }
3365
Kamil Debskiccd571c2013-04-24 10:38:24 -03003366 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003367 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3368 dst_buf->v4l2_buf.flags |=
3369 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003370 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3371
Philipp Zabelec25f682012-07-20 08:54:29 -03003372 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003373
Philipp Zabel14604e32014-07-11 06:36:22 -03003374 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003375 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3376
3377 ctx->gopcounter--;
3378 if (ctx->gopcounter < 0)
3379 ctx->gopcounter = ctx->params.gop_size - 1;
3380
3381 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3382 "job finished: encoding frame (%d) (%s)\n",
3383 dst_buf->v4l2_buf.sequence,
3384 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3385 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003386}
3387
3388static irqreturn_t coda_irq_handler(int irq, void *data)
3389{
3390 struct coda_dev *dev = data;
3391 struct coda_ctx *ctx;
3392
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003393 /* read status register to attend the IRQ */
3394 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3395 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3396 CODA_REG_BIT_INT_CLEAR);
3397
3398 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3399 if (ctx == NULL) {
3400 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3401 mutex_unlock(&dev->coda_mutex);
3402 return IRQ_HANDLED;
3403 }
3404
3405 if (ctx->aborting) {
3406 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3407 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003408 }
3409
3410 if (coda_isbusy(ctx->dev)) {
3411 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3412 "coda is still busy!!!!\n");
3413 return IRQ_NONE;
3414 }
3415
Philipp Zabel32b6f202014-07-11 06:36:20 -03003416 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003417
3418 return IRQ_HANDLED;
3419}
3420
3421static u32 coda_supported_firmwares[] = {
3422 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003423 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003424 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003425};
3426
3427static bool coda_firmware_supported(u32 vernum)
3428{
3429 int i;
3430
3431 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3432 if (vernum == coda_supported_firmwares[i])
3433 return true;
3434 return false;
3435}
3436
Philipp Zabel87048bb2012-07-02 07:03:43 -03003437static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003438{
Javier Martin186b2502012-07-26 05:53:35 -03003439 u32 data;
3440 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003441 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003442
Fabio Estevam79830db2013-08-21 11:14:17 -03003443 ret = clk_prepare_enable(dev->clk_per);
3444 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003445 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003446
3447 ret = clk_prepare_enable(dev->clk_ahb);
3448 if (ret)
3449 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003450
Philipp Zabel8f452842014-07-11 06:36:35 -03003451 if (dev->rstc)
3452 reset_control_reset(dev->rstc);
3453
Javier Martin186b2502012-07-26 05:53:35 -03003454 /*
3455 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003456 * The 16-bit chars in the code buffer are in memory access
3457 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003458 * Data in this SRAM survives a reboot.
3459 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003460 p = (u16 *)dev->codebuf.vaddr;
3461 if (dev->devtype->product == CODA_DX6) {
3462 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3463 data = CODA_DOWN_ADDRESS_SET(i) |
3464 CODA_DOWN_DATA_SET(p[i ^ 1]);
3465 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3466 }
3467 } else {
3468 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3469 data = CODA_DOWN_ADDRESS_SET(i) |
3470 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3471 3 - (i % 4)]);
3472 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3473 }
Javier Martin186b2502012-07-26 05:53:35 -03003474 }
Javier Martin186b2502012-07-26 05:53:35 -03003475
Philipp Zabel9acf7692013-05-23 10:42:56 -03003476 /* Clear registers */
3477 for (i = 0; i < 64; i++)
3478 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3479
Javier Martin186b2502012-07-26 05:53:35 -03003480 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003481 if (dev->devtype->product == CODA_960 ||
3482 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003483 coda_write(dev, dev->tempbuf.paddr,
3484 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003485 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003486 } else {
3487 coda_write(dev, dev->workbuf.paddr,
3488 CODA_REG_BIT_WORK_BUF_ADDR);
3489 }
Javier Martin186b2502012-07-26 05:53:35 -03003490 coda_write(dev, dev->codebuf.paddr,
3491 CODA_REG_BIT_CODE_BUF_ADDR);
3492 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3493
3494 /* Set default values */
3495 switch (dev->devtype->product) {
3496 case CODA_DX6:
3497 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3498 break;
3499 default:
3500 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3501 }
Philipp Zabel89548442014-07-11 06:36:17 -03003502 if (dev->devtype->product == CODA_960)
3503 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3504 else
3505 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003506
3507 if (dev->devtype->product != CODA_DX6)
3508 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3509
Javier Martin186b2502012-07-26 05:53:35 -03003510 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3511 CODA_REG_BIT_INT_ENABLE);
3512
3513 /* Reset VPU and start processor */
3514 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3515 data |= CODA_REG_RESET_ENABLE;
3516 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3517 udelay(10);
3518 data &= ~CODA_REG_RESET_ENABLE;
3519 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3520 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3521
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003522 clk_disable_unprepare(dev->clk_ahb);
3523 clk_disable_unprepare(dev->clk_per);
3524
3525 return 0;
3526
3527err_clk_ahb:
3528 clk_disable_unprepare(dev->clk_per);
3529err_clk_per:
3530 return ret;
3531}
3532
3533static int coda_check_firmware(struct coda_dev *dev)
3534{
3535 u16 product, major, minor, release;
3536 u32 data;
3537 int ret;
3538
3539 ret = clk_prepare_enable(dev->clk_per);
3540 if (ret)
3541 goto err_clk_per;
3542
3543 ret = clk_prepare_enable(dev->clk_ahb);
3544 if (ret)
3545 goto err_clk_ahb;
3546
Javier Martin186b2502012-07-26 05:53:35 -03003547 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3548 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3549 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3550 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3551 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3552 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003553 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003554 ret = -EIO;
3555 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003556 }
3557
Philipp Zabel89548442014-07-11 06:36:17 -03003558 if (dev->devtype->product == CODA_960) {
3559 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3560 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3561 data);
3562 }
3563
Javier Martin186b2502012-07-26 05:53:35 -03003564 /* Check we are compatible with the loaded firmware */
3565 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3566 product = CODA_FIRMWARE_PRODUCT(data);
3567 major = CODA_FIRMWARE_MAJOR(data);
3568 minor = CODA_FIRMWARE_MINOR(data);
3569 release = CODA_FIRMWARE_RELEASE(data);
3570
3571 clk_disable_unprepare(dev->clk_per);
3572 clk_disable_unprepare(dev->clk_ahb);
3573
3574 if (product != dev->devtype->product) {
3575 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3576 " Version: %u.%u.%u\n",
3577 coda_product_name(dev->devtype->product),
3578 coda_product_name(product), major, minor, release);
3579 return -EINVAL;
3580 }
3581
3582 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3583 coda_product_name(product));
3584
3585 if (coda_firmware_supported(data)) {
3586 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3587 major, minor, release);
3588 } else {
3589 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3590 "%u.%u.%u\n", major, minor, release);
3591 }
3592
3593 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003594
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003595err_run_cmd:
3596 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003597err_clk_ahb:
3598 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003599err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003600 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003601}
3602
Philipp Zabel121cacf2014-07-18 07:22:42 -03003603static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
3604{
3605 vfd->release = video_device_release_empty,
3606 vfd->lock = &dev->dev_mutex;
3607 vfd->v4l2_dev = &dev->v4l2_dev;
3608 vfd->vfl_dir = VFL_DIR_M2M;
3609 video_set_drvdata(vfd, dev);
3610
3611 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
3612}
3613
Javier Martin186b2502012-07-26 05:53:35 -03003614static void coda_fw_callback(const struct firmware *fw, void *context)
3615{
3616 struct coda_dev *dev = context;
3617 struct platform_device *pdev = dev->plat_dev;
3618 int ret;
3619
3620 if (!fw) {
3621 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3622 return;
3623 }
3624
3625 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003626 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3627 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003628 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003629 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3630 return;
3631 }
3632
Philipp Zabel87048bb2012-07-02 07:03:43 -03003633 /* Copy the whole firmware image to the code buffer */
3634 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3635 release_firmware(fw);
3636
Philipp Zabel1e172732014-07-11 06:36:23 -03003637 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3638 /*
3639 * Enabling power temporarily will cause coda_hw_init to be
3640 * called via coda_runtime_resume by the pm domain.
3641 */
3642 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3643 if (ret < 0) {
3644 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3645 ret);
3646 return;
3647 }
3648
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003649 ret = coda_check_firmware(dev);
3650 if (ret < 0)
3651 return;
3652
Philipp Zabel1e172732014-07-11 06:36:23 -03003653 pm_runtime_put_sync(&dev->plat_dev->dev);
3654 } else {
3655 /*
3656 * If runtime pm is disabled or pm_domain is not set,
3657 * initialize once manually.
3658 */
3659 ret = coda_hw_init(dev);
3660 if (ret < 0) {
3661 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3662 return;
3663 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003664
3665 ret = coda_check_firmware(dev);
3666 if (ret < 0)
3667 return;
Javier Martin186b2502012-07-26 05:53:35 -03003668 }
3669
Javier Martin186b2502012-07-26 05:53:35 -03003670 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3671 if (IS_ERR(dev->alloc_ctx)) {
3672 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3673 return;
3674 }
3675
3676 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3677 if (IS_ERR(dev->m2m_dev)) {
3678 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3679 goto rel_ctx;
3680 }
3681
Philipp Zabel121cacf2014-07-18 07:22:42 -03003682 dev->vfd[0].fops = &coda_encoder_fops,
3683 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
3684 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
3685 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03003686 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03003687 v4l2_err(&dev->v4l2_dev,
3688 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03003689 goto rel_m2m;
3690 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03003691
3692 dev->vfd[1].fops = &coda_decoder_fops,
3693 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
3694 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
3695 ret = coda_register_device(dev, &dev->vfd[1]);
3696 if (ret) {
3697 v4l2_err(&dev->v4l2_dev,
3698 "Failed to register decoder video device\n");
3699 goto rel_m2m;
3700 }
3701
3702 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
3703 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03003704
3705 return;
3706
3707rel_m2m:
3708 v4l2_m2m_release(dev->m2m_dev);
3709rel_ctx:
3710 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3711}
3712
3713static int coda_firmware_request(struct coda_dev *dev)
3714{
3715 char *fw = dev->devtype->firmware;
3716
3717 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3718 coda_product_name(dev->devtype->product));
3719
3720 return request_firmware_nowait(THIS_MODULE, true,
3721 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3722}
3723
3724enum coda_platform {
3725 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003726 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003727 CODA_IMX6Q,
3728 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003729};
3730
Emil Goodec06d8752012-08-14 17:44:42 -03003731static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003732 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003733 .firmware = "v4l-codadx6-imx27.bin",
3734 .product = CODA_DX6,
3735 .codecs = codadx6_codecs,
3736 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3737 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003738 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03003739 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003740 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003741 .firmware = "v4l-coda7541-imx53.bin",
3742 .product = CODA_7541,
3743 .codecs = coda7_codecs,
3744 .num_codecs = ARRAY_SIZE(coda7_codecs),
3745 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003746 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003747 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003748 },
Philipp Zabel89548442014-07-11 06:36:17 -03003749 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003750 .firmware = "v4l-coda960-imx6q.bin",
3751 .product = CODA_960,
3752 .codecs = coda9_codecs,
3753 .num_codecs = ARRAY_SIZE(coda9_codecs),
3754 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003755 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003756 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03003757 },
3758 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003759 .firmware = "v4l-coda960-imx6dl.bin",
3760 .product = CODA_960,
3761 .codecs = coda9_codecs,
3762 .num_codecs = ARRAY_SIZE(coda9_codecs),
3763 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003764 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003765 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03003766 },
Javier Martin186b2502012-07-26 05:53:35 -03003767};
3768
3769static struct platform_device_id coda_platform_ids[] = {
3770 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003771 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003772 { /* sentinel */ }
3773};
3774MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3775
3776#ifdef CONFIG_OF
3777static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003778 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003779 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003780 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3781 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003782 { /* sentinel */ }
3783};
3784MODULE_DEVICE_TABLE(of, coda_dt_ids);
3785#endif
3786
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003787static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003788{
3789 const struct of_device_id *of_id =
3790 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3791 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003792 struct coda_platform_data *pdata = pdev->dev.platform_data;
3793 struct device_node *np = pdev->dev.of_node;
3794 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003795 struct coda_dev *dev;
3796 struct resource *res;
3797 int ret, irq;
3798
3799 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3800 if (!dev) {
3801 dev_err(&pdev->dev, "Not enough memory for %s\n",
3802 CODA_NAME);
3803 return -ENOMEM;
3804 }
3805
3806 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003807 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003808
3809 dev->plat_dev = pdev;
3810 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3811 if (IS_ERR(dev->clk_per)) {
3812 dev_err(&pdev->dev, "Could not get per clock\n");
3813 return PTR_ERR(dev->clk_per);
3814 }
3815
3816 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3817 if (IS_ERR(dev->clk_ahb)) {
3818 dev_err(&pdev->dev, "Could not get ahb clock\n");
3819 return PTR_ERR(dev->clk_ahb);
3820 }
3821
3822 /* Get memory for physical registers */
3823 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003824 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3825 if (IS_ERR(dev->regs_base))
3826 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003827
3828 /* IRQ */
3829 irq = platform_get_irq(pdev, 0);
3830 if (irq < 0) {
3831 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03003832 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03003833 }
3834
Fabio Estevam08c8d142014-06-04 15:46:24 -03003835 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
3836 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
3837 if (ret < 0) {
3838 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3839 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003840 }
3841
Philipp Zabelee27aa92014-07-24 16:50:03 -03003842 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03003843 if (IS_ERR(dev->rstc)) {
3844 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03003845 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03003846 dev->rstc = NULL;
3847 } else {
3848 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3849 return ret;
3850 }
3851 }
3852
Philipp Zabel657eee72013-04-29 16:17:14 -07003853 /* Get IRAM pool from device tree or platform data */
3854 pool = of_get_named_gen_pool(np, "iram", 0);
3855 if (!pool && pdata)
3856 pool = dev_get_gen_pool(pdata->iram_dev);
3857 if (!pool) {
3858 dev_err(&pdev->dev, "iram pool not available\n");
3859 return -ENOMEM;
3860 }
3861 dev->iram_pool = pool;
3862
Javier Martin186b2502012-07-26 05:53:35 -03003863 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3864 if (ret)
3865 return ret;
3866
3867 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003868 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003869
3870 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3871
3872 if (of_id) {
3873 dev->devtype = of_id->data;
3874 } else if (pdev_id) {
3875 dev->devtype = &coda_devdata[pdev_id->driver_data];
3876 } else {
3877 v4l2_device_unregister(&dev->v4l2_dev);
3878 return -EINVAL;
3879 }
3880
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003881 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3882 if (!dev->debugfs_root)
3883 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3884
Javier Martin186b2502012-07-26 05:53:35 -03003885 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003886 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003887 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003888 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003889 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003890 if (ret < 0) {
3891 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3892 v4l2_device_unregister(&dev->v4l2_dev);
3893 return ret;
3894 }
Javier Martin186b2502012-07-26 05:53:35 -03003895 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003896
3897 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003898 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003899 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003900 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003901 if (ret < 0) {
3902 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3903 v4l2_device_unregister(&dev->v4l2_dev);
3904 return ret;
3905 }
Javier Martin186b2502012-07-26 05:53:35 -03003906 }
3907
Philipp Zabel401e9722014-07-11 06:36:43 -03003908 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003909 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3910 &dev->iram.paddr);
3911 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003912 dev_err(&pdev->dev, "unable to alloc iram\n");
3913 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003914 }
3915
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003916 dev->iram.blob.data = dev->iram.vaddr;
3917 dev->iram.blob.size = dev->iram.size;
3918 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
3919 &dev->iram.blob);
3920
Philipp Zabel32b6f202014-07-11 06:36:20 -03003921 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3922 if (!dev->workqueue) {
3923 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3924 return -ENOMEM;
3925 }
3926
Javier Martin186b2502012-07-26 05:53:35 -03003927 platform_set_drvdata(pdev, dev);
3928
Philipp Zabel1e172732014-07-11 06:36:23 -03003929 pm_runtime_enable(&pdev->dev);
3930
Javier Martin186b2502012-07-26 05:53:35 -03003931 return coda_firmware_request(dev);
3932}
3933
3934static int coda_remove(struct platform_device *pdev)
3935{
3936 struct coda_dev *dev = platform_get_drvdata(pdev);
3937
Philipp Zabel121cacf2014-07-18 07:22:42 -03003938 video_unregister_device(&dev->vfd[0]);
3939 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03003940 if (dev->m2m_dev)
3941 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003942 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003943 if (dev->alloc_ctx)
3944 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3945 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003946 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003947 if (dev->iram.vaddr)
3948 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3949 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003950 coda_free_aux_buf(dev, &dev->codebuf);
3951 coda_free_aux_buf(dev, &dev->tempbuf);
3952 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003953 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03003954 return 0;
3955}
3956
Philipp Zabel1e172732014-07-11 06:36:23 -03003957#ifdef CONFIG_PM_RUNTIME
3958static int coda_runtime_resume(struct device *dev)
3959{
3960 struct coda_dev *cdev = dev_get_drvdata(dev);
3961 int ret = 0;
3962
Philipp Zabel65919e62014-07-18 07:22:36 -03003963 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03003964 ret = coda_hw_init(cdev);
3965 if (ret)
3966 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3967 }
3968
3969 return ret;
3970}
3971#endif
3972
3973static const struct dev_pm_ops coda_pm_ops = {
3974 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3975};
3976
Javier Martin186b2502012-07-26 05:53:35 -03003977static struct platform_driver coda_driver = {
3978 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003979 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003980 .driver = {
3981 .name = CODA_NAME,
3982 .owner = THIS_MODULE,
3983 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003984 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003985 },
3986 .id_table = coda_platform_ids,
3987};
3988
3989module_platform_driver(coda_driver);
3990
3991MODULE_LICENSE("GPL");
3992MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3993MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");