blob: a1080a7728e63ed4ae7404feb77f1752ab998b05 [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;
78 data = readl(dev->regs_base + reg);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030079 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030080 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
81 return data;
82}
83
Javier Martin186b2502012-07-26 05:53:35 -030084/*
Philipp Zabelb96904e2013-05-23 10:42:58 -030085 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -030086 */
Philipp Zabel814c3762014-07-18 07:22:45 -030087static const struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -030088 {
Philipp Zabelb96904e2013-05-23 10:42:58 -030089 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -030090 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -030091 },
92 {
93 .name = "YUV 4:2:0 Planar, YCrCb",
94 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -030095 },
96 {
97 .name = "H264 Encoded Stream",
98 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -030099 },
100 {
101 .name = "MPEG4 Encoded Stream",
102 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300103 },
104};
105
Philipp Zabelb96904e2013-05-23 10:42:58 -0300106#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
107 { mode, src_fourcc, dst_fourcc, max_w, max_h }
108
109/*
110 * Arrays of codecs supported by each given version of Coda:
111 * i.MX27 -> codadx6
112 * i.MX5x -> coda7
113 * i.MX6 -> coda960
114 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
115 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300116static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300117 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
118 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300119};
120
Philipp Zabel814c3762014-07-18 07:22:45 -0300121static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300122 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
123 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300124 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
125 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300126};
127
Philipp Zabel814c3762014-07-18 07:22:45 -0300128static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300129 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
130 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
131 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
132 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300133};
134
Philipp Zabelb96904e2013-05-23 10:42:58 -0300135static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300136{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300137 switch (fourcc) {
138 case V4L2_PIX_FMT_YUV420:
139 case V4L2_PIX_FMT_YVU420:
140 return true;
141 default:
142 return false;
143 }
144}
Javier Martin186b2502012-07-26 05:53:35 -0300145
Philipp Zabelb96904e2013-05-23 10:42:58 -0300146/*
147 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
148 * tables.
149 */
150static u32 coda_format_normalize_yuv(u32 fourcc)
151{
152 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
153}
154
Philipp Zabel814c3762014-07-18 07:22:45 -0300155static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
156 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300157{
Philipp Zabel814c3762014-07-18 07:22:45 -0300158 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300159 int num_codecs = dev->devtype->num_codecs;
160 int k;
161
162 src_fourcc = coda_format_normalize_yuv(src_fourcc);
163 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
164 if (src_fourcc == dst_fourcc)
165 return NULL;
166
167 for (k = 0; k < num_codecs; k++) {
168 if (codecs[k].src_fourcc == src_fourcc &&
169 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300170 break;
171 }
172
Philipp Zabelb96904e2013-05-23 10:42:58 -0300173 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300174 return NULL;
175
Philipp Zabelb96904e2013-05-23 10:42:58 -0300176 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300177}
178
Philipp Zabel57625592013-09-30 10:34:51 -0300179static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300180 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300181 int *max_w, int *max_h)
182{
Philipp Zabel814c3762014-07-18 07:22:45 -0300183 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300184 int num_codecs = dev->devtype->num_codecs;
185 unsigned int w, h;
186 int k;
187
188 if (codec) {
189 w = codec->max_w;
190 h = codec->max_h;
191 } else {
192 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
193 w = max(w, codecs[k].max_w);
194 h = max(h, codecs[k].max_h);
195 }
196 }
197
198 if (max_w)
199 *max_w = w;
200 if (max_h)
201 *max_h = h;
202}
203
Philipp Zabel79924ca2014-07-23 12:28:45 -0300204const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300205{
206 static char buf[9];
207
208 switch (product) {
209 case CODA_DX6:
210 return "CodaDx6";
211 case CODA_7541:
212 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300213 case CODA_960:
214 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300215 default:
216 snprintf(buf, sizeof(buf), "(0x%04x)", product);
217 return buf;
218 }
219}
220
Javier Martin186b2502012-07-26 05:53:35 -0300221/*
222 * V4L2 ioctl() operations.
223 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300224static int coda_querycap(struct file *file, void *priv,
225 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300226{
Philipp Zabel927933f2013-09-30 10:34:48 -0300227 struct coda_ctx *ctx = fh_to_ctx(priv);
228
Javier Martin186b2502012-07-26 05:53:35 -0300229 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300230 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
231 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300232 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300233 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300234 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
235
236 return 0;
237}
238
Philipp Zabel22e244b2014-07-18 07:22:43 -0300239static int coda_enum_fmt(struct file *file, void *priv,
240 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300241{
242 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300243 const struct coda_codec *codecs = ctx->dev->devtype->codecs;
244 const struct coda_fmt *formats = coda_formats;
245 const struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300246 int num_codecs = ctx->dev->devtype->num_codecs;
247 int num_formats = ARRAY_SIZE(coda_formats);
248 int i, k, num = 0;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300249 bool yuv;
250
251 if (ctx->inst_type == CODA_INST_ENCODER)
252 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
253 else
254 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300255
256 for (i = 0; i < num_formats; i++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300257 /* Skip either raw or compressed formats */
258 if (yuv != coda_format_is_yuv(formats[i].fourcc))
259 continue;
260 /* All uncompressed formats are always supported */
261 if (yuv) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300262 if (num == f->index)
263 break;
264 ++num;
265 continue;
266 }
267 /* Compressed formats may be supported, check the codec list */
268 for (k = 0; k < num_codecs; k++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300269 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
270 formats[i].fourcc == codecs[k].dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300271 break;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300272 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
Philipp Zabelb96904e2013-05-23 10:42:58 -0300273 formats[i].fourcc == codecs[k].src_fourcc)
274 break;
275 }
276 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300277 if (num == f->index)
278 break;
279 ++num;
280 }
281 }
282
283 if (i < num_formats) {
284 fmt = &formats[i];
285 strlcpy(f->description, fmt->name, sizeof(f->description));
286 f->pixelformat = fmt->fourcc;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300287 if (!yuv)
Philipp Zabelbaf70212013-09-30 10:34:46 -0300288 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300289 return 0;
290 }
291
292 /* Format not found */
293 return -EINVAL;
294}
295
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300296static int coda_g_fmt(struct file *file, void *priv,
297 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300298{
Javier Martin186b2502012-07-26 05:53:35 -0300299 struct coda_q_data *q_data;
300 struct coda_ctx *ctx = fh_to_ctx(priv);
301
Javier Martin186b2502012-07-26 05:53:35 -0300302 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300303 if (!q_data)
304 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300305
306 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300307 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300308 f->fmt.pix.width = q_data->width;
309 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300310 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300311
312 f->fmt.pix.sizeimage = q_data->sizeimage;
313 f->fmt.pix.colorspace = ctx->colorspace;
314
315 return 0;
316}
317
Philipp Zabel814c3762014-07-18 07:22:45 -0300318static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300319 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300320{
Philipp Zabel57625592013-09-30 10:34:51 -0300321 struct coda_dev *dev = ctx->dev;
322 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300323 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300324 enum v4l2_field field;
325
326 field = f->fmt.pix.field;
327 if (field == V4L2_FIELD_ANY)
328 field = V4L2_FIELD_NONE;
329 else if (V4L2_FIELD_NONE != field)
330 return -EINVAL;
331
332 /* V4L2 specification suggests the driver corrects the format struct
333 * if any of the dimensions is unsupported */
334 f->fmt.pix.field = field;
335
Philipp Zabel57625592013-09-30 10:34:51 -0300336 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
337 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
338 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
339 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300340
Philipp Zabel57625592013-09-30 10:34:51 -0300341 switch (f->fmt.pix.pixelformat) {
342 case V4L2_PIX_FMT_YUV420:
343 case V4L2_PIX_FMT_YVU420:
344 case V4L2_PIX_FMT_H264:
345 case V4L2_PIX_FMT_MPEG4:
346 case V4L2_PIX_FMT_JPEG:
347 break;
348 default:
349 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300350 if (!q_data)
351 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300352 f->fmt.pix.pixelformat = q_data->fourcc;
353 }
354
355 switch (f->fmt.pix.pixelformat) {
356 case V4L2_PIX_FMT_YUV420:
357 case V4L2_PIX_FMT_YVU420:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300358 /* Frame stride must be multiple of 8, but 16 for h.264 */
359 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300360 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300361 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300362 break;
363 case V4L2_PIX_FMT_H264:
364 case V4L2_PIX_FMT_MPEG4:
365 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300366 f->fmt.pix.bytesperline = 0;
367 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300368 break;
369 default:
370 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300371 }
372
373 return 0;
374}
375
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300376static int coda_try_fmt_vid_cap(struct file *file, void *priv,
377 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300378{
Javier Martin186b2502012-07-26 05:53:35 -0300379 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300380 const struct coda_codec *codec = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300381 struct vb2_queue *src_vq;
382 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300383
Philipp Zabel918c66f2013-06-27 06:59:01 -0300384 /*
385 * If the source format is already fixed, try to find a codec that
386 * converts to the given destination format
387 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300388 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300389 if (vb2_is_streaming(src_vq)) {
390 struct coda_q_data *q_data_src;
391
392 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
393 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
394 f->fmt.pix.pixelformat);
395 if (!codec)
396 return -EINVAL;
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300397
398 f->fmt.pix.width = q_data_src->width;
399 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300400 } else {
401 /* Otherwise determine codec by encoded format, if possible */
402 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
403 f->fmt.pix.pixelformat);
404 }
Javier Martin186b2502012-07-26 05:53:35 -0300405
406 f->fmt.pix.colorspace = ctx->colorspace;
407
Philipp Zabel57625592013-09-30 10:34:51 -0300408 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300409 if (ret < 0)
410 return ret;
411
412 /* The h.264 decoder only returns complete 16x16 macroblocks */
413 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300414 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300415 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300416 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300417 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
418 f->fmt.pix.height * 3 / 2;
419 }
420
421 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300422}
423
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300424static int coda_try_fmt_vid_out(struct file *file, void *priv,
425 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300426{
427 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelfcf59762014-08-05 14:00:12 -0300428 const struct coda_codec *codec = NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300429
Philipp Zabelb96904e2013-05-23 10:42:58 -0300430 /* Determine codec by encoded format, returns NULL if raw or invalid */
Philipp Zabelfcf59762014-08-05 14:00:12 -0300431 if (ctx->inst_type == CODA_INST_DECODER) {
432 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300433 V4L2_PIX_FMT_YUV420);
434 if (!codec)
Philipp Zabelfcf59762014-08-05 14:00:12 -0300435 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_H264,
436 V4L2_PIX_FMT_YUV420);
437 if (!codec)
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300438 return -EINVAL;
439 }
Javier Martin186b2502012-07-26 05:53:35 -0300440
441 if (!f->fmt.pix.colorspace)
442 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
443
Philipp Zabel57625592013-09-30 10:34:51 -0300444 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300445}
446
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300447static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300448{
449 struct coda_q_data *q_data;
450 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300451
Philipp Zabel14604e32014-07-11 06:36:22 -0300452 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300453 if (!vq)
454 return -EINVAL;
455
456 q_data = get_q_data(ctx, f->type);
457 if (!q_data)
458 return -EINVAL;
459
460 if (vb2_is_busy(vq)) {
461 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
462 return -EBUSY;
463 }
464
Philipp Zabelb96904e2013-05-23 10:42:58 -0300465 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300466 q_data->width = f->fmt.pix.width;
467 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300468 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300469 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300470 q_data->rect.left = 0;
471 q_data->rect.top = 0;
472 q_data->rect.width = f->fmt.pix.width;
473 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300474
475 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
476 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300477 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300478
479 return 0;
480}
481
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300482static int coda_s_fmt_vid_cap(struct file *file, void *priv,
483 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300484{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300485 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300486 int ret;
487
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300488 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300489 if (ret)
490 return ret;
491
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300492 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300493}
494
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300495static int coda_s_fmt_vid_out(struct file *file, void *priv,
496 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300497{
498 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300499 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300500 int ret;
501
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300502 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300503 if (ret)
504 return ret;
505
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300506 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300507 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300508 return ret;
509
510 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300511
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300512 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
513 coda_g_fmt(file, priv, &f_cap);
514 f_cap.fmt.pix.width = f->fmt.pix.width;
515 f_cap.fmt.pix.height = f->fmt.pix.height;
516
517 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
518 if (ret)
519 return ret;
520
521 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300522}
523
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300524static int coda_qbuf(struct file *file, void *priv,
525 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300526{
527 struct coda_ctx *ctx = fh_to_ctx(priv);
528
Philipp Zabel14604e32014-07-11 06:36:22 -0300529 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300530}
531
Philipp Zabel918c66f2013-06-27 06:59:01 -0300532static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
533 struct v4l2_buffer *buf)
534{
535 struct vb2_queue *src_vq;
536
Philipp Zabel14604e32014-07-11 06:36:22 -0300537 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300538
539 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
540 (buf->sequence == (ctx->qsequence - 1)));
541}
542
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300543static int coda_dqbuf(struct file *file, void *priv,
544 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300545{
546 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300547 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300548
Philipp Zabel14604e32014-07-11 06:36:22 -0300549 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300550
551 /* If this is the last capture buffer, emit an end-of-stream event */
552 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
553 coda_buf_is_end_of_stream(ctx, buf)) {
554 const struct v4l2_event eos_event = {
555 .type = V4L2_EVENT_EOS
556 };
557
558 v4l2_event_queue_fh(&ctx->fh, &eos_event);
559 }
560
561 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300562}
563
Philipp Zabel52c41672014-07-11 06:36:19 -0300564static int coda_g_selection(struct file *file, void *fh,
565 struct v4l2_selection *s)
566{
567 struct coda_ctx *ctx = fh_to_ctx(fh);
568 struct coda_q_data *q_data;
569 struct v4l2_rect r, *rsel;
570
571 q_data = get_q_data(ctx, s->type);
572 if (!q_data)
573 return -EINVAL;
574
575 r.left = 0;
576 r.top = 0;
577 r.width = q_data->width;
578 r.height = q_data->height;
579 rsel = &q_data->rect;
580
581 switch (s->target) {
582 case V4L2_SEL_TGT_CROP_DEFAULT:
583 case V4L2_SEL_TGT_CROP_BOUNDS:
584 rsel = &r;
585 /* fallthrough */
586 case V4L2_SEL_TGT_CROP:
587 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
588 return -EINVAL;
589 break;
590 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
591 case V4L2_SEL_TGT_COMPOSE_PADDED:
592 rsel = &r;
593 /* fallthrough */
594 case V4L2_SEL_TGT_COMPOSE:
595 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
596 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
597 return -EINVAL;
598 break;
599 default:
600 return -EINVAL;
601 }
602
603 s->r = *rsel;
604
605 return 0;
606}
607
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300608static int coda_try_decoder_cmd(struct file *file, void *fh,
609 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300610{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300611 if (dc->cmd != V4L2_DEC_CMD_STOP)
612 return -EINVAL;
613
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300614 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300615 return -EINVAL;
616
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300617 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300618 return -EINVAL;
619
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300620 return 0;
621}
622
623static int coda_decoder_cmd(struct file *file, void *fh,
624 struct v4l2_decoder_cmd *dc)
625{
626 struct coda_ctx *ctx = fh_to_ctx(fh);
627 int ret;
628
629 ret = coda_try_decoder_cmd(file, fh, dc);
630 if (ret < 0)
631 return ret;
632
633 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300634 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300635 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300636
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300637 /* Set the stream-end flag on this context */
638 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300639 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300640 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300641
Philipp Zabel918c66f2013-06-27 06:59:01 -0300642 return 0;
643}
644
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300645static int coda_subscribe_event(struct v4l2_fh *fh,
646 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300647{
648 switch (sub->type) {
649 case V4L2_EVENT_EOS:
650 return v4l2_event_subscribe(fh, sub, 0, NULL);
651 default:
652 return v4l2_ctrl_subscribe_event(fh, sub);
653 }
Javier Martin186b2502012-07-26 05:53:35 -0300654}
655
656static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300657 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300658
Philipp Zabel22e244b2014-07-18 07:22:43 -0300659 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300660 .vidioc_g_fmt_vid_cap = coda_g_fmt,
661 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
662 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300663
Philipp Zabel22e244b2014-07-18 07:22:43 -0300664 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300665 .vidioc_g_fmt_vid_out = coda_g_fmt,
666 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
667 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300668
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300669 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
670 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300671
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300672 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300673 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300674 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300675 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300676
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300677 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
678 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300679
Philipp Zabel52c41672014-07-11 06:36:19 -0300680 .vidioc_g_selection = coda_g_selection,
681
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300682 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300683 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300684
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300685 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300686 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300687};
688
Philipp Zabel79924ca2014-07-23 12:28:45 -0300689void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300690{
691 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
692 struct coda_dev *dev = ctx->dev;
693 int i;
694
695 for (i = 0; i < 16; i++)
696 coda_write(dev, tiled_map->xy2ca_map[i],
697 CODA9_GDI_XY2_CAS_0 + 4 * i);
698 for (i = 0; i < 4; i++)
699 coda_write(dev, tiled_map->xy2ba_map[i],
700 CODA9_GDI_XY2_BA_0 + 4 * i);
701 for (i = 0; i < 16; i++)
702 coda_write(dev, tiled_map->xy2ra_map[i],
703 CODA9_GDI_XY2_RAS_0 + 4 * i);
704 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
705 for (i = 0; i < 32; i++)
706 coda_write(dev, tiled_map->rbc2axi_map[i],
707 CODA9_GDI_RBC2_AXI_0 + 4 * i);
708}
709
Javier Martin186b2502012-07-26 05:53:35 -0300710/*
711 * Mem-to-mem operations.
712 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300713
714static void coda_device_run(void *m2m_priv)
715{
716 struct coda_ctx *ctx = m2m_priv;
717 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300718
719 queue_work(dev->workqueue, &ctx->pic_run_work);
720}
721
Philipp Zabel32b6f202014-07-11 06:36:20 -0300722static void coda_pic_run_work(struct work_struct *work)
723{
724 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
725 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300726 int ret;
727
728 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300729 mutex_lock(&dev->coda_mutex);
730
Philipp Zabela1192a12014-07-23 12:28:40 -0300731 ret = ctx->ops->prepare_run(ctx);
732 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
733 mutex_unlock(&dev->coda_mutex);
734 mutex_unlock(&ctx->buffer_mutex);
735 /* job_finish scheduled by prepare_decode */
736 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300737 }
738
Philipp Zabel32b6f202014-07-11 06:36:20 -0300739 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
740 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300741
742 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300743
744 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300745 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300746 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300747 }
748
749 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
750 queue_work(dev->workqueue, &ctx->seq_end_work);
751
752 mutex_unlock(&dev->coda_mutex);
753 mutex_unlock(&ctx->buffer_mutex);
754
Philipp Zabel14604e32014-07-11 06:36:22 -0300755 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300756}
757
758static int coda_job_ready(void *m2m_priv)
759{
760 struct coda_ctx *ctx = m2m_priv;
761
762 /*
763 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300764 * and 1 frame are needed. In the decoder case,
765 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300766 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300767 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300768 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300769 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
770 "not ready: not enough video buffers.\n");
771 return 0;
772 }
773
Philipp Zabel14604e32014-07-11 06:36:22 -0300774 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300775 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
776 "not ready: not enough video capture buffers.\n");
777 return 0;
778 }
779
Philipp Zabel84e23652014-07-11 06:36:34 -0300780 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -0300781 ((ctx->inst_type == CODA_INST_DECODER) &&
782 (coda_get_bitstream_payload(ctx) < 512) &&
783 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
784 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
785 "%d: not ready: not enough bitstream data.\n",
786 ctx->idx);
787 return 0;
788 }
789
Philipp Zabel3e748262013-05-23 10:43:01 -0300790 if (ctx->aborting) {
791 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
792 "not ready: aborting\n");
793 return 0;
794 }
795
Javier Martin186b2502012-07-26 05:53:35 -0300796 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
797 "job ready\n");
798 return 1;
799}
800
801static void coda_job_abort(void *priv)
802{
803 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -0300804
805 ctx->aborting = 1;
806
807 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
808 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -0300809}
810
811static void coda_lock(void *m2m_priv)
812{
813 struct coda_ctx *ctx = m2m_priv;
814 struct coda_dev *pcdev = ctx->dev;
815 mutex_lock(&pcdev->dev_mutex);
816}
817
818static void coda_unlock(void *m2m_priv)
819{
820 struct coda_ctx *ctx = m2m_priv;
821 struct coda_dev *pcdev = ctx->dev;
822 mutex_unlock(&pcdev->dev_mutex);
823}
824
Philipp Zabel814c3762014-07-18 07:22:45 -0300825static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -0300826 .device_run = coda_device_run,
827 .job_ready = coda_job_ready,
828 .job_abort = coda_job_abort,
829 .lock = coda_lock,
830 .unlock = coda_unlock,
831};
832
Philipp Zabel89548442014-07-11 06:36:17 -0300833static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
834{
835 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
836 int luma_map, chro_map, i;
837
838 memset(tiled_map, 0, sizeof(*tiled_map));
839
840 luma_map = 64;
841 chro_map = 64;
842 tiled_map->map_type = tiled_map_type;
843 for (i = 0; i < 16; i++)
844 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
845 for (i = 0; i < 4; i++)
846 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
847 for (i = 0; i < 16; i++)
848 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
849
850 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
851 tiled_map->xy2rbc_config = 0;
852 } else {
853 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
854 tiled_map_type);
855 return;
856 }
857}
858
Javier Martin186b2502012-07-26 05:53:35 -0300859static void set_default_params(struct coda_ctx *ctx)
860{
Philipp Zabel121cacf2014-07-18 07:22:42 -0300861 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300862 int max_w;
863 int max_h;
864
Philipp Zabel121cacf2014-07-18 07:22:42 -0300865 if (ctx->inst_type == CODA_INST_ENCODER) {
866 src_fourcc = V4L2_PIX_FMT_YUV420;
867 dst_fourcc = V4L2_PIX_FMT_H264;
868 } else {
869 src_fourcc = V4L2_PIX_FMT_H264;
870 dst_fourcc = V4L2_PIX_FMT_YUV420;
871 }
872 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300873 max_w = ctx->codec->max_w;
874 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300875
Philipp Zabel121cacf2014-07-18 07:22:42 -0300876 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -0300877 ctx->colorspace = V4L2_COLORSPACE_REC709;
878 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300879 ctx->aborting = 0;
880
881 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300882 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
883 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
884 ctx->q_data[V4L2_M2M_SRC].width = max_w;
885 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300886 ctx->q_data[V4L2_M2M_DST].width = max_w;
887 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -0300888 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
889 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
890 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
891 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
892 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
893 } else {
894 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
895 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
896 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
897 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
898 }
Philipp Zabel52c41672014-07-11 06:36:19 -0300899 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
900 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
901 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
902 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -0300903
904 if (ctx->dev->devtype->product == CODA_960)
905 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -0300906}
907
908/*
909 * Queue operations
910 */
911static int coda_queue_setup(struct vb2_queue *vq,
912 const struct v4l2_format *fmt,
913 unsigned int *nbuffers, unsigned int *nplanes,
914 unsigned int sizes[], void *alloc_ctxs[])
915{
916 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300917 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300918 unsigned int size;
919
Philipp Zabele34db062012-08-29 08:22:00 -0300920 q_data = get_q_data(ctx, vq->type);
921 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300922
923 *nplanes = 1;
924 sizes[0] = size;
925
926 alloc_ctxs[0] = ctx->dev->alloc_ctx;
927
928 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
929 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
930
931 return 0;
932}
933
934static int coda_buf_prepare(struct vb2_buffer *vb)
935{
936 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
937 struct coda_q_data *q_data;
938
939 q_data = get_q_data(ctx, vb->vb2_queue->type);
940
941 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
942 v4l2_warn(&ctx->dev->v4l2_dev,
943 "%s data will not fit into plane (%lu < %lu)\n",
944 __func__, vb2_plane_size(vb, 0),
945 (long)q_data->sizeimage);
946 return -EINVAL;
947 }
948
Javier Martin186b2502012-07-26 05:53:35 -0300949 return 0;
950}
951
952static void coda_buf_queue(struct vb2_buffer *vb)
953{
954 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300955 struct coda_q_data *q_data;
956
957 q_data = get_q_data(ctx, vb->vb2_queue->type);
958
959 /*
960 * In the decoder case, immediately try to copy the buffer into the
961 * bitstream ringbuffer and mark it as ready to be dequeued.
962 */
963 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
964 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
965 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300966 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -0300967 * the stream end
968 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300969 if (vb2_get_plane_payload(vb, 0) == 0)
970 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300971 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -0300972 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -0300973 if (vb2_is_streaming(vb->vb2_queue))
974 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300975 mutex_unlock(&ctx->bitstream_mutex);
976 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -0300977 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300978 }
Javier Martin186b2502012-07-26 05:53:35 -0300979}
980
Philipp Zabel79924ca2014-07-23 12:28:45 -0300981int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
982 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300983{
984 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
985 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -0300986 if (!buf->vaddr) {
987 v4l2_err(&dev->v4l2_dev,
988 "Failed to allocate %s buffer of size %u\n",
989 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300990 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -0300991 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300992
993 buf->size = size;
994
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300995 if (name && parent) {
996 buf->blob.data = buf->vaddr;
997 buf->blob.size = size;
998 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
999 if (!buf->dentry)
1000 dev_warn(&dev->plat_dev->dev,
1001 "failed to create debugfs entry %s\n", name);
1002 }
1003
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001004 return 0;
1005}
1006
Philipp Zabel79924ca2014-07-23 12:28:45 -03001007void coda_free_aux_buf(struct coda_dev *dev,
1008 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001009{
1010 if (buf->vaddr) {
1011 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1012 buf->vaddr, buf->paddr);
1013 buf->vaddr = NULL;
1014 buf->size = 0;
1015 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001016 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001017}
1018
Javier Martin186b2502012-07-26 05:53:35 -03001019static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1020{
1021 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1022 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001023 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001024 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001025 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001026 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001027
Philipp Zabelb96904e2013-05-23 10:42:58 -03001028 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001029 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1030 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03001031 /* copy the buffers that where queued before streamon */
1032 mutex_lock(&ctx->bitstream_mutex);
1033 coda_fill_bitstream(ctx);
1034 mutex_unlock(&ctx->bitstream_mutex);
1035
Philipp Zabelb9063522014-08-05 14:00:10 -03001036 if (coda_get_bitstream_payload(ctx) < 512) {
1037 ret = -EINVAL;
1038 goto err;
1039 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001040 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001041 if (count < 1) {
1042 ret = -EINVAL;
1043 goto err;
1044 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001045 }
1046
1047 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001048 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001049 if (count < 1) {
1050 ret = -EINVAL;
1051 goto err;
1052 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001053
1054 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001055 }
Javier Martin186b2502012-07-26 05:53:35 -03001056
Philipp Zabelb96904e2013-05-23 10:42:58 -03001057 /* Don't start the coda unless both queues are on */
1058 if (!(ctx->streamon_out & ctx->streamon_cap))
1059 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001060
Philipp Zabeleb107512013-09-30 10:34:45 -03001061 /* Allow decoder device_run with no new buffers queued */
1062 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03001063 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001064
Philipp Zabelb96904e2013-05-23 10:42:58 -03001065 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001066 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001067 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001068
Philipp Zabelb96904e2013-05-23 10:42:58 -03001069 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1070 q_data_dst->fourcc);
1071 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001072 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001073 ret = -EINVAL;
1074 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001075 }
1076
Philipp Zabela1192a12014-07-23 12:28:40 -03001077 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001078 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001079 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001080 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001081 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001082 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001083 }
1084
Philipp Zabel89548442014-07-11 06:36:17 -03001085 ctx->initialized = 1;
1086 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001087
1088err:
1089 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1090 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1091 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1092 } else {
1093 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1094 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1095 }
1096 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001097}
1098
Hans Verkuile37559b2014-04-17 02:47:21 -03001099static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001100{
1101 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001102 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001103 struct vb2_buffer *buf;
Javier Martin186b2502012-07-26 05:53:35 -03001104
1105 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001106 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001107 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001108 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001109
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001110 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001111
Philipp Zabel918c66f2013-06-27 06:59:01 -03001112 ctx->isequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001113
1114 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1115 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001116 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001117 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001118 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001119 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001120
1121 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001122 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001123
1124 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1125 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001126 }
1127
Philipp Zabel918c66f2013-06-27 06:59:01 -03001128 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001129 struct coda_timestamp *ts;
1130
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001131 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001132 while (!list_empty(&ctx->timestamp_list)) {
1133 ts = list_first_entry(&ctx->timestamp_list,
1134 struct coda_timestamp, list);
1135 list_del(&ts->list);
1136 kfree(ts);
1137 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001138 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001139 kfifo_init(&ctx->bitstream_fifo,
1140 ctx->bitstream.vaddr, ctx->bitstream.size);
1141 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001142 }
Javier Martin186b2502012-07-26 05:53:35 -03001143}
1144
Philipp Zabel121cacf2014-07-18 07:22:42 -03001145static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001146 .queue_setup = coda_queue_setup,
1147 .buf_prepare = coda_buf_prepare,
1148 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001149 .start_streaming = coda_start_streaming,
1150 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001151 .wait_prepare = vb2_ops_wait_prepare,
1152 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001153};
1154
1155static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1156{
1157 struct coda_ctx *ctx =
1158 container_of(ctrl->handler, struct coda_ctx, ctrls);
1159
1160 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1161 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1162
1163 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001164 case V4L2_CID_HFLIP:
1165 if (ctrl->val)
1166 ctx->params.rot_mode |= CODA_MIR_HOR;
1167 else
1168 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1169 break;
1170 case V4L2_CID_VFLIP:
1171 if (ctrl->val)
1172 ctx->params.rot_mode |= CODA_MIR_VER;
1173 else
1174 ctx->params.rot_mode &= ~CODA_MIR_VER;
1175 break;
Javier Martin186b2502012-07-26 05:53:35 -03001176 case V4L2_CID_MPEG_VIDEO_BITRATE:
1177 ctx->params.bitrate = ctrl->val / 1000;
1178 break;
1179 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1180 ctx->params.gop_size = ctrl->val;
1181 break;
1182 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1183 ctx->params.h264_intra_qp = ctrl->val;
1184 break;
1185 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1186 ctx->params.h264_inter_qp = ctrl->val;
1187 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001188 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1189 ctx->params.h264_min_qp = ctrl->val;
1190 break;
1191 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1192 ctx->params.h264_max_qp = ctrl->val;
1193 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001194 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1195 ctx->params.h264_deblk_alpha = ctrl->val;
1196 break;
1197 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1198 ctx->params.h264_deblk_beta = ctrl->val;
1199 break;
1200 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1201 ctx->params.h264_deblk_enabled = (ctrl->val ==
1202 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1203 break;
Javier Martin186b2502012-07-26 05:53:35 -03001204 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1205 ctx->params.mpeg4_intra_qp = ctrl->val;
1206 break;
1207 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1208 ctx->params.mpeg4_inter_qp = ctrl->val;
1209 break;
1210 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1211 ctx->params.slice_mode = ctrl->val;
1212 break;
1213 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1214 ctx->params.slice_max_mb = ctrl->val;
1215 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001216 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1217 ctx->params.slice_max_bits = ctrl->val * 8;
1218 break;
Javier Martin186b2502012-07-26 05:53:35 -03001219 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1220 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001221 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1222 ctx->params.intra_refresh = ctrl->val;
1223 break;
Javier Martin186b2502012-07-26 05:53:35 -03001224 default:
1225 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1226 "Invalid control, id=%d, val=%d\n",
1227 ctrl->id, ctrl->val);
1228 return -EINVAL;
1229 }
1230
1231 return 0;
1232}
1233
Philipp Zabel814c3762014-07-18 07:22:45 -03001234static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001235 .s_ctrl = coda_s_ctrl,
1236};
1237
1238static int coda_ctrls_setup(struct coda_ctx *ctx)
1239{
1240 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1241
1242 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001243 V4L2_CID_HFLIP, 0, 1, 1, 0);
1244 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1245 V4L2_CID_VFLIP, 0, 1, 1, 0);
1246 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001247 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1248 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1249 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1250 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001251 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001252 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001253 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001254 if (ctx->dev->devtype->product != CODA_960) {
1255 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1256 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1257 }
1258 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1259 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001260 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001261 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1262 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1263 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1264 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1265 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1266 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1267 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1268 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001269 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1270 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1271 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1272 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1273 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001274 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1275 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001276 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1277 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001278 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1279 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001280 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1281 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1282 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1283 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1284 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001285 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1286 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001287
1288 if (ctx->ctrls.error) {
1289 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1290 ctx->ctrls.error);
1291 return -EINVAL;
1292 }
1293
1294 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1295}
1296
Philipp Zabel121cacf2014-07-18 07:22:42 -03001297static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001298{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001299 vq->drv_priv = ctx;
1300 vq->ops = &coda_qops;
1301 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1302 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1303 vq->lock = &ctx->dev->dev_mutex;
1304
1305 return vb2_queue_init(vq);
1306}
1307
Philipp Zabel79924ca2014-07-23 12:28:45 -03001308int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1309 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001310{
Javier Martin186b2502012-07-26 05:53:35 -03001311 int ret;
1312
Javier Martin186b2502012-07-26 05:53:35 -03001313 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001314 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001315 src_vq->mem_ops = &vb2_dma_contig_memops;
1316
Philipp Zabel121cacf2014-07-18 07:22:42 -03001317 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001318 if (ret)
1319 return ret;
1320
Javier Martin186b2502012-07-26 05:53:35 -03001321 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001322 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001323 dst_vq->mem_ops = &vb2_dma_contig_memops;
1324
Philipp Zabel121cacf2014-07-18 07:22:42 -03001325 return coda_queue_init(priv, dst_vq);
1326}
1327
Philipp Zabel79924ca2014-07-23 12:28:45 -03001328int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1329 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001330{
1331 int ret;
1332
1333 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1334 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1335 src_vq->mem_ops = &vb2_dma_contig_memops;
1336
1337 ret = coda_queue_init(priv, src_vq);
1338 if (ret)
1339 return ret;
1340
1341 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1342 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1343 dst_vq->mem_ops = &vb2_dma_contig_memops;
1344
1345 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001346}
1347
Philipp Zabele11f3e62012-07-25 09:16:58 -03001348static int coda_next_free_instance(struct coda_dev *dev)
1349{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001350 int idx = ffz(dev->instance_mask);
1351
1352 if ((idx < 0) ||
1353 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1354 return -EBUSY;
1355
1356 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001357}
1358
Philipp Zabela1192a12014-07-23 12:28:40 -03001359static int coda_open(struct file *file, enum coda_inst_type inst_type,
1360 const struct coda_context_ops *ctx_ops)
Javier Martin186b2502012-07-26 05:53:35 -03001361{
1362 struct coda_dev *dev = video_drvdata(file);
1363 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001364 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001365 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001366 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001367
Javier Martin186b2502012-07-26 05:53:35 -03001368 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1369 if (!ctx)
1370 return -ENOMEM;
1371
Fabio Estevamf82bc202013-08-21 11:14:16 -03001372 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001373 if (idx < 0) {
1374 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001375 goto err_coda_max;
1376 }
1377 set_bit(idx, &dev->instance_mask);
1378
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001379 name = kasprintf(GFP_KERNEL, "context%d", idx);
1380 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1381 kfree(name);
1382
Philipp Zabel121cacf2014-07-18 07:22:42 -03001383 ctx->inst_type = inst_type;
Philipp Zabela1192a12014-07-23 12:28:40 -03001384 ctx->ops = ctx_ops;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001385 init_completion(&ctx->completion);
1386 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabela1192a12014-07-23 12:28:40 -03001387 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001388 v4l2_fh_init(&ctx->fh, video_devdata(file));
1389 file->private_data = &ctx->fh;
1390 v4l2_fh_add(&ctx->fh);
1391 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001392 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001393 switch (dev->devtype->product) {
1394 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03001395 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001396 ctx->reg_idx = 0;
1397 break;
1398 default:
1399 ctx->reg_idx = idx;
1400 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001401
Philipp Zabel1e172732014-07-11 06:36:23 -03001402 /* Power up and upload firmware if necessary */
1403 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1404 if (ret < 0) {
1405 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1406 goto err_pm_get;
1407 }
1408
Fabio Estevam79830db2013-08-21 11:14:17 -03001409 ret = clk_prepare_enable(dev->clk_per);
1410 if (ret)
1411 goto err_clk_per;
1412
1413 ret = clk_prepare_enable(dev->clk_ahb);
1414 if (ret)
1415 goto err_clk_ahb;
1416
Javier Martin186b2502012-07-26 05:53:35 -03001417 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001418 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1419 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001420 if (IS_ERR(ctx->fh.m2m_ctx)) {
1421 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001422
1423 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1424 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001425 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001426 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001427
Javier Martin186b2502012-07-26 05:53:35 -03001428 ret = coda_ctrls_setup(ctx);
1429 if (ret) {
1430 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001431 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001432 }
1433
1434 ctx->fh.ctrl_handler = &ctx->ctrls;
1435
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001436 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
1437 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001438 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001439 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001440 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03001441 }
1442
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001443 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1444 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1445 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1446 if (!ctx->bitstream.vaddr) {
1447 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
1448 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001449 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001450 }
1451 kfifo_init(&ctx->bitstream_fifo,
1452 ctx->bitstream.vaddr, ctx->bitstream.size);
1453 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001454 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001455 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001456
Javier Martin186b2502012-07-26 05:53:35 -03001457 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001458 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001459 coda_unlock(ctx);
1460
Javier Martin186b2502012-07-26 05:53:35 -03001461 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1462 ctx->idx, ctx);
1463
1464 return 0;
1465
Fabio Estevamf82bc202013-08-21 11:14:16 -03001466err_dma_writecombine:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001467 if (ctx->dev->devtype->product == CODA_DX6)
1468 coda_free_aux_buf(dev, &ctx->workbuf);
1469 coda_free_aux_buf(dev, &ctx->parabuf);
1470err_dma_alloc:
1471 v4l2_ctrl_handler_free(&ctx->ctrls);
1472err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001473 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001474err_ctx_init:
1475 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001476err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001477 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001478err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001479 pm_runtime_put_sync(&dev->plat_dev->dev);
1480err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001481 v4l2_fh_del(&ctx->fh);
1482 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001483 clear_bit(ctx->idx, &dev->instance_mask);
1484err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001485 kfree(ctx);
1486 return ret;
1487}
1488
Philipp Zabel121cacf2014-07-18 07:22:42 -03001489static int coda_encoder_open(struct file *file)
1490{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001491 return coda_open(file, CODA_INST_ENCODER, &coda_bit_encode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001492}
1493
1494static int coda_decoder_open(struct file *file)
1495{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001496 return coda_open(file, CODA_INST_DECODER, &coda_bit_decode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001497}
1498
Javier Martin186b2502012-07-26 05:53:35 -03001499static int coda_release(struct file *file)
1500{
1501 struct coda_dev *dev = video_drvdata(file);
1502 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1503
1504 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1505 ctx);
1506
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001507 debugfs_remove_recursive(ctx->debugfs_entry);
1508
Philipp Zabel918c66f2013-06-27 06:59:01 -03001509 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001510 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001511
1512 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03001513 if (ctx->initialized) {
1514 queue_work(dev->workqueue, &ctx->seq_end_work);
1515 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001516 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001517
Javier Martin186b2502012-07-26 05:53:35 -03001518 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001519 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001520 coda_unlock(ctx);
1521
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001522 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1523 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001524 if (ctx->dev->devtype->product == CODA_DX6)
1525 coda_free_aux_buf(dev, &ctx->workbuf);
1526
1527 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03001528 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001529 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001530 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001531 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001532 v4l2_fh_del(&ctx->fh);
1533 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001534 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001535 if (ctx->ops->release)
1536 ctx->ops->release(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001537 kfree(ctx);
1538
1539 return 0;
1540}
1541
Philipp Zabel121cacf2014-07-18 07:22:42 -03001542static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001543 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03001544 .open = coda_encoder_open,
1545 .release = coda_release,
1546 .poll = v4l2_m2m_fop_poll,
1547 .unlocked_ioctl = video_ioctl2,
1548 .mmap = v4l2_m2m_fop_mmap,
1549};
1550
1551static const struct v4l2_file_operations coda_decoder_fops = {
1552 .owner = THIS_MODULE,
1553 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03001554 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001555 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001556 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001557 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001558};
1559
Philipp Zabel87048bb2012-07-02 07:03:43 -03001560static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001561{
Javier Martin186b2502012-07-26 05:53:35 -03001562 u32 data;
1563 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001564 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001565
Fabio Estevam79830db2013-08-21 11:14:17 -03001566 ret = clk_prepare_enable(dev->clk_per);
1567 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001568 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001569
1570 ret = clk_prepare_enable(dev->clk_ahb);
1571 if (ret)
1572 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001573
Philipp Zabel8f452842014-07-11 06:36:35 -03001574 if (dev->rstc)
1575 reset_control_reset(dev->rstc);
1576
Javier Martin186b2502012-07-26 05:53:35 -03001577 /*
1578 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001579 * The 16-bit chars in the code buffer are in memory access
1580 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001581 * Data in this SRAM survives a reboot.
1582 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001583 p = (u16 *)dev->codebuf.vaddr;
1584 if (dev->devtype->product == CODA_DX6) {
1585 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1586 data = CODA_DOWN_ADDRESS_SET(i) |
1587 CODA_DOWN_DATA_SET(p[i ^ 1]);
1588 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1589 }
1590 } else {
1591 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1592 data = CODA_DOWN_ADDRESS_SET(i) |
1593 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1594 3 - (i % 4)]);
1595 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1596 }
Javier Martin186b2502012-07-26 05:53:35 -03001597 }
Javier Martin186b2502012-07-26 05:53:35 -03001598
Philipp Zabel9acf7692013-05-23 10:42:56 -03001599 /* Clear registers */
1600 for (i = 0; i < 64; i++)
1601 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1602
Javier Martin186b2502012-07-26 05:53:35 -03001603 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001604 if (dev->devtype->product == CODA_960 ||
1605 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001606 coda_write(dev, dev->tempbuf.paddr,
1607 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001608 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001609 } else {
1610 coda_write(dev, dev->workbuf.paddr,
1611 CODA_REG_BIT_WORK_BUF_ADDR);
1612 }
Javier Martin186b2502012-07-26 05:53:35 -03001613 coda_write(dev, dev->codebuf.paddr,
1614 CODA_REG_BIT_CODE_BUF_ADDR);
1615 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1616
1617 /* Set default values */
1618 switch (dev->devtype->product) {
1619 case CODA_DX6:
1620 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1621 break;
1622 default:
1623 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1624 }
Philipp Zabel89548442014-07-11 06:36:17 -03001625 if (dev->devtype->product == CODA_960)
1626 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1627 else
1628 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001629
1630 if (dev->devtype->product != CODA_DX6)
1631 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1632
Javier Martin186b2502012-07-26 05:53:35 -03001633 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1634 CODA_REG_BIT_INT_ENABLE);
1635
1636 /* Reset VPU and start processor */
1637 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1638 data |= CODA_REG_RESET_ENABLE;
1639 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1640 udelay(10);
1641 data &= ~CODA_REG_RESET_ENABLE;
1642 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1643 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1644
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001645 clk_disable_unprepare(dev->clk_ahb);
1646 clk_disable_unprepare(dev->clk_per);
1647
1648 return 0;
1649
1650err_clk_ahb:
1651 clk_disable_unprepare(dev->clk_per);
1652err_clk_per:
1653 return ret;
1654}
1655
Philipp Zabel121cacf2014-07-18 07:22:42 -03001656static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
1657{
1658 vfd->release = video_device_release_empty,
1659 vfd->lock = &dev->dev_mutex;
1660 vfd->v4l2_dev = &dev->v4l2_dev;
1661 vfd->vfl_dir = VFL_DIR_M2M;
1662 video_set_drvdata(vfd, dev);
1663
1664 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1665}
1666
Javier Martin186b2502012-07-26 05:53:35 -03001667static void coda_fw_callback(const struct firmware *fw, void *context)
1668{
1669 struct coda_dev *dev = context;
1670 struct platform_device *pdev = dev->plat_dev;
1671 int ret;
1672
1673 if (!fw) {
1674 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1675 return;
1676 }
1677
1678 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001679 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1680 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001681 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001682 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1683 return;
1684 }
1685
Philipp Zabel87048bb2012-07-02 07:03:43 -03001686 /* Copy the whole firmware image to the code buffer */
1687 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1688 release_firmware(fw);
1689
Philipp Zabel1e172732014-07-11 06:36:23 -03001690 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
1691 /*
1692 * Enabling power temporarily will cause coda_hw_init to be
1693 * called via coda_runtime_resume by the pm domain.
1694 */
1695 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1696 if (ret < 0) {
1697 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
1698 ret);
1699 return;
1700 }
1701
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001702 ret = coda_check_firmware(dev);
1703 if (ret < 0)
1704 return;
1705
Philipp Zabel1e172732014-07-11 06:36:23 -03001706 pm_runtime_put_sync(&dev->plat_dev->dev);
1707 } else {
1708 /*
1709 * If runtime pm is disabled or pm_domain is not set,
1710 * initialize once manually.
1711 */
1712 ret = coda_hw_init(dev);
1713 if (ret < 0) {
1714 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1715 return;
1716 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001717
1718 ret = coda_check_firmware(dev);
1719 if (ret < 0)
1720 return;
Javier Martin186b2502012-07-26 05:53:35 -03001721 }
1722
Javier Martin186b2502012-07-26 05:53:35 -03001723 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1724 if (IS_ERR(dev->alloc_ctx)) {
1725 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1726 return;
1727 }
1728
1729 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1730 if (IS_ERR(dev->m2m_dev)) {
1731 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1732 goto rel_ctx;
1733 }
1734
Philipp Zabel121cacf2014-07-18 07:22:42 -03001735 dev->vfd[0].fops = &coda_encoder_fops,
1736 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
1737 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
1738 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03001739 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03001740 v4l2_err(&dev->v4l2_dev,
1741 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03001742 goto rel_m2m;
1743 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03001744
1745 dev->vfd[1].fops = &coda_decoder_fops,
1746 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
1747 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
1748 ret = coda_register_device(dev, &dev->vfd[1]);
1749 if (ret) {
1750 v4l2_err(&dev->v4l2_dev,
1751 "Failed to register decoder video device\n");
1752 goto rel_m2m;
1753 }
1754
1755 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1756 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03001757
1758 return;
1759
1760rel_m2m:
1761 v4l2_m2m_release(dev->m2m_dev);
1762rel_ctx:
1763 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1764}
1765
1766static int coda_firmware_request(struct coda_dev *dev)
1767{
1768 char *fw = dev->devtype->firmware;
1769
1770 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1771 coda_product_name(dev->devtype->product));
1772
1773 return request_firmware_nowait(THIS_MODULE, true,
1774 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1775}
1776
1777enum coda_platform {
1778 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001779 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03001780 CODA_IMX6Q,
1781 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03001782};
1783
Emil Goodec06d8752012-08-14 17:44:42 -03001784static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001785 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001786 .firmware = "v4l-codadx6-imx27.bin",
1787 .product = CODA_DX6,
1788 .codecs = codadx6_codecs,
1789 .num_codecs = ARRAY_SIZE(codadx6_codecs),
1790 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001791 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03001792 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001793 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001794 .firmware = "v4l-coda7541-imx53.bin",
1795 .product = CODA_7541,
1796 .codecs = coda7_codecs,
1797 .num_codecs = ARRAY_SIZE(coda7_codecs),
1798 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001799 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001800 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001801 },
Philipp Zabel89548442014-07-11 06:36:17 -03001802 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001803 .firmware = "v4l-coda960-imx6q.bin",
1804 .product = CODA_960,
1805 .codecs = coda9_codecs,
1806 .num_codecs = ARRAY_SIZE(coda9_codecs),
1807 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001808 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001809 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03001810 },
1811 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001812 .firmware = "v4l-coda960-imx6dl.bin",
1813 .product = CODA_960,
1814 .codecs = coda9_codecs,
1815 .num_codecs = ARRAY_SIZE(coda9_codecs),
1816 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001817 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001818 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03001819 },
Javier Martin186b2502012-07-26 05:53:35 -03001820};
1821
1822static struct platform_device_id coda_platform_ids[] = {
1823 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001824 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001825 { /* sentinel */ }
1826};
1827MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1828
1829#ifdef CONFIG_OF
1830static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03001831 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001832 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03001833 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
1834 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03001835 { /* sentinel */ }
1836};
1837MODULE_DEVICE_TABLE(of, coda_dt_ids);
1838#endif
1839
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001840static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001841{
1842 const struct of_device_id *of_id =
1843 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1844 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001845 struct coda_platform_data *pdata = pdev->dev.platform_data;
1846 struct device_node *np = pdev->dev.of_node;
1847 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001848 struct coda_dev *dev;
1849 struct resource *res;
1850 int ret, irq;
1851
1852 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1853 if (!dev) {
1854 dev_err(&pdev->dev, "Not enough memory for %s\n",
1855 CODA_NAME);
1856 return -ENOMEM;
1857 }
1858
1859 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001860 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001861
1862 dev->plat_dev = pdev;
1863 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1864 if (IS_ERR(dev->clk_per)) {
1865 dev_err(&pdev->dev, "Could not get per clock\n");
1866 return PTR_ERR(dev->clk_per);
1867 }
1868
1869 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1870 if (IS_ERR(dev->clk_ahb)) {
1871 dev_err(&pdev->dev, "Could not get ahb clock\n");
1872 return PTR_ERR(dev->clk_ahb);
1873 }
1874
1875 /* Get memory for physical registers */
1876 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03001877 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1878 if (IS_ERR(dev->regs_base))
1879 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03001880
1881 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03001882 irq = platform_get_irq_byname(pdev, "bit");
1883 if (irq < 0)
1884 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001885 if (irq < 0) {
1886 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03001887 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03001888 }
1889
Fabio Estevam08c8d142014-06-04 15:46:24 -03001890 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
1891 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
1892 if (ret < 0) {
1893 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
1894 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001895 }
1896
Philipp Zabelee27aa92014-07-24 16:50:03 -03001897 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03001898 if (IS_ERR(dev->rstc)) {
1899 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03001900 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03001901 dev->rstc = NULL;
1902 } else {
1903 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
1904 return ret;
1905 }
1906 }
1907
Philipp Zabel657eee72013-04-29 16:17:14 -07001908 /* Get IRAM pool from device tree or platform data */
1909 pool = of_get_named_gen_pool(np, "iram", 0);
1910 if (!pool && pdata)
1911 pool = dev_get_gen_pool(pdata->iram_dev);
1912 if (!pool) {
1913 dev_err(&pdev->dev, "iram pool not available\n");
1914 return -ENOMEM;
1915 }
1916 dev->iram_pool = pool;
1917
Javier Martin186b2502012-07-26 05:53:35 -03001918 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1919 if (ret)
1920 return ret;
1921
1922 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001923 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001924
1925 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
1926
1927 if (of_id) {
1928 dev->devtype = of_id->data;
1929 } else if (pdev_id) {
1930 dev->devtype = &coda_devdata[pdev_id->driver_data];
1931 } else {
1932 v4l2_device_unregister(&dev->v4l2_dev);
1933 return -EINVAL;
1934 }
1935
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001936 dev->debugfs_root = debugfs_create_dir("coda", NULL);
1937 if (!dev->debugfs_root)
1938 dev_warn(&pdev->dev, "failed to create debugfs root\n");
1939
Javier Martin186b2502012-07-26 05:53:35 -03001940 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001941 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001942 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001943 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001944 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001945 if (ret < 0) {
1946 dev_err(&pdev->dev, "failed to allocate work buffer\n");
1947 v4l2_device_unregister(&dev->v4l2_dev);
1948 return ret;
1949 }
Javier Martin186b2502012-07-26 05:53:35 -03001950 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001951
1952 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001953 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001954 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001955 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001956 if (ret < 0) {
1957 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
1958 v4l2_device_unregister(&dev->v4l2_dev);
1959 return ret;
1960 }
Javier Martin186b2502012-07-26 05:53:35 -03001961 }
1962
Philipp Zabel401e9722014-07-11 06:36:43 -03001963 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001964 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
1965 &dev->iram.paddr);
1966 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03001967 dev_warn(&pdev->dev, "unable to alloc iram\n");
1968 } else {
1969 dev->iram.blob.data = dev->iram.vaddr;
1970 dev->iram.blob.size = dev->iram.size;
1971 dev->iram.dentry = debugfs_create_blob("iram", 0644,
1972 dev->debugfs_root,
1973 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03001974 }
1975
Philipp Zabel32b6f202014-07-11 06:36:20 -03001976 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1977 if (!dev->workqueue) {
1978 dev_err(&pdev->dev, "unable to alloc workqueue\n");
1979 return -ENOMEM;
1980 }
1981
Javier Martin186b2502012-07-26 05:53:35 -03001982 platform_set_drvdata(pdev, dev);
1983
Philipp Zabel1e172732014-07-11 06:36:23 -03001984 pm_runtime_enable(&pdev->dev);
1985
Javier Martin186b2502012-07-26 05:53:35 -03001986 return coda_firmware_request(dev);
1987}
1988
1989static int coda_remove(struct platform_device *pdev)
1990{
1991 struct coda_dev *dev = platform_get_drvdata(pdev);
1992
Philipp Zabel121cacf2014-07-18 07:22:42 -03001993 video_unregister_device(&dev->vfd[0]);
1994 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03001995 if (dev->m2m_dev)
1996 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03001997 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001998 if (dev->alloc_ctx)
1999 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2000 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002001 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002002 if (dev->iram.vaddr)
2003 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2004 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002005 coda_free_aux_buf(dev, &dev->codebuf);
2006 coda_free_aux_buf(dev, &dev->tempbuf);
2007 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002008 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002009 return 0;
2010}
2011
Philipp Zabel1e172732014-07-11 06:36:23 -03002012#ifdef CONFIG_PM_RUNTIME
2013static int coda_runtime_resume(struct device *dev)
2014{
2015 struct coda_dev *cdev = dev_get_drvdata(dev);
2016 int ret = 0;
2017
Philipp Zabel65919e62014-07-18 07:22:36 -03002018 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002019 ret = coda_hw_init(cdev);
2020 if (ret)
2021 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2022 }
2023
2024 return ret;
2025}
2026#endif
2027
2028static const struct dev_pm_ops coda_pm_ops = {
2029 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2030};
2031
Javier Martin186b2502012-07-26 05:53:35 -03002032static struct platform_driver coda_driver = {
2033 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002034 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002035 .driver = {
2036 .name = CODA_NAME,
2037 .owner = THIS_MODULE,
2038 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002039 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002040 },
2041 .id_table = coda_platform_ids,
2042};
2043
2044module_platform_driver(coda_driver);
2045
2046MODULE_LICENSE("GPL");
2047MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2048MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");