blob: 02d47fabae651280de3bac79b5def063905ed4b3 [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
Philipp Zabelf61c0b12014-07-11 06:36:40 -030015#include <linux/debugfs.h>
Javier Martin186b2502012-07-26 05:53:35 -030016#include <linux/delay.h>
17#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070018#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030022#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030023#include <linux/module.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030026#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030027#include <linux/slab.h>
28#include <linux/videodev2.h>
29#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070030#include <linux/platform_data/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030031#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030032
33#include <media/v4l2-ctrls.h>
34#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030035#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030036#include <media/v4l2-ioctl.h>
37#include <media/v4l2-mem2mem.h>
38#include <media/videobuf2-core.h>
39#include <media/videobuf2-dma-contig.h>
40
Philipp Zabela2b3e462014-07-23 12:28:39 -030041#include "coda.h"
Javier Martin186b2502012-07-26 05:53:35 -030042
43#define CODA_NAME "coda"
44
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030045#define CODADX6_MAX_INSTANCES 4
Javier Martin186b2502012-07-26 05:53:35 -030046
Javier Martin186b2502012-07-26 05:53:35 -030047#define CODA_PARA_BUF_SIZE (10 * 1024)
48#define CODA_ISRAM_SIZE (2048 * 2)
49
Javier Martin186b2502012-07-26 05:53:35 -030050#define MIN_W 176
51#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030052
53#define S_ALIGN 1 /* multiple of 2 */
54#define W_ALIGN 1 /* multiple of 2 */
55#define H_ALIGN 1 /* multiple of 2 */
56
57#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
58
Philipp Zabel79924ca2014-07-23 12:28:45 -030059int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030060module_param(coda_debug, int, 0644);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030061MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
Javier Martin186b2502012-07-26 05:53:35 -030062
Javier Martin186b2502012-07-26 05:53:35 -030063struct coda_fmt {
64 char *name;
65 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030066};
67
Philipp Zabela2b3e462014-07-23 12:28:39 -030068void coda_write(struct coda_dev *dev, u32 data, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030069{
Philipp Zabeld60b18b2014-08-05 14:00:15 -030070 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030071 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
72 writel(data, dev->regs_base + reg);
73}
74
Philipp Zabela2b3e462014-07-23 12:28:39 -030075unsigned int coda_read(struct coda_dev *dev, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030076{
77 u32 data;
Philipp Zabelf23797b2014-08-06 08:02:23 -030078
Javier Martin186b2502012-07-26 05:53:35 -030079 data = readl(dev->regs_base + reg);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030080 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030081 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
82 return data;
83}
84
Philipp Zabel856d7d92014-09-29 09:53:44 -030085void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
86 struct vb2_buffer *buf, unsigned int reg_y)
87{
88 u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
89 u32 base_cb, base_cr;
90
91 switch (q_data->fourcc) {
92 case V4L2_PIX_FMT_YVU420:
93 /* Switch Cb and Cr for YVU420 format */
94 base_cr = base_y + q_data->bytesperline * q_data->height;
95 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
96 break;
97 case V4L2_PIX_FMT_YUV420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -030098 case V4L2_PIX_FMT_NV12:
Philipp Zabel856d7d92014-09-29 09:53:44 -030099 default:
100 base_cb = base_y + q_data->bytesperline * q_data->height;
101 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
102 break;
103 }
104
105 coda_write(ctx->dev, base_y, reg_y);
106 coda_write(ctx->dev, base_cb, reg_y + 4);
107 coda_write(ctx->dev, base_cr, reg_y + 8);
108}
109
Javier Martin186b2502012-07-26 05:53:35 -0300110/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300111 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300112 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300113static const struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300114 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300115 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300116 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300117 },
118 {
119 .name = "YUV 4:2:0 Planar, YCrCb",
120 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300121 },
122 {
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300123 .name = "YUV 4:2:0 Partial interleaved Y/CbCr",
124 .fourcc = V4L2_PIX_FMT_NV12,
125 },
126 {
Javier Martin186b2502012-07-26 05:53:35 -0300127 .name = "H264 Encoded Stream",
128 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300129 },
130 {
131 .name = "MPEG4 Encoded Stream",
132 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300133 },
134};
135
Philipp Zabelb96904e2013-05-23 10:42:58 -0300136#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
137 { mode, src_fourcc, dst_fourcc, max_w, max_h }
138
139/*
140 * Arrays of codecs supported by each given version of Coda:
141 * i.MX27 -> codadx6
142 * i.MX5x -> coda7
143 * i.MX6 -> coda960
144 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
145 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300146static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300147 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
148 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300149};
150
Philipp Zabel814c3762014-07-18 07:22:45 -0300151static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300152 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
153 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300154 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
155 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300156};
157
Philipp Zabel814c3762014-07-18 07:22:45 -0300158static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300159 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
160 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
161 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
162 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300163};
164
Philipp Zabelb96904e2013-05-23 10:42:58 -0300165static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300166{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300167 switch (fourcc) {
168 case V4L2_PIX_FMT_YUV420:
169 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300170 case V4L2_PIX_FMT_NV12:
Philipp Zabelb96904e2013-05-23 10:42:58 -0300171 return true;
172 default:
173 return false;
174 }
175}
Javier Martin186b2502012-07-26 05:53:35 -0300176
Philipp Zabelb96904e2013-05-23 10:42:58 -0300177/*
178 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
179 * tables.
180 */
181static u32 coda_format_normalize_yuv(u32 fourcc)
182{
183 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
184}
185
Philipp Zabel814c3762014-07-18 07:22:45 -0300186static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
187 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300188{
Philipp Zabel814c3762014-07-18 07:22:45 -0300189 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300190 int num_codecs = dev->devtype->num_codecs;
191 int k;
192
193 src_fourcc = coda_format_normalize_yuv(src_fourcc);
194 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
195 if (src_fourcc == dst_fourcc)
196 return NULL;
197
198 for (k = 0; k < num_codecs; k++) {
199 if (codecs[k].src_fourcc == src_fourcc &&
200 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300201 break;
202 }
203
Philipp Zabelb96904e2013-05-23 10:42:58 -0300204 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300205 return NULL;
206
Philipp Zabelb96904e2013-05-23 10:42:58 -0300207 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300208}
209
Philipp Zabel57625592013-09-30 10:34:51 -0300210static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300211 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300212 int *max_w, int *max_h)
213{
Philipp Zabel814c3762014-07-18 07:22:45 -0300214 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300215 int num_codecs = dev->devtype->num_codecs;
216 unsigned int w, h;
217 int k;
218
219 if (codec) {
220 w = codec->max_w;
221 h = codec->max_h;
222 } else {
223 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
224 w = max(w, codecs[k].max_w);
225 h = max(h, codecs[k].max_h);
226 }
227 }
228
229 if (max_w)
230 *max_w = w;
231 if (max_h)
232 *max_h = h;
233}
234
Philipp Zabel79924ca2014-07-23 12:28:45 -0300235const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300236{
237 static char buf[9];
238
239 switch (product) {
240 case CODA_DX6:
241 return "CodaDx6";
242 case CODA_7541:
243 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300244 case CODA_960:
245 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300246 default:
247 snprintf(buf, sizeof(buf), "(0x%04x)", product);
248 return buf;
249 }
250}
251
Javier Martin186b2502012-07-26 05:53:35 -0300252/*
253 * V4L2 ioctl() operations.
254 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300255static int coda_querycap(struct file *file, void *priv,
256 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300257{
Philipp Zabel927933f2013-09-30 10:34:48 -0300258 struct coda_ctx *ctx = fh_to_ctx(priv);
259
Javier Martin186b2502012-07-26 05:53:35 -0300260 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300261 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
262 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300263 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300264 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300265 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
266
267 return 0;
268}
269
Philipp Zabel22e244b2014-07-18 07:22:43 -0300270static int coda_enum_fmt(struct file *file, void *priv,
271 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300272{
273 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300274 const struct coda_codec *codecs = ctx->dev->devtype->codecs;
275 const struct coda_fmt *formats = coda_formats;
276 const struct coda_fmt *fmt;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300277 int num_codecs = ctx->dev->devtype->num_codecs;
278 int num_formats = ARRAY_SIZE(coda_formats);
279 int i, k, num = 0;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300280 bool yuv;
281
282 if (ctx->inst_type == CODA_INST_ENCODER)
283 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
284 else
285 yuv = (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -0300286
287 for (i = 0; i < num_formats; i++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300288 /* Skip either raw or compressed formats */
289 if (yuv != coda_format_is_yuv(formats[i].fourcc))
290 continue;
291 /* All uncompressed formats are always supported */
292 if (yuv) {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300293 if (num == f->index)
294 break;
295 ++num;
296 continue;
297 }
298 /* Compressed formats may be supported, check the codec list */
299 for (k = 0; k < num_codecs; k++) {
Philipp Zabel22e244b2014-07-18 07:22:43 -0300300 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
301 formats[i].fourcc == codecs[k].dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300302 break;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300303 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
Philipp Zabelb96904e2013-05-23 10:42:58 -0300304 formats[i].fourcc == codecs[k].src_fourcc)
305 break;
306 }
307 if (k < num_codecs) {
Javier Martin186b2502012-07-26 05:53:35 -0300308 if (num == f->index)
309 break;
310 ++num;
311 }
312 }
313
314 if (i < num_formats) {
315 fmt = &formats[i];
316 strlcpy(f->description, fmt->name, sizeof(f->description));
317 f->pixelformat = fmt->fourcc;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300318 if (!yuv)
Philipp Zabelbaf70212013-09-30 10:34:46 -0300319 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300320 return 0;
321 }
322
323 /* Format not found */
324 return -EINVAL;
325}
326
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300327static int coda_g_fmt(struct file *file, void *priv,
328 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300329{
Javier Martin186b2502012-07-26 05:53:35 -0300330 struct coda_q_data *q_data;
331 struct coda_ctx *ctx = fh_to_ctx(priv);
332
Javier Martin186b2502012-07-26 05:53:35 -0300333 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300334 if (!q_data)
335 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300336
337 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300338 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300339 f->fmt.pix.width = q_data->width;
340 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300341 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300342
343 f->fmt.pix.sizeimage = q_data->sizeimage;
344 f->fmt.pix.colorspace = ctx->colorspace;
345
346 return 0;
347}
348
Philipp Zabel814c3762014-07-18 07:22:45 -0300349static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300350 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300351{
Philipp Zabel57625592013-09-30 10:34:51 -0300352 struct coda_dev *dev = ctx->dev;
353 struct coda_q_data *q_data;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300354 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300355 enum v4l2_field field;
356
357 field = f->fmt.pix.field;
358 if (field == V4L2_FIELD_ANY)
359 field = V4L2_FIELD_NONE;
360 else if (V4L2_FIELD_NONE != field)
361 return -EINVAL;
362
363 /* V4L2 specification suggests the driver corrects the format struct
364 * if any of the dimensions is unsupported */
365 f->fmt.pix.field = field;
366
Philipp Zabel57625592013-09-30 10:34:51 -0300367 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
368 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
369 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
370 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300371
Philipp Zabel57625592013-09-30 10:34:51 -0300372 switch (f->fmt.pix.pixelformat) {
373 case V4L2_PIX_FMT_YUV420:
374 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300375 case V4L2_PIX_FMT_NV12:
Philipp Zabel57625592013-09-30 10:34:51 -0300376 case V4L2_PIX_FMT_H264:
377 case V4L2_PIX_FMT_MPEG4:
378 case V4L2_PIX_FMT_JPEG:
379 break;
380 default:
381 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300382 if (!q_data)
383 return -EINVAL;
Philipp Zabel57625592013-09-30 10:34:51 -0300384 f->fmt.pix.pixelformat = q_data->fourcc;
385 }
386
387 switch (f->fmt.pix.pixelformat) {
388 case V4L2_PIX_FMT_YUV420:
389 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300390 case V4L2_PIX_FMT_NV12:
Philipp Zabel451dfe52014-07-11 06:36:39 -0300391 /* Frame stride must be multiple of 8, but 16 for h.264 */
392 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300393 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300394 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300395 break;
396 case V4L2_PIX_FMT_H264:
397 case V4L2_PIX_FMT_MPEG4:
398 case V4L2_PIX_FMT_JPEG:
Javier Martin186b2502012-07-26 05:53:35 -0300399 f->fmt.pix.bytesperline = 0;
400 f->fmt.pix.sizeimage = CODA_MAX_FRAME_SIZE;
Philipp Zabel57625592013-09-30 10:34:51 -0300401 break;
402 default:
403 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300404 }
405
406 return 0;
407}
408
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300409static int coda_try_fmt_vid_cap(struct file *file, void *priv,
410 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300411{
Javier Martin186b2502012-07-26 05:53:35 -0300412 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel814c3762014-07-18 07:22:45 -0300413 const struct coda_codec *codec = NULL;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300414 struct vb2_queue *src_vq;
415 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300416
Philipp Zabel918c66f2013-06-27 06:59:01 -0300417 /*
418 * If the source format is already fixed, try to find a codec that
419 * converts to the given destination format
420 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300421 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300422 if (vb2_is_streaming(src_vq)) {
423 struct coda_q_data *q_data_src;
424
425 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
426 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
427 f->fmt.pix.pixelformat);
428 if (!codec)
429 return -EINVAL;
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300430
431 f->fmt.pix.width = q_data_src->width;
432 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300433 } else {
434 /* Otherwise determine codec by encoded format, if possible */
435 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_YUV420,
436 f->fmt.pix.pixelformat);
437 }
Javier Martin186b2502012-07-26 05:53:35 -0300438
439 f->fmt.pix.colorspace = ctx->colorspace;
440
Philipp Zabel57625592013-09-30 10:34:51 -0300441 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300442 if (ret < 0)
443 return ret;
444
445 /* The h.264 decoder only returns complete 16x16 macroblocks */
446 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300447 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300448 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300449 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300450 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
451 f->fmt.pix.height * 3 / 2;
452 }
453
454 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300455}
456
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300457static int coda_try_fmt_vid_out(struct file *file, void *priv,
458 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300459{
460 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelfcf59762014-08-05 14:00:12 -0300461 const struct coda_codec *codec = NULL;
Javier Martin186b2502012-07-26 05:53:35 -0300462
Philipp Zabelb96904e2013-05-23 10:42:58 -0300463 /* Determine codec by encoded format, returns NULL if raw or invalid */
Philipp Zabelfcf59762014-08-05 14:00:12 -0300464 if (ctx->inst_type == CODA_INST_DECODER) {
465 codec = coda_find_codec(ctx->dev, f->fmt.pix.pixelformat,
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300466 V4L2_PIX_FMT_YUV420);
467 if (!codec)
Philipp Zabelfcf59762014-08-05 14:00:12 -0300468 codec = coda_find_codec(ctx->dev, V4L2_PIX_FMT_H264,
469 V4L2_PIX_FMT_YUV420);
470 if (!codec)
Philipp Zabel4f31ff02014-07-18 07:22:44 -0300471 return -EINVAL;
472 }
Javier Martin186b2502012-07-26 05:53:35 -0300473
474 if (!f->fmt.pix.colorspace)
475 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
476
Philipp Zabel57625592013-09-30 10:34:51 -0300477 return coda_try_fmt(ctx, codec, f);
Javier Martin186b2502012-07-26 05:53:35 -0300478}
479
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300480static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300481{
482 struct coda_q_data *q_data;
483 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300484
Philipp Zabel14604e32014-07-11 06:36:22 -0300485 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300486 if (!vq)
487 return -EINVAL;
488
489 q_data = get_q_data(ctx, f->type);
490 if (!q_data)
491 return -EINVAL;
492
493 if (vb2_is_busy(vq)) {
494 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
495 return -EBUSY;
496 }
497
Philipp Zabelb96904e2013-05-23 10:42:58 -0300498 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300499 q_data->width = f->fmt.pix.width;
500 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300501 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300502 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300503 q_data->rect.left = 0;
504 q_data->rect.top = 0;
505 q_data->rect.width = f->fmt.pix.width;
506 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300507
508 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
509 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300510 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300511
512 return 0;
513}
514
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300515static int coda_s_fmt_vid_cap(struct file *file, void *priv,
516 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300517{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300518 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300519 int ret;
520
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300521 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300522 if (ret)
523 return ret;
524
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300525 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300526}
527
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300528static int coda_s_fmt_vid_out(struct file *file, void *priv,
529 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300530{
531 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300532 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300533 int ret;
534
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300535 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300536 if (ret)
537 return ret;
538
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300539 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300540 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300541 return ret;
542
543 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300544
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300545 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
546 coda_g_fmt(file, priv, &f_cap);
547 f_cap.fmt.pix.width = f->fmt.pix.width;
548 f_cap.fmt.pix.height = f->fmt.pix.height;
549
550 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
551 if (ret)
552 return ret;
553
554 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300555}
556
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300557static int coda_qbuf(struct file *file, void *priv,
558 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300559{
560 struct coda_ctx *ctx = fh_to_ctx(priv);
561
Philipp Zabel14604e32014-07-11 06:36:22 -0300562 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300563}
564
Philipp Zabel918c66f2013-06-27 06:59:01 -0300565static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
566 struct v4l2_buffer *buf)
567{
568 struct vb2_queue *src_vq;
569
Philipp Zabel14604e32014-07-11 06:36:22 -0300570 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300571
572 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
573 (buf->sequence == (ctx->qsequence - 1)));
574}
575
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300576static int coda_dqbuf(struct file *file, void *priv,
577 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300578{
579 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300580 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300581
Philipp Zabel14604e32014-07-11 06:36:22 -0300582 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300583
584 /* If this is the last capture buffer, emit an end-of-stream event */
585 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
586 coda_buf_is_end_of_stream(ctx, buf)) {
587 const struct v4l2_event eos_event = {
588 .type = V4L2_EVENT_EOS
589 };
590
591 v4l2_event_queue_fh(&ctx->fh, &eos_event);
592 }
593
594 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300595}
596
Philipp Zabel52c41672014-07-11 06:36:19 -0300597static int coda_g_selection(struct file *file, void *fh,
598 struct v4l2_selection *s)
599{
600 struct coda_ctx *ctx = fh_to_ctx(fh);
601 struct coda_q_data *q_data;
602 struct v4l2_rect r, *rsel;
603
604 q_data = get_q_data(ctx, s->type);
605 if (!q_data)
606 return -EINVAL;
607
608 r.left = 0;
609 r.top = 0;
610 r.width = q_data->width;
611 r.height = q_data->height;
612 rsel = &q_data->rect;
613
614 switch (s->target) {
615 case V4L2_SEL_TGT_CROP_DEFAULT:
616 case V4L2_SEL_TGT_CROP_BOUNDS:
617 rsel = &r;
618 /* fallthrough */
619 case V4L2_SEL_TGT_CROP:
620 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
621 return -EINVAL;
622 break;
623 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
624 case V4L2_SEL_TGT_COMPOSE_PADDED:
625 rsel = &r;
626 /* fallthrough */
627 case V4L2_SEL_TGT_COMPOSE:
628 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
629 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
630 return -EINVAL;
631 break;
632 default:
633 return -EINVAL;
634 }
635
636 s->r = *rsel;
637
638 return 0;
639}
640
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300641static int coda_try_decoder_cmd(struct file *file, void *fh,
642 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300643{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300644 if (dc->cmd != V4L2_DEC_CMD_STOP)
645 return -EINVAL;
646
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300647 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300648 return -EINVAL;
649
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300650 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300651 return -EINVAL;
652
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300653 return 0;
654}
655
656static int coda_decoder_cmd(struct file *file, void *fh,
657 struct v4l2_decoder_cmd *dc)
658{
659 struct coda_ctx *ctx = fh_to_ctx(fh);
660 int ret;
661
662 ret = coda_try_decoder_cmd(file, fh, dc);
663 if (ret < 0)
664 return ret;
665
666 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300667 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300668 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300669
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300670 /* Set the stream-end flag on this context */
671 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300672 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300673 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300674
Philipp Zabel918c66f2013-06-27 06:59:01 -0300675 return 0;
676}
677
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300678static int coda_subscribe_event(struct v4l2_fh *fh,
679 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300680{
681 switch (sub->type) {
682 case V4L2_EVENT_EOS:
683 return v4l2_event_subscribe(fh, sub, 0, NULL);
684 default:
685 return v4l2_ctrl_subscribe_event(fh, sub);
686 }
Javier Martin186b2502012-07-26 05:53:35 -0300687}
688
689static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300690 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300691
Philipp Zabel22e244b2014-07-18 07:22:43 -0300692 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300693 .vidioc_g_fmt_vid_cap = coda_g_fmt,
694 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
695 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300696
Philipp Zabel22e244b2014-07-18 07:22:43 -0300697 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300698 .vidioc_g_fmt_vid_out = coda_g_fmt,
699 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
700 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300701
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300702 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
703 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300704
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300705 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300706 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300707 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300708 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300709
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300710 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
711 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300712
Philipp Zabel52c41672014-07-11 06:36:19 -0300713 .vidioc_g_selection = coda_g_selection,
714
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300715 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300716 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300717
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300718 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300719 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300720};
721
Philipp Zabel79924ca2014-07-23 12:28:45 -0300722void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300723{
724 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
725 struct coda_dev *dev = ctx->dev;
726 int i;
727
728 for (i = 0; i < 16; i++)
729 coda_write(dev, tiled_map->xy2ca_map[i],
730 CODA9_GDI_XY2_CAS_0 + 4 * i);
731 for (i = 0; i < 4; i++)
732 coda_write(dev, tiled_map->xy2ba_map[i],
733 CODA9_GDI_XY2_BA_0 + 4 * i);
734 for (i = 0; i < 16; i++)
735 coda_write(dev, tiled_map->xy2ra_map[i],
736 CODA9_GDI_XY2_RAS_0 + 4 * i);
737 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
738 for (i = 0; i < 32; i++)
739 coda_write(dev, tiled_map->rbc2axi_map[i],
740 CODA9_GDI_RBC2_AXI_0 + 4 * i);
741}
742
Javier Martin186b2502012-07-26 05:53:35 -0300743/*
744 * Mem-to-mem operations.
745 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300746
747static void coda_device_run(void *m2m_priv)
748{
749 struct coda_ctx *ctx = m2m_priv;
750 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300751
752 queue_work(dev->workqueue, &ctx->pic_run_work);
753}
754
Philipp Zabel32b6f202014-07-11 06:36:20 -0300755static void coda_pic_run_work(struct work_struct *work)
756{
757 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
758 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300759 int ret;
760
761 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300762 mutex_lock(&dev->coda_mutex);
763
Philipp Zabela1192a12014-07-23 12:28:40 -0300764 ret = ctx->ops->prepare_run(ctx);
765 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
766 mutex_unlock(&dev->coda_mutex);
767 mutex_unlock(&ctx->buffer_mutex);
768 /* job_finish scheduled by prepare_decode */
769 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300770 }
771
Philipp Zabelf23797b2014-08-06 08:02:23 -0300772 if (!wait_for_completion_timeout(&ctx->completion,
773 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300774 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300775
776 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300777
778 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300779 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300780 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300781 }
782
783 if (ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out))
784 queue_work(dev->workqueue, &ctx->seq_end_work);
785
786 mutex_unlock(&dev->coda_mutex);
787 mutex_unlock(&ctx->buffer_mutex);
788
Philipp Zabel14604e32014-07-11 06:36:22 -0300789 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300790}
791
792static int coda_job_ready(void *m2m_priv)
793{
794 struct coda_ctx *ctx = m2m_priv;
795
796 /*
797 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300798 * and 1 frame are needed. In the decoder case,
799 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300800 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300801 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300802 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300803 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
804 "not ready: not enough video buffers.\n");
805 return 0;
806 }
807
Philipp Zabel14604e32014-07-11 06:36:22 -0300808 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300809 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
810 "not ready: not enough video capture buffers.\n");
811 return 0;
812 }
813
Philipp Zabel84e23652014-07-11 06:36:34 -0300814 if (ctx->hold ||
Philipp Zabel918c66f2013-06-27 06:59:01 -0300815 ((ctx->inst_type == CODA_INST_DECODER) &&
816 (coda_get_bitstream_payload(ctx) < 512) &&
817 !(ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG))) {
818 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
819 "%d: not ready: not enough bitstream data.\n",
820 ctx->idx);
821 return 0;
822 }
823
Philipp Zabel3e748262013-05-23 10:43:01 -0300824 if (ctx->aborting) {
825 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
826 "not ready: aborting\n");
827 return 0;
828 }
829
Javier Martin186b2502012-07-26 05:53:35 -0300830 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
831 "job ready\n");
832 return 1;
833}
834
835static void coda_job_abort(void *priv)
836{
837 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -0300838
839 ctx->aborting = 1;
840
841 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
842 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -0300843}
844
845static void coda_lock(void *m2m_priv)
846{
847 struct coda_ctx *ctx = m2m_priv;
848 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300849
Javier Martin186b2502012-07-26 05:53:35 -0300850 mutex_lock(&pcdev->dev_mutex);
851}
852
853static void coda_unlock(void *m2m_priv)
854{
855 struct coda_ctx *ctx = m2m_priv;
856 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -0300857
Javier Martin186b2502012-07-26 05:53:35 -0300858 mutex_unlock(&pcdev->dev_mutex);
859}
860
Philipp Zabel814c3762014-07-18 07:22:45 -0300861static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -0300862 .device_run = coda_device_run,
863 .job_ready = coda_job_ready,
864 .job_abort = coda_job_abort,
865 .lock = coda_lock,
866 .unlock = coda_unlock,
867};
868
Philipp Zabel89548442014-07-11 06:36:17 -0300869static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
870{
871 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
872 int luma_map, chro_map, i;
873
874 memset(tiled_map, 0, sizeof(*tiled_map));
875
876 luma_map = 64;
877 chro_map = 64;
878 tiled_map->map_type = tiled_map_type;
879 for (i = 0; i < 16; i++)
880 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
881 for (i = 0; i < 4; i++)
882 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
883 for (i = 0; i < 16; i++)
884 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
885
886 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
887 tiled_map->xy2rbc_config = 0;
888 } else {
889 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
890 tiled_map_type);
891 return;
892 }
893}
894
Javier Martin186b2502012-07-26 05:53:35 -0300895static void set_default_params(struct coda_ctx *ctx)
896{
Philipp Zabel121cacf2014-07-18 07:22:42 -0300897 u32 src_fourcc, dst_fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300898 int max_w;
899 int max_h;
900
Philipp Zabel121cacf2014-07-18 07:22:42 -0300901 if (ctx->inst_type == CODA_INST_ENCODER) {
902 src_fourcc = V4L2_PIX_FMT_YUV420;
903 dst_fourcc = V4L2_PIX_FMT_H264;
904 } else {
905 src_fourcc = V4L2_PIX_FMT_H264;
906 dst_fourcc = V4L2_PIX_FMT_YUV420;
907 }
908 ctx->codec = coda_find_codec(ctx->dev, src_fourcc, dst_fourcc);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300909 max_w = ctx->codec->max_w;
910 max_h = ctx->codec->max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300911
Philipp Zabel121cacf2014-07-18 07:22:42 -0300912 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -0300913 ctx->colorspace = V4L2_COLORSPACE_REC709;
914 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -0300915
916 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -0300917 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
918 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
919 ctx->q_data[V4L2_M2M_SRC].width = max_w;
920 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300921 ctx->q_data[V4L2_M2M_DST].width = max_w;
922 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -0300923 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
924 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
925 ctx->q_data[V4L2_M2M_SRC].sizeimage = (max_w * max_h * 3) / 2;
926 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
927 ctx->q_data[V4L2_M2M_DST].sizeimage = CODA_MAX_FRAME_SIZE;
928 } else {
929 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
930 ctx->q_data[V4L2_M2M_SRC].sizeimage = CODA_MAX_FRAME_SIZE;
931 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
932 ctx->q_data[V4L2_M2M_DST].sizeimage = (max_w * max_h * 3) / 2;
933 }
Philipp Zabel52c41672014-07-11 06:36:19 -0300934 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
935 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
936 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
937 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -0300938
939 if (ctx->dev->devtype->product == CODA_960)
940 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -0300941}
942
943/*
944 * Queue operations
945 */
946static int coda_queue_setup(struct vb2_queue *vq,
947 const struct v4l2_format *fmt,
948 unsigned int *nbuffers, unsigned int *nplanes,
949 unsigned int sizes[], void *alloc_ctxs[])
950{
951 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -0300952 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -0300953 unsigned int size;
954
Philipp Zabele34db062012-08-29 08:22:00 -0300955 q_data = get_q_data(ctx, vq->type);
956 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -0300957
958 *nplanes = 1;
959 sizes[0] = size;
960
961 alloc_ctxs[0] = ctx->dev->alloc_ctx;
962
963 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
964 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
965
966 return 0;
967}
968
969static int coda_buf_prepare(struct vb2_buffer *vb)
970{
971 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
972 struct coda_q_data *q_data;
973
974 q_data = get_q_data(ctx, vb->vb2_queue->type);
975
976 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
977 v4l2_warn(&ctx->dev->v4l2_dev,
978 "%s data will not fit into plane (%lu < %lu)\n",
979 __func__, vb2_plane_size(vb, 0),
980 (long)q_data->sizeimage);
981 return -EINVAL;
982 }
983
Javier Martin186b2502012-07-26 05:53:35 -0300984 return 0;
985}
986
987static void coda_buf_queue(struct vb2_buffer *vb)
988{
989 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300990 struct coda_q_data *q_data;
991
992 q_data = get_q_data(ctx, vb->vb2_queue->type);
993
994 /*
995 * In the decoder case, immediately try to copy the buffer into the
996 * bitstream ringbuffer and mark it as ready to be dequeued.
997 */
998 if (q_data->fourcc == V4L2_PIX_FMT_H264 &&
999 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1000 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001001 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001002 * the stream end
1003 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001004 if (vb2_get_plane_payload(vb, 0) == 0)
1005 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001006 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001007 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001008 if (vb2_is_streaming(vb->vb2_queue))
1009 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001010 mutex_unlock(&ctx->bitstream_mutex);
1011 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001012 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001013 }
Javier Martin186b2502012-07-26 05:53:35 -03001014}
1015
Philipp Zabel79924ca2014-07-23 12:28:45 -03001016int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1017 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001018{
1019 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1020 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001021 if (!buf->vaddr) {
1022 v4l2_err(&dev->v4l2_dev,
1023 "Failed to allocate %s buffer of size %u\n",
1024 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001025 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001026 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001027
1028 buf->size = size;
1029
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001030 if (name && parent) {
1031 buf->blob.data = buf->vaddr;
1032 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001033 buf->dentry = debugfs_create_blob(name, 0644, parent,
1034 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001035 if (!buf->dentry)
1036 dev_warn(&dev->plat_dev->dev,
1037 "failed to create debugfs entry %s\n", name);
1038 }
1039
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001040 return 0;
1041}
1042
Philipp Zabel79924ca2014-07-23 12:28:45 -03001043void coda_free_aux_buf(struct coda_dev *dev,
1044 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001045{
1046 if (buf->vaddr) {
1047 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1048 buf->vaddr, buf->paddr);
1049 buf->vaddr = NULL;
1050 buf->size = 0;
1051 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001052 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001053}
1054
Javier Martin186b2502012-07-26 05:53:35 -03001055static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1056{
1057 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1058 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001059 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001060 struct vb2_buffer *buf;
Philipp Zabelec25f682012-07-20 08:54:29 -03001061 u32 dst_fourcc;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001062 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001063
Philipp Zabelb96904e2013-05-23 10:42:58 -03001064 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001065 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1066 if (q_data_src->fourcc == V4L2_PIX_FMT_H264) {
Michael Olbricheabed932014-07-18 07:22:40 -03001067 /* copy the buffers that where queued before streamon */
1068 mutex_lock(&ctx->bitstream_mutex);
1069 coda_fill_bitstream(ctx);
1070 mutex_unlock(&ctx->bitstream_mutex);
1071
Philipp Zabelb9063522014-08-05 14:00:10 -03001072 if (coda_get_bitstream_payload(ctx) < 512) {
1073 ret = -EINVAL;
1074 goto err;
1075 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001076 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001077 if (count < 1) {
1078 ret = -EINVAL;
1079 goto err;
1080 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001081 }
1082
1083 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001084 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001085 if (count < 1) {
1086 ret = -EINVAL;
1087 goto err;
1088 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001089
1090 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001091 }
Javier Martin186b2502012-07-26 05:53:35 -03001092
Philipp Zabelb96904e2013-05-23 10:42:58 -03001093 /* Don't start the coda unless both queues are on */
1094 if (!(ctx->streamon_out & ctx->streamon_cap))
1095 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001096
Philipp Zabeleb107512013-09-30 10:34:45 -03001097 /* Allow decoder device_run with no new buffers queued */
1098 if (ctx->inst_type == CODA_INST_DECODER)
Philipp Zabel14604e32014-07-11 06:36:22 -03001099 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001100
Philipp Zabelb96904e2013-05-23 10:42:58 -03001101 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001102 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001103 dst_fourcc = q_data_dst->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -03001104
Philipp Zabelb96904e2013-05-23 10:42:58 -03001105 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1106 q_data_dst->fourcc);
1107 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001108 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001109 ret = -EINVAL;
1110 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001111 }
1112
Philipp Zabela1192a12014-07-23 12:28:40 -03001113 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001114 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001115 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001116 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001117 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001118 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001119 }
1120
Philipp Zabel89548442014-07-11 06:36:17 -03001121 ctx->initialized = 1;
1122 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001123
1124err:
1125 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1126 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1127 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1128 } else {
1129 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1130 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_DEQUEUED);
1131 }
1132 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001133}
1134
Hans Verkuile37559b2014-04-17 02:47:21 -03001135static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001136{
1137 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001138 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001139 struct vb2_buffer *buf;
Javier Martin186b2502012-07-26 05:53:35 -03001140
1141 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001142 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001143 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001144 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001145
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001146 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001147
Philipp Zabel918c66f2013-06-27 06:59:01 -03001148 ctx->isequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001149
1150 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1151 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001152 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001153 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001154 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001155 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001156
1157 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001158 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001159
1160 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1161 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001162 }
1163
Philipp Zabel918c66f2013-06-27 06:59:01 -03001164 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel846ced92014-07-11 06:36:31 -03001165 struct coda_timestamp *ts;
1166
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001167 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001168 while (!list_empty(&ctx->timestamp_list)) {
1169 ts = list_first_entry(&ctx->timestamp_list,
1170 struct coda_timestamp, list);
1171 list_del(&ts->list);
1172 kfree(ts);
1173 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001174 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001175 kfifo_init(&ctx->bitstream_fifo,
1176 ctx->bitstream.vaddr, ctx->bitstream.size);
1177 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001178 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001179 }
Javier Martin186b2502012-07-26 05:53:35 -03001180}
1181
Philipp Zabel121cacf2014-07-18 07:22:42 -03001182static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001183 .queue_setup = coda_queue_setup,
1184 .buf_prepare = coda_buf_prepare,
1185 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001186 .start_streaming = coda_start_streaming,
1187 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001188 .wait_prepare = vb2_ops_wait_prepare,
1189 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001190};
1191
1192static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1193{
1194 struct coda_ctx *ctx =
1195 container_of(ctrl->handler, struct coda_ctx, ctrls);
1196
1197 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1198 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1199
1200 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001201 case V4L2_CID_HFLIP:
1202 if (ctrl->val)
1203 ctx->params.rot_mode |= CODA_MIR_HOR;
1204 else
1205 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1206 break;
1207 case V4L2_CID_VFLIP:
1208 if (ctrl->val)
1209 ctx->params.rot_mode |= CODA_MIR_VER;
1210 else
1211 ctx->params.rot_mode &= ~CODA_MIR_VER;
1212 break;
Javier Martin186b2502012-07-26 05:53:35 -03001213 case V4L2_CID_MPEG_VIDEO_BITRATE:
1214 ctx->params.bitrate = ctrl->val / 1000;
1215 break;
1216 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1217 ctx->params.gop_size = ctrl->val;
1218 break;
1219 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1220 ctx->params.h264_intra_qp = ctrl->val;
1221 break;
1222 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1223 ctx->params.h264_inter_qp = ctrl->val;
1224 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001225 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1226 ctx->params.h264_min_qp = ctrl->val;
1227 break;
1228 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1229 ctx->params.h264_max_qp = ctrl->val;
1230 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001231 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1232 ctx->params.h264_deblk_alpha = ctrl->val;
1233 break;
1234 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1235 ctx->params.h264_deblk_beta = ctrl->val;
1236 break;
1237 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1238 ctx->params.h264_deblk_enabled = (ctrl->val ==
1239 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1240 break;
Javier Martin186b2502012-07-26 05:53:35 -03001241 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1242 ctx->params.mpeg4_intra_qp = ctrl->val;
1243 break;
1244 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1245 ctx->params.mpeg4_inter_qp = ctrl->val;
1246 break;
1247 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1248 ctx->params.slice_mode = ctrl->val;
1249 break;
1250 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1251 ctx->params.slice_max_mb = ctrl->val;
1252 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001253 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1254 ctx->params.slice_max_bits = ctrl->val * 8;
1255 break;
Javier Martin186b2502012-07-26 05:53:35 -03001256 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1257 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001258 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1259 ctx->params.intra_refresh = ctrl->val;
1260 break;
Javier Martin186b2502012-07-26 05:53:35 -03001261 default:
1262 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1263 "Invalid control, id=%d, val=%d\n",
1264 ctrl->id, ctrl->val);
1265 return -EINVAL;
1266 }
1267
1268 return 0;
1269}
1270
Philipp Zabel814c3762014-07-18 07:22:45 -03001271static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001272 .s_ctrl = coda_s_ctrl,
1273};
1274
1275static int coda_ctrls_setup(struct coda_ctx *ctx)
1276{
1277 v4l2_ctrl_handler_init(&ctx->ctrls, 9);
1278
1279 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001280 V4L2_CID_HFLIP, 0, 1, 1, 0);
1281 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1282 V4L2_CID_VFLIP, 0, 1, 1, 0);
1283 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001284 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1285 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1286 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1287 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001288 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001289 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001290 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001291 if (ctx->dev->devtype->product != CODA_960) {
1292 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1293 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1294 }
1295 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1296 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001297 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001298 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1299 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1300 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1301 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1302 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1303 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1304 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1305 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001306 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1307 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1308 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1309 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1310 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001311 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1312 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001313 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1314 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001315 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001316 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1317 500);
Javier Martin186b2502012-07-26 05:53:35 -03001318 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1319 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1320 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1321 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1322 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001323 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001324 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1325 1920 * 1088 / 256, 1, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001326
1327 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001328 v4l2_err(&ctx->dev->v4l2_dev,
1329 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001330 ctx->ctrls.error);
1331 return -EINVAL;
1332 }
1333
1334 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1335}
1336
Philipp Zabel121cacf2014-07-18 07:22:42 -03001337static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001338{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001339 vq->drv_priv = ctx;
1340 vq->ops = &coda_qops;
1341 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1342 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1343 vq->lock = &ctx->dev->dev_mutex;
1344
1345 return vb2_queue_init(vq);
1346}
1347
Philipp Zabel79924ca2014-07-23 12:28:45 -03001348int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1349 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001350{
Javier Martin186b2502012-07-26 05:53:35 -03001351 int ret;
1352
Javier Martin186b2502012-07-26 05:53:35 -03001353 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001354 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001355 src_vq->mem_ops = &vb2_dma_contig_memops;
1356
Philipp Zabel121cacf2014-07-18 07:22:42 -03001357 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001358 if (ret)
1359 return ret;
1360
Javier Martin186b2502012-07-26 05:53:35 -03001361 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001362 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001363 dst_vq->mem_ops = &vb2_dma_contig_memops;
1364
Philipp Zabel121cacf2014-07-18 07:22:42 -03001365 return coda_queue_init(priv, dst_vq);
1366}
1367
Philipp Zabel79924ca2014-07-23 12:28:45 -03001368int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1369 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001370{
1371 int ret;
1372
1373 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1374 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1375 src_vq->mem_ops = &vb2_dma_contig_memops;
1376
1377 ret = coda_queue_init(priv, src_vq);
1378 if (ret)
1379 return ret;
1380
1381 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1382 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1383 dst_vq->mem_ops = &vb2_dma_contig_memops;
1384
1385 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001386}
1387
Philipp Zabele11f3e62012-07-25 09:16:58 -03001388static int coda_next_free_instance(struct coda_dev *dev)
1389{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001390 int idx = ffz(dev->instance_mask);
1391
1392 if ((idx < 0) ||
1393 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1394 return -EBUSY;
1395
1396 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001397}
1398
Philipp Zabela1192a12014-07-23 12:28:40 -03001399static int coda_open(struct file *file, enum coda_inst_type inst_type,
1400 const struct coda_context_ops *ctx_ops)
Javier Martin186b2502012-07-26 05:53:35 -03001401{
1402 struct coda_dev *dev = video_drvdata(file);
1403 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001404 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001405 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001406 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001407
Philipp Zabelf23797b2014-08-06 08:02:23 -03001408 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001409 if (!ctx)
1410 return -ENOMEM;
1411
Fabio Estevamf82bc202013-08-21 11:14:16 -03001412 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001413 if (idx < 0) {
1414 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001415 goto err_coda_max;
1416 }
1417 set_bit(idx, &dev->instance_mask);
1418
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001419 name = kasprintf(GFP_KERNEL, "context%d", idx);
1420 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1421 kfree(name);
1422
Philipp Zabel121cacf2014-07-18 07:22:42 -03001423 ctx->inst_type = inst_type;
Philipp Zabela1192a12014-07-23 12:28:40 -03001424 ctx->ops = ctx_ops;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001425 init_completion(&ctx->completion);
1426 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabela1192a12014-07-23 12:28:40 -03001427 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001428 v4l2_fh_init(&ctx->fh, video_devdata(file));
1429 file->private_data = &ctx->fh;
1430 v4l2_fh_add(&ctx->fh);
1431 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001432 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001433 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001434 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001435 ctx->frame_mem_ctrl = 1 << 12;
1436 /* fallthrough */
1437 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001438 ctx->reg_idx = 0;
1439 break;
1440 default:
1441 ctx->reg_idx = idx;
1442 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001443
Philipp Zabel1e172732014-07-11 06:36:23 -03001444 /* Power up and upload firmware if necessary */
1445 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1446 if (ret < 0) {
1447 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1448 goto err_pm_get;
1449 }
1450
Fabio Estevam79830db2013-08-21 11:14:17 -03001451 ret = clk_prepare_enable(dev->clk_per);
1452 if (ret)
1453 goto err_clk_per;
1454
1455 ret = clk_prepare_enable(dev->clk_ahb);
1456 if (ret)
1457 goto err_clk_ahb;
1458
Javier Martin186b2502012-07-26 05:53:35 -03001459 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001460 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1461 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001462 if (IS_ERR(ctx->fh.m2m_ctx)) {
1463 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001464
1465 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1466 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001467 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001468 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001469
Javier Martin186b2502012-07-26 05:53:35 -03001470 ret = coda_ctrls_setup(ctx);
1471 if (ret) {
1472 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001473 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001474 }
1475
1476 ctx->fh.ctrl_handler = &ctx->ctrls;
1477
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001478 ret = coda_alloc_context_buf(ctx, &ctx->parabuf, CODA_PARA_BUF_SIZE,
1479 "parabuf");
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001480 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001481 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001482 goto err_dma_alloc;
Javier Martin186b2502012-07-26 05:53:35 -03001483 }
1484
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001485 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1486 ctx->bitstream.vaddr = dma_alloc_writecombine(&dev->plat_dev->dev,
1487 ctx->bitstream.size, &ctx->bitstream.paddr, GFP_KERNEL);
1488 if (!ctx->bitstream.vaddr) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001489 v4l2_err(&dev->v4l2_dev,
1490 "failed to allocate bitstream ringbuffer");
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001491 ret = -ENOMEM;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001492 goto err_dma_writecombine;
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001493 }
1494 kfifo_init(&ctx->bitstream_fifo,
1495 ctx->bitstream.vaddr, ctx->bitstream.size);
1496 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001497 mutex_init(&ctx->buffer_mutex);
Philipp Zabel846ced92014-07-11 06:36:31 -03001498 INIT_LIST_HEAD(&ctx->timestamp_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001499
Javier Martin186b2502012-07-26 05:53:35 -03001500 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001501 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001502 coda_unlock(ctx);
1503
Javier Martin186b2502012-07-26 05:53:35 -03001504 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1505 ctx->idx, ctx);
1506
1507 return 0;
1508
Fabio Estevamf82bc202013-08-21 11:14:16 -03001509err_dma_writecombine:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001510 if (ctx->dev->devtype->product == CODA_DX6)
1511 coda_free_aux_buf(dev, &ctx->workbuf);
1512 coda_free_aux_buf(dev, &ctx->parabuf);
1513err_dma_alloc:
1514 v4l2_ctrl_handler_free(&ctx->ctrls);
1515err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001516 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001517err_ctx_init:
1518 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001519err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001520 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001521err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001522 pm_runtime_put_sync(&dev->plat_dev->dev);
1523err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001524 v4l2_fh_del(&ctx->fh);
1525 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001526 clear_bit(ctx->idx, &dev->instance_mask);
1527err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001528 kfree(ctx);
1529 return ret;
1530}
1531
Philipp Zabel121cacf2014-07-18 07:22:42 -03001532static int coda_encoder_open(struct file *file)
1533{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001534 return coda_open(file, CODA_INST_ENCODER, &coda_bit_encode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001535}
1536
1537static int coda_decoder_open(struct file *file)
1538{
Philipp Zabel79924ca2014-07-23 12:28:45 -03001539 return coda_open(file, CODA_INST_DECODER, &coda_bit_decode_ops);
Philipp Zabel121cacf2014-07-18 07:22:42 -03001540}
1541
Javier Martin186b2502012-07-26 05:53:35 -03001542static int coda_release(struct file *file)
1543{
1544 struct coda_dev *dev = video_drvdata(file);
1545 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1546
1547 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1548 ctx);
1549
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001550 debugfs_remove_recursive(ctx->debugfs_entry);
1551
Philipp Zabel918c66f2013-06-27 06:59:01 -03001552 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001553 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001554
1555 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel32b6f202014-07-11 06:36:20 -03001556 if (ctx->initialized) {
1557 queue_work(dev->workqueue, &ctx->seq_end_work);
1558 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001559 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001560
Javier Martin186b2502012-07-26 05:53:35 -03001561 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001562 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001563 coda_unlock(ctx);
1564
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001565 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1566 ctx->bitstream.vaddr, ctx->bitstream.paddr);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001567 if (ctx->dev->devtype->product == CODA_DX6)
1568 coda_free_aux_buf(dev, &ctx->workbuf);
1569
1570 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03001571 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001572 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001573 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001574 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001575 v4l2_fh_del(&ctx->fh);
1576 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001577 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001578 if (ctx->ops->release)
1579 ctx->ops->release(ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001580 kfree(ctx);
1581
1582 return 0;
1583}
1584
Philipp Zabel121cacf2014-07-18 07:22:42 -03001585static const struct v4l2_file_operations coda_encoder_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001586 .owner = THIS_MODULE,
Philipp Zabel121cacf2014-07-18 07:22:42 -03001587 .open = coda_encoder_open,
1588 .release = coda_release,
1589 .poll = v4l2_m2m_fop_poll,
1590 .unlocked_ioctl = video_ioctl2,
1591 .mmap = v4l2_m2m_fop_mmap,
1592};
1593
1594static const struct v4l2_file_operations coda_decoder_fops = {
1595 .owner = THIS_MODULE,
1596 .open = coda_decoder_open,
Javier Martin186b2502012-07-26 05:53:35 -03001597 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001598 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001599 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001600 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001601};
1602
Philipp Zabel87048bb2012-07-02 07:03:43 -03001603static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001604{
Javier Martin186b2502012-07-26 05:53:35 -03001605 u32 data;
1606 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001607 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001608
Fabio Estevam79830db2013-08-21 11:14:17 -03001609 ret = clk_prepare_enable(dev->clk_per);
1610 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001611 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001612
1613 ret = clk_prepare_enable(dev->clk_ahb);
1614 if (ret)
1615 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001616
Philipp Zabel8f452842014-07-11 06:36:35 -03001617 if (dev->rstc)
1618 reset_control_reset(dev->rstc);
1619
Javier Martin186b2502012-07-26 05:53:35 -03001620 /*
1621 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001622 * The 16-bit chars in the code buffer are in memory access
1623 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001624 * Data in this SRAM survives a reboot.
1625 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001626 p = (u16 *)dev->codebuf.vaddr;
1627 if (dev->devtype->product == CODA_DX6) {
1628 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1629 data = CODA_DOWN_ADDRESS_SET(i) |
1630 CODA_DOWN_DATA_SET(p[i ^ 1]);
1631 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1632 }
1633 } else {
1634 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1635 data = CODA_DOWN_ADDRESS_SET(i) |
1636 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1637 3 - (i % 4)]);
1638 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1639 }
Javier Martin186b2502012-07-26 05:53:35 -03001640 }
Javier Martin186b2502012-07-26 05:53:35 -03001641
Philipp Zabel9acf7692013-05-23 10:42:56 -03001642 /* Clear registers */
1643 for (i = 0; i < 64; i++)
1644 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1645
Javier Martin186b2502012-07-26 05:53:35 -03001646 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001647 if (dev->devtype->product == CODA_960 ||
1648 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001649 coda_write(dev, dev->tempbuf.paddr,
1650 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001651 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001652 } else {
1653 coda_write(dev, dev->workbuf.paddr,
1654 CODA_REG_BIT_WORK_BUF_ADDR);
1655 }
Javier Martin186b2502012-07-26 05:53:35 -03001656 coda_write(dev, dev->codebuf.paddr,
1657 CODA_REG_BIT_CODE_BUF_ADDR);
1658 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1659
1660 /* Set default values */
1661 switch (dev->devtype->product) {
1662 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001663 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1664 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001665 break;
1666 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001667 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1668 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001669 }
Philipp Zabel89548442014-07-11 06:36:17 -03001670 if (dev->devtype->product == CODA_960)
1671 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1672 else
1673 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001674
1675 if (dev->devtype->product != CODA_DX6)
1676 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1677
Javier Martin186b2502012-07-26 05:53:35 -03001678 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1679 CODA_REG_BIT_INT_ENABLE);
1680
1681 /* Reset VPU and start processor */
1682 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1683 data |= CODA_REG_RESET_ENABLE;
1684 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1685 udelay(10);
1686 data &= ~CODA_REG_RESET_ENABLE;
1687 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1688 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1689
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001690 clk_disable_unprepare(dev->clk_ahb);
1691 clk_disable_unprepare(dev->clk_per);
1692
1693 return 0;
1694
1695err_clk_ahb:
1696 clk_disable_unprepare(dev->clk_per);
1697err_clk_per:
1698 return ret;
1699}
1700
Philipp Zabel121cacf2014-07-18 07:22:42 -03001701static int coda_register_device(struct coda_dev *dev, struct video_device *vfd)
1702{
1703 vfd->release = video_device_release_empty,
1704 vfd->lock = &dev->dev_mutex;
1705 vfd->v4l2_dev = &dev->v4l2_dev;
1706 vfd->vfl_dir = VFL_DIR_M2M;
1707 video_set_drvdata(vfd, dev);
1708
Philipp Zabela188a662014-08-05 14:00:20 -03001709 /* Not applicable, use the selection API instead */
1710 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1711 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1712 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1713
Philipp Zabel121cacf2014-07-18 07:22:42 -03001714 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1715}
1716
Javier Martin186b2502012-07-26 05:53:35 -03001717static void coda_fw_callback(const struct firmware *fw, void *context)
1718{
1719 struct coda_dev *dev = context;
1720 struct platform_device *pdev = dev->plat_dev;
1721 int ret;
1722
1723 if (!fw) {
1724 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001725 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001726 }
1727
1728 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001729 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1730 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001731 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001732 dev_err(&pdev->dev, "failed to allocate code buffer\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001733 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001734 }
1735
Philipp Zabel87048bb2012-07-02 07:03:43 -03001736 /* Copy the whole firmware image to the code buffer */
1737 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1738 release_firmware(fw);
1739
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001740 ret = coda_hw_init(dev);
1741 if (ret < 0) {
1742 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1743 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001744 }
1745
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001746 ret = coda_check_firmware(dev);
1747 if (ret < 0)
1748 goto put_pm;
1749
Javier Martin186b2502012-07-26 05:53:35 -03001750 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1751 if (IS_ERR(dev->alloc_ctx)) {
1752 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001753 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001754 }
1755
1756 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1757 if (IS_ERR(dev->m2m_dev)) {
1758 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1759 goto rel_ctx;
1760 }
1761
Philipp Zabel121cacf2014-07-18 07:22:42 -03001762 dev->vfd[0].fops = &coda_encoder_fops,
1763 dev->vfd[0].ioctl_ops = &coda_ioctl_ops;
1764 snprintf(dev->vfd[0].name, sizeof(dev->vfd[0].name), "coda-encoder");
1765 ret = coda_register_device(dev, &dev->vfd[0]);
Javier Martin186b2502012-07-26 05:53:35 -03001766 if (ret) {
Philipp Zabel121cacf2014-07-18 07:22:42 -03001767 v4l2_err(&dev->v4l2_dev,
1768 "Failed to register encoder video device\n");
Javier Martin186b2502012-07-26 05:53:35 -03001769 goto rel_m2m;
1770 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03001771
1772 dev->vfd[1].fops = &coda_decoder_fops,
1773 dev->vfd[1].ioctl_ops = &coda_ioctl_ops;
1774 snprintf(dev->vfd[1].name, sizeof(dev->vfd[1].name), "coda-decoder");
1775 ret = coda_register_device(dev, &dev->vfd[1]);
1776 if (ret) {
1777 v4l2_err(&dev->v4l2_dev,
1778 "Failed to register decoder video device\n");
1779 goto rel_m2m;
1780 }
1781
1782 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
1783 dev->vfd[0].num, dev->vfd[1].num);
Javier Martin186b2502012-07-26 05:53:35 -03001784
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001785 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001786 return;
1787
1788rel_m2m:
1789 v4l2_m2m_release(dev->m2m_dev);
1790rel_ctx:
1791 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001792put_pm:
1793 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001794}
1795
1796static int coda_firmware_request(struct coda_dev *dev)
1797{
1798 char *fw = dev->devtype->firmware;
1799
1800 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1801 coda_product_name(dev->devtype->product));
1802
1803 return request_firmware_nowait(THIS_MODULE, true,
1804 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
1805}
1806
1807enum coda_platform {
1808 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001809 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03001810 CODA_IMX6Q,
1811 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03001812};
1813
Emil Goodec06d8752012-08-14 17:44:42 -03001814static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03001815 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001816 .firmware = "v4l-codadx6-imx27.bin",
1817 .product = CODA_DX6,
1818 .codecs = codadx6_codecs,
1819 .num_codecs = ARRAY_SIZE(codadx6_codecs),
1820 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001821 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03001822 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001823 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001824 .firmware = "v4l-coda7541-imx53.bin",
1825 .product = CODA_7541,
1826 .codecs = coda7_codecs,
1827 .num_codecs = ARRAY_SIZE(coda7_codecs),
1828 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001829 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001830 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001831 },
Philipp Zabel89548442014-07-11 06:36:17 -03001832 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001833 .firmware = "v4l-coda960-imx6q.bin",
1834 .product = CODA_960,
1835 .codecs = coda9_codecs,
1836 .num_codecs = ARRAY_SIZE(coda9_codecs),
1837 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001838 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001839 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03001840 },
1841 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001842 .firmware = "v4l-coda960-imx6dl.bin",
1843 .product = CODA_960,
1844 .codecs = coda9_codecs,
1845 .num_codecs = ARRAY_SIZE(coda9_codecs),
1846 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001847 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03001848 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03001849 },
Javier Martin186b2502012-07-26 05:53:35 -03001850};
1851
1852static struct platform_device_id coda_platform_ids[] = {
1853 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
Fabio Estevam4e44cd02012-10-10 00:07:38 -03001854 { .name = "coda-imx53", .driver_data = CODA_IMX53 },
Javier Martin186b2502012-07-26 05:53:35 -03001855 { /* sentinel */ }
1856};
1857MODULE_DEVICE_TABLE(platform, coda_platform_ids);
1858
1859#ifdef CONFIG_OF
1860static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03001861 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03001862 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03001863 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
1864 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03001865 { /* sentinel */ }
1866};
1867MODULE_DEVICE_TABLE(of, coda_dt_ids);
1868#endif
1869
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001870static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03001871{
1872 const struct of_device_id *of_id =
1873 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
1874 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07001875 struct coda_platform_data *pdata = pdev->dev.platform_data;
1876 struct device_node *np = pdev->dev.of_node;
1877 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03001878 struct coda_dev *dev;
1879 struct resource *res;
1880 int ret, irq;
1881
Philipp Zabelf23797b2014-08-06 08:02:23 -03001882 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03001883 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03001884 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03001885
1886 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001887 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001888
1889 dev->plat_dev = pdev;
1890 dev->clk_per = devm_clk_get(&pdev->dev, "per");
1891 if (IS_ERR(dev->clk_per)) {
1892 dev_err(&pdev->dev, "Could not get per clock\n");
1893 return PTR_ERR(dev->clk_per);
1894 }
1895
1896 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1897 if (IS_ERR(dev->clk_ahb)) {
1898 dev_err(&pdev->dev, "Could not get ahb clock\n");
1899 return PTR_ERR(dev->clk_ahb);
1900 }
1901
1902 /* Get memory for physical registers */
1903 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03001904 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1905 if (IS_ERR(dev->regs_base))
1906 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03001907
1908 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03001909 irq = platform_get_irq_byname(pdev, "bit");
1910 if (irq < 0)
1911 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001912 if (irq < 0) {
1913 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03001914 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03001915 }
1916
Fabio Estevam08c8d142014-06-04 15:46:24 -03001917 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
1918 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
1919 if (ret < 0) {
1920 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
1921 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03001922 }
1923
Philipp Zabelee27aa92014-07-24 16:50:03 -03001924 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03001925 if (IS_ERR(dev->rstc)) {
1926 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03001927 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03001928 dev->rstc = NULL;
1929 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001930 dev_err(&pdev->dev, "failed get reset control: %d\n",
1931 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03001932 return ret;
1933 }
1934 }
1935
Philipp Zabel657eee72013-04-29 16:17:14 -07001936 /* Get IRAM pool from device tree or platform data */
1937 pool = of_get_named_gen_pool(np, "iram", 0);
1938 if (!pool && pdata)
1939 pool = dev_get_gen_pool(pdata->iram_dev);
1940 if (!pool) {
1941 dev_err(&pdev->dev, "iram pool not available\n");
1942 return -ENOMEM;
1943 }
1944 dev->iram_pool = pool;
1945
Javier Martin186b2502012-07-26 05:53:35 -03001946 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1947 if (ret)
1948 return ret;
1949
1950 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03001951 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03001952
1953 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
1954
1955 if (of_id) {
1956 dev->devtype = of_id->data;
1957 } else if (pdev_id) {
1958 dev->devtype = &coda_devdata[pdev_id->driver_data];
1959 } else {
1960 v4l2_device_unregister(&dev->v4l2_dev);
1961 return -EINVAL;
1962 }
1963
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001964 dev->debugfs_root = debugfs_create_dir("coda", NULL);
1965 if (!dev->debugfs_root)
1966 dev_warn(&pdev->dev, "failed to create debugfs root\n");
1967
Javier Martin186b2502012-07-26 05:53:35 -03001968 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001969 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001970 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03001971 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001972 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001973 if (ret < 0) {
1974 dev_err(&pdev->dev, "failed to allocate work buffer\n");
1975 v4l2_device_unregister(&dev->v4l2_dev);
1976 return ret;
1977 }
Javier Martin186b2502012-07-26 05:53:35 -03001978 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001979
1980 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001981 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03001982 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001983 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001984 if (ret < 0) {
1985 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
1986 v4l2_device_unregister(&dev->v4l2_dev);
1987 return ret;
1988 }
Javier Martin186b2502012-07-26 05:53:35 -03001989 }
1990
Philipp Zabel401e9722014-07-11 06:36:43 -03001991 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03001992 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
1993 &dev->iram.paddr);
1994 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03001995 dev_warn(&pdev->dev, "unable to alloc iram\n");
1996 } else {
1997 dev->iram.blob.data = dev->iram.vaddr;
1998 dev->iram.blob.size = dev->iram.size;
1999 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2000 dev->debugfs_root,
2001 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002002 }
2003
Philipp Zabel32b6f202014-07-11 06:36:20 -03002004 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2005 if (!dev->workqueue) {
2006 dev_err(&pdev->dev, "unable to alloc workqueue\n");
2007 return -ENOMEM;
2008 }
2009
Javier Martin186b2502012-07-26 05:53:35 -03002010 platform_set_drvdata(pdev, dev);
2011
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002012 /*
2013 * Start activated so we can directly call coda_hw_init in
2014 * coda_fw_callback regardless of whether CONFIG_PM_RUNTIME is
2015 * enabled or whether the device is associated with a PM domain.
2016 */
2017 pm_runtime_get_noresume(&pdev->dev);
2018 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002019 pm_runtime_enable(&pdev->dev);
2020
Javier Martin186b2502012-07-26 05:53:35 -03002021 return coda_firmware_request(dev);
2022}
2023
2024static int coda_remove(struct platform_device *pdev)
2025{
2026 struct coda_dev *dev = platform_get_drvdata(pdev);
2027
Philipp Zabel121cacf2014-07-18 07:22:42 -03002028 video_unregister_device(&dev->vfd[0]);
2029 video_unregister_device(&dev->vfd[1]);
Javier Martin186b2502012-07-26 05:53:35 -03002030 if (dev->m2m_dev)
2031 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002032 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002033 if (dev->alloc_ctx)
2034 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2035 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002036 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002037 if (dev->iram.vaddr)
2038 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2039 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002040 coda_free_aux_buf(dev, &dev->codebuf);
2041 coda_free_aux_buf(dev, &dev->tempbuf);
2042 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002043 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002044 return 0;
2045}
2046
Philipp Zabel1e172732014-07-11 06:36:23 -03002047#ifdef CONFIG_PM_RUNTIME
2048static int coda_runtime_resume(struct device *dev)
2049{
2050 struct coda_dev *cdev = dev_get_drvdata(dev);
2051 int ret = 0;
2052
Philipp Zabel65919e62014-07-18 07:22:36 -03002053 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002054 ret = coda_hw_init(cdev);
2055 if (ret)
2056 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2057 }
2058
2059 return ret;
2060}
2061#endif
2062
2063static const struct dev_pm_ops coda_pm_ops = {
2064 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2065};
2066
Javier Martin186b2502012-07-26 05:53:35 -03002067static struct platform_driver coda_driver = {
2068 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002069 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002070 .driver = {
2071 .name = CODA_NAME,
2072 .owner = THIS_MODULE,
2073 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002074 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002075 },
2076 .id_table = coda_platform_ids,
2077};
2078
2079module_platform_driver(coda_driver);
2080
2081MODULE_LICENSE("GPL");
2082MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2083MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");