blob: 546cc7bd6a71d231163825c2e7cdef359f63a17a [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
Philipp Zabelf61c0b12014-07-11 06:36:40 -030015#include <linux/debugfs.h>
Javier Martin186b2502012-07-26 05:53:35 -030016#include <linux/delay.h>
17#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070018#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030022#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030023#include <linux/module.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030026#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030027#include <linux/slab.h>
28#include <linux/videodev2.h>
29#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070030#include <linux/platform_data/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030031#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030032
33#include <media/v4l2-ctrls.h>
34#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030035#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030036#include <media/v4l2-ioctl.h>
37#include <media/v4l2-mem2mem.h>
38#include <media/videobuf2-core.h>
39#include <media/videobuf2-dma-contig.h>
40
41#include "coda.h"
42
43#define CODA_NAME "coda"
44
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030045#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030046
Javier Martin186b2502012-07-26 05:53:35 -030047#define CODA_PARA_BUF_SIZE (10 * 1024)
48#define CODA_ISRAM_SIZE (2048 * 2)
49
Philipp Zabel918c66f2013-06-27 06:59:01 -030050#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030051#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030052
53#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030054
Philipp Zabelb96904e2013-05-23 10:42:58 -030055#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030056#define FMO_SLICE_SAVE_BUF_SIZE (32)
57#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030058#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030059
60#define MIN_W 176
61#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030062
63#define S_ALIGN 1 /* multiple of 2 */
64#define W_ALIGN 1 /* multiple of 2 */
65#define H_ALIGN 1 /* multiple of 2 */
66
67#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
68
69static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030070module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030071MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
72
73enum {
74 V4L2_M2M_SRC = 0,
75 V4L2_M2M_DST = 1,
76};
77
Javier Martin186b2502012-07-26 05:53:35 -030078enum coda_inst_type {
79 CODA_INST_ENCODER,
80 CODA_INST_DECODER,
81};
82
83enum coda_product {
84 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030085 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030086 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030087};
88
89struct coda_fmt {
90 char *name;
91 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030092};
93
94struct coda_codec {
95 u32 mode;
96 u32 src_fourcc;
97 u32 dst_fourcc;
98 u32 max_w;
99 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300100};
101
102struct coda_devtype {
103 char *firmware;
104 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300105 struct coda_codec *codecs;
106 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300107 size_t workbuf_size;
Philipp Zabel5d73fad2014-07-11 06:36:42 -0300108 size_t tempbuf_size;
Philipp Zabel401e9722014-07-11 06:36:43 -0300109 size_t iram_size;
Javier Martin186b2502012-07-26 05:53:35 -0300110};
111
112/* Per-queue, driver-specific private data */
113struct coda_q_data {
114 unsigned int width;
115 unsigned int height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300116 unsigned int bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300117 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300118 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300119 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300120};
121
122struct coda_aux_buf {
123 void *vaddr;
124 dma_addr_t paddr;
125 u32 size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300126 struct debugfs_blob_wrapper blob;
127 struct dentry *dentry;
Javier Martin186b2502012-07-26 05:53:35 -0300128};
129
130struct coda_dev {
131 struct v4l2_device v4l2_dev;
Philipp Zabel121cacf2014-07-18 07:22:42 -0300132 struct video_device vfd[2];
Javier Martin186b2502012-07-26 05:53:35 -0300133 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300134 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300135
136 void __iomem *regs_base;
137 struct clk *clk_per;
138 struct clk *clk_ahb;
Philipp Zabel8f452842014-07-11 06:36:35 -0300139 struct reset_control *rstc;
Javier Martin186b2502012-07-26 05:53:35 -0300140
141 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300142 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300143 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700144 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300145 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300146
147 spinlock_t irqlock;
148 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300149 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300150 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300151 struct v4l2_m2m_dev *m2m_dev;
152 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300153 struct list_head instances;
154 unsigned long instance_mask;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300155 struct dentry *debugfs_root;
Javier Martin186b2502012-07-26 05:53:35 -0300156};
157
158struct coda_params {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -0300159 u8 rot_mode;
Javier Martin186b2502012-07-26 05:53:35 -0300160 u8 h264_intra_qp;
161 u8 h264_inter_qp;
Philipp Zabel1a5567e2014-07-11 06:36:26 -0300162 u8 h264_min_qp;
163 u8 h264_max_qp;
Philipp Zabelde23b1d2014-07-11 06:36:27 -0300164 u8 h264_deblk_enabled;
165 u8 h264_deblk_alpha;
166 u8 h264_deblk_beta;
Javier Martin186b2502012-07-26 05:53:35 -0300167 u8 mpeg4_intra_qp;
168 u8 mpeg4_inter_qp;
169 u8 gop_size;
Philipp Zabelf38f79d2014-07-11 06:36:28 -0300170 int intra_refresh;
Javier Martin186b2502012-07-26 05:53:35 -0300171 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300172 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300173 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
174 u32 framerate;
175 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300176 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300177 u32 slice_max_mb;
178};
179
Philipp Zabelc2d22512013-06-21 03:55:28 -0300180struct coda_iram_info {
181 u32 axi_sram_use;
182 phys_addr_t buf_bit_use;
183 phys_addr_t buf_ip_ac_dc_use;
184 phys_addr_t buf_dbk_y_use;
185 phys_addr_t buf_dbk_c_use;
186 phys_addr_t buf_ovl_use;
187 phys_addr_t buf_btp_use;
188 phys_addr_t search_ram_paddr;
189 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300190 int remaining;
191 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300192};
193
Philipp Zabel89548442014-07-11 06:36:17 -0300194struct gdi_tiled_map {
195 int xy2ca_map[16];
196 int xy2ba_map[16];
197 int xy2ra_map[16];
198 int rbc2axi_map[32];
199 int xy2rbc_config;
200 int map_type;
201#define GDI_LINEAR_FRAME_MAP 0
202};
203
Philipp Zabel846ced92014-07-11 06:36:31 -0300204struct coda_timestamp {
205 struct list_head list;
206 u32 sequence;
207 struct v4l2_timecode timecode;
208 struct timeval timestamp;
209};
210
Javier Martin186b2502012-07-26 05:53:35 -0300211struct coda_ctx {
212 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300213 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300214 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300215 struct work_struct pic_run_work;
216 struct work_struct seq_end_work;
217 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300218 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300219 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300220 int streamon_out;
221 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300222 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300223 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300224 u32 osequence;
Philipp Zabelcb2c0282014-07-11 06:36:33 -0300225 u32 sequence_offset;
Javier Martin186b2502012-07-26 05:53:35 -0300226 struct coda_q_data q_data[2];
227 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300228 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300229 enum v4l2_colorspace colorspace;
230 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300231 struct v4l2_ctrl_handler ctrls;
232 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300233 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300234 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300235 char vpu_header[3][64];
236 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300237 struct kfifo bitstream_fifo;
238 struct mutex bitstream_mutex;
239 struct coda_aux_buf bitstream;
Philipp Zabel84e23652014-07-11 06:36:34 -0300240 bool hold;
Javier Martin186b2502012-07-26 05:53:35 -0300241 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300242 struct coda_aux_buf psbuf;
243 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300244 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300245 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300246 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel410e5e42014-07-11 06:36:32 -0300247 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel846ced92014-07-11 06:36:31 -0300248 struct list_head timestamp_list;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300249 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300250 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300251 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300252 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300253 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300254 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300255 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300256 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300257 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300258 int display_idx;
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300259 struct dentry *debugfs_entry;
Javier Martin186b2502012-07-26 05:53:35 -0300260};
261
Javier Martin832fbb52012-10-29 08:34:59 -0300262static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
263 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
264static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300265
Javier Martin186b2502012-07-26 05:53:35 -0300266static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
267{
268 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
269 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
270 writel(data, dev->regs_base + reg);
271}
272
273static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
274{
275 u32 data;
276 data = readl(dev->regs_base + reg);
277 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
278 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
279 return data;
280}
281
282static inline unsigned long coda_isbusy(struct coda_dev *dev)
283{
284 return coda_read(dev, CODA_REG_BIT_BUSY);
285}
286
287static inline int coda_is_initialized(struct coda_dev *dev)
288{
289 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
290}
291
292static int coda_wait_timeout(struct coda_dev *dev)
293{
294 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
295
296 while (coda_isbusy(dev)) {
297 if (time_after(jiffies, timeout))
298 return -ETIMEDOUT;
299 }
300 return 0;
301}
302
303static void coda_command_async(struct coda_ctx *ctx, int cmd)
304{
305 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300306
Philipp Zabel89548442014-07-11 06:36:17 -0300307 if (dev->devtype->product == CODA_960 ||
308 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300309 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300310 coda_write(dev, ctx->bit_stream_param,
311 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300312 coda_write(dev, ctx->frm_dis_flg,
313 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300314 coda_write(dev, ctx->frame_mem_ctrl,
315 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300316 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
317 }
318
Philipp Zabel89548442014-07-11 06:36:17 -0300319 if (dev->devtype->product == CODA_960) {
320 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
321 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
322 }
323
Javier Martin186b2502012-07-26 05:53:35 -0300324 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
325
326 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
327 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300328 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
329
Javier Martin186b2502012-07-26 05:53:35 -0300330 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
331}
332
333static int coda_command_sync(struct coda_ctx *ctx, int cmd)
334{
335 struct coda_dev *dev = ctx->dev;
336
337 coda_command_async(ctx, cmd);
338 return coda_wait_timeout(dev);
339}
340
Philipp Zabel8f452842014-07-11 06:36:35 -0300341static int coda_hw_reset(struct coda_ctx *ctx)
342{
343 struct coda_dev *dev = ctx->dev;
344 unsigned long timeout;
345 unsigned int idx;
346 int ret;
347
348 if (!dev->rstc)
349 return -ENOENT;
350
351 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
352
Philipp Zabelae5abd22014-07-18 07:22:35 -0300353 if (dev->devtype->product == CODA_960) {
354 timeout = jiffies + msecs_to_jiffies(100);
355 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
356 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
357 if (time_after(jiffies, timeout))
358 return -ETIME;
359 cpu_relax();
360 }
Philipp Zabel8f452842014-07-11 06:36:35 -0300361 }
362
363 ret = reset_control_reset(dev->rstc);
364 if (ret < 0)
365 return ret;
366
Philipp Zabelae5abd22014-07-18 07:22:35 -0300367 if (dev->devtype->product == CODA_960)
368 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
Philipp Zabel8f452842014-07-11 06:36:35 -0300369 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
370 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
371 ret = coda_wait_timeout(dev);
372 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
373
374 return ret;
375}
376
Javier Martin186b2502012-07-26 05:53:35 -0300377static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
378 enum v4l2_buf_type type)
379{
380 switch (type) {
381 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
382 return &(ctx->q_data[V4L2_M2M_SRC]);
383 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
384 return &(ctx->q_data[V4L2_M2M_DST]);
385 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300386 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300387 }
Javier Martin186b2502012-07-26 05:53:35 -0300388}
389
390/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300391 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300392 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300393static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300394 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300395 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300396 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300397 },
398 {
399 .name = "YUV 4:2:0 Planar, YCrCb",
400 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300401 },
402 {
403 .name = "H264 Encoded Stream",
404 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300405 },
406 {
407 .name = "MPEG4 Encoded Stream",
408 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300409 },
410};
411
Philipp Zabelb96904e2013-05-23 10:42:58 -0300412#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
413 { mode, src_fourcc, dst_fourcc, max_w, max_h }
414
415/*
416 * Arrays of codecs supported by each given version of Coda:
417 * i.MX27 -> codadx6
418 * i.MX5x -> coda7
419 * i.MX6 -> coda960
420 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
421 */
422static struct coda_codec codadx6_codecs[] = {
423 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
424 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300425};
426
Philipp Zabelb96904e2013-05-23 10:42:58 -0300427static struct coda_codec coda7_codecs[] = {
428 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
429 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300430 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
431 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300432};
433
Philipp Zabel89548442014-07-11 06:36:17 -0300434static struct coda_codec coda9_codecs[] = {
435 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
436 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
437 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
438 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
439};
440
Philipp Zabelb96904e2013-05-23 10:42:58 -0300441static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300442{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300443 switch (fourcc) {
444 case V4L2_PIX_FMT_YUV420:
445 case V4L2_PIX_FMT_YVU420:
446 return true;
447 default:
448 return false;
449 }
450}
Javier Martin186b2502012-07-26 05:53:35 -0300451
Philipp Zabelb96904e2013-05-23 10:42:58 -0300452/*
453 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
454 * tables.
455 */
456static u32 coda_format_normalize_yuv(u32 fourcc)
457{
458 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
459}
460
461static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
462 int dst_fourcc)
463{
464 struct coda_codec *codecs = dev->devtype->codecs;
465 int num_codecs = dev->devtype->num_codecs;
466 int k;
467
468 src_fourcc = coda_format_normalize_yuv(src_fourcc);
469 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
470 if (src_fourcc == dst_fourcc)
471 return NULL;
472
473 for (k = 0; k < num_codecs; k++) {
474 if (codecs[k].src_fourcc == src_fourcc &&
475 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300476 break;
477 }
478
Philipp Zabelb96904e2013-05-23 10:42:58 -0300479 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300480 return NULL;
481
Philipp Zabelb96904e2013-05-23 10:42:58 -0300482 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300483}
484
Philipp Zabel57625592013-09-30 10:34:51 -0300485static void coda_get_max_dimensions(struct coda_dev *dev,
486 struct coda_codec *codec,
487 int *max_w, int *max_h)
488{
489 struct coda_codec *codecs = dev->devtype->codecs;
490 int num_codecs = dev->devtype->num_codecs;
491 unsigned int w, h;
492 int k;
493
494 if (codec) {
495 w = codec->max_w;
496 h = codec->max_h;
497 } else {
498 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
499 w = max(w, codecs[k].max_w);
500 h = max(h, codecs[k].max_h);
501 }
502 }
503
504 if (max_w)
505 *max_w = w;
506 if (max_h)
507 *max_h = h;
508}
509
Philipp Zabel927933f2013-09-30 10:34:48 -0300510static char *coda_product_name(int product)
511{
512 static char buf[9];
513
514 switch (product) {
515 case CODA_DX6:
516 return "CodaDx6";
517 case CODA_7541:
518 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300519 case CODA_960:
520 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300521 default:
522 snprintf(buf, sizeof(buf), "(0x%04x)", product);
523 return buf;
524 }
525}
526
Javier Martin186b2502012-07-26 05:53:35 -0300527/*
528 * V4L2 ioctl() operations.
529 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300530static int coda_querycap(struct file *file, void *priv,
531 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300532{
Philipp Zabel927933f2013-09-30 10:34:48 -0300533 struct coda_ctx *ctx = fh_to_ctx(priv);
534
Javier Martin186b2502012-07-26 05:53:35 -0300535 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300536 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
537 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300538 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300539 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300540 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
541
542 return 0;
543}
544
545static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300546 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300547{
548 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300549 struct coda_codec *codecs = ctx->dev->devtype->codecs;
550 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300551 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300552 int num_codecs = ctx->dev->devtype->num_codecs;
553 int num_formats = ARRAY_SIZE(coda_formats);
554 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300555
556 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300557 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300558 if (coda_format_is_yuv(formats[i].fourcc) &&
559 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300560 if (num == f->index)
561 break;
562 ++num;
563 continue;
564 }
565 /* Compressed formats may be supported, check the codec list */
566 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300567 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300568 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300569 formats[i].fourcc == codecs[k].dst_fourcc &&
570 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300571 break;
572 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
573 formats[i].fourcc == codecs[k].src_fourcc)
574 break;
575 }
576 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300577 if (num == f->index)
578 break;
579 ++num;
580 }
581 }
582
583 if (i < num_formats) {
584 fmt = &formats[i];
585 strlcpy(f->description, fmt->name, sizeof(f->description));
586 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300587 if (!coda_format_is_yuv(fmt->fourcc))
588 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300589 return 0;
590 }
591
592 /* Format not found */
593 return -EINVAL;
594}
595
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300596static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
597 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300598{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300599 struct coda_ctx *ctx = fh_to_ctx(priv);
600 struct vb2_queue *src_vq;
601 struct coda_q_data *q_data_src;
602
603 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300604 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300605 if (vb2_is_streaming(src_vq)) {
606 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
607
608 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
609 q_data_src->fourcc);
610 }
611
612 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300613}
614
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300615static int coda_enum_fmt_vid_out(struct file *file, void *priv,
616 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300617{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300618 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300619}
620
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300621static int coda_g_fmt(struct file *file, void *priv,
622 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300623{
Javier Martin186b2502012-07-26 05:53:35 -0300624 struct coda_q_data *q_data;
625 struct coda_ctx *ctx = fh_to_ctx(priv);
626
Javier Martin186b2502012-07-26 05:53:35 -0300627 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300628 if (!q_data)
629 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300630
631 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300632 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300633 f->fmt.pix.width = q_data->width;
634 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300635 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300636
637 f->fmt.pix.sizeimage = q_data->sizeimage;
638 f->fmt.pix.colorspace = ctx->colorspace;
639
640 return 0;
641}
642
Philipp Zabel57625592013-09-30 10:34:51 -0300643static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
644 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300645{
Philipp Zabel57625592013-09-30 10:34:51 -0300646 struct coda_dev *dev = ctx->dev;
647 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300648 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300649 enum v4l2_field field;
650
651 field = f->fmt.pix.field;
652 if (field == V4L2_FIELD_ANY)
653 field = V4L2_FIELD_NONE;
654 else if (V4L2_FIELD_NONE != field)
655 return -EINVAL;
656
657 /* V4L2 specification suggests the driver corrects the format struct
658 * if any of the dimensions is unsupported */
659 f->fmt.pix.field = field;
660
Philipp Zabel57625592013-09-30 10:34:51 -0300661 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
662 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
663 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
664 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300665
Philipp Zabel57625592013-09-30 10:34:51 -0300666 switch (f->fmt.pix.pixelformat) {
667 case V4L2_PIX_FMT_YUV420:
668 case V4L2_PIX_FMT_YVU420:
669 case V4L2_PIX_FMT_H264:
670 case V4L2_PIX_FMT_MPEG4:
671 case V4L2_PIX_FMT_JPEG:
672 break;
673 default:
674 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300675 if (!q_data)
676 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300677 f->fmt.pix.pixelformat = q_data->fourcc;
678 }
679
680 switch (f->fmt.pix.pixelformat) {
681 case V4L2_PIX_FMT_YUV420:
682 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300683 /* Frame stride must be multiple of 8, but 16 for h.264 */
684 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300685 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300686 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300687 break;
688 case V4L2_PIX_FMT_H264:
689 case V4L2_PIX_FMT_MPEG4:
690 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300691 f->fmt.pix.bytesperline = 0;
692 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300693 break;
694 default:
695 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300696 }
697
698 return 0;
699}
700
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300701static int coda_try_fmt_vid_cap(struct file *file, void *priv,
702 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300703{
Javier Martin186b2502012-07-26 05:53:35 -0300704 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300705 struct coda_codec *codec;
706 struct vb2_queue *src_vq;
707 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300708
Philipp Zabel918c66f2013-06-27 06:59:01 -0300709 /*
710 * If the source format is already fixed, try to find a codec that
711 * converts to the given destination format
712 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300713 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300714 if (vb2_is_streaming(src_vq)) {
715 struct coda_q_data *q_data_src;
716
717 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
718 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
719 f->fmt.pix.pixelformat);
720 if (!codec)
721 return -EINVAL;
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300722
723 f->fmt.pix.width = q_data_src->width;
724 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300725 } else {
726 /* Otherwise determine codec by encoded format, if possible */
727 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
728 f->fmt.pix.pixelformat);
729 }
Javier Martin186b2502012-07-26 05:53:35 -0300730
731 f->fmt.pix.colorspace = ctx->colorspace;
732
Philipp Zabel57625592013-09-30 10:34:51 -0300733 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300734 if (ret < 0)
735 return ret;
736
737 /* The h.264 decoder only returns complete 16x16 macroblocks */
738 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300739 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300740 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300741 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300742 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
743 f->fmt.pix.height * 3 / 2;
744 }
745
746 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300747}
748
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300749static int coda_try_fmt_vid_out(struct file *file, void *priv,
750 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300751{
752 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300753 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300754
Philipp Zabelb96904e2013-05-23 10:42:58 -0300755 /* Determine codec by encoded format, returns NULL if raw or invalid */
756 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
757 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300758
759 if (!f->fmt.pix.colorspace)
760 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
761
Philipp Zabel57625592013-09-30 10:34:51 -0300762 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300763}
764
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300765static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300766{
767 struct coda_q_data *q_data;
768 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300769
Philipp Zabel14604e32014-07-11 06:36:22 -0300770 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300771 if (!vq)
772 return -EINVAL;
773
774 q_data = get_q_data(ctx, f->type);
775 if (!q_data)
776 return -EINVAL;
777
778 if (vb2_is_busy(vq)) {
779 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
780 return -EBUSY;
781 }
782
Philipp Zabelb96904e2013-05-23 10:42:58 -0300783 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300784 q_data->width = f->fmt.pix.width;
785 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300786 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300787 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300788 q_data->rect.left = 0;
789 q_data->rect.top = 0;
790 q_data->rect.width = f->fmt.pix.width;
791 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300792
793 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
794 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300795 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300796
797 return 0;
798}
799
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300800static int coda_s_fmt_vid_cap(struct file *file, void *priv,
801 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300802{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300803 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300804 int ret;
805
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300806 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300807 if (ret)
808 return ret;
809
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300810 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300811}
812
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300813static int coda_s_fmt_vid_out(struct file *file, void *priv,
814 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300815{
816 struct coda_ctx *ctx = fh_to_ctx(priv);
817 int ret;
818
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300819 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300820 if (ret)
821 return ret;
822
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300823 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300824 if (ret)
825 ctx->colorspace = f->fmt.pix.colorspace;
826
827 return ret;
828}
829
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300830static int coda_qbuf(struct file *file, void *priv,
831 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300832{
833 struct coda_ctx *ctx = fh_to_ctx(priv);
834
Philipp Zabel14604e32014-07-11 06:36:22 -0300835 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300836}
837
Philipp Zabel918c66f2013-06-27 06:59:01 -0300838static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
839 struct v4l2_buffer *buf)
840{
841 struct vb2_queue *src_vq;
842
Philipp Zabel14604e32014-07-11 06:36:22 -0300843 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300844
845 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
846 (buf->sequence == (ctx->qsequence - 1)));
847}
848
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300849static int coda_dqbuf(struct file *file, void *priv,
850 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300851{
852 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300853 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300854
Philipp Zabel14604e32014-07-11 06:36:22 -0300855 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300856
857 /* If this is the last capture buffer, emit an end-of-stream event */
858 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
859 coda_buf_is_end_of_stream(ctx, buf)) {
860 const struct v4l2_event eos_event = {
861 .type = V4L2_EVENT_EOS
862 };
863
864 v4l2_event_queue_fh(&ctx->fh, &eos_event);
865 }
866
867 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300868}
869
Philipp Zabel52c41672014-07-11 06:36:19 -0300870static int coda_g_selection(struct file *file, void *fh,
871 struct v4l2_selection *s)
872{
873 struct coda_ctx *ctx = fh_to_ctx(fh);
874 struct coda_q_data *q_data;
875 struct v4l2_rect r, *rsel;
876
877 q_data = get_q_data(ctx, s->type);
878 if (!q_data)
879 return -EINVAL;
880
881 r.left = 0;
882 r.top = 0;
883 r.width = q_data->width;
884 r.height = q_data->height;
885 rsel = &q_data->rect;
886
887 switch (s->target) {
888 case V4L2_SEL_TGT_CROP_DEFAULT:
889 case V4L2_SEL_TGT_CROP_BOUNDS:
890 rsel = &r;
891 /* fallthrough */
892 case V4L2_SEL_TGT_CROP:
893 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
894 return -EINVAL;
895 break;
896 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
897 case V4L2_SEL_TGT_COMPOSE_PADDED:
898 rsel = &r;
899 /* fallthrough */
900 case V4L2_SEL_TGT_COMPOSE:
901 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
902 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
903 return -EINVAL;
904 break;
905 default:
906 return -EINVAL;
907 }
908
909 s->r = *rsel;
910
911 return 0;
912}
913
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300914static int coda_try_decoder_cmd(struct file *file, void *fh,
915 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300916{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300917 if (dc->cmd != V4L2_DEC_CMD_STOP)
918 return -EINVAL;
919
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300920 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300921 return -EINVAL;
922
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300923 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300924 return -EINVAL;
925
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300926 return 0;
927}
928
929static int coda_decoder_cmd(struct file *file, void *fh,
930 struct v4l2_decoder_cmd *dc)
931{
932 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300933 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300934 int ret;
935
936 ret = coda_try_decoder_cmd(file, fh, dc);
937 if (ret < 0)
938 return ret;
939
940 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300941 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300942 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300943
944 /* Set the strem-end flag on this context */
945 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
946
Philipp Zabel89548442014-07-11 06:36:17 -0300947 if ((dev->devtype->product == CODA_960) &&
948 coda_isbusy(dev) &&
949 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
950 /* If this context is currently running, update the hardware flag */
951 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
952 }
Philipp Zabel84e23652014-07-11 06:36:34 -0300953 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300954 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300955
Philipp Zabel918c66f2013-06-27 06:59:01 -0300956 return 0;
957}
958
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300959static int coda_subscribe_event(struct v4l2_fh *fh,
960 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300961{
962 switch (sub->type) {
963 case V4L2_EVENT_EOS:
964 return v4l2_event_subscribe(fh, sub, 0, NULL);
965 default:
966 return v4l2_ctrl_subscribe_event(fh, sub);
967 }
Javier Martin186b2502012-07-26 05:53:35 -0300968}
969
970static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300971 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300972
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300973 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
974 .vidioc_g_fmt_vid_cap = coda_g_fmt,
975 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
976 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300977
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300978 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
979 .vidioc_g_fmt_vid_out = coda_g_fmt,
980 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
981 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300982
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300983 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
984 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300985
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300986 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300987 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300988 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300989 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300990
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300991 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
992 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300993
Philipp Zabel52c41672014-07-11 06:36:19 -0300994 .vidioc_g_selection = coda_g_selection,
995
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300996 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300997 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300998
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300999 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -03001000 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -03001001};
1002
Philipp Zabel918c66f2013-06-27 06:59:01 -03001003static int coda_start_decoding(struct coda_ctx *ctx);
1004
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001005static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
1006{
1007 return kfifo_len(&ctx->bitstream_fifo);
1008}
1009
1010static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
1011{
1012 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1013 struct coda_dev *dev = ctx->dev;
1014 u32 rd_ptr;
1015
1016 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1017 kfifo->out = (kfifo->in & ~kfifo->mask) |
1018 (rd_ptr - ctx->bitstream.paddr);
1019 if (kfifo->out > kfifo->in)
1020 kfifo->out -= kfifo->mask + 1;
1021}
1022
1023static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
1024{
1025 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1026 struct coda_dev *dev = ctx->dev;
1027 u32 rd_ptr, wr_ptr;
1028
1029 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
1030 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
1031 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1032 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1033}
1034
1035static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
1036{
1037 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
1038 struct coda_dev *dev = ctx->dev;
1039 u32 wr_ptr;
1040
1041 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
1042 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
1043}
1044
1045static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
1046{
1047 u32 src_size = vb2_get_plane_payload(src_buf, 0);
1048 u32 n;
1049
1050 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1051 if (n < src_size)
1052 return -ENOSPC;
1053
1054 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1055 ctx->bitstream.size, DMA_TO_DEVICE);
1056
Philipp Zabel846ced92014-07-11 06:36:31 -03001057 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001058
1059 return 0;
1060}
1061
1062static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1063 struct vb2_buffer *src_buf)
1064{
1065 int ret;
1066
1067 if (coda_get_bitstream_payload(ctx) +
1068 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1069 return false;
1070
1071 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1072 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1073 return true;
1074 }
1075
1076 ret = coda_bitstream_queue(ctx, src_buf);
1077 if (ret < 0) {
1078 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1079 return false;
1080 }
1081 /* Sync read pointer to device */
1082 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1083 coda_kfifo_sync_to_device_write(ctx);
1084
Philipp Zabel84e23652014-07-11 06:36:34 -03001085 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001086
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001087 return true;
1088}
1089
1090static void coda_fill_bitstream(struct coda_ctx *ctx)
1091{
1092 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03001093 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001094
Philipp Zabel14604e32014-07-11 06:36:22 -03001095 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1096 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001097
1098 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001099 /*
1100 * Source buffer is queued in the bitstream ringbuffer;
1101 * queue the timestamp and mark source buffer as done
1102 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001103 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -03001104
1105 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
1106 if (ts) {
1107 ts->sequence = src_buf->v4l2_buf.sequence;
1108 ts->timecode = src_buf->v4l2_buf.timecode;
1109 ts->timestamp = src_buf->v4l2_buf.timestamp;
1110 list_add_tail(&ts->list, &ctx->timestamp_list);
1111 }
1112
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001113 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1114 } else {
1115 break;
1116 }
1117 }
1118}
1119
Philipp Zabel89548442014-07-11 06:36:17 -03001120static void coda_set_gdi_regs(struct coda_ctx *ctx)
1121{
1122 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1123 struct coda_dev *dev = ctx->dev;
1124 int i;
1125
1126 for (i = 0; i < 16; i++)
1127 coda_write(dev, tiled_map->xy2ca_map[i],
1128 CODA9_GDI_XY2_CAS_0 + 4 * i);
1129 for (i = 0; i < 4; i++)
1130 coda_write(dev, tiled_map->xy2ba_map[i],
1131 CODA9_GDI_XY2_BA_0 + 4 * i);
1132 for (i = 0; i < 16; i++)
1133 coda_write(dev, tiled_map->xy2ra_map[i],
1134 CODA9_GDI_XY2_RAS_0 + 4 * i);
1135 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1136 for (i = 0; i < 32; i++)
1137 coda_write(dev, tiled_map->rbc2axi_map[i],
1138 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1139}
1140
Javier Martin186b2502012-07-26 05:53:35 -03001141/*
1142 * Mem-to-mem operations.
1143 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001144static int coda_prepare_decode(struct coda_ctx *ctx)
1145{
1146 struct vb2_buffer *dst_buf;
1147 struct coda_dev *dev = ctx->dev;
1148 struct coda_q_data *q_data_dst;
1149 u32 stridey, height;
1150 u32 picture_y, picture_cb, picture_cr;
1151
Philipp Zabel14604e32014-07-11 06:36:22 -03001152 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001153 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1154
1155 if (ctx->params.rot_mode & CODA_ROT_90) {
1156 stridey = q_data_dst->height;
1157 height = q_data_dst->width;
1158 } else {
1159 stridey = q_data_dst->width;
1160 height = q_data_dst->height;
1161 }
1162
1163 /* Try to copy source buffer contents into the bitstream ringbuffer */
1164 mutex_lock(&ctx->bitstream_mutex);
1165 coda_fill_bitstream(ctx);
1166 mutex_unlock(&ctx->bitstream_mutex);
1167
1168 if (coda_get_bitstream_payload(ctx) < 512 &&
1169 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1170 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1171 "bitstream payload: %d, skipping\n",
1172 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001173 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001174 return -EAGAIN;
1175 }
1176
1177 /* Run coda_start_decoding (again) if not yet initialized */
1178 if (!ctx->initialized) {
1179 int ret = coda_start_decoding(ctx);
1180 if (ret < 0) {
1181 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001182 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001183 return -EAGAIN;
1184 } else {
1185 ctx->initialized = 1;
1186 }
1187 }
1188
Philipp Zabel89548442014-07-11 06:36:17 -03001189 if (dev->devtype->product == CODA_960)
1190 coda_set_gdi_regs(ctx);
1191
Philipp Zabel918c66f2013-06-27 06:59:01 -03001192 /* Set rotator output */
1193 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1194 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1195 /* Switch Cr and Cb for YVU420 format */
1196 picture_cr = picture_y + stridey * height;
1197 picture_cb = picture_cr + stridey / 2 * height / 2;
1198 } else {
1199 picture_cb = picture_y + stridey * height;
1200 picture_cr = picture_cb + stridey / 2 * height / 2;
1201 }
Philipp Zabel89548442014-07-11 06:36:17 -03001202
1203 if (dev->devtype->product == CODA_960) {
1204 /*
1205 * The CODA960 seems to have an internal list of buffers with
1206 * 64 entries that includes the registered frame buffers as
1207 * well as the rotator buffer output.
1208 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1209 */
1210 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1211 CODA9_CMD_DEC_PIC_ROT_INDEX);
1212 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1213 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1214 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1215 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1216 } else {
1217 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1218 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1219 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1220 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1221 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001222 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1223 CODA_CMD_DEC_PIC_ROT_MODE);
1224
1225 switch (dev->devtype->product) {
1226 case CODA_DX6:
1227 /* TBD */
1228 case CODA_7541:
1229 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1230 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001231 case CODA_960:
1232 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1233 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001234 }
1235
1236 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1237
1238 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1239 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1240
1241 return 0;
1242}
1243
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001244static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001245{
Javier Martin186b2502012-07-26 05:53:35 -03001246 struct coda_q_data *q_data_src, *q_data_dst;
1247 struct vb2_buffer *src_buf, *dst_buf;
1248 struct coda_dev *dev = ctx->dev;
1249 int force_ipicture;
1250 int quant_param = 0;
1251 u32 picture_y, picture_cb, picture_cr;
1252 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1253 u32 dst_fourcc;
1254
Philipp Zabel14604e32014-07-11 06:36:22 -03001255 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1256 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001257 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1258 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001259 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001260
Philipp Zabel918c66f2013-06-27 06:59:01 -03001261 src_buf->v4l2_buf.sequence = ctx->osequence;
1262 dst_buf->v4l2_buf.sequence = ctx->osequence;
1263 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001264
1265 /*
1266 * Workaround coda firmware BUG that only marks the first
1267 * frame as IDR. This is a problem for some decoders that can't
1268 * recover when a frame is lost.
1269 */
1270 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1271 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1272 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1273 } else {
1274 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1275 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1276 }
1277
Philipp Zabel89548442014-07-11 06:36:17 -03001278 if (dev->devtype->product == CODA_960)
1279 coda_set_gdi_regs(ctx);
1280
Javier Martin186b2502012-07-26 05:53:35 -03001281 /*
1282 * Copy headers at the beginning of the first frame for H.264 only.
1283 * In MPEG4 they are already copied by the coda.
1284 */
1285 if (src_buf->v4l2_buf.sequence == 0) {
1286 pic_stream_buffer_addr =
1287 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1288 ctx->vpu_header_size[0] +
1289 ctx->vpu_header_size[1] +
1290 ctx->vpu_header_size[2];
1291 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1292 ctx->vpu_header_size[0] -
1293 ctx->vpu_header_size[1] -
1294 ctx->vpu_header_size[2];
1295 memcpy(vb2_plane_vaddr(dst_buf, 0),
1296 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1297 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1298 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1299 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1300 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1301 ctx->vpu_header_size[2]);
1302 } else {
1303 pic_stream_buffer_addr =
1304 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1305 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1306 }
1307
1308 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1309 force_ipicture = 1;
1310 switch (dst_fourcc) {
1311 case V4L2_PIX_FMT_H264:
1312 quant_param = ctx->params.h264_intra_qp;
1313 break;
1314 case V4L2_PIX_FMT_MPEG4:
1315 quant_param = ctx->params.mpeg4_intra_qp;
1316 break;
1317 default:
1318 v4l2_warn(&ctx->dev->v4l2_dev,
1319 "cannot set intra qp, fmt not supported\n");
1320 break;
1321 }
1322 } else {
1323 force_ipicture = 0;
1324 switch (dst_fourcc) {
1325 case V4L2_PIX_FMT_H264:
1326 quant_param = ctx->params.h264_inter_qp;
1327 break;
1328 case V4L2_PIX_FMT_MPEG4:
1329 quant_param = ctx->params.mpeg4_inter_qp;
1330 break;
1331 default:
1332 v4l2_warn(&ctx->dev->v4l2_dev,
1333 "cannot set inter qp, fmt not supported\n");
1334 break;
1335 }
1336 }
1337
1338 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001339 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001340 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1341
1342
1343 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001344 switch (q_data_src->fourcc) {
1345 case V4L2_PIX_FMT_YVU420:
1346 /* Switch Cb and Cr for YVU420 format */
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001347 picture_cr = picture_y + q_data_src->bytesperline *
1348 q_data_src->height;
1349 picture_cb = picture_cr + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001350 q_data_src->height / 2;
1351 break;
1352 case V4L2_PIX_FMT_YUV420:
1353 default:
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001354 picture_cb = picture_y + q_data_src->bytesperline *
1355 q_data_src->height;
1356 picture_cr = picture_cb + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001357 q_data_src->height / 2;
1358 break;
1359 }
Javier Martin186b2502012-07-26 05:53:35 -03001360
Philipp Zabel89548442014-07-11 06:36:17 -03001361 if (dev->devtype->product == CODA_960) {
1362 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1363 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1364 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1365
1366 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1367 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1368 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1369 } else {
1370 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1371 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1372 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1373 }
Javier Martin186b2502012-07-26 05:53:35 -03001374 coda_write(dev, force_ipicture << 1 & 0x2,
1375 CODA_CMD_ENC_PIC_OPTION);
1376
1377 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1378 coda_write(dev, pic_stream_buffer_size / 1024,
1379 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001380
1381 if (!ctx->streamon_out) {
1382 /* After streamoff on the output side, set the stream end flag */
1383 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1384 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1385 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001386}
1387
1388static void coda_device_run(void *m2m_priv)
1389{
1390 struct coda_ctx *ctx = m2m_priv;
1391 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001392
1393 queue_work(dev->workqueue, &ctx->pic_run_work);
1394}
1395
1396static void coda_free_framebuffers(struct coda_ctx *ctx);
1397static void coda_free_context_buffers(struct coda_ctx *ctx);
1398
1399static void coda_seq_end_work(struct work_struct *work)
1400{
1401 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1402 struct coda_dev *dev = ctx->dev;
1403
1404 mutex_lock(&ctx->buffer_mutex);
1405 mutex_lock(&dev->coda_mutex);
1406
1407 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1408 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1409 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1410 v4l2_err(&dev->v4l2_dev,
1411 "CODA_COMMAND_SEQ_END failed\n");
1412 }
1413
1414 kfifo_init(&ctx->bitstream_fifo,
1415 ctx->bitstream.vaddr, ctx->bitstream.size);
1416
1417 coda_free_framebuffers(ctx);
1418 coda_free_context_buffers(ctx);
1419
1420 mutex_unlock(&dev->coda_mutex);
1421 mutex_unlock(&ctx->buffer_mutex);
1422}
1423
1424static void coda_finish_decode(struct coda_ctx *ctx);
1425static void coda_finish_encode(struct coda_ctx *ctx);
1426
1427static void coda_pic_run_work(struct work_struct *work)
1428{
1429 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1430 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001431 int ret;
1432
1433 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001434 mutex_lock(&dev->coda_mutex);
1435
Philipp Zabel918c66f2013-06-27 06:59:01 -03001436 if (ctx->inst_type == CODA_INST_DECODER) {
1437 ret = coda_prepare_decode(ctx);
1438 if (ret < 0) {
1439 mutex_unlock(&dev->coda_mutex);
1440 mutex_unlock(&ctx->buffer_mutex);
1441 /* job_finish scheduled by prepare_decode */
1442 return;
1443 }
1444 } else {
1445 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001446 }
1447
Philipp Zabelc2d22512013-06-21 03:55:28 -03001448 if (dev->devtype->product != CODA_DX6)
1449 coda_write(dev, ctx->iram_info.axi_sram_use,
1450 CODA7_REG_BIT_AXI_SRAM_USE);
1451
Philipp Zabel918c66f2013-06-27 06:59:01 -03001452 if (ctx->inst_type == CODA_INST_DECODER)
1453 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001454 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001455
1456 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1457 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001458
1459 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001460
1461 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001462 } else if (!ctx->aborting) {
1463 if (ctx->inst_type == CODA_INST_DECODER)
1464 coda_finish_decode(ctx);
1465 else
1466 coda_finish_encode(ctx);
1467 }
1468
1469 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1470 queue_work(dev->workqueue, &ctx->seq_end_work);
1471
1472 mutex_unlock(&dev->coda_mutex);
1473 mutex_unlock(&ctx->buffer_mutex);
1474
Philipp Zabel14604e32014-07-11 06:36:22 -03001475 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001476}
1477
1478static int coda_job_ready(void *m2m_priv)
1479{
1480 struct coda_ctx *ctx = m2m_priv;
1481
1482 /*
1483 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001484 * and 1 frame are needed. In the decoder case,
1485 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001486 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001487 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001488 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001489 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1490 "not ready: not enough video buffers.\n");
1491 return 0;
1492 }
1493
Philipp Zabel14604e32014-07-11 06:36:22 -03001494 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001495 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1496 "not ready: not enough video capture buffers.\n");
1497 return 0;
1498 }
1499
Philipp Zabel84e23652014-07-11 06:36:34 -03001500 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001501 ((ctx->inst_type == CODA_INST_DECODER) &&
1502 (coda_get_bitstream_payload(ctx) < 512) &&
1503 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1504 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1505 "%d: not ready: not enough bitstream data.\n",
1506 ctx->idx);
1507 return 0;
1508 }
1509
Philipp Zabel3e748262013-05-23 10:43:01 -03001510 if (ctx->aborting) {
1511 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1512 "not ready: aborting\n");
1513 return 0;
1514 }
1515
Javier Martin186b2502012-07-26 05:53:35 -03001516 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1517 "job ready\n");
1518 return 1;
1519}
1520
1521static void coda_job_abort(void *priv)
1522{
1523 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001524
1525 ctx->aborting = 1;
1526
1527 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1528 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001529}
1530
1531static void coda_lock(void *m2m_priv)
1532{
1533 struct coda_ctx *ctx = m2m_priv;
1534 struct coda_dev *pcdev = ctx->dev;
1535 mutex_lock(&pcdev->dev_mutex);
1536}
1537
1538static void coda_unlock(void *m2m_priv)
1539{
1540 struct coda_ctx *ctx = m2m_priv;
1541 struct coda_dev *pcdev = ctx->dev;
1542 mutex_unlock(&pcdev->dev_mutex);
1543}
1544
1545static struct v4l2_m2m_ops coda_m2m_ops = {
1546 .device_run = coda_device_run,
1547 .job_ready = coda_job_ready,
1548 .job_abort = coda_job_abort,
1549 .lock = coda_lock,
1550 .unlock = coda_unlock,
1551};
1552
Philipp Zabel89548442014-07-11 06:36:17 -03001553static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1554{
1555 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1556 int luma_map, chro_map, i;
1557
1558 memset(tiled_map, 0, sizeof(*tiled_map));
1559
1560 luma_map = 64;
1561 chro_map = 64;
1562 tiled_map->map_type = tiled_map_type;
1563 for (i = 0; i < 16; i++)
1564 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1565 for (i = 0; i < 4; i++)
1566 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1567 for (i = 0; i < 16; i++)
1568 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1569
1570 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1571 tiled_map->xy2rbc_config = 0;
1572 } else {
1573 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1574 tiled_map_type);
1575 return;
1576 }
1577}
1578
Javier Martin186b2502012-07-26 05:53:35 -03001579static void set_default_params(struct coda_ctx *ctx)
1580{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001581 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001582 int max_w;
1583 int max_h;
1584
Philipp Zabel121cacf2014-07-18 07:22:42 -03001585 if (ctx->inst_type == CODA_INST_ENCODER) {
1586 src_fourcc = V4L2_PIX_FMT_YUV420;
1587 dst_fourcc = V4L2_PIX_FMT_H264;
1588 } else {
1589 src_fourcc = V4L2_PIX_FMT_H264;
1590 dst_fourcc = V4L2_PIX_FMT_YUV420;
1591 }
1592 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001593 max_w = ctx->codec->max_w;
1594 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001595
Philipp Zabel121cacf2014-07-18 07:22:42 -03001596 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001597 ctx->colorspace = V4L2_COLORSPACE_REC709;
1598 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001599 ctx->aborting = 0;
1600
1601 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001602 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1603 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1604 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1605 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001606 ctx->q_data[V4L2_M2M_DST].width = max_w;
1607 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001608 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1609 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1610 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1611 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1612 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1613 } else {
1614 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1615 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
1616 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1617 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
1618 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001619 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1620 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1621 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1622 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001623
1624 if (ctx->dev->devtype->product == CODA_960)
1625 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001626}
1627
1628/*
1629 * Queue operations
1630 */
1631static int coda_queue_setup(struct vb2_queue *vq,
1632 const struct v4l2_format *fmt,
1633 unsigned int *nbuffers, unsigned int *nplanes,
1634 unsigned int sizes[], void *alloc_ctxs[])
1635{
1636 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001637 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001638 unsigned int size;
1639
Philipp Zabele34db062012-08-29 08:22:00 -03001640 q_data = get_q_data(ctx, vq->type);
1641 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001642
1643 *nplanes = 1;
1644 sizes[0] = size;
1645
1646 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1647
1648 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1649 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1650
1651 return 0;
1652}
1653
1654static int coda_buf_prepare(struct vb2_buffer *vb)
1655{
1656 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1657 struct coda_q_data *q_data;
1658
1659 q_data = get_q_data(ctx, vb->vb2_queue->type);
1660
1661 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1662 v4l2_warn(&ctx->dev->v4l2_dev,
1663 "%s data will not fit into plane (%lu < %lu)\n",
1664 __func__, vb2_plane_size(vb, 0),
1665 (long)q_data->sizeimage);
1666 return -EINVAL;
1667 }
1668
Javier Martin186b2502012-07-26 05:53:35 -03001669 return 0;
1670}
1671
1672static void coda_buf_queue(struct vb2_buffer *vb)
1673{
1674 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001675 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001676 struct coda_q_data *q_data;
1677
1678 q_data = get_q_data(ctx, vb->vb2_queue->type);
1679
1680 /*
1681 * In the decoder case, immediately try to copy the buffer into the
1682 * bitstream ringbuffer and mark it as ready to be dequeued.
1683 */
1684 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1685 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1686 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001687 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001688 * the stream end
1689 */
Philipp Zabel89548442014-07-11 06:36:17 -03001690 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001691 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001692 if ((dev->devtype->product == CODA_960) &&
1693 coda_isbusy(dev) &&
1694 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1695 /* if this decoder instance is running, set the stream end flag */
1696 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1697 }
1698 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001699 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001700 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001701 if (vb2_is_streaming(vb->vb2_queue))
1702 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001703 mutex_unlock(&ctx->bitstream_mutex);
1704 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001705 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001706 }
Javier Martin186b2502012-07-26 05:53:35 -03001707}
1708
Philipp Zabel86eda902013-05-23 10:42:57 -03001709static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1710{
1711 struct coda_dev *dev = ctx->dev;
1712 u32 *p = ctx->parabuf.vaddr;
1713
1714 if (dev->devtype->product == CODA_DX6)
1715 p[index] = value;
1716 else
1717 p[index ^ 1] = value;
1718}
1719
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001720static int coda_alloc_aux_buf(struct coda_dev *dev,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001721 struct coda_aux_buf *buf, size_t size,
1722 const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001723{
1724 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1725 GFP_KERNEL);
1726 if (!buf->vaddr)
1727 return -ENOMEM;
1728
1729 buf->size = size;
1730
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001731 if (name && parent) {
1732 buf->blob.data = buf->vaddr;
1733 buf->blob.size = size;
1734 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
1735 if (!buf->dentry)
1736 dev_warn(&dev->plat_dev->dev,
1737 "failed to create debugfs entry %s\n", name);
1738 }
1739
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001740 return 0;
1741}
1742
1743static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001744 struct coda_aux_buf *buf, size_t size,
1745 const char *name)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001746{
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001747 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001748}
1749
1750static void coda_free_aux_buf(struct coda_dev *dev,
1751 struct coda_aux_buf *buf)
1752{
1753 if (buf->vaddr) {
1754 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1755 buf->vaddr, buf->paddr);
1756 buf->vaddr = NULL;
1757 buf->size = 0;
1758 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001759 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001760}
1761
1762static void coda_free_framebuffers(struct coda_ctx *ctx)
1763{
1764 int i;
1765
1766 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1767 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1768}
1769
Philipp Zabelec25f682012-07-20 08:54:29 -03001770static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1771{
1772 struct coda_dev *dev = ctx->dev;
Philipp Zabel7c3df782014-07-11 06:36:38 -03001773 int width, height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001774 dma_addr_t paddr;
1775 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001776 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001777 int i;
1778
Philipp Zabel7c3df782014-07-11 06:36:38 -03001779 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
1780 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
1781 width = round_up(q_data->width, 16);
1782 height = round_up(q_data->height, 16);
1783 } else {
1784 width = round_up(q_data->width, 8);
1785 height = q_data->height;
1786 }
1787 ysize = width * height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001788
Philipp Zabelec25f682012-07-20 08:54:29 -03001789 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001790 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001791 size_t size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001792 char *name;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001793
Philipp Zabelaed14b02014-07-11 06:36:15 -03001794 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001795 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1796 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001797 size += ysize / 4;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001798 name = kasprintf(GFP_KERNEL, "fb%d", i);
1799 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
1800 size, name);
1801 kfree(name);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001802 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001803 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001804 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001805 }
1806 }
1807
1808 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001809 for (i = 0; i < ctx->num_internal_frames; i++) {
1810 paddr = ctx->internal_frames[i].paddr;
1811 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1812 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1813 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001814
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001815 /* mvcol buffer for h.264 */
1816 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1817 dev->devtype->product != CODA_DX6)
1818 coda_parabuf_write(ctx, 96 + i,
1819 ctx->internal_frames[i].paddr +
1820 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001821 }
1822
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001823 /* mvcol buffer for mpeg4 */
1824 if ((dev->devtype->product != CODA_DX6) &&
1825 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1826 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1827 ysize + ysize/4 + ysize/4);
1828
Philipp Zabelec25f682012-07-20 08:54:29 -03001829 return 0;
1830}
1831
Javier Martin3f3f5c72012-10-29 05:20:29 -03001832static int coda_h264_padding(int size, char *p)
1833{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001834 int nal_size;
1835 int diff;
1836
Javier Martin832fbb52012-10-29 08:34:59 -03001837 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001838 if (diff == 0)
1839 return 0;
1840
Javier Martin832fbb52012-10-29 08:34:59 -03001841 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001842 memcpy(p, coda_filler_nal, nal_size);
1843
1844 /* Add rbsp stop bit and trailing at the end */
1845 *(p + nal_size - 1) = 0x80;
1846
1847 return nal_size;
1848}
1849
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001850static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1851{
1852 phys_addr_t ret;
1853
1854 size = round_up(size, 1024);
1855 if (size > iram->remaining)
1856 return 0;
1857 iram->remaining -= size;
1858
1859 ret = iram->next_paddr;
1860 iram->next_paddr += size;
1861
1862 return ret;
1863}
1864
Philipp Zabelc2d22512013-06-21 03:55:28 -03001865static void coda_setup_iram(struct coda_ctx *ctx)
1866{
1867 struct coda_iram_info *iram_info = &ctx->iram_info;
1868 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001869 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001870 int dbk_bits;
1871 int bit_bits;
1872 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001873
1874 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001875 iram_info->next_paddr = dev->iram.paddr;
1876 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001877
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001878 switch (dev->devtype->product) {
1879 case CODA_7541:
1880 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1881 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1882 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1883 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001884 case CODA_960:
1885 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1886 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1887 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1888 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001889 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001890 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001891 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001892
1893 if (ctx->inst_type == CODA_INST_ENCODER) {
1894 struct coda_q_data *q_data_src;
1895
1896 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1897 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1898
1899 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001900 if (dev->devtype->product == CODA_7541) {
1901 iram_info->search_ram_size = round_up(mb_width * 16 *
1902 36 + 2048, 1024);
1903 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1904 iram_info->search_ram_size);
1905 if (!iram_info->search_ram_paddr) {
1906 pr_err("IRAM is smaller than the search ram size\n");
1907 goto out;
1908 }
1909 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1910 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001911 }
1912
1913 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001914 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1915 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1916 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001917 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001918 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001919
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001920 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1921 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001922 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001923 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001924
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001925 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1926 if (!iram_info->buf_ip_ac_dc_use)
1927 goto out;
1928 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001929
Philipp Zabel8358e762013-06-21 03:55:32 -03001930 /* OVL and BTP disabled for encoder */
1931 } else if (ctx->inst_type == CODA_INST_DECODER) {
1932 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001933
1934 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1935 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001936
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001937 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1938 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1939 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001940 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001941 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001942
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001943 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1944 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001945 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001946 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001947
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001948 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1949 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001950 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001951 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001952
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001953 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001954 }
1955
1956out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001957 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1958 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1959 "IRAM smaller than needed\n");
1960
1961 if (dev->devtype->product == CODA_7541) {
1962 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001963 if (ctx->inst_type == CODA_INST_DECODER) {
1964 /* fw 1.4.50 */
1965 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1966 CODA7_USE_IP_ENABLE);
1967 } else {
1968 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001969 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1970 CODA7_USE_HOST_DBK_ENABLE |
1971 CODA7_USE_IP_ENABLE |
1972 CODA7_USE_DBK_ENABLE);
1973 }
1974 }
1975}
1976
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001977static void coda_free_context_buffers(struct coda_ctx *ctx)
1978{
1979 struct coda_dev *dev = ctx->dev;
1980
Philipp Zabel918c66f2013-06-27 06:59:01 -03001981 coda_free_aux_buf(dev, &ctx->slicebuf);
1982 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001983 if (dev->devtype->product != CODA_DX6)
1984 coda_free_aux_buf(dev, &ctx->workbuf);
1985}
1986
1987static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1988 struct coda_q_data *q_data)
1989{
1990 struct coda_dev *dev = ctx->dev;
1991 size_t size;
1992 int ret;
1993
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001994 if (dev->devtype->product == CODA_DX6)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001995 return 0;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001996
Philipp Zabel918c66f2013-06-27 06:59:01 -03001997 if (ctx->psbuf.vaddr) {
1998 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1999 return -EBUSY;
2000 }
2001 if (ctx->slicebuf.vaddr) {
2002 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
2003 return -EBUSY;
2004 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002005 if (ctx->workbuf.vaddr) {
2006 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
2007 ret = -EBUSY;
2008 return -ENOMEM;
2009 }
2010
Philipp Zabel918c66f2013-06-27 06:59:01 -03002011 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
2012 /* worst case slice size */
2013 size = (DIV_ROUND_UP(q_data->width, 16) *
2014 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002015 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, "slicebuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002016 if (ret < 0) {
2017 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
2018 ctx->slicebuf.size);
2019 return ret;
2020 }
2021 }
2022
2023 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002024 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03002025 if (ret < 0) {
2026 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
2027 goto err;
2028 }
2029 }
2030
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002031 size = dev->devtype->workbuf_size;
2032 if (dev->devtype->product == CODA_960 &&
2033 q_data->fourcc == V4L2_PIX_FMT_H264)
2034 size += CODA9_PS_SAVE_SIZE;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002035 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002036 if (ret < 0) {
2037 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
2038 ctx->workbuf.size);
2039 goto err;
2040 }
2041
2042 return 0;
2043
2044err:
2045 coda_free_context_buffers(ctx);
2046 return ret;
2047}
2048
Philipp Zabel918c66f2013-06-27 06:59:01 -03002049static int coda_start_decoding(struct coda_ctx *ctx)
2050{
2051 struct coda_q_data *q_data_src, *q_data_dst;
2052 u32 bitstream_buf, bitstream_size;
2053 struct coda_dev *dev = ctx->dev;
2054 int width, height;
2055 u32 src_fourcc;
2056 u32 val;
2057 int ret;
2058
2059 /* Start decoding */
2060 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2061 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2062 bitstream_buf = ctx->bitstream.paddr;
2063 bitstream_size = ctx->bitstream.size;
2064 src_fourcc = q_data_src->fourcc;
2065
2066 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
2067
2068 /* Update coda bitstream read and write pointers from kfifo */
2069 coda_kfifo_sync_to_device_full(ctx);
2070
2071 ctx->display_idx = -1;
2072 ctx->frm_dis_flg = 0;
2073 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
2074
2075 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
2076 CODA_REG_BIT_BIT_STREAM_PARAM);
2077
2078 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
2079 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
2080 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002081 if ((dev->devtype->product == CODA_7541) ||
2082 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03002083 val |= CODA_REORDER_ENABLE;
2084 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
2085
2086 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03002087 if (dev->devtype->product == CODA_960 &&
2088 src_fourcc == V4L2_PIX_FMT_MPEG4)
2089 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
2090 else
2091 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002092 if (src_fourcc == V4L2_PIX_FMT_H264) {
2093 if (dev->devtype->product == CODA_7541) {
2094 coda_write(dev, ctx->psbuf.paddr,
2095 CODA_CMD_DEC_SEQ_PS_BB_START);
2096 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
2097 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
2098 }
Philipp Zabel89548442014-07-11 06:36:17 -03002099 if (dev->devtype->product == CODA_960) {
2100 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
2101 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
2102 }
2103 }
2104 if (dev->devtype->product != CODA_960) {
2105 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002106 }
2107
2108 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2109 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2110 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2111 return -ETIMEDOUT;
2112 }
2113
2114 /* Update kfifo out pointer from coda bitstream read pointer */
2115 coda_kfifo_sync_from_device(ctx);
2116
2117 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2118
2119 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2120 v4l2_err(&dev->v4l2_dev,
2121 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2122 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2123 return -EAGAIN;
2124 }
2125
2126 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2127 if (dev->devtype->product == CODA_DX6) {
2128 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2129 height = val & CODADX6_PICHEIGHT_MASK;
2130 } else {
2131 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2132 height = val & CODA7_PICHEIGHT_MASK;
2133 }
2134
2135 if (width > q_data_dst->width || height > q_data_dst->height) {
2136 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2137 width, height, q_data_dst->width, q_data_dst->height);
2138 return -EINVAL;
2139 }
2140
2141 width = round_up(width, 16);
2142 height = round_up(height, 16);
2143
2144 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2145 __func__, ctx->idx, width, height);
2146
Philipp Zabela500a932014-07-11 06:36:13 -03002147 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002148 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2149 v4l2_err(&dev->v4l2_dev,
2150 "not enough framebuffers to decode (%d < %d)\n",
2151 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2152 return -EINVAL;
2153 }
2154
Philipp Zabel52c41672014-07-11 06:36:19 -03002155 if (src_fourcc == V4L2_PIX_FMT_H264) {
2156 u32 left_right;
2157 u32 top_bottom;
2158
2159 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2160 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2161
2162 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2163 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2164 q_data_dst->rect.width = width - q_data_dst->rect.left -
2165 (left_right & 0x3ff);
2166 q_data_dst->rect.height = height - q_data_dst->rect.top -
2167 (top_bottom & 0x3ff);
2168 }
2169
Philipp Zabel918c66f2013-06-27 06:59:01 -03002170 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2171 if (ret < 0)
2172 return ret;
2173
2174 /* Tell the decoder how many frame buffers we allocated. */
2175 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2176 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2177
2178 if (dev->devtype->product != CODA_DX6) {
2179 /* Set secondary AXI IRAM */
2180 coda_setup_iram(ctx);
2181
2182 coda_write(dev, ctx->iram_info.buf_bit_use,
2183 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2184 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2185 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2186 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2187 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2188 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2189 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2190 coda_write(dev, ctx->iram_info.buf_ovl_use,
2191 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002192 if (dev->devtype->product == CODA_960)
2193 coda_write(dev, ctx->iram_info.buf_btp_use,
2194 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2195 }
2196
2197 if (dev->devtype->product == CODA_960) {
2198 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2199
2200 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2201 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2202 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2203 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2204 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2205 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002206 }
2207
2208 if (src_fourcc == V4L2_PIX_FMT_H264) {
2209 coda_write(dev, ctx->slicebuf.paddr,
2210 CODA_CMD_SET_FRAME_SLICE_BB_START);
2211 coda_write(dev, ctx->slicebuf.size / 1024,
2212 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2213 }
2214
2215 if (dev->devtype->product == CODA_7541) {
2216 int max_mb_x = 1920 / 16;
2217 int max_mb_y = 1088 / 16;
2218 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002219
Philipp Zabel918c66f2013-06-27 06:59:01 -03002220 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2221 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002222 } else if (dev->devtype->product == CODA_960) {
2223 int max_mb_x = 1920 / 16;
2224 int max_mb_y = 1088 / 16;
2225 int max_mb_num = max_mb_x * max_mb_y;
2226
2227 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2228 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002229 }
2230
2231 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2232 v4l2_err(&ctx->dev->v4l2_dev,
2233 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2234 return -ETIMEDOUT;
2235 }
2236
2237 return 0;
2238}
2239
Philipp Zabeld35167a2013-05-23 10:42:59 -03002240static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2241 int header_code, u8 *header, int *size)
2242{
2243 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002244 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002245 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002246 int i;
2247
2248 if (dev->devtype->product == CODA_960)
2249 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002250
2251 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2252 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002253 bufsize = vb2_plane_size(buf, 0);
2254 if (dev->devtype->product == CODA_960)
2255 bufsize /= 1024;
2256 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002257 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2258 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2259 if (ret < 0) {
2260 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2261 return ret;
2262 }
Philipp Zabel89548442014-07-11 06:36:17 -03002263
2264 if (dev->devtype->product == CODA_960) {
2265 for (i = 63; i > 0; i--)
2266 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2267 break;
2268 *size = i + 1;
2269 } else {
2270 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2271 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2272 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002273 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2274
2275 return 0;
2276}
2277
Philipp Zabel89548442014-07-11 06:36:17 -03002278static int coda_start_encoding(struct coda_ctx *ctx);
2279
Javier Martin186b2502012-07-26 05:53:35 -03002280static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2281{
2282 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2283 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002284 struct coda_dev *dev = ctx->dev;
2285 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002286 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002287 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002288
Philipp Zabelb96904e2013-05-23 10:42:58 -03002289 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002290 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2291 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03002292 /* copy the buffers that where queued before streamon */
2293 mutex_lock(&ctx->bitstream_mutex);
2294 coda_fill_bitstream(ctx);
2295 mutex_unlock(&ctx->bitstream_mutex);
2296
Philipp Zabel918c66f2013-06-27 06:59:01 -03002297 if (coda_get_bitstream_payload(ctx) < 512)
2298 return -EINVAL;
2299 } else {
2300 if (count < 1)
2301 return -EINVAL;
2302 }
2303
2304 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002305 } else {
2306 if (count < 1)
2307 return -EINVAL;
2308
2309 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002310 }
Javier Martin186b2502012-07-26 05:53:35 -03002311
Philipp Zabelb96904e2013-05-23 10:42:58 -03002312 /* Don't start the coda unless both queues are on */
2313 if (!(ctx->streamon_out & ctx->streamon_cap))
2314 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002315
Philipp Zabeleb107512013-09-30 10:34:45 -03002316 /* Allow decoder device_run with no new buffers queued */
2317 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002318 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002319
Philipp Zabelb96904e2013-05-23 10:42:58 -03002320 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002321 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002322 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002323
Philipp Zabelb96904e2013-05-23 10:42:58 -03002324 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2325 q_data_dst->fourcc);
2326 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002327 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2328 return -EINVAL;
2329 }
2330
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002331 /* Allocate per-instance buffers */
2332 ret = coda_alloc_context_buffers(ctx, q_data_src);
2333 if (ret < 0)
2334 return ret;
2335
Philipp Zabel918c66f2013-06-27 06:59:01 -03002336 if (ctx->inst_type == CODA_INST_DECODER) {
2337 mutex_lock(&dev->coda_mutex);
2338 ret = coda_start_decoding(ctx);
2339 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002340 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002341 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002342 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002343 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002344 } else {
2345 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002346 }
2347
Philipp Zabel89548442014-07-11 06:36:17 -03002348 ctx->initialized = 1;
2349 return ret;
2350}
2351
2352static int coda_start_encoding(struct coda_ctx *ctx)
2353{
2354 struct coda_dev *dev = ctx->dev;
2355 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2356 struct coda_q_data *q_data_src, *q_data_dst;
2357 u32 bitstream_buf, bitstream_size;
2358 struct vb2_buffer *buf;
2359 int gamma, ret, value;
2360 u32 dst_fourcc;
2361
2362 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2363 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2364 dst_fourcc = q_data_dst->fourcc;
2365
Philipp Zabel14604e32014-07-11 06:36:22 -03002366 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002367 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2368 bitstream_size = q_data_dst->sizeimage;
2369
Javier Martin186b2502012-07-26 05:53:35 -03002370 if (!coda_is_initialized(dev)) {
2371 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2372 return -EFAULT;
2373 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002374
2375 mutex_lock(&dev->coda_mutex);
2376
Javier Martin186b2502012-07-26 05:53:35 -03002377 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002378 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2379 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002380 switch (dev->devtype->product) {
2381 case CODA_DX6:
2382 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2383 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2384 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002385 case CODA_960:
2386 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2387 /* fallthrough */
2388 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002389 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2390 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002391 break;
Javier Martin186b2502012-07-26 05:53:35 -03002392 }
2393
Philipp Zabel89548442014-07-11 06:36:17 -03002394 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2395 value &= ~(1 << 2 | 0x7 << 9);
2396 ctx->frame_mem_ctrl = value;
2397 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2398
Philipp Zabel10436672012-07-02 09:03:55 -03002399 if (dev->devtype->product == CODA_DX6) {
2400 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002401 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002402 }
Javier Martin186b2502012-07-26 05:53:35 -03002403
2404 /* Could set rotation here if needed */
2405 switch (dev->devtype->product) {
2406 case CODA_DX6:
2407 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002408 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002409 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002410 case CODA_7541:
2411 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2412 value = (round_up(q_data_src->width, 16) &
2413 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2414 value |= (round_up(q_data_src->height, 16) &
2415 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2416 break;
2417 }
2418 /* fallthrough */
2419 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002420 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002421 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002422 }
Javier Martin186b2502012-07-26 05:53:35 -03002423 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2424 coda_write(dev, ctx->params.framerate,
2425 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2426
Philipp Zabelb96904e2013-05-23 10:42:58 -03002427 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002428 switch (dst_fourcc) {
2429 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002430 if (dev->devtype->product == CODA_960)
2431 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2432 else
2433 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002434 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2435 break;
2436 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002437 if (dev->devtype->product == CODA_960)
2438 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2439 else
2440 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002441 if (ctx->params.h264_deblk_enabled) {
2442 value = ((ctx->params.h264_deblk_alpha &
2443 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2444 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2445 ((ctx->params.h264_deblk_beta &
2446 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2447 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2448 } else {
2449 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2450 }
2451 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002452 break;
2453 default:
2454 v4l2_err(v4l2_dev,
2455 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002456 ret = -EINVAL;
2457 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002458 }
2459
Philipp Zabelc566c782012-08-08 11:59:38 -03002460 switch (ctx->params.slice_mode) {
2461 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2462 value = 0;
2463 break;
2464 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2465 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2466 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002467 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002468 break;
2469 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2470 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2471 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2472 value |= 1 & CODA_SLICING_MODE_MASK;
2473 break;
2474 }
Javier Martin186b2502012-07-26 05:53:35 -03002475 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002476 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002477 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2478
2479 if (ctx->params.bitrate) {
2480 /* Rate control enabled */
2481 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2482 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002483 if (dev->devtype->product == CODA_960)
2484 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002485 } else {
2486 value = 0;
2487 }
2488 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2489
2490 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002491 coda_write(dev, ctx->params.intra_refresh,
2492 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002493
2494 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2495 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2496
Javier Martin186b2502012-07-26 05:53:35 -03002497
Philipp Zabel89548442014-07-11 06:36:17 -03002498 value = 0;
2499 if (dev->devtype->product == CODA_960)
2500 gamma = CODA9_DEFAULT_GAMMA;
2501 else
2502 gamma = CODA_DEFAULT_GAMMA;
2503 if (gamma > 0) {
2504 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2505 CODA_CMD_ENC_SEQ_RC_GAMMA);
2506 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002507
2508 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2509 coda_write(dev,
2510 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2511 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2512 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2513 }
Philipp Zabel89548442014-07-11 06:36:17 -03002514 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002515 if (ctx->params.h264_max_qp)
2516 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002517 if (CODA_DEFAULT_GAMMA > 0)
2518 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002519 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002520 if (CODA_DEFAULT_GAMMA > 0) {
2521 if (dev->devtype->product == CODA_DX6)
2522 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2523 else
2524 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2525 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002526 if (ctx->params.h264_min_qp)
2527 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2528 if (ctx->params.h264_max_qp)
2529 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002530 }
Javier Martin186b2502012-07-26 05:53:35 -03002531 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2532
Philipp Zabel89548442014-07-11 06:36:17 -03002533 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2534
Philipp Zabelc2d22512013-06-21 03:55:28 -03002535 coda_setup_iram(ctx);
2536
Javier Martin186b2502012-07-26 05:53:35 -03002537 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002538 switch (dev->devtype->product) {
2539 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002540 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002541 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002542 break;
2543 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002544 coda_write(dev, ctx->iram_info.search_ram_paddr,
2545 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2546 coda_write(dev, ctx->iram_info.search_ram_size,
2547 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002548 break;
2549 case CODA_960:
2550 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2551 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002552 }
Javier Martin186b2502012-07-26 05:53:35 -03002553 }
2554
Philipp Zabelfcb62822013-05-23 10:43:00 -03002555 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2556 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002557 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002558 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002559 }
2560
Philipp Zabelfcb62822013-05-23 10:43:00 -03002561 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2562 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2563 ret = -EFAULT;
2564 goto out;
2565 }
Javier Martin186b2502012-07-26 05:53:35 -03002566
Philipp Zabel89548442014-07-11 06:36:17 -03002567 if (dev->devtype->product == CODA_960)
2568 ctx->num_internal_frames = 4;
2569 else
2570 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002571 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002572 if (ret < 0) {
2573 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2574 goto out;
2575 }
Javier Martin186b2502012-07-26 05:53:35 -03002576
Philipp Zabelec25f682012-07-20 08:54:29 -03002577 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002578 coda_write(dev, q_data_src->bytesperline,
2579 CODA_CMD_SET_FRAME_BUF_STRIDE);
2580 if (dev->devtype->product == CODA_7541) {
2581 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002582 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002583 }
Philipp Zabel10436672012-07-02 09:03:55 -03002584 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002585 coda_write(dev, ctx->iram_info.buf_bit_use,
2586 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2587 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2588 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2589 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2590 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2591 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2592 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2593 coda_write(dev, ctx->iram_info.buf_ovl_use,
2594 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002595 if (dev->devtype->product == CODA_960) {
2596 coda_write(dev, ctx->iram_info.buf_btp_use,
2597 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2598
2599 /* FIXME */
2600 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2601 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2602 }
Philipp Zabel10436672012-07-02 09:03:55 -03002603 }
Philipp Zabel89548442014-07-11 06:36:17 -03002604
Philipp Zabelfcb62822013-05-23 10:43:00 -03002605 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2606 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002607 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002608 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002609 }
2610
2611 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002612 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002613 switch (dst_fourcc) {
2614 case V4L2_PIX_FMT_H264:
2615 /*
2616 * Get SPS in the first frame and copy it to an
2617 * intermediate buffer.
2618 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002619 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2620 &ctx->vpu_header[0][0],
2621 &ctx->vpu_header_size[0]);
2622 if (ret < 0)
2623 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002624
2625 /*
2626 * Get PPS in the first frame and copy it to an
2627 * intermediate buffer.
2628 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002629 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2630 &ctx->vpu_header[1][0],
2631 &ctx->vpu_header_size[1]);
2632 if (ret < 0)
2633 goto out;
2634
Javier Martin3f3f5c72012-10-29 05:20:29 -03002635 /*
2636 * Length of H.264 headers is variable and thus it might not be
2637 * aligned for the coda to append the encoded frame. In that is
2638 * the case a filler NAL must be added to header 2.
2639 */
2640 ctx->vpu_header_size[2] = coda_h264_padding(
2641 (ctx->vpu_header_size[0] +
2642 ctx->vpu_header_size[1]),
2643 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002644 break;
2645 case V4L2_PIX_FMT_MPEG4:
2646 /*
2647 * Get VOS in the first frame and copy it to an
2648 * intermediate buffer
2649 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002650 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2651 &ctx->vpu_header[0][0],
2652 &ctx->vpu_header_size[0]);
2653 if (ret < 0)
2654 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002655
Philipp Zabeld35167a2013-05-23 10:42:59 -03002656 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2657 &ctx->vpu_header[1][0],
2658 &ctx->vpu_header_size[1]);
2659 if (ret < 0)
2660 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002661
Philipp Zabeld35167a2013-05-23 10:42:59 -03002662 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2663 &ctx->vpu_header[2][0],
2664 &ctx->vpu_header_size[2]);
2665 if (ret < 0)
2666 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002667 break;
2668 default:
2669 /* No more formats need to save headers at the moment */
2670 break;
2671 }
2672
Philipp Zabeld35167a2013-05-23 10:42:59 -03002673out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002674 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002675 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002676}
2677
Hans Verkuile37559b2014-04-17 02:47:21 -03002678static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002679{
2680 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002681 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002682
2683 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002684 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002685 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002686 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002687
Philipp Zabel89548442014-07-11 06:36:17 -03002688 if (ctx->inst_type == CODA_INST_DECODER &&
2689 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2690 /* if this decoder instance is running, set the stream end flag */
2691 if (dev->devtype->product == CODA_960) {
2692 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2693
2694 val |= CODA_BIT_STREAM_END_FLAG;
2695 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2696 ctx->bit_stream_param = val;
2697 }
2698 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002699 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2700
2701 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002702 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002703 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002704 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002705 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002706
2707 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002708 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002709 }
2710
Philipp Zabel918c66f2013-06-27 06:59:01 -03002711 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002712 struct coda_timestamp *ts;
2713
2714 while (!list_empty(&ctx->timestamp_list)) {
2715 ts = list_first_entry(&ctx->timestamp_list,
2716 struct coda_timestamp, list);
2717 list_del(&ts->list);
2718 kfree(ts);
2719 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002720 kfifo_init(&ctx->bitstream_fifo,
2721 ctx->bitstream.vaddr, ctx->bitstream.size);
2722 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002723 }
Javier Martin186b2502012-07-26 05:53:35 -03002724}
2725
Philipp Zabel121cacf2014-07-18 07:22:42 -03002726static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03002727 .queue_setup = coda_queue_setup,
2728 .buf_prepare = coda_buf_prepare,
2729 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002730 .start_streaming = coda_start_streaming,
2731 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002732 .wait_prepare = vb2_ops_wait_prepare,
2733 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002734};
2735
2736static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2737{
2738 struct coda_ctx *ctx =
2739 container_of(ctrl->handler, struct coda_ctx, ctrls);
2740
2741 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2742 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2743
2744 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002745 case V4L2_CID_HFLIP:
2746 if (ctrl->val)
2747 ctx->params.rot_mode |= CODA_MIR_HOR;
2748 else
2749 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2750 break;
2751 case V4L2_CID_VFLIP:
2752 if (ctrl->val)
2753 ctx->params.rot_mode |= CODA_MIR_VER;
2754 else
2755 ctx->params.rot_mode &= ~CODA_MIR_VER;
2756 break;
Javier Martin186b2502012-07-26 05:53:35 -03002757 case V4L2_CID_MPEG_VIDEO_BITRATE:
2758 ctx->params.bitrate = ctrl->val / 1000;
2759 break;
2760 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2761 ctx->params.gop_size = ctrl->val;
2762 break;
2763 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2764 ctx->params.h264_intra_qp = ctrl->val;
2765 break;
2766 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2767 ctx->params.h264_inter_qp = ctrl->val;
2768 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002769 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2770 ctx->params.h264_min_qp = ctrl->val;
2771 break;
2772 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2773 ctx->params.h264_max_qp = ctrl->val;
2774 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002775 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2776 ctx->params.h264_deblk_alpha = ctrl->val;
2777 break;
2778 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2779 ctx->params.h264_deblk_beta = ctrl->val;
2780 break;
2781 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2782 ctx->params.h264_deblk_enabled = (ctrl->val ==
2783 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2784 break;
Javier Martin186b2502012-07-26 05:53:35 -03002785 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2786 ctx->params.mpeg4_intra_qp = ctrl->val;
2787 break;
2788 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2789 ctx->params.mpeg4_inter_qp = ctrl->val;
2790 break;
2791 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2792 ctx->params.slice_mode = ctrl->val;
2793 break;
2794 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2795 ctx->params.slice_max_mb = ctrl->val;
2796 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002797 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2798 ctx->params.slice_max_bits = ctrl->val * 8;
2799 break;
Javier Martin186b2502012-07-26 05:53:35 -03002800 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2801 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002802 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2803 ctx->params.intra_refresh = ctrl->val;
2804 break;
Javier Martin186b2502012-07-26 05:53:35 -03002805 default:
2806 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2807 "Invalid control, id=%d, val=%d\n",
2808 ctrl->id, ctrl->val);
2809 return -EINVAL;
2810 }
2811
2812 return 0;
2813}
2814
2815static struct v4l2_ctrl_ops coda_ctrl_ops = {
2816 .s_ctrl = coda_s_ctrl,
2817};
2818
2819static int coda_ctrls_setup(struct coda_ctx *ctx)
2820{
2821 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2822
2823 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002824 V4L2_CID_HFLIP, 0, 1, 1, 0);
2825 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2826 V4L2_CID_VFLIP, 0, 1, 1, 0);
2827 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002828 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2829 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2830 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2831 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002832 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002833 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002834 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002835 if (ctx->dev->devtype->product != CODA_960) {
2836 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2837 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2838 }
2839 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2840 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002841 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002842 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2843 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2844 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2845 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2846 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2847 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2848 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2849 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002850 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2851 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2852 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2853 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2854 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002855 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2856 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002857 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2858 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002859 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2860 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002861 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2862 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2863 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2864 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2865 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002866 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2867 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002868
2869 if (ctx->ctrls.error) {
2870 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2871 ctx->ctrls.error);
2872 return -EINVAL;
2873 }
2874
2875 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2876}
2877
Philipp Zabel121cacf2014-07-18 07:22:42 -03002878static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03002879{
Philipp Zabel121cacf2014-07-18 07:22:42 -03002880 vq->drv_priv = ctx;
2881 vq->ops = &coda_qops;
2882 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2883 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2884 vq->lock = &ctx->dev->dev_mutex;
2885
2886 return vb2_queue_init(vq);
2887}
2888
2889static int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2890 struct vb2_queue *dst_vq)
2891{
Javier Martin186b2502012-07-26 05:53:35 -03002892 int ret;
2893
Javier Martin186b2502012-07-26 05:53:35 -03002894 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002895 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002896 src_vq->mem_ops = &vb2_dma_contig_memops;
2897
Philipp Zabel121cacf2014-07-18 07:22:42 -03002898 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002899 if (ret)
2900 return ret;
2901
Javier Martin186b2502012-07-26 05:53:35 -03002902 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002903 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002904 dst_vq->mem_ops = &vb2_dma_contig_memops;
2905
Philipp Zabel121cacf2014-07-18 07:22:42 -03002906 return coda_queue_init(priv, dst_vq);
2907}
2908
2909static int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2910 struct vb2_queue *dst_vq)
2911{
2912 int ret;
2913
2914 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2915 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2916 src_vq->mem_ops = &vb2_dma_contig_memops;
2917
2918 ret = coda_queue_init(priv, src_vq);
2919 if (ret)
2920 return ret;
2921
2922 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2923 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2924 dst_vq->mem_ops = &vb2_dma_contig_memops;
2925
2926 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002927}
2928
Philipp Zabele11f3e62012-07-25 09:16:58 -03002929static int coda_next_free_instance(struct coda_dev *dev)
2930{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002931 int idx = ffz(dev->instance_mask);
2932
2933 if ((idx < 0) ||
2934 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2935 return -EBUSY;
2936
2937 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002938}
2939
Philipp Zabel121cacf2014-07-18 07:22:42 -03002940static int coda_open(struct file *file, enum coda_inst_type inst_type)
Javier Martin186b2502012-07-26 05:53:35 -03002941{
Philipp Zabel121cacf2014-07-18 07:22:42 -03002942 int (*queue_init)(void *priv, struct vb2_queue *src_vq,
2943 struct vb2_queue *dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002944 struct coda_dev *dev = video_drvdata(file);
2945 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002946 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002947 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002948 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002949
Javier Martin186b2502012-07-26 05:53:35 -03002950 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2951 if (!ctx)
2952 return -ENOMEM;
2953
Fabio Estevamf82bc202013-08-21 11:14:16 -03002954 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002955 if (idx < 0) {
2956 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002957 goto err_coda_max;
2958 }
2959 set_bit(idx, &dev->instance_mask);
2960
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002961 name = kasprintf(GFP_KERNEL, "context%d", idx);
2962 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2963 kfree(name);
2964
Philipp Zabel121cacf2014-07-18 07:22:42 -03002965 ctx->inst_type = inst_type;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002966 init_completion(&ctx->completion);
2967 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2968 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002969 v4l2_fh_init(&ctx->fh, video_devdata(file));
2970 file->private_data = &ctx->fh;
2971 v4l2_fh_add(&ctx->fh);
2972 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002973 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002974 switch (dev->devtype->product) {
2975 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002976 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002977 ctx->reg_idx = 0;
2978 break;
2979 default:
2980 ctx->reg_idx = idx;
2981 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002982
Philipp Zabel1e172732014-07-11 06:36:23 -03002983 /* Power up and upload firmware if necessary */
2984 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2985 if (ret < 0) {
2986 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2987 goto err_pm_get;
2988 }
2989
Fabio Estevam79830db2013-08-21 11:14:17 -03002990 ret = clk_prepare_enable(dev->clk_per);
2991 if (ret)
2992 goto err_clk_per;
2993
2994 ret = clk_prepare_enable(dev->clk_ahb);
2995 if (ret)
2996 goto err_clk_ahb;
2997
Javier Martin186b2502012-07-26 05:53:35 -03002998 set_default_params(ctx);
Philipp Zabel121cacf2014-07-18 07:22:42 -03002999 if (inst_type == CODA_INST_ENCODER)
3000 queue_init = coda_encoder_queue_init;
3001 else
3002 queue_init = coda_decoder_queue_init;
3003 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03003004 if (IS_ERR(ctx->fh.m2m_ctx)) {
3005 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003006
3007 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
3008 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003009 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03003010 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003011
Javier Martin186b2502012-07-26 05:53:35 -03003012 ret = coda_ctrls_setup(ctx);
3013 if (ret) {
3014 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03003015 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03003016 }
3017
3018 ctx->fh.ctrl_handler = &ctx->ctrls;
3019
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003020 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
3021 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003022 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003023 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03003024 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03003025 }
3026
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003027 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
3028 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
3029 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
3030 if (!ctx->bitstream.vaddr) {
3031 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
3032 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03003033 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003034 }
3035 kfifo_init(&ctx->bitstream_fifo,
3036 ctx->bitstream.vaddr, ctx->bitstream.size);
3037 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003038 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03003039 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003040
Javier Martin186b2502012-07-26 05:53:35 -03003041 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003042 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003043 coda_unlock(ctx);
3044
Javier Martin186b2502012-07-26 05:53:35 -03003045 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
3046 ctx->idx, ctx);
3047
3048 return 0;
3049
Fabio Estevamf82bc202013-08-21 11:14:16 -03003050err_dma_writecombine:
3051 coda_free_context_buffers(ctx);
3052 if (ctx->dev->devtype->product == CODA_DX6)
3053 coda_free_aux_buf(dev, &ctx->workbuf);
3054 coda_free_aux_buf(dev, &ctx->parabuf);
3055err_dma_alloc:
3056 v4l2_ctrl_handler_free(&ctx->ctrls);
3057err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03003058 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003059err_ctx_init:
3060 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003061err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03003062 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03003063err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03003064 pm_runtime_put_sync(&dev->plat_dev->dev);
3065err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03003066 v4l2_fh_del(&ctx->fh);
3067 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003068 clear_bit(ctx->idx, &dev->instance_mask);
3069err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03003070 kfree(ctx);
3071 return ret;
3072}
3073
Philipp Zabel121cacf2014-07-18 07:22:42 -03003074static int coda_encoder_open(struct file *file)
3075{
3076 return coda_open(file, CODA_INST_ENCODER);
3077}
3078
3079static int coda_decoder_open(struct file *file)
3080{
3081 return coda_open(file, CODA_INST_DECODER);
3082}
3083
Javier Martin186b2502012-07-26 05:53:35 -03003084static int coda_release(struct file *file)
3085{
3086 struct coda_dev *dev = video_drvdata(file);
3087 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
3088
3089 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
3090 ctx);
3091
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003092 debugfs_remove_recursive(ctx->debugfs_entry);
3093
Philipp Zabel918c66f2013-06-27 06:59:01 -03003094 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03003095 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003096
3097 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03003098 if (ctx->initialized) {
3099 queue_work(dev->workqueue, &ctx->seq_end_work);
3100 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003101 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03003102
3103 coda_free_framebuffers(ctx);
3104
Javier Martin186b2502012-07-26 05:53:35 -03003105 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003106 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03003107 coda_unlock(ctx);
3108
Philipp Zabel9082a7c2013-06-21 03:55:31 -03003109 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
3110 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003111 coda_free_context_buffers(ctx);
3112 if (ctx->dev->devtype->product == CODA_DX6)
3113 coda_free_aux_buf(dev, &ctx->workbuf);
3114
3115 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03003116 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03003117 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03003118 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003119 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003120 v4l2_fh_del(&ctx->fh);
3121 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003122 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03003123 kfree(ctx);
3124
3125 return 0;
3126}
3127
Philipp Zabel121cacf2014-07-18 07:22:42 -03003128static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03003129 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03003130 .open = coda_encoder_open,
3131 .release = coda_release,
3132 .poll = v4l2_m2m_fop_poll,
3133 .unlocked_ioctl = video_ioctl2,
3134 .mmap = v4l2_m2m_fop_mmap,
3135};
3136
3137static const struct v4l2_file_operations coda_decoder_fops = {
3138 .owner = THIS_MODULE,
3139 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03003140 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003141 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03003142 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03003143 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03003144};
3145
Philipp Zabel918c66f2013-06-27 06:59:01 -03003146static void coda_finish_decode(struct coda_ctx *ctx)
3147{
3148 struct coda_dev *dev = ctx->dev;
3149 struct coda_q_data *q_data_src;
3150 struct coda_q_data *q_data_dst;
3151 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03003152 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003153 int width, height;
3154 int decoded_idx;
3155 int display_idx;
3156 u32 src_fourcc;
3157 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003158 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003159 u32 val;
3160
Philipp Zabel14604e32014-07-11 06:36:22 -03003161 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003162
3163 /* Update kfifo out pointer from coda bitstream read pointer */
3164 coda_kfifo_sync_from_device(ctx);
3165
3166 /*
3167 * in stream-end mode, the read pointer can overshoot the write pointer
3168 * by up to 512 bytes
3169 */
3170 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
Michael Olbrich390503b2014-07-18 07:22:39 -03003171 if (coda_get_bitstream_payload(ctx) >= CODA_MAX_FRAME_SIZE - 512)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003172 kfifo_init(&ctx->bitstream_fifo,
3173 ctx->bitstream.vaddr, ctx->bitstream.size);
3174 }
3175
3176 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
3177 src_fourcc = q_data_src->fourcc;
3178
3179 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
3180 if (val != 1)
3181 pr_err("DEC_PIC_SUCCESS = %d\n", val);
3182
3183 success = val & 0x1;
3184 if (!success)
3185 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3186
3187 if (src_fourcc == V4L2_PIX_FMT_H264) {
3188 if (val & (1 << 3))
3189 v4l2_err(&dev->v4l2_dev,
3190 "insufficient PS buffer space (%d bytes)\n",
3191 ctx->psbuf.size);
3192 if (val & (1 << 2))
3193 v4l2_err(&dev->v4l2_dev,
3194 "insufficient slice buffer space (%d bytes)\n",
3195 ctx->slicebuf.size);
3196 }
3197
3198 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3199 width = (val >> 16) & 0xffff;
3200 height = val & 0xffff;
3201
3202 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3203
Philipp Zabel52c41672014-07-11 06:36:19 -03003204 /* frame crop information */
3205 if (src_fourcc == V4L2_PIX_FMT_H264) {
3206 u32 left_right;
3207 u32 top_bottom;
3208
3209 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3210 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3211
3212 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3213 /* Keep current crop information */
3214 } else {
3215 struct v4l2_rect *rect = &q_data_dst->rect;
3216
3217 rect->left = left_right >> 16 & 0xffff;
3218 rect->top = top_bottom >> 16 & 0xffff;
3219 rect->width = width - rect->left -
3220 (left_right & 0xffff);
3221 rect->height = height - rect->top -
3222 (top_bottom & 0xffff);
3223 }
3224 } else {
3225 /* no cropping */
3226 }
3227
Philipp Zabel410e5e42014-07-11 06:36:32 -03003228 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3229 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003230 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003231 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003232
3233 if (dev->devtype->product == CODA_7541) {
3234 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3235 if (val == 0) {
3236 /* not enough bitstream data */
3237 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3238 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003239 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003240 return;
3241 }
3242 }
3243
3244 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3245
3246 /*
3247 * The previous display frame was copied out by the rotator,
3248 * now it can be overwritten again
3249 */
3250 if (ctx->display_idx >= 0 &&
3251 ctx->display_idx < ctx->num_internal_frames) {
3252 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3253 coda_write(dev, ctx->frm_dis_flg,
3254 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3255 }
3256
3257 /*
3258 * The index of the last decoded frame, not necessarily in
3259 * display order, and the index of the next display frame.
3260 * The latter could have been decoded in a previous run.
3261 */
3262 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3263 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3264
3265 if (decoded_idx == -1) {
3266 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003267 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3268 ctx->sequence_offset++;
3269 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003270 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003271 } else if (decoded_idx == -2) {
3272 /* no frame was decoded, we still return the remaining buffers */
3273 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3274 v4l2_err(&dev->v4l2_dev,
3275 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003276 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003277 ts = list_first_entry(&ctx->timestamp_list,
3278 struct coda_timestamp, list);
3279 list_del(&ts->list);
3280 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003281 val -= ctx->sequence_offset;
3282 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003283 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003284 "sequence number mismatch (%d(%d) != %d)\n",
3285 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003286 }
3287 ctx->frame_timestamps[decoded_idx] = *ts;
3288 kfree(ts);
3289
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003290 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3291 if (val == 0)
3292 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3293 else if (val == 1)
3294 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3295 else
3296 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003297
3298 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003299 }
3300
3301 if (display_idx == -1) {
3302 /*
3303 * no more frames to be decoded, but there could still
3304 * be rotator output to dequeue
3305 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003306 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003307 } else if (display_idx == -3) {
3308 /* possibly prescan failure */
3309 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3310 v4l2_err(&dev->v4l2_dev,
3311 "presentation frame index out of range: %d\n",
3312 display_idx);
3313 }
3314
3315 /* If a frame was copied out, return it */
3316 if (ctx->display_idx >= 0 &&
3317 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003318 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003319 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3320
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003321 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3322 V4L2_BUF_FLAG_PFRAME |
3323 V4L2_BUF_FLAG_BFRAME);
3324 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003325 ts = &ctx->frame_timestamps[ctx->display_idx];
3326 dst_buf->v4l2_buf.timecode = ts->timecode;
3327 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003328
Philipp Zabel918c66f2013-06-27 06:59:01 -03003329 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3330
Philipp Zabel410e5e42014-07-11 06:36:32 -03003331 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3332 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003333
3334 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3335 "job finished: decoding frame (%d) (%s)\n",
3336 dst_buf->v4l2_buf.sequence,
3337 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3338 "KEYFRAME" : "PFRAME");
3339 } else {
3340 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3341 "job finished: no frame decoded\n");
3342 }
3343
3344 /* The rotator will copy the current display frame next time */
3345 ctx->display_idx = display_idx;
3346}
3347
3348static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003349{
Philipp Zabelec25f682012-07-20 08:54:29 -03003350 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003351 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003352 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003353
Philipp Zabel14604e32014-07-11 06:36:22 -03003354 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3355 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003356
3357 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003358 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003359 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3360
Javier Martin186b2502012-07-26 05:53:35 -03003361 /* Calculate bytesused field */
3362 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003363 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3364 ctx->vpu_header_size[0] +
3365 ctx->vpu_header_size[1] +
3366 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003367 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003368 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003369 }
3370
3371 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3372 wr_ptr - start_ptr);
3373
3374 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3375 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3376
Philipp Zabel51660282013-09-30 10:34:49 -03003377 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003378 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3379 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3380 } else {
3381 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3382 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3383 }
3384
Kamil Debskiccd571c2013-04-24 10:38:24 -03003385 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003386 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3387 dst_buf->v4l2_buf.flags |=
3388 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003389 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3390
Philipp Zabelec25f682012-07-20 08:54:29 -03003391 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003392
Philipp Zabel14604e32014-07-11 06:36:22 -03003393 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003394 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3395
3396 ctx->gopcounter--;
3397 if (ctx->gopcounter < 0)
3398 ctx->gopcounter = ctx->params.gop_size - 1;
3399
3400 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3401 "job finished: encoding frame (%d) (%s)\n",
3402 dst_buf->v4l2_buf.sequence,
3403 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3404 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003405}
3406
3407static irqreturn_t coda_irq_handler(int irq, void *data)
3408{
3409 struct coda_dev *dev = data;
3410 struct coda_ctx *ctx;
3411
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003412 /* read status register to attend the IRQ */
3413 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3414 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3415 CODA_REG_BIT_INT_CLEAR);
3416
3417 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3418 if (ctx == NULL) {
3419 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3420 mutex_unlock(&dev->coda_mutex);
3421 return IRQ_HANDLED;
3422 }
3423
3424 if (ctx->aborting) {
3425 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3426 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003427 }
3428
3429 if (coda_isbusy(ctx->dev)) {
3430 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3431 "coda is still busy!!!!\n");
3432 return IRQ_NONE;
3433 }
3434
Philipp Zabel32b6f202014-07-11 06:36:20 -03003435 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003436
3437 return IRQ_HANDLED;
3438}
3439
3440static u32 coda_supported_firmwares[] = {
3441 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003442 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003443 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003444};
3445
3446static bool coda_firmware_supported(u32 vernum)
3447{
3448 int i;
3449
3450 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3451 if (vernum == coda_supported_firmwares[i])
3452 return true;
3453 return false;
3454}
3455
Philipp Zabel87048bb2012-07-02 07:03:43 -03003456static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003457{
Javier Martin186b2502012-07-26 05:53:35 -03003458 u32 data;
3459 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003460 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003461
Fabio Estevam79830db2013-08-21 11:14:17 -03003462 ret = clk_prepare_enable(dev->clk_per);
3463 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003464 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003465
3466 ret = clk_prepare_enable(dev->clk_ahb);
3467 if (ret)
3468 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003469
Philipp Zabel8f452842014-07-11 06:36:35 -03003470 if (dev->rstc)
3471 reset_control_reset(dev->rstc);
3472
Javier Martin186b2502012-07-26 05:53:35 -03003473 /*
3474 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003475 * The 16-bit chars in the code buffer are in memory access
3476 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003477 * Data in this SRAM survives a reboot.
3478 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003479 p = (u16 *)dev->codebuf.vaddr;
3480 if (dev->devtype->product == CODA_DX6) {
3481 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3482 data = CODA_DOWN_ADDRESS_SET(i) |
3483 CODA_DOWN_DATA_SET(p[i ^ 1]);
3484 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3485 }
3486 } else {
3487 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3488 data = CODA_DOWN_ADDRESS_SET(i) |
3489 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3490 3 - (i % 4)]);
3491 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3492 }
Javier Martin186b2502012-07-26 05:53:35 -03003493 }
Javier Martin186b2502012-07-26 05:53:35 -03003494
Philipp Zabel9acf7692013-05-23 10:42:56 -03003495 /* Clear registers */
3496 for (i = 0; i < 64; i++)
3497 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3498
Javier Martin186b2502012-07-26 05:53:35 -03003499 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003500 if (dev->devtype->product == CODA_960 ||
3501 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003502 coda_write(dev, dev->tempbuf.paddr,
3503 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003504 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003505 } else {
3506 coda_write(dev, dev->workbuf.paddr,
3507 CODA_REG_BIT_WORK_BUF_ADDR);
3508 }
Javier Martin186b2502012-07-26 05:53:35 -03003509 coda_write(dev, dev->codebuf.paddr,
3510 CODA_REG_BIT_CODE_BUF_ADDR);
3511 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3512
3513 /* Set default values */
3514 switch (dev->devtype->product) {
3515 case CODA_DX6:
3516 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3517 break;
3518 default:
3519 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3520 }
Philipp Zabel89548442014-07-11 06:36:17 -03003521 if (dev->devtype->product == CODA_960)
3522 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3523 else
3524 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003525
3526 if (dev->devtype->product != CODA_DX6)
3527 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3528
Javier Martin186b2502012-07-26 05:53:35 -03003529 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3530 CODA_REG_BIT_INT_ENABLE);
3531
3532 /* Reset VPU and start processor */
3533 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3534 data |= CODA_REG_RESET_ENABLE;
3535 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3536 udelay(10);
3537 data &= ~CODA_REG_RESET_ENABLE;
3538 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3539 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3540
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003541 clk_disable_unprepare(dev->clk_ahb);
3542 clk_disable_unprepare(dev->clk_per);
3543
3544 return 0;
3545
3546err_clk_ahb:
3547 clk_disable_unprepare(dev->clk_per);
3548err_clk_per:
3549 return ret;
3550}
3551
3552static int coda_check_firmware(struct coda_dev *dev)
3553{
3554 u16 product, major, minor, release;
3555 u32 data;
3556 int ret;
3557
3558 ret = clk_prepare_enable(dev->clk_per);
3559 if (ret)
3560 goto err_clk_per;
3561
3562 ret = clk_prepare_enable(dev->clk_ahb);
3563 if (ret)
3564 goto err_clk_ahb;
3565
Javier Martin186b2502012-07-26 05:53:35 -03003566 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3567 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3568 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3569 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3570 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3571 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003572 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003573 ret = -EIO;
3574 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003575 }
3576
Philipp Zabel89548442014-07-11 06:36:17 -03003577 if (dev->devtype->product == CODA_960) {
3578 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3579 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3580 data);
3581 }
3582
Javier Martin186b2502012-07-26 05:53:35 -03003583 /* Check we are compatible with the loaded firmware */
3584 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3585 product = CODA_FIRMWARE_PRODUCT(data);
3586 major = CODA_FIRMWARE_MAJOR(data);
3587 minor = CODA_FIRMWARE_MINOR(data);
3588 release = CODA_FIRMWARE_RELEASE(data);
3589
3590 clk_disable_unprepare(dev->clk_per);
3591 clk_disable_unprepare(dev->clk_ahb);
3592
3593 if (product != dev->devtype->product) {
3594 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3595 " Version: %u.%u.%u\n",
3596 coda_product_name(dev->devtype->product),
3597 coda_product_name(product), major, minor, release);
3598 return -EINVAL;
3599 }
3600
3601 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3602 coda_product_name(product));
3603
3604 if (coda_firmware_supported(data)) {
3605 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3606 major, minor, release);
3607 } else {
3608 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3609 "%u.%u.%u\n", major, minor, release);
3610 }
3611
3612 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003613
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003614err_run_cmd:
3615 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003616err_clk_ahb:
3617 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003618err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003619 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003620}
3621
Philipp Zabel121cacf2014-07-18 07:22:42 -03003622static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
3623{
3624 vfd->release = video_device_release_empty,
3625 vfd->lock = &dev->dev_mutex;
3626 vfd->v4l2_dev = &dev->v4l2_dev;
3627 vfd->vfl_dir = VFL_DIR_M2M;
3628 video_set_drvdata(vfd, dev);
3629
3630 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
3631}
3632
Javier Martin186b2502012-07-26 05:53:35 -03003633static void coda_fw_callback(const struct firmware *fw, void *context)
3634{
3635 struct coda_dev *dev = context;
3636 struct platform_device *pdev = dev->plat_dev;
3637 int ret;
3638
3639 if (!fw) {
3640 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3641 return;
3642 }
3643
3644 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003645 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3646 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003647 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003648 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3649 return;
3650 }
3651
Philipp Zabel87048bb2012-07-02 07:03:43 -03003652 /* Copy the whole firmware image to the code buffer */
3653 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3654 release_firmware(fw);
3655
Philipp Zabel1e172732014-07-11 06:36:23 -03003656 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3657 /*
3658 * Enabling power temporarily will cause coda_hw_init to be
3659 * called via coda_runtime_resume by the pm domain.
3660 */
3661 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3662 if (ret < 0) {
3663 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3664 ret);
3665 return;
3666 }
3667
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003668 ret = coda_check_firmware(dev);
3669 if (ret < 0)
3670 return;
3671
Philipp Zabel1e172732014-07-11 06:36:23 -03003672 pm_runtime_put_sync(&dev->plat_dev->dev);
3673 } else {
3674 /*
3675 * If runtime pm is disabled or pm_domain is not set,
3676 * initialize once manually.
3677 */
3678 ret = coda_hw_init(dev);
3679 if (ret < 0) {
3680 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3681 return;
3682 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003683
3684 ret = coda_check_firmware(dev);
3685 if (ret < 0)
3686 return;
Javier Martin186b2502012-07-26 05:53:35 -03003687 }
3688
Javier Martin186b2502012-07-26 05:53:35 -03003689 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3690 if (IS_ERR(dev->alloc_ctx)) {
3691 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3692 return;
3693 }
3694
3695 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3696 if (IS_ERR(dev->m2m_dev)) {
3697 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3698 goto rel_ctx;
3699 }
3700
Philipp Zabel121cacf2014-07-18 07:22:42 -03003701 dev->vfd[0].fops = &coda_encoder_fops,
3702 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
3703 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
3704 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03003705 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03003706 v4l2_err(&dev->v4l2_dev,
3707 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03003708 goto rel_m2m;
3709 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03003710
3711 dev->vfd[1].fops = &coda_decoder_fops,
3712 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
3713 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
3714 ret = coda_register_device(dev, &dev->vfd[1]);
3715 if (ret) {
3716 v4l2_err(&dev->v4l2_dev,
3717 "Failed to register decoder video device\n");
3718 goto rel_m2m;
3719 }
3720
3721 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
3722 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03003723
3724 return;
3725
3726rel_m2m:
3727 v4l2_m2m_release(dev->m2m_dev);
3728rel_ctx:
3729 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3730}
3731
3732static int coda_firmware_request(struct coda_dev *dev)
3733{
3734 char *fw = dev->devtype->firmware;
3735
3736 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3737 coda_product_name(dev->devtype->product));
3738
3739 return request_firmware_nowait(THIS_MODULE, true,
3740 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3741}
3742
3743enum coda_platform {
3744 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003745 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003746 CODA_IMX6Q,
3747 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003748};
3749
Emil Goodec06d8752012-08-14 17:44:42 -03003750static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003751 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003752 .firmware = "v4l-codadx6-imx27.bin",
3753 .product = CODA_DX6,
3754 .codecs = codadx6_codecs,
3755 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3756 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003757 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03003758 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003759 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003760 .firmware = "v4l-coda7541-imx53.bin",
3761 .product = CODA_7541,
3762 .codecs = coda7_codecs,
3763 .num_codecs = ARRAY_SIZE(coda7_codecs),
3764 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003765 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003766 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003767 },
Philipp Zabel89548442014-07-11 06:36:17 -03003768 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003769 .firmware = "v4l-coda960-imx6q.bin",
3770 .product = CODA_960,
3771 .codecs = coda9_codecs,
3772 .num_codecs = ARRAY_SIZE(coda9_codecs),
3773 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003774 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003775 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03003776 },
3777 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003778 .firmware = "v4l-coda960-imx6dl.bin",
3779 .product = CODA_960,
3780 .codecs = coda9_codecs,
3781 .num_codecs = ARRAY_SIZE(coda9_codecs),
3782 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003783 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003784 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03003785 },
Javier Martin186b2502012-07-26 05:53:35 -03003786};
3787
3788static struct platform_device_id coda_platform_ids[] = {
3789 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003790 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003791 { /* sentinel */ }
3792};
3793MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3794
3795#ifdef CONFIG_OF
3796static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003797 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003798 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003799 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3800 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003801 { /* sentinel */ }
3802};
3803MODULE_DEVICE_TABLE(of, coda_dt_ids);
3804#endif
3805
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003806static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003807{
3808 const struct of_device_id *of_id =
3809 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3810 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003811 struct coda_platform_data *pdata = pdev->dev.platform_data;
3812 struct device_node *np = pdev->dev.of_node;
3813 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003814 struct coda_dev *dev;
3815 struct resource *res;
3816 int ret, irq;
3817
3818 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3819 if (!dev) {
3820 dev_err(&pdev->dev, "Not enough memory for %s\n",
3821 CODA_NAME);
3822 return -ENOMEM;
3823 }
3824
3825 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003826 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003827
3828 dev->plat_dev = pdev;
3829 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3830 if (IS_ERR(dev->clk_per)) {
3831 dev_err(&pdev->dev, "Could not get per clock\n");
3832 return PTR_ERR(dev->clk_per);
3833 }
3834
3835 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3836 if (IS_ERR(dev->clk_ahb)) {
3837 dev_err(&pdev->dev, "Could not get ahb clock\n");
3838 return PTR_ERR(dev->clk_ahb);
3839 }
3840
3841 /* Get memory for physical registers */
3842 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003843 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3844 if (IS_ERR(dev->regs_base))
3845 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003846
3847 /* IRQ */
3848 irq = platform_get_irq(pdev, 0);
3849 if (irq < 0) {
3850 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03003851 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03003852 }
3853
Fabio Estevam08c8d142014-06-04 15:46:24 -03003854 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
3855 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
3856 if (ret < 0) {
3857 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3858 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003859 }
3860
Philipp Zabelee27aa92014-07-24 16:50:03 -03003861 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03003862 if (IS_ERR(dev->rstc)) {
3863 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03003864 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03003865 dev->rstc = NULL;
3866 } else {
3867 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3868 return ret;
3869 }
3870 }
3871
Philipp Zabel657eee72013-04-29 16:17:14 -07003872 /* Get IRAM pool from device tree or platform data */
3873 pool = of_get_named_gen_pool(np, "iram", 0);
3874 if (!pool && pdata)
3875 pool = dev_get_gen_pool(pdata->iram_dev);
3876 if (!pool) {
3877 dev_err(&pdev->dev, "iram pool not available\n");
3878 return -ENOMEM;
3879 }
3880 dev->iram_pool = pool;
3881
Javier Martin186b2502012-07-26 05:53:35 -03003882 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3883 if (ret)
3884 return ret;
3885
3886 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003887 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003888
3889 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3890
3891 if (of_id) {
3892 dev->devtype = of_id->data;
3893 } else if (pdev_id) {
3894 dev->devtype = &coda_devdata[pdev_id->driver_data];
3895 } else {
3896 v4l2_device_unregister(&dev->v4l2_dev);
3897 return -EINVAL;
3898 }
3899
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003900 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3901 if (!dev->debugfs_root)
3902 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3903
Javier Martin186b2502012-07-26 05:53:35 -03003904 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003905 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003906 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003907 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003908 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003909 if (ret < 0) {
3910 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3911 v4l2_device_unregister(&dev->v4l2_dev);
3912 return ret;
3913 }
Javier Martin186b2502012-07-26 05:53:35 -03003914 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003915
3916 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003917 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003918 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003919 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003920 if (ret < 0) {
3921 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3922 v4l2_device_unregister(&dev->v4l2_dev);
3923 return ret;
3924 }
Javier Martin186b2502012-07-26 05:53:35 -03003925 }
3926
Philipp Zabel401e9722014-07-11 06:36:43 -03003927 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003928 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3929 &dev->iram.paddr);
3930 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003931 dev_err(&pdev->dev, "unable to alloc iram\n");
3932 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003933 }
3934
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003935 dev->iram.blob.data = dev->iram.vaddr;
3936 dev->iram.blob.size = dev->iram.size;
3937 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
3938 &dev->iram.blob);
3939
Philipp Zabel32b6f202014-07-11 06:36:20 -03003940 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3941 if (!dev->workqueue) {
3942 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3943 return -ENOMEM;
3944 }
3945
Javier Martin186b2502012-07-26 05:53:35 -03003946 platform_set_drvdata(pdev, dev);
3947
Philipp Zabel1e172732014-07-11 06:36:23 -03003948 pm_runtime_enable(&pdev->dev);
3949
Javier Martin186b2502012-07-26 05:53:35 -03003950 return coda_firmware_request(dev);
3951}
3952
3953static int coda_remove(struct platform_device *pdev)
3954{
3955 struct coda_dev *dev = platform_get_drvdata(pdev);
3956
Philipp Zabel121cacf2014-07-18 07:22:42 -03003957 video_unregister_device(&dev->vfd[0]);
3958 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03003959 if (dev->m2m_dev)
3960 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003961 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003962 if (dev->alloc_ctx)
3963 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3964 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003965 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003966 if (dev->iram.vaddr)
3967 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3968 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003969 coda_free_aux_buf(dev, &dev->codebuf);
3970 coda_free_aux_buf(dev, &dev->tempbuf);
3971 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003972 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03003973 return 0;
3974}
3975
Philipp Zabel1e172732014-07-11 06:36:23 -03003976#ifdef CONFIG_PM_RUNTIME
3977static int coda_runtime_resume(struct device *dev)
3978{
3979 struct coda_dev *cdev = dev_get_drvdata(dev);
3980 int ret = 0;
3981
Philipp Zabel65919e62014-07-18 07:22:36 -03003982 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03003983 ret = coda_hw_init(cdev);
3984 if (ret)
3985 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3986 }
3987
3988 return ret;
3989}
3990#endif
3991
3992static const struct dev_pm_ops coda_pm_ops = {
3993 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3994};
3995
Javier Martin186b2502012-07-26 05:53:35 -03003996static struct platform_driver coda_driver = {
3997 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003998 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003999 .driver = {
4000 .name = CODA_NAME,
4001 .owner = THIS_MODULE,
4002 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03004003 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03004004 },
4005 .id_table = coda_platform_ids,
4006};
4007
4008module_platform_driver(coda_driver);
4009
4010MODULE_LICENSE("GPL");
4011MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
4012MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");