blob: 6020e336c99852e8a0afb946f18239d73c6a624c [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);
Javier Martin186b2502012-07-26 05:53:35 -030061MODULE_PARM_DESC(coda_debug, "Debug level (0-1)");
62
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{
70 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
71 "%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);
79 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
80 "%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);
499 int ret;
500
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300501 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300502 if (ret)
503 return ret;
504
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300505 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300506 if (ret)
507 ctx->colorspace = f->fmt.pix.colorspace;
508
509 return ret;
510}
511
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300512static int coda_qbuf(struct file *file, void *priv,
513 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300514{
515 struct coda_ctx *ctx = fh_to_ctx(priv);
516
Philipp Zabel14604e32014-07-11 06:36:22 -0300517 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300518}
519
Philipp Zabel918c66f2013-06-27 06:59:01 -0300520static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
521 struct v4l2_buffer *buf)
522{
523 struct vb2_queue *src_vq;
524
Philipp Zabel14604e32014-07-11 06:36:22 -0300525 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300526
527 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
528 (buf->sequence == (ctx->qsequence - 1)));
529}
530
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300531static int coda_dqbuf(struct file *file, void *priv,
532 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300533{
534 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300535 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300536
Philipp Zabel14604e32014-07-11 06:36:22 -0300537 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300538
539 /* If this is the last capture buffer, emit an end-of-stream event */
540 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
541 coda_buf_is_end_of_stream(ctx, buf)) {
542 const struct v4l2_event eos_event = {
543 .type = V4L2_EVENT_EOS
544 };
545
546 v4l2_event_queue_fh(&ctx->fh, &eos_event);
547 }
548
549 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300550}
551
Philipp Zabel52c41672014-07-11 06:36:19 -0300552static int coda_g_selection(struct file *file, void *fh,
553 struct v4l2_selection *s)
554{
555 struct coda_ctx *ctx = fh_to_ctx(fh);
556 struct coda_q_data *q_data;
557 struct v4l2_rect r, *rsel;
558
559 q_data = get_q_data(ctx, s->type);
560 if (!q_data)
561 return -EINVAL;
562
563 r.left = 0;
564 r.top = 0;
565 r.width = q_data->width;
566 r.height = q_data->height;
567 rsel = &q_data->rect;
568
569 switch (s->target) {
570 case V4L2_SEL_TGT_CROP_DEFAULT:
571 case V4L2_SEL_TGT_CROP_BOUNDS:
572 rsel = &r;
573 /* fallthrough */
574 case V4L2_SEL_TGT_CROP:
575 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
576 return -EINVAL;
577 break;
578 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
579 case V4L2_SEL_TGT_COMPOSE_PADDED:
580 rsel = &r;
581 /* fallthrough */
582 case V4L2_SEL_TGT_COMPOSE:
583 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
584 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
585 return -EINVAL;
586 break;
587 default:
588 return -EINVAL;
589 }
590
591 s->r = *rsel;
592
593 return 0;
594}
595
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300596static int coda_try_decoder_cmd(struct file *file, void *fh,
597 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300598{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300599 if (dc->cmd != V4L2_DEC_CMD_STOP)
600 return -EINVAL;
601
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300602 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300603 return -EINVAL;
604
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300605 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300606 return -EINVAL;
607
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300608 return 0;
609}
610
611static int coda_decoder_cmd(struct file *file, void *fh,
612 struct v4l2_decoder_cmd *dc)
613{
614 struct coda_ctx *ctx = fh_to_ctx(fh);
615 int ret;
616
617 ret = coda_try_decoder_cmd(file, fh, dc);
618 if (ret < 0)
619 return ret;
620
621 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300622 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300623 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300624
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300625 /* Set the stream-end flag on this context */
626 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300627 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300628 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300629
Philipp Zabel918c66f2013-06-27 06:59:01 -0300630 return 0;
631}
632
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300633static int coda_subscribe_event(struct v4l2_fh *fh,
634 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300635{
636 switch (sub->type) {
637 case V4L2_EVENT_EOS:
638 return v4l2_event_subscribe(fh, sub, 0, NULL);
639 default:
640 return v4l2_ctrl_subscribe_event(fh, sub);
641 }
Javier Martin186b2502012-07-26 05:53:35 -0300642}
643
644static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300645 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300646
Philipp Zabel22e244b2014-07-18 07:22:43 -0300647 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300648 .vidioc_g_fmt_vid_cap = coda_g_fmt,
649 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
650 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300651
Philipp Zabel22e244b2014-07-18 07:22:43 -0300652 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300653 .vidioc_g_fmt_vid_out = coda_g_fmt,
654 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
655 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300656
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300657 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
658 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300659
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300660 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300661 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300662 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300663 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300664
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300665 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
666 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300667
Philipp Zabel52c41672014-07-11 06:36:19 -0300668 .vidioc_g_selection = coda_g_selection,
669
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300670 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300671 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300672
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300673 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300674 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300675};
676
Philipp Zabel79924ca2014-07-23 12:28:45 -0300677void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300678{
679 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
680 struct coda_dev *dev = ctx->dev;
681 int i;
682
683 for (i = 0; i < 16; i++)
684 coda_write(dev, tiled_map->xy2ca_map[i],
685 CODA9_GDI_XY2_CAS_0 + 4 * i);
686 for (i = 0; i < 4; i++)
687 coda_write(dev, tiled_map->xy2ba_map[i],
688 CODA9_GDI_XY2_BA_0 + 4 * i);
689 for (i = 0; i < 16; i++)
690 coda_write(dev, tiled_map->xy2ra_map[i],
691 CODA9_GDI_XY2_RAS_0 + 4 * i);
692 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
693 for (i = 0; i < 32; i++)
694 coda_write(dev, tiled_map->rbc2axi_map[i],
695 CODA9_GDI_RBC2_AXI_0 + 4 * i);
696}
697
Javier Martin186b2502012-07-26 05:53:35 -0300698/*
699 * Mem-to-mem operations.
700 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300701
702static void coda_device_run(void *m2m_priv)
703{
704 struct coda_ctx *ctx = m2m_priv;
705 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300706
707 queue_work(dev->workqueue, &ctx->pic_run_work);
708}
709
Philipp Zabel32b6f202014-07-11 06:36:20 -0300710static void coda_pic_run_work(struct work_struct *work)
711{
712 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
713 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300714 int ret;
715
716 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300717 mutex_lock(&dev->coda_mutex);
718
Philipp Zabela1192a12014-07-23 12:28:40 -0300719 ret = ctx->ops->prepare_run(ctx);
720 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
721 mutex_unlock(&dev->coda_mutex);
722 mutex_unlock(&ctx->buffer_mutex);
723 /* job_finish scheduled by prepare_decode */
724 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300725 }
726
Philipp Zabel32b6f202014-07-11 06:36:20 -0300727 if (!wait_for_completion_timeout(&ctx->completion, msecs_to_jiffies(1000))) {
728 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300729
730 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300731
732 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300733 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300734 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300735 }
736
737 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
738 queue_work(dev->workqueue, &ctx->seq_end_work);
739
740 mutex_unlock(&dev->coda_mutex);
741 mutex_unlock(&ctx->buffer_mutex);
742
Philipp Zabel14604e32014-07-11 06:36:22 -0300743 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300744}
745
746static int coda_job_ready(void *m2m_priv)
747{
748 struct coda_ctx *ctx = m2m_priv;
749
750 /*
751 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300752 * and 1 frame are needed. In the decoder case,
753 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300754 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300755 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300756 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300757 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
758 "not ready: not enough video buffers.\n");
759 return 0;
760 }
761
Philipp Zabel14604e32014-07-11 06:36:22 -0300762 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300763 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
764 "not ready: not enough video capture buffers.\n");
765 return 0;
766 }
767
Philipp Zabel84e23652014-07-11 06:36:34 -0300768 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -0300769 ((ctx->inst_type == CODA_INST_DECODER) &&
770 (coda_get_bitstream_payload(ctx) < 512) &&
771 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
772 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
773 "%d: not ready: not enough bitstream data.\n",
774 ctx->idx);
775 return 0;
776 }
777
Philipp Zabel3e748262013-05-23 10:43:01 -0300778 if (ctx->aborting) {
779 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
780 "not ready: aborting\n");
781 return 0;
782 }
783
Javier Martin186b2502012-07-26 05:53:35 -0300784 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
785 "job ready\n");
786 return 1;
787}
788
789static void coda_job_abort(void *priv)
790{
791 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -0300792
793 ctx->aborting = 1;
794
795 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
796 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -0300797}
798
799static void coda_lock(void *m2m_priv)
800{
801 struct coda_ctx *ctx = m2m_priv;
802 struct coda_dev *pcdev = ctx->dev;
803 mutex_lock(&pcdev->dev_mutex);
804}
805
806static void coda_unlock(void *m2m_priv)
807{
808 struct coda_ctx *ctx = m2m_priv;
809 struct coda_dev *pcdev = ctx->dev;
810 mutex_unlock(&pcdev->dev_mutex);
811}
812
Philipp Zabel814c3762014-07-18 07:22:45 -0300813static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -0300814 .device_run = coda_device_run,
815 .job_ready = coda_job_ready,
816 .job_abort = coda_job_abort,
817 .lock = coda_lock,
818 .unlock = coda_unlock,
819};
820
Philipp Zabel89548442014-07-11 06:36:17 -0300821static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
822{
823 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
824 int luma_map, chro_map, i;
825
826 memset(tiled_map, 0, sizeof(*tiled_map));
827
828 luma_map = 64;
829 chro_map = 64;
830 tiled_map->map_type = tiled_map_type;
831 for (i = 0; i < 16; i++)
832 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
833 for (i = 0; i < 4; i++)
834 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
835 for (i = 0; i < 16; i++)
836 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
837
838 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
839 tiled_map->xy2rbc_config = 0;
840 } else {
841 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
842 tiled_map_type);
843 return;
844 }
845}
846
Javier Martin186b2502012-07-26 05:53:35 -0300847static void set_default_params(struct coda_ctx *ctx)
848{
Philipp Zabel121cacf2014-07-18 07:22:42 -0300849 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300850 int max_w;
851 int max_h;
852
Philipp Zabel121cacf2014-07-18 07:22:42 -0300853 if (ctx->inst_type == CODA_INST_ENCODER) {
854 src_fourcc = V4L2_PIX_FMT_YUV420;
855 dst_fourcc = V4L2_PIX_FMT_H264;
856 } else {
857 src_fourcc = V4L2_PIX_FMT_H264;
858 dst_fourcc = V4L2_PIX_FMT_YUV420;
859 }
860 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300861 max_w = ctx->codec->max_w;
862 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300863
Philipp Zabel121cacf2014-07-18 07:22:42 -0300864 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -0300865 ctx->colorspace = V4L2_COLORSPACE_REC709;
866 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300867 ctx->aborting = 0;
868
869 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300870 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
871 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
872 ctx->q_data[V4L2_M2M_SRC].width = max_w;
873 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300874 ctx->q_data[V4L2_M2M_DST].width = max_w;
875 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -0300876 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
877 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
878 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
879 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
880 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
881 } else {
882 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
883 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
884 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
885 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
886 }
Philipp Zabel52c41672014-07-11 06:36:19 -0300887 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
888 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
889 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
890 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -0300891
892 if (ctx->dev->devtype->product == CODA_960)
893 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -0300894}
895
896/*
897 * Queue operations
898 */
899static int coda_queue_setup(struct vb2_queue *vq,
900 const struct v4l2_format *fmt,
901 unsigned int *nbuffers, unsigned int *nplanes,
902 unsigned int sizes[], void *alloc_ctxs[])
903{
904 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300905 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300906 unsigned int size;
907
Philipp Zabele34db062012-08-29 08:22:00 -0300908 q_data = get_q_data(ctx, vq->type);
909 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300910
911 *nplanes = 1;
912 sizes[0] = size;
913
914 alloc_ctxs[0] = ctx->dev->alloc_ctx;
915
916 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
917 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
918
919 return 0;
920}
921
922static int coda_buf_prepare(struct vb2_buffer *vb)
923{
924 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
925 struct coda_q_data *q_data;
926
927 q_data = get_q_data(ctx, vb->vb2_queue->type);
928
929 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
930 v4l2_warn(&ctx->dev->v4l2_dev,
931 "%s data will not fit into plane (%lu < %lu)\n",
932 __func__, vb2_plane_size(vb, 0),
933 (long)q_data->sizeimage);
934 return -EINVAL;
935 }
936
Javier Martin186b2502012-07-26 05:53:35 -0300937 return 0;
938}
939
940static void coda_buf_queue(struct vb2_buffer *vb)
941{
942 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300943 struct coda_q_data *q_data;
944
945 q_data = get_q_data(ctx, vb->vb2_queue->type);
946
947 /*
948 * In the decoder case, immediately try to copy the buffer into the
949 * bitstream ringbuffer and mark it as ready to be dequeued.
950 */
951 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
952 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
953 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300954 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -0300955 * the stream end
956 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300957 if (vb2_get_plane_payload(vb, 0) == 0)
958 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300959 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -0300960 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -0300961 if (vb2_is_streaming(vb->vb2_queue))
962 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300963 mutex_unlock(&ctx->bitstream_mutex);
964 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -0300965 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300966 }
Javier Martin186b2502012-07-26 05:53:35 -0300967}
968
Philipp Zabel79924ca2014-07-23 12:28:45 -0300969int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
970 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300971{
972 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
973 GFP_KERNEL);
974 if (!buf->vaddr)
975 return -ENOMEM;
976
977 buf->size = size;
978
Philipp Zabelf61c0b12014-07-11 06:36:40 -0300979 if (name && parent) {
980 buf->blob.data = buf->vaddr;
981 buf->blob.size = size;
982 buf->dentry = debugfs_create_blob(name, 0644, parent, &buf->blob);
983 if (!buf->dentry)
984 dev_warn(&dev->plat_dev->dev,
985 "failed to create debugfs entry %s\n", name);
986 }
987
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300988 return 0;
989}
990
Philipp Zabel79924ca2014-07-23 12:28:45 -0300991void coda_free_aux_buf(struct coda_dev *dev,
992 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -0300993{
994 if (buf->vaddr) {
995 dma_free_coherent(&dev->plat_dev->dev, buf->size,
996 buf->vaddr, buf->paddr);
997 buf->vaddr = NULL;
998 buf->size = 0;
999 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001000 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001001}
1002
Javier Martin186b2502012-07-26 05:53:35 -03001003static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1004{
1005 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1006 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001007 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001008 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001009 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001010 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001011
Philipp Zabelb96904e2013-05-23 10:42:58 -03001012 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001013 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1014 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03001015 /* copy the buffers that where queued before streamon */
1016 mutex_lock(&ctx->bitstream_mutex);
1017 coda_fill_bitstream(ctx);
1018 mutex_unlock(&ctx->bitstream_mutex);
1019
Philipp Zabelb9063522014-08-05 14:00:10 -03001020 if (coda_get_bitstream_payload(ctx) < 512) {
1021 ret = -EINVAL;
1022 goto err;
1023 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001024 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001025 if (count < 1) {
1026 ret = -EINVAL;
1027 goto err;
1028 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001029 }
1030
1031 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001032 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001033 if (count < 1) {
1034 ret = -EINVAL;
1035 goto err;
1036 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001037
1038 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001039 }
Javier Martin186b2502012-07-26 05:53:35 -03001040
Philipp Zabelb96904e2013-05-23 10:42:58 -03001041 /* Don't start the coda unless both queues are on */
1042 if (!(ctx->streamon_out & ctx->streamon_cap))
1043 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001044
Philipp Zabeleb107512013-09-30 10:34:45 -03001045 /* Allow decoder device_run with no new buffers queued */
1046 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03001047 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001048
Philipp Zabelb96904e2013-05-23 10:42:58 -03001049 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001050 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001051 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001052
Philipp Zabelb96904e2013-05-23 10:42:58 -03001053 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1054 q_data_dst->fourcc);
1055 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001056 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001057 ret = -EINVAL;
1058 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001059 }
1060
Philipp Zabela1192a12014-07-23 12:28:40 -03001061 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001062 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001063 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001064 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001065 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001066 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001067 }
1068
Philipp Zabel89548442014-07-11 06:36:17 -03001069 ctx->initialized = 1;
1070 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001071
1072err:
1073 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1074 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1075 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1076 } else {
1077 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1078 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1079 }
1080 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001081}
1082
Hans Verkuile37559b2014-04-17 02:47:21 -03001083static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001084{
1085 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001086 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001087 struct vb2_buffer *buf;
Javier Martin186b2502012-07-26 05:53:35 -03001088
1089 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001090 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001091 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001092 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001093
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001094 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001095
Philipp Zabel918c66f2013-06-27 06:59:01 -03001096 ctx->isequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001097
1098 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1099 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001100 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001101 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001102 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001103 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001104
1105 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001106 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001107
1108 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1109 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001110 }
1111
Philipp Zabel918c66f2013-06-27 06:59:01 -03001112 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001113 struct coda_timestamp *ts;
1114
1115 while (!list_empty(&ctx->timestamp_list)) {
1116 ts = list_first_entry(&ctx->timestamp_list,
1117 struct coda_timestamp, list);
1118 list_del(&ts->list);
1119 kfree(ts);
1120 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001121 kfifo_init(&ctx->bitstream_fifo,
1122 ctx->bitstream.vaddr, ctx->bitstream.size);
1123 ctx->runcounter = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001124 }
Javier Martin186b2502012-07-26 05:53:35 -03001125}
1126
Philipp Zabel121cacf2014-07-18 07:22:42 -03001127static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001128 .queue_setup = coda_queue_setup,
1129 .buf_prepare = coda_buf_prepare,
1130 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001131 .start_streaming = coda_start_streaming,
1132 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001133 .wait_prepare = vb2_ops_wait_prepare,
1134 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001135};
1136
1137static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1138{
1139 struct coda_ctx *ctx =
1140 container_of(ctrl->handler, struct coda_ctx, ctrls);
1141
1142 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1143 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1144
1145 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001146 case V4L2_CID_HFLIP:
1147 if (ctrl->val)
1148 ctx->params.rot_mode |= CODA_MIR_HOR;
1149 else
1150 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1151 break;
1152 case V4L2_CID_VFLIP:
1153 if (ctrl->val)
1154 ctx->params.rot_mode |= CODA_MIR_VER;
1155 else
1156 ctx->params.rot_mode &= ~CODA_MIR_VER;
1157 break;
Javier Martin186b2502012-07-26 05:53:35 -03001158 case V4L2_CID_MPEG_VIDEO_BITRATE:
1159 ctx->params.bitrate = ctrl->val / 1000;
1160 break;
1161 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1162 ctx->params.gop_size = ctrl->val;
1163 break;
1164 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1165 ctx->params.h264_intra_qp = ctrl->val;
1166 break;
1167 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1168 ctx->params.h264_inter_qp = ctrl->val;
1169 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001170 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1171 ctx->params.h264_min_qp = ctrl->val;
1172 break;
1173 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1174 ctx->params.h264_max_qp = ctrl->val;
1175 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001176 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1177 ctx->params.h264_deblk_alpha = ctrl->val;
1178 break;
1179 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1180 ctx->params.h264_deblk_beta = ctrl->val;
1181 break;
1182 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1183 ctx->params.h264_deblk_enabled = (ctrl->val ==
1184 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1185 break;
Javier Martin186b2502012-07-26 05:53:35 -03001186 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1187 ctx->params.mpeg4_intra_qp = ctrl->val;
1188 break;
1189 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1190 ctx->params.mpeg4_inter_qp = ctrl->val;
1191 break;
1192 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1193 ctx->params.slice_mode = ctrl->val;
1194 break;
1195 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1196 ctx->params.slice_max_mb = ctrl->val;
1197 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001198 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1199 ctx->params.slice_max_bits = ctrl->val * 8;
1200 break;
Javier Martin186b2502012-07-26 05:53:35 -03001201 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1202 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001203 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1204 ctx->params.intra_refresh = ctrl->val;
1205 break;
Javier Martin186b2502012-07-26 05:53:35 -03001206 default:
1207 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1208 "Invalid control, id=%d, val=%d\n",
1209 ctrl->id, ctrl->val);
1210 return -EINVAL;
1211 }
1212
1213 return 0;
1214}
1215
Philipp Zabel814c3762014-07-18 07:22:45 -03001216static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001217 .s_ctrl = coda_s_ctrl,
1218};
1219
1220static int coda_ctrls_setup(struct coda_ctx *ctx)
1221{
1222 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1223
1224 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001225 V4L2_CID_HFLIP, 0, 1, 1, 0);
1226 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1227 V4L2_CID_VFLIP, 0, 1, 1, 0);
1228 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001229 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1230 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1231 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1232 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001233 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001234 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001235 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001236 if (ctx->dev->devtype->product != CODA_960) {
1237 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1238 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1239 }
1240 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1241 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001242 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001243 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1244 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1245 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1246 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1247 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1248 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1249 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1250 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001251 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1252 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1253 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1254 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1255 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001256 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1257 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001258 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1259 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001260 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1261 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 500);
Javier Martin186b2502012-07-26 05:53:35 -03001262 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1263 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1264 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1265 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1266 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001267 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1268 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001269
1270 if (ctx->ctrls.error) {
1271 v4l2_err(&ctx->dev->v4l2_dev, "control initialization error (%d)",
1272 ctx->ctrls.error);
1273 return -EINVAL;
1274 }
1275
1276 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1277}
1278
Philipp Zabel121cacf2014-07-18 07:22:42 -03001279static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001280{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001281 vq->drv_priv = ctx;
1282 vq->ops = &coda_qops;
1283 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1284 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1285 vq->lock = &ctx->dev->dev_mutex;
1286
1287 return vb2_queue_init(vq);
1288}
1289
Philipp Zabel79924ca2014-07-23 12:28:45 -03001290int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1291 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001292{
Javier Martin186b2502012-07-26 05:53:35 -03001293 int ret;
1294
Javier Martin186b2502012-07-26 05:53:35 -03001295 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001296 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001297 src_vq->mem_ops = &vb2_dma_contig_memops;
1298
Philipp Zabel121cacf2014-07-18 07:22:42 -03001299 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001300 if (ret)
1301 return ret;
1302
Javier Martin186b2502012-07-26 05:53:35 -03001303 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001304 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001305 dst_vq->mem_ops = &vb2_dma_contig_memops;
1306
Philipp Zabel121cacf2014-07-18 07:22:42 -03001307 return coda_queue_init(priv, dst_vq);
1308}
1309
Philipp Zabel79924ca2014-07-23 12:28:45 -03001310int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1311 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001312{
1313 int ret;
1314
1315 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1316 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1317 src_vq->mem_ops = &vb2_dma_contig_memops;
1318
1319 ret = coda_queue_init(priv, src_vq);
1320 if (ret)
1321 return ret;
1322
1323 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1324 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1325 dst_vq->mem_ops = &vb2_dma_contig_memops;
1326
1327 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001328}
1329
Philipp Zabele11f3e62012-07-25 09:16:58 -03001330static int coda_next_free_instance(struct coda_dev *dev)
1331{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001332 int idx = ffz(dev->instance_mask);
1333
1334 if ((idx < 0) ||
1335 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1336 return -EBUSY;
1337
1338 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001339}
1340
Philipp Zabela1192a12014-07-23 12:28:40 -03001341static int coda_open(struct file *file, enum coda_inst_type inst_type,
1342 const struct coda_context_ops *ctx_ops)
Javier Martin186b2502012-07-26 05:53:35 -03001343{
1344 struct coda_dev *dev = video_drvdata(file);
1345 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001346 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001347 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001348 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001349
Javier Martin186b2502012-07-26 05:53:35 -03001350 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
1351 if (!ctx)
1352 return -ENOMEM;
1353
Fabio Estevamf82bc202013-08-21 11:14:16 -03001354 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001355 if (idx < 0) {
1356 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001357 goto err_coda_max;
1358 }
1359 set_bit(idx, &dev->instance_mask);
1360
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001361 name = kasprintf(GFP_KERNEL, "context%d", idx);
1362 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1363 kfree(name);
1364
Philipp Zabel121cacf2014-07-18 07:22:42 -03001365 ctx->inst_type = inst_type;
Philipp Zabela1192a12014-07-23 12:28:40 -03001366 ctx->ops = ctx_ops;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001367 init_completion(&ctx->completion);
1368 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabela1192a12014-07-23 12:28:40 -03001369 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001370 v4l2_fh_init(&ctx->fh, video_devdata(file));
1371 file->private_data = &ctx->fh;
1372 v4l2_fh_add(&ctx->fh);
1373 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001374 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001375 switch (dev->devtype->product) {
1376 case CODA_7541:
Philipp Zabel89548442014-07-11 06:36:17 -03001377 case CODA_960:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001378 ctx->reg_idx = 0;
1379 break;
1380 default:
1381 ctx->reg_idx = idx;
1382 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001383
Philipp Zabel1e172732014-07-11 06:36:23 -03001384 /* Power up and upload firmware if necessary */
1385 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1386 if (ret < 0) {
1387 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1388 goto err_pm_get;
1389 }
1390
Fabio Estevam79830db2013-08-21 11:14:17 -03001391 ret = clk_prepare_enable(dev->clk_per);
1392 if (ret)
1393 goto err_clk_per;
1394
1395 ret = clk_prepare_enable(dev->clk_ahb);
1396 if (ret)
1397 goto err_clk_ahb;
1398
Javier Martin186b2502012-07-26 05:53:35 -03001399 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001400 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1401 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001402 if (IS_ERR(ctx->fh.m2m_ctx)) {
1403 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001404
1405 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1406 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001407 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001408 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001409
Javier Martin186b2502012-07-26 05:53:35 -03001410 ret = coda_ctrls_setup(ctx);
1411 if (ret) {
1412 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001413 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001414 }
1415
1416 ctx->fh.ctrl_handler = &ctx->ctrls;
1417
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001418 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
1419 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001420 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001421 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001422 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03001423 }
1424
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001425 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1426 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1427 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1428 if (!ctx->bitstream.vaddr) {
1429 v4l2_err(&dev->v4l2_dev, "failed to allocate bitstream ringbuffer");
1430 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001431 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001432 }
1433 kfifo_init(&ctx->bitstream_fifo,
1434 ctx->bitstream.vaddr, ctx->bitstream.size);
1435 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001436 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001437 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001438
Javier Martin186b2502012-07-26 05:53:35 -03001439 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001440 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001441 coda_unlock(ctx);
1442
Javier Martin186b2502012-07-26 05:53:35 -03001443 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1444 ctx->idx, ctx);
1445
1446 return 0;
1447
Fabio Estevamf82bc202013-08-21 11:14:16 -03001448err_dma_writecombine:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001449 if (ctx->dev->devtype->product == CODA_DX6)
1450 coda_free_aux_buf(dev, &ctx->workbuf);
1451 coda_free_aux_buf(dev, &ctx->parabuf);
1452err_dma_alloc:
1453 v4l2_ctrl_handler_free(&ctx->ctrls);
1454err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001455 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001456err_ctx_init:
1457 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001458err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001459 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001460err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001461 pm_runtime_put_sync(&dev->plat_dev->dev);
1462err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001463 v4l2_fh_del(&ctx->fh);
1464 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001465 clear_bit(ctx->idx, &dev->instance_mask);
1466err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001467 kfree(ctx);
1468 return ret;
1469}
1470
Philipp Zabel121cacf2014-07-18 07:22:42 -03001471static int coda_encoder_open(struct file *file)
1472{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001473 return coda_open(file, CODA_INST_ENCODER, &coda_bit_encode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001474}
1475
1476static int coda_decoder_open(struct file *file)
1477{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001478 return coda_open(file, CODA_INST_DECODER, &coda_bit_decode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001479}
1480
Javier Martin186b2502012-07-26 05:53:35 -03001481static int coda_release(struct file *file)
1482{
1483 struct coda_dev *dev = video_drvdata(file);
1484 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1485
1486 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1487 ctx);
1488
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001489 debugfs_remove_recursive(ctx->debugfs_entry);
1490
Philipp Zabel918c66f2013-06-27 06:59:01 -03001491 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001492 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001493
1494 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03001495 if (ctx->initialized) {
1496 queue_work(dev->workqueue, &ctx->seq_end_work);
1497 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001498 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001499
Javier Martin186b2502012-07-26 05:53:35 -03001500 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001501 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001502 coda_unlock(ctx);
1503
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001504 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1505 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001506 if (ctx->dev->devtype->product == CODA_DX6)
1507 coda_free_aux_buf(dev, &ctx->workbuf);
1508
1509 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03001510 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001511 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001512 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001513 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001514 v4l2_fh_del(&ctx->fh);
1515 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001516 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001517 if (ctx->ops->release)
1518 ctx->ops->release(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001519 kfree(ctx);
1520
1521 return 0;
1522}
1523
Philipp Zabel121cacf2014-07-18 07:22:42 -03001524static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001525 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03001526 .open = coda_encoder_open,
1527 .release = coda_release,
1528 .poll = v4l2_m2m_fop_poll,
1529 .unlocked_ioctl = video_ioctl2,
1530 .mmap = v4l2_m2m_fop_mmap,
1531};
1532
1533static const struct v4l2_file_operations coda_decoder_fops = {
1534 .owner = THIS_MODULE,
1535 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03001536 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001537 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001538 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001539 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001540};
1541
Philipp Zabel87048bb2012-07-02 07:03:43 -03001542static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001543{
Javier Martin186b2502012-07-26 05:53:35 -03001544 u32 data;
1545 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001546 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001547
Fabio Estevam79830db2013-08-21 11:14:17 -03001548 ret = clk_prepare_enable(dev->clk_per);
1549 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001550 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001551
1552 ret = clk_prepare_enable(dev->clk_ahb);
1553 if (ret)
1554 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001555
Philipp Zabel8f452842014-07-11 06:36:35 -03001556 if (dev->rstc)
1557 reset_control_reset(dev->rstc);
1558
Javier Martin186b2502012-07-26 05:53:35 -03001559 /*
1560 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001561 * The 16-bit chars in the code buffer are in memory access
1562 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001563 * Data in this SRAM survives a reboot.
1564 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001565 p = (u16 *)dev->codebuf.vaddr;
1566 if (dev->devtype->product == CODA_DX6) {
1567 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1568 data = CODA_DOWN_ADDRESS_SET(i) |
1569 CODA_DOWN_DATA_SET(p[i ^ 1]);
1570 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1571 }
1572 } else {
1573 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1574 data = CODA_DOWN_ADDRESS_SET(i) |
1575 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1576 3 - (i % 4)]);
1577 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1578 }
Javier Martin186b2502012-07-26 05:53:35 -03001579 }
Javier Martin186b2502012-07-26 05:53:35 -03001580
Philipp Zabel9acf7692013-05-23 10:42:56 -03001581 /* Clear registers */
1582 for (i = 0; i < 64; i++)
1583 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1584
Javier Martin186b2502012-07-26 05:53:35 -03001585 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001586 if (dev->devtype->product == CODA_960 ||
1587 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001588 coda_write(dev, dev->tempbuf.paddr,
1589 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001590 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001591 } else {
1592 coda_write(dev, dev->workbuf.paddr,
1593 CODA_REG_BIT_WORK_BUF_ADDR);
1594 }
Javier Martin186b2502012-07-26 05:53:35 -03001595 coda_write(dev, dev->codebuf.paddr,
1596 CODA_REG_BIT_CODE_BUF_ADDR);
1597 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1598
1599 /* Set default values */
1600 switch (dev->devtype->product) {
1601 case CODA_DX6:
1602 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1603 break;
1604 default:
1605 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, CODA_REG_BIT_STREAM_CTRL);
1606 }
Philipp Zabel89548442014-07-11 06:36:17 -03001607 if (dev->devtype->product == CODA_960)
1608 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1609 else
1610 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001611
1612 if (dev->devtype->product != CODA_DX6)
1613 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1614
Javier Martin186b2502012-07-26 05:53:35 -03001615 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1616 CODA_REG_BIT_INT_ENABLE);
1617
1618 /* Reset VPU and start processor */
1619 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1620 data |= CODA_REG_RESET_ENABLE;
1621 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1622 udelay(10);
1623 data &= ~CODA_REG_RESET_ENABLE;
1624 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1625 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1626
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001627 clk_disable_unprepare(dev->clk_ahb);
1628 clk_disable_unprepare(dev->clk_per);
1629
1630 return 0;
1631
1632err_clk_ahb:
1633 clk_disable_unprepare(dev->clk_per);
1634err_clk_per:
1635 return ret;
1636}
1637
Philipp Zabel121cacf2014-07-18 07:22:42 -03001638static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
1639{
1640 vfd->release = video_device_release_empty,
1641 vfd->lock = &dev->dev_mutex;
1642 vfd->v4l2_dev = &dev->v4l2_dev;
1643 vfd->vfl_dir = VFL_DIR_M2M;
1644 video_set_drvdata(vfd, dev);
1645
1646 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1647}
1648
Javier Martin186b2502012-07-26 05:53:35 -03001649static void coda_fw_callback(const struct firmware *fw, void *context)
1650{
1651 struct coda_dev *dev = context;
1652 struct platform_device *pdev = dev->plat_dev;
1653 int ret;
1654
1655 if (!fw) {
1656 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
1657 return;
1658 }
1659
1660 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001661 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1662 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001663 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001664 dev_err(&pdev->dev, "failed to allocate code buffer\n");
1665 return;
1666 }
1667
Philipp Zabel87048bb2012-07-02 07:03:43 -03001668 /* Copy the whole firmware image to the code buffer */
1669 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1670 release_firmware(fw);
1671
Philipp Zabel1e172732014-07-11 06:36:23 -03001672 if (pm_runtime_enabled(&pdev->dev) && pdev->dev.pm_domain) {
1673 /*
1674 * Enabling power temporarily will cause coda_hw_init to be
1675 * called via coda_runtime_resume by the pm domain.
1676 */
1677 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1678 if (ret < 0) {
1679 v4l2_err(&dev->v4l2_dev, "failed to power on: %d\n",
1680 ret);
1681 return;
1682 }
1683
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001684 ret = coda_check_firmware(dev);
1685 if (ret < 0)
1686 return;
1687
Philipp Zabel1e172732014-07-11 06:36:23 -03001688 pm_runtime_put_sync(&dev->plat_dev->dev);
1689 } else {
1690 /*
1691 * If runtime pm is disabled or pm_domain is not set,
1692 * initialize once manually.
1693 */
1694 ret = coda_hw_init(dev);
1695 if (ret < 0) {
1696 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1697 return;
1698 }
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001699
1700 ret = coda_check_firmware(dev);
1701 if (ret < 0)
1702 return;
Javier Martin186b2502012-07-26 05:53:35 -03001703 }
1704
Javier Martin186b2502012-07-26 05:53:35 -03001705 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1706 if (IS_ERR(dev->alloc_ctx)) {
1707 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
1708 return;
1709 }
1710
1711 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1712 if (IS_ERR(dev->m2m_dev)) {
1713 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1714 goto rel_ctx;
1715 }
1716
Philipp Zabel121cacf2014-07-18 07:22:42 -03001717 dev->vfd[0].fops = &coda_encoder_fops,
1718 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
1719 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
1720 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03001721 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03001722 v4l2_err(&dev->v4l2_dev,
1723 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03001724 goto rel_m2m;
1725 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03001726
1727 dev->vfd[1].fops = &coda_decoder_fops,
1728 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
1729 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
1730 ret = coda_register_device(dev, &dev->vfd[1]);
1731 if (ret) {
1732 v4l2_err(&dev->v4l2_dev,
1733 "Failed to register decoder video device\n");
1734 goto rel_m2m;
1735 }
1736
1737 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1738 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03001739
1740 return;
1741
1742rel_m2m:
1743 v4l2_m2m_release(dev->m2m_dev);
1744rel_ctx:
1745 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1746}
1747
1748static int coda_firmware_request(struct coda_dev *dev)
1749{
1750 char *fw = dev->devtype->firmware;
1751
1752 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1753 coda_product_name(dev->devtype->product));
1754
1755 return request_firmware_nowait(THIS_MODULE, true,
1756 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1757}
1758
1759enum coda_platform {
1760 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001761 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03001762 CODA_IMX6Q,
1763 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03001764};
1765
Emil Goodec06d8752012-08-14 17:44:42 -03001766static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001767 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001768 .firmware = "v4l-codadx6-imx27.bin",
1769 .product = CODA_DX6,
1770 .codecs = codadx6_codecs,
1771 .num_codecs = ARRAY_SIZE(codadx6_codecs),
1772 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001773 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03001774 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001775 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001776 .firmware = "v4l-coda7541-imx53.bin",
1777 .product = CODA_7541,
1778 .codecs = coda7_codecs,
1779 .num_codecs = ARRAY_SIZE(coda7_codecs),
1780 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001781 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001782 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001783 },
Philipp Zabel89548442014-07-11 06:36:17 -03001784 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001785 .firmware = "v4l-coda960-imx6q.bin",
1786 .product = CODA_960,
1787 .codecs = coda9_codecs,
1788 .num_codecs = ARRAY_SIZE(coda9_codecs),
1789 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001790 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001791 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03001792 },
1793 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001794 .firmware = "v4l-coda960-imx6dl.bin",
1795 .product = CODA_960,
1796 .codecs = coda9_codecs,
1797 .num_codecs = ARRAY_SIZE(coda9_codecs),
1798 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001799 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001800 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03001801 },
Javier Martin186b2502012-07-26 05:53:35 -03001802};
1803
1804static struct platform_device_id coda_platform_ids[] = {
1805 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001806 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001807 { /* sentinel */ }
1808};
1809MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1810
1811#ifdef CONFIG_OF
1812static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03001813 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001814 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03001815 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
1816 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03001817 { /* sentinel */ }
1818};
1819MODULE_DEVICE_TABLE(of, coda_dt_ids);
1820#endif
1821
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001822static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001823{
1824 const struct of_device_id *of_id =
1825 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1826 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001827 struct coda_platform_data *pdata = pdev->dev.platform_data;
1828 struct device_node *np = pdev->dev.of_node;
1829 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001830 struct coda_dev *dev;
1831 struct resource *res;
1832 int ret, irq;
1833
1834 dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
1835 if (!dev) {
1836 dev_err(&pdev->dev, "Not enough memory for %s\n",
1837 CODA_NAME);
1838 return -ENOMEM;
1839 }
1840
1841 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001842 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001843
1844 dev->plat_dev = pdev;
1845 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1846 if (IS_ERR(dev->clk_per)) {
1847 dev_err(&pdev->dev, "Could not get per clock\n");
1848 return PTR_ERR(dev->clk_per);
1849 }
1850
1851 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1852 if (IS_ERR(dev->clk_ahb)) {
1853 dev_err(&pdev->dev, "Could not get ahb clock\n");
1854 return PTR_ERR(dev->clk_ahb);
1855 }
1856
1857 /* Get memory for physical registers */
1858 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03001859 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1860 if (IS_ERR(dev->regs_base))
1861 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03001862
1863 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03001864 irq = platform_get_irq_byname(pdev, "bit");
1865 if (irq < 0)
1866 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001867 if (irq < 0) {
1868 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03001869 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03001870 }
1871
Fabio Estevam08c8d142014-06-04 15:46:24 -03001872 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
1873 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
1874 if (ret < 0) {
1875 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
1876 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001877 }
1878
Philipp Zabelee27aa92014-07-24 16:50:03 -03001879 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03001880 if (IS_ERR(dev->rstc)) {
1881 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03001882 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03001883 dev->rstc = NULL;
1884 } else {
1885 dev_err(&pdev->dev, "failed get reset control: %d\n", ret);
1886 return ret;
1887 }
1888 }
1889
Philipp Zabel657eee72013-04-29 16:17:14 -07001890 /* Get IRAM pool from device tree or platform data */
1891 pool = of_get_named_gen_pool(np, "iram", 0);
1892 if (!pool && pdata)
1893 pool = dev_get_gen_pool(pdata->iram_dev);
1894 if (!pool) {
1895 dev_err(&pdev->dev, "iram pool not available\n");
1896 return -ENOMEM;
1897 }
1898 dev->iram_pool = pool;
1899
Javier Martin186b2502012-07-26 05:53:35 -03001900 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1901 if (ret)
1902 return ret;
1903
1904 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001905 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001906
1907 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
1908
1909 if (of_id) {
1910 dev->devtype = of_id->data;
1911 } else if (pdev_id) {
1912 dev->devtype = &coda_devdata[pdev_id->driver_data];
1913 } else {
1914 v4l2_device_unregister(&dev->v4l2_dev);
1915 return -EINVAL;
1916 }
1917
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001918 dev->debugfs_root = debugfs_create_dir("coda", NULL);
1919 if (!dev->debugfs_root)
1920 dev_warn(&pdev->dev, "failed to create debugfs root\n");
1921
Javier Martin186b2502012-07-26 05:53:35 -03001922 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001923 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001924 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001925 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001926 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001927 if (ret < 0) {
1928 dev_err(&pdev->dev, "failed to allocate work buffer\n");
1929 v4l2_device_unregister(&dev->v4l2_dev);
1930 return ret;
1931 }
Javier Martin186b2502012-07-26 05:53:35 -03001932 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001933
1934 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001935 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001936 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001937 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001938 if (ret < 0) {
1939 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
1940 v4l2_device_unregister(&dev->v4l2_dev);
1941 return ret;
1942 }
Javier Martin186b2502012-07-26 05:53:35 -03001943 }
1944
Philipp Zabel401e9722014-07-11 06:36:43 -03001945 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001946 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
1947 &dev->iram.paddr);
1948 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03001949 dev_warn(&pdev->dev, "unable to alloc iram\n");
1950 } else {
1951 dev->iram.blob.data = dev->iram.vaddr;
1952 dev->iram.blob.size = dev->iram.size;
1953 dev->iram.dentry = debugfs_create_blob("iram", 0644,
1954 dev->debugfs_root,
1955 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03001956 }
1957
Philipp Zabel32b6f202014-07-11 06:36:20 -03001958 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1959 if (!dev->workqueue) {
1960 dev_err(&pdev->dev, "unable to alloc workqueue\n");
1961 return -ENOMEM;
1962 }
1963
Javier Martin186b2502012-07-26 05:53:35 -03001964 platform_set_drvdata(pdev, dev);
1965
Philipp Zabel1e172732014-07-11 06:36:23 -03001966 pm_runtime_enable(&pdev->dev);
1967
Javier Martin186b2502012-07-26 05:53:35 -03001968 return coda_firmware_request(dev);
1969}
1970
1971static int coda_remove(struct platform_device *pdev)
1972{
1973 struct coda_dev *dev = platform_get_drvdata(pdev);
1974
Philipp Zabel121cacf2014-07-18 07:22:42 -03001975 video_unregister_device(&dev->vfd[0]);
1976 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03001977 if (dev->m2m_dev)
1978 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03001979 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001980 if (dev->alloc_ctx)
1981 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
1982 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03001983 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001984 if (dev->iram.vaddr)
1985 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
1986 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001987 coda_free_aux_buf(dev, &dev->codebuf);
1988 coda_free_aux_buf(dev, &dev->tempbuf);
1989 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001990 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03001991 return 0;
1992}
1993
Philipp Zabel1e172732014-07-11 06:36:23 -03001994#ifdef CONFIG_PM_RUNTIME
1995static int coda_runtime_resume(struct device *dev)
1996{
1997 struct coda_dev *cdev = dev_get_drvdata(dev);
1998 int ret = 0;
1999
Philipp Zabel65919e62014-07-18 07:22:36 -03002000 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002001 ret = coda_hw_init(cdev);
2002 if (ret)
2003 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2004 }
2005
2006 return ret;
2007}
2008#endif
2009
2010static const struct dev_pm_ops coda_pm_ops = {
2011 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2012};
2013
Javier Martin186b2502012-07-26 05:53:35 -03002014static struct platform_driver coda_driver = {
2015 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002016 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002017 .driver = {
2018 .name = CODA_NAME,
2019 .owner = THIS_MODULE,
2020 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002021 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002022 },
2023 .id_table = coda_platform_ids,
2024};
2025
2026module_platform_driver(coda_driver);
2027
2028MODULE_LICENSE("GPL");
2029MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2030MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");