blob: c4a6cabe123e81266a4d9bfc25dbb81ef8c9485d [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070017#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030018#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030021#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030022#include <linux/module.h>
23#include <linux/of_device.h>
24#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030025#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030026#include <linux/slab.h>
27#include <linux/videodev2.h>
28#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070029#include <linux/platform_data/coda.h>
Javier Martin186b2502012-07-26 05:53:35 -030030
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030033#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030034#include <media/v4l2-ioctl.h>
35#include <media/v4l2-mem2mem.h>
36#include <media/videobuf2-core.h>
37#include <media/videobuf2-dma-contig.h>
38
39#include "coda.h"
40
41#define CODA_NAME "coda"
42
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030043#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030044
45#define CODA_FMO_BUF_SIZE 32
46#define CODADX6_WORK_BUF_SIZE (288 * 1024 + CODA_FMO_BUF_SIZE * 8 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030047#define CODA7_WORK_BUF_SIZE (128 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030048#define CODA9_WORK_BUF_SIZE (80 * 1024)
Philipp Zabel5677e3b2013-06-21 03:55:30 -030049#define CODA7_TEMP_BUF_SIZE (304 * 1024)
Philipp Zabel89548442014-07-11 06:36:17 -030050#define CODA9_TEMP_BUF_SIZE (204 * 1024)
Javier Martin186b2502012-07-26 05:53:35 -030051#define CODA_PARA_BUF_SIZE (10 * 1024)
52#define CODA_ISRAM_SIZE (2048 * 2)
Philipp Zabel657eee72013-04-29 16:17:14 -070053#define CODADX6_IRAM_SIZE 0xb000
Philipp Zabel918c66f2013-06-27 06:59:01 -030054#define CODA7_IRAM_SIZE 0x14000
Philipp Zabel89548442014-07-11 06:36:17 -030055#define CODA9_IRAM_SIZE 0x21000
Javier Martin186b2502012-07-26 05:53:35 -030056
Philipp Zabel918c66f2013-06-27 06:59:01 -030057#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030058#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030059
60#define CODA_MAX_FRAMEBUFFERS 8
Javier Martin186b2502012-07-26 05:53:35 -030061
Philipp Zabelb96904e2013-05-23 10:42:58 -030062#define CODA_MAX_FRAME_SIZE 0x100000
Javier Martin186b2502012-07-26 05:53:35 -030063#define FMO_SLICE_SAVE_BUF_SIZE (32)
64#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030065#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030066
67#define MIN_W 176
68#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030069
70#define S_ALIGN 1 /* multiple of 2 */
71#define W_ALIGN 1 /* multiple of 2 */
72#define H_ALIGN 1 /* multiple of 2 */
73
74#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
75
76static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030077module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030078MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
79
80enum {
81 V4L2_M2M_SRC = 0,
82 V4L2_M2M_DST = 1,
83};
84
Javier Martin186b2502012-07-26 05:53:35 -030085enum coda_inst_type {
86 CODA_INST_ENCODER,
87 CODA_INST_DECODER,
88};
89
90enum coda_product {
91 CODA_DX6 = 0xf001,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -030092 CODA_7541 = 0xf012,
Philipp Zabel89548442014-07-11 06:36:17 -030093 CODA_960 = 0xf020,
Javier Martin186b2502012-07-26 05:53:35 -030094};
95
96struct coda_fmt {
97 char *name;
98 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030099};
100
101struct coda_codec {
102 u32 mode;
103 u32 src_fourcc;
104 u32 dst_fourcc;
105 u32 max_w;
106 u32 max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300107};
108
109struct coda_devtype {
110 char *firmware;
111 enum coda_product product;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300112 struct coda_codec *codecs;
113 unsigned int num_codecs;
Javier Martin186b2502012-07-26 05:53:35 -0300114 size_t workbuf_size;
115};
116
117/* Per-queue, driver-specific private data */
118struct coda_q_data {
119 unsigned int width;
120 unsigned int height;
121 unsigned int sizeimage;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300122 unsigned int fourcc;
Philipp Zabel52c41672014-07-11 06:36:19 -0300123 struct v4l2_rect rect;
Javier Martin186b2502012-07-26 05:53:35 -0300124};
125
126struct coda_aux_buf {
127 void *vaddr;
128 dma_addr_t paddr;
129 u32 size;
130};
131
132struct coda_dev {
133 struct v4l2_device v4l2_dev;
134 struct video_device vfd;
135 struct platform_device *plat_dev;
Emil Goodec06d8752012-08-14 17:44:42 -0300136 const struct coda_devtype *devtype;
Javier Martin186b2502012-07-26 05:53:35 -0300137
138 void __iomem *regs_base;
139 struct clk *clk_per;
140 struct clk *clk_ahb;
141
142 struct coda_aux_buf codebuf;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300143 struct coda_aux_buf tempbuf;
Javier Martin186b2502012-07-26 05:53:35 -0300144 struct coda_aux_buf workbuf;
Philipp Zabel657eee72013-04-29 16:17:14 -0700145 struct gen_pool *iram_pool;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300146 struct coda_aux_buf iram;
Javier Martin186b2502012-07-26 05:53:35 -0300147
148 spinlock_t irqlock;
149 struct mutex dev_mutex;
Philipp Zabelfcb62822013-05-23 10:43:00 -0300150 struct mutex coda_mutex;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300151 struct workqueue_struct *workqueue;
Javier Martin186b2502012-07-26 05:53:35 -0300152 struct v4l2_m2m_dev *m2m_dev;
153 struct vb2_alloc_ctx *alloc_ctx;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300154 struct list_head instances;
155 unsigned long instance_mask;
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;
Javier Martin186b2502012-07-26 05:53:35 -0300164 u8 mpeg4_intra_qp;
165 u8 mpeg4_inter_qp;
166 u8 gop_size;
167 int codec_mode;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300168 int codec_mode_aux;
Javier Martin186b2502012-07-26 05:53:35 -0300169 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
170 u32 framerate;
171 u16 bitrate;
Philipp Zabelc566c782012-08-08 11:59:38 -0300172 u32 slice_max_bits;
Javier Martin186b2502012-07-26 05:53:35 -0300173 u32 slice_max_mb;
174};
175
Philipp Zabelc2d22512013-06-21 03:55:28 -0300176struct coda_iram_info {
177 u32 axi_sram_use;
178 phys_addr_t buf_bit_use;
179 phys_addr_t buf_ip_ac_dc_use;
180 phys_addr_t buf_dbk_y_use;
181 phys_addr_t buf_dbk_c_use;
182 phys_addr_t buf_ovl_use;
183 phys_addr_t buf_btp_use;
184 phys_addr_t search_ram_paddr;
185 int search_ram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -0300186 int remaining;
187 phys_addr_t next_paddr;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300188};
189
Philipp Zabel89548442014-07-11 06:36:17 -0300190struct gdi_tiled_map {
191 int xy2ca_map[16];
192 int xy2ba_map[16];
193 int xy2ra_map[16];
194 int rbc2axi_map[32];
195 int xy2rbc_config;
196 int map_type;
197#define GDI_LINEAR_FRAME_MAP 0
198};
199
Javier Martin186b2502012-07-26 05:53:35 -0300200struct coda_ctx {
201 struct coda_dev *dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300202 struct mutex buffer_mutex;
Philipp Zabele11f3e62012-07-25 09:16:58 -0300203 struct list_head list;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300204 struct work_struct pic_run_work;
205 struct work_struct seq_end_work;
206 struct completion completion;
Javier Martin186b2502012-07-26 05:53:35 -0300207 int aborting;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300208 int initialized;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300209 int streamon_out;
210 int streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300211 u32 isequence;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300212 u32 qsequence;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300213 u32 osequence;
Javier Martin186b2502012-07-26 05:53:35 -0300214 struct coda_q_data q_data[2];
215 enum coda_inst_type inst_type;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300216 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300217 enum v4l2_colorspace colorspace;
218 struct coda_params params;
Javier Martin186b2502012-07-26 05:53:35 -0300219 struct v4l2_ctrl_handler ctrls;
220 struct v4l2_fh fh;
Javier Martin186b2502012-07-26 05:53:35 -0300221 int gopcounter;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300222 int runcounter;
Javier Martin186b2502012-07-26 05:53:35 -0300223 char vpu_header[3][64];
224 int vpu_header_size[3];
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300225 struct kfifo bitstream_fifo;
226 struct mutex bitstream_mutex;
227 struct coda_aux_buf bitstream;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300228 bool prescan_failed;
Javier Martin186b2502012-07-26 05:53:35 -0300229 struct coda_aux_buf parabuf;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300230 struct coda_aux_buf psbuf;
231 struct coda_aux_buf slicebuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300232 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel1a8b3812014-07-11 06:36:12 -0300233 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300234 struct coda_aux_buf workbuf;
Philipp Zabelec25f682012-07-20 08:54:29 -0300235 int num_internal_frames;
Javier Martin186b2502012-07-26 05:53:35 -0300236 int idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300237 int reg_idx;
Philipp Zabelc2d22512013-06-21 03:55:28 -0300238 struct coda_iram_info iram_info;
Philipp Zabel89548442014-07-11 06:36:17 -0300239 struct gdi_tiled_map tiled_map;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300240 u32 bit_stream_param;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300241 u32 frm_dis_flg;
Philipp Zabel89548442014-07-11 06:36:17 -0300242 u32 frame_mem_ctrl;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300243 int display_idx;
Javier Martin186b2502012-07-26 05:53:35 -0300244};
245
Javier Martin832fbb52012-10-29 08:34:59 -0300246static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
247 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
248static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -0300249
Javier Martin186b2502012-07-26 05:53:35 -0300250static inline void coda_write(struct coda_dev *dev, u32 data, u32 reg)
251{
252 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
253 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
254 writel(data, dev->regs_base + reg);
255}
256
257static inline unsigned int coda_read(struct coda_dev *dev, u32 reg)
258{
259 u32 data;
260 data = readl(dev->regs_base + reg);
261 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
262 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
263 return data;
264}
265
266static inline unsigned long coda_isbusy(struct coda_dev *dev)
267{
268 return coda_read(dev, CODA_REG_BIT_BUSY);
269}
270
271static inline int coda_is_initialized(struct coda_dev *dev)
272{
273 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
274}
275
276static int coda_wait_timeout(struct coda_dev *dev)
277{
278 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
279
280 while (coda_isbusy(dev)) {
281 if (time_after(jiffies, timeout))
282 return -ETIMEDOUT;
283 }
284 return 0;
285}
286
287static void coda_command_async(struct coda_ctx *ctx, int cmd)
288{
289 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300290
Philipp Zabel89548442014-07-11 06:36:17 -0300291 if (dev->devtype->product == CODA_960 ||
292 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300293 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300294 coda_write(dev, ctx->bit_stream_param,
295 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300296 coda_write(dev, ctx->frm_dis_flg,
297 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300298 coda_write(dev, ctx->frame_mem_ctrl,
299 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300300 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
301 }
302
Philipp Zabel89548442014-07-11 06:36:17 -0300303 if (dev->devtype->product == CODA_960) {
304 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
305 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
306 }
307
Javier Martin186b2502012-07-26 05:53:35 -0300308 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
309
310 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
311 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300312 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
313
Javier Martin186b2502012-07-26 05:53:35 -0300314 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
315}
316
317static int coda_command_sync(struct coda_ctx *ctx, int cmd)
318{
319 struct coda_dev *dev = ctx->dev;
320
321 coda_command_async(ctx, cmd);
322 return coda_wait_timeout(dev);
323}
324
325static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
326 enum v4l2_buf_type type)
327{
328 switch (type) {
329 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
330 return &(ctx->q_data[V4L2_M2M_SRC]);
331 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
332 return &(ctx->q_data[V4L2_M2M_DST]);
333 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300334 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300335 }
Javier Martin186b2502012-07-26 05:53:35 -0300336}
337
338/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300339 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300340 */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300341static struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300342 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300343 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300344 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300345 },
346 {
347 .name = "YUV 4:2:0 Planar, YCrCb",
348 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300349 },
350 {
351 .name = "H264 Encoded Stream",
352 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300353 },
354 {
355 .name = "MPEG4 Encoded Stream",
356 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300357 },
358};
359
Philipp Zabelb96904e2013-05-23 10:42:58 -0300360#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
361 { mode, src_fourcc, dst_fourcc, max_w, max_h }
362
363/*
364 * Arrays of codecs supported by each given version of Coda:
365 * i.MX27 -> codadx6
366 * i.MX5x -> coda7
367 * i.MX6 -> coda960
368 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
369 */
370static struct coda_codec codadx6_codecs[] = {
371 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
372 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300373};
374
Philipp Zabelb96904e2013-05-23 10:42:58 -0300375static struct coda_codec coda7_codecs[] = {
376 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
377 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300378 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
379 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300380};
381
Philipp Zabel89548442014-07-11 06:36:17 -0300382static struct coda_codec coda9_codecs[] = {
383 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
384 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
385 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
386 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
387};
388
Philipp Zabelb96904e2013-05-23 10:42:58 -0300389static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300390{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300391 switch (fourcc) {
392 case V4L2_PIX_FMT_YUV420:
393 case V4L2_PIX_FMT_YVU420:
394 return true;
395 default:
396 return false;
397 }
398}
Javier Martin186b2502012-07-26 05:53:35 -0300399
Philipp Zabelb96904e2013-05-23 10:42:58 -0300400/*
401 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
402 * tables.
403 */
404static u32 coda_format_normalize_yuv(u32 fourcc)
405{
406 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
407}
408
409static struct coda_codec *coda_find_codec(struct coda_dev *dev, int src_fourcc,
410 int dst_fourcc)
411{
412 struct coda_codec *codecs = dev->devtype->codecs;
413 int num_codecs = dev->devtype->num_codecs;
414 int k;
415
416 src_fourcc = coda_format_normalize_yuv(src_fourcc);
417 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
418 if (src_fourcc == dst_fourcc)
419 return NULL;
420
421 for (k = 0; k < num_codecs; k++) {
422 if (codecs[k].src_fourcc == src_fourcc &&
423 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300424 break;
425 }
426
Philipp Zabelb96904e2013-05-23 10:42:58 -0300427 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300428 return NULL;
429
Philipp Zabelb96904e2013-05-23 10:42:58 -0300430 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300431}
432
Philipp Zabel57625592013-09-30 10:34:51 -0300433static void coda_get_max_dimensions(struct coda_dev *dev,
434 struct coda_codec *codec,
435 int *max_w, int *max_h)
436{
437 struct coda_codec *codecs = dev->devtype->codecs;
438 int num_codecs = dev->devtype->num_codecs;
439 unsigned int w, h;
440 int k;
441
442 if (codec) {
443 w = codec->max_w;
444 h = codec->max_h;
445 } else {
446 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
447 w = max(w, codecs[k].max_w);
448 h = max(h, codecs[k].max_h);
449 }
450 }
451
452 if (max_w)
453 *max_w = w;
454 if (max_h)
455 *max_h = h;
456}
457
Philipp Zabel927933f2013-09-30 10:34:48 -0300458static char *coda_product_name(int product)
459{
460 static char buf[9];
461
462 switch (product) {
463 case CODA_DX6:
464 return "CodaDx6";
465 case CODA_7541:
466 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300467 case CODA_960:
468 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300469 default:
470 snprintf(buf, sizeof(buf), "(0x%04x)", product);
471 return buf;
472 }
473}
474
Javier Martin186b2502012-07-26 05:53:35 -0300475/*
476 * V4L2 ioctl() operations.
477 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300478static int coda_querycap(struct file *file, void *priv,
479 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300480{
Philipp Zabel927933f2013-09-30 10:34:48 -0300481 struct coda_ctx *ctx = fh_to_ctx(priv);
482
Javier Martin186b2502012-07-26 05:53:35 -0300483 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300484 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
485 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300486 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Sylwester Nawrockic3aac8b2012-08-22 18:00:19 -0300487 /*
488 * This is only a mem-to-mem video device. The capture and output
489 * device capability flags are left only for backward compatibility
490 * and are scheduled for removal.
491 */
492 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
493 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300494 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
495
496 return 0;
497}
498
499static int enum_fmt(void *priv, struct v4l2_fmtdesc *f,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300500 enum v4l2_buf_type type, int src_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300501{
502 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300503 struct coda_codec *codecs = ctx->dev->devtype->codecs;
504 struct coda_fmt *formats = coda_formats;
Javier Martin186b2502012-07-26 05:53:35 -0300505 struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300506 int num_codecs = ctx->dev->devtype->num_codecs;
507 int num_formats = ARRAY_SIZE(coda_formats);
508 int i, k, num = 0;
Javier Martin186b2502012-07-26 05:53:35 -0300509
510 for (i = 0; i < num_formats; i++) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300511 /* Both uncompressed formats are always supported */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300512 if (coda_format_is_yuv(formats[i].fourcc) &&
513 !coda_format_is_yuv(src_fourcc)) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300514 if (num == f->index)
515 break;
516 ++num;
517 continue;
518 }
519 /* Compressed formats may be supported, check the codec list */
520 for (k = 0; k < num_codecs; k++) {
Philipp Zabel918c66f2013-06-27 06:59:01 -0300521 /* if src_fourcc is set, only consider matching codecs */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300522 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
Philipp Zabel918c66f2013-06-27 06:59:01 -0300523 formats[i].fourcc == codecs[k].dst_fourcc &&
524 (!src_fourcc || src_fourcc == codecs[k].src_fourcc))
Philipp Zabelb96904e2013-05-23 10:42:58 -0300525 break;
526 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
527 formats[i].fourcc == codecs[k].src_fourcc)
528 break;
529 }
530 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300531 if (num == f->index)
532 break;
533 ++num;
534 }
535 }
536
537 if (i < num_formats) {
538 fmt = &formats[i];
539 strlcpy(f->description, fmt->name, sizeof(f->description));
540 f->pixelformat = fmt->fourcc;
Philipp Zabelbaf70212013-09-30 10:34:46 -0300541 if (!coda_format_is_yuv(fmt->fourcc))
542 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300543 return 0;
544 }
545
546 /* Format not found */
547 return -EINVAL;
548}
549
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300550static int coda_enum_fmt_vid_cap(struct file *file, void *priv,
551 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300552{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300553 struct coda_ctx *ctx = fh_to_ctx(priv);
554 struct vb2_queue *src_vq;
555 struct coda_q_data *q_data_src;
556
557 /* If the source format is already fixed, only list matching formats */
Philipp Zabel14604e32014-07-11 06:36:22 -0300558 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300559 if (vb2_is_streaming(src_vq)) {
560 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
561
562 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE,
563 q_data_src->fourcc);
564 }
565
566 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300567}
568
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300569static int coda_enum_fmt_vid_out(struct file *file, void *priv,
570 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300571{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300572 return enum_fmt(priv, f, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0);
Javier Martin186b2502012-07-26 05:53:35 -0300573}
574
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300575static int coda_g_fmt(struct file *file, void *priv,
576 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300577{
Javier Martin186b2502012-07-26 05:53:35 -0300578 struct coda_q_data *q_data;
579 struct coda_ctx *ctx = fh_to_ctx(priv);
580
Javier Martin186b2502012-07-26 05:53:35 -0300581 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300582 if (!q_data)
583 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300584
585 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300586 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300587 f->fmt.pix.width = q_data->width;
588 f->fmt.pix.height = q_data->height;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300589 if (coda_format_is_yuv(f->fmt.pix.pixelformat))
Javier Martin186b2502012-07-26 05:53:35 -0300590 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 2);
591 else /* encoded formats h.264/mpeg4 */
592 f->fmt.pix.bytesperline = 0;
593
594 f->fmt.pix.sizeimage = q_data->sizeimage;
595 f->fmt.pix.colorspace = ctx->colorspace;
596
597 return 0;
598}
599
Philipp Zabel57625592013-09-30 10:34:51 -0300600static int coda_try_fmt(struct coda_ctx *ctx, struct coda_codec *codec,
601 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300602{
Philipp Zabel57625592013-09-30 10:34:51 -0300603 struct coda_dev *dev = ctx->dev;
604 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300605 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300606 enum v4l2_field field;
607
608 field = f->fmt.pix.field;
609 if (field == V4L2_FIELD_ANY)
610 field = V4L2_FIELD_NONE;
611 else if (V4L2_FIELD_NONE != field)
612 return -EINVAL;
613
614 /* V4L2 specification suggests the driver corrects the format struct
615 * if any of the dimensions is unsupported */
616 f->fmt.pix.field = field;
617
Philipp Zabel57625592013-09-30 10:34:51 -0300618 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
619 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
620 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
621 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300622
Philipp Zabel57625592013-09-30 10:34:51 -0300623 switch (f->fmt.pix.pixelformat) {
624 case V4L2_PIX_FMT_YUV420:
625 case V4L2_PIX_FMT_YVU420:
626 case V4L2_PIX_FMT_H264:
627 case V4L2_PIX_FMT_MPEG4:
628 case V4L2_PIX_FMT_JPEG:
629 break;
630 default:
631 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300632 if (!q_data)
633 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300634 f->fmt.pix.pixelformat = q_data->fourcc;
635 }
636
637 switch (f->fmt.pix.pixelformat) {
638 case V4L2_PIX_FMT_YUV420:
639 case V4L2_PIX_FMT_YVU420:
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300640 /* Frame stride must be multiple of 8 */
641 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 8);
642 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300643 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300644 break;
645 case V4L2_PIX_FMT_H264:
646 case V4L2_PIX_FMT_MPEG4:
647 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300648 f->fmt.pix.bytesperline = 0;
649 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300650 break;
651 default:
652 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300653 }
654
655 return 0;
656}
657
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300658static int coda_try_fmt_vid_cap(struct file *file, void *priv,
659 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300660{
Javier Martin186b2502012-07-26 05:53:35 -0300661 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300662 struct coda_codec *codec;
663 struct vb2_queue *src_vq;
664 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300665
Philipp Zabel918c66f2013-06-27 06:59:01 -0300666 /*
667 * If the source format is already fixed, try to find a codec that
668 * converts to the given destination format
669 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300670 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300671 if (vb2_is_streaming(src_vq)) {
672 struct coda_q_data *q_data_src;
673
674 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
675 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
676 f->fmt.pix.pixelformat);
677 if (!codec)
678 return -EINVAL;
679 } else {
680 /* Otherwise determine codec by encoded format, if possible */
681 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
682 f->fmt.pix.pixelformat);
683 }
Javier Martin186b2502012-07-26 05:53:35 -0300684
685 f->fmt.pix.colorspace = ctx->colorspace;
686
Philipp Zabel57625592013-09-30 10:34:51 -0300687 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300688 if (ret < 0)
689 return ret;
690
691 /* The h.264 decoder only returns complete 16x16 macroblocks */
692 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
693 f->fmt.pix.width = round_up(f->fmt.pix.width, 16);
694 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
695 f->fmt.pix.bytesperline = f->fmt.pix.width;
696 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
697 f->fmt.pix.height * 3 / 2;
698 }
699
700 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300701}
702
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300703static int coda_try_fmt_vid_out(struct file *file, void *priv,
704 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300705{
706 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300707 struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300708
Philipp Zabelb96904e2013-05-23 10:42:58 -0300709 /* Determine codec by encoded format, returns NULL if raw or invalid */
710 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
711 V4L2_PIX_FMT_YUV420);
Javier Martin186b2502012-07-26 05:53:35 -0300712
713 if (!f->fmt.pix.colorspace)
714 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
715
Philipp Zabel57625592013-09-30 10:34:51 -0300716 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300717}
718
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300719static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300720{
721 struct coda_q_data *q_data;
722 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300723
Philipp Zabel14604e32014-07-11 06:36:22 -0300724 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300725 if (!vq)
726 return -EINVAL;
727
728 q_data = get_q_data(ctx, f->type);
729 if (!q_data)
730 return -EINVAL;
731
732 if (vb2_is_busy(vq)) {
733 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
734 return -EBUSY;
735 }
736
Philipp Zabelb96904e2013-05-23 10:42:58 -0300737 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300738 q_data->width = f->fmt.pix.width;
739 q_data->height = f->fmt.pix.height;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300740 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300741 q_data->rect.left = 0;
742 q_data->rect.top = 0;
743 q_data->rect.width = f->fmt.pix.width;
744 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300745
746 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
747 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300748 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300749
750 return 0;
751}
752
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300753static int coda_s_fmt_vid_cap(struct file *file, void *priv,
754 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300755{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300756 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300757 int ret;
758
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300759 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300760 if (ret)
761 return ret;
762
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300763 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300764}
765
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300766static int coda_s_fmt_vid_out(struct file *file, void *priv,
767 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300768{
769 struct coda_ctx *ctx = fh_to_ctx(priv);
770 int ret;
771
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300772 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300773 if (ret)
774 return ret;
775
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300776 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300777 if (ret)
778 ctx->colorspace = f->fmt.pix.colorspace;
779
780 return ret;
781}
782
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300783static int coda_qbuf(struct file *file, void *priv,
784 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300785{
786 struct coda_ctx *ctx = fh_to_ctx(priv);
787
Philipp Zabel14604e32014-07-11 06:36:22 -0300788 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300789}
790
Philipp Zabel918c66f2013-06-27 06:59:01 -0300791static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
792 struct v4l2_buffer *buf)
793{
794 struct vb2_queue *src_vq;
795
Philipp Zabel14604e32014-07-11 06:36:22 -0300796 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300797
798 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
799 (buf->sequence == (ctx->qsequence - 1)));
800}
801
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300802static int coda_dqbuf(struct file *file, void *priv,
803 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300804{
805 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300806 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300807
Philipp Zabel14604e32014-07-11 06:36:22 -0300808 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300809
810 /* If this is the last capture buffer, emit an end-of-stream event */
811 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
812 coda_buf_is_end_of_stream(ctx, buf)) {
813 const struct v4l2_event eos_event = {
814 .type = V4L2_EVENT_EOS
815 };
816
817 v4l2_event_queue_fh(&ctx->fh, &eos_event);
818 }
819
820 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300821}
822
Philipp Zabel52c41672014-07-11 06:36:19 -0300823static int coda_g_selection(struct file *file, void *fh,
824 struct v4l2_selection *s)
825{
826 struct coda_ctx *ctx = fh_to_ctx(fh);
827 struct coda_q_data *q_data;
828 struct v4l2_rect r, *rsel;
829
830 q_data = get_q_data(ctx, s->type);
831 if (!q_data)
832 return -EINVAL;
833
834 r.left = 0;
835 r.top = 0;
836 r.width = q_data->width;
837 r.height = q_data->height;
838 rsel = &q_data->rect;
839
840 switch (s->target) {
841 case V4L2_SEL_TGT_CROP_DEFAULT:
842 case V4L2_SEL_TGT_CROP_BOUNDS:
843 rsel = &r;
844 /* fallthrough */
845 case V4L2_SEL_TGT_CROP:
846 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
847 return -EINVAL;
848 break;
849 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
850 case V4L2_SEL_TGT_COMPOSE_PADDED:
851 rsel = &r;
852 /* fallthrough */
853 case V4L2_SEL_TGT_COMPOSE:
854 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
855 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
856 return -EINVAL;
857 break;
858 default:
859 return -EINVAL;
860 }
861
862 s->r = *rsel;
863
864 return 0;
865}
866
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300867static int coda_try_decoder_cmd(struct file *file, void *fh,
868 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300869{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300870 if (dc->cmd != V4L2_DEC_CMD_STOP)
871 return -EINVAL;
872
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300873 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300874 return -EINVAL;
875
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300876 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300877 return -EINVAL;
878
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300879 return 0;
880}
881
882static int coda_decoder_cmd(struct file *file, void *fh,
883 struct v4l2_decoder_cmd *dc)
884{
885 struct coda_ctx *ctx = fh_to_ctx(fh);
Philipp Zabel89548442014-07-11 06:36:17 -0300886 struct coda_dev *dev = ctx->dev;
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300887 int ret;
888
889 ret = coda_try_decoder_cmd(file, fh, dc);
890 if (ret < 0)
891 return ret;
892
893 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300894 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300895 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300896
897 /* Set the strem-end flag on this context */
898 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
899
Philipp Zabel89548442014-07-11 06:36:17 -0300900 if ((dev->devtype->product == CODA_960) &&
901 coda_isbusy(dev) &&
902 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
903 /* If this context is currently running, update the hardware flag */
904 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
905 }
906
Philipp Zabel918c66f2013-06-27 06:59:01 -0300907 return 0;
908}
909
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300910static int coda_subscribe_event(struct v4l2_fh *fh,
911 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300912{
913 switch (sub->type) {
914 case V4L2_EVENT_EOS:
915 return v4l2_event_subscribe(fh, sub, 0, NULL);
916 default:
917 return v4l2_ctrl_subscribe_event(fh, sub);
918 }
Javier Martin186b2502012-07-26 05:53:35 -0300919}
920
921static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300922 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300923
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300924 .vidioc_enum_fmt_vid_cap = coda_enum_fmt_vid_cap,
925 .vidioc_g_fmt_vid_cap = coda_g_fmt,
926 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
927 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300928
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300929 .vidioc_enum_fmt_vid_out = coda_enum_fmt_vid_out,
930 .vidioc_g_fmt_vid_out = coda_g_fmt,
931 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
932 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300933
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300934 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
935 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300936
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300937 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300938 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300939 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300940 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300941
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300942 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
943 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300944
Philipp Zabel52c41672014-07-11 06:36:19 -0300945 .vidioc_g_selection = coda_g_selection,
946
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300947 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300948 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300949
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300950 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300951 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300952};
953
Philipp Zabel918c66f2013-06-27 06:59:01 -0300954static int coda_start_decoding(struct coda_ctx *ctx);
955
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300956static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
957{
958 return kfifo_len(&ctx->bitstream_fifo);
959}
960
961static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
962{
963 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
964 struct coda_dev *dev = ctx->dev;
965 u32 rd_ptr;
966
967 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
968 kfifo->out = (kfifo->in & ~kfifo->mask) |
969 (rd_ptr - ctx->bitstream.paddr);
970 if (kfifo->out > kfifo->in)
971 kfifo->out -= kfifo->mask + 1;
972}
973
974static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
975{
976 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
977 struct coda_dev *dev = ctx->dev;
978 u32 rd_ptr, wr_ptr;
979
980 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
981 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
982 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
983 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
984}
985
986static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
987{
988 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
989 struct coda_dev *dev = ctx->dev;
990 u32 wr_ptr;
991
992 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
993 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
994}
995
996static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
997{
998 u32 src_size = vb2_get_plane_payload(src_buf, 0);
999 u32 n;
1000
1001 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
1002 if (n < src_size)
1003 return -ENOSPC;
1004
1005 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
1006 ctx->bitstream.size, DMA_TO_DEVICE);
1007
1008 ctx->qsequence++;
1009
1010 return 0;
1011}
1012
1013static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
1014 struct vb2_buffer *src_buf)
1015{
1016 int ret;
1017
1018 if (coda_get_bitstream_payload(ctx) +
1019 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
1020 return false;
1021
1022 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
1023 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
1024 return true;
1025 }
1026
1027 ret = coda_bitstream_queue(ctx, src_buf);
1028 if (ret < 0) {
1029 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
1030 return false;
1031 }
1032 /* Sync read pointer to device */
1033 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
1034 coda_kfifo_sync_to_device_write(ctx);
1035
Philipp Zabel918c66f2013-06-27 06:59:01 -03001036 ctx->prescan_failed = false;
1037
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001038 return true;
1039}
1040
1041static void coda_fill_bitstream(struct coda_ctx *ctx)
1042{
1043 struct vb2_buffer *src_buf;
1044
Philipp Zabel14604e32014-07-11 06:36:22 -03001045 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
1046 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001047
1048 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel14604e32014-07-11 06:36:22 -03001049 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001050 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1051 } else {
1052 break;
1053 }
1054 }
1055}
1056
Philipp Zabel89548442014-07-11 06:36:17 -03001057static void coda_set_gdi_regs(struct coda_ctx *ctx)
1058{
1059 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1060 struct coda_dev *dev = ctx->dev;
1061 int i;
1062
1063 for (i = 0; i < 16; i++)
1064 coda_write(dev, tiled_map->xy2ca_map[i],
1065 CODA9_GDI_XY2_CAS_0 + 4 * i);
1066 for (i = 0; i < 4; i++)
1067 coda_write(dev, tiled_map->xy2ba_map[i],
1068 CODA9_GDI_XY2_BA_0 + 4 * i);
1069 for (i = 0; i < 16; i++)
1070 coda_write(dev, tiled_map->xy2ra_map[i],
1071 CODA9_GDI_XY2_RAS_0 + 4 * i);
1072 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
1073 for (i = 0; i < 32; i++)
1074 coda_write(dev, tiled_map->rbc2axi_map[i],
1075 CODA9_GDI_RBC2_AXI_0 + 4 * i);
1076}
1077
Javier Martin186b2502012-07-26 05:53:35 -03001078/*
1079 * Mem-to-mem operations.
1080 */
Philipp Zabel918c66f2013-06-27 06:59:01 -03001081static int coda_prepare_decode(struct coda_ctx *ctx)
1082{
1083 struct vb2_buffer *dst_buf;
1084 struct coda_dev *dev = ctx->dev;
1085 struct coda_q_data *q_data_dst;
1086 u32 stridey, height;
1087 u32 picture_y, picture_cb, picture_cr;
1088
Philipp Zabel14604e32014-07-11 06:36:22 -03001089 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001090 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1091
1092 if (ctx->params.rot_mode & CODA_ROT_90) {
1093 stridey = q_data_dst->height;
1094 height = q_data_dst->width;
1095 } else {
1096 stridey = q_data_dst->width;
1097 height = q_data_dst->height;
1098 }
1099
1100 /* Try to copy source buffer contents into the bitstream ringbuffer */
1101 mutex_lock(&ctx->bitstream_mutex);
1102 coda_fill_bitstream(ctx);
1103 mutex_unlock(&ctx->bitstream_mutex);
1104
1105 if (coda_get_bitstream_payload(ctx) < 512 &&
1106 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1107 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1108 "bitstream payload: %d, skipping\n",
1109 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -03001110 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001111 return -EAGAIN;
1112 }
1113
1114 /* Run coda_start_decoding (again) if not yet initialized */
1115 if (!ctx->initialized) {
1116 int ret = coda_start_decoding(ctx);
1117 if (ret < 0) {
1118 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -03001119 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001120 return -EAGAIN;
1121 } else {
1122 ctx->initialized = 1;
1123 }
1124 }
1125
Philipp Zabel89548442014-07-11 06:36:17 -03001126 if (dev->devtype->product == CODA_960)
1127 coda_set_gdi_regs(ctx);
1128
Philipp Zabel918c66f2013-06-27 06:59:01 -03001129 /* Set rotator output */
1130 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1131 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1132 /* Switch Cr and Cb for YVU420 format */
1133 picture_cr = picture_y + stridey * height;
1134 picture_cb = picture_cr + stridey / 2 * height / 2;
1135 } else {
1136 picture_cb = picture_y + stridey * height;
1137 picture_cr = picture_cb + stridey / 2 * height / 2;
1138 }
Philipp Zabel89548442014-07-11 06:36:17 -03001139
1140 if (dev->devtype->product == CODA_960) {
1141 /*
1142 * The CODA960 seems to have an internal list of buffers with
1143 * 64 entries that includes the registered frame buffers as
1144 * well as the rotator buffer output.
1145 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1146 */
1147 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1148 CODA9_CMD_DEC_PIC_ROT_INDEX);
1149 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1150 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1151 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1152 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1153 } else {
1154 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1155 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1156 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1157 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1158 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001159 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1160 CODA_CMD_DEC_PIC_ROT_MODE);
1161
1162 switch (dev->devtype->product) {
1163 case CODA_DX6:
1164 /* TBD */
1165 case CODA_7541:
1166 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1167 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001168 case CODA_960:
1169 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1170 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001171 }
1172
1173 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1174
1175 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1176 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1177
1178 return 0;
1179}
1180
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001181static void coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001182{
Javier Martin186b2502012-07-26 05:53:35 -03001183 struct coda_q_data *q_data_src, *q_data_dst;
1184 struct vb2_buffer *src_buf, *dst_buf;
1185 struct coda_dev *dev = ctx->dev;
1186 int force_ipicture;
1187 int quant_param = 0;
1188 u32 picture_y, picture_cb, picture_cr;
1189 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1190 u32 dst_fourcc;
1191
Philipp Zabel14604e32014-07-11 06:36:22 -03001192 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1193 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001194 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1195 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001196 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001197
Philipp Zabel918c66f2013-06-27 06:59:01 -03001198 src_buf->v4l2_buf.sequence = ctx->osequence;
1199 dst_buf->v4l2_buf.sequence = ctx->osequence;
1200 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001201
1202 /*
1203 * Workaround coda firmware BUG that only marks the first
1204 * frame as IDR. This is a problem for some decoders that can't
1205 * recover when a frame is lost.
1206 */
1207 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1208 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1209 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1210 } else {
1211 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1212 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1213 }
1214
Philipp Zabel89548442014-07-11 06:36:17 -03001215 if (dev->devtype->product == CODA_960)
1216 coda_set_gdi_regs(ctx);
1217
Javier Martin186b2502012-07-26 05:53:35 -03001218 /*
1219 * Copy headers at the beginning of the first frame for H.264 only.
1220 * In MPEG4 they are already copied by the coda.
1221 */
1222 if (src_buf->v4l2_buf.sequence == 0) {
1223 pic_stream_buffer_addr =
1224 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1225 ctx->vpu_header_size[0] +
1226 ctx->vpu_header_size[1] +
1227 ctx->vpu_header_size[2];
1228 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1229 ctx->vpu_header_size[0] -
1230 ctx->vpu_header_size[1] -
1231 ctx->vpu_header_size[2];
1232 memcpy(vb2_plane_vaddr(dst_buf, 0),
1233 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1234 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1235 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1236 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1237 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1238 ctx->vpu_header_size[2]);
1239 } else {
1240 pic_stream_buffer_addr =
1241 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1242 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1243 }
1244
1245 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1246 force_ipicture = 1;
1247 switch (dst_fourcc) {
1248 case V4L2_PIX_FMT_H264:
1249 quant_param = ctx->params.h264_intra_qp;
1250 break;
1251 case V4L2_PIX_FMT_MPEG4:
1252 quant_param = ctx->params.mpeg4_intra_qp;
1253 break;
1254 default:
1255 v4l2_warn(&ctx->dev->v4l2_dev,
1256 "cannot set intra qp, fmt not supported\n");
1257 break;
1258 }
1259 } else {
1260 force_ipicture = 0;
1261 switch (dst_fourcc) {
1262 case V4L2_PIX_FMT_H264:
1263 quant_param = ctx->params.h264_inter_qp;
1264 break;
1265 case V4L2_PIX_FMT_MPEG4:
1266 quant_param = ctx->params.mpeg4_inter_qp;
1267 break;
1268 default:
1269 v4l2_warn(&ctx->dev->v4l2_dev,
1270 "cannot set inter qp, fmt not supported\n");
1271 break;
1272 }
1273 }
1274
1275 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001276 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001277 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1278
1279
1280 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001281 switch (q_data_src->fourcc) {
1282 case V4L2_PIX_FMT_YVU420:
1283 /* Switch Cb and Cr for YVU420 format */
1284 picture_cr = picture_y + q_data_src->width * q_data_src->height;
1285 picture_cb = picture_cr + q_data_src->width / 2 *
1286 q_data_src->height / 2;
1287 break;
1288 case V4L2_PIX_FMT_YUV420:
1289 default:
1290 picture_cb = picture_y + q_data_src->width * q_data_src->height;
1291 picture_cr = picture_cb + q_data_src->width / 2 *
1292 q_data_src->height / 2;
1293 break;
1294 }
Javier Martin186b2502012-07-26 05:53:35 -03001295
Philipp Zabel89548442014-07-11 06:36:17 -03001296 if (dev->devtype->product == CODA_960) {
1297 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1298 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1299 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1300
1301 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1302 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1303 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1304 } else {
1305 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1306 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1307 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1308 }
Javier Martin186b2502012-07-26 05:53:35 -03001309 coda_write(dev, force_ipicture << 1 & 0x2,
1310 CODA_CMD_ENC_PIC_OPTION);
1311
1312 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1313 coda_write(dev, pic_stream_buffer_size / 1024,
1314 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001315
1316 if (!ctx->streamon_out) {
1317 /* After streamoff on the output side, set the stream end flag */
1318 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1319 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1320 }
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001321}
1322
1323static void coda_device_run(void *m2m_priv)
1324{
1325 struct coda_ctx *ctx = m2m_priv;
1326 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001327
1328 queue_work(dev->workqueue, &ctx->pic_run_work);
1329}
1330
1331static void coda_free_framebuffers(struct coda_ctx *ctx);
1332static void coda_free_context_buffers(struct coda_ctx *ctx);
1333
1334static void coda_seq_end_work(struct work_struct *work)
1335{
1336 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1337 struct coda_dev *dev = ctx->dev;
1338
1339 mutex_lock(&ctx->buffer_mutex);
1340 mutex_lock(&dev->coda_mutex);
1341
1342 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1343 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1344 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1345 v4l2_err(&dev->v4l2_dev,
1346 "CODA_COMMAND_SEQ_END failed\n");
1347 }
1348
1349 kfifo_init(&ctx->bitstream_fifo,
1350 ctx->bitstream.vaddr, ctx->bitstream.size);
1351
1352 coda_free_framebuffers(ctx);
1353 coda_free_context_buffers(ctx);
1354
1355 mutex_unlock(&dev->coda_mutex);
1356 mutex_unlock(&ctx->buffer_mutex);
1357}
1358
1359static void coda_finish_decode(struct coda_ctx *ctx);
1360static void coda_finish_encode(struct coda_ctx *ctx);
1361
1362static void coda_pic_run_work(struct work_struct *work)
1363{
1364 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1365 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001366 int ret;
1367
1368 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001369 mutex_lock(&dev->coda_mutex);
1370
Philipp Zabel918c66f2013-06-27 06:59:01 -03001371 if (ctx->inst_type == CODA_INST_DECODER) {
1372 ret = coda_prepare_decode(ctx);
1373 if (ret < 0) {
1374 mutex_unlock(&dev->coda_mutex);
1375 mutex_unlock(&ctx->buffer_mutex);
1376 /* job_finish scheduled by prepare_decode */
1377 return;
1378 }
1379 } else {
1380 coda_prepare_encode(ctx);
Philipp Zabel10436672012-07-02 09:03:55 -03001381 }
1382
Philipp Zabelc2d22512013-06-21 03:55:28 -03001383 if (dev->devtype->product != CODA_DX6)
1384 coda_write(dev, ctx->iram_info.axi_sram_use,
1385 CODA7_REG_BIT_AXI_SRAM_USE);
1386
Philipp Zabel918c66f2013-06-27 06:59:01 -03001387 if (ctx->inst_type == CODA_INST_DECODER)
1388 coda_kfifo_sync_to_device_full(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001389 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001390
1391 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1392 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
1393 } else if (!ctx->aborting) {
1394 if (ctx->inst_type == CODA_INST_DECODER)
1395 coda_finish_decode(ctx);
1396 else
1397 coda_finish_encode(ctx);
1398 }
1399
1400 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1401 queue_work(dev->workqueue, &ctx->seq_end_work);
1402
1403 mutex_unlock(&dev->coda_mutex);
1404 mutex_unlock(&ctx->buffer_mutex);
1405
Philipp Zabel14604e32014-07-11 06:36:22 -03001406 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001407}
1408
1409static int coda_job_ready(void *m2m_priv)
1410{
1411 struct coda_ctx *ctx = m2m_priv;
1412
1413 /*
1414 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001415 * and 1 frame are needed. In the decoder case,
1416 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001417 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001418 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001419 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001420 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1421 "not ready: not enough video buffers.\n");
1422 return 0;
1423 }
1424
Philipp Zabel14604e32014-07-11 06:36:22 -03001425 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001426 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1427 "not ready: not enough video capture buffers.\n");
1428 return 0;
1429 }
1430
Philipp Zabel918c66f2013-06-27 06:59:01 -03001431 if (ctx->prescan_failed ||
1432 ((ctx->inst_type == CODA_INST_DECODER) &&
1433 (coda_get_bitstream_payload(ctx) < 512) &&
1434 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1435 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1436 "%d: not ready: not enough bitstream data.\n",
1437 ctx->idx);
1438 return 0;
1439 }
1440
Philipp Zabel3e748262013-05-23 10:43:01 -03001441 if (ctx->aborting) {
1442 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1443 "not ready: aborting\n");
1444 return 0;
1445 }
1446
Javier Martin186b2502012-07-26 05:53:35 -03001447 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1448 "job ready\n");
1449 return 1;
1450}
1451
1452static void coda_job_abort(void *priv)
1453{
1454 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001455
1456 ctx->aborting = 1;
1457
1458 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1459 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001460}
1461
1462static void coda_lock(void *m2m_priv)
1463{
1464 struct coda_ctx *ctx = m2m_priv;
1465 struct coda_dev *pcdev = ctx->dev;
1466 mutex_lock(&pcdev->dev_mutex);
1467}
1468
1469static void coda_unlock(void *m2m_priv)
1470{
1471 struct coda_ctx *ctx = m2m_priv;
1472 struct coda_dev *pcdev = ctx->dev;
1473 mutex_unlock(&pcdev->dev_mutex);
1474}
1475
1476static struct v4l2_m2m_ops coda_m2m_ops = {
1477 .device_run = coda_device_run,
1478 .job_ready = coda_job_ready,
1479 .job_abort = coda_job_abort,
1480 .lock = coda_lock,
1481 .unlock = coda_unlock,
1482};
1483
Philipp Zabel89548442014-07-11 06:36:17 -03001484static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1485{
1486 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1487 int luma_map, chro_map, i;
1488
1489 memset(tiled_map, 0, sizeof(*tiled_map));
1490
1491 luma_map = 64;
1492 chro_map = 64;
1493 tiled_map->map_type = tiled_map_type;
1494 for (i = 0; i < 16; i++)
1495 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1496 for (i = 0; i < 4; i++)
1497 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1498 for (i = 0; i < 16; i++)
1499 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1500
1501 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1502 tiled_map->xy2rbc_config = 0;
1503 } else {
1504 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1505 tiled_map_type);
1506 return;
1507 }
1508}
1509
Javier Martin186b2502012-07-26 05:53:35 -03001510static void set_default_params(struct coda_ctx *ctx)
1511{
Philipp Zabelb96904e2013-05-23 10:42:58 -03001512 int max_w;
1513 int max_h;
1514
1515 ctx->codec = &ctx->dev->devtype->codecs[0];
1516 max_w = ctx->codec->max_w;
1517 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001518
1519 ctx->params.codec_mode = CODA_MODE_INVALID;
1520 ctx->colorspace = V4L2_COLORSPACE_REC709;
1521 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001522 ctx->aborting = 0;
1523
1524 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001525 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1526 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1527 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1528 ctx->q_data[V4L2_M2M_SRC].height = max_h;
1529 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1530 ctx->q_data[V4L2_M2M_DST].width = max_w;
1531 ctx->q_data[V4L2_M2M_DST].height = max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001532 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel52c41672014-07-11 06:36:19 -03001533 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1534 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1535 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1536 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001537
1538 if (ctx->dev->devtype->product == CODA_960)
1539 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001540}
1541
1542/*
1543 * Queue operations
1544 */
1545static int coda_queue_setup(struct vb2_queue *vq,
1546 const struct v4l2_format *fmt,
1547 unsigned int *nbuffers, unsigned int *nplanes,
1548 unsigned int sizes[], void *alloc_ctxs[])
1549{
1550 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001551 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001552 unsigned int size;
1553
Philipp Zabele34db062012-08-29 08:22:00 -03001554 q_data = get_q_data(ctx, vq->type);
1555 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001556
1557 *nplanes = 1;
1558 sizes[0] = size;
1559
1560 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1561
1562 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1563 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1564
1565 return 0;
1566}
1567
1568static int coda_buf_prepare(struct vb2_buffer *vb)
1569{
1570 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1571 struct coda_q_data *q_data;
1572
1573 q_data = get_q_data(ctx, vb->vb2_queue->type);
1574
1575 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1576 v4l2_warn(&ctx->dev->v4l2_dev,
1577 "%s data will not fit into plane (%lu < %lu)\n",
1578 __func__, vb2_plane_size(vb, 0),
1579 (long)q_data->sizeimage);
1580 return -EINVAL;
1581 }
1582
Javier Martin186b2502012-07-26 05:53:35 -03001583 return 0;
1584}
1585
1586static void coda_buf_queue(struct vb2_buffer *vb)
1587{
1588 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel89548442014-07-11 06:36:17 -03001589 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001590 struct coda_q_data *q_data;
1591
1592 q_data = get_q_data(ctx, vb->vb2_queue->type);
1593
1594 /*
1595 * In the decoder case, immediately try to copy the buffer into the
1596 * bitstream ringbuffer and mark it as ready to be dequeued.
1597 */
1598 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1599 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1600 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001601 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001602 * the stream end
1603 */
Philipp Zabel89548442014-07-11 06:36:17 -03001604 if (vb2_get_plane_payload(vb, 0) == 0) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001605 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
Philipp Zabel89548442014-07-11 06:36:17 -03001606 if ((dev->devtype->product == CODA_960) &&
1607 coda_isbusy(dev) &&
1608 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
1609 /* if this decoder instance is running, set the stream end flag */
1610 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1611 }
1612 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001613 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001614 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001615 coda_fill_bitstream(ctx);
1616 mutex_unlock(&ctx->bitstream_mutex);
1617 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001618 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001619 }
Javier Martin186b2502012-07-26 05:53:35 -03001620}
1621
Philipp Zabel86eda902013-05-23 10:42:57 -03001622static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1623{
1624 struct coda_dev *dev = ctx->dev;
1625 u32 *p = ctx->parabuf.vaddr;
1626
1627 if (dev->devtype->product == CODA_DX6)
1628 p[index] = value;
1629 else
1630 p[index ^ 1] = value;
1631}
1632
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001633static int coda_alloc_aux_buf(struct coda_dev *dev,
1634 struct coda_aux_buf *buf, size_t size)
1635{
1636 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1637 GFP_KERNEL);
1638 if (!buf->vaddr)
1639 return -ENOMEM;
1640
1641 buf->size = size;
1642
1643 return 0;
1644}
1645
1646static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
1647 struct coda_aux_buf *buf, size_t size)
1648{
1649 return coda_alloc_aux_buf(ctx->dev, buf, size);
1650}
1651
1652static void coda_free_aux_buf(struct coda_dev *dev,
1653 struct coda_aux_buf *buf)
1654{
1655 if (buf->vaddr) {
1656 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1657 buf->vaddr, buf->paddr);
1658 buf->vaddr = NULL;
1659 buf->size = 0;
1660 }
1661}
1662
1663static void coda_free_framebuffers(struct coda_ctx *ctx)
1664{
1665 int i;
1666
1667 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1668 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1669}
1670
Philipp Zabelec25f682012-07-20 08:54:29 -03001671static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1672{
1673 struct coda_dev *dev = ctx->dev;
Philipp Zabelec25f682012-07-20 08:54:29 -03001674 int height = q_data->height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001675 dma_addr_t paddr;
1676 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001677 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001678 int i;
1679
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001680 if (ctx->codec && ctx->codec->src_fourcc == V4L2_PIX_FMT_H264)
1681 height = round_up(height, 16);
Philipp Zabel86eda902013-05-23 10:42:57 -03001682 ysize = round_up(q_data->width, 8) * height;
1683
Philipp Zabelec25f682012-07-20 08:54:29 -03001684 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001685 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001686 size_t size;
1687
Philipp Zabelaed14b02014-07-11 06:36:15 -03001688 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001689 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1690 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001691 size += ysize / 4;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001692 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i], size);
1693 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001694 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001695 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001696 }
1697 }
1698
1699 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001700 for (i = 0; i < ctx->num_internal_frames; i++) {
1701 paddr = ctx->internal_frames[i].paddr;
1702 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1703 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1704 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001705
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001706 /* mvcol buffer for h.264 */
1707 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1708 dev->devtype->product != CODA_DX6)
1709 coda_parabuf_write(ctx, 96 + i,
1710 ctx->internal_frames[i].paddr +
1711 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001712 }
1713
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001714 /* mvcol buffer for mpeg4 */
1715 if ((dev->devtype->product != CODA_DX6) &&
1716 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1717 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1718 ysize + ysize/4 + ysize/4);
1719
Philipp Zabelec25f682012-07-20 08:54:29 -03001720 return 0;
1721}
1722
Javier Martin3f3f5c72012-10-29 05:20:29 -03001723static int coda_h264_padding(int size, char *p)
1724{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001725 int nal_size;
1726 int diff;
1727
Javier Martin832fbb52012-10-29 08:34:59 -03001728 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001729 if (diff == 0)
1730 return 0;
1731
Javier Martin832fbb52012-10-29 08:34:59 -03001732 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001733 memcpy(p, coda_filler_nal, nal_size);
1734
1735 /* Add rbsp stop bit and trailing at the end */
1736 *(p + nal_size - 1) = 0x80;
1737
1738 return nal_size;
1739}
1740
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001741static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1742{
1743 phys_addr_t ret;
1744
1745 size = round_up(size, 1024);
1746 if (size > iram->remaining)
1747 return 0;
1748 iram->remaining -= size;
1749
1750 ret = iram->next_paddr;
1751 iram->next_paddr += size;
1752
1753 return ret;
1754}
1755
Philipp Zabelc2d22512013-06-21 03:55:28 -03001756static void coda_setup_iram(struct coda_ctx *ctx)
1757{
1758 struct coda_iram_info *iram_info = &ctx->iram_info;
1759 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001760 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001761 int dbk_bits;
1762 int bit_bits;
1763 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001764
1765 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001766 iram_info->next_paddr = dev->iram.paddr;
1767 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001768
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001769 switch (dev->devtype->product) {
1770 case CODA_7541:
1771 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1772 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1773 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1774 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001775 case CODA_960:
1776 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1777 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1778 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1779 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001780 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001781 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001782 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001783
1784 if (ctx->inst_type == CODA_INST_ENCODER) {
1785 struct coda_q_data *q_data_src;
1786
1787 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1788 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1789
1790 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001791 if (dev->devtype->product == CODA_7541) {
1792 iram_info->search_ram_size = round_up(mb_width * 16 *
1793 36 + 2048, 1024);
1794 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1795 iram_info->search_ram_size);
1796 if (!iram_info->search_ram_paddr) {
1797 pr_err("IRAM is smaller than the search ram size\n");
1798 goto out;
1799 }
1800 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1801 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001802 }
1803
1804 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001805 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1806 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1807 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001808 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001809 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001810
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001811 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1812 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001813 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001814 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001815
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001816 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1817 if (!iram_info->buf_ip_ac_dc_use)
1818 goto out;
1819 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001820
Philipp Zabel8358e762013-06-21 03:55:32 -03001821 /* OVL and BTP disabled for encoder */
1822 } else if (ctx->inst_type == CODA_INST_DECODER) {
1823 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001824
1825 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1826 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001827
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001828 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1829 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1830 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001831 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001832 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001833
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001834 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1835 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001836 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001837 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001838
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001839 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1840 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001841 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001842 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001843
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001844 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001845 }
1846
1847out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001848 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1849 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1850 "IRAM smaller than needed\n");
1851
1852 if (dev->devtype->product == CODA_7541) {
1853 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001854 if (ctx->inst_type == CODA_INST_DECODER) {
1855 /* fw 1.4.50 */
1856 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1857 CODA7_USE_IP_ENABLE);
1858 } else {
1859 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001860 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1861 CODA7_USE_HOST_DBK_ENABLE |
1862 CODA7_USE_IP_ENABLE |
1863 CODA7_USE_DBK_ENABLE);
1864 }
1865 }
1866}
1867
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001868static void coda_free_context_buffers(struct coda_ctx *ctx)
1869{
1870 struct coda_dev *dev = ctx->dev;
1871
Philipp Zabel918c66f2013-06-27 06:59:01 -03001872 coda_free_aux_buf(dev, &ctx->slicebuf);
1873 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001874 if (dev->devtype->product != CODA_DX6)
1875 coda_free_aux_buf(dev, &ctx->workbuf);
1876}
1877
1878static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1879 struct coda_q_data *q_data)
1880{
1881 struct coda_dev *dev = ctx->dev;
1882 size_t size;
1883 int ret;
1884
1885 switch (dev->devtype->product) {
1886 case CODA_7541:
1887 size = CODA7_WORK_BUF_SIZE;
1888 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001889 case CODA_960:
1890 size = CODA9_WORK_BUF_SIZE;
1891 if (q_data->fourcc == V4L2_PIX_FMT_H264)
1892 size += CODA9_PS_SAVE_SIZE;
1893 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001894 default:
1895 return 0;
1896 }
1897
Philipp Zabel918c66f2013-06-27 06:59:01 -03001898 if (ctx->psbuf.vaddr) {
1899 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1900 return -EBUSY;
1901 }
1902 if (ctx->slicebuf.vaddr) {
1903 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1904 return -EBUSY;
1905 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001906 if (ctx->workbuf.vaddr) {
1907 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1908 ret = -EBUSY;
1909 return -ENOMEM;
1910 }
1911
Philipp Zabel918c66f2013-06-27 06:59:01 -03001912 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1913 /* worst case slice size */
1914 size = (DIV_ROUND_UP(q_data->width, 16) *
1915 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
1916 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size);
1917 if (ret < 0) {
1918 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1919 ctx->slicebuf.size);
1920 return ret;
1921 }
1922 }
1923
1924 if (dev->devtype->product == CODA_7541) {
1925 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE);
1926 if (ret < 0) {
1927 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1928 goto err;
1929 }
1930 }
1931
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001932 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size);
1933 if (ret < 0) {
1934 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1935 ctx->workbuf.size);
1936 goto err;
1937 }
1938
1939 return 0;
1940
1941err:
1942 coda_free_context_buffers(ctx);
1943 return ret;
1944}
1945
Philipp Zabel918c66f2013-06-27 06:59:01 -03001946static int coda_start_decoding(struct coda_ctx *ctx)
1947{
1948 struct coda_q_data *q_data_src, *q_data_dst;
1949 u32 bitstream_buf, bitstream_size;
1950 struct coda_dev *dev = ctx->dev;
1951 int width, height;
1952 u32 src_fourcc;
1953 u32 val;
1954 int ret;
1955
1956 /* Start decoding */
1957 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1958 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1959 bitstream_buf = ctx->bitstream.paddr;
1960 bitstream_size = ctx->bitstream.size;
1961 src_fourcc = q_data_src->fourcc;
1962
1963 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1964
1965 /* Update coda bitstream read and write pointers from kfifo */
1966 coda_kfifo_sync_to_device_full(ctx);
1967
1968 ctx->display_idx = -1;
1969 ctx->frm_dis_flg = 0;
1970 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1971
1972 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1973 CODA_REG_BIT_BIT_STREAM_PARAM);
1974
1975 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1976 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1977 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001978 if ((dev->devtype->product == CODA_7541) ||
1979 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03001980 val |= CODA_REORDER_ENABLE;
1981 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1982
1983 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03001984 if (dev->devtype->product == CODA_960 &&
1985 src_fourcc == V4L2_PIX_FMT_MPEG4)
1986 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1987 else
1988 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001989 if (src_fourcc == V4L2_PIX_FMT_H264) {
1990 if (dev->devtype->product == CODA_7541) {
1991 coda_write(dev, ctx->psbuf.paddr,
1992 CODA_CMD_DEC_SEQ_PS_BB_START);
1993 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1994 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1995 }
Philipp Zabel89548442014-07-11 06:36:17 -03001996 if (dev->devtype->product == CODA_960) {
1997 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1998 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1999 }
2000 }
2001 if (dev->devtype->product != CODA_960) {
2002 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002003 }
2004
2005 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
2006 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
2007 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2008 return -ETIMEDOUT;
2009 }
2010
2011 /* Update kfifo out pointer from coda bitstream read pointer */
2012 coda_kfifo_sync_from_device(ctx);
2013
2014 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
2015
2016 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
2017 v4l2_err(&dev->v4l2_dev,
2018 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
2019 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
2020 return -EAGAIN;
2021 }
2022
2023 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
2024 if (dev->devtype->product == CODA_DX6) {
2025 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
2026 height = val & CODADX6_PICHEIGHT_MASK;
2027 } else {
2028 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
2029 height = val & CODA7_PICHEIGHT_MASK;
2030 }
2031
2032 if (width > q_data_dst->width || height > q_data_dst->height) {
2033 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
2034 width, height, q_data_dst->width, q_data_dst->height);
2035 return -EINVAL;
2036 }
2037
2038 width = round_up(width, 16);
2039 height = round_up(height, 16);
2040
2041 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
2042 __func__, ctx->idx, width, height);
2043
Philipp Zabela500a932014-07-11 06:36:13 -03002044 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002045 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
2046 v4l2_err(&dev->v4l2_dev,
2047 "not enough framebuffers to decode (%d < %d)\n",
2048 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
2049 return -EINVAL;
2050 }
2051
Philipp Zabel52c41672014-07-11 06:36:19 -03002052 if (src_fourcc == V4L2_PIX_FMT_H264) {
2053 u32 left_right;
2054 u32 top_bottom;
2055
2056 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
2057 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
2058
2059 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
2060 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
2061 q_data_dst->rect.width = width - q_data_dst->rect.left -
2062 (left_right & 0x3ff);
2063 q_data_dst->rect.height = height - q_data_dst->rect.top -
2064 (top_bottom & 0x3ff);
2065 }
2066
Philipp Zabel918c66f2013-06-27 06:59:01 -03002067 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
2068 if (ret < 0)
2069 return ret;
2070
2071 /* Tell the decoder how many frame buffers we allocated. */
2072 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
2073 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
2074
2075 if (dev->devtype->product != CODA_DX6) {
2076 /* Set secondary AXI IRAM */
2077 coda_setup_iram(ctx);
2078
2079 coda_write(dev, ctx->iram_info.buf_bit_use,
2080 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2081 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2082 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2083 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2084 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2085 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2086 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2087 coda_write(dev, ctx->iram_info.buf_ovl_use,
2088 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002089 if (dev->devtype->product == CODA_960)
2090 coda_write(dev, ctx->iram_info.buf_btp_use,
2091 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2092 }
2093
2094 if (dev->devtype->product == CODA_960) {
2095 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2096
2097 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2098 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2099 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2100 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2101 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2102 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002103 }
2104
2105 if (src_fourcc == V4L2_PIX_FMT_H264) {
2106 coda_write(dev, ctx->slicebuf.paddr,
2107 CODA_CMD_SET_FRAME_SLICE_BB_START);
2108 coda_write(dev, ctx->slicebuf.size / 1024,
2109 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2110 }
2111
2112 if (dev->devtype->product == CODA_7541) {
2113 int max_mb_x = 1920 / 16;
2114 int max_mb_y = 1088 / 16;
2115 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002116
Philipp Zabel918c66f2013-06-27 06:59:01 -03002117 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2118 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002119 } else if (dev->devtype->product == CODA_960) {
2120 int max_mb_x = 1920 / 16;
2121 int max_mb_y = 1088 / 16;
2122 int max_mb_num = max_mb_x * max_mb_y;
2123
2124 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2125 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002126 }
2127
2128 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2129 v4l2_err(&ctx->dev->v4l2_dev,
2130 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2131 return -ETIMEDOUT;
2132 }
2133
2134 return 0;
2135}
2136
Philipp Zabeld35167a2013-05-23 10:42:59 -03002137static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2138 int header_code, u8 *header, int *size)
2139{
2140 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002141 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002142 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002143 int i;
2144
2145 if (dev->devtype->product == CODA_960)
2146 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002147
2148 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2149 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002150 bufsize = vb2_plane_size(buf, 0);
2151 if (dev->devtype->product == CODA_960)
2152 bufsize /= 1024;
2153 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002154 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2155 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2156 if (ret < 0) {
2157 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2158 return ret;
2159 }
Philipp Zabel89548442014-07-11 06:36:17 -03002160
2161 if (dev->devtype->product == CODA_960) {
2162 for (i = 63; i > 0; i--)
2163 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2164 break;
2165 *size = i + 1;
2166 } else {
2167 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2168 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2169 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002170 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2171
2172 return 0;
2173}
2174
Philipp Zabel89548442014-07-11 06:36:17 -03002175static int coda_start_encoding(struct coda_ctx *ctx);
2176
Javier Martin186b2502012-07-26 05:53:35 -03002177static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2178{
2179 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2180 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002181 struct coda_dev *dev = ctx->dev;
2182 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002183 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002184 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002185
Philipp Zabelb96904e2013-05-23 10:42:58 -03002186 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002187 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2188 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
2189 if (coda_get_bitstream_payload(ctx) < 512)
2190 return -EINVAL;
2191 } else {
2192 if (count < 1)
2193 return -EINVAL;
2194 }
2195
2196 ctx->streamon_out = 1;
2197
Philipp Zabelb96904e2013-05-23 10:42:58 -03002198 if (coda_format_is_yuv(q_data_src->fourcc))
2199 ctx->inst_type = CODA_INST_ENCODER;
2200 else
2201 ctx->inst_type = CODA_INST_DECODER;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002202 } else {
2203 if (count < 1)
2204 return -EINVAL;
2205
2206 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002207 }
Javier Martin186b2502012-07-26 05:53:35 -03002208
Philipp Zabelb96904e2013-05-23 10:42:58 -03002209 /* Don't start the coda unless both queues are on */
2210 if (!(ctx->streamon_out & ctx->streamon_cap))
2211 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002212
Philipp Zabeleb107512013-09-30 10:34:45 -03002213 /* Allow decoder device_run with no new buffers queued */
2214 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002215 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002216
Philipp Zabelb96904e2013-05-23 10:42:58 -03002217 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002218 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002219 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002220
Philipp Zabelb96904e2013-05-23 10:42:58 -03002221 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2222 q_data_dst->fourcc);
2223 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002224 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2225 return -EINVAL;
2226 }
2227
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002228 /* Allocate per-instance buffers */
2229 ret = coda_alloc_context_buffers(ctx, q_data_src);
2230 if (ret < 0)
2231 return ret;
2232
Philipp Zabel918c66f2013-06-27 06:59:01 -03002233 if (ctx->inst_type == CODA_INST_DECODER) {
2234 mutex_lock(&dev->coda_mutex);
2235 ret = coda_start_decoding(ctx);
2236 mutex_unlock(&dev->coda_mutex);
Philipp Zabel89548442014-07-11 06:36:17 -03002237 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002238 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002239 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002240 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002241 } else {
2242 ret = coda_start_encoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002243 }
2244
Philipp Zabel89548442014-07-11 06:36:17 -03002245 ctx->initialized = 1;
2246 return ret;
2247}
2248
2249static int coda_start_encoding(struct coda_ctx *ctx)
2250{
2251 struct coda_dev *dev = ctx->dev;
2252 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2253 struct coda_q_data *q_data_src, *q_data_dst;
2254 u32 bitstream_buf, bitstream_size;
2255 struct vb2_buffer *buf;
2256 int gamma, ret, value;
2257 u32 dst_fourcc;
2258
2259 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2260 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2261 dst_fourcc = q_data_dst->fourcc;
2262
Philipp Zabel14604e32014-07-11 06:36:22 -03002263 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002264 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2265 bitstream_size = q_data_dst->sizeimage;
2266
Javier Martin186b2502012-07-26 05:53:35 -03002267 if (!coda_is_initialized(dev)) {
2268 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2269 return -EFAULT;
2270 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002271
2272 mutex_lock(&dev->coda_mutex);
2273
Javier Martin186b2502012-07-26 05:53:35 -03002274 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002275 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2276 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002277 switch (dev->devtype->product) {
2278 case CODA_DX6:
2279 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2280 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2281 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002282 case CODA_960:
2283 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2284 /* fallthrough */
2285 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002286 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2287 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002288 break;
Javier Martin186b2502012-07-26 05:53:35 -03002289 }
2290
Philipp Zabel89548442014-07-11 06:36:17 -03002291 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2292 value &= ~(1 << 2 | 0x7 << 9);
2293 ctx->frame_mem_ctrl = value;
2294 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2295
Philipp Zabel10436672012-07-02 09:03:55 -03002296 if (dev->devtype->product == CODA_DX6) {
2297 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002298 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002299 }
Javier Martin186b2502012-07-26 05:53:35 -03002300
2301 /* Could set rotation here if needed */
2302 switch (dev->devtype->product) {
2303 case CODA_DX6:
2304 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002305 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002306 break;
2307 default:
2308 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002309 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002310 }
Javier Martin186b2502012-07-26 05:53:35 -03002311 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2312 coda_write(dev, ctx->params.framerate,
2313 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2314
Philipp Zabelb96904e2013-05-23 10:42:58 -03002315 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002316 switch (dst_fourcc) {
2317 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002318 if (dev->devtype->product == CODA_960)
2319 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2320 else
2321 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002322 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2323 break;
2324 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002325 if (dev->devtype->product == CODA_960)
2326 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2327 else
2328 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002329 coda_write(dev, 0, CODA_CMD_ENC_SEQ_264_PARA);
2330 break;
2331 default:
2332 v4l2_err(v4l2_dev,
2333 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002334 ret = -EINVAL;
2335 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002336 }
2337
Philipp Zabelc566c782012-08-08 11:59:38 -03002338 switch (ctx->params.slice_mode) {
2339 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2340 value = 0;
2341 break;
2342 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2343 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2344 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002345 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002346 break;
2347 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2348 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2349 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2350 value |= 1 & CODA_SLICING_MODE_MASK;
2351 break;
2352 }
Javier Martin186b2502012-07-26 05:53:35 -03002353 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002354 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002355 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2356
2357 if (ctx->params.bitrate) {
2358 /* Rate control enabled */
2359 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2360 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002361 if (dev->devtype->product == CODA_960)
2362 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002363 } else {
2364 value = 0;
2365 }
2366 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2367
2368 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
2369 coda_write(dev, 0, CODA_CMD_ENC_SEQ_INTRA_REFRESH);
2370
2371 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2372 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2373
Javier Martin186b2502012-07-26 05:53:35 -03002374
Philipp Zabel89548442014-07-11 06:36:17 -03002375 value = 0;
2376 if (dev->devtype->product == CODA_960)
2377 gamma = CODA9_DEFAULT_GAMMA;
2378 else
2379 gamma = CODA_DEFAULT_GAMMA;
2380 if (gamma > 0) {
2381 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2382 CODA_CMD_ENC_SEQ_RC_GAMMA);
2383 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002384
2385 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2386 coda_write(dev,
2387 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2388 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2389 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2390 }
Philipp Zabel89548442014-07-11 06:36:17 -03002391 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002392 if (ctx->params.h264_max_qp)
2393 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002394 if (CODA_DEFAULT_GAMMA > 0)
2395 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002396 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002397 if (CODA_DEFAULT_GAMMA > 0) {
2398 if (dev->devtype->product == CODA_DX6)
2399 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2400 else
2401 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2402 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002403 if (ctx->params.h264_min_qp)
2404 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2405 if (ctx->params.h264_max_qp)
2406 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002407 }
Javier Martin186b2502012-07-26 05:53:35 -03002408 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2409
Philipp Zabel89548442014-07-11 06:36:17 -03002410 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2411
Philipp Zabelc2d22512013-06-21 03:55:28 -03002412 coda_setup_iram(ctx);
2413
Javier Martin186b2502012-07-26 05:53:35 -03002414 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002415 switch (dev->devtype->product) {
2416 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002417 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002418 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002419 break;
2420 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002421 coda_write(dev, ctx->iram_info.search_ram_paddr,
2422 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2423 coda_write(dev, ctx->iram_info.search_ram_size,
2424 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002425 break;
2426 case CODA_960:
2427 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2428 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002429 }
Javier Martin186b2502012-07-26 05:53:35 -03002430 }
2431
Philipp Zabelfcb62822013-05-23 10:43:00 -03002432 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2433 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002434 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002435 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002436 }
2437
Philipp Zabelfcb62822013-05-23 10:43:00 -03002438 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2439 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2440 ret = -EFAULT;
2441 goto out;
2442 }
Javier Martin186b2502012-07-26 05:53:35 -03002443
Philipp Zabel89548442014-07-11 06:36:17 -03002444 if (dev->devtype->product == CODA_960)
2445 ctx->num_internal_frames = 4;
2446 else
2447 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002448 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002449 if (ret < 0) {
2450 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2451 goto out;
2452 }
Javier Martin186b2502012-07-26 05:53:35 -03002453
Philipp Zabelec25f682012-07-20 08:54:29 -03002454 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel10436672012-07-02 09:03:55 -03002455 coda_write(dev, round_up(q_data_src->width, 8), CODA_CMD_SET_FRAME_BUF_STRIDE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002456 if (dev->devtype->product == CODA_7541)
2457 coda_write(dev, round_up(q_data_src->width, 8),
2458 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel10436672012-07-02 09:03:55 -03002459 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002460 coda_write(dev, ctx->iram_info.buf_bit_use,
2461 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2462 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2463 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2464 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2465 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2466 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2467 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2468 coda_write(dev, ctx->iram_info.buf_ovl_use,
2469 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002470 if (dev->devtype->product == CODA_960) {
2471 coda_write(dev, ctx->iram_info.buf_btp_use,
2472 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2473
2474 /* FIXME */
2475 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2476 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2477 }
Philipp Zabel10436672012-07-02 09:03:55 -03002478 }
Philipp Zabel89548442014-07-11 06:36:17 -03002479
Philipp Zabelfcb62822013-05-23 10:43:00 -03002480 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2481 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002482 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002483 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002484 }
2485
2486 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002487 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002488 switch (dst_fourcc) {
2489 case V4L2_PIX_FMT_H264:
2490 /*
2491 * Get SPS in the first frame and copy it to an
2492 * intermediate buffer.
2493 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002494 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2495 &ctx->vpu_header[0][0],
2496 &ctx->vpu_header_size[0]);
2497 if (ret < 0)
2498 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002499
2500 /*
2501 * Get PPS in the first frame and copy it to an
2502 * intermediate buffer.
2503 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002504 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2505 &ctx->vpu_header[1][0],
2506 &ctx->vpu_header_size[1]);
2507 if (ret < 0)
2508 goto out;
2509
Javier Martin3f3f5c72012-10-29 05:20:29 -03002510 /*
2511 * Length of H.264 headers is variable and thus it might not be
2512 * aligned for the coda to append the encoded frame. In that is
2513 * the case a filler NAL must be added to header 2.
2514 */
2515 ctx->vpu_header_size[2] = coda_h264_padding(
2516 (ctx->vpu_header_size[0] +
2517 ctx->vpu_header_size[1]),
2518 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002519 break;
2520 case V4L2_PIX_FMT_MPEG4:
2521 /*
2522 * Get VOS in the first frame and copy it to an
2523 * intermediate buffer
2524 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002525 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2526 &ctx->vpu_header[0][0],
2527 &ctx->vpu_header_size[0]);
2528 if (ret < 0)
2529 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002530
Philipp Zabeld35167a2013-05-23 10:42:59 -03002531 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2532 &ctx->vpu_header[1][0],
2533 &ctx->vpu_header_size[1]);
2534 if (ret < 0)
2535 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002536
Philipp Zabeld35167a2013-05-23 10:42:59 -03002537 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2538 &ctx->vpu_header[2][0],
2539 &ctx->vpu_header_size[2]);
2540 if (ret < 0)
2541 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002542 break;
2543 default:
2544 /* No more formats need to save headers at the moment */
2545 break;
2546 }
2547
Philipp Zabeld35167a2013-05-23 10:42:59 -03002548out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002549 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002550 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002551}
2552
Hans Verkuile37559b2014-04-17 02:47:21 -03002553static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002554{
2555 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002556 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002557
2558 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002559 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002560 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002561 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002562
Philipp Zabel89548442014-07-11 06:36:17 -03002563 if (ctx->inst_type == CODA_INST_DECODER &&
2564 coda_isbusy(dev) && ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX)) {
2565 /* if this decoder instance is running, set the stream end flag */
2566 if (dev->devtype->product == CODA_960) {
2567 u32 val = coda_read(dev, CODA_REG_BIT_BIT_STREAM_PARAM);
2568
2569 val |= CODA_BIT_STREAM_END_FLAG;
2570 coda_write(dev, val, CODA_REG_BIT_BIT_STREAM_PARAM);
2571 ctx->bit_stream_param = val;
2572 }
2573 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002574 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
2575
2576 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002577 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002578 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002579 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002580 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002581
2582 ctx->osequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002583 }
2584
Philipp Zabel918c66f2013-06-27 06:59:01 -03002585 if (!ctx->streamon_out && !ctx->streamon_cap) {
2586 kfifo_init(&ctx->bitstream_fifo,
2587 ctx->bitstream.vaddr, ctx->bitstream.size);
2588 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002589 }
Javier Martin186b2502012-07-26 05:53:35 -03002590}
2591
2592static struct vb2_ops coda_qops = {
2593 .queue_setup = coda_queue_setup,
2594 .buf_prepare = coda_buf_prepare,
2595 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002596 .start_streaming = coda_start_streaming,
2597 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002598 .wait_prepare = vb2_ops_wait_prepare,
2599 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002600};
2601
2602static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2603{
2604 struct coda_ctx *ctx =
2605 container_of(ctrl->handler, struct coda_ctx, ctrls);
2606
2607 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2608 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2609
2610 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002611 case V4L2_CID_HFLIP:
2612 if (ctrl->val)
2613 ctx->params.rot_mode |= CODA_MIR_HOR;
2614 else
2615 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2616 break;
2617 case V4L2_CID_VFLIP:
2618 if (ctrl->val)
2619 ctx->params.rot_mode |= CODA_MIR_VER;
2620 else
2621 ctx->params.rot_mode &= ~CODA_MIR_VER;
2622 break;
Javier Martin186b2502012-07-26 05:53:35 -03002623 case V4L2_CID_MPEG_VIDEO_BITRATE:
2624 ctx->params.bitrate = ctrl->val / 1000;
2625 break;
2626 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2627 ctx->params.gop_size = ctrl->val;
2628 break;
2629 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2630 ctx->params.h264_intra_qp = ctrl->val;
2631 break;
2632 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2633 ctx->params.h264_inter_qp = ctrl->val;
2634 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002635 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2636 ctx->params.h264_min_qp = ctrl->val;
2637 break;
2638 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2639 ctx->params.h264_max_qp = ctrl->val;
2640 break;
Javier Martin186b2502012-07-26 05:53:35 -03002641 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2642 ctx->params.mpeg4_intra_qp = ctrl->val;
2643 break;
2644 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2645 ctx->params.mpeg4_inter_qp = ctrl->val;
2646 break;
2647 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2648 ctx->params.slice_mode = ctrl->val;
2649 break;
2650 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2651 ctx->params.slice_max_mb = ctrl->val;
2652 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002653 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2654 ctx->params.slice_max_bits = ctrl->val * 8;
2655 break;
Javier Martin186b2502012-07-26 05:53:35 -03002656 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2657 break;
2658 default:
2659 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2660 "Invalid control, id=%d, val=%d\n",
2661 ctrl->id, ctrl->val);
2662 return -EINVAL;
2663 }
2664
2665 return 0;
2666}
2667
2668static struct v4l2_ctrl_ops coda_ctrl_ops = {
2669 .s_ctrl = coda_s_ctrl,
2670};
2671
2672static int coda_ctrls_setup(struct coda_ctx *ctx)
2673{
2674 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2675
2676 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002677 V4L2_CID_HFLIP, 0, 1, 1, 0);
2678 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2679 V4L2_CID_VFLIP, 0, 1, 1, 0);
2680 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002681 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2682 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2683 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2684 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002685 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002686 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002687 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002688 if (ctx->dev->devtype->product != CODA_960) {
2689 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2690 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2691 }
2692 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2693 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002694 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2695 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2696 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2697 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2698 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2699 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002700 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2701 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002702 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2703 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002704 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2705 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002706 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2707 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2708 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2709 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2710 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
2711
2712 if (ctx->ctrls.error) {
2713 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2714 ctx->ctrls.error);
2715 return -EINVAL;
2716 }
2717
2718 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2719}
2720
2721static int coda_queue_init(void *priv, struct vb2_queue *src_vq,
2722 struct vb2_queue *dst_vq)
2723{
2724 struct coda_ctx *ctx = priv;
2725 int ret;
2726
Javier Martin186b2502012-07-26 05:53:35 -03002727 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabel419869c2013-05-23 07:06:30 -03002728 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002729 src_vq->drv_priv = ctx;
2730 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2731 src_vq->ops = &coda_qops;
2732 src_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002733 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002734 src_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002735
2736 ret = vb2_queue_init(src_vq);
2737 if (ret)
2738 return ret;
2739
Javier Martin186b2502012-07-26 05:53:35 -03002740 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabel419869c2013-05-23 07:06:30 -03002741 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
Javier Martin186b2502012-07-26 05:53:35 -03002742 dst_vq->drv_priv = ctx;
2743 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2744 dst_vq->ops = &coda_qops;
2745 dst_vq->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03002746 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002747 dst_vq->lock = &ctx->dev->dev_mutex;
Javier Martin186b2502012-07-26 05:53:35 -03002748
2749 return vb2_queue_init(dst_vq);
2750}
2751
Philipp Zabele11f3e62012-07-25 09:16:58 -03002752static int coda_next_free_instance(struct coda_dev *dev)
2753{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002754 int idx = ffz(dev->instance_mask);
2755
2756 if ((idx < 0) ||
2757 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2758 return -EBUSY;
2759
2760 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002761}
2762
Javier Martin186b2502012-07-26 05:53:35 -03002763static int coda_open(struct file *file)
2764{
2765 struct coda_dev *dev = video_drvdata(file);
2766 struct coda_ctx *ctx = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002767 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002768 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002769
Javier Martin186b2502012-07-26 05:53:35 -03002770 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2771 if (!ctx)
2772 return -ENOMEM;
2773
Fabio Estevamf82bc202013-08-21 11:14:16 -03002774 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002775 if (idx < 0) {
2776 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002777 goto err_coda_max;
2778 }
2779 set_bit(idx, &dev->instance_mask);
2780
Philipp Zabel32b6f202014-07-11 06:36:20 -03002781 init_completion(&ctx->completion);
2782 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
2783 INIT_WORK(&ctx->seq_end_work, coda_seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002784 v4l2_fh_init(&ctx->fh, video_devdata(file));
2785 file->private_data = &ctx->fh;
2786 v4l2_fh_add(&ctx->fh);
2787 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002788 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002789 switch (dev->devtype->product) {
2790 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002791 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002792 ctx->reg_idx = 0;
2793 break;
2794 default:
2795 ctx->reg_idx = idx;
2796 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002797
Philipp Zabel1e172732014-07-11 06:36:23 -03002798 /* Power up and upload firmware if necessary */
2799 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2800 if (ret < 0) {
2801 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2802 goto err_pm_get;
2803 }
2804
Fabio Estevam79830db2013-08-21 11:14:17 -03002805 ret = clk_prepare_enable(dev->clk_per);
2806 if (ret)
2807 goto err_clk_per;
2808
2809 ret = clk_prepare_enable(dev->clk_ahb);
2810 if (ret)
2811 goto err_clk_ahb;
2812
Javier Martin186b2502012-07-26 05:53:35 -03002813 set_default_params(ctx);
Philipp Zabel14604e32014-07-11 06:36:22 -03002814 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
Javier Martin186b2502012-07-26 05:53:35 -03002815 &coda_queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002816 if (IS_ERR(ctx->fh.m2m_ctx)) {
2817 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002818
2819 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2820 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002821 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002822 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002823
Javier Martin186b2502012-07-26 05:53:35 -03002824 ret = coda_ctrls_setup(ctx);
2825 if (ret) {
2826 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002827 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002828 }
2829
2830 ctx->fh.ctrl_handler = &ctx->ctrls;
2831
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002832 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE);
2833 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002834 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002835 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002836 }
2837
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002838 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2839 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2840 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2841 if (!ctx->bitstream.vaddr) {
2842 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2843 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002844 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002845 }
2846 kfifo_init(&ctx->bitstream_fifo,
2847 ctx->bitstream.vaddr, ctx->bitstream.size);
2848 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002849 mutex_init(&ctx->buffer_mutex);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002850
Javier Martin186b2502012-07-26 05:53:35 -03002851 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002852 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002853 coda_unlock(ctx);
2854
Javier Martin186b2502012-07-26 05:53:35 -03002855 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2856 ctx->idx, ctx);
2857
2858 return 0;
2859
Fabio Estevamf82bc202013-08-21 11:14:16 -03002860err_dma_writecombine:
2861 coda_free_context_buffers(ctx);
2862 if (ctx->dev->devtype->product == CODA_DX6)
2863 coda_free_aux_buf(dev, &ctx->workbuf);
2864 coda_free_aux_buf(dev, &ctx->parabuf);
2865err_dma_alloc:
2866 v4l2_ctrl_handler_free(&ctx->ctrls);
2867err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002868 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002869err_ctx_init:
2870 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002871err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002872 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002873err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002874 pm_runtime_put_sync(&dev->plat_dev->dev);
2875err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002876 v4l2_fh_del(&ctx->fh);
2877 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002878 clear_bit(ctx->idx, &dev->instance_mask);
2879err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002880 kfree(ctx);
2881 return ret;
2882}
2883
2884static int coda_release(struct file *file)
2885{
2886 struct coda_dev *dev = video_drvdata(file);
2887 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2888
2889 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2890 ctx);
2891
Philipp Zabel918c66f2013-06-27 06:59:01 -03002892 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002893 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002894
2895 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002896 if (ctx->initialized) {
2897 queue_work(dev->workqueue, &ctx->seq_end_work);
2898 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002899 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002900
2901 coda_free_framebuffers(ctx);
2902
Javier Martin186b2502012-07-26 05:53:35 -03002903 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002904 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002905 coda_unlock(ctx);
2906
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002907 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2908 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002909 coda_free_context_buffers(ctx);
2910 if (ctx->dev->devtype->product == CODA_DX6)
2911 coda_free_aux_buf(dev, &ctx->workbuf);
2912
2913 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002914 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002915 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002916 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002917 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002918 v4l2_fh_del(&ctx->fh);
2919 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002920 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002921 kfree(ctx);
2922
2923 return 0;
2924}
2925
Javier Martin186b2502012-07-26 05:53:35 -03002926static const struct v4l2_file_operations coda_fops = {
2927 .owner = THIS_MODULE,
2928 .open = coda_open,
2929 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002930 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002931 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002932 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002933};
2934
Philipp Zabel918c66f2013-06-27 06:59:01 -03002935static void coda_finish_decode(struct coda_ctx *ctx)
2936{
2937 struct coda_dev *dev = ctx->dev;
2938 struct coda_q_data *q_data_src;
2939 struct coda_q_data *q_data_dst;
2940 struct vb2_buffer *dst_buf;
2941 int width, height;
2942 int decoded_idx;
2943 int display_idx;
2944 u32 src_fourcc;
2945 int success;
2946 u32 val;
2947
Philipp Zabel14604e32014-07-11 06:36:22 -03002948 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002949
2950 /* Update kfifo out pointer from coda bitstream read pointer */
2951 coda_kfifo_sync_from_device(ctx);
2952
2953 /*
2954 * in stream-end mode, the read pointer can overshoot the write pointer
2955 * by up to 512 bytes
2956 */
2957 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
2958 if (coda_get_bitstream_payload(ctx) >= 0x100000 - 512)
2959 kfifo_init(&ctx->bitstream_fifo,
2960 ctx->bitstream.vaddr, ctx->bitstream.size);
2961 }
2962
2963 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2964 src_fourcc = q_data_src->fourcc;
2965
2966 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2967 if (val != 1)
2968 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2969
2970 success = val & 0x1;
2971 if (!success)
2972 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2973
2974 if (src_fourcc == V4L2_PIX_FMT_H264) {
2975 if (val & (1 << 3))
2976 v4l2_err(&dev->v4l2_dev,
2977 "insufficient PS buffer space (%d bytes)\n",
2978 ctx->psbuf.size);
2979 if (val & (1 << 2))
2980 v4l2_err(&dev->v4l2_dev,
2981 "insufficient slice buffer space (%d bytes)\n",
2982 ctx->slicebuf.size);
2983 }
2984
2985 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
2986 width = (val >> 16) & 0xffff;
2987 height = val & 0xffff;
2988
2989 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2990
Philipp Zabel52c41672014-07-11 06:36:19 -03002991 /* frame crop information */
2992 if (src_fourcc == V4L2_PIX_FMT_H264) {
2993 u32 left_right;
2994 u32 top_bottom;
2995
2996 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
2997 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
2998
2999 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3000 /* Keep current crop information */
3001 } else {
3002 struct v4l2_rect *rect = &q_data_dst->rect;
3003
3004 rect->left = left_right >> 16 & 0xffff;
3005 rect->top = top_bottom >> 16 & 0xffff;
3006 rect->width = width - rect->left -
3007 (left_right & 0xffff);
3008 rect->height = height - rect->top -
3009 (top_bottom & 0xffff);
3010 }
3011 } else {
3012 /* no cropping */
3013 }
3014
Philipp Zabel918c66f2013-06-27 06:59:01 -03003015 val = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3016 if (val > 0)
3017 v4l2_err(&dev->v4l2_dev,
3018 "errors in %d macroblocks\n", val);
3019
3020 if (dev->devtype->product == CODA_7541) {
3021 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3022 if (val == 0) {
3023 /* not enough bitstream data */
3024 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3025 "prescan failed: %d\n", val);
3026 ctx->prescan_failed = true;
3027 return;
3028 }
3029 }
3030
3031 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3032
3033 /*
3034 * The previous display frame was copied out by the rotator,
3035 * now it can be overwritten again
3036 */
3037 if (ctx->display_idx >= 0 &&
3038 ctx->display_idx < ctx->num_internal_frames) {
3039 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3040 coda_write(dev, ctx->frm_dis_flg,
3041 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3042 }
3043
3044 /*
3045 * The index of the last decoded frame, not necessarily in
3046 * display order, and the index of the next display frame.
3047 * The latter could have been decoded in a previous run.
3048 */
3049 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3050 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3051
3052 if (decoded_idx == -1) {
3053 /* no frame was decoded, but we might have a display frame */
3054 if (display_idx < 0 && ctx->display_idx < 0)
3055 ctx->prescan_failed = true;
3056 } else if (decoded_idx == -2) {
3057 /* no frame was decoded, we still return the remaining buffers */
3058 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3059 v4l2_err(&dev->v4l2_dev,
3060 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003061 } else {
3062 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3063 if (val == 0)
3064 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3065 else if (val == 1)
3066 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3067 else
3068 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003069 }
3070
3071 if (display_idx == -1) {
3072 /*
3073 * no more frames to be decoded, but there could still
3074 * be rotator output to dequeue
3075 */
3076 ctx->prescan_failed = true;
3077 } else if (display_idx == -3) {
3078 /* possibly prescan failure */
3079 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3080 v4l2_err(&dev->v4l2_dev,
3081 "presentation frame index out of range: %d\n",
3082 display_idx);
3083 }
3084
3085 /* If a frame was copied out, return it */
3086 if (ctx->display_idx >= 0 &&
3087 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003088 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003089 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3090
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003091 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3092 V4L2_BUF_FLAG_PFRAME |
3093 V4L2_BUF_FLAG_BFRAME);
3094 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
3095
Philipp Zabel918c66f2013-06-27 06:59:01 -03003096 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3097
3098 v4l2_m2m_buf_done(dst_buf, success ? VB2_BUF_STATE_DONE :
3099 VB2_BUF_STATE_ERROR);
3100
3101 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3102 "job finished: decoding frame (%d) (%s)\n",
3103 dst_buf->v4l2_buf.sequence,
3104 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3105 "KEYFRAME" : "PFRAME");
3106 } else {
3107 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3108 "job finished: no frame decoded\n");
3109 }
3110
3111 /* The rotator will copy the current display frame next time */
3112 ctx->display_idx = display_idx;
3113}
3114
3115static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003116{
Philipp Zabelec25f682012-07-20 08:54:29 -03003117 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003118 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003119 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003120
Philipp Zabel14604e32014-07-11 06:36:22 -03003121 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3122 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003123
3124 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003125 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003126 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3127
Javier Martin186b2502012-07-26 05:53:35 -03003128 /* Calculate bytesused field */
3129 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003130 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3131 ctx->vpu_header_size[0] +
3132 ctx->vpu_header_size[1] +
3133 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003134 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003135 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003136 }
3137
3138 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3139 wr_ptr - start_ptr);
3140
3141 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3142 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3143
Philipp Zabel51660282013-09-30 10:34:49 -03003144 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003145 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3146 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3147 } else {
3148 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3149 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3150 }
3151
Kamil Debskiccd571c2013-04-24 10:38:24 -03003152 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003153 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3154 dst_buf->v4l2_buf.flags |=
3155 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003156 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3157
Philipp Zabelec25f682012-07-20 08:54:29 -03003158 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003159
Philipp Zabel14604e32014-07-11 06:36:22 -03003160 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003161 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3162
3163 ctx->gopcounter--;
3164 if (ctx->gopcounter < 0)
3165 ctx->gopcounter = ctx->params.gop_size - 1;
3166
3167 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3168 "job finished: encoding frame (%d) (%s)\n",
3169 dst_buf->v4l2_buf.sequence,
3170 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3171 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003172}
3173
3174static irqreturn_t coda_irq_handler(int irq, void *data)
3175{
3176 struct coda_dev *dev = data;
3177 struct coda_ctx *ctx;
3178
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003179 /* read status register to attend the IRQ */
3180 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3181 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3182 CODA_REG_BIT_INT_CLEAR);
3183
3184 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3185 if (ctx == NULL) {
3186 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3187 mutex_unlock(&dev->coda_mutex);
3188 return IRQ_HANDLED;
3189 }
3190
3191 if (ctx->aborting) {
3192 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3193 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003194 }
3195
3196 if (coda_isbusy(ctx->dev)) {
3197 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3198 "coda is still busy!!!!\n");
3199 return IRQ_NONE;
3200 }
3201
Philipp Zabel32b6f202014-07-11 06:36:20 -03003202 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003203
3204 return IRQ_HANDLED;
3205}
3206
3207static u32 coda_supported_firmwares[] = {
3208 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003209 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003210 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003211};
3212
3213static bool coda_firmware_supported(u32 vernum)
3214{
3215 int i;
3216
3217 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3218 if (vernum == coda_supported_firmwares[i])
3219 return true;
3220 return false;
3221}
3222
Philipp Zabel87048bb2012-07-02 07:03:43 -03003223static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003224{
Javier Martin186b2502012-07-26 05:53:35 -03003225 u32 data;
3226 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003227 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003228
Fabio Estevam79830db2013-08-21 11:14:17 -03003229 ret = clk_prepare_enable(dev->clk_per);
3230 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003231 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003232
3233 ret = clk_prepare_enable(dev->clk_ahb);
3234 if (ret)
3235 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003236
Javier Martin186b2502012-07-26 05:53:35 -03003237 /*
3238 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003239 * The 16-bit chars in the code buffer are in memory access
3240 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003241 * Data in this SRAM survives a reboot.
3242 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003243 p = (u16 *)dev->codebuf.vaddr;
3244 if (dev->devtype->product == CODA_DX6) {
3245 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3246 data = CODA_DOWN_ADDRESS_SET(i) |
3247 CODA_DOWN_DATA_SET(p[i ^ 1]);
3248 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3249 }
3250 } else {
3251 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3252 data = CODA_DOWN_ADDRESS_SET(i) |
3253 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3254 3 - (i % 4)]);
3255 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3256 }
Javier Martin186b2502012-07-26 05:53:35 -03003257 }
Javier Martin186b2502012-07-26 05:53:35 -03003258
Philipp Zabel9acf7692013-05-23 10:42:56 -03003259 /* Clear registers */
3260 for (i = 0; i < 64; i++)
3261 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3262
Javier Martin186b2502012-07-26 05:53:35 -03003263 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003264 if (dev->devtype->product == CODA_960 ||
3265 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003266 coda_write(dev, dev->tempbuf.paddr,
3267 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003268 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003269 } else {
3270 coda_write(dev, dev->workbuf.paddr,
3271 CODA_REG_BIT_WORK_BUF_ADDR);
3272 }
Javier Martin186b2502012-07-26 05:53:35 -03003273 coda_write(dev, dev->codebuf.paddr,
3274 CODA_REG_BIT_CODE_BUF_ADDR);
3275 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3276
3277 /* Set default values */
3278 switch (dev->devtype->product) {
3279 case CODA_DX6:
3280 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3281 break;
3282 default:
3283 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3284 }
Philipp Zabel89548442014-07-11 06:36:17 -03003285 if (dev->devtype->product == CODA_960)
3286 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3287 else
3288 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003289
3290 if (dev->devtype->product != CODA_DX6)
3291 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3292
Javier Martin186b2502012-07-26 05:53:35 -03003293 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3294 CODA_REG_BIT_INT_ENABLE);
3295
3296 /* Reset VPU and start processor */
3297 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3298 data |= CODA_REG_RESET_ENABLE;
3299 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3300 udelay(10);
3301 data &= ~CODA_REG_RESET_ENABLE;
3302 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3303 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3304
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003305 clk_disable_unprepare(dev->clk_ahb);
3306 clk_disable_unprepare(dev->clk_per);
3307
3308 return 0;
3309
3310err_clk_ahb:
3311 clk_disable_unprepare(dev->clk_per);
3312err_clk_per:
3313 return ret;
3314}
3315
3316static int coda_check_firmware(struct coda_dev *dev)
3317{
3318 u16 product, major, minor, release;
3319 u32 data;
3320 int ret;
3321
3322 ret = clk_prepare_enable(dev->clk_per);
3323 if (ret)
3324 goto err_clk_per;
3325
3326 ret = clk_prepare_enable(dev->clk_ahb);
3327 if (ret)
3328 goto err_clk_ahb;
3329
Javier Martin186b2502012-07-26 05:53:35 -03003330 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3331 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3332 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3333 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3334 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3335 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003336 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003337 ret = -EIO;
3338 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003339 }
3340
Philipp Zabel89548442014-07-11 06:36:17 -03003341 if (dev->devtype->product == CODA_960) {
3342 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3343 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3344 data);
3345 }
3346
Javier Martin186b2502012-07-26 05:53:35 -03003347 /* Check we are compatible with the loaded firmware */
3348 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3349 product = CODA_FIRMWARE_PRODUCT(data);
3350 major = CODA_FIRMWARE_MAJOR(data);
3351 minor = CODA_FIRMWARE_MINOR(data);
3352 release = CODA_FIRMWARE_RELEASE(data);
3353
3354 clk_disable_unprepare(dev->clk_per);
3355 clk_disable_unprepare(dev->clk_ahb);
3356
3357 if (product != dev->devtype->product) {
3358 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3359 " Version: %u.%u.%u\n",
3360 coda_product_name(dev->devtype->product),
3361 coda_product_name(product), major, minor, release);
3362 return -EINVAL;
3363 }
3364
3365 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3366 coda_product_name(product));
3367
3368 if (coda_firmware_supported(data)) {
3369 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3370 major, minor, release);
3371 } else {
3372 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3373 "%u.%u.%u\n", major, minor, release);
3374 }
3375
3376 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003377
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003378err_run_cmd:
3379 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003380err_clk_ahb:
3381 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003382err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003383 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003384}
3385
3386static void coda_fw_callback(const struct firmware *fw, void *context)
3387{
3388 struct coda_dev *dev = context;
3389 struct platform_device *pdev = dev->plat_dev;
3390 int ret;
3391
3392 if (!fw) {
3393 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3394 return;
3395 }
3396
3397 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003398 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size);
3399 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003400 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3401 return;
3402 }
3403
Philipp Zabel87048bb2012-07-02 07:03:43 -03003404 /* Copy the whole firmware image to the code buffer */
3405 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3406 release_firmware(fw);
3407
Philipp Zabel1e172732014-07-11 06:36:23 -03003408 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3409 /*
3410 * Enabling power temporarily will cause coda_hw_init to be
3411 * called via coda_runtime_resume by the pm domain.
3412 */
3413 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3414 if (ret < 0) {
3415 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3416 ret);
3417 return;
3418 }
3419
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003420 ret = coda_check_firmware(dev);
3421 if (ret < 0)
3422 return;
3423
Philipp Zabel1e172732014-07-11 06:36:23 -03003424 pm_runtime_put_sync(&dev->plat_dev->dev);
3425 } else {
3426 /*
3427 * If runtime pm is disabled or pm_domain is not set,
3428 * initialize once manually.
3429 */
3430 ret = coda_hw_init(dev);
3431 if (ret < 0) {
3432 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3433 return;
3434 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003435
3436 ret = coda_check_firmware(dev);
3437 if (ret < 0)
3438 return;
Javier Martin186b2502012-07-26 05:53:35 -03003439 }
3440
3441 dev->vfd.fops = &coda_fops,
3442 dev->vfd.ioctl_ops = &coda_ioctl_ops;
3443 dev->vfd.release = video_device_release_empty,
3444 dev->vfd.lock = &dev->dev_mutex;
3445 dev->vfd.v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03003446 dev->vfd.vfl_dir = VFL_DIR_M2M;
Javier Martin186b2502012-07-26 05:53:35 -03003447 snprintf(dev->vfd.name, sizeof(dev->vfd.name), "%s", CODA_NAME);
3448 video_set_drvdata(&dev->vfd, dev);
3449
3450 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3451 if (IS_ERR(dev->alloc_ctx)) {
3452 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3453 return;
3454 }
3455
3456 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3457 if (IS_ERR(dev->m2m_dev)) {
3458 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3459 goto rel_ctx;
3460 }
3461
3462 ret = video_register_device(&dev->vfd, VFL_TYPE_GRABBER, 0);
3463 if (ret) {
3464 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3465 goto rel_m2m;
3466 }
3467 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video%d\n",
3468 dev->vfd.num);
3469
3470 return;
3471
3472rel_m2m:
3473 v4l2_m2m_release(dev->m2m_dev);
3474rel_ctx:
3475 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3476}
3477
3478static int coda_firmware_request(struct coda_dev *dev)
3479{
3480 char *fw = dev->devtype->firmware;
3481
3482 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3483 coda_product_name(dev->devtype->product));
3484
3485 return request_firmware_nowait(THIS_MODULE, true,
3486 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3487}
3488
3489enum coda_platform {
3490 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003491 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003492 CODA_IMX6Q,
3493 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003494};
3495
Emil Goodec06d8752012-08-14 17:44:42 -03003496static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003497 [CODA_IMX27] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003498 .firmware = "v4l-codadx6-imx27.bin",
3499 .product = CODA_DX6,
3500 .codecs = codadx6_codecs,
3501 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Javier Martin186b2502012-07-26 05:53:35 -03003502 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003503 [CODA_IMX53] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -03003504 .firmware = "v4l-coda7541-imx53.bin",
3505 .product = CODA_7541,
3506 .codecs = coda7_codecs,
3507 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003508 },
Philipp Zabel89548442014-07-11 06:36:17 -03003509 [CODA_IMX6Q] = {
3510 .firmware = "v4l-coda960-imx6q.bin",
3511 .product = CODA_960,
3512 .codecs = coda9_codecs,
3513 .num_codecs = ARRAY_SIZE(coda9_codecs),
3514 },
3515 [CODA_IMX6DL] = {
3516 .firmware = "v4l-coda960-imx6dl.bin",
3517 .product = CODA_960,
3518 .codecs = coda9_codecs,
3519 .num_codecs = ARRAY_SIZE(coda9_codecs),
3520 },
Javier Martin186b2502012-07-26 05:53:35 -03003521};
3522
3523static struct platform_device_id coda_platform_ids[] = {
3524 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003525 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003526 { /* sentinel */ }
3527};
3528MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3529
3530#ifdef CONFIG_OF
3531static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003532 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003533 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003534 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3535 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003536 { /* sentinel */ }
3537};
3538MODULE_DEVICE_TABLE(of, coda_dt_ids);
3539#endif
3540
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003541static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003542{
3543 const struct of_device_id *of_id =
3544 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3545 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003546 struct coda_platform_data *pdata = pdev->dev.platform_data;
3547 struct device_node *np = pdev->dev.of_node;
3548 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003549 struct coda_dev *dev;
3550 struct resource *res;
3551 int ret, irq;
3552
3553 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3554 if (!dev) {
3555 dev_err(&pdev->dev, "Not enough memory for %s\n",
3556 CODA_NAME);
3557 return -ENOMEM;
3558 }
3559
3560 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003561 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003562
3563 dev->plat_dev = pdev;
3564 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3565 if (IS_ERR(dev->clk_per)) {
3566 dev_err(&pdev->dev, "Could not get per clock\n");
3567 return PTR_ERR(dev->clk_per);
3568 }
3569
3570 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3571 if (IS_ERR(dev->clk_ahb)) {
3572 dev_err(&pdev->dev, "Could not get ahb clock\n");
3573 return PTR_ERR(dev->clk_ahb);
3574 }
3575
3576 /* Get memory for physical registers */
3577 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003578 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3579 if (IS_ERR(dev->regs_base))
3580 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003581
3582 /* IRQ */
3583 irq = platform_get_irq(pdev, 0);
3584 if (irq < 0) {
3585 dev_err(&pdev->dev, "failed to get irq resource\n");
3586 return -ENOENT;
3587 }
3588
Philipp Zabel918c66f2013-06-27 06:59:01 -03003589 if (devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
Alexander Shiyan08a72e42014-04-26 06:14:46 -03003590 IRQF_ONESHOT, dev_name(&pdev->dev), dev) < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003591 dev_err(&pdev->dev, "failed to request irq\n");
3592 return -ENOENT;
3593 }
3594
Philipp Zabel657eee72013-04-29 16:17:14 -07003595 /* Get IRAM pool from device tree or platform data */
3596 pool = of_get_named_gen_pool(np, "iram", 0);
3597 if (!pool && pdata)
3598 pool = dev_get_gen_pool(pdata->iram_dev);
3599 if (!pool) {
3600 dev_err(&pdev->dev, "iram pool not available\n");
3601 return -ENOMEM;
3602 }
3603 dev->iram_pool = pool;
3604
Javier Martin186b2502012-07-26 05:53:35 -03003605 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3606 if (ret)
3607 return ret;
3608
3609 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003610 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003611
3612 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3613
3614 if (of_id) {
3615 dev->devtype = of_id->data;
3616 } else if (pdev_id) {
3617 dev->devtype = &coda_devdata[pdev_id->driver_data];
3618 } else {
3619 v4l2_device_unregister(&dev->v4l2_dev);
3620 return -EINVAL;
3621 }
3622
3623 /* allocate auxiliary per-device buffers for the BIT processor */
3624 switch (dev->devtype->product) {
3625 case CODA_DX6:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003626 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
3627 CODADX6_WORK_BUF_SIZE);
3628 if (ret < 0) {
3629 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3630 v4l2_device_unregister(&dev->v4l2_dev);
3631 return ret;
3632 }
Javier Martin186b2502012-07-26 05:53:35 -03003633 break;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003634 case CODA_7541:
3635 dev->tempbuf.size = CODA7_TEMP_BUF_SIZE;
3636 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003637 case CODA_960:
3638 dev->tempbuf.size = CODA9_TEMP_BUF_SIZE;
3639 break;
Javier Martin186b2502012-07-26 05:53:35 -03003640 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003641 if (dev->tempbuf.size) {
3642 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
3643 dev->tempbuf.size);
3644 if (ret < 0) {
3645 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3646 v4l2_device_unregister(&dev->v4l2_dev);
3647 return ret;
3648 }
Javier Martin186b2502012-07-26 05:53:35 -03003649 }
3650
Philipp Zabel918c66f2013-06-27 06:59:01 -03003651 switch (dev->devtype->product) {
3652 case CODA_DX6:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003653 dev->iram.size = CODADX6_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003654 break;
3655 case CODA_7541:
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003656 dev->iram.size = CODA7_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003657 break;
Philipp Zabel89548442014-07-11 06:36:17 -03003658 case CODA_960:
3659 dev->iram.size = CODA9_IRAM_SIZE;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003660 }
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003661 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3662 &dev->iram.paddr);
3663 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003664 dev_err(&pdev->dev, "unable to alloc iram\n");
3665 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003666 }
3667
Philipp Zabel32b6f202014-07-11 06:36:20 -03003668 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3669 if (!dev->workqueue) {
3670 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3671 return -ENOMEM;
3672 }
3673
Javier Martin186b2502012-07-26 05:53:35 -03003674 platform_set_drvdata(pdev, dev);
3675
Philipp Zabel1e172732014-07-11 06:36:23 -03003676 pm_runtime_enable(&pdev->dev);
3677
Javier Martin186b2502012-07-26 05:53:35 -03003678 return coda_firmware_request(dev);
3679}
3680
3681static int coda_remove(struct platform_device *pdev)
3682{
3683 struct coda_dev *dev = platform_get_drvdata(pdev);
3684
3685 video_unregister_device(&dev->vfd);
3686 if (dev->m2m_dev)
3687 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003688 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003689 if (dev->alloc_ctx)
3690 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3691 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003692 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003693 if (dev->iram.vaddr)
3694 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3695 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003696 coda_free_aux_buf(dev, &dev->codebuf);
3697 coda_free_aux_buf(dev, &dev->tempbuf);
3698 coda_free_aux_buf(dev, &dev->workbuf);
Javier Martin186b2502012-07-26 05:53:35 -03003699 return 0;
3700}
3701
Philipp Zabel1e172732014-07-11 06:36:23 -03003702#ifdef CONFIG_PM_RUNTIME
3703static int coda_runtime_resume(struct device *dev)
3704{
3705 struct coda_dev *cdev = dev_get_drvdata(dev);
3706 int ret = 0;
3707
3708 if (dev->pm_domain) {
3709 ret = coda_hw_init(cdev);
3710 if (ret)
3711 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3712 }
3713
3714 return ret;
3715}
3716#endif
3717
3718static const struct dev_pm_ops coda_pm_ops = {
3719 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3720};
3721
Javier Martin186b2502012-07-26 05:53:35 -03003722static struct platform_driver coda_driver = {
3723 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003724 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003725 .driver = {
3726 .name = CODA_NAME,
3727 .owner = THIS_MODULE,
3728 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003729 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003730 },
3731 .id_table = coda_platform_ids,
3732};
3733
3734module_platform_driver(coda_driver);
3735
3736MODULE_LICENSE("GPL");
3737MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3738MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");