blob: a8a6c36a1f000daec9d96ddaef0d6c8d861af78b [file] [log] [blame]
Javier Martin186b2502012-07-26 05:53:35 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/clk.h>
Philipp Zabelf61c0b12014-07-11 06:36:40 -030015#include <linux/debugfs.h>
Javier Martin186b2502012-07-26 05:53:35 -030016#include <linux/delay.h>
17#include <linux/firmware.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070018#include <linux/genalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/irq.h>
Philipp Zabel9082a7c2013-06-21 03:55:31 -030022#include <linux/kfifo.h>
Javier Martin186b2502012-07-26 05:53:35 -030023#include <linux/module.h>
24#include <linux/of_device.h>
25#include <linux/platform_device.h>
Philipp Zabel1e172732014-07-11 06:36:23 -030026#include <linux/pm_runtime.h>
Javier Martin186b2502012-07-26 05:53:35 -030027#include <linux/slab.h>
28#include <linux/videodev2.h>
29#include <linux/of.h>
Philipp Zabel657eee72013-04-29 16:17:14 -070030#include <linux/platform_data/coda.h>
Philipp Zabel8f452842014-07-11 06:36:35 -030031#include <linux/reset.h>
Javier Martin186b2502012-07-26 05:53:35 -030032
33#include <media/v4l2-ctrls.h>
34#include <media/v4l2-device.h>
Philipp Zabel918c66f2013-06-27 06:59:01 -030035#include <media/v4l2-event.h>
Javier Martin186b2502012-07-26 05:53:35 -030036#include <media/v4l2-ioctl.h>
37#include <media/v4l2-mem2mem.h>
38#include <media/videobuf2-core.h>
39#include <media/videobuf2-dma-contig.h>
Philipp Zabelbb04aa62015-01-23 13:51:30 -030040#include <media/videobuf2-vmalloc.h>
Javier Martin186b2502012-07-26 05:53:35 -030041
Philipp Zabela2b3e462014-07-23 12:28:39 -030042#include "coda.h"
Javier Martin186b2502012-07-26 05:53:35 -030043
44#define CODA_NAME "coda"
45
Philipp Zabel0cddc7e2013-09-30 10:34:44 -030046#define CODADX6_MAX_INSTANCES 4
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030047#define CODA_MAX_FORMATS 4
Javier Martin186b2502012-07-26 05:53:35 -030048
Javier Martin186b2502012-07-26 05:53:35 -030049#define CODA_PARA_BUF_SIZE (10 * 1024)
50#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
Javier Martin186b2502012-07-26 05:53:35 -030065struct coda_fmt {
66 char *name;
67 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030068};
69
Philipp Zabela2b3e462014-07-23 12:28:39 -030070void coda_write(struct coda_dev *dev, u32 data, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030071{
Philipp Zabeld60b18b2014-08-05 14:00:15 -030072 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030073 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
74 writel(data, dev->regs_base + reg);
75}
76
Philipp Zabela2b3e462014-07-23 12:28:39 -030077unsigned int coda_read(struct coda_dev *dev, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030078{
79 u32 data;
Philipp Zabelf23797b2014-08-06 08:02:23 -030080
Javier Martin186b2502012-07-26 05:53:35 -030081 data = readl(dev->regs_base + reg);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030082 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030083 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
84 return data;
85}
86
Philipp Zabel856d7d92014-09-29 09:53:44 -030087void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
88 struct vb2_buffer *buf, unsigned int reg_y)
89{
90 u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
91 u32 base_cb, base_cr;
92
93 switch (q_data->fourcc) {
94 case V4L2_PIX_FMT_YVU420:
95 /* Switch Cb and Cr for YVU420 format */
96 base_cr = base_y + q_data->bytesperline * q_data->height;
97 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
98 break;
99 case V4L2_PIX_FMT_YUV420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300100 case V4L2_PIX_FMT_NV12:
Philipp Zabel856d7d92014-09-29 09:53:44 -0300101 default:
102 base_cb = base_y + q_data->bytesperline * q_data->height;
103 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
104 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300105 case V4L2_PIX_FMT_YUV422P:
106 base_cb = base_y + q_data->bytesperline * q_data->height;
107 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
Philipp Zabel856d7d92014-09-29 09:53:44 -0300108 }
109
110 coda_write(ctx->dev, base_y, reg_y);
111 coda_write(ctx->dev, base_cb, reg_y + 4);
112 coda_write(ctx->dev, base_cr, reg_y + 8);
113}
114
Javier Martin186b2502012-07-26 05:53:35 -0300115/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300116 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300117 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300118static const struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300119 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300120 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300121 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300122 },
123 {
124 .name = "YUV 4:2:0 Planar, YCrCb",
125 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300126 },
127 {
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300128 .name = "YUV 4:2:0 Partial interleaved Y/CbCr",
129 .fourcc = V4L2_PIX_FMT_NV12,
130 },
131 {
Philipp Zabel4de69312014-10-02 14:08:26 -0300132 .name = "YUV 4:2:2 Planar, YCbCr",
133 .fourcc = V4L2_PIX_FMT_YUV422P,
134 },
135 {
Javier Martin186b2502012-07-26 05:53:35 -0300136 .name = "H264 Encoded Stream",
137 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300138 },
139 {
140 .name = "MPEG4 Encoded Stream",
141 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300142 },
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300143 {
144 .name = "JPEG Encoded Images",
145 .fourcc = V4L2_PIX_FMT_JPEG,
146 },
Javier Martin186b2502012-07-26 05:53:35 -0300147};
148
Philipp Zabelb96904e2013-05-23 10:42:58 -0300149#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
150 { mode, src_fourcc, dst_fourcc, max_w, max_h }
151
152/*
153 * Arrays of codecs supported by each given version of Coda:
154 * i.MX27 -> codadx6
155 * i.MX5x -> coda7
156 * i.MX6 -> coda960
157 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
158 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300159static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300160 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
161 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300162};
163
Philipp Zabel814c3762014-07-18 07:22:45 -0300164static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300165 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
166 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300167 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300168 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
169 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300170 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300171};
172
Philipp Zabel814c3762014-07-18 07:22:45 -0300173static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300174 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
175 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
176 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
177 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300178};
179
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300180struct coda_video_device {
181 const char *name;
182 enum coda_inst_type type;
183 const struct coda_context_ops *ops;
Philipp Zabela22496c2015-01-23 13:51:33 -0300184 bool direct;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300185 u32 src_formats[CODA_MAX_FORMATS];
186 u32 dst_formats[CODA_MAX_FORMATS];
187};
188
189static const struct coda_video_device coda_bit_encoder = {
190 .name = "coda-encoder",
191 .type = CODA_INST_ENCODER,
192 .ops = &coda_bit_encode_ops,
193 .src_formats = {
194 V4L2_PIX_FMT_YUV420,
195 V4L2_PIX_FMT_YVU420,
196 V4L2_PIX_FMT_NV12,
197 },
198 .dst_formats = {
199 V4L2_PIX_FMT_H264,
200 V4L2_PIX_FMT_MPEG4,
201 },
202};
203
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300204static const struct coda_video_device coda_bit_jpeg_encoder = {
205 .name = "coda-jpeg-encoder",
206 .type = CODA_INST_ENCODER,
207 .ops = &coda_bit_encode_ops,
208 .src_formats = {
209 V4L2_PIX_FMT_YUV420,
210 V4L2_PIX_FMT_YVU420,
211 V4L2_PIX_FMT_NV12,
212 V4L2_PIX_FMT_YUV422P,
213 },
214 .dst_formats = {
215 V4L2_PIX_FMT_JPEG,
216 },
217};
218
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300219static const struct coda_video_device coda_bit_decoder = {
220 .name = "coda-decoder",
221 .type = CODA_INST_DECODER,
222 .ops = &coda_bit_decode_ops,
223 .src_formats = {
224 V4L2_PIX_FMT_H264,
225 V4L2_PIX_FMT_MPEG4,
226 },
227 .dst_formats = {
228 V4L2_PIX_FMT_YUV420,
229 V4L2_PIX_FMT_YVU420,
230 V4L2_PIX_FMT_NV12,
231 },
232};
233
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300234static const struct coda_video_device coda_bit_jpeg_decoder = {
235 .name = "coda-jpeg-decoder",
236 .type = CODA_INST_DECODER,
237 .ops = &coda_bit_decode_ops,
238 .src_formats = {
239 V4L2_PIX_FMT_JPEG,
240 },
241 .dst_formats = {
242 V4L2_PIX_FMT_YUV420,
243 V4L2_PIX_FMT_YVU420,
244 V4L2_PIX_FMT_NV12,
245 V4L2_PIX_FMT_YUV422P,
246 },
247};
248
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300249static const struct coda_video_device *codadx6_video_devices[] = {
250 &coda_bit_encoder,
251};
252
253static const struct coda_video_device *coda7_video_devices[] = {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300254 &coda_bit_jpeg_encoder,
255 &coda_bit_jpeg_decoder,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300256 &coda_bit_encoder,
257 &coda_bit_decoder,
258};
259
260static const struct coda_video_device *coda9_video_devices[] = {
261 &coda_bit_encoder,
262 &coda_bit_decoder,
263};
264
Philipp Zabelb96904e2013-05-23 10:42:58 -0300265static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300266{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300267 switch (fourcc) {
268 case V4L2_PIX_FMT_YUV420:
269 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300270 case V4L2_PIX_FMT_NV12:
Philipp Zabel4de69312014-10-02 14:08:26 -0300271 case V4L2_PIX_FMT_YUV422P:
Philipp Zabelb96904e2013-05-23 10:42:58 -0300272 return true;
273 default:
274 return false;
275 }
276}
Javier Martin186b2502012-07-26 05:53:35 -0300277
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300278static const char *coda_format_name(u32 fourcc)
279{
280 int i;
281
282 for (i = 0; i < ARRAY_SIZE(coda_formats); i++) {
283 if (coda_formats[i].fourcc == fourcc)
284 return coda_formats[i].name;
285 }
286
287 return NULL;
288}
289
Philipp Zabelb96904e2013-05-23 10:42:58 -0300290/*
291 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
292 * tables.
293 */
294static u32 coda_format_normalize_yuv(u32 fourcc)
295{
296 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
297}
298
Philipp Zabel814c3762014-07-18 07:22:45 -0300299static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
300 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300301{
Philipp Zabel814c3762014-07-18 07:22:45 -0300302 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300303 int num_codecs = dev->devtype->num_codecs;
304 int k;
305
306 src_fourcc = coda_format_normalize_yuv(src_fourcc);
307 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
308 if (src_fourcc == dst_fourcc)
309 return NULL;
310
311 for (k = 0; k < num_codecs; k++) {
312 if (codecs[k].src_fourcc == src_fourcc &&
313 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300314 break;
315 }
316
Philipp Zabelb96904e2013-05-23 10:42:58 -0300317 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300318 return NULL;
319
Philipp Zabelb96904e2013-05-23 10:42:58 -0300320 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300321}
322
Philipp Zabel57625592013-09-30 10:34:51 -0300323static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300324 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300325 int *max_w, int *max_h)
326{
Philipp Zabel814c3762014-07-18 07:22:45 -0300327 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300328 int num_codecs = dev->devtype->num_codecs;
329 unsigned int w, h;
330 int k;
331
332 if (codec) {
333 w = codec->max_w;
334 h = codec->max_h;
335 } else {
336 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
337 w = max(w, codecs[k].max_w);
338 h = max(h, codecs[k].max_h);
339 }
340 }
341
342 if (max_w)
343 *max_w = w;
344 if (max_h)
345 *max_h = h;
346}
347
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300348const struct coda_video_device *to_coda_video_device(struct video_device *vdev)
349{
350 struct coda_dev *dev = video_get_drvdata(vdev);
351 unsigned int i = vdev - dev->vfd;
352
353 if (i >= dev->devtype->num_vdevs)
354 return NULL;
355
356 return dev->devtype->vdevs[i];
357}
358
Philipp Zabel79924ca2014-07-23 12:28:45 -0300359const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300360{
361 static char buf[9];
362
363 switch (product) {
364 case CODA_DX6:
365 return "CodaDx6";
366 case CODA_7541:
367 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300368 case CODA_960:
369 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300370 default:
371 snprintf(buf, sizeof(buf), "(0x%04x)", product);
372 return buf;
373 }
374}
375
Javier Martin186b2502012-07-26 05:53:35 -0300376/*
377 * V4L2 ioctl() operations.
378 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300379static int coda_querycap(struct file *file, void *priv,
380 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300381{
Philipp Zabel927933f2013-09-30 10:34:48 -0300382 struct coda_ctx *ctx = fh_to_ctx(priv);
383
Javier Martin186b2502012-07-26 05:53:35 -0300384 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300385 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
386 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300387 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300388 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300389 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
390
391 return 0;
392}
393
Philipp Zabel22e244b2014-07-18 07:22:43 -0300394static int coda_enum_fmt(struct file *file, void *priv,
395 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300396{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300397 struct video_device *vdev = video_devdata(file);
398 const struct coda_video_device *cvd = to_coda_video_device(vdev);
399 const u32 *formats;
400 const char *name;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300401
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300402 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
403 formats = cvd->src_formats;
404 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
405 formats = cvd->dst_formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300406 else
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300407 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300408
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300409 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
410 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300411
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300412 name = coda_format_name(formats[f->index]);
413 strlcpy(f->description, name, sizeof(f->description));
414 f->pixelformat = formats[f->index];
415 if (!coda_format_is_yuv(formats[f->index]))
416 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300417
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300418 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300419}
420
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300421static int coda_g_fmt(struct file *file, void *priv,
422 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300423{
Javier Martin186b2502012-07-26 05:53:35 -0300424 struct coda_q_data *q_data;
425 struct coda_ctx *ctx = fh_to_ctx(priv);
426
Javier Martin186b2502012-07-26 05:53:35 -0300427 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300428 if (!q_data)
429 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300430
431 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300432 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300433 f->fmt.pix.width = q_data->width;
434 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300435 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300436
437 f->fmt.pix.sizeimage = q_data->sizeimage;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300438 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
439 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
440 else
441 f->fmt.pix.colorspace = ctx->colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300442
443 return 0;
444}
445
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300446static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
447{
448 struct coda_q_data *q_data;
449 const u32 *formats;
450 int i;
451
452 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
453 formats = ctx->cvd->src_formats;
454 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
455 formats = ctx->cvd->dst_formats;
456 else
457 return -EINVAL;
458
459 for (i = 0; i < CODA_MAX_FORMATS; i++) {
460 if (formats[i] == f->fmt.pix.pixelformat) {
461 f->fmt.pix.pixelformat = formats[i];
462 return 0;
463 }
464 }
465
466 /* Fall back to currently set pixelformat */
467 q_data = get_q_data(ctx, f->type);
468 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300469
470 return 0;
471}
472
Philipp Zabel8e428d52015-01-23 13:51:29 -0300473static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
474 u32 width, u32 height)
475{
476 /*
477 * This is a rough estimate for sensible compressed buffer
478 * sizes (between 1 and 16 bits per pixel). This could be
479 * improved by better format specific worst case estimates.
480 */
481 return round_up(clamp(sizeimage, width * height / 8,
482 width * height * 2), PAGE_SIZE);
483}
484
Philipp Zabel814c3762014-07-18 07:22:45 -0300485static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300486 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300487{
Philipp Zabel57625592013-09-30 10:34:51 -0300488 struct coda_dev *dev = ctx->dev;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300489 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300490 enum v4l2_field field;
491
492 field = f->fmt.pix.field;
493 if (field == V4L2_FIELD_ANY)
494 field = V4L2_FIELD_NONE;
495 else if (V4L2_FIELD_NONE != field)
496 return -EINVAL;
497
498 /* V4L2 specification suggests the driver corrects the format struct
499 * if any of the dimensions is unsupported */
500 f->fmt.pix.field = field;
501
Philipp Zabel57625592013-09-30 10:34:51 -0300502 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
503 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
504 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
505 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300506
Philipp Zabel57625592013-09-30 10:34:51 -0300507 switch (f->fmt.pix.pixelformat) {
508 case V4L2_PIX_FMT_YUV420:
509 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300510 case V4L2_PIX_FMT_NV12:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300511 /*
512 * Frame stride must be at least multiple of 8,
513 * but multiple of 16 for h.264 or JPEG 4:2:x
514 */
Philipp Zabel451dfe52014-07-11 06:36:39 -0300515 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300516 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300517 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300518 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300519 case V4L2_PIX_FMT_YUV422P:
520 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
521 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
522 f->fmt.pix.height * 2;
523 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300524 case V4L2_PIX_FMT_JPEG:
525 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
526 /* fallthrough */
Philipp Zabel57625592013-09-30 10:34:51 -0300527 case V4L2_PIX_FMT_H264:
528 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -0300529 f->fmt.pix.bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -0300530 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
531 f->fmt.pix.sizeimage,
532 f->fmt.pix.width,
533 f->fmt.pix.height);
Philipp Zabel57625592013-09-30 10:34:51 -0300534 break;
535 default:
536 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300537 }
538
539 return 0;
540}
541
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300542static int coda_try_fmt_vid_cap(struct file *file, void *priv,
543 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300544{
Javier Martin186b2502012-07-26 05:53:35 -0300545 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300546 const struct coda_q_data *q_data_src;
547 const struct coda_codec *codec;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300548 struct vb2_queue *src_vq;
549 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300550
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300551 ret = coda_try_pixelformat(ctx, f);
552 if (ret < 0)
553 return ret;
554
555 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
556
Philipp Zabel918c66f2013-06-27 06:59:01 -0300557 /*
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300558 * If the source format is already fixed, only allow the same output
559 * resolution
Philipp Zabel918c66f2013-06-27 06:59:01 -0300560 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300561 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300562 if (vb2_is_streaming(src_vq)) {
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300563 f->fmt.pix.width = q_data_src->width;
564 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300565 }
Javier Martin186b2502012-07-26 05:53:35 -0300566
567 f->fmt.pix.colorspace = ctx->colorspace;
568
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300569 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
570 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
571 f->fmt.pix.pixelformat);
572 if (!codec)
573 return -EINVAL;
574
Philipp Zabel57625592013-09-30 10:34:51 -0300575 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300576 if (ret < 0)
577 return ret;
578
579 /* The h.264 decoder only returns complete 16x16 macroblocks */
580 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300581 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300582 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300583 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300584 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
585 f->fmt.pix.height * 3 / 2;
586 }
587
588 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300589}
590
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300591static int coda_try_fmt_vid_out(struct file *file, void *priv,
592 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300593{
594 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300595 struct coda_dev *dev = ctx->dev;
596 const struct coda_q_data *q_data_dst;
597 const struct coda_codec *codec;
598 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300599
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300600 ret = coda_try_pixelformat(ctx, f);
601 if (ret < 0)
602 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300603
Philipp Zabel5f9826e2015-01-23 13:51:21 -0300604 switch (f->fmt.pix.colorspace) {
605 case V4L2_COLORSPACE_REC709:
606 case V4L2_COLORSPACE_JPEG:
607 break;
608 default:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300609 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
610 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
611 else
612 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Javier Martin186b2502012-07-26 05:53:35 -0300613 }
614
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300615 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
616 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300617
618 return coda_try_fmt(ctx, codec, f);
619}
620
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300621static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300622{
623 struct coda_q_data *q_data;
624 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300625
Philipp Zabel14604e32014-07-11 06:36:22 -0300626 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300627 if (!vq)
628 return -EINVAL;
629
630 q_data = get_q_data(ctx, f->type);
631 if (!q_data)
632 return -EINVAL;
633
634 if (vb2_is_busy(vq)) {
635 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
636 return -EBUSY;
637 }
638
Philipp Zabelb96904e2013-05-23 10:42:58 -0300639 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300640 q_data->width = f->fmt.pix.width;
641 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300642 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300643 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300644 q_data->rect.left = 0;
645 q_data->rect.top = 0;
646 q_data->rect.width = f->fmt.pix.width;
647 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300648
649 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
650 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300651 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300652
653 return 0;
654}
655
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300656static int coda_s_fmt_vid_cap(struct file *file, void *priv,
657 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300658{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300659 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300660 int ret;
661
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300662 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300663 if (ret)
664 return ret;
665
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300666 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300667}
668
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300669static int coda_s_fmt_vid_out(struct file *file, void *priv,
670 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300671{
672 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300673 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300674 int ret;
675
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300676 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300677 if (ret)
678 return ret;
679
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300680 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300681 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300682 return ret;
683
684 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300685
Philipp Zabel1c2f6a92015-01-23 13:51:22 -0300686 memset(&f_cap, 0, sizeof(f_cap));
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300687 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
688 coda_g_fmt(file, priv, &f_cap);
689 f_cap.fmt.pix.width = f->fmt.pix.width;
690 f_cap.fmt.pix.height = f->fmt.pix.height;
691
692 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
693 if (ret)
694 return ret;
695
696 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300697}
698
Philipp Zabel73751da2015-03-24 14:30:51 -0300699static int coda_reqbufs(struct file *file, void *priv,
700 struct v4l2_requestbuffers *rb)
701{
702 struct coda_ctx *ctx = fh_to_ctx(priv);
703 int ret;
704
705 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
706 if (ret)
707 return ret;
708
709 /*
710 * Allow to allocate instance specific per-context buffers, such as
711 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
712 */
713 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
714 return ctx->ops->reqbufs(ctx, rb);
715
716 return 0;
717}
718
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300719static int coda_qbuf(struct file *file, void *priv,
720 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300721{
722 struct coda_ctx *ctx = fh_to_ctx(priv);
723
Philipp Zabel14604e32014-07-11 06:36:22 -0300724 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300725}
726
Philipp Zabel918c66f2013-06-27 06:59:01 -0300727static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
728 struct v4l2_buffer *buf)
729{
730 struct vb2_queue *src_vq;
731
Philipp Zabel14604e32014-07-11 06:36:22 -0300732 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300733
734 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
735 (buf->sequence == (ctx->qsequence - 1)));
736}
737
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300738static int coda_dqbuf(struct file *file, void *priv,
739 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300740{
741 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300742 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300743
Philipp Zabel14604e32014-07-11 06:36:22 -0300744 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300745
746 /* If this is the last capture buffer, emit an end-of-stream event */
747 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
748 coda_buf_is_end_of_stream(ctx, buf)) {
749 const struct v4l2_event eos_event = {
750 .type = V4L2_EVENT_EOS
751 };
752
753 v4l2_event_queue_fh(&ctx->fh, &eos_event);
754 }
755
756 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300757}
758
Philipp Zabel52c41672014-07-11 06:36:19 -0300759static int coda_g_selection(struct file *file, void *fh,
760 struct v4l2_selection *s)
761{
762 struct coda_ctx *ctx = fh_to_ctx(fh);
763 struct coda_q_data *q_data;
764 struct v4l2_rect r, *rsel;
765
766 q_data = get_q_data(ctx, s->type);
767 if (!q_data)
768 return -EINVAL;
769
770 r.left = 0;
771 r.top = 0;
772 r.width = q_data->width;
773 r.height = q_data->height;
774 rsel = &q_data->rect;
775
776 switch (s->target) {
777 case V4L2_SEL_TGT_CROP_DEFAULT:
778 case V4L2_SEL_TGT_CROP_BOUNDS:
779 rsel = &r;
780 /* fallthrough */
781 case V4L2_SEL_TGT_CROP:
782 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
783 return -EINVAL;
784 break;
785 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
786 case V4L2_SEL_TGT_COMPOSE_PADDED:
787 rsel = &r;
788 /* fallthrough */
789 case V4L2_SEL_TGT_COMPOSE:
790 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
791 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
792 return -EINVAL;
793 break;
794 default:
795 return -EINVAL;
796 }
797
798 s->r = *rsel;
799
800 return 0;
801}
802
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300803static int coda_try_decoder_cmd(struct file *file, void *fh,
804 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300805{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300806 if (dc->cmd != V4L2_DEC_CMD_STOP)
807 return -EINVAL;
808
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300809 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300810 return -EINVAL;
811
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300812 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300813 return -EINVAL;
814
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300815 return 0;
816}
817
818static int coda_decoder_cmd(struct file *file, void *fh,
819 struct v4l2_decoder_cmd *dc)
820{
821 struct coda_ctx *ctx = fh_to_ctx(fh);
822 int ret;
823
824 ret = coda_try_decoder_cmd(file, fh, dc);
825 if (ret < 0)
826 return ret;
827
828 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300829 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300830 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300831
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300832 /* Set the stream-end flag on this context */
833 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300834 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300835 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300836
Philipp Zabel918c66f2013-06-27 06:59:01 -0300837 return 0;
838}
839
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300840static int coda_subscribe_event(struct v4l2_fh *fh,
841 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300842{
843 switch (sub->type) {
844 case V4L2_EVENT_EOS:
845 return v4l2_event_subscribe(fh, sub, 0, NULL);
846 default:
847 return v4l2_ctrl_subscribe_event(fh, sub);
848 }
Javier Martin186b2502012-07-26 05:53:35 -0300849}
850
851static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300852 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300853
Philipp Zabel22e244b2014-07-18 07:22:43 -0300854 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300855 .vidioc_g_fmt_vid_cap = coda_g_fmt,
856 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
857 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300858
Philipp Zabel22e244b2014-07-18 07:22:43 -0300859 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300860 .vidioc_g_fmt_vid_out = coda_g_fmt,
861 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
862 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300863
Philipp Zabel73751da2015-03-24 14:30:51 -0300864 .vidioc_reqbufs = coda_reqbufs,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300865 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300866
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300867 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300868 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300869 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300870 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300871
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300872 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
873 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300874
Philipp Zabel52c41672014-07-11 06:36:19 -0300875 .vidioc_g_selection = coda_g_selection,
876
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300877 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300878 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300879
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300880 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300881 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300882};
883
Philipp Zabel79924ca2014-07-23 12:28:45 -0300884void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300885{
886 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
887 struct coda_dev *dev = ctx->dev;
888 int i;
889
890 for (i = 0; i < 16; i++)
891 coda_write(dev, tiled_map->xy2ca_map[i],
892 CODA9_GDI_XY2_CAS_0 + 4 * i);
893 for (i = 0; i < 4; i++)
894 coda_write(dev, tiled_map->xy2ba_map[i],
895 CODA9_GDI_XY2_BA_0 + 4 * i);
896 for (i = 0; i < 16; i++)
897 coda_write(dev, tiled_map->xy2ra_map[i],
898 CODA9_GDI_XY2_RAS_0 + 4 * i);
899 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
900 for (i = 0; i < 32; i++)
901 coda_write(dev, tiled_map->rbc2axi_map[i],
902 CODA9_GDI_RBC2_AXI_0 + 4 * i);
903}
904
Javier Martin186b2502012-07-26 05:53:35 -0300905/*
906 * Mem-to-mem operations.
907 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300908
909static void coda_device_run(void *m2m_priv)
910{
911 struct coda_ctx *ctx = m2m_priv;
912 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300913
914 queue_work(dev->workqueue, &ctx->pic_run_work);
915}
916
Philipp Zabel32b6f202014-07-11 06:36:20 -0300917static void coda_pic_run_work(struct work_struct *work)
918{
919 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
920 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300921 int ret;
922
923 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300924 mutex_lock(&dev->coda_mutex);
925
Philipp Zabela1192a12014-07-23 12:28:40 -0300926 ret = ctx->ops->prepare_run(ctx);
927 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
928 mutex_unlock(&dev->coda_mutex);
929 mutex_unlock(&ctx->buffer_mutex);
930 /* job_finish scheduled by prepare_decode */
931 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300932 }
933
Philipp Zabelf23797b2014-08-06 08:02:23 -0300934 if (!wait_for_completion_timeout(&ctx->completion,
935 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300936 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300937
938 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300939
940 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300941 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300942 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300943 }
944
Philipp Zabel747d7642015-01-23 13:51:31 -0300945 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
946 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -0300947 queue_work(dev->workqueue, &ctx->seq_end_work);
948
949 mutex_unlock(&dev->coda_mutex);
950 mutex_unlock(&ctx->buffer_mutex);
951
Philipp Zabel14604e32014-07-11 06:36:22 -0300952 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300953}
954
955static int coda_job_ready(void *m2m_priv)
956{
957 struct coda_ctx *ctx = m2m_priv;
958
959 /*
960 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300961 * and 1 frame are needed. In the decoder case,
962 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300963 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300964 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300965 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300966 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
967 "not ready: not enough video buffers.\n");
968 return 0;
969 }
970
Philipp Zabel14604e32014-07-11 06:36:22 -0300971 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300972 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
973 "not ready: not enough video capture buffers.\n");
974 return 0;
975 }
976
Philipp Zabela22496c2015-01-23 13:51:33 -0300977 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -0300978 struct list_head *meta;
979 bool stream_end;
980 int num_metas;
981 int src_bufs;
982
983 if (ctx->hold && !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
984 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
985 "%d: not ready: on hold for more buffers.\n",
986 ctx->idx);
987 return 0;
988 }
989
990 stream_end = ctx->bit_stream_param &
991 CODA_BIT_STREAM_END_FLAG;
992
993 num_metas = 0;
994 list_for_each(meta, &ctx->buffer_meta_list)
995 num_metas++;
996
997 src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
998
999 if (!stream_end && (num_metas + src_bufs) < 2) {
1000 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1001 "%d: not ready: need 2 buffers available (%d, %d)\n",
1002 ctx->idx, num_metas, src_bufs);
1003 return 0;
1004 }
1005
1006
1007 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
1008 !stream_end && (coda_get_bitstream_payload(ctx) < 512)) {
1009 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1010 "%d: not ready: not enough bitstream data (%d).\n",
1011 ctx->idx, coda_get_bitstream_payload(ctx));
1012 return 0;
1013 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001014 }
1015
Philipp Zabel3e748262013-05-23 10:43:01 -03001016 if (ctx->aborting) {
1017 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1018 "not ready: aborting\n");
1019 return 0;
1020 }
1021
Javier Martin186b2502012-07-26 05:53:35 -03001022 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1023 "job ready\n");
1024 return 1;
1025}
1026
1027static void coda_job_abort(void *priv)
1028{
1029 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001030
1031 ctx->aborting = 1;
1032
1033 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1034 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001035}
1036
1037static void coda_lock(void *m2m_priv)
1038{
1039 struct coda_ctx *ctx = m2m_priv;
1040 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001041
Javier Martin186b2502012-07-26 05:53:35 -03001042 mutex_lock(&pcdev->dev_mutex);
1043}
1044
1045static void coda_unlock(void *m2m_priv)
1046{
1047 struct coda_ctx *ctx = m2m_priv;
1048 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001049
Javier Martin186b2502012-07-26 05:53:35 -03001050 mutex_unlock(&pcdev->dev_mutex);
1051}
1052
Philipp Zabel814c3762014-07-18 07:22:45 -03001053static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001054 .device_run = coda_device_run,
1055 .job_ready = coda_job_ready,
1056 .job_abort = coda_job_abort,
1057 .lock = coda_lock,
1058 .unlock = coda_unlock,
1059};
1060
Philipp Zabel89548442014-07-11 06:36:17 -03001061static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1062{
1063 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1064 int luma_map, chro_map, i;
1065
1066 memset(tiled_map, 0, sizeof(*tiled_map));
1067
1068 luma_map = 64;
1069 chro_map = 64;
1070 tiled_map->map_type = tiled_map_type;
1071 for (i = 0; i < 16; i++)
1072 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1073 for (i = 0; i < 4; i++)
1074 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1075 for (i = 0; i < 16; i++)
1076 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1077
1078 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1079 tiled_map->xy2rbc_config = 0;
1080 } else {
1081 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1082 tiled_map_type);
1083 return;
1084 }
1085}
1086
Javier Martin186b2502012-07-26 05:53:35 -03001087static void set_default_params(struct coda_ctx *ctx)
1088{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001089 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001090
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001091 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1092 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001093 max_w = min(ctx->codec->max_w, 1920U);
1094 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001095 usize = max_w * max_h * 3 / 2;
1096 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001097
Philipp Zabel121cacf2014-07-18 07:22:42 -03001098 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001099 ctx->colorspace = V4L2_COLORSPACE_REC709;
1100 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001101
1102 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001103 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1104 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1105 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1106 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001107 ctx->q_data[V4L2_M2M_DST].width = max_w;
1108 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001109 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1110 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001111 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001112 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001113 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001114 } else {
1115 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001116 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001117 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001118 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001119 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001120 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1121 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1122 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1123 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001124
1125 if (ctx->dev->devtype->product == CODA_960)
1126 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001127}
1128
1129/*
1130 * Queue operations
1131 */
1132static int coda_queue_setup(struct vb2_queue *vq,
1133 const struct v4l2_format *fmt,
1134 unsigned int *nbuffers, unsigned int *nplanes,
1135 unsigned int sizes[], void *alloc_ctxs[])
1136{
1137 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001138 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001139 unsigned int size;
1140
Philipp Zabele34db062012-08-29 08:22:00 -03001141 q_data = get_q_data(ctx, vq->type);
1142 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001143
1144 *nplanes = 1;
1145 sizes[0] = size;
1146
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001147 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
Javier Martin186b2502012-07-26 05:53:35 -03001148 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1149
1150 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1151 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1152
1153 return 0;
1154}
1155
1156static int coda_buf_prepare(struct vb2_buffer *vb)
1157{
1158 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1159 struct coda_q_data *q_data;
1160
1161 q_data = get_q_data(ctx, vb->vb2_queue->type);
1162
1163 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1164 v4l2_warn(&ctx->dev->v4l2_dev,
1165 "%s data will not fit into plane (%lu < %lu)\n",
1166 __func__, vb2_plane_size(vb, 0),
1167 (long)q_data->sizeimage);
1168 return -EINVAL;
1169 }
1170
Javier Martin186b2502012-07-26 05:53:35 -03001171 return 0;
1172}
1173
1174static void coda_buf_queue(struct vb2_buffer *vb)
1175{
1176 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabelcb06b702015-01-23 13:51:35 -03001177 struct vb2_queue *vq = vb->vb2_queue;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001178 struct coda_q_data *q_data;
1179
1180 q_data = get_q_data(ctx, vb->vb2_queue->type);
1181
1182 /*
1183 * In the decoder case, immediately try to copy the buffer into the
1184 * bitstream ringbuffer and mark it as ready to be dequeued.
1185 */
Philipp Zabelcb06b702015-01-23 13:51:35 -03001186 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001187 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001188 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001189 * the stream end
1190 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001191 if (vb2_get_plane_payload(vb, 0) == 0)
1192 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001193 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001194 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001195 if (vb2_is_streaming(vb->vb2_queue))
1196 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001197 mutex_unlock(&ctx->bitstream_mutex);
1198 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001199 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001200 }
Javier Martin186b2502012-07-26 05:53:35 -03001201}
1202
Philipp Zabel79924ca2014-07-23 12:28:45 -03001203int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1204 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001205{
1206 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1207 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001208 if (!buf->vaddr) {
1209 v4l2_err(&dev->v4l2_dev,
1210 "Failed to allocate %s buffer of size %u\n",
1211 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001212 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001213 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001214
1215 buf->size = size;
1216
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001217 if (name && parent) {
1218 buf->blob.data = buf->vaddr;
1219 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001220 buf->dentry = debugfs_create_blob(name, 0644, parent,
1221 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001222 if (!buf->dentry)
1223 dev_warn(&dev->plat_dev->dev,
1224 "failed to create debugfs entry %s\n", name);
1225 }
1226
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001227 return 0;
1228}
1229
Philipp Zabel79924ca2014-07-23 12:28:45 -03001230void coda_free_aux_buf(struct coda_dev *dev,
1231 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001232{
1233 if (buf->vaddr) {
1234 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1235 buf->vaddr, buf->paddr);
1236 buf->vaddr = NULL;
1237 buf->size = 0;
Peter Seidererd446ec82015-03-24 14:30:48 -03001238 debugfs_remove(buf->dentry);
1239 buf->dentry = NULL;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001240 }
1241}
1242
Javier Martin186b2502012-07-26 05:53:35 -03001243static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1244{
1245 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1246 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001247 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001248 struct vb2_buffer *buf;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001249 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001250
Philipp Zabelb96904e2013-05-23 10:42:58 -03001251 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001252 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001253 if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
1254 (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
1255 ctx->dev->devtype->product == CODA_7541)) {
Michael Olbricheabed932014-07-18 07:22:40 -03001256 /* copy the buffers that where queued before streamon */
1257 mutex_lock(&ctx->bitstream_mutex);
1258 coda_fill_bitstream(ctx);
1259 mutex_unlock(&ctx->bitstream_mutex);
1260
Philipp Zabelb9063522014-08-05 14:00:10 -03001261 if (coda_get_bitstream_payload(ctx) < 512) {
1262 ret = -EINVAL;
1263 goto err;
1264 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001265 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001266 if (count < 1) {
1267 ret = -EINVAL;
1268 goto err;
1269 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001270 }
1271
1272 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001273 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001274 if (count < 1) {
1275 ret = -EINVAL;
1276 goto err;
1277 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001278
1279 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001280 }
Javier Martin186b2502012-07-26 05:53:35 -03001281
Philipp Zabelb96904e2013-05-23 10:42:58 -03001282 /* Don't start the coda unless both queues are on */
1283 if (!(ctx->streamon_out & ctx->streamon_cap))
1284 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001285
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001286 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001287 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001288 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001289
Philipp Zabelb96904e2013-05-23 10:42:58 -03001290 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001291 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -03001292
Philipp Zabelb96904e2013-05-23 10:42:58 -03001293 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1294 q_data_dst->fourcc);
1295 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001296 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001297 ret = -EINVAL;
1298 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001299 }
1300
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001301 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1302 ctx->params.gop_size = 1;
1303 ctx->gopcounter = ctx->params.gop_size - 1;
1304
Philipp Zabela1192a12014-07-23 12:28:40 -03001305 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001306 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001307 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001308 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001309 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001310 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001311 }
1312
Philipp Zabel89548442014-07-11 06:36:17 -03001313 ctx->initialized = 1;
1314 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001315
1316err:
1317 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1318 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001319 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001320 } else {
1321 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001322 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001323 }
1324 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001325}
1326
Hans Verkuile37559b2014-04-17 02:47:21 -03001327static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001328{
1329 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001330 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001331 struct vb2_buffer *buf;
Javier Martin186b2502012-07-26 05:53:35 -03001332
1333 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001334 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001335 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001336 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001337
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001338 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001339
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001340 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001341
1342 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1343 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001344 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001345 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001346 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001347 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001348
1349 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001350 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001351
1352 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1353 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001354 }
1355
Philipp Zabel918c66f2013-06-27 06:59:01 -03001356 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001357 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001358
Philipp Zabelf4706d62015-01-23 13:51:27 -03001359 if (ctx->ops->seq_end_work) {
1360 queue_work(dev->workqueue, &ctx->seq_end_work);
1361 flush_work(&ctx->seq_end_work);
1362 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001363 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001364 while (!list_empty(&ctx->buffer_meta_list)) {
1365 meta = list_first_entry(&ctx->buffer_meta_list,
1366 struct coda_buffer_meta, list);
1367 list_del(&meta->list);
1368 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001369 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001370 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001371 kfifo_init(&ctx->bitstream_fifo,
1372 ctx->bitstream.vaddr, ctx->bitstream.size);
Philipp Zabelf4706d62015-01-23 13:51:27 -03001373 ctx->initialized = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001374 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001375 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001376 }
Javier Martin186b2502012-07-26 05:53:35 -03001377}
1378
Philipp Zabel121cacf2014-07-18 07:22:42 -03001379static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001380 .queue_setup = coda_queue_setup,
1381 .buf_prepare = coda_buf_prepare,
1382 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001383 .start_streaming = coda_start_streaming,
1384 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001385 .wait_prepare = vb2_ops_wait_prepare,
1386 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001387};
1388
1389static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1390{
1391 struct coda_ctx *ctx =
1392 container_of(ctrl->handler, struct coda_ctx, ctrls);
1393
1394 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1395 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1396
1397 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001398 case V4L2_CID_HFLIP:
1399 if (ctrl->val)
1400 ctx->params.rot_mode |= CODA_MIR_HOR;
1401 else
1402 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1403 break;
1404 case V4L2_CID_VFLIP:
1405 if (ctrl->val)
1406 ctx->params.rot_mode |= CODA_MIR_VER;
1407 else
1408 ctx->params.rot_mode &= ~CODA_MIR_VER;
1409 break;
Javier Martin186b2502012-07-26 05:53:35 -03001410 case V4L2_CID_MPEG_VIDEO_BITRATE:
1411 ctx->params.bitrate = ctrl->val / 1000;
1412 break;
1413 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1414 ctx->params.gop_size = ctrl->val;
1415 break;
1416 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1417 ctx->params.h264_intra_qp = ctrl->val;
1418 break;
1419 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1420 ctx->params.h264_inter_qp = ctrl->val;
1421 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001422 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1423 ctx->params.h264_min_qp = ctrl->val;
1424 break;
1425 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1426 ctx->params.h264_max_qp = ctrl->val;
1427 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001428 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1429 ctx->params.h264_deblk_alpha = ctrl->val;
1430 break;
1431 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1432 ctx->params.h264_deblk_beta = ctrl->val;
1433 break;
1434 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1435 ctx->params.h264_deblk_enabled = (ctrl->val ==
1436 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1437 break;
Javier Martin186b2502012-07-26 05:53:35 -03001438 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1439 ctx->params.mpeg4_intra_qp = ctrl->val;
1440 break;
1441 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1442 ctx->params.mpeg4_inter_qp = ctrl->val;
1443 break;
1444 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1445 ctx->params.slice_mode = ctrl->val;
1446 break;
1447 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1448 ctx->params.slice_max_mb = ctrl->val;
1449 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001450 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1451 ctx->params.slice_max_bits = ctrl->val * 8;
1452 break;
Javier Martin186b2502012-07-26 05:53:35 -03001453 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1454 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001455 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1456 ctx->params.intra_refresh = ctrl->val;
1457 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001458 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1459 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1460 break;
1461 case V4L2_CID_JPEG_RESTART_INTERVAL:
1462 ctx->params.jpeg_restart_interval = ctrl->val;
1463 break;
Javier Martin186b2502012-07-26 05:53:35 -03001464 default:
1465 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1466 "Invalid control, id=%d, val=%d\n",
1467 ctrl->id, ctrl->val);
1468 return -EINVAL;
1469 }
1470
1471 return 0;
1472}
1473
Philipp Zabel814c3762014-07-18 07:22:45 -03001474static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001475 .s_ctrl = coda_s_ctrl,
1476};
1477
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001478static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001479{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001480 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel92b042e2015-03-18 08:15:36 -03001481 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001482 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1483 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1484 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001485 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001486 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001487 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001488 if (ctx->dev->devtype->product != CODA_960) {
1489 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1490 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1491 }
1492 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1493 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001494 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001495 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1496 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1497 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1498 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1499 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1500 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1501 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1502 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001503 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1504 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1505 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1506 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1507 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001508 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1509 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001510 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1511 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001512 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001513 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1514 500);
Javier Martin186b2502012-07-26 05:53:35 -03001515 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1516 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1517 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1518 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1519 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001520 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001521 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1522 1920 * 1088 / 256, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001523}
1524
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001525static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1526{
1527 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1528 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1529 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1530 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1531}
1532
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001533static int coda_ctrls_setup(struct coda_ctx *ctx)
1534{
1535 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1536
1537 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1538 V4L2_CID_HFLIP, 0, 1, 1, 0);
1539 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1540 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001541 if (ctx->inst_type == CODA_INST_ENCODER) {
1542 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1543 coda_jpeg_encode_ctrls(ctx);
1544 else
1545 coda_encode_ctrls(ctx);
1546 }
Javier Martin186b2502012-07-26 05:53:35 -03001547
1548 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001549 v4l2_err(&ctx->dev->v4l2_dev,
1550 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001551 ctx->ctrls.error);
1552 return -EINVAL;
1553 }
1554
1555 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1556}
1557
Philipp Zabel121cacf2014-07-18 07:22:42 -03001558static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001559{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001560 vq->drv_priv = ctx;
1561 vq->ops = &coda_qops;
1562 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1563 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1564 vq->lock = &ctx->dev->dev_mutex;
Kamil Debskie4af23d2015-02-23 09:26:18 -03001565 /* One way to indicate end-of-stream for coda is to set the
1566 * bytesused == 0. However by default videobuf2 handles bytesused
1567 * equal to 0 as a special case and changes its value to the size
1568 * of the buffer. Set the allow_zero_bytesused flag, so
1569 * that videobuf2 will keep the value of bytesused intact.
1570 */
1571 vq->allow_zero_bytesused = 1;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001572
1573 return vb2_queue_init(vq);
1574}
1575
Philipp Zabel79924ca2014-07-23 12:28:45 -03001576int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1577 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001578{
Javier Martin186b2502012-07-26 05:53:35 -03001579 int ret;
1580
Javier Martin186b2502012-07-26 05:53:35 -03001581 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001582 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001583 src_vq->mem_ops = &vb2_dma_contig_memops;
1584
Philipp Zabel121cacf2014-07-18 07:22:42 -03001585 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001586 if (ret)
1587 return ret;
1588
Javier Martin186b2502012-07-26 05:53:35 -03001589 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001590 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001591 dst_vq->mem_ops = &vb2_dma_contig_memops;
1592
Philipp Zabel121cacf2014-07-18 07:22:42 -03001593 return coda_queue_init(priv, dst_vq);
1594}
1595
Philipp Zabel79924ca2014-07-23 12:28:45 -03001596int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1597 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001598{
1599 int ret;
1600
1601 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001602 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1603 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001604
1605 ret = coda_queue_init(priv, src_vq);
1606 if (ret)
1607 return ret;
1608
1609 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1610 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1611 dst_vq->mem_ops = &vb2_dma_contig_memops;
1612
1613 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001614}
1615
Philipp Zabele11f3e62012-07-25 09:16:58 -03001616static int coda_next_free_instance(struct coda_dev *dev)
1617{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001618 int idx = ffz(dev->instance_mask);
1619
1620 if ((idx < 0) ||
1621 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1622 return -EBUSY;
1623
1624 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001625}
1626
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001627/*
1628 * File operations
1629 */
1630
1631static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001632{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001633 struct video_device *vdev = video_devdata(file);
1634 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001635 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001636 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001637 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001638 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001639
Philipp Zabelf23797b2014-08-06 08:02:23 -03001640 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001641 if (!ctx)
1642 return -ENOMEM;
1643
Fabio Estevamf82bc202013-08-21 11:14:16 -03001644 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001645 if (idx < 0) {
1646 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001647 goto err_coda_max;
1648 }
1649 set_bit(idx, &dev->instance_mask);
1650
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001651 name = kasprintf(GFP_KERNEL, "context%d", idx);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001652 if (!name) {
1653 ret = -ENOMEM;
1654 goto err_coda_name_init;
1655 }
1656
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001657 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1658 kfree(name);
1659
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001660 ctx->cvd = to_coda_video_device(vdev);
1661 ctx->inst_type = ctx->cvd->type;
1662 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001663 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001664 init_completion(&ctx->completion);
1665 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001666 if (ctx->ops->seq_end_work)
1667 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001668 v4l2_fh_init(&ctx->fh, video_devdata(file));
1669 file->private_data = &ctx->fh;
1670 v4l2_fh_add(&ctx->fh);
1671 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001672 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001673 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001674 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001675 ctx->frame_mem_ctrl = 1 << 12;
1676 /* fallthrough */
1677 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001678 ctx->reg_idx = 0;
1679 break;
1680 default:
1681 ctx->reg_idx = idx;
1682 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001683
Philipp Zabel1e172732014-07-11 06:36:23 -03001684 /* Power up and upload firmware if necessary */
1685 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1686 if (ret < 0) {
1687 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1688 goto err_pm_get;
1689 }
1690
Fabio Estevam79830db2013-08-21 11:14:17 -03001691 ret = clk_prepare_enable(dev->clk_per);
1692 if (ret)
1693 goto err_clk_per;
1694
1695 ret = clk_prepare_enable(dev->clk_ahb);
1696 if (ret)
1697 goto err_clk_ahb;
1698
Javier Martin186b2502012-07-26 05:53:35 -03001699 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001700 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1701 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001702 if (IS_ERR(ctx->fh.m2m_ctx)) {
1703 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001704
1705 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1706 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001707 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001708 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001709
Javier Martin186b2502012-07-26 05:53:35 -03001710 ret = coda_ctrls_setup(ctx);
1711 if (ret) {
1712 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001713 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001714 }
1715
1716 ctx->fh.ctrl_handler = &ctx->ctrls;
1717
Philipp Zabela22496c2015-01-23 13:51:33 -03001718 if (ctx->use_bit) {
1719 ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
1720 CODA_PARA_BUF_SIZE, "parabuf");
1721 if (ret < 0) {
1722 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1723 goto err_dma_alloc;
1724 }
Philipp Zabele55317e2015-01-23 13:51:34 -03001725 }
1726 if (ctx->use_bit && ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabela22496c2015-01-23 13:51:33 -03001727 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1728 ctx->bitstream.vaddr = dma_alloc_writecombine(
1729 &dev->plat_dev->dev, ctx->bitstream.size,
1730 &ctx->bitstream.paddr, GFP_KERNEL);
1731 if (!ctx->bitstream.vaddr) {
1732 v4l2_err(&dev->v4l2_dev,
1733 "failed to allocate bitstream ringbuffer");
1734 ret = -ENOMEM;
1735 goto err_dma_writecombine;
1736 }
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001737 }
1738 kfifo_init(&ctx->bitstream_fifo,
1739 ctx->bitstream.vaddr, ctx->bitstream.size);
1740 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001741 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001742 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001743
Javier Martin186b2502012-07-26 05:53:35 -03001744 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001745 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001746 coda_unlock(ctx);
1747
Javier Martin186b2502012-07-26 05:53:35 -03001748 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1749 ctx->idx, ctx);
1750
1751 return 0;
1752
Fabio Estevamf82bc202013-08-21 11:14:16 -03001753err_dma_writecombine:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001754 if (ctx->dev->devtype->product == CODA_DX6)
1755 coda_free_aux_buf(dev, &ctx->workbuf);
1756 coda_free_aux_buf(dev, &ctx->parabuf);
1757err_dma_alloc:
1758 v4l2_ctrl_handler_free(&ctx->ctrls);
1759err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001760 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001761err_ctx_init:
1762 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001763err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001764 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001765err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001766 pm_runtime_put_sync(&dev->plat_dev->dev);
1767err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001768 v4l2_fh_del(&ctx->fh);
1769 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001770 clear_bit(ctx->idx, &dev->instance_mask);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001771err_coda_name_init:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001772err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001773 kfree(ctx);
1774 return ret;
1775}
1776
1777static int coda_release(struct file *file)
1778{
1779 struct coda_dev *dev = video_drvdata(file);
1780 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1781
1782 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1783 ctx);
1784
Philipp Zabela22496c2015-01-23 13:51:33 -03001785 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03001786 coda_bit_stream_end_flag(ctx);
1787
Philipp Zabel918c66f2013-06-27 06:59:01 -03001788 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001789 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001790
1791 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel747d7642015-01-23 13:51:31 -03001792 if (ctx->initialized && ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001793 queue_work(dev->workqueue, &ctx->seq_end_work);
1794 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001795 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001796
Javier Martin186b2502012-07-26 05:53:35 -03001797 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001798 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001799 coda_unlock(ctx);
1800
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001801 if (ctx->bitstream.vaddr) {
1802 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1803 ctx->bitstream.vaddr, ctx->bitstream.paddr);
1804 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001805 if (ctx->dev->devtype->product == CODA_DX6)
1806 coda_free_aux_buf(dev, &ctx->workbuf);
1807
1808 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03001809 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001810 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001811 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001812 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001813 v4l2_fh_del(&ctx->fh);
1814 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001815 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001816 if (ctx->ops->release)
1817 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03001818 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03001819 kfree(ctx);
1820
1821 return 0;
1822}
1823
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001824static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001825 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001826 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03001827 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001828 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001829 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001830 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001831};
1832
Philipp Zabel87048bb2012-07-02 07:03:43 -03001833static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001834{
Javier Martin186b2502012-07-26 05:53:35 -03001835 u32 data;
1836 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001837 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001838
Fabio Estevam79830db2013-08-21 11:14:17 -03001839 ret = clk_prepare_enable(dev->clk_per);
1840 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001841 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001842
1843 ret = clk_prepare_enable(dev->clk_ahb);
1844 if (ret)
1845 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001846
Philipp Zabel8f452842014-07-11 06:36:35 -03001847 if (dev->rstc)
1848 reset_control_reset(dev->rstc);
1849
Javier Martin186b2502012-07-26 05:53:35 -03001850 /*
1851 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001852 * The 16-bit chars in the code buffer are in memory access
1853 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001854 * Data in this SRAM survives a reboot.
1855 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001856 p = (u16 *)dev->codebuf.vaddr;
1857 if (dev->devtype->product == CODA_DX6) {
1858 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1859 data = CODA_DOWN_ADDRESS_SET(i) |
1860 CODA_DOWN_DATA_SET(p[i ^ 1]);
1861 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1862 }
1863 } else {
1864 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1865 data = CODA_DOWN_ADDRESS_SET(i) |
1866 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1867 3 - (i % 4)]);
1868 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1869 }
Javier Martin186b2502012-07-26 05:53:35 -03001870 }
Javier Martin186b2502012-07-26 05:53:35 -03001871
Philipp Zabel9acf7692013-05-23 10:42:56 -03001872 /* Clear registers */
1873 for (i = 0; i < 64; i++)
1874 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1875
Javier Martin186b2502012-07-26 05:53:35 -03001876 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001877 if (dev->devtype->product == CODA_960 ||
1878 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001879 coda_write(dev, dev->tempbuf.paddr,
1880 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001881 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001882 } else {
1883 coda_write(dev, dev->workbuf.paddr,
1884 CODA_REG_BIT_WORK_BUF_ADDR);
1885 }
Javier Martin186b2502012-07-26 05:53:35 -03001886 coda_write(dev, dev->codebuf.paddr,
1887 CODA_REG_BIT_CODE_BUF_ADDR);
1888 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1889
1890 /* Set default values */
1891 switch (dev->devtype->product) {
1892 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001893 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1894 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001895 break;
1896 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001897 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1898 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001899 }
Philipp Zabel89548442014-07-11 06:36:17 -03001900 if (dev->devtype->product == CODA_960)
1901 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1902 else
1903 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001904
1905 if (dev->devtype->product != CODA_DX6)
1906 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1907
Javier Martin186b2502012-07-26 05:53:35 -03001908 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1909 CODA_REG_BIT_INT_ENABLE);
1910
1911 /* Reset VPU and start processor */
1912 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1913 data |= CODA_REG_RESET_ENABLE;
1914 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1915 udelay(10);
1916 data &= ~CODA_REG_RESET_ENABLE;
1917 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1918 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1919
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001920 clk_disable_unprepare(dev->clk_ahb);
1921 clk_disable_unprepare(dev->clk_per);
1922
1923 return 0;
1924
1925err_clk_ahb:
1926 clk_disable_unprepare(dev->clk_per);
1927err_clk_per:
1928 return ret;
1929}
1930
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001931static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001932{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001933 struct video_device *vfd = &dev->vfd[i];
1934
Dan Carpenter88ca3af2015-01-08 07:07:08 -03001935 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001936 return -EINVAL;
1937
Philipp Zabelbf249da2015-03-24 14:30:50 -03001938 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001939 vfd->fops = &coda_fops;
1940 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001941 vfd->release = video_device_release_empty,
1942 vfd->lock = &dev->dev_mutex;
1943 vfd->v4l2_dev = &dev->v4l2_dev;
1944 vfd->vfl_dir = VFL_DIR_M2M;
1945 video_set_drvdata(vfd, dev);
1946
Philipp Zabela188a662014-08-05 14:00:20 -03001947 /* Not applicable, use the selection API instead */
1948 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1949 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1950 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1951
Philipp Zabel121cacf2014-07-18 07:22:42 -03001952 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1953}
1954
Javier Martin186b2502012-07-26 05:53:35 -03001955static void coda_fw_callback(const struct firmware *fw, void *context)
1956{
1957 struct coda_dev *dev = context;
1958 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001959 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001960
1961 if (!fw) {
1962 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001963 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001964 }
1965
1966 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001967 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1968 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001969 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001970 dev_err(&pdev->dev, "failed to allocate code buffer\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001971 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001972 }
1973
Philipp Zabel87048bb2012-07-02 07:03:43 -03001974 /* Copy the whole firmware image to the code buffer */
1975 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1976 release_firmware(fw);
1977
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001978 ret = coda_hw_init(dev);
1979 if (ret < 0) {
1980 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1981 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001982 }
1983
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001984 ret = coda_check_firmware(dev);
1985 if (ret < 0)
1986 goto put_pm;
1987
Javier Martin186b2502012-07-26 05:53:35 -03001988 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1989 if (IS_ERR(dev->alloc_ctx)) {
1990 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001991 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001992 }
1993
1994 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1995 if (IS_ERR(dev->m2m_dev)) {
1996 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1997 goto rel_ctx;
1998 }
1999
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002000 for (i = 0; i < dev->devtype->num_vdevs; i++) {
2001 ret = coda_register_device(dev, i);
2002 if (ret) {
2003 v4l2_err(&dev->v4l2_dev,
2004 "Failed to register %s video device: %d\n",
2005 dev->devtype->vdevs[i]->name, ret);
2006 goto rel_vfd;
2007 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03002008 }
2009
2010 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002011 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03002012
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002013 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002014 return;
2015
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002016rel_vfd:
2017 while (--i >= 0)
2018 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03002019 v4l2_m2m_release(dev->m2m_dev);
2020rel_ctx:
2021 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002022put_pm:
2023 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002024}
2025
2026static int coda_firmware_request(struct coda_dev *dev)
2027{
2028 char *fw = dev->devtype->firmware;
2029
2030 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2031 coda_product_name(dev->devtype->product));
2032
2033 return request_firmware_nowait(THIS_MODULE, true,
2034 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2035}
2036
2037enum coda_platform {
2038 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002039 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002040 CODA_IMX6Q,
2041 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002042};
2043
Emil Goodec06d8752012-08-14 17:44:42 -03002044static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002045 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002046 .firmware = "v4l-codadx6-imx27.bin",
2047 .product = CODA_DX6,
2048 .codecs = codadx6_codecs,
2049 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002050 .vdevs = codadx6_video_devices,
2051 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002052 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002053 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002054 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002055 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002056 .firmware = "v4l-coda7541-imx53.bin",
2057 .product = CODA_7541,
2058 .codecs = coda7_codecs,
2059 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002060 .vdevs = coda7_video_devices,
2061 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002062 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002063 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002064 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002065 },
Philipp Zabel89548442014-07-11 06:36:17 -03002066 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002067 .firmware = "v4l-coda960-imx6q.bin",
2068 .product = CODA_960,
2069 .codecs = coda9_codecs,
2070 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002071 .vdevs = coda9_video_devices,
2072 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002073 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002074 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002075 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002076 },
2077 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002078 .firmware = "v4l-coda960-imx6dl.bin",
2079 .product = CODA_960,
2080 .codecs = coda9_codecs,
2081 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002082 .vdevs = coda9_video_devices,
2083 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002084 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002085 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002086 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002087 },
Javier Martin186b2502012-07-26 05:53:35 -03002088};
2089
2090static struct platform_device_id coda_platform_ids[] = {
2091 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2092 { /* sentinel */ }
2093};
2094MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2095
2096#ifdef CONFIG_OF
2097static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002098 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002099 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002100 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2101 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002102 { /* sentinel */ }
2103};
2104MODULE_DEVICE_TABLE(of, coda_dt_ids);
2105#endif
2106
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002107static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002108{
2109 const struct of_device_id *of_id =
2110 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2111 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002112 struct coda_platform_data *pdata = pdev->dev.platform_data;
2113 struct device_node *np = pdev->dev.of_node;
2114 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002115 struct coda_dev *dev;
2116 struct resource *res;
2117 int ret, irq;
2118
Philipp Zabelf23797b2014-08-06 08:02:23 -03002119 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002120 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002121 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002122
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002123 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2124
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002125 if (of_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002126 dev->devtype = of_id->data;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002127 } else if (pdev_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002128 dev->devtype = &coda_devdata[pdev_id->driver_data];
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002129 } else {
2130 ret = -EINVAL;
2131 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002132 }
2133
2134 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002135 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002136
2137 dev->plat_dev = pdev;
2138 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2139 if (IS_ERR(dev->clk_per)) {
2140 dev_err(&pdev->dev, "Could not get per clock\n");
2141 return PTR_ERR(dev->clk_per);
2142 }
2143
2144 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2145 if (IS_ERR(dev->clk_ahb)) {
2146 dev_err(&pdev->dev, "Could not get ahb clock\n");
2147 return PTR_ERR(dev->clk_ahb);
2148 }
2149
2150 /* Get memory for physical registers */
2151 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002152 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2153 if (IS_ERR(dev->regs_base))
2154 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002155
2156 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002157 irq = platform_get_irq_byname(pdev, "bit");
2158 if (irq < 0)
2159 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002160 if (irq < 0) {
2161 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002162 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002163 }
2164
Fabio Estevam08c8d142014-06-04 15:46:24 -03002165 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2166 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2167 if (ret < 0) {
2168 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2169 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002170 }
2171
Philipp Zabelee27aa92014-07-24 16:50:03 -03002172 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002173 if (IS_ERR(dev->rstc)) {
2174 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03002175 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002176 dev->rstc = NULL;
2177 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002178 dev_err(&pdev->dev, "failed get reset control: %d\n",
2179 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002180 return ret;
2181 }
2182 }
2183
Philipp Zabel657eee72013-04-29 16:17:14 -07002184 /* Get IRAM pool from device tree or platform data */
2185 pool = of_get_named_gen_pool(np, "iram", 0);
2186 if (!pool && pdata)
2187 pool = dev_get_gen_pool(pdata->iram_dev);
2188 if (!pool) {
2189 dev_err(&pdev->dev, "iram pool not available\n");
2190 return -ENOMEM;
2191 }
2192 dev->iram_pool = pool;
2193
Javier Martin186b2502012-07-26 05:53:35 -03002194 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2195 if (ret)
2196 return ret;
2197
2198 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002199 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002200
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002201 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2202 if (!dev->debugfs_root)
2203 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2204
Javier Martin186b2502012-07-26 05:53:35 -03002205 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002206 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002207 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002208 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002209 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002210 if (ret < 0) {
2211 dev_err(&pdev->dev, "failed to allocate work buffer\n");
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002212 goto err_v4l2_register;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002213 }
Javier Martin186b2502012-07-26 05:53:35 -03002214 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002215
2216 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002217 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002218 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002219 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002220 if (ret < 0) {
2221 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002222 goto err_v4l2_register;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002223 }
Javier Martin186b2502012-07-26 05:53:35 -03002224 }
2225
Philipp Zabel401e9722014-07-11 06:36:43 -03002226 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002227 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2228 &dev->iram.paddr);
2229 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002230 dev_warn(&pdev->dev, "unable to alloc iram\n");
2231 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002232 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002233 dev->iram.blob.data = dev->iram.vaddr;
2234 dev->iram.blob.size = dev->iram.size;
2235 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2236 dev->debugfs_root,
2237 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002238 }
2239
Philipp Zabel32b6f202014-07-11 06:36:20 -03002240 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2241 if (!dev->workqueue) {
2242 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002243 ret = -ENOMEM;
2244 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002245 }
2246
Javier Martin186b2502012-07-26 05:53:35 -03002247 platform_set_drvdata(pdev, dev);
2248
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002249 /*
2250 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002251 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002252 * enabled or whether the device is associated with a PM domain.
2253 */
2254 pm_runtime_get_noresume(&pdev->dev);
2255 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002256 pm_runtime_enable(&pdev->dev);
2257
Javier Martin186b2502012-07-26 05:53:35 -03002258 return coda_firmware_request(dev);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002259
2260err_v4l2_register:
2261 v4l2_device_unregister(&dev->v4l2_dev);
2262 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002263}
2264
2265static int coda_remove(struct platform_device *pdev)
2266{
2267 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002268 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002269
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002270 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2271 if (video_get_drvdata(&dev->vfd[i]))
2272 video_unregister_device(&dev->vfd[i]);
2273 }
Javier Martin186b2502012-07-26 05:53:35 -03002274 if (dev->m2m_dev)
2275 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002276 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002277 if (dev->alloc_ctx)
2278 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2279 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002280 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002281 if (dev->iram.vaddr)
2282 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2283 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002284 coda_free_aux_buf(dev, &dev->codebuf);
2285 coda_free_aux_buf(dev, &dev->tempbuf);
2286 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002287 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002288 return 0;
2289}
2290
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002291#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002292static int coda_runtime_resume(struct device *dev)
2293{
2294 struct coda_dev *cdev = dev_get_drvdata(dev);
2295 int ret = 0;
2296
Philipp Zabel65919e62014-07-18 07:22:36 -03002297 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002298 ret = coda_hw_init(cdev);
2299 if (ret)
2300 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2301 }
2302
2303 return ret;
2304}
2305#endif
2306
2307static const struct dev_pm_ops coda_pm_ops = {
2308 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2309};
2310
Javier Martin186b2502012-07-26 05:53:35 -03002311static struct platform_driver coda_driver = {
2312 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002313 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002314 .driver = {
2315 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002316 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002317 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002318 },
2319 .id_table = coda_platform_ids,
2320};
2321
2322module_platform_driver(coda_driver);
2323
2324MODULE_LICENSE("GPL");
2325MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2326MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");