blob: 0997b5c677bdd7cfa60f83d8a7af8a2f10162158 [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
Javier Martin186b2502012-07-26 05:53:35 -030085/*
Philipp Zabelb96904e2013-05-23 10:42:58 -030086 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -030087 */
Philipp Zabel814c3762014-07-18 07:22:45 -030088static const struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -030089 {
Philipp Zabelb96904e2013-05-23 10:42:58 -030090 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -030091 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -030092 },
93 {
94 .name = "YUV 4:2:0 Planar, YCrCb",
95 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -030096 },
97 {
98 .name = "H264 Encoded Stream",
99 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300100 },
101 {
102 .name = "MPEG4 Encoded Stream",
103 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300104 },
105};
106
Philipp Zabelb96904e2013-05-23 10:42:58 -0300107#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
108 { mode, src_fourcc, dst_fourcc, max_w, max_h }
109
110/*
111 * Arrays of codecs supported by each given version of Coda:
112 * i.MX27 -> codadx6
113 * i.MX5x -> coda7
114 * i.MX6 -> coda960
115 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
116 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300117static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300118 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
119 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300120};
121
Philipp Zabel814c3762014-07-18 07:22:45 -0300122static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300123 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
124 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300125 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
126 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300127};
128
Philipp Zabel814c3762014-07-18 07:22:45 -0300129static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300130 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
131 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
132 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
133 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300134};
135
Philipp Zabelb96904e2013-05-23 10:42:58 -0300136static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300137{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300138 switch (fourcc) {
139 case V4L2_PIX_FMT_YUV420:
140 case V4L2_PIX_FMT_YVU420:
141 return true;
142 default:
143 return false;
144 }
145}
Javier Martin186b2502012-07-26 05:53:35 -0300146
Philipp Zabelb96904e2013-05-23 10:42:58 -0300147/*
148 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
149 * tables.
150 */
151static u32 coda_format_normalize_yuv(u32 fourcc)
152{
153 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
154}
155
Philipp Zabel814c3762014-07-18 07:22:45 -0300156static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
157 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300158{
Philipp Zabel814c3762014-07-18 07:22:45 -0300159 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300160 int num_codecs = dev->devtype->num_codecs;
161 int k;
162
163 src_fourcc = coda_format_normalize_yuv(src_fourcc);
164 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
165 if (src_fourcc == dst_fourcc)
166 return NULL;
167
168 for (k = 0; k < num_codecs; k++) {
169 if (codecs[k].src_fourcc == src_fourcc &&
170 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300171 break;
172 }
173
Philipp Zabelb96904e2013-05-23 10:42:58 -0300174 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300175 return NULL;
176
Philipp Zabelb96904e2013-05-23 10:42:58 -0300177 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300178}
179
Philipp Zabel57625592013-09-30 10:34:51 -0300180static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300181 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300182 int *max_w, int *max_h)
183{
Philipp Zabel814c3762014-07-18 07:22:45 -0300184 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300185 int num_codecs = dev->devtype->num_codecs;
186 unsigned int w, h;
187 int k;
188
189 if (codec) {
190 w = codec->max_w;
191 h = codec->max_h;
192 } else {
193 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
194 w = max(w, codecs[k].max_w);
195 h = max(h, codecs[k].max_h);
196 }
197 }
198
199 if (max_w)
200 *max_w = w;
201 if (max_h)
202 *max_h = h;
203}
204
Philipp Zabel79924ca2014-07-23 12:28:45 -0300205const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300206{
207 static char buf[9];
208
209 switch (product) {
210 case CODA_DX6:
211 return "CodaDx6";
212 case CODA_7541:
213 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300214 case CODA_960:
215 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300216 default:
217 snprintf(buf, sizeof(buf), "(0x%04x)", product);
218 return buf;
219 }
220}
221
Javier Martin186b2502012-07-26 05:53:35 -0300222/*
223 * V4L2 ioctl() operations.
224 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300225static int coda_querycap(struct file *file, void *priv,
226 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300227{
Philipp Zabel927933f2013-09-30 10:34:48 -0300228 struct coda_ctx *ctx = fh_to_ctx(priv);
229
Javier Martin186b2502012-07-26 05:53:35 -0300230 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300231 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
232 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300233 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300234 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300235 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
236
237 return 0;
238}
239
Philipp Zabel22e244b2014-07-18 07:22:43 -0300240static int coda_enum_fmt(struct file *file, void *priv,
241 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300242{
243 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300244 const struct coda_codec *codecs = ctx->dev->devtype->codecs;
245 const struct coda_fmt *formats = coda_formats;
246 const struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300247 int num_codecs = ctx->dev->devtype->num_codecs;
248 int num_formats = ARRAY_SIZE(coda_formats);
249 int i, k, num = 0;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300250 bool yuv;
251
252 if (ctx->inst_type == CODA_INST_ENCODER)
253 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
254 else
255 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300256
257 for (i = 0; i < num_formats; i++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300258 /* Skip either raw or compressed formats */
259 if (yuv != coda_format_is_yuv(formats[i].fourcc))
260 continue;
261 /* All uncompressed formats are always supported */
262 if (yuv) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300263 if (num == f->index)
264 break;
265 ++num;
266 continue;
267 }
268 /* Compressed formats may be supported, check the codec list */
269 for (k = 0; k < num_codecs; k++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300270 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
271 formats[i].fourcc == codecs[k].dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300272 break;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300273 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
Philipp Zabelb96904e2013-05-23 10:42:58 -0300274 formats[i].fourcc == codecs[k].src_fourcc)
275 break;
276 }
277 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300278 if (num == f->index)
279 break;
280 ++num;
281 }
282 }
283
284 if (i < num_formats) {
285 fmt = &formats[i];
286 strlcpy(f->description, fmt->name, sizeof(f->description));
287 f->pixelformat = fmt->fourcc;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300288 if (!yuv)
Philipp Zabelbaf70212013-09-30 10:34:46 -0300289 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300290 return 0;
291 }
292
293 /* Format not found */
294 return -EINVAL;
295}
296
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300297static int coda_g_fmt(struct file *file, void *priv,
298 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300299{
Javier Martin186b2502012-07-26 05:53:35 -0300300 struct coda_q_data *q_data;
301 struct coda_ctx *ctx = fh_to_ctx(priv);
302
Javier Martin186b2502012-07-26 05:53:35 -0300303 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300304 if (!q_data)
305 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300306
307 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300308 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300309 f->fmt.pix.width = q_data->width;
310 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300311 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300312
313 f->fmt.pix.sizeimage = q_data->sizeimage;
314 f->fmt.pix.colorspace = ctx->colorspace;
315
316 return 0;
317}
318
Philipp Zabel814c3762014-07-18 07:22:45 -0300319static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300320 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300321{
Philipp Zabel57625592013-09-30 10:34:51 -0300322 struct coda_dev *dev = ctx->dev;
323 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300324 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300325 enum v4l2_field field;
326
327 field = f->fmt.pix.field;
328 if (field == V4L2_FIELD_ANY)
329 field = V4L2_FIELD_NONE;
330 else if (V4L2_FIELD_NONE != field)
331 return -EINVAL;
332
333 /* V4L2 specification suggests the driver corrects the format struct
334 * if any of the dimensions is unsupported */
335 f->fmt.pix.field = field;
336
Philipp Zabel57625592013-09-30 10:34:51 -0300337 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
338 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
339 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
340 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300341
Philipp Zabel57625592013-09-30 10:34:51 -0300342 switch (f->fmt.pix.pixelformat) {
343 case V4L2_PIX_FMT_YUV420:
344 case V4L2_PIX_FMT_YVU420:
345 case V4L2_PIX_FMT_H264:
346 case V4L2_PIX_FMT_MPEG4:
347 case V4L2_PIX_FMT_JPEG:
348 break;
349 default:
350 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300351 if (!q_data)
352 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300353 f->fmt.pix.pixelformat = q_data->fourcc;
354 }
355
356 switch (f->fmt.pix.pixelformat) {
357 case V4L2_PIX_FMT_YUV420:
358 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300359 /* Frame stride must be multiple of 8, but 16 for h.264 */
360 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300361 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300362 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300363 break;
364 case V4L2_PIX_FMT_H264:
365 case V4L2_PIX_FMT_MPEG4:
366 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300367 f->fmt.pix.bytesperline = 0;
368 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300369 break;
370 default:
371 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300372 }
373
374 return 0;
375}
376
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300377static int coda_try_fmt_vid_cap(struct file *file, void *priv,
378 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300379{
Javier Martin186b2502012-07-26 05:53:35 -0300380 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300381 const struct coda_codec *codec = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300382 struct vb2_queue *src_vq;
383 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300384
Philipp Zabel918c66f2013-06-27 06:59:01 -0300385 /*
386 * If the source format is already fixed, try to find a codec that
387 * converts to the given destination format
388 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300389 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300390 if (vb2_is_streaming(src_vq)) {
391 struct coda_q_data *q_data_src;
392
393 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
394 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
395 f->fmt.pix.pixelformat);
396 if (!codec)
397 return -EINVAL;
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300398
399 f->fmt.pix.width = q_data_src->width;
400 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300401 } else {
402 /* Otherwise determine codec by encoded format, if possible */
403 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
404 f->fmt.pix.pixelformat);
405 }
Javier Martin186b2502012-07-26 05:53:35 -0300406
407 f->fmt.pix.colorspace = ctx->colorspace;
408
Philipp Zabel57625592013-09-30 10:34:51 -0300409 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300410 if (ret < 0)
411 return ret;
412
413 /* The h.264 decoder only returns complete 16x16 macroblocks */
414 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300415 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300416 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300417 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300418 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
419 f->fmt.pix.height * 3 / 2;
420 }
421
422 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300423}
424
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300425static int coda_try_fmt_vid_out(struct file *file, void *priv,
426 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300427{
428 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelfcf59762014-08-05 14:00:12 -0300429 const struct coda_codec *codec = NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300430
Philipp Zabelb96904e2013-05-23 10:42:58 -0300431 /* Determine codec by encoded format, returns NULL if raw or invalid */
Philipp Zabelfcf59762014-08-05 14:00:12 -0300432 if (ctx->inst_type == CODA_INST_DECODER) {
433 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300434 V4L2_PIX_FMT_YUV420);
435 if (!codec)
Philipp Zabelfcf59762014-08-05 14:00:12 -0300436 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_H264,
437 V4L2_PIX_FMT_YUV420);
438 if (!codec)
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300439 return -EINVAL;
440 }
Javier Martin186b2502012-07-26 05:53:35 -0300441
442 if (!f->fmt.pix.colorspace)
443 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
444
Philipp Zabel57625592013-09-30 10:34:51 -0300445 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300446}
447
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300448static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300449{
450 struct coda_q_data *q_data;
451 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300452
Philipp Zabel14604e32014-07-11 06:36:22 -0300453 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300454 if (!vq)
455 return -EINVAL;
456
457 q_data = get_q_data(ctx, f->type);
458 if (!q_data)
459 return -EINVAL;
460
461 if (vb2_is_busy(vq)) {
462 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
463 return -EBUSY;
464 }
465
Philipp Zabelb96904e2013-05-23 10:42:58 -0300466 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300467 q_data->width = f->fmt.pix.width;
468 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300469 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300470 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300471 q_data->rect.left = 0;
472 q_data->rect.top = 0;
473 q_data->rect.width = f->fmt.pix.width;
474 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300475
476 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
477 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300478 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300479
480 return 0;
481}
482
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300483static int coda_s_fmt_vid_cap(struct file *file, void *priv,
484 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300485{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300486 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300487 int ret;
488
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300489 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300490 if (ret)
491 return ret;
492
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300493 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300494}
495
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300496static int coda_s_fmt_vid_out(struct file *file, void *priv,
497 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300498{
499 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300500 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300501 int ret;
502
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300503 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300504 if (ret)
505 return ret;
506
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300507 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300508 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300509 return ret;
510
511 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300512
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300513 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
514 coda_g_fmt(file, priv, &f_cap);
515 f_cap.fmt.pix.width = f->fmt.pix.width;
516 f_cap.fmt.pix.height = f->fmt.pix.height;
517
518 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
519 if (ret)
520 return ret;
521
522 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300523}
524
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300525static int coda_qbuf(struct file *file, void *priv,
526 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300527{
528 struct coda_ctx *ctx = fh_to_ctx(priv);
529
Philipp Zabel14604e32014-07-11 06:36:22 -0300530 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300531}
532
Philipp Zabel918c66f2013-06-27 06:59:01 -0300533static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
534 struct v4l2_buffer *buf)
535{
536 struct vb2_queue *src_vq;
537
Philipp Zabel14604e32014-07-11 06:36:22 -0300538 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300539
540 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
541 (buf->sequence == (ctx->qsequence - 1)));
542}
543
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300544static int coda_dqbuf(struct file *file, void *priv,
545 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300546{
547 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300548 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300549
Philipp Zabel14604e32014-07-11 06:36:22 -0300550 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300551
552 /* If this is the last capture buffer, emit an end-of-stream event */
553 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
554 coda_buf_is_end_of_stream(ctx, buf)) {
555 const struct v4l2_event eos_event = {
556 .type = V4L2_EVENT_EOS
557 };
558
559 v4l2_event_queue_fh(&ctx->fh, &eos_event);
560 }
561
562 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300563}
564
Philipp Zabel52c41672014-07-11 06:36:19 -0300565static int coda_g_selection(struct file *file, void *fh,
566 struct v4l2_selection *s)
567{
568 struct coda_ctx *ctx = fh_to_ctx(fh);
569 struct coda_q_data *q_data;
570 struct v4l2_rect r, *rsel;
571
572 q_data = get_q_data(ctx, s->type);
573 if (!q_data)
574 return -EINVAL;
575
576 r.left = 0;
577 r.top = 0;
578 r.width = q_data->width;
579 r.height = q_data->height;
580 rsel = &q_data->rect;
581
582 switch (s->target) {
583 case V4L2_SEL_TGT_CROP_DEFAULT:
584 case V4L2_SEL_TGT_CROP_BOUNDS:
585 rsel = &r;
586 /* fallthrough */
587 case V4L2_SEL_TGT_CROP:
588 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
589 return -EINVAL;
590 break;
591 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
592 case V4L2_SEL_TGT_COMPOSE_PADDED:
593 rsel = &r;
594 /* fallthrough */
595 case V4L2_SEL_TGT_COMPOSE:
596 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
597 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
598 return -EINVAL;
599 break;
600 default:
601 return -EINVAL;
602 }
603
604 s->r = *rsel;
605
606 return 0;
607}
608
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300609static int coda_try_decoder_cmd(struct file *file, void *fh,
610 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300611{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300612 if (dc->cmd != V4L2_DEC_CMD_STOP)
613 return -EINVAL;
614
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300615 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300616 return -EINVAL;
617
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300618 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300619 return -EINVAL;
620
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300621 return 0;
622}
623
624static int coda_decoder_cmd(struct file *file, void *fh,
625 struct v4l2_decoder_cmd *dc)
626{
627 struct coda_ctx *ctx = fh_to_ctx(fh);
628 int ret;
629
630 ret = coda_try_decoder_cmd(file, fh, dc);
631 if (ret < 0)
632 return ret;
633
634 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300635 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300636 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300637
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300638 /* Set the stream-end flag on this context */
639 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300640 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300641 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300642
Philipp Zabel918c66f2013-06-27 06:59:01 -0300643 return 0;
644}
645
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300646static int coda_subscribe_event(struct v4l2_fh *fh,
647 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300648{
649 switch (sub->type) {
650 case V4L2_EVENT_EOS:
651 return v4l2_event_subscribe(fh, sub, 0, NULL);
652 default:
653 return v4l2_ctrl_subscribe_event(fh, sub);
654 }
Javier Martin186b2502012-07-26 05:53:35 -0300655}
656
657static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300658 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300659
Philipp Zabel22e244b2014-07-18 07:22:43 -0300660 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300661 .vidioc_g_fmt_vid_cap = coda_g_fmt,
662 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
663 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300664
Philipp Zabel22e244b2014-07-18 07:22:43 -0300665 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300666 .vidioc_g_fmt_vid_out = coda_g_fmt,
667 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
668 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300669
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300670 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
671 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300672
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300673 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300674 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300675 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300676 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300677
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300678 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
679 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300680
Philipp Zabel52c41672014-07-11 06:36:19 -0300681 .vidioc_g_selection = coda_g_selection,
682
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300683 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300684 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300685
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300686 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300687 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300688};
689
Philipp Zabel79924ca2014-07-23 12:28:45 -0300690void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300691{
692 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
693 struct coda_dev *dev = ctx->dev;
694 int i;
695
696 for (i = 0; i < 16; i++)
697 coda_write(dev, tiled_map->xy2ca_map[i],
698 CODA9_GDI_XY2_CAS_0 + 4 * i);
699 for (i = 0; i < 4; i++)
700 coda_write(dev, tiled_map->xy2ba_map[i],
701 CODA9_GDI_XY2_BA_0 + 4 * i);
702 for (i = 0; i < 16; i++)
703 coda_write(dev, tiled_map->xy2ra_map[i],
704 CODA9_GDI_XY2_RAS_0 + 4 * i);
705 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
706 for (i = 0; i < 32; i++)
707 coda_write(dev, tiled_map->rbc2axi_map[i],
708 CODA9_GDI_RBC2_AXI_0 + 4 * i);
709}
710
Javier Martin186b2502012-07-26 05:53:35 -0300711/*
712 * Mem-to-mem operations.
713 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300714
715static void coda_device_run(void *m2m_priv)
716{
717 struct coda_ctx *ctx = m2m_priv;
718 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300719
720 queue_work(dev->workqueue, &ctx->pic_run_work);
721}
722
Philipp Zabel32b6f202014-07-11 06:36:20 -0300723static void coda_pic_run_work(struct work_struct *work)
724{
725 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
726 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300727 int ret;
728
729 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300730 mutex_lock(&dev->coda_mutex);
731
Philipp Zabela1192a12014-07-23 12:28:40 -0300732 ret = ctx->ops->prepare_run(ctx);
733 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
734 mutex_unlock(&dev->coda_mutex);
735 mutex_unlock(&ctx->buffer_mutex);
736 /* job_finish scheduled by prepare_decode */
737 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300738 }
739
Philipp Zabelf23797b2014-08-06 08:02:23 -0300740 if (!wait_for_completion_timeout(&ctx->completion,
741 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300742 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300743
744 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300745
746 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300747 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300748 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300749 }
750
751 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
752 queue_work(dev->workqueue, &ctx->seq_end_work);
753
754 mutex_unlock(&dev->coda_mutex);
755 mutex_unlock(&ctx->buffer_mutex);
756
Philipp Zabel14604e32014-07-11 06:36:22 -0300757 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300758}
759
760static int coda_job_ready(void *m2m_priv)
761{
762 struct coda_ctx *ctx = m2m_priv;
763
764 /*
765 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300766 * and 1 frame are needed. In the decoder case,
767 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300768 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300769 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300770 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300771 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
772 "not ready: not enough video buffers.\n");
773 return 0;
774 }
775
Philipp Zabel14604e32014-07-11 06:36:22 -0300776 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300777 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
778 "not ready: not enough video capture buffers.\n");
779 return 0;
780 }
781
Philipp Zabel84e23652014-07-11 06:36:34 -0300782 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -0300783 ((ctx->inst_type == CODA_INST_DECODER) &&
784 (coda_get_bitstream_payload(ctx) < 512) &&
785 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
786 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
787 "%d: not ready: not enough bitstream data.\n",
788 ctx->idx);
789 return 0;
790 }
791
Philipp Zabel3e748262013-05-23 10:43:01 -0300792 if (ctx->aborting) {
793 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
794 "not ready: aborting\n");
795 return 0;
796 }
797
Javier Martin186b2502012-07-26 05:53:35 -0300798 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
799 "job ready\n");
800 return 1;
801}
802
803static void coda_job_abort(void *priv)
804{
805 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -0300806
807 ctx->aborting = 1;
808
809 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
810 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -0300811}
812
813static void coda_lock(void *m2m_priv)
814{
815 struct coda_ctx *ctx = m2m_priv;
816 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300817
Javier Martin186b2502012-07-26 05:53:35 -0300818 mutex_lock(&pcdev->dev_mutex);
819}
820
821static void coda_unlock(void *m2m_priv)
822{
823 struct coda_ctx *ctx = m2m_priv;
824 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300825
Javier Martin186b2502012-07-26 05:53:35 -0300826 mutex_unlock(&pcdev->dev_mutex);
827}
828
Philipp Zabel814c3762014-07-18 07:22:45 -0300829static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -0300830 .device_run = coda_device_run,
831 .job_ready = coda_job_ready,
832 .job_abort = coda_job_abort,
833 .lock = coda_lock,
834 .unlock = coda_unlock,
835};
836
Philipp Zabel89548442014-07-11 06:36:17 -0300837static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
838{
839 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
840 int luma_map, chro_map, i;
841
842 memset(tiled_map, 0, sizeof(*tiled_map));
843
844 luma_map = 64;
845 chro_map = 64;
846 tiled_map->map_type = tiled_map_type;
847 for (i = 0; i < 16; i++)
848 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
849 for (i = 0; i < 4; i++)
850 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
851 for (i = 0; i < 16; i++)
852 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
853
854 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
855 tiled_map->xy2rbc_config = 0;
856 } else {
857 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
858 tiled_map_type);
859 return;
860 }
861}
862
Javier Martin186b2502012-07-26 05:53:35 -0300863static void set_default_params(struct coda_ctx *ctx)
864{
Philipp Zabel121cacf2014-07-18 07:22:42 -0300865 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300866 int max_w;
867 int max_h;
868
Philipp Zabel121cacf2014-07-18 07:22:42 -0300869 if (ctx->inst_type == CODA_INST_ENCODER) {
870 src_fourcc = V4L2_PIX_FMT_YUV420;
871 dst_fourcc = V4L2_PIX_FMT_H264;
872 } else {
873 src_fourcc = V4L2_PIX_FMT_H264;
874 dst_fourcc = V4L2_PIX_FMT_YUV420;
875 }
876 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300877 max_w = ctx->codec->max_w;
878 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300879
Philipp Zabel121cacf2014-07-18 07:22:42 -0300880 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -0300881 ctx->colorspace = V4L2_COLORSPACE_REC709;
882 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300883 ctx->aborting = 0;
884
885 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300886 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
887 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
888 ctx->q_data[V4L2_M2M_SRC].width = max_w;
889 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300890 ctx->q_data[V4L2_M2M_DST].width = max_w;
891 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -0300892 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
893 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
894 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
895 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
896 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
897 } else {
898 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
899 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
900 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
901 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
902 }
Philipp Zabel52c41672014-07-11 06:36:19 -0300903 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
904 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
905 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
906 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -0300907
908 if (ctx->dev->devtype->product == CODA_960)
909 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -0300910}
911
912/*
913 * Queue operations
914 */
915static int coda_queue_setup(struct vb2_queue *vq,
916 const struct v4l2_format *fmt,
917 unsigned int *nbuffers, unsigned int *nplanes,
918 unsigned int sizes[], void *alloc_ctxs[])
919{
920 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300921 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300922 unsigned int size;
923
Philipp Zabele34db062012-08-29 08:22:00 -0300924 q_data = get_q_data(ctx, vq->type);
925 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300926
927 *nplanes = 1;
928 sizes[0] = size;
929
930 alloc_ctxs[0] = ctx->dev->alloc_ctx;
931
932 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
933 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
934
935 return 0;
936}
937
938static int coda_buf_prepare(struct vb2_buffer *vb)
939{
940 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
941 struct coda_q_data *q_data;
942
943 q_data = get_q_data(ctx, vb->vb2_queue->type);
944
945 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
946 v4l2_warn(&ctx->dev->v4l2_dev,
947 "%s data will not fit into plane (%lu < %lu)\n",
948 __func__, vb2_plane_size(vb, 0),
949 (long)q_data->sizeimage);
950 return -EINVAL;
951 }
952
Javier Martin186b2502012-07-26 05:53:35 -0300953 return 0;
954}
955
956static void coda_buf_queue(struct vb2_buffer *vb)
957{
958 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300959 struct coda_q_data *q_data;
960
961 q_data = get_q_data(ctx, vb->vb2_queue->type);
962
963 /*
964 * In the decoder case, immediately try to copy the buffer into the
965 * bitstream ringbuffer and mark it as ready to be dequeued.
966 */
967 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
968 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
969 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300970 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -0300971 * the stream end
972 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300973 if (vb2_get_plane_payload(vb, 0) == 0)
974 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300975 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -0300976 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -0300977 if (vb2_is_streaming(vb->vb2_queue))
978 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300979 mutex_unlock(&ctx->bitstream_mutex);
980 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -0300981 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300982 }
Javier Martin186b2502012-07-26 05:53:35 -0300983}
984
Philipp Zabel79924ca2014-07-23 12:28:45 -0300985int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
986 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300987{
988 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
989 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -0300990 if (!buf->vaddr) {
991 v4l2_err(&dev->v4l2_dev,
992 "Failed to allocate %s buffer of size %u\n",
993 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300994 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -0300995 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300996
997 buf->size = size;
998
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300999 if (name && parent) {
1000 buf->blob.data = buf->vaddr;
1001 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001002 buf->dentry = debugfs_create_blob(name, 0644, parent,
1003 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001004 if (!buf->dentry)
1005 dev_warn(&dev->plat_dev->dev,
1006 "failed to create debugfs entry %s\n", name);
1007 }
1008
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001009 return 0;
1010}
1011
Philipp Zabel79924ca2014-07-23 12:28:45 -03001012void coda_free_aux_buf(struct coda_dev *dev,
1013 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001014{
1015 if (buf->vaddr) {
1016 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1017 buf->vaddr, buf->paddr);
1018 buf->vaddr = NULL;
1019 buf->size = 0;
1020 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001021 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001022}
1023
Javier Martin186b2502012-07-26 05:53:35 -03001024static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1025{
1026 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1027 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001028 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001029 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001030 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001031 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001032
Philipp Zabelb96904e2013-05-23 10:42:58 -03001033 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001034 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1035 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03001036 /* copy the buffers that where queued before streamon */
1037 mutex_lock(&ctx->bitstream_mutex);
1038 coda_fill_bitstream(ctx);
1039 mutex_unlock(&ctx->bitstream_mutex);
1040
Philipp Zabelb9063522014-08-05 14:00:10 -03001041 if (coda_get_bitstream_payload(ctx) < 512) {
1042 ret = -EINVAL;
1043 goto err;
1044 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001045 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001046 if (count < 1) {
1047 ret = -EINVAL;
1048 goto err;
1049 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001050 }
1051
1052 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001053 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001054 if (count < 1) {
1055 ret = -EINVAL;
1056 goto err;
1057 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001058
1059 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001060 }
Javier Martin186b2502012-07-26 05:53:35 -03001061
Philipp Zabelb96904e2013-05-23 10:42:58 -03001062 /* Don't start the coda unless both queues are on */
1063 if (!(ctx->streamon_out & ctx->streamon_cap))
1064 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001065
Philipp Zabeleb107512013-09-30 10:34:45 -03001066 /* Allow decoder device_run with no new buffers queued */
1067 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03001068 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001069
Philipp Zabelb96904e2013-05-23 10:42:58 -03001070 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001071 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001072 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001073
Philipp Zabelb96904e2013-05-23 10:42:58 -03001074 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1075 q_data_dst->fourcc);
1076 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001077 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001078 ret = -EINVAL;
1079 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001080 }
1081
Philipp Zabela1192a12014-07-23 12:28:40 -03001082 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001083 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001084 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001085 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001086 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001087 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001088 }
1089
Philipp Zabel89548442014-07-11 06:36:17 -03001090 ctx->initialized = 1;
1091 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001092
1093err:
1094 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1095 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1096 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1097 } else {
1098 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1099 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1100 }
1101 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001102}
1103
Hans Verkuile37559b2014-04-17 02:47:21 -03001104static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001105{
1106 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001107 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001108 struct vb2_buffer *buf;
Javier Martin186b2502012-07-26 05:53:35 -03001109
1110 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001111 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001112 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001113 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001114
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001115 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001116
Philipp Zabel918c66f2013-06-27 06:59:01 -03001117 ctx->isequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001118
1119 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1120 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001121 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001122 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001123 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001124 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001125
1126 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001127 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001128
1129 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1130 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001131 }
1132
Philipp Zabel918c66f2013-06-27 06:59:01 -03001133 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001134 struct coda_timestamp *ts;
1135
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001136 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001137 while (!list_empty(&ctx->timestamp_list)) {
1138 ts = list_first_entry(&ctx->timestamp_list,
1139 struct coda_timestamp, list);
1140 list_del(&ts->list);
1141 kfree(ts);
1142 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001143 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001144 kfifo_init(&ctx->bitstream_fifo,
1145 ctx->bitstream.vaddr, ctx->bitstream.size);
1146 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001147 }
Javier Martin186b2502012-07-26 05:53:35 -03001148}
1149
Philipp Zabel121cacf2014-07-18 07:22:42 -03001150static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001151 .queue_setup = coda_queue_setup,
1152 .buf_prepare = coda_buf_prepare,
1153 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001154 .start_streaming = coda_start_streaming,
1155 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001156 .wait_prepare = vb2_ops_wait_prepare,
1157 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001158};
1159
1160static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1161{
1162 struct coda_ctx *ctx =
1163 container_of(ctrl->handler, struct coda_ctx, ctrls);
1164
1165 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1166 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1167
1168 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001169 case V4L2_CID_HFLIP:
1170 if (ctrl->val)
1171 ctx->params.rot_mode |= CODA_MIR_HOR;
1172 else
1173 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1174 break;
1175 case V4L2_CID_VFLIP:
1176 if (ctrl->val)
1177 ctx->params.rot_mode |= CODA_MIR_VER;
1178 else
1179 ctx->params.rot_mode &= ~CODA_MIR_VER;
1180 break;
Javier Martin186b2502012-07-26 05:53:35 -03001181 case V4L2_CID_MPEG_VIDEO_BITRATE:
1182 ctx->params.bitrate = ctrl->val / 1000;
1183 break;
1184 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1185 ctx->params.gop_size = ctrl->val;
1186 break;
1187 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1188 ctx->params.h264_intra_qp = ctrl->val;
1189 break;
1190 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1191 ctx->params.h264_inter_qp = ctrl->val;
1192 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001193 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1194 ctx->params.h264_min_qp = ctrl->val;
1195 break;
1196 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1197 ctx->params.h264_max_qp = ctrl->val;
1198 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001199 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1200 ctx->params.h264_deblk_alpha = ctrl->val;
1201 break;
1202 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1203 ctx->params.h264_deblk_beta = ctrl->val;
1204 break;
1205 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1206 ctx->params.h264_deblk_enabled = (ctrl->val ==
1207 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1208 break;
Javier Martin186b2502012-07-26 05:53:35 -03001209 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1210 ctx->params.mpeg4_intra_qp = ctrl->val;
1211 break;
1212 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1213 ctx->params.mpeg4_inter_qp = ctrl->val;
1214 break;
1215 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1216 ctx->params.slice_mode = ctrl->val;
1217 break;
1218 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1219 ctx->params.slice_max_mb = ctrl->val;
1220 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001221 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1222 ctx->params.slice_max_bits = ctrl->val * 8;
1223 break;
Javier Martin186b2502012-07-26 05:53:35 -03001224 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1225 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001226 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1227 ctx->params.intra_refresh = ctrl->val;
1228 break;
Javier Martin186b2502012-07-26 05:53:35 -03001229 default:
1230 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1231 "Invalid control, id=%d, val=%d\n",
1232 ctrl->id, ctrl->val);
1233 return -EINVAL;
1234 }
1235
1236 return 0;
1237}
1238
Philipp Zabel814c3762014-07-18 07:22:45 -03001239static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001240 .s_ctrl = coda_s_ctrl,
1241};
1242
1243static int coda_ctrls_setup(struct coda_ctx *ctx)
1244{
1245 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1246
1247 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001248 V4L2_CID_HFLIP, 0, 1, 1, 0);
1249 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1250 V4L2_CID_VFLIP, 0, 1, 1, 0);
1251 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001252 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1253 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1254 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1255 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001256 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001257 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001258 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001259 if (ctx->dev->devtype->product != CODA_960) {
1260 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1261 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1262 }
1263 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1264 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001265 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001266 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1267 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1268 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1269 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1270 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1271 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1272 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1273 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001274 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1275 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1276 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1277 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1278 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001279 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1280 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001281 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1282 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001283 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001284 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1285 500);
Javier Martin186b2502012-07-26 05:53:35 -03001286 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1287 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1288 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1289 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1290 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001291 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001292 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1293 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001294
1295 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001296 v4l2_err(&ctx->dev->v4l2_dev,
1297 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001298 ctx->ctrls.error);
1299 return -EINVAL;
1300 }
1301
1302 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1303}
1304
Philipp Zabel121cacf2014-07-18 07:22:42 -03001305static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001306{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001307 vq->drv_priv = ctx;
1308 vq->ops = &coda_qops;
1309 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1310 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1311 vq->lock = &ctx->dev->dev_mutex;
1312
1313 return vb2_queue_init(vq);
1314}
1315
Philipp Zabel79924ca2014-07-23 12:28:45 -03001316int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1317 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001318{
Javier Martin186b2502012-07-26 05:53:35 -03001319 int ret;
1320
Javier Martin186b2502012-07-26 05:53:35 -03001321 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001322 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001323 src_vq->mem_ops = &vb2_dma_contig_memops;
1324
Philipp Zabel121cacf2014-07-18 07:22:42 -03001325 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001326 if (ret)
1327 return ret;
1328
Javier Martin186b2502012-07-26 05:53:35 -03001329 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001330 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001331 dst_vq->mem_ops = &vb2_dma_contig_memops;
1332
Philipp Zabel121cacf2014-07-18 07:22:42 -03001333 return coda_queue_init(priv, dst_vq);
1334}
1335
Philipp Zabel79924ca2014-07-23 12:28:45 -03001336int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1337 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001338{
1339 int ret;
1340
1341 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1342 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1343 src_vq->mem_ops = &vb2_dma_contig_memops;
1344
1345 ret = coda_queue_init(priv, src_vq);
1346 if (ret)
1347 return ret;
1348
1349 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1350 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1351 dst_vq->mem_ops = &vb2_dma_contig_memops;
1352
1353 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001354}
1355
Philipp Zabele11f3e62012-07-25 09:16:58 -03001356static int coda_next_free_instance(struct coda_dev *dev)
1357{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001358 int idx = ffz(dev->instance_mask);
1359
1360 if ((idx < 0) ||
1361 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1362 return -EBUSY;
1363
1364 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001365}
1366
Philipp Zabela1192a12014-07-23 12:28:40 -03001367static int coda_open(struct file *file, enum coda_inst_type inst_type,
1368 const struct coda_context_ops *ctx_ops)
Javier Martin186b2502012-07-26 05:53:35 -03001369{
1370 struct coda_dev *dev = video_drvdata(file);
1371 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001372 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001373 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001374 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001375
Philipp Zabelf23797b2014-08-06 08:02:23 -03001376 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001377 if (!ctx)
1378 return -ENOMEM;
1379
Fabio Estevamf82bc202013-08-21 11:14:16 -03001380 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001381 if (idx < 0) {
1382 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001383 goto err_coda_max;
1384 }
1385 set_bit(idx, &dev->instance_mask);
1386
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001387 name = kasprintf(GFP_KERNEL, "context%d", idx);
1388 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1389 kfree(name);
1390
Philipp Zabel121cacf2014-07-18 07:22:42 -03001391 ctx->inst_type = inst_type;
Philipp Zabela1192a12014-07-23 12:28:40 -03001392 ctx->ops = ctx_ops;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001393 init_completion(&ctx->completion);
1394 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabela1192a12014-07-23 12:28:40 -03001395 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001396 v4l2_fh_init(&ctx->fh, video_devdata(file));
1397 file->private_data = &ctx->fh;
1398 v4l2_fh_add(&ctx->fh);
1399 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001400 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001401 switch (dev->devtype->product) {
1402 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03001403 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001404 ctx->reg_idx = 0;
1405 break;
1406 default:
1407 ctx->reg_idx = idx;
1408 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001409
Philipp Zabel1e172732014-07-11 06:36:23 -03001410 /* Power up and upload firmware if necessary */
1411 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1412 if (ret < 0) {
1413 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1414 goto err_pm_get;
1415 }
1416
Fabio Estevam79830db2013-08-21 11:14:17 -03001417 ret = clk_prepare_enable(dev->clk_per);
1418 if (ret)
1419 goto err_clk_per;
1420
1421 ret = clk_prepare_enable(dev->clk_ahb);
1422 if (ret)
1423 goto err_clk_ahb;
1424
Javier Martin186b2502012-07-26 05:53:35 -03001425 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001426 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1427 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001428 if (IS_ERR(ctx->fh.m2m_ctx)) {
1429 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001430
1431 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1432 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001433 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001434 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001435
Javier Martin186b2502012-07-26 05:53:35 -03001436 ret = coda_ctrls_setup(ctx);
1437 if (ret) {
1438 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001439 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001440 }
1441
1442 ctx->fh.ctrl_handler = &ctx->ctrls;
1443
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001444 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
1445 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001446 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001447 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001448 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03001449 }
1450
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001451 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1452 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1453 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1454 if (!ctx->bitstream.vaddr) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001455 v4l2_err(&dev->v4l2_dev,
1456 "failed to allocate bitstream ringbuffer");
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001457 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001458 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001459 }
1460 kfifo_init(&ctx->bitstream_fifo,
1461 ctx->bitstream.vaddr, ctx->bitstream.size);
1462 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001463 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001464 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001465
Javier Martin186b2502012-07-26 05:53:35 -03001466 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001467 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001468 coda_unlock(ctx);
1469
Javier Martin186b2502012-07-26 05:53:35 -03001470 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1471 ctx->idx, ctx);
1472
1473 return 0;
1474
Fabio Estevamf82bc202013-08-21 11:14:16 -03001475err_dma_writecombine:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001476 if (ctx->dev->devtype->product == CODA_DX6)
1477 coda_free_aux_buf(dev, &ctx->workbuf);
1478 coda_free_aux_buf(dev, &ctx->parabuf);
1479err_dma_alloc:
1480 v4l2_ctrl_handler_free(&ctx->ctrls);
1481err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001482 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001483err_ctx_init:
1484 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001485err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001486 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001487err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001488 pm_runtime_put_sync(&dev->plat_dev->dev);
1489err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001490 v4l2_fh_del(&ctx->fh);
1491 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001492 clear_bit(ctx->idx, &dev->instance_mask);
1493err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001494 kfree(ctx);
1495 return ret;
1496}
1497
Philipp Zabel121cacf2014-07-18 07:22:42 -03001498static int coda_encoder_open(struct file *file)
1499{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001500 return coda_open(file, CODA_INST_ENCODER, &coda_bit_encode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001501}
1502
1503static int coda_decoder_open(struct file *file)
1504{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001505 return coda_open(file, CODA_INST_DECODER, &coda_bit_decode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001506}
1507
Javier Martin186b2502012-07-26 05:53:35 -03001508static int coda_release(struct file *file)
1509{
1510 struct coda_dev *dev = video_drvdata(file);
1511 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1512
1513 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1514 ctx);
1515
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001516 debugfs_remove_recursive(ctx->debugfs_entry);
1517
Philipp Zabel918c66f2013-06-27 06:59:01 -03001518 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001519 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001520
1521 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03001522 if (ctx->initialized) {
1523 queue_work(dev->workqueue, &ctx->seq_end_work);
1524 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001525 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001526
Javier Martin186b2502012-07-26 05:53:35 -03001527 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001528 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001529 coda_unlock(ctx);
1530
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001531 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1532 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001533 if (ctx->dev->devtype->product == CODA_DX6)
1534 coda_free_aux_buf(dev, &ctx->workbuf);
1535
1536 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03001537 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001538 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001539 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001540 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001541 v4l2_fh_del(&ctx->fh);
1542 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001543 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001544 if (ctx->ops->release)
1545 ctx->ops->release(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001546 kfree(ctx);
1547
1548 return 0;
1549}
1550
Philipp Zabel121cacf2014-07-18 07:22:42 -03001551static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001552 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03001553 .open = coda_encoder_open,
1554 .release = coda_release,
1555 .poll = v4l2_m2m_fop_poll,
1556 .unlocked_ioctl = video_ioctl2,
1557 .mmap = v4l2_m2m_fop_mmap,
1558};
1559
1560static const struct v4l2_file_operations coda_decoder_fops = {
1561 .owner = THIS_MODULE,
1562 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03001563 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001564 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001565 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001566 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001567};
1568
Philipp Zabel87048bb2012-07-02 07:03:43 -03001569static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001570{
Javier Martin186b2502012-07-26 05:53:35 -03001571 u32 data;
1572 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001573 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001574
Fabio Estevam79830db2013-08-21 11:14:17 -03001575 ret = clk_prepare_enable(dev->clk_per);
1576 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001577 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001578
1579 ret = clk_prepare_enable(dev->clk_ahb);
1580 if (ret)
1581 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001582
Philipp Zabel8f452842014-07-11 06:36:35 -03001583 if (dev->rstc)
1584 reset_control_reset(dev->rstc);
1585
Javier Martin186b2502012-07-26 05:53:35 -03001586 /*
1587 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001588 * The 16-bit chars in the code buffer are in memory access
1589 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001590 * Data in this SRAM survives a reboot.
1591 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001592 p = (u16 *)dev->codebuf.vaddr;
1593 if (dev->devtype->product == CODA_DX6) {
1594 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1595 data = CODA_DOWN_ADDRESS_SET(i) |
1596 CODA_DOWN_DATA_SET(p[i ^ 1]);
1597 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1598 }
1599 } else {
1600 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1601 data = CODA_DOWN_ADDRESS_SET(i) |
1602 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1603 3 - (i % 4)]);
1604 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1605 }
Javier Martin186b2502012-07-26 05:53:35 -03001606 }
Javier Martin186b2502012-07-26 05:53:35 -03001607
Philipp Zabel9acf7692013-05-23 10:42:56 -03001608 /* Clear registers */
1609 for (i = 0; i < 64; i++)
1610 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1611
Javier Martin186b2502012-07-26 05:53:35 -03001612 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001613 if (dev->devtype->product == CODA_960 ||
1614 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001615 coda_write(dev, dev->tempbuf.paddr,
1616 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001617 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001618 } else {
1619 coda_write(dev, dev->workbuf.paddr,
1620 CODA_REG_BIT_WORK_BUF_ADDR);
1621 }
Javier Martin186b2502012-07-26 05:53:35 -03001622 coda_write(dev, dev->codebuf.paddr,
1623 CODA_REG_BIT_CODE_BUF_ADDR);
1624 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1625
1626 /* Set default values */
1627 switch (dev->devtype->product) {
1628 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001629 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1630 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001631 break;
1632 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001633 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1634 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001635 }
Philipp Zabel89548442014-07-11 06:36:17 -03001636 if (dev->devtype->product == CODA_960)
1637 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1638 else
1639 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001640
1641 if (dev->devtype->product != CODA_DX6)
1642 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1643
Javier Martin186b2502012-07-26 05:53:35 -03001644 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1645 CODA_REG_BIT_INT_ENABLE);
1646
1647 /* Reset VPU and start processor */
1648 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1649 data |= CODA_REG_RESET_ENABLE;
1650 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1651 udelay(10);
1652 data &= ~CODA_REG_RESET_ENABLE;
1653 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1654 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1655
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001656 clk_disable_unprepare(dev->clk_ahb);
1657 clk_disable_unprepare(dev->clk_per);
1658
1659 return 0;
1660
1661err_clk_ahb:
1662 clk_disable_unprepare(dev->clk_per);
1663err_clk_per:
1664 return ret;
1665}
1666
Philipp Zabel121cacf2014-07-18 07:22:42 -03001667static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
1668{
1669 vfd->release = video_device_release_empty,
1670 vfd->lock = &dev->dev_mutex;
1671 vfd->v4l2_dev = &dev->v4l2_dev;
1672 vfd->vfl_dir = VFL_DIR_M2M;
1673 video_set_drvdata(vfd, dev);
1674
Philipp Zabela188a662014-08-05 14:00:20 -03001675 /* Not applicable, use the selection API instead */
1676 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1677 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1678 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1679
Philipp Zabel121cacf2014-07-18 07:22:42 -03001680 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1681}
1682
Javier Martin186b2502012-07-26 05:53:35 -03001683static void coda_fw_callback(const struct firmware *fw, void *context)
1684{
1685 struct coda_dev *dev = context;
1686 struct platform_device *pdev = dev->plat_dev;
1687 int ret;
1688
1689 if (!fw) {
1690 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1691 return;
1692 }
1693
1694 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001695 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1696 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001697 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001698 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1699 return;
1700 }
1701
Philipp Zabel87048bb2012-07-02 07:03:43 -03001702 /* Copy the whole firmware image to the code buffer */
1703 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1704 release_firmware(fw);
1705
Philipp Zabel1e172732014-07-11 06:36:23 -03001706 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
1707 /*
1708 * Enabling power temporarily will cause coda_hw_init to be
1709 * called via coda_runtime_resume by the pm domain.
1710 */
1711 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1712 if (ret < 0) {
1713 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
1714 ret);
1715 return;
1716 }
1717
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001718 ret = coda_check_firmware(dev);
1719 if (ret < 0)
1720 return;
1721
Philipp Zabel1e172732014-07-11 06:36:23 -03001722 pm_runtime_put_sync(&dev->plat_dev->dev);
1723 } else {
1724 /*
1725 * If runtime pm is disabled or pm_domain is not set,
1726 * initialize once manually.
1727 */
1728 ret = coda_hw_init(dev);
1729 if (ret < 0) {
1730 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1731 return;
1732 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001733
1734 ret = coda_check_firmware(dev);
1735 if (ret < 0)
1736 return;
Javier Martin186b2502012-07-26 05:53:35 -03001737 }
1738
Javier Martin186b2502012-07-26 05:53:35 -03001739 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1740 if (IS_ERR(dev->alloc_ctx)) {
1741 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1742 return;
1743 }
1744
1745 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1746 if (IS_ERR(dev->m2m_dev)) {
1747 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1748 goto rel_ctx;
1749 }
1750
Philipp Zabel121cacf2014-07-18 07:22:42 -03001751 dev->vfd[0].fops = &coda_encoder_fops,
1752 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
1753 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
1754 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03001755 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03001756 v4l2_err(&dev->v4l2_dev,
1757 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03001758 goto rel_m2m;
1759 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03001760
1761 dev->vfd[1].fops = &coda_decoder_fops,
1762 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
1763 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
1764 ret = coda_register_device(dev, &dev->vfd[1]);
1765 if (ret) {
1766 v4l2_err(&dev->v4l2_dev,
1767 "Failed to register decoder video device\n");
1768 goto rel_m2m;
1769 }
1770
1771 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1772 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03001773
1774 return;
1775
1776rel_m2m:
1777 v4l2_m2m_release(dev->m2m_dev);
1778rel_ctx:
1779 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1780}
1781
1782static int coda_firmware_request(struct coda_dev *dev)
1783{
1784 char *fw = dev->devtype->firmware;
1785
1786 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1787 coda_product_name(dev->devtype->product));
1788
1789 return request_firmware_nowait(THIS_MODULE, true,
1790 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1791}
1792
1793enum coda_platform {
1794 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001795 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03001796 CODA_IMX6Q,
1797 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03001798};
1799
Emil Goodec06d8752012-08-14 17:44:42 -03001800static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001801 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001802 .firmware = "v4l-codadx6-imx27.bin",
1803 .product = CODA_DX6,
1804 .codecs = codadx6_codecs,
1805 .num_codecs = ARRAY_SIZE(codadx6_codecs),
1806 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001807 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03001808 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001809 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001810 .firmware = "v4l-coda7541-imx53.bin",
1811 .product = CODA_7541,
1812 .codecs = coda7_codecs,
1813 .num_codecs = ARRAY_SIZE(coda7_codecs),
1814 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001815 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001816 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001817 },
Philipp Zabel89548442014-07-11 06:36:17 -03001818 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001819 .firmware = "v4l-coda960-imx6q.bin",
1820 .product = CODA_960,
1821 .codecs = coda9_codecs,
1822 .num_codecs = ARRAY_SIZE(coda9_codecs),
1823 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001824 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001825 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03001826 },
1827 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001828 .firmware = "v4l-coda960-imx6dl.bin",
1829 .product = CODA_960,
1830 .codecs = coda9_codecs,
1831 .num_codecs = ARRAY_SIZE(coda9_codecs),
1832 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001833 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001834 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03001835 },
Javier Martin186b2502012-07-26 05:53:35 -03001836};
1837
1838static struct platform_device_id coda_platform_ids[] = {
1839 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001840 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001841 { /* sentinel */ }
1842};
1843MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1844
1845#ifdef CONFIG_OF
1846static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03001847 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001848 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03001849 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
1850 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03001851 { /* sentinel */ }
1852};
1853MODULE_DEVICE_TABLE(of, coda_dt_ids);
1854#endif
1855
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001856static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001857{
1858 const struct of_device_id *of_id =
1859 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1860 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001861 struct coda_platform_data *pdata = pdev->dev.platform_data;
1862 struct device_node *np = pdev->dev.of_node;
1863 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001864 struct coda_dev *dev;
1865 struct resource *res;
1866 int ret, irq;
1867
Philipp Zabelf23797b2014-08-06 08:02:23 -03001868 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001869 if (!dev) {
1870 dev_err(&pdev->dev, "Not enough memory for %s\n",
1871 CODA_NAME);
1872 return -ENOMEM;
1873 }
1874
1875 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001876 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001877
1878 dev->plat_dev = pdev;
1879 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1880 if (IS_ERR(dev->clk_per)) {
1881 dev_err(&pdev->dev, "Could not get per clock\n");
1882 return PTR_ERR(dev->clk_per);
1883 }
1884
1885 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1886 if (IS_ERR(dev->clk_ahb)) {
1887 dev_err(&pdev->dev, "Could not get ahb clock\n");
1888 return PTR_ERR(dev->clk_ahb);
1889 }
1890
1891 /* Get memory for physical registers */
1892 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03001893 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1894 if (IS_ERR(dev->regs_base))
1895 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03001896
1897 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03001898 irq = platform_get_irq_byname(pdev, "bit");
1899 if (irq < 0)
1900 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001901 if (irq < 0) {
1902 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03001903 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03001904 }
1905
Fabio Estevam08c8d142014-06-04 15:46:24 -03001906 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
1907 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
1908 if (ret < 0) {
1909 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
1910 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001911 }
1912
Philipp Zabelee27aa92014-07-24 16:50:03 -03001913 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03001914 if (IS_ERR(dev->rstc)) {
1915 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03001916 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03001917 dev->rstc = NULL;
1918 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001919 dev_err(&pdev->dev, "failed get reset control: %d\n",
1920 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03001921 return ret;
1922 }
1923 }
1924
Philipp Zabel657eee72013-04-29 16:17:14 -07001925 /* Get IRAM pool from device tree or platform data */
1926 pool = of_get_named_gen_pool(np, "iram", 0);
1927 if (!pool && pdata)
1928 pool = dev_get_gen_pool(pdata->iram_dev);
1929 if (!pool) {
1930 dev_err(&pdev->dev, "iram pool not available\n");
1931 return -ENOMEM;
1932 }
1933 dev->iram_pool = pool;
1934
Javier Martin186b2502012-07-26 05:53:35 -03001935 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1936 if (ret)
1937 return ret;
1938
1939 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001940 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001941
1942 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
1943
1944 if (of_id) {
1945 dev->devtype = of_id->data;
1946 } else if (pdev_id) {
1947 dev->devtype = &coda_devdata[pdev_id->driver_data];
1948 } else {
1949 v4l2_device_unregister(&dev->v4l2_dev);
1950 return -EINVAL;
1951 }
1952
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001953 dev->debugfs_root = debugfs_create_dir("coda", NULL);
1954 if (!dev->debugfs_root)
1955 dev_warn(&pdev->dev, "failed to create debugfs root\n");
1956
Javier Martin186b2502012-07-26 05:53:35 -03001957 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001958 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001959 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001960 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001961 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001962 if (ret < 0) {
1963 dev_err(&pdev->dev, "failed to allocate work buffer\n");
1964 v4l2_device_unregister(&dev->v4l2_dev);
1965 return ret;
1966 }
Javier Martin186b2502012-07-26 05:53:35 -03001967 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001968
1969 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001970 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001971 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001972 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001973 if (ret < 0) {
1974 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
1975 v4l2_device_unregister(&dev->v4l2_dev);
1976 return ret;
1977 }
Javier Martin186b2502012-07-26 05:53:35 -03001978 }
1979
Philipp Zabel401e9722014-07-11 06:36:43 -03001980 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001981 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
1982 &dev->iram.paddr);
1983 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03001984 dev_warn(&pdev->dev, "unable to alloc iram\n");
1985 } else {
1986 dev->iram.blob.data = dev->iram.vaddr;
1987 dev->iram.blob.size = dev->iram.size;
1988 dev->iram.dentry = debugfs_create_blob("iram", 0644,
1989 dev->debugfs_root,
1990 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03001991 }
1992
Philipp Zabel32b6f202014-07-11 06:36:20 -03001993 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1994 if (!dev->workqueue) {
1995 dev_err(&pdev->dev, "unable to alloc workqueue\n");
1996 return -ENOMEM;
1997 }
1998
Javier Martin186b2502012-07-26 05:53:35 -03001999 platform_set_drvdata(pdev, dev);
2000
Philipp Zabel1e172732014-07-11 06:36:23 -03002001 pm_runtime_enable(&pdev->dev);
2002
Javier Martin186b2502012-07-26 05:53:35 -03002003 return coda_firmware_request(dev);
2004}
2005
2006static int coda_remove(struct platform_device *pdev)
2007{
2008 struct coda_dev *dev = platform_get_drvdata(pdev);
2009
Philipp Zabel121cacf2014-07-18 07:22:42 -03002010 video_unregister_device(&dev->vfd[0]);
2011 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03002012 if (dev->m2m_dev)
2013 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002014 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002015 if (dev->alloc_ctx)
2016 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2017 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002018 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002019 if (dev->iram.vaddr)
2020 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2021 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002022 coda_free_aux_buf(dev, &dev->codebuf);
2023 coda_free_aux_buf(dev, &dev->tempbuf);
2024 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002025 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002026 return 0;
2027}
2028
Philipp Zabel1e172732014-07-11 06:36:23 -03002029#ifdef CONFIG_PM_RUNTIME
2030static int coda_runtime_resume(struct device *dev)
2031{
2032 struct coda_dev *cdev = dev_get_drvdata(dev);
2033 int ret = 0;
2034
Philipp Zabel65919e62014-07-18 07:22:36 -03002035 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002036 ret = coda_hw_init(cdev);
2037 if (ret)
2038 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2039 }
2040
2041 return ret;
2042}
2043#endif
2044
2045static const struct dev_pm_ops coda_pm_ops = {
2046 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2047};
2048
Javier Martin186b2502012-07-26 05:53:35 -03002049static struct platform_driver coda_driver = {
2050 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002051 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002052 .driver = {
2053 .name = CODA_NAME,
2054 .owner = THIS_MODULE,
2055 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002056 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002057 },
2058 .id_table = coda_platform_ids,
2059};
2060
2061module_platform_driver(coda_driver);
2062
2063MODULE_LICENSE("GPL");
2064MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2065MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");