blob: 9abb3214de52411a7119e8ac350403233bea422e [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 Zabel2e9e4f12013-09-30 10:34:50 -0300699static int coda_qbuf(struct file *file, void *priv,
700 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300701{
702 struct coda_ctx *ctx = fh_to_ctx(priv);
703
Philipp Zabel14604e32014-07-11 06:36:22 -0300704 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300705}
706
Philipp Zabel918c66f2013-06-27 06:59:01 -0300707static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
708 struct v4l2_buffer *buf)
709{
710 struct vb2_queue *src_vq;
711
Philipp Zabel14604e32014-07-11 06:36:22 -0300712 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300713
714 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
715 (buf->sequence == (ctx->qsequence - 1)));
716}
717
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300718static int coda_dqbuf(struct file *file, void *priv,
719 struct v4l2_buffer *buf)
Javier Martin186b2502012-07-26 05:53:35 -0300720{
721 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300722 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300723
Philipp Zabel14604e32014-07-11 06:36:22 -0300724 ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300725
726 /* If this is the last capture buffer, emit an end-of-stream event */
727 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
728 coda_buf_is_end_of_stream(ctx, buf)) {
729 const struct v4l2_event eos_event = {
730 .type = V4L2_EVENT_EOS
731 };
732
733 v4l2_event_queue_fh(&ctx->fh, &eos_event);
734 }
735
736 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300737}
738
Philipp Zabel52c41672014-07-11 06:36:19 -0300739static int coda_g_selection(struct file *file, void *fh,
740 struct v4l2_selection *s)
741{
742 struct coda_ctx *ctx = fh_to_ctx(fh);
743 struct coda_q_data *q_data;
744 struct v4l2_rect r, *rsel;
745
746 q_data = get_q_data(ctx, s->type);
747 if (!q_data)
748 return -EINVAL;
749
750 r.left = 0;
751 r.top = 0;
752 r.width = q_data->width;
753 r.height = q_data->height;
754 rsel = &q_data->rect;
755
756 switch (s->target) {
757 case V4L2_SEL_TGT_CROP_DEFAULT:
758 case V4L2_SEL_TGT_CROP_BOUNDS:
759 rsel = &r;
760 /* fallthrough */
761 case V4L2_SEL_TGT_CROP:
762 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
763 return -EINVAL;
764 break;
765 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
766 case V4L2_SEL_TGT_COMPOSE_PADDED:
767 rsel = &r;
768 /* fallthrough */
769 case V4L2_SEL_TGT_COMPOSE:
770 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
771 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
772 return -EINVAL;
773 break;
774 default:
775 return -EINVAL;
776 }
777
778 s->r = *rsel;
779
780 return 0;
781}
782
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300783static int coda_try_decoder_cmd(struct file *file, void *fh,
784 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300785{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300786 if (dc->cmd != V4L2_DEC_CMD_STOP)
787 return -EINVAL;
788
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300789 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300790 return -EINVAL;
791
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300792 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300793 return -EINVAL;
794
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300795 return 0;
796}
797
798static int coda_decoder_cmd(struct file *file, void *fh,
799 struct v4l2_decoder_cmd *dc)
800{
801 struct coda_ctx *ctx = fh_to_ctx(fh);
802 int ret;
803
804 ret = coda_try_decoder_cmd(file, fh, dc);
805 if (ret < 0)
806 return ret;
807
808 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300809 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300810 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300811
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300812 /* Set the stream-end flag on this context */
813 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300814 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300815 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300816
Philipp Zabel918c66f2013-06-27 06:59:01 -0300817 return 0;
818}
819
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300820static int coda_subscribe_event(struct v4l2_fh *fh,
821 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300822{
823 switch (sub->type) {
824 case V4L2_EVENT_EOS:
825 return v4l2_event_subscribe(fh, sub, 0, NULL);
826 default:
827 return v4l2_ctrl_subscribe_event(fh, sub);
828 }
Javier Martin186b2502012-07-26 05:53:35 -0300829}
830
831static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300832 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300833
Philipp Zabel22e244b2014-07-18 07:22:43 -0300834 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300835 .vidioc_g_fmt_vid_cap = coda_g_fmt,
836 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
837 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300838
Philipp Zabel22e244b2014-07-18 07:22:43 -0300839 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300840 .vidioc_g_fmt_vid_out = coda_g_fmt,
841 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
842 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300843
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300844 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
845 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300846
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300847 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300848 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300849 .vidioc_dqbuf = coda_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300850 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300851
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300852 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
853 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300854
Philipp Zabel52c41672014-07-11 06:36:19 -0300855 .vidioc_g_selection = coda_g_selection,
856
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300857 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300858 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300859
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300860 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300861 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300862};
863
Philipp Zabel79924ca2014-07-23 12:28:45 -0300864void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300865{
866 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
867 struct coda_dev *dev = ctx->dev;
868 int i;
869
870 for (i = 0; i < 16; i++)
871 coda_write(dev, tiled_map->xy2ca_map[i],
872 CODA9_GDI_XY2_CAS_0 + 4 * i);
873 for (i = 0; i < 4; i++)
874 coda_write(dev, tiled_map->xy2ba_map[i],
875 CODA9_GDI_XY2_BA_0 + 4 * i);
876 for (i = 0; i < 16; i++)
877 coda_write(dev, tiled_map->xy2ra_map[i],
878 CODA9_GDI_XY2_RAS_0 + 4 * i);
879 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
880 for (i = 0; i < 32; i++)
881 coda_write(dev, tiled_map->rbc2axi_map[i],
882 CODA9_GDI_RBC2_AXI_0 + 4 * i);
883}
884
Javier Martin186b2502012-07-26 05:53:35 -0300885/*
886 * Mem-to-mem operations.
887 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300888
889static void coda_device_run(void *m2m_priv)
890{
891 struct coda_ctx *ctx = m2m_priv;
892 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300893
894 queue_work(dev->workqueue, &ctx->pic_run_work);
895}
896
Philipp Zabel32b6f202014-07-11 06:36:20 -0300897static void coda_pic_run_work(struct work_struct *work)
898{
899 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
900 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300901 int ret;
902
903 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300904 mutex_lock(&dev->coda_mutex);
905
Philipp Zabela1192a12014-07-23 12:28:40 -0300906 ret = ctx->ops->prepare_run(ctx);
907 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
908 mutex_unlock(&dev->coda_mutex);
909 mutex_unlock(&ctx->buffer_mutex);
910 /* job_finish scheduled by prepare_decode */
911 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300912 }
913
Philipp Zabelf23797b2014-08-06 08:02:23 -0300914 if (!wait_for_completion_timeout(&ctx->completion,
915 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300916 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300917
918 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300919
920 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300921 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300922 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300923 }
924
Philipp Zabel747d7642015-01-23 13:51:31 -0300925 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
926 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -0300927 queue_work(dev->workqueue, &ctx->seq_end_work);
928
929 mutex_unlock(&dev->coda_mutex);
930 mutex_unlock(&ctx->buffer_mutex);
931
Philipp Zabel14604e32014-07-11 06:36:22 -0300932 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300933}
934
935static int coda_job_ready(void *m2m_priv)
936{
937 struct coda_ctx *ctx = m2m_priv;
938
939 /*
940 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300941 * and 1 frame are needed. In the decoder case,
942 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300943 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300944 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300945 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300946 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
947 "not ready: not enough video buffers.\n");
948 return 0;
949 }
950
Philipp Zabel14604e32014-07-11 06:36:22 -0300951 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300952 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
953 "not ready: not enough video capture buffers.\n");
954 return 0;
955 }
956
Philipp Zabela22496c2015-01-23 13:51:33 -0300957 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -0300958 struct list_head *meta;
959 bool stream_end;
960 int num_metas;
961 int src_bufs;
962
963 if (ctx->hold && !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
964 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
965 "%d: not ready: on hold for more buffers.\n",
966 ctx->idx);
967 return 0;
968 }
969
970 stream_end = ctx->bit_stream_param &
971 CODA_BIT_STREAM_END_FLAG;
972
973 num_metas = 0;
974 list_for_each(meta, &ctx->buffer_meta_list)
975 num_metas++;
976
977 src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
978
979 if (!stream_end && (num_metas + src_bufs) < 2) {
980 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
981 "%d: not ready: need 2 buffers available (%d, %d)\n",
982 ctx->idx, num_metas, src_bufs);
983 return 0;
984 }
985
986
987 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
988 !stream_end && (coda_get_bitstream_payload(ctx) < 512)) {
989 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
990 "%d: not ready: not enough bitstream data (%d).\n",
991 ctx->idx, coda_get_bitstream_payload(ctx));
992 return 0;
993 }
Philipp Zabel918c66f2013-06-27 06:59:01 -0300994 }
995
Philipp Zabel3e748262013-05-23 10:43:01 -0300996 if (ctx->aborting) {
997 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
998 "not ready: aborting\n");
999 return 0;
1000 }
1001
Javier Martin186b2502012-07-26 05:53:35 -03001002 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1003 "job ready\n");
1004 return 1;
1005}
1006
1007static void coda_job_abort(void *priv)
1008{
1009 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001010
1011 ctx->aborting = 1;
1012
1013 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1014 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001015}
1016
1017static void coda_lock(void *m2m_priv)
1018{
1019 struct coda_ctx *ctx = m2m_priv;
1020 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001021
Javier Martin186b2502012-07-26 05:53:35 -03001022 mutex_lock(&pcdev->dev_mutex);
1023}
1024
1025static void coda_unlock(void *m2m_priv)
1026{
1027 struct coda_ctx *ctx = m2m_priv;
1028 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001029
Javier Martin186b2502012-07-26 05:53:35 -03001030 mutex_unlock(&pcdev->dev_mutex);
1031}
1032
Philipp Zabel814c3762014-07-18 07:22:45 -03001033static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001034 .device_run = coda_device_run,
1035 .job_ready = coda_job_ready,
1036 .job_abort = coda_job_abort,
1037 .lock = coda_lock,
1038 .unlock = coda_unlock,
1039};
1040
Philipp Zabel89548442014-07-11 06:36:17 -03001041static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1042{
1043 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1044 int luma_map, chro_map, i;
1045
1046 memset(tiled_map, 0, sizeof(*tiled_map));
1047
1048 luma_map = 64;
1049 chro_map = 64;
1050 tiled_map->map_type = tiled_map_type;
1051 for (i = 0; i < 16; i++)
1052 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1053 for (i = 0; i < 4; i++)
1054 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1055 for (i = 0; i < 16; i++)
1056 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1057
1058 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1059 tiled_map->xy2rbc_config = 0;
1060 } else {
1061 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1062 tiled_map_type);
1063 return;
1064 }
1065}
1066
Javier Martin186b2502012-07-26 05:53:35 -03001067static void set_default_params(struct coda_ctx *ctx)
1068{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001069 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001070
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001071 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1072 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001073 max_w = min(ctx->codec->max_w, 1920U);
1074 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001075 usize = max_w * max_h * 3 / 2;
1076 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001077
Philipp Zabel121cacf2014-07-18 07:22:42 -03001078 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001079 ctx->colorspace = V4L2_COLORSPACE_REC709;
1080 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001081
1082 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001083 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1084 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1085 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1086 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001087 ctx->q_data[V4L2_M2M_DST].width = max_w;
1088 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001089 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1090 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001091 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001092 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001093 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001094 } else {
1095 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001096 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001097 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001098 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001099 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001100 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1101 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1102 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1103 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001104
1105 if (ctx->dev->devtype->product == CODA_960)
1106 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001107}
1108
1109/*
1110 * Queue operations
1111 */
1112static int coda_queue_setup(struct vb2_queue *vq,
1113 const struct v4l2_format *fmt,
1114 unsigned int *nbuffers, unsigned int *nplanes,
1115 unsigned int sizes[], void *alloc_ctxs[])
1116{
1117 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001118 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001119 unsigned int size;
1120
Philipp Zabele34db062012-08-29 08:22:00 -03001121 q_data = get_q_data(ctx, vq->type);
1122 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001123
1124 *nplanes = 1;
1125 sizes[0] = size;
1126
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001127 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
Javier Martin186b2502012-07-26 05:53:35 -03001128 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1129
1130 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1131 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1132
1133 return 0;
1134}
1135
1136static int coda_buf_prepare(struct vb2_buffer *vb)
1137{
1138 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1139 struct coda_q_data *q_data;
1140
1141 q_data = get_q_data(ctx, vb->vb2_queue->type);
1142
1143 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1144 v4l2_warn(&ctx->dev->v4l2_dev,
1145 "%s data will not fit into plane (%lu < %lu)\n",
1146 __func__, vb2_plane_size(vb, 0),
1147 (long)q_data->sizeimage);
1148 return -EINVAL;
1149 }
1150
Javier Martin186b2502012-07-26 05:53:35 -03001151 return 0;
1152}
1153
1154static void coda_buf_queue(struct vb2_buffer *vb)
1155{
1156 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001157 struct coda_q_data *q_data;
1158
1159 q_data = get_q_data(ctx, vb->vb2_queue->type);
1160
1161 /*
1162 * In the decoder case, immediately try to copy the buffer into the
1163 * bitstream ringbuffer and mark it as ready to be dequeued.
1164 */
Philipp Zabela22496c2015-01-23 13:51:33 -03001165 if (ctx->use_bit && ctx->inst_type == CODA_INST_DECODER &&
Philipp Zabel918c66f2013-06-27 06:59:01 -03001166 vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1167 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001168 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001169 * the stream end
1170 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001171 if (vb2_get_plane_payload(vb, 0) == 0)
1172 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001173 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001174 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001175 if (vb2_is_streaming(vb->vb2_queue))
1176 coda_fill_bitstream(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001177 mutex_unlock(&ctx->bitstream_mutex);
1178 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001179 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001180 }
Javier Martin186b2502012-07-26 05:53:35 -03001181}
1182
Philipp Zabel79924ca2014-07-23 12:28:45 -03001183int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1184 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001185{
1186 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1187 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001188 if (!buf->vaddr) {
1189 v4l2_err(&dev->v4l2_dev,
1190 "Failed to allocate %s buffer of size %u\n",
1191 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001192 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001193 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001194
1195 buf->size = size;
1196
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001197 if (name && parent) {
1198 buf->blob.data = buf->vaddr;
1199 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001200 buf->dentry = debugfs_create_blob(name, 0644, parent,
1201 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001202 if (!buf->dentry)
1203 dev_warn(&dev->plat_dev->dev,
1204 "failed to create debugfs entry %s\n", name);
1205 }
1206
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001207 return 0;
1208}
1209
Philipp Zabel79924ca2014-07-23 12:28:45 -03001210void coda_free_aux_buf(struct coda_dev *dev,
1211 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001212{
1213 if (buf->vaddr) {
1214 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1215 buf->vaddr, buf->paddr);
1216 buf->vaddr = NULL;
1217 buf->size = 0;
1218 }
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001219 debugfs_remove(buf->dentry);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001220}
1221
Javier Martin186b2502012-07-26 05:53:35 -03001222static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1223{
1224 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1225 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001226 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001227 struct vb2_buffer *buf;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001228 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001229
Philipp Zabelb96904e2013-05-23 10:42:58 -03001230 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001231 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001232 if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
1233 (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
1234 ctx->dev->devtype->product == CODA_7541)) {
Michael Olbricheabed932014-07-18 07:22:40 -03001235 /* copy the buffers that where queued before streamon */
1236 mutex_lock(&ctx->bitstream_mutex);
1237 coda_fill_bitstream(ctx);
1238 mutex_unlock(&ctx->bitstream_mutex);
1239
Philipp Zabelb9063522014-08-05 14:00:10 -03001240 if (coda_get_bitstream_payload(ctx) < 512) {
1241 ret = -EINVAL;
1242 goto err;
1243 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001244 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001245 if (count < 1) {
1246 ret = -EINVAL;
1247 goto err;
1248 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001249 }
1250
1251 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001252 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001253 if (count < 1) {
1254 ret = -EINVAL;
1255 goto err;
1256 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001257
1258 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001259 }
Javier Martin186b2502012-07-26 05:53:35 -03001260
Philipp Zabelb96904e2013-05-23 10:42:58 -03001261 /* Don't start the coda unless both queues are on */
1262 if (!(ctx->streamon_out & ctx->streamon_cap))
1263 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001264
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001265 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001266 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001267 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001268
Philipp Zabelb96904e2013-05-23 10:42:58 -03001269 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001270 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Javier Martin186b2502012-07-26 05:53:35 -03001271
Philipp Zabelb96904e2013-05-23 10:42:58 -03001272 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1273 q_data_dst->fourcc);
1274 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001275 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001276 ret = -EINVAL;
1277 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001278 }
1279
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001280 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1281 ctx->params.gop_size = 1;
1282 ctx->gopcounter = ctx->params.gop_size - 1;
1283
Philipp Zabela1192a12014-07-23 12:28:40 -03001284 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001285 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001286 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001287 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001288 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001289 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001290 }
1291
Philipp Zabel89548442014-07-11 06:36:17 -03001292 ctx->initialized = 1;
1293 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001294
1295err:
1296 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1297 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001298 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001299 } else {
1300 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001301 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001302 }
1303 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001304}
1305
Hans Verkuile37559b2014-04-17 02:47:21 -03001306static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001307{
1308 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001309 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001310 struct vb2_buffer *buf;
Javier Martin186b2502012-07-26 05:53:35 -03001311
1312 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001313 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001314 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001315 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001316
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001317 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001318
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001319 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001320
1321 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1322 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001323 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001324 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001325 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001326 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001327
1328 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001329 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001330
1331 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1332 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001333 }
1334
Philipp Zabel918c66f2013-06-27 06:59:01 -03001335 if (!ctx->streamon_out && !ctx->streamon_cap) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001336 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001337
Philipp Zabelf4706d62015-01-23 13:51:27 -03001338 if (ctx->ops->seq_end_work) {
1339 queue_work(dev->workqueue, &ctx->seq_end_work);
1340 flush_work(&ctx->seq_end_work);
1341 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001342 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001343 while (!list_empty(&ctx->buffer_meta_list)) {
1344 meta = list_first_entry(&ctx->buffer_meta_list,
1345 struct coda_buffer_meta, list);
1346 list_del(&meta->list);
1347 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001348 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001349 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001350 kfifo_init(&ctx->bitstream_fifo,
1351 ctx->bitstream.vaddr, ctx->bitstream.size);
Philipp Zabelf4706d62015-01-23 13:51:27 -03001352 ctx->initialized = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001353 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001354 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001355 }
Javier Martin186b2502012-07-26 05:53:35 -03001356}
1357
Philipp Zabel121cacf2014-07-18 07:22:42 -03001358static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001359 .queue_setup = coda_queue_setup,
1360 .buf_prepare = coda_buf_prepare,
1361 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001362 .start_streaming = coda_start_streaming,
1363 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001364 .wait_prepare = vb2_ops_wait_prepare,
1365 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001366};
1367
1368static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1369{
1370 struct coda_ctx *ctx =
1371 container_of(ctrl->handler, struct coda_ctx, ctrls);
1372
1373 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1374 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1375
1376 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001377 case V4L2_CID_HFLIP:
1378 if (ctrl->val)
1379 ctx->params.rot_mode |= CODA_MIR_HOR;
1380 else
1381 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1382 break;
1383 case V4L2_CID_VFLIP:
1384 if (ctrl->val)
1385 ctx->params.rot_mode |= CODA_MIR_VER;
1386 else
1387 ctx->params.rot_mode &= ~CODA_MIR_VER;
1388 break;
Javier Martin186b2502012-07-26 05:53:35 -03001389 case V4L2_CID_MPEG_VIDEO_BITRATE:
1390 ctx->params.bitrate = ctrl->val / 1000;
1391 break;
1392 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1393 ctx->params.gop_size = ctrl->val;
1394 break;
1395 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1396 ctx->params.h264_intra_qp = ctrl->val;
1397 break;
1398 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1399 ctx->params.h264_inter_qp = ctrl->val;
1400 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001401 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1402 ctx->params.h264_min_qp = ctrl->val;
1403 break;
1404 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1405 ctx->params.h264_max_qp = ctrl->val;
1406 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001407 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1408 ctx->params.h264_deblk_alpha = ctrl->val;
1409 break;
1410 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1411 ctx->params.h264_deblk_beta = ctrl->val;
1412 break;
1413 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1414 ctx->params.h264_deblk_enabled = (ctrl->val ==
1415 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1416 break;
Javier Martin186b2502012-07-26 05:53:35 -03001417 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1418 ctx->params.mpeg4_intra_qp = ctrl->val;
1419 break;
1420 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1421 ctx->params.mpeg4_inter_qp = ctrl->val;
1422 break;
1423 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1424 ctx->params.slice_mode = ctrl->val;
1425 break;
1426 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1427 ctx->params.slice_max_mb = ctrl->val;
1428 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001429 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1430 ctx->params.slice_max_bits = ctrl->val * 8;
1431 break;
Javier Martin186b2502012-07-26 05:53:35 -03001432 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1433 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001434 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1435 ctx->params.intra_refresh = ctrl->val;
1436 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001437 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1438 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1439 break;
1440 case V4L2_CID_JPEG_RESTART_INTERVAL:
1441 ctx->params.jpeg_restart_interval = ctrl->val;
1442 break;
Javier Martin186b2502012-07-26 05:53:35 -03001443 default:
1444 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1445 "Invalid control, id=%d, val=%d\n",
1446 ctrl->id, ctrl->val);
1447 return -EINVAL;
1448 }
1449
1450 return 0;
1451}
1452
Philipp Zabel814c3762014-07-18 07:22:45 -03001453static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001454 .s_ctrl = coda_s_ctrl,
1455};
1456
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001457static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001458{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001459 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001460 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1, 0);
1461 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1462 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1463 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001464 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001465 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001466 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001467 if (ctx->dev->devtype->product != CODA_960) {
1468 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1469 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1470 }
1471 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1472 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001473 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001474 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1475 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1476 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1477 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1478 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1479 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1480 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1481 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001482 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1483 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1484 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1485 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1486 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001487 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1488 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001489 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1490 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001491 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001492 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1493 500);
Javier Martin186b2502012-07-26 05:53:35 -03001494 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1495 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1496 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1497 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1498 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001499 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001500 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1501 1920 * 1088 / 256, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001502}
1503
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001504static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1505{
1506 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1507 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1508 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1509 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1510}
1511
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001512static int coda_ctrls_setup(struct coda_ctx *ctx)
1513{
1514 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1515
1516 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1517 V4L2_CID_HFLIP, 0, 1, 1, 0);
1518 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1519 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001520 if (ctx->inst_type == CODA_INST_ENCODER) {
1521 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1522 coda_jpeg_encode_ctrls(ctx);
1523 else
1524 coda_encode_ctrls(ctx);
1525 }
Javier Martin186b2502012-07-26 05:53:35 -03001526
1527 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001528 v4l2_err(&ctx->dev->v4l2_dev,
1529 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001530 ctx->ctrls.error);
1531 return -EINVAL;
1532 }
1533
1534 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1535}
1536
Philipp Zabel121cacf2014-07-18 07:22:42 -03001537static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001538{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001539 vq->drv_priv = ctx;
1540 vq->ops = &coda_qops;
1541 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1542 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1543 vq->lock = &ctx->dev->dev_mutex;
1544
1545 return vb2_queue_init(vq);
1546}
1547
Philipp Zabel79924ca2014-07-23 12:28:45 -03001548int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1549 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001550{
Javier Martin186b2502012-07-26 05:53:35 -03001551 int ret;
1552
Javier Martin186b2502012-07-26 05:53:35 -03001553 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001554 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001555 src_vq->mem_ops = &vb2_dma_contig_memops;
1556
Philipp Zabel121cacf2014-07-18 07:22:42 -03001557 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001558 if (ret)
1559 return ret;
1560
Javier Martin186b2502012-07-26 05:53:35 -03001561 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001562 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001563 dst_vq->mem_ops = &vb2_dma_contig_memops;
1564
Philipp Zabel121cacf2014-07-18 07:22:42 -03001565 return coda_queue_init(priv, dst_vq);
1566}
1567
Philipp Zabel79924ca2014-07-23 12:28:45 -03001568int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1569 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001570{
1571 int ret;
1572
1573 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001574 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1575 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001576
1577 ret = coda_queue_init(priv, src_vq);
1578 if (ret)
1579 return ret;
1580
1581 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1582 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1583 dst_vq->mem_ops = &vb2_dma_contig_memops;
1584
1585 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001586}
1587
Philipp Zabele11f3e62012-07-25 09:16:58 -03001588static int coda_next_free_instance(struct coda_dev *dev)
1589{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001590 int idx = ffz(dev->instance_mask);
1591
1592 if ((idx < 0) ||
1593 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1594 return -EBUSY;
1595
1596 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001597}
1598
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001599/*
1600 * File operations
1601 */
1602
1603static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001604{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001605 struct video_device *vdev = video_devdata(file);
1606 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001607 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001608 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001609 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001610 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001611
Philipp Zabelf23797b2014-08-06 08:02:23 -03001612 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001613 if (!ctx)
1614 return -ENOMEM;
1615
Fabio Estevamf82bc202013-08-21 11:14:16 -03001616 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001617 if (idx < 0) {
1618 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001619 goto err_coda_max;
1620 }
1621 set_bit(idx, &dev->instance_mask);
1622
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001623 name = kasprintf(GFP_KERNEL, "context%d", idx);
1624 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1625 kfree(name);
1626
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001627 ctx->cvd = to_coda_video_device(vdev);
1628 ctx->inst_type = ctx->cvd->type;
1629 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001630 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001631 init_completion(&ctx->completion);
1632 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001633 if (ctx->ops->seq_end_work)
1634 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001635 v4l2_fh_init(&ctx->fh, video_devdata(file));
1636 file->private_data = &ctx->fh;
1637 v4l2_fh_add(&ctx->fh);
1638 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001639 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001640 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001641 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001642 ctx->frame_mem_ctrl = 1 << 12;
1643 /* fallthrough */
1644 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001645 ctx->reg_idx = 0;
1646 break;
1647 default:
1648 ctx->reg_idx = idx;
1649 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001650
Philipp Zabel1e172732014-07-11 06:36:23 -03001651 /* Power up and upload firmware if necessary */
1652 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1653 if (ret < 0) {
1654 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1655 goto err_pm_get;
1656 }
1657
Fabio Estevam79830db2013-08-21 11:14:17 -03001658 ret = clk_prepare_enable(dev->clk_per);
1659 if (ret)
1660 goto err_clk_per;
1661
1662 ret = clk_prepare_enable(dev->clk_ahb);
1663 if (ret)
1664 goto err_clk_ahb;
1665
Javier Martin186b2502012-07-26 05:53:35 -03001666 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001667 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1668 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001669 if (IS_ERR(ctx->fh.m2m_ctx)) {
1670 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001671
1672 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1673 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001674 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001675 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001676
Javier Martin186b2502012-07-26 05:53:35 -03001677 ret = coda_ctrls_setup(ctx);
1678 if (ret) {
1679 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001680 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001681 }
1682
1683 ctx->fh.ctrl_handler = &ctx->ctrls;
1684
Philipp Zabela22496c2015-01-23 13:51:33 -03001685 if (ctx->use_bit) {
1686 ret = coda_alloc_context_buf(ctx, &ctx->parabuf,
1687 CODA_PARA_BUF_SIZE, "parabuf");
1688 if (ret < 0) {
1689 v4l2_err(&dev->v4l2_dev, "failed to allocate parabuf");
1690 goto err_dma_alloc;
1691 }
Javier Martin186b2502012-07-26 05:53:35 -03001692
Philipp Zabela22496c2015-01-23 13:51:33 -03001693 ctx->bitstream.size = CODA_MAX_FRAME_SIZE;
1694 ctx->bitstream.vaddr = dma_alloc_writecombine(
1695 &dev->plat_dev->dev, ctx->bitstream.size,
1696 &ctx->bitstream.paddr, GFP_KERNEL);
1697 if (!ctx->bitstream.vaddr) {
1698 v4l2_err(&dev->v4l2_dev,
1699 "failed to allocate bitstream ringbuffer");
1700 ret = -ENOMEM;
1701 goto err_dma_writecombine;
1702 }
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001703 }
1704 kfifo_init(&ctx->bitstream_fifo,
1705 ctx->bitstream.vaddr, ctx->bitstream.size);
1706 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001707 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001708 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001709
Javier Martin186b2502012-07-26 05:53:35 -03001710 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001711 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001712 coda_unlock(ctx);
1713
Javier Martin186b2502012-07-26 05:53:35 -03001714 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1715 ctx->idx, ctx);
1716
1717 return 0;
1718
Fabio Estevamf82bc202013-08-21 11:14:16 -03001719err_dma_writecombine:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001720 if (ctx->dev->devtype->product == CODA_DX6)
1721 coda_free_aux_buf(dev, &ctx->workbuf);
1722 coda_free_aux_buf(dev, &ctx->parabuf);
1723err_dma_alloc:
1724 v4l2_ctrl_handler_free(&ctx->ctrls);
1725err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001726 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001727err_ctx_init:
1728 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001729err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001730 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001731err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001732 pm_runtime_put_sync(&dev->plat_dev->dev);
1733err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001734 v4l2_fh_del(&ctx->fh);
1735 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001736 clear_bit(ctx->idx, &dev->instance_mask);
1737err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001738 kfree(ctx);
1739 return ret;
1740}
1741
1742static int coda_release(struct file *file)
1743{
1744 struct coda_dev *dev = video_drvdata(file);
1745 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1746
1747 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1748 ctx);
1749
Philipp Zabela22496c2015-01-23 13:51:33 -03001750 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03001751 coda_bit_stream_end_flag(ctx);
1752
Philipp Zabel918c66f2013-06-27 06:59:01 -03001753 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001754 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001755
1756 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel747d7642015-01-23 13:51:31 -03001757 if (ctx->initialized && ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001758 queue_work(dev->workqueue, &ctx->seq_end_work);
1759 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001760 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001761
Javier Martin186b2502012-07-26 05:53:35 -03001762 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001763 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001764 coda_unlock(ctx);
1765
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001766 if (ctx->bitstream.vaddr) {
1767 dma_free_writecombine(&dev->plat_dev->dev, ctx->bitstream.size,
1768 ctx->bitstream.vaddr, ctx->bitstream.paddr);
1769 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001770 if (ctx->dev->devtype->product == CODA_DX6)
1771 coda_free_aux_buf(dev, &ctx->workbuf);
1772
1773 coda_free_aux_buf(dev, &ctx->parabuf);
Javier Martin186b2502012-07-26 05:53:35 -03001774 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001775 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001776 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001777 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001778 v4l2_fh_del(&ctx->fh);
1779 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001780 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001781 if (ctx->ops->release)
1782 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03001783 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03001784 kfree(ctx);
1785
1786 return 0;
1787}
1788
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001789static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001790 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001791 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03001792 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001793 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001794 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001795 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001796};
1797
Philipp Zabel87048bb2012-07-02 07:03:43 -03001798static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001799{
Javier Martin186b2502012-07-26 05:53:35 -03001800 u32 data;
1801 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001802 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001803
Fabio Estevam79830db2013-08-21 11:14:17 -03001804 ret = clk_prepare_enable(dev->clk_per);
1805 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001806 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001807
1808 ret = clk_prepare_enable(dev->clk_ahb);
1809 if (ret)
1810 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001811
Philipp Zabel8f452842014-07-11 06:36:35 -03001812 if (dev->rstc)
1813 reset_control_reset(dev->rstc);
1814
Javier Martin186b2502012-07-26 05:53:35 -03001815 /*
1816 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001817 * The 16-bit chars in the code buffer are in memory access
1818 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001819 * Data in this SRAM survives a reboot.
1820 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001821 p = (u16 *)dev->codebuf.vaddr;
1822 if (dev->devtype->product == CODA_DX6) {
1823 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1824 data = CODA_DOWN_ADDRESS_SET(i) |
1825 CODA_DOWN_DATA_SET(p[i ^ 1]);
1826 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1827 }
1828 } else {
1829 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1830 data = CODA_DOWN_ADDRESS_SET(i) |
1831 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1832 3 - (i % 4)]);
1833 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1834 }
Javier Martin186b2502012-07-26 05:53:35 -03001835 }
Javier Martin186b2502012-07-26 05:53:35 -03001836
Philipp Zabel9acf7692013-05-23 10:42:56 -03001837 /* Clear registers */
1838 for (i = 0; i < 64; i++)
1839 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1840
Javier Martin186b2502012-07-26 05:53:35 -03001841 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001842 if (dev->devtype->product == CODA_960 ||
1843 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001844 coda_write(dev, dev->tempbuf.paddr,
1845 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001846 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001847 } else {
1848 coda_write(dev, dev->workbuf.paddr,
1849 CODA_REG_BIT_WORK_BUF_ADDR);
1850 }
Javier Martin186b2502012-07-26 05:53:35 -03001851 coda_write(dev, dev->codebuf.paddr,
1852 CODA_REG_BIT_CODE_BUF_ADDR);
1853 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1854
1855 /* Set default values */
1856 switch (dev->devtype->product) {
1857 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001858 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1859 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001860 break;
1861 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001862 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1863 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001864 }
Philipp Zabel89548442014-07-11 06:36:17 -03001865 if (dev->devtype->product == CODA_960)
1866 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1867 else
1868 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001869
1870 if (dev->devtype->product != CODA_DX6)
1871 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1872
Javier Martin186b2502012-07-26 05:53:35 -03001873 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1874 CODA_REG_BIT_INT_ENABLE);
1875
1876 /* Reset VPU and start processor */
1877 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1878 data |= CODA_REG_RESET_ENABLE;
1879 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1880 udelay(10);
1881 data &= ~CODA_REG_RESET_ENABLE;
1882 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1883 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1884
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001885 clk_disable_unprepare(dev->clk_ahb);
1886 clk_disable_unprepare(dev->clk_per);
1887
1888 return 0;
1889
1890err_clk_ahb:
1891 clk_disable_unprepare(dev->clk_per);
1892err_clk_per:
1893 return ret;
1894}
1895
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001896static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001897{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001898 struct video_device *vfd = &dev->vfd[i];
1899
Dan Carpenter88ca3af2015-01-08 07:07:08 -03001900 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001901 return -EINVAL;
1902
Dan Carpenter88ca3af2015-01-08 07:07:08 -03001903 snprintf(vfd->name, sizeof(vfd->name), "%s",
1904 dev->devtype->vdevs[i]->name);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001905 vfd->fops = &coda_fops;
1906 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001907 vfd->release = video_device_release_empty,
1908 vfd->lock = &dev->dev_mutex;
1909 vfd->v4l2_dev = &dev->v4l2_dev;
1910 vfd->vfl_dir = VFL_DIR_M2M;
1911 video_set_drvdata(vfd, dev);
1912
Philipp Zabela188a662014-08-05 14:00:20 -03001913 /* Not applicable, use the selection API instead */
1914 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1915 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1916 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1917
Philipp Zabel121cacf2014-07-18 07:22:42 -03001918 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1919}
1920
Javier Martin186b2502012-07-26 05:53:35 -03001921static void coda_fw_callback(const struct firmware *fw, void *context)
1922{
1923 struct coda_dev *dev = context;
1924 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001925 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001926
1927 if (!fw) {
1928 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001929 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001930 }
1931
1932 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001933 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1934 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001935 if (ret < 0) {
Javier Martin186b2502012-07-26 05:53:35 -03001936 dev_err(&pdev->dev, "failed to allocate code buffer\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001937 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001938 }
1939
Philipp Zabel87048bb2012-07-02 07:03:43 -03001940 /* Copy the whole firmware image to the code buffer */
1941 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1942 release_firmware(fw);
1943
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001944 ret = coda_hw_init(dev);
1945 if (ret < 0) {
1946 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1947 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001948 }
1949
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001950 ret = coda_check_firmware(dev);
1951 if (ret < 0)
1952 goto put_pm;
1953
Javier Martin186b2502012-07-26 05:53:35 -03001954 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1955 if (IS_ERR(dev->alloc_ctx)) {
1956 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001957 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001958 }
1959
1960 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1961 if (IS_ERR(dev->m2m_dev)) {
1962 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1963 goto rel_ctx;
1964 }
1965
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001966 for (i = 0; i < dev->devtype->num_vdevs; i++) {
1967 ret = coda_register_device(dev, i);
1968 if (ret) {
1969 v4l2_err(&dev->v4l2_dev,
1970 "Failed to register %s video device: %d\n",
1971 dev->devtype->vdevs[i]->name, ret);
1972 goto rel_vfd;
1973 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03001974 }
1975
1976 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001977 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03001978
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001979 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001980 return;
1981
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001982rel_vfd:
1983 while (--i >= 0)
1984 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03001985 v4l2_m2m_release(dev->m2m_dev);
1986rel_ctx:
1987 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001988put_pm:
1989 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001990}
1991
1992static int coda_firmware_request(struct coda_dev *dev)
1993{
1994 char *fw = dev->devtype->firmware;
1995
1996 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
1997 coda_product_name(dev->devtype->product));
1998
1999 return request_firmware_nowait(THIS_MODULE, true,
2000 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2001}
2002
2003enum coda_platform {
2004 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002005 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002006 CODA_IMX6Q,
2007 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002008};
2009
Emil Goodec06d8752012-08-14 17:44:42 -03002010static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002011 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002012 .firmware = "v4l-codadx6-imx27.bin",
2013 .product = CODA_DX6,
2014 .codecs = codadx6_codecs,
2015 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002016 .vdevs = codadx6_video_devices,
2017 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002018 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002019 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002020 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002021 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002022 .firmware = "v4l-coda7541-imx53.bin",
2023 .product = CODA_7541,
2024 .codecs = coda7_codecs,
2025 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002026 .vdevs = coda7_video_devices,
2027 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002028 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002029 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002030 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002031 },
Philipp Zabel89548442014-07-11 06:36:17 -03002032 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002033 .firmware = "v4l-coda960-imx6q.bin",
2034 .product = CODA_960,
2035 .codecs = coda9_codecs,
2036 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002037 .vdevs = coda9_video_devices,
2038 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002039 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002040 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002041 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002042 },
2043 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002044 .firmware = "v4l-coda960-imx6dl.bin",
2045 .product = CODA_960,
2046 .codecs = coda9_codecs,
2047 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002048 .vdevs = coda9_video_devices,
2049 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002050 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002051 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002052 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002053 },
Javier Martin186b2502012-07-26 05:53:35 -03002054};
2055
2056static struct platform_device_id coda_platform_ids[] = {
2057 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2058 { /* sentinel */ }
2059};
2060MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2061
2062#ifdef CONFIG_OF
2063static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002064 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002065 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002066 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2067 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002068 { /* sentinel */ }
2069};
2070MODULE_DEVICE_TABLE(of, coda_dt_ids);
2071#endif
2072
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002073static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002074{
2075 const struct of_device_id *of_id =
2076 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2077 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002078 struct coda_platform_data *pdata = pdev->dev.platform_data;
2079 struct device_node *np = pdev->dev.of_node;
2080 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002081 struct coda_dev *dev;
2082 struct resource *res;
2083 int ret, irq;
2084
Philipp Zabelf23797b2014-08-06 08:02:23 -03002085 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002086 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002087 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002088
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002089 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2090
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002091 if (of_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002092 dev->devtype = of_id->data;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002093 } else if (pdev_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002094 dev->devtype = &coda_devdata[pdev_id->driver_data];
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002095 } else {
2096 ret = -EINVAL;
2097 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002098 }
2099
2100 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002101 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002102
2103 dev->plat_dev = pdev;
2104 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2105 if (IS_ERR(dev->clk_per)) {
2106 dev_err(&pdev->dev, "Could not get per clock\n");
2107 return PTR_ERR(dev->clk_per);
2108 }
2109
2110 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2111 if (IS_ERR(dev->clk_ahb)) {
2112 dev_err(&pdev->dev, "Could not get ahb clock\n");
2113 return PTR_ERR(dev->clk_ahb);
2114 }
2115
2116 /* Get memory for physical registers */
2117 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002118 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2119 if (IS_ERR(dev->regs_base))
2120 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002121
2122 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002123 irq = platform_get_irq_byname(pdev, "bit");
2124 if (irq < 0)
2125 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002126 if (irq < 0) {
2127 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002128 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002129 }
2130
Fabio Estevam08c8d142014-06-04 15:46:24 -03002131 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2132 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2133 if (ret < 0) {
2134 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2135 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002136 }
2137
Philipp Zabelee27aa92014-07-24 16:50:03 -03002138 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002139 if (IS_ERR(dev->rstc)) {
2140 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03002141 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002142 dev->rstc = NULL;
2143 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002144 dev_err(&pdev->dev, "failed get reset control: %d\n",
2145 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002146 return ret;
2147 }
2148 }
2149
Philipp Zabel657eee72013-04-29 16:17:14 -07002150 /* Get IRAM pool from device tree or platform data */
2151 pool = of_get_named_gen_pool(np, "iram", 0);
2152 if (!pool && pdata)
2153 pool = dev_get_gen_pool(pdata->iram_dev);
2154 if (!pool) {
2155 dev_err(&pdev->dev, "iram pool not available\n");
2156 return -ENOMEM;
2157 }
2158 dev->iram_pool = pool;
2159
Javier Martin186b2502012-07-26 05:53:35 -03002160 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2161 if (ret)
2162 return ret;
2163
2164 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002165 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002166
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002167 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2168 if (!dev->debugfs_root)
2169 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2170
Javier Martin186b2502012-07-26 05:53:35 -03002171 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002172 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002173 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002174 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002175 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002176 if (ret < 0) {
2177 dev_err(&pdev->dev, "failed to allocate work buffer\n");
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002178 goto err_v4l2_register;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002179 }
Javier Martin186b2502012-07-26 05:53:35 -03002180 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002181
2182 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002183 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002184 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002185 dev->debugfs_root);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002186 if (ret < 0) {
2187 dev_err(&pdev->dev, "failed to allocate temp buffer\n");
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002188 goto err_v4l2_register;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002189 }
Javier Martin186b2502012-07-26 05:53:35 -03002190 }
2191
Philipp Zabel401e9722014-07-11 06:36:43 -03002192 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002193 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2194 &dev->iram.paddr);
2195 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002196 dev_warn(&pdev->dev, "unable to alloc iram\n");
2197 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002198 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002199 dev->iram.blob.data = dev->iram.vaddr;
2200 dev->iram.blob.size = dev->iram.size;
2201 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2202 dev->debugfs_root,
2203 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002204 }
2205
Philipp Zabel32b6f202014-07-11 06:36:20 -03002206 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2207 if (!dev->workqueue) {
2208 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002209 ret = -ENOMEM;
2210 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002211 }
2212
Javier Martin186b2502012-07-26 05:53:35 -03002213 platform_set_drvdata(pdev, dev);
2214
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002215 /*
2216 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002217 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002218 * enabled or whether the device is associated with a PM domain.
2219 */
2220 pm_runtime_get_noresume(&pdev->dev);
2221 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002222 pm_runtime_enable(&pdev->dev);
2223
Javier Martin186b2502012-07-26 05:53:35 -03002224 return coda_firmware_request(dev);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002225
2226err_v4l2_register:
2227 v4l2_device_unregister(&dev->v4l2_dev);
2228 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002229}
2230
2231static int coda_remove(struct platform_device *pdev)
2232{
2233 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002234 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002235
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002236 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2237 if (video_get_drvdata(&dev->vfd[i]))
2238 video_unregister_device(&dev->vfd[i]);
2239 }
Javier Martin186b2502012-07-26 05:53:35 -03002240 if (dev->m2m_dev)
2241 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002242 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002243 if (dev->alloc_ctx)
2244 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2245 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002246 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002247 if (dev->iram.vaddr)
2248 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2249 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002250 coda_free_aux_buf(dev, &dev->codebuf);
2251 coda_free_aux_buf(dev, &dev->tempbuf);
2252 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002253 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002254 return 0;
2255}
2256
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002257#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002258static int coda_runtime_resume(struct device *dev)
2259{
2260 struct coda_dev *cdev = dev_get_drvdata(dev);
2261 int ret = 0;
2262
Philipp Zabel65919e62014-07-18 07:22:36 -03002263 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002264 ret = coda_hw_init(cdev);
2265 if (ret)
2266 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2267 }
2268
2269 return ret;
2270}
2271#endif
2272
2273static const struct dev_pm_ops coda_pm_ops = {
2274 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2275};
2276
Javier Martin186b2502012-07-26 05:53:35 -03002277static struct platform_driver coda_driver = {
2278 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002279 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002280 .driver = {
2281 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002282 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002283 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002284 },
2285 .id_table = coda_platform_ids,
2286};
2287
2288module_platform_driver(coda_driver);
2289
2290MODULE_LICENSE("GPL");
2291MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2292MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");