blob: 58f65486de332cd893d6175ad98ca405c999a221 [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_ISRAM_SIZE (2048 * 2)
50
Javier Martin186b2502012-07-26 05:53:35 -030051#define MIN_W 176
52#define MIN_H 144
Javier Martin186b2502012-07-26 05:53:35 -030053
54#define S_ALIGN 1 /* multiple of 2 */
55#define W_ALIGN 1 /* multiple of 2 */
56#define H_ALIGN 1 /* multiple of 2 */
57
58#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh)
59
Philipp Zabel79924ca2014-07-23 12:28:45 -030060int coda_debug;
Philipp Zabelc4eb1bfc2013-05-21 04:24:16 -030061module_param(coda_debug, int, 0644);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030062MODULE_PARM_DESC(coda_debug, "Debug level (0-2)");
Javier Martin186b2502012-07-26 05:53:35 -030063
Javier Martin186b2502012-07-26 05:53:35 -030064struct coda_fmt {
65 char *name;
66 u32 fourcc;
Philipp Zabelb96904e2013-05-23 10:42:58 -030067};
68
Philipp Zabela2b3e462014-07-23 12:28:39 -030069void coda_write(struct coda_dev *dev, u32 data, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030070{
Philipp Zabeld60b18b2014-08-05 14:00:15 -030071 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030072 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
73 writel(data, dev->regs_base + reg);
74}
75
Philipp Zabela2b3e462014-07-23 12:28:39 -030076unsigned int coda_read(struct coda_dev *dev, u32 reg)
Javier Martin186b2502012-07-26 05:53:35 -030077{
78 u32 data;
Philipp Zabelf23797b2014-08-06 08:02:23 -030079
Javier Martin186b2502012-07-26 05:53:35 -030080 data = readl(dev->regs_base + reg);
Philipp Zabeld60b18b2014-08-05 14:00:15 -030081 v4l2_dbg(2, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -030082 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg);
83 return data;
84}
85
Philipp Zabel856d7d92014-09-29 09:53:44 -030086void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
87 struct vb2_buffer *buf, unsigned int reg_y)
88{
89 u32 base_y = vb2_dma_contig_plane_dma_addr(buf, 0);
90 u32 base_cb, base_cr;
91
92 switch (q_data->fourcc) {
93 case V4L2_PIX_FMT_YVU420:
94 /* Switch Cb and Cr for YVU420 format */
95 base_cr = base_y + q_data->bytesperline * q_data->height;
96 base_cb = base_cr + q_data->bytesperline * q_data->height / 4;
97 break;
98 case V4L2_PIX_FMT_YUV420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -030099 case V4L2_PIX_FMT_NV12:
Philipp Zabel856d7d92014-09-29 09:53:44 -0300100 default:
101 base_cb = base_y + q_data->bytesperline * q_data->height;
102 base_cr = base_cb + q_data->bytesperline * q_data->height / 4;
103 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300104 case V4L2_PIX_FMT_YUV422P:
105 base_cb = base_y + q_data->bytesperline * q_data->height;
106 base_cr = base_cb + q_data->bytesperline * q_data->height / 2;
Philipp Zabel856d7d92014-09-29 09:53:44 -0300107 }
108
109 coda_write(ctx->dev, base_y, reg_y);
110 coda_write(ctx->dev, base_cb, reg_y + 4);
111 coda_write(ctx->dev, base_cr, reg_y + 8);
112}
113
Javier Martin186b2502012-07-26 05:53:35 -0300114/*
Philipp Zabelb96904e2013-05-23 10:42:58 -0300115 * Array of all formats supported by any version of Coda:
Javier Martin186b2502012-07-26 05:53:35 -0300116 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300117static const struct coda_fmt coda_formats[] = {
Javier Martin186b2502012-07-26 05:53:35 -0300118 {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300119 .name = "YUV 4:2:0 Planar, YCbCr",
Javier Martin186b2502012-07-26 05:53:35 -0300120 .fourcc = V4L2_PIX_FMT_YUV420,
Philipp Zabelb96904e2013-05-23 10:42:58 -0300121 },
122 {
123 .name = "YUV 4:2:0 Planar, YCrCb",
124 .fourcc = V4L2_PIX_FMT_YVU420,
Javier Martin186b2502012-07-26 05:53:35 -0300125 },
126 {
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300127 .name = "YUV 4:2:0 Partial interleaved Y/CbCr",
128 .fourcc = V4L2_PIX_FMT_NV12,
129 },
130 {
Philipp Zabel4de69312014-10-02 14:08:26 -0300131 .name = "YUV 4:2:2 Planar, YCbCr",
132 .fourcc = V4L2_PIX_FMT_YUV422P,
133 },
134 {
Javier Martin186b2502012-07-26 05:53:35 -0300135 .name = "H264 Encoded Stream",
136 .fourcc = V4L2_PIX_FMT_H264,
Javier Martin186b2502012-07-26 05:53:35 -0300137 },
138 {
139 .name = "MPEG4 Encoded Stream",
140 .fourcc = V4L2_PIX_FMT_MPEG4,
Javier Martin186b2502012-07-26 05:53:35 -0300141 },
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300142 {
143 .name = "JPEG Encoded Images",
144 .fourcc = V4L2_PIX_FMT_JPEG,
145 },
Javier Martin186b2502012-07-26 05:53:35 -0300146};
147
Philipp Zabelb96904e2013-05-23 10:42:58 -0300148#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \
149 { mode, src_fourcc, dst_fourcc, max_w, max_h }
150
151/*
152 * Arrays of codecs supported by each given version of Coda:
153 * i.MX27 -> codadx6
154 * i.MX5x -> coda7
155 * i.MX6 -> coda960
156 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants
157 */
Philipp Zabel814c3762014-07-18 07:22:45 -0300158static const struct coda_codec codadx6_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300159 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576),
160 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576),
Philipp Zabeldf1e74c2012-07-02 06:07:10 -0300161};
162
Philipp Zabel814c3762014-07-18 07:22:45 -0300163static const struct coda_codec coda7_codecs[] = {
Philipp Zabelb96904e2013-05-23 10:42:58 -0300164 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720),
165 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300166 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192),
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300167 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
168 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300169 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192),
Philipp Zabelb96904e2013-05-23 10:42:58 -0300170};
171
Philipp Zabel814c3762014-07-18 07:22:45 -0300172static const struct coda_codec coda9_codecs[] = {
Philipp Zabelb0ed05b2014-08-05 14:00:14 -0300173 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088),
174 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088),
175 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088),
176 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088),
Philipp Zabel89548442014-07-11 06:36:17 -0300177};
178
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300179struct coda_video_device {
180 const char *name;
181 enum coda_inst_type type;
182 const struct coda_context_ops *ops;
Philipp Zabela22496c2015-01-23 13:51:33 -0300183 bool direct;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300184 u32 src_formats[CODA_MAX_FORMATS];
185 u32 dst_formats[CODA_MAX_FORMATS];
186};
187
188static const struct coda_video_device coda_bit_encoder = {
189 .name = "coda-encoder",
190 .type = CODA_INST_ENCODER,
191 .ops = &coda_bit_encode_ops,
192 .src_formats = {
193 V4L2_PIX_FMT_YUV420,
194 V4L2_PIX_FMT_YVU420,
195 V4L2_PIX_FMT_NV12,
196 },
197 .dst_formats = {
198 V4L2_PIX_FMT_H264,
199 V4L2_PIX_FMT_MPEG4,
200 },
201};
202
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300203static const struct coda_video_device coda_bit_jpeg_encoder = {
204 .name = "coda-jpeg-encoder",
205 .type = CODA_INST_ENCODER,
206 .ops = &coda_bit_encode_ops,
207 .src_formats = {
208 V4L2_PIX_FMT_YUV420,
209 V4L2_PIX_FMT_YVU420,
210 V4L2_PIX_FMT_NV12,
211 V4L2_PIX_FMT_YUV422P,
212 },
213 .dst_formats = {
214 V4L2_PIX_FMT_JPEG,
215 },
216};
217
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300218static const struct coda_video_device coda_bit_decoder = {
219 .name = "coda-decoder",
220 .type = CODA_INST_DECODER,
221 .ops = &coda_bit_decode_ops,
222 .src_formats = {
223 V4L2_PIX_FMT_H264,
224 V4L2_PIX_FMT_MPEG4,
225 },
226 .dst_formats = {
227 V4L2_PIX_FMT_YUV420,
228 V4L2_PIX_FMT_YVU420,
229 V4L2_PIX_FMT_NV12,
230 },
231};
232
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300233static const struct coda_video_device coda_bit_jpeg_decoder = {
234 .name = "coda-jpeg-decoder",
235 .type = CODA_INST_DECODER,
236 .ops = &coda_bit_decode_ops,
237 .src_formats = {
238 V4L2_PIX_FMT_JPEG,
239 },
240 .dst_formats = {
241 V4L2_PIX_FMT_YUV420,
242 V4L2_PIX_FMT_YVU420,
243 V4L2_PIX_FMT_NV12,
244 V4L2_PIX_FMT_YUV422P,
245 },
246};
247
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300248static const struct coda_video_device *codadx6_video_devices[] = {
249 &coda_bit_encoder,
250};
251
252static const struct coda_video_device *coda7_video_devices[] = {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300253 &coda_bit_jpeg_encoder,
254 &coda_bit_jpeg_decoder,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300255 &coda_bit_encoder,
256 &coda_bit_decoder,
257};
258
259static const struct coda_video_device *coda9_video_devices[] = {
260 &coda_bit_encoder,
261 &coda_bit_decoder,
262};
263
Philipp Zabelb96904e2013-05-23 10:42:58 -0300264static bool coda_format_is_yuv(u32 fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300265{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300266 switch (fourcc) {
267 case V4L2_PIX_FMT_YUV420:
268 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300269 case V4L2_PIX_FMT_NV12:
Philipp Zabel4de69312014-10-02 14:08:26 -0300270 case V4L2_PIX_FMT_YUV422P:
Philipp Zabelb96904e2013-05-23 10:42:58 -0300271 return true;
272 default:
273 return false;
274 }
275}
Javier Martin186b2502012-07-26 05:53:35 -0300276
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300277static const char *coda_format_name(u32 fourcc)
278{
279 int i;
280
281 for (i = 0; i < ARRAY_SIZE(coda_formats); i++) {
282 if (coda_formats[i].fourcc == fourcc)
283 return coda_formats[i].name;
284 }
285
286 return NULL;
287}
288
Philipp Zabelb96904e2013-05-23 10:42:58 -0300289/*
290 * Normalize all supported YUV 4:2:0 formats to the value used in the codec
291 * tables.
292 */
293static u32 coda_format_normalize_yuv(u32 fourcc)
294{
295 return coda_format_is_yuv(fourcc) ? V4L2_PIX_FMT_YUV420 : fourcc;
296}
297
Philipp Zabel814c3762014-07-18 07:22:45 -0300298static const struct coda_codec *coda_find_codec(struct coda_dev *dev,
299 int src_fourcc, int dst_fourcc)
Philipp Zabelb96904e2013-05-23 10:42:58 -0300300{
Philipp Zabel814c3762014-07-18 07:22:45 -0300301 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300302 int num_codecs = dev->devtype->num_codecs;
303 int k;
304
305 src_fourcc = coda_format_normalize_yuv(src_fourcc);
306 dst_fourcc = coda_format_normalize_yuv(dst_fourcc);
307 if (src_fourcc == dst_fourcc)
308 return NULL;
309
310 for (k = 0; k < num_codecs; k++) {
311 if (codecs[k].src_fourcc == src_fourcc &&
312 codecs[k].dst_fourcc == dst_fourcc)
Javier Martin186b2502012-07-26 05:53:35 -0300313 break;
314 }
315
Philipp Zabelb96904e2013-05-23 10:42:58 -0300316 if (k == num_codecs)
Javier Martin186b2502012-07-26 05:53:35 -0300317 return NULL;
318
Philipp Zabelb96904e2013-05-23 10:42:58 -0300319 return &codecs[k];
Javier Martin186b2502012-07-26 05:53:35 -0300320}
321
Philipp Zabel57625592013-09-30 10:34:51 -0300322static void coda_get_max_dimensions(struct coda_dev *dev,
Philipp Zabel814c3762014-07-18 07:22:45 -0300323 const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300324 int *max_w, int *max_h)
325{
Philipp Zabel814c3762014-07-18 07:22:45 -0300326 const struct coda_codec *codecs = dev->devtype->codecs;
Philipp Zabel57625592013-09-30 10:34:51 -0300327 int num_codecs = dev->devtype->num_codecs;
328 unsigned int w, h;
329 int k;
330
331 if (codec) {
332 w = codec->max_w;
333 h = codec->max_h;
334 } else {
335 for (k = 0, w = 0, h = 0; k < num_codecs; k++) {
336 w = max(w, codecs[k].max_w);
337 h = max(h, codecs[k].max_h);
338 }
339 }
340
341 if (max_w)
342 *max_w = w;
343 if (max_h)
344 *max_h = h;
345}
346
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300347const struct coda_video_device *to_coda_video_device(struct video_device *vdev)
348{
349 struct coda_dev *dev = video_get_drvdata(vdev);
350 unsigned int i = vdev - dev->vfd;
351
352 if (i >= dev->devtype->num_vdevs)
353 return NULL;
354
355 return dev->devtype->vdevs[i];
356}
357
Philipp Zabel79924ca2014-07-23 12:28:45 -0300358const char *coda_product_name(int product)
Philipp Zabel927933f2013-09-30 10:34:48 -0300359{
360 static char buf[9];
361
362 switch (product) {
363 case CODA_DX6:
364 return "CodaDx6";
365 case CODA_7541:
366 return "CODA7541";
Philipp Zabel89548442014-07-11 06:36:17 -0300367 case CODA_960:
368 return "CODA960";
Philipp Zabel927933f2013-09-30 10:34:48 -0300369 default:
370 snprintf(buf, sizeof(buf), "(0x%04x)", product);
371 return buf;
372 }
373}
374
Javier Martin186b2502012-07-26 05:53:35 -0300375/*
376 * V4L2 ioctl() operations.
377 */
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300378static int coda_querycap(struct file *file, void *priv,
379 struct v4l2_capability *cap)
Javier Martin186b2502012-07-26 05:53:35 -0300380{
Philipp Zabel927933f2013-09-30 10:34:48 -0300381 struct coda_ctx *ctx = fh_to_ctx(priv);
382
Javier Martin186b2502012-07-26 05:53:35 -0300383 strlcpy(cap->driver, CODA_NAME, sizeof(cap->driver));
Philipp Zabel927933f2013-09-30 10:34:48 -0300384 strlcpy(cap->card, coda_product_name(ctx->dev->devtype->product),
385 sizeof(cap->card));
Philipp Zabeldad0e1c2013-05-21 04:18:22 -0300386 strlcpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info));
Philipp Zabel3898e7a2014-07-18 07:22:37 -0300387 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Javier Martin186b2502012-07-26 05:53:35 -0300388 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
389
390 return 0;
391}
392
Philipp Zabel22e244b2014-07-18 07:22:43 -0300393static int coda_enum_fmt(struct file *file, void *priv,
394 struct v4l2_fmtdesc *f)
Javier Martin186b2502012-07-26 05:53:35 -0300395{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300396 struct video_device *vdev = video_devdata(file);
397 const struct coda_video_device *cvd = to_coda_video_device(vdev);
398 const u32 *formats;
399 const char *name;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300400
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300401 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
402 formats = cvd->src_formats;
403 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
404 formats = cvd->dst_formats;
Philipp Zabel22e244b2014-07-18 07:22:43 -0300405 else
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300406 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300407
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300408 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0)
409 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300410
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300411 name = coda_format_name(formats[f->index]);
412 strlcpy(f->description, name, sizeof(f->description));
413 f->pixelformat = formats[f->index];
414 if (!coda_format_is_yuv(formats[f->index]))
415 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
Javier Martin186b2502012-07-26 05:53:35 -0300416
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300417 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300418}
419
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300420static int coda_g_fmt(struct file *file, void *priv,
421 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300422{
Javier Martin186b2502012-07-26 05:53:35 -0300423 struct coda_q_data *q_data;
424 struct coda_ctx *ctx = fh_to_ctx(priv);
425
Javier Martin186b2502012-07-26 05:53:35 -0300426 q_data = get_q_data(ctx, f->type);
Philipp Zabelb9736292014-07-11 06:36:18 -0300427 if (!q_data)
428 return -EINVAL;
Javier Martin186b2502012-07-26 05:53:35 -0300429
430 f->fmt.pix.field = V4L2_FIELD_NONE;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300431 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300432 f->fmt.pix.width = q_data->width;
433 f->fmt.pix.height = q_data->height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300434 f->fmt.pix.bytesperline = q_data->bytesperline;
Javier Martin186b2502012-07-26 05:53:35 -0300435
436 f->fmt.pix.sizeimage = q_data->sizeimage;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300437 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
438 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
439 else
440 f->fmt.pix.colorspace = ctx->colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300441
442 return 0;
443}
444
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300445static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f)
446{
447 struct coda_q_data *q_data;
448 const u32 *formats;
449 int i;
450
451 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
452 formats = ctx->cvd->src_formats;
453 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
454 formats = ctx->cvd->dst_formats;
455 else
456 return -EINVAL;
457
458 for (i = 0; i < CODA_MAX_FORMATS; i++) {
459 if (formats[i] == f->fmt.pix.pixelformat) {
460 f->fmt.pix.pixelformat = formats[i];
461 return 0;
462 }
463 }
464
465 /* Fall back to currently set pixelformat */
466 q_data = get_q_data(ctx, f->type);
467 f->fmt.pix.pixelformat = q_data->fourcc;
Javier Martin186b2502012-07-26 05:53:35 -0300468
469 return 0;
470}
471
Philipp Zabel8e428d52015-01-23 13:51:29 -0300472static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage,
473 u32 width, u32 height)
474{
475 /*
476 * This is a rough estimate for sensible compressed buffer
477 * sizes (between 1 and 16 bits per pixel). This could be
478 * improved by better format specific worst case estimates.
479 */
480 return round_up(clamp(sizeimage, width * height / 8,
481 width * height * 2), PAGE_SIZE);
482}
483
Philipp Zabel814c3762014-07-18 07:22:45 -0300484static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec,
Philipp Zabel57625592013-09-30 10:34:51 -0300485 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300486{
Philipp Zabel57625592013-09-30 10:34:51 -0300487 struct coda_dev *dev = ctx->dev;
Philipp Zabelb96904e2013-05-23 10:42:58 -0300488 unsigned int max_w, max_h;
Javier Martin186b2502012-07-26 05:53:35 -0300489 enum v4l2_field field;
490
491 field = f->fmt.pix.field;
492 if (field == V4L2_FIELD_ANY)
493 field = V4L2_FIELD_NONE;
494 else if (V4L2_FIELD_NONE != field)
495 return -EINVAL;
496
497 /* V4L2 specification suggests the driver corrects the format struct
498 * if any of the dimensions is unsupported */
499 f->fmt.pix.field = field;
500
Philipp Zabel57625592013-09-30 10:34:51 -0300501 coda_get_max_dimensions(dev, codec, &max_w, &max_h);
502 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN,
503 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN,
504 S_ALIGN);
Philipp Zabelb96904e2013-05-23 10:42:58 -0300505
Philipp Zabel57625592013-09-30 10:34:51 -0300506 switch (f->fmt.pix.pixelformat) {
507 case V4L2_PIX_FMT_YUV420:
508 case V4L2_PIX_FMT_YVU420:
Philipp Zabel1cb12cf2014-09-29 09:53:47 -0300509 case V4L2_PIX_FMT_NV12:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300510 /*
511 * Frame stride must be at least multiple of 8,
512 * but multiple of 16 for h.264 or JPEG 4:2:x
513 */
Philipp Zabel451dfe52014-07-11 06:36:39 -0300514 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel47cf0c62013-05-23 10:42:54 -0300515 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
Philipp Zabel451d43a2012-07-26 09:18:27 -0300516 f->fmt.pix.height * 3 / 2;
Philipp Zabel57625592013-09-30 10:34:51 -0300517 break;
Philipp Zabel4de69312014-10-02 14:08:26 -0300518 case V4L2_PIX_FMT_YUV422P:
519 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
520 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
521 f->fmt.pix.height * 2;
522 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300523 case V4L2_PIX_FMT_JPEG:
524 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
525 /* fallthrough */
Philipp Zabel57625592013-09-30 10:34:51 -0300526 case V4L2_PIX_FMT_H264:
527 case V4L2_PIX_FMT_MPEG4:
Javier Martin186b2502012-07-26 05:53:35 -0300528 f->fmt.pix.bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -0300529 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx,
530 f->fmt.pix.sizeimage,
531 f->fmt.pix.width,
532 f->fmt.pix.height);
Philipp Zabel57625592013-09-30 10:34:51 -0300533 break;
534 default:
535 BUG();
Javier Martin186b2502012-07-26 05:53:35 -0300536 }
537
538 return 0;
539}
540
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300541static int coda_try_fmt_vid_cap(struct file *file, void *priv,
542 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300543{
Javier Martin186b2502012-07-26 05:53:35 -0300544 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300545 const struct coda_q_data *q_data_src;
546 const struct coda_codec *codec;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300547 struct vb2_queue *src_vq;
548 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300549
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300550 ret = coda_try_pixelformat(ctx, f);
551 if (ret < 0)
552 return ret;
553
554 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
555
Philipp Zabel918c66f2013-06-27 06:59:01 -0300556 /*
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300557 * If the source format is already fixed, only allow the same output
558 * resolution
Philipp Zabel918c66f2013-06-27 06:59:01 -0300559 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300560 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300561 if (vb2_is_streaming(src_vq)) {
Philipp Zabel91b5841e2014-07-18 07:22:41 -0300562 f->fmt.pix.width = q_data_src->width;
563 f->fmt.pix.height = q_data_src->height;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300564 }
Javier Martin186b2502012-07-26 05:53:35 -0300565
566 f->fmt.pix.colorspace = ctx->colorspace;
567
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300568 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
569 codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
570 f->fmt.pix.pixelformat);
571 if (!codec)
572 return -EINVAL;
573
Philipp Zabel57625592013-09-30 10:34:51 -0300574 ret = coda_try_fmt(ctx, codec, f);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300575 if (ret < 0)
576 return ret;
577
578 /* The h.264 decoder only returns complete 16x16 macroblocks */
579 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) {
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300580 f->fmt.pix.width = f->fmt.pix.width;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300581 f->fmt.pix.height = round_up(f->fmt.pix.height, 16);
Philipp Zabel1b0ed7b2014-07-11 06:36:37 -0300582 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300583 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
584 f->fmt.pix.height * 3 / 2;
585 }
586
587 return 0;
Javier Martin186b2502012-07-26 05:53:35 -0300588}
589
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300590static int coda_try_fmt_vid_out(struct file *file, void *priv,
591 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300592{
593 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300594 struct coda_dev *dev = ctx->dev;
595 const struct coda_q_data *q_data_dst;
596 const struct coda_codec *codec;
597 int ret;
Javier Martin186b2502012-07-26 05:53:35 -0300598
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300599 ret = coda_try_pixelformat(ctx, f);
600 if (ret < 0)
601 return ret;
Javier Martin186b2502012-07-26 05:53:35 -0300602
Philipp Zabel5f9826e2015-01-23 13:51:21 -0300603 switch (f->fmt.pix.colorspace) {
604 case V4L2_COLORSPACE_REC709:
605 case V4L2_COLORSPACE_JPEG:
606 break;
607 default:
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300608 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_JPEG)
609 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
610 else
611 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Javier Martin186b2502012-07-26 05:53:35 -0300612 }
613
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300614 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
615 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300616
617 return coda_try_fmt(ctx, codec, f);
618}
619
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300620static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300621{
622 struct coda_q_data *q_data;
623 struct vb2_queue *vq;
Javier Martin186b2502012-07-26 05:53:35 -0300624
Philipp Zabel14604e32014-07-11 06:36:22 -0300625 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Javier Martin186b2502012-07-26 05:53:35 -0300626 if (!vq)
627 return -EINVAL;
628
629 q_data = get_q_data(ctx, f->type);
630 if (!q_data)
631 return -EINVAL;
632
633 if (vb2_is_busy(vq)) {
634 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
635 return -EBUSY;
636 }
637
Philipp Zabelb96904e2013-05-23 10:42:58 -0300638 q_data->fourcc = f->fmt.pix.pixelformat;
Javier Martin186b2502012-07-26 05:53:35 -0300639 q_data->width = f->fmt.pix.width;
640 q_data->height = f->fmt.pix.height;
Philipp Zabel2fd6a372014-07-11 06:36:36 -0300641 q_data->bytesperline = f->fmt.pix.bytesperline;
Philipp Zabel451d43a2012-07-26 09:18:27 -0300642 q_data->sizeimage = f->fmt.pix.sizeimage;
Philipp Zabel52c41672014-07-11 06:36:19 -0300643 q_data->rect.left = 0;
644 q_data->rect.top = 0;
645 q_data->rect.width = f->fmt.pix.width;
646 q_data->rect.height = f->fmt.pix.height;
Javier Martin186b2502012-07-26 05:53:35 -0300647
648 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
649 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
Philipp Zabelb96904e2013-05-23 10:42:58 -0300650 f->type, q_data->width, q_data->height, q_data->fourcc);
Javier Martin186b2502012-07-26 05:53:35 -0300651
652 return 0;
653}
654
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300655static int coda_s_fmt_vid_cap(struct file *file, void *priv,
656 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300657{
Philipp Zabelb96904e2013-05-23 10:42:58 -0300658 struct coda_ctx *ctx = fh_to_ctx(priv);
Javier Martin186b2502012-07-26 05:53:35 -0300659 int ret;
660
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300661 ret = coda_try_fmt_vid_cap(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300662 if (ret)
663 return ret;
664
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300665 return coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300666}
667
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300668static int coda_s_fmt_vid_out(struct file *file, void *priv,
669 struct v4l2_format *f)
Javier Martin186b2502012-07-26 05:53:35 -0300670{
671 struct coda_ctx *ctx = fh_to_ctx(priv);
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300672 struct v4l2_format f_cap;
Javier Martin186b2502012-07-26 05:53:35 -0300673 int ret;
674
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300675 ret = coda_try_fmt_vid_out(file, priv, f);
Javier Martin186b2502012-07-26 05:53:35 -0300676 if (ret)
677 return ret;
678
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300679 ret = coda_s_fmt(ctx, f);
Javier Martin186b2502012-07-26 05:53:35 -0300680 if (ret)
Philipp Zabel2dc546d2014-08-05 14:00:18 -0300681 return ret;
682
683 ctx->colorspace = f->fmt.pix.colorspace;
Javier Martin186b2502012-07-26 05:53:35 -0300684
Philipp Zabel1c2f6a92015-01-23 13:51:22 -0300685 memset(&f_cap, 0, sizeof(f_cap));
Philipp Zabelf95a6ce2014-08-05 14:00:19 -0300686 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
687 coda_g_fmt(file, priv, &f_cap);
688 f_cap.fmt.pix.width = f->fmt.pix.width;
689 f_cap.fmt.pix.height = f->fmt.pix.height;
690
691 ret = coda_try_fmt_vid_cap(file, priv, &f_cap);
692 if (ret)
693 return ret;
694
695 return coda_s_fmt(ctx, &f_cap);
Javier Martin186b2502012-07-26 05:53:35 -0300696}
697
Philipp Zabel73751da2015-03-24 14:30:51 -0300698static int coda_reqbufs(struct file *file, void *priv,
699 struct v4l2_requestbuffers *rb)
700{
701 struct coda_ctx *ctx = fh_to_ctx(priv);
702 int ret;
703
704 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb);
705 if (ret)
706 return ret;
707
708 /*
709 * Allow to allocate instance specific per-context buffers, such as
710 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed.
711 */
712 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs)
713 return ctx->ops->reqbufs(ctx, rb);
714
715 return 0;
716}
717
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300718static int coda_qbuf(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);
722
Philipp Zabel14604e32014-07-11 06:36:22 -0300723 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
Javier Martin186b2502012-07-26 05:53:35 -0300724}
725
Philipp Zabel918c66f2013-06-27 06:59:01 -0300726static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300727 struct vb2_buffer *buf)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300728{
729 struct vb2_queue *src_vq;
730
Philipp Zabel14604e32014-07-11 06:36:22 -0300731 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -0300732
733 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) &&
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300734 (buf->v4l2_buf.sequence == (ctx->qsequence - 1)));
Philipp Zabel918c66f2013-06-27 06:59:01 -0300735}
736
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300737void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_buffer *buf,
738 enum vb2_buffer_state state)
Javier Martin186b2502012-07-26 05:53:35 -0300739{
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300740 const struct v4l2_event eos_event = {
741 .type = V4L2_EVENT_EOS
742 };
Javier Martin186b2502012-07-26 05:53:35 -0300743
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300744 if (coda_buf_is_end_of_stream(ctx, buf)) {
745 buf->v4l2_buf.flags |= V4L2_BUF_FLAG_LAST;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300746
747 v4l2_event_queue_fh(&ctx->fh, &eos_event);
748 }
749
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300750 v4l2_m2m_buf_done(buf, state);
Javier Martin186b2502012-07-26 05:53:35 -0300751}
752
Philipp Zabel52c41672014-07-11 06:36:19 -0300753static int coda_g_selection(struct file *file, void *fh,
754 struct v4l2_selection *s)
755{
756 struct coda_ctx *ctx = fh_to_ctx(fh);
757 struct coda_q_data *q_data;
758 struct v4l2_rect r, *rsel;
759
760 q_data = get_q_data(ctx, s->type);
761 if (!q_data)
762 return -EINVAL;
763
764 r.left = 0;
765 r.top = 0;
766 r.width = q_data->width;
767 r.height = q_data->height;
768 rsel = &q_data->rect;
769
770 switch (s->target) {
771 case V4L2_SEL_TGT_CROP_DEFAULT:
772 case V4L2_SEL_TGT_CROP_BOUNDS:
773 rsel = &r;
774 /* fallthrough */
775 case V4L2_SEL_TGT_CROP:
776 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
777 return -EINVAL;
778 break;
779 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
780 case V4L2_SEL_TGT_COMPOSE_PADDED:
781 rsel = &r;
782 /* fallthrough */
783 case V4L2_SEL_TGT_COMPOSE:
784 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
785 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
786 return -EINVAL;
787 break;
788 default:
789 return -EINVAL;
790 }
791
792 s->r = *rsel;
793
794 return 0;
795}
796
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300797static int coda_try_decoder_cmd(struct file *file, void *fh,
798 struct v4l2_decoder_cmd *dc)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300799{
Philipp Zabel918c66f2013-06-27 06:59:01 -0300800 if (dc->cmd != V4L2_DEC_CMD_STOP)
801 return -EINVAL;
802
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300803 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300804 return -EINVAL;
805
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300806 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
Philipp Zabel918c66f2013-06-27 06:59:01 -0300807 return -EINVAL;
808
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300809 return 0;
810}
811
812static int coda_decoder_cmd(struct file *file, void *fh,
813 struct v4l2_decoder_cmd *dc)
814{
815 struct coda_ctx *ctx = fh_to_ctx(fh);
816 int ret;
817
818 ret = coda_try_decoder_cmd(file, fh, dc);
819 if (ret < 0)
820 return ret;
821
822 /* Ignore decoder stop command silently in encoder context */
Philipp Zabel918c66f2013-06-27 06:59:01 -0300823 if (ctx->inst_type != CODA_INST_DECODER)
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300824 return 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300825
Philipp Zabel347bb7f2014-07-23 12:28:42 -0300826 /* Set the stream-end flag on this context */
827 coda_bit_stream_end_flag(ctx);
Philipp Zabel84e23652014-07-11 06:36:34 -0300828 ctx->hold = false;
Michael Olbrichf3497da2014-07-11 06:36:30 -0300829 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
Philipp Zabel89548442014-07-11 06:36:17 -0300830
Philipp Zabel918c66f2013-06-27 06:59:01 -0300831 return 0;
832}
833
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300834static int coda_subscribe_event(struct v4l2_fh *fh,
835 const struct v4l2_event_subscription *sub)
Philipp Zabel918c66f2013-06-27 06:59:01 -0300836{
837 switch (sub->type) {
838 case V4L2_EVENT_EOS:
839 return v4l2_event_subscribe(fh, sub, 0, NULL);
840 default:
841 return v4l2_ctrl_subscribe_event(fh, sub);
842 }
Javier Martin186b2502012-07-26 05:53:35 -0300843}
844
845static const struct v4l2_ioctl_ops coda_ioctl_ops = {
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300846 .vidioc_querycap = coda_querycap,
Javier Martin186b2502012-07-26 05:53:35 -0300847
Philipp Zabel22e244b2014-07-18 07:22:43 -0300848 .vidioc_enum_fmt_vid_cap = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300849 .vidioc_g_fmt_vid_cap = coda_g_fmt,
850 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap,
851 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap,
Javier Martin186b2502012-07-26 05:53:35 -0300852
Philipp Zabel22e244b2014-07-18 07:22:43 -0300853 .vidioc_enum_fmt_vid_out = coda_enum_fmt,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300854 .vidioc_g_fmt_vid_out = coda_g_fmt,
855 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out,
856 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out,
Javier Martin186b2502012-07-26 05:53:35 -0300857
Philipp Zabel73751da2015-03-24 14:30:51 -0300858 .vidioc_reqbufs = coda_reqbufs,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300859 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
Javier Martin186b2502012-07-26 05:53:35 -0300860
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300861 .vidioc_qbuf = coda_qbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300862 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300863 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300864 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Javier Martin186b2502012-07-26 05:53:35 -0300865
Philipp Zabel152ea1c2014-07-11 06:36:21 -0300866 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
867 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300868
Philipp Zabel52c41672014-07-11 06:36:19 -0300869 .vidioc_g_selection = coda_g_selection,
870
Philipp Zabel64ba40a2013-09-30 10:34:52 -0300871 .vidioc_try_decoder_cmd = coda_try_decoder_cmd,
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300872 .vidioc_decoder_cmd = coda_decoder_cmd,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300873
Philipp Zabel2e9e4f12013-09-30 10:34:50 -0300874 .vidioc_subscribe_event = coda_subscribe_event,
Philipp Zabel918c66f2013-06-27 06:59:01 -0300875 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Javier Martin186b2502012-07-26 05:53:35 -0300876};
877
Philipp Zabel79924ca2014-07-23 12:28:45 -0300878void coda_set_gdi_regs(struct coda_ctx *ctx)
Philipp Zabel89548442014-07-11 06:36:17 -0300879{
880 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
881 struct coda_dev *dev = ctx->dev;
882 int i;
883
884 for (i = 0; i < 16; i++)
885 coda_write(dev, tiled_map->xy2ca_map[i],
886 CODA9_GDI_XY2_CAS_0 + 4 * i);
887 for (i = 0; i < 4; i++)
888 coda_write(dev, tiled_map->xy2ba_map[i],
889 CODA9_GDI_XY2_BA_0 + 4 * i);
890 for (i = 0; i < 16; i++)
891 coda_write(dev, tiled_map->xy2ra_map[i],
892 CODA9_GDI_XY2_RAS_0 + 4 * i);
893 coda_write(dev, tiled_map->xy2rbc_config, CODA9_GDI_XY2_RBC_CONFIG);
894 for (i = 0; i < 32; i++)
895 coda_write(dev, tiled_map->rbc2axi_map[i],
896 CODA9_GDI_RBC2_AXI_0 + 4 * i);
897}
898
Javier Martin186b2502012-07-26 05:53:35 -0300899/*
900 * Mem-to-mem operations.
901 */
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300902
903static void coda_device_run(void *m2m_priv)
904{
905 struct coda_ctx *ctx = m2m_priv;
906 struct coda_dev *dev = ctx->dev;
Philipp Zabel32b6f202014-07-11 06:36:20 -0300907
908 queue_work(dev->workqueue, &ctx->pic_run_work);
909}
910
Philipp Zabel32b6f202014-07-11 06:36:20 -0300911static void coda_pic_run_work(struct work_struct *work)
912{
913 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work);
914 struct coda_dev *dev = ctx->dev;
Philipp Zabel918c66f2013-06-27 06:59:01 -0300915 int ret;
916
917 mutex_lock(&ctx->buffer_mutex);
Philipp Zabel477c1cf2013-06-21 03:55:33 -0300918 mutex_lock(&dev->coda_mutex);
919
Philipp Zabela1192a12014-07-23 12:28:40 -0300920 ret = ctx->ops->prepare_run(ctx);
921 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) {
922 mutex_unlock(&dev->coda_mutex);
923 mutex_unlock(&ctx->buffer_mutex);
924 /* job_finish scheduled by prepare_decode */
925 return;
Philipp Zabel10436672012-07-02 09:03:55 -0300926 }
927
Philipp Zabelf23797b2014-08-06 08:02:23 -0300928 if (!wait_for_completion_timeout(&ctx->completion,
929 msecs_to_jiffies(1000))) {
Philipp Zabel32b6f202014-07-11 06:36:20 -0300930 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n");
Philipp Zabel84e23652014-07-11 06:36:34 -0300931
932 ctx->hold = true;
Philipp Zabel8f452842014-07-11 06:36:35 -0300933
934 coda_hw_reset(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300935 } else if (!ctx->aborting) {
Philipp Zabela1192a12014-07-23 12:28:40 -0300936 ctx->ops->finish_run(ctx);
Philipp Zabel32b6f202014-07-11 06:36:20 -0300937 }
938
Philipp Zabel747d7642015-01-23 13:51:31 -0300939 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) &&
940 ctx->ops->seq_end_work)
Philipp Zabel32b6f202014-07-11 06:36:20 -0300941 queue_work(dev->workqueue, &ctx->seq_end_work);
942
943 mutex_unlock(&dev->coda_mutex);
944 mutex_unlock(&ctx->buffer_mutex);
945
Philipp Zabel14604e32014-07-11 06:36:22 -0300946 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -0300947}
948
949static int coda_job_ready(void *m2m_priv)
950{
951 struct coda_ctx *ctx = m2m_priv;
952
953 /*
954 * For both 'P' and 'key' frame cases 1 picture
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300955 * and 1 frame are needed. In the decoder case,
956 * the compressed frame can be in the bitstream.
Javier Martin186b2502012-07-26 05:53:35 -0300957 */
Philipp Zabel14604e32014-07-11 06:36:22 -0300958 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300959 ctx->inst_type != CODA_INST_DECODER) {
Javier Martin186b2502012-07-26 05:53:35 -0300960 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
961 "not ready: not enough video buffers.\n");
962 return 0;
963 }
964
Philipp Zabel14604e32014-07-11 06:36:22 -0300965 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
Philipp Zabel9082a7c2013-06-21 03:55:31 -0300966 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
967 "not ready: not enough video capture buffers.\n");
968 return 0;
969 }
970
Philipp Zabela22496c2015-01-23 13:51:33 -0300971 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) {
Philipp Zabelf77fd8a2015-01-23 13:51:20 -0300972 struct list_head *meta;
973 bool stream_end;
974 int num_metas;
975 int src_bufs;
976
977 if (ctx->hold && !v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
978 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
979 "%d: not ready: on hold for more buffers.\n",
980 ctx->idx);
981 return 0;
982 }
983
984 stream_end = ctx->bit_stream_param &
985 CODA_BIT_STREAM_END_FLAG;
986
987 num_metas = 0;
988 list_for_each(meta, &ctx->buffer_meta_list)
989 num_metas++;
990
991 src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
992
993 if (!stream_end && (num_metas + src_bufs) < 2) {
994 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
995 "%d: not ready: need 2 buffers available (%d, %d)\n",
996 ctx->idx, num_metas, src_bufs);
997 return 0;
998 }
999
1000
1001 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) &&
1002 !stream_end && (coda_get_bitstream_payload(ctx) < 512)) {
1003 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1004 "%d: not ready: not enough bitstream data (%d).\n",
1005 ctx->idx, coda_get_bitstream_payload(ctx));
1006 return 0;
1007 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001008 }
1009
Philipp Zabel3e748262013-05-23 10:43:01 -03001010 if (ctx->aborting) {
1011 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1012 "not ready: aborting\n");
1013 return 0;
1014 }
1015
Javier Martin186b2502012-07-26 05:53:35 -03001016 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1017 "job ready\n");
1018 return 1;
1019}
1020
1021static void coda_job_abort(void *priv)
1022{
1023 struct coda_ctx *ctx = priv;
Javier Martin186b2502012-07-26 05:53:35 -03001024
1025 ctx->aborting = 1;
1026
1027 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1028 "Aborting task\n");
Javier Martin186b2502012-07-26 05:53:35 -03001029}
1030
1031static void coda_lock(void *m2m_priv)
1032{
1033 struct coda_ctx *ctx = m2m_priv;
1034 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001035
Javier Martin186b2502012-07-26 05:53:35 -03001036 mutex_lock(&pcdev->dev_mutex);
1037}
1038
1039static void coda_unlock(void *m2m_priv)
1040{
1041 struct coda_ctx *ctx = m2m_priv;
1042 struct coda_dev *pcdev = ctx->dev;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001043
Javier Martin186b2502012-07-26 05:53:35 -03001044 mutex_unlock(&pcdev->dev_mutex);
1045}
1046
Philipp Zabel814c3762014-07-18 07:22:45 -03001047static const struct v4l2_m2m_ops coda_m2m_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001048 .device_run = coda_device_run,
1049 .job_ready = coda_job_ready,
1050 .job_abort = coda_job_abort,
1051 .lock = coda_lock,
1052 .unlock = coda_unlock,
1053};
1054
Philipp Zabel89548442014-07-11 06:36:17 -03001055static void coda_set_tiled_map_type(struct coda_ctx *ctx, int tiled_map_type)
1056{
1057 struct gdi_tiled_map *tiled_map = &ctx->tiled_map;
1058 int luma_map, chro_map, i;
1059
1060 memset(tiled_map, 0, sizeof(*tiled_map));
1061
1062 luma_map = 64;
1063 chro_map = 64;
1064 tiled_map->map_type = tiled_map_type;
1065 for (i = 0; i < 16; i++)
1066 tiled_map->xy2ca_map[i] = luma_map << 8 | chro_map;
1067 for (i = 0; i < 4; i++)
1068 tiled_map->xy2ba_map[i] = luma_map << 8 | chro_map;
1069 for (i = 0; i < 16; i++)
1070 tiled_map->xy2ra_map[i] = luma_map << 8 | chro_map;
1071
1072 if (tiled_map_type == GDI_LINEAR_FRAME_MAP) {
1073 tiled_map->xy2rbc_config = 0;
1074 } else {
1075 dev_err(&ctx->dev->plat_dev->dev, "invalid map type: %d\n",
1076 tiled_map_type);
1077 return;
1078 }
1079}
1080
Javier Martin186b2502012-07-26 05:53:35 -03001081static void set_default_params(struct coda_ctx *ctx)
1082{
Philipp Zabel8e428d52015-01-23 13:51:29 -03001083 unsigned int max_w, max_h, usize, csize;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001084
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001085 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0],
1086 ctx->cvd->dst_formats[0]);
Philipp Zabeld4c6a412014-10-02 14:08:35 -03001087 max_w = min(ctx->codec->max_w, 1920U);
1088 max_h = min(ctx->codec->max_h, 1088U);
Philipp Zabel8e428d52015-01-23 13:51:29 -03001089 usize = max_w * max_h * 3 / 2;
1090 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h);
Javier Martin186b2502012-07-26 05:53:35 -03001091
Philipp Zabel121cacf2014-07-18 07:22:42 -03001092 ctx->params.codec_mode = ctx->codec->mode;
Javier Martin186b2502012-07-26 05:53:35 -03001093 ctx->colorspace = V4L2_COLORSPACE_REC709;
1094 ctx->params.framerate = 30;
Javier Martin186b2502012-07-26 05:53:35 -03001095
1096 /* Default formats for output and input queues */
Philipp Zabelb96904e2013-05-23 10:42:58 -03001097 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->codec->src_fourcc;
1098 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->codec->dst_fourcc;
1099 ctx->q_data[V4L2_M2M_SRC].width = max_w;
1100 ctx->q_data[V4L2_M2M_SRC].height = max_h;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001101 ctx->q_data[V4L2_M2M_DST].width = max_w;
1102 ctx->q_data[V4L2_M2M_DST].height = max_h;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001103 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) {
1104 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001105 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001106 ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001107 ctx->q_data[V4L2_M2M_DST].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001108 } else {
1109 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001110 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001111 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w;
Philipp Zabel8e428d52015-01-23 13:51:29 -03001112 ctx->q_data[V4L2_M2M_DST].sizeimage = usize;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001113 }
Philipp Zabel52c41672014-07-11 06:36:19 -03001114 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w;
1115 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h;
1116 ctx->q_data[V4L2_M2M_DST].rect.width = max_w;
1117 ctx->q_data[V4L2_M2M_DST].rect.height = max_h;
Philipp Zabel89548442014-07-11 06:36:17 -03001118
1119 if (ctx->dev->devtype->product == CODA_960)
1120 coda_set_tiled_map_type(ctx, GDI_LINEAR_FRAME_MAP);
Javier Martin186b2502012-07-26 05:53:35 -03001121}
1122
1123/*
1124 * Queue operations
1125 */
1126static int coda_queue_setup(struct vb2_queue *vq,
1127 const struct v4l2_format *fmt,
1128 unsigned int *nbuffers, unsigned int *nplanes,
1129 unsigned int sizes[], void *alloc_ctxs[])
1130{
1131 struct coda_ctx *ctx = vb2_get_drv_priv(vq);
Philipp Zabele34db062012-08-29 08:22:00 -03001132 struct coda_q_data *q_data;
Javier Martin186b2502012-07-26 05:53:35 -03001133 unsigned int size;
1134
Philipp Zabele34db062012-08-29 08:22:00 -03001135 q_data = get_q_data(ctx, vq->type);
1136 size = q_data->sizeimage;
Javier Martin186b2502012-07-26 05:53:35 -03001137
1138 *nplanes = 1;
1139 sizes[0] = size;
1140
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001141 /* Set to vb2-dma-contig allocator context, ignored by vb2-vmalloc */
Javier Martin186b2502012-07-26 05:53:35 -03001142 alloc_ctxs[0] = ctx->dev->alloc_ctx;
1143
1144 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1145 "get %d buffer(s) of size %d each.\n", *nbuffers, size);
1146
1147 return 0;
1148}
1149
1150static int coda_buf_prepare(struct vb2_buffer *vb)
1151{
1152 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1153 struct coda_q_data *q_data;
1154
1155 q_data = get_q_data(ctx, vb->vb2_queue->type);
1156
1157 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1158 v4l2_warn(&ctx->dev->v4l2_dev,
1159 "%s data will not fit into plane (%lu < %lu)\n",
1160 __func__, vb2_plane_size(vb, 0),
1161 (long)q_data->sizeimage);
1162 return -EINVAL;
1163 }
1164
Javier Martin186b2502012-07-26 05:53:35 -03001165 return 0;
1166}
1167
1168static void coda_buf_queue(struct vb2_buffer *vb)
1169{
1170 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Philipp Zabelcb06b702015-01-23 13:51:35 -03001171 struct vb2_queue *vq = vb->vb2_queue;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001172 struct coda_q_data *q_data;
1173
1174 q_data = get_q_data(ctx, vb->vb2_queue->type);
1175
1176 /*
1177 * In the decoder case, immediately try to copy the buffer into the
1178 * bitstream ringbuffer and mark it as ready to be dequeued.
1179 */
Philipp Zabelcb06b702015-01-23 13:51:35 -03001180 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001181 /*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001182 * For backwards compatibility, queuing an empty buffer marks
Philipp Zabel918c66f2013-06-27 06:59:01 -03001183 * the stream end
1184 */
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001185 if (vb2_get_plane_payload(vb, 0) == 0)
1186 coda_bit_stream_end_flag(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001187 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel14604e32014-07-11 06:36:22 -03001188 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbricheabed932014-07-18 07:22:40 -03001189 if (vb2_is_streaming(vb->vb2_queue))
Philipp Zabel582d88722015-03-24 14:30:57 -03001190 coda_fill_bitstream(ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001191 mutex_unlock(&ctx->bitstream_mutex);
1192 } else {
Philipp Zabel14604e32014-07-11 06:36:22 -03001193 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001194 }
Javier Martin186b2502012-07-26 05:53:35 -03001195}
1196
Philipp Zabel79924ca2014-07-23 12:28:45 -03001197int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
1198 size_t size, const char *name, struct dentry *parent)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001199{
1200 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr,
1201 GFP_KERNEL);
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001202 if (!buf->vaddr) {
1203 v4l2_err(&dev->v4l2_dev,
1204 "Failed to allocate %s buffer of size %u\n",
1205 name, size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001206 return -ENOMEM;
Philipp Zabel68fc31c2014-08-05 14:00:16 -03001207 }
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001208
1209 buf->size = size;
1210
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001211 if (name && parent) {
1212 buf->blob.data = buf->vaddr;
1213 buf->blob.size = size;
Philipp Zabelf23797b2014-08-06 08:02:23 -03001214 buf->dentry = debugfs_create_blob(name, 0644, parent,
1215 &buf->blob);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001216 if (!buf->dentry)
1217 dev_warn(&dev->plat_dev->dev,
1218 "failed to create debugfs entry %s\n", name);
1219 }
1220
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001221 return 0;
1222}
1223
Philipp Zabel79924ca2014-07-23 12:28:45 -03001224void coda_free_aux_buf(struct coda_dev *dev,
1225 struct coda_aux_buf *buf)
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001226{
1227 if (buf->vaddr) {
1228 dma_free_coherent(&dev->plat_dev->dev, buf->size,
1229 buf->vaddr, buf->paddr);
1230 buf->vaddr = NULL;
1231 buf->size = 0;
Peter Seidererd446ec82015-03-24 14:30:48 -03001232 debugfs_remove(buf->dentry);
1233 buf->dentry = NULL;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001234 }
1235}
1236
Javier Martin186b2502012-07-26 05:53:35 -03001237static int coda_start_streaming(struct vb2_queue *q, unsigned int count)
1238{
1239 struct coda_ctx *ctx = vb2_get_drv_priv(q);
1240 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev;
Javier Martin186b2502012-07-26 05:53:35 -03001241 struct coda_q_data *q_data_src, *q_data_dst;
Philipp Zabelb9063522014-08-05 14:00:10 -03001242 struct vb2_buffer *buf;
Philipp Zabeld35167a2013-05-23 10:42:59 -03001243 int ret = 0;
Javier Martin186b2502012-07-26 05:53:35 -03001244
Philipp Zabelb96904e2013-05-23 10:42:58 -03001245 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001246 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001247 if (q_data_src->fourcc == V4L2_PIX_FMT_H264 ||
1248 (q_data_src->fourcc == V4L2_PIX_FMT_JPEG &&
1249 ctx->dev->devtype->product == CODA_7541)) {
Philipp Zabel582d88722015-03-24 14:30:57 -03001250 /* copy the buffers that were queued before streamon */
Michael Olbricheabed932014-07-18 07:22:40 -03001251 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel582d88722015-03-24 14:30:57 -03001252 coda_fill_bitstream(ctx, false);
Michael Olbricheabed932014-07-18 07:22:40 -03001253 mutex_unlock(&ctx->bitstream_mutex);
1254
Philipp Zabelb9063522014-08-05 14:00:10 -03001255 if (coda_get_bitstream_payload(ctx) < 512) {
1256 ret = -EINVAL;
1257 goto err;
1258 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001259 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001260 if (count < 1) {
1261 ret = -EINVAL;
1262 goto err;
1263 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001264 }
1265
1266 ctx->streamon_out = 1;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001267 } else {
Philipp Zabelb9063522014-08-05 14:00:10 -03001268 if (count < 1) {
1269 ret = -EINVAL;
1270 goto err;
1271 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001272
1273 ctx->streamon_cap = 1;
Philipp Zabelb96904e2013-05-23 10:42:58 -03001274 }
Javier Martin186b2502012-07-26 05:53:35 -03001275
Philipp Zabelb96904e2013-05-23 10:42:58 -03001276 /* Don't start the coda unless both queues are on */
1277 if (!(ctx->streamon_out & ctx->streamon_cap))
1278 return 0;
Javier Martin186b2502012-07-26 05:53:35 -03001279
Philipp Zabel23b6ee52015-03-24 14:30:55 -03001280 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1281 if ((q_data_src->width != q_data_dst->width &&
1282 round_up(q_data_src->width, 16) != q_data_dst->width) ||
1283 (q_data_src->height != q_data_dst->height &&
1284 round_up(q_data_src->height, 16) != q_data_dst->height)) {
1285 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n",
1286 q_data_src->width, q_data_src->height,
1287 q_data_dst->width, q_data_dst->height);
1288 ret = -EINVAL;
1289 goto err;
1290 }
1291
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001292 /* Allow BIT decoder device_run with no new buffers queued */
Philipp Zabela22496c2015-01-23 13:51:33 -03001293 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabel14604e32014-07-11 06:36:22 -03001294 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001295
Philipp Zabelb96904e2013-05-23 10:42:58 -03001296 ctx->gopcounter = ctx->params.gop_size - 1;
Javier Martin186b2502012-07-26 05:53:35 -03001297
Philipp Zabelb96904e2013-05-23 10:42:58 -03001298 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc,
1299 q_data_dst->fourcc);
1300 if (!ctx->codec) {
Javier Martin186b2502012-07-26 05:53:35 -03001301 v4l2_err(v4l2_dev, "couldn't tell instance type.\n");
Philipp Zabelb9063522014-08-05 14:00:10 -03001302 ret = -EINVAL;
1303 goto err;
Javier Martin186b2502012-07-26 05:53:35 -03001304 }
1305
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001306 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG)
1307 ctx->params.gop_size = 1;
1308 ctx->gopcounter = ctx->params.gop_size - 1;
1309
Philipp Zabela1192a12014-07-23 12:28:40 -03001310 ret = ctx->ops->start_streaming(ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001311 if (ctx->inst_type == CODA_INST_DECODER) {
Philipp Zabel89548442014-07-11 06:36:17 -03001312 if (ret == -EAGAIN)
Philipp Zabel918c66f2013-06-27 06:59:01 -03001313 return 0;
Philipp Zabel89548442014-07-11 06:36:17 -03001314 else if (ret < 0)
Philipp Zabelb9063522014-08-05 14:00:10 -03001315 goto err;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001316 }
1317
Philipp Zabel89548442014-07-11 06:36:17 -03001318 ctx->initialized = 1;
1319 return ret;
Philipp Zabelb9063522014-08-05 14:00:10 -03001320
1321err:
1322 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1323 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001324 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001325 } else {
1326 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
Philipp Zabelb8b1b58c52014-10-21 13:25:52 -03001327 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
Philipp Zabelb9063522014-08-05 14:00:10 -03001328 }
1329 return ret;
Philipp Zabel89548442014-07-11 06:36:17 -03001330}
1331
Hans Verkuile37559b2014-04-17 02:47:21 -03001332static void coda_stop_streaming(struct vb2_queue *q)
Javier Martin186b2502012-07-26 05:53:35 -03001333{
1334 struct coda_ctx *ctx = vb2_get_drv_priv(q);
Philipp Zabel2fb57f02012-07-25 09:22:07 -03001335 struct coda_dev *dev = ctx->dev;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001336 struct vb2_buffer *buf;
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001337 bool stop;
1338
1339 stop = ctx->streamon_out && ctx->streamon_cap;
Javier Martin186b2502012-07-26 05:53:35 -03001340
1341 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001342 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001343 "%s: output\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001344 ctx->streamon_out = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001345
Philipp Zabel347bb7f2014-07-23 12:28:42 -03001346 coda_bit_stream_end_flag(ctx);
Philipp Zabel4a31b522014-08-05 14:00:11 -03001347
Philipp Zabel6dd5ef52015-01-23 13:51:26 -03001348 ctx->qsequence = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001349
1350 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1351 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001352 } else {
Philipp Zabel918c66f2013-06-27 06:59:01 -03001353 v4l2_dbg(1, coda_debug, &dev->v4l2_dev,
Javier Martin186b2502012-07-26 05:53:35 -03001354 "%s: capture\n", __func__);
Philipp Zabelb96904e2013-05-23 10:42:58 -03001355 ctx->streamon_cap = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001356
1357 ctx->osequence = 0;
Philipp Zabelcb2c0282014-07-11 06:36:33 -03001358 ctx->sequence_offset = 0;
Philipp Zabel4a31b522014-08-05 14:00:11 -03001359
1360 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1361 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR);
Javier Martin186b2502012-07-26 05:53:35 -03001362 }
1363
Philipp Zabel5c76c2c2015-03-24 14:30:56 -03001364 if (stop) {
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001365 struct coda_buffer_meta *meta;
Philipp Zabel846ced92014-07-11 06:36:31 -03001366
Philipp Zabelf4706d62015-01-23 13:51:27 -03001367 if (ctx->ops->seq_end_work) {
1368 queue_work(dev->workqueue, &ctx->seq_end_work);
1369 flush_work(&ctx->seq_end_work);
1370 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001371 mutex_lock(&ctx->bitstream_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001372 while (!list_empty(&ctx->buffer_meta_list)) {
1373 meta = list_first_entry(&ctx->buffer_meta_list,
1374 struct coda_buffer_meta, list);
1375 list_del(&meta->list);
1376 kfree(meta);
Philipp Zabel846ced92014-07-11 06:36:31 -03001377 }
Philipp Zabel18fd0cc2014-08-05 14:00:17 -03001378 mutex_unlock(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001379 kfifo_init(&ctx->bitstream_fifo,
1380 ctx->bitstream.vaddr, ctx->bitstream.size);
Philipp Zabelf4706d62015-01-23 13:51:27 -03001381 ctx->initialized = 0;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001382 ctx->runcounter = 0;
Philipp Zabelf157cf42014-09-29 09:53:42 -03001383 ctx->aborting = 0;
Philipp Zabel62bed142012-07-25 10:40:39 -03001384 }
Javier Martin186b2502012-07-26 05:53:35 -03001385}
1386
Philipp Zabel121cacf2014-07-18 07:22:42 -03001387static const struct vb2_ops coda_qops = {
Javier Martin186b2502012-07-26 05:53:35 -03001388 .queue_setup = coda_queue_setup,
1389 .buf_prepare = coda_buf_prepare,
1390 .buf_queue = coda_buf_queue,
Javier Martin186b2502012-07-26 05:53:35 -03001391 .start_streaming = coda_start_streaming,
1392 .stop_streaming = coda_stop_streaming,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001393 .wait_prepare = vb2_ops_wait_prepare,
1394 .wait_finish = vb2_ops_wait_finish,
Javier Martin186b2502012-07-26 05:53:35 -03001395};
1396
1397static int coda_s_ctrl(struct v4l2_ctrl *ctrl)
1398{
1399 struct coda_ctx *ctx =
1400 container_of(ctrl->handler, struct coda_ctx, ctrls);
1401
1402 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1403 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val);
1404
1405 switch (ctrl->id) {
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001406 case V4L2_CID_HFLIP:
1407 if (ctrl->val)
1408 ctx->params.rot_mode |= CODA_MIR_HOR;
1409 else
1410 ctx->params.rot_mode &= ~CODA_MIR_HOR;
1411 break;
1412 case V4L2_CID_VFLIP:
1413 if (ctrl->val)
1414 ctx->params.rot_mode |= CODA_MIR_VER;
1415 else
1416 ctx->params.rot_mode &= ~CODA_MIR_VER;
1417 break;
Javier Martin186b2502012-07-26 05:53:35 -03001418 case V4L2_CID_MPEG_VIDEO_BITRATE:
1419 ctx->params.bitrate = ctrl->val / 1000;
1420 break;
1421 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1422 ctx->params.gop_size = ctrl->val;
1423 break;
1424 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
1425 ctx->params.h264_intra_qp = ctrl->val;
1426 break;
1427 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
1428 ctx->params.h264_inter_qp = ctrl->val;
1429 break;
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001430 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
1431 ctx->params.h264_min_qp = ctrl->val;
1432 break;
1433 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
1434 ctx->params.h264_max_qp = ctrl->val;
1435 break;
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001436 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
1437 ctx->params.h264_deblk_alpha = ctrl->val;
1438 break;
1439 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
1440 ctx->params.h264_deblk_beta = ctrl->val;
1441 break;
1442 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
1443 ctx->params.h264_deblk_enabled = (ctrl->val ==
1444 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1445 break;
Javier Martin186b2502012-07-26 05:53:35 -03001446 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP:
1447 ctx->params.mpeg4_intra_qp = ctrl->val;
1448 break;
1449 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP:
1450 ctx->params.mpeg4_inter_qp = ctrl->val;
1451 break;
1452 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
1453 ctx->params.slice_mode = ctrl->val;
1454 break;
1455 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
1456 ctx->params.slice_max_mb = ctrl->val;
1457 break;
Philipp Zabelc566c782012-08-08 11:59:38 -03001458 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
1459 ctx->params.slice_max_bits = ctrl->val * 8;
1460 break;
Javier Martin186b2502012-07-26 05:53:35 -03001461 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
1462 break;
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001463 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
1464 ctx->params.intra_refresh = ctrl->val;
1465 break;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001466 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1467 coda_set_jpeg_compression_quality(ctx, ctrl->val);
1468 break;
1469 case V4L2_CID_JPEG_RESTART_INTERVAL:
1470 ctx->params.jpeg_restart_interval = ctrl->val;
1471 break;
Javier Martin186b2502012-07-26 05:53:35 -03001472 default:
1473 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev,
1474 "Invalid control, id=%d, val=%d\n",
1475 ctrl->id, ctrl->val);
1476 return -EINVAL;
1477 }
1478
1479 return 0;
1480}
1481
Philipp Zabel814c3762014-07-18 07:22:45 -03001482static const struct v4l2_ctrl_ops coda_ctrl_ops = {
Javier Martin186b2502012-07-26 05:53:35 -03001483 .s_ctrl = coda_s_ctrl,
1484};
1485
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001486static void coda_encode_ctrls(struct coda_ctx *ctx)
Javier Martin186b2502012-07-26 05:53:35 -03001487{
Philipp Zabel8f35c7b2012-07-09 04:25:52 -03001488 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel92b042e2015-03-18 08:15:36 -03001489 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0);
Javier Martin186b2502012-07-26 05:53:35 -03001490 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1491 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 60, 1, 16);
1492 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001493 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25);
Javier Martin186b2502012-07-26 05:53:35 -03001494 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabel594a7502014-07-11 06:36:14 -03001495 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25);
Philipp Zabel1a5567e2014-07-11 06:36:26 -03001496 if (ctx->dev->devtype->product != CODA_960) {
1497 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1498 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12);
1499 }
1500 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1501 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51);
Javier Martin186b2502012-07-26 05:53:35 -03001502 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelde23b1d2014-07-11 06:36:27 -03001503 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0);
1504 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1505 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0);
1506 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1507 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
1508 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0,
1509 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED);
1510 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Javier Martin186b2502012-07-26 05:53:35 -03001511 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2);
1512 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1513 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2);
1514 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1515 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
Philipp Zabelc566c782012-08-08 11:59:38 -03001516 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0,
1517 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
Javier Martin186b2502012-07-26 05:53:35 -03001518 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1519 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1);
Philipp Zabelc566c782012-08-08 11:59:38 -03001520 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001521 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1,
1522 500);
Javier Martin186b2502012-07-26 05:53:35 -03001523 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops,
1524 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1525 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1526 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE),
1527 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
Philipp Zabelf38f79d2014-07-11 06:36:28 -03001528 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
Philipp Zabelf23797b2014-08-06 08:02:23 -03001529 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0,
1530 1920 * 1088 / 256, 1, 0);
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001531}
1532
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001533static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx)
1534{
1535 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1536 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50);
1537 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1538 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0);
1539}
1540
Philipp Zabelbfc732f2014-10-02 14:08:29 -03001541static int coda_ctrls_setup(struct coda_ctx *ctx)
1542{
1543 v4l2_ctrl_handler_init(&ctx->ctrls, 2);
1544
1545 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1546 V4L2_CID_HFLIP, 0, 1, 1, 0);
1547 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops,
1548 V4L2_CID_VFLIP, 0, 1, 1, 0);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -03001549 if (ctx->inst_type == CODA_INST_ENCODER) {
1550 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG)
1551 coda_jpeg_encode_ctrls(ctx);
1552 else
1553 coda_encode_ctrls(ctx);
1554 }
Javier Martin186b2502012-07-26 05:53:35 -03001555
1556 if (ctx->ctrls.error) {
Philipp Zabelf23797b2014-08-06 08:02:23 -03001557 v4l2_err(&ctx->dev->v4l2_dev,
1558 "control initialization error (%d)",
Javier Martin186b2502012-07-26 05:53:35 -03001559 ctx->ctrls.error);
1560 return -EINVAL;
1561 }
1562
1563 return v4l2_ctrl_handler_setup(&ctx->ctrls);
1564}
1565
Philipp Zabel121cacf2014-07-18 07:22:42 -03001566static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq)
Javier Martin186b2502012-07-26 05:53:35 -03001567{
Philipp Zabel121cacf2014-07-18 07:22:42 -03001568 vq->drv_priv = ctx;
1569 vq->ops = &coda_qops;
1570 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1571 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1572 vq->lock = &ctx->dev->dev_mutex;
Kamil Debskie4af23d2015-02-23 09:26:18 -03001573 /* One way to indicate end-of-stream for coda is to set the
1574 * bytesused == 0. However by default videobuf2 handles bytesused
1575 * equal to 0 as a special case and changes its value to the size
1576 * of the buffer. Set the allow_zero_bytesused flag, so
1577 * that videobuf2 will keep the value of bytesused intact.
1578 */
1579 vq->allow_zero_bytesused = 1;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001580
1581 return vb2_queue_init(vq);
1582}
1583
Philipp Zabel79924ca2014-07-23 12:28:45 -03001584int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
1585 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001586{
Javier Martin186b2502012-07-26 05:53:35 -03001587 int ret;
1588
Javier Martin186b2502012-07-26 05:53:35 -03001589 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001590 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001591 src_vq->mem_ops = &vb2_dma_contig_memops;
1592
Philipp Zabel121cacf2014-07-18 07:22:42 -03001593 ret = coda_queue_init(priv, src_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001594 if (ret)
1595 return ret;
1596
Javier Martin186b2502012-07-26 05:53:35 -03001597 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Philipp Zabeld29a8cf2014-07-18 07:22:38 -03001598 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
Javier Martin186b2502012-07-26 05:53:35 -03001599 dst_vq->mem_ops = &vb2_dma_contig_memops;
1600
Philipp Zabel121cacf2014-07-18 07:22:42 -03001601 return coda_queue_init(priv, dst_vq);
1602}
1603
Philipp Zabel79924ca2014-07-23 12:28:45 -03001604int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
1605 struct vb2_queue *dst_vq)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001606{
1607 int ret;
1608
1609 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Philipp Zabelbb04aa62015-01-23 13:51:30 -03001610 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1611 src_vq->mem_ops = &vb2_vmalloc_memops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001612
1613 ret = coda_queue_init(priv, src_vq);
1614 if (ret)
1615 return ret;
1616
1617 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1618 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
1619 dst_vq->mem_ops = &vb2_dma_contig_memops;
1620
1621 return coda_queue_init(priv, dst_vq);
Javier Martin186b2502012-07-26 05:53:35 -03001622}
1623
Philipp Zabele11f3e62012-07-25 09:16:58 -03001624static int coda_next_free_instance(struct coda_dev *dev)
1625{
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001626 int idx = ffz(dev->instance_mask);
1627
1628 if ((idx < 0) ||
1629 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES))
1630 return -EBUSY;
1631
1632 return idx;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001633}
1634
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001635/*
1636 * File operations
1637 */
1638
1639static int coda_open(struct file *file)
Javier Martin186b2502012-07-26 05:53:35 -03001640{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001641 struct video_device *vdev = video_devdata(file);
1642 struct coda_dev *dev = video_get_drvdata(vdev);
Javier Martin186b2502012-07-26 05:53:35 -03001643 struct coda_ctx *ctx = NULL;
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001644 char *name;
Philipp Zabel918c66f2013-06-27 06:59:01 -03001645 int ret;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001646 int idx;
Javier Martin186b2502012-07-26 05:53:35 -03001647
Philipp Zabelf23797b2014-08-06 08:02:23 -03001648 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Javier Martin186b2502012-07-26 05:53:35 -03001649 if (!ctx)
1650 return -ENOMEM;
1651
Fabio Estevamf82bc202013-08-21 11:14:16 -03001652 idx = coda_next_free_instance(dev);
Philipp Zabel0cddc7e2013-09-30 10:34:44 -03001653 if (idx < 0) {
1654 ret = idx;
Fabio Estevamf82bc202013-08-21 11:14:16 -03001655 goto err_coda_max;
1656 }
1657 set_bit(idx, &dev->instance_mask);
1658
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001659 name = kasprintf(GFP_KERNEL, "context%d", idx);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001660 if (!name) {
1661 ret = -ENOMEM;
1662 goto err_coda_name_init;
1663 }
1664
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001665 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root);
1666 kfree(name);
1667
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001668 ctx->cvd = to_coda_video_device(vdev);
1669 ctx->inst_type = ctx->cvd->type;
1670 ctx->ops = ctx->cvd->ops;
Philipp Zabela22496c2015-01-23 13:51:33 -03001671 ctx->use_bit = !ctx->cvd->direct;
Philipp Zabel32b6f202014-07-11 06:36:20 -03001672 init_completion(&ctx->completion);
1673 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work);
Philipp Zabel747d7642015-01-23 13:51:31 -03001674 if (ctx->ops->seq_end_work)
1675 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work);
Javier Martin186b2502012-07-26 05:53:35 -03001676 v4l2_fh_init(&ctx->fh, video_devdata(file));
1677 file->private_data = &ctx->fh;
1678 v4l2_fh_add(&ctx->fh);
1679 ctx->dev = dev;
Philipp Zabele11f3e62012-07-25 09:16:58 -03001680 ctx->idx = idx;
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001681 switch (dev->devtype->product) {
Philipp Zabel89548442014-07-11 06:36:17 -03001682 case CODA_960:
Philipp Zabel2bf299c2014-09-29 09:53:46 -03001683 ctx->frame_mem_ctrl = 1 << 12;
1684 /* fallthrough */
1685 case CODA_7541:
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001686 ctx->reg_idx = 0;
1687 break;
1688 default:
1689 ctx->reg_idx = idx;
1690 }
Fabio Estevamf82bc202013-08-21 11:14:16 -03001691
Philipp Zabel1e172732014-07-11 06:36:23 -03001692 /* Power up and upload firmware if necessary */
1693 ret = pm_runtime_get_sync(&dev->plat_dev->dev);
1694 if (ret < 0) {
1695 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret);
1696 goto err_pm_get;
1697 }
1698
Fabio Estevam79830db2013-08-21 11:14:17 -03001699 ret = clk_prepare_enable(dev->clk_per);
1700 if (ret)
1701 goto err_clk_per;
1702
1703 ret = clk_prepare_enable(dev->clk_ahb);
1704 if (ret)
1705 goto err_clk_ahb;
1706
Javier Martin186b2502012-07-26 05:53:35 -03001707 set_default_params(ctx);
Philipp Zabela1192a12014-07-23 12:28:40 -03001708 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx,
1709 ctx->ops->queue_init);
Philipp Zabel14604e32014-07-11 06:36:22 -03001710 if (IS_ERR(ctx->fh.m2m_ctx)) {
1711 ret = PTR_ERR(ctx->fh.m2m_ctx);
Javier Martin186b2502012-07-26 05:53:35 -03001712
1713 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n",
1714 __func__, ret);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001715 goto err_ctx_init;
Javier Martin186b2502012-07-26 05:53:35 -03001716 }
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001717
Javier Martin186b2502012-07-26 05:53:35 -03001718 ret = coda_ctrls_setup(ctx);
1719 if (ret) {
1720 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n");
Fabio Estevamf82bc202013-08-21 11:14:16 -03001721 goto err_ctrls_setup;
Javier Martin186b2502012-07-26 05:53:35 -03001722 }
1723
1724 ctx->fh.ctrl_handler = &ctx->ctrls;
1725
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001726 mutex_init(&ctx->bitstream_mutex);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001727 mutex_init(&ctx->buffer_mutex);
Philipp Zabel7cbb1052014-10-02 14:08:32 -03001728 INIT_LIST_HEAD(&ctx->buffer_meta_list);
Philipp Zabel9082a7c2013-06-21 03:55:31 -03001729
Javier Martin186b2502012-07-26 05:53:35 -03001730 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001731 list_add(&ctx->list, &dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03001732 coda_unlock(ctx);
1733
Javier Martin186b2502012-07-26 05:53:35 -03001734 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n",
1735 ctx->idx, ctx);
1736
1737 return 0;
1738
Fabio Estevamf82bc202013-08-21 11:14:16 -03001739err_ctrls_setup:
Philipp Zabel14604e32014-07-11 06:36:22 -03001740 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001741err_ctx_init:
1742 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevam79830db2013-08-21 11:14:17 -03001743err_clk_ahb:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001744 clk_disable_unprepare(dev->clk_per);
Fabio Estevam79830db2013-08-21 11:14:17 -03001745err_clk_per:
Philipp Zabel1e172732014-07-11 06:36:23 -03001746 pm_runtime_put_sync(&dev->plat_dev->dev);
1747err_pm_get:
Javier Martin186b2502012-07-26 05:53:35 -03001748 v4l2_fh_del(&ctx->fh);
1749 v4l2_fh_exit(&ctx->fh);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001750 clear_bit(ctx->idx, &dev->instance_mask);
Peter Seiderera7f933a2015-03-24 14:30:47 -03001751err_coda_name_init:
Fabio Estevamf82bc202013-08-21 11:14:16 -03001752err_coda_max:
Javier Martin186b2502012-07-26 05:53:35 -03001753 kfree(ctx);
1754 return ret;
1755}
1756
1757static int coda_release(struct file *file)
1758{
1759 struct coda_dev *dev = video_drvdata(file);
1760 struct coda_ctx *ctx = fh_to_ctx(file->private_data);
1761
1762 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n",
1763 ctx);
1764
Philipp Zabela22496c2015-01-23 13:51:33 -03001765 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit)
Philipp Zabeld4bb75f2014-10-08 13:09:11 -03001766 coda_bit_stream_end_flag(ctx);
1767
Philipp Zabel918c66f2013-06-27 06:59:01 -03001768 /* If this instance is running, call .job_abort and wait for it to end */
Philipp Zabel14604e32014-07-11 06:36:22 -03001769 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001770
1771 /* In case the instance was not running, we still need to call SEQ_END */
Philipp Zabel747d7642015-01-23 13:51:31 -03001772 if (ctx->initialized && ctx->ops->seq_end_work) {
Philipp Zabel32b6f202014-07-11 06:36:20 -03001773 queue_work(dev->workqueue, &ctx->seq_end_work);
1774 flush_work(&ctx->seq_end_work);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001775 }
Philipp Zabel918c66f2013-06-27 06:59:01 -03001776
Javier Martin186b2502012-07-26 05:53:35 -03001777 coda_lock(ctx);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001778 list_del(&ctx->list);
Javier Martin186b2502012-07-26 05:53:35 -03001779 coda_unlock(ctx);
1780
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001781 if (ctx->dev->devtype->product == CODA_DX6)
1782 coda_free_aux_buf(dev, &ctx->workbuf);
1783
Javier Martin186b2502012-07-26 05:53:35 -03001784 v4l2_ctrl_handler_free(&ctx->ctrls);
Javier Martin186b2502012-07-26 05:53:35 -03001785 clk_disable_unprepare(dev->clk_ahb);
Fabio Estevamf82bc202013-08-21 11:14:16 -03001786 clk_disable_unprepare(dev->clk_per);
Philipp Zabel1e172732014-07-11 06:36:23 -03001787 pm_runtime_put_sync(&dev->plat_dev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001788 v4l2_fh_del(&ctx->fh);
1789 v4l2_fh_exit(&ctx->fh);
Philipp Zabele11f3e62012-07-25 09:16:58 -03001790 clear_bit(ctx->idx, &dev->instance_mask);
Philipp Zabel58b76772014-07-23 12:28:43 -03001791 if (ctx->ops->release)
1792 ctx->ops->release(ctx);
Philipp Zabele1519e82015-01-23 13:51:17 -03001793 debugfs_remove_recursive(ctx->debugfs_entry);
Javier Martin186b2502012-07-26 05:53:35 -03001794 kfree(ctx);
1795
1796 return 0;
1797}
1798
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001799static const struct v4l2_file_operations coda_fops = {
Javier Martin186b2502012-07-26 05:53:35 -03001800 .owner = THIS_MODULE,
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001801 .open = coda_open,
Javier Martin186b2502012-07-26 05:53:35 -03001802 .release = coda_release,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001803 .poll = v4l2_m2m_fop_poll,
Javier Martin186b2502012-07-26 05:53:35 -03001804 .unlocked_ioctl = video_ioctl2,
Philipp Zabel152ea1c2014-07-11 06:36:21 -03001805 .mmap = v4l2_m2m_fop_mmap,
Javier Martin186b2502012-07-26 05:53:35 -03001806};
1807
Philipp Zabel87048bb2012-07-02 07:03:43 -03001808static int coda_hw_init(struct coda_dev *dev)
Javier Martin186b2502012-07-26 05:53:35 -03001809{
Javier Martin186b2502012-07-26 05:53:35 -03001810 u32 data;
1811 u16 *p;
Fabio Estevam79830db2013-08-21 11:14:17 -03001812 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001813
Fabio Estevam79830db2013-08-21 11:14:17 -03001814 ret = clk_prepare_enable(dev->clk_per);
1815 if (ret)
Philipp Zabel1e172732014-07-11 06:36:23 -03001816 goto err_clk_per;
Fabio Estevam79830db2013-08-21 11:14:17 -03001817
1818 ret = clk_prepare_enable(dev->clk_ahb);
1819 if (ret)
1820 goto err_clk_ahb;
Javier Martin186b2502012-07-26 05:53:35 -03001821
Philipp Zabel8f452842014-07-11 06:36:35 -03001822 if (dev->rstc)
1823 reset_control_reset(dev->rstc);
1824
Javier Martin186b2502012-07-26 05:53:35 -03001825 /*
1826 * Copy the first CODA_ISRAM_SIZE in the internal SRAM.
Philipp Zabel87048bb2012-07-02 07:03:43 -03001827 * The 16-bit chars in the code buffer are in memory access
1828 * order, re-sort them to CODA order for register download.
Javier Martin186b2502012-07-26 05:53:35 -03001829 * Data in this SRAM survives a reboot.
1830 */
Philipp Zabel87048bb2012-07-02 07:03:43 -03001831 p = (u16 *)dev->codebuf.vaddr;
1832 if (dev->devtype->product == CODA_DX6) {
1833 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1834 data = CODA_DOWN_ADDRESS_SET(i) |
1835 CODA_DOWN_DATA_SET(p[i ^ 1]);
1836 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1837 }
1838 } else {
1839 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) {
1840 data = CODA_DOWN_ADDRESS_SET(i) |
1841 CODA_DOWN_DATA_SET(p[round_down(i, 4) +
1842 3 - (i % 4)]);
1843 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN);
1844 }
Javier Martin186b2502012-07-26 05:53:35 -03001845 }
Javier Martin186b2502012-07-26 05:53:35 -03001846
Philipp Zabel9acf7692013-05-23 10:42:56 -03001847 /* Clear registers */
1848 for (i = 0; i < 64; i++)
1849 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4);
1850
Javier Martin186b2502012-07-26 05:53:35 -03001851 /* Tell the BIT where to find everything it needs */
Philipp Zabel89548442014-07-11 06:36:17 -03001852 if (dev->devtype->product == CODA_960 ||
1853 dev->devtype->product == CODA_7541) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001854 coda_write(dev, dev->tempbuf.paddr,
1855 CODA_REG_BIT_TEMP_BUF_ADDR);
Philipp Zabel918c66f2013-06-27 06:59:01 -03001856 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03001857 } else {
1858 coda_write(dev, dev->workbuf.paddr,
1859 CODA_REG_BIT_WORK_BUF_ADDR);
1860 }
Javier Martin186b2502012-07-26 05:53:35 -03001861 coda_write(dev, dev->codebuf.paddr,
1862 CODA_REG_BIT_CODE_BUF_ADDR);
1863 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN);
1864
1865 /* Set default values */
1866 switch (dev->devtype->product) {
1867 case CODA_DX6:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001868 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH,
1869 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001870 break;
1871 default:
Philipp Zabelf23797b2014-08-06 08:02:23 -03001872 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH,
1873 CODA_REG_BIT_STREAM_CTRL);
Javier Martin186b2502012-07-26 05:53:35 -03001874 }
Philipp Zabel89548442014-07-11 06:36:17 -03001875 if (dev->devtype->product == CODA_960)
1876 coda_write(dev, 1 << 12, CODA_REG_BIT_FRAME_MEM_CTRL);
1877 else
1878 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL);
Philipp Zabel10436672012-07-02 09:03:55 -03001879
1880 if (dev->devtype->product != CODA_DX6)
1881 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE);
1882
Javier Martin186b2502012-07-26 05:53:35 -03001883 coda_write(dev, CODA_INT_INTERRUPT_ENABLE,
1884 CODA_REG_BIT_INT_ENABLE);
1885
1886 /* Reset VPU and start processor */
1887 data = coda_read(dev, CODA_REG_BIT_CODE_RESET);
1888 data |= CODA_REG_RESET_ENABLE;
1889 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1890 udelay(10);
1891 data &= ~CODA_REG_RESET_ENABLE;
1892 coda_write(dev, data, CODA_REG_BIT_CODE_RESET);
1893 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN);
1894
Philipp Zabelfe7554e2014-07-11 06:36:24 -03001895 clk_disable_unprepare(dev->clk_ahb);
1896 clk_disable_unprepare(dev->clk_per);
1897
1898 return 0;
1899
1900err_clk_ahb:
1901 clk_disable_unprepare(dev->clk_per);
1902err_clk_per:
1903 return ret;
1904}
1905
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001906static int coda_register_device(struct coda_dev *dev, int i)
Philipp Zabel121cacf2014-07-18 07:22:42 -03001907{
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001908 struct video_device *vfd = &dev->vfd[i];
1909
Dan Carpenter88ca3af2015-01-08 07:07:08 -03001910 if (i >= dev->devtype->num_vdevs)
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001911 return -EINVAL;
1912
Philipp Zabelbf249da2015-03-24 14:30:50 -03001913 strlcpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name));
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001914 vfd->fops = &coda_fops;
1915 vfd->ioctl_ops = &coda_ioctl_ops;
Philipp Zabel121cacf2014-07-18 07:22:42 -03001916 vfd->release = video_device_release_empty,
1917 vfd->lock = &dev->dev_mutex;
1918 vfd->v4l2_dev = &dev->v4l2_dev;
1919 vfd->vfl_dir = VFL_DIR_M2M;
1920 video_set_drvdata(vfd, dev);
1921
Philipp Zabela188a662014-08-05 14:00:20 -03001922 /* Not applicable, use the selection API instead */
1923 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP);
1924 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP);
1925 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP);
1926
Philipp Zabel121cacf2014-07-18 07:22:42 -03001927 return video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1928}
1929
Javier Martin186b2502012-07-26 05:53:35 -03001930static void coda_fw_callback(const struct firmware *fw, void *context)
1931{
1932 struct coda_dev *dev = context;
1933 struct platform_device *pdev = dev->plat_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001934 int i, ret;
Javier Martin186b2502012-07-26 05:53:35 -03001935
1936 if (!fw) {
1937 v4l2_err(&dev->v4l2_dev, "firmware request failed\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001938 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001939 }
1940
1941 /* allocate auxiliary per-device code buffer for the BIT processor */
Philipp Zabelf61c0b12014-07-11 06:36:40 -03001942 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf",
1943 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03001944 if (ret < 0)
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001945 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001946
Philipp Zabel87048bb2012-07-02 07:03:43 -03001947 /* Copy the whole firmware image to the code buffer */
1948 memcpy(dev->codebuf.vaddr, fw->data, fw->size);
1949 release_firmware(fw);
1950
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001951 ret = coda_hw_init(dev);
1952 if (ret < 0) {
1953 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n");
1954 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001955 }
1956
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001957 ret = coda_check_firmware(dev);
1958 if (ret < 0)
1959 goto put_pm;
1960
Javier Martin186b2502012-07-26 05:53:35 -03001961 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1962 if (IS_ERR(dev->alloc_ctx)) {
1963 v4l2_err(&dev->v4l2_dev, "Failed to alloc vb2 context\n");
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001964 goto put_pm;
Javier Martin186b2502012-07-26 05:53:35 -03001965 }
1966
1967 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops);
1968 if (IS_ERR(dev->m2m_dev)) {
1969 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1970 goto rel_ctx;
1971 }
1972
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001973 for (i = 0; i < dev->devtype->num_vdevs; i++) {
1974 ret = coda_register_device(dev, i);
1975 if (ret) {
1976 v4l2_err(&dev->v4l2_dev,
1977 "Failed to register %s video device: %d\n",
1978 dev->devtype->vdevs[i]->name, ret);
1979 goto rel_vfd;
1980 }
Philipp Zabel121cacf2014-07-18 07:22:42 -03001981 }
1982
1983 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n",
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001984 dev->vfd[0].num, dev->vfd[i - 1].num);
Javier Martin186b2502012-07-26 05:53:35 -03001985
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001986 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001987 return;
1988
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03001989rel_vfd:
1990 while (--i >= 0)
1991 video_unregister_device(&dev->vfd[i]);
Javier Martin186b2502012-07-26 05:53:35 -03001992 v4l2_m2m_release(dev->m2m_dev);
1993rel_ctx:
1994 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03001995put_pm:
1996 pm_runtime_put_sync(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03001997}
1998
1999static int coda_firmware_request(struct coda_dev *dev)
2000{
2001 char *fw = dev->devtype->firmware;
2002
2003 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw,
2004 coda_product_name(dev->devtype->product));
2005
2006 return request_firmware_nowait(THIS_MODULE, true,
2007 fw, &dev->plat_dev->dev, GFP_KERNEL, dev, coda_fw_callback);
2008}
2009
2010enum coda_platform {
2011 CODA_IMX27,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002012 CODA_IMX53,
Philipp Zabel89548442014-07-11 06:36:17 -03002013 CODA_IMX6Q,
2014 CODA_IMX6DL,
Javier Martin186b2502012-07-26 05:53:35 -03002015};
2016
Emil Goodec06d8752012-08-14 17:44:42 -03002017static const struct coda_devtype coda_devdata[] = {
Javier Martin186b2502012-07-26 05:53:35 -03002018 [CODA_IMX27] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002019 .firmware = "v4l-codadx6-imx27.bin",
2020 .product = CODA_DX6,
2021 .codecs = codadx6_codecs,
2022 .num_codecs = ARRAY_SIZE(codadx6_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002023 .vdevs = codadx6_video_devices,
2024 .num_vdevs = ARRAY_SIZE(codadx6_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002025 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002026 .iram_size = 0xb000,
Javier Martin186b2502012-07-26 05:53:35 -03002027 },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002028 [CODA_IMX53] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002029 .firmware = "v4l-coda7541-imx53.bin",
2030 .product = CODA_7541,
2031 .codecs = coda7_codecs,
2032 .num_codecs = ARRAY_SIZE(coda7_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002033 .vdevs = coda7_video_devices,
2034 .num_vdevs = ARRAY_SIZE(coda7_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002035 .workbuf_size = 128 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002036 .tempbuf_size = 304 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002037 .iram_size = 0x14000,
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002038 },
Philipp Zabel89548442014-07-11 06:36:17 -03002039 [CODA_IMX6Q] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002040 .firmware = "v4l-coda960-imx6q.bin",
2041 .product = CODA_960,
2042 .codecs = coda9_codecs,
2043 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002044 .vdevs = coda9_video_devices,
2045 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002046 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002047 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002048 .iram_size = 0x21000,
Philipp Zabel89548442014-07-11 06:36:17 -03002049 },
2050 [CODA_IMX6DL] = {
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002051 .firmware = "v4l-coda960-imx6dl.bin",
2052 .product = CODA_960,
2053 .codecs = coda9_codecs,
2054 .num_codecs = ARRAY_SIZE(coda9_codecs),
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002055 .vdevs = coda9_video_devices,
2056 .num_vdevs = ARRAY_SIZE(coda9_video_devices),
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002057 .workbuf_size = 80 * 1024,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002058 .tempbuf_size = 204 * 1024,
Philipp Zabel401e9722014-07-11 06:36:43 -03002059 .iram_size = 0x20000,
Philipp Zabel89548442014-07-11 06:36:17 -03002060 },
Javier Martin186b2502012-07-26 05:53:35 -03002061};
2062
2063static struct platform_device_id coda_platform_ids[] = {
2064 { .name = "coda-imx27", .driver_data = CODA_IMX27 },
2065 { /* sentinel */ }
2066};
2067MODULE_DEVICE_TABLE(platform, coda_platform_ids);
2068
2069#ifdef CONFIG_OF
2070static const struct of_device_id coda_dt_ids[] = {
Alexander Shiyan7b0dd9e2013-06-15 08:09:57 -03002071 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] },
Philipp Zabeldf1e74c2012-07-02 06:07:10 -03002072 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] },
Philipp Zabel89548442014-07-11 06:36:17 -03002073 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] },
2074 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] },
Javier Martin186b2502012-07-26 05:53:35 -03002075 { /* sentinel */ }
2076};
2077MODULE_DEVICE_TABLE(of, coda_dt_ids);
2078#endif
2079
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002080static int coda_probe(struct platform_device *pdev)
Javier Martin186b2502012-07-26 05:53:35 -03002081{
2082 const struct of_device_id *of_id =
2083 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev);
2084 const struct platform_device_id *pdev_id;
Philipp Zabel657eee72013-04-29 16:17:14 -07002085 struct coda_platform_data *pdata = pdev->dev.platform_data;
2086 struct device_node *np = pdev->dev.of_node;
2087 struct gen_pool *pool;
Javier Martin186b2502012-07-26 05:53:35 -03002088 struct coda_dev *dev;
2089 struct resource *res;
2090 int ret, irq;
2091
Philipp Zabelf23797b2014-08-06 08:02:23 -03002092 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Philipp Zabel6da999d2014-09-29 09:53:43 -03002093 if (!dev)
Javier Martin186b2502012-07-26 05:53:35 -03002094 return -ENOMEM;
Javier Martin186b2502012-07-26 05:53:35 -03002095
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002096 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev);
2097
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002098 if (of_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002099 dev->devtype = of_id->data;
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002100 } else if (pdev_id) {
Philipp Zabelb2f91ae2014-10-02 14:08:27 -03002101 dev->devtype = &coda_devdata[pdev_id->driver_data];
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002102 } else {
2103 ret = -EINVAL;
2104 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002105 }
2106
2107 spin_lock_init(&dev->irqlock);
Philipp Zabele11f3e62012-07-25 09:16:58 -03002108 INIT_LIST_HEAD(&dev->instances);
Javier Martin186b2502012-07-26 05:53:35 -03002109
2110 dev->plat_dev = pdev;
2111 dev->clk_per = devm_clk_get(&pdev->dev, "per");
2112 if (IS_ERR(dev->clk_per)) {
2113 dev_err(&pdev->dev, "Could not get per clock\n");
2114 return PTR_ERR(dev->clk_per);
2115 }
2116
2117 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
2118 if (IS_ERR(dev->clk_ahb)) {
2119 dev_err(&pdev->dev, "Could not get ahb clock\n");
2120 return PTR_ERR(dev->clk_ahb);
2121 }
2122
2123 /* Get memory for physical registers */
2124 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Philipp Zabel611cbbf2013-05-21 04:19:27 -03002125 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
2126 if (IS_ERR(dev->regs_base))
2127 return PTR_ERR(dev->regs_base);
Javier Martin186b2502012-07-26 05:53:35 -03002128
2129 /* IRQ */
Philipp Zabel540b72e2014-08-05 14:00:09 -03002130 irq = platform_get_irq_byname(pdev, "bit");
2131 if (irq < 0)
2132 irq = platform_get_irq(pdev, 0);
Javier Martin186b2502012-07-26 05:53:35 -03002133 if (irq < 0) {
2134 dev_err(&pdev->dev, "failed to get irq resource\n");
Fabio Estevam7d377432014-06-04 15:46:23 -03002135 return irq;
Javier Martin186b2502012-07-26 05:53:35 -03002136 }
2137
Fabio Estevam08c8d142014-06-04 15:46:24 -03002138 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler,
2139 IRQF_ONESHOT, dev_name(&pdev->dev), dev);
2140 if (ret < 0) {
2141 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
2142 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002143 }
2144
Philipp Zabelee27aa92014-07-24 16:50:03 -03002145 dev->rstc = devm_reset_control_get_optional(&pdev->dev, NULL);
Philipp Zabel8f452842014-07-11 06:36:35 -03002146 if (IS_ERR(dev->rstc)) {
2147 ret = PTR_ERR(dev->rstc);
Philipp Zabelee27aa92014-07-24 16:50:03 -03002148 if (ret == -ENOENT || ret == -ENOSYS) {
Philipp Zabel8f452842014-07-11 06:36:35 -03002149 dev->rstc = NULL;
2150 } else {
Philipp Zabelf23797b2014-08-06 08:02:23 -03002151 dev_err(&pdev->dev, "failed get reset control: %d\n",
2152 ret);
Philipp Zabel8f452842014-07-11 06:36:35 -03002153 return ret;
2154 }
2155 }
2156
Philipp Zabel657eee72013-04-29 16:17:14 -07002157 /* Get IRAM pool from device tree or platform data */
Vladimir Zapolskiyabdd4a72015-06-30 15:00:07 -07002158 pool = of_gen_pool_get(np, "iram", 0);
Philipp Zabel657eee72013-04-29 16:17:14 -07002159 if (!pool && pdata)
Vladimir Zapolskiy0030edf2015-06-30 15:00:03 -07002160 pool = gen_pool_get(pdata->iram_dev);
Philipp Zabel657eee72013-04-29 16:17:14 -07002161 if (!pool) {
2162 dev_err(&pdev->dev, "iram pool not available\n");
2163 return -ENOMEM;
2164 }
2165 dev->iram_pool = pool;
2166
Javier Martin186b2502012-07-26 05:53:35 -03002167 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2168 if (ret)
2169 return ret;
2170
2171 mutex_init(&dev->dev_mutex);
Philipp Zabelfcb62822013-05-23 10:43:00 -03002172 mutex_init(&dev->coda_mutex);
Javier Martin186b2502012-07-26 05:53:35 -03002173
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002174 dev->debugfs_root = debugfs_create_dir("coda", NULL);
2175 if (!dev->debugfs_root)
2176 dev_warn(&pdev->dev, "failed to create debugfs root\n");
2177
Javier Martin186b2502012-07-26 05:53:35 -03002178 /* allocate auxiliary per-device buffers for the BIT processor */
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002179 if (dev->devtype->product == CODA_DX6) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002180 ret = coda_alloc_aux_buf(dev, &dev->workbuf,
Philipp Zabele5b0d1c2014-07-11 06:36:41 -03002181 dev->devtype->workbuf_size, "workbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002182 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002183 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002184 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002185 }
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002186
2187 if (dev->devtype->tempbuf_size) {
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002188 ret = coda_alloc_aux_buf(dev, &dev->tempbuf,
Philipp Zabel5d73fad2014-07-11 06:36:42 -03002189 dev->devtype->tempbuf_size, "tempbuf",
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002190 dev->debugfs_root);
Philipp Zabel6ba53b82015-03-24 14:30:54 -03002191 if (ret < 0)
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002192 goto err_v4l2_register;
Javier Martin186b2502012-07-26 05:53:35 -03002193 }
2194
Philipp Zabel401e9722014-07-11 06:36:43 -03002195 dev->iram.size = dev->devtype->iram_size;
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002196 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size,
2197 &dev->iram.paddr);
2198 if (!dev->iram.vaddr) {
Philipp Zabel8be31c82014-08-05 14:00:13 -03002199 dev_warn(&pdev->dev, "unable to alloc iram\n");
2200 } else {
Philipp Zabel811a6932015-01-23 13:51:23 -03002201 memset(dev->iram.vaddr, 0, dev->iram.size);
Philipp Zabel8be31c82014-08-05 14:00:13 -03002202 dev->iram.blob.data = dev->iram.vaddr;
2203 dev->iram.blob.size = dev->iram.size;
2204 dev->iram.dentry = debugfs_create_blob("iram", 0644,
2205 dev->debugfs_root,
2206 &dev->iram.blob);
Philipp Zabel10436672012-07-02 09:03:55 -03002207 }
2208
Philipp Zabel32b6f202014-07-11 06:36:20 -03002209 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
2210 if (!dev->workqueue) {
2211 dev_err(&pdev->dev, "unable to alloc workqueue\n");
Fabio Estevam74d08d52014-10-04 16:40:51 -03002212 ret = -ENOMEM;
2213 goto err_v4l2_register;
Philipp Zabel32b6f202014-07-11 06:36:20 -03002214 }
2215
Javier Martin186b2502012-07-26 05:53:35 -03002216 platform_set_drvdata(pdev, dev);
2217
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002218 /*
2219 * Start activated so we can directly call coda_hw_init in
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002220 * coda_fw_callback regardless of whether CONFIG_PM is
Ulf Hanssonc5d28e22014-09-22 13:05:56 -03002221 * enabled or whether the device is associated with a PM domain.
2222 */
2223 pm_runtime_get_noresume(&pdev->dev);
2224 pm_runtime_set_active(&pdev->dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002225 pm_runtime_enable(&pdev->dev);
2226
Javier Martin186b2502012-07-26 05:53:35 -03002227 return coda_firmware_request(dev);
Fabio Estevamb7bd6602014-10-04 16:40:50 -03002228
2229err_v4l2_register:
2230 v4l2_device_unregister(&dev->v4l2_dev);
2231 return ret;
Javier Martin186b2502012-07-26 05:53:35 -03002232}
2233
2234static int coda_remove(struct platform_device *pdev)
2235{
2236 struct coda_dev *dev = platform_get_drvdata(pdev);
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002237 int i;
Javier Martin186b2502012-07-26 05:53:35 -03002238
Philipp Zabel2c11d1b2014-10-02 14:08:28 -03002239 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) {
2240 if (video_get_drvdata(&dev->vfd[i]))
2241 video_unregister_device(&dev->vfd[i]);
2242 }
Javier Martin186b2502012-07-26 05:53:35 -03002243 if (dev->m2m_dev)
2244 v4l2_m2m_release(dev->m2m_dev);
Philipp Zabel1e172732014-07-11 06:36:23 -03002245 pm_runtime_disable(&pdev->dev);
Javier Martin186b2502012-07-26 05:53:35 -03002246 if (dev->alloc_ctx)
2247 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2248 v4l2_device_unregister(&dev->v4l2_dev);
Philipp Zabel32b6f202014-07-11 06:36:20 -03002249 destroy_workqueue(dev->workqueue);
Philipp Zabelb313bcc2014-07-11 06:36:16 -03002250 if (dev->iram.vaddr)
2251 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr,
2252 dev->iram.size);
Philipp Zabel5677e3b2013-06-21 03:55:30 -03002253 coda_free_aux_buf(dev, &dev->codebuf);
2254 coda_free_aux_buf(dev, &dev->tempbuf);
2255 coda_free_aux_buf(dev, &dev->workbuf);
Philipp Zabelf61c0b12014-07-11 06:36:40 -03002256 debugfs_remove_recursive(dev->debugfs_root);
Javier Martin186b2502012-07-26 05:53:35 -03002257 return 0;
2258}
2259
Rafael J. Wysockie243c7c2014-12-04 01:10:10 +01002260#ifdef CONFIG_PM
Philipp Zabel1e172732014-07-11 06:36:23 -03002261static int coda_runtime_resume(struct device *dev)
2262{
2263 struct coda_dev *cdev = dev_get_drvdata(dev);
2264 int ret = 0;
2265
Philipp Zabel65919e62014-07-18 07:22:36 -03002266 if (dev->pm_domain && cdev->codebuf.vaddr) {
Philipp Zabel1e172732014-07-11 06:36:23 -03002267 ret = coda_hw_init(cdev);
2268 if (ret)
2269 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n");
2270 }
2271
2272 return ret;
2273}
2274#endif
2275
2276static const struct dev_pm_ops coda_pm_ops = {
2277 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL)
2278};
2279
Javier Martin186b2502012-07-26 05:53:35 -03002280static struct platform_driver coda_driver = {
2281 .probe = coda_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002282 .remove = coda_remove,
Javier Martin186b2502012-07-26 05:53:35 -03002283 .driver = {
2284 .name = CODA_NAME,
Javier Martin186b2502012-07-26 05:53:35 -03002285 .of_match_table = of_match_ptr(coda_dt_ids),
Philipp Zabel1e172732014-07-11 06:36:23 -03002286 .pm = &coda_pm_ops,
Javier Martin186b2502012-07-26 05:53:35 -03002287 },
2288 .id_table = coda_platform_ids,
2289};
2290
2291module_platform_driver(coda_driver);
2292
2293MODULE_LICENSE("GPL");
2294MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
2295MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");