blob: 64151aafd21fc503ec085bbe3b004e16debe85a9 [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
Philipp Zabel58b76772014-07-23 12:28:43 -03001864 /* Allocate per-instance buffers */
1865 ret = coda_alloc_context_buffers(ctx, q_data_src);
1866 if (ret < 0)
1867 return ret;
1868
Philipp Zabel918c66f2013-06-27 06:59:01 -03001869 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
1870
1871 /* Update coda bitstream read and write pointers from kfifo */
1872 coda_kfifo_sync_to_device_full(ctx);
1873
1874 ctx->display_idx = -1;
1875 ctx->frm_dis_flg = 0;
1876 coda_write(dev, 0, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
1877
1878 coda_write(dev, CODA_BIT_DEC_SEQ_INIT_ESCAPE,
1879 CODA_REG_BIT_BIT_STREAM_PARAM);
1880
1881 coda_write(dev, bitstream_buf, CODA_CMD_DEC_SEQ_BB_START);
1882 coda_write(dev, bitstream_size / 1024, CODA_CMD_DEC_SEQ_BB_SIZE);
1883 val = 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001884 if ((dev->devtype->product == CODA_7541) ||
1885 (dev->devtype->product == CODA_960))
Philipp Zabel918c66f2013-06-27 06:59:01 -03001886 val |= CODA_REORDER_ENABLE;
1887 coda_write(dev, val, CODA_CMD_DEC_SEQ_OPTION);
1888
1889 ctx->params.codec_mode = ctx->codec->mode;
Philipp Zabel89548442014-07-11 06:36:17 -03001890 if (dev->devtype->product == CODA_960 &&
1891 src_fourcc == V4L2_PIX_FMT_MPEG4)
1892 ctx->params.codec_mode_aux = CODA_MP4_AUX_MPEG4;
1893 else
1894 ctx->params.codec_mode_aux = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001895 if (src_fourcc == V4L2_PIX_FMT_H264) {
1896 if (dev->devtype->product == CODA_7541) {
1897 coda_write(dev, ctx->psbuf.paddr,
1898 CODA_CMD_DEC_SEQ_PS_BB_START);
1899 coda_write(dev, (CODA7_PS_BUF_SIZE / 1024),
1900 CODA_CMD_DEC_SEQ_PS_BB_SIZE);
1901 }
Philipp Zabel89548442014-07-11 06:36:17 -03001902 if (dev->devtype->product == CODA_960) {
1903 coda_write(dev, 0, CODA_CMD_DEC_SEQ_X264_MV_EN);
1904 coda_write(dev, 512, CODA_CMD_DEC_SEQ_SPP_CHUNK_SIZE);
1905 }
1906 }
1907 if (dev->devtype->product != CODA_960) {
1908 coda_write(dev, 0, CODA_CMD_DEC_SEQ_SRC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001909 }
1910
1911 if (coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT)) {
1912 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
1913 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1914 return -ETIMEDOUT;
1915 }
1916
1917 /* Update kfifo out pointer from coda bitstream read pointer */
1918 coda_kfifo_sync_from_device(ctx);
1919
1920 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
1921
1922 if (coda_read(dev, CODA_RET_DEC_SEQ_SUCCESS) == 0) {
1923 v4l2_err(&dev->v4l2_dev,
1924 "CODA_COMMAND_SEQ_INIT failed, error code = %d\n",
1925 coda_read(dev, CODA_RET_DEC_SEQ_ERR_REASON));
1926 return -EAGAIN;
1927 }
1928
1929 val = coda_read(dev, CODA_RET_DEC_SEQ_SRC_SIZE);
1930 if (dev->devtype->product == CODA_DX6) {
1931 width = (val >> CODADX6_PICWIDTH_OFFSET) & CODADX6_PICWIDTH_MASK;
1932 height = val & CODADX6_PICHEIGHT_MASK;
1933 } else {
1934 width = (val >> CODA7_PICWIDTH_OFFSET) & CODA7_PICWIDTH_MASK;
1935 height = val & CODA7_PICHEIGHT_MASK;
1936 }
1937
1938 if (width > q_data_dst->width || height > q_data_dst->height) {
1939 v4l2_err(&dev->v4l2_dev, "stream is %dx%d, not %dx%d\n",
1940 width, height, q_data_dst->width, q_data_dst->height);
1941 return -EINVAL;
1942 }
1943
1944 width = round_up(width, 16);
1945 height = round_up(height, 16);
1946
1947 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "%s instance %d now: %dx%d\n",
1948 __func__, ctx->idx, width, height);
1949
Philipp Zabela500a932014-07-11 06:36:13 -03001950 ctx->num_internal_frames = coda_read(dev, CODA_RET_DEC_SEQ_FRAME_NEED);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001951 if (ctx->num_internal_frames > CODA_MAX_FRAMEBUFFERS) {
1952 v4l2_err(&dev->v4l2_dev,
1953 "not enough framebuffers to decode (%d < %d)\n",
1954 CODA_MAX_FRAMEBUFFERS, ctx->num_internal_frames);
1955 return -EINVAL;
1956 }
1957
Philipp Zabel52c41672014-07-11 06:36:19 -03001958 if (src_fourcc == V4L2_PIX_FMT_H264) {
1959 u32 left_right;
1960 u32 top_bottom;
1961
1962 left_right = coda_read(dev, CODA_RET_DEC_SEQ_CROP_LEFT_RIGHT);
1963 top_bottom = coda_read(dev, CODA_RET_DEC_SEQ_CROP_TOP_BOTTOM);
1964
1965 q_data_dst->rect.left = (left_right >> 10) & 0x3ff;
1966 q_data_dst->rect.top = (top_bottom >> 10) & 0x3ff;
1967 q_data_dst->rect.width = width - q_data_dst->rect.left -
1968 (left_right & 0x3ff);
1969 q_data_dst->rect.height = height - q_data_dst->rect.top -
1970 (top_bottom & 0x3ff);
1971 }
1972
Philipp Zabel918c66f2013-06-27 06:59:01 -03001973 ret = coda_alloc_framebuffers(ctx, q_data_dst, src_fourcc);
1974 if (ret < 0)
1975 return ret;
1976
1977 /* Tell the decoder how many frame buffers we allocated. */
1978 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
1979 coda_write(dev, width, CODA_CMD_SET_FRAME_BUF_STRIDE);
1980
1981 if (dev->devtype->product != CODA_DX6) {
1982 /* Set secondary AXI IRAM */
1983 coda_setup_iram(ctx);
1984
1985 coda_write(dev, ctx->iram_info.buf_bit_use,
1986 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
1987 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
1988 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
1989 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
1990 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
1991 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
1992 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
1993 coda_write(dev, ctx->iram_info.buf_ovl_use,
1994 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03001995 if (dev->devtype->product == CODA_960)
1996 coda_write(dev, ctx->iram_info.buf_btp_use,
1997 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
1998 }
1999
2000 if (dev->devtype->product == CODA_960) {
2001 coda_write(dev, -1, CODA9_CMD_SET_FRAME_DELAY);
2002
2003 coda_write(dev, 0x20262024, CODA9_CMD_SET_FRAME_CACHE_SIZE);
2004 coda_write(dev, 2 << CODA9_CACHE_PAGEMERGE_OFFSET |
2005 32 << CODA9_CACHE_LUMA_BUFFER_SIZE_OFFSET |
2006 8 << CODA9_CACHE_CB_BUFFER_SIZE_OFFSET |
2007 8 << CODA9_CACHE_CR_BUFFER_SIZE_OFFSET,
2008 CODA9_CMD_SET_FRAME_CACHE_CONFIG);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002009 }
2010
2011 if (src_fourcc == V4L2_PIX_FMT_H264) {
2012 coda_write(dev, ctx->slicebuf.paddr,
2013 CODA_CMD_SET_FRAME_SLICE_BB_START);
2014 coda_write(dev, ctx->slicebuf.size / 1024,
2015 CODA_CMD_SET_FRAME_SLICE_BB_SIZE);
2016 }
2017
2018 if (dev->devtype->product == CODA_7541) {
2019 int max_mb_x = 1920 / 16;
2020 int max_mb_y = 1088 / 16;
2021 int max_mb_num = max_mb_x * max_mb_y;
Philipp Zabel89548442014-07-11 06:36:17 -03002022
Philipp Zabel918c66f2013-06-27 06:59:01 -03002023 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2024 CODA7_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002025 } else if (dev->devtype->product == CODA_960) {
2026 int max_mb_x = 1920 / 16;
2027 int max_mb_y = 1088 / 16;
2028 int max_mb_num = max_mb_x * max_mb_y;
2029
2030 coda_write(dev, max_mb_num << 16 | max_mb_x << 8 | max_mb_y,
2031 CODA9_CMD_SET_FRAME_MAX_DEC_SIZE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002032 }
2033
2034 if (coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF)) {
2035 v4l2_err(&ctx->dev->v4l2_dev,
2036 "CODA_COMMAND_SET_FRAME_BUF timeout\n");
2037 return -ETIMEDOUT;
2038 }
2039
2040 return 0;
2041}
2042
Philipp Zabela1192a12014-07-23 12:28:40 -03002043static int coda_start_decoding(struct coda_ctx *ctx)
2044{
2045 struct coda_dev *dev = ctx->dev;
2046 int ret;
2047
2048 mutex_lock(&dev->coda_mutex);
2049 ret = __coda_start_decoding(ctx);
2050 mutex_unlock(&dev->coda_mutex);
2051
2052 return ret;
2053}
2054
Philipp Zabeld35167a2013-05-23 10:42:59 -03002055static int coda_encode_header(struct coda_ctx *ctx, struct vb2_buffer *buf,
2056 int header_code, u8 *header, int *size)
2057{
2058 struct coda_dev *dev = ctx->dev;
Philipp Zabel89548442014-07-11 06:36:17 -03002059 size_t bufsize;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002060 int ret;
Philipp Zabel89548442014-07-11 06:36:17 -03002061 int i;
2062
2063 if (dev->devtype->product == CODA_960)
2064 memset(vb2_plane_vaddr(buf, 0), 0, 64);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002065
2066 coda_write(dev, vb2_dma_contig_plane_dma_addr(buf, 0),
2067 CODA_CMD_ENC_HEADER_BB_START);
Philipp Zabel89548442014-07-11 06:36:17 -03002068 bufsize = vb2_plane_size(buf, 0);
2069 if (dev->devtype->product == CODA_960)
2070 bufsize /= 1024;
2071 coda_write(dev, bufsize, CODA_CMD_ENC_HEADER_BB_SIZE);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002072 coda_write(dev, header_code, CODA_CMD_ENC_HEADER_CODE);
2073 ret = coda_command_sync(ctx, CODA_COMMAND_ENCODE_HEADER);
2074 if (ret < 0) {
2075 v4l2_err(&dev->v4l2_dev, "CODA_COMMAND_ENCODE_HEADER timeout\n");
2076 return ret;
2077 }
Philipp Zabel89548442014-07-11 06:36:17 -03002078
2079 if (dev->devtype->product == CODA_960) {
2080 for (i = 63; i > 0; i--)
2081 if (((char *)vb2_plane_vaddr(buf, 0))[i] != 0)
2082 break;
2083 *size = i + 1;
2084 } else {
2085 *size = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx)) -
2086 coda_read(dev, CODA_CMD_ENC_HEADER_BB_START);
2087 }
Philipp Zabeld35167a2013-05-23 10:42:59 -03002088 memcpy(header, vb2_plane_vaddr(buf, 0), *size);
2089
2090 return 0;
2091}
2092
Philipp Zabel89548442014-07-11 06:36:17 -03002093static int coda_start_encoding(struct coda_ctx *ctx);
2094
Javier Martin186b2502012-07-26 05:53:35 -03002095static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
2096{
2097 struct coda_ctx *ctx = vb2_get_drv_priv(q);
2098 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03002099 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelec25f682012-07-20 08:54:29 -03002100 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03002101 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002102
Philipp Zabelb96904e2013-05-23 10:42:58 -03002103 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002104 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2105 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03002106 /* copy the buffers that where queued before streamon */
2107 mutex_lock(&ctx->bitstream_mutex);
2108 coda_fill_bitstream(ctx);
2109 mutex_unlock(&ctx->bitstream_mutex);
2110
Philipp Zabel918c66f2013-06-27 06:59:01 -03002111 if (coda_get_bitstream_payload(ctx) < 512)
2112 return -EINVAL;
2113 } else {
2114 if (count < 1)
2115 return -EINVAL;
2116 }
2117
2118 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002119 } else {
2120 if (count < 1)
2121 return -EINVAL;
2122
2123 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002124 }
Javier Martin186b2502012-07-26 05:53:35 -03002125
Philipp Zabelb96904e2013-05-23 10:42:58 -03002126 /* Don't start the coda unless both queues are on */
2127 if (!(ctx->streamon_out & ctx->streamon_cap))
2128 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03002129
Philipp Zabeleb107512013-09-30 10:34:45 -03002130 /* Allow decoder device_run with no new buffers queued */
2131 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03002132 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002133
Philipp Zabelb96904e2013-05-23 10:42:58 -03002134 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03002135 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002136 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03002137
Philipp Zabelb96904e2013-05-23 10:42:58 -03002138 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
2139 q_data_dst->fourcc);
2140 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03002141 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
2142 return -EINVAL;
2143 }
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 Zabel58b76772014-07-23 12:28:43 -03002171 /* Allocate per-instance buffers */
2172 ret = coda_alloc_context_buffers(ctx, q_data_src);
2173 if (ret < 0)
2174 return ret;
2175
Philipp Zabel14604e32014-07-11 06:36:22 -03002176 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -03002177 bitstream_buf = vb2_dma_contig_plane_dma_addr(buf, 0);
2178 bitstream_size = q_data_dst->sizeimage;
2179
Javier Martin186b2502012-07-26 05:53:35 -03002180 if (!coda_is_initialized(dev)) {
2181 v4l2_err(v4l2_dev, "coda is not initialized.\n");
2182 return -EFAULT;
2183 }
Philipp Zabelfcb62822013-05-23 10:43:00 -03002184
2185 mutex_lock(&dev->coda_mutex);
2186
Javier Martin186b2502012-07-26 05:53:35 -03002187 coda_write(dev, ctx->parabuf.paddr, CODA_REG_BIT_PARA_BUF_ADDR);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002188 coda_write(dev, bitstream_buf, CODA_REG_BIT_RD_PTR(ctx->reg_idx));
2189 coda_write(dev, bitstream_buf, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
Javier Martin186b2502012-07-26 05:53:35 -03002190 switch (dev->devtype->product) {
2191 case CODA_DX6:
2192 coda_write(dev, CODADX6_STREAM_BUF_DYNALLOC_EN |
2193 CODADX6_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
2194 break;
Philipp Zabel89548442014-07-11 06:36:17 -03002195 case CODA_960:
2196 coda_write(dev, 0, CODA9_GDI_WPROT_RGN_EN);
2197 /* fallthrough */
2198 case CODA_7541:
Javier Martin186b2502012-07-26 05:53:35 -03002199 coda_write(dev, CODA7_STREAM_BUF_DYNALLOC_EN |
2200 CODA7_STREAM_BUF_PIC_RESET, CODA_REG_BIT_STREAM_CTRL);
Philipp Zabel89548442014-07-11 06:36:17 -03002201 break;
Javier Martin186b2502012-07-26 05:53:35 -03002202 }
2203
Philipp Zabel89548442014-07-11 06:36:17 -03002204 value = coda_read(dev, CODA_REG_BIT_FRAME_MEM_CTRL);
2205 value &= ~(1 << 2 | 0x7 << 9);
2206 ctx->frame_mem_ctrl = value;
2207 coda_write(dev, value, CODA_REG_BIT_FRAME_MEM_CTRL);
2208
Philipp Zabel10436672012-07-02 09:03:55 -03002209 if (dev->devtype->product == CODA_DX6) {
2210 /* Configure the coda */
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002211 coda_write(dev, dev->iram.paddr, CODADX6_REG_BIT_SEARCH_RAM_BASE_ADDR);
Philipp Zabel10436672012-07-02 09:03:55 -03002212 }
Javier Martin186b2502012-07-26 05:53:35 -03002213
2214 /* Could set rotation here if needed */
2215 switch (dev->devtype->product) {
2216 case CODA_DX6:
2217 value = (q_data_src->width & CODADX6_PICWIDTH_MASK) << CODADX6_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002218 value |= (q_data_src->height & CODADX6_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002219 break;
Philipp Zabel7c3df782014-07-11 06:36:38 -03002220 case CODA_7541:
2221 if (dst_fourcc == V4L2_PIX_FMT_H264) {
2222 value = (round_up(q_data_src->width, 16) &
2223 CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
2224 value |= (round_up(q_data_src->height, 16) &
2225 CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
2226 break;
2227 }
2228 /* fallthrough */
2229 case CODA_960:
Javier Martin186b2502012-07-26 05:53:35 -03002230 value = (q_data_src->width & CODA7_PICWIDTH_MASK) << CODA7_PICWIDTH_OFFSET;
Philipp Zabelb96904e2013-05-23 10:42:58 -03002231 value |= (q_data_src->height & CODA7_PICHEIGHT_MASK) << CODA_PICHEIGHT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002232 }
Javier Martin186b2502012-07-26 05:53:35 -03002233 coda_write(dev, value, CODA_CMD_ENC_SEQ_SRC_SIZE);
2234 coda_write(dev, ctx->params.framerate,
2235 CODA_CMD_ENC_SEQ_SRC_F_RATE);
2236
Philipp Zabelb96904e2013-05-23 10:42:58 -03002237 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03002238 switch (dst_fourcc) {
2239 case V4L2_PIX_FMT_MPEG4:
Philipp Zabel89548442014-07-11 06:36:17 -03002240 if (dev->devtype->product == CODA_960)
2241 coda_write(dev, CODA9_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
2242 else
2243 coda_write(dev, CODA_STD_MPEG4, CODA_CMD_ENC_SEQ_COD_STD);
Javier Martin186b2502012-07-26 05:53:35 -03002244 coda_write(dev, 0, CODA_CMD_ENC_SEQ_MP4_PARA);
2245 break;
2246 case V4L2_PIX_FMT_H264:
Philipp Zabel89548442014-07-11 06:36:17 -03002247 if (dev->devtype->product == CODA_960)
2248 coda_write(dev, CODA9_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
2249 else
2250 coda_write(dev, CODA_STD_H264, CODA_CMD_ENC_SEQ_COD_STD);
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002251 if (ctx->params.h264_deblk_enabled) {
2252 value = ((ctx->params.h264_deblk_alpha &
2253 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_MASK) <<
2254 CODA_264PARAM_DEBLKFILTEROFFSETALPHA_OFFSET) |
2255 ((ctx->params.h264_deblk_beta &
2256 CODA_264PARAM_DEBLKFILTEROFFSETBETA_MASK) <<
2257 CODA_264PARAM_DEBLKFILTEROFFSETBETA_OFFSET);
2258 } else {
2259 value = 1 << CODA_264PARAM_DISABLEDEBLK_OFFSET;
2260 }
2261 coda_write(dev, value, CODA_CMD_ENC_SEQ_264_PARA);
Javier Martin186b2502012-07-26 05:53:35 -03002262 break;
2263 default:
2264 v4l2_err(v4l2_dev,
2265 "dst format (0x%08x) invalid.\n", dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002266 ret = -EINVAL;
2267 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002268 }
2269
Philipp Zabelc566c782012-08-08 11:59:38 -03002270 switch (ctx->params.slice_mode) {
2271 case V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE:
2272 value = 0;
2273 break;
2274 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_MB:
2275 value = (ctx->params.slice_max_mb & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2276 value |= (1 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
Javier Martin186b2502012-07-26 05:53:35 -03002277 value |= 1 & CODA_SLICING_MODE_MASK;
Philipp Zabelc566c782012-08-08 11:59:38 -03002278 break;
2279 case V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES:
2280 value = (ctx->params.slice_max_bits & CODA_SLICING_SIZE_MASK) << CODA_SLICING_SIZE_OFFSET;
2281 value |= (0 & CODA_SLICING_UNIT_MASK) << CODA_SLICING_UNIT_OFFSET;
2282 value |= 1 & CODA_SLICING_MODE_MASK;
2283 break;
2284 }
Javier Martin186b2502012-07-26 05:53:35 -03002285 coda_write(dev, value, CODA_CMD_ENC_SEQ_SLICE_MODE);
Philipp Zabelc566c782012-08-08 11:59:38 -03002286 value = ctx->params.gop_size & CODA_GOP_SIZE_MASK;
Javier Martin186b2502012-07-26 05:53:35 -03002287 coda_write(dev, value, CODA_CMD_ENC_SEQ_GOP_SIZE);
2288
2289 if (ctx->params.bitrate) {
2290 /* Rate control enabled */
2291 value = (ctx->params.bitrate & CODA_RATECONTROL_BITRATE_MASK) << CODA_RATECONTROL_BITRATE_OFFSET;
2292 value |= 1 & CODA_RATECONTROL_ENABLE_MASK;
Philipp Zabel89548442014-07-11 06:36:17 -03002293 if (dev->devtype->product == CODA_960)
2294 value |= BIT(31); /* disable autoskip */
Javier Martin186b2502012-07-26 05:53:35 -03002295 } else {
2296 value = 0;
2297 }
2298 coda_write(dev, value, CODA_CMD_ENC_SEQ_RC_PARA);
2299
2300 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_BUF_SIZE);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002301 coda_write(dev, ctx->params.intra_refresh,
2302 CODA_CMD_ENC_SEQ_INTRA_REFRESH);
Javier Martin186b2502012-07-26 05:53:35 -03002303
2304 coda_write(dev, bitstream_buf, CODA_CMD_ENC_SEQ_BB_START);
2305 coda_write(dev, bitstream_size / 1024, CODA_CMD_ENC_SEQ_BB_SIZE);
2306
Javier Martin186b2502012-07-26 05:53:35 -03002307
Philipp Zabel89548442014-07-11 06:36:17 -03002308 value = 0;
2309 if (dev->devtype->product == CODA_960)
2310 gamma = CODA9_DEFAULT_GAMMA;
2311 else
2312 gamma = CODA_DEFAULT_GAMMA;
2313 if (gamma > 0) {
2314 coda_write(dev, (gamma & CODA_GAMMA_MASK) << CODA_GAMMA_OFFSET,
2315 CODA_CMD_ENC_SEQ_RC_GAMMA);
2316 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002317
2318 if (ctx->params.h264_min_qp || ctx->params.h264_max_qp) {
2319 coda_write(dev,
2320 ctx->params.h264_min_qp << CODA_QPMIN_OFFSET |
2321 ctx->params.h264_max_qp << CODA_QPMAX_OFFSET,
2322 CODA_CMD_ENC_SEQ_RC_QP_MIN_MAX);
2323 }
Philipp Zabel89548442014-07-11 06:36:17 -03002324 if (dev->devtype->product == CODA_960) {
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002325 if (ctx->params.h264_max_qp)
2326 value |= 1 << CODA9_OPTION_RCQPMAX_OFFSET;
Philipp Zabel89548442014-07-11 06:36:17 -03002327 if (CODA_DEFAULT_GAMMA > 0)
2328 value |= 1 << CODA9_OPTION_GAMMA_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002329 } else {
Philipp Zabel89548442014-07-11 06:36:17 -03002330 if (CODA_DEFAULT_GAMMA > 0) {
2331 if (dev->devtype->product == CODA_DX6)
2332 value |= 1 << CODADX6_OPTION_GAMMA_OFFSET;
2333 else
2334 value |= 1 << CODA7_OPTION_GAMMA_OFFSET;
2335 }
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002336 if (ctx->params.h264_min_qp)
2337 value |= 1 << CODA7_OPTION_RCQPMIN_OFFSET;
2338 if (ctx->params.h264_max_qp)
2339 value |= 1 << CODA7_OPTION_RCQPMAX_OFFSET;
Philipp Zabelfb1fcf12013-05-23 10:42:53 -03002340 }
Javier Martin186b2502012-07-26 05:53:35 -03002341 coda_write(dev, value, CODA_CMD_ENC_SEQ_OPTION);
2342
Philipp Zabel89548442014-07-11 06:36:17 -03002343 coda_write(dev, 0, CODA_CMD_ENC_SEQ_RC_INTERVAL_MODE);
2344
Philipp Zabelc2d22512013-06-21 03:55:28 -03002345 coda_setup_iram(ctx);
2346
Javier Martin186b2502012-07-26 05:53:35 -03002347 if (dst_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel89548442014-07-11 06:36:17 -03002348 switch (dev->devtype->product) {
2349 case CODA_DX6:
Philipp Zabel0fd84dc2013-09-30 10:34:47 -03002350 value = FMO_SLICE_SAVE_BUF_SIZE << 7;
Philipp Zabel10436672012-07-02 09:03:55 -03002351 coda_write(dev, value, CODADX6_CMD_ENC_SEQ_FMO);
Philipp Zabel89548442014-07-11 06:36:17 -03002352 break;
2353 case CODA_7541:
Philipp Zabelc2d22512013-06-21 03:55:28 -03002354 coda_write(dev, ctx->iram_info.search_ram_paddr,
2355 CODA7_CMD_ENC_SEQ_SEARCH_BASE);
2356 coda_write(dev, ctx->iram_info.search_ram_size,
2357 CODA7_CMD_ENC_SEQ_SEARCH_SIZE);
Philipp Zabel89548442014-07-11 06:36:17 -03002358 break;
2359 case CODA_960:
2360 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_ME_OPTION);
2361 coda_write(dev, 0, CODA9_CMD_ENC_SEQ_INTRA_WEIGHT);
Philipp Zabel10436672012-07-02 09:03:55 -03002362 }
Javier Martin186b2502012-07-26 05:53:35 -03002363 }
2364
Philipp Zabelfcb62822013-05-23 10:43:00 -03002365 ret = coda_command_sync(ctx, CODA_COMMAND_SEQ_INIT);
2366 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002367 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002368 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002369 }
2370
Philipp Zabelfcb62822013-05-23 10:43:00 -03002371 if (coda_read(dev, CODA_RET_ENC_SEQ_SUCCESS) == 0) {
2372 v4l2_err(v4l2_dev, "CODA_COMMAND_SEQ_INIT failed\n");
2373 ret = -EFAULT;
2374 goto out;
2375 }
Javier Martin186b2502012-07-26 05:53:35 -03002376
Philipp Zabel89548442014-07-11 06:36:17 -03002377 if (dev->devtype->product == CODA_960)
2378 ctx->num_internal_frames = 4;
2379 else
2380 ctx->num_internal_frames = 2;
Philipp Zabelec25f682012-07-20 08:54:29 -03002381 ret = coda_alloc_framebuffers(ctx, q_data_src, dst_fourcc);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002382 if (ret < 0) {
2383 v4l2_err(v4l2_dev, "failed to allocate framebuffers\n");
2384 goto out;
2385 }
Javier Martin186b2502012-07-26 05:53:35 -03002386
Philipp Zabelec25f682012-07-20 08:54:29 -03002387 coda_write(dev, ctx->num_internal_frames, CODA_CMD_SET_FRAME_BUF_NUM);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002388 coda_write(dev, q_data_src->bytesperline,
2389 CODA_CMD_SET_FRAME_BUF_STRIDE);
2390 if (dev->devtype->product == CODA_7541) {
2391 coda_write(dev, q_data_src->bytesperline,
Philipp Zabel918c66f2013-06-27 06:59:01 -03002392 CODA7_CMD_SET_FRAME_SOURCE_BUF_STRIDE);
Philipp Zabel2fd6a372014-07-11 06:36:36 -03002393 }
Philipp Zabel10436672012-07-02 09:03:55 -03002394 if (dev->devtype->product != CODA_DX6) {
Philipp Zabelc2d22512013-06-21 03:55:28 -03002395 coda_write(dev, ctx->iram_info.buf_bit_use,
2396 CODA7_CMD_SET_FRAME_AXI_BIT_ADDR);
2397 coda_write(dev, ctx->iram_info.buf_ip_ac_dc_use,
2398 CODA7_CMD_SET_FRAME_AXI_IPACDC_ADDR);
2399 coda_write(dev, ctx->iram_info.buf_dbk_y_use,
2400 CODA7_CMD_SET_FRAME_AXI_DBKY_ADDR);
2401 coda_write(dev, ctx->iram_info.buf_dbk_c_use,
2402 CODA7_CMD_SET_FRAME_AXI_DBKC_ADDR);
2403 coda_write(dev, ctx->iram_info.buf_ovl_use,
2404 CODA7_CMD_SET_FRAME_AXI_OVL_ADDR);
Philipp Zabel89548442014-07-11 06:36:17 -03002405 if (dev->devtype->product == CODA_960) {
2406 coda_write(dev, ctx->iram_info.buf_btp_use,
2407 CODA9_CMD_SET_FRAME_AXI_BTP_ADDR);
2408
2409 /* FIXME */
2410 coda_write(dev, ctx->internal_frames[2].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_A);
2411 coda_write(dev, ctx->internal_frames[3].paddr, CODA9_CMD_SET_FRAME_SUBSAMP_B);
2412 }
Philipp Zabel10436672012-07-02 09:03:55 -03002413 }
Philipp Zabel89548442014-07-11 06:36:17 -03002414
Philipp Zabelfcb62822013-05-23 10:43:00 -03002415 ret = coda_command_sync(ctx, CODA_COMMAND_SET_FRAME_BUF);
2416 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002417 v4l2_err(v4l2_dev, "CODA_COMMAND_SET_FRAME_BUF timeout\n");
Philipp Zabelfcb62822013-05-23 10:43:00 -03002418 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002419 }
2420
2421 /* Save stream headers */
Philipp Zabel14604e32014-07-11 06:36:22 -03002422 buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002423 switch (dst_fourcc) {
2424 case V4L2_PIX_FMT_H264:
2425 /*
2426 * Get SPS in the first frame and copy it to an
2427 * intermediate buffer.
2428 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002429 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_SPS,
2430 &ctx->vpu_header[0][0],
2431 &ctx->vpu_header_size[0]);
2432 if (ret < 0)
2433 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002434
2435 /*
2436 * Get PPS in the first frame and copy it to an
2437 * intermediate buffer.
2438 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002439 ret = coda_encode_header(ctx, buf, CODA_HEADER_H264_PPS,
2440 &ctx->vpu_header[1][0],
2441 &ctx->vpu_header_size[1]);
2442 if (ret < 0)
2443 goto out;
2444
Javier Martin3f3f5c72012-10-29 05:20:29 -03002445 /*
2446 * Length of H.264 headers is variable and thus it might not be
2447 * aligned for the coda to append the encoded frame. In that is
2448 * the case a filler NAL must be added to header 2.
2449 */
2450 ctx->vpu_header_size[2] = coda_h264_padding(
2451 (ctx->vpu_header_size[0] +
2452 ctx->vpu_header_size[1]),
2453 ctx->vpu_header[2]);
Javier Martin186b2502012-07-26 05:53:35 -03002454 break;
2455 case V4L2_PIX_FMT_MPEG4:
2456 /*
2457 * Get VOS in the first frame and copy it to an
2458 * intermediate buffer
2459 */
Philipp Zabeld35167a2013-05-23 10:42:59 -03002460 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOS,
2461 &ctx->vpu_header[0][0],
2462 &ctx->vpu_header_size[0]);
2463 if (ret < 0)
2464 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002465
Philipp Zabeld35167a2013-05-23 10:42:59 -03002466 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VIS,
2467 &ctx->vpu_header[1][0],
2468 &ctx->vpu_header_size[1]);
2469 if (ret < 0)
2470 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002471
Philipp Zabeld35167a2013-05-23 10:42:59 -03002472 ret = coda_encode_header(ctx, buf, CODA_HEADER_MP4V_VOL,
2473 &ctx->vpu_header[2][0],
2474 &ctx->vpu_header_size[2]);
2475 if (ret < 0)
2476 goto out;
Javier Martin186b2502012-07-26 05:53:35 -03002477 break;
2478 default:
2479 /* No more formats need to save headers at the moment */
2480 break;
2481 }
2482
Philipp Zabeld35167a2013-05-23 10:42:59 -03002483out:
Philipp Zabelfcb62822013-05-23 10:43:00 -03002484 mutex_unlock(&dev->coda_mutex);
Philipp Zabeld35167a2013-05-23 10:42:59 -03002485 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002486}
2487
Hans Verkuile37559b2014-04-17 02:47:21 -03002488static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03002489{
2490 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03002491 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03002492
2493 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002494 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002495 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002496 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002497
Philipp Zabel347bb7f2014-07-23 12:28:42 -03002498 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002499 ctx->isequence = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002500 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03002501 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03002502 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03002503 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002504
2505 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03002506 ctx->sequence_offset = 0;
Javier Martin186b2502012-07-26 05:53:35 -03002507 }
2508
Philipp Zabel918c66f2013-06-27 06:59:01 -03002509 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03002510 struct coda_timestamp *ts;
2511
2512 while (!list_empty(&ctx->timestamp_list)) {
2513 ts = list_first_entry(&ctx->timestamp_list,
2514 struct coda_timestamp, list);
2515 list_del(&ts->list);
2516 kfree(ts);
2517 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002518 kfifo_init(&ctx->bitstream_fifo,
2519 ctx->bitstream.vaddr, ctx->bitstream.size);
2520 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03002521 }
Javier Martin186b2502012-07-26 05:53:35 -03002522}
2523
Philipp Zabel121cacf2014-07-18 07:22:42 -03002524static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03002525 .queue_setup = coda_queue_setup,
2526 .buf_prepare = coda_buf_prepare,
2527 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03002528 .start_streaming = coda_start_streaming,
2529 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002530 .wait_prepare = vb2_ops_wait_prepare,
2531 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03002532};
2533
2534static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
2535{
2536 struct coda_ctx *ctx =
2537 container_of(ctrl->handler, struct coda_ctx, ctrls);
2538
2539 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2540 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
2541
2542 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002543 case V4L2_CID_HFLIP:
2544 if (ctrl->val)
2545 ctx->params.rot_mode |= CODA_MIR_HOR;
2546 else
2547 ctx->params.rot_mode &= ~CODA_MIR_HOR;
2548 break;
2549 case V4L2_CID_VFLIP:
2550 if (ctrl->val)
2551 ctx->params.rot_mode |= CODA_MIR_VER;
2552 else
2553 ctx->params.rot_mode &= ~CODA_MIR_VER;
2554 break;
Javier Martin186b2502012-07-26 05:53:35 -03002555 case V4L2_CID_MPEG_VIDEO_BITRATE:
2556 ctx->params.bitrate = ctrl->val / 1000;
2557 break;
2558 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
2559 ctx->params.gop_size = ctrl->val;
2560 break;
2561 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
2562 ctx->params.h264_intra_qp = ctrl->val;
2563 break;
2564 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
2565 ctx->params.h264_inter_qp = ctrl->val;
2566 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002567 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
2568 ctx->params.h264_min_qp = ctrl->val;
2569 break;
2570 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
2571 ctx->params.h264_max_qp = ctrl->val;
2572 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002573 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
2574 ctx->params.h264_deblk_alpha = ctrl->val;
2575 break;
2576 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
2577 ctx->params.h264_deblk_beta = ctrl->val;
2578 break;
2579 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
2580 ctx->params.h264_deblk_enabled = (ctrl->val ==
2581 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2582 break;
Javier Martin186b2502012-07-26 05:53:35 -03002583 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
2584 ctx->params.mpeg4_intra_qp = ctrl->val;
2585 break;
2586 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
2587 ctx->params.mpeg4_inter_qp = ctrl->val;
2588 break;
2589 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
2590 ctx->params.slice_mode = ctrl->val;
2591 break;
2592 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
2593 ctx->params.slice_max_mb = ctrl->val;
2594 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03002595 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
2596 ctx->params.slice_max_bits = ctrl->val * 8;
2597 break;
Javier Martin186b2502012-07-26 05:53:35 -03002598 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
2599 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002600 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
2601 ctx->params.intra_refresh = ctrl->val;
2602 break;
Javier Martin186b2502012-07-26 05:53:35 -03002603 default:
2604 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
2605 "Invalid control, id=%d, val=%d\n",
2606 ctrl->id, ctrl->val);
2607 return -EINVAL;
2608 }
2609
2610 return 0;
2611}
2612
Philipp Zabel814c3762014-07-18 07:22:45 -03002613static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03002614 .s_ctrl = coda_s_ctrl,
2615};
2616
2617static int coda_ctrls_setup(struct coda_ctx *ctx)
2618{
2619 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
2620
2621 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03002622 V4L2_CID_HFLIP, 0, 1, 1, 0);
2623 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2624 V4L2_CID_VFLIP, 0, 1, 1, 0);
2625 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002626 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
2627 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2628 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
2629 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002630 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03002631 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03002632 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03002633 if (ctx->dev->devtype->product != CODA_960) {
2634 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2635 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
2636 }
2637 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2638 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03002639 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03002640 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
2641 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2642 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
2643 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2644 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
2645 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
2646 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
2647 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002648 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
2649 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2650 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
2651 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2652 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03002653 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
2654 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03002655 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2656 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03002657 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2658 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03002659 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
2660 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
2661 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
2662 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
2663 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03002664 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
2665 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002666
2667 if (ctx->ctrls.error) {
2668 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
2669 ctx->ctrls.error);
2670 return -EINVAL;
2671 }
2672
2673 return v4l2_ctrl_handler_setup(&ctx->ctrls);
2674}
2675
Philipp Zabel121cacf2014-07-18 07:22:42 -03002676static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03002677{
Philipp Zabel121cacf2014-07-18 07:22:42 -03002678 vq->drv_priv = ctx;
2679 vq->ops = &coda_qops;
2680 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
2681 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2682 vq->lock = &ctx->dev->dev_mutex;
2683
2684 return vb2_queue_init(vq);
2685}
2686
2687static int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
2688 struct vb2_queue *dst_vq)
2689{
Javier Martin186b2502012-07-26 05:53:35 -03002690 int ret;
2691
Javier Martin186b2502012-07-26 05:53:35 -03002692 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002693 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002694 src_vq->mem_ops = &vb2_dma_contig_memops;
2695
Philipp Zabel121cacf2014-07-18 07:22:42 -03002696 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002697 if (ret)
2698 return ret;
2699
Javier Martin186b2502012-07-26 05:53:35 -03002700 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03002701 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03002702 dst_vq->mem_ops = &vb2_dma_contig_memops;
2703
Philipp Zabel121cacf2014-07-18 07:22:42 -03002704 return coda_queue_init(priv, dst_vq);
2705}
2706
2707static int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
2708 struct vb2_queue *dst_vq)
2709{
2710 int ret;
2711
2712 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2713 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2714 src_vq->mem_ops = &vb2_dma_contig_memops;
2715
2716 ret = coda_queue_init(priv, src_vq);
2717 if (ret)
2718 return ret;
2719
2720 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2721 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2722 dst_vq->mem_ops = &vb2_dma_contig_memops;
2723
2724 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03002725}
2726
Philipp Zabele11f3e62012-07-25 09:16:58 -03002727static int coda_next_free_instance(struct coda_dev *dev)
2728{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002729 int idx = ffz(dev->instance_mask);
2730
2731 if ((idx < 0) ||
2732 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
2733 return -EBUSY;
2734
2735 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002736}
2737
Philipp Zabela1192a12014-07-23 12:28:40 -03002738static int coda_open(struct file *file, enum coda_inst_type inst_type,
2739 const struct coda_context_ops *ctx_ops)
Javier Martin186b2502012-07-26 05:53:35 -03002740{
2741 struct coda_dev *dev = video_drvdata(file);
2742 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002743 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002744 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002745 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03002746
Javier Martin186b2502012-07-26 05:53:35 -03002747 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
2748 if (!ctx)
2749 return -ENOMEM;
2750
Fabio Estevamf82bc202013-08-21 11:14:16 -03002751 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03002752 if (idx < 0) {
2753 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002754 goto err_coda_max;
2755 }
2756 set_bit(idx, &dev->instance_mask);
2757
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002758 name = kasprintf(GFP_KERNEL, "context%d", idx);
2759 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
2760 kfree(name);
2761
Philipp Zabel121cacf2014-07-18 07:22:42 -03002762 ctx->inst_type = inst_type;
Philipp Zabela1192a12014-07-23 12:28:40 -03002763 ctx->ops = ctx_ops;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002764 init_completion(&ctx->completion);
2765 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabela1192a12014-07-23 12:28:40 -03002766 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03002767 v4l2_fh_init(&ctx->fh, video_devdata(file));
2768 file->private_data = &ctx->fh;
2769 v4l2_fh_add(&ctx->fh);
2770 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03002771 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002772 switch (dev->devtype->product) {
2773 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03002774 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002775 ctx->reg_idx = 0;
2776 break;
2777 default:
2778 ctx->reg_idx = idx;
2779 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03002780
Philipp Zabel1e172732014-07-11 06:36:23 -03002781 /* Power up and upload firmware if necessary */
2782 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
2783 if (ret < 0) {
2784 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
2785 goto err_pm_get;
2786 }
2787
Fabio Estevam79830db2013-08-21 11:14:17 -03002788 ret = clk_prepare_enable(dev->clk_per);
2789 if (ret)
2790 goto err_clk_per;
2791
2792 ret = clk_prepare_enable(dev->clk_ahb);
2793 if (ret)
2794 goto err_clk_ahb;
2795
Javier Martin186b2502012-07-26 05:53:35 -03002796 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03002797 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
2798 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03002799 if (IS_ERR(ctx->fh.m2m_ctx)) {
2800 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002801
2802 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
2803 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002804 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03002805 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002806
Javier Martin186b2502012-07-26 05:53:35 -03002807 ret = coda_ctrls_setup(ctx);
2808 if (ret) {
2809 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002810 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03002811 }
2812
2813 ctx->fh.ctrl_handler = &ctx->ctrls;
2814
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002815 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
2816 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002817 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03002818 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03002819 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03002820 }
2821
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002822 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
2823 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
2824 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
2825 if (!ctx->bitstream.vaddr) {
2826 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
2827 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03002828 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002829 }
2830 kfifo_init(&ctx->bitstream_fifo,
2831 ctx->bitstream.vaddr, ctx->bitstream.size);
2832 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002833 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03002834 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002835
Javier Martin186b2502012-07-26 05:53:35 -03002836 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002837 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002838 coda_unlock(ctx);
2839
Javier Martin186b2502012-07-26 05:53:35 -03002840 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
2841 ctx->idx, ctx);
2842
2843 return 0;
2844
Fabio Estevamf82bc202013-08-21 11:14:16 -03002845err_dma_writecombine:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002846 if (ctx->dev->devtype->product == CODA_DX6)
2847 coda_free_aux_buf(dev, &ctx->workbuf);
2848 coda_free_aux_buf(dev, &ctx->parabuf);
2849err_dma_alloc:
2850 v4l2_ctrl_handler_free(&ctx->ctrls);
2851err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03002852 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002853err_ctx_init:
2854 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03002855err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03002856 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03002857err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03002858 pm_runtime_put_sync(&dev->plat_dev->dev);
2859err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03002860 v4l2_fh_del(&ctx->fh);
2861 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002862 clear_bit(ctx->idx, &dev->instance_mask);
2863err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03002864 kfree(ctx);
2865 return ret;
2866}
2867
Philipp Zabel58b76772014-07-23 12:28:43 -03002868static void coda_bit_release(struct coda_ctx *ctx)
2869{
2870 coda_free_framebuffers(ctx);
2871 coda_free_context_buffers(ctx);
2872}
2873
Philipp Zabela1192a12014-07-23 12:28:40 -03002874struct coda_context_ops coda_encode_ops = {
2875 .queue_init = coda_encoder_queue_init,
2876 .start_streaming = coda_start_encoding,
2877 .prepare_run = coda_prepare_encode,
2878 .finish_run = coda_finish_encode,
2879 .seq_end_work = coda_seq_end_work,
Philipp Zabel58b76772014-07-23 12:28:43 -03002880 .release = coda_bit_release,
Philipp Zabela1192a12014-07-23 12:28:40 -03002881};
2882
2883struct coda_context_ops coda_decode_ops = {
2884 .queue_init = coda_decoder_queue_init,
2885 .start_streaming = coda_start_decoding,
2886 .prepare_run = coda_prepare_decode,
2887 .finish_run = coda_finish_decode,
Philipp Zabel58b76772014-07-23 12:28:43 -03002888 .seq_end_work = coda_seq_end_work,
2889 .release = coda_bit_release,
Philipp Zabela1192a12014-07-23 12:28:40 -03002890};
2891
Philipp Zabel121cacf2014-07-18 07:22:42 -03002892static int coda_encoder_open(struct file *file)
2893{
Philipp Zabela1192a12014-07-23 12:28:40 -03002894 return coda_open(file, CODA_INST_ENCODER, &coda_encode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03002895}
2896
2897static int coda_decoder_open(struct file *file)
2898{
Philipp Zabela1192a12014-07-23 12:28:40 -03002899 return coda_open(file, CODA_INST_DECODER, &coda_decode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03002900}
2901
Javier Martin186b2502012-07-26 05:53:35 -03002902static int coda_release(struct file *file)
2903{
2904 struct coda_dev *dev = video_drvdata(file);
2905 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
2906
2907 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
2908 ctx);
2909
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002910 debugfs_remove_recursive(ctx->debugfs_entry);
2911
Philipp Zabel918c66f2013-06-27 06:59:01 -03002912 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03002913 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002914
2915 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03002916 if (ctx->initialized) {
2917 queue_work(dev->workqueue, &ctx->seq_end_work);
2918 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002919 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03002920
Javier Martin186b2502012-07-26 05:53:35 -03002921 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002922 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03002923 coda_unlock(ctx);
2924
Philipp Zabel9082a7c2013-06-21 03:55:31 -03002925 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
2926 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002927 if (ctx->dev->devtype->product == CODA_DX6)
2928 coda_free_aux_buf(dev, &ctx->workbuf);
2929
2930 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03002931 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03002932 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03002933 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03002934 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002935 v4l2_fh_del(&ctx->fh);
2936 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002937 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03002938 if (ctx->ops->release)
2939 ctx->ops->release(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03002940 kfree(ctx);
2941
2942 return 0;
2943}
2944
Philipp Zabel121cacf2014-07-18 07:22:42 -03002945static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03002946 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03002947 .open = coda_encoder_open,
2948 .release = coda_release,
2949 .poll = v4l2_m2m_fop_poll,
2950 .unlocked_ioctl = video_ioctl2,
2951 .mmap = v4l2_m2m_fop_mmap,
2952};
2953
2954static const struct v4l2_file_operations coda_decoder_fops = {
2955 .owner = THIS_MODULE,
2956 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03002957 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002958 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03002959 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03002960 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03002961};
2962
Philipp Zabel918c66f2013-06-27 06:59:01 -03002963static void coda_finish_decode(struct coda_ctx *ctx)
2964{
2965 struct coda_dev *dev = ctx->dev;
2966 struct coda_q_data *q_data_src;
2967 struct coda_q_data *q_data_dst;
2968 struct vb2_buffer *dst_buf;
Philipp Zabel846ced92014-07-11 06:36:31 -03002969 struct coda_timestamp *ts;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002970 int width, height;
2971 int decoded_idx;
2972 int display_idx;
2973 u32 src_fourcc;
2974 int success;
Philipp Zabel410e5e42014-07-11 06:36:32 -03002975 u32 err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03002976 u32 val;
2977
Philipp Zabel14604e32014-07-11 06:36:22 -03002978 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03002979
2980 /* Update kfifo out pointer from coda bitstream read pointer */
2981 coda_kfifo_sync_from_device(ctx);
2982
2983 /*
2984 * in stream-end mode, the read pointer can overshoot the write pointer
2985 * by up to 512 bytes
2986 */
2987 if (ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) {
Michael Olbrich390503b2014-07-18 07:22:39 -03002988 if (coda_get_bitstream_payload(ctx) >= CODA_MAX_FRAME_SIZE - 512)
Philipp Zabel918c66f2013-06-27 06:59:01 -03002989 kfifo_init(&ctx->bitstream_fifo,
2990 ctx->bitstream.vaddr, ctx->bitstream.size);
2991 }
2992
2993 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
2994 src_fourcc = q_data_src->fourcc;
2995
2996 val = coda_read(dev, CODA_RET_DEC_PIC_SUCCESS);
2997 if (val != 1)
2998 pr_err("DEC_PIC_SUCCESS = %d\n", val);
2999
3000 success = val & 0x1;
3001 if (!success)
3002 v4l2_err(&dev->v4l2_dev, "decode failed\n");
3003
3004 if (src_fourcc == V4L2_PIX_FMT_H264) {
3005 if (val & (1 << 3))
3006 v4l2_err(&dev->v4l2_dev,
3007 "insufficient PS buffer space (%d bytes)\n",
3008 ctx->psbuf.size);
3009 if (val & (1 << 2))
3010 v4l2_err(&dev->v4l2_dev,
3011 "insufficient slice buffer space (%d bytes)\n",
3012 ctx->slicebuf.size);
3013 }
3014
3015 val = coda_read(dev, CODA_RET_DEC_PIC_SIZE);
3016 width = (val >> 16) & 0xffff;
3017 height = val & 0xffff;
3018
3019 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
3020
Philipp Zabel52c41672014-07-11 06:36:19 -03003021 /* frame crop information */
3022 if (src_fourcc == V4L2_PIX_FMT_H264) {
3023 u32 left_right;
3024 u32 top_bottom;
3025
3026 left_right = coda_read(dev, CODA_RET_DEC_PIC_CROP_LEFT_RIGHT);
3027 top_bottom = coda_read(dev, CODA_RET_DEC_PIC_CROP_TOP_BOTTOM);
3028
3029 if (left_right == 0xffffffff && top_bottom == 0xffffffff) {
3030 /* Keep current crop information */
3031 } else {
3032 struct v4l2_rect *rect = &q_data_dst->rect;
3033
3034 rect->left = left_right >> 16 & 0xffff;
3035 rect->top = top_bottom >> 16 & 0xffff;
3036 rect->width = width - rect->left -
3037 (left_right & 0xffff);
3038 rect->height = height - rect->top -
3039 (top_bottom & 0xffff);
3040 }
3041 } else {
3042 /* no cropping */
3043 }
3044
Philipp Zabel410e5e42014-07-11 06:36:32 -03003045 err_mb = coda_read(dev, CODA_RET_DEC_PIC_ERR_MB);
3046 if (err_mb > 0)
Philipp Zabel918c66f2013-06-27 06:59:01 -03003047 v4l2_err(&dev->v4l2_dev,
Philipp Zabel410e5e42014-07-11 06:36:32 -03003048 "errors in %d macroblocks\n", err_mb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003049
3050 if (dev->devtype->product == CODA_7541) {
3051 val = coda_read(dev, CODA_RET_DEC_PIC_OPTION);
3052 if (val == 0) {
3053 /* not enough bitstream data */
3054 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3055 "prescan failed: %d\n", val);
Philipp Zabel84e23652014-07-11 06:36:34 -03003056 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003057 return;
3058 }
3059 }
3060
3061 ctx->frm_dis_flg = coda_read(dev, CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3062
3063 /*
3064 * The previous display frame was copied out by the rotator,
3065 * now it can be overwritten again
3066 */
3067 if (ctx->display_idx >= 0 &&
3068 ctx->display_idx < ctx->num_internal_frames) {
3069 ctx->frm_dis_flg &= ~(1 << ctx->display_idx);
3070 coda_write(dev, ctx->frm_dis_flg,
3071 CODA_REG_BIT_FRM_DIS_FLG(ctx->reg_idx));
3072 }
3073
3074 /*
3075 * The index of the last decoded frame, not necessarily in
3076 * display order, and the index of the next display frame.
3077 * The latter could have been decoded in a previous run.
3078 */
3079 decoded_idx = coda_read(dev, CODA_RET_DEC_PIC_CUR_IDX);
3080 display_idx = coda_read(dev, CODA_RET_DEC_PIC_FRAME_IDX);
3081
3082 if (decoded_idx == -1) {
3083 /* no frame was decoded, but we might have a display frame */
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003084 if (display_idx >= 0 && display_idx < ctx->num_internal_frames)
3085 ctx->sequence_offset++;
3086 else if (ctx->display_idx < 0)
Philipp Zabel84e23652014-07-11 06:36:34 -03003087 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003088 } else if (decoded_idx == -2) {
3089 /* no frame was decoded, we still return the remaining buffers */
3090 } else if (decoded_idx < 0 || decoded_idx >= ctx->num_internal_frames) {
3091 v4l2_err(&dev->v4l2_dev,
3092 "decoded frame index out of range: %d\n", decoded_idx);
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003093 } else {
Philipp Zabel846ced92014-07-11 06:36:31 -03003094 ts = list_first_entry(&ctx->timestamp_list,
3095 struct coda_timestamp, list);
3096 list_del(&ts->list);
3097 val = coda_read(dev, CODA_RET_DEC_PIC_FRAME_NUM) - 1;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003098 val -= ctx->sequence_offset;
3099 if (val != (ts->sequence & 0xffff)) {
Philipp Zabel846ced92014-07-11 06:36:31 -03003100 v4l2_err(&dev->v4l2_dev,
Philipp Zabelcb2c0282014-07-11 06:36:33 -03003101 "sequence number mismatch (%d(%d) != %d)\n",
3102 val, ctx->sequence_offset, ts->sequence);
Philipp Zabel846ced92014-07-11 06:36:31 -03003103 }
3104 ctx->frame_timestamps[decoded_idx] = *ts;
3105 kfree(ts);
3106
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003107 val = coda_read(dev, CODA_RET_DEC_PIC_TYPE) & 0x7;
3108 if (val == 0)
3109 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_KEYFRAME;
3110 else if (val == 1)
3111 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_PFRAME;
3112 else
3113 ctx->frame_types[decoded_idx] = V4L2_BUF_FLAG_BFRAME;
Philipp Zabel410e5e42014-07-11 06:36:32 -03003114
3115 ctx->frame_errors[decoded_idx] = err_mb;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003116 }
3117
3118 if (display_idx == -1) {
3119 /*
3120 * no more frames to be decoded, but there could still
3121 * be rotator output to dequeue
3122 */
Philipp Zabel84e23652014-07-11 06:36:34 -03003123 ctx->hold = true;
Philipp Zabel918c66f2013-06-27 06:59:01 -03003124 } else if (display_idx == -3) {
3125 /* possibly prescan failure */
3126 } else if (display_idx < 0 || display_idx >= ctx->num_internal_frames) {
3127 v4l2_err(&dev->v4l2_dev,
3128 "presentation frame index out of range: %d\n",
3129 display_idx);
3130 }
3131
3132 /* If a frame was copied out, return it */
3133 if (ctx->display_idx >= 0 &&
3134 ctx->display_idx < ctx->num_internal_frames) {
Philipp Zabel14604e32014-07-11 06:36:22 -03003135 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003136 dst_buf->v4l2_buf.sequence = ctx->osequence++;
3137
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003138 dst_buf->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME |
3139 V4L2_BUF_FLAG_PFRAME |
3140 V4L2_BUF_FLAG_BFRAME);
3141 dst_buf->v4l2_buf.flags |= ctx->frame_types[ctx->display_idx];
Philipp Zabel846ced92014-07-11 06:36:31 -03003142 ts = &ctx->frame_timestamps[ctx->display_idx];
3143 dst_buf->v4l2_buf.timecode = ts->timecode;
3144 dst_buf->v4l2_buf.timestamp = ts->timestamp;
Philipp Zabel1a8b3812014-07-11 06:36:12 -03003145
Philipp Zabel918c66f2013-06-27 06:59:01 -03003146 vb2_set_plane_payload(dst_buf, 0, width * height * 3 / 2);
3147
Philipp Zabel410e5e42014-07-11 06:36:32 -03003148 v4l2_m2m_buf_done(dst_buf, ctx->frame_errors[display_idx] ?
3149 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003150
3151 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3152 "job finished: decoding frame (%d) (%s)\n",
3153 dst_buf->v4l2_buf.sequence,
3154 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3155 "KEYFRAME" : "PFRAME");
3156 } else {
3157 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3158 "job finished: no frame decoded\n");
3159 }
3160
3161 /* The rotator will copy the current display frame next time */
3162 ctx->display_idx = display_idx;
3163}
3164
3165static void coda_finish_encode(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03003166{
Philipp Zabelec25f682012-07-20 08:54:29 -03003167 struct vb2_buffer *src_buf, *dst_buf;
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003168 struct coda_dev *dev = ctx->dev;
Javier Martin186b2502012-07-26 05:53:35 -03003169 u32 wr_ptr, start_ptr;
Javier Martin186b2502012-07-26 05:53:35 -03003170
Philipp Zabel14604e32014-07-11 06:36:22 -03003171 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
3172 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003173
3174 /* Get results from the coda */
Javier Martin186b2502012-07-26 05:53:35 -03003175 start_ptr = coda_read(dev, CODA_CMD_ENC_PIC_BB_START);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003176 wr_ptr = coda_read(dev, CODA_REG_BIT_WR_PTR(ctx->reg_idx));
3177
Javier Martin186b2502012-07-26 05:53:35 -03003178 /* Calculate bytesused field */
3179 if (dst_buf->v4l2_buf.sequence == 0) {
Philipp Zabel366108f2013-06-21 03:55:27 -03003180 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr +
3181 ctx->vpu_header_size[0] +
3182 ctx->vpu_header_size[1] +
3183 ctx->vpu_header_size[2]);
Javier Martin186b2502012-07-26 05:53:35 -03003184 } else {
Philipp Zabel366108f2013-06-21 03:55:27 -03003185 vb2_set_plane_payload(dst_buf, 0, wr_ptr - start_ptr);
Javier Martin186b2502012-07-26 05:53:35 -03003186 }
3187
3188 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "frame size = %u\n",
3189 wr_ptr - start_ptr);
3190
3191 coda_read(dev, CODA_RET_ENC_PIC_SLICE_NUM);
3192 coda_read(dev, CODA_RET_ENC_PIC_FLAG);
3193
Philipp Zabel51660282013-09-30 10:34:49 -03003194 if (coda_read(dev, CODA_RET_ENC_PIC_TYPE) == 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003195 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
3196 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_PFRAME;
3197 } else {
3198 dst_buf->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
3199 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_KEYFRAME;
3200 }
3201
Kamil Debskiccd571c2013-04-24 10:38:24 -03003202 dst_buf->v4l2_buf.timestamp = src_buf->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -03003203 dst_buf->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
3204 dst_buf->v4l2_buf.flags |=
3205 src_buf->v4l2_buf.flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiccd571c2013-04-24 10:38:24 -03003206 dst_buf->v4l2_buf.timecode = src_buf->v4l2_buf.timecode;
3207
Philipp Zabelec25f682012-07-20 08:54:29 -03003208 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
Philipp Zabel89548442014-07-11 06:36:17 -03003209
Philipp Zabel14604e32014-07-11 06:36:22 -03003210 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03003211 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
3212
3213 ctx->gopcounter--;
3214 if (ctx->gopcounter < 0)
3215 ctx->gopcounter = ctx->params.gop_size - 1;
3216
3217 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
3218 "job finished: encoding frame (%d) (%s)\n",
3219 dst_buf->v4l2_buf.sequence,
3220 (dst_buf->v4l2_buf.flags & V4L2_BUF_FLAG_KEYFRAME) ?
3221 "KEYFRAME" : "PFRAME");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003222}
3223
3224static irqreturn_t coda_irq_handler(int irq, void *data)
3225{
3226 struct coda_dev *dev = data;
3227 struct coda_ctx *ctx;
3228
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003229 /* read status register to attend the IRQ */
3230 coda_read(dev, CODA_REG_BIT_INT_STATUS);
3231 coda_write(dev, CODA_REG_BIT_INT_CLEAR_SET,
3232 CODA_REG_BIT_INT_CLEAR);
3233
3234 ctx = v4l2_m2m_get_curr_priv(dev->m2m_dev);
3235 if (ctx == NULL) {
3236 v4l2_err(&dev->v4l2_dev, "Instance released before the end of transaction\n");
3237 mutex_unlock(&dev->coda_mutex);
3238 return IRQ_HANDLED;
3239 }
3240
3241 if (ctx->aborting) {
3242 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3243 "task has been aborted\n");
Philipp Zabel477c1cf2013-06-21 03:55:33 -03003244 }
3245
3246 if (coda_isbusy(ctx->dev)) {
3247 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
3248 "coda is still busy!!!!\n");
3249 return IRQ_NONE;
3250 }
3251
Philipp Zabel32b6f202014-07-11 06:36:20 -03003252 complete(&ctx->completion);
Javier Martin186b2502012-07-26 05:53:35 -03003253
3254 return IRQ_HANDLED;
3255}
3256
3257static u32 coda_supported_firmwares[] = {
3258 CODA_FIRMWARE_VERNUM(CODA_DX6, 2, 2, 5),
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003259 CODA_FIRMWARE_VERNUM(CODA_7541, 1, 4, 50),
Philipp Zabel89548442014-07-11 06:36:17 -03003260 CODA_FIRMWARE_VERNUM(CODA_960, 2, 1, 5),
Javier Martin186b2502012-07-26 05:53:35 -03003261};
3262
3263static bool coda_firmware_supported(u32 vernum)
3264{
3265 int i;
3266
3267 for (i = 0; i < ARRAY_SIZE(coda_supported_firmwares); i++)
3268 if (vernum == coda_supported_firmwares[i])
3269 return true;
3270 return false;
3271}
3272
Philipp Zabel87048bb2012-07-02 07:03:43 -03003273static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03003274{
Javier Martin186b2502012-07-26 05:53:35 -03003275 u32 data;
3276 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03003277 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03003278
Fabio Estevam79830db2013-08-21 11:14:17 -03003279 ret = clk_prepare_enable(dev->clk_per);
3280 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03003281 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03003282
3283 ret = clk_prepare_enable(dev->clk_ahb);
3284 if (ret)
3285 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03003286
Philipp Zabel8f452842014-07-11 06:36:35 -03003287 if (dev->rstc)
3288 reset_control_reset(dev->rstc);
3289
Javier Martin186b2502012-07-26 05:53:35 -03003290 /*
3291 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03003292 * The 16-bit chars in the code buffer are in memory access
3293 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03003294 * Data in this SRAM survives a reboot.
3295 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03003296 p = (u16 *)dev->codebuf.vaddr;
3297 if (dev->devtype->product == CODA_DX6) {
3298 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3299 data = CODA_DOWN_ADDRESS_SET(i) |
3300 CODA_DOWN_DATA_SET(p[i ^ 1]);
3301 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3302 }
3303 } else {
3304 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
3305 data = CODA_DOWN_ADDRESS_SET(i) |
3306 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
3307 3 - (i % 4)]);
3308 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
3309 }
Javier Martin186b2502012-07-26 05:53:35 -03003310 }
Javier Martin186b2502012-07-26 05:53:35 -03003311
Philipp Zabel9acf7692013-05-23 10:42:56 -03003312 /* Clear registers */
3313 for (i = 0; i < 64; i++)
3314 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
3315
Javier Martin186b2502012-07-26 05:53:35 -03003316 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03003317 if (dev->devtype->product == CODA_960 ||
3318 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003319 coda_write(dev, dev->tempbuf.paddr,
3320 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03003321 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003322 } else {
3323 coda_write(dev, dev->workbuf.paddr,
3324 CODA_REG_BIT_WORK_BUF_ADDR);
3325 }
Javier Martin186b2502012-07-26 05:53:35 -03003326 coda_write(dev, dev->codebuf.paddr,
3327 CODA_REG_BIT_CODE_BUF_ADDR);
3328 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
3329
3330 /* Set default values */
3331 switch (dev->devtype->product) {
3332 case CODA_DX6:
3333 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3334 break;
3335 default:
3336 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
3337 }
Philipp Zabel89548442014-07-11 06:36:17 -03003338 if (dev->devtype->product == CODA_960)
3339 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
3340 else
3341 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03003342
3343 if (dev->devtype->product != CODA_DX6)
3344 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
3345
Javier Martin186b2502012-07-26 05:53:35 -03003346 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
3347 CODA_REG_BIT_INT_ENABLE);
3348
3349 /* Reset VPU and start processor */
3350 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
3351 data |= CODA_REG_RESET_ENABLE;
3352 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3353 udelay(10);
3354 data &= ~CODA_REG_RESET_ENABLE;
3355 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
3356 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
3357
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003358 clk_disable_unprepare(dev->clk_ahb);
3359 clk_disable_unprepare(dev->clk_per);
3360
3361 return 0;
3362
3363err_clk_ahb:
3364 clk_disable_unprepare(dev->clk_per);
3365err_clk_per:
3366 return ret;
3367}
3368
3369static int coda_check_firmware(struct coda_dev *dev)
3370{
3371 u16 product, major, minor, release;
3372 u32 data;
3373 int ret;
3374
3375 ret = clk_prepare_enable(dev->clk_per);
3376 if (ret)
3377 goto err_clk_per;
3378
3379 ret = clk_prepare_enable(dev->clk_ahb);
3380 if (ret)
3381 goto err_clk_ahb;
3382
Javier Martin186b2502012-07-26 05:53:35 -03003383 coda_write(dev, 0, CODA_CMD_FIRMWARE_VERNUM);
3384 coda_write(dev, CODA_REG_BIT_BUSY_FLAG, CODA_REG_BIT_BUSY);
3385 coda_write(dev, 0, CODA_REG_BIT_RUN_INDEX);
3386 coda_write(dev, 0, CODA_REG_BIT_RUN_COD_STD);
3387 coda_write(dev, CODA_COMMAND_FIRMWARE_GET, CODA_REG_BIT_RUN_COMMAND);
3388 if (coda_wait_timeout(dev)) {
Javier Martin186b2502012-07-26 05:53:35 -03003389 v4l2_err(&dev->v4l2_dev, "firmware get command error\n");
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003390 ret = -EIO;
3391 goto err_run_cmd;
Javier Martin186b2502012-07-26 05:53:35 -03003392 }
3393
Philipp Zabel89548442014-07-11 06:36:17 -03003394 if (dev->devtype->product == CODA_960) {
3395 data = coda_read(dev, CODA9_CMD_FIRMWARE_CODE_REV);
3396 v4l2_info(&dev->v4l2_dev, "Firmware code revision: %d\n",
3397 data);
3398 }
3399
Javier Martin186b2502012-07-26 05:53:35 -03003400 /* Check we are compatible with the loaded firmware */
3401 data = coda_read(dev, CODA_CMD_FIRMWARE_VERNUM);
3402 product = CODA_FIRMWARE_PRODUCT(data);
3403 major = CODA_FIRMWARE_MAJOR(data);
3404 minor = CODA_FIRMWARE_MINOR(data);
3405 release = CODA_FIRMWARE_RELEASE(data);
3406
3407 clk_disable_unprepare(dev->clk_per);
3408 clk_disable_unprepare(dev->clk_ahb);
3409
3410 if (product != dev->devtype->product) {
3411 v4l2_err(&dev->v4l2_dev, "Wrong firmware. Hw: %s, Fw: %s,"
3412 " Version: %u.%u.%u\n",
3413 coda_product_name(dev->devtype->product),
3414 coda_product_name(product), major, minor, release);
3415 return -EINVAL;
3416 }
3417
3418 v4l2_info(&dev->v4l2_dev, "Initialized %s.\n",
3419 coda_product_name(product));
3420
3421 if (coda_firmware_supported(data)) {
3422 v4l2_info(&dev->v4l2_dev, "Firmware version: %u.%u.%u\n",
3423 major, minor, release);
3424 } else {
3425 v4l2_warn(&dev->v4l2_dev, "Unsupported firmware version: "
3426 "%u.%u.%u\n", major, minor, release);
3427 }
3428
3429 return 0;
Fabio Estevam79830db2013-08-21 11:14:17 -03003430
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003431err_run_cmd:
3432 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03003433err_clk_ahb:
3434 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03003435err_clk_per:
Fabio Estevam79830db2013-08-21 11:14:17 -03003436 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003437}
3438
Philipp Zabel121cacf2014-07-18 07:22:42 -03003439static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
3440{
3441 vfd->release = video_device_release_empty,
3442 vfd->lock = &dev->dev_mutex;
3443 vfd->v4l2_dev = &dev->v4l2_dev;
3444 vfd->vfl_dir = VFL_DIR_M2M;
3445 video_set_drvdata(vfd, dev);
3446
3447 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
3448}
3449
Javier Martin186b2502012-07-26 05:53:35 -03003450static void coda_fw_callback(const struct firmware *fw, void *context)
3451{
3452 struct coda_dev *dev = context;
3453 struct platform_device *pdev = dev->plat_dev;
3454 int ret;
3455
3456 if (!fw) {
3457 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
3458 return;
3459 }
3460
3461 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003462 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
3463 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003464 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03003465 dev_err(&pdev->dev, "failed to allocate code buffer\n");
3466 return;
3467 }
3468
Philipp Zabel87048bb2012-07-02 07:03:43 -03003469 /* Copy the whole firmware image to the code buffer */
3470 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
3471 release_firmware(fw);
3472
Philipp Zabel1e172732014-07-11 06:36:23 -03003473 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
3474 /*
3475 * Enabling power temporarily will cause coda_hw_init to be
3476 * called via coda_runtime_resume by the pm domain.
3477 */
3478 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
3479 if (ret < 0) {
3480 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
3481 ret);
3482 return;
3483 }
3484
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003485 ret = coda_check_firmware(dev);
3486 if (ret < 0)
3487 return;
3488
Philipp Zabel1e172732014-07-11 06:36:23 -03003489 pm_runtime_put_sync(&dev->plat_dev->dev);
3490 } else {
3491 /*
3492 * If runtime pm is disabled or pm_domain is not set,
3493 * initialize once manually.
3494 */
3495 ret = coda_hw_init(dev);
3496 if (ret < 0) {
3497 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
3498 return;
3499 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03003500
3501 ret = coda_check_firmware(dev);
3502 if (ret < 0)
3503 return;
Javier Martin186b2502012-07-26 05:53:35 -03003504 }
3505
Javier Martin186b2502012-07-26 05:53:35 -03003506 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
3507 if (IS_ERR(dev->alloc_ctx)) {
3508 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
3509 return;
3510 }
3511
3512 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
3513 if (IS_ERR(dev->m2m_dev)) {
3514 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
3515 goto rel_ctx;
3516 }
3517
Philipp Zabel121cacf2014-07-18 07:22:42 -03003518 dev->vfd[0].fops = &coda_encoder_fops,
3519 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
3520 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
3521 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03003522 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03003523 v4l2_err(&dev->v4l2_dev,
3524 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03003525 goto rel_m2m;
3526 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03003527
3528 dev->vfd[1].fops = &coda_decoder_fops,
3529 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
3530 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
3531 ret = coda_register_device(dev, &dev->vfd[1]);
3532 if (ret) {
3533 v4l2_err(&dev->v4l2_dev,
3534 "Failed to register decoder video device\n");
3535 goto rel_m2m;
3536 }
3537
3538 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
3539 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03003540
3541 return;
3542
3543rel_m2m:
3544 v4l2_m2m_release(dev->m2m_dev);
3545rel_ctx:
3546 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3547}
3548
3549static int coda_firmware_request(struct coda_dev *dev)
3550{
3551 char *fw = dev->devtype->firmware;
3552
3553 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
3554 coda_product_name(dev->devtype->product));
3555
3556 return request_firmware_nowait(THIS_MODULE, true,
3557 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
3558}
3559
3560enum coda_platform {
3561 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003562 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03003563 CODA_IMX6Q,
3564 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03003565};
3566
Emil Goodec06d8752012-08-14 17:44:42 -03003567static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03003568 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003569 .firmware = "v4l-codadx6-imx27.bin",
3570 .product = CODA_DX6,
3571 .codecs = codadx6_codecs,
3572 .num_codecs = ARRAY_SIZE(codadx6_codecs),
3573 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003574 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03003575 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003576 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003577 .firmware = "v4l-coda7541-imx53.bin",
3578 .product = CODA_7541,
3579 .codecs = coda7_codecs,
3580 .num_codecs = ARRAY_SIZE(coda7_codecs),
3581 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003582 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003583 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003584 },
Philipp Zabel89548442014-07-11 06:36:17 -03003585 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003586 .firmware = "v4l-coda960-imx6q.bin",
3587 .product = CODA_960,
3588 .codecs = coda9_codecs,
3589 .num_codecs = ARRAY_SIZE(coda9_codecs),
3590 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003591 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003592 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03003593 },
3594 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003595 .firmware = "v4l-coda960-imx6dl.bin",
3596 .product = CODA_960,
3597 .codecs = coda9_codecs,
3598 .num_codecs = ARRAY_SIZE(coda9_codecs),
3599 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003600 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03003601 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03003602 },
Javier Martin186b2502012-07-26 05:53:35 -03003603};
3604
3605static struct platform_device_id coda_platform_ids[] = {
3606 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03003607 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03003608 { /* sentinel */ }
3609};
3610MODULE_DEVICE_TABLE(platform, coda_platform_ids);
3611
3612#ifdef CONFIG_OF
3613static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03003614 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03003615 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03003616 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
3617 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03003618 { /* sentinel */ }
3619};
3620MODULE_DEVICE_TABLE(of, coda_dt_ids);
3621#endif
3622
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003623static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03003624{
3625 const struct of_device_id *of_id =
3626 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
3627 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07003628 struct coda_platform_data *pdata = pdev->dev.platform_data;
3629 struct device_node *np = pdev->dev.of_node;
3630 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03003631 struct coda_dev *dev;
3632 struct resource *res;
3633 int ret, irq;
3634
3635 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
3636 if (!dev) {
3637 dev_err(&pdev->dev, "Not enough memory for %s\n",
3638 CODA_NAME);
3639 return -ENOMEM;
3640 }
3641
3642 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03003643 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03003644
3645 dev->plat_dev = pdev;
3646 dev->clk_per = devm_clk_get(&pdev->dev, "per");
3647 if (IS_ERR(dev->clk_per)) {
3648 dev_err(&pdev->dev, "Could not get per clock\n");
3649 return PTR_ERR(dev->clk_per);
3650 }
3651
3652 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
3653 if (IS_ERR(dev->clk_ahb)) {
3654 dev_err(&pdev->dev, "Could not get ahb clock\n");
3655 return PTR_ERR(dev->clk_ahb);
3656 }
3657
3658 /* Get memory for physical registers */
3659 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03003660 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
3661 if (IS_ERR(dev->regs_base))
3662 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03003663
3664 /* IRQ */
3665 irq = platform_get_irq(pdev, 0);
3666 if (irq < 0) {
3667 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03003668 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03003669 }
3670
Fabio Estevam08c8d142014-06-04 15:46:24 -03003671 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
3672 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
3673 if (ret < 0) {
3674 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3675 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03003676 }
3677
Philipp Zabelee27aa92014-07-24 16:50:03 -03003678 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03003679 if (IS_ERR(dev->rstc)) {
3680 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03003681 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03003682 dev->rstc = NULL;
3683 } else {
3684 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
3685 return ret;
3686 }
3687 }
3688
Philipp Zabel657eee72013-04-29 16:17:14 -07003689 /* Get IRAM pool from device tree or platform data */
3690 pool = of_get_named_gen_pool(np, "iram", 0);
3691 if (!pool && pdata)
3692 pool = dev_get_gen_pool(pdata->iram_dev);
3693 if (!pool) {
3694 dev_err(&pdev->dev, "iram pool not available\n");
3695 return -ENOMEM;
3696 }
3697 dev->iram_pool = pool;
3698
Javier Martin186b2502012-07-26 05:53:35 -03003699 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3700 if (ret)
3701 return ret;
3702
3703 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03003704 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03003705
3706 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
3707
3708 if (of_id) {
3709 dev->devtype = of_id->data;
3710 } else if (pdev_id) {
3711 dev->devtype = &coda_devdata[pdev_id->driver_data];
3712 } else {
3713 v4l2_device_unregister(&dev->v4l2_dev);
3714 return -EINVAL;
3715 }
3716
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003717 dev->debugfs_root = debugfs_create_dir("coda", NULL);
3718 if (!dev->debugfs_root)
3719 dev_warn(&pdev->dev, "failed to create debugfs root\n");
3720
Javier Martin186b2502012-07-26 05:53:35 -03003721 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003722 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003723 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03003724 dev->devtype->workbuf_size, "workbuf",
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 work buffer\n");
3728 v4l2_device_unregister(&dev->v4l2_dev);
3729 return ret;
3730 }
Javier Martin186b2502012-07-26 05:53:35 -03003731 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003732
3733 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003734 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03003735 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003736 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003737 if (ret < 0) {
3738 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
3739 v4l2_device_unregister(&dev->v4l2_dev);
3740 return ret;
3741 }
Javier Martin186b2502012-07-26 05:53:35 -03003742 }
3743
Philipp Zabel401e9722014-07-11 06:36:43 -03003744 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003745 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
3746 &dev->iram.paddr);
3747 if (!dev->iram.vaddr) {
Philipp Zabel657eee72013-04-29 16:17:14 -07003748 dev_err(&pdev->dev, "unable to alloc iram\n");
3749 return -ENOMEM;
Philipp Zabel10436672012-07-02 09:03:55 -03003750 }
3751
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003752 dev->iram.blob.data = dev->iram.vaddr;
3753 dev->iram.blob.size = dev->iram.size;
3754 dev->iram.dentry = debugfs_create_blob("iram", 0644, dev->debugfs_root,
3755 &dev->iram.blob);
3756
Philipp Zabel32b6f202014-07-11 06:36:20 -03003757 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
3758 if (!dev->workqueue) {
3759 dev_err(&pdev->dev, "unable to alloc workqueue\n");
3760 return -ENOMEM;
3761 }
3762
Javier Martin186b2502012-07-26 05:53:35 -03003763 platform_set_drvdata(pdev, dev);
3764
Philipp Zabel1e172732014-07-11 06:36:23 -03003765 pm_runtime_enable(&pdev->dev);
3766
Javier Martin186b2502012-07-26 05:53:35 -03003767 return coda_firmware_request(dev);
3768}
3769
3770static int coda_remove(struct platform_device *pdev)
3771{
3772 struct coda_dev *dev = platform_get_drvdata(pdev);
3773
Philipp Zabel121cacf2014-07-18 07:22:42 -03003774 video_unregister_device(&dev->vfd[0]);
3775 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03003776 if (dev->m2m_dev)
3777 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03003778 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03003779 if (dev->alloc_ctx)
3780 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
3781 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03003782 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03003783 if (dev->iram.vaddr)
3784 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
3785 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03003786 coda_free_aux_buf(dev, &dev->codebuf);
3787 coda_free_aux_buf(dev, &dev->tempbuf);
3788 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03003789 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03003790 return 0;
3791}
3792
Philipp Zabel1e172732014-07-11 06:36:23 -03003793#ifdef CONFIG_PM_RUNTIME
3794static int coda_runtime_resume(struct device *dev)
3795{
3796 struct coda_dev *cdev = dev_get_drvdata(dev);
3797 int ret = 0;
3798
Philipp Zabel65919e62014-07-18 07:22:36 -03003799 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03003800 ret = coda_hw_init(cdev);
3801 if (ret)
3802 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
3803 }
3804
3805 return ret;
3806}
3807#endif
3808
3809static const struct dev_pm_ops coda_pm_ops = {
3810 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
3811};
3812
Javier Martin186b2502012-07-26 05:53:35 -03003813static struct platform_driver coda_driver = {
3814 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08003815 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03003816 .driver = {
3817 .name = CODA_NAME,
3818 .owner = THIS_MODULE,
3819 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03003820 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03003821 },
3822 .id_table = coda_platform_ids,
3823};
3824
3825module_platform_driver(coda_driver);
3826
3827MODULE_LICENSE("GPL");
3828MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
3829MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");