blob: 594b0c7c41516bb96bbf3eff959d67c93344223c [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 Zabel814c3762014-07-18 07:22:45 -0300105 const struct coda_codec *codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300106 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300107 size_t workbuf_size;
Philipp Zabel5d73fad2014-07-11 06:36:42 -0300108 size_t tempbuf_size;
Philipp Zabel401e9722014-07-11 06:36:43 -0300109 size_t iram_size;
Javier Martin186b2502012-07-26 05:53:35 -0300110};
111
112/* Per-queue, driver-specific private data */
113struct coda_q_data {
114 unsigned int width;
115 unsigned int height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300116 unsigned int bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300117 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300118 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300119 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300120};
121
122struct coda_aux_buf {
123 void *vaddr;
124 dma_addr_t paddr;
125 u32 size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300126 struct debugfs_blob_wrapper blob;
127 struct dentry *dentry;
Javier Martin186b2502012-07-26 05:53:35 -0300128};
129
130struct coda_dev {
131 struct v4l2_device v4l2_dev;
Philipp Zabel121cacf2014-07-18 07:22:42 -0300132 struct video_device vfd[2];
Javier Martin186b2502012-07-26 05:53:35 -0300133 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300134 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300135
136 void __iomem *regs_base;
137 struct clk *clk_per;
138 struct clk *clk_ahb;
Philipp Zabel8f452842014-07-11 06:36:35 -0300139 struct reset_control *rstc;
Javier Martin186b2502012-07-26 05:53:35 -0300140
141 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300142 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300143 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700144 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300145 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300146
147 spinlock_t irqlock;
148 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300149 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300150 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300151 struct v4l2_m2m_dev *m2m_dev;
152 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300153 struct list_head instances;
154 unsigned long instance_mask;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300155 struct dentry *debugfs_root;
Javier Martin186b2502012-07-26 05:53:35 -0300156};
157
158struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300159 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300160 u8 h264_intra_qp;
161 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300162 u8 h264_min_qp;
163 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300164 u8 h264_deblk_enabled;
165 u8 h264_deblk_alpha;
166 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300167 u8 mpeg4_intra_qp;
168 u8 mpeg4_inter_qp;
169 u8 gop_size;
Philipp Zabelf38f79d2014-07-11 06:36:28 -0300170 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300171 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300172 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300173 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
174 u32 framerate;
175 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300176 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300177 u32 slice_max_mb;
178};
179
Philipp Zabelc2d22512013-06-21 03:55:28 -0300180struct coda_iram_info {
181 u32 axi_sram_use;
182 phys_addr_t buf_bit_use;
183 phys_addr_t buf_ip_ac_dc_use;
184 phys_addr_t buf_dbk_y_use;
185 phys_addr_t buf_dbk_c_use;
186 phys_addr_t buf_ovl_use;
187 phys_addr_t buf_btp_use;
188 phys_addr_t search_ram_paddr;
189 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300190 int remaining;
191 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300192};
193
Philipp Zabel89548442014-07-11 06:36:17 -0300194struct gdi_tiled_map {
195 int xy2ca_map[16];
196 int xy2ba_map[16];
197 int xy2ra_map[16];
198 int rbc2axi_map[32];
199 int xy2rbc_config;
200 int map_type;
201#define GDI_LINEAR_FRAME_MAP 0
202};
203
Philipp Zabel846ced92014-07-11 06:36:31 -0300204struct coda_timestamp {
205 struct list_head list;
206 u32 sequence;
207 struct v4l2_timecode timecode;
208 struct timeval timestamp;
209};
210
Javier Martin186b2502012-07-26 05:53:35 -0300211struct coda_ctx {
212 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300213 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300214 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300215 struct work_struct pic_run_work;
216 struct work_struct seq_end_work;
217 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300218 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300219 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300220 int streamon_out;
221 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300222 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300223 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300224 u32 osequence;
Philipp Zabelcb2c0282014-07-11 06:36:33 -0300225 u32 sequence_offset;
Javier Martin186b2502012-07-26 05:53:35 -0300226 struct coda_q_data q_data[2];
227 enum coda_inst_type inst_type;
Philipp Zabel814c3762014-07-18 07:22:45 -0300228 const struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300229 enum v4l2_colorspace colorspace;
230 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300231 struct v4l2_ctrl_handler ctrls;
232 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300233 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300234 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300235 char vpu_header[3][64];
236 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300237 struct kfifo bitstream_fifo;
238 struct mutex bitstream_mutex;
239 struct coda_aux_buf bitstream;
Philipp Zabel84e23652014-07-11 06:36:34 -0300240 bool hold;
Javier Martin186b2502012-07-26 05:53:35 -0300241 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300242 struct coda_aux_buf psbuf;
243 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300244 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300245 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300246 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300247 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300248 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300249 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300250 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300251 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300252 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300253 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300254 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300255 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300256 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300257 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300258 int display_idx;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300259 struct dentry *debugfs_entry;
Javier Martin186b2502012-07-26 05:53:35 -0300260};
261
Javier Martin832fbb52012-10-29 08:34:59 -0300262static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
263 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
264static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300265
Javier Martin186b2502012-07-26 05:53:35 -0300266static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
267{
268 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
269 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
270 writel(data, dev->regs_base + reg);
271}
272
273static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
274{
275 u32 data;
276 data = readl(dev->regs_base + reg);
277 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
278 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
279 return data;
280}
281
282static inline unsigned long coda_isbusy(struct coda_dev *dev)
283{
284 return coda_read(dev, CODA_REG_BIT_BUSY);
285}
286
287static inline int coda_is_initialized(struct coda_dev *dev)
288{
289 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
290}
291
292static int coda_wait_timeout(struct coda_dev *dev)
293{
294 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
295
296 while (coda_isbusy(dev)) {
297 if (time_after(jiffies, timeout))
298 return -ETIMEDOUT;
299 }
300 return 0;
301}
302
303static void coda_command_async(struct coda_ctx *ctx, int cmd)
304{
305 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300306
Philipp Zabel89548442014-07-11 06:36:17 -0300307 if (dev->devtype->product == CODA_960 ||
308 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300309 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300310 coda_write(dev, ctx->bit_stream_param,
311 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300312 coda_write(dev, ctx->frm_dis_flg,
313 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300314 coda_write(dev, ctx->frame_mem_ctrl,
315 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300316 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
317 }
318
Philipp Zabel89548442014-07-11 06:36:17 -0300319 if (dev->devtype->product == CODA_960) {
320 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
321 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
322 }
323
Javier Martin186b2502012-07-26 05:53:35 -0300324 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
325
326 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
327 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300328 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
329
Javier Martin186b2502012-07-26 05:53:35 -0300330 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
331}
332
333static int coda_command_sync(struct coda_ctx *ctx, int cmd)
334{
335 struct coda_dev *dev = ctx->dev;
336
337 coda_command_async(ctx, cmd);
338 return coda_wait_timeout(dev);
339}
340
Philipp Zabel8f452842014-07-11 06:36:35 -0300341static int coda_hw_reset(struct coda_ctx *ctx)
342{
343 struct coda_dev *dev = ctx->dev;
344 unsigned long timeout;
345 unsigned int idx;
346 int ret;
347
348 if (!dev->rstc)
349 return -ENOENT;
350
351 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
352
Philipp Zabelae5abd22014-07-18 07:22:35 -0300353 if (dev->devtype->product == CODA_960) {
354 timeout = jiffies + msecs_to_jiffies(100);
355 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
356 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
357 if (time_after(jiffies, timeout))
358 return -ETIME;
359 cpu_relax();
360 }
Philipp Zabel8f452842014-07-11 06:36:35 -0300361 }
362
363 ret = reset_control_reset(dev->rstc);
364 if (ret < 0)
365 return ret;
366
Philipp Zabelae5abd22014-07-18 07:22:35 -0300367 if (dev->devtype->product == CODA_960)
368 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
Philipp Zabel8f452842014-07-11 06:36:35 -0300369 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
370 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
371 ret = coda_wait_timeout(dev);
372 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
373
374 return ret;
375}
376
Javier Martin186b2502012-07-26 05:53:35 -0300377static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
378 enum v4l2_buf_type type)
379{
380 switch (type) {
381 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
382 return &(ctx->q_data[V4L2_M2M_SRC]);
383 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
384 return &(ctx->q_data[V4L2_M2M_DST]);
385 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300386 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300387 }
Javier Martin186b2502012-07-26 05:53:35 -0300388}
389
390/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300391 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300392 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300393static const struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300394 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300395 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300396 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300397 },
398 {
399 .name = "YUV 4:2:0 Planar, YCrCb",
400 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300401 },
402 {
403 .name = "H264 Encoded Stream",
404 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300405 },
406 {
407 .name = "MPEG4 Encoded Stream",
408 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300409 },
410};
411
Philipp Zabelb96904e2013-05-23 10:42:58 -0300412#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
413 { mode, src_fourcc, dst_fourcc, max_w, max_h }
414
415/*
416 * Arrays of codecs supported by each given version of Coda:
417 * i.MX27 -> codadx6
418 * i.MX5x -> coda7
419 * i.MX6 -> coda960
420 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
421 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300422static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300423 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
424 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300425};
426
Philipp Zabel814c3762014-07-18 07:22:45 -0300427static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300428 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
429 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300430 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
431 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300432};
433
Philipp Zabel814c3762014-07-18 07:22:45 -0300434static const struct coda_codec coda9_codecs[] = {
Philipp Zabel89548442014-07-11 06:36:17 -0300435 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
436 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
437 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
438 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
439};
440
Philipp Zabelb96904e2013-05-23 10:42:58 -0300441static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300442{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300443 switch (fourcc) {
444 case V4L2_PIX_FMT_YUV420:
445 case V4L2_PIX_FMT_YVU420:
446 return true;
447 default:
448 return false;
449 }
450}
Javier Martin186b2502012-07-26 05:53:35 -0300451
Philipp Zabelb96904e2013-05-23 10:42:58 -0300452/*
453 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
454 * tables.
455 */
456static u32 coda_format_normalize_yuv(u32 fourcc)
457{
458 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
459}
460
Philipp Zabel814c3762014-07-18 07:22:45 -0300461static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
462 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300463{
Philipp Zabel814c3762014-07-18 07:22:45 -0300464 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300465 int num_codecs = dev->devtype->num_codecs;
466 int k;
467
468 src_fourcc = coda_format_normalize_yuv(src_fourcc);
469 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
470 if (src_fourcc == dst_fourcc)
471 return NULL;
472
473 for (k = 0; k < num_codecs; k++) {
474 if (codecs[k].src_fourcc == src_fourcc &&
475 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300476 break;
477 }
478
Philipp Zabelb96904e2013-05-23 10:42:58 -0300479 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300480 return NULL;
481
Philipp Zabelb96904e2013-05-23 10:42:58 -0300482 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300483}
484
Philipp Zabel57625592013-09-30 10:34:51 -0300485static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300486 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300487 int *max_w, int *max_h)
488{
Philipp Zabel814c3762014-07-18 07:22:45 -0300489 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300490 int num_codecs = dev->devtype->num_codecs;
491 unsigned int w, h;
492 int k;
493
494 if (codec) {
495 w = codec->max_w;
496 h = codec->max_h;
497 } else {
498 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
499 w = max(w, codecs[k].max_w);
500 h = max(h, codecs[k].max_h);
501 }
502 }
503
504 if (max_w)
505 *max_w = w;
506 if (max_h)
507 *max_h = h;
508}
509
Philipp Zabel927933f2013-09-30 10:34:48 -0300510static char *coda_product_name(int product)
511{
512 static char buf[9];
513
514 switch (product) {
515 case CODA_DX6:
516 return "CodaDx6";
517 case CODA_7541:
518 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300519 case CODA_960:
520 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300521 default:
522 snprintf(buf, sizeof(buf), "(0x%04x)", product);
523 return buf;
524 }
525}
526
Javier Martin186b2502012-07-26 05:53:35 -0300527/*
528 * V4L2 ioctl() operations.
529 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300530static int coda_querycap(struct file *file, void *priv,
531 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300532{
Philipp Zabel927933f2013-09-30 10:34:48 -0300533 struct coda_ctx *ctx = fh_to_ctx(priv);
534
Javier Martin186b2502012-07-26 05:53:35 -0300535 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300536 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
537 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300538 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300539 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300540 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
541
542 return 0;
543}
544
Philipp Zabel22e244b2014-07-18 07:22:43 -0300545static int coda_enum_fmt(struct file *file, void *priv,
546 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300547{
548 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300549 const struct coda_codec *codecs = ctx->dev->devtype->codecs;
550 const struct coda_fmt *formats = coda_formats;
551 const struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300552 int num_codecs = ctx->dev->devtype->num_codecs;
553 int num_formats = ARRAY_SIZE(coda_formats);
554 int i, k, num = 0;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300555 bool yuv;
556
557 if (ctx->inst_type == CODA_INST_ENCODER)
558 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
559 else
560 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300561
562 for (i = 0; i < num_formats; i++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300563 /* Skip either raw or compressed formats */
564 if (yuv != coda_format_is_yuv(formats[i].fourcc))
565 continue;
566 /* All uncompressed formats are always supported */
567 if (yuv) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300568 if (num == f->index)
569 break;
570 ++num;
571 continue;
572 }
573 /* Compressed formats may be supported, check the codec list */
574 for (k = 0; k < num_codecs; k++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300575 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
576 formats[i].fourcc == codecs[k].dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300577 break;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300578 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
Philipp Zabelb96904e2013-05-23 10:42:58 -0300579 formats[i].fourcc == codecs[k].src_fourcc)
580 break;
581 }
582 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300583 if (num == f->index)
584 break;
585 ++num;
586 }
587 }
588
589 if (i < num_formats) {
590 fmt = &formats[i];
591 strlcpy(f->description, fmt->name, sizeof(f->description));
592 f->pixelformat = fmt->fourcc;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300593 if (!yuv)
Philipp Zabelbaf70212013-09-30 10:34:46 -0300594 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300595 return 0;
596 }
597
598 /* Format not found */
599 return -EINVAL;
600}
601
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300602static int coda_g_fmt(struct file *file, void *priv,
603 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300604{
Javier Martin186b2502012-07-26 05:53:35 -0300605 struct coda_q_data *q_data;
606 struct coda_ctx *ctx = fh_to_ctx(priv);
607
Javier Martin186b2502012-07-26 05:53:35 -0300608 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300609 if (!q_data)
610 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300611
612 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300613 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300614 f->fmt.pix.width = q_data->width;
615 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300616 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300617
618 f->fmt.pix.sizeimage = q_data->sizeimage;
619 f->fmt.pix.colorspace = ctx->colorspace;
620
621 return 0;
622}
623
Philipp Zabel814c3762014-07-18 07:22:45 -0300624static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300625 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300626{
Philipp Zabel57625592013-09-30 10:34:51 -0300627 struct coda_dev *dev = ctx->dev;
628 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300629 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300630 enum v4l2_field field;
631
632 field = f->fmt.pix.field;
633 if (field == V4L2_FIELD_ANY)
634 field = V4L2_FIELD_NONE;
635 else if (V4L2_FIELD_NONE != field)
636 return -EINVAL;
637
638 /* V4L2 specification suggests the driver corrects the format struct
639 * if any of the dimensions is unsupported */
640 f->fmt.pix.field = field;
641
Philipp Zabel57625592013-09-30 10:34:51 -0300642 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
643 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
644 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
645 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300646
Philipp Zabel57625592013-09-30 10:34:51 -0300647 switch (f->fmt.pix.pixelformat) {
648 case V4L2_PIX_FMT_YUV420:
649 case V4L2_PIX_FMT_YVU420:
650 case V4L2_PIX_FMT_H264:
651 case V4L2_PIX_FMT_MPEG4:
652 case V4L2_PIX_FMT_JPEG:
653 break;
654 default:
655 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300656 if (!q_data)
657 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300658 f->fmt.pix.pixelformat = q_data->fourcc;
659 }
660
661 switch (f->fmt.pix.pixelformat) {
662 case V4L2_PIX_FMT_YUV420:
663 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300664 /* Frame stride must be multiple of 8, but 16 for h.264 */
665 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300666 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300667 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300668 break;
669 case V4L2_PIX_FMT_H264:
670 case V4L2_PIX_FMT_MPEG4:
671 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300672 f->fmt.pix.bytesperline = 0;
673 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300674 break;
675 default:
676 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300677 }
678
679 return 0;
680}
681
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300682static int coda_try_fmt_vid_cap(struct file *file, void *priv,
683 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300684{
Javier Martin186b2502012-07-26 05:53:35 -0300685 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300686 const struct coda_codec *codec = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300687 struct vb2_queue *src_vq;
688 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300689
Philipp Zabel918c66f2013-06-27 06:59:01 -0300690 /*
691 * If the source format is already fixed, try to find a codec that
692 * converts to the given destination format
693 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300694 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300695 if (vb2_is_streaming(src_vq)) {
696 struct coda_q_data *q_data_src;
697
698 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
699 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
700 f->fmt.pix.pixelformat);
701 if (!codec)
702 return -EINVAL;
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300703
704 f->fmt.pix.width = q_data_src->width;
705 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300706 } else {
707 /* Otherwise determine codec by encoded format, if possible */
708 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
709 f->fmt.pix.pixelformat);
710 }
Javier Martin186b2502012-07-26 05:53:35 -0300711
712 f->fmt.pix.colorspace = ctx->colorspace;
713
Philipp Zabel57625592013-09-30 10:34:51 -0300714 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300715 if (ret < 0)
716 return ret;
717
718 /* The h.264 decoder only returns complete 16x16 macroblocks */
719 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300720 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300721 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300722 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300723 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
724 f->fmt.pix.height * 3 / 2;
725 }
726
727 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300728}
729
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300730static int coda_try_fmt_vid_out(struct file *file, void *priv,
731 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300732{
733 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300734 const struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300735
Philipp Zabelb96904e2013-05-23 10:42:58 -0300736 /* Determine codec by encoded format, returns NULL if raw or invalid */
737 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
738 V4L2_PIX_FMT_YUV420);
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300739 if (!codec && ctx->inst_type == CODA_INST_DECODER) {
740 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_H264,
741 V4L2_PIX_FMT_YUV420);
742 if (!codec)
743 return -EINVAL;
744 }
Javier Martin186b2502012-07-26 05:53:35 -0300745
746 if (!f->fmt.pix.colorspace)
747 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
748
Philipp Zabel57625592013-09-30 10:34:51 -0300749 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300750}
751
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300752static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300753{
754 struct coda_q_data *q_data;
755 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300756
Philipp Zabel14604e32014-07-11 06:36:22 -0300757 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300758 if (!vq)
759 return -EINVAL;
760
761 q_data = get_q_data(ctx, f->type);
762 if (!q_data)
763 return -EINVAL;
764
765 if (vb2_is_busy(vq)) {
766 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
767 return -EBUSY;
768 }
769
Philipp Zabelb96904e2013-05-23 10:42:58 -0300770 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300771 q_data->width = f->fmt.pix.width;
772 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300773 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300774 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300775 q_data->rect.left = 0;
776 q_data->rect.top = 0;
777 q_data->rect.width = f->fmt.pix.width;
778 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300779
780 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
781 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300782 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300783
784 return 0;
785}
786
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300787static int coda_s_fmt_vid_cap(struct file *file, void *priv,
788 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300789{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300790 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300791 int ret;
792
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300793 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300794 if (ret)
795 return ret;
796
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300797 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300798}
799
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300800static int coda_s_fmt_vid_out(struct file *file, void *priv,
801 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300802{
803 struct coda_ctx *ctx = fh_to_ctx(priv);
804 int ret;
805
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300806 ret = coda_try_fmt_vid_out(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 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300811 if (ret)
812 ctx->colorspace = f->fmt.pix.colorspace;
813
814 return ret;
815}
816
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300817static int coda_qbuf(struct file *file, void *priv,
818 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300819{
820 struct coda_ctx *ctx = fh_to_ctx(priv);
821
Philipp Zabel14604e32014-07-11 06:36:22 -0300822 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300823}
824
Philipp Zabel918c66f2013-06-27 06:59:01 -0300825static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
826 struct v4l2_buffer *buf)
827{
828 struct vb2_queue *src_vq;
829
Philipp Zabel14604e32014-07-11 06:36:22 -0300830 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300831
832 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
833 (buf->sequence == (ctx->qsequence - 1)));
834}
835
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300836static int coda_dqbuf(struct file *file, void *priv,
837 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300838{
839 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300840 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300841
Philipp Zabel14604e32014-07-11 06:36:22 -0300842 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300843
844 /* If this is the last capture buffer, emit an end-of-stream event */
845 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
846 coda_buf_is_end_of_stream(ctx, buf)) {
847 const struct v4l2_event eos_event = {
848 .type = V4L2_EVENT_EOS
849 };
850
851 v4l2_event_queue_fh(&ctx->fh, &eos_event);
852 }
853
854 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300855}
856
Philipp Zabel52c41672014-07-11 06:36:19 -0300857static int coda_g_selection(struct file *file, void *fh,
858 struct v4l2_selection *s)
859{
860 struct coda_ctx *ctx = fh_to_ctx(fh);
861 struct coda_q_data *q_data;
862 struct v4l2_rect r, *rsel;
863
864 q_data = get_q_data(ctx, s->type);
865 if (!q_data)
866 return -EINVAL;
867
868 r.left = 0;
869 r.top = 0;
870 r.width = q_data->width;
871 r.height = q_data->height;
872 rsel = &q_data->rect;
873
874 switch (s->target) {
875 case V4L2_SEL_TGT_CROP_DEFAULT:
876 case V4L2_SEL_TGT_CROP_BOUNDS:
877 rsel = &r;
878 /* fallthrough */
879 case V4L2_SEL_TGT_CROP:
880 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
881 return -EINVAL;
882 break;
883 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
884 case V4L2_SEL_TGT_COMPOSE_PADDED:
885 rsel = &r;
886 /* fallthrough */
887 case V4L2_SEL_TGT_COMPOSE:
888 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
889 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 return -EINVAL;
891 break;
892 default:
893 return -EINVAL;
894 }
895
896 s->r = *rsel;
897
898 return 0;
899}
900
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300901static int coda_try_decoder_cmd(struct file *file, void *fh,
902 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300903{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300904 if (dc->cmd != V4L2_DEC_CMD_STOP)
905 return -EINVAL;
906
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300907 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300908 return -EINVAL;
909
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300910 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300911 return -EINVAL;
912
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300913 return 0;
914}
915
916static int coda_decoder_cmd(struct file *file, void *fh,
917 struct v4l2_decoder_cmd *dc)
918{
919 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300920 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300921 int ret;
922
923 ret = coda_try_decoder_cmd(file, fh, dc);
924 if (ret < 0)
925 return ret;
926
927 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300928 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300929 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300930
931 /* Set the strem-end flag on this context */
932 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
933
Philipp Zabel89548442014-07-11 06:36:17 -0300934 if ((dev->devtype->product == CODA_960) &&
935 coda_isbusy(dev) &&
936 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
937 /* If this context is currently running, update the hardware flag */
938 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
939 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300940 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300941 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300942
Philipp Zabel918c66f2013-06-27 06:59:01 -0300943 return 0;
944}
945
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300946static int coda_subscribe_event(struct v4l2_fh *fh,
947 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300948{
949 switch (sub->type) {
950 case V4L2_EVENT_EOS:
951 return v4l2_event_subscribe(fh, sub, 0, NULL);
952 default:
953 return v4l2_ctrl_subscribe_event(fh, sub);
954 }
Javier Martin186b2502012-07-26 05:53:35 -0300955}
956
957static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300958 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300959
Philipp Zabel22e244b2014-07-18 07:22:43 -0300960 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300961 .vidioc_g_fmt_vid_cap = coda_g_fmt,
962 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
963 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300964
Philipp Zabel22e244b2014-07-18 07:22:43 -0300965 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300966 .vidioc_g_fmt_vid_out = coda_g_fmt,
967 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
968 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300969
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300970 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
971 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300972
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300973 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300974 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300975 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300976 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300977
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300978 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
979 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300980
Philipp Zabel52c41672014-07-11 06:36:19 -0300981 .vidioc_g_selection = coda_g_selection,
982
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300983 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300984 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300985
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300986 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300987 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300988};
989
Philipp Zabel918c66f2013-06-27 06:59:01 -0300990static int coda_start_decoding(struct coda_ctx *ctx);
991
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300992static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
993{
994 return kfifo_len(&ctx->bitstream_fifo);
995}
996
997static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
998{
999 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1000 struct coda_dev *dev = ctx->dev;
1001 u32 rd_ptr;
1002
1003 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1004 kfifo->out = (kfifo->in & ~kfifo->mask) |
1005 (rd_ptr - ctx->bitstream.paddr);
1006 if (kfifo->out > kfifo->in)
1007 kfifo->out -= kfifo->mask + 1;
1008}
1009
1010static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1011{
1012 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1013 struct coda_dev *dev = ctx->dev;
1014 u32 rd_ptr, wr_ptr;
1015
1016 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1017 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1018 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1019 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1020}
1021
1022static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1023{
1024 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1025 struct coda_dev *dev = ctx->dev;
1026 u32 wr_ptr;
1027
1028 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1029 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1030}
1031
1032static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1033{
1034 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1035 u32 n;
1036
1037 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1038 if (n < src_size)
1039 return -ENOSPC;
1040
1041 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1042 ctx->bitstream.size, DMA_TO_DEVICE);
1043
Philipp Zabel846ced92014-07-11 06:36:31 -03001044 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001045
1046 return 0;
1047}
1048
1049static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1050 struct vb2_buffer *src_buf)
1051{
1052 int ret;
1053
1054 if (coda_get_bitstream_payload(ctx) +
1055 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1056 return false;
1057
1058 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1059 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1060 return true;
1061 }
1062
1063 ret = coda_bitstream_queue(ctx, src_buf);
1064 if (ret < 0) {
1065 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1066 return false;
1067 }
1068 /* Sync read pointer to device */
1069 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1070 coda_kfifo_sync_to_device_write(ctx);
1071
Philipp Zabel84e23652014-07-11 06:36:34 -03001072 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001073
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001074 return true;
1075}
1076
1077static void coda_fill_bitstream(struct coda_ctx *ctx)
1078{
1079 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001080 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001081
Philipp Zabel14604e32014-07-11 06:36:22 -03001082 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1083 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001084
1085 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001086 /*
1087 * Source buffer is queued in the bitstream ringbuffer;
1088 * queue the timestamp and mark source buffer as done
1089 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001090 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001091
1092 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1093 if (ts) {
1094 ts->sequence = src_buf->v4l2_buf.sequence;
1095 ts->timecode = src_buf->v4l2_buf.timecode;
1096 ts->timestamp = src_buf->v4l2_buf.timestamp;
1097 list_add_tail(&ts->list, &ctx->timestamp_list);
1098 }
1099
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001100 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1101 } else {
1102 break;
1103 }
1104 }
1105}
1106
Philipp Zabel89548442014-07-11 06:36:17 -03001107static void coda_set_gdi_regs(struct coda_ctx *ctx)
1108{
1109 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1110 struct coda_dev *dev = ctx->dev;
1111 int i;
1112
1113 for (i = 0; i < 16; i++)
1114 coda_write(dev, tiled_map->xy2ca_map[i],
1115 CODA9_GDI_XY2_CAS_0 + 4 * i);
1116 for (i = 0; i < 4; i++)
1117 coda_write(dev, tiled_map->xy2ba_map[i],
1118 CODA9_GDI_XY2_BA_0 + 4 * i);
1119 for (i = 0; i < 16; i++)
1120 coda_write(dev, tiled_map->xy2ra_map[i],
1121 CODA9_GDI_XY2_RAS_0 + 4 * i);
1122 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1123 for (i = 0; i < 32; i++)
1124 coda_write(dev, tiled_map->rbc2axi_map[i],
1125 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1126}
1127
Javier Martin186b2502012-07-26 05:53:35 -03001128/*
1129 * Mem-to-mem operations.
1130 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001131static int coda_prepare_decode(struct coda_ctx *ctx)
1132{
1133 struct vb2_buffer *dst_buf;
1134 struct coda_dev *dev = ctx->dev;
1135 struct coda_q_data *q_data_dst;
1136 u32 stridey, height;
1137 u32 picture_y, picture_cb, picture_cr;
1138
Philipp Zabel14604e32014-07-11 06:36:22 -03001139 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001140 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1141
1142 if (ctx->params.rot_mode & CODA_ROT_90) {
1143 stridey = q_data_dst->height;
1144 height = q_data_dst->width;
1145 } else {
1146 stridey = q_data_dst->width;
1147 height = q_data_dst->height;
1148 }
1149
1150 /* Try to copy source buffer contents into the bitstream ringbuffer */
1151 mutex_lock(&ctx->bitstream_mutex);
1152 coda_fill_bitstream(ctx);
1153 mutex_unlock(&ctx->bitstream_mutex);
1154
1155 if (coda_get_bitstream_payload(ctx) < 512 &&
1156 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1157 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1158 "bitstream payload: %d, skipping\n",
1159 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001160 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001161 return -EAGAIN;
1162 }
1163
1164 /* Run coda_start_decoding (again) if not yet initialized */
1165 if (!ctx->initialized) {
1166 int ret = coda_start_decoding(ctx);
1167 if (ret < 0) {
1168 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001169 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001170 return -EAGAIN;
1171 } else {
1172 ctx->initialized = 1;
1173 }
1174 }
1175
Philipp Zabel89548442014-07-11 06:36:17 -03001176 if (dev->devtype->product == CODA_960)
1177 coda_set_gdi_regs(ctx);
1178
Philipp Zabel918c66f2013-06-27 06:59:01 -03001179 /* Set rotator output */
1180 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1181 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1182 /* Switch Cr and Cb for YVU420 format */
1183 picture_cr = picture_y + stridey * height;
1184 picture_cb = picture_cr + stridey / 2 * height / 2;
1185 } else {
1186 picture_cb = picture_y + stridey * height;
1187 picture_cr = picture_cb + stridey / 2 * height / 2;
1188 }
Philipp Zabel89548442014-07-11 06:36:17 -03001189
1190 if (dev->devtype->product == CODA_960) {
1191 /*
1192 * The CODA960 seems to have an internal list of buffers with
1193 * 64 entries that includes the registered frame buffers as
1194 * well as the rotator buffer output.
1195 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1196 */
1197 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1198 CODA9_CMD_DEC_PIC_ROT_INDEX);
1199 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1200 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1201 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1202 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1203 } else {
1204 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1205 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1206 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1207 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1208 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001209 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1210 CODA_CMD_DEC_PIC_ROT_MODE);
1211
1212 switch (dev->devtype->product) {
1213 case CODA_DX6:
1214 /* TBD */
1215 case CODA_7541:
1216 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1217 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001218 case CODA_960:
1219 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1220 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001221 }
1222
1223 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1224
1225 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1226 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1227
1228 return 0;
1229}
1230
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001231static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001232{
Javier Martin186b2502012-07-26 05:53:35 -03001233 struct coda_q_data *q_data_src, *q_data_dst;
1234 struct vb2_buffer *src_buf, *dst_buf;
1235 struct coda_dev *dev = ctx->dev;
1236 int force_ipicture;
1237 int quant_param = 0;
1238 u32 picture_y, picture_cb, picture_cr;
1239 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1240 u32 dst_fourcc;
1241
Philipp Zabel14604e32014-07-11 06:36:22 -03001242 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1243 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001244 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1245 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001246 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001247
Philipp Zabel918c66f2013-06-27 06:59:01 -03001248 src_buf->v4l2_buf.sequence = ctx->osequence;
1249 dst_buf->v4l2_buf.sequence = ctx->osequence;
1250 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001251
1252 /*
1253 * Workaround coda firmware BUG that only marks the first
1254 * frame as IDR. This is a problem for some decoders that can't
1255 * recover when a frame is lost.
1256 */
1257 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1258 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1259 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1260 } else {
1261 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1262 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1263 }
1264
Philipp Zabel89548442014-07-11 06:36:17 -03001265 if (dev->devtype->product == CODA_960)
1266 coda_set_gdi_regs(ctx);
1267
Javier Martin186b2502012-07-26 05:53:35 -03001268 /*
1269 * Copy headers at the beginning of the first frame for H.264 only.
1270 * In MPEG4 they are already copied by the coda.
1271 */
1272 if (src_buf->v4l2_buf.sequence == 0) {
1273 pic_stream_buffer_addr =
1274 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1275 ctx->vpu_header_size[0] +
1276 ctx->vpu_header_size[1] +
1277 ctx->vpu_header_size[2];
1278 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1279 ctx->vpu_header_size[0] -
1280 ctx->vpu_header_size[1] -
1281 ctx->vpu_header_size[2];
1282 memcpy(vb2_plane_vaddr(dst_buf, 0),
1283 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1284 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1285 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1286 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1287 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1288 ctx->vpu_header_size[2]);
1289 } else {
1290 pic_stream_buffer_addr =
1291 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1292 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1293 }
1294
1295 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1296 force_ipicture = 1;
1297 switch (dst_fourcc) {
1298 case V4L2_PIX_FMT_H264:
1299 quant_param = ctx->params.h264_intra_qp;
1300 break;
1301 case V4L2_PIX_FMT_MPEG4:
1302 quant_param = ctx->params.mpeg4_intra_qp;
1303 break;
1304 default:
1305 v4l2_warn(&ctx->dev->v4l2_dev,
1306 "cannot set intra qp, fmt not supported\n");
1307 break;
1308 }
1309 } else {
1310 force_ipicture = 0;
1311 switch (dst_fourcc) {
1312 case V4L2_PIX_FMT_H264:
1313 quant_param = ctx->params.h264_inter_qp;
1314 break;
1315 case V4L2_PIX_FMT_MPEG4:
1316 quant_param = ctx->params.mpeg4_inter_qp;
1317 break;
1318 default:
1319 v4l2_warn(&ctx->dev->v4l2_dev,
1320 "cannot set inter qp, fmt not supported\n");
1321 break;
1322 }
1323 }
1324
1325 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001326 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001327 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1328
1329
1330 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001331 switch (q_data_src->fourcc) {
1332 case V4L2_PIX_FMT_YVU420:
1333 /* Switch Cb and Cr for YVU420 format */
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001334 picture_cr = picture_y + q_data_src->bytesperline *
1335 q_data_src->height;
1336 picture_cb = picture_cr + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001337 q_data_src->height / 2;
1338 break;
1339 case V4L2_PIX_FMT_YUV420:
1340 default:
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001341 picture_cb = picture_y + q_data_src->bytesperline *
1342 q_data_src->height;
1343 picture_cr = picture_cb + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001344 q_data_src->height / 2;
1345 break;
1346 }
Javier Martin186b2502012-07-26 05:53:35 -03001347
Philipp Zabel89548442014-07-11 06:36:17 -03001348 if (dev->devtype->product == CODA_960) {
1349 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1350 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1351 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1352
1353 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1354 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1355 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1356 } else {
1357 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1358 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1359 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1360 }
Javier Martin186b2502012-07-26 05:53:35 -03001361 coda_write(dev, force_ipicture << 1 & 0x2,
1362 CODA_CMD_ENC_PIC_OPTION);
1363
1364 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1365 coda_write(dev, pic_stream_buffer_size / 1024,
1366 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001367
1368 if (!ctx->streamon_out) {
1369 /* After streamoff on the output side, set the stream end flag */
1370 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1371 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1372 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001373}
1374
1375static void coda_device_run(void *m2m_priv)
1376{
1377 struct coda_ctx *ctx = m2m_priv;
1378 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001379
1380 queue_work(dev->workqueue, &ctx->pic_run_work);
1381}
1382
1383static void coda_free_framebuffers(struct coda_ctx *ctx);
1384static void coda_free_context_buffers(struct coda_ctx *ctx);
1385
1386static void coda_seq_end_work(struct work_struct *work)
1387{
1388 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1389 struct coda_dev *dev = ctx->dev;
1390
1391 mutex_lock(&ctx->buffer_mutex);
1392 mutex_lock(&dev->coda_mutex);
1393
1394 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1395 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1396 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1397 v4l2_err(&dev->v4l2_dev,
1398 "CODA_COMMAND_SEQ_END failed\n");
1399 }
1400
1401 kfifo_init(&ctx->bitstream_fifo,
1402 ctx->bitstream.vaddr, ctx->bitstream.size);
1403
1404 coda_free_framebuffers(ctx);
1405 coda_free_context_buffers(ctx);
1406
1407 mutex_unlock(&dev->coda_mutex);
1408 mutex_unlock(&ctx->buffer_mutex);
1409}
1410
1411static void coda_finish_decode(struct coda_ctx *ctx);
1412static void coda_finish_encode(struct coda_ctx *ctx);
1413
1414static void coda_pic_run_work(struct work_struct *work)
1415{
1416 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1417 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001418 int ret;
1419
1420 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001421 mutex_lock(&dev->coda_mutex);
1422
Philipp Zabel918c66f2013-06-27 06:59:01 -03001423 if (ctx->inst_type == CODA_INST_DECODER) {
1424 ret = coda_prepare_decode(ctx);
1425 if (ret < 0) {
1426 mutex_unlock(&dev->coda_mutex);
1427 mutex_unlock(&ctx->buffer_mutex);
1428 /* job_finish scheduled by prepare_decode */
1429 return;
1430 }
1431 } else {
1432 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001433 }
1434
Philipp Zabelc2d22512013-06-21 03:55:28 -03001435 if (dev->devtype->product != CODA_DX6)
1436 coda_write(dev, ctx->iram_info.axi_sram_use,
1437 CODA7_REG_BIT_AXI_SRAM_USE);
1438
Philipp Zabel918c66f2013-06-27 06:59:01 -03001439 if (ctx->inst_type == CODA_INST_DECODER)
1440 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001441 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001442
1443 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1444 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001445
1446 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001447
1448 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001449 } else if (!ctx->aborting) {
1450 if (ctx->inst_type == CODA_INST_DECODER)
1451 coda_finish_decode(ctx);
1452 else
1453 coda_finish_encode(ctx);
1454 }
1455
1456 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1457 queue_work(dev->workqueue, &ctx->seq_end_work);
1458
1459 mutex_unlock(&dev->coda_mutex);
1460 mutex_unlock(&ctx->buffer_mutex);
1461
Philipp Zabel14604e32014-07-11 06:36:22 -03001462 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001463}
1464
1465static int coda_job_ready(void *m2m_priv)
1466{
1467 struct coda_ctx *ctx = m2m_priv;
1468
1469 /*
1470 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001471 * and 1 frame are needed. In the decoder case,
1472 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001473 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001474 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001475 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001476 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1477 "not ready: not enough video buffers.\n");
1478 return 0;
1479 }
1480
Philipp Zabel14604e32014-07-11 06:36:22 -03001481 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001482 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1483 "not ready: not enough video capture buffers.\n");
1484 return 0;
1485 }
1486
Philipp Zabel84e23652014-07-11 06:36:34 -03001487 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001488 ((ctx->inst_type == CODA_INST_DECODER) &&
1489 (coda_get_bitstream_payload(ctx) < 512) &&
1490 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1491 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1492 "%d: not ready: not enough bitstream data.\n",
1493 ctx->idx);
1494 return 0;
1495 }
1496
Philipp Zabel3e748262013-05-23 10:43:01 -03001497 if (ctx->aborting) {
1498 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1499 "not ready: aborting\n");
1500 return 0;
1501 }
1502
Javier Martin186b2502012-07-26 05:53:35 -03001503 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1504 "job ready\n");
1505 return 1;
1506}
1507
1508static void coda_job_abort(void *priv)
1509{
1510 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001511
1512 ctx->aborting = 1;
1513
1514 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1515 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001516}
1517
1518static void coda_lock(void *m2m_priv)
1519{
1520 struct coda_ctx *ctx = m2m_priv;
1521 struct coda_dev *pcdev = ctx->dev;
1522 mutex_lock(&pcdev->dev_mutex);
1523}
1524
1525static void coda_unlock(void *m2m_priv)
1526{
1527 struct coda_ctx *ctx = m2m_priv;
1528 struct coda_dev *pcdev = ctx->dev;
1529 mutex_unlock(&pcdev->dev_mutex);
1530}
1531
Philipp Zabel814c3762014-07-18 07:22:45 -03001532static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001533 .device_run = coda_device_run,
1534 .job_ready = coda_job_ready,
1535 .job_abort = coda_job_abort,
1536 .lock = coda_lock,
1537 .unlock = coda_unlock,
1538};
1539
Philipp Zabel89548442014-07-11 06:36:17 -03001540static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1541{
1542 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1543 int luma_map, chro_map, i;
1544
1545 memset(tiled_map, 0, sizeof(*tiled_map));
1546
1547 luma_map = 64;
1548 chro_map = 64;
1549 tiled_map->map_type = tiled_map_type;
1550 for (i = 0; i < 16; i++)
1551 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1552 for (i = 0; i < 4; i++)
1553 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1554 for (i = 0; i < 16; i++)
1555 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1556
1557 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1558 tiled_map->xy2rbc_config = 0;
1559 } else {
1560 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1561 tiled_map_type);
1562 return;
1563 }
1564}
1565
Javier Martin186b2502012-07-26 05:53:35 -03001566static void set_default_params(struct coda_ctx *ctx)
1567{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001568 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001569 int max_w;
1570 int max_h;
1571
Philipp Zabel121cacf2014-07-18 07:22:42 -03001572 if (ctx->inst_type == CODA_INST_ENCODER) {
1573 src_fourcc = V4L2_PIX_FMT_YUV420;
1574 dst_fourcc = V4L2_PIX_FMT_H264;
1575 } else {
1576 src_fourcc = V4L2_PIX_FMT_H264;
1577 dst_fourcc = V4L2_PIX_FMT_YUV420;
1578 }
1579 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001580 max_w = ctx->codec->max_w;
1581 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001582
Philipp Zabel121cacf2014-07-18 07:22:42 -03001583 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001584 ctx->colorspace = V4L2_COLORSPACE_REC709;
1585 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001586 ctx->aborting = 0;
1587
1588 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001589 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1590 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1591 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1592 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001593 ctx->q_data[V4L2_M2M_DST].width = max_w;
1594 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001595 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1596 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1597 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1598 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1599 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1600 } else {
1601 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1602 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
1603 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1604 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
1605 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001606 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1607 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1608 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1609 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001610
1611 if (ctx->dev->devtype->product == CODA_960)
1612 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001613}
1614
1615/*
1616 * Queue operations
1617 */
1618static int coda_queue_setup(struct vb2_queue *vq,
1619 const struct v4l2_format *fmt,
1620 unsigned int *nbuffers, unsigned int *nplanes,
1621 unsigned int sizes[], void *alloc_ctxs[])
1622{
1623 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001624 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001625 unsigned int size;
1626
Philipp Zabele34db062012-08-29 08:22:00 -03001627 q_data = get_q_data(ctx, vq->type);
1628 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001629
1630 *nplanes = 1;
1631 sizes[0] = size;
1632
1633 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1634
1635 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1636 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1637
1638 return 0;
1639}
1640
1641static int coda_buf_prepare(struct vb2_buffer *vb)
1642{
1643 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1644 struct coda_q_data *q_data;
1645
1646 q_data = get_q_data(ctx, vb->vb2_queue->type);
1647
1648 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1649 v4l2_warn(&ctx->dev->v4l2_dev,
1650 "%s data will not fit into plane (%lu < %lu)\n",
1651 __func__, vb2_plane_size(vb, 0),
1652 (long)q_data->sizeimage);
1653 return -EINVAL;
1654 }
1655
Javier Martin186b2502012-07-26 05:53:35 -03001656 return 0;
1657}
1658
1659static void coda_buf_queue(struct vb2_buffer *vb)
1660{
1661 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001662 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001663 struct coda_q_data *q_data;
1664
1665 q_data = get_q_data(ctx, vb->vb2_queue->type);
1666
1667 /*
1668 * In the decoder case, immediately try to copy the buffer into the
1669 * bitstream ringbuffer and mark it as ready to be dequeued.
1670 */
1671 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1672 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1673 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001674 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001675 * the stream end
1676 */
Philipp Zabel89548442014-07-11 06:36:17 -03001677 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001678 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001679 if ((dev->devtype->product == CODA_960) &&
1680 coda_isbusy(dev) &&
1681 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1682 /* if this decoder instance is running, set the stream end flag */
1683 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1684 }
1685 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001686 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001687 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001688 if (vb2_is_streaming(vb->vb2_queue))
1689 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001690 mutex_unlock(&ctx->bitstream_mutex);
1691 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001692 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001693 }
Javier Martin186b2502012-07-26 05:53:35 -03001694}
1695
Philipp Zabel86eda902013-05-23 10:42:57 -03001696static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1697{
1698 struct coda_dev *dev = ctx->dev;
1699 u32 *p = ctx->parabuf.vaddr;
1700
1701 if (dev->devtype->product == CODA_DX6)
1702 p[index] = value;
1703 else
1704 p[index ^ 1] = value;
1705}
1706
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001707static int coda_alloc_aux_buf(struct coda_dev *dev,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001708 struct coda_aux_buf *buf, size_t size,
1709 const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001710{
1711 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1712 GFP_KERNEL);
1713 if (!buf->vaddr)
1714 return -ENOMEM;
1715
1716 buf->size = size;
1717
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001718 if (name && parent) {
1719 buf->blob.data = buf->vaddr;
1720 buf->blob.size = size;
1721 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
1722 if (!buf->dentry)
1723 dev_warn(&dev->plat_dev->dev,
1724 "failed to create debugfs entry %s\n", name);
1725 }
1726
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001727 return 0;
1728}
1729
1730static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001731 struct coda_aux_buf *buf, size_t size,
1732 const char *name)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001733{
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001734 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001735}
1736
1737static void coda_free_aux_buf(struct coda_dev *dev,
1738 struct coda_aux_buf *buf)
1739{
1740 if (buf->vaddr) {
1741 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1742 buf->vaddr, buf->paddr);
1743 buf->vaddr = NULL;
1744 buf->size = 0;
1745 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001746 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001747}
1748
1749static void coda_free_framebuffers(struct coda_ctx *ctx)
1750{
1751 int i;
1752
1753 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1754 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1755}
1756
Philipp Zabelec25f682012-07-20 08:54:29 -03001757static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1758{
1759 struct coda_dev *dev = ctx->dev;
Philipp Zabel7c3df782014-07-11 06:36:38 -03001760 int width, height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001761 dma_addr_t paddr;
1762 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001763 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001764 int i;
1765
Philipp Zabel7c3df782014-07-11 06:36:38 -03001766 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
1767 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
1768 width = round_up(q_data->width, 16);
1769 height = round_up(q_data->height, 16);
1770 } else {
1771 width = round_up(q_data->width, 8);
1772 height = q_data->height;
1773 }
1774 ysize = width * height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001775
Philipp Zabelec25f682012-07-20 08:54:29 -03001776 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001777 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001778 size_t size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001779 char *name;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001780
Philipp Zabelaed14b02014-07-11 06:36:15 -03001781 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001782 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1783 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001784 size += ysize / 4;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001785 name = kasprintf(GFP_KERNEL, "fb%d", i);
1786 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
1787 size, name);
1788 kfree(name);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001789 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001790 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001791 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001792 }
1793 }
1794
1795 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001796 for (i = 0; i < ctx->num_internal_frames; i++) {
1797 paddr = ctx->internal_frames[i].paddr;
1798 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1799 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1800 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001801
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001802 /* mvcol buffer for h.264 */
1803 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1804 dev->devtype->product != CODA_DX6)
1805 coda_parabuf_write(ctx, 96 + i,
1806 ctx->internal_frames[i].paddr +
1807 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001808 }
1809
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001810 /* mvcol buffer for mpeg4 */
1811 if ((dev->devtype->product != CODA_DX6) &&
1812 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1813 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1814 ysize + ysize/4 + ysize/4);
1815
Philipp Zabelec25f682012-07-20 08:54:29 -03001816 return 0;
1817}
1818
Javier Martin3f3f5c72012-10-29 05:20:29 -03001819static int coda_h264_padding(int size, char *p)
1820{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001821 int nal_size;
1822 int diff;
1823
Javier Martin832fbb52012-10-29 08:34:59 -03001824 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001825 if (diff == 0)
1826 return 0;
1827
Javier Martin832fbb52012-10-29 08:34:59 -03001828 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001829 memcpy(p, coda_filler_nal, nal_size);
1830
1831 /* Add rbsp stop bit and trailing at the end */
1832 *(p + nal_size - 1) = 0x80;
1833
1834 return nal_size;
1835}
1836
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001837static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1838{
1839 phys_addr_t ret;
1840
1841 size = round_up(size, 1024);
1842 if (size > iram->remaining)
1843 return 0;
1844 iram->remaining -= size;
1845
1846 ret = iram->next_paddr;
1847 iram->next_paddr += size;
1848
1849 return ret;
1850}
1851
Philipp Zabelc2d22512013-06-21 03:55:28 -03001852static void coda_setup_iram(struct coda_ctx *ctx)
1853{
1854 struct coda_iram_info *iram_info = &ctx->iram_info;
1855 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001856 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001857 int dbk_bits;
1858 int bit_bits;
1859 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001860
1861 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001862 iram_info->next_paddr = dev->iram.paddr;
1863 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001864
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001865 switch (dev->devtype->product) {
1866 case CODA_7541:
1867 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1868 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1869 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1870 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001871 case CODA_960:
1872 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1873 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1874 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1875 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001876 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001877 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001878 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001879
1880 if (ctx->inst_type == CODA_INST_ENCODER) {
1881 struct coda_q_data *q_data_src;
1882
1883 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1884 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1885
1886 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001887 if (dev->devtype->product == CODA_7541) {
1888 iram_info->search_ram_size = round_up(mb_width * 16 *
1889 36 + 2048, 1024);
1890 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1891 iram_info->search_ram_size);
1892 if (!iram_info->search_ram_paddr) {
1893 pr_err("IRAM is smaller than the search ram size\n");
1894 goto out;
1895 }
1896 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1897 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001898 }
1899
1900 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001901 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1902 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1903 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001904 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001905 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001906
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001907 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1908 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001909 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001910 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001911
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001912 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1913 if (!iram_info->buf_ip_ac_dc_use)
1914 goto out;
1915 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001916
Philipp Zabel8358e762013-06-21 03:55:32 -03001917 /* OVL and BTP disabled for encoder */
1918 } else if (ctx->inst_type == CODA_INST_DECODER) {
1919 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001920
1921 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1922 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001923
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001924 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1925 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1926 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001927 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001928 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001929
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001930 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1931 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001932 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001933 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001934
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001935 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1936 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001937 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001938 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001939
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001940 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001941 }
1942
1943out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001944 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1945 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1946 "IRAM smaller than needed\n");
1947
1948 if (dev->devtype->product == CODA_7541) {
1949 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001950 if (ctx->inst_type == CODA_INST_DECODER) {
1951 /* fw 1.4.50 */
1952 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1953 CODA7_USE_IP_ENABLE);
1954 } else {
1955 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001956 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1957 CODA7_USE_HOST_DBK_ENABLE |
1958 CODA7_USE_IP_ENABLE |
1959 CODA7_USE_DBK_ENABLE);
1960 }
1961 }
1962}
1963
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001964static void coda_free_context_buffers(struct coda_ctx *ctx)
1965{
1966 struct coda_dev *dev = ctx->dev;
1967
Philipp Zabel918c66f2013-06-27 06:59:01 -03001968 coda_free_aux_buf(dev, &ctx->slicebuf);
1969 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001970 if (dev->devtype->product != CODA_DX6)
1971 coda_free_aux_buf(dev, &ctx->workbuf);
1972}
1973
1974static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1975 struct coda_q_data *q_data)
1976{
1977 struct coda_dev *dev = ctx->dev;
1978 size_t size;
1979 int ret;
1980
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001981 if (dev->devtype->product == CODA_DX6)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001982 return 0;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001983
Philipp Zabel918c66f2013-06-27 06:59:01 -03001984 if (ctx->psbuf.vaddr) {
1985 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1986 return -EBUSY;
1987 }
1988 if (ctx->slicebuf.vaddr) {
1989 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1990 return -EBUSY;
1991 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001992 if (ctx->workbuf.vaddr) {
1993 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1994 ret = -EBUSY;
1995 return -ENOMEM;
1996 }
1997
Philipp Zabel918c66f2013-06-27 06:59:01 -03001998 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1999 /* worst case slice size */
2000 size = (DIV_ROUND_UP(q_data->width, 16) *
2001 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002002 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, "slicebuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002003 if (ret < 0) {
2004 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
2005 ctx->slicebuf.size);
2006 return ret;
2007 }
2008 }
2009
2010 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002011 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002012 if (ret < 0) {
2013 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
2014 goto err;
2015 }
2016 }
2017
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002018 size = dev->devtype->workbuf_size;
2019 if (dev->devtype->product == CODA_960 &&
2020 q_data->fourcc == V4L2_PIX_FMT_H264)
2021 size += CODA9_PS_SAVE_SIZE;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002022 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002023 if (ret < 0) {
2024 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2025 ctx->workbuf.size);
2026 goto err;
2027 }
2028
2029 return 0;
2030
2031err:
2032 coda_free_context_buffers(ctx);
2033 return ret;
2034}
2035
Philipp Zabel918c66f2013-06-27 06:59:01 -03002036static int coda_start_decoding(struct coda_ctx *ctx)
2037{
2038 struct coda_q_data *q_data_src, *q_data_dst;
2039 u32 bitstream_buf, bitstream_size;
2040 struct coda_dev *dev = ctx->dev;
2041 int width, height;
2042 u32 src_fourcc;
2043 u32 val;
2044 int ret;
2045
2046 /* Start decoding */
2047 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2048 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2049 bitstream_buf = ctx->bitstream.paddr;
2050 bitstream_size = ctx->bitstream.size;
2051 src_fourcc = q_data_src->fourcc;
2052
2053 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2054
2055 /* Update coda bitstream read and write pointers from kfifo */
2056 coda_kfifo_sync_to_device_full(ctx);
2057
2058 ctx->display_idx = -1;
2059 ctx->frm_dis_flg = 0;
2060 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2061
2062 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2063 CODA_REG_BIT_BIT_STREAM_PARAM);
2064
2065 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2066 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2067 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002068 if ((dev->devtype->product == CODA_7541) ||
2069 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002070 val |= CODA_REORDER_ENABLE;
2071 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2072
2073 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002074 if (dev->devtype->product == CODA_960 &&
2075 src_fourcc == V4L2_PIX_FMT_MPEG4)
2076 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2077 else
2078 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002079 if (src_fourcc == V4L2_PIX_FMT_H264) {
2080 if (dev->devtype->product == CODA_7541) {
2081 coda_write(dev, ctx->psbuf.paddr,
2082 CODA_CMD_DEC_SEQ_PS_BB_START);
2083 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2084 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2085 }
Philipp Zabel89548442014-07-11 06:36:17 -03002086 if (dev->devtype->product == CODA_960) {
2087 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2088 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2089 }
2090 }
2091 if (dev->devtype->product != CODA_960) {
2092 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002093 }
2094
2095 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2096 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2097 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2098 return -ETIMEDOUT;
2099 }
2100
2101 /* Update kfifo out pointer from coda bitstream read pointer */
2102 coda_kfifo_sync_from_device(ctx);
2103
2104 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2105
2106 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2107 v4l2_err(&dev->v4l2_dev,
2108 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2109 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2110 return -EAGAIN;
2111 }
2112
2113 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2114 if (dev->devtype->product == CODA_DX6) {
2115 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2116 height = val & CODADX6_PICHEIGHT_MASK;
2117 } else {
2118 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2119 height = val & CODA7_PICHEIGHT_MASK;
2120 }
2121
2122 if (width > q_data_dst->width || height > q_data_dst->height) {
2123 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2124 width, height, q_data_dst->width, q_data_dst->height);
2125 return -EINVAL;
2126 }
2127
2128 width = round_up(width, 16);
2129 height = round_up(height, 16);
2130
2131 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2132 __func__, ctx->idx, width, height);
2133
Philipp Zabela500a932014-07-11 06:36:13 -03002134 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002135 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2136 v4l2_err(&dev->v4l2_dev,
2137 "not enough framebuffers to decode (%d < %d)\n",
2138 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2139 return -EINVAL;
2140 }
2141
Philipp Zabel52c41672014-07-11 06:36:19 -03002142 if (src_fourcc == V4L2_PIX_FMT_H264) {
2143 u32 left_right;
2144 u32 top_bottom;
2145
2146 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2147 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2148
2149 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2150 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2151 q_data_dst->rect.width = width - q_data_dst->rect.left -
2152 (left_right & 0x3ff);
2153 q_data_dst->rect.height = height - q_data_dst->rect.top -
2154 (top_bottom & 0x3ff);
2155 }
2156
Philipp Zabel918c66f2013-06-27 06:59:01 -03002157 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2158 if (ret < 0)
2159 return ret;
2160
2161 /* Tell the decoder how many frame buffers we allocated. */
2162 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2163 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2164
2165 if (dev->devtype->product != CODA_DX6) {
2166 /* Set secondary AXI IRAM */
2167 coda_setup_iram(ctx);
2168
2169 coda_write(dev, ctx->iram_info.buf_bit_use,
2170 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2171 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2172 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2173 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2174 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2175 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2176 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2177 coda_write(dev, ctx->iram_info.buf_ovl_use,
2178 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002179 if (dev->devtype->product == CODA_960)
2180 coda_write(dev, ctx->iram_info.buf_btp_use,
2181 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2182 }
2183
2184 if (dev->devtype->product == CODA_960) {
2185 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2186
2187 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2188 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2189 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2190 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2191 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2192 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002193 }
2194
2195 if (src_fourcc == V4L2_PIX_FMT_H264) {
2196 coda_write(dev, ctx->slicebuf.paddr,
2197 CODA_CMD_SET_FRAME_SLICE_BB_START);
2198 coda_write(dev, ctx->slicebuf.size / 1024,
2199 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2200 }
2201
2202 if (dev->devtype->product == CODA_7541) {
2203 int max_mb_x = 1920 / 16;
2204 int max_mb_y = 1088 / 16;
2205 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002206
Philipp Zabel918c66f2013-06-27 06:59:01 -03002207 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2208 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002209 } else if (dev->devtype->product == CODA_960) {
2210 int max_mb_x = 1920 / 16;
2211 int max_mb_y = 1088 / 16;
2212 int max_mb_num = max_mb_x * max_mb_y;
2213
2214 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2215 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002216 }
2217
2218 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2219 v4l2_err(&ctx->dev->v4l2_dev,
2220 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2221 return -ETIMEDOUT;
2222 }
2223
2224 return 0;
2225}
2226
Philipp Zabeld35167a2013-05-23 10:42:59 -03002227static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2228 int header_code, u8 *header, int *size)
2229{
2230 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002231 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002232 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002233 int i;
2234
2235 if (dev->devtype->product == CODA_960)
2236 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002237
2238 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2239 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002240 bufsize = vb2_plane_size(buf, 0);
2241 if (dev->devtype->product == CODA_960)
2242 bufsize /= 1024;
2243 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002244 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2245 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2246 if (ret < 0) {
2247 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2248 return ret;
2249 }
Philipp Zabel89548442014-07-11 06:36:17 -03002250
2251 if (dev->devtype->product == CODA_960) {
2252 for (i = 63; i > 0; i--)
2253 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2254 break;
2255 *size = i + 1;
2256 } else {
2257 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2258 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2259 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002260 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2261
2262 return 0;
2263}
2264
Philipp Zabel89548442014-07-11 06:36:17 -03002265static int coda_start_encoding(struct coda_ctx *ctx);
2266
Javier Martin186b2502012-07-26 05:53:35 -03002267static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2268{
2269 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2270 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002271 struct coda_dev *dev = ctx->dev;
2272 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002273 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002274 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002275
Philipp Zabelb96904e2013-05-23 10:42:58 -03002276 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002277 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2278 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03002279 /* copy the buffers that where queued before streamon */
2280 mutex_lock(&ctx->bitstream_mutex);
2281 coda_fill_bitstream(ctx);
2282 mutex_unlock(&ctx->bitstream_mutex);
2283
Philipp Zabel918c66f2013-06-27 06:59:01 -03002284 if (coda_get_bitstream_payload(ctx) < 512)
2285 return -EINVAL;
2286 } else {
2287 if (count < 1)
2288 return -EINVAL;
2289 }
2290
2291 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002292 } else {
2293 if (count < 1)
2294 return -EINVAL;
2295
2296 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002297 }
Javier Martin186b2502012-07-26 05:53:35 -03002298
Philipp Zabelb96904e2013-05-23 10:42:58 -03002299 /* Don't start the coda unless both queues are on */
2300 if (!(ctx->streamon_out & ctx->streamon_cap))
2301 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002302
Philipp Zabeleb107512013-09-30 10:34:45 -03002303 /* Allow decoder device_run with no new buffers queued */
2304 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002305 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002306
Philipp Zabelb96904e2013-05-23 10:42:58 -03002307 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002308 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002309 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002310
Philipp Zabelb96904e2013-05-23 10:42:58 -03002311 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2312 q_data_dst->fourcc);
2313 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002314 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2315 return -EINVAL;
2316 }
2317
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002318 /* Allocate per-instance buffers */
2319 ret = coda_alloc_context_buffers(ctx, q_data_src);
2320 if (ret < 0)
2321 return ret;
2322
Philipp Zabel918c66f2013-06-27 06:59:01 -03002323 if (ctx->inst_type == CODA_INST_DECODER) {
2324 mutex_lock(&dev->coda_mutex);
2325 ret = coda_start_decoding(ctx);
2326 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002327 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002328 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002329 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002330 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002331 } else {
2332 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002333 }
2334
Philipp Zabel89548442014-07-11 06:36:17 -03002335 ctx->initialized = 1;
2336 return ret;
2337}
2338
2339static int coda_start_encoding(struct coda_ctx *ctx)
2340{
2341 struct coda_dev *dev = ctx->dev;
2342 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2343 struct coda_q_data *q_data_src, *q_data_dst;
2344 u32 bitstream_buf, bitstream_size;
2345 struct vb2_buffer *buf;
2346 int gamma, ret, value;
2347 u32 dst_fourcc;
2348
2349 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2350 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2351 dst_fourcc = q_data_dst->fourcc;
2352
Philipp Zabel14604e32014-07-11 06:36:22 -03002353 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002354 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2355 bitstream_size = q_data_dst->sizeimage;
2356
Javier Martin186b2502012-07-26 05:53:35 -03002357 if (!coda_is_initialized(dev)) {
2358 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2359 return -EFAULT;
2360 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002361
2362 mutex_lock(&dev->coda_mutex);
2363
Javier Martin186b2502012-07-26 05:53:35 -03002364 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002365 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2366 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002367 switch (dev->devtype->product) {
2368 case CODA_DX6:
2369 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2370 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2371 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002372 case CODA_960:
2373 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2374 /* fallthrough */
2375 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002376 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2377 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002378 break;
Javier Martin186b2502012-07-26 05:53:35 -03002379 }
2380
Philipp Zabel89548442014-07-11 06:36:17 -03002381 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2382 value &= ~(1 << 2 | 0x7 << 9);
2383 ctx->frame_mem_ctrl = value;
2384 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2385
Philipp Zabel10436672012-07-02 09:03:55 -03002386 if (dev->devtype->product == CODA_DX6) {
2387 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002388 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002389 }
Javier Martin186b2502012-07-26 05:53:35 -03002390
2391 /* Could set rotation here if needed */
2392 switch (dev->devtype->product) {
2393 case CODA_DX6:
2394 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002395 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002396 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002397 case CODA_7541:
2398 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2399 value = (round_up(q_data_src->width, 16) &
2400 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2401 value |= (round_up(q_data_src->height, 16) &
2402 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2403 break;
2404 }
2405 /* fallthrough */
2406 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002407 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002408 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002409 }
Javier Martin186b2502012-07-26 05:53:35 -03002410 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2411 coda_write(dev, ctx->params.framerate,
2412 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2413
Philipp Zabelb96904e2013-05-23 10:42:58 -03002414 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002415 switch (dst_fourcc) {
2416 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002417 if (dev->devtype->product == CODA_960)
2418 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2419 else
2420 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002421 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2422 break;
2423 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002424 if (dev->devtype->product == CODA_960)
2425 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2426 else
2427 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002428 if (ctx->params.h264_deblk_enabled) {
2429 value = ((ctx->params.h264_deblk_alpha &
2430 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2431 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2432 ((ctx->params.h264_deblk_beta &
2433 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2434 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2435 } else {
2436 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2437 }
2438 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002439 break;
2440 default:
2441 v4l2_err(v4l2_dev,
2442 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002443 ret = -EINVAL;
2444 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002445 }
2446
Philipp Zabelc566c782012-08-08 11:59:38 -03002447 switch (ctx->params.slice_mode) {
2448 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2449 value = 0;
2450 break;
2451 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2452 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2453 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002454 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002455 break;
2456 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2457 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2458 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2459 value |= 1 & CODA_SLICING_MODE_MASK;
2460 break;
2461 }
Javier Martin186b2502012-07-26 05:53:35 -03002462 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002463 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002464 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2465
2466 if (ctx->params.bitrate) {
2467 /* Rate control enabled */
2468 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2469 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002470 if (dev->devtype->product == CODA_960)
2471 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002472 } else {
2473 value = 0;
2474 }
2475 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2476
2477 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002478 coda_write(dev, ctx->params.intra_refresh,
2479 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002480
2481 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2482 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2483
Javier Martin186b2502012-07-26 05:53:35 -03002484
Philipp Zabel89548442014-07-11 06:36:17 -03002485 value = 0;
2486 if (dev->devtype->product == CODA_960)
2487 gamma = CODA9_DEFAULT_GAMMA;
2488 else
2489 gamma = CODA_DEFAULT_GAMMA;
2490 if (gamma > 0) {
2491 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2492 CODA_CMD_ENC_SEQ_RC_GAMMA);
2493 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002494
2495 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2496 coda_write(dev,
2497 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2498 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2499 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2500 }
Philipp Zabel89548442014-07-11 06:36:17 -03002501 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002502 if (ctx->params.h264_max_qp)
2503 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002504 if (CODA_DEFAULT_GAMMA > 0)
2505 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002506 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002507 if (CODA_DEFAULT_GAMMA > 0) {
2508 if (dev->devtype->product == CODA_DX6)
2509 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2510 else
2511 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2512 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002513 if (ctx->params.h264_min_qp)
2514 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2515 if (ctx->params.h264_max_qp)
2516 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002517 }
Javier Martin186b2502012-07-26 05:53:35 -03002518 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2519
Philipp Zabel89548442014-07-11 06:36:17 -03002520 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2521
Philipp Zabelc2d22512013-06-21 03:55:28 -03002522 coda_setup_iram(ctx);
2523
Javier Martin186b2502012-07-26 05:53:35 -03002524 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002525 switch (dev->devtype->product) {
2526 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002527 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002528 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002529 break;
2530 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002531 coda_write(dev, ctx->iram_info.search_ram_paddr,
2532 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2533 coda_write(dev, ctx->iram_info.search_ram_size,
2534 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002535 break;
2536 case CODA_960:
2537 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2538 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002539 }
Javier Martin186b2502012-07-26 05:53:35 -03002540 }
2541
Philipp Zabelfcb62822013-05-23 10:43:00 -03002542 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2543 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002544 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002545 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002546 }
2547
Philipp Zabelfcb62822013-05-23 10:43:00 -03002548 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2549 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2550 ret = -EFAULT;
2551 goto out;
2552 }
Javier Martin186b2502012-07-26 05:53:35 -03002553
Philipp Zabel89548442014-07-11 06:36:17 -03002554 if (dev->devtype->product == CODA_960)
2555 ctx->num_internal_frames = 4;
2556 else
2557 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002558 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002559 if (ret < 0) {
2560 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2561 goto out;
2562 }
Javier Martin186b2502012-07-26 05:53:35 -03002563
Philipp Zabelec25f682012-07-20 08:54:29 -03002564 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002565 coda_write(dev, q_data_src->bytesperline,
2566 CODA_CMD_SET_FRAME_BUF_STRIDE);
2567 if (dev->devtype->product == CODA_7541) {
2568 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002569 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002570 }
Philipp Zabel10436672012-07-02 09:03:55 -03002571 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002572 coda_write(dev, ctx->iram_info.buf_bit_use,
2573 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2574 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2575 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2576 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2577 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2578 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2579 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2580 coda_write(dev, ctx->iram_info.buf_ovl_use,
2581 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002582 if (dev->devtype->product == CODA_960) {
2583 coda_write(dev, ctx->iram_info.buf_btp_use,
2584 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2585
2586 /* FIXME */
2587 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2588 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2589 }
Philipp Zabel10436672012-07-02 09:03:55 -03002590 }
Philipp Zabel89548442014-07-11 06:36:17 -03002591
Philipp Zabelfcb62822013-05-23 10:43:00 -03002592 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2593 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002594 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002595 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002596 }
2597
2598 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002599 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002600 switch (dst_fourcc) {
2601 case V4L2_PIX_FMT_H264:
2602 /*
2603 * Get SPS in the first frame and copy it to an
2604 * intermediate buffer.
2605 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002606 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2607 &ctx->vpu_header[0][0],
2608 &ctx->vpu_header_size[0]);
2609 if (ret < 0)
2610 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002611
2612 /*
2613 * Get PPS in the first frame and copy it to an
2614 * intermediate buffer.
2615 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002616 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2617 &ctx->vpu_header[1][0],
2618 &ctx->vpu_header_size[1]);
2619 if (ret < 0)
2620 goto out;
2621
Javier Martin3f3f5c72012-10-29 05:20:29 -03002622 /*
2623 * Length of H.264 headers is variable and thus it might not be
2624 * aligned for the coda to append the encoded frame. In that is
2625 * the case a filler NAL must be added to header 2.
2626 */
2627 ctx->vpu_header_size[2] = coda_h264_padding(
2628 (ctx->vpu_header_size[0] +
2629 ctx->vpu_header_size[1]),
2630 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002631 break;
2632 case V4L2_PIX_FMT_MPEG4:
2633 /*
2634 * Get VOS in the first frame and copy it to an
2635 * intermediate buffer
2636 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002637 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2638 &ctx->vpu_header[0][0],
2639 &ctx->vpu_header_size[0]);
2640 if (ret < 0)
2641 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002642
Philipp Zabeld35167a2013-05-23 10:42:59 -03002643 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2644 &ctx->vpu_header[1][0],
2645 &ctx->vpu_header_size[1]);
2646 if (ret < 0)
2647 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002648
Philipp Zabeld35167a2013-05-23 10:42:59 -03002649 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2650 &ctx->vpu_header[2][0],
2651 &ctx->vpu_header_size[2]);
2652 if (ret < 0)
2653 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002654 break;
2655 default:
2656 /* No more formats need to save headers at the moment */
2657 break;
2658 }
2659
Philipp Zabeld35167a2013-05-23 10:42:59 -03002660out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002661 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002662 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002663}
2664
Hans Verkuile37559b2014-04-17 02:47:21 -03002665static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002666{
2667 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002668 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002669
2670 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002671 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002672 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002673 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002674
Philipp Zabel89548442014-07-11 06:36:17 -03002675 if (ctx->inst_type == CODA_INST_DECODER &&
2676 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2677 /* if this decoder instance is running, set the stream end flag */
2678 if (dev->devtype->product == CODA_960) {
2679 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2680
2681 val |= CODA_BIT_STREAM_END_FLAG;
2682 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2683 ctx->bit_stream_param = val;
2684 }
2685 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002686 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2687
2688 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002689 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002690 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002691 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002692 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002693
2694 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002695 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002696 }
2697
Philipp Zabel918c66f2013-06-27 06:59:01 -03002698 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002699 struct coda_timestamp *ts;
2700
2701 while (!list_empty(&ctx->timestamp_list)) {
2702 ts = list_first_entry(&ctx->timestamp_list,
2703 struct coda_timestamp, list);
2704 list_del(&ts->list);
2705 kfree(ts);
2706 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002707 kfifo_init(&ctx->bitstream_fifo,
2708 ctx->bitstream.vaddr, ctx->bitstream.size);
2709 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002710 }
Javier Martin186b2502012-07-26 05:53:35 -03002711}
2712
Philipp Zabel121cacf2014-07-18 07:22:42 -03002713static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03002714 .queue_setup = coda_queue_setup,
2715 .buf_prepare = coda_buf_prepare,
2716 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002717 .start_streaming = coda_start_streaming,
2718 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002719 .wait_prepare = vb2_ops_wait_prepare,
2720 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002721};
2722
2723static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2724{
2725 struct coda_ctx *ctx =
2726 container_of(ctrl->handler, struct coda_ctx, ctrls);
2727
2728 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2729 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2730
2731 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002732 case V4L2_CID_HFLIP:
2733 if (ctrl->val)
2734 ctx->params.rot_mode |= CODA_MIR_HOR;
2735 else
2736 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2737 break;
2738 case V4L2_CID_VFLIP:
2739 if (ctrl->val)
2740 ctx->params.rot_mode |= CODA_MIR_VER;
2741 else
2742 ctx->params.rot_mode &= ~CODA_MIR_VER;
2743 break;
Javier Martin186b2502012-07-26 05:53:35 -03002744 case V4L2_CID_MPEG_VIDEO_BITRATE:
2745 ctx->params.bitrate = ctrl->val / 1000;
2746 break;
2747 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2748 ctx->params.gop_size = ctrl->val;
2749 break;
2750 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2751 ctx->params.h264_intra_qp = ctrl->val;
2752 break;
2753 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2754 ctx->params.h264_inter_qp = ctrl->val;
2755 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002756 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2757 ctx->params.h264_min_qp = ctrl->val;
2758 break;
2759 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2760 ctx->params.h264_max_qp = ctrl->val;
2761 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002762 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2763 ctx->params.h264_deblk_alpha = ctrl->val;
2764 break;
2765 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2766 ctx->params.h264_deblk_beta = ctrl->val;
2767 break;
2768 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2769 ctx->params.h264_deblk_enabled = (ctrl->val ==
2770 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2771 break;
Javier Martin186b2502012-07-26 05:53:35 -03002772 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2773 ctx->params.mpeg4_intra_qp = ctrl->val;
2774 break;
2775 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2776 ctx->params.mpeg4_inter_qp = ctrl->val;
2777 break;
2778 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2779 ctx->params.slice_mode = ctrl->val;
2780 break;
2781 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2782 ctx->params.slice_max_mb = ctrl->val;
2783 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002784 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2785 ctx->params.slice_max_bits = ctrl->val * 8;
2786 break;
Javier Martin186b2502012-07-26 05:53:35 -03002787 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2788 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002789 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2790 ctx->params.intra_refresh = ctrl->val;
2791 break;
Javier Martin186b2502012-07-26 05:53:35 -03002792 default:
2793 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2794 "Invalid control, id=%d, val=%d\n",
2795 ctrl->id, ctrl->val);
2796 return -EINVAL;
2797 }
2798
2799 return 0;
2800}
2801
Philipp Zabel814c3762014-07-18 07:22:45 -03002802static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03002803 .s_ctrl = coda_s_ctrl,
2804};
2805
2806static int coda_ctrls_setup(struct coda_ctx *ctx)
2807{
2808 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2809
2810 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002811 V4L2_CID_HFLIP, 0, 1, 1, 0);
2812 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2813 V4L2_CID_VFLIP, 0, 1, 1, 0);
2814 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002815 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2816 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2817 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2818 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002819 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002820 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002821 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002822 if (ctx->dev->devtype->product != CODA_960) {
2823 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2824 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2825 }
2826 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2827 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002828 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002829 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2830 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2831 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2832 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2833 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2834 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2835 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2836 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002837 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2838 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2839 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2840 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2841 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002842 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2843 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002844 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2845 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002846 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2847 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002848 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2849 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2850 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2851 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2852 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002853 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2854 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002855
2856 if (ctx->ctrls.error) {
2857 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2858 ctx->ctrls.error);
2859 return -EINVAL;
2860 }
2861
2862 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2863}
2864
Philipp Zabel121cacf2014-07-18 07:22:42 -03002865static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03002866{
Philipp Zabel121cacf2014-07-18 07:22:42 -03002867 vq->drv_priv = ctx;
2868 vq->ops = &coda_qops;
2869 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2870 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2871 vq->lock = &ctx->dev->dev_mutex;
2872
2873 return vb2_queue_init(vq);
2874}
2875
2876static int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2877 struct vb2_queue *dst_vq)
2878{
Javier Martin186b2502012-07-26 05:53:35 -03002879 int ret;
2880
Javier Martin186b2502012-07-26 05:53:35 -03002881 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002882 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002883 src_vq->mem_ops = &vb2_dma_contig_memops;
2884
Philipp Zabel121cacf2014-07-18 07:22:42 -03002885 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002886 if (ret)
2887 return ret;
2888
Javier Martin186b2502012-07-26 05:53:35 -03002889 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002890 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002891 dst_vq->mem_ops = &vb2_dma_contig_memops;
2892
Philipp Zabel121cacf2014-07-18 07:22:42 -03002893 return coda_queue_init(priv, dst_vq);
2894}
2895
2896static int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2897 struct vb2_queue *dst_vq)
2898{
2899 int ret;
2900
2901 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2902 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2903 src_vq->mem_ops = &vb2_dma_contig_memops;
2904
2905 ret = coda_queue_init(priv, src_vq);
2906 if (ret)
2907 return ret;
2908
2909 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2910 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2911 dst_vq->mem_ops = &vb2_dma_contig_memops;
2912
2913 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002914}
2915
Philipp Zabele11f3e62012-07-25 09:16:58 -03002916static int coda_next_free_instance(struct coda_dev *dev)
2917{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002918 int idx = ffz(dev->instance_mask);
2919
2920 if ((idx < 0) ||
2921 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2922 return -EBUSY;
2923
2924 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002925}
2926
Philipp Zabel121cacf2014-07-18 07:22:42 -03002927static int coda_open(struct file *file, enum coda_inst_type inst_type)
Javier Martin186b2502012-07-26 05:53:35 -03002928{
Philipp Zabel121cacf2014-07-18 07:22:42 -03002929 int (*queue_init)(void *priv, struct vb2_queue *src_vq,
2930 struct vb2_queue *dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002931 struct coda_dev *dev = video_drvdata(file);
2932 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002933 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002934 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002935 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002936
Javier Martin186b2502012-07-26 05:53:35 -03002937 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2938 if (!ctx)
2939 return -ENOMEM;
2940
Fabio Estevamf82bc202013-08-21 11:14:16 -03002941 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002942 if (idx < 0) {
2943 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002944 goto err_coda_max;
2945 }
2946 set_bit(idx, &dev->instance_mask);
2947
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002948 name = kasprintf(GFP_KERNEL, "context%d", idx);
2949 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2950 kfree(name);
2951
Philipp Zabel121cacf2014-07-18 07:22:42 -03002952 ctx->inst_type = inst_type;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002953 init_completion(&ctx->completion);
2954 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2955 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002956 v4l2_fh_init(&ctx->fh, video_devdata(file));
2957 file->private_data = &ctx->fh;
2958 v4l2_fh_add(&ctx->fh);
2959 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002960 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002961 switch (dev->devtype->product) {
2962 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002963 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002964 ctx->reg_idx = 0;
2965 break;
2966 default:
2967 ctx->reg_idx = idx;
2968 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002969
Philipp Zabel1e172732014-07-11 06:36:23 -03002970 /* Power up and upload firmware if necessary */
2971 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2972 if (ret < 0) {
2973 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2974 goto err_pm_get;
2975 }
2976
Fabio Estevam79830db2013-08-21 11:14:17 -03002977 ret = clk_prepare_enable(dev->clk_per);
2978 if (ret)
2979 goto err_clk_per;
2980
2981 ret = clk_prepare_enable(dev->clk_ahb);
2982 if (ret)
2983 goto err_clk_ahb;
2984
Javier Martin186b2502012-07-26 05:53:35 -03002985 set_default_params(ctx);
Philipp Zabel121cacf2014-07-18 07:22:42 -03002986 if (inst_type == CODA_INST_ENCODER)
2987 queue_init = coda_encoder_queue_init;
2988 else
2989 queue_init = coda_decoder_queue_init;
2990 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002991 if (IS_ERR(ctx->fh.m2m_ctx)) {
2992 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002993
2994 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2995 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002996 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002997 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002998
Javier Martin186b2502012-07-26 05:53:35 -03002999 ret = coda_ctrls_setup(ctx);
3000 if (ret) {
3001 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03003002 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03003003 }
3004
3005 ctx->fh.ctrl_handler = &ctx->ctrls;
3006
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003007 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
3008 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003009 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003010 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03003011 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03003012 }
3013
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003014 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
3015 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
3016 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
3017 if (!ctx->bitstream.vaddr) {
3018 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
3019 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03003020 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003021 }
3022 kfifo_init(&ctx->bitstream_fifo,
3023 ctx->bitstream.vaddr, ctx->bitstream.size);
3024 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003025 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03003026 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003027
Javier Martin186b2502012-07-26 05:53:35 -03003028 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003029 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003030 coda_unlock(ctx);
3031
Javier Martin186b2502012-07-26 05:53:35 -03003032 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
3033 ctx->idx, ctx);
3034
3035 return 0;
3036
Fabio Estevamf82bc202013-08-21 11:14:16 -03003037err_dma_writecombine:
3038 coda_free_context_buffers(ctx);
3039 if (ctx->dev->devtype->product == CODA_DX6)
3040 coda_free_aux_buf(dev, &ctx->workbuf);
3041 coda_free_aux_buf(dev, &ctx->parabuf);
3042err_dma_alloc:
3043 v4l2_ctrl_handler_free(&ctx->ctrls);
3044err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03003045 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003046err_ctx_init:
3047 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003048err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03003049 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03003050err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03003051 pm_runtime_put_sync(&dev->plat_dev->dev);
3052err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03003053 v4l2_fh_del(&ctx->fh);
3054 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003055 clear_bit(ctx->idx, &dev->instance_mask);
3056err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03003057 kfree(ctx);
3058 return ret;
3059}
3060
Philipp Zabel121cacf2014-07-18 07:22:42 -03003061static int coda_encoder_open(struct file *file)
3062{
3063 return coda_open(file, CODA_INST_ENCODER);
3064}
3065
3066static int coda_decoder_open(struct file *file)
3067{
3068 return coda_open(file, CODA_INST_DECODER);
3069}
3070
Javier Martin186b2502012-07-26 05:53:35 -03003071static int coda_release(struct file *file)
3072{
3073 struct coda_dev *dev = video_drvdata(file);
3074 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3075
3076 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3077 ctx);
3078
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003079 debugfs_remove_recursive(ctx->debugfs_entry);
3080
Philipp Zabel918c66f2013-06-27 06:59:01 -03003081 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003082 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003083
3084 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003085 if (ctx->initialized) {
3086 queue_work(dev->workqueue, &ctx->seq_end_work);
3087 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003088 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003089
3090 coda_free_framebuffers(ctx);
3091
Javier Martin186b2502012-07-26 05:53:35 -03003092 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003093 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003094 coda_unlock(ctx);
3095
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003096 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3097 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003098 coda_free_context_buffers(ctx);
3099 if (ctx->dev->devtype->product == CODA_DX6)
3100 coda_free_aux_buf(dev, &ctx->workbuf);
3101
3102 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003103 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003104 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003105 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003106 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003107 v4l2_fh_del(&ctx->fh);
3108 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003109 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003110 kfree(ctx);
3111
3112 return 0;
3113}
3114
Philipp Zabel121cacf2014-07-18 07:22:42 -03003115static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03003116 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03003117 .open = coda_encoder_open,
3118 .release = coda_release,
3119 .poll = v4l2_m2m_fop_poll,
3120 .unlocked_ioctl = video_ioctl2,
3121 .mmap = v4l2_m2m_fop_mmap,
3122};
3123
3124static const struct v4l2_file_operations coda_decoder_fops = {
3125 .owner = THIS_MODULE,
3126 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03003127 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003128 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003129 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003130 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003131};
3132
Philipp Zabel918c66f2013-06-27 06:59:01 -03003133static void coda_finish_decode(struct coda_ctx *ctx)
3134{
3135 struct coda_dev *dev = ctx->dev;
3136 struct coda_q_data *q_data_src;
3137 struct coda_q_data *q_data_dst;
3138 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003139 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003140 int width, height;
3141 int decoded_idx;
3142 int display_idx;
3143 u32 src_fourcc;
3144 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003145 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003146 u32 val;
3147
Philipp Zabel14604e32014-07-11 06:36:22 -03003148 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003149
3150 /* Update kfifo out pointer from coda bitstream read pointer */
3151 coda_kfifo_sync_from_device(ctx);
3152
3153 /*
3154 * in stream-end mode, the read pointer can overshoot the write pointer
3155 * by up to 512 bytes
3156 */
3157 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
Michael Olbrich390503b2014-07-18 07:22:39 -03003158 if (coda_get_bitstream_payload(ctx) >= CODA_MAX_FRAME_SIZE - 512)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003159 kfifo_init(&ctx->bitstream_fifo,
3160 ctx->bitstream.vaddr, ctx->bitstream.size);
3161 }
3162
3163 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3164 src_fourcc = q_data_src->fourcc;
3165
3166 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3167 if (val != 1)
3168 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3169
3170 success = val & 0x1;
3171 if (!success)
3172 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3173
3174 if (src_fourcc == V4L2_PIX_FMT_H264) {
3175 if (val & (1 << 3))
3176 v4l2_err(&dev->v4l2_dev,
3177 "insufficient PS buffer space (%d bytes)\n",
3178 ctx->psbuf.size);
3179 if (val & (1 << 2))
3180 v4l2_err(&dev->v4l2_dev,
3181 "insufficient slice buffer space (%d bytes)\n",
3182 ctx->slicebuf.size);
3183 }
3184
3185 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3186 width = (val >> 16) & 0xffff;
3187 height = val & 0xffff;
3188
3189 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3190
Philipp Zabel52c41672014-07-11 06:36:19 -03003191 /* frame crop information */
3192 if (src_fourcc == V4L2_PIX_FMT_H264) {
3193 u32 left_right;
3194 u32 top_bottom;
3195
3196 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3197 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3198
3199 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3200 /* Keep current crop information */
3201 } else {
3202 struct v4l2_rect *rect = &q_data_dst->rect;
3203
3204 rect->left = left_right >> 16 & 0xffff;
3205 rect->top = top_bottom >> 16 & 0xffff;
3206 rect->width = width - rect->left -
3207 (left_right & 0xffff);
3208 rect->height = height - rect->top -
3209 (top_bottom & 0xffff);
3210 }
3211 } else {
3212 /* no cropping */
3213 }
3214
Philipp Zabel410e5e42014-07-11 06:36:32 -03003215 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3216 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003217 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003218 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003219
3220 if (dev->devtype->product == CODA_7541) {
3221 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3222 if (val == 0) {
3223 /* not enough bitstream data */
3224 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3225 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003226 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003227 return;
3228 }
3229 }
3230
3231 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3232
3233 /*
3234 * The previous display frame was copied out by the rotator,
3235 * now it can be overwritten again
3236 */
3237 if (ctx->display_idx >= 0 &&
3238 ctx->display_idx < ctx->num_internal_frames) {
3239 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3240 coda_write(dev, ctx->frm_dis_flg,
3241 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3242 }
3243
3244 /*
3245 * The index of the last decoded frame, not necessarily in
3246 * display order, and the index of the next display frame.
3247 * The latter could have been decoded in a previous run.
3248 */
3249 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3250 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3251
3252 if (decoded_idx == -1) {
3253 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003254 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3255 ctx->sequence_offset++;
3256 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003257 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003258 } else if (decoded_idx == -2) {
3259 /* no frame was decoded, we still return the remaining buffers */
3260 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3261 v4l2_err(&dev->v4l2_dev,
3262 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003263 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003264 ts = list_first_entry(&ctx->timestamp_list,
3265 struct coda_timestamp, list);
3266 list_del(&ts->list);
3267 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003268 val -= ctx->sequence_offset;
3269 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003270 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003271 "sequence number mismatch (%d(%d) != %d)\n",
3272 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003273 }
3274 ctx->frame_timestamps[decoded_idx] = *ts;
3275 kfree(ts);
3276
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003277 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3278 if (val == 0)
3279 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3280 else if (val == 1)
3281 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3282 else
3283 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003284
3285 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003286 }
3287
3288 if (display_idx == -1) {
3289 /*
3290 * no more frames to be decoded, but there could still
3291 * be rotator output to dequeue
3292 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003293 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003294 } else if (display_idx == -3) {
3295 /* possibly prescan failure */
3296 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3297 v4l2_err(&dev->v4l2_dev,
3298 "presentation frame index out of range: %d\n",
3299 display_idx);
3300 }
3301
3302 /* If a frame was copied out, return it */
3303 if (ctx->display_idx >= 0 &&
3304 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003305 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003306 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3307
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003308 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3309 V4L2_BUF_FLAG_PFRAME |
3310 V4L2_BUF_FLAG_BFRAME);
3311 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003312 ts = &ctx->frame_timestamps[ctx->display_idx];
3313 dst_buf->v4l2_buf.timecode = ts->timecode;
3314 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003315
Philipp Zabel918c66f2013-06-27 06:59:01 -03003316 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3317
Philipp Zabel410e5e42014-07-11 06:36:32 -03003318 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3319 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003320
3321 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3322 "job finished: decoding frame (%d) (%s)\n",
3323 dst_buf->v4l2_buf.sequence,
3324 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3325 "KEYFRAME" : "PFRAME");
3326 } else {
3327 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3328 "job finished: no frame decoded\n");
3329 }
3330
3331 /* The rotator will copy the current display frame next time */
3332 ctx->display_idx = display_idx;
3333}
3334
3335static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003336{
Philipp Zabelec25f682012-07-20 08:54:29 -03003337 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003338 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003339 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003340
Philipp Zabel14604e32014-07-11 06:36:22 -03003341 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3342 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003343
3344 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003345 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003346 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3347
Javier Martin186b2502012-07-26 05:53:35 -03003348 /* Calculate bytesused field */
3349 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003350 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3351 ctx->vpu_header_size[0] +
3352 ctx->vpu_header_size[1] +
3353 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003354 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003355 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003356 }
3357
3358 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3359 wr_ptr - start_ptr);
3360
3361 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3362 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3363
Philipp Zabel51660282013-09-30 10:34:49 -03003364 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003365 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3366 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3367 } else {
3368 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3369 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3370 }
3371
Kamil Debskiccd571c2013-04-24 10:38:24 -03003372 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003373 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3374 dst_buf->v4l2_buf.flags |=
3375 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003376 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3377
Philipp Zabelec25f682012-07-20 08:54:29 -03003378 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003379
Philipp Zabel14604e32014-07-11 06:36:22 -03003380 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003381 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3382
3383 ctx->gopcounter--;
3384 if (ctx->gopcounter < 0)
3385 ctx->gopcounter = ctx->params.gop_size - 1;
3386
3387 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3388 "job finished: encoding frame (%d) (%s)\n",
3389 dst_buf->v4l2_buf.sequence,
3390 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3391 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003392}
3393
3394static irqreturn_t coda_irq_handler(int irq, void *data)
3395{
3396 struct coda_dev *dev = data;
3397 struct coda_ctx *ctx;
3398
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003399 /* read status register to attend the IRQ */
3400 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3401 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3402 CODA_REG_BIT_INT_CLEAR);
3403
3404 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3405 if (ctx == NULL) {
3406 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3407 mutex_unlock(&dev->coda_mutex);
3408 return IRQ_HANDLED;
3409 }
3410
3411 if (ctx->aborting) {
3412 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3413 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003414 }
3415
3416 if (coda_isbusy(ctx->dev)) {
3417 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3418 "coda is still busy!!!!\n");
3419 return IRQ_NONE;
3420 }
3421
Philipp Zabel32b6f202014-07-11 06:36:20 -03003422 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003423
3424 return IRQ_HANDLED;
3425}
3426
3427static u32 coda_supported_firmwares[] = {
3428 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003429 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003430 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003431};
3432
3433static bool coda_firmware_supported(u32 vernum)
3434{
3435 int i;
3436
3437 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3438 if (vernum == coda_supported_firmwares[i])
3439 return true;
3440 return false;
3441}
3442
Philipp Zabel87048bb2012-07-02 07:03:43 -03003443static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003444{
Javier Martin186b2502012-07-26 05:53:35 -03003445 u32 data;
3446 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003447 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003448
Fabio Estevam79830db2013-08-21 11:14:17 -03003449 ret = clk_prepare_enable(dev->clk_per);
3450 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003451 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003452
3453 ret = clk_prepare_enable(dev->clk_ahb);
3454 if (ret)
3455 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003456
Philipp Zabel8f452842014-07-11 06:36:35 -03003457 if (dev->rstc)
3458 reset_control_reset(dev->rstc);
3459
Javier Martin186b2502012-07-26 05:53:35 -03003460 /*
3461 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003462 * The 16-bit chars in the code buffer are in memory access
3463 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003464 * Data in this SRAM survives a reboot.
3465 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003466 p = (u16 *)dev->codebuf.vaddr;
3467 if (dev->devtype->product == CODA_DX6) {
3468 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3469 data = CODA_DOWN_ADDRESS_SET(i) |
3470 CODA_DOWN_DATA_SET(p[i ^ 1]);
3471 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3472 }
3473 } else {
3474 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3475 data = CODA_DOWN_ADDRESS_SET(i) |
3476 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3477 3 - (i % 4)]);
3478 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3479 }
Javier Martin186b2502012-07-26 05:53:35 -03003480 }
Javier Martin186b2502012-07-26 05:53:35 -03003481
Philipp Zabel9acf7692013-05-23 10:42:56 -03003482 /* Clear registers */
3483 for (i = 0; i < 64; i++)
3484 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3485
Javier Martin186b2502012-07-26 05:53:35 -03003486 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003487 if (dev->devtype->product == CODA_960 ||
3488 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003489 coda_write(dev, dev->tempbuf.paddr,
3490 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003491 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003492 } else {
3493 coda_write(dev, dev->workbuf.paddr,
3494 CODA_REG_BIT_WORK_BUF_ADDR);
3495 }
Javier Martin186b2502012-07-26 05:53:35 -03003496 coda_write(dev, dev->codebuf.paddr,
3497 CODA_REG_BIT_CODE_BUF_ADDR);
3498 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3499
3500 /* Set default values */
3501 switch (dev->devtype->product) {
3502 case CODA_DX6:
3503 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3504 break;
3505 default:
3506 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3507 }
Philipp Zabel89548442014-07-11 06:36:17 -03003508 if (dev->devtype->product == CODA_960)
3509 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3510 else
3511 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003512
3513 if (dev->devtype->product != CODA_DX6)
3514 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3515
Javier Martin186b2502012-07-26 05:53:35 -03003516 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3517 CODA_REG_BIT_INT_ENABLE);
3518
3519 /* Reset VPU and start processor */
3520 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3521 data |= CODA_REG_RESET_ENABLE;
3522 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3523 udelay(10);
3524 data &= ~CODA_REG_RESET_ENABLE;
3525 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3526 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3527
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003528 clk_disable_unprepare(dev->clk_ahb);
3529 clk_disable_unprepare(dev->clk_per);
3530
3531 return 0;
3532
3533err_clk_ahb:
3534 clk_disable_unprepare(dev->clk_per);
3535err_clk_per:
3536 return ret;
3537}
3538
3539static int coda_check_firmware(struct coda_dev *dev)
3540{
3541 u16 product, major, minor, release;
3542 u32 data;
3543 int ret;
3544
3545 ret = clk_prepare_enable(dev->clk_per);
3546 if (ret)
3547 goto err_clk_per;
3548
3549 ret = clk_prepare_enable(dev->clk_ahb);
3550 if (ret)
3551 goto err_clk_ahb;
3552
Javier Martin186b2502012-07-26 05:53:35 -03003553 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3554 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3555 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3556 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3557 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3558 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003559 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003560 ret = -EIO;
3561 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003562 }
3563
Philipp Zabel89548442014-07-11 06:36:17 -03003564 if (dev->devtype->product == CODA_960) {
3565 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3566 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3567 data);
3568 }
3569
Javier Martin186b2502012-07-26 05:53:35 -03003570 /* Check we are compatible with the loaded firmware */
3571 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3572 product = CODA_FIRMWARE_PRODUCT(data);
3573 major = CODA_FIRMWARE_MAJOR(data);
3574 minor = CODA_FIRMWARE_MINOR(data);
3575 release = CODA_FIRMWARE_RELEASE(data);
3576
3577 clk_disable_unprepare(dev->clk_per);
3578 clk_disable_unprepare(dev->clk_ahb);
3579
3580 if (product != dev->devtype->product) {
3581 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3582 " Version: %u.%u.%u\n",
3583 coda_product_name(dev->devtype->product),
3584 coda_product_name(product), major, minor, release);
3585 return -EINVAL;
3586 }
3587
3588 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3589 coda_product_name(product));
3590
3591 if (coda_firmware_supported(data)) {
3592 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3593 major, minor, release);
3594 } else {
3595 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3596 "%u.%u.%u\n", major, minor, release);
3597 }
3598
3599 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003600
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003601err_run_cmd:
3602 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003603err_clk_ahb:
3604 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003605err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003606 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003607}
3608
Philipp Zabel121cacf2014-07-18 07:22:42 -03003609static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
3610{
3611 vfd->release = video_device_release_empty,
3612 vfd->lock = &dev->dev_mutex;
3613 vfd->v4l2_dev = &dev->v4l2_dev;
3614 vfd->vfl_dir = VFL_DIR_M2M;
3615 video_set_drvdata(vfd, dev);
3616
3617 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
3618}
3619
Javier Martin186b2502012-07-26 05:53:35 -03003620static void coda_fw_callback(const struct firmware *fw, void *context)
3621{
3622 struct coda_dev *dev = context;
3623 struct platform_device *pdev = dev->plat_dev;
3624 int ret;
3625
3626 if (!fw) {
3627 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3628 return;
3629 }
3630
3631 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003632 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3633 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003634 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003635 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3636 return;
3637 }
3638
Philipp Zabel87048bb2012-07-02 07:03:43 -03003639 /* Copy the whole firmware image to the code buffer */
3640 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3641 release_firmware(fw);
3642
Philipp Zabel1e172732014-07-11 06:36:23 -03003643 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3644 /*
3645 * Enabling power temporarily will cause coda_hw_init to be
3646 * called via coda_runtime_resume by the pm domain.
3647 */
3648 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3649 if (ret < 0) {
3650 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3651 ret);
3652 return;
3653 }
3654
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003655 ret = coda_check_firmware(dev);
3656 if (ret < 0)
3657 return;
3658
Philipp Zabel1e172732014-07-11 06:36:23 -03003659 pm_runtime_put_sync(&dev->plat_dev->dev);
3660 } else {
3661 /*
3662 * If runtime pm is disabled or pm_domain is not set,
3663 * initialize once manually.
3664 */
3665 ret = coda_hw_init(dev);
3666 if (ret < 0) {
3667 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3668 return;
3669 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003670
3671 ret = coda_check_firmware(dev);
3672 if (ret < 0)
3673 return;
Javier Martin186b2502012-07-26 05:53:35 -03003674 }
3675
Javier Martin186b2502012-07-26 05:53:35 -03003676 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3677 if (IS_ERR(dev->alloc_ctx)) {
3678 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3679 return;
3680 }
3681
3682 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3683 if (IS_ERR(dev->m2m_dev)) {
3684 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3685 goto rel_ctx;
3686 }
3687
Philipp Zabel121cacf2014-07-18 07:22:42 -03003688 dev->vfd[0].fops = &coda_encoder_fops,
3689 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
3690 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
3691 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03003692 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03003693 v4l2_err(&dev->v4l2_dev,
3694 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03003695 goto rel_m2m;
3696 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03003697
3698 dev->vfd[1].fops = &coda_decoder_fops,
3699 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
3700 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
3701 ret = coda_register_device(dev, &dev->vfd[1]);
3702 if (ret) {
3703 v4l2_err(&dev->v4l2_dev,
3704 "Failed to register decoder video device\n");
3705 goto rel_m2m;
3706 }
3707
3708 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
3709 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03003710
3711 return;
3712
3713rel_m2m:
3714 v4l2_m2m_release(dev->m2m_dev);
3715rel_ctx:
3716 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3717}
3718
3719static int coda_firmware_request(struct coda_dev *dev)
3720{
3721 char *fw = dev->devtype->firmware;
3722
3723 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3724 coda_product_name(dev->devtype->product));
3725
3726 return request_firmware_nowait(THIS_MODULE, true,
3727 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3728}
3729
3730enum coda_platform {
3731 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003732 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003733 CODA_IMX6Q,
3734 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003735};
3736
Emil Goodec06d8752012-08-14 17:44:42 -03003737static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003738 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003739 .firmware = "v4l-codadx6-imx27.bin",
3740 .product = CODA_DX6,
3741 .codecs = codadx6_codecs,
3742 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3743 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003744 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03003745 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003746 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003747 .firmware = "v4l-coda7541-imx53.bin",
3748 .product = CODA_7541,
3749 .codecs = coda7_codecs,
3750 .num_codecs = ARRAY_SIZE(coda7_codecs),
3751 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003752 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003753 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003754 },
Philipp Zabel89548442014-07-11 06:36:17 -03003755 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003756 .firmware = "v4l-coda960-imx6q.bin",
3757 .product = CODA_960,
3758 .codecs = coda9_codecs,
3759 .num_codecs = ARRAY_SIZE(coda9_codecs),
3760 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003761 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003762 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03003763 },
3764 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003765 .firmware = "v4l-coda960-imx6dl.bin",
3766 .product = CODA_960,
3767 .codecs = coda9_codecs,
3768 .num_codecs = ARRAY_SIZE(coda9_codecs),
3769 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003770 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003771 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03003772 },
Javier Martin186b2502012-07-26 05:53:35 -03003773};
3774
3775static struct platform_device_id coda_platform_ids[] = {
3776 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003777 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003778 { /* sentinel */ }
3779};
3780MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3781
3782#ifdef CONFIG_OF
3783static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003784 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003785 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003786 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3787 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003788 { /* sentinel */ }
3789};
3790MODULE_DEVICE_TABLE(of, coda_dt_ids);
3791#endif
3792
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003793static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003794{
3795 const struct of_device_id *of_id =
3796 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3797 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003798 struct coda_platform_data *pdata = pdev->dev.platform_data;
3799 struct device_node *np = pdev->dev.of_node;
3800 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003801 struct coda_dev *dev;
3802 struct resource *res;
3803 int ret, irq;
3804
3805 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3806 if (!dev) {
3807 dev_err(&pdev->dev, "Not enough memory for %s\n",
3808 CODA_NAME);
3809 return -ENOMEM;
3810 }
3811
3812 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003813 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003814
3815 dev->plat_dev = pdev;
3816 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3817 if (IS_ERR(dev->clk_per)) {
3818 dev_err(&pdev->dev, "Could not get per clock\n");
3819 return PTR_ERR(dev->clk_per);
3820 }
3821
3822 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3823 if (IS_ERR(dev->clk_ahb)) {
3824 dev_err(&pdev->dev, "Could not get ahb clock\n");
3825 return PTR_ERR(dev->clk_ahb);
3826 }
3827
3828 /* Get memory for physical registers */
3829 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003830 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3831 if (IS_ERR(dev->regs_base))
3832 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003833
3834 /* IRQ */
3835 irq = platform_get_irq(pdev, 0);
3836 if (irq < 0) {
3837 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03003838 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03003839 }
3840
Fabio Estevam08c8d142014-06-04 15:46:24 -03003841 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
3842 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
3843 if (ret < 0) {
3844 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3845 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003846 }
3847
Philipp Zabelee27aa92014-07-24 16:50:03 -03003848 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03003849 if (IS_ERR(dev->rstc)) {
3850 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03003851 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03003852 dev->rstc = NULL;
3853 } else {
3854 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3855 return ret;
3856 }
3857 }
3858
Philipp Zabel657eee72013-04-29 16:17:14 -07003859 /* Get IRAM pool from device tree or platform data */
3860 pool = of_get_named_gen_pool(np, "iram", 0);
3861 if (!pool && pdata)
3862 pool = dev_get_gen_pool(pdata->iram_dev);
3863 if (!pool) {
3864 dev_err(&pdev->dev, "iram pool not available\n");
3865 return -ENOMEM;
3866 }
3867 dev->iram_pool = pool;
3868
Javier Martin186b2502012-07-26 05:53:35 -03003869 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3870 if (ret)
3871 return ret;
3872
3873 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003874 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003875
3876 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3877
3878 if (of_id) {
3879 dev->devtype = of_id->data;
3880 } else if (pdev_id) {
3881 dev->devtype = &coda_devdata[pdev_id->driver_data];
3882 } else {
3883 v4l2_device_unregister(&dev->v4l2_dev);
3884 return -EINVAL;
3885 }
3886
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003887 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3888 if (!dev->debugfs_root)
3889 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3890
Javier Martin186b2502012-07-26 05:53:35 -03003891 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003892 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003893 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003894 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003895 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003896 if (ret < 0) {
3897 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3898 v4l2_device_unregister(&dev->v4l2_dev);
3899 return ret;
3900 }
Javier Martin186b2502012-07-26 05:53:35 -03003901 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003902
3903 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003904 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003905 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003906 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003907 if (ret < 0) {
3908 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3909 v4l2_device_unregister(&dev->v4l2_dev);
3910 return ret;
3911 }
Javier Martin186b2502012-07-26 05:53:35 -03003912 }
3913
Philipp Zabel401e9722014-07-11 06:36:43 -03003914 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003915 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3916 &dev->iram.paddr);
3917 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003918 dev_err(&pdev->dev, "unable to alloc iram\n");
3919 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003920 }
3921
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003922 dev->iram.blob.data = dev->iram.vaddr;
3923 dev->iram.blob.size = dev->iram.size;
3924 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
3925 &dev->iram.blob);
3926
Philipp Zabel32b6f202014-07-11 06:36:20 -03003927 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3928 if (!dev->workqueue) {
3929 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3930 return -ENOMEM;
3931 }
3932
Javier Martin186b2502012-07-26 05:53:35 -03003933 platform_set_drvdata(pdev, dev);
3934
Philipp Zabel1e172732014-07-11 06:36:23 -03003935 pm_runtime_enable(&pdev->dev);
3936
Javier Martin186b2502012-07-26 05:53:35 -03003937 return coda_firmware_request(dev);
3938}
3939
3940static int coda_remove(struct platform_device *pdev)
3941{
3942 struct coda_dev *dev = platform_get_drvdata(pdev);
3943
Philipp Zabel121cacf2014-07-18 07:22:42 -03003944 video_unregister_device(&dev->vfd[0]);
3945 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03003946 if (dev->m2m_dev)
3947 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003948 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003949 if (dev->alloc_ctx)
3950 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3951 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003952 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003953 if (dev->iram.vaddr)
3954 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3955 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003956 coda_free_aux_buf(dev, &dev->codebuf);
3957 coda_free_aux_buf(dev, &dev->tempbuf);
3958 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003959 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03003960 return 0;
3961}
3962
Philipp Zabel1e172732014-07-11 06:36:23 -03003963#ifdef CONFIG_PM_RUNTIME
3964static int coda_runtime_resume(struct device *dev)
3965{
3966 struct coda_dev *cdev = dev_get_drvdata(dev);
3967 int ret = 0;
3968
Philipp Zabel65919e62014-07-18 07:22:36 -03003969 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03003970 ret = coda_hw_init(cdev);
3971 if (ret)
3972 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3973 }
3974
3975 return ret;
3976}
3977#endif
3978
3979static const struct dev_pm_ops coda_pm_ops = {
3980 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3981};
3982
Javier Martin186b2502012-07-26 05:53:35 -03003983static struct platform_driver coda_driver = {
3984 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003985 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003986 .driver = {
3987 .name = CODA_NAME,
3988 .owner = THIS_MODULE,
3989 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003990 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003991 },
3992 .id_table = coda_platform_ids,
3993};
3994
3995module_platform_driver(coda_driver);
3996
3997MODULE_LICENSE("GPL");
3998MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3999MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");