blob: 840402f57e2b9494a601f999fd7989c9b6f3334a [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;
132 struct video_device vfd;
133 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
353 timeout = jiffies + msecs_to_jiffies(100);
354 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
355 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
356 if (time_after(jiffies, timeout))
357 return -ETIME;
358 cpu_relax();
359 }
360
361 ret = reset_control_reset(dev->rstc);
362 if (ret < 0)
363 return ret;
364
365 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
366 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
367 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
368 ret = coda_wait_timeout(dev);
369 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
370
371 return ret;
372}
373
Javier Martin186b2502012-07-26 05:53:35 -0300374static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
375 enum v4l2_buf_type type)
376{
377 switch (type) {
378 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
379 return &(ctx->q_data[V4L2_M2M_SRC]);
380 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
381 return &(ctx->q_data[V4L2_M2M_DST]);
382 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300383 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300384 }
Javier Martin186b2502012-07-26 05:53:35 -0300385}
386
387/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300388 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300389 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300390static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300391 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300392 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300393 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300394 },
395 {
396 .name = "YUV 4:2:0 Planar, YCrCb",
397 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300398 },
399 {
400 .name = "H264 Encoded Stream",
401 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300402 },
403 {
404 .name = "MPEG4 Encoded Stream",
405 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300406 },
407};
408
Philipp Zabelb96904e2013-05-23 10:42:58 -0300409#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
410 { mode, src_fourcc, dst_fourcc, max_w, max_h }
411
412/*
413 * Arrays of codecs supported by each given version of Coda:
414 * i.MX27 -> codadx6
415 * i.MX5x -> coda7
416 * i.MX6 -> coda960
417 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
418 */
419static struct coda_codec codadx6_codecs[] = {
420 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
421 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300422};
423
Philipp Zabelb96904e2013-05-23 10:42:58 -0300424static struct coda_codec coda7_codecs[] = {
425 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
426 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300427 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
428 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300429};
430
Philipp Zabel89548442014-07-11 06:36:17 -0300431static struct coda_codec coda9_codecs[] = {
432 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
433 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
434 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
435 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
436};
437
Philipp Zabelb96904e2013-05-23 10:42:58 -0300438static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300439{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300440 switch (fourcc) {
441 case V4L2_PIX_FMT_YUV420:
442 case V4L2_PIX_FMT_YVU420:
443 return true;
444 default:
445 return false;
446 }
447}
Javier Martin186b2502012-07-26 05:53:35 -0300448
Philipp Zabelb96904e2013-05-23 10:42:58 -0300449/*
450 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
451 * tables.
452 */
453static u32 coda_format_normalize_yuv(u32 fourcc)
454{
455 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
456}
457
458static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
459 int dst_fourcc)
460{
461 struct coda_codec *codecs = dev->devtype->codecs;
462 int num_codecs = dev->devtype->num_codecs;
463 int k;
464
465 src_fourcc = coda_format_normalize_yuv(src_fourcc);
466 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
467 if (src_fourcc == dst_fourcc)
468 return NULL;
469
470 for (k = 0; k < num_codecs; k++) {
471 if (codecs[k].src_fourcc == src_fourcc &&
472 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300473 break;
474 }
475
Philipp Zabelb96904e2013-05-23 10:42:58 -0300476 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300477 return NULL;
478
Philipp Zabelb96904e2013-05-23 10:42:58 -0300479 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300480}
481
Philipp Zabel57625592013-09-30 10:34:51 -0300482static void coda_get_max_dimensions(struct coda_dev *dev,
483 struct coda_codec *codec,
484 int *max_w, int *max_h)
485{
486 struct coda_codec *codecs = dev->devtype->codecs;
487 int num_codecs = dev->devtype->num_codecs;
488 unsigned int w, h;
489 int k;
490
491 if (codec) {
492 w = codec->max_w;
493 h = codec->max_h;
494 } else {
495 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
496 w = max(w, codecs[k].max_w);
497 h = max(h, codecs[k].max_h);
498 }
499 }
500
501 if (max_w)
502 *max_w = w;
503 if (max_h)
504 *max_h = h;
505}
506
Philipp Zabel927933f2013-09-30 10:34:48 -0300507static char *coda_product_name(int product)
508{
509 static char buf[9];
510
511 switch (product) {
512 case CODA_DX6:
513 return "CodaDx6";
514 case CODA_7541:
515 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300516 case CODA_960:
517 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300518 default:
519 snprintf(buf, sizeof(buf), "(0x%04x)", product);
520 return buf;
521 }
522}
523
Javier Martin186b2502012-07-26 05:53:35 -0300524/*
525 * V4L2 ioctl() operations.
526 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300527static int coda_querycap(struct file *file, void *priv,
528 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300529{
Philipp Zabel927933f2013-09-30 10:34:48 -0300530 struct coda_ctx *ctx = fh_to_ctx(priv);
531
Javier Martin186b2502012-07-26 05:53:35 -0300532 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300533 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
534 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300535 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300536 /*
537 * This is only a mem-to-mem video device. The capture and output
538 * device capability flags are left only for backward compatibility
539 * and are scheduled for removal.
540 */
541 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
542 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300543 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
544
545 return 0;
546}
547
548static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300549 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300550{
551 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300552 struct coda_codec *codecs = ctx->dev->devtype->codecs;
553 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300554 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300555 int num_codecs = ctx->dev->devtype->num_codecs;
556 int num_formats = ARRAY_SIZE(coda_formats);
557 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300558
559 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300560 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300561 if (coda_format_is_yuv(formats[i].fourcc) &&
562 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300563 if (num == f->index)
564 break;
565 ++num;
566 continue;
567 }
568 /* Compressed formats may be supported, check the codec list */
569 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300570 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300571 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300572 formats[i].fourcc == codecs[k].dst_fourcc &&
573 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300574 break;
575 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
576 formats[i].fourcc == codecs[k].src_fourcc)
577 break;
578 }
579 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300580 if (num == f->index)
581 break;
582 ++num;
583 }
584 }
585
586 if (i < num_formats) {
587 fmt = &formats[i];
588 strlcpy(f->description, fmt->name, sizeof(f->description));
589 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300590 if (!coda_format_is_yuv(fmt->fourcc))
591 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300592 return 0;
593 }
594
595 /* Format not found */
596 return -EINVAL;
597}
598
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300599static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
600 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300601{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300602 struct coda_ctx *ctx = fh_to_ctx(priv);
603 struct vb2_queue *src_vq;
604 struct coda_q_data *q_data_src;
605
606 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300607 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300608 if (vb2_is_streaming(src_vq)) {
609 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
610
611 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
612 q_data_src->fourcc);
613 }
614
615 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300616}
617
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300618static int coda_enum_fmt_vid_out(struct file *file, void *priv,
619 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300620{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300621 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300622}
623
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300624static int coda_g_fmt(struct file *file, void *priv,
625 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300626{
Javier Martin186b2502012-07-26 05:53:35 -0300627 struct coda_q_data *q_data;
628 struct coda_ctx *ctx = fh_to_ctx(priv);
629
Javier Martin186b2502012-07-26 05:53:35 -0300630 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300631 if (!q_data)
632 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300633
634 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300635 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300636 f->fmt.pix.width = q_data->width;
637 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300638 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300639
640 f->fmt.pix.sizeimage = q_data->sizeimage;
641 f->fmt.pix.colorspace = ctx->colorspace;
642
643 return 0;
644}
645
Philipp Zabel57625592013-09-30 10:34:51 -0300646static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
647 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300648{
Philipp Zabel57625592013-09-30 10:34:51 -0300649 struct coda_dev *dev = ctx->dev;
650 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300651 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300652 enum v4l2_field field;
653
654 field = f->fmt.pix.field;
655 if (field == V4L2_FIELD_ANY)
656 field = V4L2_FIELD_NONE;
657 else if (V4L2_FIELD_NONE != field)
658 return -EINVAL;
659
660 /* V4L2 specification suggests the driver corrects the format struct
661 * if any of the dimensions is unsupported */
662 f->fmt.pix.field = field;
663
Philipp Zabel57625592013-09-30 10:34:51 -0300664 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
665 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
666 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
667 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300668
Philipp Zabel57625592013-09-30 10:34:51 -0300669 switch (f->fmt.pix.pixelformat) {
670 case V4L2_PIX_FMT_YUV420:
671 case V4L2_PIX_FMT_YVU420:
672 case V4L2_PIX_FMT_H264:
673 case V4L2_PIX_FMT_MPEG4:
674 case V4L2_PIX_FMT_JPEG:
675 break;
676 default:
677 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300678 if (!q_data)
679 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300680 f->fmt.pix.pixelformat = q_data->fourcc;
681 }
682
683 switch (f->fmt.pix.pixelformat) {
684 case V4L2_PIX_FMT_YUV420:
685 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300686 /* Frame stride must be multiple of 8, but 16 for h.264 */
687 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300688 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300689 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300690 break;
691 case V4L2_PIX_FMT_H264:
692 case V4L2_PIX_FMT_MPEG4:
693 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300694 f->fmt.pix.bytesperline = 0;
695 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300696 break;
697 default:
698 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300699 }
700
701 return 0;
702}
703
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300704static int coda_try_fmt_vid_cap(struct file *file, void *priv,
705 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300706{
Javier Martin186b2502012-07-26 05:53:35 -0300707 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300708 struct coda_codec *codec;
709 struct vb2_queue *src_vq;
710 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300711
Philipp Zabel918c66f2013-06-27 06:59:01 -0300712 /*
713 * If the source format is already fixed, try to find a codec that
714 * converts to the given destination format
715 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300716 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300717 if (vb2_is_streaming(src_vq)) {
718 struct coda_q_data *q_data_src;
719
720 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
721 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
722 f->fmt.pix.pixelformat);
723 if (!codec)
724 return -EINVAL;
725 } else {
726 /* Otherwise determine codec by encoded format, if possible */
727 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
728 f->fmt.pix.pixelformat);
729 }
Javier Martin186b2502012-07-26 05:53:35 -0300730
731 f->fmt.pix.colorspace = ctx->colorspace;
732
Philipp Zabel57625592013-09-30 10:34:51 -0300733 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300734 if (ret < 0)
735 return ret;
736
737 /* The h.264 decoder only returns complete 16x16 macroblocks */
738 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300739 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300740 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300741 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300742 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
743 f->fmt.pix.height * 3 / 2;
744 }
745
746 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300747}
748
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300749static int coda_try_fmt_vid_out(struct file *file, void *priv,
750 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300751{
752 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300753 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300754
Philipp Zabelb96904e2013-05-23 10:42:58 -0300755 /* Determine codec by encoded format, returns NULL if raw or invalid */
756 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
757 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300758
759 if (!f->fmt.pix.colorspace)
760 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
761
Philipp Zabel57625592013-09-30 10:34:51 -0300762 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300763}
764
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300765static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300766{
767 struct coda_q_data *q_data;
768 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300769
Philipp Zabel14604e32014-07-11 06:36:22 -0300770 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300771 if (!vq)
772 return -EINVAL;
773
774 q_data = get_q_data(ctx, f->type);
775 if (!q_data)
776 return -EINVAL;
777
778 if (vb2_is_busy(vq)) {
779 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
780 return -EBUSY;
781 }
782
Philipp Zabelb96904e2013-05-23 10:42:58 -0300783 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300784 q_data->width = f->fmt.pix.width;
785 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300786 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300787 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300788 q_data->rect.left = 0;
789 q_data->rect.top = 0;
790 q_data->rect.width = f->fmt.pix.width;
791 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300792
793 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
794 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300795 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300796
797 return 0;
798}
799
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300800static int coda_s_fmt_vid_cap(struct file *file, void *priv,
801 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300802{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300803 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300804 int ret;
805
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300806 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300807 if (ret)
808 return ret;
809
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300810 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300811}
812
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300813static int coda_s_fmt_vid_out(struct file *file, void *priv,
814 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300815{
816 struct coda_ctx *ctx = fh_to_ctx(priv);
817 int ret;
818
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300819 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300820 if (ret)
821 return ret;
822
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300823 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300824 if (ret)
825 ctx->colorspace = f->fmt.pix.colorspace;
826
827 return ret;
828}
829
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300830static int coda_qbuf(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);
834
Philipp Zabel14604e32014-07-11 06:36:22 -0300835 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300836}
837
Philipp Zabel918c66f2013-06-27 06:59:01 -0300838static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
839 struct v4l2_buffer *buf)
840{
841 struct vb2_queue *src_vq;
842
Philipp Zabel14604e32014-07-11 06:36:22 -0300843 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300844
845 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
846 (buf->sequence == (ctx->qsequence - 1)));
847}
848
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300849static int coda_dqbuf(struct file *file, void *priv,
850 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300851{
852 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300853 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300854
Philipp Zabel14604e32014-07-11 06:36:22 -0300855 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300856
857 /* If this is the last capture buffer, emit an end-of-stream event */
858 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
859 coda_buf_is_end_of_stream(ctx, buf)) {
860 const struct v4l2_event eos_event = {
861 .type = V4L2_EVENT_EOS
862 };
863
864 v4l2_event_queue_fh(&ctx->fh, &eos_event);
865 }
866
867 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300868}
869
Philipp Zabel52c41672014-07-11 06:36:19 -0300870static int coda_g_selection(struct file *file, void *fh,
871 struct v4l2_selection *s)
872{
873 struct coda_ctx *ctx = fh_to_ctx(fh);
874 struct coda_q_data *q_data;
875 struct v4l2_rect r, *rsel;
876
877 q_data = get_q_data(ctx, s->type);
878 if (!q_data)
879 return -EINVAL;
880
881 r.left = 0;
882 r.top = 0;
883 r.width = q_data->width;
884 r.height = q_data->height;
885 rsel = &q_data->rect;
886
887 switch (s->target) {
888 case V4L2_SEL_TGT_CROP_DEFAULT:
889 case V4L2_SEL_TGT_CROP_BOUNDS:
890 rsel = &r;
891 /* fallthrough */
892 case V4L2_SEL_TGT_CROP:
893 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
894 return -EINVAL;
895 break;
896 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
897 case V4L2_SEL_TGT_COMPOSE_PADDED:
898 rsel = &r;
899 /* fallthrough */
900 case V4L2_SEL_TGT_COMPOSE:
901 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
902 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
903 return -EINVAL;
904 break;
905 default:
906 return -EINVAL;
907 }
908
909 s->r = *rsel;
910
911 return 0;
912}
913
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300914static int coda_try_decoder_cmd(struct file *file, void *fh,
915 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300916{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300917 if (dc->cmd != V4L2_DEC_CMD_STOP)
918 return -EINVAL;
919
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300920 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300921 return -EINVAL;
922
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300923 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300924 return -EINVAL;
925
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300926 return 0;
927}
928
929static int coda_decoder_cmd(struct file *file, void *fh,
930 struct v4l2_decoder_cmd *dc)
931{
932 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300933 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300934 int ret;
935
936 ret = coda_try_decoder_cmd(file, fh, dc);
937 if (ret < 0)
938 return ret;
939
940 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300941 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300942 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300943
944 /* Set the strem-end flag on this context */
945 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
946
Philipp Zabel89548442014-07-11 06:36:17 -0300947 if ((dev->devtype->product == CODA_960) &&
948 coda_isbusy(dev) &&
949 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
950 /* If this context is currently running, update the hardware flag */
951 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
952 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300953 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300954 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300955
Philipp Zabel918c66f2013-06-27 06:59:01 -0300956 return 0;
957}
958
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300959static int coda_subscribe_event(struct v4l2_fh *fh,
960 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300961{
962 switch (sub->type) {
963 case V4L2_EVENT_EOS:
964 return v4l2_event_subscribe(fh, sub, 0, NULL);
965 default:
966 return v4l2_ctrl_subscribe_event(fh, sub);
967 }
Javier Martin186b2502012-07-26 05:53:35 -0300968}
969
970static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300971 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300972
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300973 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
974 .vidioc_g_fmt_vid_cap = coda_g_fmt,
975 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
976 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300977
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300978 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
979 .vidioc_g_fmt_vid_out = coda_g_fmt,
980 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
981 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300982
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300983 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
984 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300985
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300986 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300987 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300988 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300989 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300990
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300991 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
992 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300993
Philipp Zabel52c41672014-07-11 06:36:19 -0300994 .vidioc_g_selection = coda_g_selection,
995
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300996 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300997 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300998
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300999 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001000 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001001};
1002
Philipp Zabel918c66f2013-06-27 06:59:01 -03001003static int coda_start_decoding(struct coda_ctx *ctx);
1004
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001005static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
1006{
1007 return kfifo_len(&ctx->bitstream_fifo);
1008}
1009
1010static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
1011{
1012 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1013 struct coda_dev *dev = ctx->dev;
1014 u32 rd_ptr;
1015
1016 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1017 kfifo->out = (kfifo->in & ~kfifo->mask) |
1018 (rd_ptr - ctx->bitstream.paddr);
1019 if (kfifo->out > kfifo->in)
1020 kfifo->out -= kfifo->mask + 1;
1021}
1022
1023static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1024{
1025 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1026 struct coda_dev *dev = ctx->dev;
1027 u32 rd_ptr, wr_ptr;
1028
1029 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1030 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1031 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1032 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1033}
1034
1035static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1036{
1037 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1038 struct coda_dev *dev = ctx->dev;
1039 u32 wr_ptr;
1040
1041 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1042 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1043}
1044
1045static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1046{
1047 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1048 u32 n;
1049
1050 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1051 if (n < src_size)
1052 return -ENOSPC;
1053
1054 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1055 ctx->bitstream.size, DMA_TO_DEVICE);
1056
Philipp Zabel846ced92014-07-11 06:36:31 -03001057 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001058
1059 return 0;
1060}
1061
1062static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1063 struct vb2_buffer *src_buf)
1064{
1065 int ret;
1066
1067 if (coda_get_bitstream_payload(ctx) +
1068 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1069 return false;
1070
1071 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1072 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1073 return true;
1074 }
1075
1076 ret = coda_bitstream_queue(ctx, src_buf);
1077 if (ret < 0) {
1078 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1079 return false;
1080 }
1081 /* Sync read pointer to device */
1082 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1083 coda_kfifo_sync_to_device_write(ctx);
1084
Philipp Zabel84e23652014-07-11 06:36:34 -03001085 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001086
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001087 return true;
1088}
1089
1090static void coda_fill_bitstream(struct coda_ctx *ctx)
1091{
1092 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001093 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001094
Philipp Zabel14604e32014-07-11 06:36:22 -03001095 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1096 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001097
1098 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001099 /*
1100 * Source buffer is queued in the bitstream ringbuffer;
1101 * queue the timestamp and mark source buffer as done
1102 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001103 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001104
1105 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1106 if (ts) {
1107 ts->sequence = src_buf->v4l2_buf.sequence;
1108 ts->timecode = src_buf->v4l2_buf.timecode;
1109 ts->timestamp = src_buf->v4l2_buf.timestamp;
1110 list_add_tail(&ts->list, &ctx->timestamp_list);
1111 }
1112
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001113 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1114 } else {
1115 break;
1116 }
1117 }
1118}
1119
Philipp Zabel89548442014-07-11 06:36:17 -03001120static void coda_set_gdi_regs(struct coda_ctx *ctx)
1121{
1122 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1123 struct coda_dev *dev = ctx->dev;
1124 int i;
1125
1126 for (i = 0; i < 16; i++)
1127 coda_write(dev, tiled_map->xy2ca_map[i],
1128 CODA9_GDI_XY2_CAS_0 + 4 * i);
1129 for (i = 0; i < 4; i++)
1130 coda_write(dev, tiled_map->xy2ba_map[i],
1131 CODA9_GDI_XY2_BA_0 + 4 * i);
1132 for (i = 0; i < 16; i++)
1133 coda_write(dev, tiled_map->xy2ra_map[i],
1134 CODA9_GDI_XY2_RAS_0 + 4 * i);
1135 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1136 for (i = 0; i < 32; i++)
1137 coda_write(dev, tiled_map->rbc2axi_map[i],
1138 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1139}
1140
Javier Martin186b2502012-07-26 05:53:35 -03001141/*
1142 * Mem-to-mem operations.
1143 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001144static int coda_prepare_decode(struct coda_ctx *ctx)
1145{
1146 struct vb2_buffer *dst_buf;
1147 struct coda_dev *dev = ctx->dev;
1148 struct coda_q_data *q_data_dst;
1149 u32 stridey, height;
1150 u32 picture_y, picture_cb, picture_cr;
1151
Philipp Zabel14604e32014-07-11 06:36:22 -03001152 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001153 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1154
1155 if (ctx->params.rot_mode & CODA_ROT_90) {
1156 stridey = q_data_dst->height;
1157 height = q_data_dst->width;
1158 } else {
1159 stridey = q_data_dst->width;
1160 height = q_data_dst->height;
1161 }
1162
1163 /* Try to copy source buffer contents into the bitstream ringbuffer */
1164 mutex_lock(&ctx->bitstream_mutex);
1165 coda_fill_bitstream(ctx);
1166 mutex_unlock(&ctx->bitstream_mutex);
1167
1168 if (coda_get_bitstream_payload(ctx) < 512 &&
1169 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1170 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1171 "bitstream payload: %d, skipping\n",
1172 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001173 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001174 return -EAGAIN;
1175 }
1176
1177 /* Run coda_start_decoding (again) if not yet initialized */
1178 if (!ctx->initialized) {
1179 int ret = coda_start_decoding(ctx);
1180 if (ret < 0) {
1181 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001182 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001183 return -EAGAIN;
1184 } else {
1185 ctx->initialized = 1;
1186 }
1187 }
1188
Philipp Zabel89548442014-07-11 06:36:17 -03001189 if (dev->devtype->product == CODA_960)
1190 coda_set_gdi_regs(ctx);
1191
Philipp Zabel918c66f2013-06-27 06:59:01 -03001192 /* Set rotator output */
1193 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1194 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1195 /* Switch Cr and Cb for YVU420 format */
1196 picture_cr = picture_y + stridey * height;
1197 picture_cb = picture_cr + stridey / 2 * height / 2;
1198 } else {
1199 picture_cb = picture_y + stridey * height;
1200 picture_cr = picture_cb + stridey / 2 * height / 2;
1201 }
Philipp Zabel89548442014-07-11 06:36:17 -03001202
1203 if (dev->devtype->product == CODA_960) {
1204 /*
1205 * The CODA960 seems to have an internal list of buffers with
1206 * 64 entries that includes the registered frame buffers as
1207 * well as the rotator buffer output.
1208 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1209 */
1210 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1211 CODA9_CMD_DEC_PIC_ROT_INDEX);
1212 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1213 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1214 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1215 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1216 } else {
1217 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1218 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1219 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1220 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1221 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001222 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1223 CODA_CMD_DEC_PIC_ROT_MODE);
1224
1225 switch (dev->devtype->product) {
1226 case CODA_DX6:
1227 /* TBD */
1228 case CODA_7541:
1229 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1230 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001231 case CODA_960:
1232 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1233 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001234 }
1235
1236 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1237
1238 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1239 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1240
1241 return 0;
1242}
1243
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001244static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001245{
Javier Martin186b2502012-07-26 05:53:35 -03001246 struct coda_q_data *q_data_src, *q_data_dst;
1247 struct vb2_buffer *src_buf, *dst_buf;
1248 struct coda_dev *dev = ctx->dev;
1249 int force_ipicture;
1250 int quant_param = 0;
1251 u32 picture_y, picture_cb, picture_cr;
1252 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1253 u32 dst_fourcc;
1254
Philipp Zabel14604e32014-07-11 06:36:22 -03001255 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1256 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001257 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1258 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001259 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001260
Philipp Zabel918c66f2013-06-27 06:59:01 -03001261 src_buf->v4l2_buf.sequence = ctx->osequence;
1262 dst_buf->v4l2_buf.sequence = ctx->osequence;
1263 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001264
1265 /*
1266 * Workaround coda firmware BUG that only marks the first
1267 * frame as IDR. This is a problem for some decoders that can't
1268 * recover when a frame is lost.
1269 */
1270 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1271 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1272 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1273 } else {
1274 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1275 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1276 }
1277
Philipp Zabel89548442014-07-11 06:36:17 -03001278 if (dev->devtype->product == CODA_960)
1279 coda_set_gdi_regs(ctx);
1280
Javier Martin186b2502012-07-26 05:53:35 -03001281 /*
1282 * Copy headers at the beginning of the first frame for H.264 only.
1283 * In MPEG4 they are already copied by the coda.
1284 */
1285 if (src_buf->v4l2_buf.sequence == 0) {
1286 pic_stream_buffer_addr =
1287 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1288 ctx->vpu_header_size[0] +
1289 ctx->vpu_header_size[1] +
1290 ctx->vpu_header_size[2];
1291 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1292 ctx->vpu_header_size[0] -
1293 ctx->vpu_header_size[1] -
1294 ctx->vpu_header_size[2];
1295 memcpy(vb2_plane_vaddr(dst_buf, 0),
1296 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1297 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1298 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1299 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1300 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1301 ctx->vpu_header_size[2]);
1302 } else {
1303 pic_stream_buffer_addr =
1304 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1305 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1306 }
1307
1308 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1309 force_ipicture = 1;
1310 switch (dst_fourcc) {
1311 case V4L2_PIX_FMT_H264:
1312 quant_param = ctx->params.h264_intra_qp;
1313 break;
1314 case V4L2_PIX_FMT_MPEG4:
1315 quant_param = ctx->params.mpeg4_intra_qp;
1316 break;
1317 default:
1318 v4l2_warn(&ctx->dev->v4l2_dev,
1319 "cannot set intra qp, fmt not supported\n");
1320 break;
1321 }
1322 } else {
1323 force_ipicture = 0;
1324 switch (dst_fourcc) {
1325 case V4L2_PIX_FMT_H264:
1326 quant_param = ctx->params.h264_inter_qp;
1327 break;
1328 case V4L2_PIX_FMT_MPEG4:
1329 quant_param = ctx->params.mpeg4_inter_qp;
1330 break;
1331 default:
1332 v4l2_warn(&ctx->dev->v4l2_dev,
1333 "cannot set inter qp, fmt not supported\n");
1334 break;
1335 }
1336 }
1337
1338 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001339 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001340 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1341
1342
1343 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001344 switch (q_data_src->fourcc) {
1345 case V4L2_PIX_FMT_YVU420:
1346 /* Switch Cb and Cr for YVU420 format */
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001347 picture_cr = picture_y + q_data_src->bytesperline *
1348 q_data_src->height;
1349 picture_cb = picture_cr + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001350 q_data_src->height / 2;
1351 break;
1352 case V4L2_PIX_FMT_YUV420:
1353 default:
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001354 picture_cb = picture_y + q_data_src->bytesperline *
1355 q_data_src->height;
1356 picture_cr = picture_cb + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001357 q_data_src->height / 2;
1358 break;
1359 }
Javier Martin186b2502012-07-26 05:53:35 -03001360
Philipp Zabel89548442014-07-11 06:36:17 -03001361 if (dev->devtype->product == CODA_960) {
1362 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1363 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1364 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1365
1366 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1367 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1368 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1369 } else {
1370 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1371 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1372 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1373 }
Javier Martin186b2502012-07-26 05:53:35 -03001374 coda_write(dev, force_ipicture << 1 & 0x2,
1375 CODA_CMD_ENC_PIC_OPTION);
1376
1377 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1378 coda_write(dev, pic_stream_buffer_size / 1024,
1379 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001380
1381 if (!ctx->streamon_out) {
1382 /* After streamoff on the output side, set the stream end flag */
1383 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1384 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1385 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001386}
1387
1388static void coda_device_run(void *m2m_priv)
1389{
1390 struct coda_ctx *ctx = m2m_priv;
1391 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001392
1393 queue_work(dev->workqueue, &ctx->pic_run_work);
1394}
1395
1396static void coda_free_framebuffers(struct coda_ctx *ctx);
1397static void coda_free_context_buffers(struct coda_ctx *ctx);
1398
1399static void coda_seq_end_work(struct work_struct *work)
1400{
1401 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1402 struct coda_dev *dev = ctx->dev;
1403
1404 mutex_lock(&ctx->buffer_mutex);
1405 mutex_lock(&dev->coda_mutex);
1406
1407 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1408 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1409 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1410 v4l2_err(&dev->v4l2_dev,
1411 "CODA_COMMAND_SEQ_END failed\n");
1412 }
1413
1414 kfifo_init(&ctx->bitstream_fifo,
1415 ctx->bitstream.vaddr, ctx->bitstream.size);
1416
1417 coda_free_framebuffers(ctx);
1418 coda_free_context_buffers(ctx);
1419
1420 mutex_unlock(&dev->coda_mutex);
1421 mutex_unlock(&ctx->buffer_mutex);
1422}
1423
1424static void coda_finish_decode(struct coda_ctx *ctx);
1425static void coda_finish_encode(struct coda_ctx *ctx);
1426
1427static void coda_pic_run_work(struct work_struct *work)
1428{
1429 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1430 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001431 int ret;
1432
1433 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001434 mutex_lock(&dev->coda_mutex);
1435
Philipp Zabel918c66f2013-06-27 06:59:01 -03001436 if (ctx->inst_type == CODA_INST_DECODER) {
1437 ret = coda_prepare_decode(ctx);
1438 if (ret < 0) {
1439 mutex_unlock(&dev->coda_mutex);
1440 mutex_unlock(&ctx->buffer_mutex);
1441 /* job_finish scheduled by prepare_decode */
1442 return;
1443 }
1444 } else {
1445 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001446 }
1447
Philipp Zabelc2d22512013-06-21 03:55:28 -03001448 if (dev->devtype->product != CODA_DX6)
1449 coda_write(dev, ctx->iram_info.axi_sram_use,
1450 CODA7_REG_BIT_AXI_SRAM_USE);
1451
Philipp Zabel918c66f2013-06-27 06:59:01 -03001452 if (ctx->inst_type == CODA_INST_DECODER)
1453 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001454 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001455
1456 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1457 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001458
1459 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001460
1461 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001462 } else if (!ctx->aborting) {
1463 if (ctx->inst_type == CODA_INST_DECODER)
1464 coda_finish_decode(ctx);
1465 else
1466 coda_finish_encode(ctx);
1467 }
1468
1469 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1470 queue_work(dev->workqueue, &ctx->seq_end_work);
1471
1472 mutex_unlock(&dev->coda_mutex);
1473 mutex_unlock(&ctx->buffer_mutex);
1474
Philipp Zabel14604e32014-07-11 06:36:22 -03001475 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001476}
1477
1478static int coda_job_ready(void *m2m_priv)
1479{
1480 struct coda_ctx *ctx = m2m_priv;
1481
1482 /*
1483 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001484 * and 1 frame are needed. In the decoder case,
1485 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001486 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001487 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001488 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001489 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1490 "not ready: not enough video buffers.\n");
1491 return 0;
1492 }
1493
Philipp Zabel14604e32014-07-11 06:36:22 -03001494 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001495 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1496 "not ready: not enough video capture buffers.\n");
1497 return 0;
1498 }
1499
Philipp Zabel84e23652014-07-11 06:36:34 -03001500 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001501 ((ctx->inst_type == CODA_INST_DECODER) &&
1502 (coda_get_bitstream_payload(ctx) < 512) &&
1503 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1504 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1505 "%d: not ready: not enough bitstream data.\n",
1506 ctx->idx);
1507 return 0;
1508 }
1509
Philipp Zabel3e748262013-05-23 10:43:01 -03001510 if (ctx->aborting) {
1511 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1512 "not ready: aborting\n");
1513 return 0;
1514 }
1515
Javier Martin186b2502012-07-26 05:53:35 -03001516 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1517 "job ready\n");
1518 return 1;
1519}
1520
1521static void coda_job_abort(void *priv)
1522{
1523 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001524
1525 ctx->aborting = 1;
1526
1527 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1528 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001529}
1530
1531static void coda_lock(void *m2m_priv)
1532{
1533 struct coda_ctx *ctx = m2m_priv;
1534 struct coda_dev *pcdev = ctx->dev;
1535 mutex_lock(&pcdev->dev_mutex);
1536}
1537
1538static void coda_unlock(void *m2m_priv)
1539{
1540 struct coda_ctx *ctx = m2m_priv;
1541 struct coda_dev *pcdev = ctx->dev;
1542 mutex_unlock(&pcdev->dev_mutex);
1543}
1544
1545static struct v4l2_m2m_ops coda_m2m_ops = {
1546 .device_run = coda_device_run,
1547 .job_ready = coda_job_ready,
1548 .job_abort = coda_job_abort,
1549 .lock = coda_lock,
1550 .unlock = coda_unlock,
1551};
1552
Philipp Zabel89548442014-07-11 06:36:17 -03001553static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1554{
1555 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1556 int luma_map, chro_map, i;
1557
1558 memset(tiled_map, 0, sizeof(*tiled_map));
1559
1560 luma_map = 64;
1561 chro_map = 64;
1562 tiled_map->map_type = tiled_map_type;
1563 for (i = 0; i < 16; i++)
1564 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1565 for (i = 0; i < 4; i++)
1566 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1567 for (i = 0; i < 16; i++)
1568 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1569
1570 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1571 tiled_map->xy2rbc_config = 0;
1572 } else {
1573 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1574 tiled_map_type);
1575 return;
1576 }
1577}
1578
Javier Martin186b2502012-07-26 05:53:35 -03001579static void set_default_params(struct coda_ctx *ctx)
1580{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001581 int max_w;
1582 int max_h;
1583
1584 ctx->codec = &ctx->dev->devtype->codecs[0];
1585 max_w = ctx->codec->max_w;
1586 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001587
1588 ctx->params.codec_mode = CODA_MODE_INVALID;
1589 ctx->colorspace = V4L2_COLORSPACE_REC709;
1590 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001591 ctx->aborting = 0;
1592
1593 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001594 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1595 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1596 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1597 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001598 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001599 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1600 ctx->q_data[V4L2_M2M_DST].width = max_w;
1601 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001602 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001603 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001604 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1605 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1606 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1607 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001608
1609 if (ctx->dev->devtype->product == CODA_960)
1610 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001611}
1612
1613/*
1614 * Queue operations
1615 */
1616static int coda_queue_setup(struct vb2_queue *vq,
1617 const struct v4l2_format *fmt,
1618 unsigned int *nbuffers, unsigned int *nplanes,
1619 unsigned int sizes[], void *alloc_ctxs[])
1620{
1621 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001622 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001623 unsigned int size;
1624
Philipp Zabele34db062012-08-29 08:22:00 -03001625 q_data = get_q_data(ctx, vq->type);
1626 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001627
1628 *nplanes = 1;
1629 sizes[0] = size;
1630
1631 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1632
1633 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1634 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1635
1636 return 0;
1637}
1638
1639static int coda_buf_prepare(struct vb2_buffer *vb)
1640{
1641 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1642 struct coda_q_data *q_data;
1643
1644 q_data = get_q_data(ctx, vb->vb2_queue->type);
1645
1646 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1647 v4l2_warn(&ctx->dev->v4l2_dev,
1648 "%s data will not fit into plane (%lu < %lu)\n",
1649 __func__, vb2_plane_size(vb, 0),
1650 (long)q_data->sizeimage);
1651 return -EINVAL;
1652 }
1653
Javier Martin186b2502012-07-26 05:53:35 -03001654 return 0;
1655}
1656
1657static void coda_buf_queue(struct vb2_buffer *vb)
1658{
1659 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001660 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001661 struct coda_q_data *q_data;
1662
1663 q_data = get_q_data(ctx, vb->vb2_queue->type);
1664
1665 /*
1666 * In the decoder case, immediately try to copy the buffer into the
1667 * bitstream ringbuffer and mark it as ready to be dequeued.
1668 */
1669 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1670 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1671 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001672 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001673 * the stream end
1674 */
Philipp Zabel89548442014-07-11 06:36:17 -03001675 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001676 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001677 if ((dev->devtype->product == CODA_960) &&
1678 coda_isbusy(dev) &&
1679 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1680 /* if this decoder instance is running, set the stream end flag */
1681 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1682 }
1683 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001684 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001685 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001686 coda_fill_bitstream(ctx);
1687 mutex_unlock(&ctx->bitstream_mutex);
1688 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001689 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001690 }
Javier Martin186b2502012-07-26 05:53:35 -03001691}
1692
Philipp Zabel86eda902013-05-23 10:42:57 -03001693static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1694{
1695 struct coda_dev *dev = ctx->dev;
1696 u32 *p = ctx->parabuf.vaddr;
1697
1698 if (dev->devtype->product == CODA_DX6)
1699 p[index] = value;
1700 else
1701 p[index ^ 1] = value;
1702}
1703
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001704static int coda_alloc_aux_buf(struct coda_dev *dev,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001705 struct coda_aux_buf *buf, size_t size,
1706 const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001707{
1708 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1709 GFP_KERNEL);
1710 if (!buf->vaddr)
1711 return -ENOMEM;
1712
1713 buf->size = size;
1714
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001715 if (name && parent) {
1716 buf->blob.data = buf->vaddr;
1717 buf->blob.size = size;
1718 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
1719 if (!buf->dentry)
1720 dev_warn(&dev->plat_dev->dev,
1721 "failed to create debugfs entry %s\n", name);
1722 }
1723
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001724 return 0;
1725}
1726
1727static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001728 struct coda_aux_buf *buf, size_t size,
1729 const char *name)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001730{
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001731 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001732}
1733
1734static void coda_free_aux_buf(struct coda_dev *dev,
1735 struct coda_aux_buf *buf)
1736{
1737 if (buf->vaddr) {
1738 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1739 buf->vaddr, buf->paddr);
1740 buf->vaddr = NULL;
1741 buf->size = 0;
1742 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001743 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001744}
1745
1746static void coda_free_framebuffers(struct coda_ctx *ctx)
1747{
1748 int i;
1749
1750 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1751 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1752}
1753
Philipp Zabelec25f682012-07-20 08:54:29 -03001754static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1755{
1756 struct coda_dev *dev = ctx->dev;
Philipp Zabel7c3df782014-07-11 06:36:38 -03001757 int width, height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001758 dma_addr_t paddr;
1759 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001760 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001761 int i;
1762
Philipp Zabel7c3df782014-07-11 06:36:38 -03001763 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
1764 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
1765 width = round_up(q_data->width, 16);
1766 height = round_up(q_data->height, 16);
1767 } else {
1768 width = round_up(q_data->width, 8);
1769 height = q_data->height;
1770 }
1771 ysize = width * height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001772
Philipp Zabelec25f682012-07-20 08:54:29 -03001773 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001774 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001775 size_t size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001776 char *name;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001777
Philipp Zabelaed14b02014-07-11 06:36:15 -03001778 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001779 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1780 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001781 size += ysize / 4;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001782 name = kasprintf(GFP_KERNEL, "fb%d", i);
1783 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
1784 size, name);
1785 kfree(name);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001786 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001787 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001788 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001789 }
1790 }
1791
1792 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001793 for (i = 0; i < ctx->num_internal_frames; i++) {
1794 paddr = ctx->internal_frames[i].paddr;
1795 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1796 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1797 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001798
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001799 /* mvcol buffer for h.264 */
1800 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1801 dev->devtype->product != CODA_DX6)
1802 coda_parabuf_write(ctx, 96 + i,
1803 ctx->internal_frames[i].paddr +
1804 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001805 }
1806
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001807 /* mvcol buffer for mpeg4 */
1808 if ((dev->devtype->product != CODA_DX6) &&
1809 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1810 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1811 ysize + ysize/4 + ysize/4);
1812
Philipp Zabelec25f682012-07-20 08:54:29 -03001813 return 0;
1814}
1815
Javier Martin3f3f5c72012-10-29 05:20:29 -03001816static int coda_h264_padding(int size, char *p)
1817{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001818 int nal_size;
1819 int diff;
1820
Javier Martin832fbb52012-10-29 08:34:59 -03001821 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001822 if (diff == 0)
1823 return 0;
1824
Javier Martin832fbb52012-10-29 08:34:59 -03001825 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001826 memcpy(p, coda_filler_nal, nal_size);
1827
1828 /* Add rbsp stop bit and trailing at the end */
1829 *(p + nal_size - 1) = 0x80;
1830
1831 return nal_size;
1832}
1833
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001834static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1835{
1836 phys_addr_t ret;
1837
1838 size = round_up(size, 1024);
1839 if (size > iram->remaining)
1840 return 0;
1841 iram->remaining -= size;
1842
1843 ret = iram->next_paddr;
1844 iram->next_paddr += size;
1845
1846 return ret;
1847}
1848
Philipp Zabelc2d22512013-06-21 03:55:28 -03001849static void coda_setup_iram(struct coda_ctx *ctx)
1850{
1851 struct coda_iram_info *iram_info = &ctx->iram_info;
1852 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001853 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001854 int dbk_bits;
1855 int bit_bits;
1856 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001857
1858 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001859 iram_info->next_paddr = dev->iram.paddr;
1860 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001861
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001862 switch (dev->devtype->product) {
1863 case CODA_7541:
1864 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1865 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1866 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1867 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001868 case CODA_960:
1869 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1870 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1871 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1872 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001873 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001874 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001875 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001876
1877 if (ctx->inst_type == CODA_INST_ENCODER) {
1878 struct coda_q_data *q_data_src;
1879
1880 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1881 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1882
1883 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001884 if (dev->devtype->product == CODA_7541) {
1885 iram_info->search_ram_size = round_up(mb_width * 16 *
1886 36 + 2048, 1024);
1887 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1888 iram_info->search_ram_size);
1889 if (!iram_info->search_ram_paddr) {
1890 pr_err("IRAM is smaller than the search ram size\n");
1891 goto out;
1892 }
1893 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1894 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001895 }
1896
1897 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001898 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1899 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1900 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001901 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001902 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001903
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001904 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1905 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001906 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001907 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001908
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001909 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1910 if (!iram_info->buf_ip_ac_dc_use)
1911 goto out;
1912 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001913
Philipp Zabel8358e762013-06-21 03:55:32 -03001914 /* OVL and BTP disabled for encoder */
1915 } else if (ctx->inst_type == CODA_INST_DECODER) {
1916 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001917
1918 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1919 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001920
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001921 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1922 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1923 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001924 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001925 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001926
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001927 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1928 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001929 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001930 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001931
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001932 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1933 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001934 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001935 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001936
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001937 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001938 }
1939
1940out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001941 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1942 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1943 "IRAM smaller than needed\n");
1944
1945 if (dev->devtype->product == CODA_7541) {
1946 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001947 if (ctx->inst_type == CODA_INST_DECODER) {
1948 /* fw 1.4.50 */
1949 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1950 CODA7_USE_IP_ENABLE);
1951 } else {
1952 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001953 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1954 CODA7_USE_HOST_DBK_ENABLE |
1955 CODA7_USE_IP_ENABLE |
1956 CODA7_USE_DBK_ENABLE);
1957 }
1958 }
1959}
1960
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001961static void coda_free_context_buffers(struct coda_ctx *ctx)
1962{
1963 struct coda_dev *dev = ctx->dev;
1964
Philipp Zabel918c66f2013-06-27 06:59:01 -03001965 coda_free_aux_buf(dev, &ctx->slicebuf);
1966 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001967 if (dev->devtype->product != CODA_DX6)
1968 coda_free_aux_buf(dev, &ctx->workbuf);
1969}
1970
1971static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1972 struct coda_q_data *q_data)
1973{
1974 struct coda_dev *dev = ctx->dev;
1975 size_t size;
1976 int ret;
1977
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001978 if (dev->devtype->product == CODA_DX6)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001979 return 0;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001980
Philipp Zabel918c66f2013-06-27 06:59:01 -03001981 if (ctx->psbuf.vaddr) {
1982 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1983 return -EBUSY;
1984 }
1985 if (ctx->slicebuf.vaddr) {
1986 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1987 return -EBUSY;
1988 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001989 if (ctx->workbuf.vaddr) {
1990 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1991 ret = -EBUSY;
1992 return -ENOMEM;
1993 }
1994
Philipp Zabel918c66f2013-06-27 06:59:01 -03001995 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1996 /* worst case slice size */
1997 size = (DIV_ROUND_UP(q_data->width, 16) *
1998 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001999 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, "slicebuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002000 if (ret < 0) {
2001 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
2002 ctx->slicebuf.size);
2003 return ret;
2004 }
2005 }
2006
2007 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002008 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002009 if (ret < 0) {
2010 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
2011 goto err;
2012 }
2013 }
2014
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002015 size = dev->devtype->workbuf_size;
2016 if (dev->devtype->product == CODA_960 &&
2017 q_data->fourcc == V4L2_PIX_FMT_H264)
2018 size += CODA9_PS_SAVE_SIZE;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002019 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002020 if (ret < 0) {
2021 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2022 ctx->workbuf.size);
2023 goto err;
2024 }
2025
2026 return 0;
2027
2028err:
2029 coda_free_context_buffers(ctx);
2030 return ret;
2031}
2032
Philipp Zabel918c66f2013-06-27 06:59:01 -03002033static int coda_start_decoding(struct coda_ctx *ctx)
2034{
2035 struct coda_q_data *q_data_src, *q_data_dst;
2036 u32 bitstream_buf, bitstream_size;
2037 struct coda_dev *dev = ctx->dev;
2038 int width, height;
2039 u32 src_fourcc;
2040 u32 val;
2041 int ret;
2042
2043 /* Start decoding */
2044 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2045 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2046 bitstream_buf = ctx->bitstream.paddr;
2047 bitstream_size = ctx->bitstream.size;
2048 src_fourcc = q_data_src->fourcc;
2049
2050 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2051
2052 /* Update coda bitstream read and write pointers from kfifo */
2053 coda_kfifo_sync_to_device_full(ctx);
2054
2055 ctx->display_idx = -1;
2056 ctx->frm_dis_flg = 0;
2057 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2058
2059 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2060 CODA_REG_BIT_BIT_STREAM_PARAM);
2061
2062 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2063 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2064 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002065 if ((dev->devtype->product == CODA_7541) ||
2066 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002067 val |= CODA_REORDER_ENABLE;
2068 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2069
2070 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002071 if (dev->devtype->product == CODA_960 &&
2072 src_fourcc == V4L2_PIX_FMT_MPEG4)
2073 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2074 else
2075 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002076 if (src_fourcc == V4L2_PIX_FMT_H264) {
2077 if (dev->devtype->product == CODA_7541) {
2078 coda_write(dev, ctx->psbuf.paddr,
2079 CODA_CMD_DEC_SEQ_PS_BB_START);
2080 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2081 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2082 }
Philipp Zabel89548442014-07-11 06:36:17 -03002083 if (dev->devtype->product == CODA_960) {
2084 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2085 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2086 }
2087 }
2088 if (dev->devtype->product != CODA_960) {
2089 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002090 }
2091
2092 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2093 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2094 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2095 return -ETIMEDOUT;
2096 }
2097
2098 /* Update kfifo out pointer from coda bitstream read pointer */
2099 coda_kfifo_sync_from_device(ctx);
2100
2101 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2102
2103 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2104 v4l2_err(&dev->v4l2_dev,
2105 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2106 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2107 return -EAGAIN;
2108 }
2109
2110 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2111 if (dev->devtype->product == CODA_DX6) {
2112 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2113 height = val & CODADX6_PICHEIGHT_MASK;
2114 } else {
2115 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2116 height = val & CODA7_PICHEIGHT_MASK;
2117 }
2118
2119 if (width > q_data_dst->width || height > q_data_dst->height) {
2120 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2121 width, height, q_data_dst->width, q_data_dst->height);
2122 return -EINVAL;
2123 }
2124
2125 width = round_up(width, 16);
2126 height = round_up(height, 16);
2127
2128 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2129 __func__, ctx->idx, width, height);
2130
Philipp Zabela500a932014-07-11 06:36:13 -03002131 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002132 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2133 v4l2_err(&dev->v4l2_dev,
2134 "not enough framebuffers to decode (%d < %d)\n",
2135 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2136 return -EINVAL;
2137 }
2138
Philipp Zabel52c41672014-07-11 06:36:19 -03002139 if (src_fourcc == V4L2_PIX_FMT_H264) {
2140 u32 left_right;
2141 u32 top_bottom;
2142
2143 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2144 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2145
2146 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2147 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2148 q_data_dst->rect.width = width - q_data_dst->rect.left -
2149 (left_right & 0x3ff);
2150 q_data_dst->rect.height = height - q_data_dst->rect.top -
2151 (top_bottom & 0x3ff);
2152 }
2153
Philipp Zabel918c66f2013-06-27 06:59:01 -03002154 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2155 if (ret < 0)
2156 return ret;
2157
2158 /* Tell the decoder how many frame buffers we allocated. */
2159 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2160 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2161
2162 if (dev->devtype->product != CODA_DX6) {
2163 /* Set secondary AXI IRAM */
2164 coda_setup_iram(ctx);
2165
2166 coda_write(dev, ctx->iram_info.buf_bit_use,
2167 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2168 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2169 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2170 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2171 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2172 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2173 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2174 coda_write(dev, ctx->iram_info.buf_ovl_use,
2175 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002176 if (dev->devtype->product == CODA_960)
2177 coda_write(dev, ctx->iram_info.buf_btp_use,
2178 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2179 }
2180
2181 if (dev->devtype->product == CODA_960) {
2182 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2183
2184 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2185 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2186 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2187 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2188 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2189 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002190 }
2191
2192 if (src_fourcc == V4L2_PIX_FMT_H264) {
2193 coda_write(dev, ctx->slicebuf.paddr,
2194 CODA_CMD_SET_FRAME_SLICE_BB_START);
2195 coda_write(dev, ctx->slicebuf.size / 1024,
2196 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2197 }
2198
2199 if (dev->devtype->product == CODA_7541) {
2200 int max_mb_x = 1920 / 16;
2201 int max_mb_y = 1088 / 16;
2202 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002203
Philipp Zabel918c66f2013-06-27 06:59:01 -03002204 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2205 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002206 } else if (dev->devtype->product == CODA_960) {
2207 int max_mb_x = 1920 / 16;
2208 int max_mb_y = 1088 / 16;
2209 int max_mb_num = max_mb_x * max_mb_y;
2210
2211 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2212 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002213 }
2214
2215 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2216 v4l2_err(&ctx->dev->v4l2_dev,
2217 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2218 return -ETIMEDOUT;
2219 }
2220
2221 return 0;
2222}
2223
Philipp Zabeld35167a2013-05-23 10:42:59 -03002224static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2225 int header_code, u8 *header, int *size)
2226{
2227 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002228 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002229 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002230 int i;
2231
2232 if (dev->devtype->product == CODA_960)
2233 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002234
2235 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2236 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002237 bufsize = vb2_plane_size(buf, 0);
2238 if (dev->devtype->product == CODA_960)
2239 bufsize /= 1024;
2240 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002241 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2242 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2243 if (ret < 0) {
2244 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2245 return ret;
2246 }
Philipp Zabel89548442014-07-11 06:36:17 -03002247
2248 if (dev->devtype->product == CODA_960) {
2249 for (i = 63; i > 0; i--)
2250 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2251 break;
2252 *size = i + 1;
2253 } else {
2254 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2255 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2256 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002257 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2258
2259 return 0;
2260}
2261
Philipp Zabel89548442014-07-11 06:36:17 -03002262static int coda_start_encoding(struct coda_ctx *ctx);
2263
Javier Martin186b2502012-07-26 05:53:35 -03002264static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2265{
2266 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2267 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002268 struct coda_dev *dev = ctx->dev;
2269 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002270 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002271 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002272
Philipp Zabelb96904e2013-05-23 10:42:58 -03002273 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002274 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2275 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2276 if (coda_get_bitstream_payload(ctx) < 512)
2277 return -EINVAL;
2278 } else {
2279 if (count < 1)
2280 return -EINVAL;
2281 }
2282
2283 ctx->streamon_out = 1;
2284
Philipp Zabelb96904e2013-05-23 10:42:58 -03002285 if (coda_format_is_yuv(q_data_src->fourcc))
2286 ctx->inst_type = CODA_INST_ENCODER;
2287 else
2288 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002289 } else {
2290 if (count < 1)
2291 return -EINVAL;
2292
2293 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002294 }
Javier Martin186b2502012-07-26 05:53:35 -03002295
Philipp Zabelb96904e2013-05-23 10:42:58 -03002296 /* Don't start the coda unless both queues are on */
2297 if (!(ctx->streamon_out & ctx->streamon_cap))
2298 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002299
Philipp Zabeleb107512013-09-30 10:34:45 -03002300 /* Allow decoder device_run with no new buffers queued */
2301 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002302 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002303
Philipp Zabelb96904e2013-05-23 10:42:58 -03002304 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002305 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002306 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002307
Philipp Zabelb96904e2013-05-23 10:42:58 -03002308 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2309 q_data_dst->fourcc);
2310 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002311 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2312 return -EINVAL;
2313 }
2314
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002315 /* Allocate per-instance buffers */
2316 ret = coda_alloc_context_buffers(ctx, q_data_src);
2317 if (ret < 0)
2318 return ret;
2319
Philipp Zabel918c66f2013-06-27 06:59:01 -03002320 if (ctx->inst_type == CODA_INST_DECODER) {
2321 mutex_lock(&dev->coda_mutex);
2322 ret = coda_start_decoding(ctx);
2323 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002324 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002325 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002326 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002327 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002328 } else {
2329 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002330 }
2331
Philipp Zabel89548442014-07-11 06:36:17 -03002332 ctx->initialized = 1;
2333 return ret;
2334}
2335
2336static int coda_start_encoding(struct coda_ctx *ctx)
2337{
2338 struct coda_dev *dev = ctx->dev;
2339 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2340 struct coda_q_data *q_data_src, *q_data_dst;
2341 u32 bitstream_buf, bitstream_size;
2342 struct vb2_buffer *buf;
2343 int gamma, ret, value;
2344 u32 dst_fourcc;
2345
2346 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2347 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2348 dst_fourcc = q_data_dst->fourcc;
2349
Philipp Zabel14604e32014-07-11 06:36:22 -03002350 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002351 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2352 bitstream_size = q_data_dst->sizeimage;
2353
Javier Martin186b2502012-07-26 05:53:35 -03002354 if (!coda_is_initialized(dev)) {
2355 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2356 return -EFAULT;
2357 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002358
2359 mutex_lock(&dev->coda_mutex);
2360
Javier Martin186b2502012-07-26 05:53:35 -03002361 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002362 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2363 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002364 switch (dev->devtype->product) {
2365 case CODA_DX6:
2366 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2367 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2368 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002369 case CODA_960:
2370 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2371 /* fallthrough */
2372 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002373 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2374 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002375 break;
Javier Martin186b2502012-07-26 05:53:35 -03002376 }
2377
Philipp Zabel89548442014-07-11 06:36:17 -03002378 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2379 value &= ~(1 << 2 | 0x7 << 9);
2380 ctx->frame_mem_ctrl = value;
2381 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2382
Philipp Zabel10436672012-07-02 09:03:55 -03002383 if (dev->devtype->product == CODA_DX6) {
2384 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002385 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002386 }
Javier Martin186b2502012-07-26 05:53:35 -03002387
2388 /* Could set rotation here if needed */
2389 switch (dev->devtype->product) {
2390 case CODA_DX6:
2391 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002392 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002393 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002394 case CODA_7541:
2395 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2396 value = (round_up(q_data_src->width, 16) &
2397 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2398 value |= (round_up(q_data_src->height, 16) &
2399 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2400 break;
2401 }
2402 /* fallthrough */
2403 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002404 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002405 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002406 }
Javier Martin186b2502012-07-26 05:53:35 -03002407 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2408 coda_write(dev, ctx->params.framerate,
2409 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2410
Philipp Zabelb96904e2013-05-23 10:42:58 -03002411 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002412 switch (dst_fourcc) {
2413 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002414 if (dev->devtype->product == CODA_960)
2415 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2416 else
2417 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002418 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2419 break;
2420 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002421 if (dev->devtype->product == CODA_960)
2422 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2423 else
2424 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002425 if (ctx->params.h264_deblk_enabled) {
2426 value = ((ctx->params.h264_deblk_alpha &
2427 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2428 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2429 ((ctx->params.h264_deblk_beta &
2430 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2431 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2432 } else {
2433 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2434 }
2435 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002436 break;
2437 default:
2438 v4l2_err(v4l2_dev,
2439 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002440 ret = -EINVAL;
2441 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002442 }
2443
Philipp Zabelc566c782012-08-08 11:59:38 -03002444 switch (ctx->params.slice_mode) {
2445 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2446 value = 0;
2447 break;
2448 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2449 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2450 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002451 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002452 break;
2453 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2454 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2455 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2456 value |= 1 & CODA_SLICING_MODE_MASK;
2457 break;
2458 }
Javier Martin186b2502012-07-26 05:53:35 -03002459 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002460 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002461 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2462
2463 if (ctx->params.bitrate) {
2464 /* Rate control enabled */
2465 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2466 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002467 if (dev->devtype->product == CODA_960)
2468 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002469 } else {
2470 value = 0;
2471 }
2472 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2473
2474 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002475 coda_write(dev, ctx->params.intra_refresh,
2476 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002477
2478 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2479 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2480
Javier Martin186b2502012-07-26 05:53:35 -03002481
Philipp Zabel89548442014-07-11 06:36:17 -03002482 value = 0;
2483 if (dev->devtype->product == CODA_960)
2484 gamma = CODA9_DEFAULT_GAMMA;
2485 else
2486 gamma = CODA_DEFAULT_GAMMA;
2487 if (gamma > 0) {
2488 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2489 CODA_CMD_ENC_SEQ_RC_GAMMA);
2490 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002491
2492 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2493 coda_write(dev,
2494 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2495 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2496 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2497 }
Philipp Zabel89548442014-07-11 06:36:17 -03002498 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002499 if (ctx->params.h264_max_qp)
2500 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002501 if (CODA_DEFAULT_GAMMA > 0)
2502 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002503 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002504 if (CODA_DEFAULT_GAMMA > 0) {
2505 if (dev->devtype->product == CODA_DX6)
2506 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2507 else
2508 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2509 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002510 if (ctx->params.h264_min_qp)
2511 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2512 if (ctx->params.h264_max_qp)
2513 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002514 }
Javier Martin186b2502012-07-26 05:53:35 -03002515 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2516
Philipp Zabel89548442014-07-11 06:36:17 -03002517 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2518
Philipp Zabelc2d22512013-06-21 03:55:28 -03002519 coda_setup_iram(ctx);
2520
Javier Martin186b2502012-07-26 05:53:35 -03002521 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002522 switch (dev->devtype->product) {
2523 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002524 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002525 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002526 break;
2527 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002528 coda_write(dev, ctx->iram_info.search_ram_paddr,
2529 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2530 coda_write(dev, ctx->iram_info.search_ram_size,
2531 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002532 break;
2533 case CODA_960:
2534 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2535 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002536 }
Javier Martin186b2502012-07-26 05:53:35 -03002537 }
2538
Philipp Zabelfcb62822013-05-23 10:43:00 -03002539 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2540 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002541 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002542 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002543 }
2544
Philipp Zabelfcb62822013-05-23 10:43:00 -03002545 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2546 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2547 ret = -EFAULT;
2548 goto out;
2549 }
Javier Martin186b2502012-07-26 05:53:35 -03002550
Philipp Zabel89548442014-07-11 06:36:17 -03002551 if (dev->devtype->product == CODA_960)
2552 ctx->num_internal_frames = 4;
2553 else
2554 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002555 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002556 if (ret < 0) {
2557 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2558 goto out;
2559 }
Javier Martin186b2502012-07-26 05:53:35 -03002560
Philipp Zabelec25f682012-07-20 08:54:29 -03002561 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002562 coda_write(dev, q_data_src->bytesperline,
2563 CODA_CMD_SET_FRAME_BUF_STRIDE);
2564 if (dev->devtype->product == CODA_7541) {
2565 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002566 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002567 }
Philipp Zabel10436672012-07-02 09:03:55 -03002568 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002569 coda_write(dev, ctx->iram_info.buf_bit_use,
2570 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2571 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2572 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2573 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2574 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2575 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2576 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2577 coda_write(dev, ctx->iram_info.buf_ovl_use,
2578 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002579 if (dev->devtype->product == CODA_960) {
2580 coda_write(dev, ctx->iram_info.buf_btp_use,
2581 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2582
2583 /* FIXME */
2584 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2585 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2586 }
Philipp Zabel10436672012-07-02 09:03:55 -03002587 }
Philipp Zabel89548442014-07-11 06:36:17 -03002588
Philipp Zabelfcb62822013-05-23 10:43:00 -03002589 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2590 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002591 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002592 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002593 }
2594
2595 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002596 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002597 switch (dst_fourcc) {
2598 case V4L2_PIX_FMT_H264:
2599 /*
2600 * Get SPS in the first frame and copy it to an
2601 * intermediate buffer.
2602 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002603 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2604 &ctx->vpu_header[0][0],
2605 &ctx->vpu_header_size[0]);
2606 if (ret < 0)
2607 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002608
2609 /*
2610 * Get PPS in the first frame and copy it to an
2611 * intermediate buffer.
2612 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002613 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2614 &ctx->vpu_header[1][0],
2615 &ctx->vpu_header_size[1]);
2616 if (ret < 0)
2617 goto out;
2618
Javier Martin3f3f5c72012-10-29 05:20:29 -03002619 /*
2620 * Length of H.264 headers is variable and thus it might not be
2621 * aligned for the coda to append the encoded frame. In that is
2622 * the case a filler NAL must be added to header 2.
2623 */
2624 ctx->vpu_header_size[2] = coda_h264_padding(
2625 (ctx->vpu_header_size[0] +
2626 ctx->vpu_header_size[1]),
2627 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002628 break;
2629 case V4L2_PIX_FMT_MPEG4:
2630 /*
2631 * Get VOS in the first frame and copy it to an
2632 * intermediate buffer
2633 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002634 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2635 &ctx->vpu_header[0][0],
2636 &ctx->vpu_header_size[0]);
2637 if (ret < 0)
2638 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002639
Philipp Zabeld35167a2013-05-23 10:42:59 -03002640 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2641 &ctx->vpu_header[1][0],
2642 &ctx->vpu_header_size[1]);
2643 if (ret < 0)
2644 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002645
Philipp Zabeld35167a2013-05-23 10:42:59 -03002646 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2647 &ctx->vpu_header[2][0],
2648 &ctx->vpu_header_size[2]);
2649 if (ret < 0)
2650 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002651 break;
2652 default:
2653 /* No more formats need to save headers at the moment */
2654 break;
2655 }
2656
Philipp Zabeld35167a2013-05-23 10:42:59 -03002657out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002658 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002659 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002660}
2661
Hans Verkuile37559b2014-04-17 02:47:21 -03002662static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002663{
2664 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002665 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002666
2667 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002668 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002669 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002670 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002671
Philipp Zabel89548442014-07-11 06:36:17 -03002672 if (ctx->inst_type == CODA_INST_DECODER &&
2673 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2674 /* if this decoder instance is running, set the stream end flag */
2675 if (dev->devtype->product == CODA_960) {
2676 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2677
2678 val |= CODA_BIT_STREAM_END_FLAG;
2679 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2680 ctx->bit_stream_param = val;
2681 }
2682 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002683 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2684
2685 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002686 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002687 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002688 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002689 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002690
2691 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002692 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002693 }
2694
Philipp Zabel918c66f2013-06-27 06:59:01 -03002695 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002696 struct coda_timestamp *ts;
2697
2698 while (!list_empty(&ctx->timestamp_list)) {
2699 ts = list_first_entry(&ctx->timestamp_list,
2700 struct coda_timestamp, list);
2701 list_del(&ts->list);
2702 kfree(ts);
2703 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002704 kfifo_init(&ctx->bitstream_fifo,
2705 ctx->bitstream.vaddr, ctx->bitstream.size);
2706 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002707 }
Javier Martin186b2502012-07-26 05:53:35 -03002708}
2709
2710static struct vb2_ops coda_qops = {
2711 .queue_setup = coda_queue_setup,
2712 .buf_prepare = coda_buf_prepare,
2713 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002714 .start_streaming = coda_start_streaming,
2715 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002716 .wait_prepare = vb2_ops_wait_prepare,
2717 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002718};
2719
2720static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2721{
2722 struct coda_ctx *ctx =
2723 container_of(ctrl->handler, struct coda_ctx, ctrls);
2724
2725 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2726 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2727
2728 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002729 case V4L2_CID_HFLIP:
2730 if (ctrl->val)
2731 ctx->params.rot_mode |= CODA_MIR_HOR;
2732 else
2733 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2734 break;
2735 case V4L2_CID_VFLIP:
2736 if (ctrl->val)
2737 ctx->params.rot_mode |= CODA_MIR_VER;
2738 else
2739 ctx->params.rot_mode &= ~CODA_MIR_VER;
2740 break;
Javier Martin186b2502012-07-26 05:53:35 -03002741 case V4L2_CID_MPEG_VIDEO_BITRATE:
2742 ctx->params.bitrate = ctrl->val / 1000;
2743 break;
2744 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2745 ctx->params.gop_size = ctrl->val;
2746 break;
2747 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2748 ctx->params.h264_intra_qp = ctrl->val;
2749 break;
2750 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2751 ctx->params.h264_inter_qp = ctrl->val;
2752 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002753 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2754 ctx->params.h264_min_qp = ctrl->val;
2755 break;
2756 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2757 ctx->params.h264_max_qp = ctrl->val;
2758 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002759 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2760 ctx->params.h264_deblk_alpha = ctrl->val;
2761 break;
2762 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2763 ctx->params.h264_deblk_beta = ctrl->val;
2764 break;
2765 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2766 ctx->params.h264_deblk_enabled = (ctrl->val ==
2767 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2768 break;
Javier Martin186b2502012-07-26 05:53:35 -03002769 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2770 ctx->params.mpeg4_intra_qp = ctrl->val;
2771 break;
2772 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2773 ctx->params.mpeg4_inter_qp = ctrl->val;
2774 break;
2775 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2776 ctx->params.slice_mode = ctrl->val;
2777 break;
2778 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2779 ctx->params.slice_max_mb = ctrl->val;
2780 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002781 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2782 ctx->params.slice_max_bits = ctrl->val * 8;
2783 break;
Javier Martin186b2502012-07-26 05:53:35 -03002784 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2785 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002786 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2787 ctx->params.intra_refresh = ctrl->val;
2788 break;
Javier Martin186b2502012-07-26 05:53:35 -03002789 default:
2790 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2791 "Invalid control, id=%d, val=%d\n",
2792 ctrl->id, ctrl->val);
2793 return -EINVAL;
2794 }
2795
2796 return 0;
2797}
2798
2799static struct v4l2_ctrl_ops coda_ctrl_ops = {
2800 .s_ctrl = coda_s_ctrl,
2801};
2802
2803static int coda_ctrls_setup(struct coda_ctx *ctx)
2804{
2805 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2806
2807 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002808 V4L2_CID_HFLIP, 0, 1, 1, 0);
2809 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2810 V4L2_CID_VFLIP, 0, 1, 1, 0);
2811 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002812 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2813 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2814 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2815 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002816 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002817 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002818 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002819 if (ctx->dev->devtype->product != CODA_960) {
2820 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2821 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2822 }
2823 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2824 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002825 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002826 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2827 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2828 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2829 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2830 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2831 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2832 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2833 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002834 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2835 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2836 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2837 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2838 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002839 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2840 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002841 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2842 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002843 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2844 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002845 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2846 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2847 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2848 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2849 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002850 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2851 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002852
2853 if (ctx->ctrls.error) {
2854 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2855 ctx->ctrls.error);
2856 return -EINVAL;
2857 }
2858
2859 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2860}
2861
2862static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2863 struct vb2_queue *dst_vq)
2864{
2865 struct coda_ctx *ctx = priv;
2866 int ret;
2867
Javier Martin186b2502012-07-26 05:53:35 -03002868 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002869 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002870 src_vq->drv_priv = ctx;
2871 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2872 src_vq->ops = &coda_qops;
2873 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002874 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002875 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002876
2877 ret = vb2_queue_init(src_vq);
2878 if (ret)
2879 return ret;
2880
Javier Martin186b2502012-07-26 05:53:35 -03002881 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002882 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002883 dst_vq->drv_priv = ctx;
2884 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2885 dst_vq->ops = &coda_qops;
2886 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002887 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002888 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002889
2890 return vb2_queue_init(dst_vq);
2891}
2892
Philipp Zabele11f3e62012-07-25 09:16:58 -03002893static int coda_next_free_instance(struct coda_dev *dev)
2894{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002895 int idx = ffz(dev->instance_mask);
2896
2897 if ((idx < 0) ||
2898 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2899 return -EBUSY;
2900
2901 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002902}
2903
Javier Martin186b2502012-07-26 05:53:35 -03002904static int coda_open(struct file *file)
2905{
2906 struct coda_dev *dev = video_drvdata(file);
2907 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002908 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002909 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002910 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002911
Javier Martin186b2502012-07-26 05:53:35 -03002912 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2913 if (!ctx)
2914 return -ENOMEM;
2915
Fabio Estevamf82bc202013-08-21 11:14:16 -03002916 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002917 if (idx < 0) {
2918 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002919 goto err_coda_max;
2920 }
2921 set_bit(idx, &dev->instance_mask);
2922
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002923 name = kasprintf(GFP_KERNEL, "context%d", idx);
2924 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2925 kfree(name);
2926
Philipp Zabel32b6f202014-07-11 06:36:20 -03002927 init_completion(&ctx->completion);
2928 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2929 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002930 v4l2_fh_init(&ctx->fh, video_devdata(file));
2931 file->private_data = &ctx->fh;
2932 v4l2_fh_add(&ctx->fh);
2933 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002934 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002935 switch (dev->devtype->product) {
2936 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002937 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002938 ctx->reg_idx = 0;
2939 break;
2940 default:
2941 ctx->reg_idx = idx;
2942 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002943
Philipp Zabel1e172732014-07-11 06:36:23 -03002944 /* Power up and upload firmware if necessary */
2945 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2946 if (ret < 0) {
2947 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2948 goto err_pm_get;
2949 }
2950
Fabio Estevam79830db2013-08-21 11:14:17 -03002951 ret = clk_prepare_enable(dev->clk_per);
2952 if (ret)
2953 goto err_clk_per;
2954
2955 ret = clk_prepare_enable(dev->clk_ahb);
2956 if (ret)
2957 goto err_clk_ahb;
2958
Javier Martin186b2502012-07-26 05:53:35 -03002959 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002960 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002961 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002962 if (IS_ERR(ctx->fh.m2m_ctx)) {
2963 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002964
2965 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2966 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002967 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002968 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002969
Javier Martin186b2502012-07-26 05:53:35 -03002970 ret = coda_ctrls_setup(ctx);
2971 if (ret) {
2972 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002973 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002974 }
2975
2976 ctx->fh.ctrl_handler = &ctx->ctrls;
2977
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002978 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
2979 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002980 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002981 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002982 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002983 }
2984
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002985 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2986 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2987 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2988 if (!ctx->bitstream.vaddr) {
2989 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2990 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002991 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002992 }
2993 kfifo_init(&ctx->bitstream_fifo,
2994 ctx->bitstream.vaddr, ctx->bitstream.size);
2995 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002996 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002997 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002998
Javier Martin186b2502012-07-26 05:53:35 -03002999 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003000 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003001 coda_unlock(ctx);
3002
Javier Martin186b2502012-07-26 05:53:35 -03003003 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
3004 ctx->idx, ctx);
3005
3006 return 0;
3007
Fabio Estevamf82bc202013-08-21 11:14:16 -03003008err_dma_writecombine:
3009 coda_free_context_buffers(ctx);
3010 if (ctx->dev->devtype->product == CODA_DX6)
3011 coda_free_aux_buf(dev, &ctx->workbuf);
3012 coda_free_aux_buf(dev, &ctx->parabuf);
3013err_dma_alloc:
3014 v4l2_ctrl_handler_free(&ctx->ctrls);
3015err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03003016 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003017err_ctx_init:
3018 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003019err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03003020 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03003021err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03003022 pm_runtime_put_sync(&dev->plat_dev->dev);
3023err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03003024 v4l2_fh_del(&ctx->fh);
3025 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003026 clear_bit(ctx->idx, &dev->instance_mask);
3027err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03003028 kfree(ctx);
3029 return ret;
3030}
3031
3032static int coda_release(struct file *file)
3033{
3034 struct coda_dev *dev = video_drvdata(file);
3035 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3036
3037 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3038 ctx);
3039
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003040 debugfs_remove_recursive(ctx->debugfs_entry);
3041
Philipp Zabel918c66f2013-06-27 06:59:01 -03003042 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003043 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003044
3045 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003046 if (ctx->initialized) {
3047 queue_work(dev->workqueue, &ctx->seq_end_work);
3048 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003049 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003050
3051 coda_free_framebuffers(ctx);
3052
Javier Martin186b2502012-07-26 05:53:35 -03003053 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003054 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003055 coda_unlock(ctx);
3056
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003057 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3058 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003059 coda_free_context_buffers(ctx);
3060 if (ctx->dev->devtype->product == CODA_DX6)
3061 coda_free_aux_buf(dev, &ctx->workbuf);
3062
3063 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003064 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003065 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003066 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003067 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003068 v4l2_fh_del(&ctx->fh);
3069 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003070 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003071 kfree(ctx);
3072
3073 return 0;
3074}
3075
Javier Martin186b2502012-07-26 05:53:35 -03003076static const struct v4l2_file_operations coda_fops = {
3077 .owner = THIS_MODULE,
3078 .open = coda_open,
3079 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003080 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003081 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003082 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003083};
3084
Philipp Zabel918c66f2013-06-27 06:59:01 -03003085static void coda_finish_decode(struct coda_ctx *ctx)
3086{
3087 struct coda_dev *dev = ctx->dev;
3088 struct coda_q_data *q_data_src;
3089 struct coda_q_data *q_data_dst;
3090 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003091 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003092 int width, height;
3093 int decoded_idx;
3094 int display_idx;
3095 u32 src_fourcc;
3096 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003097 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003098 u32 val;
3099
Philipp Zabel14604e32014-07-11 06:36:22 -03003100 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003101
3102 /* Update kfifo out pointer from coda bitstream read pointer */
3103 coda_kfifo_sync_from_device(ctx);
3104
3105 /*
3106 * in stream-end mode, the read pointer can overshoot the write pointer
3107 * by up to 512 bytes
3108 */
3109 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
3110 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
3111 kfifo_init(&ctx->bitstream_fifo,
3112 ctx->bitstream.vaddr, ctx->bitstream.size);
3113 }
3114
3115 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3116 src_fourcc = q_data_src->fourcc;
3117
3118 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3119 if (val != 1)
3120 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3121
3122 success = val & 0x1;
3123 if (!success)
3124 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3125
3126 if (src_fourcc == V4L2_PIX_FMT_H264) {
3127 if (val & (1 << 3))
3128 v4l2_err(&dev->v4l2_dev,
3129 "insufficient PS buffer space (%d bytes)\n",
3130 ctx->psbuf.size);
3131 if (val & (1 << 2))
3132 v4l2_err(&dev->v4l2_dev,
3133 "insufficient slice buffer space (%d bytes)\n",
3134 ctx->slicebuf.size);
3135 }
3136
3137 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3138 width = (val >> 16) & 0xffff;
3139 height = val & 0xffff;
3140
3141 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3142
Philipp Zabel52c41672014-07-11 06:36:19 -03003143 /* frame crop information */
3144 if (src_fourcc == V4L2_PIX_FMT_H264) {
3145 u32 left_right;
3146 u32 top_bottom;
3147
3148 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3149 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3150
3151 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3152 /* Keep current crop information */
3153 } else {
3154 struct v4l2_rect *rect = &q_data_dst->rect;
3155
3156 rect->left = left_right >> 16 & 0xffff;
3157 rect->top = top_bottom >> 16 & 0xffff;
3158 rect->width = width - rect->left -
3159 (left_right & 0xffff);
3160 rect->height = height - rect->top -
3161 (top_bottom & 0xffff);
3162 }
3163 } else {
3164 /* no cropping */
3165 }
3166
Philipp Zabel410e5e42014-07-11 06:36:32 -03003167 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3168 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003169 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003170 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003171
3172 if (dev->devtype->product == CODA_7541) {
3173 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3174 if (val == 0) {
3175 /* not enough bitstream data */
3176 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3177 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003178 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003179 return;
3180 }
3181 }
3182
3183 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3184
3185 /*
3186 * The previous display frame was copied out by the rotator,
3187 * now it can be overwritten again
3188 */
3189 if (ctx->display_idx >= 0 &&
3190 ctx->display_idx < ctx->num_internal_frames) {
3191 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3192 coda_write(dev, ctx->frm_dis_flg,
3193 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3194 }
3195
3196 /*
3197 * The index of the last decoded frame, not necessarily in
3198 * display order, and the index of the next display frame.
3199 * The latter could have been decoded in a previous run.
3200 */
3201 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3202 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3203
3204 if (decoded_idx == -1) {
3205 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003206 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3207 ctx->sequence_offset++;
3208 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003209 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003210 } else if (decoded_idx == -2) {
3211 /* no frame was decoded, we still return the remaining buffers */
3212 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3213 v4l2_err(&dev->v4l2_dev,
3214 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003215 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003216 ts = list_first_entry(&ctx->timestamp_list,
3217 struct coda_timestamp, list);
3218 list_del(&ts->list);
3219 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003220 val -= ctx->sequence_offset;
3221 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003222 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003223 "sequence number mismatch (%d(%d) != %d)\n",
3224 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003225 }
3226 ctx->frame_timestamps[decoded_idx] = *ts;
3227 kfree(ts);
3228
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003229 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3230 if (val == 0)
3231 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3232 else if (val == 1)
3233 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3234 else
3235 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003236
3237 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003238 }
3239
3240 if (display_idx == -1) {
3241 /*
3242 * no more frames to be decoded, but there could still
3243 * be rotator output to dequeue
3244 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003245 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003246 } else if (display_idx == -3) {
3247 /* possibly prescan failure */
3248 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3249 v4l2_err(&dev->v4l2_dev,
3250 "presentation frame index out of range: %d\n",
3251 display_idx);
3252 }
3253
3254 /* If a frame was copied out, return it */
3255 if (ctx->display_idx >= 0 &&
3256 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003257 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003258 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3259
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003260 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3261 V4L2_BUF_FLAG_PFRAME |
3262 V4L2_BUF_FLAG_BFRAME);
3263 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003264 ts = &ctx->frame_timestamps[ctx->display_idx];
3265 dst_buf->v4l2_buf.timecode = ts->timecode;
3266 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003267
Philipp Zabel918c66f2013-06-27 06:59:01 -03003268 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3269
Philipp Zabel410e5e42014-07-11 06:36:32 -03003270 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3271 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003272
3273 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3274 "job finished: decoding frame (%d) (%s)\n",
3275 dst_buf->v4l2_buf.sequence,
3276 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3277 "KEYFRAME" : "PFRAME");
3278 } else {
3279 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3280 "job finished: no frame decoded\n");
3281 }
3282
3283 /* The rotator will copy the current display frame next time */
3284 ctx->display_idx = display_idx;
3285}
3286
3287static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003288{
Philipp Zabelec25f682012-07-20 08:54:29 -03003289 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003290 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003291 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003292
Philipp Zabel14604e32014-07-11 06:36:22 -03003293 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3294 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003295
3296 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003297 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003298 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3299
Javier Martin186b2502012-07-26 05:53:35 -03003300 /* Calculate bytesused field */
3301 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003302 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3303 ctx->vpu_header_size[0] +
3304 ctx->vpu_header_size[1] +
3305 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003306 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003307 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003308 }
3309
3310 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3311 wr_ptr - start_ptr);
3312
3313 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3314 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3315
Philipp Zabel51660282013-09-30 10:34:49 -03003316 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003317 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3318 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3319 } else {
3320 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3321 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3322 }
3323
Kamil Debskiccd571c2013-04-24 10:38:24 -03003324 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003325 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3326 dst_buf->v4l2_buf.flags |=
3327 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003328 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3329
Philipp Zabelec25f682012-07-20 08:54:29 -03003330 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003331
Philipp Zabel14604e32014-07-11 06:36:22 -03003332 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003333 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3334
3335 ctx->gopcounter--;
3336 if (ctx->gopcounter < 0)
3337 ctx->gopcounter = ctx->params.gop_size - 1;
3338
3339 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3340 "job finished: encoding frame (%d) (%s)\n",
3341 dst_buf->v4l2_buf.sequence,
3342 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3343 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003344}
3345
3346static irqreturn_t coda_irq_handler(int irq, void *data)
3347{
3348 struct coda_dev *dev = data;
3349 struct coda_ctx *ctx;
3350
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003351 /* read status register to attend the IRQ */
3352 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3353 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3354 CODA_REG_BIT_INT_CLEAR);
3355
3356 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3357 if (ctx == NULL) {
3358 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3359 mutex_unlock(&dev->coda_mutex);
3360 return IRQ_HANDLED;
3361 }
3362
3363 if (ctx->aborting) {
3364 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3365 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003366 }
3367
3368 if (coda_isbusy(ctx->dev)) {
3369 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3370 "coda is still busy!!!!\n");
3371 return IRQ_NONE;
3372 }
3373
Philipp Zabel32b6f202014-07-11 06:36:20 -03003374 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003375
3376 return IRQ_HANDLED;
3377}
3378
3379static u32 coda_supported_firmwares[] = {
3380 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003381 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003382 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003383};
3384
3385static bool coda_firmware_supported(u32 vernum)
3386{
3387 int i;
3388
3389 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3390 if (vernum == coda_supported_firmwares[i])
3391 return true;
3392 return false;
3393}
3394
Philipp Zabel87048bb2012-07-02 07:03:43 -03003395static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003396{
Javier Martin186b2502012-07-26 05:53:35 -03003397 u32 data;
3398 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003399 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003400
Fabio Estevam79830db2013-08-21 11:14:17 -03003401 ret = clk_prepare_enable(dev->clk_per);
3402 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003403 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003404
3405 ret = clk_prepare_enable(dev->clk_ahb);
3406 if (ret)
3407 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003408
Philipp Zabel8f452842014-07-11 06:36:35 -03003409 if (dev->rstc)
3410 reset_control_reset(dev->rstc);
3411
Javier Martin186b2502012-07-26 05:53:35 -03003412 /*
3413 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003414 * The 16-bit chars in the code buffer are in memory access
3415 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003416 * Data in this SRAM survives a reboot.
3417 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003418 p = (u16 *)dev->codebuf.vaddr;
3419 if (dev->devtype->product == CODA_DX6) {
3420 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3421 data = CODA_DOWN_ADDRESS_SET(i) |
3422 CODA_DOWN_DATA_SET(p[i ^ 1]);
3423 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3424 }
3425 } else {
3426 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3427 data = CODA_DOWN_ADDRESS_SET(i) |
3428 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3429 3 - (i % 4)]);
3430 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3431 }
Javier Martin186b2502012-07-26 05:53:35 -03003432 }
Javier Martin186b2502012-07-26 05:53:35 -03003433
Philipp Zabel9acf7692013-05-23 10:42:56 -03003434 /* Clear registers */
3435 for (i = 0; i < 64; i++)
3436 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3437
Javier Martin186b2502012-07-26 05:53:35 -03003438 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003439 if (dev->devtype->product == CODA_960 ||
3440 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003441 coda_write(dev, dev->tempbuf.paddr,
3442 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003443 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003444 } else {
3445 coda_write(dev, dev->workbuf.paddr,
3446 CODA_REG_BIT_WORK_BUF_ADDR);
3447 }
Javier Martin186b2502012-07-26 05:53:35 -03003448 coda_write(dev, dev->codebuf.paddr,
3449 CODA_REG_BIT_CODE_BUF_ADDR);
3450 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3451
3452 /* Set default values */
3453 switch (dev->devtype->product) {
3454 case CODA_DX6:
3455 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3456 break;
3457 default:
3458 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3459 }
Philipp Zabel89548442014-07-11 06:36:17 -03003460 if (dev->devtype->product == CODA_960)
3461 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3462 else
3463 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003464
3465 if (dev->devtype->product != CODA_DX6)
3466 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3467
Javier Martin186b2502012-07-26 05:53:35 -03003468 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3469 CODA_REG_BIT_INT_ENABLE);
3470
3471 /* Reset VPU and start processor */
3472 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3473 data |= CODA_REG_RESET_ENABLE;
3474 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3475 udelay(10);
3476 data &= ~CODA_REG_RESET_ENABLE;
3477 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3478 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3479
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003480 clk_disable_unprepare(dev->clk_ahb);
3481 clk_disable_unprepare(dev->clk_per);
3482
3483 return 0;
3484
3485err_clk_ahb:
3486 clk_disable_unprepare(dev->clk_per);
3487err_clk_per:
3488 return ret;
3489}
3490
3491static int coda_check_firmware(struct coda_dev *dev)
3492{
3493 u16 product, major, minor, release;
3494 u32 data;
3495 int ret;
3496
3497 ret = clk_prepare_enable(dev->clk_per);
3498 if (ret)
3499 goto err_clk_per;
3500
3501 ret = clk_prepare_enable(dev->clk_ahb);
3502 if (ret)
3503 goto err_clk_ahb;
3504
Javier Martin186b2502012-07-26 05:53:35 -03003505 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3506 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3507 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3508 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3509 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3510 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003511 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003512 ret = -EIO;
3513 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003514 }
3515
Philipp Zabel89548442014-07-11 06:36:17 -03003516 if (dev->devtype->product == CODA_960) {
3517 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3518 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3519 data);
3520 }
3521
Javier Martin186b2502012-07-26 05:53:35 -03003522 /* Check we are compatible with the loaded firmware */
3523 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3524 product = CODA_FIRMWARE_PRODUCT(data);
3525 major = CODA_FIRMWARE_MAJOR(data);
3526 minor = CODA_FIRMWARE_MINOR(data);
3527 release = CODA_FIRMWARE_RELEASE(data);
3528
3529 clk_disable_unprepare(dev->clk_per);
3530 clk_disable_unprepare(dev->clk_ahb);
3531
3532 if (product != dev->devtype->product) {
3533 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3534 " Version: %u.%u.%u\n",
3535 coda_product_name(dev->devtype->product),
3536 coda_product_name(product), major, minor, release);
3537 return -EINVAL;
3538 }
3539
3540 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3541 coda_product_name(product));
3542
3543 if (coda_firmware_supported(data)) {
3544 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3545 major, minor, release);
3546 } else {
3547 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3548 "%u.%u.%u\n", major, minor, release);
3549 }
3550
3551 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003552
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003553err_run_cmd:
3554 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003555err_clk_ahb:
3556 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003557err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003558 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003559}
3560
3561static void coda_fw_callback(const struct firmware *fw, void *context)
3562{
3563 struct coda_dev *dev = context;
3564 struct platform_device *pdev = dev->plat_dev;
3565 int ret;
3566
3567 if (!fw) {
3568 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3569 return;
3570 }
3571
3572 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003573 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3574 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003575 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003576 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3577 return;
3578 }
3579
Philipp Zabel87048bb2012-07-02 07:03:43 -03003580 /* Copy the whole firmware image to the code buffer */
3581 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3582 release_firmware(fw);
3583
Philipp Zabel1e172732014-07-11 06:36:23 -03003584 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3585 /*
3586 * Enabling power temporarily will cause coda_hw_init to be
3587 * called via coda_runtime_resume by the pm domain.
3588 */
3589 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3590 if (ret < 0) {
3591 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3592 ret);
3593 return;
3594 }
3595
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003596 ret = coda_check_firmware(dev);
3597 if (ret < 0)
3598 return;
3599
Philipp Zabel1e172732014-07-11 06:36:23 -03003600 pm_runtime_put_sync(&dev->plat_dev->dev);
3601 } else {
3602 /*
3603 * If runtime pm is disabled or pm_domain is not set,
3604 * initialize once manually.
3605 */
3606 ret = coda_hw_init(dev);
3607 if (ret < 0) {
3608 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3609 return;
3610 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003611
3612 ret = coda_check_firmware(dev);
3613 if (ret < 0)
3614 return;
Javier Martin186b2502012-07-26 05:53:35 -03003615 }
3616
3617 dev->vfd.fops = &coda_fops,
3618 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3619 dev->vfd.release = video_device_release_empty,
3620 dev->vfd.lock = &dev->dev_mutex;
3621 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003622 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003623 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3624 video_set_drvdata(&dev->vfd, dev);
3625
3626 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3627 if (IS_ERR(dev->alloc_ctx)) {
3628 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3629 return;
3630 }
3631
3632 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3633 if (IS_ERR(dev->m2m_dev)) {
3634 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3635 goto rel_ctx;
3636 }
3637
3638 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3639 if (ret) {
3640 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3641 goto rel_m2m;
3642 }
3643 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3644 dev->vfd.num);
3645
3646 return;
3647
3648rel_m2m:
3649 v4l2_m2m_release(dev->m2m_dev);
3650rel_ctx:
3651 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3652}
3653
3654static int coda_firmware_request(struct coda_dev *dev)
3655{
3656 char *fw = dev->devtype->firmware;
3657
3658 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3659 coda_product_name(dev->devtype->product));
3660
3661 return request_firmware_nowait(THIS_MODULE, true,
3662 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3663}
3664
3665enum coda_platform {
3666 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003667 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003668 CODA_IMX6Q,
3669 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003670};
3671
Emil Goodec06d8752012-08-14 17:44:42 -03003672static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003673 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003674 .firmware = "v4l-codadx6-imx27.bin",
3675 .product = CODA_DX6,
3676 .codecs = codadx6_codecs,
3677 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3678 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003679 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03003680 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003681 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003682 .firmware = "v4l-coda7541-imx53.bin",
3683 .product = CODA_7541,
3684 .codecs = coda7_codecs,
3685 .num_codecs = ARRAY_SIZE(coda7_codecs),
3686 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003687 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003688 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003689 },
Philipp Zabel89548442014-07-11 06:36:17 -03003690 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003691 .firmware = "v4l-coda960-imx6q.bin",
3692 .product = CODA_960,
3693 .codecs = coda9_codecs,
3694 .num_codecs = ARRAY_SIZE(coda9_codecs),
3695 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003696 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003697 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03003698 },
3699 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003700 .firmware = "v4l-coda960-imx6dl.bin",
3701 .product = CODA_960,
3702 .codecs = coda9_codecs,
3703 .num_codecs = ARRAY_SIZE(coda9_codecs),
3704 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003705 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003706 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03003707 },
Javier Martin186b2502012-07-26 05:53:35 -03003708};
3709
3710static struct platform_device_id coda_platform_ids[] = {
3711 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003712 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003713 { /* sentinel */ }
3714};
3715MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3716
3717#ifdef CONFIG_OF
3718static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003719 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003720 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003721 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3722 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003723 { /* sentinel */ }
3724};
3725MODULE_DEVICE_TABLE(of, coda_dt_ids);
3726#endif
3727
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003728static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003729{
3730 const struct of_device_id *of_id =
3731 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3732 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003733 struct coda_platform_data *pdata = pdev->dev.platform_data;
3734 struct device_node *np = pdev->dev.of_node;
3735 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003736 struct coda_dev *dev;
3737 struct resource *res;
3738 int ret, irq;
3739
3740 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3741 if (!dev) {
3742 dev_err(&pdev->dev, "Not enough memory for %s\n",
3743 CODA_NAME);
3744 return -ENOMEM;
3745 }
3746
3747 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003748 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003749
3750 dev->plat_dev = pdev;
3751 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3752 if (IS_ERR(dev->clk_per)) {
3753 dev_err(&pdev->dev, "Could not get per clock\n");
3754 return PTR_ERR(dev->clk_per);
3755 }
3756
3757 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3758 if (IS_ERR(dev->clk_ahb)) {
3759 dev_err(&pdev->dev, "Could not get ahb clock\n");
3760 return PTR_ERR(dev->clk_ahb);
3761 }
3762
3763 /* Get memory for physical registers */
3764 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003765 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3766 if (IS_ERR(dev->regs_base))
3767 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003768
3769 /* IRQ */
3770 irq = platform_get_irq(pdev, 0);
3771 if (irq < 0) {
3772 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03003773 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03003774 }
3775
Philipp Zabel918c66f2013-06-27 06:59:01 -03003776 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003777 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003778 dev_err(&pdev->dev, "failed to request irq\n");
3779 return -ENOENT;
3780 }
3781
Philipp Zabel8f452842014-07-11 06:36:35 -03003782 dev->rstc = devm_reset_control_get(&pdev->dev, NULL);
3783 if (IS_ERR(dev->rstc)) {
3784 ret = PTR_ERR(dev->rstc);
3785 if (ret == -ENOENT) {
3786 dev->rstc = NULL;
3787 } else {
3788 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3789 return ret;
3790 }
3791 }
3792
Philipp Zabel657eee72013-04-29 16:17:14 -07003793 /* Get IRAM pool from device tree or platform data */
3794 pool = of_get_named_gen_pool(np, "iram", 0);
3795 if (!pool && pdata)
3796 pool = dev_get_gen_pool(pdata->iram_dev);
3797 if (!pool) {
3798 dev_err(&pdev->dev, "iram pool not available\n");
3799 return -ENOMEM;
3800 }
3801 dev->iram_pool = pool;
3802
Javier Martin186b2502012-07-26 05:53:35 -03003803 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3804 if (ret)
3805 return ret;
3806
3807 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003808 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003809
3810 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3811
3812 if (of_id) {
3813 dev->devtype = of_id->data;
3814 } else if (pdev_id) {
3815 dev->devtype = &coda_devdata[pdev_id->driver_data];
3816 } else {
3817 v4l2_device_unregister(&dev->v4l2_dev);
3818 return -EINVAL;
3819 }
3820
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003821 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3822 if (!dev->debugfs_root)
3823 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3824
Javier Martin186b2502012-07-26 05:53:35 -03003825 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003826 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003827 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003828 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003829 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003830 if (ret < 0) {
3831 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3832 v4l2_device_unregister(&dev->v4l2_dev);
3833 return ret;
3834 }
Javier Martin186b2502012-07-26 05:53:35 -03003835 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003836
3837 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003838 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003839 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003840 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003841 if (ret < 0) {
3842 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3843 v4l2_device_unregister(&dev->v4l2_dev);
3844 return ret;
3845 }
Javier Martin186b2502012-07-26 05:53:35 -03003846 }
3847
Philipp Zabel401e9722014-07-11 06:36:43 -03003848 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003849 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3850 &dev->iram.paddr);
3851 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003852 dev_err(&pdev->dev, "unable to alloc iram\n");
3853 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003854 }
3855
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003856 dev->iram.blob.data = dev->iram.vaddr;
3857 dev->iram.blob.size = dev->iram.size;
3858 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
3859 &dev->iram.blob);
3860
Philipp Zabel32b6f202014-07-11 06:36:20 -03003861 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3862 if (!dev->workqueue) {
3863 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3864 return -ENOMEM;
3865 }
3866
Javier Martin186b2502012-07-26 05:53:35 -03003867 platform_set_drvdata(pdev, dev);
3868
Philipp Zabel1e172732014-07-11 06:36:23 -03003869 pm_runtime_enable(&pdev->dev);
3870
Javier Martin186b2502012-07-26 05:53:35 -03003871 return coda_firmware_request(dev);
3872}
3873
3874static int coda_remove(struct platform_device *pdev)
3875{
3876 struct coda_dev *dev = platform_get_drvdata(pdev);
3877
3878 video_unregister_device(&dev->vfd);
3879 if (dev->m2m_dev)
3880 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003881 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003882 if (dev->alloc_ctx)
3883 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3884 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003885 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003886 if (dev->iram.vaddr)
3887 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3888 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003889 coda_free_aux_buf(dev, &dev->codebuf);
3890 coda_free_aux_buf(dev, &dev->tempbuf);
3891 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003892 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03003893 return 0;
3894}
3895
Philipp Zabel1e172732014-07-11 06:36:23 -03003896#ifdef CONFIG_PM_RUNTIME
3897static int coda_runtime_resume(struct device *dev)
3898{
3899 struct coda_dev *cdev = dev_get_drvdata(dev);
3900 int ret = 0;
3901
3902 if (dev->pm_domain) {
3903 ret = coda_hw_init(cdev);
3904 if (ret)
3905 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3906 }
3907
3908 return ret;
3909}
3910#endif
3911
3912static const struct dev_pm_ops coda_pm_ops = {
3913 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3914};
3915
Javier Martin186b2502012-07-26 05:53:35 -03003916static struct platform_driver coda_driver = {
3917 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003918 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003919 .driver = {
3920 .name = CODA_NAME,
3921 .owner = THIS_MODULE,
3922 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003923 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003924 },
3925 .id_table = coda_platform_ids,
3926};
3927
3928module_platform_driver(coda_driver);
3929
3930MODULE_LICENSE("GPL");
3931MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3932MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");