blob: 3e232441549bc23715043d08498a32d9297151af [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
Philipp Zabelf61c0b12014-07-11 06:36:40 -030015#include <linux/debugfs.h>
Javier Martin186b2502012-07-26 05:53:35 -030016#include <linux/delay.h>
17#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070018#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030022#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030023#include <linux/module.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030026#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030027#include <linux/slab.h>
28#include <linux/videodev2.h>
29#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070030#include <linux/platform_data/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030031#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030032
33#include <media/v4l2-ctrls.h>
34#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030035#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030036#include <media/v4l2-ioctl.h>
37#include <media/v4l2-mem2mem.h>
38#include <media/videobuf2-core.h>
39#include <media/videobuf2-dma-contig.h>
40
Philipp Zabela2b3e462014-07-23 12:28:39 -030041#include "coda.h"
Philipp Zabele19a7632014-07-23 12:28:38 -030042#include "coda_regs.h"
Javier Martin186b2502012-07-26 05:53:35 -030043
44#define CODA_NAME "coda"
45
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030046#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030047
Javier Martin186b2502012-07-26 05:53:35 -030048#define CODA_PARA_BUF_SIZE (10 * 1024)
49#define CODA_ISRAM_SIZE (2048 * 2)
50
Philipp Zabel918c66f2013-06-27 06:59:01 -030051#define CODA7_PS_BUF_SIZE 0x28000
Philipp Zabel89548442014-07-11 06:36:17 -030052#define CODA9_PS_SAVE_SIZE (512 * 1024)
Philipp Zabel918c66f2013-06-27 06:59:01 -030053
Javier Martin186b2502012-07-26 05:53:35 -030054#define CODA_DEFAULT_GAMMA 4096
Philipp Zabel89548442014-07-11 06:36:17 -030055#define CODA9_DEFAULT_GAMMA 24576 /* 0.75 * 32768 */
Javier Martin186b2502012-07-26 05:53:35 -030056
57#define MIN_W 176
58#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030059
60#define S_ALIGN 1 /* multiple of 2 */
61#define W_ALIGN 1 /* multiple of 2 */
62#define H_ALIGN 1 /* multiple of 2 */
63
64#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
65
66static int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030067module_param(coda_debug, int, 0644);
Javier Martin186b2502012-07-26 05:53:35 -030068MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
69
Javier Martin186b2502012-07-26 05:53:35 -030070struct coda_fmt {
71 char *name;
72 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030073};
74
Javier Martin832fbb52012-10-29 08:34:59 -030075static const u8 coda_filler_nal[14] = { 0x00, 0x00, 0x00, 0x01, 0x0c, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 };
77static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
Javier Martin3f3f5c72012-10-29 05:20:29 -030078
Philipp Zabela2b3e462014-07-23 12:28:39 -030079void coda_write(struct coda_dev *dev, u32 data, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030080{
81 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
82 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
83 writel(data, dev->regs_base + reg);
84}
85
Philipp Zabela2b3e462014-07-23 12:28:39 -030086unsigned int coda_read(struct coda_dev *dev, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030087{
88 u32 data;
89 data = readl(dev->regs_base + reg);
90 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
91 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
92 return data;
93}
94
95static inline unsigned long coda_isbusy(struct coda_dev *dev)
96{
97 return coda_read(dev, CODA_REG_BIT_BUSY);
98}
99
100static inline int coda_is_initialized(struct coda_dev *dev)
101{
102 return (coda_read(dev, CODA_REG_BIT_CUR_PC) != 0);
103}
104
105static int coda_wait_timeout(struct coda_dev *dev)
106{
107 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
108
109 while (coda_isbusy(dev)) {
110 if (time_after(jiffies, timeout))
111 return -ETIMEDOUT;
112 }
113 return 0;
114}
115
116static void coda_command_async(struct coda_ctx *ctx, int cmd)
117{
118 struct coda_dev *dev = ctx->dev;
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300119
Philipp Zabel89548442014-07-11 06:36:17 -0300120 if (dev->devtype->product == CODA_960 ||
121 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300122 /* Restore context related registers to CODA */
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300123 coda_write(dev, ctx->bit_stream_param,
124 CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300125 coda_write(dev, ctx->frm_dis_flg,
126 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
Philipp Zabel89548442014-07-11 06:36:17 -0300127 coda_write(dev, ctx->frame_mem_ctrl,
128 CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300129 coda_write(dev, ctx->workbuf.paddr, CODA_REG_BIT_WORK_BUF_ADDR);
130 }
131
Philipp Zabel89548442014-07-11 06:36:17 -0300132 if (dev->devtype->product == CODA_960) {
133 coda_write(dev, 1, CODA9_GDI_WPROT_ERR_CLR);
134 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
135 }
136
Javier Martin186b2502012-07-26 05:53:35 -0300137 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
138
139 coda_write(dev, ctx->idx, CODA_REG_BIT_RUN_INDEX);
140 coda_write(dev, ctx->params.codec_mode, CODA_REG_BIT_RUN_COD_STD);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300141 coda_write(dev, ctx->params.codec_mode_aux, CODA7_REG_BIT_RUN_AUX_STD);
142
Javier Martin186b2502012-07-26 05:53:35 -0300143 coda_write(dev, cmd, CODA_REG_BIT_RUN_COMMAND);
144}
145
146static int coda_command_sync(struct coda_ctx *ctx, int cmd)
147{
148 struct coda_dev *dev = ctx->dev;
149
150 coda_command_async(ctx, cmd);
151 return coda_wait_timeout(dev);
152}
153
Philipp Zabel8f452842014-07-11 06:36:35 -0300154static int coda_hw_reset(struct coda_ctx *ctx)
155{
156 struct coda_dev *dev = ctx->dev;
157 unsigned long timeout;
158 unsigned int idx;
159 int ret;
160
161 if (!dev->rstc)
162 return -ENOENT;
163
164 idx = coda_read(dev, CODA_REG_BIT_RUN_INDEX);
165
Philipp Zabelae5abd22014-07-18 07:22:35 -0300166 if (dev->devtype->product == CODA_960) {
167 timeout = jiffies + msecs_to_jiffies(100);
168 coda_write(dev, 0x11, CODA9_GDI_BUS_CTRL);
169 while (coda_read(dev, CODA9_GDI_BUS_STATUS) != 0x77) {
170 if (time_after(jiffies, timeout))
171 return -ETIME;
172 cpu_relax();
173 }
Philipp Zabel8f452842014-07-11 06:36:35 -0300174 }
175
176 ret = reset_control_reset(dev->rstc);
177 if (ret < 0)
178 return ret;
179
Philipp Zabelae5abd22014-07-18 07:22:35 -0300180 if (dev->devtype->product == CODA_960)
181 coda_write(dev, 0x00, CODA9_GDI_BUS_CTRL);
Philipp Zabel8f452842014-07-11 06:36:35 -0300182 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
183 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
184 ret = coda_wait_timeout(dev);
185 coda_write(dev, idx, CODA_REG_BIT_RUN_INDEX);
186
187 return ret;
188}
189
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300190static void coda_bit_stream_end_flag(struct coda_ctx *ctx)
191{
192 struct coda_dev *dev = ctx->dev;
193
194 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
195
196 if ((dev->devtype->product == CODA_960) &&
197 coda_isbusy(dev) &&
198 (ctx->idx == coda_read(dev, CODA_REG_BIT_RUN_INDEX))) {
199 /* If this context is currently running, update the hardware flag */
200 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
201 }
202}
203
Javier Martin186b2502012-07-26 05:53:35 -0300204static struct coda_q_data *get_q_data(struct coda_ctx *ctx,
205 enum v4l2_buf_type type)
206{
207 switch (type) {
208 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
209 return &(ctx->q_data[V4L2_M2M_SRC]);
210 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
211 return &(ctx->q_data[V4L2_M2M_DST]);
212 default:
Philipp Zabelb9736292014-07-11 06:36:18 -0300213 return NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300214 }
Javier Martin186b2502012-07-26 05:53:35 -0300215}
216
217/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300218 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300219 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300220static const struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300221 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300222 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300223 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300224 },
225 {
226 .name = "YUV 4:2:0 Planar, YCrCb",
227 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300228 },
229 {
230 .name = "H264 Encoded Stream",
231 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300232 },
233 {
234 .name = "MPEG4 Encoded Stream",
235 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300236 },
237};
238
Philipp Zabelb96904e2013-05-23 10:42:58 -0300239#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
240 { mode, src_fourcc, dst_fourcc, max_w, max_h }
241
242/*
243 * Arrays of codecs supported by each given version of Coda:
244 * i.MX27 -> codadx6
245 * i.MX5x -> coda7
246 * i.MX6 -> coda960
247 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
248 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300249static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300250 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
251 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300252};
253
Philipp Zabel814c3762014-07-18 07:22:45 -0300254static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300255 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
256 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabel918c66f2013-06-27 06:59:01 -0300257 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
258 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300259};
260
Philipp Zabel814c3762014-07-18 07:22:45 -0300261static const struct coda_codec coda9_codecs[] = {
Philipp Zabel89548442014-07-11 06:36:17 -0300262 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1080),
263 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1080),
264 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1080),
265 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1080),
266};
267
Philipp Zabelb96904e2013-05-23 10:42:58 -0300268static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300269{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300270 switch (fourcc) {
271 case V4L2_PIX_FMT_YUV420:
272 case V4L2_PIX_FMT_YVU420:
273 return true;
274 default:
275 return false;
276 }
277}
Javier Martin186b2502012-07-26 05:53:35 -0300278
Philipp Zabelb96904e2013-05-23 10:42:58 -0300279/*
280 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
281 * tables.
282 */
283static u32 coda_format_normalize_yuv(u32 fourcc)
284{
285 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
286}
287
Philipp Zabel814c3762014-07-18 07:22:45 -0300288static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
289 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300290{
Philipp Zabel814c3762014-07-18 07:22:45 -0300291 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300292 int num_codecs = dev->devtype->num_codecs;
293 int k;
294
295 src_fourcc = coda_format_normalize_yuv(src_fourcc);
296 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
297 if (src_fourcc == dst_fourcc)
298 return NULL;
299
300 for (k = 0; k < num_codecs; k++) {
301 if (codecs[k].src_fourcc == src_fourcc &&
302 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300303 break;
304 }
305
Philipp Zabelb96904e2013-05-23 10:42:58 -0300306 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300307 return NULL;
308
Philipp Zabelb96904e2013-05-23 10:42:58 -0300309 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300310}
311
Philipp Zabel57625592013-09-30 10:34:51 -0300312static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300313 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300314 int *max_w, int *max_h)
315{
Philipp Zabel814c3762014-07-18 07:22:45 -0300316 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300317 int num_codecs = dev->devtype->num_codecs;
318 unsigned int w, h;
319 int k;
320
321 if (codec) {
322 w = codec->max_w;
323 h = codec->max_h;
324 } else {
325 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
326 w = max(w, codecs[k].max_w);
327 h = max(h, codecs[k].max_h);
328 }
329 }
330
331 if (max_w)
332 *max_w = w;
333 if (max_h)
334 *max_h = h;
335}
336
Philipp Zabel927933f2013-09-30 10:34:48 -0300337static char *coda_product_name(int product)
338{
339 static char buf[9];
340
341 switch (product) {
342 case CODA_DX6:
343 return "CodaDx6";
344 case CODA_7541:
345 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300346 case CODA_960:
347 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300348 default:
349 snprintf(buf, sizeof(buf), "(0x%04x)", product);
350 return buf;
351 }
352}
353
Javier Martin186b2502012-07-26 05:53:35 -0300354/*
355 * V4L2 ioctl() operations.
356 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300357static int coda_querycap(struct file *file, void *priv,
358 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300359{
Philipp Zabel927933f2013-09-30 10:34:48 -0300360 struct coda_ctx *ctx = fh_to_ctx(priv);
361
Javier Martin186b2502012-07-26 05:53:35 -0300362 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300363 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
364 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300365 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300366 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300367 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
368
369 return 0;
370}
371
Philipp Zabel22e244b2014-07-18 07:22:43 -0300372static int coda_enum_fmt(struct file *file, void *priv,
373 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300374{
375 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300376 const struct coda_codec *codecs = ctx->dev->devtype->codecs;
377 const struct coda_fmt *formats = coda_formats;
378 const struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300379 int num_codecs = ctx->dev->devtype->num_codecs;
380 int num_formats = ARRAY_SIZE(coda_formats);
381 int i, k, num = 0;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300382 bool yuv;
383
384 if (ctx->inst_type == CODA_INST_ENCODER)
385 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
386 else
387 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300388
389 for (i = 0; i < num_formats; i++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300390 /* Skip either raw or compressed formats */
391 if (yuv != coda_format_is_yuv(formats[i].fourcc))
392 continue;
393 /* All uncompressed formats are always supported */
394 if (yuv) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300395 if (num == f->index)
396 break;
397 ++num;
398 continue;
399 }
400 /* Compressed formats may be supported, check the codec list */
401 for (k = 0; k < num_codecs; k++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300402 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
403 formats[i].fourcc == codecs[k].dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300404 break;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300405 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
Philipp Zabelb96904e2013-05-23 10:42:58 -0300406 formats[i].fourcc == codecs[k].src_fourcc)
407 break;
408 }
409 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300410 if (num == f->index)
411 break;
412 ++num;
413 }
414 }
415
416 if (i < num_formats) {
417 fmt = &formats[i];
418 strlcpy(f->description, fmt->name, sizeof(f->description));
419 f->pixelformat = fmt->fourcc;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300420 if (!yuv)
Philipp Zabelbaf70212013-09-30 10:34:46 -0300421 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300422 return 0;
423 }
424
425 /* Format not found */
426 return -EINVAL;
427}
428
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300429static int coda_g_fmt(struct file *file, void *priv,
430 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300431{
Javier Martin186b2502012-07-26 05:53:35 -0300432 struct coda_q_data *q_data;
433 struct coda_ctx *ctx = fh_to_ctx(priv);
434
Javier Martin186b2502012-07-26 05:53:35 -0300435 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300436 if (!q_data)
437 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300438
439 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300440 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300441 f->fmt.pix.width = q_data->width;
442 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300443 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300444
445 f->fmt.pix.sizeimage = q_data->sizeimage;
446 f->fmt.pix.colorspace = ctx->colorspace;
447
448 return 0;
449}
450
Philipp Zabel814c3762014-07-18 07:22:45 -0300451static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300452 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300453{
Philipp Zabel57625592013-09-30 10:34:51 -0300454 struct coda_dev *dev = ctx->dev;
455 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300456 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300457 enum v4l2_field field;
458
459 field = f->fmt.pix.field;
460 if (field == V4L2_FIELD_ANY)
461 field = V4L2_FIELD_NONE;
462 else if (V4L2_FIELD_NONE != field)
463 return -EINVAL;
464
465 /* V4L2 specification suggests the driver corrects the format struct
466 * if any of the dimensions is unsupported */
467 f->fmt.pix.field = field;
468
Philipp Zabel57625592013-09-30 10:34:51 -0300469 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
470 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
471 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
472 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300473
Philipp Zabel57625592013-09-30 10:34:51 -0300474 switch (f->fmt.pix.pixelformat) {
475 case V4L2_PIX_FMT_YUV420:
476 case V4L2_PIX_FMT_YVU420:
477 case V4L2_PIX_FMT_H264:
478 case V4L2_PIX_FMT_MPEG4:
479 case V4L2_PIX_FMT_JPEG:
480 break;
481 default:
482 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300483 if (!q_data)
484 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300485 f->fmt.pix.pixelformat = q_data->fourcc;
486 }
487
488 switch (f->fmt.pix.pixelformat) {
489 case V4L2_PIX_FMT_YUV420:
490 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300491 /* Frame stride must be multiple of 8, but 16 for h.264 */
492 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300493 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300494 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300495 break;
496 case V4L2_PIX_FMT_H264:
497 case V4L2_PIX_FMT_MPEG4:
498 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300499 f->fmt.pix.bytesperline = 0;
500 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300501 break;
502 default:
503 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300504 }
505
506 return 0;
507}
508
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300509static int coda_try_fmt_vid_cap(struct file *file, void *priv,
510 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300511{
Javier Martin186b2502012-07-26 05:53:35 -0300512 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300513 const struct coda_codec *codec = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300514 struct vb2_queue *src_vq;
515 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300516
Philipp Zabel918c66f2013-06-27 06:59:01 -0300517 /*
518 * If the source format is already fixed, try to find a codec that
519 * converts to the given destination format
520 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300521 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300522 if (vb2_is_streaming(src_vq)) {
523 struct coda_q_data *q_data_src;
524
525 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
526 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
527 f->fmt.pix.pixelformat);
528 if (!codec)
529 return -EINVAL;
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300530
531 f->fmt.pix.width = q_data_src->width;
532 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300533 } else {
534 /* Otherwise determine codec by encoded format, if possible */
535 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
536 f->fmt.pix.pixelformat);
537 }
Javier Martin186b2502012-07-26 05:53:35 -0300538
539 f->fmt.pix.colorspace = ctx->colorspace;
540
Philipp Zabel57625592013-09-30 10:34:51 -0300541 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300542 if (ret < 0)
543 return ret;
544
545 /* The h.264 decoder only returns complete 16x16 macroblocks */
546 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300547 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300548 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300549 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300550 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
551 f->fmt.pix.height * 3 / 2;
552 }
553
554 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300555}
556
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300557static int coda_try_fmt_vid_out(struct file *file, void *priv,
558 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300559{
560 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300561 const struct coda_codec *codec;
Javier Martin186b2502012-07-26 05:53:35 -0300562
Philipp Zabelb96904e2013-05-23 10:42:58 -0300563 /* Determine codec by encoded format, returns NULL if raw or invalid */
564 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
565 V4L2_PIX_FMT_YUV420);
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300566 if (!codec && ctx->inst_type == CODA_INST_DECODER) {
567 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_H264,
568 V4L2_PIX_FMT_YUV420);
569 if (!codec)
570 return -EINVAL;
571 }
Javier Martin186b2502012-07-26 05:53:35 -0300572
573 if (!f->fmt.pix.colorspace)
574 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
575
Philipp Zabel57625592013-09-30 10:34:51 -0300576 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300577}
578
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300579static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300580{
581 struct coda_q_data *q_data;
582 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300583
Philipp Zabel14604e32014-07-11 06:36:22 -0300584 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300585 if (!vq)
586 return -EINVAL;
587
588 q_data = get_q_data(ctx, f->type);
589 if (!q_data)
590 return -EINVAL;
591
592 if (vb2_is_busy(vq)) {
593 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
594 return -EBUSY;
595 }
596
Philipp Zabelb96904e2013-05-23 10:42:58 -0300597 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300598 q_data->width = f->fmt.pix.width;
599 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300600 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300601 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300602 q_data->rect.left = 0;
603 q_data->rect.top = 0;
604 q_data->rect.width = f->fmt.pix.width;
605 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300606
607 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
608 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300609 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300610
611 return 0;
612}
613
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300614static int coda_s_fmt_vid_cap(struct file *file, void *priv,
615 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300616{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300617 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300618 int ret;
619
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300620 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300621 if (ret)
622 return ret;
623
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300624 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300625}
626
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300627static int coda_s_fmt_vid_out(struct file *file, void *priv,
628 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300629{
630 struct coda_ctx *ctx = fh_to_ctx(priv);
631 int ret;
632
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300633 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300634 if (ret)
635 return ret;
636
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300637 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300638 if (ret)
639 ctx->colorspace = f->fmt.pix.colorspace;
640
641 return ret;
642}
643
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300644static int coda_qbuf(struct file *file, void *priv,
645 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300646{
647 struct coda_ctx *ctx = fh_to_ctx(priv);
648
Philipp Zabel14604e32014-07-11 06:36:22 -0300649 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300650}
651
Philipp Zabel918c66f2013-06-27 06:59:01 -0300652static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
653 struct v4l2_buffer *buf)
654{
655 struct vb2_queue *src_vq;
656
Philipp Zabel14604e32014-07-11 06:36:22 -0300657 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300658
659 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
660 (buf->sequence == (ctx->qsequence - 1)));
661}
662
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300663static int coda_dqbuf(struct file *file, void *priv,
664 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300665{
666 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300667 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300668
Philipp Zabel14604e32014-07-11 06:36:22 -0300669 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300670
671 /* If this is the last capture buffer, emit an end-of-stream event */
672 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
673 coda_buf_is_end_of_stream(ctx, buf)) {
674 const struct v4l2_event eos_event = {
675 .type = V4L2_EVENT_EOS
676 };
677
678 v4l2_event_queue_fh(&ctx->fh, &eos_event);
679 }
680
681 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300682}
683
Philipp Zabel52c41672014-07-11 06:36:19 -0300684static int coda_g_selection(struct file *file, void *fh,
685 struct v4l2_selection *s)
686{
687 struct coda_ctx *ctx = fh_to_ctx(fh);
688 struct coda_q_data *q_data;
689 struct v4l2_rect r, *rsel;
690
691 q_data = get_q_data(ctx, s->type);
692 if (!q_data)
693 return -EINVAL;
694
695 r.left = 0;
696 r.top = 0;
697 r.width = q_data->width;
698 r.height = q_data->height;
699 rsel = &q_data->rect;
700
701 switch (s->target) {
702 case V4L2_SEL_TGT_CROP_DEFAULT:
703 case V4L2_SEL_TGT_CROP_BOUNDS:
704 rsel = &r;
705 /* fallthrough */
706 case V4L2_SEL_TGT_CROP:
707 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
708 return -EINVAL;
709 break;
710 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
711 case V4L2_SEL_TGT_COMPOSE_PADDED:
712 rsel = &r;
713 /* fallthrough */
714 case V4L2_SEL_TGT_COMPOSE:
715 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
716 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
717 return -EINVAL;
718 break;
719 default:
720 return -EINVAL;
721 }
722
723 s->r = *rsel;
724
725 return 0;
726}
727
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300728static int coda_try_decoder_cmd(struct file *file, void *fh,
729 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300730{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300731 if (dc->cmd != V4L2_DEC_CMD_STOP)
732 return -EINVAL;
733
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300734 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300735 return -EINVAL;
736
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300737 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300738 return -EINVAL;
739
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300740 return 0;
741}
742
743static int coda_decoder_cmd(struct file *file, void *fh,
744 struct v4l2_decoder_cmd *dc)
745{
746 struct coda_ctx *ctx = fh_to_ctx(fh);
747 int ret;
748
749 ret = coda_try_decoder_cmd(file, fh, dc);
750 if (ret < 0)
751 return ret;
752
753 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300754 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300755 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300756
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300757 /* Set the stream-end flag on this context */
758 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300759 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300760 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300761
Philipp Zabel918c66f2013-06-27 06:59:01 -0300762 return 0;
763}
764
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300765static int coda_subscribe_event(struct v4l2_fh *fh,
766 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300767{
768 switch (sub->type) {
769 case V4L2_EVENT_EOS:
770 return v4l2_event_subscribe(fh, sub, 0, NULL);
771 default:
772 return v4l2_ctrl_subscribe_event(fh, sub);
773 }
Javier Martin186b2502012-07-26 05:53:35 -0300774}
775
776static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300777 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300778
Philipp Zabel22e244b2014-07-18 07:22:43 -0300779 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300780 .vidioc_g_fmt_vid_cap = coda_g_fmt,
781 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
782 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300783
Philipp Zabel22e244b2014-07-18 07:22:43 -0300784 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300785 .vidioc_g_fmt_vid_out = coda_g_fmt,
786 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
787 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300788
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300789 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
790 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300791
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300792 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300793 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300794 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300795 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300796
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300797 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
798 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300799
Philipp Zabel52c41672014-07-11 06:36:19 -0300800 .vidioc_g_selection = coda_g_selection,
801
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300802 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300803 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300804
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300805 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300806 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300807};
808
Philipp Zabela1192a12014-07-23 12:28:40 -0300809static int __coda_start_decoding(struct coda_ctx *ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300810
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300811static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
812{
813 return kfifo_len(&ctx->bitstream_fifo);
814}
815
816static void coda_kfifo_sync_from_device(struct coda_ctx *ctx)
817{
818 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
819 struct coda_dev *dev = ctx->dev;
820 u32 rd_ptr;
821
822 rd_ptr = coda_read(dev, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
823 kfifo->out = (kfifo->in & ~kfifo->mask) |
824 (rd_ptr - ctx->bitstream.paddr);
825 if (kfifo->out > kfifo->in)
826 kfifo->out -= kfifo->mask + 1;
827}
828
829static void coda_kfifo_sync_to_device_full(struct coda_ctx *ctx)
830{
831 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
832 struct coda_dev *dev = ctx->dev;
833 u32 rd_ptr, wr_ptr;
834
835 rd_ptr = ctx->bitstream.paddr + (kfifo->out & kfifo->mask);
836 coda_write(dev, rd_ptr, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
837 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
838 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
839}
840
841static void coda_kfifo_sync_to_device_write(struct coda_ctx *ctx)
842{
843 struct __kfifo *kfifo = &ctx->bitstream_fifo.kfifo;
844 struct coda_dev *dev = ctx->dev;
845 u32 wr_ptr;
846
847 wr_ptr = ctx->bitstream.paddr + (kfifo->in & kfifo->mask);
848 coda_write(dev, wr_ptr, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
849}
850
851static int coda_bitstream_queue(struct coda_ctx *ctx, struct vb2_buffer *src_buf)
852{
853 u32 src_size = vb2_get_plane_payload(src_buf, 0);
854 u32 n;
855
856 n = kfifo_in(&ctx->bitstream_fifo, vb2_plane_vaddr(src_buf, 0), src_size);
857 if (n < src_size)
858 return -ENOSPC;
859
860 dma_sync_single_for_device(&ctx->dev->plat_dev->dev, ctx->bitstream.paddr,
861 ctx->bitstream.size, DMA_TO_DEVICE);
862
Philipp Zabel846ced92014-07-11 06:36:31 -0300863 src_buf->v4l2_buf.sequence = ctx->qsequence++;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300864
865 return 0;
866}
867
868static bool coda_bitstream_try_queue(struct coda_ctx *ctx,
869 struct vb2_buffer *src_buf)
870{
871 int ret;
872
873 if (coda_get_bitstream_payload(ctx) +
874 vb2_get_plane_payload(src_buf, 0) + 512 >= ctx->bitstream.size)
875 return false;
876
877 if (vb2_plane_vaddr(src_buf, 0) == NULL) {
878 v4l2_err(&ctx->dev->v4l2_dev, "trying to queue empty buffer\n");
879 return true;
880 }
881
882 ret = coda_bitstream_queue(ctx, src_buf);
883 if (ret < 0) {
884 v4l2_err(&ctx->dev->v4l2_dev, "bitstream buffer overflow\n");
885 return false;
886 }
887 /* Sync read pointer to device */
888 if (ctx == v4l2_m2m_get_curr_priv(ctx->dev->m2m_dev))
889 coda_kfifo_sync_to_device_write(ctx);
890
Philipp Zabel84e23652014-07-11 06:36:34 -0300891 ctx->hold = false;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300892
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300893 return true;
894}
895
896static void coda_fill_bitstream(struct coda_ctx *ctx)
897{
898 struct vb2_buffer *src_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -0300899 struct coda_timestamp *ts;
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300900
Philipp Zabel14604e32014-07-11 06:36:22 -0300901 while (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) > 0) {
902 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300903
904 if (coda_bitstream_try_queue(ctx, src_buf)) {
Philipp Zabel846ced92014-07-11 06:36:31 -0300905 /*
906 * Source buffer is queued in the bitstream ringbuffer;
907 * queue the timestamp and mark source buffer as done
908 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300909 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel846ced92014-07-11 06:36:31 -0300910
911 ts = kmalloc(sizeof(*ts), GFP_KERNEL);
912 if (ts) {
913 ts->sequence = src_buf->v4l2_buf.sequence;
914 ts->timecode = src_buf->v4l2_buf.timecode;
915 ts->timestamp = src_buf->v4l2_buf.timestamp;
916 list_add_tail(&ts->list, &ctx->timestamp_list);
917 }
918
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300919 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
920 } else {
921 break;
922 }
923 }
924}
925
Philipp Zabel89548442014-07-11 06:36:17 -0300926static void coda_set_gdi_regs(struct coda_ctx *ctx)
927{
928 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
929 struct coda_dev *dev = ctx->dev;
930 int i;
931
932 for (i = 0; i < 16; i++)
933 coda_write(dev, tiled_map->xy2ca_map[i],
934 CODA9_GDI_XY2_CAS_0 + 4 * i);
935 for (i = 0; i < 4; i++)
936 coda_write(dev, tiled_map->xy2ba_map[i],
937 CODA9_GDI_XY2_BA_0 + 4 * i);
938 for (i = 0; i < 16; i++)
939 coda_write(dev, tiled_map->xy2ra_map[i],
940 CODA9_GDI_XY2_RAS_0 + 4 * i);
941 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
942 for (i = 0; i < 32; i++)
943 coda_write(dev, tiled_map->rbc2axi_map[i],
944 CODA9_GDI_RBC2_AXI_0 + 4 * i);
945}
946
Javier Martin186b2502012-07-26 05:53:35 -0300947/*
948 * Mem-to-mem operations.
949 */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300950static int coda_prepare_decode(struct coda_ctx *ctx)
951{
952 struct vb2_buffer *dst_buf;
953 struct coda_dev *dev = ctx->dev;
954 struct coda_q_data *q_data_dst;
955 u32 stridey, height;
956 u32 picture_y, picture_cb, picture_cr;
957
Philipp Zabel14604e32014-07-11 06:36:22 -0300958 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300959 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
960
961 if (ctx->params.rot_mode & CODA_ROT_90) {
962 stridey = q_data_dst->height;
963 height = q_data_dst->width;
964 } else {
965 stridey = q_data_dst->width;
966 height = q_data_dst->height;
967 }
968
969 /* Try to copy source buffer contents into the bitstream ringbuffer */
970 mutex_lock(&ctx->bitstream_mutex);
971 coda_fill_bitstream(ctx);
972 mutex_unlock(&ctx->bitstream_mutex);
973
974 if (coda_get_bitstream_payload(ctx) < 512 &&
975 (!(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
976 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
977 "bitstream payload: %d, skipping\n",
978 coda_get_bitstream_payload(ctx));
Philipp Zabel14604e32014-07-11 06:36:22 -0300979 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300980 return -EAGAIN;
981 }
982
983 /* Run coda_start_decoding (again) if not yet initialized */
984 if (!ctx->initialized) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300985 int ret = __coda_start_decoding(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300986 if (ret < 0) {
987 v4l2_err(&dev->v4l2_dev, "failed to start decoding\n");
Philipp Zabel14604e32014-07-11 06:36:22 -0300988 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300989 return -EAGAIN;
990 } else {
991 ctx->initialized = 1;
992 }
993 }
994
Philipp Zabel89548442014-07-11 06:36:17 -0300995 if (dev->devtype->product == CODA_960)
996 coda_set_gdi_regs(ctx);
997
Philipp Zabel918c66f2013-06-27 06:59:01 -0300998 /* Set rotator output */
999 picture_y = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1000 if (q_data_dst->fourcc == V4L2_PIX_FMT_YVU420) {
1001 /* Switch Cr and Cb for YVU420 format */
1002 picture_cr = picture_y + stridey * height;
1003 picture_cb = picture_cr + stridey / 2 * height / 2;
1004 } else {
1005 picture_cb = picture_y + stridey * height;
1006 picture_cr = picture_cb + stridey / 2 * height / 2;
1007 }
Philipp Zabel89548442014-07-11 06:36:17 -03001008
1009 if (dev->devtype->product == CODA_960) {
1010 /*
1011 * The CODA960 seems to have an internal list of buffers with
1012 * 64 entries that includes the registered frame buffers as
1013 * well as the rotator buffer output.
1014 * ROT_INDEX needs to be < 0x40, but > ctx->num_internal_frames.
1015 */
1016 coda_write(dev, CODA_MAX_FRAMEBUFFERS + dst_buf->v4l2_buf.index,
1017 CODA9_CMD_DEC_PIC_ROT_INDEX);
1018 coda_write(dev, picture_y, CODA9_CMD_DEC_PIC_ROT_ADDR_Y);
1019 coda_write(dev, picture_cb, CODA9_CMD_DEC_PIC_ROT_ADDR_CB);
1020 coda_write(dev, picture_cr, CODA9_CMD_DEC_PIC_ROT_ADDR_CR);
1021 coda_write(dev, stridey, CODA9_CMD_DEC_PIC_ROT_STRIDE);
1022 } else {
1023 coda_write(dev, picture_y, CODA_CMD_DEC_PIC_ROT_ADDR_Y);
1024 coda_write(dev, picture_cb, CODA_CMD_DEC_PIC_ROT_ADDR_CB);
1025 coda_write(dev, picture_cr, CODA_CMD_DEC_PIC_ROT_ADDR_CR);
1026 coda_write(dev, stridey, CODA_CMD_DEC_PIC_ROT_STRIDE);
1027 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001028 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode,
1029 CODA_CMD_DEC_PIC_ROT_MODE);
1030
1031 switch (dev->devtype->product) {
1032 case CODA_DX6:
1033 /* TBD */
1034 case CODA_7541:
1035 coda_write(dev, CODA_PRE_SCAN_EN, CODA_CMD_DEC_PIC_OPTION);
1036 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001037 case CODA_960:
1038 coda_write(dev, (1 << 10), CODA_CMD_DEC_PIC_OPTION); /* 'hardcode to use interrupt disable mode'? */
1039 break;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001040 }
1041
1042 coda_write(dev, 0, CODA_CMD_DEC_PIC_SKIP_NUM);
1043
1044 coda_write(dev, 0, CODA_CMD_DEC_PIC_BB_START);
1045 coda_write(dev, 0, CODA_CMD_DEC_PIC_START_BYTE);
1046
Philipp Zabel8a82c6b2014-07-23 12:28:41 -03001047 if (dev->devtype->product != CODA_DX6)
1048 coda_write(dev, ctx->iram_info.axi_sram_use,
1049 CODA7_REG_BIT_AXI_SRAM_USE);
1050
1051 coda_kfifo_sync_to_device_full(ctx);
1052 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1053
Philipp Zabel918c66f2013-06-27 06:59:01 -03001054 return 0;
1055}
1056
Philipp Zabela1192a12014-07-23 12:28:40 -03001057static int coda_prepare_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001058{
Javier Martin186b2502012-07-26 05:53:35 -03001059 struct coda_q_data *q_data_src, *q_data_dst;
1060 struct vb2_buffer *src_buf, *dst_buf;
1061 struct coda_dev *dev = ctx->dev;
1062 int force_ipicture;
1063 int quant_param = 0;
1064 u32 picture_y, picture_cb, picture_cr;
1065 u32 pic_stream_buffer_addr, pic_stream_buffer_size;
1066 u32 dst_fourcc;
1067
Philipp Zabel14604e32014-07-11 06:36:22 -03001068 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
1069 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001070 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1071 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001072 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001073
Philipp Zabel918c66f2013-06-27 06:59:01 -03001074 src_buf->v4l2_buf.sequence = ctx->osequence;
1075 dst_buf->v4l2_buf.sequence = ctx->osequence;
1076 ctx->osequence++;
Javier Martin186b2502012-07-26 05:53:35 -03001077
1078 /*
1079 * Workaround coda firmware BUG that only marks the first
1080 * frame as IDR. This is a problem for some decoders that can't
1081 * recover when a frame is lost.
1082 */
1083 if (src_buf->v4l2_buf.sequence % ctx->params.gop_size) {
1084 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
1085 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
1086 } else {
1087 src_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
1088 src_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
1089 }
1090
Philipp Zabel89548442014-07-11 06:36:17 -03001091 if (dev->devtype->product == CODA_960)
1092 coda_set_gdi_regs(ctx);
1093
Javier Martin186b2502012-07-26 05:53:35 -03001094 /*
1095 * Copy headers at the beginning of the first frame for H.264 only.
1096 * In MPEG4 they are already copied by the coda.
1097 */
1098 if (src_buf->v4l2_buf.sequence == 0) {
1099 pic_stream_buffer_addr =
1100 vb2_dma_contig_plane_dma_addr(dst_buf, 0) +
1101 ctx->vpu_header_size[0] +
1102 ctx->vpu_header_size[1] +
1103 ctx->vpu_header_size[2];
1104 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE -
1105 ctx->vpu_header_size[0] -
1106 ctx->vpu_header_size[1] -
1107 ctx->vpu_header_size[2];
1108 memcpy(vb2_plane_vaddr(dst_buf, 0),
1109 &ctx->vpu_header[0][0], ctx->vpu_header_size[0]);
1110 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0],
1111 &ctx->vpu_header[1][0], ctx->vpu_header_size[1]);
1112 memcpy(vb2_plane_vaddr(dst_buf, 0) + ctx->vpu_header_size[0] +
1113 ctx->vpu_header_size[1], &ctx->vpu_header[2][0],
1114 ctx->vpu_header_size[2]);
1115 } else {
1116 pic_stream_buffer_addr =
1117 vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1118 pic_stream_buffer_size = CODA_MAX_FRAME_SIZE;
1119 }
1120
1121 if (src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) {
1122 force_ipicture = 1;
1123 switch (dst_fourcc) {
1124 case V4L2_PIX_FMT_H264:
1125 quant_param = ctx->params.h264_intra_qp;
1126 break;
1127 case V4L2_PIX_FMT_MPEG4:
1128 quant_param = ctx->params.mpeg4_intra_qp;
1129 break;
1130 default:
1131 v4l2_warn(&ctx->dev->v4l2_dev,
1132 "cannot set intra qp, fmt not supported\n");
1133 break;
1134 }
1135 } else {
1136 force_ipicture = 0;
1137 switch (dst_fourcc) {
1138 case V4L2_PIX_FMT_H264:
1139 quant_param = ctx->params.h264_inter_qp;
1140 break;
1141 case V4L2_PIX_FMT_MPEG4:
1142 quant_param = ctx->params.mpeg4_inter_qp;
1143 break;
1144 default:
1145 v4l2_warn(&ctx->dev->v4l2_dev,
1146 "cannot set inter qp, fmt not supported\n");
1147 break;
1148 }
1149 }
1150
1151 /* submit */
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001152 coda_write(dev, CODA_ROT_MIR_ENABLE | ctx->params.rot_mode, CODA_CMD_ENC_PIC_ROT_MODE);
Javier Martin186b2502012-07-26 05:53:35 -03001153 coda_write(dev, quant_param, CODA_CMD_ENC_PIC_QS);
1154
1155
1156 picture_y = vb2_dma_contig_plane_dma_addr(src_buf, 0);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001157 switch (q_data_src->fourcc) {
1158 case V4L2_PIX_FMT_YVU420:
1159 /* Switch Cb and Cr for YVU420 format */
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001160 picture_cr = picture_y + q_data_src->bytesperline *
1161 q_data_src->height;
1162 picture_cb = picture_cr + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001163 q_data_src->height / 2;
1164 break;
1165 case V4L2_PIX_FMT_YUV420:
1166 default:
Philipp Zabel2fd6a372014-07-11 06:36:36 -03001167 picture_cb = picture_y + q_data_src->bytesperline *
1168 q_data_src->height;
1169 picture_cr = picture_cb + q_data_src->bytesperline / 2 *
Philipp Zabelb96904e2013-05-23 10:42:58 -03001170 q_data_src->height / 2;
1171 break;
1172 }
Javier Martin186b2502012-07-26 05:53:35 -03001173
Philipp Zabel89548442014-07-11 06:36:17 -03001174 if (dev->devtype->product == CODA_960) {
1175 coda_write(dev, 4/*FIXME: 0*/, CODA9_CMD_ENC_PIC_SRC_INDEX);
1176 coda_write(dev, q_data_src->width, CODA9_CMD_ENC_PIC_SRC_STRIDE);
1177 coda_write(dev, 0, CODA9_CMD_ENC_PIC_SUB_FRAME_SYNC);
1178
1179 coda_write(dev, picture_y, CODA9_CMD_ENC_PIC_SRC_ADDR_Y);
1180 coda_write(dev, picture_cb, CODA9_CMD_ENC_PIC_SRC_ADDR_CB);
1181 coda_write(dev, picture_cr, CODA9_CMD_ENC_PIC_SRC_ADDR_CR);
1182 } else {
1183 coda_write(dev, picture_y, CODA_CMD_ENC_PIC_SRC_ADDR_Y);
1184 coda_write(dev, picture_cb, CODA_CMD_ENC_PIC_SRC_ADDR_CB);
1185 coda_write(dev, picture_cr, CODA_CMD_ENC_PIC_SRC_ADDR_CR);
1186 }
Javier Martin186b2502012-07-26 05:53:35 -03001187 coda_write(dev, force_ipicture << 1 & 0x2,
1188 CODA_CMD_ENC_PIC_OPTION);
1189
1190 coda_write(dev, pic_stream_buffer_addr, CODA_CMD_ENC_PIC_BB_START);
1191 coda_write(dev, pic_stream_buffer_size / 1024,
1192 CODA_CMD_ENC_PIC_BB_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03001193
1194 if (!ctx->streamon_out) {
1195 /* After streamoff on the output side, set the stream end flag */
1196 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG;
1197 coda_write(dev, ctx->bit_stream_param, CODA_REG_BIT_BIT_STREAM_PARAM);
1198 }
Philipp Zabela1192a12014-07-23 12:28:40 -03001199
Philipp Zabel8a82c6b2014-07-23 12:28:41 -03001200 if (dev->devtype->product != CODA_DX6)
1201 coda_write(dev, ctx->iram_info.axi_sram_use,
1202 CODA7_REG_BIT_AXI_SRAM_USE);
1203
1204 coda_command_async(ctx, CODA_COMMAND_PIC_RUN);
1205
Philipp Zabela1192a12014-07-23 12:28:40 -03001206 return 0;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001207}
1208
1209static void coda_device_run(void *m2m_priv)
1210{
1211 struct coda_ctx *ctx = m2m_priv;
1212 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001213
1214 queue_work(dev->workqueue, &ctx->pic_run_work);
1215}
1216
1217static void coda_free_framebuffers(struct coda_ctx *ctx);
1218static void coda_free_context_buffers(struct coda_ctx *ctx);
1219
1220static void coda_seq_end_work(struct work_struct *work)
1221{
1222 struct coda_ctx *ctx = container_of(work, struct coda_ctx, seq_end_work);
1223 struct coda_dev *dev = ctx->dev;
1224
1225 mutex_lock(&ctx->buffer_mutex);
1226 mutex_lock(&dev->coda_mutex);
1227
1228 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
1229 "%d: %s: sent command 'SEQ_END' to coda\n", ctx->idx, __func__);
1230 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_END)) {
1231 v4l2_err(&dev->v4l2_dev,
1232 "CODA_COMMAND_SEQ_END failed\n");
1233 }
1234
1235 kfifo_init(&ctx->bitstream_fifo,
1236 ctx->bitstream.vaddr, ctx->bitstream.size);
1237
1238 coda_free_framebuffers(ctx);
1239 coda_free_context_buffers(ctx);
1240
1241 mutex_unlock(&dev->coda_mutex);
1242 mutex_unlock(&ctx->buffer_mutex);
1243}
1244
1245static void coda_finish_decode(struct coda_ctx *ctx);
1246static void coda_finish_encode(struct coda_ctx *ctx);
1247
1248static void coda_pic_run_work(struct work_struct *work)
1249{
1250 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
1251 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001252 int ret;
1253
1254 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -03001255 mutex_lock(&dev->coda_mutex);
1256
Philipp Zabela1192a12014-07-23 12:28:40 -03001257 ret = ctx->ops->prepare_run(ctx);
1258 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
1259 mutex_unlock(&dev->coda_mutex);
1260 mutex_unlock(&ctx->buffer_mutex);
1261 /* job_finish scheduled by prepare_decode */
1262 return;
Philipp Zabel10436672012-07-02 09:03:55 -03001263 }
1264
Philipp Zabel32b6f202014-07-11 06:36:20 -03001265 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
1266 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -03001267
1268 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -03001269
1270 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001271 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -03001272 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001273 }
1274
1275 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
1276 queue_work(dev->workqueue, &ctx->seq_end_work);
1277
1278 mutex_unlock(&dev->coda_mutex);
1279 mutex_unlock(&ctx->buffer_mutex);
1280
Philipp Zabel14604e32014-07-11 06:36:22 -03001281 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001282}
1283
1284static int coda_job_ready(void *m2m_priv)
1285{
1286 struct coda_ctx *ctx = m2m_priv;
1287
1288 /*
1289 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001290 * and 1 frame are needed. In the decoder case,
1291 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -03001292 */
Philipp Zabel14604e32014-07-11 06:36:22 -03001293 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001294 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001295 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1296 "not ready: not enough video buffers.\n");
1297 return 0;
1298 }
1299
Philipp Zabel14604e32014-07-11 06:36:22 -03001300 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001301 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1302 "not ready: not enough video capture buffers.\n");
1303 return 0;
1304 }
1305
Philipp Zabel84e23652014-07-11 06:36:34 -03001306 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -03001307 ((ctx->inst_type == CODA_INST_DECODER) &&
1308 (coda_get_bitstream_payload(ctx) < 512) &&
1309 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
1310 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1311 "%d: not ready: not enough bitstream data.\n",
1312 ctx->idx);
1313 return 0;
1314 }
1315
Philipp Zabel3e748262013-05-23 10:43:01 -03001316 if (ctx->aborting) {
1317 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1318 "not ready: aborting\n");
1319 return 0;
1320 }
1321
Javier Martin186b2502012-07-26 05:53:35 -03001322 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1323 "job ready\n");
1324 return 1;
1325}
1326
1327static void coda_job_abort(void *priv)
1328{
1329 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001330
1331 ctx->aborting = 1;
1332
1333 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1334 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001335}
1336
1337static void coda_lock(void *m2m_priv)
1338{
1339 struct coda_ctx *ctx = m2m_priv;
1340 struct coda_dev *pcdev = ctx->dev;
1341 mutex_lock(&pcdev->dev_mutex);
1342}
1343
1344static void coda_unlock(void *m2m_priv)
1345{
1346 struct coda_ctx *ctx = m2m_priv;
1347 struct coda_dev *pcdev = ctx->dev;
1348 mutex_unlock(&pcdev->dev_mutex);
1349}
1350
Philipp Zabel814c3762014-07-18 07:22:45 -03001351static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001352 .device_run = coda_device_run,
1353 .job_ready = coda_job_ready,
1354 .job_abort = coda_job_abort,
1355 .lock = coda_lock,
1356 .unlock = coda_unlock,
1357};
1358
Philipp Zabel89548442014-07-11 06:36:17 -03001359static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1360{
1361 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1362 int luma_map, chro_map, i;
1363
1364 memset(tiled_map, 0, sizeof(*tiled_map));
1365
1366 luma_map = 64;
1367 chro_map = 64;
1368 tiled_map->map_type = tiled_map_type;
1369 for (i = 0; i < 16; i++)
1370 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1371 for (i = 0; i < 4; i++)
1372 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1373 for (i = 0; i < 16; i++)
1374 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1375
1376 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1377 tiled_map->xy2rbc_config = 0;
1378 } else {
1379 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1380 tiled_map_type);
1381 return;
1382 }
1383}
1384
Javier Martin186b2502012-07-26 05:53:35 -03001385static void set_default_params(struct coda_ctx *ctx)
1386{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001387 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001388 int max_w;
1389 int max_h;
1390
Philipp Zabel121cacf2014-07-18 07:22:42 -03001391 if (ctx->inst_type == CODA_INST_ENCODER) {
1392 src_fourcc = V4L2_PIX_FMT_YUV420;
1393 dst_fourcc = V4L2_PIX_FMT_H264;
1394 } else {
1395 src_fourcc = V4L2_PIX_FMT_H264;
1396 dst_fourcc = V4L2_PIX_FMT_YUV420;
1397 }
1398 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001399 max_w = ctx->codec->max_w;
1400 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -03001401
Philipp Zabel121cacf2014-07-18 07:22:42 -03001402 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001403 ctx->colorspace = V4L2_COLORSPACE_REC709;
1404 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001405 ctx->aborting = 0;
1406
1407 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001408 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1409 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1410 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1411 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001412 ctx->q_data[V4L2_M2M_DST].width = max_w;
1413 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001414 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1415 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
1416 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
1417 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
1418 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
1419 } else {
1420 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
1421 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
1422 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
1423 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
1424 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001425 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1426 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1427 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1428 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001429
1430 if (ctx->dev->devtype->product == CODA_960)
1431 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001432}
1433
1434/*
1435 * Queue operations
1436 */
1437static int coda_queue_setup(struct vb2_queue *vq,
1438 const struct v4l2_format *fmt,
1439 unsigned int *nbuffers, unsigned int *nplanes,
1440 unsigned int sizes[], void *alloc_ctxs[])
1441{
1442 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001443 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001444 unsigned int size;
1445
Philipp Zabele34db062012-08-29 08:22:00 -03001446 q_data = get_q_data(ctx, vq->type);
1447 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001448
1449 *nplanes = 1;
1450 sizes[0] = size;
1451
1452 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1453
1454 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1455 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1456
1457 return 0;
1458}
1459
1460static int coda_buf_prepare(struct vb2_buffer *vb)
1461{
1462 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1463 struct coda_q_data *q_data;
1464
1465 q_data = get_q_data(ctx, vb->vb2_queue->type);
1466
1467 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1468 v4l2_warn(&ctx->dev->v4l2_dev,
1469 "%s data will not fit into plane (%lu < %lu)\n",
1470 __func__, vb2_plane_size(vb, 0),
1471 (long)q_data->sizeimage);
1472 return -EINVAL;
1473 }
1474
Javier Martin186b2502012-07-26 05:53:35 -03001475 return 0;
1476}
1477
1478static void coda_buf_queue(struct vb2_buffer *vb)
1479{
1480 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001481 struct coda_q_data *q_data;
1482
1483 q_data = get_q_data(ctx, vb->vb2_queue->type);
1484
1485 /*
1486 * In the decoder case, immediately try to copy the buffer into the
1487 * bitstream ringbuffer and mark it as ready to be dequeued.
1488 */
1489 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1490 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1491 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001492 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001493 * the stream end
1494 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001495 if (vb2_get_plane_payload(vb, 0) == 0)
1496 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001497 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001498 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001499 if (vb2_is_streaming(vb->vb2_queue))
1500 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001501 mutex_unlock(&ctx->bitstream_mutex);
1502 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001503 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001504 }
Javier Martin186b2502012-07-26 05:53:35 -03001505}
1506
Philipp Zabel86eda902013-05-23 10:42:57 -03001507static void coda_parabuf_write(struct coda_ctx *ctx, int index, u32 value)
1508{
1509 struct coda_dev *dev = ctx->dev;
1510 u32 *p = ctx->parabuf.vaddr;
1511
1512 if (dev->devtype->product == CODA_DX6)
1513 p[index] = value;
1514 else
1515 p[index ^ 1] = value;
1516}
1517
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001518static int coda_alloc_aux_buf(struct coda_dev *dev,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001519 struct coda_aux_buf *buf, size_t size,
1520 const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001521{
1522 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1523 GFP_KERNEL);
1524 if (!buf->vaddr)
1525 return -ENOMEM;
1526
1527 buf->size = size;
1528
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001529 if (name && parent) {
1530 buf->blob.data = buf->vaddr;
1531 buf->blob.size = size;
1532 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
1533 if (!buf->dentry)
1534 dev_warn(&dev->plat_dev->dev,
1535 "failed to create debugfs entry %s\n", name);
1536 }
1537
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001538 return 0;
1539}
1540
1541static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001542 struct coda_aux_buf *buf, size_t size,
1543 const char *name)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001544{
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001545 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001546}
1547
1548static void coda_free_aux_buf(struct coda_dev *dev,
1549 struct coda_aux_buf *buf)
1550{
1551 if (buf->vaddr) {
1552 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1553 buf->vaddr, buf->paddr);
1554 buf->vaddr = NULL;
1555 buf->size = 0;
1556 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001557 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001558}
1559
1560static void coda_free_framebuffers(struct coda_ctx *ctx)
1561{
1562 int i;
1563
1564 for (i = 0; i < CODA_MAX_FRAMEBUFFERS; i++)
1565 coda_free_aux_buf(ctx->dev, &ctx->internal_frames[i]);
1566}
1567
Philipp Zabelec25f682012-07-20 08:54:29 -03001568static int coda_alloc_framebuffers(struct coda_ctx *ctx, struct coda_q_data *q_data, u32 fourcc)
1569{
1570 struct coda_dev *dev = ctx->dev;
Philipp Zabel7c3df782014-07-11 06:36:38 -03001571 int width, height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001572 dma_addr_t paddr;
1573 int ysize;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001574 int ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001575 int i;
1576
Philipp Zabel7c3df782014-07-11 06:36:38 -03001577 if (ctx->codec && (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 ||
1578 ctx->codec->dst_fourcc == V4L2_PIX_FMT_H264)) {
1579 width = round_up(q_data->width, 16);
1580 height = round_up(q_data->height, 16);
1581 } else {
1582 width = round_up(q_data->width, 8);
1583 height = q_data->height;
1584 }
1585 ysize = width * height;
Philipp Zabel86eda902013-05-23 10:42:57 -03001586
Philipp Zabelec25f682012-07-20 08:54:29 -03001587 /* Allocate frame buffers */
Philipp Zabelec25f682012-07-20 08:54:29 -03001588 for (i = 0; i < ctx->num_internal_frames; i++) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001589 size_t size;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001590 char *name;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001591
Philipp Zabelaed14b02014-07-11 06:36:15 -03001592 size = ysize + ysize / 2;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001593 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1594 dev->devtype->product != CODA_DX6)
Philipp Zabelaed14b02014-07-11 06:36:15 -03001595 size += ysize / 4;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001596 name = kasprintf(GFP_KERNEL, "fb%d", i);
1597 ret = coda_alloc_context_buf(ctx, &ctx->internal_frames[i],
1598 size, name);
1599 kfree(name);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001600 if (ret < 0) {
Philipp Zabelec25f682012-07-20 08:54:29 -03001601 coda_free_framebuffers(ctx);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001602 return ret;
Philipp Zabelec25f682012-07-20 08:54:29 -03001603 }
1604 }
1605
1606 /* Register frame buffers in the parameter buffer */
Philipp Zabel86eda902013-05-23 10:42:57 -03001607 for (i = 0; i < ctx->num_internal_frames; i++) {
1608 paddr = ctx->internal_frames[i].paddr;
1609 coda_parabuf_write(ctx, i * 3 + 0, paddr); /* Y */
1610 coda_parabuf_write(ctx, i * 3 + 1, paddr + ysize); /* Cb */
1611 coda_parabuf_write(ctx, i * 3 + 2, paddr + ysize + ysize/4); /* Cr */
Philipp Zabelec25f682012-07-20 08:54:29 -03001612
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001613 /* mvcol buffer for h.264 */
1614 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_H264 &&
1615 dev->devtype->product != CODA_DX6)
1616 coda_parabuf_write(ctx, 96 + i,
1617 ctx->internal_frames[i].paddr +
1618 ysize + ysize/4 + ysize/4);
Philipp Zabelec25f682012-07-20 08:54:29 -03001619 }
1620
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001621 /* mvcol buffer for mpeg4 */
1622 if ((dev->devtype->product != CODA_DX6) &&
1623 (ctx->codec->src_fourcc == V4L2_PIX_FMT_MPEG4))
1624 coda_parabuf_write(ctx, 97, ctx->internal_frames[i].paddr +
1625 ysize + ysize/4 + ysize/4);
1626
Philipp Zabelec25f682012-07-20 08:54:29 -03001627 return 0;
1628}
1629
Javier Martin3f3f5c72012-10-29 05:20:29 -03001630static int coda_h264_padding(int size, char *p)
1631{
Javier Martin3f3f5c72012-10-29 05:20:29 -03001632 int nal_size;
1633 int diff;
1634
Javier Martin832fbb52012-10-29 08:34:59 -03001635 diff = size - (size & ~0x7);
Javier Martin3f3f5c72012-10-29 05:20:29 -03001636 if (diff == 0)
1637 return 0;
1638
Javier Martin832fbb52012-10-29 08:34:59 -03001639 nal_size = coda_filler_size[diff];
Javier Martin3f3f5c72012-10-29 05:20:29 -03001640 memcpy(p, coda_filler_nal, nal_size);
1641
1642 /* Add rbsp stop bit and trailing at the end */
1643 *(p + nal_size - 1) = 0x80;
1644
1645 return nal_size;
1646}
1647
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001648static phys_addr_t coda_iram_alloc(struct coda_iram_info *iram, size_t size)
1649{
1650 phys_addr_t ret;
1651
1652 size = round_up(size, 1024);
1653 if (size > iram->remaining)
1654 return 0;
1655 iram->remaining -= size;
1656
1657 ret = iram->next_paddr;
1658 iram->next_paddr += size;
1659
1660 return ret;
1661}
1662
Philipp Zabelc2d22512013-06-21 03:55:28 -03001663static void coda_setup_iram(struct coda_ctx *ctx)
1664{
1665 struct coda_iram_info *iram_info = &ctx->iram_info;
1666 struct coda_dev *dev = ctx->dev;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001667 int mb_width;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001668 int dbk_bits;
1669 int bit_bits;
1670 int ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001671
1672 memset(iram_info, 0, sizeof(*iram_info));
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001673 iram_info->next_paddr = dev->iram.paddr;
1674 iram_info->remaining = dev->iram.size;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001675
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001676 switch (dev->devtype->product) {
1677 case CODA_7541:
1678 dbk_bits = CODA7_USE_HOST_DBK_ENABLE | CODA7_USE_DBK_ENABLE;
1679 bit_bits = CODA7_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1680 ip_bits = CODA7_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1681 break;
Philipp Zabel89548442014-07-11 06:36:17 -03001682 case CODA_960:
1683 dbk_bits = CODA9_USE_HOST_DBK_ENABLE | CODA9_USE_DBK_ENABLE;
1684 bit_bits = CODA9_USE_HOST_BIT_ENABLE | CODA7_USE_BIT_ENABLE;
1685 ip_bits = CODA9_USE_HOST_IP_ENABLE | CODA7_USE_IP_ENABLE;
1686 break;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001687 default: /* CODA_DX6 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001688 return;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001689 }
Philipp Zabelc2d22512013-06-21 03:55:28 -03001690
1691 if (ctx->inst_type == CODA_INST_ENCODER) {
1692 struct coda_q_data *q_data_src;
1693
1694 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1695 mb_width = DIV_ROUND_UP(q_data_src->width, 16);
1696
1697 /* Prioritize in case IRAM is too small for everything */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001698 if (dev->devtype->product == CODA_7541) {
1699 iram_info->search_ram_size = round_up(mb_width * 16 *
1700 36 + 2048, 1024);
1701 iram_info->search_ram_paddr = coda_iram_alloc(iram_info,
1702 iram_info->search_ram_size);
1703 if (!iram_info->search_ram_paddr) {
1704 pr_err("IRAM is smaller than the search ram size\n");
1705 goto out;
1706 }
1707 iram_info->axi_sram_use |= CODA7_USE_HOST_ME_ENABLE |
1708 CODA7_USE_ME_ENABLE;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001709 }
1710
1711 /* Only H.264BP and H.263P3 are considered */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001712 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 64 * mb_width);
1713 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 64 * mb_width);
1714 if (!iram_info->buf_dbk_c_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001715 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001716 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001717
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001718 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1719 if (!iram_info->buf_bit_use)
Philipp Zabelc2d22512013-06-21 03:55:28 -03001720 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001721 iram_info->axi_sram_use |= bit_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001722
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001723 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1724 if (!iram_info->buf_ip_ac_dc_use)
1725 goto out;
1726 iram_info->axi_sram_use |= ip_bits;
Philipp Zabelc2d22512013-06-21 03:55:28 -03001727
Philipp Zabel8358e762013-06-21 03:55:32 -03001728 /* OVL and BTP disabled for encoder */
1729 } else if (ctx->inst_type == CODA_INST_DECODER) {
1730 struct coda_q_data *q_data_dst;
Philipp Zabel8358e762013-06-21 03:55:32 -03001731
1732 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1733 mb_width = DIV_ROUND_UP(q_data_dst->width, 16);
Philipp Zabel8358e762013-06-21 03:55:32 -03001734
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001735 iram_info->buf_dbk_y_use = coda_iram_alloc(iram_info, 128 * mb_width);
1736 iram_info->buf_dbk_c_use = coda_iram_alloc(iram_info, 128 * mb_width);
1737 if (!iram_info->buf_dbk_c_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001738 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001739 iram_info->axi_sram_use |= dbk_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001740
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001741 iram_info->buf_bit_use = coda_iram_alloc(iram_info, 128 * mb_width);
1742 if (!iram_info->buf_bit_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001743 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001744 iram_info->axi_sram_use |= bit_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001745
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001746 iram_info->buf_ip_ac_dc_use = coda_iram_alloc(iram_info, 128 * mb_width);
1747 if (!iram_info->buf_ip_ac_dc_use)
Philipp Zabel8358e762013-06-21 03:55:32 -03001748 goto out;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001749 iram_info->axi_sram_use |= ip_bits;
Philipp Zabel8358e762013-06-21 03:55:32 -03001750
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001751 /* OVL and BTP unused as there is no VC1 support yet */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001752 }
1753
1754out:
Philipp Zabelc2d22512013-06-21 03:55:28 -03001755 if (!(iram_info->axi_sram_use & CODA7_USE_HOST_IP_ENABLE))
1756 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1757 "IRAM smaller than needed\n");
1758
1759 if (dev->devtype->product == CODA_7541) {
1760 /* TODO - Enabling these causes picture errors on CODA7541 */
Philipp Zabel8358e762013-06-21 03:55:32 -03001761 if (ctx->inst_type == CODA_INST_DECODER) {
1762 /* fw 1.4.50 */
1763 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1764 CODA7_USE_IP_ENABLE);
1765 } else {
1766 /* fw 13.4.29 */
Philipp Zabelc2d22512013-06-21 03:55:28 -03001767 iram_info->axi_sram_use &= ~(CODA7_USE_HOST_IP_ENABLE |
1768 CODA7_USE_HOST_DBK_ENABLE |
1769 CODA7_USE_IP_ENABLE |
1770 CODA7_USE_DBK_ENABLE);
1771 }
1772 }
1773}
1774
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001775static void coda_free_context_buffers(struct coda_ctx *ctx)
1776{
1777 struct coda_dev *dev = ctx->dev;
1778
Philipp Zabel918c66f2013-06-27 06:59:01 -03001779 coda_free_aux_buf(dev, &ctx->slicebuf);
1780 coda_free_aux_buf(dev, &ctx->psbuf);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001781 if (dev->devtype->product != CODA_DX6)
1782 coda_free_aux_buf(dev, &ctx->workbuf);
1783}
1784
1785static int coda_alloc_context_buffers(struct coda_ctx *ctx,
1786 struct coda_q_data *q_data)
1787{
1788 struct coda_dev *dev = ctx->dev;
1789 size_t size;
1790 int ret;
1791
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001792 if (dev->devtype->product == CODA_DX6)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001793 return 0;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001794
Philipp Zabel918c66f2013-06-27 06:59:01 -03001795 if (ctx->psbuf.vaddr) {
1796 v4l2_err(&dev->v4l2_dev, "psmembuf still allocated\n");
1797 return -EBUSY;
1798 }
1799 if (ctx->slicebuf.vaddr) {
1800 v4l2_err(&dev->v4l2_dev, "slicebuf still allocated\n");
1801 return -EBUSY;
1802 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001803 if (ctx->workbuf.vaddr) {
1804 v4l2_err(&dev->v4l2_dev, "context buffer still allocated\n");
1805 ret = -EBUSY;
1806 return -ENOMEM;
1807 }
1808
Philipp Zabel918c66f2013-06-27 06:59:01 -03001809 if (q_data->fourcc == V4L2_PIX_FMT_H264) {
1810 /* worst case slice size */
1811 size = (DIV_ROUND_UP(q_data->width, 16) *
1812 DIV_ROUND_UP(q_data->height, 16)) * 3200 / 8 + 512;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001813 ret = coda_alloc_context_buf(ctx, &ctx->slicebuf, size, "slicebuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03001814 if (ret < 0) {
1815 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte slice buffer",
1816 ctx->slicebuf.size);
1817 return ret;
1818 }
1819 }
1820
1821 if (dev->devtype->product == CODA_7541) {
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001822 ret = coda_alloc_context_buf(ctx, &ctx->psbuf, CODA7_PS_BUF_SIZE, "psbuf");
Philipp Zabel918c66f2013-06-27 06:59:01 -03001823 if (ret < 0) {
1824 v4l2_err(&dev->v4l2_dev, "failed to allocate psmem buffer");
1825 goto err;
1826 }
1827 }
1828
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001829 size = dev->devtype->workbuf_size;
1830 if (dev->devtype->product == CODA_960 &&
1831 q_data->fourcc == V4L2_PIX_FMT_H264)
1832 size += CODA9_PS_SAVE_SIZE;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001833 ret = coda_alloc_context_buf(ctx, &ctx->workbuf, size, "workbuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001834 if (ret < 0) {
1835 v4l2_err(&dev->v4l2_dev, "failed to allocate %d byte context buffer",
1836 ctx->workbuf.size);
1837 goto err;
1838 }
1839
1840 return 0;
1841
1842err:
1843 coda_free_context_buffers(ctx);
1844 return ret;
1845}
1846
Philipp Zabela1192a12014-07-23 12:28:40 -03001847static int __coda_start_decoding(struct coda_ctx *ctx)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001848{
1849 struct coda_q_data *q_data_src, *q_data_dst;
1850 u32 bitstream_buf, bitstream_size;
1851 struct coda_dev *dev = ctx->dev;
1852 int width, height;
1853 u32 src_fourcc;
1854 u32 val;
1855 int ret;
1856
1857 /* Start decoding */
1858 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
1859 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1860 bitstream_buf = ctx->bitstream.paddr;
1861 bitstream_size = ctx->bitstream.size;
1862 src_fourcc = q_data_src->fourcc;
1863
1864 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1865
1866 /* Update coda bitstream read and write pointers from kfifo */
1867 coda_kfifo_sync_to_device_full(ctx);
1868
1869 ctx->display_idx = -1;
1870 ctx->frm_dis_flg = 0;
1871 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1872
1873 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1874 CODA_REG_BIT_BIT_STREAM_PARAM);
1875
1876 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1877 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1878 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001879 if ((dev->devtype->product == CODA_7541) ||
1880 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03001881 val |= CODA_REORDER_ENABLE;
1882 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1883
1884 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03001885 if (dev->devtype->product == CODA_960 &&
1886 src_fourcc == V4L2_PIX_FMT_MPEG4)
1887 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1888 else
1889 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001890 if (src_fourcc == V4L2_PIX_FMT_H264) {
1891 if (dev->devtype->product == CODA_7541) {
1892 coda_write(dev, ctx->psbuf.paddr,
1893 CODA_CMD_DEC_SEQ_PS_BB_START);
1894 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1895 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1896 }
Philipp Zabel89548442014-07-11 06:36:17 -03001897 if (dev->devtype->product == CODA_960) {
1898 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1899 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1900 }
1901 }
1902 if (dev->devtype->product != CODA_960) {
1903 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001904 }
1905
1906 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1907 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1908 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1909 return -ETIMEDOUT;
1910 }
1911
1912 /* Update kfifo out pointer from coda bitstream read pointer */
1913 coda_kfifo_sync_from_device(ctx);
1914
1915 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1916
1917 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1918 v4l2_err(&dev->v4l2_dev,
1919 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1920 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1921 return -EAGAIN;
1922 }
1923
1924 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1925 if (dev->devtype->product == CODA_DX6) {
1926 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1927 height = val & CODADX6_PICHEIGHT_MASK;
1928 } else {
1929 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1930 height = val & CODA7_PICHEIGHT_MASK;
1931 }
1932
1933 if (width > q_data_dst->width || height > q_data_dst->height) {
1934 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1935 width, height, q_data_dst->width, q_data_dst->height);
1936 return -EINVAL;
1937 }
1938
1939 width = round_up(width, 16);
1940 height = round_up(height, 16);
1941
1942 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1943 __func__, ctx->idx, width, height);
1944
Philipp Zabela500a932014-07-11 06:36:13 -03001945 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001946 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1947 v4l2_err(&dev->v4l2_dev,
1948 "not enough framebuffers to decode (%d < %d)\n",
1949 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1950 return -EINVAL;
1951 }
1952
Philipp Zabel52c41672014-07-11 06:36:19 -03001953 if (src_fourcc == V4L2_PIX_FMT_H264) {
1954 u32 left_right;
1955 u32 top_bottom;
1956
1957 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
1958 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
1959
1960 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
1961 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
1962 q_data_dst->rect.width = width - q_data_dst->rect.left -
1963 (left_right & 0x3ff);
1964 q_data_dst->rect.height = height - q_data_dst->rect.top -
1965 (top_bottom & 0x3ff);
1966 }
1967
Philipp Zabel918c66f2013-06-27 06:59:01 -03001968 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
1969 if (ret < 0)
1970 return ret;
1971
1972 /* Tell the decoder how many frame buffers we allocated. */
1973 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1974 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1975
1976 if (dev->devtype->product != CODA_DX6) {
1977 /* Set secondary AXI IRAM */
1978 coda_setup_iram(ctx);
1979
1980 coda_write(dev, ctx->iram_info.buf_bit_use,
1981 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1982 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1983 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1984 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1985 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1986 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1987 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1988 coda_write(dev, ctx->iram_info.buf_ovl_use,
1989 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03001990 if (dev->devtype->product == CODA_960)
1991 coda_write(dev, ctx->iram_info.buf_btp_use,
1992 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1993 }
1994
1995 if (dev->devtype->product == CODA_960) {
1996 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
1997
1998 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
1999 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2000 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2001 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2002 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2003 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002004 }
2005
2006 if (src_fourcc == V4L2_PIX_FMT_H264) {
2007 coda_write(dev, ctx->slicebuf.paddr,
2008 CODA_CMD_SET_FRAME_SLICE_BB_START);
2009 coda_write(dev, ctx->slicebuf.size / 1024,
2010 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2011 }
2012
2013 if (dev->devtype->product == CODA_7541) {
2014 int max_mb_x = 1920 / 16;
2015 int max_mb_y = 1088 / 16;
2016 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002017
Philipp Zabel918c66f2013-06-27 06:59:01 -03002018 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2019 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002020 } else if (dev->devtype->product == CODA_960) {
2021 int max_mb_x = 1920 / 16;
2022 int max_mb_y = 1088 / 16;
2023 int max_mb_num = max_mb_x * max_mb_y;
2024
2025 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2026 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002027 }
2028
2029 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2030 v4l2_err(&ctx->dev->v4l2_dev,
2031 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2032 return -ETIMEDOUT;
2033 }
2034
2035 return 0;
2036}
2037
Philipp Zabela1192a12014-07-23 12:28:40 -03002038static int coda_start_decoding(struct coda_ctx *ctx)
2039{
2040 struct coda_dev *dev = ctx->dev;
2041 int ret;
2042
2043 mutex_lock(&dev->coda_mutex);
2044 ret = __coda_start_decoding(ctx);
2045 mutex_unlock(&dev->coda_mutex);
2046
2047 return ret;
2048}
2049
Philipp Zabeld35167a2013-05-23 10:42:59 -03002050static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2051 int header_code, u8 *header, int *size)
2052{
2053 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002054 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002055 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002056 int i;
2057
2058 if (dev->devtype->product == CODA_960)
2059 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002060
2061 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2062 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002063 bufsize = vb2_plane_size(buf, 0);
2064 if (dev->devtype->product == CODA_960)
2065 bufsize /= 1024;
2066 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002067 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2068 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2069 if (ret < 0) {
2070 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2071 return ret;
2072 }
Philipp Zabel89548442014-07-11 06:36:17 -03002073
2074 if (dev->devtype->product == CODA_960) {
2075 for (i = 63; i > 0; i--)
2076 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2077 break;
2078 *size = i + 1;
2079 } else {
2080 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2081 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2082 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002083 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2084
2085 return 0;
2086}
2087
Philipp Zabel89548442014-07-11 06:36:17 -03002088static int coda_start_encoding(struct coda_ctx *ctx);
2089
Javier Martin186b2502012-07-26 05:53:35 -03002090static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2091{
2092 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2093 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002094 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002095 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002096 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002097
Philipp Zabelb96904e2013-05-23 10:42:58 -03002098 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002099 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2100 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03002101 /* copy the buffers that where queued before streamon */
2102 mutex_lock(&ctx->bitstream_mutex);
2103 coda_fill_bitstream(ctx);
2104 mutex_unlock(&ctx->bitstream_mutex);
2105
Philipp Zabel918c66f2013-06-27 06:59:01 -03002106 if (coda_get_bitstream_payload(ctx) < 512)
2107 return -EINVAL;
2108 } else {
2109 if (count < 1)
2110 return -EINVAL;
2111 }
2112
2113 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002114 } else {
2115 if (count < 1)
2116 return -EINVAL;
2117
2118 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002119 }
Javier Martin186b2502012-07-26 05:53:35 -03002120
Philipp Zabelb96904e2013-05-23 10:42:58 -03002121 /* Don't start the coda unless both queues are on */
2122 if (!(ctx->streamon_out & ctx->streamon_cap))
2123 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002124
Philipp Zabeleb107512013-09-30 10:34:45 -03002125 /* Allow decoder device_run with no new buffers queued */
2126 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002127 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002128
Philipp Zabelb96904e2013-05-23 10:42:58 -03002129 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002130 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002131 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002132
Philipp Zabelb96904e2013-05-23 10:42:58 -03002133 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2134 q_data_dst->fourcc);
2135 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002136 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2137 return -EINVAL;
2138 }
2139
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002140 /* Allocate per-instance buffers */
2141 ret = coda_alloc_context_buffers(ctx, q_data_src);
2142 if (ret < 0)
2143 return ret;
2144
Philipp Zabela1192a12014-07-23 12:28:40 -03002145 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002146 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03002147 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002148 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03002149 else if (ret < 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002150 return ret;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002151 }
2152
Philipp Zabel89548442014-07-11 06:36:17 -03002153 ctx->initialized = 1;
2154 return ret;
2155}
2156
2157static int coda_start_encoding(struct coda_ctx *ctx)
2158{
2159 struct coda_dev *dev = ctx->dev;
2160 struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
2161 struct coda_q_data *q_data_src, *q_data_dst;
2162 u32 bitstream_buf, bitstream_size;
2163 struct vb2_buffer *buf;
2164 int gamma, ret, value;
2165 u32 dst_fourcc;
2166
2167 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2168 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
2169 dst_fourcc = q_data_dst->fourcc;
2170
Philipp Zabel14604e32014-07-11 06:36:22 -03002171 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002172 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2173 bitstream_size = q_data_dst->sizeimage;
2174
Javier Martin186b2502012-07-26 05:53:35 -03002175 if (!coda_is_initialized(dev)) {
2176 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2177 return -EFAULT;
2178 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002179
2180 mutex_lock(&dev->coda_mutex);
2181
Javier Martin186b2502012-07-26 05:53:35 -03002182 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002183 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2184 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002185 switch (dev->devtype->product) {
2186 case CODA_DX6:
2187 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2188 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2189 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002190 case CODA_960:
2191 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2192 /* fallthrough */
2193 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002194 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2195 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002196 break;
Javier Martin186b2502012-07-26 05:53:35 -03002197 }
2198
Philipp Zabel89548442014-07-11 06:36:17 -03002199 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2200 value &= ~(1 << 2 | 0x7 << 9);
2201 ctx->frame_mem_ctrl = value;
2202 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2203
Philipp Zabel10436672012-07-02 09:03:55 -03002204 if (dev->devtype->product == CODA_DX6) {
2205 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002206 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002207 }
Javier Martin186b2502012-07-26 05:53:35 -03002208
2209 /* Could set rotation here if needed */
2210 switch (dev->devtype->product) {
2211 case CODA_DX6:
2212 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002213 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002214 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002215 case CODA_7541:
2216 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2217 value = (round_up(q_data_src->width, 16) &
2218 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2219 value |= (round_up(q_data_src->height, 16) &
2220 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2221 break;
2222 }
2223 /* fallthrough */
2224 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002225 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002226 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002227 }
Javier Martin186b2502012-07-26 05:53:35 -03002228 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2229 coda_write(dev, ctx->params.framerate,
2230 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2231
Philipp Zabelb96904e2013-05-23 10:42:58 -03002232 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002233 switch (dst_fourcc) {
2234 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002235 if (dev->devtype->product == CODA_960)
2236 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2237 else
2238 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002239 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2240 break;
2241 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002242 if (dev->devtype->product == CODA_960)
2243 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2244 else
2245 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002246 if (ctx->params.h264_deblk_enabled) {
2247 value = ((ctx->params.h264_deblk_alpha &
2248 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2249 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2250 ((ctx->params.h264_deblk_beta &
2251 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2252 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2253 } else {
2254 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2255 }
2256 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002257 break;
2258 default:
2259 v4l2_err(v4l2_dev,
2260 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002261 ret = -EINVAL;
2262 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002263 }
2264
Philipp Zabelc566c782012-08-08 11:59:38 -03002265 switch (ctx->params.slice_mode) {
2266 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2267 value = 0;
2268 break;
2269 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2270 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2271 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002272 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002273 break;
2274 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2275 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2276 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2277 value |= 1 & CODA_SLICING_MODE_MASK;
2278 break;
2279 }
Javier Martin186b2502012-07-26 05:53:35 -03002280 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002281 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002282 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2283
2284 if (ctx->params.bitrate) {
2285 /* Rate control enabled */
2286 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2287 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002288 if (dev->devtype->product == CODA_960)
2289 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002290 } else {
2291 value = 0;
2292 }
2293 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2294
2295 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002296 coda_write(dev, ctx->params.intra_refresh,
2297 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002298
2299 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2300 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2301
Javier Martin186b2502012-07-26 05:53:35 -03002302
Philipp Zabel89548442014-07-11 06:36:17 -03002303 value = 0;
2304 if (dev->devtype->product == CODA_960)
2305 gamma = CODA9_DEFAULT_GAMMA;
2306 else
2307 gamma = CODA_DEFAULT_GAMMA;
2308 if (gamma > 0) {
2309 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2310 CODA_CMD_ENC_SEQ_RC_GAMMA);
2311 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002312
2313 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2314 coda_write(dev,
2315 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2316 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2317 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2318 }
Philipp Zabel89548442014-07-11 06:36:17 -03002319 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002320 if (ctx->params.h264_max_qp)
2321 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002322 if (CODA_DEFAULT_GAMMA > 0)
2323 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002324 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002325 if (CODA_DEFAULT_GAMMA > 0) {
2326 if (dev->devtype->product == CODA_DX6)
2327 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2328 else
2329 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2330 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002331 if (ctx->params.h264_min_qp)
2332 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2333 if (ctx->params.h264_max_qp)
2334 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002335 }
Javier Martin186b2502012-07-26 05:53:35 -03002336 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2337
Philipp Zabel89548442014-07-11 06:36:17 -03002338 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2339
Philipp Zabelc2d22512013-06-21 03:55:28 -03002340 coda_setup_iram(ctx);
2341
Javier Martin186b2502012-07-26 05:53:35 -03002342 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002343 switch (dev->devtype->product) {
2344 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002345 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002346 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002347 break;
2348 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002349 coda_write(dev, ctx->iram_info.search_ram_paddr,
2350 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2351 coda_write(dev, ctx->iram_info.search_ram_size,
2352 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002353 break;
2354 case CODA_960:
2355 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2356 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002357 }
Javier Martin186b2502012-07-26 05:53:35 -03002358 }
2359
Philipp Zabelfcb62822013-05-23 10:43:00 -03002360 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2361 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002362 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002363 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002364 }
2365
Philipp Zabelfcb62822013-05-23 10:43:00 -03002366 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2367 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2368 ret = -EFAULT;
2369 goto out;
2370 }
Javier Martin186b2502012-07-26 05:53:35 -03002371
Philipp Zabel89548442014-07-11 06:36:17 -03002372 if (dev->devtype->product == CODA_960)
2373 ctx->num_internal_frames = 4;
2374 else
2375 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002376 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002377 if (ret < 0) {
2378 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2379 goto out;
2380 }
Javier Martin186b2502012-07-26 05:53:35 -03002381
Philipp Zabelec25f682012-07-20 08:54:29 -03002382 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002383 coda_write(dev, q_data_src->bytesperline,
2384 CODA_CMD_SET_FRAME_BUF_STRIDE);
2385 if (dev->devtype->product == CODA_7541) {
2386 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002387 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002388 }
Philipp Zabel10436672012-07-02 09:03:55 -03002389 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002390 coda_write(dev, ctx->iram_info.buf_bit_use,
2391 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2392 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2393 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2394 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2395 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2396 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2397 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2398 coda_write(dev, ctx->iram_info.buf_ovl_use,
2399 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002400 if (dev->devtype->product == CODA_960) {
2401 coda_write(dev, ctx->iram_info.buf_btp_use,
2402 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2403
2404 /* FIXME */
2405 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2406 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2407 }
Philipp Zabel10436672012-07-02 09:03:55 -03002408 }
Philipp Zabel89548442014-07-11 06:36:17 -03002409
Philipp Zabelfcb62822013-05-23 10:43:00 -03002410 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2411 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002412 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002413 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002414 }
2415
2416 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002417 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002418 switch (dst_fourcc) {
2419 case V4L2_PIX_FMT_H264:
2420 /*
2421 * Get SPS in the first frame and copy it to an
2422 * intermediate buffer.
2423 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002424 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2425 &ctx->vpu_header[0][0],
2426 &ctx->vpu_header_size[0]);
2427 if (ret < 0)
2428 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002429
2430 /*
2431 * Get PPS in the first frame and copy it to an
2432 * intermediate buffer.
2433 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002434 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2435 &ctx->vpu_header[1][0],
2436 &ctx->vpu_header_size[1]);
2437 if (ret < 0)
2438 goto out;
2439
Javier Martin3f3f5c72012-10-29 05:20:29 -03002440 /*
2441 * Length of H.264 headers is variable and thus it might not be
2442 * aligned for the coda to append the encoded frame. In that is
2443 * the case a filler NAL must be added to header 2.
2444 */
2445 ctx->vpu_header_size[2] = coda_h264_padding(
2446 (ctx->vpu_header_size[0] +
2447 ctx->vpu_header_size[1]),
2448 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002449 break;
2450 case V4L2_PIX_FMT_MPEG4:
2451 /*
2452 * Get VOS in the first frame and copy it to an
2453 * intermediate buffer
2454 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002455 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2456 &ctx->vpu_header[0][0],
2457 &ctx->vpu_header_size[0]);
2458 if (ret < 0)
2459 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002460
Philipp Zabeld35167a2013-05-23 10:42:59 -03002461 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2462 &ctx->vpu_header[1][0],
2463 &ctx->vpu_header_size[1]);
2464 if (ret < 0)
2465 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002466
Philipp Zabeld35167a2013-05-23 10:42:59 -03002467 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2468 &ctx->vpu_header[2][0],
2469 &ctx->vpu_header_size[2]);
2470 if (ret < 0)
2471 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002472 break;
2473 default:
2474 /* No more formats need to save headers at the moment */
2475 break;
2476 }
2477
Philipp Zabeld35167a2013-05-23 10:42:59 -03002478out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002479 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002480 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002481}
2482
Hans Verkuile37559b2014-04-17 02:47:21 -03002483static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002484{
2485 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002486 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002487
2488 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002489 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002490 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002491 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002492
Philipp Zabel347bb7f2014-07-23 12:28:42 -03002493 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002494 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002495 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002496 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002497 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002498 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002499
2500 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002501 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002502 }
2503
Philipp Zabel918c66f2013-06-27 06:59:01 -03002504 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002505 struct coda_timestamp *ts;
2506
2507 while (!list_empty(&ctx->timestamp_list)) {
2508 ts = list_first_entry(&ctx->timestamp_list,
2509 struct coda_timestamp, list);
2510 list_del(&ts->list);
2511 kfree(ts);
2512 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002513 kfifo_init(&ctx->bitstream_fifo,
2514 ctx->bitstream.vaddr, ctx->bitstream.size);
2515 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002516 }
Javier Martin186b2502012-07-26 05:53:35 -03002517}
2518
Philipp Zabel121cacf2014-07-18 07:22:42 -03002519static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03002520 .queue_setup = coda_queue_setup,
2521 .buf_prepare = coda_buf_prepare,
2522 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002523 .start_streaming = coda_start_streaming,
2524 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002525 .wait_prepare = vb2_ops_wait_prepare,
2526 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002527};
2528
2529static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2530{
2531 struct coda_ctx *ctx =
2532 container_of(ctrl->handler, struct coda_ctx, ctrls);
2533
2534 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2535 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2536
2537 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002538 case V4L2_CID_HFLIP:
2539 if (ctrl->val)
2540 ctx->params.rot_mode |= CODA_MIR_HOR;
2541 else
2542 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2543 break;
2544 case V4L2_CID_VFLIP:
2545 if (ctrl->val)
2546 ctx->params.rot_mode |= CODA_MIR_VER;
2547 else
2548 ctx->params.rot_mode &= ~CODA_MIR_VER;
2549 break;
Javier Martin186b2502012-07-26 05:53:35 -03002550 case V4L2_CID_MPEG_VIDEO_BITRATE:
2551 ctx->params.bitrate = ctrl->val / 1000;
2552 break;
2553 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2554 ctx->params.gop_size = ctrl->val;
2555 break;
2556 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2557 ctx->params.h264_intra_qp = ctrl->val;
2558 break;
2559 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2560 ctx->params.h264_inter_qp = ctrl->val;
2561 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002562 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2563 ctx->params.h264_min_qp = ctrl->val;
2564 break;
2565 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2566 ctx->params.h264_max_qp = ctrl->val;
2567 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002568 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2569 ctx->params.h264_deblk_alpha = ctrl->val;
2570 break;
2571 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2572 ctx->params.h264_deblk_beta = ctrl->val;
2573 break;
2574 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2575 ctx->params.h264_deblk_enabled = (ctrl->val ==
2576 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2577 break;
Javier Martin186b2502012-07-26 05:53:35 -03002578 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2579 ctx->params.mpeg4_intra_qp = ctrl->val;
2580 break;
2581 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2582 ctx->params.mpeg4_inter_qp = ctrl->val;
2583 break;
2584 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2585 ctx->params.slice_mode = ctrl->val;
2586 break;
2587 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2588 ctx->params.slice_max_mb = ctrl->val;
2589 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002590 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2591 ctx->params.slice_max_bits = ctrl->val * 8;
2592 break;
Javier Martin186b2502012-07-26 05:53:35 -03002593 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2594 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002595 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2596 ctx->params.intra_refresh = ctrl->val;
2597 break;
Javier Martin186b2502012-07-26 05:53:35 -03002598 default:
2599 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2600 "Invalid control, id=%d, val=%d\n",
2601 ctrl->id, ctrl->val);
2602 return -EINVAL;
2603 }
2604
2605 return 0;
2606}
2607
Philipp Zabel814c3762014-07-18 07:22:45 -03002608static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03002609 .s_ctrl = coda_s_ctrl,
2610};
2611
2612static int coda_ctrls_setup(struct coda_ctx *ctx)
2613{
2614 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2615
2616 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002617 V4L2_CID_HFLIP, 0, 1, 1, 0);
2618 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2619 V4L2_CID_VFLIP, 0, 1, 1, 0);
2620 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002621 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2622 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2623 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2624 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002625 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002626 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002627 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002628 if (ctx->dev->devtype->product != CODA_960) {
2629 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2630 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2631 }
2632 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2633 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002634 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002635 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2636 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2637 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2638 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2639 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2640 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2641 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2642 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002643 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2644 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2645 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2646 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2647 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002648 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2649 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002650 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2651 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002652 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2653 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002654 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2655 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2656 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2657 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2658 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002659 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2660 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002661
2662 if (ctx->ctrls.error) {
2663 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2664 ctx->ctrls.error);
2665 return -EINVAL;
2666 }
2667
2668 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2669}
2670
Philipp Zabel121cacf2014-07-18 07:22:42 -03002671static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03002672{
Philipp Zabel121cacf2014-07-18 07:22:42 -03002673 vq->drv_priv = ctx;
2674 vq->ops = &coda_qops;
2675 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2676 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2677 vq->lock = &ctx->dev->dev_mutex;
2678
2679 return vb2_queue_init(vq);
2680}
2681
2682static int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2683 struct vb2_queue *dst_vq)
2684{
Javier Martin186b2502012-07-26 05:53:35 -03002685 int ret;
2686
Javier Martin186b2502012-07-26 05:53:35 -03002687 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002688 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002689 src_vq->mem_ops = &vb2_dma_contig_memops;
2690
Philipp Zabel121cacf2014-07-18 07:22:42 -03002691 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002692 if (ret)
2693 return ret;
2694
Javier Martin186b2502012-07-26 05:53:35 -03002695 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002696 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002697 dst_vq->mem_ops = &vb2_dma_contig_memops;
2698
Philipp Zabel121cacf2014-07-18 07:22:42 -03002699 return coda_queue_init(priv, dst_vq);
2700}
2701
2702static int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2703 struct vb2_queue *dst_vq)
2704{
2705 int ret;
2706
2707 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2708 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2709 src_vq->mem_ops = &vb2_dma_contig_memops;
2710
2711 ret = coda_queue_init(priv, src_vq);
2712 if (ret)
2713 return ret;
2714
2715 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2716 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2717 dst_vq->mem_ops = &vb2_dma_contig_memops;
2718
2719 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002720}
2721
Philipp Zabele11f3e62012-07-25 09:16:58 -03002722static int coda_next_free_instance(struct coda_dev *dev)
2723{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002724 int idx = ffz(dev->instance_mask);
2725
2726 if ((idx < 0) ||
2727 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2728 return -EBUSY;
2729
2730 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002731}
2732
Philipp Zabela1192a12014-07-23 12:28:40 -03002733static int coda_open(struct file *file, enum coda_inst_type inst_type,
2734 const struct coda_context_ops *ctx_ops)
Javier Martin186b2502012-07-26 05:53:35 -03002735{
2736 struct coda_dev *dev = video_drvdata(file);
2737 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002738 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002739 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002740 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002741
Javier Martin186b2502012-07-26 05:53:35 -03002742 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2743 if (!ctx)
2744 return -ENOMEM;
2745
Fabio Estevamf82bc202013-08-21 11:14:16 -03002746 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002747 if (idx < 0) {
2748 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002749 goto err_coda_max;
2750 }
2751 set_bit(idx, &dev->instance_mask);
2752
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002753 name = kasprintf(GFP_KERNEL, "context%d", idx);
2754 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2755 kfree(name);
2756
Philipp Zabel121cacf2014-07-18 07:22:42 -03002757 ctx->inst_type = inst_type;
Philipp Zabela1192a12014-07-23 12:28:40 -03002758 ctx->ops = ctx_ops;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002759 init_completion(&ctx->completion);
2760 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabela1192a12014-07-23 12:28:40 -03002761 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002762 v4l2_fh_init(&ctx->fh, video_devdata(file));
2763 file->private_data = &ctx->fh;
2764 v4l2_fh_add(&ctx->fh);
2765 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002766 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002767 switch (dev->devtype->product) {
2768 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002769 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002770 ctx->reg_idx = 0;
2771 break;
2772 default:
2773 ctx->reg_idx = idx;
2774 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002775
Philipp Zabel1e172732014-07-11 06:36:23 -03002776 /* Power up and upload firmware if necessary */
2777 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2778 if (ret < 0) {
2779 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2780 goto err_pm_get;
2781 }
2782
Fabio Estevam79830db2013-08-21 11:14:17 -03002783 ret = clk_prepare_enable(dev->clk_per);
2784 if (ret)
2785 goto err_clk_per;
2786
2787 ret = clk_prepare_enable(dev->clk_ahb);
2788 if (ret)
2789 goto err_clk_ahb;
2790
Javier Martin186b2502012-07-26 05:53:35 -03002791 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03002792 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2793 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002794 if (IS_ERR(ctx->fh.m2m_ctx)) {
2795 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002796
2797 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2798 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002799 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002800 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002801
Javier Martin186b2502012-07-26 05:53:35 -03002802 ret = coda_ctrls_setup(ctx);
2803 if (ret) {
2804 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002805 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002806 }
2807
2808 ctx->fh.ctrl_handler = &ctx->ctrls;
2809
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002810 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
2811 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002812 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002813 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002814 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002815 }
2816
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002817 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2818 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2819 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2820 if (!ctx->bitstream.vaddr) {
2821 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2822 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002823 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002824 }
2825 kfifo_init(&ctx->bitstream_fifo,
2826 ctx->bitstream.vaddr, ctx->bitstream.size);
2827 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002828 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002829 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002830
Javier Martin186b2502012-07-26 05:53:35 -03002831 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002832 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002833 coda_unlock(ctx);
2834
Javier Martin186b2502012-07-26 05:53:35 -03002835 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2836 ctx->idx, ctx);
2837
2838 return 0;
2839
Fabio Estevamf82bc202013-08-21 11:14:16 -03002840err_dma_writecombine:
2841 coda_free_context_buffers(ctx);
2842 if (ctx->dev->devtype->product == CODA_DX6)
2843 coda_free_aux_buf(dev, &ctx->workbuf);
2844 coda_free_aux_buf(dev, &ctx->parabuf);
2845err_dma_alloc:
2846 v4l2_ctrl_handler_free(&ctx->ctrls);
2847err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002848 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002849err_ctx_init:
2850 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002851err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002852 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002853err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002854 pm_runtime_put_sync(&dev->plat_dev->dev);
2855err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002856 v4l2_fh_del(&ctx->fh);
2857 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002858 clear_bit(ctx->idx, &dev->instance_mask);
2859err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002860 kfree(ctx);
2861 return ret;
2862}
2863
Philipp Zabela1192a12014-07-23 12:28:40 -03002864struct coda_context_ops coda_encode_ops = {
2865 .queue_init = coda_encoder_queue_init,
2866 .start_streaming = coda_start_encoding,
2867 .prepare_run = coda_prepare_encode,
2868 .finish_run = coda_finish_encode,
2869 .seq_end_work = coda_seq_end_work,
2870};
2871
2872struct coda_context_ops coda_decode_ops = {
2873 .queue_init = coda_decoder_queue_init,
2874 .start_streaming = coda_start_decoding,
2875 .prepare_run = coda_prepare_decode,
2876 .finish_run = coda_finish_decode,
2877 .seq_end_work = coda_seq_end_work
2878};
2879
Philipp Zabel121cacf2014-07-18 07:22:42 -03002880static int coda_encoder_open(struct file *file)
2881{
Philipp Zabela1192a12014-07-23 12:28:40 -03002882 return coda_open(file, CODA_INST_ENCODER, &coda_encode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03002883}
2884
2885static int coda_decoder_open(struct file *file)
2886{
Philipp Zabela1192a12014-07-23 12:28:40 -03002887 return coda_open(file, CODA_INST_DECODER, &coda_decode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03002888}
2889
Javier Martin186b2502012-07-26 05:53:35 -03002890static int coda_release(struct file *file)
2891{
2892 struct coda_dev *dev = video_drvdata(file);
2893 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2894
2895 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2896 ctx);
2897
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002898 debugfs_remove_recursive(ctx->debugfs_entry);
2899
Philipp Zabel918c66f2013-06-27 06:59:01 -03002900 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002901 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002902
2903 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002904 if (ctx->initialized) {
2905 queue_work(dev->workqueue, &ctx->seq_end_work);
2906 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002907 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002908
2909 coda_free_framebuffers(ctx);
2910
Javier Martin186b2502012-07-26 05:53:35 -03002911 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002912 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002913 coda_unlock(ctx);
2914
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002915 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2916 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002917 coda_free_context_buffers(ctx);
2918 if (ctx->dev->devtype->product == CODA_DX6)
2919 coda_free_aux_buf(dev, &ctx->workbuf);
2920
2921 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002922 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002923 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002924 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002925 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002926 v4l2_fh_del(&ctx->fh);
2927 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002928 clear_bit(ctx->idx, &dev->instance_mask);
Javier Martin186b2502012-07-26 05:53:35 -03002929 kfree(ctx);
2930
2931 return 0;
2932}
2933
Philipp Zabel121cacf2014-07-18 07:22:42 -03002934static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03002935 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03002936 .open = coda_encoder_open,
2937 .release = coda_release,
2938 .poll = v4l2_m2m_fop_poll,
2939 .unlocked_ioctl = video_ioctl2,
2940 .mmap = v4l2_m2m_fop_mmap,
2941};
2942
2943static const struct v4l2_file_operations coda_decoder_fops = {
2944 .owner = THIS_MODULE,
2945 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03002946 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002947 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002948 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002949 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002950};
2951
Philipp Zabel918c66f2013-06-27 06:59:01 -03002952static void coda_finish_decode(struct coda_ctx *ctx)
2953{
2954 struct coda_dev *dev = ctx->dev;
2955 struct coda_q_data *q_data_src;
2956 struct coda_q_data *q_data_dst;
2957 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03002958 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002959 int width, height;
2960 int decoded_idx;
2961 int display_idx;
2962 u32 src_fourcc;
2963 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03002964 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002965 u32 val;
2966
Philipp Zabel14604e32014-07-11 06:36:22 -03002967 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002968
2969 /* Update kfifo out pointer from coda bitstream read pointer */
2970 coda_kfifo_sync_from_device(ctx);
2971
2972 /*
2973 * in stream-end mode, the read pointer can overshoot the write pointer
2974 * by up to 512 bytes
2975 */
2976 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
Michael Olbrich390503b2014-07-18 07:22:39 -03002977 if (coda_get_bitstream_payload(ctx) >= CODA_MAX_FRAME_SIZE - 512)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002978 kfifo_init(&ctx->bitstream_fifo,
2979 ctx->bitstream.vaddr, ctx->bitstream.size);
2980 }
2981
2982 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2983 src_fourcc = q_data_src->fourcc;
2984
2985 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2986 if (val != 1)
2987 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2988
2989 success = val & 0x1;
2990 if (!success)
2991 v4l2_err(&dev->v4l2_dev, "decode failed\n");
2992
2993 if (src_fourcc == V4L2_PIX_FMT_H264) {
2994 if (val & (1 << 3))
2995 v4l2_err(&dev->v4l2_dev,
2996 "insufficient PS buffer space (%d bytes)\n",
2997 ctx->psbuf.size);
2998 if (val & (1 << 2))
2999 v4l2_err(&dev->v4l2_dev,
3000 "insufficient slice buffer space (%d bytes)\n",
3001 ctx->slicebuf.size);
3002 }
3003
3004 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3005 width = (val >> 16) & 0xffff;
3006 height = val & 0xffff;
3007
3008 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3009
Philipp Zabel52c41672014-07-11 06:36:19 -03003010 /* frame crop information */
3011 if (src_fourcc == V4L2_PIX_FMT_H264) {
3012 u32 left_right;
3013 u32 top_bottom;
3014
3015 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3016 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3017
3018 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3019 /* Keep current crop information */
3020 } else {
3021 struct v4l2_rect *rect = &q_data_dst->rect;
3022
3023 rect->left = left_right >> 16 & 0xffff;
3024 rect->top = top_bottom >> 16 & 0xffff;
3025 rect->width = width - rect->left -
3026 (left_right & 0xffff);
3027 rect->height = height - rect->top -
3028 (top_bottom & 0xffff);
3029 }
3030 } else {
3031 /* no cropping */
3032 }
3033
Philipp Zabel410e5e42014-07-11 06:36:32 -03003034 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3035 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003036 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003037 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003038
3039 if (dev->devtype->product == CODA_7541) {
3040 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3041 if (val == 0) {
3042 /* not enough bitstream data */
3043 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3044 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003045 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003046 return;
3047 }
3048 }
3049
3050 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3051
3052 /*
3053 * The previous display frame was copied out by the rotator,
3054 * now it can be overwritten again
3055 */
3056 if (ctx->display_idx >= 0 &&
3057 ctx->display_idx < ctx->num_internal_frames) {
3058 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3059 coda_write(dev, ctx->frm_dis_flg,
3060 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3061 }
3062
3063 /*
3064 * The index of the last decoded frame, not necessarily in
3065 * display order, and the index of the next display frame.
3066 * The latter could have been decoded in a previous run.
3067 */
3068 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3069 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3070
3071 if (decoded_idx == -1) {
3072 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003073 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3074 ctx->sequence_offset++;
3075 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003076 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003077 } else if (decoded_idx == -2) {
3078 /* no frame was decoded, we still return the remaining buffers */
3079 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3080 v4l2_err(&dev->v4l2_dev,
3081 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003082 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003083 ts = list_first_entry(&ctx->timestamp_list,
3084 struct coda_timestamp, list);
3085 list_del(&ts->list);
3086 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003087 val -= ctx->sequence_offset;
3088 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003089 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003090 "sequence number mismatch (%d(%d) != %d)\n",
3091 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003092 }
3093 ctx->frame_timestamps[decoded_idx] = *ts;
3094 kfree(ts);
3095
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003096 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3097 if (val == 0)
3098 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3099 else if (val == 1)
3100 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3101 else
3102 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003103
3104 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003105 }
3106
3107 if (display_idx == -1) {
3108 /*
3109 * no more frames to be decoded, but there could still
3110 * be rotator output to dequeue
3111 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003112 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003113 } else if (display_idx == -3) {
3114 /* possibly prescan failure */
3115 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3116 v4l2_err(&dev->v4l2_dev,
3117 "presentation frame index out of range: %d\n",
3118 display_idx);
3119 }
3120
3121 /* If a frame was copied out, return it */
3122 if (ctx->display_idx >= 0 &&
3123 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003124 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003125 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3126
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003127 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3128 V4L2_BUF_FLAG_PFRAME |
3129 V4L2_BUF_FLAG_BFRAME);
3130 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003131 ts = &ctx->frame_timestamps[ctx->display_idx];
3132 dst_buf->v4l2_buf.timecode = ts->timecode;
3133 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003134
Philipp Zabel918c66f2013-06-27 06:59:01 -03003135 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3136
Philipp Zabel410e5e42014-07-11 06:36:32 -03003137 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3138 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003139
3140 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3141 "job finished: decoding frame (%d) (%s)\n",
3142 dst_buf->v4l2_buf.sequence,
3143 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3144 "KEYFRAME" : "PFRAME");
3145 } else {
3146 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3147 "job finished: no frame decoded\n");
3148 }
3149
3150 /* The rotator will copy the current display frame next time */
3151 ctx->display_idx = display_idx;
3152}
3153
3154static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003155{
Philipp Zabelec25f682012-07-20 08:54:29 -03003156 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003157 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003158 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003159
Philipp Zabel14604e32014-07-11 06:36:22 -03003160 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3161 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003162
3163 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003164 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003165 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3166
Javier Martin186b2502012-07-26 05:53:35 -03003167 /* Calculate bytesused field */
3168 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003169 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3170 ctx->vpu_header_size[0] +
3171 ctx->vpu_header_size[1] +
3172 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003173 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003174 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003175 }
3176
3177 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3178 wr_ptr - start_ptr);
3179
3180 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3181 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3182
Philipp Zabel51660282013-09-30 10:34:49 -03003183 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003184 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3185 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3186 } else {
3187 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3188 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3189 }
3190
Kamil Debskiccd571c2013-04-24 10:38:24 -03003191 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003192 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3193 dst_buf->v4l2_buf.flags |=
3194 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003195 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3196
Philipp Zabelec25f682012-07-20 08:54:29 -03003197 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003198
Philipp Zabel14604e32014-07-11 06:36:22 -03003199 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003200 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3201
3202 ctx->gopcounter--;
3203 if (ctx->gopcounter < 0)
3204 ctx->gopcounter = ctx->params.gop_size - 1;
3205
3206 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3207 "job finished: encoding frame (%d) (%s)\n",
3208 dst_buf->v4l2_buf.sequence,
3209 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3210 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003211}
3212
3213static irqreturn_t coda_irq_handler(int irq, void *data)
3214{
3215 struct coda_dev *dev = data;
3216 struct coda_ctx *ctx;
3217
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003218 /* read status register to attend the IRQ */
3219 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3220 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3221 CODA_REG_BIT_INT_CLEAR);
3222
3223 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3224 if (ctx == NULL) {
3225 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3226 mutex_unlock(&dev->coda_mutex);
3227 return IRQ_HANDLED;
3228 }
3229
3230 if (ctx->aborting) {
3231 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3232 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003233 }
3234
3235 if (coda_isbusy(ctx->dev)) {
3236 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3237 "coda is still busy!!!!\n");
3238 return IRQ_NONE;
3239 }
3240
Philipp Zabel32b6f202014-07-11 06:36:20 -03003241 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003242
3243 return IRQ_HANDLED;
3244}
3245
3246static u32 coda_supported_firmwares[] = {
3247 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003248 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003249 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003250};
3251
3252static bool coda_firmware_supported(u32 vernum)
3253{
3254 int i;
3255
3256 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3257 if (vernum == coda_supported_firmwares[i])
3258 return true;
3259 return false;
3260}
3261
Philipp Zabel87048bb2012-07-02 07:03:43 -03003262static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003263{
Javier Martin186b2502012-07-26 05:53:35 -03003264 u32 data;
3265 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003266 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003267
Fabio Estevam79830db2013-08-21 11:14:17 -03003268 ret = clk_prepare_enable(dev->clk_per);
3269 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003270 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003271
3272 ret = clk_prepare_enable(dev->clk_ahb);
3273 if (ret)
3274 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003275
Philipp Zabel8f452842014-07-11 06:36:35 -03003276 if (dev->rstc)
3277 reset_control_reset(dev->rstc);
3278
Javier Martin186b2502012-07-26 05:53:35 -03003279 /*
3280 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003281 * The 16-bit chars in the code buffer are in memory access
3282 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003283 * Data in this SRAM survives a reboot.
3284 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003285 p = (u16 *)dev->codebuf.vaddr;
3286 if (dev->devtype->product == CODA_DX6) {
3287 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3288 data = CODA_DOWN_ADDRESS_SET(i) |
3289 CODA_DOWN_DATA_SET(p[i ^ 1]);
3290 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3291 }
3292 } else {
3293 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3294 data = CODA_DOWN_ADDRESS_SET(i) |
3295 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3296 3 - (i % 4)]);
3297 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3298 }
Javier Martin186b2502012-07-26 05:53:35 -03003299 }
Javier Martin186b2502012-07-26 05:53:35 -03003300
Philipp Zabel9acf7692013-05-23 10:42:56 -03003301 /* Clear registers */
3302 for (i = 0; i < 64; i++)
3303 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3304
Javier Martin186b2502012-07-26 05:53:35 -03003305 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003306 if (dev->devtype->product == CODA_960 ||
3307 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003308 coda_write(dev, dev->tempbuf.paddr,
3309 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003310 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003311 } else {
3312 coda_write(dev, dev->workbuf.paddr,
3313 CODA_REG_BIT_WORK_BUF_ADDR);
3314 }
Javier Martin186b2502012-07-26 05:53:35 -03003315 coda_write(dev, dev->codebuf.paddr,
3316 CODA_REG_BIT_CODE_BUF_ADDR);
3317 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3318
3319 /* Set default values */
3320 switch (dev->devtype->product) {
3321 case CODA_DX6:
3322 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3323 break;
3324 default:
3325 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3326 }
Philipp Zabel89548442014-07-11 06:36:17 -03003327 if (dev->devtype->product == CODA_960)
3328 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3329 else
3330 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003331
3332 if (dev->devtype->product != CODA_DX6)
3333 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3334
Javier Martin186b2502012-07-26 05:53:35 -03003335 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3336 CODA_REG_BIT_INT_ENABLE);
3337
3338 /* Reset VPU and start processor */
3339 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3340 data |= CODA_REG_RESET_ENABLE;
3341 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3342 udelay(10);
3343 data &= ~CODA_REG_RESET_ENABLE;
3344 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3345 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3346
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003347 clk_disable_unprepare(dev->clk_ahb);
3348 clk_disable_unprepare(dev->clk_per);
3349
3350 return 0;
3351
3352err_clk_ahb:
3353 clk_disable_unprepare(dev->clk_per);
3354err_clk_per:
3355 return ret;
3356}
3357
3358static int coda_check_firmware(struct coda_dev *dev)
3359{
3360 u16 product, major, minor, release;
3361 u32 data;
3362 int ret;
3363
3364 ret = clk_prepare_enable(dev->clk_per);
3365 if (ret)
3366 goto err_clk_per;
3367
3368 ret = clk_prepare_enable(dev->clk_ahb);
3369 if (ret)
3370 goto err_clk_ahb;
3371
Javier Martin186b2502012-07-26 05:53:35 -03003372 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3373 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3374 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3375 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3376 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3377 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003378 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003379 ret = -EIO;
3380 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003381 }
3382
Philipp Zabel89548442014-07-11 06:36:17 -03003383 if (dev->devtype->product == CODA_960) {
3384 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3385 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3386 data);
3387 }
3388
Javier Martin186b2502012-07-26 05:53:35 -03003389 /* Check we are compatible with the loaded firmware */
3390 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3391 product = CODA_FIRMWARE_PRODUCT(data);
3392 major = CODA_FIRMWARE_MAJOR(data);
3393 minor = CODA_FIRMWARE_MINOR(data);
3394 release = CODA_FIRMWARE_RELEASE(data);
3395
3396 clk_disable_unprepare(dev->clk_per);
3397 clk_disable_unprepare(dev->clk_ahb);
3398
3399 if (product != dev->devtype->product) {
3400 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3401 " Version: %u.%u.%u\n",
3402 coda_product_name(dev->devtype->product),
3403 coda_product_name(product), major, minor, release);
3404 return -EINVAL;
3405 }
3406
3407 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3408 coda_product_name(product));
3409
3410 if (coda_firmware_supported(data)) {
3411 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3412 major, minor, release);
3413 } else {
3414 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3415 "%u.%u.%u\n", major, minor, release);
3416 }
3417
3418 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003419
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003420err_run_cmd:
3421 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003422err_clk_ahb:
3423 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003424err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003425 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003426}
3427
Philipp Zabel121cacf2014-07-18 07:22:42 -03003428static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
3429{
3430 vfd->release = video_device_release_empty,
3431 vfd->lock = &dev->dev_mutex;
3432 vfd->v4l2_dev = &dev->v4l2_dev;
3433 vfd->vfl_dir = VFL_DIR_M2M;
3434 video_set_drvdata(vfd, dev);
3435
3436 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
3437}
3438
Javier Martin186b2502012-07-26 05:53:35 -03003439static void coda_fw_callback(const struct firmware *fw, void *context)
3440{
3441 struct coda_dev *dev = context;
3442 struct platform_device *pdev = dev->plat_dev;
3443 int ret;
3444
3445 if (!fw) {
3446 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3447 return;
3448 }
3449
3450 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003451 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3452 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003453 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003454 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3455 return;
3456 }
3457
Philipp Zabel87048bb2012-07-02 07:03:43 -03003458 /* Copy the whole firmware image to the code buffer */
3459 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3460 release_firmware(fw);
3461
Philipp Zabel1e172732014-07-11 06:36:23 -03003462 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3463 /*
3464 * Enabling power temporarily will cause coda_hw_init to be
3465 * called via coda_runtime_resume by the pm domain.
3466 */
3467 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3468 if (ret < 0) {
3469 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3470 ret);
3471 return;
3472 }
3473
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003474 ret = coda_check_firmware(dev);
3475 if (ret < 0)
3476 return;
3477
Philipp Zabel1e172732014-07-11 06:36:23 -03003478 pm_runtime_put_sync(&dev->plat_dev->dev);
3479 } else {
3480 /*
3481 * If runtime pm is disabled or pm_domain is not set,
3482 * initialize once manually.
3483 */
3484 ret = coda_hw_init(dev);
3485 if (ret < 0) {
3486 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3487 return;
3488 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003489
3490 ret = coda_check_firmware(dev);
3491 if (ret < 0)
3492 return;
Javier Martin186b2502012-07-26 05:53:35 -03003493 }
3494
Javier Martin186b2502012-07-26 05:53:35 -03003495 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3496 if (IS_ERR(dev->alloc_ctx)) {
3497 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3498 return;
3499 }
3500
3501 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3502 if (IS_ERR(dev->m2m_dev)) {
3503 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3504 goto rel_ctx;
3505 }
3506
Philipp Zabel121cacf2014-07-18 07:22:42 -03003507 dev->vfd[0].fops = &coda_encoder_fops,
3508 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
3509 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
3510 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03003511 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03003512 v4l2_err(&dev->v4l2_dev,
3513 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03003514 goto rel_m2m;
3515 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03003516
3517 dev->vfd[1].fops = &coda_decoder_fops,
3518 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
3519 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
3520 ret = coda_register_device(dev, &dev->vfd[1]);
3521 if (ret) {
3522 v4l2_err(&dev->v4l2_dev,
3523 "Failed to register decoder video device\n");
3524 goto rel_m2m;
3525 }
3526
3527 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
3528 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03003529
3530 return;
3531
3532rel_m2m:
3533 v4l2_m2m_release(dev->m2m_dev);
3534rel_ctx:
3535 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3536}
3537
3538static int coda_firmware_request(struct coda_dev *dev)
3539{
3540 char *fw = dev->devtype->firmware;
3541
3542 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3543 coda_product_name(dev->devtype->product));
3544
3545 return request_firmware_nowait(THIS_MODULE, true,
3546 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3547}
3548
3549enum coda_platform {
3550 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003551 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003552 CODA_IMX6Q,
3553 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003554};
3555
Emil Goodec06d8752012-08-14 17:44:42 -03003556static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003557 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003558 .firmware = "v4l-codadx6-imx27.bin",
3559 .product = CODA_DX6,
3560 .codecs = codadx6_codecs,
3561 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3562 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003563 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03003564 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003565 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003566 .firmware = "v4l-coda7541-imx53.bin",
3567 .product = CODA_7541,
3568 .codecs = coda7_codecs,
3569 .num_codecs = ARRAY_SIZE(coda7_codecs),
3570 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003571 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003572 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003573 },
Philipp Zabel89548442014-07-11 06:36:17 -03003574 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003575 .firmware = "v4l-coda960-imx6q.bin",
3576 .product = CODA_960,
3577 .codecs = coda9_codecs,
3578 .num_codecs = ARRAY_SIZE(coda9_codecs),
3579 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003580 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003581 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03003582 },
3583 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003584 .firmware = "v4l-coda960-imx6dl.bin",
3585 .product = CODA_960,
3586 .codecs = coda9_codecs,
3587 .num_codecs = ARRAY_SIZE(coda9_codecs),
3588 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003589 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003590 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03003591 },
Javier Martin186b2502012-07-26 05:53:35 -03003592};
3593
3594static struct platform_device_id coda_platform_ids[] = {
3595 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003596 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003597 { /* sentinel */ }
3598};
3599MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3600
3601#ifdef CONFIG_OF
3602static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003603 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003604 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003605 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3606 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003607 { /* sentinel */ }
3608};
3609MODULE_DEVICE_TABLE(of, coda_dt_ids);
3610#endif
3611
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003612static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003613{
3614 const struct of_device_id *of_id =
3615 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3616 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003617 struct coda_platform_data *pdata = pdev->dev.platform_data;
3618 struct device_node *np = pdev->dev.of_node;
3619 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003620 struct coda_dev *dev;
3621 struct resource *res;
3622 int ret, irq;
3623
3624 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3625 if (!dev) {
3626 dev_err(&pdev->dev, "Not enough memory for %s\n",
3627 CODA_NAME);
3628 return -ENOMEM;
3629 }
3630
3631 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003632 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003633
3634 dev->plat_dev = pdev;
3635 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3636 if (IS_ERR(dev->clk_per)) {
3637 dev_err(&pdev->dev, "Could not get per clock\n");
3638 return PTR_ERR(dev->clk_per);
3639 }
3640
3641 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3642 if (IS_ERR(dev->clk_ahb)) {
3643 dev_err(&pdev->dev, "Could not get ahb clock\n");
3644 return PTR_ERR(dev->clk_ahb);
3645 }
3646
3647 /* Get memory for physical registers */
3648 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003649 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3650 if (IS_ERR(dev->regs_base))
3651 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003652
3653 /* IRQ */
3654 irq = platform_get_irq(pdev, 0);
3655 if (irq < 0) {
3656 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03003657 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03003658 }
3659
Fabio Estevam08c8d142014-06-04 15:46:24 -03003660 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
3661 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
3662 if (ret < 0) {
3663 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3664 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003665 }
3666
Philipp Zabelee27aa92014-07-24 16:50:03 -03003667 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03003668 if (IS_ERR(dev->rstc)) {
3669 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03003670 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03003671 dev->rstc = NULL;
3672 } else {
3673 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3674 return ret;
3675 }
3676 }
3677
Philipp Zabel657eee72013-04-29 16:17:14 -07003678 /* Get IRAM pool from device tree or platform data */
3679 pool = of_get_named_gen_pool(np, "iram", 0);
3680 if (!pool && pdata)
3681 pool = dev_get_gen_pool(pdata->iram_dev);
3682 if (!pool) {
3683 dev_err(&pdev->dev, "iram pool not available\n");
3684 return -ENOMEM;
3685 }
3686 dev->iram_pool = pool;
3687
Javier Martin186b2502012-07-26 05:53:35 -03003688 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3689 if (ret)
3690 return ret;
3691
3692 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003693 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003694
3695 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3696
3697 if (of_id) {
3698 dev->devtype = of_id->data;
3699 } else if (pdev_id) {
3700 dev->devtype = &coda_devdata[pdev_id->driver_data];
3701 } else {
3702 v4l2_device_unregister(&dev->v4l2_dev);
3703 return -EINVAL;
3704 }
3705
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003706 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3707 if (!dev->debugfs_root)
3708 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3709
Javier Martin186b2502012-07-26 05:53:35 -03003710 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003711 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003712 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003713 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003714 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003715 if (ret < 0) {
3716 dev_err(&pdev->dev, "failed to allocate work buffer\n");
3717 v4l2_device_unregister(&dev->v4l2_dev);
3718 return ret;
3719 }
Javier Martin186b2502012-07-26 05:53:35 -03003720 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003721
3722 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003723 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003724 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003725 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003726 if (ret < 0) {
3727 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3728 v4l2_device_unregister(&dev->v4l2_dev);
3729 return ret;
3730 }
Javier Martin186b2502012-07-26 05:53:35 -03003731 }
3732
Philipp Zabel401e9722014-07-11 06:36:43 -03003733 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003734 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3735 &dev->iram.paddr);
3736 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003737 dev_err(&pdev->dev, "unable to alloc iram\n");
3738 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003739 }
3740
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003741 dev->iram.blob.data = dev->iram.vaddr;
3742 dev->iram.blob.size = dev->iram.size;
3743 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
3744 &dev->iram.blob);
3745
Philipp Zabel32b6f202014-07-11 06:36:20 -03003746 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3747 if (!dev->workqueue) {
3748 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3749 return -ENOMEM;
3750 }
3751
Javier Martin186b2502012-07-26 05:53:35 -03003752 platform_set_drvdata(pdev, dev);
3753
Philipp Zabel1e172732014-07-11 06:36:23 -03003754 pm_runtime_enable(&pdev->dev);
3755
Javier Martin186b2502012-07-26 05:53:35 -03003756 return coda_firmware_request(dev);
3757}
3758
3759static int coda_remove(struct platform_device *pdev)
3760{
3761 struct coda_dev *dev = platform_get_drvdata(pdev);
3762
Philipp Zabel121cacf2014-07-18 07:22:42 -03003763 video_unregister_device(&dev->vfd[0]);
3764 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03003765 if (dev->m2m_dev)
3766 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003767 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003768 if (dev->alloc_ctx)
3769 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3770 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003771 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003772 if (dev->iram.vaddr)
3773 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3774 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003775 coda_free_aux_buf(dev, &dev->codebuf);
3776 coda_free_aux_buf(dev, &dev->tempbuf);
3777 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003778 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03003779 return 0;
3780}
3781
Philipp Zabel1e172732014-07-11 06:36:23 -03003782#ifdef CONFIG_PM_RUNTIME
3783static int coda_runtime_resume(struct device *dev)
3784{
3785 struct coda_dev *cdev = dev_get_drvdata(dev);
3786 int ret = 0;
3787
Philipp Zabel65919e62014-07-18 07:22:36 -03003788 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03003789 ret = coda_hw_init(cdev);
3790 if (ret)
3791 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3792 }
3793
3794 return ret;
3795}
3796#endif
3797
3798static const struct dev_pm_ops coda_pm_ops = {
3799 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3800};
3801
Javier Martin186b2502012-07-26 05:53:35 -03003802static struct platform_driver coda_driver = {
3803 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003804 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003805 .driver = {
3806 .name = CODA_NAME,
3807 .owner = THIS_MODULE,
3808 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003809 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003810 },
3811 .id_table = coda_platform_ids,
3812};
3813
3814module_platform_driver(coda_driver);
3815
3816MODULE_LICENSE("GPL");
3817MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3818MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");