blob: a7cab14d10c969bd4a5a361bc0d0828a2fb48d41 [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 Zabelcde29ef2015-07-16 13:13:24 -030018#include <linux/gcd.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070019#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030020#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030023#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030024#include <linux/module.h>
25#include <linux/of_device.h>
26#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030027#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030028#include <linux/slab.h>
29#include <linux/videodev2.h>
30#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070031#include <linux/platform_data/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030032#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030033
34#include <media/v4l2-ctrls.h>
35#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030036#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030037#include <media/v4l2-ioctl.h>
38#include <media/v4l2-mem2mem.h>
39#include <media/videobuf2-core.h>
40#include <media/videobuf2-dma-contig.h>
Philipp Zabelbb04aa62015-01-23 13:51:30 -030041#include <media/videobuf2-vmalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030042
Philipp Zabela2b3e462014-07-23 12:28:39 -030043#include "coda.h"
Javier Martin186b2502012-07-26 05:53:35 -030044
45#define CODA_NAME "coda"
46
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030047#define CODADX6_MAX_INSTANCES 4
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030048#define CODA_MAX_FORMATS 4
Javier Martin186b2502012-07-26 05:53:35 -030049
Javier Martin186b2502012-07-26 05:53:35 -030050#define CODA_ISRAM_SIZE (2048 * 2)
51
Javier Martin186b2502012-07-26 05:53:35 -030052#define MIN_W 176
53#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030054
55#define S_ALIGN 1 /* multiple of 2 */
56#define W_ALIGN 1 /* multiple of 2 */
57#define H_ALIGN 1 /* multiple of 2 */
58
59#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
60
Philipp Zabel79924ca2014-07-23 12:28:45 -030061int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030062module_param(coda_debug, int, 0644);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030063MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
Javier Martin186b2502012-07-26 05:53:35 -030064
Philipp Zabela2b3e462014-07-23 12:28:39 -030065void coda_write(struct coda_dev *dev, u32 data, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030066{
Philipp Zabeld60b18b2014-08-05 14:00:15 -030067 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030068 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
69 writel(data, dev->regs_base + reg);
70}
71
Philipp Zabela2b3e462014-07-23 12:28:39 -030072unsigned int coda_read(struct coda_dev *dev, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030073{
74 u32 data;
Philipp Zabelf23797b2014-08-06 08:02:23 -030075
Javier Martin186b2502012-07-26 05:53:35 -030076 data = readl(dev->regs_base + reg);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030077 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030078 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
79 return data;
80}
81
Philipp Zabel856d7d92014-09-29 09:53:44 -030082void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
83 struct vb2_buffer *buf, unsigned int reg_y)
84{
85 u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
86 u32 base_cb, base_cr;
87
88 switch (q_data->fourcc) {
89 case V4L2_PIX_FMT_YVU420:
90 /* Switch Cb and Cr for YVU420 format */
91 base_cr = base_y + q_data->bytesperline * q_data->height;
92 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
93 break;
94 case V4L2_PIX_FMT_YUV420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -030095 case V4L2_PIX_FMT_NV12:
Philipp Zabel856d7d92014-09-29 09:53:44 -030096 default:
97 base_cb = base_y + q_data->bytesperline * q_data->height;
98 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
99 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300100 case V4L2_PIX_FMT_YUV422P:
101 base_cb = base_y + q_data->bytesperline * q_data->height;
102 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
Philipp Zabel856d7d92014-09-29 09:53:44 -0300103 }
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
Philipp Zabelb96904e2013-05-23 10:42:58 -0300110#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
111 { mode, src_fourcc, dst_fourcc, max_w, max_h }
112
113/*
114 * Arrays of codecs supported by each given version of Coda:
115 * i.MX27 -> codadx6
116 * i.MX5x -> coda7
117 * i.MX6 -> coda960
118 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
119 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300120static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300121 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
122 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300123};
124
Philipp Zabel814c3762014-07-18 07:22:45 -0300125static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300126 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
127 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300128 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300129 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
130 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300131 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300132};
133
Philipp Zabel814c3762014-07-18 07:22:45 -0300134static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300135 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
136 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
137 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
138 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300139};
140
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300141struct coda_video_device {
142 const char *name;
143 enum coda_inst_type type;
144 const struct coda_context_ops *ops;
Philipp Zabela22496c2015-01-23 13:51:33 -0300145 bool direct;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300146 u32 src_formats[CODA_MAX_FORMATS];
147 u32 dst_formats[CODA_MAX_FORMATS];
148};
149
150static const struct coda_video_device coda_bit_encoder = {
151 .name = "coda-encoder",
152 .type = CODA_INST_ENCODER,
153 .ops = &coda_bit_encode_ops,
154 .src_formats = {
155 V4L2_PIX_FMT_YUV420,
156 V4L2_PIX_FMT_YVU420,
157 V4L2_PIX_FMT_NV12,
158 },
159 .dst_formats = {
160 V4L2_PIX_FMT_H264,
161 V4L2_PIX_FMT_MPEG4,
162 },
163};
164
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300165static const struct coda_video_device coda_bit_jpeg_encoder = {
166 .name = "coda-jpeg-encoder",
167 .type = CODA_INST_ENCODER,
168 .ops = &coda_bit_encode_ops,
169 .src_formats = {
170 V4L2_PIX_FMT_YUV420,
171 V4L2_PIX_FMT_YVU420,
172 V4L2_PIX_FMT_NV12,
173 V4L2_PIX_FMT_YUV422P,
174 },
175 .dst_formats = {
176 V4L2_PIX_FMT_JPEG,
177 },
178};
179
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300180static const struct coda_video_device coda_bit_decoder = {
181 .name = "coda-decoder",
182 .type = CODA_INST_DECODER,
183 .ops = &coda_bit_decode_ops,
184 .src_formats = {
185 V4L2_PIX_FMT_H264,
186 V4L2_PIX_FMT_MPEG4,
187 },
188 .dst_formats = {
189 V4L2_PIX_FMT_YUV420,
190 V4L2_PIX_FMT_YVU420,
191 V4L2_PIX_FMT_NV12,
192 },
193};
194
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300195static const struct coda_video_device coda_bit_jpeg_decoder = {
196 .name = "coda-jpeg-decoder",
197 .type = CODA_INST_DECODER,
198 .ops = &coda_bit_decode_ops,
199 .src_formats = {
200 V4L2_PIX_FMT_JPEG,
201 },
202 .dst_formats = {
203 V4L2_PIX_FMT_YUV420,
204 V4L2_PIX_FMT_YVU420,
205 V4L2_PIX_FMT_NV12,
206 V4L2_PIX_FMT_YUV422P,
207 },
208};
209
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300210static const struct coda_video_device *codadx6_video_devices[] = {
211 &coda_bit_encoder,
212};
213
214static const struct coda_video_device *coda7_video_devices[] = {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300215 &coda_bit_jpeg_encoder,
216 &coda_bit_jpeg_decoder,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300217 &coda_bit_encoder,
218 &coda_bit_decoder,
219};
220
221static const struct coda_video_device *coda9_video_devices[] = {
222 &coda_bit_encoder,
223 &coda_bit_decoder,
224};
225
Philipp Zabelb96904e2013-05-23 10:42:58 -0300226/*
227 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
228 * tables.
229 */
230static u32 coda_format_normalize_yuv(u32 fourcc)
231{
Philipp Zabel8076c7e2015-07-09 07:10:18 -0300232 switch (fourcc) {
233 case V4L2_PIX_FMT_YUV420:
234 case V4L2_PIX_FMT_YVU420:
235 case V4L2_PIX_FMT_NV12:
236 case V4L2_PIX_FMT_YUV422P:
237 return V4L2_PIX_FMT_YUV420;
238 default:
239 return fourcc;
240 }
Philipp Zabelb96904e2013-05-23 10:42:58 -0300241}
242
Philipp Zabel814c3762014-07-18 07:22:45 -0300243static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
244 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300245{
Philipp Zabel814c3762014-07-18 07:22:45 -0300246 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300247 int num_codecs = dev->devtype->num_codecs;
248 int k;
249
250 src_fourcc = coda_format_normalize_yuv(src_fourcc);
251 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
252 if (src_fourcc == dst_fourcc)
253 return NULL;
254
255 for (k = 0; k < num_codecs; k++) {
256 if (codecs[k].src_fourcc == src_fourcc &&
257 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300258 break;
259 }
260
Philipp Zabelb96904e2013-05-23 10:42:58 -0300261 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300262 return NULL;
263
Philipp Zabelb96904e2013-05-23 10:42:58 -0300264 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300265}
266
Philipp Zabel57625592013-09-30 10:34:51 -0300267static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300268 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300269 int *max_w, int *max_h)
270{
Philipp Zabel814c3762014-07-18 07:22:45 -0300271 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300272 int num_codecs = dev->devtype->num_codecs;
273 unsigned int w, h;
274 int k;
275
276 if (codec) {
277 w = codec->max_w;
278 h = codec->max_h;
279 } else {
280 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
281 w = max(w, codecs[k].max_w);
282 h = max(h, codecs[k].max_h);
283 }
284 }
285
286 if (max_w)
287 *max_w = w;
288 if (max_h)
289 *max_h = h;
290}
291
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300292const struct coda_video_device *to_coda_video_device(struct video_device *vdev)
293{
294 struct coda_dev *dev = video_get_drvdata(vdev);
295 unsigned int i = vdev - dev->vfd;
296
297 if (i >= dev->devtype->num_vdevs)
298 return NULL;
299
300 return dev->devtype->vdevs[i];
301}
302
Philipp Zabel79924ca2014-07-23 12:28:45 -0300303const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300304{
305 static char buf[9];
306
307 switch (product) {
308 case CODA_DX6:
309 return "CodaDx6";
310 case CODA_7541:
311 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300312 case CODA_960:
313 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300314 default:
315 snprintf(buf, sizeof(buf), "(0x%04x)", product);
316 return buf;
317 }
318}
319
Javier Martin186b2502012-07-26 05:53:35 -0300320/*
321 * V4L2 ioctl() operations.
322 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300323static int coda_querycap(struct file *file, void *priv,
324 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300325{
Philipp Zabel927933f2013-09-30 10:34:48 -0300326 struct coda_ctx *ctx = fh_to_ctx(priv);
327
Javier Martin186b2502012-07-26 05:53:35 -0300328 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300329 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
330 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300331 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300332 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300333 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
334
335 return 0;
336}
337
Philipp Zabel22e244b2014-07-18 07:22:43 -0300338static int coda_enum_fmt(struct file *file, void *priv,
339 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300340{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300341 struct video_device *vdev = video_devdata(file);
342 const struct coda_video_device *cvd = to_coda_video_device(vdev);
343 const u32 *formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300344
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300345 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
346 formats = cvd->src_formats;
347 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
348 formats = cvd->dst_formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300349 else
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300350 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300351
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300352 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
353 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300354
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300355 f->pixelformat = formats[f->index];
Javier Martin186b2502012-07-26 05:53:35 -0300356
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300357 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300358}
359
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300360static int coda_g_fmt(struct file *file, void *priv,
361 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300362{
Javier Martin186b2502012-07-26 05:53:35 -0300363 struct coda_q_data *q_data;
364 struct coda_ctx *ctx = fh_to_ctx(priv);
365
Javier Martin186b2502012-07-26 05:53:35 -0300366 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300367 if (!q_data)
368 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300369
370 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300371 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300372 f->fmt.pix.width = q_data->width;
373 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300374 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300375
376 f->fmt.pix.sizeimage = q_data->sizeimage;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300377 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
378 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
379 else
380 f->fmt.pix.colorspace = ctx->colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300381
382 return 0;
383}
384
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300385static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
386{
387 struct coda_q_data *q_data;
388 const u32 *formats;
389 int i;
390
391 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
392 formats = ctx->cvd->src_formats;
393 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
394 formats = ctx->cvd->dst_formats;
395 else
396 return -EINVAL;
397
398 for (i = 0; i < CODA_MAX_FORMATS; i++) {
399 if (formats[i] == f->fmt.pix.pixelformat) {
400 f->fmt.pix.pixelformat = formats[i];
401 return 0;
402 }
403 }
404
405 /* Fall back to currently set pixelformat */
406 q_data = get_q_data(ctx, f->type);
407 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300408
409 return 0;
410}
411
Philipp Zabel8e428d52015-01-23 13:51:29 -0300412static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
413 u32 width, u32 height)
414{
415 /*
416 * This is a rough estimate for sensible compressed buffer
417 * sizes (between 1 and 16 bits per pixel). This could be
418 * improved by better format specific worst case estimates.
419 */
420 return round_up(clamp(sizeimage, width * height / 8,
421 width * height * 2), PAGE_SIZE);
422}
423
Philipp Zabel814c3762014-07-18 07:22:45 -0300424static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300425 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300426{
Philipp Zabel57625592013-09-30 10:34:51 -0300427 struct coda_dev *dev = ctx->dev;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300428 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300429 enum v4l2_field field;
430
431 field = f->fmt.pix.field;
432 if (field == V4L2_FIELD_ANY)
433 field = V4L2_FIELD_NONE;
434 else if (V4L2_FIELD_NONE != field)
435 return -EINVAL;
436
437 /* V4L2 specification suggests the driver corrects the format struct
438 * if any of the dimensions is unsupported */
439 f->fmt.pix.field = field;
440
Philipp Zabel57625592013-09-30 10:34:51 -0300441 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
442 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
443 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
444 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300445
Philipp Zabel57625592013-09-30 10:34:51 -0300446 switch (f->fmt.pix.pixelformat) {
447 case V4L2_PIX_FMT_YUV420:
448 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300449 case V4L2_PIX_FMT_NV12:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300450 /*
451 * Frame stride must be at least multiple of 8,
452 * but multiple of 16 for h.264 or JPEG 4:2:x
453 */
Philipp Zabel451dfe52014-07-11 06:36:39 -0300454 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300455 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300456 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300457 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300458 case V4L2_PIX_FMT_YUV422P:
459 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
460 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
461 f->fmt.pix.height * 2;
462 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300463 case V4L2_PIX_FMT_JPEG:
464 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
465 /* fallthrough */
Philipp Zabel57625592013-09-30 10:34:51 -0300466 case V4L2_PIX_FMT_H264:
467 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -0300468 f->fmt.pix.bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -0300469 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
470 f->fmt.pix.sizeimage,
471 f->fmt.pix.width,
472 f->fmt.pix.height);
Philipp Zabel57625592013-09-30 10:34:51 -0300473 break;
474 default:
475 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300476 }
477
478 return 0;
479}
480
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300481static int coda_try_fmt_vid_cap(struct file *file, void *priv,
482 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300483{
Javier Martin186b2502012-07-26 05:53:35 -0300484 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300485 const struct coda_q_data *q_data_src;
486 const struct coda_codec *codec;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300487 struct vb2_queue *src_vq;
488 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300489
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300490 ret = coda_try_pixelformat(ctx, f);
491 if (ret < 0)
492 return ret;
493
494 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
495
Philipp Zabel918c66f2013-06-27 06:59:01 -0300496 /*
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300497 * If the source format is already fixed, only allow the same output
498 * resolution
Philipp Zabel918c66f2013-06-27 06:59:01 -0300499 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300500 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300501 if (vb2_is_streaming(src_vq)) {
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300502 f->fmt.pix.width = q_data_src->width;
503 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300504 }
Javier Martin186b2502012-07-26 05:53:35 -0300505
506 f->fmt.pix.colorspace = ctx->colorspace;
507
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300508 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
509 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
510 f->fmt.pix.pixelformat);
511 if (!codec)
512 return -EINVAL;
513
Philipp Zabel57625592013-09-30 10:34:51 -0300514 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300515 if (ret < 0)
516 return ret;
517
518 /* The h.264 decoder only returns complete 16x16 macroblocks */
519 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300520 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300521 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300522 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300523 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
524 f->fmt.pix.height * 3 / 2;
525 }
526
527 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300528}
529
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300530static int coda_try_fmt_vid_out(struct file *file, void *priv,
531 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300532{
533 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300534 struct coda_dev *dev = ctx->dev;
535 const struct coda_q_data *q_data_dst;
536 const struct coda_codec *codec;
537 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300538
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300539 ret = coda_try_pixelformat(ctx, f);
540 if (ret < 0)
541 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300542
Philipp Zabel5f9826e2015-01-23 13:51:21 -0300543 switch (f->fmt.pix.colorspace) {
544 case V4L2_COLORSPACE_REC709:
545 case V4L2_COLORSPACE_JPEG:
546 break;
547 default:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300548 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
549 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
550 else
551 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Javier Martin186b2502012-07-26 05:53:35 -0300552 }
553
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300554 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
555 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300556
557 return coda_try_fmt(ctx, codec, f);
558}
559
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300560static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300561{
562 struct coda_q_data *q_data;
563 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300564
Philipp Zabel14604e32014-07-11 06:36:22 -0300565 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300566 if (!vq)
567 return -EINVAL;
568
569 q_data = get_q_data(ctx, f->type);
570 if (!q_data)
571 return -EINVAL;
572
573 if (vb2_is_busy(vq)) {
574 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
575 return -EBUSY;
576 }
577
Philipp Zabelb96904e2013-05-23 10:42:58 -0300578 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300579 q_data->width = f->fmt.pix.width;
580 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300581 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300582 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300583 q_data->rect.left = 0;
584 q_data->rect.top = 0;
585 q_data->rect.width = f->fmt.pix.width;
586 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300587
588 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
589 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300590 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300591
592 return 0;
593}
594
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300595static int coda_s_fmt_vid_cap(struct file *file, void *priv,
596 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300597{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300598 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300599 int ret;
600
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300601 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300602 if (ret)
603 return ret;
604
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300605 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300606}
607
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300608static int coda_s_fmt_vid_out(struct file *file, void *priv,
609 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300610{
611 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300612 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300613 int ret;
614
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300615 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300616 if (ret)
617 return ret;
618
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300619 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300620 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300621 return ret;
622
623 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300624
Philipp Zabel1c2f6a92015-01-23 13:51:22 -0300625 memset(&f_cap, 0, sizeof(f_cap));
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300626 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
627 coda_g_fmt(file, priv, &f_cap);
628 f_cap.fmt.pix.width = f->fmt.pix.width;
629 f_cap.fmt.pix.height = f->fmt.pix.height;
630
631 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
632 if (ret)
633 return ret;
634
635 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300636}
637
Philipp Zabel73751da2015-03-24 14:30:51 -0300638static int coda_reqbufs(struct file *file, void *priv,
639 struct v4l2_requestbuffers *rb)
640{
641 struct coda_ctx *ctx = fh_to_ctx(priv);
642 int ret;
643
644 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
645 if (ret)
646 return ret;
647
648 /*
649 * Allow to allocate instance specific per-context buffers, such as
650 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
651 */
652 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
653 return ctx->ops->reqbufs(ctx, rb);
654
655 return 0;
656}
657
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300658static int coda_qbuf(struct file *file, void *priv,
659 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300660{
661 struct coda_ctx *ctx = fh_to_ctx(priv);
662
Philipp Zabel14604e32014-07-11 06:36:22 -0300663 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300664}
665
Philipp Zabel918c66f2013-06-27 06:59:01 -0300666static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300667 struct vb2_buffer *buf)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300668{
669 struct vb2_queue *src_vq;
670
Philipp Zabel14604e32014-07-11 06:36:22 -0300671 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300672
673 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300674 (buf->v4l2_buf.sequence == (ctx->qsequence - 1)));
Philipp Zabel918c66f2013-06-27 06:59:01 -0300675}
676
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300677void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_buffer *buf,
678 enum vb2_buffer_state state)
Javier Martin186b2502012-07-26 05:53:35 -0300679{
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300680 const struct v4l2_event eos_event = {
681 .type = V4L2_EVENT_EOS
682 };
Javier Martin186b2502012-07-26 05:53:35 -0300683
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300684 if (coda_buf_is_end_of_stream(ctx, buf)) {
685 buf->v4l2_buf.flags |= V4L2_BUF_FLAG_LAST;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300686
687 v4l2_event_queue_fh(&ctx->fh, &eos_event);
688 }
689
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300690 v4l2_m2m_buf_done(buf, state);
Javier Martin186b2502012-07-26 05:53:35 -0300691}
692
Philipp Zabel52c41672014-07-11 06:36:19 -0300693static int coda_g_selection(struct file *file, void *fh,
694 struct v4l2_selection *s)
695{
696 struct coda_ctx *ctx = fh_to_ctx(fh);
697 struct coda_q_data *q_data;
698 struct v4l2_rect r, *rsel;
699
700 q_data = get_q_data(ctx, s->type);
701 if (!q_data)
702 return -EINVAL;
703
704 r.left = 0;
705 r.top = 0;
706 r.width = q_data->width;
707 r.height = q_data->height;
708 rsel = &q_data->rect;
709
710 switch (s->target) {
711 case V4L2_SEL_TGT_CROP_DEFAULT:
712 case V4L2_SEL_TGT_CROP_BOUNDS:
713 rsel = &r;
714 /* fallthrough */
715 case V4L2_SEL_TGT_CROP:
716 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
717 return -EINVAL;
718 break;
719 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
720 case V4L2_SEL_TGT_COMPOSE_PADDED:
721 rsel = &r;
722 /* fallthrough */
723 case V4L2_SEL_TGT_COMPOSE:
724 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
725 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
726 return -EINVAL;
727 break;
728 default:
729 return -EINVAL;
730 }
731
732 s->r = *rsel;
733
734 return 0;
735}
736
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300737static int coda_try_decoder_cmd(struct file *file, void *fh,
738 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300739{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300740 if (dc->cmd != V4L2_DEC_CMD_STOP)
741 return -EINVAL;
742
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300743 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300744 return -EINVAL;
745
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300746 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300747 return -EINVAL;
748
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300749 return 0;
750}
751
752static int coda_decoder_cmd(struct file *file, void *fh,
753 struct v4l2_decoder_cmd *dc)
754{
755 struct coda_ctx *ctx = fh_to_ctx(fh);
756 int ret;
757
758 ret = coda_try_decoder_cmd(file, fh, dc);
759 if (ret < 0)
760 return ret;
761
762 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300763 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300764 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300765
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300766 /* Set the stream-end flag on this context */
767 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300768 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300769 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300770
Philipp Zabel918c66f2013-06-27 06:59:01 -0300771 return 0;
772}
773
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300774static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
775{
776 struct coda_ctx *ctx = fh_to_ctx(fh);
777 struct v4l2_fract *tpf;
778
779 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
780 return -EINVAL;
781
782 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
783 tpf = &a->parm.output.timeperframe;
784 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK;
785 tpf->numerator = 1 + (ctx->params.framerate >>
786 CODA_FRATE_DIV_OFFSET);
787
788 return 0;
789}
790
791/*
792 * Approximate timeperframe v4l2_fract with values that can be written
793 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields.
794 */
795static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe)
796{
797 struct v4l2_fract s = *timeperframe;
798 struct v4l2_fract f0;
799 struct v4l2_fract f1 = { 1, 0 };
800 struct v4l2_fract f2 = { 0, 1 };
801 unsigned int i, div, s_denominator;
802
803 /* Lower bound is 1/65535 */
804 if (s.numerator == 0 || s.denominator / s.numerator > 65535) {
805 timeperframe->numerator = 1;
806 timeperframe->denominator = 65535;
807 return;
808 }
809
810 /* Upper bound is 65536/1, map everything above to infinity */
811 if (s.denominator == 0 || s.numerator / s.denominator > 65536) {
812 timeperframe->numerator = 1;
813 timeperframe->denominator = 0;
814 return;
815 }
816
817 /* Reduce fraction to lowest terms */
818 div = gcd(s.numerator, s.denominator);
819 if (div > 1) {
820 s.numerator /= div;
821 s.denominator /= div;
822 }
823
824 if (s.numerator <= 65536 && s.denominator < 65536) {
825 *timeperframe = s;
826 return;
827 }
828
829 /* Find successive convergents from continued fraction expansion */
830 while (f2.numerator <= 65536 && f2.denominator < 65536) {
831 f0 = f1;
832 f1 = f2;
833
834 /* Stop when f2 exactly equals timeperframe */
835 if (s.numerator == 0)
836 break;
837
838 i = s.denominator / s.numerator;
839
840 f2.numerator = f0.numerator + i * f1.numerator;
841 f2.denominator = f0.denominator + i * f2.denominator;
842
843 s_denominator = s.numerator;
844 s.numerator = s.denominator % s.numerator;
845 s.denominator = s_denominator;
846 }
847
848 *timeperframe = f1;
849}
850
851static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe)
852{
853 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) |
854 timeperframe->denominator;
855}
856
857static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
858{
859 struct coda_ctx *ctx = fh_to_ctx(fh);
860 struct v4l2_fract *tpf;
861
862 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
863 return -EINVAL;
864
865 tpf = &a->parm.output.timeperframe;
866 coda_approximate_timeperframe(tpf);
867 ctx->params.framerate = coda_timeperframe_to_frate(tpf);
868
869 return 0;
870}
871
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300872static int coda_subscribe_event(struct v4l2_fh *fh,
873 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300874{
875 switch (sub->type) {
876 case V4L2_EVENT_EOS:
877 return v4l2_event_subscribe(fh, sub, 0, NULL);
878 default:
879 return v4l2_ctrl_subscribe_event(fh, sub);
880 }
Javier Martin186b2502012-07-26 05:53:35 -0300881}
882
883static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300884 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300885
Philipp Zabel22e244b2014-07-18 07:22:43 -0300886 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300887 .vidioc_g_fmt_vid_cap = coda_g_fmt,
888 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
889 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300890
Philipp Zabel22e244b2014-07-18 07:22:43 -0300891 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300892 .vidioc_g_fmt_vid_out = coda_g_fmt,
893 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
894 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300895
Philipp Zabel73751da2015-03-24 14:30:51 -0300896 .vidioc_reqbufs = coda_reqbufs,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300897 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300898
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300899 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300900 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300901 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300902 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300903
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300904 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
905 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300906
Philipp Zabel52c41672014-07-11 06:36:19 -0300907 .vidioc_g_selection = coda_g_selection,
908
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300909 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300910 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300911
Philipp Zabelcde29ef2015-07-16 13:13:24 -0300912 .vidioc_g_parm = coda_g_parm,
913 .vidioc_s_parm = coda_s_parm,
914
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300915 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300916 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300917};
918
Philipp Zabel79924ca2014-07-23 12:28:45 -0300919void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300920{
921 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
922 struct coda_dev *dev = ctx->dev;
923 int i;
924
925 for (i = 0; i < 16; i++)
926 coda_write(dev, tiled_map->xy2ca_map[i],
927 CODA9_GDI_XY2_CAS_0 + 4 * i);
928 for (i = 0; i < 4; i++)
929 coda_write(dev, tiled_map->xy2ba_map[i],
930 CODA9_GDI_XY2_BA_0 + 4 * i);
931 for (i = 0; i < 16; i++)
932 coda_write(dev, tiled_map->xy2ra_map[i],
933 CODA9_GDI_XY2_RAS_0 + 4 * i);
934 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
935 for (i = 0; i < 32; i++)
936 coda_write(dev, tiled_map->rbc2axi_map[i],
937 CODA9_GDI_RBC2_AXI_0 + 4 * i);
938}
939
Javier Martin186b2502012-07-26 05:53:35 -0300940/*
941 * Mem-to-mem operations.
942 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300943
944static void coda_device_run(void *m2m_priv)
945{
946 struct coda_ctx *ctx = m2m_priv;
947 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300948
949 queue_work(dev->workqueue, &ctx->pic_run_work);
950}
951
Philipp Zabel32b6f202014-07-11 06:36:20 -0300952static void coda_pic_run_work(struct work_struct *work)
953{
954 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
955 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300956 int ret;
957
958 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300959 mutex_lock(&dev->coda_mutex);
960
Philipp Zabela1192a12014-07-23 12:28:40 -0300961 ret = ctx->ops->prepare_run(ctx);
962 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
963 mutex_unlock(&dev->coda_mutex);
964 mutex_unlock(&ctx->buffer_mutex);
965 /* job_finish scheduled by prepare_decode */
966 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300967 }
968
Philipp Zabelf23797b2014-08-06 08:02:23 -0300969 if (!wait_for_completion_timeout(&ctx->completion,
970 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300971 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300972
973 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300974
975 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300976 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300977 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300978 }
979
Philipp Zabel747d7642015-01-23 13:51:31 -0300980 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
981 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -0300982 queue_work(dev->workqueue, &ctx->seq_end_work);
983
984 mutex_unlock(&dev->coda_mutex);
985 mutex_unlock(&ctx->buffer_mutex);
986
Philipp Zabel14604e32014-07-11 06:36:22 -0300987 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300988}
989
990static int coda_job_ready(void *m2m_priv)
991{
992 struct coda_ctx *ctx = m2m_priv;
Philipp Zabel2cf251c2015-07-09 07:10:20 -0300993 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300994
995 /*
996 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300997 * and 1 frame are needed. In the decoder case,
998 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300999 */
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001000 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -03001001 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1002 "not ready: not enough video buffers.\n");
1003 return 0;
1004 }
1005
Philipp Zabel14604e32014-07-11 06:36:22 -03001006 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001007 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1008 "not ready: not enough video capture buffers.\n");
1009 return 0;
1010 }
1011
Philipp Zabela22496c2015-01-23 13:51:33 -03001012 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001013 bool stream_end = ctx->bit_stream_param &
1014 CODA_BIT_STREAM_END_FLAG;
1015 int num_metas = ctx->num_metas;
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001016
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001017 if (ctx->hold && !src_bufs) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001018 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1019 "%d: not ready: on hold for more buffers.\n",
1020 ctx->idx);
1021 return 0;
1022 }
1023
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001024 if (!stream_end && (num_metas + src_bufs) < 2) {
1025 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1026 "%d: not ready: need 2 buffers available (%d, %d)\n",
1027 ctx->idx, num_metas, src_bufs);
1028 return 0;
1029 }
1030
1031
Philipp Zabel2cf251c2015-07-09 07:10:20 -03001032 if (!src_bufs && !stream_end &&
1033 (coda_get_bitstream_payload(ctx) < 512)) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -03001034 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1035 "%d: not ready: not enough bitstream data (%d).\n",
1036 ctx->idx, coda_get_bitstream_payload(ctx));
1037 return 0;
1038 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001039 }
1040
Philipp Zabel3e748262013-05-23 10:43:01 -03001041 if (ctx->aborting) {
1042 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1043 "not ready: aborting\n");
1044 return 0;
1045 }
1046
Javier Martin186b2502012-07-26 05:53:35 -03001047 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1048 "job ready\n");
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001049
Javier Martin186b2502012-07-26 05:53:35 -03001050 return 1;
1051}
1052
1053static void coda_job_abort(void *priv)
1054{
1055 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001056
1057 ctx->aborting = 1;
1058
1059 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1060 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001061}
1062
1063static void coda_lock(void *m2m_priv)
1064{
1065 struct coda_ctx *ctx = m2m_priv;
1066 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001067
Javier Martin186b2502012-07-26 05:53:35 -03001068 mutex_lock(&pcdev->dev_mutex);
1069}
1070
1071static void coda_unlock(void *m2m_priv)
1072{
1073 struct coda_ctx *ctx = m2m_priv;
1074 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001075
Javier Martin186b2502012-07-26 05:53:35 -03001076 mutex_unlock(&pcdev->dev_mutex);
1077}
1078
Philipp Zabel814c3762014-07-18 07:22:45 -03001079static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001080 .device_run = coda_device_run,
1081 .job_ready = coda_job_ready,
1082 .job_abort = coda_job_abort,
1083 .lock = coda_lock,
1084 .unlock = coda_unlock,
1085};
1086
Philipp Zabel89548442014-07-11 06:36:17 -03001087static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1088{
1089 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1090 int luma_map, chro_map, i;
1091
1092 memset(tiled_map, 0, sizeof(*tiled_map));
1093
1094 luma_map = 64;
1095 chro_map = 64;
1096 tiled_map->map_type = tiled_map_type;
1097 for (i = 0; i < 16; i++)
1098 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1099 for (i = 0; i < 4; i++)
1100 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1101 for (i = 0; i < 16; i++)
1102 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1103
1104 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1105 tiled_map->xy2rbc_config = 0;
1106 } else {
1107 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1108 tiled_map_type);
1109 return;
1110 }
1111}
1112
Javier Martin186b2502012-07-26 05:53:35 -03001113static void set_default_params(struct coda_ctx *ctx)
1114{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001115 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001116
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001117 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1118 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001119 max_w = min(ctx->codec->max_w, 1920U);
1120 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001121 usize = max_w * max_h * 3 / 2;
1122 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001123
Philipp Zabel121cacf2014-07-18 07:22:42 -03001124 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001125 ctx->colorspace = V4L2_COLORSPACE_REC709;
1126 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001127
1128 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001129 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1130 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1131 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1132 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001133 ctx->q_data[V4L2_M2M_DST].width = max_w;
1134 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001135 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1136 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001137 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001138 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001139 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001140 } else {
1141 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001142 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001143 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001144 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001145 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001146 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1147 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1148 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1149 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001150
1151 if (ctx->dev->devtype->product == CODA_960)
1152 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001153}
1154
1155/*
1156 * Queue operations
1157 */
1158static int coda_queue_setup(struct vb2_queue *vq,
1159 const struct v4l2_format *fmt,
1160 unsigned int *nbuffers, unsigned int *nplanes,
1161 unsigned int sizes[], void *alloc_ctxs[])
1162{
1163 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001164 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001165 unsigned int size;
1166
Philipp Zabele34db062012-08-29 08:22:00 -03001167 q_data = get_q_data(ctx, vq->type);
1168 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001169
1170 *nplanes = 1;
1171 sizes[0] = size;
1172
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001173 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
Javier Martin186b2502012-07-26 05:53:35 -03001174 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1175
1176 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1177 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1178
1179 return 0;
1180}
1181
1182static int coda_buf_prepare(struct vb2_buffer *vb)
1183{
1184 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1185 struct coda_q_data *q_data;
1186
1187 q_data = get_q_data(ctx, vb->vb2_queue->type);
1188
1189 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1190 v4l2_warn(&ctx->dev->v4l2_dev,
1191 "%s data will not fit into plane (%lu < %lu)\n",
1192 __func__, vb2_plane_size(vb, 0),
1193 (long)q_data->sizeimage);
1194 return -EINVAL;
1195 }
1196
Javier Martin186b2502012-07-26 05:53:35 -03001197 return 0;
1198}
1199
1200static void coda_buf_queue(struct vb2_buffer *vb)
1201{
1202 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabelcb06b702015-01-23 13:51:35 -03001203 struct vb2_queue *vq = vb->vb2_queue;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001204 struct coda_q_data *q_data;
1205
1206 q_data = get_q_data(ctx, vb->vb2_queue->type);
1207
1208 /*
1209 * In the decoder case, immediately try to copy the buffer into the
1210 * bitstream ringbuffer and mark it as ready to be dequeued.
1211 */
Philipp Zabelcb06b702015-01-23 13:51:35 -03001212 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001213 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001214 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001215 * the stream end
1216 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001217 if (vb2_get_plane_payload(vb, 0) == 0)
1218 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001219 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001220 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001221 if (vb2_is_streaming(vb->vb2_queue))
Philipp Zabel582d88722015-03-24 14:30:57 -03001222 coda_fill_bitstream(ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001223 mutex_unlock(&ctx->bitstream_mutex);
1224 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001225 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001226 }
Javier Martin186b2502012-07-26 05:53:35 -03001227}
1228
Philipp Zabel79924ca2014-07-23 12:28:45 -03001229int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1230 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001231{
1232 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1233 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001234 if (!buf->vaddr) {
1235 v4l2_err(&dev->v4l2_dev,
1236 "Failed to allocate %s buffer of size %u\n",
1237 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001238 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001239 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001240
1241 buf->size = size;
1242
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001243 if (name && parent) {
1244 buf->blob.data = buf->vaddr;
1245 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001246 buf->dentry = debugfs_create_blob(name, 0644, parent,
1247 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001248 if (!buf->dentry)
1249 dev_warn(&dev->plat_dev->dev,
1250 "failed to create debugfs entry %s\n", name);
1251 }
1252
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001253 return 0;
1254}
1255
Philipp Zabel79924ca2014-07-23 12:28:45 -03001256void coda_free_aux_buf(struct coda_dev *dev,
1257 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001258{
1259 if (buf->vaddr) {
1260 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1261 buf->vaddr, buf->paddr);
1262 buf->vaddr = NULL;
1263 buf->size = 0;
Peter Seidererd446ec82015-03-24 14:30:48 -03001264 debugfs_remove(buf->dentry);
1265 buf->dentry = NULL;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001266 }
1267}
1268
Javier Martin186b2502012-07-26 05:53:35 -03001269static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1270{
1271 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1272 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001273 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001274 struct vb2_buffer *buf;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001275 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001276
Philipp Zabelb96904e2013-05-23 10:42:58 -03001277 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001278 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel58bc7ed2015-07-09 07:10:14 -03001279 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabel582d88722015-03-24 14:30:57 -03001280 /* copy the buffers that were queued before streamon */
Michael Olbricheabed932014-07-18 07:22:40 -03001281 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel582d88722015-03-24 14:30:57 -03001282 coda_fill_bitstream(ctx, false);
Michael Olbricheabed932014-07-18 07:22:40 -03001283 mutex_unlock(&ctx->bitstream_mutex);
1284
Philipp Zabelb9063522014-08-05 14:00:10 -03001285 if (coda_get_bitstream_payload(ctx) < 512) {
1286 ret = -EINVAL;
1287 goto err;
1288 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001289 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001290 if (count < 1) {
1291 ret = -EINVAL;
1292 goto err;
1293 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001294 }
1295
1296 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001297 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001298 if (count < 1) {
1299 ret = -EINVAL;
1300 goto err;
1301 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001302
1303 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001304 }
Javier Martin186b2502012-07-26 05:53:35 -03001305
Philipp Zabelb96904e2013-05-23 10:42:58 -03001306 /* Don't start the coda unless both queues are on */
1307 if (!(ctx->streamon_out & ctx->streamon_cap))
1308 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001309
Philipp Zabel23b6ee52015-03-24 14:30:55 -03001310 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1311 if ((q_data_src->width != q_data_dst->width &&
1312 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1313 (q_data_src->height != q_data_dst->height &&
1314 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1315 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1316 q_data_src->width, q_data_src->height,
1317 q_data_dst->width, q_data_dst->height);
1318 ret = -EINVAL;
1319 goto err;
1320 }
1321
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001322 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001323 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001324 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001325
Philipp Zabelb96904e2013-05-23 10:42:58 -03001326 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001327
Philipp Zabelb96904e2013-05-23 10:42:58 -03001328 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1329 q_data_dst->fourcc);
1330 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001331 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001332 ret = -EINVAL;
1333 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001334 }
1335
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001336 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1337 ctx->params.gop_size = 1;
1338 ctx->gopcounter = ctx->params.gop_size - 1;
1339
Philipp Zabela1192a12014-07-23 12:28:40 -03001340 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001341 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001342 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001343 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001344 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001345 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001346 }
1347
Philipp Zabel89548442014-07-11 06:36:17 -03001348 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001349
1350err:
1351 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1352 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001353 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001354 } else {
1355 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001356 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001357 }
1358 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001359}
1360
Hans Verkuile37559b2014-04-17 02:47:21 -03001361static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001362{
1363 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001364 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001365 struct vb2_buffer *buf;
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001366 unsigned long flags;
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001367 bool stop;
1368
1369 stop = ctx->streamon_out && ctx->streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -03001370
1371 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001372 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001373 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001374 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001375
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001376 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001377
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001378 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001379
1380 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1381 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001382 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001383 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001384 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001385 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001386
1387 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001388 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001389
1390 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1391 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001392 }
1393
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001394 if (stop) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001395 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001396
Philipp Zabelf4706d62015-01-23 13:51:27 -03001397 if (ctx->ops->seq_end_work) {
1398 queue_work(dev->workqueue, &ctx->seq_end_work);
1399 flush_work(&ctx->seq_end_work);
1400 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001401 spin_lock_irqsave(&ctx->buffer_meta_lock, flags);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001402 while (!list_empty(&ctx->buffer_meta_list)) {
1403 meta = list_first_entry(&ctx->buffer_meta_list,
1404 struct coda_buffer_meta, list);
1405 list_del(&meta->list);
1406 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001407 }
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001408 ctx->num_metas = 0;
1409 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001410 kfifo_init(&ctx->bitstream_fifo,
1411 ctx->bitstream.vaddr, ctx->bitstream.size);
1412 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001413 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001414 }
Philipp Zabelc1ae0b22015-07-09 07:10:17 -03001415
1416 if (!ctx->streamon_out && !ctx->streamon_cap)
1417 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG;
Javier Martin186b2502012-07-26 05:53:35 -03001418}
1419
Philipp Zabel121cacf2014-07-18 07:22:42 -03001420static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001421 .queue_setup = coda_queue_setup,
1422 .buf_prepare = coda_buf_prepare,
1423 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001424 .start_streaming = coda_start_streaming,
1425 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001426 .wait_prepare = vb2_ops_wait_prepare,
1427 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001428};
1429
1430static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1431{
1432 struct coda_ctx *ctx =
1433 container_of(ctrl->handler, struct coda_ctx, ctrls);
1434
1435 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1436 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1437
1438 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001439 case V4L2_CID_HFLIP:
1440 if (ctrl->val)
1441 ctx->params.rot_mode |= CODA_MIR_HOR;
1442 else
1443 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1444 break;
1445 case V4L2_CID_VFLIP:
1446 if (ctrl->val)
1447 ctx->params.rot_mode |= CODA_MIR_VER;
1448 else
1449 ctx->params.rot_mode &= ~CODA_MIR_VER;
1450 break;
Javier Martin186b2502012-07-26 05:53:35 -03001451 case V4L2_CID_MPEG_VIDEO_BITRATE:
1452 ctx->params.bitrate = ctrl->val / 1000;
1453 break;
1454 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1455 ctx->params.gop_size = ctrl->val;
1456 break;
1457 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1458 ctx->params.h264_intra_qp = ctrl->val;
1459 break;
1460 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1461 ctx->params.h264_inter_qp = ctrl->val;
1462 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001463 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1464 ctx->params.h264_min_qp = ctrl->val;
1465 break;
1466 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1467 ctx->params.h264_max_qp = ctrl->val;
1468 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001469 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1470 ctx->params.h264_deblk_alpha = ctrl->val;
1471 break;
1472 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1473 ctx->params.h264_deblk_beta = ctrl->val;
1474 break;
1475 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1476 ctx->params.h264_deblk_enabled = (ctrl->val ==
1477 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1478 break;
Javier Martin186b2502012-07-26 05:53:35 -03001479 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1480 ctx->params.mpeg4_intra_qp = ctrl->val;
1481 break;
1482 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1483 ctx->params.mpeg4_inter_qp = ctrl->val;
1484 break;
1485 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1486 ctx->params.slice_mode = ctrl->val;
1487 break;
1488 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1489 ctx->params.slice_max_mb = ctrl->val;
1490 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001491 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1492 ctx->params.slice_max_bits = ctrl->val * 8;
1493 break;
Javier Martin186b2502012-07-26 05:53:35 -03001494 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1495 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001496 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1497 ctx->params.intra_refresh = ctrl->val;
1498 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001499 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1500 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1501 break;
1502 case V4L2_CID_JPEG_RESTART_INTERVAL:
1503 ctx->params.jpeg_restart_interval = ctrl->val;
1504 break;
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001505 case V4L2_CID_MPEG_VIDEO_VBV_DELAY:
1506 ctx->params.vbv_delay = ctrl->val;
1507 break;
1508 case V4L2_CID_MPEG_VIDEO_VBV_SIZE:
1509 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff);
1510 break;
Javier Martin186b2502012-07-26 05:53:35 -03001511 default:
1512 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1513 "Invalid control, id=%d, val=%d\n",
1514 ctrl->id, ctrl->val);
1515 return -EINVAL;
1516 }
1517
1518 return 0;
1519}
1520
Philipp Zabel814c3762014-07-18 07:22:45 -03001521static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001522 .s_ctrl = coda_s_ctrl,
1523};
1524
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001525static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001526{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001527 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel92b042e2015-03-18 08:15:36 -03001528 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001529 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1530 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1531 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001532 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001533 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001534 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001535 if (ctx->dev->devtype->product != CODA_960) {
1536 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1537 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1538 }
1539 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1540 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001541 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001542 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1543 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1544 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1545 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1546 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1547 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1548 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1549 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001550 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1551 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1552 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1553 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1554 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001555 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1556 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001557 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1558 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001559 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001560 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1561 500);
Javier Martin186b2502012-07-26 05:53:35 -03001562 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1563 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1564 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1565 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1566 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001567 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001568 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1569 1920 * 1088 / 256, 1, 0);
Philipp Zabelda2b3b32015-07-10 10:37:52 -03001570 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1571 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0);
1572 /*
1573 * The maximum VBV size value is 0x7fffffff bits,
1574 * one bit less than 262144 KiB
1575 */
1576 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1577 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001578}
1579
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001580static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1581{
1582 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1583 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1584 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1585 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1586}
1587
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001588static int coda_ctrls_setup(struct coda_ctx *ctx)
1589{
1590 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1591
1592 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1593 V4L2_CID_HFLIP, 0, 1, 1, 0);
1594 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1595 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001596 if (ctx->inst_type == CODA_INST_ENCODER) {
1597 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1598 coda_jpeg_encode_ctrls(ctx);
1599 else
1600 coda_encode_ctrls(ctx);
1601 }
Javier Martin186b2502012-07-26 05:53:35 -03001602
1603 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001604 v4l2_err(&ctx->dev->v4l2_dev,
1605 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001606 ctx->ctrls.error);
1607 return -EINVAL;
1608 }
1609
1610 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1611}
1612
Philipp Zabel121cacf2014-07-18 07:22:42 -03001613static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001614{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001615 vq->drv_priv = ctx;
1616 vq->ops = &coda_qops;
1617 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1618 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1619 vq->lock = &ctx->dev->dev_mutex;
Kamil Debskie4af23d2015-02-23 09:26:18 -03001620 /* One way to indicate end-of-stream for coda is to set the
1621 * bytesused == 0. However by default videobuf2 handles bytesused
1622 * equal to 0 as a special case and changes its value to the size
1623 * of the buffer. Set the allow_zero_bytesused flag, so
1624 * that videobuf2 will keep the value of bytesused intact.
1625 */
1626 vq->allow_zero_bytesused = 1;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001627
1628 return vb2_queue_init(vq);
1629}
1630
Philipp Zabel79924ca2014-07-23 12:28:45 -03001631int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1632 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001633{
Javier Martin186b2502012-07-26 05:53:35 -03001634 int ret;
1635
Javier Martin186b2502012-07-26 05:53:35 -03001636 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001637 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001638 src_vq->mem_ops = &vb2_dma_contig_memops;
1639
Philipp Zabel121cacf2014-07-18 07:22:42 -03001640 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001641 if (ret)
1642 return ret;
1643
Javier Martin186b2502012-07-26 05:53:35 -03001644 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001645 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001646 dst_vq->mem_ops = &vb2_dma_contig_memops;
1647
Philipp Zabel121cacf2014-07-18 07:22:42 -03001648 return coda_queue_init(priv, dst_vq);
1649}
1650
Philipp Zabel79924ca2014-07-23 12:28:45 -03001651int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1652 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001653{
1654 int ret;
1655
1656 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001657 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1658 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001659
1660 ret = coda_queue_init(priv, src_vq);
1661 if (ret)
1662 return ret;
1663
1664 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1665 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1666 dst_vq->mem_ops = &vb2_dma_contig_memops;
1667
1668 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001669}
1670
Philipp Zabele11f3e62012-07-25 09:16:58 -03001671static int coda_next_free_instance(struct coda_dev *dev)
1672{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001673 int idx = ffz(dev->instance_mask);
1674
1675 if ((idx < 0) ||
1676 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1677 return -EBUSY;
1678
1679 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001680}
1681
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001682/*
1683 * File operations
1684 */
1685
1686static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001687{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001688 struct video_device *vdev = video_devdata(file);
1689 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001690 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001691 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001692 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001693 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001694
Philipp Zabelf23797b2014-08-06 08:02:23 -03001695 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001696 if (!ctx)
1697 return -ENOMEM;
1698
Fabio Estevamf82bc202013-08-21 11:14:16 -03001699 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001700 if (idx < 0) {
1701 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001702 goto err_coda_max;
1703 }
1704 set_bit(idx, &dev->instance_mask);
1705
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001706 name = kasprintf(GFP_KERNEL, "context%d", idx);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001707 if (!name) {
1708 ret = -ENOMEM;
1709 goto err_coda_name_init;
1710 }
1711
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001712 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1713 kfree(name);
1714
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001715 ctx->cvd = to_coda_video_device(vdev);
1716 ctx->inst_type = ctx->cvd->type;
1717 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001718 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001719 init_completion(&ctx->completion);
1720 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001721 if (ctx->ops->seq_end_work)
1722 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001723 v4l2_fh_init(&ctx->fh, video_devdata(file));
1724 file->private_data = &ctx->fh;
1725 v4l2_fh_add(&ctx->fh);
1726 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001727 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001728 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001729 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001730 ctx->frame_mem_ctrl = 1 << 12;
1731 /* fallthrough */
1732 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001733 ctx->reg_idx = 0;
1734 break;
1735 default:
1736 ctx->reg_idx = idx;
1737 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001738
Philipp Zabel1e172732014-07-11 06:36:23 -03001739 /* Power up and upload firmware if necessary */
1740 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1741 if (ret < 0) {
1742 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1743 goto err_pm_get;
1744 }
1745
Fabio Estevam79830db2013-08-21 11:14:17 -03001746 ret = clk_prepare_enable(dev->clk_per);
1747 if (ret)
1748 goto err_clk_per;
1749
1750 ret = clk_prepare_enable(dev->clk_ahb);
1751 if (ret)
1752 goto err_clk_ahb;
1753
Javier Martin186b2502012-07-26 05:53:35 -03001754 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001755 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1756 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001757 if (IS_ERR(ctx->fh.m2m_ctx)) {
1758 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001759
1760 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1761 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001762 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001763 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001764
Javier Martin186b2502012-07-26 05:53:35 -03001765 ret = coda_ctrls_setup(ctx);
1766 if (ret) {
1767 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001768 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001769 }
1770
1771 ctx->fh.ctrl_handler = &ctx->ctrls;
1772
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001773 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001774 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001775 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel47f3fa62015-07-09 07:10:21 -03001776 spin_lock_init(&ctx->buffer_meta_lock);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001777
Javier Martin186b2502012-07-26 05:53:35 -03001778 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001779 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001780 coda_unlock(ctx);
1781
Javier Martin186b2502012-07-26 05:53:35 -03001782 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1783 ctx->idx, ctx);
1784
1785 return 0;
1786
Fabio Estevamf82bc202013-08-21 11:14:16 -03001787err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001788 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001789err_ctx_init:
1790 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001791err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001792 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001793err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001794 pm_runtime_put_sync(&dev->plat_dev->dev);
1795err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001796 v4l2_fh_del(&ctx->fh);
1797 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001798 clear_bit(ctx->idx, &dev->instance_mask);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001799err_coda_name_init:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001800err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001801 kfree(ctx);
1802 return ret;
1803}
1804
1805static int coda_release(struct file *file)
1806{
1807 struct coda_dev *dev = video_drvdata(file);
1808 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1809
1810 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1811 ctx);
1812
Philipp Zabela22496c2015-01-23 13:51:33 -03001813 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03001814 coda_bit_stream_end_flag(ctx);
1815
Philipp Zabel918c66f2013-06-27 06:59:01 -03001816 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001817 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001818
1819 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel5c718bb2015-07-09 07:10:16 -03001820 if (ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001821 queue_work(dev->workqueue, &ctx->seq_end_work);
1822 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001823 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001824
Javier Martin186b2502012-07-26 05:53:35 -03001825 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001826 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001827 coda_unlock(ctx);
1828
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001829 if (ctx->dev->devtype->product == CODA_DX6)
1830 coda_free_aux_buf(dev, &ctx->workbuf);
1831
Javier Martin186b2502012-07-26 05:53:35 -03001832 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001833 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001834 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001835 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001836 v4l2_fh_del(&ctx->fh);
1837 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001838 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001839 if (ctx->ops->release)
1840 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03001841 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03001842 kfree(ctx);
1843
1844 return 0;
1845}
1846
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001847static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001848 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001849 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03001850 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001851 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001852 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001853 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001854};
1855
Philipp Zabel87048bb2012-07-02 07:03:43 -03001856static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001857{
Javier Martin186b2502012-07-26 05:53:35 -03001858 u32 data;
1859 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001860 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001861
Fabio Estevam79830db2013-08-21 11:14:17 -03001862 ret = clk_prepare_enable(dev->clk_per);
1863 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001864 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001865
1866 ret = clk_prepare_enable(dev->clk_ahb);
1867 if (ret)
1868 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001869
Philipp Zabel8f452842014-07-11 06:36:35 -03001870 if (dev->rstc)
1871 reset_control_reset(dev->rstc);
1872
Javier Martin186b2502012-07-26 05:53:35 -03001873 /*
1874 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001875 * The 16-bit chars in the code buffer are in memory access
1876 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001877 * Data in this SRAM survives a reboot.
1878 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001879 p = (u16 *)dev->codebuf.vaddr;
1880 if (dev->devtype->product == CODA_DX6) {
1881 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1882 data = CODA_DOWN_ADDRESS_SET(i) |
1883 CODA_DOWN_DATA_SET(p[i ^ 1]);
1884 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1885 }
1886 } else {
1887 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1888 data = CODA_DOWN_ADDRESS_SET(i) |
1889 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1890 3 - (i % 4)]);
1891 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1892 }
Javier Martin186b2502012-07-26 05:53:35 -03001893 }
Javier Martin186b2502012-07-26 05:53:35 -03001894
Philipp Zabel9acf7692013-05-23 10:42:56 -03001895 /* Clear registers */
1896 for (i = 0; i < 64; i++)
1897 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1898
Javier Martin186b2502012-07-26 05:53:35 -03001899 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001900 if (dev->devtype->product == CODA_960 ||
1901 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001902 coda_write(dev, dev->tempbuf.paddr,
1903 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001904 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001905 } else {
1906 coda_write(dev, dev->workbuf.paddr,
1907 CODA_REG_BIT_WORK_BUF_ADDR);
1908 }
Javier Martin186b2502012-07-26 05:53:35 -03001909 coda_write(dev, dev->codebuf.paddr,
1910 CODA_REG_BIT_CODE_BUF_ADDR);
1911 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1912
1913 /* Set default values */
1914 switch (dev->devtype->product) {
1915 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001916 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1917 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001918 break;
1919 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001920 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1921 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001922 }
Philipp Zabel89548442014-07-11 06:36:17 -03001923 if (dev->devtype->product == CODA_960)
1924 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1925 else
1926 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001927
1928 if (dev->devtype->product != CODA_DX6)
1929 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1930
Javier Martin186b2502012-07-26 05:53:35 -03001931 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1932 CODA_REG_BIT_INT_ENABLE);
1933
1934 /* Reset VPU and start processor */
1935 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1936 data |= CODA_REG_RESET_ENABLE;
1937 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1938 udelay(10);
1939 data &= ~CODA_REG_RESET_ENABLE;
1940 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1941 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1942
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001943 clk_disable_unprepare(dev->clk_ahb);
1944 clk_disable_unprepare(dev->clk_per);
1945
1946 return 0;
1947
1948err_clk_ahb:
1949 clk_disable_unprepare(dev->clk_per);
1950err_clk_per:
1951 return ret;
1952}
1953
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001954static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001955{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001956 struct video_device *vfd = &dev->vfd[i];
1957
Dan Carpenter88ca3af2015-01-08 07:07:08 -03001958 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001959 return -EINVAL;
1960
Philipp Zabelbf249da2015-03-24 14:30:50 -03001961 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001962 vfd->fops = &coda_fops;
1963 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001964 vfd->release = video_device_release_empty,
1965 vfd->lock = &dev->dev_mutex;
1966 vfd->v4l2_dev = &dev->v4l2_dev;
1967 vfd->vfl_dir = VFL_DIR_M2M;
1968 video_set_drvdata(vfd, dev);
1969
Philipp Zabela188a662014-08-05 14:00:20 -03001970 /* Not applicable, use the selection API instead */
1971 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1972 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1973 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1974
Philipp Zabel121cacf2014-07-18 07:22:42 -03001975 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1976}
1977
Javier Martin186b2502012-07-26 05:53:35 -03001978static void coda_fw_callback(const struct firmware *fw, void *context)
1979{
1980 struct coda_dev *dev = context;
1981 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001982 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001983
1984 if (!fw) {
1985 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001986 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001987 }
1988
1989 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001990 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1991 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03001992 if (ret < 0)
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001993 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001994
Philipp Zabel87048bb2012-07-02 07:03:43 -03001995 /* Copy the whole firmware image to the code buffer */
1996 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1997 release_firmware(fw);
1998
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001999 ret = coda_hw_init(dev);
2000 if (ret < 0) {
2001 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
2002 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002003 }
2004
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002005 ret = coda_check_firmware(dev);
2006 if (ret < 0)
2007 goto put_pm;
2008
Javier Martin186b2502012-07-26 05:53:35 -03002009 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2010 if (IS_ERR(dev->alloc_ctx)) {
2011 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002012 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03002013 }
2014
2015 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
2016 if (IS_ERR(dev->m2m_dev)) {
2017 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
2018 goto rel_ctx;
2019 }
2020
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002021 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2022 ret = coda_register_device(dev, i);
2023 if (ret) {
2024 v4l2_err(&dev->v4l2_dev,
2025 "Failed to register %s video device: %d\n",
2026 dev->devtype->vdevs[i]->name, ret);
2027 goto rel_vfd;
2028 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03002029 }
2030
2031 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002032 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03002033
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002034 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002035 return;
2036
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002037rel_vfd:
2038 while (--i >= 0)
2039 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03002040 v4l2_m2m_release(dev->m2m_dev);
2041rel_ctx:
2042 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002043put_pm:
2044 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002045}
2046
2047static int coda_firmware_request(struct coda_dev *dev)
2048{
2049 char *fw = dev->devtype->firmware;
2050
2051 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2052 coda_product_name(dev->devtype->product));
2053
2054 return request_firmware_nowait(THIS_MODULE, true,
2055 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2056}
2057
2058enum coda_platform {
2059 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002060 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002061 CODA_IMX6Q,
2062 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002063};
2064
Emil Goodec06d8752012-08-14 17:44:42 -03002065static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002066 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002067 .firmware = "v4l-codadx6-imx27.bin",
2068 .product = CODA_DX6,
2069 .codecs = codadx6_codecs,
2070 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002071 .vdevs = codadx6_video_devices,
2072 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002073 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002074 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002075 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002076 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002077 .firmware = "v4l-coda7541-imx53.bin",
2078 .product = CODA_7541,
2079 .codecs = coda7_codecs,
2080 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002081 .vdevs = coda7_video_devices,
2082 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002083 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002084 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002085 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002086 },
Philipp Zabel89548442014-07-11 06:36:17 -03002087 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002088 .firmware = "v4l-coda960-imx6q.bin",
2089 .product = CODA_960,
2090 .codecs = coda9_codecs,
2091 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002092 .vdevs = coda9_video_devices,
2093 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002094 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002095 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002096 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002097 },
2098 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002099 .firmware = "v4l-coda960-imx6dl.bin",
2100 .product = CODA_960,
2101 .codecs = coda9_codecs,
2102 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002103 .vdevs = coda9_video_devices,
2104 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002105 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002106 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002107 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002108 },
Javier Martin186b2502012-07-26 05:53:35 -03002109};
2110
2111static struct platform_device_id coda_platform_ids[] = {
2112 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2113 { /* sentinel */ }
2114};
2115MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2116
2117#ifdef CONFIG_OF
2118static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002119 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002120 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002121 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2122 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002123 { /* sentinel */ }
2124};
2125MODULE_DEVICE_TABLE(of, coda_dt_ids);
2126#endif
2127
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002128static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002129{
2130 const struct of_device_id *of_id =
2131 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2132 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002133 struct coda_platform_data *pdata = pdev->dev.platform_data;
2134 struct device_node *np = pdev->dev.of_node;
2135 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002136 struct coda_dev *dev;
2137 struct resource *res;
2138 int ret, irq;
2139
Philipp Zabelf23797b2014-08-06 08:02:23 -03002140 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002141 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002142 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002143
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002144 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2145
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002146 if (of_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002147 dev->devtype = of_id->data;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002148 } else if (pdev_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002149 dev->devtype = &coda_devdata[pdev_id->driver_data];
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002150 } else {
2151 ret = -EINVAL;
2152 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002153 }
2154
2155 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002156 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002157
2158 dev->plat_dev = pdev;
2159 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2160 if (IS_ERR(dev->clk_per)) {
2161 dev_err(&pdev->dev, "Could not get per clock\n");
2162 return PTR_ERR(dev->clk_per);
2163 }
2164
2165 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2166 if (IS_ERR(dev->clk_ahb)) {
2167 dev_err(&pdev->dev, "Could not get ahb clock\n");
2168 return PTR_ERR(dev->clk_ahb);
2169 }
2170
2171 /* Get memory for physical registers */
2172 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002173 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2174 if (IS_ERR(dev->regs_base))
2175 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002176
2177 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002178 irq = platform_get_irq_byname(pdev, "bit");
2179 if (irq < 0)
2180 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002181 if (irq < 0) {
2182 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002183 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002184 }
2185
Fabio Estevam08c8d142014-06-04 15:46:24 -03002186 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2187 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2188 if (ret < 0) {
2189 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2190 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002191 }
2192
Philipp Zabelee27aa92014-07-24 16:50:03 -03002193 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002194 if (IS_ERR(dev->rstc)) {
2195 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03002196 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002197 dev->rstc = NULL;
2198 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002199 dev_err(&pdev->dev, "failed get reset control: %d\n",
2200 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002201 return ret;
2202 }
2203 }
2204
Philipp Zabel657eee72013-04-29 16:17:14 -07002205 /* Get IRAM pool from device tree or platform data */
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -07002206 pool = of_gen_pool_get(np, "iram", 0);
Philipp Zabel657eee72013-04-29 16:17:14 -07002207 if (!pool && pdata)
Vladimir Zapolskiy0030edf2015-06-30 15:00:03 -07002208 pool = gen_pool_get(pdata->iram_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002209 if (!pool) {
2210 dev_err(&pdev->dev, "iram pool not available\n");
2211 return -ENOMEM;
2212 }
2213 dev->iram_pool = pool;
2214
Javier Martin186b2502012-07-26 05:53:35 -03002215 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2216 if (ret)
2217 return ret;
2218
2219 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002220 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002221
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002222 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2223 if (!dev->debugfs_root)
2224 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2225
Javier Martin186b2502012-07-26 05:53:35 -03002226 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002227 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002228 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002229 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002230 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002231 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002232 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002233 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002234
2235 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002236 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002237 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002238 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002239 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002240 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002241 }
2242
Philipp Zabel401e9722014-07-11 06:36:43 -03002243 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002244 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2245 &dev->iram.paddr);
2246 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002247 dev_warn(&pdev->dev, "unable to alloc iram\n");
2248 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002249 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002250 dev->iram.blob.data = dev->iram.vaddr;
2251 dev->iram.blob.size = dev->iram.size;
2252 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2253 dev->debugfs_root,
2254 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002255 }
2256
Philipp Zabel32b6f202014-07-11 06:36:20 -03002257 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2258 if (!dev->workqueue) {
2259 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002260 ret = -ENOMEM;
2261 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002262 }
2263
Javier Martin186b2502012-07-26 05:53:35 -03002264 platform_set_drvdata(pdev, dev);
2265
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002266 /*
2267 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002268 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002269 * enabled or whether the device is associated with a PM domain.
2270 */
2271 pm_runtime_get_noresume(&pdev->dev);
2272 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002273 pm_runtime_enable(&pdev->dev);
2274
Javier Martin186b2502012-07-26 05:53:35 -03002275 return coda_firmware_request(dev);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002276
2277err_v4l2_register:
2278 v4l2_device_unregister(&dev->v4l2_dev);
2279 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002280}
2281
2282static int coda_remove(struct platform_device *pdev)
2283{
2284 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002285 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002286
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002287 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2288 if (video_get_drvdata(&dev->vfd[i]))
2289 video_unregister_device(&dev->vfd[i]);
2290 }
Javier Martin186b2502012-07-26 05:53:35 -03002291 if (dev->m2m_dev)
2292 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002293 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002294 if (dev->alloc_ctx)
2295 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2296 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002297 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002298 if (dev->iram.vaddr)
2299 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2300 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002301 coda_free_aux_buf(dev, &dev->codebuf);
2302 coda_free_aux_buf(dev, &dev->tempbuf);
2303 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002304 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002305 return 0;
2306}
2307
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002308#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002309static int coda_runtime_resume(struct device *dev)
2310{
2311 struct coda_dev *cdev = dev_get_drvdata(dev);
2312 int ret = 0;
2313
Philipp Zabel65919e62014-07-18 07:22:36 -03002314 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002315 ret = coda_hw_init(cdev);
2316 if (ret)
2317 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2318 }
2319
2320 return ret;
2321}
2322#endif
2323
2324static const struct dev_pm_ops coda_pm_ops = {
2325 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2326};
2327
Javier Martin186b2502012-07-26 05:53:35 -03002328static struct platform_driver coda_driver = {
2329 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002330 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002331 .driver = {
2332 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002333 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002334 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002335 },
2336 .id_table = coda_platform_ids,
2337};
2338
2339module_platform_driver(coda_driver);
2340
2341MODULE_LICENSE("GPL");
2342MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2343MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");