blob: 48be97330ee108afe1c1790ac89b3619ef67f86c [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"
Javier Martin186b2502012-07-26 05:53:35 -030042
43#define CODA_NAME "coda"
44
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030045#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030046
Javier Martin186b2502012-07-26 05:53:35 -030047#define CODA_PARA_BUF_SIZE (10 * 1024)
48#define CODA_ISRAM_SIZE (2048 * 2)
49
Javier Martin186b2502012-07-26 05:53:35 -030050#define MIN_W 176
51#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030052
53#define S_ALIGN 1 /* multiple of 2 */
54#define W_ALIGN 1 /* multiple of 2 */
55#define H_ALIGN 1 /* multiple of 2 */
56
57#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
58
Philipp Zabel79924ca2014-07-23 12:28:45 -030059int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030060module_param(coda_debug, int, 0644);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030061MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
Javier Martin186b2502012-07-26 05:53:35 -030062
Javier Martin186b2502012-07-26 05:53:35 -030063struct coda_fmt {
64 char *name;
65 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030066};
67
Philipp Zabela2b3e462014-07-23 12:28:39 -030068void coda_write(struct coda_dev *dev, u32 data, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030069{
Philipp Zabeld60b18b2014-08-05 14:00:15 -030070 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030071 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
72 writel(data, dev->regs_base + reg);
73}
74
Philipp Zabela2b3e462014-07-23 12:28:39 -030075unsigned int coda_read(struct coda_dev *dev, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030076{
77 u32 data;
Philipp Zabelf23797b2014-08-06 08:02:23 -030078
Javier Martin186b2502012-07-26 05:53:35 -030079 data = readl(dev->regs_base + reg);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030080 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030081 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
82 return data;
83}
84
Philipp Zabel856d7d92014-09-29 09:53:44 -030085void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
86 struct vb2_buffer *buf, unsigned int reg_y)
87{
88 u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
89 u32 base_cb, base_cr;
90
91 switch (q_data->fourcc) {
92 case V4L2_PIX_FMT_YVU420:
93 /* Switch Cb and Cr for YVU420 format */
94 base_cr = base_y + q_data->bytesperline * q_data->height;
95 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
96 break;
97 case V4L2_PIX_FMT_YUV420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -030098 case V4L2_PIX_FMT_NV12:
Philipp Zabel856d7d92014-09-29 09:53:44 -030099 default:
100 base_cb = base_y + q_data->bytesperline * q_data->height;
101 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
102 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300103 case V4L2_PIX_FMT_YUV422P:
104 base_cb = base_y + q_data->bytesperline * q_data->height;
105 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
Philipp Zabel856d7d92014-09-29 09:53:44 -0300106 }
107
108 coda_write(ctx->dev, base_y, reg_y);
109 coda_write(ctx->dev, base_cb, reg_y + 4);
110 coda_write(ctx->dev, base_cr, reg_y + 8);
111}
112
Javier Martin186b2502012-07-26 05:53:35 -0300113/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300114 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300115 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300116static const struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300117 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300118 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300119 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300120 },
121 {
122 .name = "YUV 4:2:0 Planar, YCrCb",
123 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300124 },
125 {
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300126 .name = "YUV 4:2:0 Partial interleaved Y/CbCr",
127 .fourcc = V4L2_PIX_FMT_NV12,
128 },
129 {
Philipp Zabel4de69312014-10-02 14:08:26 -0300130 .name = "YUV 4:2:2 Planar, YCbCr",
131 .fourcc = V4L2_PIX_FMT_YUV422P,
132 },
133 {
Javier Martin186b2502012-07-26 05:53:35 -0300134 .name = "H264 Encoded Stream",
135 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300136 },
137 {
138 .name = "MPEG4 Encoded Stream",
139 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300140 },
141};
142
Philipp Zabelb96904e2013-05-23 10:42:58 -0300143#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
144 { mode, src_fourcc, dst_fourcc, max_w, max_h }
145
146/*
147 * Arrays of codecs supported by each given version of Coda:
148 * i.MX27 -> codadx6
149 * i.MX5x -> coda7
150 * i.MX6 -> coda960
151 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
152 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300153static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300154 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
155 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300156};
157
Philipp Zabel814c3762014-07-18 07:22:45 -0300158static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300159 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
160 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300161 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
162 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300163};
164
Philipp Zabel814c3762014-07-18 07:22:45 -0300165static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300166 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
167 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
168 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
169 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300170};
171
Philipp Zabelb96904e2013-05-23 10:42:58 -0300172static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300173{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300174 switch (fourcc) {
175 case V4L2_PIX_FMT_YUV420:
176 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300177 case V4L2_PIX_FMT_NV12:
Philipp Zabel4de69312014-10-02 14:08:26 -0300178 case V4L2_PIX_FMT_YUV422P:
Philipp Zabelb96904e2013-05-23 10:42:58 -0300179 return true;
180 default:
181 return false;
182 }
183}
Javier Martin186b2502012-07-26 05:53:35 -0300184
Philipp Zabelb96904e2013-05-23 10:42:58 -0300185/*
186 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
187 * tables.
188 */
189static u32 coda_format_normalize_yuv(u32 fourcc)
190{
191 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
192}
193
Philipp Zabel814c3762014-07-18 07:22:45 -0300194static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
195 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300196{
Philipp Zabel814c3762014-07-18 07:22:45 -0300197 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300198 int num_codecs = dev->devtype->num_codecs;
199 int k;
200
201 src_fourcc = coda_format_normalize_yuv(src_fourcc);
202 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
203 if (src_fourcc == dst_fourcc)
204 return NULL;
205
206 for (k = 0; k < num_codecs; k++) {
207 if (codecs[k].src_fourcc == src_fourcc &&
208 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300209 break;
210 }
211
Philipp Zabelb96904e2013-05-23 10:42:58 -0300212 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300213 return NULL;
214
Philipp Zabelb96904e2013-05-23 10:42:58 -0300215 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300216}
217
Philipp Zabel57625592013-09-30 10:34:51 -0300218static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300219 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300220 int *max_w, int *max_h)
221{
Philipp Zabel814c3762014-07-18 07:22:45 -0300222 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300223 int num_codecs = dev->devtype->num_codecs;
224 unsigned int w, h;
225 int k;
226
227 if (codec) {
228 w = codec->max_w;
229 h = codec->max_h;
230 } else {
231 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
232 w = max(w, codecs[k].max_w);
233 h = max(h, codecs[k].max_h);
234 }
235 }
236
237 if (max_w)
238 *max_w = w;
239 if (max_h)
240 *max_h = h;
241}
242
Philipp Zabel79924ca2014-07-23 12:28:45 -0300243const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300244{
245 static char buf[9];
246
247 switch (product) {
248 case CODA_DX6:
249 return "CodaDx6";
250 case CODA_7541:
251 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300252 case CODA_960:
253 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300254 default:
255 snprintf(buf, sizeof(buf), "(0x%04x)", product);
256 return buf;
257 }
258}
259
Javier Martin186b2502012-07-26 05:53:35 -0300260/*
261 * V4L2 ioctl() operations.
262 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300263static int coda_querycap(struct file *file, void *priv,
264 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300265{
Philipp Zabel927933f2013-09-30 10:34:48 -0300266 struct coda_ctx *ctx = fh_to_ctx(priv);
267
Javier Martin186b2502012-07-26 05:53:35 -0300268 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300269 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
270 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300271 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300272 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300273 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
274
275 return 0;
276}
277
Philipp Zabel22e244b2014-07-18 07:22:43 -0300278static int coda_enum_fmt(struct file *file, void *priv,
279 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300280{
281 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300282 const struct coda_codec *codecs = ctx->dev->devtype->codecs;
283 const struct coda_fmt *formats = coda_formats;
284 const struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300285 int num_codecs = ctx->dev->devtype->num_codecs;
286 int num_formats = ARRAY_SIZE(coda_formats);
287 int i, k, num = 0;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300288 bool yuv;
289
290 if (ctx->inst_type == CODA_INST_ENCODER)
291 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
292 else
293 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300294
295 for (i = 0; i < num_formats; i++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300296 /* Skip either raw or compressed formats */
297 if (yuv != coda_format_is_yuv(formats[i].fourcc))
298 continue;
299 /* All uncompressed formats are always supported */
300 if (yuv) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300301 if (num == f->index)
302 break;
303 ++num;
304 continue;
305 }
306 /* Compressed formats may be supported, check the codec list */
307 for (k = 0; k < num_codecs; k++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300308 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
309 formats[i].fourcc == codecs[k].dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300310 break;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300311 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
Philipp Zabelb96904e2013-05-23 10:42:58 -0300312 formats[i].fourcc == codecs[k].src_fourcc)
313 break;
314 }
315 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300316 if (num == f->index)
317 break;
318 ++num;
319 }
320 }
321
322 if (i < num_formats) {
323 fmt = &formats[i];
324 strlcpy(f->description, fmt->name, sizeof(f->description));
325 f->pixelformat = fmt->fourcc;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300326 if (!yuv)
Philipp Zabelbaf70212013-09-30 10:34:46 -0300327 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300328 return 0;
329 }
330
331 /* Format not found */
332 return -EINVAL;
333}
334
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300335static int coda_g_fmt(struct file *file, void *priv,
336 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300337{
Javier Martin186b2502012-07-26 05:53:35 -0300338 struct coda_q_data *q_data;
339 struct coda_ctx *ctx = fh_to_ctx(priv);
340
Javier Martin186b2502012-07-26 05:53:35 -0300341 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300342 if (!q_data)
343 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300344
345 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300346 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300347 f->fmt.pix.width = q_data->width;
348 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300349 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300350
351 f->fmt.pix.sizeimage = q_data->sizeimage;
352 f->fmt.pix.colorspace = ctx->colorspace;
353
354 return 0;
355}
356
Philipp Zabel814c3762014-07-18 07:22:45 -0300357static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300358 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300359{
Philipp Zabel57625592013-09-30 10:34:51 -0300360 struct coda_dev *dev = ctx->dev;
361 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300362 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300363 enum v4l2_field field;
364
365 field = f->fmt.pix.field;
366 if (field == V4L2_FIELD_ANY)
367 field = V4L2_FIELD_NONE;
368 else if (V4L2_FIELD_NONE != field)
369 return -EINVAL;
370
371 /* V4L2 specification suggests the driver corrects the format struct
372 * if any of the dimensions is unsupported */
373 f->fmt.pix.field = field;
374
Philipp Zabel57625592013-09-30 10:34:51 -0300375 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
376 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
377 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
378 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300379
Philipp Zabel57625592013-09-30 10:34:51 -0300380 switch (f->fmt.pix.pixelformat) {
381 case V4L2_PIX_FMT_YUV420:
382 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300383 case V4L2_PIX_FMT_NV12:
Philipp Zabel57625592013-09-30 10:34:51 -0300384 case V4L2_PIX_FMT_H264:
385 case V4L2_PIX_FMT_MPEG4:
386 case V4L2_PIX_FMT_JPEG:
387 break;
388 default:
389 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300390 if (!q_data)
391 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300392 f->fmt.pix.pixelformat = q_data->fourcc;
393 }
394
395 switch (f->fmt.pix.pixelformat) {
396 case V4L2_PIX_FMT_YUV420:
397 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300398 case V4L2_PIX_FMT_NV12:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300399 /* Frame stride must be multiple of 8, but 16 for h.264 */
400 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300401 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300402 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300403 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300404 case V4L2_PIX_FMT_YUV422P:
405 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
406 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
407 f->fmt.pix.height * 2;
408 break;
Philipp Zabel57625592013-09-30 10:34:51 -0300409 case V4L2_PIX_FMT_H264:
410 case V4L2_PIX_FMT_MPEG4:
411 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300412 f->fmt.pix.bytesperline = 0;
413 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300414 break;
415 default:
416 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300417 }
418
419 return 0;
420}
421
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300422static int coda_try_fmt_vid_cap(struct file *file, void *priv,
423 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300424{
Javier Martin186b2502012-07-26 05:53:35 -0300425 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300426 const struct coda_codec *codec = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300427 struct vb2_queue *src_vq;
428 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300429
Philipp Zabel918c66f2013-06-27 06:59:01 -0300430 /*
431 * If the source format is already fixed, try to find a codec that
432 * converts to the given destination format
433 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300434 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300435 if (vb2_is_streaming(src_vq)) {
436 struct coda_q_data *q_data_src;
437
438 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
439 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
440 f->fmt.pix.pixelformat);
441 if (!codec)
442 return -EINVAL;
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300443
444 f->fmt.pix.width = q_data_src->width;
445 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300446 } else {
447 /* Otherwise determine codec by encoded format, if possible */
448 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
449 f->fmt.pix.pixelformat);
450 }
Javier Martin186b2502012-07-26 05:53:35 -0300451
452 f->fmt.pix.colorspace = ctx->colorspace;
453
Philipp Zabel57625592013-09-30 10:34:51 -0300454 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300455 if (ret < 0)
456 return ret;
457
458 /* The h.264 decoder only returns complete 16x16 macroblocks */
459 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300460 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300461 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300462 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300463 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
464 f->fmt.pix.height * 3 / 2;
465 }
466
467 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300468}
469
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300470static int coda_try_fmt_vid_out(struct file *file, void *priv,
471 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300472{
473 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelfcf59762014-08-05 14:00:12 -0300474 const struct coda_codec *codec = NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300475
Philipp Zabelb96904e2013-05-23 10:42:58 -0300476 /* Determine codec by encoded format, returns NULL if raw or invalid */
Philipp Zabelfcf59762014-08-05 14:00:12 -0300477 if (ctx->inst_type == CODA_INST_DECODER) {
478 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300479 V4L2_PIX_FMT_YUV420);
480 if (!codec)
Philipp Zabelfcf59762014-08-05 14:00:12 -0300481 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_H264,
482 V4L2_PIX_FMT_YUV420);
483 if (!codec)
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300484 return -EINVAL;
485 }
Javier Martin186b2502012-07-26 05:53:35 -0300486
487 if (!f->fmt.pix.colorspace)
488 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
489
Philipp Zabel57625592013-09-30 10:34:51 -0300490 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300491}
492
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300493static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300494{
495 struct coda_q_data *q_data;
496 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300497
Philipp Zabel14604e32014-07-11 06:36:22 -0300498 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300499 if (!vq)
500 return -EINVAL;
501
502 q_data = get_q_data(ctx, f->type);
503 if (!q_data)
504 return -EINVAL;
505
506 if (vb2_is_busy(vq)) {
507 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
508 return -EBUSY;
509 }
510
Philipp Zabelb96904e2013-05-23 10:42:58 -0300511 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300512 q_data->width = f->fmt.pix.width;
513 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300514 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300515 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300516 q_data->rect.left = 0;
517 q_data->rect.top = 0;
518 q_data->rect.width = f->fmt.pix.width;
519 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300520
521 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
522 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300523 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300524
525 return 0;
526}
527
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300528static int coda_s_fmt_vid_cap(struct file *file, void *priv,
529 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300530{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300531 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300532 int ret;
533
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300534 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300535 if (ret)
536 return ret;
537
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300538 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300539}
540
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300541static int coda_s_fmt_vid_out(struct file *file, void *priv,
542 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300543{
544 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300545 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300546 int ret;
547
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300548 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300549 if (ret)
550 return ret;
551
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300552 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300553 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300554 return ret;
555
556 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300557
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300558 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
559 coda_g_fmt(file, priv, &f_cap);
560 f_cap.fmt.pix.width = f->fmt.pix.width;
561 f_cap.fmt.pix.height = f->fmt.pix.height;
562
563 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
564 if (ret)
565 return ret;
566
567 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300568}
569
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300570static int coda_qbuf(struct file *file, void *priv,
571 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300572{
573 struct coda_ctx *ctx = fh_to_ctx(priv);
574
Philipp Zabel14604e32014-07-11 06:36:22 -0300575 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300576}
577
Philipp Zabel918c66f2013-06-27 06:59:01 -0300578static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
579 struct v4l2_buffer *buf)
580{
581 struct vb2_queue *src_vq;
582
Philipp Zabel14604e32014-07-11 06:36:22 -0300583 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300584
585 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
586 (buf->sequence == (ctx->qsequence - 1)));
587}
588
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300589static int coda_dqbuf(struct file *file, void *priv,
590 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300591{
592 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300593 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300594
Philipp Zabel14604e32014-07-11 06:36:22 -0300595 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300596
597 /* If this is the last capture buffer, emit an end-of-stream event */
598 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
599 coda_buf_is_end_of_stream(ctx, buf)) {
600 const struct v4l2_event eos_event = {
601 .type = V4L2_EVENT_EOS
602 };
603
604 v4l2_event_queue_fh(&ctx->fh, &eos_event);
605 }
606
607 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300608}
609
Philipp Zabel52c41672014-07-11 06:36:19 -0300610static int coda_g_selection(struct file *file, void *fh,
611 struct v4l2_selection *s)
612{
613 struct coda_ctx *ctx = fh_to_ctx(fh);
614 struct coda_q_data *q_data;
615 struct v4l2_rect r, *rsel;
616
617 q_data = get_q_data(ctx, s->type);
618 if (!q_data)
619 return -EINVAL;
620
621 r.left = 0;
622 r.top = 0;
623 r.width = q_data->width;
624 r.height = q_data->height;
625 rsel = &q_data->rect;
626
627 switch (s->target) {
628 case V4L2_SEL_TGT_CROP_DEFAULT:
629 case V4L2_SEL_TGT_CROP_BOUNDS:
630 rsel = &r;
631 /* fallthrough */
632 case V4L2_SEL_TGT_CROP:
633 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
634 return -EINVAL;
635 break;
636 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
637 case V4L2_SEL_TGT_COMPOSE_PADDED:
638 rsel = &r;
639 /* fallthrough */
640 case V4L2_SEL_TGT_COMPOSE:
641 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
642 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
643 return -EINVAL;
644 break;
645 default:
646 return -EINVAL;
647 }
648
649 s->r = *rsel;
650
651 return 0;
652}
653
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300654static int coda_try_decoder_cmd(struct file *file, void *fh,
655 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300656{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300657 if (dc->cmd != V4L2_DEC_CMD_STOP)
658 return -EINVAL;
659
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300660 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300661 return -EINVAL;
662
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300663 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300664 return -EINVAL;
665
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300666 return 0;
667}
668
669static int coda_decoder_cmd(struct file *file, void *fh,
670 struct v4l2_decoder_cmd *dc)
671{
672 struct coda_ctx *ctx = fh_to_ctx(fh);
673 int ret;
674
675 ret = coda_try_decoder_cmd(file, fh, dc);
676 if (ret < 0)
677 return ret;
678
679 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300680 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300681 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300682
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300683 /* Set the stream-end flag on this context */
684 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300685 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300686 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300687
Philipp Zabel918c66f2013-06-27 06:59:01 -0300688 return 0;
689}
690
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300691static int coda_subscribe_event(struct v4l2_fh *fh,
692 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300693{
694 switch (sub->type) {
695 case V4L2_EVENT_EOS:
696 return v4l2_event_subscribe(fh, sub, 0, NULL);
697 default:
698 return v4l2_ctrl_subscribe_event(fh, sub);
699 }
Javier Martin186b2502012-07-26 05:53:35 -0300700}
701
702static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300703 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300704
Philipp Zabel22e244b2014-07-18 07:22:43 -0300705 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300706 .vidioc_g_fmt_vid_cap = coda_g_fmt,
707 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
708 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300709
Philipp Zabel22e244b2014-07-18 07:22:43 -0300710 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300711 .vidioc_g_fmt_vid_out = coda_g_fmt,
712 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
713 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300714
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300715 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
716 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300717
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300718 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300719 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300720 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300721 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300722
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300723 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
724 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300725
Philipp Zabel52c41672014-07-11 06:36:19 -0300726 .vidioc_g_selection = coda_g_selection,
727
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300728 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300729 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300730
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300731 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300732 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300733};
734
Philipp Zabel79924ca2014-07-23 12:28:45 -0300735void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300736{
737 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
738 struct coda_dev *dev = ctx->dev;
739 int i;
740
741 for (i = 0; i < 16; i++)
742 coda_write(dev, tiled_map->xy2ca_map[i],
743 CODA9_GDI_XY2_CAS_0 + 4 * i);
744 for (i = 0; i < 4; i++)
745 coda_write(dev, tiled_map->xy2ba_map[i],
746 CODA9_GDI_XY2_BA_0 + 4 * i);
747 for (i = 0; i < 16; i++)
748 coda_write(dev, tiled_map->xy2ra_map[i],
749 CODA9_GDI_XY2_RAS_0 + 4 * i);
750 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
751 for (i = 0; i < 32; i++)
752 coda_write(dev, tiled_map->rbc2axi_map[i],
753 CODA9_GDI_RBC2_AXI_0 + 4 * i);
754}
755
Javier Martin186b2502012-07-26 05:53:35 -0300756/*
757 * Mem-to-mem operations.
758 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300759
760static void coda_device_run(void *m2m_priv)
761{
762 struct coda_ctx *ctx = m2m_priv;
763 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300764
765 queue_work(dev->workqueue, &ctx->pic_run_work);
766}
767
Philipp Zabel32b6f202014-07-11 06:36:20 -0300768static void coda_pic_run_work(struct work_struct *work)
769{
770 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
771 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300772 int ret;
773
774 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300775 mutex_lock(&dev->coda_mutex);
776
Philipp Zabela1192a12014-07-23 12:28:40 -0300777 ret = ctx->ops->prepare_run(ctx);
778 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
779 mutex_unlock(&dev->coda_mutex);
780 mutex_unlock(&ctx->buffer_mutex);
781 /* job_finish scheduled by prepare_decode */
782 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300783 }
784
Philipp Zabelf23797b2014-08-06 08:02:23 -0300785 if (!wait_for_completion_timeout(&ctx->completion,
786 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300787 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300788
789 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300790
791 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300792 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300793 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300794 }
795
796 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
797 queue_work(dev->workqueue, &ctx->seq_end_work);
798
799 mutex_unlock(&dev->coda_mutex);
800 mutex_unlock(&ctx->buffer_mutex);
801
Philipp Zabel14604e32014-07-11 06:36:22 -0300802 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300803}
804
805static int coda_job_ready(void *m2m_priv)
806{
807 struct coda_ctx *ctx = m2m_priv;
808
809 /*
810 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300811 * and 1 frame are needed. In the decoder case,
812 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300813 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300814 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300815 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300816 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
817 "not ready: not enough video buffers.\n");
818 return 0;
819 }
820
Philipp Zabel14604e32014-07-11 06:36:22 -0300821 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300822 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
823 "not ready: not enough video capture buffers.\n");
824 return 0;
825 }
826
Philipp Zabel84e23652014-07-11 06:36:34 -0300827 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -0300828 ((ctx->inst_type == CODA_INST_DECODER) &&
829 (coda_get_bitstream_payload(ctx) < 512) &&
830 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
831 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
832 "%d: not ready: not enough bitstream data.\n",
833 ctx->idx);
834 return 0;
835 }
836
Philipp Zabel3e748262013-05-23 10:43:01 -0300837 if (ctx->aborting) {
838 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
839 "not ready: aborting\n");
840 return 0;
841 }
842
Javier Martin186b2502012-07-26 05:53:35 -0300843 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
844 "job ready\n");
845 return 1;
846}
847
848static void coda_job_abort(void *priv)
849{
850 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -0300851
852 ctx->aborting = 1;
853
854 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
855 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -0300856}
857
858static void coda_lock(void *m2m_priv)
859{
860 struct coda_ctx *ctx = m2m_priv;
861 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300862
Javier Martin186b2502012-07-26 05:53:35 -0300863 mutex_lock(&pcdev->dev_mutex);
864}
865
866static void coda_unlock(void *m2m_priv)
867{
868 struct coda_ctx *ctx = m2m_priv;
869 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300870
Javier Martin186b2502012-07-26 05:53:35 -0300871 mutex_unlock(&pcdev->dev_mutex);
872}
873
Philipp Zabel814c3762014-07-18 07:22:45 -0300874static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -0300875 .device_run = coda_device_run,
876 .job_ready = coda_job_ready,
877 .job_abort = coda_job_abort,
878 .lock = coda_lock,
879 .unlock = coda_unlock,
880};
881
Philipp Zabel89548442014-07-11 06:36:17 -0300882static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
883{
884 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
885 int luma_map, chro_map, i;
886
887 memset(tiled_map, 0, sizeof(*tiled_map));
888
889 luma_map = 64;
890 chro_map = 64;
891 tiled_map->map_type = tiled_map_type;
892 for (i = 0; i < 16; i++)
893 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
894 for (i = 0; i < 4; i++)
895 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
896 for (i = 0; i < 16; i++)
897 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
898
899 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
900 tiled_map->xy2rbc_config = 0;
901 } else {
902 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
903 tiled_map_type);
904 return;
905 }
906}
907
Javier Martin186b2502012-07-26 05:53:35 -0300908static void set_default_params(struct coda_ctx *ctx)
909{
Philipp Zabel121cacf2014-07-18 07:22:42 -0300910 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300911 int max_w;
912 int max_h;
913
Philipp Zabel121cacf2014-07-18 07:22:42 -0300914 if (ctx->inst_type == CODA_INST_ENCODER) {
915 src_fourcc = V4L2_PIX_FMT_YUV420;
916 dst_fourcc = V4L2_PIX_FMT_H264;
917 } else {
918 src_fourcc = V4L2_PIX_FMT_H264;
919 dst_fourcc = V4L2_PIX_FMT_YUV420;
920 }
921 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300922 max_w = ctx->codec->max_w;
923 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300924
Philipp Zabel121cacf2014-07-18 07:22:42 -0300925 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -0300926 ctx->colorspace = V4L2_COLORSPACE_REC709;
927 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300928
929 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300930 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
931 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
932 ctx->q_data[V4L2_M2M_SRC].width = max_w;
933 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300934 ctx->q_data[V4L2_M2M_DST].width = max_w;
935 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -0300936 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
937 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
938 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
939 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
940 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
941 } else {
942 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
943 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
944 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
945 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
946 }
Philipp Zabel52c41672014-07-11 06:36:19 -0300947 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
948 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
949 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
950 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -0300951
952 if (ctx->dev->devtype->product == CODA_960)
953 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -0300954}
955
956/*
957 * Queue operations
958 */
959static int coda_queue_setup(struct vb2_queue *vq,
960 const struct v4l2_format *fmt,
961 unsigned int *nbuffers, unsigned int *nplanes,
962 unsigned int sizes[], void *alloc_ctxs[])
963{
964 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300965 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300966 unsigned int size;
967
Philipp Zabele34db062012-08-29 08:22:00 -0300968 q_data = get_q_data(ctx, vq->type);
969 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300970
971 *nplanes = 1;
972 sizes[0] = size;
973
974 alloc_ctxs[0] = ctx->dev->alloc_ctx;
975
976 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
977 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
978
979 return 0;
980}
981
982static int coda_buf_prepare(struct vb2_buffer *vb)
983{
984 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
985 struct coda_q_data *q_data;
986
987 q_data = get_q_data(ctx, vb->vb2_queue->type);
988
989 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
990 v4l2_warn(&ctx->dev->v4l2_dev,
991 "%s data will not fit into plane (%lu < %lu)\n",
992 __func__, vb2_plane_size(vb, 0),
993 (long)q_data->sizeimage);
994 return -EINVAL;
995 }
996
Javier Martin186b2502012-07-26 05:53:35 -0300997 return 0;
998}
999
1000static void coda_buf_queue(struct vb2_buffer *vb)
1001{
1002 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001003 struct coda_q_data *q_data;
1004
1005 q_data = get_q_data(ctx, vb->vb2_queue->type);
1006
1007 /*
1008 * In the decoder case, immediately try to copy the buffer into the
1009 * bitstream ringbuffer and mark it as ready to be dequeued.
1010 */
1011 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
1012 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1013 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001014 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001015 * the stream end
1016 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001017 if (vb2_get_plane_payload(vb, 0) == 0)
1018 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001019 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001020 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001021 if (vb2_is_streaming(vb->vb2_queue))
1022 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001023 mutex_unlock(&ctx->bitstream_mutex);
1024 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001025 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001026 }
Javier Martin186b2502012-07-26 05:53:35 -03001027}
1028
Philipp Zabel79924ca2014-07-23 12:28:45 -03001029int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1030 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001031{
1032 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1033 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001034 if (!buf->vaddr) {
1035 v4l2_err(&dev->v4l2_dev,
1036 "Failed to allocate %s buffer of size %u\n",
1037 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001038 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001039 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001040
1041 buf->size = size;
1042
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001043 if (name && parent) {
1044 buf->blob.data = buf->vaddr;
1045 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001046 buf->dentry = debugfs_create_blob(name, 0644, parent,
1047 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001048 if (!buf->dentry)
1049 dev_warn(&dev->plat_dev->dev,
1050 "failed to create debugfs entry %s\n", name);
1051 }
1052
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001053 return 0;
1054}
1055
Philipp Zabel79924ca2014-07-23 12:28:45 -03001056void coda_free_aux_buf(struct coda_dev *dev,
1057 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001058{
1059 if (buf->vaddr) {
1060 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1061 buf->vaddr, buf->paddr);
1062 buf->vaddr = NULL;
1063 buf->size = 0;
1064 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001065 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001066}
1067
Javier Martin186b2502012-07-26 05:53:35 -03001068static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1069{
1070 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1071 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001072 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001073 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001074 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001075 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001076
Philipp Zabelb96904e2013-05-23 10:42:58 -03001077 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001078 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1079 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03001080 /* copy the buffers that where queued before streamon */
1081 mutex_lock(&ctx->bitstream_mutex);
1082 coda_fill_bitstream(ctx);
1083 mutex_unlock(&ctx->bitstream_mutex);
1084
Philipp Zabelb9063522014-08-05 14:00:10 -03001085 if (coda_get_bitstream_payload(ctx) < 512) {
1086 ret = -EINVAL;
1087 goto err;
1088 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001089 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001090 if (count < 1) {
1091 ret = -EINVAL;
1092 goto err;
1093 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001094 }
1095
1096 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001097 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001098 if (count < 1) {
1099 ret = -EINVAL;
1100 goto err;
1101 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001102
1103 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001104 }
Javier Martin186b2502012-07-26 05:53:35 -03001105
Philipp Zabelb96904e2013-05-23 10:42:58 -03001106 /* Don't start the coda unless both queues are on */
1107 if (!(ctx->streamon_out & ctx->streamon_cap))
1108 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001109
Philipp Zabeleb107512013-09-30 10:34:45 -03001110 /* Allow decoder device_run with no new buffers queued */
1111 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03001112 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001113
Philipp Zabelb96904e2013-05-23 10:42:58 -03001114 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001115 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001116 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001117
Philipp Zabelb96904e2013-05-23 10:42:58 -03001118 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1119 q_data_dst->fourcc);
1120 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001121 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001122 ret = -EINVAL;
1123 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001124 }
1125
Philipp Zabela1192a12014-07-23 12:28:40 -03001126 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001127 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001128 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001129 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001130 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001131 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001132 }
1133
Philipp Zabel89548442014-07-11 06:36:17 -03001134 ctx->initialized = 1;
1135 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001136
1137err:
1138 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1139 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1140 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1141 } else {
1142 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1143 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1144 }
1145 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001146}
1147
Hans Verkuile37559b2014-04-17 02:47:21 -03001148static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001149{
1150 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001151 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001152 struct vb2_buffer *buf;
Javier Martin186b2502012-07-26 05:53:35 -03001153
1154 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001155 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001156 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001157 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001158
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001159 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001160
Philipp Zabel918c66f2013-06-27 06:59:01 -03001161 ctx->isequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001162
1163 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1164 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001165 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001166 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001167 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001168 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001169
1170 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001171 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001172
1173 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1174 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001175 }
1176
Philipp Zabel918c66f2013-06-27 06:59:01 -03001177 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001178 struct coda_timestamp *ts;
1179
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001180 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001181 while (!list_empty(&ctx->timestamp_list)) {
1182 ts = list_first_entry(&ctx->timestamp_list,
1183 struct coda_timestamp, list);
1184 list_del(&ts->list);
1185 kfree(ts);
1186 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001187 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001188 kfifo_init(&ctx->bitstream_fifo,
1189 ctx->bitstream.vaddr, ctx->bitstream.size);
1190 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001191 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001192 }
Javier Martin186b2502012-07-26 05:53:35 -03001193}
1194
Philipp Zabel121cacf2014-07-18 07:22:42 -03001195static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001196 .queue_setup = coda_queue_setup,
1197 .buf_prepare = coda_buf_prepare,
1198 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001199 .start_streaming = coda_start_streaming,
1200 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001201 .wait_prepare = vb2_ops_wait_prepare,
1202 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001203};
1204
1205static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1206{
1207 struct coda_ctx *ctx =
1208 container_of(ctrl->handler, struct coda_ctx, ctrls);
1209
1210 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1211 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1212
1213 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001214 case V4L2_CID_HFLIP:
1215 if (ctrl->val)
1216 ctx->params.rot_mode |= CODA_MIR_HOR;
1217 else
1218 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1219 break;
1220 case V4L2_CID_VFLIP:
1221 if (ctrl->val)
1222 ctx->params.rot_mode |= CODA_MIR_VER;
1223 else
1224 ctx->params.rot_mode &= ~CODA_MIR_VER;
1225 break;
Javier Martin186b2502012-07-26 05:53:35 -03001226 case V4L2_CID_MPEG_VIDEO_BITRATE:
1227 ctx->params.bitrate = ctrl->val / 1000;
1228 break;
1229 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1230 ctx->params.gop_size = ctrl->val;
1231 break;
1232 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1233 ctx->params.h264_intra_qp = ctrl->val;
1234 break;
1235 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1236 ctx->params.h264_inter_qp = ctrl->val;
1237 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001238 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1239 ctx->params.h264_min_qp = ctrl->val;
1240 break;
1241 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1242 ctx->params.h264_max_qp = ctrl->val;
1243 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001244 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1245 ctx->params.h264_deblk_alpha = ctrl->val;
1246 break;
1247 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1248 ctx->params.h264_deblk_beta = ctrl->val;
1249 break;
1250 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1251 ctx->params.h264_deblk_enabled = (ctrl->val ==
1252 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1253 break;
Javier Martin186b2502012-07-26 05:53:35 -03001254 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1255 ctx->params.mpeg4_intra_qp = ctrl->val;
1256 break;
1257 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1258 ctx->params.mpeg4_inter_qp = ctrl->val;
1259 break;
1260 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1261 ctx->params.slice_mode = ctrl->val;
1262 break;
1263 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1264 ctx->params.slice_max_mb = ctrl->val;
1265 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001266 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1267 ctx->params.slice_max_bits = ctrl->val * 8;
1268 break;
Javier Martin186b2502012-07-26 05:53:35 -03001269 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1270 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001271 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1272 ctx->params.intra_refresh = ctrl->val;
1273 break;
Javier Martin186b2502012-07-26 05:53:35 -03001274 default:
1275 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1276 "Invalid control, id=%d, val=%d\n",
1277 ctrl->id, ctrl->val);
1278 return -EINVAL;
1279 }
1280
1281 return 0;
1282}
1283
Philipp Zabel814c3762014-07-18 07:22:45 -03001284static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001285 .s_ctrl = coda_s_ctrl,
1286};
1287
1288static int coda_ctrls_setup(struct coda_ctx *ctx)
1289{
1290 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1291
1292 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001293 V4L2_CID_HFLIP, 0, 1, 1, 0);
1294 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1295 V4L2_CID_VFLIP, 0, 1, 1, 0);
1296 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001297 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1298 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1299 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1300 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001301 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001302 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001303 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001304 if (ctx->dev->devtype->product != CODA_960) {
1305 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1306 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1307 }
1308 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1309 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001310 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001311 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1312 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1313 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1314 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1315 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1316 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1317 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1318 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001319 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1320 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1321 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1322 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1323 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001324 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1325 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001326 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1327 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001328 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001329 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1330 500);
Javier Martin186b2502012-07-26 05:53:35 -03001331 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1332 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1333 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1334 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1335 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001336 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001337 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1338 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001339
1340 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001341 v4l2_err(&ctx->dev->v4l2_dev,
1342 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001343 ctx->ctrls.error);
1344 return -EINVAL;
1345 }
1346
1347 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1348}
1349
Philipp Zabel121cacf2014-07-18 07:22:42 -03001350static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001351{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001352 vq->drv_priv = ctx;
1353 vq->ops = &coda_qops;
1354 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1355 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1356 vq->lock = &ctx->dev->dev_mutex;
1357
1358 return vb2_queue_init(vq);
1359}
1360
Philipp Zabel79924ca2014-07-23 12:28:45 -03001361int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1362 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001363{
Javier Martin186b2502012-07-26 05:53:35 -03001364 int ret;
1365
Javier Martin186b2502012-07-26 05:53:35 -03001366 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001367 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001368 src_vq->mem_ops = &vb2_dma_contig_memops;
1369
Philipp Zabel121cacf2014-07-18 07:22:42 -03001370 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001371 if (ret)
1372 return ret;
1373
Javier Martin186b2502012-07-26 05:53:35 -03001374 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001375 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001376 dst_vq->mem_ops = &vb2_dma_contig_memops;
1377
Philipp Zabel121cacf2014-07-18 07:22:42 -03001378 return coda_queue_init(priv, dst_vq);
1379}
1380
Philipp Zabel79924ca2014-07-23 12:28:45 -03001381int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1382 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001383{
1384 int ret;
1385
1386 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1387 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1388 src_vq->mem_ops = &vb2_dma_contig_memops;
1389
1390 ret = coda_queue_init(priv, src_vq);
1391 if (ret)
1392 return ret;
1393
1394 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1395 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1396 dst_vq->mem_ops = &vb2_dma_contig_memops;
1397
1398 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001399}
1400
Philipp Zabele11f3e62012-07-25 09:16:58 -03001401static int coda_next_free_instance(struct coda_dev *dev)
1402{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001403 int idx = ffz(dev->instance_mask);
1404
1405 if ((idx < 0) ||
1406 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1407 return -EBUSY;
1408
1409 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001410}
1411
Philipp Zabela1192a12014-07-23 12:28:40 -03001412static int coda_open(struct file *file, enum coda_inst_type inst_type,
1413 const struct coda_context_ops *ctx_ops)
Javier Martin186b2502012-07-26 05:53:35 -03001414{
1415 struct coda_dev *dev = video_drvdata(file);
1416 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001417 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001418 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001419 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001420
Philipp Zabelf23797b2014-08-06 08:02:23 -03001421 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001422 if (!ctx)
1423 return -ENOMEM;
1424
Fabio Estevamf82bc202013-08-21 11:14:16 -03001425 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001426 if (idx < 0) {
1427 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001428 goto err_coda_max;
1429 }
1430 set_bit(idx, &dev->instance_mask);
1431
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001432 name = kasprintf(GFP_KERNEL, "context%d", idx);
1433 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1434 kfree(name);
1435
Philipp Zabel121cacf2014-07-18 07:22:42 -03001436 ctx->inst_type = inst_type;
Philipp Zabela1192a12014-07-23 12:28:40 -03001437 ctx->ops = ctx_ops;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001438 init_completion(&ctx->completion);
1439 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabela1192a12014-07-23 12:28:40 -03001440 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001441 v4l2_fh_init(&ctx->fh, video_devdata(file));
1442 file->private_data = &ctx->fh;
1443 v4l2_fh_add(&ctx->fh);
1444 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001445 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001446 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001447 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001448 ctx->frame_mem_ctrl = 1 << 12;
1449 /* fallthrough */
1450 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001451 ctx->reg_idx = 0;
1452 break;
1453 default:
1454 ctx->reg_idx = idx;
1455 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001456
Philipp Zabel1e172732014-07-11 06:36:23 -03001457 /* Power up and upload firmware if necessary */
1458 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1459 if (ret < 0) {
1460 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1461 goto err_pm_get;
1462 }
1463
Fabio Estevam79830db2013-08-21 11:14:17 -03001464 ret = clk_prepare_enable(dev->clk_per);
1465 if (ret)
1466 goto err_clk_per;
1467
1468 ret = clk_prepare_enable(dev->clk_ahb);
1469 if (ret)
1470 goto err_clk_ahb;
1471
Javier Martin186b2502012-07-26 05:53:35 -03001472 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001473 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1474 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001475 if (IS_ERR(ctx->fh.m2m_ctx)) {
1476 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001477
1478 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1479 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001480 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001481 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001482
Javier Martin186b2502012-07-26 05:53:35 -03001483 ret = coda_ctrls_setup(ctx);
1484 if (ret) {
1485 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001486 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001487 }
1488
1489 ctx->fh.ctrl_handler = &ctx->ctrls;
1490
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001491 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
1492 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001493 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001494 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001495 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03001496 }
1497
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001498 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1499 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1500 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1501 if (!ctx->bitstream.vaddr) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001502 v4l2_err(&dev->v4l2_dev,
1503 "failed to allocate bitstream ringbuffer");
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001504 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001505 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001506 }
1507 kfifo_init(&ctx->bitstream_fifo,
1508 ctx->bitstream.vaddr, ctx->bitstream.size);
1509 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001510 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001511 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001512
Javier Martin186b2502012-07-26 05:53:35 -03001513 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001514 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001515 coda_unlock(ctx);
1516
Javier Martin186b2502012-07-26 05:53:35 -03001517 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1518 ctx->idx, ctx);
1519
1520 return 0;
1521
Fabio Estevamf82bc202013-08-21 11:14:16 -03001522err_dma_writecombine:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001523 if (ctx->dev->devtype->product == CODA_DX6)
1524 coda_free_aux_buf(dev, &ctx->workbuf);
1525 coda_free_aux_buf(dev, &ctx->parabuf);
1526err_dma_alloc:
1527 v4l2_ctrl_handler_free(&ctx->ctrls);
1528err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001529 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001530err_ctx_init:
1531 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001532err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001533 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001534err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001535 pm_runtime_put_sync(&dev->plat_dev->dev);
1536err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001537 v4l2_fh_del(&ctx->fh);
1538 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001539 clear_bit(ctx->idx, &dev->instance_mask);
1540err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001541 kfree(ctx);
1542 return ret;
1543}
1544
Philipp Zabel121cacf2014-07-18 07:22:42 -03001545static int coda_encoder_open(struct file *file)
1546{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001547 return coda_open(file, CODA_INST_ENCODER, &coda_bit_encode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001548}
1549
1550static int coda_decoder_open(struct file *file)
1551{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001552 return coda_open(file, CODA_INST_DECODER, &coda_bit_decode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001553}
1554
Javier Martin186b2502012-07-26 05:53:35 -03001555static int coda_release(struct file *file)
1556{
1557 struct coda_dev *dev = video_drvdata(file);
1558 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1559
1560 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1561 ctx);
1562
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001563 debugfs_remove_recursive(ctx->debugfs_entry);
1564
Philipp Zabel918c66f2013-06-27 06:59:01 -03001565 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001566 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001567
1568 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03001569 if (ctx->initialized) {
1570 queue_work(dev->workqueue, &ctx->seq_end_work);
1571 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001572 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001573
Javier Martin186b2502012-07-26 05:53:35 -03001574 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001575 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001576 coda_unlock(ctx);
1577
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001578 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1579 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001580 if (ctx->dev->devtype->product == CODA_DX6)
1581 coda_free_aux_buf(dev, &ctx->workbuf);
1582
1583 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03001584 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001585 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001586 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001587 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001588 v4l2_fh_del(&ctx->fh);
1589 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001590 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001591 if (ctx->ops->release)
1592 ctx->ops->release(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001593 kfree(ctx);
1594
1595 return 0;
1596}
1597
Philipp Zabel121cacf2014-07-18 07:22:42 -03001598static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001599 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03001600 .open = coda_encoder_open,
1601 .release = coda_release,
1602 .poll = v4l2_m2m_fop_poll,
1603 .unlocked_ioctl = video_ioctl2,
1604 .mmap = v4l2_m2m_fop_mmap,
1605};
1606
1607static const struct v4l2_file_operations coda_decoder_fops = {
1608 .owner = THIS_MODULE,
1609 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03001610 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001611 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001612 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001613 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001614};
1615
Philipp Zabel87048bb2012-07-02 07:03:43 -03001616static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001617{
Javier Martin186b2502012-07-26 05:53:35 -03001618 u32 data;
1619 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001620 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001621
Fabio Estevam79830db2013-08-21 11:14:17 -03001622 ret = clk_prepare_enable(dev->clk_per);
1623 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001624 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001625
1626 ret = clk_prepare_enable(dev->clk_ahb);
1627 if (ret)
1628 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001629
Philipp Zabel8f452842014-07-11 06:36:35 -03001630 if (dev->rstc)
1631 reset_control_reset(dev->rstc);
1632
Javier Martin186b2502012-07-26 05:53:35 -03001633 /*
1634 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001635 * The 16-bit chars in the code buffer are in memory access
1636 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001637 * Data in this SRAM survives a reboot.
1638 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001639 p = (u16 *)dev->codebuf.vaddr;
1640 if (dev->devtype->product == CODA_DX6) {
1641 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1642 data = CODA_DOWN_ADDRESS_SET(i) |
1643 CODA_DOWN_DATA_SET(p[i ^ 1]);
1644 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1645 }
1646 } else {
1647 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1648 data = CODA_DOWN_ADDRESS_SET(i) |
1649 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1650 3 - (i % 4)]);
1651 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1652 }
Javier Martin186b2502012-07-26 05:53:35 -03001653 }
Javier Martin186b2502012-07-26 05:53:35 -03001654
Philipp Zabel9acf7692013-05-23 10:42:56 -03001655 /* Clear registers */
1656 for (i = 0; i < 64; i++)
1657 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1658
Javier Martin186b2502012-07-26 05:53:35 -03001659 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001660 if (dev->devtype->product == CODA_960 ||
1661 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001662 coda_write(dev, dev->tempbuf.paddr,
1663 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001664 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001665 } else {
1666 coda_write(dev, dev->workbuf.paddr,
1667 CODA_REG_BIT_WORK_BUF_ADDR);
1668 }
Javier Martin186b2502012-07-26 05:53:35 -03001669 coda_write(dev, dev->codebuf.paddr,
1670 CODA_REG_BIT_CODE_BUF_ADDR);
1671 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1672
1673 /* Set default values */
1674 switch (dev->devtype->product) {
1675 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001676 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1677 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001678 break;
1679 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001680 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1681 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001682 }
Philipp Zabel89548442014-07-11 06:36:17 -03001683 if (dev->devtype->product == CODA_960)
1684 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1685 else
1686 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001687
1688 if (dev->devtype->product != CODA_DX6)
1689 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1690
Javier Martin186b2502012-07-26 05:53:35 -03001691 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1692 CODA_REG_BIT_INT_ENABLE);
1693
1694 /* Reset VPU and start processor */
1695 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1696 data |= CODA_REG_RESET_ENABLE;
1697 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1698 udelay(10);
1699 data &= ~CODA_REG_RESET_ENABLE;
1700 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1701 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1702
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001703 clk_disable_unprepare(dev->clk_ahb);
1704 clk_disable_unprepare(dev->clk_per);
1705
1706 return 0;
1707
1708err_clk_ahb:
1709 clk_disable_unprepare(dev->clk_per);
1710err_clk_per:
1711 return ret;
1712}
1713
Philipp Zabel121cacf2014-07-18 07:22:42 -03001714static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
1715{
1716 vfd->release = video_device_release_empty,
1717 vfd->lock = &dev->dev_mutex;
1718 vfd->v4l2_dev = &dev->v4l2_dev;
1719 vfd->vfl_dir = VFL_DIR_M2M;
1720 video_set_drvdata(vfd, dev);
1721
Philipp Zabela188a662014-08-05 14:00:20 -03001722 /* Not applicable, use the selection API instead */
1723 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1724 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1725 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1726
Philipp Zabel121cacf2014-07-18 07:22:42 -03001727 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1728}
1729
Javier Martin186b2502012-07-26 05:53:35 -03001730static void coda_fw_callback(const struct firmware *fw, void *context)
1731{
1732 struct coda_dev *dev = context;
1733 struct platform_device *pdev = dev->plat_dev;
1734 int ret;
1735
1736 if (!fw) {
1737 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001738 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001739 }
1740
1741 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001742 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1743 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001744 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001745 dev_err(&pdev->dev, "failed to allocate code buffer\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001746 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001747 }
1748
Philipp Zabel87048bb2012-07-02 07:03:43 -03001749 /* Copy the whole firmware image to the code buffer */
1750 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1751 release_firmware(fw);
1752
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001753 ret = coda_hw_init(dev);
1754 if (ret < 0) {
1755 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1756 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001757 }
1758
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001759 ret = coda_check_firmware(dev);
1760 if (ret < 0)
1761 goto put_pm;
1762
Javier Martin186b2502012-07-26 05:53:35 -03001763 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1764 if (IS_ERR(dev->alloc_ctx)) {
1765 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001766 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001767 }
1768
1769 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1770 if (IS_ERR(dev->m2m_dev)) {
1771 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1772 goto rel_ctx;
1773 }
1774
Philipp Zabel121cacf2014-07-18 07:22:42 -03001775 dev->vfd[0].fops = &coda_encoder_fops,
1776 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
1777 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
1778 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03001779 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03001780 v4l2_err(&dev->v4l2_dev,
1781 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03001782 goto rel_m2m;
1783 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03001784
1785 dev->vfd[1].fops = &coda_decoder_fops,
1786 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
1787 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
1788 ret = coda_register_device(dev, &dev->vfd[1]);
1789 if (ret) {
1790 v4l2_err(&dev->v4l2_dev,
1791 "Failed to register decoder video device\n");
1792 goto rel_m2m;
1793 }
1794
1795 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1796 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03001797
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001798 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001799 return;
1800
1801rel_m2m:
1802 v4l2_m2m_release(dev->m2m_dev);
1803rel_ctx:
1804 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001805put_pm:
1806 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001807}
1808
1809static int coda_firmware_request(struct coda_dev *dev)
1810{
1811 char *fw = dev->devtype->firmware;
1812
1813 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1814 coda_product_name(dev->devtype->product));
1815
1816 return request_firmware_nowait(THIS_MODULE, true,
1817 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1818}
1819
1820enum coda_platform {
1821 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001822 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03001823 CODA_IMX6Q,
1824 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03001825};
1826
Emil Goodec06d8752012-08-14 17:44:42 -03001827static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001828 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001829 .firmware = "v4l-codadx6-imx27.bin",
1830 .product = CODA_DX6,
1831 .codecs = codadx6_codecs,
1832 .num_codecs = ARRAY_SIZE(codadx6_codecs),
1833 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001834 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03001835 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001836 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001837 .firmware = "v4l-coda7541-imx53.bin",
1838 .product = CODA_7541,
1839 .codecs = coda7_codecs,
1840 .num_codecs = ARRAY_SIZE(coda7_codecs),
1841 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001842 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001843 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001844 },
Philipp Zabel89548442014-07-11 06:36:17 -03001845 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001846 .firmware = "v4l-coda960-imx6q.bin",
1847 .product = CODA_960,
1848 .codecs = coda9_codecs,
1849 .num_codecs = ARRAY_SIZE(coda9_codecs),
1850 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001851 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001852 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03001853 },
1854 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001855 .firmware = "v4l-coda960-imx6dl.bin",
1856 .product = CODA_960,
1857 .codecs = coda9_codecs,
1858 .num_codecs = ARRAY_SIZE(coda9_codecs),
1859 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001860 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001861 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03001862 },
Javier Martin186b2502012-07-26 05:53:35 -03001863};
1864
1865static struct platform_device_id coda_platform_ids[] = {
1866 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001867 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001868 { /* sentinel */ }
1869};
1870MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1871
1872#ifdef CONFIG_OF
1873static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03001874 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001875 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03001876 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
1877 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03001878 { /* sentinel */ }
1879};
1880MODULE_DEVICE_TABLE(of, coda_dt_ids);
1881#endif
1882
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001883static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001884{
1885 const struct of_device_id *of_id =
1886 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1887 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001888 struct coda_platform_data *pdata = pdev->dev.platform_data;
1889 struct device_node *np = pdev->dev.of_node;
1890 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001891 struct coda_dev *dev;
1892 struct resource *res;
1893 int ret, irq;
1894
Philipp Zabelf23797b2014-08-06 08:02:23 -03001895 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03001896 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03001897 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03001898
1899 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001900 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001901
1902 dev->plat_dev = pdev;
1903 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1904 if (IS_ERR(dev->clk_per)) {
1905 dev_err(&pdev->dev, "Could not get per clock\n");
1906 return PTR_ERR(dev->clk_per);
1907 }
1908
1909 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1910 if (IS_ERR(dev->clk_ahb)) {
1911 dev_err(&pdev->dev, "Could not get ahb clock\n");
1912 return PTR_ERR(dev->clk_ahb);
1913 }
1914
1915 /* Get memory for physical registers */
1916 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03001917 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1918 if (IS_ERR(dev->regs_base))
1919 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03001920
1921 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03001922 irq = platform_get_irq_byname(pdev, "bit");
1923 if (irq < 0)
1924 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001925 if (irq < 0) {
1926 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03001927 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03001928 }
1929
Fabio Estevam08c8d142014-06-04 15:46:24 -03001930 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
1931 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
1932 if (ret < 0) {
1933 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
1934 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001935 }
1936
Philipp Zabelee27aa92014-07-24 16:50:03 -03001937 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03001938 if (IS_ERR(dev->rstc)) {
1939 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03001940 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03001941 dev->rstc = NULL;
1942 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001943 dev_err(&pdev->dev, "failed get reset control: %d\n",
1944 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03001945 return ret;
1946 }
1947 }
1948
Philipp Zabel657eee72013-04-29 16:17:14 -07001949 /* Get IRAM pool from device tree or platform data */
1950 pool = of_get_named_gen_pool(np, "iram", 0);
1951 if (!pool && pdata)
1952 pool = dev_get_gen_pool(pdata->iram_dev);
1953 if (!pool) {
1954 dev_err(&pdev->dev, "iram pool not available\n");
1955 return -ENOMEM;
1956 }
1957 dev->iram_pool = pool;
1958
Javier Martin186b2502012-07-26 05:53:35 -03001959 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1960 if (ret)
1961 return ret;
1962
1963 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001964 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001965
1966 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
1967
1968 if (of_id) {
1969 dev->devtype = of_id->data;
1970 } else if (pdev_id) {
1971 dev->devtype = &coda_devdata[pdev_id->driver_data];
1972 } else {
1973 v4l2_device_unregister(&dev->v4l2_dev);
1974 return -EINVAL;
1975 }
1976
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001977 dev->debugfs_root = debugfs_create_dir("coda", NULL);
1978 if (!dev->debugfs_root)
1979 dev_warn(&pdev->dev, "failed to create debugfs root\n");
1980
Javier Martin186b2502012-07-26 05:53:35 -03001981 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001982 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001983 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001984 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001985 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001986 if (ret < 0) {
1987 dev_err(&pdev->dev, "failed to allocate work buffer\n");
1988 v4l2_device_unregister(&dev->v4l2_dev);
1989 return ret;
1990 }
Javier Martin186b2502012-07-26 05:53:35 -03001991 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001992
1993 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001994 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001995 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001996 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001997 if (ret < 0) {
1998 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
1999 v4l2_device_unregister(&dev->v4l2_dev);
2000 return ret;
2001 }
Javier Martin186b2502012-07-26 05:53:35 -03002002 }
2003
Philipp Zabel401e9722014-07-11 06:36:43 -03002004 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002005 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2006 &dev->iram.paddr);
2007 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002008 dev_warn(&pdev->dev, "unable to alloc iram\n");
2009 } else {
2010 dev->iram.blob.data = dev->iram.vaddr;
2011 dev->iram.blob.size = dev->iram.size;
2012 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2013 dev->debugfs_root,
2014 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002015 }
2016
Philipp Zabel32b6f202014-07-11 06:36:20 -03002017 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2018 if (!dev->workqueue) {
2019 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2020 return -ENOMEM;
2021 }
2022
Javier Martin186b2502012-07-26 05:53:35 -03002023 platform_set_drvdata(pdev, dev);
2024
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002025 /*
2026 * Start activated so we can directly call coda_hw_init in
2027 * coda_fw_callback regardless of whether CONFIG_PM_RUNTIME is
2028 * enabled or whether the device is associated with a PM domain.
2029 */
2030 pm_runtime_get_noresume(&pdev->dev);
2031 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002032 pm_runtime_enable(&pdev->dev);
2033
Javier Martin186b2502012-07-26 05:53:35 -03002034 return coda_firmware_request(dev);
2035}
2036
2037static int coda_remove(struct platform_device *pdev)
2038{
2039 struct coda_dev *dev = platform_get_drvdata(pdev);
2040
Philipp Zabel121cacf2014-07-18 07:22:42 -03002041 video_unregister_device(&dev->vfd[0]);
2042 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03002043 if (dev->m2m_dev)
2044 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002045 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002046 if (dev->alloc_ctx)
2047 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2048 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002049 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002050 if (dev->iram.vaddr)
2051 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2052 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002053 coda_free_aux_buf(dev, &dev->codebuf);
2054 coda_free_aux_buf(dev, &dev->tempbuf);
2055 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002056 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002057 return 0;
2058}
2059
Philipp Zabel1e172732014-07-11 06:36:23 -03002060#ifdef CONFIG_PM_RUNTIME
2061static int coda_runtime_resume(struct device *dev)
2062{
2063 struct coda_dev *cdev = dev_get_drvdata(dev);
2064 int ret = 0;
2065
Philipp Zabel65919e62014-07-18 07:22:36 -03002066 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002067 ret = coda_hw_init(cdev);
2068 if (ret)
2069 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2070 }
2071
2072 return ret;
2073}
2074#endif
2075
2076static const struct dev_pm_ops coda_pm_ops = {
2077 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2078};
2079
Javier Martin186b2502012-07-26 05:53:35 -03002080static struct platform_driver coda_driver = {
2081 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002082 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002083 .driver = {
2084 .name = CODA_NAME,
2085 .owner = THIS_MODULE,
2086 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002087 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002088 },
2089 .id_table = coda_platform_ids,
2090};
2091
2092module_platform_driver(coda_driver);
2093
2094MODULE_LICENSE("GPL");
2095MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2096MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");